@ai-sdk/anthropic 3.0.0-beta.43 → 3.0.0-beta.45

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.mjs CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "3.0.0-beta.43" : "0.0.0-test";
14
+ var VERSION = true ? "3.0.0-beta.45" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
@@ -625,7 +625,16 @@ var anthropicProviderOptions = z3.object({
625
625
  version: z3.string().optional()
626
626
  })
627
627
  ).optional()
628
- }).optional()
628
+ }).optional(),
629
+ /**
630
+ * Whether to enable tool streaming (and structured output streaming).
631
+ *
632
+ * When set to false, the model will return all tool calls and results
633
+ * at once after a delay.
634
+ *
635
+ * @default true
636
+ */
637
+ toolStreaming: z3.boolean().optional()
629
638
  });
630
639
 
631
640
  // src/anthropic-prepare-tools.ts
@@ -1899,9 +1908,10 @@ var AnthropicMessagesLanguageModel = class {
1899
1908
  seed,
1900
1909
  tools,
1901
1910
  toolChoice,
1902
- providerOptions
1911
+ providerOptions,
1912
+ stream
1903
1913
  }) {
1904
- var _a, _b, _c, _d;
1914
+ var _a, _b, _c, _d, _e;
1905
1915
  const warnings = [];
1906
1916
  if (frequencyPenalty != null) {
1907
1917
  warnings.push({
@@ -1928,12 +1938,6 @@ var AnthropicMessagesLanguageModel = class {
1928
1938
  setting: "responseFormat",
1929
1939
  details: "JSON response format requires a schema. The response format is ignored."
1930
1940
  });
1931
- } else if (tools != null) {
1932
- warnings.push({
1933
- type: "unsupported-setting",
1934
- setting: "tools",
1935
- details: "JSON response format does not support tools. The provided tools are ignored."
1936
- });
1937
1941
  }
1938
1942
  }
1939
1943
  const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
@@ -2057,6 +2061,9 @@ var AnthropicMessagesLanguageModel = class {
2057
2061
  });
2058
2062
  }
2059
2063
  }
2064
+ if (stream && ((_e = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _e : true)) {
2065
+ betas.add("fine-grained-tool-streaming-2025-05-14");
2066
+ }
2060
2067
  const {
2061
2068
  tools: anthropicTools2,
2062
2069
  toolChoice: anthropicToolChoice,
@@ -2064,8 +2071,8 @@ var AnthropicMessagesLanguageModel = class {
2064
2071
  betas: toolsBetas
2065
2072
  } = await prepareTools(
2066
2073
  jsonResponseTool != null ? {
2067
- tools: [jsonResponseTool],
2068
- toolChoice: { type: "tool", toolName: jsonResponseTool.name },
2074
+ tools: [...tools != null ? tools : [], jsonResponseTool],
2075
+ toolChoice: { type: "required" },
2069
2076
  disableParallelToolUse: true,
2070
2077
  cacheControlValidator
2071
2078
  } : {
@@ -2080,7 +2087,9 @@ var AnthropicMessagesLanguageModel = class {
2080
2087
  args: {
2081
2088
  ...baseArgs,
2082
2089
  tools: anthropicTools2,
2083
- tool_choice: anthropicToolChoice
2090
+ tool_choice: anthropicToolChoice,
2091
+ stream: stream === true ? true : void 0
2092
+ // do not send when not streaming
2084
2093
  },
2085
2094
  warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
2086
2095
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
@@ -2130,7 +2139,10 @@ var AnthropicMessagesLanguageModel = class {
2130
2139
  }
2131
2140
  async doGenerate(options) {
2132
2141
  var _a, _b, _c, _d, _e, _f, _g, _h;
2133
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2142
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2143
+ ...options,
2144
+ stream: false
2145
+ });
2134
2146
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2135
2147
  const {
2136
2148
  responseHeaders,
@@ -2149,6 +2161,7 @@ var AnthropicMessagesLanguageModel = class {
2149
2161
  });
2150
2162
  const content = [];
2151
2163
  const mcpToolCalls = {};
2164
+ let isJsonResponseFromTool = false;
2152
2165
  for (const part of response.content) {
2153
2166
  switch (part.type) {
2154
2167
  case "text": {
@@ -2194,18 +2207,21 @@ var AnthropicMessagesLanguageModel = class {
2194
2207
  break;
2195
2208
  }
2196
2209
  case "tool_use": {
2197
- content.push(
2198
- // when a json response tool is used, the tool call becomes the text:
2199
- usesJsonResponseTool ? {
2210
+ const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2211
+ if (isJsonResponseTool) {
2212
+ isJsonResponseFromTool = true;
2213
+ content.push({
2200
2214
  type: "text",
2201
2215
  text: JSON.stringify(part.input)
2202
- } : {
2216
+ });
2217
+ } else {
2218
+ content.push({
2203
2219
  type: "tool-call",
2204
2220
  toolCallId: part.id,
2205
2221
  toolName: part.name,
2206
2222
  input: JSON.stringify(part.input)
2207
- }
2208
- );
2223
+ });
2224
+ }
2209
2225
  break;
2210
2226
  }
2211
2227
  case "server_tool_use": {
@@ -2392,7 +2408,7 @@ var AnthropicMessagesLanguageModel = class {
2392
2408
  content,
2393
2409
  finishReason: mapAnthropicStopReason({
2394
2410
  finishReason: response.stop_reason,
2395
- isJsonResponseFromTool: usesJsonResponseTool
2411
+ isJsonResponseFromTool
2396
2412
  }),
2397
2413
  usage: {
2398
2414
  inputTokens: response.usage.input_tokens,
@@ -2427,9 +2443,16 @@ var AnthropicMessagesLanguageModel = class {
2427
2443
  };
2428
2444
  }
2429
2445
  async doStream(options) {
2430
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2446
+ const {
2447
+ args: body,
2448
+ warnings,
2449
+ betas,
2450
+ usesJsonResponseTool
2451
+ } = await this.getArgs({
2452
+ ...options,
2453
+ stream: true
2454
+ });
2431
2455
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2432
- const body = { ...args, stream: true };
2433
2456
  const { responseHeaders, value: response } = await postJsonToApi({
2434
2457
  url: this.buildRequestUrl(true),
2435
2458
  headers: await this.getHeaders({ betas, headers: options.headers }),
@@ -2453,6 +2476,7 @@ var AnthropicMessagesLanguageModel = class {
2453
2476
  let cacheCreationInputTokens = null;
2454
2477
  let stopSequence = null;
2455
2478
  let container = null;
2479
+ let isJsonResponseFromTool = false;
2456
2480
  let blockType = void 0;
2457
2481
  const generateId3 = this.generateId;
2458
2482
  return {
@@ -2481,6 +2505,9 @@ var AnthropicMessagesLanguageModel = class {
2481
2505
  blockType = contentBlockType;
2482
2506
  switch (contentBlockType) {
2483
2507
  case "text": {
2508
+ if (usesJsonResponseTool) {
2509
+ return;
2510
+ }
2484
2511
  contentBlocks[value.index] = { type: "text" };
2485
2512
  controller.enqueue({
2486
2513
  type: "text-start",
@@ -2510,20 +2537,28 @@ var AnthropicMessagesLanguageModel = class {
2510
2537
  return;
2511
2538
  }
2512
2539
  case "tool_use": {
2513
- contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2514
- type: "tool-call",
2515
- toolCallId: part.id,
2516
- toolName: part.name,
2517
- input: "",
2518
- firstDelta: true
2519
- };
2520
- controller.enqueue(
2521
- usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2540
+ const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2541
+ if (isJsonResponseTool) {
2542
+ isJsonResponseFromTool = true;
2543
+ contentBlocks[value.index] = { type: "text" };
2544
+ controller.enqueue({
2545
+ type: "text-start",
2546
+ id: String(value.index)
2547
+ });
2548
+ } else {
2549
+ contentBlocks[value.index] = {
2550
+ type: "tool-call",
2551
+ toolCallId: part.id,
2552
+ toolName: part.name,
2553
+ input: "",
2554
+ firstDelta: true
2555
+ };
2556
+ controller.enqueue({
2522
2557
  type: "tool-input-start",
2523
2558
  id: part.id,
2524
2559
  toolName: part.name
2525
- }
2526
- );
2560
+ });
2561
+ }
2527
2562
  return;
2528
2563
  }
2529
2564
  case "server_tool_use": {
@@ -2739,7 +2774,8 @@ var AnthropicMessagesLanguageModel = class {
2739
2774
  break;
2740
2775
  }
2741
2776
  case "tool-call":
2742
- if (!usesJsonResponseTool) {
2777
+ const isJsonResponseTool = usesJsonResponseTool && contentBlock.toolName === "json";
2778
+ if (!isJsonResponseTool) {
2743
2779
  controller.enqueue({
2744
2780
  type: "tool-input-end",
2745
2781
  id: contentBlock.toolCallId
@@ -2803,7 +2839,7 @@ var AnthropicMessagesLanguageModel = class {
2803
2839
  if (delta.length === 0) {
2804
2840
  return;
2805
2841
  }
2806
- if (usesJsonResponseTool) {
2842
+ if (isJsonResponseFromTool) {
2807
2843
  if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2808
2844
  return;
2809
2845
  }
@@ -2868,7 +2904,7 @@ var AnthropicMessagesLanguageModel = class {
2868
2904
  usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2869
2905
  finishReason = mapAnthropicStopReason({
2870
2906
  finishReason: value.delta.stop_reason,
2871
- isJsonResponseFromTool: usesJsonResponseTool
2907
+ isJsonResponseFromTool
2872
2908
  });
2873
2909
  stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2874
2910
  container = value.delta.container != null ? {