ai 3.1.21 → 3.1.22

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