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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "2.2.7",
3
+ "version": "2.2.8",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -305,34 +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
- let hasFollowingResponse = false;
329
- for (const message of messagesAndDataOrJustMessage.messages) {
330
- if (message.function_call === void 0 || typeof message.function_call === "string") {
331
- continue;
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 = message.function_call;
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
- return null;
370
- }
371
- if (onError && err instanceof Error) {
372
- onError(err);
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
- setError(err);
375
- } finally {
376
- mutateLoading(false);
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
- async function triggerRequest(prompt, options) {
514
- try {
515
- mutateLoading(true);
516
- const abortController2 = new AbortController();
517
- setAbortController(abortController2);
518
- mutate("", false);
519
- const res = await fetch(api, {
520
- method: "POST",
521
- body: JSON.stringify({
522
- prompt,
523
- ...extraMetadataRef.current.body,
524
- ...options == null ? void 0 : options.body
525
- }),
526
- credentials: extraMetadataRef.current.credentials,
527
- headers: {
528
- ...extraMetadataRef.current.headers,
529
- ...options == null ? void 0 : options.headers
530
- },
531
- signal: abortController2.signal
532
- }).catch((err) => {
533
- throw err;
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
- if (!res.ok) {
543
- throw new Error(
544
- await res.text() || "Failed to fetch the chat response."
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
- result += decoder(value);
559
- mutate(result, false);
560
- if (abortController2 === null) {
561
- reader.cancel();
562
- 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);
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 null;
574
- }
575
- if (err instanceof Error) {
576
- if (onError) {
577
- onError(err);
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
- setError(err);
581
- } finally {
582
- mutateLoading(false);
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();
@@ -269,34 +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
- let hasFollowingResponse = false;
293
- for (const message of messagesAndDataOrJustMessage.messages) {
294
- if (message.function_call === void 0 || typeof message.function_call === "string") {
295
- continue;
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 = message.function_call;
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
- return null;
334
- }
335
- if (onError && err instanceof Error) {
336
- onError(err);
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
- setError(err);
339
- } finally {
340
- mutateLoading(false);
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
- async function triggerRequest(prompt, options) {
478
- try {
479
- mutateLoading(true);
480
- const abortController2 = new AbortController();
481
- setAbortController(abortController2);
482
- mutate("", false);
483
- const res = await fetch(api, {
484
- method: "POST",
485
- body: JSON.stringify({
486
- prompt,
487
- ...extraMetadataRef.current.body,
488
- ...options == null ? void 0 : options.body
489
- }),
490
- credentials: extraMetadataRef.current.credentials,
491
- headers: {
492
- ...extraMetadataRef.current.headers,
493
- ...options == null ? void 0 : options.headers
494
- },
495
- signal: abortController2.signal
496
- }).catch((err) => {
497
- throw err;
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
- if (!res.ok) {
507
- throw new Error(
508
- await res.text() || "Failed to fetch the chat response."
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
- result += decoder(value);
523
- mutate(result, false);
524
- if (abortController2 === null) {
525
- reader.cancel();
526
- 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);
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 null;
538
- }
539
- if (err instanceof Error) {
540
- if (onError) {
541
- onError(err);
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
- setError(err);
545
- } finally {
546
- mutateLoading(false);
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();