@ai-sdk/anthropic 2.0.111 → 2.0.113

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.
@@ -24,7 +24,8 @@ __export(index_exports, {
24
24
  anthropicTools: () => anthropicTools,
25
25
  getModelCapabilities: () => getModelCapabilities,
26
26
  prepareTools: () => prepareTools,
27
- sanitizeJsonSchema: () => sanitizeJsonSchema
27
+ sanitizeJsonSchema: () => sanitizeJsonSchema,
28
+ supportsNativeStructuredOutputSchema: () => supportsNativeStructuredOutputSchema
28
29
  });
29
30
  module.exports = __toCommonJS(index_exports);
30
31
 
@@ -1030,10 +1031,17 @@ var computerToolset_20260801Members = [
1030
1031
  "hold_key",
1031
1032
  "wait"
1032
1033
  ];
1033
- var computerToolset_20260801InputSchema = (0, import_provider_utils4.lazySchema)(
1034
+ var computerToolset_20260801InputSchema = (configs) => (0, import_provider_utils4.lazySchema)(
1034
1035
  () => (0, import_provider_utils4.zodSchema)(
1035
1036
  import_v44.z.object({
1036
- action: import_v44.z.enum(computerToolset_20260801Members),
1037
+ action: import_v44.z.enum(
1038
+ computerToolset_20260801Members.filter(
1039
+ (member) => {
1040
+ var _a;
1041
+ return ((_a = configs == null ? void 0 : configs[member]) == null ? void 0 : _a.enabled) !== false;
1042
+ }
1043
+ )
1044
+ ),
1037
1045
  coordinate: import_v44.z.tuple([import_v44.z.number().int(), import_v44.z.number().int()]).optional(),
1038
1046
  duration: import_v44.z.number().optional(),
1039
1047
  region: import_v44.z.tuple([
@@ -1063,10 +1071,14 @@ var computerToolset_20260801ArgsSchema = (0, import_provider_utils4.lazySchema)(
1063
1071
  })
1064
1072
  )
1065
1073
  );
1066
- var computerToolset_20260801 = (0, import_provider_utils4.createProviderDefinedToolFactory)({
1074
+ var factory = (0, import_provider_utils4.createProviderDefinedToolFactory)({
1067
1075
  id: "anthropic.computer_toolset_20260801",
1068
1076
  name: "computer",
1069
- inputSchema: computerToolset_20260801InputSchema
1077
+ inputSchema: computerToolset_20260801InputSchema()
1078
+ });
1079
+ var computerToolset_20260801 = (args) => ({
1080
+ ...factory(args),
1081
+ inputSchema: computerToolset_20260801InputSchema(args.configs)
1070
1082
  });
1071
1083
 
1072
1084
  // src/tool/text-editor_20250728.ts
@@ -1092,13 +1104,13 @@ var textEditor_20250728InputSchema = (0, import_provider_utils5.lazySchema)(
1092
1104
  })
1093
1105
  )
1094
1106
  );
1095
- var factory = (0, import_provider_utils5.createProviderDefinedToolFactory)({
1107
+ var factory2 = (0, import_provider_utils5.createProviderDefinedToolFactory)({
1096
1108
  id: "anthropic.text_editor_20250728",
1097
1109
  name: "str_replace_based_edit_tool",
1098
1110
  inputSchema: textEditor_20250728InputSchema
1099
1111
  });
1100
1112
  var textEditor_20250728 = (args = {}) => {
1101
- return factory(args);
1113
+ return factory2(args);
1102
1114
  };
1103
1115
 
1104
1116
  // src/tool/web-search_20250305.ts
@@ -1140,14 +1152,14 @@ var webSearch_20250305InputSchema = (0, import_provider_utils6.lazySchema)(
1140
1152
  })
1141
1153
  )
1142
1154
  );
1143
- var factory2 = (0, import_provider_utils6.createProviderDefinedToolFactoryWithOutputSchema)({
1155
+ var factory3 = (0, import_provider_utils6.createProviderDefinedToolFactoryWithOutputSchema)({
1144
1156
  id: "anthropic.web_search_20250305",
1145
1157
  name: "web_search",
1146
1158
  inputSchema: webSearch_20250305InputSchema,
1147
1159
  outputSchema: webSearch_20250305OutputSchema
1148
1160
  });
1149
1161
  var webSearch_20250305 = (args = {}) => {
1150
- return factory2(args);
1162
+ return factory3(args);
1151
1163
  };
1152
1164
 
1153
1165
  // src/tool/web-fetch-20250910.ts
@@ -1197,14 +1209,14 @@ var webFetch_20250910InputSchema = (0, import_provider_utils7.lazySchema)(
1197
1209
  })
1198
1210
  )
1199
1211
  );
1200
- var factory3 = (0, import_provider_utils7.createProviderDefinedToolFactoryWithOutputSchema)({
1212
+ var factory4 = (0, import_provider_utils7.createProviderDefinedToolFactoryWithOutputSchema)({
1201
1213
  id: "anthropic.web_fetch_20250910",
1202
1214
  name: "web_fetch",
1203
1215
  inputSchema: webFetch_20250910InputSchema,
1204
1216
  outputSchema: webFetch_20250910OutputSchema
1205
1217
  });
1206
1218
  var webFetch_20250910 = (args = {}) => {
1207
- return factory3(args);
1219
+ return factory4(args);
1208
1220
  };
1209
1221
 
1210
1222
  // src/anthropic-prepare-tools.ts
@@ -1541,14 +1553,14 @@ var codeExecution_20250522InputSchema = (0, import_provider_utils9.lazySchema)(
1541
1553
  })
1542
1554
  )
1543
1555
  );
1544
- var factory4 = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
1556
+ var factory5 = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
1545
1557
  id: "anthropic.code_execution_20250522",
1546
1558
  name: "code_execution",
1547
1559
  inputSchema: codeExecution_20250522InputSchema,
1548
1560
  outputSchema: codeExecution_20250522OutputSchema
1549
1561
  });
1550
1562
  var codeExecution_20250522 = (args = {}) => {
1551
- return factory4(args);
1563
+ return factory5(args);
1552
1564
  };
1553
1565
 
1554
1566
  // src/tool/code-execution_20250825.ts
@@ -1630,14 +1642,14 @@ var codeExecution_20250825InputSchema = (0, import_provider_utils10.lazySchema)(
1630
1642
  ])
1631
1643
  )
1632
1644
  );
1633
- var factory5 = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
1645
+ var factory6 = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
1634
1646
  id: "anthropic.code_execution_20250825",
1635
1647
  name: "code_execution",
1636
1648
  inputSchema: codeExecution_20250825InputSchema,
1637
1649
  outputSchema: codeExecution_20250825OutputSchema
1638
1650
  });
1639
1651
  var codeExecution_20250825 = (args = {}) => {
1640
- return factory5(args);
1652
+ return factory6(args);
1641
1653
  };
1642
1654
 
1643
1655
  // src/convert-to-anthropic-messages-prompt.ts
@@ -1932,7 +1944,7 @@ async function convertToAnthropicMessagesPrompt({
1932
1944
  case "json":
1933
1945
  case "error-json":
1934
1946
  default:
1935
- contentValue = JSON.stringify(output.value);
1947
+ contentValue = (0, import_provider_utils11.stringifyWithBigIntAndCycles)(output.value);
1936
1948
  break;
1937
1949
  }
1938
1950
  const toolsetName = getAnthropicToolsetName({
@@ -2106,7 +2118,7 @@ async function convertToAnthropicMessagesPrompt({
2106
2118
  type: "tool_use",
2107
2119
  id: part.toolCallId,
2108
2120
  name: part.toolName,
2109
- input: part.input,
2121
+ input: parseToolCallInput(part.input),
2110
2122
  cache_control: cacheControl
2111
2123
  });
2112
2124
  break;
@@ -2341,6 +2353,18 @@ function getAnthropicToolsetName({
2341
2353
  const fromMetadata = (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.toolsetName;
2342
2354
  return typeof fromMetadata === "string" ? fromMetadata : void 0;
2343
2355
  }
2356
+ function parseToolCallInput(input) {
2357
+ if (typeof input === "string") {
2358
+ try {
2359
+ const parsed = (0, import_provider_utils11.secureJsonParse)(input);
2360
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
2361
+ return parsed;
2362
+ }
2363
+ } catch (e) {
2364
+ }
2365
+ }
2366
+ return input;
2367
+ }
2344
2368
 
2345
2369
  // src/map-anthropic-stop-reason.ts
2346
2370
  function mapAnthropicStopReason({
@@ -2511,7 +2535,204 @@ function isPlainObject(value) {
2511
2535
  return typeof value === "object" && value !== null && !Array.isArray(value);
2512
2536
  }
2513
2537
 
2538
+ // src/has-circular-schema-reference.ts
2539
+ var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
2540
+ "dependentSchemas",
2541
+ "patternProperties",
2542
+ "properties"
2543
+ ]);
2544
+ var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
2545
+ function hasCircularSchemaReference(schema) {
2546
+ const visiting = /* @__PURE__ */ new Set();
2547
+ const visited = /* @__PURE__ */ new Set();
2548
+ const visit = (pointer) => {
2549
+ if (visiting.has(pointer)) {
2550
+ return true;
2551
+ }
2552
+ if (visited.has(pointer)) {
2553
+ return false;
2554
+ }
2555
+ const target = resolveSchemaPointer(schema, pointer);
2556
+ if (target === void 0) {
2557
+ return false;
2558
+ }
2559
+ visiting.add(pointer);
2560
+ const isCircular = collectLocalRefs(target).some(visit);
2561
+ visiting.delete(pointer);
2562
+ visited.add(pointer);
2563
+ return isCircular;
2564
+ };
2565
+ return visit("#");
2566
+ }
2567
+ function collectLocalRefs(node, refs = []) {
2568
+ if (Array.isArray(node)) {
2569
+ for (const item of node) {
2570
+ collectLocalRefs(item, refs);
2571
+ }
2572
+ return refs;
2573
+ }
2574
+ if (!isRecord(node)) {
2575
+ return refs;
2576
+ }
2577
+ for (const [key, value] of Object.entries(node)) {
2578
+ if (key === "$ref") {
2579
+ if (typeof value === "string" && value.startsWith("#")) {
2580
+ refs.push(value);
2581
+ }
2582
+ } else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
2583
+ for (const subschema of Object.values(value)) {
2584
+ collectLocalRefs(subschema, refs);
2585
+ }
2586
+ } else if (
2587
+ // definitions are only reached through a `$ref`:
2588
+ key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
2589
+ ) {
2590
+ collectLocalRefs(value, refs);
2591
+ }
2592
+ }
2593
+ return refs;
2594
+ }
2595
+ function resolveSchemaPointer(schema, pointer) {
2596
+ if (pointer === "#") {
2597
+ return schema;
2598
+ }
2599
+ if (!pointer.startsWith("#/")) {
2600
+ return void 0;
2601
+ }
2602
+ let decodedPointer;
2603
+ try {
2604
+ decodedPointer = decodeURIComponent(pointer.slice(2));
2605
+ } catch (e) {
2606
+ return void 0;
2607
+ }
2608
+ let current = schema;
2609
+ for (const rawSegment of decodedPointer.split("/")) {
2610
+ const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
2611
+ if (Array.isArray(current)) {
2612
+ current = current[Number(segment)];
2613
+ } else if (isRecord(current)) {
2614
+ current = current[segment];
2615
+ } else {
2616
+ return void 0;
2617
+ }
2618
+ }
2619
+ return current;
2620
+ }
2621
+ function isRecord(value) {
2622
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2623
+ }
2624
+
2625
+ // src/supports-native-structured-output-schema.ts
2626
+ var MAX_OPTIONAL_PROPERTIES = 10;
2627
+ var SUBSCHEMA_KEYWORDS = [
2628
+ "additionalProperties",
2629
+ "contains",
2630
+ "else",
2631
+ "if",
2632
+ "items",
2633
+ "not",
2634
+ "then"
2635
+ ];
2636
+ var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
2637
+ var SUBSCHEMA_MAP_KEYWORDS = [
2638
+ "dependentSchemas",
2639
+ "patternProperties",
2640
+ "properties"
2641
+ ];
2642
+ function supportsNativeStructuredOutputSchema(schema) {
2643
+ if (schema.$ref != null || hasCircularSchemaReference(schema)) {
2644
+ return false;
2645
+ }
2646
+ const { optionalProperties, hasOpenObject } = getSchemaStats(
2647
+ schema,
2648
+ schema,
2649
+ /* @__PURE__ */ new Map()
2650
+ );
2651
+ return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
2652
+ }
2653
+ function getSchemaStats(node, root, refStats) {
2654
+ const stats = { optionalProperties: 0, hasOpenObject: false };
2655
+ if (!isRecord(node)) {
2656
+ return stats;
2657
+ }
2658
+ const add = (child) => {
2659
+ const childStats = getSchemaStats(child, root, refStats);
2660
+ stats.optionalProperties += childStats.optionalProperties;
2661
+ stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
2662
+ };
2663
+ if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
2664
+ let resolved = refStats.get(node.$ref);
2665
+ if (resolved == null) {
2666
+ resolved = getSchemaStats(
2667
+ resolveSchemaPointer(root, node.$ref),
2668
+ root,
2669
+ refStats
2670
+ );
2671
+ refStats.set(node.$ref, resolved);
2672
+ }
2673
+ stats.optionalProperties += resolved.optionalProperties;
2674
+ stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
2675
+ }
2676
+ if (isOpenObject(node)) {
2677
+ stats.hasOpenObject = true;
2678
+ }
2679
+ if (isRecord(node.properties)) {
2680
+ const required = new Set(Array.isArray(node.required) ? node.required : []);
2681
+ stats.optionalProperties += Object.keys(node.properties).filter(
2682
+ (name) => !required.has(name)
2683
+ ).length;
2684
+ }
2685
+ for (const keyword of SUBSCHEMA_KEYWORDS) {
2686
+ const value = node[keyword];
2687
+ if (Array.isArray(value)) {
2688
+ value.forEach(add);
2689
+ } else {
2690
+ add(value);
2691
+ }
2692
+ }
2693
+ for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
2694
+ const value = node[keyword];
2695
+ if (Array.isArray(value)) {
2696
+ value.forEach(add);
2697
+ }
2698
+ }
2699
+ for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
2700
+ const value = node[keyword];
2701
+ if (isRecord(value)) {
2702
+ Object.values(value).forEach(add);
2703
+ }
2704
+ }
2705
+ return stats;
2706
+ }
2707
+ function isOpenObject(node) {
2708
+ const { additionalProperties, patternProperties, properties, type } = node;
2709
+ if (additionalProperties === true || isRecord(additionalProperties)) {
2710
+ return true;
2711
+ }
2712
+ if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
2713
+ return true;
2714
+ }
2715
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
2716
+ return isObjectType && properties == null && additionalProperties !== false;
2717
+ }
2718
+
2514
2719
  // src/anthropic-messages-language-model.ts
2720
+ function validateToolsetMember({
2721
+ toolsetName,
2722
+ memberName,
2723
+ tools
2724
+ }) {
2725
+ var _a, _b;
2726
+ const toolset = tools == null ? void 0 : tools.find(
2727
+ (tool) => "type" in tool && tool.type === "computer_toolset_20260801"
2728
+ );
2729
+ if (toolsetName !== "computer" || toolset == null || !computerToolset_20260801Members.some((member) => member === memberName) || ((_b = (_a = toolset.configs) == null ? void 0 : _a[memberName]) == null ? void 0 : _b.enabled) === false) {
2730
+ throw new import_provider3.InvalidResponseDataError({
2731
+ data: { toolsetName, memberName },
2732
+ message: `Received an unavailable toolset member: ${toolsetName}.${memberName}.`
2733
+ });
2734
+ }
2735
+ }
2515
2736
  function getAnthropicToolsetNames(tools) {
2516
2737
  const toolsetNames = {};
2517
2738
  for (const tool of tools != null ? tools : []) {
@@ -2526,8 +2747,8 @@ function toToolsetMemberInput({
2526
2747
  input
2527
2748
  }) {
2528
2749
  return {
2529
- action: memberName,
2530
- ...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
2750
+ ...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {},
2751
+ action: memberName
2531
2752
  };
2532
2753
  }
2533
2754
  function getJsonResponseToolName(tools) {
@@ -2568,6 +2789,31 @@ function createCitationSource(citation, citationDocuments, generateId2) {
2568
2789
  }
2569
2790
  };
2570
2791
  }
2792
+ var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
2793
+ "redacted_thinking",
2794
+ "text",
2795
+ "thinking"
2796
+ ]);
2797
+ function isContinuationStopReason(stopReason) {
2798
+ return stopReason === "pause_turn" || stopReason === "tool_use";
2799
+ }
2800
+ function getJsonResponseToolFallbackText(content, stopReason, jsonResponseToolName) {
2801
+ const text = /* @__PURE__ */ new Set();
2802
+ if (isContinuationStopReason(stopReason)) {
2803
+ return text;
2804
+ }
2805
+ for (const part of content) {
2806
+ if (part.type === "tool_use" && part.name === jsonResponseToolName) {
2807
+ return /* @__PURE__ */ new Set();
2808
+ }
2809
+ if (part.type === "text") {
2810
+ text.add(part);
2811
+ } else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
2812
+ text.clear();
2813
+ }
2814
+ }
2815
+ return text;
2816
+ }
2571
2817
  var AnthropicMessagesLanguageModel = class {
2572
2818
  constructor(modelId, config) {
2573
2819
  this.specificationVersion = "v2";
@@ -2695,9 +2941,10 @@ var AnthropicMessagesLanguageModel = class {
2695
2941
  }
2696
2942
  }
2697
2943
  const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
2944
+ const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
2698
2945
  const structureOutputMode = (_a = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _a : this.modelId.includes("claude-fable-5-1") || this.modelId.includes("claude-opus-5-5") || this.modelId.includes("claude-sonnet-5-5") ? "auto" : "jsonTool";
2699
- let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
2700
- if (!useStructuredOutput && rejectsForcedToolUse && supportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
2946
+ let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
2947
+ if (!useStructuredOutput && rejectsForcedToolUse && supportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsResponseSchema) {
2701
2948
  warnings.push({
2702
2949
  type: "unsupported-setting",
2703
2950
  setting: "providerOptions.anthropic.structuredOutputMode",
@@ -3006,8 +3253,13 @@ var AnthropicMessagesLanguageModel = class {
3006
3253
  betas: toolsBetas
3007
3254
  } = await prepareTools(
3008
3255
  jsonResponseTool != null ? {
3009
- tools: [...tools != null ? tools : [], jsonResponseTool],
3010
- toolChoice: tools != null && tools.length > 0 ? { type: "required" } : { type: "tool", toolName: jsonResponseTool.name },
3256
+ tools: [
3257
+ ...(tools != null ? tools : []).filter(
3258
+ (tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
3259
+ ),
3260
+ jsonResponseTool
3261
+ ],
3262
+ toolChoice: rejectsForcedToolUse ? { type: "auto" } : tools != null && tools.length > 0 ? { type: "required" } : { type: "tool", toolName: jsonResponseTool.name },
3011
3263
  disableParallelToolUse: true,
3012
3264
  cacheControlValidator,
3013
3265
  rejectsForcedToolUse
@@ -3113,10 +3365,15 @@ var AnthropicMessagesLanguageModel = class {
3113
3365
  });
3114
3366
  const content = [];
3115
3367
  let isJsonResponseFromTool = false;
3368
+ const jsonResponseToolFallbackText = jsonResponseToolName != null ? getJsonResponseToolFallbackText(
3369
+ response.content,
3370
+ response.stop_reason,
3371
+ jsonResponseToolName
3372
+ ) : void 0;
3116
3373
  for (const part of response.content) {
3117
3374
  switch (part.type) {
3118
3375
  case "text": {
3119
- if (jsonResponseToolName == null) {
3376
+ if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
3120
3377
  const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
3121
3378
  (citation) => citation.type === "web_search_result_location"
3122
3379
  );
@@ -3191,6 +3448,11 @@ var AnthropicMessagesLanguageModel = class {
3191
3448
  text: JSON.stringify(part.input)
3192
3449
  });
3193
3450
  } else if (part.toolset_name != null) {
3451
+ validateToolsetMember({
3452
+ toolsetName: part.toolset_name,
3453
+ memberName: part.name,
3454
+ tools: args.tools
3455
+ });
3194
3456
  content.push({
3195
3457
  type: "tool-call",
3196
3458
  toolCallId: part.id,
@@ -3495,6 +3757,16 @@ var AnthropicMessagesLanguageModel = class {
3495
3757
  let iterations = null;
3496
3758
  let contextManagement = null;
3497
3759
  let isJsonResponseFromTool = false;
3760
+ let rawStopReason;
3761
+ const heldBackText = [];
3762
+ const heldBackTextByIndex = /* @__PURE__ */ new Map();
3763
+ const createTextEnd = (id, citations) => ({
3764
+ type: "text-end",
3765
+ id,
3766
+ ...citations.length > 0 && {
3767
+ providerMetadata: { anthropic: { citations } }
3768
+ }
3769
+ });
3498
3770
  let isMessageOpen = false;
3499
3771
  let activeMessageId;
3500
3772
  let hasInvalidMessageSequence = false;
@@ -3506,7 +3778,7 @@ var AnthropicMessagesLanguageModel = class {
3506
3778
  controller.enqueue({ type: "stream-start", warnings });
3507
3779
  },
3508
3780
  transform(chunk, controller) {
3509
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3781
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
3510
3782
  if (hasInvalidMessageSequence) {
3511
3783
  return;
3512
3784
  }
@@ -3528,8 +3800,21 @@ var AnthropicMessagesLanguageModel = class {
3528
3800
  return;
3529
3801
  }
3530
3802
  blockType = contentBlockType;
3803
+ if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
3804
+ heldBackText.length = 0;
3805
+ }
3531
3806
  switch (contentBlockType) {
3532
3807
  case "text": {
3808
+ if (jsonResponseToolName != null) {
3809
+ const entry = {
3810
+ id: String(value.index),
3811
+ text: "",
3812
+ citations: []
3813
+ };
3814
+ heldBackText.push(entry);
3815
+ heldBackTextByIndex.set(value.index, entry);
3816
+ return;
3817
+ }
3533
3818
  contentBlocks[value.index] = {
3534
3819
  type: "text",
3535
3820
  citations: []
@@ -3590,6 +3875,11 @@ var AnthropicMessagesLanguageModel = class {
3590
3875
  id: String(value.index)
3591
3876
  });
3592
3877
  } else if (value.content_block.toolset_name != null) {
3878
+ validateToolsetMember({
3879
+ toolsetName: value.content_block.toolset_name,
3880
+ memberName: value.content_block.name,
3881
+ tools: args.tools
3882
+ });
3593
3883
  contentBlocks[value.index] = {
3594
3884
  type: "tool-call",
3595
3885
  toolCallId: value.content_block.id,
@@ -3819,11 +4109,14 @@ var AnthropicMessagesLanguageModel = class {
3819
4109
  if (contentBlock.toolset != null) {
3820
4110
  let memberInput = {};
3821
4111
  try {
3822
- memberInput = contentBlock.input === "" ? {} : JSON.parse(contentBlock.input);
4112
+ memberInput = contentBlock.input === "" ? {} : (0, import_provider_utils12.secureJsonParse)(contentBlock.input);
3823
4113
  } catch (e) {
3824
- memberInput = void 0;
4114
+ throw new import_provider3.InvalidResponseDataError({
4115
+ data: contentBlock.input,
4116
+ message: "Invalid toolset member input JSON."
4117
+ });
3825
4118
  }
3826
- contentBlock.input = memberInput === void 0 ? contentBlock.input : JSON.stringify(
4119
+ contentBlock.input = JSON.stringify(
3827
4120
  toToolsetMemberInput({
3828
4121
  memberName: contentBlock.toolset.memberName,
3829
4122
  input: memberInput
@@ -3865,6 +4158,8 @@ var AnthropicMessagesLanguageModel = class {
3865
4158
  switch (deltaType) {
3866
4159
  case "text_delta": {
3867
4160
  if (jsonResponseToolName != null) {
4161
+ const entry = heldBackTextByIndex.get(value.index);
4162
+ if (entry != null) entry.text += value.delta.text;
3868
4163
  return;
3869
4164
  }
3870
4165
  controller.enqueue({
@@ -3943,8 +4238,10 @@ var AnthropicMessagesLanguageModel = class {
3943
4238
  case "citations_delta": {
3944
4239
  const citation = value.delta.citation;
3945
4240
  const contentBlock = contentBlocks[value.index];
3946
- if ((contentBlock == null ? void 0 : contentBlock.type) === "text" && citation.type === "web_search_result_location") {
3947
- contentBlock.citations.push(citation);
4241
+ if (citation.type === "web_search_result_location") {
4242
+ if ((contentBlock == null ? void 0 : contentBlock.type) === "text")
4243
+ contentBlock.citations.push(citation);
4244
+ (_b2 = heldBackTextByIndex.get(value.index)) == null ? void 0 : _b2.citations.push(citation);
3948
4245
  }
3949
4246
  const source = createCitationSource(
3950
4247
  citation,
@@ -3981,16 +4278,17 @@ var AnthropicMessagesLanguageModel = class {
3981
4278
  }
3982
4279
  isMessageOpen = true;
3983
4280
  activeMessageId = value.message.id;
4281
+ heldBackTextByIndex.clear();
3984
4282
  usage.inputTokens = value.message.usage.input_tokens;
3985
- usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
4283
+ usage.cachedInputTokens = (_c = value.message.usage.cache_read_input_tokens) != null ? _c : void 0;
3986
4284
  rawUsage = {
3987
4285
  ...value.message.usage
3988
4286
  };
3989
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
4287
+ cacheCreationInputTokens = (_d = value.message.usage.cache_creation_input_tokens) != null ? _d : null;
3990
4288
  controller.enqueue({
3991
4289
  type: "response-metadata",
3992
- id: (_d = value.message.id) != null ? _d : void 0,
3993
- modelId: (_e = value.message.model) != null ? _e : void 0
4290
+ id: (_e = value.message.id) != null ? _e : void 0,
4291
+ modelId: (_f = value.message.model) != null ? _f : void 0
3994
4292
  });
3995
4293
  return;
3996
4294
  }
@@ -4004,7 +4302,7 @@ var AnthropicMessagesLanguageModel = class {
4004
4302
  if (value.usage.iterations != null) {
4005
4303
  iterations = value.usage.iterations;
4006
4304
  }
4007
- const servedByFallback = (_f = value.usage.iterations) == null ? void 0 : _f.some(
4305
+ const servedByFallback = (_g = value.usage.iterations) == null ? void 0 : _g.some(
4008
4306
  (iter) => iter.type === "fallback_message"
4009
4307
  );
4010
4308
  if (value.usage.iterations && value.usage.iterations.length > 0 && !servedByFallback) {
@@ -4023,21 +4321,22 @@ var AnthropicMessagesLanguageModel = class {
4023
4321
  }
4024
4322
  usage.outputTokens = value.usage.output_tokens;
4025
4323
  }
4026
- usage.totalTokens = ((_g = usage.inputTokens) != null ? _g : 0) + ((_h = usage.outputTokens) != null ? _h : 0);
4324
+ usage.totalTokens = ((_h = usage.inputTokens) != null ? _h : 0) + ((_i = usage.outputTokens) != null ? _i : 0);
4325
+ rawStopReason = value.delta.stop_reason;
4027
4326
  finishReason = mapAnthropicStopReason({
4028
4327
  finishReason: value.delta.stop_reason,
4029
4328
  isJsonResponseFromTool
4030
4329
  });
4031
4330
  stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
4032
- stopSequence = (_i = value.delta.stop_sequence) != null ? _i : null;
4331
+ stopSequence = (_j = value.delta.stop_sequence) != null ? _j : null;
4033
4332
  container = value.delta.container != null ? {
4034
4333
  expiresAt: value.delta.container.expires_at,
4035
4334
  id: value.delta.container.id,
4036
- skills: (_k = (_j = value.delta.container.skills) == null ? void 0 : _j.map((skill) => ({
4335
+ skills: (_l = (_k = value.delta.container.skills) == null ? void 0 : _k.map((skill) => ({
4037
4336
  type: skill.type,
4038
4337
  skillId: skill.skill_id,
4039
4338
  version: skill.version
4040
- }))) != null ? _k : null
4339
+ }))) != null ? _l : null
4041
4340
  } : null;
4042
4341
  if (value.context_management != null) {
4043
4342
  contextManagement = {
@@ -4075,6 +4374,15 @@ var AnthropicMessagesLanguageModel = class {
4075
4374
  case "message_stop": {
4076
4375
  isMessageOpen = false;
4077
4376
  activeMessageId = void 0;
4377
+ if (!isJsonResponseFromTool && !isContinuationStopReason(rawStopReason)) {
4378
+ for (const { id, text, citations } of heldBackText) {
4379
+ if (text.length === 0) continue;
4380
+ controller.enqueue({ type: "text-start", id });
4381
+ controller.enqueue({ type: "text-delta", id, delta: text });
4382
+ controller.enqueue(createTextEnd(id, citations));
4383
+ }
4384
+ }
4385
+ heldBackText.length = 0;
4078
4386
  controller.enqueue({
4079
4387
  type: "finish",
4080
4388
  finishReason,
@@ -4478,14 +4786,14 @@ var codeExecution_20260120InputSchema = (0, import_provider_utils15.lazySchema)(
4478
4786
  ])
4479
4787
  )
4480
4788
  );
4481
- var factory6 = (0, import_provider_utils15.createProviderDefinedToolFactoryWithOutputSchema)({
4789
+ var factory7 = (0, import_provider_utils15.createProviderDefinedToolFactoryWithOutputSchema)({
4482
4790
  id: "anthropic.code_execution_20260120",
4483
4791
  name: "code_execution",
4484
4792
  inputSchema: codeExecution_20260120InputSchema,
4485
4793
  outputSchema: codeExecution_20260120OutputSchema
4486
4794
  });
4487
4795
  var codeExecution_20260120 = (args = {}) => {
4488
- return factory6(args);
4796
+ return factory7(args);
4489
4797
  };
4490
4798
 
4491
4799
  // src/tool/computer_20241022.ts
@@ -4863,6 +5171,7 @@ var anthropicTools = {
4863
5171
  anthropicTools,
4864
5172
  getModelCapabilities,
4865
5173
  prepareTools,
4866
- sanitizeJsonSchema
5174
+ sanitizeJsonSchema,
5175
+ supportsNativeStructuredOutputSchema
4867
5176
  });
4868
5177
  //# sourceMappingURL=index.js.map