-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle14.min.js
More file actions
122 lines (107 loc) · 3.6 KB
/
bundle14.min.js
File metadata and controls
122 lines (107 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(() => {
class ChatClient {
constructor(hostUrl, apiKey, flowId) {
this.hostUrl = hostUrl;
this.apiKey = apiKey;
this.flowId = flowId;
this.callbacks = {
onMessage: null,
onStatusChange: null
};
console.log('[ChatClient] Initialized with:', { hostUrl, flowId });
}
setCallbacks(callbacks) {
this.callbacks = { ...this.callbacks, ...callbacks };
}
async connect() {
console.log('[ChatClient] Connecting...');
try {
await this.testConnection();
if (this.callbacks.onStatusChange) {
this.callbacks.onStatusChange('CONNECTED');
}
return true;
} catch (error) {
console.error('[ChatClient] Connection failed:', error);
if (this.callbacks.onStatusChange) {
this.callbacks.onStatusChange('DISCONNECTED');
}
return false;
}
}
async testConnection() {
console.log('[ChatClient] Testing connection...');
const headers = this.getHeaders();
try {
const response = await fetch(`${this.hostUrl}/health`, {
method: 'GET',
headers: headers,
credentials: 'include'
});
if (!response.ok) {
console.error('[ChatClient] Health check failed:', {
status: response.status,
statusText: response.statusText
});
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error('[ChatClient] Health check error:', error);
throw error;
}
}
getHeaders() {
const headers = new Headers();
headers.append('Content-Type', 'application/json');
// Add API key in both formats to ensure compatibility
headers.append('x-api-key', this.apiKey);
headers.append('api-key', this.apiKey);
headers.append('Authorization', `Bearer ${this.apiKey}`);
console.log('[ChatClient] Headers set:', headers);
return headers;
}
async sendMessage(message) {
console.log('[ChatClient] Sending message:', message);
const headers = this.getHeaders();
try {
console.log('[ChatClient] Making request to:', `${this.hostUrl}/api/v1/process/${this.flowId}`);
const response = await fetch(`${this.hostUrl}/api/v1/process/${this.flowId}`, {
method: 'POST',
headers: headers,
credentials: 'include',
body: JSON.stringify({
inputs: {
question: message
},
conversation_id: null,
history: []
})
});
if (!response.ok) {
console.error('[ChatClient] Request failed:', {
status: response.status,
statusText: response.statusText
});
const errorText = await response.text();
console.error('[ChatClient] Error response:', errorText);
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log('[ChatClient] Received response:', data);
if (this.callbacks.onMessage) {
this.callbacks.onMessage(data);
}
return data;
} catch (error) {
console.error('[ChatClient] Error sending message:', error);
throw error;
}
}
}
// Rest of the code remains unchanged...
[Previous LangFlowChat class implementation]
// Register the custom element
customElements.define('langflow-chat', LangFlowChat);
console.log('[Bundle] LangFlow Chat Widget loaded and registered');
})();