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