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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.2.6",
3
+ "version": "2.2.8",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -305,32 +305,54 @@ function useChat({
305
305
  };
306
306
  }, [credentials, headers, body]);
307
307
  const [error, setError] = (0, import_react.useState)();
308
- async function triggerRequest(chatRequest) {
309
- try {
310
- mutateLoading(true);
311
- const abortController = new AbortController();
312
- abortControllerRef.current = abortController;
313
- while (true) {
314
- const messagesAndDataOrJustMessage = await getStreamedResponse(
315
- api,
316
- chatRequest,
317
- mutate,
318
- mutateStreamData,
319
- streamData,
320
- extraMetadataRef,
321
- messagesRef,
322
- abortControllerRef,
323
- onFinish,
324
- onResponse,
325
- sendExtraMessageFields
326
- );
327
- if ("messages" in messagesAndDataOrJustMessage) {
328
- for (const message of messagesAndDataOrJustMessage.messages) {
329
- if (message.function_call === void 0 || typeof message.function_call === "string") {
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 = message.function_call;
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
- } catch (err) {
362
- if (err.name === "AbortError") {
363
- abortControllerRef.current = null;
364
- return null;
365
- }
366
- if (onError && err instanceof Error) {
367
- onError(err);
368
- }
369
- setError(err);
370
- } finally {
371
- mutateLoading(false);
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
- async function triggerRequest(prompt, options) {
509
- try {
510
- mutateLoading(true);
511
- const abortController2 = new AbortController();
512
- setAbortController(abortController2);
513
- mutate("", false);
514
- const res = await fetch(api, {
515
- method: "POST",
516
- body: JSON.stringify({
517
- prompt,
518
- ...extraMetadataRef.current.body,
519
- ...options == null ? void 0 : options.body
520
- }),
521
- credentials: extraMetadataRef.current.credentials,
522
- headers: {
523
- ...extraMetadataRef.current.headers,
524
- ...options == null ? void 0 : options.headers
525
- },
526
- signal: abortController2.signal
527
- }).catch((err) => {
528
- throw err;
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
- if (!res.ok) {
538
- throw new Error(
539
- await res.text() || "Failed to fetch the chat response."
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
- result += decoder(value);
554
- mutate(result, false);
555
- if (abortController2 === null) {
556
- reader.cancel();
557
- break;
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 null;
569
- }
570
- if (err instanceof Error) {
571
- if (onError) {
572
- onError(err);
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
- setError(err);
576
- } finally {
577
- mutateLoading(false);
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();
@@ -269,32 +269,54 @@ function useChat({
269
269
  };
270
270
  }, [credentials, headers, body]);
271
271
  const [error, setError] = useState();
272
- async function triggerRequest(chatRequest) {
273
- try {
274
- mutateLoading(true);
275
- const abortController = new AbortController();
276
- abortControllerRef.current = abortController;
277
- while (true) {
278
- const messagesAndDataOrJustMessage = await getStreamedResponse(
279
- api,
280
- chatRequest,
281
- mutate,
282
- mutateStreamData,
283
- streamData,
284
- extraMetadataRef,
285
- messagesRef,
286
- abortControllerRef,
287
- onFinish,
288
- onResponse,
289
- sendExtraMessageFields
290
- );
291
- if ("messages" in messagesAndDataOrJustMessage) {
292
- for (const message of messagesAndDataOrJustMessage.messages) {
293
- if (message.function_call === void 0 || typeof message.function_call === "string") {
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 = message.function_call;
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
- } catch (err) {
326
- if (err.name === "AbortError") {
327
- abortControllerRef.current = null;
328
- return null;
329
- }
330
- if (onError && err instanceof Error) {
331
- onError(err);
332
- }
333
- setError(err);
334
- } finally {
335
- mutateLoading(false);
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
- async function triggerRequest(prompt, options) {
473
- try {
474
- mutateLoading(true);
475
- const abortController2 = new AbortController();
476
- setAbortController(abortController2);
477
- mutate("", false);
478
- const res = await fetch(api, {
479
- method: "POST",
480
- body: JSON.stringify({
481
- prompt,
482
- ...extraMetadataRef.current.body,
483
- ...options == null ? void 0 : options.body
484
- }),
485
- credentials: extraMetadataRef.current.credentials,
486
- headers: {
487
- ...extraMetadataRef.current.headers,
488
- ...options == null ? void 0 : options.headers
489
- },
490
- signal: abortController2.signal
491
- }).catch((err) => {
492
- throw err;
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
- if (!res.ok) {
502
- throw new Error(
503
- await res.text() || "Failed to fetch the chat response."
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
- result += decoder(value);
518
- mutate(result, false);
519
- if (abortController2 === null) {
520
- reader.cancel();
521
- break;
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 null;
533
- }
534
- if (err instanceof Error) {
535
- if (onError) {
536
- onError(err);
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
- setError(err);
540
- } finally {
541
- mutateLoading(false);
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();