ai 3.1.5 → 3.1.7

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.5",
3
+ "version": "3.1.7",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -57,8 +57,8 @@
57
57
  }
58
58
  },
59
59
  "dependencies": {
60
- "@ai-sdk/provider": "0.0.3",
61
- "@ai-sdk/provider-utils": "0.0.6",
60
+ "@ai-sdk/provider": "0.0.4",
61
+ "@ai-sdk/provider-utils": "0.0.7",
62
62
  "secure-json-parse": "2.7.0",
63
63
  "eventsource-parser": "1.1.2",
64
64
  "jsondiffpatch": "0.6.0",
@@ -1,3 +1,45 @@
1
+ /**
2
+ Typed tool call that is returned by generateText and streamText.
3
+ It contains the tool call ID, the tool name, and the tool arguments.
4
+ */
5
+ interface ToolCall$1<NAME extends string, ARGS> {
6
+ /**
7
+ ID of the tool call. This ID is used to match the tool call with the tool result.
8
+ */
9
+ toolCallId: string;
10
+ /**
11
+ Name of the tool that is being called.
12
+ */
13
+ toolName: NAME;
14
+ /**
15
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
16
+ */
17
+ args: ARGS;
18
+ }
19
+
20
+ /**
21
+ Typed tool result that is returned by generateText and streamText.
22
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
23
+ */
24
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
25
+ /**
26
+ ID of the tool call. This ID is used to match the tool call with the tool result.
27
+ */
28
+ toolCallId: string;
29
+ /**
30
+ Name of the tool that was called.
31
+ */
32
+ toolName: NAME;
33
+ /**
34
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
35
+ */
36
+ args: ARGS;
37
+ /**
38
+ Result of the tool call. This is the result of the tool's execution.
39
+ */
40
+ result: RESULT;
41
+ }
42
+
1
43
  interface FunctionCall {
2
44
  /**
3
45
  * The arguments to call the function with, as generated by the model in JSON
@@ -22,6 +64,12 @@ interface ToolCall {
22
64
  arguments: string;
23
65
  };
24
66
  }
67
+ /**
68
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
69
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
70
+ Once the call is complete, the invocation is a tool result.
71
+ */
72
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
25
73
  /**
26
74
  * Shared types between the API and UI packages.
27
75
  */
@@ -30,9 +78,13 @@ interface Message {
30
78
  tool_call_id?: string;
31
79
  createdAt?: Date;
32
80
  content: string;
81
+ /**
82
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
83
+ */
33
84
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
34
85
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
35
86
  /**
87
+ *
36
88
  * If the message has a role of `function`, the `name` field is the name of the function.
37
89
  * Otherwise, the name field should not be set.
38
90
  */
@@ -53,6 +105,11 @@ interface Message {
53
105
  * Additional message-specific information added on the server via StreamData
54
106
  */
55
107
  annotations?: JSONValue[] | undefined;
108
+ /**
109
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
110
+ that the assistant made as part of this message.
111
+ */
112
+ toolInvocations?: Array<ToolInvocation>;
56
113
  }
57
114
  type JSONValue = null | string | number | boolean | {
58
115
  [x: string]: JSONValue;
@@ -1,3 +1,45 @@
1
+ /**
2
+ Typed tool call that is returned by generateText and streamText.
3
+ It contains the tool call ID, the tool name, and the tool arguments.
4
+ */
5
+ interface ToolCall$1<NAME extends string, ARGS> {
6
+ /**
7
+ ID of the tool call. This ID is used to match the tool call with the tool result.
8
+ */
9
+ toolCallId: string;
10
+ /**
11
+ Name of the tool that is being called.
12
+ */
13
+ toolName: NAME;
14
+ /**
15
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
16
+ */
17
+ args: ARGS;
18
+ }
19
+
20
+ /**
21
+ Typed tool result that is returned by generateText and streamText.
22
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
23
+ */
24
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
25
+ /**
26
+ ID of the tool call. This ID is used to match the tool call with the tool result.
27
+ */
28
+ toolCallId: string;
29
+ /**
30
+ Name of the tool that was called.
31
+ */
32
+ toolName: NAME;
33
+ /**
34
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
35
+ */
36
+ args: ARGS;
37
+ /**
38
+ Result of the tool call. This is the result of the tool's execution.
39
+ */
40
+ result: RESULT;
41
+ }
42
+
1
43
  interface FunctionCall {
2
44
  /**
3
45
  * The arguments to call the function with, as generated by the model in JSON
@@ -22,6 +64,12 @@ interface ToolCall {
22
64
  arguments: string;
23
65
  };
24
66
  }
67
+ /**
68
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
69
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
70
+ Once the call is complete, the invocation is a tool result.
71
+ */
72
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
25
73
  /**
26
74
  * Shared types between the API and UI packages.
27
75
  */
@@ -30,9 +78,13 @@ interface Message {
30
78
  tool_call_id?: string;
31
79
  createdAt?: Date;
32
80
  content: string;
81
+ /**
82
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
83
+ */
33
84
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
34
85
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
35
86
  /**
87
+ *
36
88
  * If the message has a role of `function`, the `name` field is the name of the function.
37
89
  * Otherwise, the name field should not be set.
38
90
  */
@@ -53,6 +105,11 @@ interface Message {
53
105
  * Additional message-specific information added on the server via StreamData
54
106
  */
55
107
  annotations?: JSONValue[] | undefined;
108
+ /**
109
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
110
+ that the assistant made as part of this message.
111
+ */
112
+ toolInvocations?: Array<ToolInvocation>;
56
113
  }
57
114
  type JSONValue = null | string | number | boolean | {
58
115
  [x: string]: JSONValue;
@@ -1,3 +1,45 @@
1
+ /**
2
+ Typed tool call that is returned by generateText and streamText.
3
+ It contains the tool call ID, the tool name, and the tool arguments.
4
+ */
5
+ interface ToolCall$1<NAME extends string, ARGS> {
6
+ /**
7
+ ID of the tool call. This ID is used to match the tool call with the tool result.
8
+ */
9
+ toolCallId: string;
10
+ /**
11
+ Name of the tool that is being called.
12
+ */
13
+ toolName: NAME;
14
+ /**
15
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
16
+ */
17
+ args: ARGS;
18
+ }
19
+
20
+ /**
21
+ Typed tool result that is returned by generateText and streamText.
22
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
23
+ */
24
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
25
+ /**
26
+ ID of the tool call. This ID is used to match the tool call with the tool result.
27
+ */
28
+ toolCallId: string;
29
+ /**
30
+ Name of the tool that was called.
31
+ */
32
+ toolName: NAME;
33
+ /**
34
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
35
+ */
36
+ args: ARGS;
37
+ /**
38
+ Result of the tool call. This is the result of the tool's execution.
39
+ */
40
+ result: RESULT;
41
+ }
42
+
1
43
  interface FunctionCall {
2
44
  /**
3
45
  * The arguments to call the function with, as generated by the model in JSON
@@ -66,6 +108,12 @@ interface Function {
66
108
  description?: string;
67
109
  }
68
110
  type IdGenerator = () => string;
111
+ /**
112
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
113
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
114
+ Once the call is complete, the invocation is a tool result.
115
+ */
116
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
69
117
  /**
70
118
  * Shared types between the API and UI packages.
71
119
  */
@@ -74,9 +122,13 @@ interface Message {
74
122
  tool_call_id?: string;
75
123
  createdAt?: Date;
76
124
  content: string;
125
+ /**
126
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
127
+ */
77
128
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
78
129
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
79
130
  /**
131
+ *
80
132
  * If the message has a role of `function`, the `name` field is the name of the function.
81
133
  * Otherwise, the name field should not be set.
82
134
  */
@@ -97,6 +149,11 @@ interface Message {
97
149
  * Additional message-specific information added on the server via StreamData
98
150
  */
99
151
  annotations?: JSONValue[] | undefined;
152
+ /**
153
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
154
+ that the assistant made as part of this message.
155
+ */
156
+ toolInvocations?: Array<ToolInvocation>;
100
157
  }
101
158
  type CreateMessage = Omit<Message, 'id'> & {
102
159
  id?: Message['id'];
@@ -297,7 +354,9 @@ declare class StreamData {
297
354
 
298
355
  type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
299
356
  /**
300
- * A utility class for streaming React responses.
357
+ A utility class for streaming React responses.
358
+
359
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
301
360
  */
302
361
  declare class experimental_StreamingReactResponse {
303
362
  constructor(res: ReadableStream, options?: {
@@ -352,6 +411,9 @@ type UseChatHelpers = {
352
411
  /** Additional data added on the server via StreamData */
353
412
  data?: JSONValue[];
354
413
  };
414
+ /**
415
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
416
+ */
355
417
  type StreamingReactResponseAction = (payload: {
356
418
  messages: Message[];
357
419
  data?: Record<string, string>;
@@ -359,7 +421,12 @@ type StreamingReactResponseAction = (payload: {
359
421
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
360
422
  api?: string | StreamingReactResponseAction;
361
423
  key?: string;
362
- }): UseChatHelpers;
424
+ }): UseChatHelpers & {
425
+ experimental_addToolResult: ({ toolCallId, result, }: {
426
+ toolCallId: string;
427
+ result: any;
428
+ }) => void;
429
+ };
363
430
 
364
431
  type UseCompletionHelpers = {
365
432
  /** The current completion result */
@@ -1,3 +1,45 @@
1
+ /**
2
+ Typed tool call that is returned by generateText and streamText.
3
+ It contains the tool call ID, the tool name, and the tool arguments.
4
+ */
5
+ interface ToolCall$1<NAME extends string, ARGS> {
6
+ /**
7
+ ID of the tool call. This ID is used to match the tool call with the tool result.
8
+ */
9
+ toolCallId: string;
10
+ /**
11
+ Name of the tool that is being called.
12
+ */
13
+ toolName: NAME;
14
+ /**
15
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
16
+ */
17
+ args: ARGS;
18
+ }
19
+
20
+ /**
21
+ Typed tool result that is returned by generateText and streamText.
22
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
23
+ */
24
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
25
+ /**
26
+ ID of the tool call. This ID is used to match the tool call with the tool result.
27
+ */
28
+ toolCallId: string;
29
+ /**
30
+ Name of the tool that was called.
31
+ */
32
+ toolName: NAME;
33
+ /**
34
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
35
+ */
36
+ args: ARGS;
37
+ /**
38
+ Result of the tool call. This is the result of the tool's execution.
39
+ */
40
+ result: RESULT;
41
+ }
42
+
1
43
  interface FunctionCall {
2
44
  /**
3
45
  * The arguments to call the function with, as generated by the model in JSON
@@ -66,6 +108,12 @@ interface Function {
66
108
  description?: string;
67
109
  }
68
110
  type IdGenerator = () => string;
111
+ /**
112
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
113
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
114
+ Once the call is complete, the invocation is a tool result.
115
+ */
116
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
69
117
  /**
70
118
  * Shared types between the API and UI packages.
71
119
  */
@@ -74,9 +122,13 @@ interface Message {
74
122
  tool_call_id?: string;
75
123
  createdAt?: Date;
76
124
  content: string;
125
+ /**
126
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
127
+ */
77
128
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
78
129
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
79
130
  /**
131
+ *
80
132
  * If the message has a role of `function`, the `name` field is the name of the function.
81
133
  * Otherwise, the name field should not be set.
82
134
  */
@@ -97,6 +149,11 @@ interface Message {
97
149
  * Additional message-specific information added on the server via StreamData
98
150
  */
99
151
  annotations?: JSONValue[] | undefined;
152
+ /**
153
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
154
+ that the assistant made as part of this message.
155
+ */
156
+ toolInvocations?: Array<ToolInvocation>;
100
157
  }
101
158
  type CreateMessage = Omit<Message, 'id'> & {
102
159
  id?: Message['id'];
@@ -297,7 +354,9 @@ declare class StreamData {
297
354
 
298
355
  type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
299
356
  /**
300
- * A utility class for streaming React responses.
357
+ A utility class for streaming React responses.
358
+
359
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
301
360
  */
302
361
  declare class experimental_StreamingReactResponse {
303
362
  constructor(res: ReadableStream, options?: {
@@ -352,6 +411,9 @@ type UseChatHelpers = {
352
411
  /** Additional data added on the server via StreamData */
353
412
  data?: JSONValue[];
354
413
  };
414
+ /**
415
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
416
+ */
355
417
  type StreamingReactResponseAction = (payload: {
356
418
  messages: Message[];
357
419
  data?: Record<string, string>;
@@ -359,7 +421,12 @@ type StreamingReactResponseAction = (payload: {
359
421
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
360
422
  api?: string | StreamingReactResponseAction;
361
423
  key?: string;
362
- }): UseChatHelpers;
424
+ }): UseChatHelpers & {
425
+ experimental_addToolResult: ({ toolCallId, result, }: {
426
+ toolCallId: string;
427
+ result: any;
428
+ }) => void;
429
+ };
363
430
 
364
431
  type UseCompletionHelpers = {
365
432
  /** The current completion result */
@@ -42,6 +42,13 @@ module.exports = __toCommonJS(react_exports);
42
42
  var import_react = require("react");
43
43
  var import_swr = __toESM(require("swr"));
44
44
 
45
+ // shared/generate-id.ts
46
+ var import_non_secure = require("nanoid/non-secure");
47
+ var generateId = (0, import_non_secure.customAlphabet)(
48
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
49
+ 7
50
+ );
51
+
45
52
  // shared/stream-parts.ts
46
53
  var textStreamPart = {
47
54
  code: "0",
@@ -138,7 +145,7 @@ var dataMessageStreamPart = {
138
145
  };
139
146
  }
140
147
  };
141
- var toolCallStreamPart = {
148
+ var toolCallsStreamPart = {
142
149
  code: "7",
143
150
  name: "tool_calls",
144
151
  parse: (value) => {
@@ -165,6 +172,36 @@ var messageAnnotationsStreamPart = {
165
172
  return { type: "message_annotations", value };
166
173
  }
167
174
  };
175
+ var toolCallStreamPart = {
176
+ code: "9",
177
+ name: "tool_call",
178
+ parse: (value) => {
179
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
180
+ throw new Error(
181
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
182
+ );
183
+ }
184
+ return {
185
+ type: "tool_call",
186
+ value
187
+ };
188
+ }
189
+ };
190
+ var toolResultStreamPart = {
191
+ code: "a",
192
+ name: "tool_result",
193
+ parse: (value) => {
194
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
195
+ throw new Error(
196
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
197
+ );
198
+ }
199
+ return {
200
+ type: "tool_result",
201
+ value
202
+ };
203
+ }
204
+ };
168
205
  var streamParts = [
169
206
  textStreamPart,
170
207
  functionCallStreamPart,
@@ -173,8 +210,10 @@ var streamParts = [
173
210
  assistantMessageStreamPart,
174
211
  assistantControlDataStreamPart,
175
212
  dataMessageStreamPart,
213
+ toolCallsStreamPart,
214
+ messageAnnotationsStreamPart,
176
215
  toolCallStreamPart,
177
- messageAnnotationsStreamPart
216
+ toolResultStreamPart
178
217
  ];
179
218
  var streamPartsByCode = {
180
219
  [textStreamPart.code]: textStreamPart,
@@ -184,8 +223,10 @@ var streamPartsByCode = {
184
223
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
185
224
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
186
225
  [dataMessageStreamPart.code]: dataMessageStreamPart,
226
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
227
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
187
228
  [toolCallStreamPart.code]: toolCallStreamPart,
188
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
229
+ [toolResultStreamPart.code]: toolResultStreamPart
189
230
  };
190
231
  var StreamStringPrefixes = {
191
232
  [textStreamPart.name]: textStreamPart.code,
@@ -195,8 +236,10 @@ var StreamStringPrefixes = {
195
236
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
196
237
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
197
238
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
239
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
240
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
198
241
  [toolCallStreamPart.name]: toolCallStreamPart.code,
199
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
242
+ [toolResultStreamPart.name]: toolResultStreamPart.code
200
243
  };
201
244
  var validCodes = streamParts.map((part) => part.code);
202
245
  var parseStreamPart = (line) => {
@@ -257,13 +300,6 @@ async function* readDataStream(reader, {
257
300
  }
258
301
  }
259
302
 
260
- // shared/generate-id.ts
261
- var import_non_secure = require("nanoid/non-secure");
262
- var generateId = (0, import_non_secure.customAlphabet)(
263
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
264
- 7
265
- );
266
-
267
303
  // shared/parse-complex-response.ts
268
304
  function assignAnnotationsToMessage(message, annotations) {
269
305
  if (!message || !annotations || !annotations.length)
@@ -301,6 +337,40 @@ async function parseComplexResponse({
301
337
  };
302
338
  }
303
339
  }
340
+ if (type === "tool_call") {
341
+ if (prefixMap.text == null) {
342
+ prefixMap.text = {
343
+ id: generateId2(),
344
+ role: "assistant",
345
+ content: "",
346
+ createdAt
347
+ };
348
+ }
349
+ if (prefixMap.text.toolInvocations == null) {
350
+ prefixMap.text.toolInvocations = [];
351
+ }
352
+ prefixMap.text.toolInvocations.push(value);
353
+ } else if (type === "tool_result") {
354
+ if (prefixMap.text == null) {
355
+ prefixMap.text = {
356
+ id: generateId2(),
357
+ role: "assistant",
358
+ content: "",
359
+ createdAt
360
+ };
361
+ }
362
+ if (prefixMap.text.toolInvocations == null) {
363
+ prefixMap.text.toolInvocations = [];
364
+ }
365
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
366
+ (invocation) => invocation.toolCallId === value.toolCallId
367
+ );
368
+ if (toolInvocationIndex !== -1) {
369
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
370
+ } else {
371
+ prefixMap.text.toolInvocations.push(value);
372
+ }
373
+ }
304
374
  let functionCallMessage = null;
305
375
  if (type === "function_call") {
306
376
  prefixMap["function_call"] = {
@@ -610,17 +680,23 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
610
680
  const previousMessages = messagesRef.current;
611
681
  mutate(chatRequest.messages, false);
612
682
  const constructedMessagesPayload = sendExtraMessageFields ? chatRequest.messages : chatRequest.messages.map(
613
- ({ role, content, name, function_call, tool_calls, tool_call_id }) => ({
683
+ ({
684
+ role,
685
+ content,
686
+ name,
687
+ toolInvocations,
688
+ function_call,
689
+ tool_calls,
690
+ tool_call_id
691
+ }) => ({
614
692
  role,
615
693
  content,
616
- tool_call_id,
617
694
  ...name !== void 0 && { name },
618
- ...function_call !== void 0 && {
619
- function_call
620
- },
621
- ...tool_calls !== void 0 && {
622
- tool_calls
623
- }
695
+ ...toolInvocations !== void 0 && { toolInvocations },
696
+ // outdated function/tool call handling (TODO deprecate):
697
+ tool_call_id,
698
+ ...function_call !== void 0 && { function_call },
699
+ ...tool_calls !== void 0 && { tool_calls }
624
700
  })
625
701
  );
626
702
  if (typeof api !== "string") {
@@ -919,7 +995,30 @@ function useChat({
919
995
  handleInputChange,
920
996
  handleSubmit,
921
997
  isLoading,
922
- data: streamData
998
+ data: streamData,
999
+ experimental_addToolResult: ({
1000
+ toolCallId,
1001
+ result
1002
+ }) => {
1003
+ const updatedMessages = messagesRef.current.map(
1004
+ (message, index, arr) => (
1005
+ // update the tool calls in the last assistant message:
1006
+ index === arr.length - 1 && message.role === "assistant" && message.toolInvocations ? {
1007
+ ...message,
1008
+ toolInvocations: message.toolInvocations.map(
1009
+ (toolInvocation) => toolInvocation.toolCallId === toolCallId ? { ...toolInvocation, result } : toolInvocation
1010
+ )
1011
+ } : message
1012
+ )
1013
+ );
1014
+ mutate(updatedMessages, false);
1015
+ const lastMessage = updatedMessages[updatedMessages.length - 1];
1016
+ if (lastMessage.role === "assistant" && lastMessage.toolInvocations && lastMessage.toolInvocations.length > 0 && lastMessage.toolInvocations.every(
1017
+ (toolInvocation) => "result" in toolInvocation
1018
+ )) {
1019
+ triggerRequest({ messages: updatedMessages });
1020
+ }
1021
+ }
923
1022
  };
924
1023
  }
925
1024