ai 2.2.6 → 2.2.8
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/package.json +1 -1
- package/react/dist/index.js +152 -116
- package/react/dist/index.mjs +152 -116
package/package.json
CHANGED
package/react/dist/index.js
CHANGED
@@ -305,32 +305,54 @@ function useChat({
|
|
305
305
|
};
|
306
306
|
}, [credentials, headers, body]);
|
307
307
|
const [error, setError] = (0, import_react.useState)();
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
308
|
+
const triggerRequest = (0, import_react.useCallback)(
|
309
|
+
async (chatRequest) => {
|
310
|
+
try {
|
311
|
+
mutateLoading(true);
|
312
|
+
const abortController = new AbortController();
|
313
|
+
abortControllerRef.current = abortController;
|
314
|
+
while (true) {
|
315
|
+
const messagesAndDataOrJustMessage = await getStreamedResponse(
|
316
|
+
api,
|
317
|
+
chatRequest,
|
318
|
+
mutate,
|
319
|
+
mutateStreamData,
|
320
|
+
streamData,
|
321
|
+
extraMetadataRef,
|
322
|
+
messagesRef,
|
323
|
+
abortControllerRef,
|
324
|
+
onFinish,
|
325
|
+
onResponse,
|
326
|
+
sendExtraMessageFields
|
327
|
+
);
|
328
|
+
if ("messages" in messagesAndDataOrJustMessage) {
|
329
|
+
let hasFollowingResponse = false;
|
330
|
+
for (const message of messagesAndDataOrJustMessage.messages) {
|
331
|
+
if (message.function_call === void 0 || typeof message.function_call === "string") {
|
332
|
+
continue;
|
333
|
+
}
|
334
|
+
hasFollowingResponse = true;
|
335
|
+
if (experimental_onFunctionCall) {
|
336
|
+
const functionCall = message.function_call;
|
337
|
+
const functionCallResponse = await experimental_onFunctionCall(
|
338
|
+
messagesRef.current,
|
339
|
+
functionCall
|
340
|
+
);
|
341
|
+
if (functionCallResponse === void 0)
|
342
|
+
break;
|
343
|
+
chatRequest = functionCallResponse;
|
344
|
+
}
|
345
|
+
}
|
346
|
+
if (!hasFollowingResponse) {
|
347
|
+
break;
|
348
|
+
}
|
349
|
+
} else {
|
350
|
+
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
351
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
330
352
|
break;
|
331
353
|
}
|
332
354
|
if (experimental_onFunctionCall) {
|
333
|
-
const functionCall =
|
355
|
+
const functionCall = streamedResponseMessage.function_call;
|
334
356
|
const functionCallResponse = await experimental_onFunctionCall(
|
335
357
|
messagesRef.current,
|
336
358
|
functionCall
|
@@ -340,37 +362,38 @@ function useChat({
|
|
340
362
|
chatRequest = functionCallResponse;
|
341
363
|
}
|
342
364
|
}
|
343
|
-
} else {
|
344
|
-
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
345
|
-
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
346
|
-
break;
|
347
|
-
}
|
348
|
-
if (experimental_onFunctionCall) {
|
349
|
-
const functionCall = streamedResponseMessage.function_call;
|
350
|
-
const functionCallResponse = await experimental_onFunctionCall(
|
351
|
-
messagesRef.current,
|
352
|
-
functionCall
|
353
|
-
);
|
354
|
-
if (functionCallResponse === void 0)
|
355
|
-
break;
|
356
|
-
chatRequest = functionCallResponse;
|
357
|
-
}
|
358
365
|
}
|
359
366
|
abortControllerRef.current = null;
|
367
|
+
} catch (err) {
|
368
|
+
if (err.name === "AbortError") {
|
369
|
+
abortControllerRef.current = null;
|
370
|
+
return null;
|
371
|
+
}
|
372
|
+
if (onError && err instanceof Error) {
|
373
|
+
onError(err);
|
374
|
+
}
|
375
|
+
setError(err);
|
376
|
+
} finally {
|
377
|
+
mutateLoading(false);
|
360
378
|
}
|
361
|
-
}
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
379
|
+
},
|
380
|
+
[
|
381
|
+
mutate,
|
382
|
+
mutateLoading,
|
383
|
+
api,
|
384
|
+
extraMetadataRef,
|
385
|
+
onResponse,
|
386
|
+
onFinish,
|
387
|
+
onError,
|
388
|
+
setError,
|
389
|
+
mutateStreamData,
|
390
|
+
streamData,
|
391
|
+
sendExtraMessageFields,
|
392
|
+
experimental_onFunctionCall,
|
393
|
+
messagesRef.current,
|
394
|
+
abortControllerRef.current
|
395
|
+
]
|
396
|
+
);
|
374
397
|
const append = (0, import_react.useCallback)(
|
375
398
|
async (message, { options, functions, function_call } = {}) => {
|
376
399
|
if (!message.id) {
|
@@ -505,78 +528,91 @@ function useCompletion({
|
|
505
528
|
body
|
506
529
|
};
|
507
530
|
}, [credentials, headers, body]);
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
});
|
530
|
-
if (onResponse) {
|
531
|
-
try {
|
532
|
-
await onResponse(res);
|
533
|
-
} catch (err) {
|
531
|
+
const triggerRequest = (0, import_react2.useCallback)(
|
532
|
+
async (prompt, options) => {
|
533
|
+
try {
|
534
|
+
mutateLoading(true);
|
535
|
+
const abortController2 = new AbortController();
|
536
|
+
setAbortController(abortController2);
|
537
|
+
mutate("", false);
|
538
|
+
const res = await fetch(api, {
|
539
|
+
method: "POST",
|
540
|
+
body: JSON.stringify({
|
541
|
+
prompt,
|
542
|
+
...extraMetadataRef.current.body,
|
543
|
+
...options == null ? void 0 : options.body
|
544
|
+
}),
|
545
|
+
credentials: extraMetadataRef.current.credentials,
|
546
|
+
headers: {
|
547
|
+
...extraMetadataRef.current.headers,
|
548
|
+
...options == null ? void 0 : options.headers
|
549
|
+
},
|
550
|
+
signal: abortController2.signal
|
551
|
+
}).catch((err) => {
|
534
552
|
throw err;
|
553
|
+
});
|
554
|
+
if (onResponse) {
|
555
|
+
try {
|
556
|
+
await onResponse(res);
|
557
|
+
} catch (err) {
|
558
|
+
throw err;
|
559
|
+
}
|
535
560
|
}
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
);
|
541
|
-
}
|
542
|
-
if (!res.body) {
|
543
|
-
throw new Error("The response body is empty.");
|
544
|
-
}
|
545
|
-
let result = "";
|
546
|
-
const reader = res.body.getReader();
|
547
|
-
const decoder = createChunkDecoder();
|
548
|
-
while (true) {
|
549
|
-
const { done, value } = await reader.read();
|
550
|
-
if (done) {
|
551
|
-
break;
|
561
|
+
if (!res.ok) {
|
562
|
+
throw new Error(
|
563
|
+
await res.text() || "Failed to fetch the chat response."
|
564
|
+
);
|
552
565
|
}
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
566
|
+
if (!res.body) {
|
567
|
+
throw new Error("The response body is empty.");
|
568
|
+
}
|
569
|
+
let result = "";
|
570
|
+
const reader = res.body.getReader();
|
571
|
+
const decoder = createChunkDecoder();
|
572
|
+
while (true) {
|
573
|
+
const { done, value } = await reader.read();
|
574
|
+
if (done) {
|
575
|
+
break;
|
576
|
+
}
|
577
|
+
result += decoder(value);
|
578
|
+
mutate(result, false);
|
579
|
+
if (abortController2 === null) {
|
580
|
+
reader.cancel();
|
581
|
+
break;
|
582
|
+
}
|
583
|
+
}
|
584
|
+
if (onFinish) {
|
585
|
+
onFinish(prompt, result);
|
558
586
|
}
|
559
|
-
}
|
560
|
-
if (onFinish) {
|
561
|
-
onFinish(prompt, result);
|
562
|
-
}
|
563
|
-
setAbortController(null);
|
564
|
-
return result;
|
565
|
-
} catch (err) {
|
566
|
-
if (err.name === "AbortError") {
|
567
587
|
setAbortController(null);
|
568
|
-
return
|
569
|
-
}
|
570
|
-
|
571
|
-
|
572
|
-
|
588
|
+
return result;
|
589
|
+
} catch (err) {
|
590
|
+
if (err.name === "AbortError") {
|
591
|
+
setAbortController(null);
|
592
|
+
return null;
|
573
593
|
}
|
594
|
+
if (err instanceof Error) {
|
595
|
+
if (onError) {
|
596
|
+
onError(err);
|
597
|
+
}
|
598
|
+
}
|
599
|
+
setError(err);
|
600
|
+
} finally {
|
601
|
+
mutateLoading(false);
|
574
602
|
}
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
603
|
+
},
|
604
|
+
[
|
605
|
+
mutate,
|
606
|
+
mutateLoading,
|
607
|
+
api,
|
608
|
+
extraMetadataRef,
|
609
|
+
setAbortController,
|
610
|
+
onResponse,
|
611
|
+
onFinish,
|
612
|
+
onError,
|
613
|
+
setError
|
614
|
+
]
|
615
|
+
);
|
580
616
|
const stop = (0, import_react2.useCallback)(() => {
|
581
617
|
if (abortController) {
|
582
618
|
abortController.abort();
|
package/react/dist/index.mjs
CHANGED
@@ -269,32 +269,54 @@ function useChat({
|
|
269
269
|
};
|
270
270
|
}, [credentials, headers, body]);
|
271
271
|
const [error, setError] = useState();
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
272
|
+
const triggerRequest = useCallback(
|
273
|
+
async (chatRequest) => {
|
274
|
+
try {
|
275
|
+
mutateLoading(true);
|
276
|
+
const abortController = new AbortController();
|
277
|
+
abortControllerRef.current = abortController;
|
278
|
+
while (true) {
|
279
|
+
const messagesAndDataOrJustMessage = await getStreamedResponse(
|
280
|
+
api,
|
281
|
+
chatRequest,
|
282
|
+
mutate,
|
283
|
+
mutateStreamData,
|
284
|
+
streamData,
|
285
|
+
extraMetadataRef,
|
286
|
+
messagesRef,
|
287
|
+
abortControllerRef,
|
288
|
+
onFinish,
|
289
|
+
onResponse,
|
290
|
+
sendExtraMessageFields
|
291
|
+
);
|
292
|
+
if ("messages" in messagesAndDataOrJustMessage) {
|
293
|
+
let hasFollowingResponse = false;
|
294
|
+
for (const message of messagesAndDataOrJustMessage.messages) {
|
295
|
+
if (message.function_call === void 0 || typeof message.function_call === "string") {
|
296
|
+
continue;
|
297
|
+
}
|
298
|
+
hasFollowingResponse = true;
|
299
|
+
if (experimental_onFunctionCall) {
|
300
|
+
const functionCall = message.function_call;
|
301
|
+
const functionCallResponse = await experimental_onFunctionCall(
|
302
|
+
messagesRef.current,
|
303
|
+
functionCall
|
304
|
+
);
|
305
|
+
if (functionCallResponse === void 0)
|
306
|
+
break;
|
307
|
+
chatRequest = functionCallResponse;
|
308
|
+
}
|
309
|
+
}
|
310
|
+
if (!hasFollowingResponse) {
|
311
|
+
break;
|
312
|
+
}
|
313
|
+
} else {
|
314
|
+
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
315
|
+
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
294
316
|
break;
|
295
317
|
}
|
296
318
|
if (experimental_onFunctionCall) {
|
297
|
-
const functionCall =
|
319
|
+
const functionCall = streamedResponseMessage.function_call;
|
298
320
|
const functionCallResponse = await experimental_onFunctionCall(
|
299
321
|
messagesRef.current,
|
300
322
|
functionCall
|
@@ -304,37 +326,38 @@ function useChat({
|
|
304
326
|
chatRequest = functionCallResponse;
|
305
327
|
}
|
306
328
|
}
|
307
|
-
} else {
|
308
|
-
const streamedResponseMessage = messagesAndDataOrJustMessage;
|
309
|
-
if (streamedResponseMessage.function_call === void 0 || typeof streamedResponseMessage.function_call === "string") {
|
310
|
-
break;
|
311
|
-
}
|
312
|
-
if (experimental_onFunctionCall) {
|
313
|
-
const functionCall = streamedResponseMessage.function_call;
|
314
|
-
const functionCallResponse = await experimental_onFunctionCall(
|
315
|
-
messagesRef.current,
|
316
|
-
functionCall
|
317
|
-
);
|
318
|
-
if (functionCallResponse === void 0)
|
319
|
-
break;
|
320
|
-
chatRequest = functionCallResponse;
|
321
|
-
}
|
322
329
|
}
|
323
330
|
abortControllerRef.current = null;
|
331
|
+
} catch (err) {
|
332
|
+
if (err.name === "AbortError") {
|
333
|
+
abortControllerRef.current = null;
|
334
|
+
return null;
|
335
|
+
}
|
336
|
+
if (onError && err instanceof Error) {
|
337
|
+
onError(err);
|
338
|
+
}
|
339
|
+
setError(err);
|
340
|
+
} finally {
|
341
|
+
mutateLoading(false);
|
324
342
|
}
|
325
|
-
}
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
343
|
+
},
|
344
|
+
[
|
345
|
+
mutate,
|
346
|
+
mutateLoading,
|
347
|
+
api,
|
348
|
+
extraMetadataRef,
|
349
|
+
onResponse,
|
350
|
+
onFinish,
|
351
|
+
onError,
|
352
|
+
setError,
|
353
|
+
mutateStreamData,
|
354
|
+
streamData,
|
355
|
+
sendExtraMessageFields,
|
356
|
+
experimental_onFunctionCall,
|
357
|
+
messagesRef.current,
|
358
|
+
abortControllerRef.current
|
359
|
+
]
|
360
|
+
);
|
338
361
|
const append = useCallback(
|
339
362
|
async (message, { options, functions, function_call } = {}) => {
|
340
363
|
if (!message.id) {
|
@@ -469,78 +492,91 @@ function useCompletion({
|
|
469
492
|
body
|
470
493
|
};
|
471
494
|
}, [credentials, headers, body]);
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
});
|
494
|
-
if (onResponse) {
|
495
|
-
try {
|
496
|
-
await onResponse(res);
|
497
|
-
} catch (err) {
|
495
|
+
const triggerRequest = useCallback2(
|
496
|
+
async (prompt, options) => {
|
497
|
+
try {
|
498
|
+
mutateLoading(true);
|
499
|
+
const abortController2 = new AbortController();
|
500
|
+
setAbortController(abortController2);
|
501
|
+
mutate("", false);
|
502
|
+
const res = await fetch(api, {
|
503
|
+
method: "POST",
|
504
|
+
body: JSON.stringify({
|
505
|
+
prompt,
|
506
|
+
...extraMetadataRef.current.body,
|
507
|
+
...options == null ? void 0 : options.body
|
508
|
+
}),
|
509
|
+
credentials: extraMetadataRef.current.credentials,
|
510
|
+
headers: {
|
511
|
+
...extraMetadataRef.current.headers,
|
512
|
+
...options == null ? void 0 : options.headers
|
513
|
+
},
|
514
|
+
signal: abortController2.signal
|
515
|
+
}).catch((err) => {
|
498
516
|
throw err;
|
517
|
+
});
|
518
|
+
if (onResponse) {
|
519
|
+
try {
|
520
|
+
await onResponse(res);
|
521
|
+
} catch (err) {
|
522
|
+
throw err;
|
523
|
+
}
|
499
524
|
}
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
);
|
505
|
-
}
|
506
|
-
if (!res.body) {
|
507
|
-
throw new Error("The response body is empty.");
|
508
|
-
}
|
509
|
-
let result = "";
|
510
|
-
const reader = res.body.getReader();
|
511
|
-
const decoder = createChunkDecoder();
|
512
|
-
while (true) {
|
513
|
-
const { done, value } = await reader.read();
|
514
|
-
if (done) {
|
515
|
-
break;
|
525
|
+
if (!res.ok) {
|
526
|
+
throw new Error(
|
527
|
+
await res.text() || "Failed to fetch the chat response."
|
528
|
+
);
|
516
529
|
}
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
530
|
+
if (!res.body) {
|
531
|
+
throw new Error("The response body is empty.");
|
532
|
+
}
|
533
|
+
let result = "";
|
534
|
+
const reader = res.body.getReader();
|
535
|
+
const decoder = createChunkDecoder();
|
536
|
+
while (true) {
|
537
|
+
const { done, value } = await reader.read();
|
538
|
+
if (done) {
|
539
|
+
break;
|
540
|
+
}
|
541
|
+
result += decoder(value);
|
542
|
+
mutate(result, false);
|
543
|
+
if (abortController2 === null) {
|
544
|
+
reader.cancel();
|
545
|
+
break;
|
546
|
+
}
|
547
|
+
}
|
548
|
+
if (onFinish) {
|
549
|
+
onFinish(prompt, result);
|
522
550
|
}
|
523
|
-
}
|
524
|
-
if (onFinish) {
|
525
|
-
onFinish(prompt, result);
|
526
|
-
}
|
527
|
-
setAbortController(null);
|
528
|
-
return result;
|
529
|
-
} catch (err) {
|
530
|
-
if (err.name === "AbortError") {
|
531
551
|
setAbortController(null);
|
532
|
-
return
|
533
|
-
}
|
534
|
-
|
535
|
-
|
536
|
-
|
552
|
+
return result;
|
553
|
+
} catch (err) {
|
554
|
+
if (err.name === "AbortError") {
|
555
|
+
setAbortController(null);
|
556
|
+
return null;
|
537
557
|
}
|
558
|
+
if (err instanceof Error) {
|
559
|
+
if (onError) {
|
560
|
+
onError(err);
|
561
|
+
}
|
562
|
+
}
|
563
|
+
setError(err);
|
564
|
+
} finally {
|
565
|
+
mutateLoading(false);
|
538
566
|
}
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
567
|
+
},
|
568
|
+
[
|
569
|
+
mutate,
|
570
|
+
mutateLoading,
|
571
|
+
api,
|
572
|
+
extraMetadataRef,
|
573
|
+
setAbortController,
|
574
|
+
onResponse,
|
575
|
+
onFinish,
|
576
|
+
onError,
|
577
|
+
setError
|
578
|
+
]
|
579
|
+
);
|
544
580
|
const stop = useCallback2(() => {
|
545
581
|
if (abortController) {
|
546
582
|
abortController.abort();
|