convo-ai-sdk 1.2.5 → 1.2.6
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 +2 -0
- package/dist/index.js +90 -76
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ export declare class ChatClient {
|
|
|
4
4
|
private listeners;
|
|
5
5
|
status: ConnectionStatus;
|
|
6
6
|
conversationStatus: ConversationStatus;
|
|
7
|
+
awaitingResponse: boolean;
|
|
7
8
|
messages: ChatMessage[];
|
|
8
9
|
config: Partial<ChatConfig>;
|
|
9
10
|
constructor(options: ClientOptions);
|
|
10
11
|
private _emit;
|
|
11
12
|
private _setStatus;
|
|
12
13
|
private _setConversationStatus;
|
|
14
|
+
private _setAwaitingResponse;
|
|
13
15
|
private _addMessage;
|
|
14
16
|
private _loadGreeting;
|
|
15
17
|
private _loadHistory;
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export class ChatClient {
|
|
|
4
4
|
listeners = {};
|
|
5
5
|
status = 'disconnected';
|
|
6
6
|
conversationStatus = 'started';
|
|
7
|
+
awaitingResponse = false;
|
|
7
8
|
messages = [];
|
|
8
9
|
config = {};
|
|
9
10
|
constructor(options) {
|
|
@@ -28,6 +29,10 @@ export class ChatClient {
|
|
|
28
29
|
this.conversationStatus = newStatus;
|
|
29
30
|
this._emit('conversationStatusChange', this.conversationStatus);
|
|
30
31
|
}
|
|
32
|
+
_setAwaitingResponse(awaiting) {
|
|
33
|
+
this.awaitingResponse = awaiting;
|
|
34
|
+
this._emit('awaitingResponse', this.awaitingResponse);
|
|
35
|
+
}
|
|
31
36
|
_addMessage(message) {
|
|
32
37
|
this.messages.push(message);
|
|
33
38
|
}
|
|
@@ -319,91 +324,100 @@ export class ChatClient {
|
|
|
319
324
|
if (message.from === 'widget') {
|
|
320
325
|
input.content = message.data.content;
|
|
321
326
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
throw new Error('Response body is null');
|
|
336
|
-
}
|
|
337
|
-
const reader = response.body.getReader();
|
|
338
|
-
const decoder = new TextDecoder('utf-8');
|
|
339
|
-
let buffer = '';
|
|
340
|
-
let currentMessage = null;
|
|
341
|
-
if (message.from === 'widget') {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
while (true) {
|
|
345
|
-
const { done, value } = await reader.read();
|
|
346
|
-
if (done) {
|
|
347
|
-
break;
|
|
327
|
+
try {
|
|
328
|
+
this._setAwaitingResponse(true);
|
|
329
|
+
const response = await fetch(this.config.streamApiEndpoint + 'conversation', {
|
|
330
|
+
method: 'POST',
|
|
331
|
+
headers: {
|
|
332
|
+
'Content-Type': 'application/json',
|
|
333
|
+
'Authorization': `Bearer ${this.config.sessionToken}`,
|
|
334
|
+
'Accept': 'text/event-stream'
|
|
335
|
+
},
|
|
336
|
+
body: JSON.stringify(input)
|
|
337
|
+
});
|
|
338
|
+
if (!response.ok) {
|
|
339
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
348
340
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
341
|
+
if (!response.body) {
|
|
342
|
+
throw new Error('Response body is null');
|
|
343
|
+
}
|
|
344
|
+
const reader = response.body.getReader();
|
|
345
|
+
const decoder = new TextDecoder('utf-8');
|
|
346
|
+
let buffer = '';
|
|
347
|
+
let currentMessage = null;
|
|
348
|
+
if (message.from === 'widget') {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
while (true) {
|
|
352
|
+
const { done, value } = await reader.read();
|
|
353
|
+
if (done) {
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
buffer += decoder.decode(value, { stream: true });
|
|
357
|
+
const lines = buffer.split('\n');
|
|
358
|
+
buffer = lines.pop() || '';
|
|
359
|
+
for (const line of lines) {
|
|
360
|
+
try {
|
|
361
|
+
if (line.startsWith('thought: ')) {
|
|
362
|
+
const data = JSON.parse(line.substring(9));
|
|
363
|
+
this._emit('thought', data);
|
|
362
364
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
this._emit('messageData', data.p);
|
|
365
|
+
else if (line.startsWith('start: ')) {
|
|
366
|
+
if (currentMessage) {
|
|
367
|
+
this._addMessage(currentMessage);
|
|
368
|
+
this._emit('messageDone', currentMessage);
|
|
369
|
+
}
|
|
370
|
+
const data = JSON.parse(line.substring(7));
|
|
371
|
+
currentMessage = {
|
|
372
|
+
id: data.message.messageId,
|
|
373
|
+
data: { content: data.message?.data?.content || '' },
|
|
374
|
+
from: data.message.senderType,
|
|
375
|
+
timestamp: data.message.timestamp || Date.now(),
|
|
376
|
+
};
|
|
377
|
+
this._emit('messageStart', currentMessage);
|
|
377
378
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
379
|
+
else if (line.startsWith('data: ') && currentMessage) {
|
|
380
|
+
const data = JSON.parse(line.substring(6));
|
|
381
|
+
currentMessage.data.content += data.p;
|
|
382
|
+
if (data.p) {
|
|
383
|
+
this._emit('messageData', data.p);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
else if (line.startsWith('tool_call: ')) {
|
|
387
|
+
const data = JSON.parse(line.substring(11));
|
|
388
|
+
this._emit('toolCall', data);
|
|
389
|
+
}
|
|
390
|
+
else if (line.startsWith('error: ')) {
|
|
391
|
+
const error = JSON.parse(line.substring(7));
|
|
392
|
+
this._emit('messageError', error);
|
|
393
|
+
currentMessage = null;
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
else if (line.startsWith('status: ')) {
|
|
397
|
+
const data = JSON.parse(line.substring(8));
|
|
398
|
+
if (data.value) {
|
|
399
|
+
this._setConversationStatus(data.value);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
else if (line.startsWith('done') && currentMessage) {
|
|
403
|
+
this._addMessage(currentMessage);
|
|
404
|
+
this._emit('messageDone', currentMessage);
|
|
405
|
+
currentMessage = null;
|
|
406
|
+
break;
|
|
393
407
|
}
|
|
394
408
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
this._emit('messageDone', currentMessage);
|
|
398
|
-
currentMessage = null;
|
|
399
|
-
break;
|
|
409
|
+
catch (e) {
|
|
410
|
+
console.warn('Could not parse event data as JSON:', line);
|
|
400
411
|
}
|
|
401
412
|
}
|
|
402
|
-
catch (e) {
|
|
403
|
-
console.warn('Could not parse event data as JSON:', line);
|
|
404
|
-
}
|
|
405
413
|
}
|
|
406
414
|
}
|
|
415
|
+
catch (error) {
|
|
416
|
+
this._emit('messageError', error);
|
|
417
|
+
}
|
|
418
|
+
finally {
|
|
419
|
+
this._setAwaitingResponse(false);
|
|
420
|
+
}
|
|
407
421
|
}
|
|
408
422
|
disconnect() {
|
|
409
423
|
this._setStatus('disconnected');
|
package/dist/types.d.ts
CHANGED
|
@@ -20,4 +20,4 @@ export interface ClientOptions {
|
|
|
20
20
|
identifier?: string;
|
|
21
21
|
dynamicVariables?: Record<string, any>;
|
|
22
22
|
}
|
|
23
|
-
export type ChatEvent = 'statusChange' | 'conversationStatusChange' | 'message' | 'configLoaded' | 'historyLoaded' | 'messageStart' | 'messageData' | 'messageDone' | 'messageError' | 'toolCall' | 'thought' | 'reset';
|
|
23
|
+
export type ChatEvent = 'statusChange' | 'conversationStatusChange' | 'message' | 'configLoaded' | 'historyLoaded' | 'messageStart' | 'messageData' | 'messageDone' | 'messageError' | 'toolCall' | 'thought' | 'reset' | 'awaitingResponse';
|