@ai-sdk/anthropic 4.0.45 → 4.0.47

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
@@ -71,7 +71,9 @@ var AnthropicFiles = class {
71
71
  async uploadFile({
72
72
  data,
73
73
  mediaType,
74
- filename
74
+ filename,
75
+ abortSignal,
76
+ headers
75
77
  }) {
76
78
  var _a, _b;
77
79
  const fileBytes = convertInlineFileDataToUint8Array(data);
@@ -84,14 +86,17 @@ var AnthropicFiles = class {
84
86
  }
85
87
  const { value: response } = await postFormDataToApi({
86
88
  url: `${this.config.baseURL}/files`,
87
- headers: combineHeaders(this.config.headers(), {
88
- "anthropic-beta": "files-api-2025-04-14"
89
- }),
89
+ headers: combineHeaders(
90
+ this.config.headers(),
91
+ { "anthropic-beta": "files-api-2025-04-14" },
92
+ headers
93
+ ),
90
94
  formData,
91
95
  failedResponseHandler: anthropicFailedResponseHandler,
92
96
  successfulResponseHandler: createJsonResponseHandler(
93
97
  anthropicUploadFileResponseSchema
94
98
  ),
99
+ abortSignal,
95
100
  fetch: this.config.fetch
96
101
  });
97
102
  return {
@@ -124,6 +129,7 @@ import {
124
129
  createJsonLinesResponseHandler,
125
130
  createJsonResponseHandler as createJsonResponseHandler3,
126
131
  getFromApi,
132
+ isRecord as isRecord2,
127
133
  lazySchema as lazySchema14,
128
134
  normalizeBatchRequestCounts,
129
135
  normalizeHeaders,
@@ -162,6 +168,66 @@ var anthropicToolCallCallerSchema = z3.union([
162
168
  type: z3.literal("direct")
163
169
  })
164
170
  ]);
171
+ var anthropicCitationSchema = z3.discriminatedUnion("type", [
172
+ z3.object({
173
+ type: z3.literal("web_search_result_location"),
174
+ cited_text: z3.string(),
175
+ url: z3.string(),
176
+ title: z3.string().nullable(),
177
+ encrypted_index: z3.string()
178
+ }),
179
+ z3.object({
180
+ type: z3.literal("page_location"),
181
+ cited_text: z3.string(),
182
+ document_index: z3.number(),
183
+ document_title: z3.string().nullable(),
184
+ start_page_number: z3.number(),
185
+ end_page_number: z3.number(),
186
+ file_id: z3.string().nullish()
187
+ }),
188
+ z3.object({
189
+ type: z3.literal("char_location"),
190
+ cited_text: z3.string(),
191
+ document_index: z3.number(),
192
+ document_title: z3.string().nullable(),
193
+ start_char_index: z3.number(),
194
+ end_char_index: z3.number(),
195
+ file_id: z3.string().nullish()
196
+ }),
197
+ z3.object({
198
+ type: z3.literal("content_block_location"),
199
+ cited_text: z3.string(),
200
+ document_index: z3.number(),
201
+ document_title: z3.string().nullable(),
202
+ start_block_index: z3.number(),
203
+ end_block_index: z3.number(),
204
+ file_id: z3.string().nullable()
205
+ }),
206
+ z3.object({
207
+ type: z3.literal("search_result_location"),
208
+ cited_text: z3.string(),
209
+ search_result_index: z3.number(),
210
+ source: z3.string(),
211
+ title: z3.string().nullable(),
212
+ start_block_index: z3.number(),
213
+ end_block_index: z3.number()
214
+ })
215
+ ]);
216
+ var anthropicMcpToolResultContentSchema = z3.union([
217
+ z3.string(),
218
+ z3.array(
219
+ z3.union([
220
+ z3.string(),
221
+ z3.object({
222
+ type: z3.literal("text"),
223
+ text: z3.string(),
224
+ // MCP tool-result citations are opaque third-party pass-through data.
225
+ // Keep them permissive so new shapes do not reject whole responses.
226
+ citations: z3.array(z3.json()).nullable().optional()
227
+ })
228
+ ])
229
+ )
230
+ ]);
165
231
  var anthropicResponseSchema = lazySchema3(
166
232
  () => zodSchema3(
167
233
  z3.object({
@@ -173,33 +239,7 @@ var anthropicResponseSchema = lazySchema3(
173
239
  z3.object({
174
240
  type: z3.literal("text"),
175
241
  text: z3.string(),
176
- citations: z3.array(
177
- z3.discriminatedUnion("type", [
178
- z3.object({
179
- type: z3.literal("web_search_result_location"),
180
- cited_text: z3.string(),
181
- url: z3.string(),
182
- title: z3.string(),
183
- encrypted_index: z3.string()
184
- }),
185
- z3.object({
186
- type: z3.literal("page_location"),
187
- cited_text: z3.string(),
188
- document_index: z3.number(),
189
- document_title: z3.string().nullable(),
190
- start_page_number: z3.number(),
191
- end_page_number: z3.number()
192
- }),
193
- z3.object({
194
- type: z3.literal("char_location"),
195
- cited_text: z3.string(),
196
- document_index: z3.number(),
197
- document_title: z3.string().nullable(),
198
- start_char_index: z3.number(),
199
- end_char_index: z3.number()
200
- })
201
- ])
202
- ).optional()
242
+ citations: z3.array(anthropicCitationSchema).nullable().optional()
203
243
  }),
204
244
  z3.object({
205
245
  type: z3.literal("thinking"),
@@ -240,12 +280,7 @@ var anthropicResponseSchema = lazySchema3(
240
280
  type: z3.literal("mcp_tool_result"),
241
281
  tool_use_id: z3.string(),
242
282
  is_error: z3.boolean(),
243
- content: z3.array(
244
- z3.union([
245
- z3.string(),
246
- z3.object({ type: z3.literal("text"), text: z3.string() })
247
- ])
248
- )
283
+ content: anthropicMcpToolResultContentSchema
249
284
  }),
250
285
  z3.object({
251
286
  type: z3.literal("web_fetch_tool_result"),
@@ -255,7 +290,7 @@ var anthropicResponseSchema = lazySchema3(
255
290
  z3.object({
256
291
  type: z3.literal("web_fetch_result"),
257
292
  url: z3.string(),
258
- retrieved_at: z3.string(),
293
+ retrieved_at: z3.string().nullable(),
259
294
  content: z3.object({
260
295
  type: z3.literal("document"),
261
296
  title: z3.string().nullable(),
@@ -289,7 +324,7 @@ var anthropicResponseSchema = lazySchema3(
289
324
  z3.object({
290
325
  type: z3.literal("web_search_result"),
291
326
  url: z3.string(),
292
- title: z3.string(),
327
+ title: z3.string().nullable(),
293
328
  encrypted_content: z3.string(),
294
329
  page_age: z3.string().nullish()
295
330
  })
@@ -411,6 +446,10 @@ var anthropicResponseSchema = lazySchema3(
411
446
  })
412
447
  ])
413
448
  }),
449
+ z3.object({
450
+ type: z3.literal("container_upload"),
451
+ file_id: z3.string()
452
+ }),
414
453
  // advisor results for advisor_20260301:
415
454
  z3.object({
416
455
  type: z3.literal("advisor_tool_result"),
@@ -580,12 +619,7 @@ var anthropicChunkSchema = lazySchema3(
580
619
  type: z3.literal("mcp_tool_result"),
581
620
  tool_use_id: z3.string(),
582
621
  is_error: z3.boolean(),
583
- content: z3.array(
584
- z3.union([
585
- z3.string(),
586
- z3.object({ type: z3.literal("text"), text: z3.string() })
587
- ])
588
- )
622
+ content: anthropicMcpToolResultContentSchema
589
623
  }),
590
624
  z3.object({
591
625
  type: z3.literal("web_fetch_tool_result"),
@@ -595,7 +629,7 @@ var anthropicChunkSchema = lazySchema3(
595
629
  z3.object({
596
630
  type: z3.literal("web_fetch_result"),
597
631
  url: z3.string(),
598
- retrieved_at: z3.string(),
632
+ retrieved_at: z3.string().nullable(),
599
633
  content: z3.object({
600
634
  type: z3.literal("document"),
601
635
  title: z3.string().nullable(),
@@ -629,7 +663,7 @@ var anthropicChunkSchema = lazySchema3(
629
663
  z3.object({
630
664
  type: z3.literal("web_search_result"),
631
665
  url: z3.string(),
632
- title: z3.string(),
666
+ title: z3.string().nullable(),
633
667
  encrypted_content: z3.string(),
634
668
  page_age: z3.string().nullish()
635
669
  })
@@ -805,31 +839,7 @@ var anthropicChunkSchema = lazySchema3(
805
839
  }),
806
840
  z3.object({
807
841
  type: z3.literal("citations_delta"),
808
- citation: z3.discriminatedUnion("type", [
809
- z3.object({
810
- type: z3.literal("web_search_result_location"),
811
- cited_text: z3.string(),
812
- url: z3.string(),
813
- title: z3.string(),
814
- encrypted_index: z3.string()
815
- }),
816
- z3.object({
817
- type: z3.literal("page_location"),
818
- cited_text: z3.string(),
819
- document_index: z3.number(),
820
- document_title: z3.string().nullable(),
821
- start_page_number: z3.number(),
822
- end_page_number: z3.number()
823
- }),
824
- z3.object({
825
- type: z3.literal("char_location"),
826
- cited_text: z3.string(),
827
- document_index: z3.number(),
828
- document_title: z3.string().nullable(),
829
- start_char_index: z3.number(),
830
- end_char_index: z3.number()
831
- })
832
- ])
842
+ citation: anthropicCitationSchema
833
843
  })
834
844
  ])
835
845
  }),
@@ -1151,6 +1161,8 @@ var anthropicLanguageModelOptions = z4.object({
1151
1161
  * Only supported with claude-opus-4-6.
1152
1162
  */
1153
1163
  speed: z4.enum(["fast", "standard"]).optional(),
1164
+ /** Selects Anthropic's service tier for the request. */
1165
+ serviceTier: z4.enum(["auto", "standard_only"]).optional(),
1154
1166
  /**
1155
1167
  * Controls where model inference runs for this request.
1156
1168
  *
@@ -3654,14 +3666,14 @@ function getAnthropicStreamErrorMetadata(type) {
3654
3666
  }
3655
3667
  }
3656
3668
  function createCitationSource(citation, citationDocuments, generateId3) {
3657
- var _a;
3669
+ var _a, _b;
3658
3670
  if (citation.type === "web_search_result_location") {
3659
3671
  return {
3660
3672
  type: "source",
3661
3673
  sourceType: "url",
3662
3674
  id: generateId3(),
3663
3675
  url: citation.url,
3664
- title: citation.title,
3676
+ title: (_a = citation.title) != null ? _a : void 0,
3665
3677
  providerMetadata: {
3666
3678
  anthropic: {
3667
3679
  citedText: citation.cited_text,
@@ -3682,7 +3694,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
3682
3694
  sourceType: "document",
3683
3695
  id: generateId3(),
3684
3696
  mediaType: documentInfo.mediaType,
3685
- title: (_a = citation.document_title) != null ? _a : documentInfo.title,
3697
+ title: (_b = citation.document_title) != null ? _b : documentInfo.title,
3686
3698
  filename: documentInfo.filename,
3687
3699
  providerMetadata: {
3688
3700
  anthropic: citation.type === "page_location" ? {
@@ -3979,6 +3991,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
3979
3991
  ...(anthropicOptions == null ? void 0 : anthropicOptions.speed) && {
3980
3992
  speed: anthropicOptions.speed
3981
3993
  },
3994
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.serviceTier) && {
3995
+ service_tier: anthropicOptions.serviceTier
3996
+ },
3982
3997
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
3983
3998
  inference_geo: anthropicOptions.inferenceGeo
3984
3999
  },
@@ -4366,6 +4381,14 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4366
4381
  });
4367
4382
  break;
4368
4383
  }
4384
+ case "container_upload": {
4385
+ content.push({
4386
+ type: "custom",
4387
+ kind: "anthropic.container_upload",
4388
+ providerMetadata: { anthropic: { fileId: part.file_id } }
4389
+ });
4390
+ break;
4391
+ }
4369
4392
  case "compaction": {
4370
4393
  content.push({
4371
4394
  type: "text",
@@ -4532,7 +4555,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4532
4555
  var _a2;
4533
4556
  return {
4534
4557
  url: result.url,
4535
- title: result.title,
4558
+ ...result.title != null ? { title: result.title } : {},
4536
4559
  pageAge: (_a2 = result.page_age) != null ? _a2 : null,
4537
4560
  encryptedContent: result.encrypted_content,
4538
4561
  type: result.type
@@ -4546,7 +4569,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4546
4569
  sourceType: "url",
4547
4570
  id: this.generateId(),
4548
4571
  url: result.url,
4549
- title: result.title,
4572
+ ...result.title != null ? { title: result.title } : {},
4550
4573
  providerMetadata: {
4551
4574
  anthropic: {
4552
4575
  pageAge: (_c = result.page_age) != null ? _c : null
@@ -5084,7 +5107,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5084
5107
  var _a3;
5085
5108
  return {
5086
5109
  url: result.url,
5087
- title: result.title,
5110
+ ...result.title != null ? { title: result.title } : {},
5088
5111
  pageAge: (_a3 = result.page_age) != null ? _a3 : null,
5089
5112
  encryptedContent: result.encrypted_content,
5090
5113
  type: result.type
@@ -5098,7 +5121,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5098
5121
  sourceType: "url",
5099
5122
  id: generateId3(),
5100
5123
  url: result.url,
5101
- title: result.title,
5124
+ ...result.title != null ? { title: result.title } : {},
5102
5125
  providerMetadata: {
5103
5126
  anthropic: {
5104
5127
  pageAge: (_b2 = result.page_age) != null ? _b2 : null
@@ -5813,7 +5836,7 @@ function hasWebTool20260209WithoutCodeExecution(tools) {
5813
5836
  hasWebTool20260209 = true;
5814
5837
  continue;
5815
5838
  }
5816
- if (tool.name === "code_execution") {
5839
+ if ("type" in tool && (tool.type === "code_execution_20250522" || tool.type === "code_execution_20250825" || tool.type === "code_execution_20260120")) {
5817
5840
  hasCodeExecutionTool = true;
5818
5841
  break;
5819
5842
  }
@@ -5919,33 +5942,59 @@ var anthropicBatchResponseSchema = lazySchema14(
5919
5942
  created_at: z15.string(),
5920
5943
  expires_at: z15.string(),
5921
5944
  archived_at: z15.string().nullish(),
5945
+ cancel_initiated_at: z15.string().nullish(),
5946
+ ended_at: z15.string().nullish(),
5922
5947
  results_url: z15.string().nullish()
5923
5948
  })
5924
5949
  )
5925
5950
  );
5951
+ var knownAnthropicBatchContentTypes = /* @__PURE__ */ new Set([
5952
+ "advisor_tool_result",
5953
+ "bash_code_execution_tool_result",
5954
+ "code_execution_tool_result",
5955
+ "compaction",
5956
+ "container_upload",
5957
+ "fallback",
5958
+ "mcp_tool_result",
5959
+ "mcp_tool_use",
5960
+ "redacted_thinking",
5961
+ "server_tool_use",
5962
+ "text",
5963
+ "text_editor_code_execution_tool_result",
5964
+ "thinking",
5965
+ "tool_search_tool_result",
5966
+ "tool_use",
5967
+ "web_fetch_tool_result",
5968
+ "web_search_tool_result"
5969
+ ]);
5970
+ var anthropicBatchResultSchema = lazySchema14(
5971
+ () => zodSchema14(
5972
+ z15.discriminatedUnion("type", [
5973
+ z15.object({
5974
+ type: z15.literal("succeeded"),
5975
+ message: z15.unknown()
5976
+ }),
5977
+ z15.object({
5978
+ type: z15.literal("errored"),
5979
+ error: z15.object({
5980
+ type: z15.literal("error"),
5981
+ error: z15.object({
5982
+ type: z15.string(),
5983
+ message: z15.string()
5984
+ }),
5985
+ request_id: z15.string().nullish()
5986
+ })
5987
+ }),
5988
+ z15.object({ type: z15.literal("canceled") }),
5989
+ z15.object({ type: z15.literal("expired") })
5990
+ ])
5991
+ )
5992
+ );
5926
5993
  var anthropicBatchResultLineSchema = lazySchema14(
5927
5994
  () => zodSchema14(
5928
5995
  z15.object({
5929
5996
  custom_id: z15.string(),
5930
- result: z15.discriminatedUnion("type", [
5931
- z15.object({
5932
- type: z15.literal("succeeded"),
5933
- message: z15.unknown()
5934
- }),
5935
- z15.object({
5936
- type: z15.literal("errored"),
5937
- error: z15.object({
5938
- type: z15.literal("error"),
5939
- error: z15.object({
5940
- type: z15.string(),
5941
- message: z15.string()
5942
- }),
5943
- request_id: z15.string().nullish()
5944
- })
5945
- }),
5946
- z15.object({ type: z15.literal("canceled") }),
5947
- z15.object({ type: z15.literal("expired") })
5948
- ])
5997
+ result: z15.unknown()
5949
5998
  })
5950
5999
  )
5951
6000
  );
@@ -5969,6 +6018,7 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
5969
6018
  abortSignal,
5970
6019
  webhookUrl
5971
6020
  }) {
6021
+ var _a;
5972
6022
  validateRequestIds(requests);
5973
6023
  const explicitBatchBetas = new Set(
5974
6024
  await getAnthropicBatchProviderBetas({
@@ -6003,6 +6053,21 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
6003
6053
  stream: false,
6004
6054
  userSuppliedBetas: new Set(explicitBatchBetas)
6005
6055
  });
6056
+ if (prepared.usesJsonResponseTool) {
6057
+ throw new UnsupportedFunctionalityError3({
6058
+ functionality: "batch responseFormat JSON-tool fallback",
6059
+ message: `Anthropic Message Batches cannot decode the JSON-tool structured-output fallback (request "${request.id}") because batch results are retrieved independently of the start call. Use a model that supports native output_format structured outputs.`
6060
+ });
6061
+ }
6062
+ const aliasedProviderTool = (_a = request.options.tools) == null ? void 0 : _a.find(
6063
+ (tool) => tool.type === "provider" && prepared.toolNameMapping.toProviderToolName(tool.name) !== tool.name
6064
+ );
6065
+ if (aliasedProviderTool != null) {
6066
+ throw new UnsupportedFunctionalityError3({
6067
+ functionality: "aliased provider tool names in batches",
6068
+ message: `Anthropic Message Batches cannot restore the custom provider-tool name "${aliasedProviderTool.name}" when results are retrieved independently of the start call (request "${request.id}"). Use the provider's canonical tool name.`
6069
+ });
6070
+ }
6006
6071
  const body = this.transformRequestBody(prepared.args, prepared.betas);
6007
6072
  validateAnthropicBatchBody({
6008
6073
  body,
@@ -6089,7 +6154,7 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
6089
6154
  }
6090
6155
  async *iterateBatchResults(lines) {
6091
6156
  for await (const line of lines) {
6092
- yield await convertAnthropicBatchResult(line);
6157
+ yield await convertAnthropicBatchResult(line, this.generateId);
6093
6158
  }
6094
6159
  }
6095
6160
  getBatchUrl(path) {
@@ -6174,13 +6239,23 @@ function validateAnthropicBatchBody({
6174
6239
  }
6175
6240
  }
6176
6241
  function convertAnthropicBatchStatus(batch) {
6242
+ var _a, _b, _c, _d;
6177
6243
  const requestCounts = convertAnthropicRequestCounts(batch.request_counts);
6178
6244
  return {
6179
6245
  status: mapAnthropicBatchStatus(batch.processing_status),
6180
6246
  rawStatus: batch.processing_status,
6181
6247
  ...requestCounts != null ? { requestCounts } : {},
6182
6248
  createdAt: batch.created_at,
6183
- expiresAt: batch.expires_at
6249
+ expiresAt: batch.expires_at,
6250
+ providerMetadata: {
6251
+ anthropic: {
6252
+ archivedAt: (_a = batch.archived_at) != null ? _a : null,
6253
+ cancelInitiatedAt: (_b = batch.cancel_initiated_at) != null ? _b : null,
6254
+ endedAt: (_c = batch.ended_at) != null ? _c : null,
6255
+ requestCounts: batch.request_counts,
6256
+ resultsUrl: (_d = batch.results_url) != null ? _d : null
6257
+ }
6258
+ }
6184
6259
  };
6185
6260
  }
6186
6261
  function mapAnthropicBatchStatus(rawStatus) {
@@ -6201,20 +6276,28 @@ function convertAnthropicRequestCounts(counts) {
6201
6276
  failed: counts.errored + counts.canceled + counts.expired
6202
6277
  });
6203
6278
  }
6204
- async function convertAnthropicBatchResult(line) {
6205
- switch (line.result.type) {
6279
+ async function convertAnthropicBatchResult(line, generateId3) {
6280
+ const resultValidation = await safeValidateTypes({
6281
+ value: line.result,
6282
+ schema: anthropicBatchResultSchema
6283
+ });
6284
+ if (!resultValidation.success) {
6285
+ return invalidAnthropicBatchResult(line.custom_id);
6286
+ }
6287
+ const result = resultValidation.value;
6288
+ switch (result.type) {
6206
6289
  case "canceled":
6207
6290
  return { id: line.custom_id, status: "cancelled" };
6208
6291
  case "expired":
6209
6292
  return { id: line.custom_id, status: "expired" };
6210
6293
  case "errored": {
6211
- const requestId = line.result.error.request_id;
6294
+ const requestId = result.error.request_id;
6212
6295
  return {
6213
6296
  id: line.custom_id,
6214
6297
  status: "failed",
6215
6298
  error: {
6216
- message: line.result.error.error.message,
6217
- type: line.result.error.error.type
6299
+ message: result.error.error.message,
6300
+ type: result.error.error.type
6218
6301
  },
6219
6302
  ...requestId != null ? {
6220
6303
  providerMetadata: {
@@ -6224,58 +6307,84 @@ async function convertAnthropicBatchResult(line) {
6224
6307
  };
6225
6308
  }
6226
6309
  case "succeeded": {
6227
- const validation = await safeValidateTypes({
6228
- value: line.result.message,
6229
- schema: anthropicResponseSchema
6230
- });
6231
- if (!validation.success) {
6232
- return {
6233
- id: line.custom_id,
6234
- status: "failed",
6235
- error: {
6236
- message: "Anthropic returned an invalid Message batch result.",
6237
- code: "invalid_response"
6238
- }
6239
- };
6240
- }
6241
- const response = validation.value;
6242
- const unsupportedPart = response.content.find(
6243
- (part) => !supportedBatchContentTypes.has(part.type)
6244
- );
6245
- if (unsupportedPart != null) {
6246
- return {
6247
- id: line.custom_id,
6248
- status: "failed",
6249
- error: {
6250
- message: `Anthropic returned a "${unsupportedPart.type}" content block, but tool content is not supported in AI SDK text batches.`,
6251
- code: "unsupported_content"
6252
- }
6253
- };
6310
+ const response = await parseAnthropicBatchResponse(result.message);
6311
+ if (response == null) {
6312
+ return invalidAnthropicBatchResult(line.custom_id);
6254
6313
  }
6255
6314
  return {
6256
6315
  id: line.custom_id,
6257
6316
  status: "succeeded",
6258
- result: convertAnthropicBatchResponse(response)
6317
+ result: convertAnthropicBatchResponse(response, generateId3)
6259
6318
  };
6260
6319
  }
6261
6320
  }
6262
6321
  }
6263
- var supportedBatchContentTypes = /* @__PURE__ */ new Set([
6264
- "text",
6265
- "thinking",
6266
- "redacted_thinking",
6267
- "compaction",
6268
- // The normal Anthropic response conversion intentionally drops this marker;
6269
- // the fallback hop remains available through usage.iterations metadata.
6270
- "fallback"
6271
- ]);
6272
- function convertAnthropicBatchResponse(response) {
6273
- var _a, _b, _c;
6322
+ function invalidAnthropicBatchResult(id) {
6323
+ return {
6324
+ id,
6325
+ status: "failed",
6326
+ error: {
6327
+ message: "Anthropic returned an invalid Message batch result.",
6328
+ code: "invalid_response"
6329
+ }
6330
+ };
6331
+ }
6332
+ async function parseAnthropicBatchResponse(message) {
6333
+ const validation = await safeValidateTypes({
6334
+ value: message,
6335
+ schema: anthropicResponseSchema
6336
+ });
6337
+ if (validation.success) {
6338
+ return validation.value;
6339
+ }
6340
+ if (!isRecord2(message) || !Array.isArray(message.content)) {
6341
+ return void 0;
6342
+ }
6274
6343
  const content = [];
6344
+ for (const part of message.content) {
6345
+ const partValidation = await safeValidateTypes({
6346
+ value: { ...message, content: [part] },
6347
+ schema: anthropicResponseSchema
6348
+ });
6349
+ if (partValidation.success) {
6350
+ const [validatedPart] = partValidation.value.content;
6351
+ if (validatedPart != null) {
6352
+ content.push(validatedPart);
6353
+ }
6354
+ } else if (!isRecord2(part) || typeof part.type !== "string" || knownAnthropicBatchContentTypes.has(part.type)) {
6355
+ return void 0;
6356
+ }
6357
+ }
6358
+ const recovered = await safeValidateTypes({
6359
+ value: { ...message, content },
6360
+ schema: anthropicResponseSchema
6361
+ });
6362
+ return recovered.success ? recovered.value : void 0;
6363
+ }
6364
+ function convertAnthropicBatchResponse(response, generateId3) {
6365
+ var _a, _b, _c, _d, _e, _f, _g, _h;
6366
+ const content = [];
6367
+ const mcpToolCalls = {};
6368
+ const serverToolCalls = {};
6275
6369
  for (const part of response.content) {
6276
6370
  switch (part.type) {
6277
6371
  case "text":
6278
- content.push({ type: "text", text: part.text });
6372
+ const citations = part.citations;
6373
+ content.push({
6374
+ type: "text",
6375
+ text: part.text,
6376
+ ...citations != null && citations.length > 0 && {
6377
+ providerMetadata: {
6378
+ anthropic: { citations }
6379
+ }
6380
+ }
6381
+ });
6382
+ for (const citation of (_a = part.citations) != null ? _a : []) {
6383
+ const source = createCitationSource(citation, [], generateId3);
6384
+ if (source != null) {
6385
+ content.push(source);
6386
+ }
6387
+ }
6279
6388
  break;
6280
6389
  case "thinking":
6281
6390
  content.push({
@@ -6299,6 +6408,13 @@ function convertAnthropicBatchResponse(response) {
6299
6408
  }
6300
6409
  });
6301
6410
  break;
6411
+ case "container_upload":
6412
+ content.push({
6413
+ type: "custom",
6414
+ kind: "anthropic.container_upload",
6415
+ providerMetadata: { anthropic: { fileId: part.file_id } }
6416
+ });
6417
+ break;
6302
6418
  case "compaction":
6303
6419
  content.push({
6304
6420
  type: "text",
@@ -6306,6 +6422,226 @@ function convertAnthropicBatchResponse(response) {
6306
6422
  providerMetadata: { anthropic: { type: "compaction" } }
6307
6423
  });
6308
6424
  break;
6425
+ case "tool_use":
6426
+ content.push({
6427
+ type: "tool-call",
6428
+ toolCallId: part.id,
6429
+ toolName: part.name,
6430
+ input: JSON.stringify(part.input),
6431
+ ...anthropicCallerMetadata(part.caller)
6432
+ });
6433
+ break;
6434
+ case "server_tool_use": {
6435
+ const isCodeExecutionAlias = part.name === "bash_code_execution" || part.name === "text_editor_code_execution";
6436
+ const isCodeExecution = isCodeExecutionAlias || part.name === "code_execution";
6437
+ const toolName = isCodeExecutionAlias ? "code_execution" : part.name;
6438
+ if (part.name === "tool_search_tool_bm25" || part.name === "tool_search_tool_regex") {
6439
+ serverToolCalls[part.id] = part.name;
6440
+ }
6441
+ content.push({
6442
+ type: "tool-call",
6443
+ toolCallId: part.id,
6444
+ toolName,
6445
+ input: JSON.stringify(
6446
+ isCodeExecutionAlias ? { type: part.name, ...(_b = part.input) != null ? _b : {} } : part.name === "code_execution" && part.input != null && "code" in part.input && !("type" in part.input) ? { type: "programmatic-tool-call", ...part.input } : part.input
6447
+ ),
6448
+ providerExecuted: true,
6449
+ // Batch results are retrieved without the original request tools, so
6450
+ // implicitly provisioned code execution must remain self-describing.
6451
+ ...isCodeExecution ? { dynamic: true } : {},
6452
+ ...anthropicCallerMetadata(part.caller)
6453
+ });
6454
+ break;
6455
+ }
6456
+ case "mcp_tool_use": {
6457
+ const toolCall = {
6458
+ type: "tool-call",
6459
+ toolCallId: part.id,
6460
+ toolName: part.name,
6461
+ input: JSON.stringify(part.input),
6462
+ providerExecuted: true,
6463
+ dynamic: true,
6464
+ providerMetadata: {
6465
+ anthropic: {
6466
+ serverName: part.server_name,
6467
+ type: "mcp-tool-use"
6468
+ }
6469
+ }
6470
+ };
6471
+ mcpToolCalls[part.id] = toolCall;
6472
+ content.push(toolCall);
6473
+ break;
6474
+ }
6475
+ case "mcp_tool_result": {
6476
+ const toolCall = mcpToolCalls[part.tool_use_id];
6477
+ content.push({
6478
+ type: "tool-result",
6479
+ toolCallId: part.tool_use_id,
6480
+ toolName: (_c = toolCall == null ? void 0 : toolCall.toolName) != null ? _c : "mcp",
6481
+ isError: part.is_error,
6482
+ result: part.content,
6483
+ dynamic: true,
6484
+ ...(toolCall == null ? void 0 : toolCall.providerMetadata) != null && {
6485
+ providerMetadata: toolCall.providerMetadata
6486
+ }
6487
+ });
6488
+ break;
6489
+ }
6490
+ case "web_fetch_tool_result":
6491
+ content.push({
6492
+ type: "tool-result",
6493
+ toolCallId: part.tool_use_id,
6494
+ toolName: "web_fetch",
6495
+ ...part.content.type === "web_fetch_tool_result_error" ? {
6496
+ isError: true,
6497
+ result: {
6498
+ errorCode: part.content.error_code,
6499
+ type: part.content.type
6500
+ }
6501
+ } : {
6502
+ result: {
6503
+ content: {
6504
+ citations: part.content.content.citations,
6505
+ source: {
6506
+ data: part.content.content.source.data,
6507
+ mediaType: part.content.content.source.media_type,
6508
+ type: part.content.content.source.type
6509
+ },
6510
+ title: part.content.content.title,
6511
+ type: part.content.content.type
6512
+ },
6513
+ retrievedAt: part.content.retrieved_at,
6514
+ type: part.content.type,
6515
+ url: part.content.url
6516
+ }
6517
+ },
6518
+ ...anthropicCallerMetadata(part.caller)
6519
+ });
6520
+ break;
6521
+ case "web_search_tool_result":
6522
+ content.push({
6523
+ type: "tool-result",
6524
+ toolCallId: part.tool_use_id,
6525
+ toolName: "web_search",
6526
+ ...Array.isArray(part.content) ? {
6527
+ result: part.content.map((result) => {
6528
+ var _a2;
6529
+ return {
6530
+ encryptedContent: result.encrypted_content,
6531
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
6532
+ ...result.title != null ? { title: result.title } : {},
6533
+ type: result.type,
6534
+ url: result.url
6535
+ };
6536
+ })
6537
+ } : {
6538
+ isError: true,
6539
+ result: {
6540
+ errorCode: part.content.error_code,
6541
+ type: part.content.type
6542
+ }
6543
+ },
6544
+ ...anthropicCallerMetadata(part.caller)
6545
+ });
6546
+ if (Array.isArray(part.content)) {
6547
+ for (const result of part.content) {
6548
+ content.push({
6549
+ type: "source",
6550
+ sourceType: "url",
6551
+ id: generateId3(),
6552
+ url: result.url,
6553
+ ...result.title != null ? { title: result.title } : {},
6554
+ providerMetadata: {
6555
+ anthropic: {
6556
+ pageAge: (_d = result.page_age) != null ? _d : null
6557
+ }
6558
+ }
6559
+ });
6560
+ }
6561
+ }
6562
+ break;
6563
+ case "code_execution_tool_result":
6564
+ content.push({
6565
+ type: "tool-result",
6566
+ toolCallId: part.tool_use_id,
6567
+ toolName: "code_execution",
6568
+ ...part.content.type === "code_execution_tool_result_error" ? {
6569
+ isError: true,
6570
+ result: {
6571
+ errorCode: part.content.error_code,
6572
+ type: part.content.type
6573
+ }
6574
+ } : { result: part.content }
6575
+ });
6576
+ break;
6577
+ case "bash_code_execution_tool_result":
6578
+ case "text_editor_code_execution_tool_result":
6579
+ content.push({
6580
+ type: "tool-result",
6581
+ toolCallId: part.tool_use_id,
6582
+ toolName: "code_execution",
6583
+ result: part.content
6584
+ });
6585
+ break;
6586
+ case "tool_search_tool_result": {
6587
+ const toolName = (_e = serverToolCalls[part.tool_use_id]) != null ? _e : "tool_search_tool_regex";
6588
+ content.push({
6589
+ type: "tool-result",
6590
+ toolCallId: part.tool_use_id,
6591
+ toolName,
6592
+ ...part.content.type === "tool_search_tool_result_error" ? {
6593
+ isError: true,
6594
+ result: {
6595
+ errorCode: part.content.error_code,
6596
+ type: part.content.type
6597
+ }
6598
+ } : {
6599
+ result: part.content.tool_references.map((reference) => ({
6600
+ toolName: reference.tool_name,
6601
+ type: reference.type
6602
+ }))
6603
+ }
6604
+ });
6605
+ break;
6606
+ }
6607
+ case "advisor_tool_result":
6608
+ if (part.content.type === "advisor_result") {
6609
+ content.push({
6610
+ type: "tool-result",
6611
+ toolCallId: part.tool_use_id,
6612
+ toolName: "advisor",
6613
+ result: {
6614
+ type: part.content.type,
6615
+ text: part.content.text,
6616
+ ...part.content.stop_reason != null ? { stopReason: part.content.stop_reason } : {}
6617
+ }
6618
+ });
6619
+ } else if (part.content.type === "advisor_redacted_result") {
6620
+ content.push({
6621
+ type: "tool-result",
6622
+ toolCallId: part.tool_use_id,
6623
+ toolName: "advisor",
6624
+ result: {
6625
+ type: part.content.type,
6626
+ encryptedContent: part.content.encrypted_content,
6627
+ ...part.content.stop_reason != null ? { stopReason: part.content.stop_reason } : {}
6628
+ }
6629
+ });
6630
+ } else {
6631
+ content.push({
6632
+ type: "tool-result",
6633
+ toolCallId: part.tool_use_id,
6634
+ toolName: "advisor",
6635
+ isError: true,
6636
+ result: {
6637
+ errorCode: part.content.error_code,
6638
+ type: part.content.type
6639
+ }
6640
+ });
6641
+ }
6642
+ break;
6643
+ case "fallback":
6644
+ break;
6309
6645
  }
6310
6646
  }
6311
6647
  return {
@@ -6314,12 +6650,12 @@ function convertAnthropicBatchResponse(response) {
6314
6650
  unified: mapAnthropicStopReason({
6315
6651
  finishReason: response.stop_reason
6316
6652
  }),
6317
- raw: (_a = response.stop_reason) != null ? _a : void 0
6653
+ raw: (_f = response.stop_reason) != null ? _f : void 0
6318
6654
  },
6319
6655
  usage: convertAnthropicUsage({ usage: response.usage }),
6320
6656
  response: {
6321
- id: (_b = response.id) != null ? _b : void 0,
6322
- modelId: (_c = response.model) != null ? _c : void 0
6657
+ id: (_g = response.id) != null ? _g : void 0,
6658
+ modelId: (_h = response.model) != null ? _h : void 0
6323
6659
  },
6324
6660
  warnings: [],
6325
6661
  providerMetadata: {
@@ -6327,6 +6663,18 @@ function convertAnthropicBatchResponse(response) {
6327
6663
  }
6328
6664
  };
6329
6665
  }
6666
+ function anthropicCallerMetadata(caller) {
6667
+ return caller == null ? {} : {
6668
+ providerMetadata: {
6669
+ anthropic: {
6670
+ caller: {
6671
+ type: caller.type,
6672
+ toolId: caller.tool_id
6673
+ }
6674
+ }
6675
+ }
6676
+ };
6677
+ }
6330
6678
  function convertAnthropicMessageMetadata(response) {
6331
6679
  var _a, _b, _c;
6332
6680
  const stopDetails = mapAnthropicStopDetails2(response.stop_details);
@@ -7136,7 +7484,7 @@ var AnthropicSkills = class {
7136
7484
  };
7137
7485
 
7138
7486
  // src/version.ts
7139
- var VERSION = true ? "4.0.45" : "0.0.0-test";
7487
+ var VERSION = true ? "4.0.47" : "0.0.0-test";
7140
7488
 
7141
7489
  // src/anthropic-provider.ts
7142
7490
  var ANTHROPIC_API_URL = "https://api.anthropic.com";