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.
@@ -1,5 +1,47 @@
1
1
  import { Resource, Accessor, Setter } from 'solid-js';
2
2
 
3
+ /**
4
+ Typed tool call that is returned by generateText and streamText.
5
+ It contains the tool call ID, the tool name, and the tool arguments.
6
+ */
7
+ interface ToolCall$1<NAME extends string, ARGS> {
8
+ /**
9
+ ID of the tool call. This ID is used to match the tool call with the tool result.
10
+ */
11
+ toolCallId: string;
12
+ /**
13
+ Name of the tool that is being called.
14
+ */
15
+ toolName: NAME;
16
+ /**
17
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
18
+ */
19
+ args: ARGS;
20
+ }
21
+
22
+ /**
23
+ Typed tool result that is returned by generateText and streamText.
24
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
25
+ */
26
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
27
+ /**
28
+ ID of the tool call. This ID is used to match the tool call with the tool result.
29
+ */
30
+ toolCallId: string;
31
+ /**
32
+ Name of the tool that was called.
33
+ */
34
+ toolName: NAME;
35
+ /**
36
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
37
+ */
38
+ args: ARGS;
39
+ /**
40
+ Result of the tool call. This is the result of the tool's execution.
41
+ */
42
+ result: RESULT;
43
+ }
44
+
3
45
  interface FunctionCall {
4
46
  /**
5
47
  * The arguments to call the function with, as generated by the model in JSON
@@ -68,6 +110,12 @@ interface Function {
68
110
  description?: string;
69
111
  }
70
112
  type IdGenerator = () => string;
113
+ /**
114
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
115
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
116
+ Once the call is complete, the invocation is a tool result.
117
+ */
118
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
71
119
  /**
72
120
  * Shared types between the API and UI packages.
73
121
  */
@@ -76,9 +124,13 @@ interface Message {
76
124
  tool_call_id?: string;
77
125
  createdAt?: Date;
78
126
  content: string;
127
+ /**
128
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
129
+ */
79
130
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
80
131
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
81
132
  /**
133
+ *
82
134
  * If the message has a role of `function`, the `name` field is the name of the function.
83
135
  * Otherwise, the name field should not be set.
84
136
  */
@@ -99,6 +151,11 @@ interface Message {
99
151
  * Additional message-specific information added on the server via StreamData
100
152
  */
101
153
  annotations?: JSONValue[] | undefined;
154
+ /**
155
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
156
+ that the assistant made as part of this message.
157
+ */
158
+ toolInvocations?: Array<ToolInvocation>;
102
159
  }
103
160
  type CreateMessage = Omit<Message, 'id'> & {
104
161
  id?: Message['id'];
@@ -1,5 +1,47 @@
1
1
  import { Resource, Accessor, Setter } from 'solid-js';
2
2
 
3
+ /**
4
+ Typed tool call that is returned by generateText and streamText.
5
+ It contains the tool call ID, the tool name, and the tool arguments.
6
+ */
7
+ interface ToolCall$1<NAME extends string, ARGS> {
8
+ /**
9
+ ID of the tool call. This ID is used to match the tool call with the tool result.
10
+ */
11
+ toolCallId: string;
12
+ /**
13
+ Name of the tool that is being called.
14
+ */
15
+ toolName: NAME;
16
+ /**
17
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
18
+ */
19
+ args: ARGS;
20
+ }
21
+
22
+ /**
23
+ Typed tool result that is returned by generateText and streamText.
24
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
25
+ */
26
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
27
+ /**
28
+ ID of the tool call. This ID is used to match the tool call with the tool result.
29
+ */
30
+ toolCallId: string;
31
+ /**
32
+ Name of the tool that was called.
33
+ */
34
+ toolName: NAME;
35
+ /**
36
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
37
+ */
38
+ args: ARGS;
39
+ /**
40
+ Result of the tool call. This is the result of the tool's execution.
41
+ */
42
+ result: RESULT;
43
+ }
44
+
3
45
  interface FunctionCall {
4
46
  /**
5
47
  * The arguments to call the function with, as generated by the model in JSON
@@ -68,6 +110,12 @@ interface Function {
68
110
  description?: string;
69
111
  }
70
112
  type IdGenerator = () => string;
113
+ /**
114
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
115
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
116
+ Once the call is complete, the invocation is a tool result.
117
+ */
118
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
71
119
  /**
72
120
  * Shared types between the API and UI packages.
73
121
  */
@@ -76,9 +124,13 @@ interface Message {
76
124
  tool_call_id?: string;
77
125
  createdAt?: Date;
78
126
  content: string;
127
+ /**
128
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
129
+ */
79
130
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
80
131
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
81
132
  /**
133
+ *
82
134
  * If the message has a role of `function`, the `name` field is the name of the function.
83
135
  * Otherwise, the name field should not be set.
84
136
  */
@@ -99,6 +151,11 @@ interface Message {
99
151
  * Additional message-specific information added on the server via StreamData
100
152
  */
101
153
  annotations?: JSONValue[] | undefined;
154
+ /**
155
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
156
+ that the assistant made as part of this message.
157
+ */
158
+ toolInvocations?: Array<ToolInvocation>;
102
159
  }
103
160
  type CreateMessage = Omit<Message, 'id'> & {
104
161
  id?: Message['id'];
@@ -30,6 +30,13 @@ var import_solid_js = require("solid-js");
30
30
  var import_solid_swr_store = require("solid-swr-store");
31
31
  var import_swr_store = require("swr-store");
32
32
 
33
+ // shared/generate-id.ts
34
+ var import_non_secure = require("nanoid/non-secure");
35
+ var generateId = (0, import_non_secure.customAlphabet)(
36
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
37
+ 7
38
+ );
39
+
33
40
  // shared/stream-parts.ts
34
41
  var textStreamPart = {
35
42
  code: "0",
@@ -126,7 +133,7 @@ var dataMessageStreamPart = {
126
133
  };
127
134
  }
128
135
  };
129
- var toolCallStreamPart = {
136
+ var toolCallsStreamPart = {
130
137
  code: "7",
131
138
  name: "tool_calls",
132
139
  parse: (value) => {
@@ -153,6 +160,36 @@ var messageAnnotationsStreamPart = {
153
160
  return { type: "message_annotations", value };
154
161
  }
155
162
  };
163
+ var toolCallStreamPart = {
164
+ code: "9",
165
+ name: "tool_call",
166
+ parse: (value) => {
167
+ 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") {
168
+ throw new Error(
169
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
170
+ );
171
+ }
172
+ return {
173
+ type: "tool_call",
174
+ value
175
+ };
176
+ }
177
+ };
178
+ var toolResultStreamPart = {
179
+ code: "a",
180
+ name: "tool_result",
181
+ parse: (value) => {
182
+ 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)) {
183
+ throw new Error(
184
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
185
+ );
186
+ }
187
+ return {
188
+ type: "tool_result",
189
+ value
190
+ };
191
+ }
192
+ };
156
193
  var streamParts = [
157
194
  textStreamPart,
158
195
  functionCallStreamPart,
@@ -161,8 +198,10 @@ var streamParts = [
161
198
  assistantMessageStreamPart,
162
199
  assistantControlDataStreamPart,
163
200
  dataMessageStreamPart,
201
+ toolCallsStreamPart,
202
+ messageAnnotationsStreamPart,
164
203
  toolCallStreamPart,
165
- messageAnnotationsStreamPart
204
+ toolResultStreamPart
166
205
  ];
167
206
  var streamPartsByCode = {
168
207
  [textStreamPart.code]: textStreamPart,
@@ -172,8 +211,10 @@ var streamPartsByCode = {
172
211
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
173
212
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
174
213
  [dataMessageStreamPart.code]: dataMessageStreamPart,
214
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
215
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
175
216
  [toolCallStreamPart.code]: toolCallStreamPart,
176
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
217
+ [toolResultStreamPart.code]: toolResultStreamPart
177
218
  };
178
219
  var StreamStringPrefixes = {
179
220
  [textStreamPart.name]: textStreamPart.code,
@@ -183,8 +224,10 @@ var StreamStringPrefixes = {
183
224
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
184
225
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
185
226
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
227
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
228
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
186
229
  [toolCallStreamPart.name]: toolCallStreamPart.code,
187
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
230
+ [toolResultStreamPart.name]: toolResultStreamPart.code
188
231
  };
189
232
  var validCodes = streamParts.map((part) => part.code);
190
233
  var parseStreamPart = (line) => {
@@ -245,13 +288,6 @@ async function* readDataStream(reader, {
245
288
  }
246
289
  }
247
290
 
248
- // shared/generate-id.ts
249
- var import_non_secure = require("nanoid/non-secure");
250
- var generateId = (0, import_non_secure.customAlphabet)(
251
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
252
- 7
253
- );
254
-
255
291
  // shared/parse-complex-response.ts
256
292
  function assignAnnotationsToMessage(message, annotations) {
257
293
  if (!message || !annotations || !annotations.length)
@@ -289,6 +325,40 @@ async function parseComplexResponse({
289
325
  };
290
326
  }
291
327
  }
328
+ if (type === "tool_call") {
329
+ if (prefixMap.text == null) {
330
+ prefixMap.text = {
331
+ id: generateId2(),
332
+ role: "assistant",
333
+ content: "",
334
+ createdAt
335
+ };
336
+ }
337
+ if (prefixMap.text.toolInvocations == null) {
338
+ prefixMap.text.toolInvocations = [];
339
+ }
340
+ prefixMap.text.toolInvocations.push(value);
341
+ } else if (type === "tool_result") {
342
+ if (prefixMap.text == null) {
343
+ prefixMap.text = {
344
+ id: generateId2(),
345
+ role: "assistant",
346
+ content: "",
347
+ createdAt
348
+ };
349
+ }
350
+ if (prefixMap.text.toolInvocations == null) {
351
+ prefixMap.text.toolInvocations = [];
352
+ }
353
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
354
+ (invocation) => invocation.toolCallId === value.toolCallId
355
+ );
356
+ if (toolInvocationIndex !== -1) {
357
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
358
+ } else {
359
+ prefixMap.text.toolInvocations.push(value);
360
+ }
361
+ }
292
362
  let functionCallMessage = null;
293
363
  if (type === "function_call") {
294
364
  prefixMap["function_call"] = {