ai 3.1.6 → 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.6",
3
+ "version": "3.1.7",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -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
  */
@@ -57,6 +105,11 @@ interface Message {
57
105
  * Additional message-specific information added on the server via StreamData
58
106
  */
59
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>;
60
113
  }
61
114
  type JSONValue = null | string | number | boolean | {
62
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
  */
@@ -57,6 +105,11 @@ interface Message {
57
105
  * Additional message-specific information added on the server via StreamData
58
106
  */
59
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>;
60
113
  }
61
114
  type JSONValue = null | string | number | boolean | {
62
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
  */
@@ -101,6 +149,11 @@ interface Message {
101
149
  * Additional message-specific information added on the server via StreamData
102
150
  */
103
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>;
104
157
  }
105
158
  type CreateMessage = Omit<Message, 'id'> & {
106
159
  id?: Message['id'];
@@ -368,7 +421,12 @@ type StreamingReactResponseAction = (payload: {
368
421
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
369
422
  api?: string | StreamingReactResponseAction;
370
423
  key?: string;
371
- }): UseChatHelpers;
424
+ }): UseChatHelpers & {
425
+ experimental_addToolResult: ({ toolCallId, result, }: {
426
+ toolCallId: string;
427
+ result: any;
428
+ }) => void;
429
+ };
372
430
 
373
431
  type UseCompletionHelpers = {
374
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
  */
@@ -101,6 +149,11 @@ interface Message {
101
149
  * Additional message-specific information added on the server via StreamData
102
150
  */
103
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>;
104
157
  }
105
158
  type CreateMessage = Omit<Message, 'id'> & {
106
159
  id?: Message['id'];
@@ -368,7 +421,12 @@ type StreamingReactResponseAction = (payload: {
368
421
  declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
369
422
  api?: string | StreamingReactResponseAction;
370
423
  key?: string;
371
- }): UseChatHelpers;
424
+ }): UseChatHelpers & {
425
+ experimental_addToolResult: ({ toolCallId, result, }: {
426
+ toolCallId: string;
427
+ result: any;
428
+ }) => void;
429
+ };
372
430
 
373
431
  type UseCompletionHelpers = {
374
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