ai 3.1.21 → 3.1.23

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": "3.1.21",
3
+ "version": "3.1.23",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -486,10 +486,18 @@ type StreamingReactResponseAction = (payload: {
486
486
  messages: Message[];
487
487
  data?: Record<string, string>;
488
488
  }) => Promise<experimental_StreamingReactResponse>;
489
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
489
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
490
490
  api?: string | StreamingReactResponseAction;
491
491
  key?: string;
492
492
  /**
493
+ @deprecated Use `maxToolRoundtrips` instead.
494
+ */
495
+ experimental_maxAutomaticRoundtrips?: number;
496
+ /**
497
+ @deprecated Use `maxToolRoundtrips` instead.
498
+ */
499
+ maxAutomaticRoundtrips?: number;
500
+ /**
493
501
  Maximal number of automatic roundtrips for tool calls.
494
502
 
495
503
  An automatic tool call roundtrip is a call to the server with the
@@ -501,12 +509,19 @@ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMess
501
509
 
502
510
  By default, it's set to 0, which will disable the feature.
503
511
  */
504
- experimental_maxAutomaticRoundtrips?: number;
512
+ maxToolRoundtrips?: number;
505
513
  }): UseChatHelpers & {
514
+ /**
515
+ * @deprecated Use `addToolResult` instead.
516
+ */
506
517
  experimental_addToolResult: ({ toolCallId, result, }: {
507
518
  toolCallId: string;
508
519
  result: any;
509
520
  }) => void;
521
+ addToolResult: ({ toolCallId, result, }: {
522
+ toolCallId: string;
523
+ result: any;
524
+ }) => void;
510
525
  };
511
526
 
512
527
  type UseCompletionHelpers = {
@@ -486,10 +486,18 @@ type StreamingReactResponseAction = (payload: {
486
486
  messages: Message[];
487
487
  data?: Record<string, string>;
488
488
  }) => Promise<experimental_StreamingReactResponse>;
489
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
489
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, maxAutomaticRoundtrips, maxToolRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
490
490
  api?: string | StreamingReactResponseAction;
491
491
  key?: string;
492
492
  /**
493
+ @deprecated Use `maxToolRoundtrips` instead.
494
+ */
495
+ experimental_maxAutomaticRoundtrips?: number;
496
+ /**
497
+ @deprecated Use `maxToolRoundtrips` instead.
498
+ */
499
+ maxAutomaticRoundtrips?: number;
500
+ /**
493
501
  Maximal number of automatic roundtrips for tool calls.
494
502
 
495
503
  An automatic tool call roundtrip is a call to the server with the
@@ -501,12 +509,19 @@ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMess
501
509
 
502
510
  By default, it's set to 0, which will disable the feature.
503
511
  */
504
- experimental_maxAutomaticRoundtrips?: number;
512
+ maxToolRoundtrips?: number;
505
513
  }): UseChatHelpers & {
514
+ /**
515
+ * @deprecated Use `addToolResult` instead.
516
+ */
506
517
  experimental_addToolResult: ({ toolCallId, result, }: {
507
518
  toolCallId: string;
508
519
  result: any;
509
520
  }) => void;
521
+ addToolResult: ({ toolCallId, result, }: {
522
+ toolCallId: string;
523
+ result: any;
524
+ }) => void;
510
525
  };
511
526
 
512
527
  type UseCompletionHelpers = {
@@ -791,6 +791,8 @@ function useChat({
791
791
  experimental_onToolCall,
792
792
  onToolCall,
793
793
  experimental_maxAutomaticRoundtrips = 0,
794
+ maxAutomaticRoundtrips = experimental_maxAutomaticRoundtrips,
795
+ maxToolRoundtrips = maxAutomaticRoundtrips,
794
796
  streamMode,
795
797
  onResponse,
796
798
  onFinish,
@@ -881,9 +883,9 @@ function useChat({
881
883
  if (
882
884
  // ensure there is a last message:
883
885
  lastMessage != null && // check if the feature is enabled:
884
- experimental_maxAutomaticRoundtrips > 0 && // check that roundtrip is possible:
886
+ maxToolRoundtrips > 0 && // check that roundtrip is possible:
885
887
  isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic roundtrips:
886
- countTrailingAssistantMessages(messages2) <= experimental_maxAutomaticRoundtrips
888
+ countTrailingAssistantMessages(messages2) <= maxToolRoundtrips
887
889
  ) {
888
890
  await triggerRequest({ messages: messages2 });
889
891
  }
@@ -904,7 +906,7 @@ function useChat({
904
906
  experimental_onFunctionCall,
905
907
  experimental_onToolCall,
906
908
  onToolCall,
907
- experimental_maxAutomaticRoundtrips,
909
+ maxToolRoundtrips,
908
910
  messagesRef,
909
911
  abortControllerRef,
910
912
  generateId2
@@ -1009,6 +1011,27 @@ function useChat({
1009
1011
  const handleInputChange = (e) => {
1010
1012
  setInput(e.target.value);
1011
1013
  };
1014
+ const addToolResult = ({
1015
+ toolCallId,
1016
+ result
1017
+ }) => {
1018
+ const updatedMessages = messagesRef.current.map(
1019
+ (message, index, arr) => (
1020
+ // update the tool calls in the last assistant message:
1021
+ index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
1022
+ ...message,
1023
+ toolInvocations: message.toolInvocations.map(
1024
+ (toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
1025
+ )
1026
+ } : message
1027
+ )
1028
+ );
1029
+ mutate(updatedMessages, false);
1030
+ const lastMessage = updatedMessages[updatedMessages.length - 1];
1031
+ if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
1032
+ triggerRequest({ messages: updatedMessages });
1033
+ }
1034
+ };
1012
1035
  return {
1013
1036
  messages: messages || [],
1014
1037
  error,
@@ -1022,27 +1045,8 @@ function useChat({
1022
1045
  handleSubmit,
1023
1046
  isLoading,
1024
1047
  data: streamData,
1025
- experimental_addToolResult: ({
1026
- toolCallId,
1027
- result
1028
- }) => {
1029
- const updatedMessages = messagesRef.current.map(
1030
- (message, index, arr) => (
1031
- // update the tool calls in the last assistant message:
1032
- index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
1033
- ...message,
1034
- toolInvocations: message.toolInvocations.map(
1035
- (toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
1036
- )
1037
- } : message
1038
- )
1039
- );
1040
- mutate(updatedMessages, false);
1041
- const lastMessage = updatedMessages[updatedMessages.length - 1];
1042
- if (isAssistantMessageWithCompletedToolCalls(lastMessage)) {
1043
- triggerRequest({ messages: updatedMessages });
1044
- }
1045
- }
1048
+ addToolResult,
1049
+ experimental_addToolResult: addToolResult
1046
1050
  };
1047
1051
  }
1048
1052
  function isAssistantMessageWithCompletedToolCalls(message) {