@ai-sdk/anthropic 4.0.84 → 4.0.86

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.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { APICallError, InvalidArgumentError, InvalidResponseDataError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
2
- import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertInlineFileDataToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonLinesResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderExecutedToolFactory, createProviderStreamError, createToolNameMapping, experimental_toolCaller, generateId, getFromApi, getTopLevelMediaType, isCustomReasoning, isNonNullable, isProviderStreamError, isRecord, lazySchema, loadApiKey, loadOptionalSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, normalizeBatchRequestCounts, normalizeHeaders, parseProviderOptions, postFormDataToApi, postJsonToApi, resolve, resolveFullMediaType, resolveProviderReference, safeValidateTypes, secureJsonParse, serializeModelOptions, validateBaseURL, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
2
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertInlineFileDataToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonLinesResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderExecutedToolFactory, createProviderStreamError, createToolNameMapping, encodePathSegment, experimental_toolCaller, generateId, getFromApi, getTopLevelMediaType, isCustomReasoning, isNonNullable, isProviderStreamError, isRecord, lazySchema, loadApiKey, loadOptionalSetting, mapReasoningToProviderBudget, mapReasoningToProviderEffort, normalizeBatchRequestCounts, normalizeHeaders, parseProviderOptions, postFormDataToApi, postJsonToApi, resolve, resolveFullMediaType, resolveProviderReference, safeValidateTypes, secureJsonParse, serializeModelOptions, validateBaseURL, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
3
3
  import { Experimental_DecisionLanguageModel } from "@ai-sdk/provider-utils/experimental-decision";
4
4
  import { z } from "zod/v4";
5
5
  //#region src/anthropic-error.ts
@@ -96,18 +96,19 @@ function mapAnthropicCitation(citation, documents) {
96
96
  }
97
97
  //#endregion
98
98
  //#region src/anthropic-api.ts
99
- const anthropicFallbackContentSchema = z.object({
99
+ const anthropicFallbackContentSchema = () => z.object({
100
100
  type: z.literal("fallback"),
101
101
  from: z.object({ model: z.string() }),
102
102
  to: z.object({ model: z.string() })
103
103
  });
104
- const anthropicStopDetailsSchema = z.object({
104
+ const anthropicFallbackContentSchemaValidator = lazySchema(() => zodSchema(anthropicFallbackContentSchema()));
105
+ const anthropicStopDetailsSchema = () => z.object({
105
106
  type: z.string(),
106
107
  category: z.string().nullish(),
107
108
  explanation: z.string().nullish(),
108
109
  recommended_model: z.string().nullish()
109
110
  });
110
- const anthropicSafeguardResultSchema = z.object({
111
+ const anthropicSafeguardResultSchema = () => z.object({
111
112
  type: z.string(),
112
113
  status: z.object({
113
114
  type: z.string(),
@@ -118,7 +119,7 @@ const anthropicSafeguardResultSchema = z.object({
118
119
  })).nullish()
119
120
  })
120
121
  });
121
- const anthropicToolCallCallerSchema = z.union([
122
+ const anthropicToolCallCallerSchema = () => z.union([
122
123
  z.object({
123
124
  type: z.literal("code_execution_20250825"),
124
125
  tool_id: z.string()
@@ -129,7 +130,7 @@ const anthropicToolCallCallerSchema = z.union([
129
130
  }),
130
131
  z.object({ type: z.literal("direct") })
131
132
  ]);
132
- const anthropicCitationSchema = z.discriminatedUnion("type", [
133
+ const anthropicCitationSchema = () => z.discriminatedUnion("type", [
133
134
  z.object({
134
135
  type: z.literal("web_search_result_location"),
135
136
  cited_text: z.string(),
@@ -174,12 +175,12 @@ const anthropicCitationSchema = z.discriminatedUnion("type", [
174
175
  end_block_index: z.number()
175
176
  })
176
177
  ]);
177
- const anthropicMcpToolResultContentSchema = z.union([z.string(), z.array(z.union([z.string(), z.object({
178
+ const anthropicMcpToolResultContentSchema = () => z.union([z.string(), z.array(z.union([z.string(), z.object({
178
179
  type: z.literal("text"),
179
180
  text: z.string(),
180
181
  citations: z.array(z.json()).nullable().optional()
181
182
  })]))]);
182
- const anthropicInputTransformationSchema = z.object({
183
+ const anthropicInputTransformationSchema = () => z.object({
183
184
  type: z.string(),
184
185
  path: z.string(),
185
186
  reason: z.string()
@@ -192,7 +193,7 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
192
193
  z.object({
193
194
  type: z.literal("text"),
194
195
  text: z.string(),
195
- citations: z.array(anthropicCitationSchema).nullable().optional()
196
+ citations: z.array(anthropicCitationSchema()).nullable().optional()
196
197
  }),
197
198
  z.object({
198
199
  type: z.literal("thinking"),
@@ -213,7 +214,7 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
213
214
  id: z.string(),
214
215
  name: z.string(),
215
216
  input: z.unknown(),
216
- caller: anthropicToolCallCallerSchema.optional(),
217
+ caller: anthropicToolCallCallerSchema().optional(),
217
218
  toolset_name: z.string().nullish()
218
219
  }),
219
220
  z.object({
@@ -221,7 +222,7 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
221
222
  id: z.string(),
222
223
  name: z.string(),
223
224
  input: z.record(z.string(), z.unknown()).nullish(),
224
- caller: anthropicToolCallCallerSchema.optional()
225
+ caller: anthropicToolCallCallerSchema().optional()
225
226
  }),
226
227
  z.object({
227
228
  type: z.literal("mcp_tool_use"),
@@ -234,12 +235,12 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
234
235
  type: z.literal("mcp_tool_result"),
235
236
  tool_use_id: z.string(),
236
237
  is_error: z.boolean(),
237
- content: anthropicMcpToolResultContentSchema
238
+ content: anthropicMcpToolResultContentSchema()
238
239
  }),
239
240
  z.object({
240
241
  type: z.literal("web_fetch_tool_result"),
241
242
  tool_use_id: z.string(),
242
- caller: anthropicToolCallCallerSchema.optional(),
243
+ caller: anthropicToolCallCallerSchema().optional(),
243
244
  content: z.union([z.object({
244
245
  type: z.literal("web_fetch_result"),
245
246
  url: z.string(),
@@ -266,7 +267,7 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
266
267
  z.object({
267
268
  type: z.literal("web_search_tool_result"),
268
269
  tool_use_id: z.string(),
269
- caller: anthropicToolCallCallerSchema.optional(),
270
+ caller: anthropicToolCallCallerSchema().optional(),
270
271
  content: z.union([z.array(z.object({
271
272
  type: z.literal("web_search_result"),
272
273
  url: z.string(),
@@ -393,13 +394,13 @@ const anthropicResponseSchema = lazySchema(() => zodSchema(z.object({
393
394
  })
394
395
  ])
395
396
  }),
396
- anthropicFallbackContentSchema
397
+ anthropicFallbackContentSchema()
397
398
  ])),
398
399
  stop_reason: z.string().nullish(),
399
400
  stop_sequence: z.string().nullish(),
400
- stop_details: anthropicStopDetailsSchema.nullish(),
401
- input_transformations: z.array(anthropicInputTransformationSchema).nullish(),
402
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
401
+ stop_details: anthropicStopDetailsSchema().nullish(),
402
+ input_transformations: z.array(anthropicInputTransformationSchema()).nullish(),
403
+ safeguard_results: z.array(anthropicSafeguardResultSchema()).nullish(),
403
404
  usage: z.looseObject({
404
405
  input_tokens: z.number(),
405
406
  output_tokens: z.number(),
@@ -460,11 +461,11 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
460
461
  id: z.string(),
461
462
  name: z.string(),
462
463
  input: z.unknown(),
463
- caller: anthropicToolCallCallerSchema.optional(),
464
+ caller: anthropicToolCallCallerSchema().optional(),
464
465
  toolset_name: z.string().nullish()
465
466
  })])).nullish(),
466
467
  stop_reason: z.string().nullish(),
467
- input_transformations: z.array(anthropicInputTransformationSchema).nullish(),
468
+ input_transformations: z.array(anthropicInputTransformationSchema()).nullish(),
468
469
  container: z.object({
469
470
  expires_at: z.string(),
470
471
  id: z.string()
@@ -488,7 +489,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
488
489
  id: z.string(),
489
490
  name: z.string(),
490
491
  input: z.record(z.string(), z.unknown()).optional(),
491
- caller: anthropicToolCallCallerSchema.optional(),
492
+ caller: anthropicToolCallCallerSchema().optional(),
492
493
  toolset_name: z.string().nullish()
493
494
  }),
494
495
  z.object({
@@ -505,7 +506,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
505
506
  id: z.string(),
506
507
  name: z.string(),
507
508
  input: z.record(z.string(), z.unknown()).nullish(),
508
- caller: anthropicToolCallCallerSchema.optional()
509
+ caller: anthropicToolCallCallerSchema().optional()
509
510
  }),
510
511
  z.object({
511
512
  type: z.literal("mcp_tool_use"),
@@ -518,12 +519,12 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
518
519
  type: z.literal("mcp_tool_result"),
519
520
  tool_use_id: z.string(),
520
521
  is_error: z.boolean(),
521
- content: anthropicMcpToolResultContentSchema
522
+ content: anthropicMcpToolResultContentSchema()
522
523
  }),
523
524
  z.object({
524
525
  type: z.literal("web_fetch_tool_result"),
525
526
  tool_use_id: z.string(),
526
- caller: anthropicToolCallCallerSchema.optional(),
527
+ caller: anthropicToolCallCallerSchema().optional(),
527
528
  content: z.union([z.object({
528
529
  type: z.literal("web_fetch_result"),
529
530
  url: z.string(),
@@ -550,7 +551,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
550
551
  z.object({
551
552
  type: z.literal("web_search_tool_result"),
552
553
  tool_use_id: z.string(),
553
- caller: anthropicToolCallCallerSchema.optional(),
554
+ caller: anthropicToolCallCallerSchema().optional(),
554
555
  content: z.union([z.array(z.object({
555
556
  type: z.literal("web_search_result"),
556
557
  url: z.string(),
@@ -673,7 +674,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
673
674
  })
674
675
  ])
675
676
  }),
676
- anthropicFallbackContentSchema
677
+ anthropicFallbackContentSchema()
677
678
  ])
678
679
  }),
679
680
  z.object({
@@ -702,7 +703,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
702
703
  }),
703
704
  z.object({
704
705
  type: z.literal("citations_delta"),
705
- citation: anthropicCitationSchema
706
+ citation: anthropicCitationSchema()
706
707
  })
707
708
  ])
708
709
  }),
@@ -726,8 +727,8 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
726
727
  delta: z.object({
727
728
  stop_reason: z.string().nullish(),
728
729
  stop_sequence: z.string().nullish(),
729
- stop_details: anthropicStopDetailsSchema.nullish(),
730
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
730
+ stop_details: anthropicStopDetailsSchema().nullish(),
731
+ safeguard_results: z.array(anthropicSafeguardResultSchema()).nullish(),
731
732
  container: z.object({
732
733
  expires_at: z.string(),
733
734
  id: z.string(),
@@ -758,7 +759,7 @@ const anthropicChunkSchema = lazySchema(() => zodSchema(z.discriminatedUnion("ty
758
759
  cache_read_input_tokens: z.number().nullish()
759
760
  })).nullish()
760
761
  }),
761
- input_transformations: z.array(anthropicInputTransformationSchema).nullish(),
762
+ input_transformations: z.array(anthropicInputTransformationSchema()).nullish(),
762
763
  context_management: z.object({ applied_edits: z.array(z.union([
763
764
  z.object({
764
765
  type: z.literal("clear_tool_uses_20250919"),
@@ -786,7 +787,7 @@ const anthropicReasoningMetadataSchema = lazySchema(() => zodSchema(z.object({
786
787
  * Anthropic file part provider options for document-specific features.
787
788
  * These options apply to individual file parts (documents).
788
789
  */
789
- const anthropicFilePartProviderOptions = z.object({
790
+ const anthropicFilePartProviderOptions = lazySchema(() => zodSchema(z.object({
790
791
  /**
791
792
  * Upload this file into the code execution container instead of sending it as
792
793
  * a normal document or image content block.
@@ -812,11 +813,11 @@ enabled: z.boolean() }).optional(),
812
813
  * Useful for storing document metadata as text or stringified JSON.
813
814
  */
814
815
  context: z.string().optional()
815
- });
816
+ })));
816
817
  /**
817
818
  * Anthropic provider options for system messages.
818
819
  */
819
- const anthropicSystemMessageProviderOptions = z.object({
820
+ const anthropicSystemMessageProviderOptions = lazySchema(() => zodSchema(z.object({
820
821
  /**
821
822
  * Controls when an ephemeral mid-conversation system message is cleared.
822
823
  *
@@ -859,8 +860,8 @@ const anthropicSystemMessageProviderOptions = z.object({
859
860
  type: z.literal("tool_removal"),
860
861
  toolName: z.string()
861
862
  })])).optional()
862
- });
863
- const anthropicLanguageModelOptions = z.object({
863
+ })));
864
+ const anthropicLanguageModelOptions = lazySchema(() => zodSchema(z.object({
864
865
  /**
865
866
  * Whether to send reasoning to the model.
866
867
  *
@@ -1131,7 +1132,7 @@ userId: z.string().optional() }).optional(),
1131
1132
  instructions: z.string().optional()
1132
1133
  })
1133
1134
  ])) }).optional()
1134
- });
1135
+ })));
1135
1136
  //#endregion
1136
1137
  //#region src/get-cache-control.ts
1137
1138
  const MAX_CACHE_BREAKPOINTS = 4;
@@ -2745,7 +2746,7 @@ async function convertToAnthropicPrompt({ prompt, sendReasoning, warnings, cache
2745
2746
  if (part.kind !== "anthropic.fallback") break;
2746
2747
  const fallbackMetadata = await safeValidateTypes({
2747
2748
  value: part.providerOptions?.anthropic,
2748
- schema: anthropicFallbackContentSchema
2749
+ schema: anthropicFallbackContentSchemaValidator
2749
2750
  });
2750
2751
  if (!fallbackMetadata.success) {
2751
2752
  warnings.push({
@@ -4502,7 +4503,7 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
4502
4503
  let hasInvalidMessageSequence = false;
4503
4504
  let blockType = void 0;
4504
4505
  const generateId = this.generateId;
4505
- const [streamForFirstChunk, streamForConsumer] = response.pipeThrough(new TransformStream({
4506
+ const transformedStream = response.pipeThrough(new TransformStream({
4506
4507
  start(controller) {
4507
4508
  controller.enqueue({
4508
4509
  type: "stream-start",
@@ -5222,6 +5223,55 @@ var AnthropicLanguageModel = class AnthropicLanguageModel {
5222
5223
  default: throw new Error(`Unsupported chunk type: ${value}`);
5223
5224
  }
5224
5225
  }
5226
+ }));
5227
+ let hasOutput = false;
5228
+ let hasStreamError = false;
5229
+ let lastFinish;
5230
+ const [streamForFirstChunk, streamForConsumer] = transformedStream.pipeThrough(new TransformStream({
5231
+ transform(chunk, controller) {
5232
+ switch (chunk.type) {
5233
+ case "response-metadata":
5234
+ hasOutput = false;
5235
+ break;
5236
+ case "finish":
5237
+ hasOutput = false;
5238
+ lastFinish = chunk;
5239
+ break;
5240
+ case "error":
5241
+ hasStreamError = true;
5242
+ break;
5243
+ case "text-delta":
5244
+ case "reasoning-delta":
5245
+ case "tool-input-delta":
5246
+ hasOutput ||= chunk.delta.length > 0;
5247
+ break;
5248
+ case "tool-call": hasOutput = true;
5249
+ }
5250
+ controller.enqueue(chunk);
5251
+ },
5252
+ flush(controller) {
5253
+ if (hasOutput && !hasStreamError) controller.enqueue({
5254
+ type: "finish",
5255
+ finishReason: {
5256
+ unified: "interrupted",
5257
+ raw: void 0
5258
+ },
5259
+ usage: lastFinish?.usage ?? {
5260
+ inputTokens: {
5261
+ total: void 0,
5262
+ noCache: void 0,
5263
+ cacheRead: void 0,
5264
+ cacheWrite: void 0
5265
+ },
5266
+ outputTokens: {
5267
+ total: void 0,
5268
+ text: void 0,
5269
+ reasoning: void 0
5270
+ }
5271
+ },
5272
+ ...lastFinish?.providerMetadata != null ? { providerMetadata: lastFinish.providerMetadata } : {}
5273
+ });
5274
+ }
5225
5275
  })).tee();
5226
5276
  const firstChunkReader = streamForFirstChunk.getReader();
5227
5277
  try {
@@ -5557,7 +5607,7 @@ function mapAnthropicStopDetails$1(stopDetails) {
5557
5607
  //#endregion
5558
5608
  //#region src/anthropic-batch.ts
5559
5609
  const anthropicBatchRequestIdPattern = /^[A-Za-z0-9_-]{1,64}$/;
5560
- const anthropicBatchProviderOptionsSchema = anthropicLanguageModelOptions.pick({ anthropicBeta: true });
5610
+ const anthropicBatchProviderOptionsSchema = lazySchema(() => zodSchema(z.object({ anthropicBeta: z.array(z.string()).optional() })));
5561
5611
  const anthropicBatchResponseZodSchema = () => z.object({
5562
5612
  id: z.string(),
5563
5613
  type: z.literal("message_batch"),
@@ -6903,10 +6953,6 @@ const anthropicSkillVersionResponseSchema = lazySchema(() => zodSchema(z.object(
6903
6953
  })));
6904
6954
  //#endregion
6905
6955
  //#region src/skills/anthropic-skills.ts
6906
- function encodePathSegment(value) {
6907
- const encodedValue = encodeURIComponent(value);
6908
- return encodedValue === "." ? "%252E" : encodedValue === ".." ? "%252E%252E" : encodedValue;
6909
- }
6910
6956
  var AnthropicSkills = class {
6911
6957
  get provider() {
6912
6958
  return this.config.provider;
@@ -6973,7 +7019,7 @@ var AnthropicSkills = class {
6973
7019
  };
6974
7020
  //#endregion
6975
7021
  //#region src/version.ts
6976
- const VERSION = "4.0.84";
7022
+ const VERSION = "4.0.86";
6977
7023
  //#endregion
6978
7024
  //#region src/anthropic-provider.ts
6979
7025
  const ANTHROPIC_API_URL = "https://api.anthropic.com";