@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 2.0.113
4
+
5
+ ### Patch Changes
6
+
7
+ - 16b4736: Keep computer toolset actions tied to the returned member name and reject disabled or unavailable members before emitting tool calls. Apply member configuration to local input validation and reject malformed streamed member input.
8
+ - Updated dependencies [49c554d]
9
+ - Updated dependencies [b753c03]
10
+ - @ai-sdk/provider-utils@3.0.45
11
+
12
+ ## 2.0.112
13
+
14
+ ### Patch Changes
15
+
16
+ - 0a069bd: Enable native structured output for supported Claude 5 models on Bedrock. In automatic mode, Bedrock and Anthropic now fall back for schemas that native structured output cannot represent: recursive references, open objects, or more than ten optional properties. Anthropic uses the JSON response tool; Bedrock Converse uses the JSON tool or JSON instructions for models that reject forced tool use. The JSON-tool fallback excludes disabled and unselected application tools. Explicit `outputFormat` selection remains unchanged.
17
+ - 0a069bd: fix(anthropic): parse valid JSON string tool input and safely serialize BigInt and cycles in tool results
18
+ - 0a069bd: Preserve final JSON text when a model answers without calling the JSON response tool, including open-schema fallbacks on Claude models that reject forced tools. Keep tool preambles out of structured output in generation and streaming.
19
+
20
+ When the model answers in text, streaming buffers that text until the message finishes to avoid exposing tool preambles as partial structured output.
21
+
22
+ - Updated dependencies [0a069bd]
23
+ - Updated dependencies [0a069bd]
24
+ - Updated dependencies [0a069bd]
25
+ - @ai-sdk/provider-utils@3.0.44
26
+
3
27
  ## 2.0.111
4
28
 
5
29
  ### Patch Changes
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
31
31
  var import_provider_utils22 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/version.ts
34
- var VERSION = true ? "2.0.111" : "0.0.0-test";
34
+ var VERSION = true ? "2.0.113" : "0.0.0-test";
35
35
 
36
36
  // src/anthropic-messages-language-model.ts
37
37
  var import_provider3 = require("@ai-sdk/provider");
@@ -1035,10 +1035,17 @@ var computerToolset_20260801Members = [
1035
1035
  "hold_key",
1036
1036
  "wait"
1037
1037
  ];
1038
- var computerToolset_20260801InputSchema = (0, import_provider_utils4.lazySchema)(
1038
+ var computerToolset_20260801InputSchema = (configs) => (0, import_provider_utils4.lazySchema)(
1039
1039
  () => (0, import_provider_utils4.zodSchema)(
1040
1040
  import_v44.z.object({
1041
- action: import_v44.z.enum(computerToolset_20260801Members),
1041
+ action: import_v44.z.enum(
1042
+ computerToolset_20260801Members.filter(
1043
+ (member) => {
1044
+ var _a;
1045
+ return ((_a = configs == null ? void 0 : configs[member]) == null ? void 0 : _a.enabled) !== false;
1046
+ }
1047
+ )
1048
+ ),
1042
1049
  coordinate: import_v44.z.tuple([import_v44.z.number().int(), import_v44.z.number().int()]).optional(),
1043
1050
  duration: import_v44.z.number().optional(),
1044
1051
  region: import_v44.z.tuple([
@@ -1068,10 +1075,14 @@ var computerToolset_20260801ArgsSchema = (0, import_provider_utils4.lazySchema)(
1068
1075
  })
1069
1076
  )
1070
1077
  );
1071
- var computerToolset_20260801 = (0, import_provider_utils4.createProviderDefinedToolFactory)({
1078
+ var factory = (0, import_provider_utils4.createProviderDefinedToolFactory)({
1072
1079
  id: "anthropic.computer_toolset_20260801",
1073
1080
  name: "computer",
1074
- inputSchema: computerToolset_20260801InputSchema
1081
+ inputSchema: computerToolset_20260801InputSchema()
1082
+ });
1083
+ var computerToolset_20260801 = (args) => ({
1084
+ ...factory(args),
1085
+ inputSchema: computerToolset_20260801InputSchema(args.configs)
1075
1086
  });
1076
1087
 
1077
1088
  // src/tool/text-editor_20250728.ts
@@ -1097,13 +1108,13 @@ var textEditor_20250728InputSchema = (0, import_provider_utils5.lazySchema)(
1097
1108
  })
1098
1109
  )
1099
1110
  );
1100
- var factory = (0, import_provider_utils5.createProviderDefinedToolFactory)({
1111
+ var factory2 = (0, import_provider_utils5.createProviderDefinedToolFactory)({
1101
1112
  id: "anthropic.text_editor_20250728",
1102
1113
  name: "str_replace_based_edit_tool",
1103
1114
  inputSchema: textEditor_20250728InputSchema
1104
1115
  });
1105
1116
  var textEditor_20250728 = (args = {}) => {
1106
- return factory(args);
1117
+ return factory2(args);
1107
1118
  };
1108
1119
 
1109
1120
  // src/tool/web-search_20250305.ts
@@ -1145,14 +1156,14 @@ var webSearch_20250305InputSchema = (0, import_provider_utils6.lazySchema)(
1145
1156
  })
1146
1157
  )
1147
1158
  );
1148
- var factory2 = (0, import_provider_utils6.createProviderDefinedToolFactoryWithOutputSchema)({
1159
+ var factory3 = (0, import_provider_utils6.createProviderDefinedToolFactoryWithOutputSchema)({
1149
1160
  id: "anthropic.web_search_20250305",
1150
1161
  name: "web_search",
1151
1162
  inputSchema: webSearch_20250305InputSchema,
1152
1163
  outputSchema: webSearch_20250305OutputSchema
1153
1164
  });
1154
1165
  var webSearch_20250305 = (args = {}) => {
1155
- return factory2(args);
1166
+ return factory3(args);
1156
1167
  };
1157
1168
 
1158
1169
  // src/tool/web-fetch-20250910.ts
@@ -1202,14 +1213,14 @@ var webFetch_20250910InputSchema = (0, import_provider_utils7.lazySchema)(
1202
1213
  })
1203
1214
  )
1204
1215
  );
1205
- var factory3 = (0, import_provider_utils7.createProviderDefinedToolFactoryWithOutputSchema)({
1216
+ var factory4 = (0, import_provider_utils7.createProviderDefinedToolFactoryWithOutputSchema)({
1206
1217
  id: "anthropic.web_fetch_20250910",
1207
1218
  name: "web_fetch",
1208
1219
  inputSchema: webFetch_20250910InputSchema,
1209
1220
  outputSchema: webFetch_20250910OutputSchema
1210
1221
  });
1211
1222
  var webFetch_20250910 = (args = {}) => {
1212
- return factory3(args);
1223
+ return factory4(args);
1213
1224
  };
1214
1225
 
1215
1226
  // src/anthropic-prepare-tools.ts
@@ -1546,14 +1557,14 @@ var codeExecution_20250522InputSchema = (0, import_provider_utils9.lazySchema)(
1546
1557
  })
1547
1558
  )
1548
1559
  );
1549
- var factory4 = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
1560
+ var factory5 = (0, import_provider_utils9.createProviderDefinedToolFactoryWithOutputSchema)({
1550
1561
  id: "anthropic.code_execution_20250522",
1551
1562
  name: "code_execution",
1552
1563
  inputSchema: codeExecution_20250522InputSchema,
1553
1564
  outputSchema: codeExecution_20250522OutputSchema
1554
1565
  });
1555
1566
  var codeExecution_20250522 = (args = {}) => {
1556
- return factory4(args);
1567
+ return factory5(args);
1557
1568
  };
1558
1569
 
1559
1570
  // src/tool/code-execution_20250825.ts
@@ -1635,14 +1646,14 @@ var codeExecution_20250825InputSchema = (0, import_provider_utils10.lazySchema)(
1635
1646
  ])
1636
1647
  )
1637
1648
  );
1638
- var factory5 = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
1649
+ var factory6 = (0, import_provider_utils10.createProviderDefinedToolFactoryWithOutputSchema)({
1639
1650
  id: "anthropic.code_execution_20250825",
1640
1651
  name: "code_execution",
1641
1652
  inputSchema: codeExecution_20250825InputSchema,
1642
1653
  outputSchema: codeExecution_20250825OutputSchema
1643
1654
  });
1644
1655
  var codeExecution_20250825 = (args = {}) => {
1645
- return factory5(args);
1656
+ return factory6(args);
1646
1657
  };
1647
1658
 
1648
1659
  // src/convert-to-anthropic-messages-prompt.ts
@@ -1937,7 +1948,7 @@ async function convertToAnthropicMessagesPrompt({
1937
1948
  case "json":
1938
1949
  case "error-json":
1939
1950
  default:
1940
- contentValue = JSON.stringify(output.value);
1951
+ contentValue = (0, import_provider_utils11.stringifyWithBigIntAndCycles)(output.value);
1941
1952
  break;
1942
1953
  }
1943
1954
  const toolsetName = getAnthropicToolsetName({
@@ -2111,7 +2122,7 @@ async function convertToAnthropicMessagesPrompt({
2111
2122
  type: "tool_use",
2112
2123
  id: part.toolCallId,
2113
2124
  name: part.toolName,
2114
- input: part.input,
2125
+ input: parseToolCallInput(part.input),
2115
2126
  cache_control: cacheControl
2116
2127
  });
2117
2128
  break;
@@ -2346,6 +2357,18 @@ function getAnthropicToolsetName({
2346
2357
  const fromMetadata = (_a = providerOptions == null ? void 0 : providerOptions.anthropic) == null ? void 0 : _a.toolsetName;
2347
2358
  return typeof fromMetadata === "string" ? fromMetadata : void 0;
2348
2359
  }
2360
+ function parseToolCallInput(input) {
2361
+ if (typeof input === "string") {
2362
+ try {
2363
+ const parsed = (0, import_provider_utils11.secureJsonParse)(input);
2364
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
2365
+ return parsed;
2366
+ }
2367
+ } catch (e) {
2368
+ }
2369
+ }
2370
+ return input;
2371
+ }
2349
2372
 
2350
2373
  // src/map-anthropic-stop-reason.ts
2351
2374
  function mapAnthropicStopReason({
@@ -2516,7 +2539,204 @@ function isPlainObject(value) {
2516
2539
  return typeof value === "object" && value !== null && !Array.isArray(value);
2517
2540
  }
2518
2541
 
2542
+ // src/has-circular-schema-reference.ts
2543
+ var SCHEMA_MAP_KEYWORDS = /* @__PURE__ */ new Set([
2544
+ "dependentSchemas",
2545
+ "patternProperties",
2546
+ "properties"
2547
+ ]);
2548
+ var DATA_KEYWORDS = /* @__PURE__ */ new Set(["const", "default", "enum", "examples"]);
2549
+ function hasCircularSchemaReference(schema) {
2550
+ const visiting = /* @__PURE__ */ new Set();
2551
+ const visited = /* @__PURE__ */ new Set();
2552
+ const visit = (pointer) => {
2553
+ if (visiting.has(pointer)) {
2554
+ return true;
2555
+ }
2556
+ if (visited.has(pointer)) {
2557
+ return false;
2558
+ }
2559
+ const target = resolveSchemaPointer(schema, pointer);
2560
+ if (target === void 0) {
2561
+ return false;
2562
+ }
2563
+ visiting.add(pointer);
2564
+ const isCircular = collectLocalRefs(target).some(visit);
2565
+ visiting.delete(pointer);
2566
+ visited.add(pointer);
2567
+ return isCircular;
2568
+ };
2569
+ return visit("#");
2570
+ }
2571
+ function collectLocalRefs(node, refs = []) {
2572
+ if (Array.isArray(node)) {
2573
+ for (const item of node) {
2574
+ collectLocalRefs(item, refs);
2575
+ }
2576
+ return refs;
2577
+ }
2578
+ if (!isRecord(node)) {
2579
+ return refs;
2580
+ }
2581
+ for (const [key, value] of Object.entries(node)) {
2582
+ if (key === "$ref") {
2583
+ if (typeof value === "string" && value.startsWith("#")) {
2584
+ refs.push(value);
2585
+ }
2586
+ } else if (SCHEMA_MAP_KEYWORDS.has(key) && isRecord(value)) {
2587
+ for (const subschema of Object.values(value)) {
2588
+ collectLocalRefs(subschema, refs);
2589
+ }
2590
+ } else if (
2591
+ // definitions are only reached through a `$ref`:
2592
+ key !== "$defs" && key !== "definitions" && !DATA_KEYWORDS.has(key)
2593
+ ) {
2594
+ collectLocalRefs(value, refs);
2595
+ }
2596
+ }
2597
+ return refs;
2598
+ }
2599
+ function resolveSchemaPointer(schema, pointer) {
2600
+ if (pointer === "#") {
2601
+ return schema;
2602
+ }
2603
+ if (!pointer.startsWith("#/")) {
2604
+ return void 0;
2605
+ }
2606
+ let decodedPointer;
2607
+ try {
2608
+ decodedPointer = decodeURIComponent(pointer.slice(2));
2609
+ } catch (e) {
2610
+ return void 0;
2611
+ }
2612
+ let current = schema;
2613
+ for (const rawSegment of decodedPointer.split("/")) {
2614
+ const segment = rawSegment.replace(/~1/g, "/").replace(/~0/g, "~");
2615
+ if (Array.isArray(current)) {
2616
+ current = current[Number(segment)];
2617
+ } else if (isRecord(current)) {
2618
+ current = current[segment];
2619
+ } else {
2620
+ return void 0;
2621
+ }
2622
+ }
2623
+ return current;
2624
+ }
2625
+ function isRecord(value) {
2626
+ return typeof value === "object" && value !== null && !Array.isArray(value);
2627
+ }
2628
+
2629
+ // src/supports-native-structured-output-schema.ts
2630
+ var MAX_OPTIONAL_PROPERTIES = 10;
2631
+ var SUBSCHEMA_KEYWORDS = [
2632
+ "additionalProperties",
2633
+ "contains",
2634
+ "else",
2635
+ "if",
2636
+ "items",
2637
+ "not",
2638
+ "then"
2639
+ ];
2640
+ var SUBSCHEMA_LIST_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems"];
2641
+ var SUBSCHEMA_MAP_KEYWORDS = [
2642
+ "dependentSchemas",
2643
+ "patternProperties",
2644
+ "properties"
2645
+ ];
2646
+ function supportsNativeStructuredOutputSchema(schema) {
2647
+ if (schema.$ref != null || hasCircularSchemaReference(schema)) {
2648
+ return false;
2649
+ }
2650
+ const { optionalProperties, hasOpenObject } = getSchemaStats(
2651
+ schema,
2652
+ schema,
2653
+ /* @__PURE__ */ new Map()
2654
+ );
2655
+ return !hasOpenObject && optionalProperties <= MAX_OPTIONAL_PROPERTIES;
2656
+ }
2657
+ function getSchemaStats(node, root, refStats) {
2658
+ const stats = { optionalProperties: 0, hasOpenObject: false };
2659
+ if (!isRecord(node)) {
2660
+ return stats;
2661
+ }
2662
+ const add = (child) => {
2663
+ const childStats = getSchemaStats(child, root, refStats);
2664
+ stats.optionalProperties += childStats.optionalProperties;
2665
+ stats.hasOpenObject || (stats.hasOpenObject = childStats.hasOpenObject);
2666
+ };
2667
+ if (typeof node.$ref === "string" && node.$ref.startsWith("#")) {
2668
+ let resolved = refStats.get(node.$ref);
2669
+ if (resolved == null) {
2670
+ resolved = getSchemaStats(
2671
+ resolveSchemaPointer(root, node.$ref),
2672
+ root,
2673
+ refStats
2674
+ );
2675
+ refStats.set(node.$ref, resolved);
2676
+ }
2677
+ stats.optionalProperties += resolved.optionalProperties;
2678
+ stats.hasOpenObject || (stats.hasOpenObject = resolved.hasOpenObject);
2679
+ }
2680
+ if (isOpenObject(node)) {
2681
+ stats.hasOpenObject = true;
2682
+ }
2683
+ if (isRecord(node.properties)) {
2684
+ const required = new Set(Array.isArray(node.required) ? node.required : []);
2685
+ stats.optionalProperties += Object.keys(node.properties).filter(
2686
+ (name) => !required.has(name)
2687
+ ).length;
2688
+ }
2689
+ for (const keyword of SUBSCHEMA_KEYWORDS) {
2690
+ const value = node[keyword];
2691
+ if (Array.isArray(value)) {
2692
+ value.forEach(add);
2693
+ } else {
2694
+ add(value);
2695
+ }
2696
+ }
2697
+ for (const keyword of SUBSCHEMA_LIST_KEYWORDS) {
2698
+ const value = node[keyword];
2699
+ if (Array.isArray(value)) {
2700
+ value.forEach(add);
2701
+ }
2702
+ }
2703
+ for (const keyword of SUBSCHEMA_MAP_KEYWORDS) {
2704
+ const value = node[keyword];
2705
+ if (isRecord(value)) {
2706
+ Object.values(value).forEach(add);
2707
+ }
2708
+ }
2709
+ return stats;
2710
+ }
2711
+ function isOpenObject(node) {
2712
+ const { additionalProperties, patternProperties, properties, type } = node;
2713
+ if (additionalProperties === true || isRecord(additionalProperties)) {
2714
+ return true;
2715
+ }
2716
+ if (isRecord(patternProperties) && Object.keys(patternProperties).length > 0) {
2717
+ return true;
2718
+ }
2719
+ const isObjectType = type === "object" || Array.isArray(type) && type.includes("object");
2720
+ return isObjectType && properties == null && additionalProperties !== false;
2721
+ }
2722
+
2519
2723
  // src/anthropic-messages-language-model.ts
2724
+ function validateToolsetMember({
2725
+ toolsetName,
2726
+ memberName,
2727
+ tools
2728
+ }) {
2729
+ var _a, _b;
2730
+ const toolset = tools == null ? void 0 : tools.find(
2731
+ (tool) => "type" in tool && tool.type === "computer_toolset_20260801"
2732
+ );
2733
+ 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) {
2734
+ throw new import_provider3.InvalidResponseDataError({
2735
+ data: { toolsetName, memberName },
2736
+ message: `Received an unavailable toolset member: ${toolsetName}.${memberName}.`
2737
+ });
2738
+ }
2739
+ }
2520
2740
  function getAnthropicToolsetNames(tools) {
2521
2741
  const toolsetNames = {};
2522
2742
  for (const tool of tools != null ? tools : []) {
@@ -2531,8 +2751,8 @@ function toToolsetMemberInput({
2531
2751
  input
2532
2752
  }) {
2533
2753
  return {
2534
- action: memberName,
2535
- ...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {}
2754
+ ...typeof input === "object" && input !== null && !Array.isArray(input) ? input : {},
2755
+ action: memberName
2536
2756
  };
2537
2757
  }
2538
2758
  function getJsonResponseToolName(tools) {
@@ -2573,6 +2793,31 @@ function createCitationSource(citation, citationDocuments, generateId3) {
2573
2793
  }
2574
2794
  };
2575
2795
  }
2796
+ var TEXT_OR_THINKING_BLOCK_TYPES = /* @__PURE__ */ new Set([
2797
+ "redacted_thinking",
2798
+ "text",
2799
+ "thinking"
2800
+ ]);
2801
+ function isContinuationStopReason(stopReason) {
2802
+ return stopReason === "pause_turn" || stopReason === "tool_use";
2803
+ }
2804
+ function getJsonResponseToolFallbackText(content, stopReason, jsonResponseToolName) {
2805
+ const text = /* @__PURE__ */ new Set();
2806
+ if (isContinuationStopReason(stopReason)) {
2807
+ return text;
2808
+ }
2809
+ for (const part of content) {
2810
+ if (part.type === "tool_use" && part.name === jsonResponseToolName) {
2811
+ return /* @__PURE__ */ new Set();
2812
+ }
2813
+ if (part.type === "text") {
2814
+ text.add(part);
2815
+ } else if (!TEXT_OR_THINKING_BLOCK_TYPES.has(part.type)) {
2816
+ text.clear();
2817
+ }
2818
+ }
2819
+ return text;
2820
+ }
2576
2821
  var AnthropicMessagesLanguageModel = class {
2577
2822
  constructor(modelId, config) {
2578
2823
  this.specificationVersion = "v2";
@@ -2700,9 +2945,10 @@ var AnthropicMessagesLanguageModel = class {
2700
2945
  }
2701
2946
  }
2702
2947
  const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
2948
+ const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
2703
2949
  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";
2704
- let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
2705
- if (!useStructuredOutput && rejectsForcedToolUse && supportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
2950
+ let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
2951
+ if (!useStructuredOutput && rejectsForcedToolUse && supportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsResponseSchema) {
2706
2952
  warnings.push({
2707
2953
  type: "unsupported-setting",
2708
2954
  setting: "providerOptions.anthropic.structuredOutputMode",
@@ -3011,8 +3257,13 @@ var AnthropicMessagesLanguageModel = class {
3011
3257
  betas: toolsBetas
3012
3258
  } = await prepareTools(
3013
3259
  jsonResponseTool != null ? {
3014
- tools: [...tools != null ? tools : [], jsonResponseTool],
3015
- toolChoice: tools != null && tools.length > 0 ? { type: "required" } : { type: "tool", toolName: jsonResponseTool.name },
3260
+ tools: [
3261
+ ...(tools != null ? tools : []).filter(
3262
+ (tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
3263
+ ),
3264
+ jsonResponseTool
3265
+ ],
3266
+ toolChoice: rejectsForcedToolUse ? { type: "auto" } : tools != null && tools.length > 0 ? { type: "required" } : { type: "tool", toolName: jsonResponseTool.name },
3016
3267
  disableParallelToolUse: true,
3017
3268
  cacheControlValidator,
3018
3269
  rejectsForcedToolUse
@@ -3118,10 +3369,15 @@ var AnthropicMessagesLanguageModel = class {
3118
3369
  });
3119
3370
  const content = [];
3120
3371
  let isJsonResponseFromTool = false;
3372
+ const jsonResponseToolFallbackText = jsonResponseToolName != null ? getJsonResponseToolFallbackText(
3373
+ response.content,
3374
+ response.stop_reason,
3375
+ jsonResponseToolName
3376
+ ) : void 0;
3121
3377
  for (const part of response.content) {
3122
3378
  switch (part.type) {
3123
3379
  case "text": {
3124
- if (jsonResponseToolName == null) {
3380
+ if (jsonResponseToolFallbackText == null || jsonResponseToolFallbackText.has(part)) {
3125
3381
  const webSearchCitations = (_a = part.citations) == null ? void 0 : _a.filter(
3126
3382
  (citation) => citation.type === "web_search_result_location"
3127
3383
  );
@@ -3196,6 +3452,11 @@ var AnthropicMessagesLanguageModel = class {
3196
3452
  text: JSON.stringify(part.input)
3197
3453
  });
3198
3454
  } else if (part.toolset_name != null) {
3455
+ validateToolsetMember({
3456
+ toolsetName: part.toolset_name,
3457
+ memberName: part.name,
3458
+ tools: args.tools
3459
+ });
3199
3460
  content.push({
3200
3461
  type: "tool-call",
3201
3462
  toolCallId: part.id,
@@ -3500,6 +3761,16 @@ var AnthropicMessagesLanguageModel = class {
3500
3761
  let iterations = null;
3501
3762
  let contextManagement = null;
3502
3763
  let isJsonResponseFromTool = false;
3764
+ let rawStopReason;
3765
+ const heldBackText = [];
3766
+ const heldBackTextByIndex = /* @__PURE__ */ new Map();
3767
+ const createTextEnd = (id, citations) => ({
3768
+ type: "text-end",
3769
+ id,
3770
+ ...citations.length > 0 && {
3771
+ providerMetadata: { anthropic: { citations } }
3772
+ }
3773
+ });
3503
3774
  let isMessageOpen = false;
3504
3775
  let activeMessageId;
3505
3776
  let hasInvalidMessageSequence = false;
@@ -3511,7 +3782,7 @@ var AnthropicMessagesLanguageModel = class {
3511
3782
  controller.enqueue({ type: "stream-start", warnings });
3512
3783
  },
3513
3784
  transform(chunk, controller) {
3514
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3785
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
3515
3786
  if (hasInvalidMessageSequence) {
3516
3787
  return;
3517
3788
  }
@@ -3533,8 +3804,21 @@ var AnthropicMessagesLanguageModel = class {
3533
3804
  return;
3534
3805
  }
3535
3806
  blockType = contentBlockType;
3807
+ if (!TEXT_OR_THINKING_BLOCK_TYPES.has(contentBlockType)) {
3808
+ heldBackText.length = 0;
3809
+ }
3536
3810
  switch (contentBlockType) {
3537
3811
  case "text": {
3812
+ if (jsonResponseToolName != null) {
3813
+ const entry = {
3814
+ id: String(value.index),
3815
+ text: "",
3816
+ citations: []
3817
+ };
3818
+ heldBackText.push(entry);
3819
+ heldBackTextByIndex.set(value.index, entry);
3820
+ return;
3821
+ }
3538
3822
  contentBlocks[value.index] = {
3539
3823
  type: "text",
3540
3824
  citations: []
@@ -3595,6 +3879,11 @@ var AnthropicMessagesLanguageModel = class {
3595
3879
  id: String(value.index)
3596
3880
  });
3597
3881
  } else if (value.content_block.toolset_name != null) {
3882
+ validateToolsetMember({
3883
+ toolsetName: value.content_block.toolset_name,
3884
+ memberName: value.content_block.name,
3885
+ tools: args.tools
3886
+ });
3598
3887
  contentBlocks[value.index] = {
3599
3888
  type: "tool-call",
3600
3889
  toolCallId: value.content_block.id,
@@ -3824,11 +4113,14 @@ var AnthropicMessagesLanguageModel = class {
3824
4113
  if (contentBlock.toolset != null) {
3825
4114
  let memberInput = {};
3826
4115
  try {
3827
- memberInput = contentBlock.input === "" ? {} : JSON.parse(contentBlock.input);
4116
+ memberInput = contentBlock.input === "" ? {} : (0, import_provider_utils12.secureJsonParse)(contentBlock.input);
3828
4117
  } catch (e) {
3829
- memberInput = void 0;
4118
+ throw new import_provider3.InvalidResponseDataError({
4119
+ data: contentBlock.input,
4120
+ message: "Invalid toolset member input JSON."
4121
+ });
3830
4122
  }
3831
- contentBlock.input = memberInput === void 0 ? contentBlock.input : JSON.stringify(
4123
+ contentBlock.input = JSON.stringify(
3832
4124
  toToolsetMemberInput({
3833
4125
  memberName: contentBlock.toolset.memberName,
3834
4126
  input: memberInput
@@ -3870,6 +4162,8 @@ var AnthropicMessagesLanguageModel = class {
3870
4162
  switch (deltaType) {
3871
4163
  case "text_delta": {
3872
4164
  if (jsonResponseToolName != null) {
4165
+ const entry = heldBackTextByIndex.get(value.index);
4166
+ if (entry != null) entry.text += value.delta.text;
3873
4167
  return;
3874
4168
  }
3875
4169
  controller.enqueue({
@@ -3948,8 +4242,10 @@ var AnthropicMessagesLanguageModel = class {
3948
4242
  case "citations_delta": {
3949
4243
  const citation = value.delta.citation;
3950
4244
  const contentBlock = contentBlocks[value.index];
3951
- if ((contentBlock == null ? void 0 : contentBlock.type) === "text" && citation.type === "web_search_result_location") {
3952
- contentBlock.citations.push(citation);
4245
+ if (citation.type === "web_search_result_location") {
4246
+ if ((contentBlock == null ? void 0 : contentBlock.type) === "text")
4247
+ contentBlock.citations.push(citation);
4248
+ (_b2 = heldBackTextByIndex.get(value.index)) == null ? void 0 : _b2.citations.push(citation);
3953
4249
  }
3954
4250
  const source = createCitationSource(
3955
4251
  citation,
@@ -3986,16 +4282,17 @@ var AnthropicMessagesLanguageModel = class {
3986
4282
  }
3987
4283
  isMessageOpen = true;
3988
4284
  activeMessageId = value.message.id;
4285
+ heldBackTextByIndex.clear();
3989
4286
  usage.inputTokens = value.message.usage.input_tokens;
3990
- usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
4287
+ usage.cachedInputTokens = (_c = value.message.usage.cache_read_input_tokens) != null ? _c : void 0;
3991
4288
  rawUsage = {
3992
4289
  ...value.message.usage
3993
4290
  };
3994
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
4291
+ cacheCreationInputTokens = (_d = value.message.usage.cache_creation_input_tokens) != null ? _d : null;
3995
4292
  controller.enqueue({
3996
4293
  type: "response-metadata",
3997
- id: (_d = value.message.id) != null ? _d : void 0,
3998
- modelId: (_e = value.message.model) != null ? _e : void 0
4294
+ id: (_e = value.message.id) != null ? _e : void 0,
4295
+ modelId: (_f = value.message.model) != null ? _f : void 0
3999
4296
  });
4000
4297
  return;
4001
4298
  }
@@ -4009,7 +4306,7 @@ var AnthropicMessagesLanguageModel = class {
4009
4306
  if (value.usage.iterations != null) {
4010
4307
  iterations = value.usage.iterations;
4011
4308
  }
4012
- const servedByFallback = (_f = value.usage.iterations) == null ? void 0 : _f.some(
4309
+ const servedByFallback = (_g = value.usage.iterations) == null ? void 0 : _g.some(
4013
4310
  (iter) => iter.type === "fallback_message"
4014
4311
  );
4015
4312
  if (value.usage.iterations && value.usage.iterations.length > 0 && !servedByFallback) {
@@ -4028,21 +4325,22 @@ var AnthropicMessagesLanguageModel = class {
4028
4325
  }
4029
4326
  usage.outputTokens = value.usage.output_tokens;
4030
4327
  }
4031
- usage.totalTokens = ((_g = usage.inputTokens) != null ? _g : 0) + ((_h = usage.outputTokens) != null ? _h : 0);
4328
+ usage.totalTokens = ((_h = usage.inputTokens) != null ? _h : 0) + ((_i = usage.outputTokens) != null ? _i : 0);
4329
+ rawStopReason = value.delta.stop_reason;
4032
4330
  finishReason = mapAnthropicStopReason({
4033
4331
  finishReason: value.delta.stop_reason,
4034
4332
  isJsonResponseFromTool
4035
4333
  });
4036
4334
  stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
4037
- stopSequence = (_i = value.delta.stop_sequence) != null ? _i : null;
4335
+ stopSequence = (_j = value.delta.stop_sequence) != null ? _j : null;
4038
4336
  container = value.delta.container != null ? {
4039
4337
  expiresAt: value.delta.container.expires_at,
4040
4338
  id: value.delta.container.id,
4041
- skills: (_k = (_j = value.delta.container.skills) == null ? void 0 : _j.map((skill) => ({
4339
+ skills: (_l = (_k = value.delta.container.skills) == null ? void 0 : _k.map((skill) => ({
4042
4340
  type: skill.type,
4043
4341
  skillId: skill.skill_id,
4044
4342
  version: skill.version
4045
- }))) != null ? _k : null
4343
+ }))) != null ? _l : null
4046
4344
  } : null;
4047
4345
  if (value.context_management != null) {
4048
4346
  contextManagement = {
@@ -4080,6 +4378,15 @@ var AnthropicMessagesLanguageModel = class {
4080
4378
  case "message_stop": {
4081
4379
  isMessageOpen = false;
4082
4380
  activeMessageId = void 0;
4381
+ if (!isJsonResponseFromTool && !isContinuationStopReason(rawStopReason)) {
4382
+ for (const { id, text, citations } of heldBackText) {
4383
+ if (text.length === 0) continue;
4384
+ controller.enqueue({ type: "text-start", id });
4385
+ controller.enqueue({ type: "text-delta", id, delta: text });
4386
+ controller.enqueue(createTextEnd(id, citations));
4387
+ }
4388
+ }
4389
+ heldBackText.length = 0;
4083
4390
  controller.enqueue({
4084
4391
  type: "finish",
4085
4392
  finishReason,
@@ -4483,14 +4790,14 @@ var codeExecution_20260120InputSchema = (0, import_provider_utils15.lazySchema)(
4483
4790
  ])
4484
4791
  )
4485
4792
  );
4486
- var factory6 = (0, import_provider_utils15.createProviderDefinedToolFactoryWithOutputSchema)({
4793
+ var factory7 = (0, import_provider_utils15.createProviderDefinedToolFactoryWithOutputSchema)({
4487
4794
  id: "anthropic.code_execution_20260120",
4488
4795
  name: "code_execution",
4489
4796
  inputSchema: codeExecution_20260120InputSchema,
4490
4797
  outputSchema: codeExecution_20260120OutputSchema
4491
4798
  });
4492
4799
  var codeExecution_20260120 = (args = {}) => {
4493
- return factory6(args);
4800
+ return factory7(args);
4494
4801
  };
4495
4802
 
4496
4803
  // src/tool/computer_20241022.ts