convo-ai-sdk 1.1.5 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +21 -5
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export declare class ChatClient {
|
|
|
15
15
|
private _loadHistory;
|
|
16
16
|
on<T>(event: ChatEvent, callback: (data: T) => void): void;
|
|
17
17
|
connect(sessionToken?: string): Promise<void>;
|
|
18
|
+
submitDataMessage(submitType: string | undefined, data: Record<string, any>): Promise<void>;
|
|
18
19
|
requestWidget(widgetType: string, widgetData: any): Promise<void>;
|
|
19
20
|
sendToolResult(toolCallId: string, toolCallName: any, result: any): Promise<void>;
|
|
20
21
|
sendMessage(text: string): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -182,6 +182,14 @@ export class ChatClient {
|
|
|
182
182
|
}
|
|
183
183
|
this._setStatus('connected');
|
|
184
184
|
}
|
|
185
|
+
async submitDataMessage(submitType = 'form_submitted', data) {
|
|
186
|
+
if (this.status !== 'connected') {
|
|
187
|
+
console.error('Cannot submit data, not connected.');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const submitMessageContent = `<${submitType}>${JSON.stringify(data)}<{/${submitType}>`;
|
|
191
|
+
await this.sendMessage(submitMessageContent);
|
|
192
|
+
}
|
|
185
193
|
async requestWidget(widgetType, widgetData) {
|
|
186
194
|
if (this.status !== 'connected') {
|
|
187
195
|
console.error('Cannot request widget, not connected.');
|
|
@@ -189,10 +197,12 @@ export class ChatClient {
|
|
|
189
197
|
}
|
|
190
198
|
const widgetMessage = {
|
|
191
199
|
id: `widget-${Date.now()}`,
|
|
192
|
-
data: { widgetType: widgetType,
|
|
200
|
+
data: { content: JSON.stringify({ widgetType: widgetType, widgetData: widgetData }) },
|
|
193
201
|
from: 'widget',
|
|
194
202
|
timestamp: Date.now(),
|
|
195
203
|
};
|
|
204
|
+
this._addMessage(widgetMessage);
|
|
205
|
+
this._emit('messageDone', widgetMessage);
|
|
196
206
|
await this.sendStreamMessage(widgetMessage);
|
|
197
207
|
}
|
|
198
208
|
async sendToolResult(toolCallId, toolCallName, result) {
|
|
@@ -239,6 +249,9 @@ export class ChatClient {
|
|
|
239
249
|
input.toolCallId = message.data.toolCallId;
|
|
240
250
|
input.toolName = message.data.toolCallName;
|
|
241
251
|
}
|
|
252
|
+
if (message.from === 'widget') {
|
|
253
|
+
input.content = message.data.content;
|
|
254
|
+
}
|
|
242
255
|
const response = await fetch(this.config.streamApiEndpoint + 'conversation', {
|
|
243
256
|
method: 'POST',
|
|
244
257
|
headers: {
|
|
@@ -258,6 +271,9 @@ export class ChatClient {
|
|
|
258
271
|
const decoder = new TextDecoder('utf-8');
|
|
259
272
|
let buffer = '';
|
|
260
273
|
let currentMessage = null;
|
|
274
|
+
if (message.from === 'widget') {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
261
277
|
while (true) {
|
|
262
278
|
const { done, value } = await reader.read();
|
|
263
279
|
if (done) {
|
|
@@ -279,10 +295,10 @@ export class ChatClient {
|
|
|
279
295
|
}
|
|
280
296
|
const data = JSON.parse(line.substring(7));
|
|
281
297
|
currentMessage = {
|
|
282
|
-
id: data.messageId,
|
|
283
|
-
data: { content: '' },
|
|
284
|
-
from:
|
|
285
|
-
timestamp: data.timestamp || Date.now(),
|
|
298
|
+
id: data.message.messageId,
|
|
299
|
+
data: { content: data.message?.data?.content || '' },
|
|
300
|
+
from: data.message.senderType,
|
|
301
|
+
timestamp: data.message.timestamp || Date.now(),
|
|
286
302
|
};
|
|
287
303
|
this._emit('messageStart', currentMessage);
|
|
288
304
|
}
|