@ai-sdk/anthropic 4.0.45 → 4.0.46

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
@@ -124,6 +124,7 @@ import {
124
124
  createJsonLinesResponseHandler,
125
125
  createJsonResponseHandler as createJsonResponseHandler3,
126
126
  getFromApi,
127
+ isRecord as isRecord2,
127
128
  lazySchema as lazySchema14,
128
129
  normalizeBatchRequestCounts,
129
130
  normalizeHeaders,
@@ -162,6 +163,66 @@ var anthropicToolCallCallerSchema = z3.union([
162
163
  type: z3.literal("direct")
163
164
  })
164
165
  ]);
166
+ var anthropicCitationSchema = z3.discriminatedUnion("type", [
167
+ z3.object({
168
+ type: z3.literal("web_search_result_location"),
169
+ cited_text: z3.string(),
170
+ url: z3.string(),
171
+ title: z3.string().nullable(),
172
+ encrypted_index: z3.string()
173
+ }),
174
+ z3.object({
175
+ type: z3.literal("page_location"),
176
+ cited_text: z3.string(),
177
+ document_index: z3.number(),
178
+ document_title: z3.string().nullable(),
179
+ start_page_number: z3.number(),
180
+ end_page_number: z3.number(),
181
+ file_id: z3.string().nullish()
182
+ }),
183
+ z3.object({
184
+ type: z3.literal("char_location"),
185
+ cited_text: z3.string(),
186
+ document_index: z3.number(),
187
+ document_title: z3.string().nullable(),
188
+ start_char_index: z3.number(),
189
+ end_char_index: z3.number(),
190
+ file_id: z3.string().nullish()
191
+ }),
192
+ z3.object({
193
+ type: z3.literal("content_block_location"),
194
+ cited_text: z3.string(),
195
+ document_index: z3.number(),
196
+ document_title: z3.string().nullable(),
197
+ start_block_index: z3.number(),
198
+ end_block_index: z3.number(),
199
+ file_id: z3.string().nullable()
200
+ }),
201
+ z3.object({
202
+ type: z3.literal("search_result_location"),
203
+ cited_text: z3.string(),
204
+ search_result_index: z3.number(),
205
+ source: z3.string(),
206
+ title: z3.string().nullable(),
207
+ start_block_index: z3.number(),
208
+ end_block_index: z3.number()
209
+ })
210
+ ]);
211
+ var anthropicMcpToolResultContentSchema = z3.union([
212
+ z3.string(),
213
+ z3.array(
214
+ z3.union([
215
+ z3.string(),
216
+ z3.object({
217
+ type: z3.literal("text"),
218
+ text: z3.string(),
219
+ // MCP tool-result citations are opaque third-party pass-through data.
220
+ // Keep them permissive so new shapes do not reject whole responses.
221
+ citations: z3.array(z3.json()).nullable().optional()
222
+ })
223
+ ])
224
+ )
225
+ ]);
165
226
  var anthropicResponseSchema = lazySchema3(
166
227
  () => zodSchema3(
167
228
  z3.object({
@@ -173,33 +234,7 @@ var anthropicResponseSchema = lazySchema3(
173
234
  z3.object({
174
235
  type: z3.literal("text"),
175
236
  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()
237
+ citations: z3.array(anthropicCitationSchema).nullable().optional()
203
238
  }),
204
239
  z3.object({
205
240
  type: z3.literal("thinking"),
@@ -240,12 +275,7 @@ var anthropicResponseSchema = lazySchema3(
240
275
  type: z3.literal("mcp_tool_result"),
241
276
  tool_use_id: z3.string(),
242
277
  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
- )
278
+ content: anthropicMcpToolResultContentSchema
249
279
  }),
250
280
  z3.object({
251
281
  type: z3.literal("web_fetch_tool_result"),
@@ -255,7 +285,7 @@ var anthropicResponseSchema = lazySchema3(
255
285
  z3.object({
256
286
  type: z3.literal("web_fetch_result"),
257
287
  url: z3.string(),
258
- retrieved_at: z3.string(),
288
+ retrieved_at: z3.string().nullable(),
259
289
  content: z3.object({
260
290
  type: z3.literal("document"),
261
291
  title: z3.string().nullable(),
@@ -289,7 +319,7 @@ var anthropicResponseSchema = lazySchema3(
289
319
  z3.object({
290
320
  type: z3.literal("web_search_result"),
291
321
  url: z3.string(),
292
- title: z3.string(),
322
+ title: z3.string().nullable(),
293
323
  encrypted_content: z3.string(),
294
324
  page_age: z3.string().nullish()
295
325
  })
@@ -411,6 +441,10 @@ var anthropicResponseSchema = lazySchema3(
411
441
  })
412
442
  ])
413
443
  }),
444
+ z3.object({
445
+ type: z3.literal("container_upload"),
446
+ file_id: z3.string()
447
+ }),
414
448
  // advisor results for advisor_20260301:
415
449
  z3.object({
416
450
  type: z3.literal("advisor_tool_result"),
@@ -580,12 +614,7 @@ var anthropicChunkSchema = lazySchema3(
580
614
  type: z3.literal("mcp_tool_result"),
581
615
  tool_use_id: z3.string(),
582
616
  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
- )
617
+ content: anthropicMcpToolResultContentSchema
589
618
  }),
590
619
  z3.object({
591
620
  type: z3.literal("web_fetch_tool_result"),
@@ -595,7 +624,7 @@ var anthropicChunkSchema = lazySchema3(
595
624
  z3.object({
596
625
  type: z3.literal("web_fetch_result"),
597
626
  url: z3.string(),
598
- retrieved_at: z3.string(),
627
+ retrieved_at: z3.string().nullable(),
599
628
  content: z3.object({
600
629
  type: z3.literal("document"),
601
630
  title: z3.string().nullable(),
@@ -629,7 +658,7 @@ var anthropicChunkSchema = lazySchema3(
629
658
  z3.object({
630
659
  type: z3.literal("web_search_result"),
631
660
  url: z3.string(),
632
- title: z3.string(),
661
+ title: z3.string().nullable(),
633
662
  encrypted_content: z3.string(),
634
663
  page_age: z3.string().nullish()
635
664
  })
@@ -805,31 +834,7 @@ var anthropicChunkSchema = lazySchema3(
805
834
  }),
806
835
  z3.object({
807
836
  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
- ])
837
+ citation: anthropicCitationSchema
833
838
  })
834
839
  ])
835
840
  }),
@@ -1151,6 +1156,8 @@ var anthropicLanguageModelOptions = z4.object({
1151
1156
  * Only supported with claude-opus-4-6.
1152
1157
  */
1153
1158
  speed: z4.enum(["fast", "standard"]).optional(),
1159
+ /** Selects Anthropic's service tier for the request. */
1160
+ serviceTier: z4.enum(["auto", "standard_only"]).optional(),
1154
1161
  /**
1155
1162
  * Controls where model inference runs for this request.
1156
1163
  *
@@ -3654,14 +3661,14 @@ function getAnthropicStreamErrorMetadata(type) {
3654
3661
  }
3655
3662
  }
3656
3663
  function createCitationSource(citation, citationDocuments, generateId3) {
3657
- var _a;
3664
+ var _a, _b;
3658
3665
  if (citation.type === "web_search_result_location") {
3659
3666
  return {
3660
3667
  type: "source",
3661
3668
  sourceType: "url",
3662
3669
  id: generateId3(),
3663
3670
  url: citation.url,
3664
- title: citation.title,
3671
+ title: (_a = citation.title) != null ? _a : void 0,
3665
3672
  providerMetadata: {
3666
3673
  anthropic: {
3667
3674
  citedText: citation.cited_text,
@@ -3682,7 +3689,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
3682
3689
  sourceType: "document",
3683
3690
  id: generateId3(),
3684
3691
  mediaType: documentInfo.mediaType,
3685
- title: (_a = citation.document_title) != null ? _a : documentInfo.title,
3692
+ title: (_b = citation.document_title) != null ? _b : documentInfo.title,
3686
3693
  filename: documentInfo.filename,
3687
3694
  providerMetadata: {
3688
3695
  anthropic: citation.type === "page_location" ? {
@@ -3979,6 +3986,9 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
3979
3986
  ...(anthropicOptions == null ? void 0 : anthropicOptions.speed) && {
3980
3987
  speed: anthropicOptions.speed
3981
3988
  },
3989
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.serviceTier) && {
3990
+ service_tier: anthropicOptions.serviceTier
3991
+ },
3982
3992
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
3983
3993
  inference_geo: anthropicOptions.inferenceGeo
3984
3994
  },
@@ -4366,6 +4376,14 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4366
4376
  });
4367
4377
  break;
4368
4378
  }
4379
+ case "container_upload": {
4380
+ content.push({
4381
+ type: "custom",
4382
+ kind: "anthropic.container_upload",
4383
+ providerMetadata: { anthropic: { fileId: part.file_id } }
4384
+ });
4385
+ break;
4386
+ }
4369
4387
  case "compaction": {
4370
4388
  content.push({
4371
4389
  type: "text",
@@ -4532,7 +4550,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4532
4550
  var _a2;
4533
4551
  return {
4534
4552
  url: result.url,
4535
- title: result.title,
4553
+ ...result.title != null ? { title: result.title } : {},
4536
4554
  pageAge: (_a2 = result.page_age) != null ? _a2 : null,
4537
4555
  encryptedContent: result.encrypted_content,
4538
4556
  type: result.type
@@ -4546,7 +4564,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
4546
4564
  sourceType: "url",
4547
4565
  id: this.generateId(),
4548
4566
  url: result.url,
4549
- title: result.title,
4567
+ ...result.title != null ? { title: result.title } : {},
4550
4568
  providerMetadata: {
4551
4569
  anthropic: {
4552
4570
  pageAge: (_c = result.page_age) != null ? _c : null
@@ -5084,7 +5102,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5084
5102
  var _a3;
5085
5103
  return {
5086
5104
  url: result.url,
5087
- title: result.title,
5105
+ ...result.title != null ? { title: result.title } : {},
5088
5106
  pageAge: (_a3 = result.page_age) != null ? _a3 : null,
5089
5107
  encryptedContent: result.encrypted_content,
5090
5108
  type: result.type
@@ -5098,7 +5116,7 @@ var AnthropicLanguageModel = class _AnthropicLanguageModel {
5098
5116
  sourceType: "url",
5099
5117
  id: generateId3(),
5100
5118
  url: result.url,
5101
- title: result.title,
5119
+ ...result.title != null ? { title: result.title } : {},
5102
5120
  providerMetadata: {
5103
5121
  anthropic: {
5104
5122
  pageAge: (_b2 = result.page_age) != null ? _b2 : null
@@ -5813,7 +5831,7 @@ function hasWebTool20260209WithoutCodeExecution(tools) {
5813
5831
  hasWebTool20260209 = true;
5814
5832
  continue;
5815
5833
  }
5816
- if (tool.name === "code_execution") {
5834
+ if ("type" in tool && (tool.type === "code_execution_20250522" || tool.type === "code_execution_20250825" || tool.type === "code_execution_20260120")) {
5817
5835
  hasCodeExecutionTool = true;
5818
5836
  break;
5819
5837
  }
@@ -5919,33 +5937,59 @@ var anthropicBatchResponseSchema = lazySchema14(
5919
5937
  created_at: z15.string(),
5920
5938
  expires_at: z15.string(),
5921
5939
  archived_at: z15.string().nullish(),
5940
+ cancel_initiated_at: z15.string().nullish(),
5941
+ ended_at: z15.string().nullish(),
5922
5942
  results_url: z15.string().nullish()
5923
5943
  })
5924
5944
  )
5925
5945
  );
5946
+ var knownAnthropicBatchContentTypes = /* @__PURE__ */ new Set([
5947
+ "advisor_tool_result",
5948
+ "bash_code_execution_tool_result",
5949
+ "code_execution_tool_result",
5950
+ "compaction",
5951
+ "container_upload",
5952
+ "fallback",
5953
+ "mcp_tool_result",
5954
+ "mcp_tool_use",
5955
+ "redacted_thinking",
5956
+ "server_tool_use",
5957
+ "text",
5958
+ "text_editor_code_execution_tool_result",
5959
+ "thinking",
5960
+ "tool_search_tool_result",
5961
+ "tool_use",
5962
+ "web_fetch_tool_result",
5963
+ "web_search_tool_result"
5964
+ ]);
5965
+ var anthropicBatchResultSchema = lazySchema14(
5966
+ () => zodSchema14(
5967
+ z15.discriminatedUnion("type", [
5968
+ z15.object({
5969
+ type: z15.literal("succeeded"),
5970
+ message: z15.unknown()
5971
+ }),
5972
+ z15.object({
5973
+ type: z15.literal("errored"),
5974
+ error: z15.object({
5975
+ type: z15.literal("error"),
5976
+ error: z15.object({
5977
+ type: z15.string(),
5978
+ message: z15.string()
5979
+ }),
5980
+ request_id: z15.string().nullish()
5981
+ })
5982
+ }),
5983
+ z15.object({ type: z15.literal("canceled") }),
5984
+ z15.object({ type: z15.literal("expired") })
5985
+ ])
5986
+ )
5987
+ );
5926
5988
  var anthropicBatchResultLineSchema = lazySchema14(
5927
5989
  () => zodSchema14(
5928
5990
  z15.object({
5929
5991
  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
- ])
5992
+ result: z15.unknown()
5949
5993
  })
5950
5994
  )
5951
5995
  );
@@ -5969,6 +6013,7 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
5969
6013
  abortSignal,
5970
6014
  webhookUrl
5971
6015
  }) {
6016
+ var _a;
5972
6017
  validateRequestIds(requests);
5973
6018
  const explicitBatchBetas = new Set(
5974
6019
  await getAnthropicBatchProviderBetas({
@@ -6003,6 +6048,21 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
6003
6048
  stream: false,
6004
6049
  userSuppliedBetas: new Set(explicitBatchBetas)
6005
6050
  });
6051
+ if (prepared.usesJsonResponseTool) {
6052
+ throw new UnsupportedFunctionalityError3({
6053
+ functionality: "batch responseFormat JSON-tool fallback",
6054
+ 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.`
6055
+ });
6056
+ }
6057
+ const aliasedProviderTool = (_a = request.options.tools) == null ? void 0 : _a.find(
6058
+ (tool) => tool.type === "provider" && prepared.toolNameMapping.toProviderToolName(tool.name) !== tool.name
6059
+ );
6060
+ if (aliasedProviderTool != null) {
6061
+ throw new UnsupportedFunctionalityError3({
6062
+ functionality: "aliased provider tool names in batches",
6063
+ 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.`
6064
+ });
6065
+ }
6006
6066
  const body = this.transformRequestBody(prepared.args, prepared.betas);
6007
6067
  validateAnthropicBatchBody({
6008
6068
  body,
@@ -6089,7 +6149,7 @@ var AnthropicMessagesBatchLanguageModel = class _AnthropicMessagesBatchLanguageM
6089
6149
  }
6090
6150
  async *iterateBatchResults(lines) {
6091
6151
  for await (const line of lines) {
6092
- yield await convertAnthropicBatchResult(line);
6152
+ yield await convertAnthropicBatchResult(line, this.generateId);
6093
6153
  }
6094
6154
  }
6095
6155
  getBatchUrl(path) {
@@ -6174,13 +6234,23 @@ function validateAnthropicBatchBody({
6174
6234
  }
6175
6235
  }
6176
6236
  function convertAnthropicBatchStatus(batch) {
6237
+ var _a, _b, _c, _d;
6177
6238
  const requestCounts = convertAnthropicRequestCounts(batch.request_counts);
6178
6239
  return {
6179
6240
  status: mapAnthropicBatchStatus(batch.processing_status),
6180
6241
  rawStatus: batch.processing_status,
6181
6242
  ...requestCounts != null ? { requestCounts } : {},
6182
6243
  createdAt: batch.created_at,
6183
- expiresAt: batch.expires_at
6244
+ expiresAt: batch.expires_at,
6245
+ providerMetadata: {
6246
+ anthropic: {
6247
+ archivedAt: (_a = batch.archived_at) != null ? _a : null,
6248
+ cancelInitiatedAt: (_b = batch.cancel_initiated_at) != null ? _b : null,
6249
+ endedAt: (_c = batch.ended_at) != null ? _c : null,
6250
+ requestCounts: batch.request_counts,
6251
+ resultsUrl: (_d = batch.results_url) != null ? _d : null
6252
+ }
6253
+ }
6184
6254
  };
6185
6255
  }
6186
6256
  function mapAnthropicBatchStatus(rawStatus) {
@@ -6201,20 +6271,28 @@ function convertAnthropicRequestCounts(counts) {
6201
6271
  failed: counts.errored + counts.canceled + counts.expired
6202
6272
  });
6203
6273
  }
6204
- async function convertAnthropicBatchResult(line) {
6205
- switch (line.result.type) {
6274
+ async function convertAnthropicBatchResult(line, generateId3) {
6275
+ const resultValidation = await safeValidateTypes({
6276
+ value: line.result,
6277
+ schema: anthropicBatchResultSchema
6278
+ });
6279
+ if (!resultValidation.success) {
6280
+ return invalidAnthropicBatchResult(line.custom_id);
6281
+ }
6282
+ const result = resultValidation.value;
6283
+ switch (result.type) {
6206
6284
  case "canceled":
6207
6285
  return { id: line.custom_id, status: "cancelled" };
6208
6286
  case "expired":
6209
6287
  return { id: line.custom_id, status: "expired" };
6210
6288
  case "errored": {
6211
- const requestId = line.result.error.request_id;
6289
+ const requestId = result.error.request_id;
6212
6290
  return {
6213
6291
  id: line.custom_id,
6214
6292
  status: "failed",
6215
6293
  error: {
6216
- message: line.result.error.error.message,
6217
- type: line.result.error.error.type
6294
+ message: result.error.error.message,
6295
+ type: result.error.error.type
6218
6296
  },
6219
6297
  ...requestId != null ? {
6220
6298
  providerMetadata: {
@@ -6224,58 +6302,84 @@ async function convertAnthropicBatchResult(line) {
6224
6302
  };
6225
6303
  }
6226
6304
  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
- };
6305
+ const response = await parseAnthropicBatchResponse(result.message);
6306
+ if (response == null) {
6307
+ return invalidAnthropicBatchResult(line.custom_id);
6254
6308
  }
6255
6309
  return {
6256
6310
  id: line.custom_id,
6257
6311
  status: "succeeded",
6258
- result: convertAnthropicBatchResponse(response)
6312
+ result: convertAnthropicBatchResponse(response, generateId3)
6259
6313
  };
6260
6314
  }
6261
6315
  }
6262
6316
  }
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;
6317
+ function invalidAnthropicBatchResult(id) {
6318
+ return {
6319
+ id,
6320
+ status: "failed",
6321
+ error: {
6322
+ message: "Anthropic returned an invalid Message batch result.",
6323
+ code: "invalid_response"
6324
+ }
6325
+ };
6326
+ }
6327
+ async function parseAnthropicBatchResponse(message) {
6328
+ const validation = await safeValidateTypes({
6329
+ value: message,
6330
+ schema: anthropicResponseSchema
6331
+ });
6332
+ if (validation.success) {
6333
+ return validation.value;
6334
+ }
6335
+ if (!isRecord2(message) || !Array.isArray(message.content)) {
6336
+ return void 0;
6337
+ }
6274
6338
  const content = [];
6339
+ for (const part of message.content) {
6340
+ const partValidation = await safeValidateTypes({
6341
+ value: { ...message, content: [part] },
6342
+ schema: anthropicResponseSchema
6343
+ });
6344
+ if (partValidation.success) {
6345
+ const [validatedPart] = partValidation.value.content;
6346
+ if (validatedPart != null) {
6347
+ content.push(validatedPart);
6348
+ }
6349
+ } else if (!isRecord2(part) || typeof part.type !== "string" || knownAnthropicBatchContentTypes.has(part.type)) {
6350
+ return void 0;
6351
+ }
6352
+ }
6353
+ const recovered = await safeValidateTypes({
6354
+ value: { ...message, content },
6355
+ schema: anthropicResponseSchema
6356
+ });
6357
+ return recovered.success ? recovered.value : void 0;
6358
+ }
6359
+ function convertAnthropicBatchResponse(response, generateId3) {
6360
+ var _a, _b, _c, _d, _e, _f, _g, _h;
6361
+ const content = [];
6362
+ const mcpToolCalls = {};
6363
+ const serverToolCalls = {};
6275
6364
  for (const part of response.content) {
6276
6365
  switch (part.type) {
6277
6366
  case "text":
6278
- content.push({ type: "text", text: part.text });
6367
+ const citations = part.citations;
6368
+ content.push({
6369
+ type: "text",
6370
+ text: part.text,
6371
+ ...citations != null && citations.length > 0 && {
6372
+ providerMetadata: {
6373
+ anthropic: { citations }
6374
+ }
6375
+ }
6376
+ });
6377
+ for (const citation of (_a = part.citations) != null ? _a : []) {
6378
+ const source = createCitationSource(citation, [], generateId3);
6379
+ if (source != null) {
6380
+ content.push(source);
6381
+ }
6382
+ }
6279
6383
  break;
6280
6384
  case "thinking":
6281
6385
  content.push({
@@ -6299,6 +6403,13 @@ function convertAnthropicBatchResponse(response) {
6299
6403
  }
6300
6404
  });
6301
6405
  break;
6406
+ case "container_upload":
6407
+ content.push({
6408
+ type: "custom",
6409
+ kind: "anthropic.container_upload",
6410
+ providerMetadata: { anthropic: { fileId: part.file_id } }
6411
+ });
6412
+ break;
6302
6413
  case "compaction":
6303
6414
  content.push({
6304
6415
  type: "text",
@@ -6306,6 +6417,226 @@ function convertAnthropicBatchResponse(response) {
6306
6417
  providerMetadata: { anthropic: { type: "compaction" } }
6307
6418
  });
6308
6419
  break;
6420
+ case "tool_use":
6421
+ content.push({
6422
+ type: "tool-call",
6423
+ toolCallId: part.id,
6424
+ toolName: part.name,
6425
+ input: JSON.stringify(part.input),
6426
+ ...anthropicCallerMetadata(part.caller)
6427
+ });
6428
+ break;
6429
+ case "server_tool_use": {
6430
+ const isCodeExecutionAlias = part.name === "bash_code_execution" || part.name === "text_editor_code_execution";
6431
+ const isCodeExecution = isCodeExecutionAlias || part.name === "code_execution";
6432
+ const toolName = isCodeExecutionAlias ? "code_execution" : part.name;
6433
+ if (part.name === "tool_search_tool_bm25" || part.name === "tool_search_tool_regex") {
6434
+ serverToolCalls[part.id] = part.name;
6435
+ }
6436
+ content.push({
6437
+ type: "tool-call",
6438
+ toolCallId: part.id,
6439
+ toolName,
6440
+ input: JSON.stringify(
6441
+ 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
6442
+ ),
6443
+ providerExecuted: true,
6444
+ // Batch results are retrieved without the original request tools, so
6445
+ // implicitly provisioned code execution must remain self-describing.
6446
+ ...isCodeExecution ? { dynamic: true } : {},
6447
+ ...anthropicCallerMetadata(part.caller)
6448
+ });
6449
+ break;
6450
+ }
6451
+ case "mcp_tool_use": {
6452
+ const toolCall = {
6453
+ type: "tool-call",
6454
+ toolCallId: part.id,
6455
+ toolName: part.name,
6456
+ input: JSON.stringify(part.input),
6457
+ providerExecuted: true,
6458
+ dynamic: true,
6459
+ providerMetadata: {
6460
+ anthropic: {
6461
+ serverName: part.server_name,
6462
+ type: "mcp-tool-use"
6463
+ }
6464
+ }
6465
+ };
6466
+ mcpToolCalls[part.id] = toolCall;
6467
+ content.push(toolCall);
6468
+ break;
6469
+ }
6470
+ case "mcp_tool_result": {
6471
+ const toolCall = mcpToolCalls[part.tool_use_id];
6472
+ content.push({
6473
+ type: "tool-result",
6474
+ toolCallId: part.tool_use_id,
6475
+ toolName: (_c = toolCall == null ? void 0 : toolCall.toolName) != null ? _c : "mcp",
6476
+ isError: part.is_error,
6477
+ result: part.content,
6478
+ dynamic: true,
6479
+ ...(toolCall == null ? void 0 : toolCall.providerMetadata) != null && {
6480
+ providerMetadata: toolCall.providerMetadata
6481
+ }
6482
+ });
6483
+ break;
6484
+ }
6485
+ case "web_fetch_tool_result":
6486
+ content.push({
6487
+ type: "tool-result",
6488
+ toolCallId: part.tool_use_id,
6489
+ toolName: "web_fetch",
6490
+ ...part.content.type === "web_fetch_tool_result_error" ? {
6491
+ isError: true,
6492
+ result: {
6493
+ errorCode: part.content.error_code,
6494
+ type: part.content.type
6495
+ }
6496
+ } : {
6497
+ result: {
6498
+ content: {
6499
+ citations: part.content.content.citations,
6500
+ source: {
6501
+ data: part.content.content.source.data,
6502
+ mediaType: part.content.content.source.media_type,
6503
+ type: part.content.content.source.type
6504
+ },
6505
+ title: part.content.content.title,
6506
+ type: part.content.content.type
6507
+ },
6508
+ retrievedAt: part.content.retrieved_at,
6509
+ type: part.content.type,
6510
+ url: part.content.url
6511
+ }
6512
+ },
6513
+ ...anthropicCallerMetadata(part.caller)
6514
+ });
6515
+ break;
6516
+ case "web_search_tool_result":
6517
+ content.push({
6518
+ type: "tool-result",
6519
+ toolCallId: part.tool_use_id,
6520
+ toolName: "web_search",
6521
+ ...Array.isArray(part.content) ? {
6522
+ result: part.content.map((result) => {
6523
+ var _a2;
6524
+ return {
6525
+ encryptedContent: result.encrypted_content,
6526
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
6527
+ ...result.title != null ? { title: result.title } : {},
6528
+ type: result.type,
6529
+ url: result.url
6530
+ };
6531
+ })
6532
+ } : {
6533
+ isError: true,
6534
+ result: {
6535
+ errorCode: part.content.error_code,
6536
+ type: part.content.type
6537
+ }
6538
+ },
6539
+ ...anthropicCallerMetadata(part.caller)
6540
+ });
6541
+ if (Array.isArray(part.content)) {
6542
+ for (const result of part.content) {
6543
+ content.push({
6544
+ type: "source",
6545
+ sourceType: "url",
6546
+ id: generateId3(),
6547
+ url: result.url,
6548
+ ...result.title != null ? { title: result.title } : {},
6549
+ providerMetadata: {
6550
+ anthropic: {
6551
+ pageAge: (_d = result.page_age) != null ? _d : null
6552
+ }
6553
+ }
6554
+ });
6555
+ }
6556
+ }
6557
+ break;
6558
+ case "code_execution_tool_result":
6559
+ content.push({
6560
+ type: "tool-result",
6561
+ toolCallId: part.tool_use_id,
6562
+ toolName: "code_execution",
6563
+ ...part.content.type === "code_execution_tool_result_error" ? {
6564
+ isError: true,
6565
+ result: {
6566
+ errorCode: part.content.error_code,
6567
+ type: part.content.type
6568
+ }
6569
+ } : { result: part.content }
6570
+ });
6571
+ break;
6572
+ case "bash_code_execution_tool_result":
6573
+ case "text_editor_code_execution_tool_result":
6574
+ content.push({
6575
+ type: "tool-result",
6576
+ toolCallId: part.tool_use_id,
6577
+ toolName: "code_execution",
6578
+ result: part.content
6579
+ });
6580
+ break;
6581
+ case "tool_search_tool_result": {
6582
+ const toolName = (_e = serverToolCalls[part.tool_use_id]) != null ? _e : "tool_search_tool_regex";
6583
+ content.push({
6584
+ type: "tool-result",
6585
+ toolCallId: part.tool_use_id,
6586
+ toolName,
6587
+ ...part.content.type === "tool_search_tool_result_error" ? {
6588
+ isError: true,
6589
+ result: {
6590
+ errorCode: part.content.error_code,
6591
+ type: part.content.type
6592
+ }
6593
+ } : {
6594
+ result: part.content.tool_references.map((reference) => ({
6595
+ toolName: reference.tool_name,
6596
+ type: reference.type
6597
+ }))
6598
+ }
6599
+ });
6600
+ break;
6601
+ }
6602
+ case "advisor_tool_result":
6603
+ if (part.content.type === "advisor_result") {
6604
+ content.push({
6605
+ type: "tool-result",
6606
+ toolCallId: part.tool_use_id,
6607
+ toolName: "advisor",
6608
+ result: {
6609
+ type: part.content.type,
6610
+ text: part.content.text,
6611
+ ...part.content.stop_reason != null ? { stopReason: part.content.stop_reason } : {}
6612
+ }
6613
+ });
6614
+ } else if (part.content.type === "advisor_redacted_result") {
6615
+ content.push({
6616
+ type: "tool-result",
6617
+ toolCallId: part.tool_use_id,
6618
+ toolName: "advisor",
6619
+ result: {
6620
+ type: part.content.type,
6621
+ encryptedContent: part.content.encrypted_content,
6622
+ ...part.content.stop_reason != null ? { stopReason: part.content.stop_reason } : {}
6623
+ }
6624
+ });
6625
+ } else {
6626
+ content.push({
6627
+ type: "tool-result",
6628
+ toolCallId: part.tool_use_id,
6629
+ toolName: "advisor",
6630
+ isError: true,
6631
+ result: {
6632
+ errorCode: part.content.error_code,
6633
+ type: part.content.type
6634
+ }
6635
+ });
6636
+ }
6637
+ break;
6638
+ case "fallback":
6639
+ break;
6309
6640
  }
6310
6641
  }
6311
6642
  return {
@@ -6314,12 +6645,12 @@ function convertAnthropicBatchResponse(response) {
6314
6645
  unified: mapAnthropicStopReason({
6315
6646
  finishReason: response.stop_reason
6316
6647
  }),
6317
- raw: (_a = response.stop_reason) != null ? _a : void 0
6648
+ raw: (_f = response.stop_reason) != null ? _f : void 0
6318
6649
  },
6319
6650
  usage: convertAnthropicUsage({ usage: response.usage }),
6320
6651
  response: {
6321
- id: (_b = response.id) != null ? _b : void 0,
6322
- modelId: (_c = response.model) != null ? _c : void 0
6652
+ id: (_g = response.id) != null ? _g : void 0,
6653
+ modelId: (_h = response.model) != null ? _h : void 0
6323
6654
  },
6324
6655
  warnings: [],
6325
6656
  providerMetadata: {
@@ -6327,6 +6658,18 @@ function convertAnthropicBatchResponse(response) {
6327
6658
  }
6328
6659
  };
6329
6660
  }
6661
+ function anthropicCallerMetadata(caller) {
6662
+ return caller == null ? {} : {
6663
+ providerMetadata: {
6664
+ anthropic: {
6665
+ caller: {
6666
+ type: caller.type,
6667
+ toolId: caller.tool_id
6668
+ }
6669
+ }
6670
+ }
6671
+ };
6672
+ }
6330
6673
  function convertAnthropicMessageMetadata(response) {
6331
6674
  var _a, _b, _c;
6332
6675
  const stopDetails = mapAnthropicStopDetails2(response.stop_details);
@@ -7136,7 +7479,7 @@ var AnthropicSkills = class {
7136
7479
  };
7137
7480
 
7138
7481
  // src/version.ts
7139
- var VERSION = true ? "4.0.45" : "0.0.0-test";
7482
+ var VERSION = true ? "4.0.46" : "0.0.0-test";
7140
7483
 
7141
7484
  // src/anthropic-provider.ts
7142
7485
  var ANTHROPIC_API_URL = "https://api.anthropic.com";