ai 5.0.6 → 5.0.8

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/dist/index.mjs CHANGED
@@ -17,7 +17,8 @@ import {
17
17
 
18
18
  // src/generate-text/generate-text.ts
19
19
  import {
20
- createIdGenerator
20
+ createIdGenerator,
21
+ getErrorMessage as getErrorMessage5
21
22
  } from "@ai-sdk/provider-utils";
22
23
 
23
24
  // src/error/no-output-specified-error.ts
@@ -1691,38 +1692,50 @@ async function parseToolCall({
1691
1692
  system,
1692
1693
  messages
1693
1694
  }) {
1694
- if (tools == null) {
1695
- throw new NoSuchToolError({ toolName: toolCall.toolName });
1696
- }
1697
1695
  try {
1698
- return await doParseToolCall({ toolCall, tools });
1699
- } catch (error) {
1700
- if (repairToolCall == null || !(NoSuchToolError.isInstance(error) || InvalidToolInputError.isInstance(error))) {
1701
- throw error;
1696
+ if (tools == null) {
1697
+ throw new NoSuchToolError({ toolName: toolCall.toolName });
1702
1698
  }
1703
- let repairedToolCall = null;
1704
1699
  try {
1705
- repairedToolCall = await repairToolCall({
1706
- toolCall,
1707
- tools,
1708
- inputSchema: ({ toolName }) => {
1709
- const { inputSchema } = tools[toolName];
1710
- return asSchema2(inputSchema).jsonSchema;
1711
- },
1712
- system,
1713
- messages,
1714
- error
1715
- });
1716
- } catch (repairError) {
1717
- throw new ToolCallRepairError({
1718
- cause: repairError,
1719
- originalError: error
1720
- });
1721
- }
1722
- if (repairedToolCall == null) {
1723
- throw error;
1700
+ return await doParseToolCall({ toolCall, tools });
1701
+ } catch (error) {
1702
+ if (repairToolCall == null || !(NoSuchToolError.isInstance(error) || InvalidToolInputError.isInstance(error))) {
1703
+ throw error;
1704
+ }
1705
+ let repairedToolCall = null;
1706
+ try {
1707
+ repairedToolCall = await repairToolCall({
1708
+ toolCall,
1709
+ tools,
1710
+ inputSchema: ({ toolName }) => {
1711
+ const { inputSchema } = tools[toolName];
1712
+ return asSchema2(inputSchema).jsonSchema;
1713
+ },
1714
+ system,
1715
+ messages,
1716
+ error
1717
+ });
1718
+ } catch (repairError) {
1719
+ throw new ToolCallRepairError({
1720
+ cause: repairError,
1721
+ originalError: error
1722
+ });
1723
+ }
1724
+ if (repairedToolCall == null) {
1725
+ throw error;
1726
+ }
1727
+ return await doParseToolCall({ toolCall: repairedToolCall, tools });
1724
1728
  }
1725
- return await doParseToolCall({ toolCall: repairedToolCall, tools });
1729
+ } catch (error) {
1730
+ return {
1731
+ type: "tool-call",
1732
+ toolCallId: toolCall.toolCallId,
1733
+ toolName: toolCall.toolName,
1734
+ input: toolCall.input,
1735
+ dynamic: true,
1736
+ invalid: true,
1737
+ error
1738
+ };
1726
1739
  }
1727
1740
  }
1728
1741
  async function doParseToolCall({
@@ -2174,6 +2187,9 @@ async function generateText({
2174
2187
  )
2175
2188
  );
2176
2189
  for (const toolCall of stepToolCalls) {
2190
+ if (toolCall.invalid) {
2191
+ continue;
2192
+ }
2177
2193
  const tool3 = tools[toolCall.toolName];
2178
2194
  if ((tool3 == null ? void 0 : tool3.onInputAvailable) != null) {
2179
2195
  await tool3.onInputAvailable({
@@ -2185,18 +2201,38 @@ async function generateText({
2185
2201
  });
2186
2202
  }
2187
2203
  }
2204
+ const invalidToolCalls = stepToolCalls.filter(
2205
+ (toolCall) => toolCall.invalid && toolCall.dynamic
2206
+ );
2207
+ clientToolOutputs = [];
2208
+ for (const toolCall of invalidToolCalls) {
2209
+ clientToolOutputs.push({
2210
+ type: "tool-error",
2211
+ toolCallId: toolCall.toolCallId,
2212
+ toolName: toolCall.toolName,
2213
+ input: toolCall.input,
2214
+ error: getErrorMessage5(toolCall.error),
2215
+ dynamic: true
2216
+ });
2217
+ }
2188
2218
  clientToolCalls = stepToolCalls.filter(
2189
- (toolCall) => toolCall.providerExecuted !== true
2219
+ (toolCall) => !toolCall.providerExecuted
2190
2220
  );
2191
- clientToolOutputs = tools == null ? [] : await executeTools({
2192
- toolCalls: clientToolCalls,
2193
- tools,
2194
- tracer,
2195
- telemetry,
2196
- messages: stepInputMessages,
2197
- abortSignal,
2198
- experimental_context
2199
- });
2221
+ if (tools != null) {
2222
+ clientToolOutputs.push(
2223
+ ...await executeTools({
2224
+ toolCalls: clientToolCalls.filter(
2225
+ (toolCall) => !toolCall.invalid
2226
+ ),
2227
+ tools,
2228
+ tracer,
2229
+ telemetry,
2230
+ messages: stepInputMessages,
2231
+ abortSignal,
2232
+ experimental_context
2233
+ })
2234
+ );
2235
+ }
2200
2236
  const stepContent = asContent({
2201
2237
  content: currentModelResponse.content,
2202
2238
  toolCalls: stepToolCalls,
@@ -2505,7 +2541,7 @@ function asContent({
2505
2541
 
2506
2542
  // src/generate-text/stream-text.ts
2507
2543
  import {
2508
- getErrorMessage as getErrorMessage5
2544
+ getErrorMessage as getErrorMessage7
2509
2545
  } from "@ai-sdk/provider";
2510
2546
  import {
2511
2547
  createIdGenerator as createIdGenerator2,
@@ -2695,6 +2731,16 @@ var uiMessageChunkSchema = z7.union([
2695
2731
  providerMetadata: providerMetadataSchema.optional(),
2696
2732
  dynamic: z7.boolean().optional()
2697
2733
  }),
2734
+ z7.strictObject({
2735
+ type: z7.literal("tool-input-error"),
2736
+ toolCallId: z7.string(),
2737
+ toolName: z7.string(),
2738
+ input: z7.unknown(),
2739
+ providerExecuted: z7.boolean().optional(),
2740
+ providerMetadata: providerMetadataSchema.optional(),
2741
+ dynamic: z7.boolean().optional(),
2742
+ errorText: z7.string()
2743
+ }),
2698
2744
  z7.strictObject({
2699
2745
  type: z7.literal("tool-output-available"),
2700
2746
  toolCallId: z7.string(),
@@ -3234,6 +3280,7 @@ function processUIMessageStream({
3234
3280
  anyPart.input = anyOptions.input;
3235
3281
  anyPart.output = anyOptions.output;
3236
3282
  anyPart.errorText = anyOptions.errorText;
3283
+ anyPart.rawInput = anyOptions.rawInput;
3237
3284
  anyPart.providerExecuted = (_a17 = anyOptions.providerExecuted) != null ? _a17 : part.providerExecuted;
3238
3285
  if (anyOptions.providerMetadata != null && part.state === "input-available") {
3239
3286
  part.callProviderMetadata = anyOptions.providerMetadata;
@@ -3245,6 +3292,7 @@ function processUIMessageStream({
3245
3292
  state: options.state,
3246
3293
  input: anyOptions.input,
3247
3294
  output: anyOptions.output,
3295
+ rawInput: anyOptions.rawInput,
3248
3296
  errorText: anyOptions.errorText,
3249
3297
  providerExecuted: anyOptions.providerExecuted,
3250
3298
  ...anyOptions.providerMetadata != null ? { callProviderMetadata: anyOptions.providerMetadata } : {}
@@ -3252,6 +3300,7 @@ function processUIMessageStream({
3252
3300
  }
3253
3301
  }
3254
3302
  function updateDynamicToolPart(options) {
3303
+ var _a17;
3255
3304
  const part = state.message.parts.find(
3256
3305
  (part2) => part2.type === "dynamic-tool" && part2.toolCallId === options.toolCallId
3257
3306
  );
@@ -3263,6 +3312,7 @@ function processUIMessageStream({
3263
3312
  anyPart.input = anyOptions.input;
3264
3313
  anyPart.output = anyOptions.output;
3265
3314
  anyPart.errorText = anyOptions.errorText;
3315
+ anyPart.rawInput = (_a17 = anyOptions.rawInput) != null ? _a17 : anyPart.rawInput;
3266
3316
  if (anyOptions.providerMetadata != null && part.state === "input-available") {
3267
3317
  part.callProviderMetadata = anyOptions.providerMetadata;
3268
3318
  }
@@ -3456,6 +3506,31 @@ function processUIMessageStream({
3456
3506
  }
3457
3507
  break;
3458
3508
  }
3509
+ case "tool-input-error": {
3510
+ if (chunk.dynamic) {
3511
+ updateDynamicToolPart({
3512
+ toolCallId: chunk.toolCallId,
3513
+ toolName: chunk.toolName,
3514
+ state: "output-error",
3515
+ input: chunk.input,
3516
+ errorText: chunk.errorText,
3517
+ providerMetadata: chunk.providerMetadata
3518
+ });
3519
+ } else {
3520
+ updateToolPart({
3521
+ toolCallId: chunk.toolCallId,
3522
+ toolName: chunk.toolName,
3523
+ state: "output-error",
3524
+ input: void 0,
3525
+ rawInput: chunk.input,
3526
+ errorText: chunk.errorText,
3527
+ providerExecuted: chunk.providerExecuted,
3528
+ providerMetadata: chunk.providerMetadata
3529
+ });
3530
+ }
3531
+ write();
3532
+ break;
3533
+ }
3459
3534
  case "tool-output-available": {
3460
3535
  if (chunk.dynamic) {
3461
3536
  const toolInvocation = getDynamicToolInvocation(
@@ -3501,6 +3576,7 @@ function processUIMessageStream({
3501
3576
  toolName: getToolName(toolInvocation),
3502
3577
  state: "output-error",
3503
3578
  input: toolInvocation.input,
3579
+ rawInput: toolInvocation.rawInput,
3504
3580
  errorText: chunk.errorText
3505
3581
  });
3506
3582
  }
@@ -3870,7 +3946,10 @@ function now() {
3870
3946
  }
3871
3947
 
3872
3948
  // src/generate-text/run-tools-transformation.ts
3873
- import { generateId } from "@ai-sdk/provider-utils";
3949
+ import {
3950
+ generateId,
3951
+ getErrorMessage as getErrorMessage6
3952
+ } from "@ai-sdk/provider-utils";
3874
3953
  function runToolsTransformation({
3875
3954
  tools,
3876
3955
  generatorStream,
@@ -3950,6 +4029,17 @@ function runToolsTransformation({
3950
4029
  messages
3951
4030
  });
3952
4031
  controller.enqueue(toolCall);
4032
+ if (toolCall.invalid) {
4033
+ toolResultsStreamController.enqueue({
4034
+ type: "tool-error",
4035
+ toolCallId: toolCall.toolCallId,
4036
+ toolName: toolCall.toolName,
4037
+ input: toolCall.input,
4038
+ error: getErrorMessage6(toolCall.error),
4039
+ dynamic: true
4040
+ });
4041
+ break;
4042
+ }
3953
4043
  const tool3 = tools[toolCall.toolName];
3954
4044
  toolInputs.set(toolCall.toolCallId, toolCall.input);
3955
4045
  if (tool3.onInputAvailable != null) {
@@ -4262,6 +4352,7 @@ var DefaultStreamTextResult = class {
4262
4352
  this._steps = new DelayedPromise();
4263
4353
  this.output = output;
4264
4354
  this.includeRawChunks = includeRawChunks;
4355
+ this.tools = tools;
4265
4356
  let stepFinish;
4266
4357
  let recordedContent = [];
4267
4358
  const recordedResponseMessages = [];
@@ -5064,12 +5155,19 @@ var DefaultStreamTextResult = class {
5064
5155
  sendSources = false,
5065
5156
  sendStart = true,
5066
5157
  sendFinish = true,
5067
- onError = getErrorMessage5
5158
+ onError = getErrorMessage7
5068
5159
  } = {}) {
5069
5160
  const responseMessageId = generateMessageId != null ? getResponseUIMessageId({
5070
5161
  originalMessages,
5071
5162
  responseMessageId: generateMessageId
5072
5163
  }) : void 0;
5164
+ const toolNamesByCallId = {};
5165
+ const isDynamic = (toolCallId) => {
5166
+ var _a16, _b;
5167
+ const toolName = toolNamesByCallId[toolCallId];
5168
+ const dynamic = ((_b = (_a16 = this.tools) == null ? void 0 : _a16[toolName]) == null ? void 0 : _b.type) === "dynamic";
5169
+ return dynamic ? true : void 0;
5170
+ };
5073
5171
  const baseStream = this.fullStream.pipeThrough(
5074
5172
  new TransformStream({
5075
5173
  transform: async (part, controller) => {
@@ -5159,12 +5257,14 @@ var DefaultStreamTextResult = class {
5159
5257
  break;
5160
5258
  }
5161
5259
  case "tool-input-start": {
5260
+ toolNamesByCallId[part.id] = part.toolName;
5261
+ const dynamic = isDynamic(part.id);
5162
5262
  controller.enqueue({
5163
5263
  type: "tool-input-start",
5164
5264
  toolCallId: part.id,
5165
5265
  toolName: part.toolName,
5166
5266
  ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5167
- ...part.dynamic != null ? { dynamic: part.dynamic } : {}
5267
+ ...dynamic != null ? { dynamic } : {}
5168
5268
  });
5169
5269
  break;
5170
5270
  }
@@ -5177,34 +5277,51 @@ var DefaultStreamTextResult = class {
5177
5277
  break;
5178
5278
  }
5179
5279
  case "tool-call": {
5180
- controller.enqueue({
5181
- type: "tool-input-available",
5182
- toolCallId: part.toolCallId,
5183
- toolName: part.toolName,
5184
- input: part.input,
5185
- ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5186
- ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
5187
- ...part.dynamic != null ? { dynamic: part.dynamic } : {}
5188
- });
5280
+ toolNamesByCallId[part.toolCallId] = part.toolName;
5281
+ const dynamic = isDynamic(part.toolCallId);
5282
+ if (part.invalid) {
5283
+ controller.enqueue({
5284
+ type: "tool-input-error",
5285
+ toolCallId: part.toolCallId,
5286
+ toolName: part.toolName,
5287
+ input: part.input,
5288
+ ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5289
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
5290
+ ...dynamic != null ? { dynamic } : {},
5291
+ errorText: onError(part.error)
5292
+ });
5293
+ } else {
5294
+ controller.enqueue({
5295
+ type: "tool-input-available",
5296
+ toolCallId: part.toolCallId,
5297
+ toolName: part.toolName,
5298
+ input: part.input,
5299
+ ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5300
+ ...part.providerMetadata != null ? { providerMetadata: part.providerMetadata } : {},
5301
+ ...dynamic != null ? { dynamic } : {}
5302
+ });
5303
+ }
5189
5304
  break;
5190
5305
  }
5191
5306
  case "tool-result": {
5307
+ const dynamic = isDynamic(part.toolCallId);
5192
5308
  controller.enqueue({
5193
5309
  type: "tool-output-available",
5194
5310
  toolCallId: part.toolCallId,
5195
5311
  output: part.output,
5196
5312
  ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5197
- ...part.dynamic != null ? { dynamic: part.dynamic } : {}
5313
+ ...dynamic != null ? { dynamic } : {}
5198
5314
  });
5199
5315
  break;
5200
5316
  }
5201
5317
  case "tool-error": {
5318
+ const dynamic = isDynamic(part.toolCallId);
5202
5319
  controller.enqueue({
5203
5320
  type: "tool-output-error",
5204
5321
  toolCallId: part.toolCallId,
5205
5322
  errorText: onError(part.error),
5206
5323
  ...part.providerExecuted != null ? { providerExecuted: part.providerExecuted } : {},
5207
- ...part.dynamic != null ? { dynamic: part.dynamic } : {}
5324
+ ...dynamic != null ? { dynamic } : {}
5208
5325
  });
5209
5326
  break;
5210
5327
  }
@@ -9253,7 +9370,7 @@ function convertToModelMessages(messages, options) {
9253
9370
  case "assistant": {
9254
9371
  if (message.parts != null) {
9255
9372
  let processBlock2 = function() {
9256
- var _a16;
9373
+ var _a16, _b;
9257
9374
  if (block.length === 0) {
9258
9375
  return;
9259
9376
  }
@@ -9305,7 +9422,7 @@ function convertToModelMessages(messages, options) {
9305
9422
  type: "tool-call",
9306
9423
  toolCallId: part.toolCallId,
9307
9424
  toolName,
9308
- input: part.input,
9425
+ input: part.state === "output-error" ? (_a16 = part.input) != null ? _a16 : part.rawInput : part.input,
9309
9426
  providerExecuted: part.providerExecuted,
9310
9427
  ...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
9311
9428
  });
@@ -9316,7 +9433,7 @@ function convertToModelMessages(messages, options) {
9316
9433
  toolName,
9317
9434
  output: createToolModelOutput({
9318
9435
  output: part.state === "output-error" ? part.errorText : part.output,
9319
- tool: (_a16 = options == null ? void 0 : options.tools) == null ? void 0 : _a16[toolName],
9436
+ tool: (_b = options == null ? void 0 : options.tools) == null ? void 0 : _b[toolName],
9320
9437
  errorMode: part.state === "output-error" ? "json" : "none"
9321
9438
  })
9322
9439
  });
@@ -9449,11 +9566,11 @@ var TextStreamChatTransport = class extends HttpChatTransport {
9449
9566
  // src/ui-message-stream/create-ui-message-stream.ts
9450
9567
  import {
9451
9568
  generateId as generateIdFunc2,
9452
- getErrorMessage as getErrorMessage6
9569
+ getErrorMessage as getErrorMessage8
9453
9570
  } from "@ai-sdk/provider-utils";
9454
9571
  function createUIMessageStream({
9455
9572
  execute,
9456
- onError = getErrorMessage6,
9573
+ onError = getErrorMessage8,
9457
9574
  originalMessages,
9458
9575
  onFinish,
9459
9576
  generateId: generateId3 = generateIdFunc2