@ai-sdk/anthropic 3.0.0-beta.62 → 3.0.0-beta.64

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
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
31
31
  var import_provider_utils20 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/version.ts
34
- var VERSION = true ? "3.0.0-beta.62" : "0.0.0-test";
34
+ var VERSION = true ? "3.0.0-beta.64" : "0.0.0-test";
35
35
 
36
36
  // src/anthropic-messages-language-model.ts
37
37
  var import_provider3 = require("@ai-sdk/provider");
@@ -692,8 +692,8 @@ var CacheControlValidator = class {
692
692
  }
693
693
  if (!context.canCache) {
694
694
  this.warnings.push({
695
- type: "unsupported-setting",
696
- setting: "cacheControl",
695
+ type: "unsupported",
696
+ feature: "cache_control on non-cacheable context",
697
697
  details: `cache_control cannot be set on ${context.type}. It will be ignored.`
698
698
  });
699
699
  return void 0;
@@ -701,8 +701,8 @@ var CacheControlValidator = class {
701
701
  this.breakpointCount++;
702
702
  if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
703
703
  this.warnings.push({
704
- type: "unsupported-setting",
705
- setting: "cacheControl",
704
+ type: "unsupported",
705
+ feature: "cacheControl breakpoint limit",
706
706
  details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
707
707
  });
708
708
  return void 0;
@@ -1028,14 +1028,20 @@ async function prepareTools({
1028
1028
  break;
1029
1029
  }
1030
1030
  default: {
1031
- toolWarnings.push({ type: "unsupported-tool", tool });
1031
+ toolWarnings.push({
1032
+ type: "unsupported",
1033
+ feature: `provider-defined tool ${tool.id}`
1034
+ });
1032
1035
  break;
1033
1036
  }
1034
1037
  }
1035
1038
  break;
1036
1039
  }
1037
1040
  default: {
1038
- toolWarnings.push({ type: "unsupported-tool", tool });
1041
+ toolWarnings.push({
1042
+ type: "unsupported",
1043
+ feature: `tool ${tool}`
1044
+ });
1039
1045
  break;
1040
1046
  }
1041
1047
  }
@@ -1922,34 +1928,25 @@ var AnthropicMessagesLanguageModel = class {
1922
1928
  var _a, _b, _c, _d, _e, _f;
1923
1929
  const warnings = [];
1924
1930
  if (frequencyPenalty != null) {
1925
- warnings.push({
1926
- type: "unsupported-setting",
1927
- setting: "frequencyPenalty"
1928
- });
1931
+ warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
1929
1932
  }
1930
1933
  if (presencePenalty != null) {
1931
- warnings.push({
1932
- type: "unsupported-setting",
1933
- setting: "presencePenalty"
1934
- });
1934
+ warnings.push({ type: "unsupported", feature: "presencePenalty" });
1935
1935
  }
1936
1936
  if (seed != null) {
1937
- warnings.push({
1938
- type: "unsupported-setting",
1939
- setting: "seed"
1940
- });
1937
+ warnings.push({ type: "unsupported", feature: "seed" });
1941
1938
  }
1942
1939
  if (temperature != null && temperature > 1) {
1943
1940
  warnings.push({
1944
- type: "unsupported-setting",
1945
- setting: "temperature",
1941
+ type: "unsupported",
1942
+ feature: "temperature",
1946
1943
  details: `${temperature} exceeds anthropic maximum of 1.0. clamped to 1.0`
1947
1944
  });
1948
1945
  temperature = 1;
1949
1946
  } else if (temperature != null && temperature < 0) {
1950
1947
  warnings.push({
1951
- type: "unsupported-setting",
1952
- setting: "temperature",
1948
+ type: "unsupported",
1949
+ feature: "temperature",
1953
1950
  details: `${temperature} is below anthropic minimum of 0. clamped to 0`
1954
1951
  });
1955
1952
  temperature = 0;
@@ -1957,8 +1954,8 @@ var AnthropicMessagesLanguageModel = class {
1957
1954
  if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
1958
1955
  if (responseFormat.schema == null) {
1959
1956
  warnings.push({
1960
- type: "unsupported-setting",
1961
- setting: "responseFormat",
1957
+ type: "unsupported",
1958
+ feature: "responseFormat",
1962
1959
  details: "JSON response format requires a schema. The response format is ignored."
1963
1960
  });
1964
1961
  }
@@ -2051,24 +2048,24 @@ var AnthropicMessagesLanguageModel = class {
2051
2048
  if (baseArgs.temperature != null) {
2052
2049
  baseArgs.temperature = void 0;
2053
2050
  warnings.push({
2054
- type: "unsupported-setting",
2055
- setting: "temperature",
2051
+ type: "unsupported",
2052
+ feature: "temperature",
2056
2053
  details: "temperature is not supported when thinking is enabled"
2057
2054
  });
2058
2055
  }
2059
2056
  if (topK != null) {
2060
2057
  baseArgs.top_k = void 0;
2061
2058
  warnings.push({
2062
- type: "unsupported-setting",
2063
- setting: "topK",
2059
+ type: "unsupported",
2060
+ feature: "topK",
2064
2061
  details: "topK is not supported when thinking is enabled"
2065
2062
  });
2066
2063
  }
2067
2064
  if (topP != null) {
2068
2065
  baseArgs.top_p = void 0;
2069
2066
  warnings.push({
2070
- type: "unsupported-setting",
2071
- setting: "topP",
2067
+ type: "unsupported",
2068
+ feature: "topP",
2072
2069
  details: "topP is not supported when thinking is enabled"
2073
2070
  });
2074
2071
  }
@@ -2077,8 +2074,8 @@ var AnthropicMessagesLanguageModel = class {
2077
2074
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2078
2075
  if (maxOutputTokens != null) {
2079
2076
  warnings.push({
2080
- type: "unsupported-setting",
2081
- setting: "maxOutputTokens",
2077
+ type: "unsupported",
2078
+ feature: "maxOutputTokens",
2082
2079
  details: `${baseArgs.max_tokens} (maxOutputTokens + thinkingBudget) is greater than ${this.modelId} ${maxOutputTokensForModel} max output tokens. The max output tokens have been limited to ${maxOutputTokensForModel}.`
2083
2080
  });
2084
2081
  }
@@ -2504,8 +2501,9 @@ var AnthropicMessagesLanguageModel = class {
2504
2501
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2505
2502
  });
2506
2503
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2504
+ const url = this.buildRequestUrl(true);
2507
2505
  const { responseHeaders, value: response } = await (0, import_provider_utils11.postJsonToApi)({
2508
- url: this.buildRequestUrl(true),
2506
+ url,
2509
2507
  headers: await this.getHeaders({ betas, headers: options.headers }),
2510
2508
  body: this.transformRequestBody(body),
2511
2509
  failedResponseHandler: anthropicFailedResponseHandler,
@@ -2530,473 +2528,523 @@ var AnthropicMessagesLanguageModel = class {
2530
2528
  let isJsonResponseFromTool = false;
2531
2529
  let blockType = void 0;
2532
2530
  const generateId3 = this.generateId;
2533
- return {
2534
- stream: response.pipeThrough(
2535
- new TransformStream({
2536
- start(controller) {
2537
- controller.enqueue({ type: "stream-start", warnings });
2538
- },
2539
- transform(chunk, controller) {
2540
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2541
- if (options.includeRawChunks) {
2542
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2531
+ let isFirstChunk = true;
2532
+ let stream = void 0;
2533
+ const returnPromise = new import_provider_utils11.DelayedPromise();
2534
+ const transformedStream = response.pipeThrough(
2535
+ new TransformStream({
2536
+ start(controller) {
2537
+ controller.enqueue({ type: "stream-start", warnings });
2538
+ },
2539
+ async flush() {
2540
+ if (returnPromise.isPending()) {
2541
+ returnPromise.resolve({
2542
+ stream,
2543
+ request: { body },
2544
+ response: { headers: responseHeaders }
2545
+ });
2546
+ }
2547
+ },
2548
+ transform(chunk, controller) {
2549
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2550
+ if (options.includeRawChunks) {
2551
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2552
+ }
2553
+ if (!chunk.success) {
2554
+ controller.enqueue({ type: "error", error: chunk.error });
2555
+ return;
2556
+ }
2557
+ if (isFirstChunk) {
2558
+ if (chunk.value.type === "error") {
2559
+ returnPromise.reject(
2560
+ new import_provider3.APICallError({
2561
+ message: chunk.value.error.message,
2562
+ url,
2563
+ requestBodyValues: body,
2564
+ statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
2565
+ responseHeaders,
2566
+ responseBody: JSON.stringify(chunk.value.error),
2567
+ isRetryable: chunk.value.error.type === "overloaded_error"
2568
+ })
2569
+ );
2570
+ controller.terminate();
2571
+ return;
2543
2572
  }
2544
- if (!chunk.success) {
2545
- controller.enqueue({ type: "error", error: chunk.error });
2573
+ isFirstChunk = false;
2574
+ returnPromise.resolve({
2575
+ stream,
2576
+ request: { body },
2577
+ response: { headers: responseHeaders }
2578
+ });
2579
+ }
2580
+ const value = chunk.value;
2581
+ switch (value.type) {
2582
+ case "ping": {
2546
2583
  return;
2547
2584
  }
2548
- const value = chunk.value;
2549
- switch (value.type) {
2550
- case "ping": {
2551
- return;
2552
- }
2553
- case "content_block_start": {
2554
- const part = value.content_block;
2555
- const contentBlockType = part.type;
2556
- blockType = contentBlockType;
2557
- switch (contentBlockType) {
2558
- case "text": {
2559
- if (usesJsonResponseTool) {
2560
- return;
2585
+ case "content_block_start": {
2586
+ const part = value.content_block;
2587
+ const contentBlockType = part.type;
2588
+ blockType = contentBlockType;
2589
+ switch (contentBlockType) {
2590
+ case "text": {
2591
+ if (usesJsonResponseTool) {
2592
+ return;
2593
+ }
2594
+ contentBlocks[value.index] = { type: "text" };
2595
+ controller.enqueue({
2596
+ type: "text-start",
2597
+ id: String(value.index)
2598
+ });
2599
+ return;
2600
+ }
2601
+ case "thinking": {
2602
+ contentBlocks[value.index] = { type: "reasoning" };
2603
+ controller.enqueue({
2604
+ type: "reasoning-start",
2605
+ id: String(value.index)
2606
+ });
2607
+ return;
2608
+ }
2609
+ case "redacted_thinking": {
2610
+ contentBlocks[value.index] = { type: "reasoning" };
2611
+ controller.enqueue({
2612
+ type: "reasoning-start",
2613
+ id: String(value.index),
2614
+ providerMetadata: {
2615
+ anthropic: {
2616
+ redactedData: part.data
2617
+ }
2561
2618
  }
2619
+ });
2620
+ return;
2621
+ }
2622
+ case "tool_use": {
2623
+ const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2624
+ if (isJsonResponseTool) {
2625
+ isJsonResponseFromTool = true;
2562
2626
  contentBlocks[value.index] = { type: "text" };
2563
2627
  controller.enqueue({
2564
2628
  type: "text-start",
2565
2629
  id: String(value.index)
2566
2630
  });
2567
- return;
2568
- }
2569
- case "thinking": {
2570
- contentBlocks[value.index] = { type: "reasoning" };
2631
+ } else {
2632
+ contentBlocks[value.index] = {
2633
+ type: "tool-call",
2634
+ toolCallId: part.id,
2635
+ toolName: part.name,
2636
+ input: "",
2637
+ firstDelta: true
2638
+ };
2571
2639
  controller.enqueue({
2572
- type: "reasoning-start",
2573
- id: String(value.index)
2640
+ type: "tool-input-start",
2641
+ id: part.id,
2642
+ toolName: part.name
2574
2643
  });
2575
- return;
2576
2644
  }
2577
- case "redacted_thinking": {
2578
- contentBlocks[value.index] = { type: "reasoning" };
2645
+ return;
2646
+ }
2647
+ case "server_tool_use": {
2648
+ if ([
2649
+ "web_fetch",
2650
+ "web_search",
2651
+ // code execution 20250825:
2652
+ "code_execution",
2653
+ // code execution 20250825 text editor:
2654
+ "text_editor_code_execution",
2655
+ // code execution 20250825 bash:
2656
+ "bash_code_execution"
2657
+ ].includes(part.name)) {
2658
+ contentBlocks[value.index] = {
2659
+ type: "tool-call",
2660
+ toolCallId: part.id,
2661
+ toolName: part.name,
2662
+ input: "",
2663
+ providerExecuted: true,
2664
+ firstDelta: true
2665
+ };
2666
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2579
2667
  controller.enqueue({
2580
- type: "reasoning-start",
2581
- id: String(value.index),
2582
- providerMetadata: {
2583
- anthropic: {
2584
- redactedData: part.data
2585
- }
2586
- }
2668
+ type: "tool-input-start",
2669
+ id: part.id,
2670
+ toolName: mappedToolName,
2671
+ providerExecuted: true
2587
2672
  });
2588
- return;
2589
- }
2590
- case "tool_use": {
2591
- const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2592
- if (isJsonResponseTool) {
2593
- isJsonResponseFromTool = true;
2594
- contentBlocks[value.index] = { type: "text" };
2595
- controller.enqueue({
2596
- type: "text-start",
2597
- id: String(value.index)
2598
- });
2599
- } else {
2600
- contentBlocks[value.index] = {
2601
- type: "tool-call",
2602
- toolCallId: part.id,
2603
- toolName: part.name,
2604
- input: "",
2605
- firstDelta: true
2606
- };
2607
- controller.enqueue({
2608
- type: "tool-input-start",
2609
- id: part.id,
2610
- toolName: part.name
2611
- });
2612
- }
2613
- return;
2614
2673
  }
2615
- case "server_tool_use": {
2616
- if ([
2617
- "web_fetch",
2618
- "web_search",
2619
- // code execution 20250825:
2620
- "code_execution",
2621
- // code execution 20250825 text editor:
2622
- "text_editor_code_execution",
2623
- // code execution 20250825 bash:
2624
- "bash_code_execution"
2625
- ].includes(part.name)) {
2626
- contentBlocks[value.index] = {
2627
- type: "tool-call",
2628
- toolCallId: part.id,
2629
- toolName: part.name,
2630
- input: "",
2631
- providerExecuted: true,
2632
- firstDelta: true
2633
- };
2634
- const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2635
- controller.enqueue({
2636
- type: "tool-input-start",
2637
- id: part.id,
2638
- toolName: mappedToolName,
2639
- providerExecuted: true
2640
- });
2641
- }
2642
- return;
2643
- }
2644
- case "web_fetch_tool_result": {
2645
- if (part.content.type === "web_fetch_result") {
2646
- controller.enqueue({
2647
- type: "tool-result",
2648
- toolCallId: part.tool_use_id,
2649
- toolName: "web_fetch",
2650
- result: {
2651
- type: "web_fetch_result",
2652
- url: part.content.url,
2653
- retrievedAt: part.content.retrieved_at,
2654
- content: {
2655
- type: part.content.content.type,
2656
- title: part.content.content.title,
2657
- citations: part.content.content.citations,
2658
- source: {
2659
- type: part.content.content.source.type,
2660
- mediaType: part.content.content.source.media_type,
2661
- data: part.content.content.source.data
2662
- }
2674
+ return;
2675
+ }
2676
+ case "web_fetch_tool_result": {
2677
+ if (part.content.type === "web_fetch_result") {
2678
+ controller.enqueue({
2679
+ type: "tool-result",
2680
+ toolCallId: part.tool_use_id,
2681
+ toolName: "web_fetch",
2682
+ result: {
2683
+ type: "web_fetch_result",
2684
+ url: part.content.url,
2685
+ retrievedAt: part.content.retrieved_at,
2686
+ content: {
2687
+ type: part.content.content.type,
2688
+ title: part.content.content.title,
2689
+ citations: part.content.content.citations,
2690
+ source: {
2691
+ type: part.content.content.source.type,
2692
+ mediaType: part.content.content.source.media_type,
2693
+ data: part.content.content.source.data
2663
2694
  }
2664
2695
  }
2665
- });
2666
- } else if (part.content.type === "web_fetch_tool_result_error") {
2667
- controller.enqueue({
2668
- type: "tool-result",
2669
- toolCallId: part.tool_use_id,
2670
- toolName: "web_fetch",
2671
- isError: true,
2672
- result: {
2673
- type: "web_fetch_tool_result_error",
2674
- errorCode: part.content.error_code
2675
- }
2676
- });
2677
- }
2678
- return;
2696
+ }
2697
+ });
2698
+ } else if (part.content.type === "web_fetch_tool_result_error") {
2699
+ controller.enqueue({
2700
+ type: "tool-result",
2701
+ toolCallId: part.tool_use_id,
2702
+ toolName: "web_fetch",
2703
+ isError: true,
2704
+ result: {
2705
+ type: "web_fetch_tool_result_error",
2706
+ errorCode: part.content.error_code
2707
+ }
2708
+ });
2679
2709
  }
2680
- case "web_search_tool_result": {
2681
- if (Array.isArray(part.content)) {
2682
- controller.enqueue({
2683
- type: "tool-result",
2684
- toolCallId: part.tool_use_id,
2685
- toolName: "web_search",
2686
- result: part.content.map((result) => {
2687
- var _a2;
2688
- return {
2689
- url: result.url,
2690
- title: result.title,
2691
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2692
- encryptedContent: result.encrypted_content,
2693
- type: result.type
2694
- };
2695
- })
2696
- });
2697
- for (const result of part.content) {
2698
- controller.enqueue({
2699
- type: "source",
2700
- sourceType: "url",
2701
- id: generateId3(),
2710
+ return;
2711
+ }
2712
+ case "web_search_tool_result": {
2713
+ if (Array.isArray(part.content)) {
2714
+ controller.enqueue({
2715
+ type: "tool-result",
2716
+ toolCallId: part.tool_use_id,
2717
+ toolName: "web_search",
2718
+ result: part.content.map((result) => {
2719
+ var _a2;
2720
+ return {
2702
2721
  url: result.url,
2703
2722
  title: result.title,
2704
- providerMetadata: {
2705
- anthropic: {
2706
- pageAge: (_a = result.page_age) != null ? _a : null
2707
- }
2708
- }
2709
- });
2710
- }
2711
- } else {
2712
- controller.enqueue({
2713
- type: "tool-result",
2714
- toolCallId: part.tool_use_id,
2715
- toolName: "web_search",
2716
- isError: true,
2717
- result: {
2718
- type: "web_search_tool_result_error",
2719
- errorCode: part.content.error_code
2720
- }
2721
- });
2722
- }
2723
- return;
2724
- }
2725
- // code execution 20250522:
2726
- case "code_execution_tool_result": {
2727
- if (part.content.type === "code_execution_result") {
2728
- controller.enqueue({
2729
- type: "tool-result",
2730
- toolCallId: part.tool_use_id,
2731
- toolName: "code_execution",
2732
- result: {
2733
- type: part.content.type,
2734
- stdout: part.content.stdout,
2735
- stderr: part.content.stderr,
2736
- return_code: part.content.return_code
2737
- }
2738
- });
2739
- } else if (part.content.type === "code_execution_tool_result_error") {
2723
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2724
+ encryptedContent: result.encrypted_content,
2725
+ type: result.type
2726
+ };
2727
+ })
2728
+ });
2729
+ for (const result of part.content) {
2740
2730
  controller.enqueue({
2741
- type: "tool-result",
2742
- toolCallId: part.tool_use_id,
2743
- toolName: "code_execution",
2744
- isError: true,
2745
- result: {
2746
- type: "code_execution_tool_result_error",
2747
- errorCode: part.content.error_code
2731
+ type: "source",
2732
+ sourceType: "url",
2733
+ id: generateId3(),
2734
+ url: result.url,
2735
+ title: result.title,
2736
+ providerMetadata: {
2737
+ anthropic: {
2738
+ pageAge: (_a = result.page_age) != null ? _a : null
2739
+ }
2748
2740
  }
2749
2741
  });
2750
2742
  }
2751
- return;
2752
- }
2753
- // code execution 20250825:
2754
- case "bash_code_execution_tool_result":
2755
- case "text_editor_code_execution_tool_result": {
2743
+ } else {
2756
2744
  controller.enqueue({
2757
2745
  type: "tool-result",
2758
2746
  toolCallId: part.tool_use_id,
2759
- toolName: "code_execution",
2760
- result: part.content
2747
+ toolName: "web_search",
2748
+ isError: true,
2749
+ result: {
2750
+ type: "web_search_tool_result_error",
2751
+ errorCode: part.content.error_code
2752
+ }
2761
2753
  });
2762
- return;
2763
2754
  }
2764
- case "mcp_tool_use": {
2765
- mcpToolCalls[part.id] = {
2766
- type: "tool-call",
2767
- toolCallId: part.id,
2768
- toolName: part.name,
2769
- input: JSON.stringify(part.input),
2770
- providerExecuted: true,
2771
- dynamic: true,
2772
- providerMetadata: {
2773
- anthropic: {
2774
- type: "mcp-tool-use",
2775
- serverName: part.server_name
2776
- }
2755
+ return;
2756
+ }
2757
+ // code execution 20250522:
2758
+ case "code_execution_tool_result": {
2759
+ if (part.content.type === "code_execution_result") {
2760
+ controller.enqueue({
2761
+ type: "tool-result",
2762
+ toolCallId: part.tool_use_id,
2763
+ toolName: "code_execution",
2764
+ result: {
2765
+ type: part.content.type,
2766
+ stdout: part.content.stdout,
2767
+ stderr: part.content.stderr,
2768
+ return_code: part.content.return_code
2777
2769
  }
2778
- };
2779
- controller.enqueue(mcpToolCalls[part.id]);
2780
- return;
2781
- }
2782
- case "mcp_tool_result": {
2770
+ });
2771
+ } else if (part.content.type === "code_execution_tool_result_error") {
2783
2772
  controller.enqueue({
2784
2773
  type: "tool-result",
2785
2774
  toolCallId: part.tool_use_id,
2786
- toolName: mcpToolCalls[part.tool_use_id].toolName,
2787
- isError: part.is_error,
2788
- result: part.content,
2789
- dynamic: true,
2790
- providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2775
+ toolName: "code_execution",
2776
+ isError: true,
2777
+ result: {
2778
+ type: "code_execution_tool_result_error",
2779
+ errorCode: part.content.error_code
2780
+ }
2791
2781
  });
2792
- return;
2793
- }
2794
- default: {
2795
- const _exhaustiveCheck = contentBlockType;
2796
- throw new Error(
2797
- `Unsupported content block type: ${_exhaustiveCheck}`
2798
- );
2799
2782
  }
2783
+ return;
2784
+ }
2785
+ // code execution 20250825:
2786
+ case "bash_code_execution_tool_result":
2787
+ case "text_editor_code_execution_tool_result": {
2788
+ controller.enqueue({
2789
+ type: "tool-result",
2790
+ toolCallId: part.tool_use_id,
2791
+ toolName: "code_execution",
2792
+ result: part.content
2793
+ });
2794
+ return;
2795
+ }
2796
+ case "mcp_tool_use": {
2797
+ mcpToolCalls[part.id] = {
2798
+ type: "tool-call",
2799
+ toolCallId: part.id,
2800
+ toolName: part.name,
2801
+ input: JSON.stringify(part.input),
2802
+ providerExecuted: true,
2803
+ dynamic: true,
2804
+ providerMetadata: {
2805
+ anthropic: {
2806
+ type: "mcp-tool-use",
2807
+ serverName: part.server_name
2808
+ }
2809
+ }
2810
+ };
2811
+ controller.enqueue(mcpToolCalls[part.id]);
2812
+ return;
2813
+ }
2814
+ case "mcp_tool_result": {
2815
+ controller.enqueue({
2816
+ type: "tool-result",
2817
+ toolCallId: part.tool_use_id,
2818
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2819
+ isError: part.is_error,
2820
+ result: part.content,
2821
+ dynamic: true,
2822
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2823
+ });
2824
+ return;
2825
+ }
2826
+ default: {
2827
+ const _exhaustiveCheck = contentBlockType;
2828
+ throw new Error(
2829
+ `Unsupported content block type: ${_exhaustiveCheck}`
2830
+ );
2800
2831
  }
2801
2832
  }
2802
- case "content_block_stop": {
2803
- if (contentBlocks[value.index] != null) {
2804
- const contentBlock = contentBlocks[value.index];
2805
- switch (contentBlock.type) {
2806
- case "text": {
2833
+ }
2834
+ case "content_block_stop": {
2835
+ if (contentBlocks[value.index] != null) {
2836
+ const contentBlock = contentBlocks[value.index];
2837
+ switch (contentBlock.type) {
2838
+ case "text": {
2839
+ controller.enqueue({
2840
+ type: "text-end",
2841
+ id: String(value.index)
2842
+ });
2843
+ break;
2844
+ }
2845
+ case "reasoning": {
2846
+ controller.enqueue({
2847
+ type: "reasoning-end",
2848
+ id: String(value.index)
2849
+ });
2850
+ break;
2851
+ }
2852
+ case "tool-call":
2853
+ const isJsonResponseTool = usesJsonResponseTool && contentBlock.toolName === "json";
2854
+ if (!isJsonResponseTool) {
2807
2855
  controller.enqueue({
2808
- type: "text-end",
2809
- id: String(value.index)
2856
+ type: "tool-input-end",
2857
+ id: contentBlock.toolCallId
2810
2858
  });
2811
- break;
2812
- }
2813
- case "reasoning": {
2859
+ const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2814
2860
  controller.enqueue({
2815
- type: "reasoning-end",
2816
- id: String(value.index)
2861
+ type: "tool-call",
2862
+ toolCallId: contentBlock.toolCallId,
2863
+ toolName,
2864
+ input: contentBlock.input,
2865
+ providerExecuted: contentBlock.providerExecuted
2817
2866
  });
2818
- break;
2819
2867
  }
2820
- case "tool-call":
2821
- const isJsonResponseTool = usesJsonResponseTool && contentBlock.toolName === "json";
2822
- if (!isJsonResponseTool) {
2823
- controller.enqueue({
2824
- type: "tool-input-end",
2825
- id: contentBlock.toolCallId
2826
- });
2827
- const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2828
- controller.enqueue({
2829
- type: "tool-call",
2830
- toolCallId: contentBlock.toolCallId,
2831
- toolName,
2832
- input: contentBlock.input,
2833
- providerExecuted: contentBlock.providerExecuted
2834
- });
2835
- }
2836
- break;
2837
- }
2838
- delete contentBlocks[value.index];
2868
+ break;
2839
2869
  }
2840
- blockType = void 0;
2841
- return;
2870
+ delete contentBlocks[value.index];
2842
2871
  }
2843
- case "content_block_delta": {
2844
- const deltaType = value.delta.type;
2845
- switch (deltaType) {
2846
- case "text_delta": {
2847
- if (usesJsonResponseTool) {
2848
- return;
2849
- }
2850
- controller.enqueue({
2851
- type: "text-delta",
2852
- id: String(value.index),
2853
- delta: value.delta.text
2854
- });
2872
+ blockType = void 0;
2873
+ return;
2874
+ }
2875
+ case "content_block_delta": {
2876
+ const deltaType = value.delta.type;
2877
+ switch (deltaType) {
2878
+ case "text_delta": {
2879
+ if (usesJsonResponseTool) {
2855
2880
  return;
2856
2881
  }
2857
- case "thinking_delta": {
2882
+ controller.enqueue({
2883
+ type: "text-delta",
2884
+ id: String(value.index),
2885
+ delta: value.delta.text
2886
+ });
2887
+ return;
2888
+ }
2889
+ case "thinking_delta": {
2890
+ controller.enqueue({
2891
+ type: "reasoning-delta",
2892
+ id: String(value.index),
2893
+ delta: value.delta.thinking
2894
+ });
2895
+ return;
2896
+ }
2897
+ case "signature_delta": {
2898
+ if (blockType === "thinking") {
2858
2899
  controller.enqueue({
2859
2900
  type: "reasoning-delta",
2860
2901
  id: String(value.index),
2861
- delta: value.delta.thinking
2902
+ delta: "",
2903
+ providerMetadata: {
2904
+ anthropic: {
2905
+ signature: value.delta.signature
2906
+ }
2907
+ }
2862
2908
  });
2863
- return;
2864
2909
  }
2865
- case "signature_delta": {
2866
- if (blockType === "thinking") {
2867
- controller.enqueue({
2868
- type: "reasoning-delta",
2869
- id: String(value.index),
2870
- delta: "",
2871
- providerMetadata: {
2872
- anthropic: {
2873
- signature: value.delta.signature
2874
- }
2875
- }
2876
- });
2877
- }
2910
+ return;
2911
+ }
2912
+ case "input_json_delta": {
2913
+ const contentBlock = contentBlocks[value.index];
2914
+ let delta = value.delta.partial_json;
2915
+ if (delta.length === 0) {
2878
2916
  return;
2879
2917
  }
2880
- case "input_json_delta": {
2881
- const contentBlock = contentBlocks[value.index];
2882
- let delta = value.delta.partial_json;
2883
- if (delta.length === 0) {
2918
+ if (isJsonResponseFromTool) {
2919
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2884
2920
  return;
2885
2921
  }
2886
- if (isJsonResponseFromTool) {
2887
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2888
- return;
2889
- }
2890
- controller.enqueue({
2891
- type: "text-delta",
2892
- id: String(value.index),
2893
- delta
2894
- });
2895
- } else {
2896
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2897
- return;
2898
- }
2899
- if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2900
- delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2901
- }
2902
- controller.enqueue({
2903
- type: "tool-input-delta",
2904
- id: contentBlock.toolCallId,
2905
- delta
2906
- });
2907
- contentBlock.input += delta;
2908
- contentBlock.firstDelta = false;
2922
+ controller.enqueue({
2923
+ type: "text-delta",
2924
+ id: String(value.index),
2925
+ delta
2926
+ });
2927
+ } else {
2928
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2929
+ return;
2909
2930
  }
2910
- return;
2911
- }
2912
- case "citations_delta": {
2913
- const citation = value.delta.citation;
2914
- const source = createCitationSource(
2915
- citation,
2916
- citationDocuments,
2917
- generateId3
2918
- );
2919
- if (source) {
2920
- controller.enqueue(source);
2931
+ if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2932
+ delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2921
2933
  }
2922
- return;
2934
+ controller.enqueue({
2935
+ type: "tool-input-delta",
2936
+ id: contentBlock.toolCallId,
2937
+ delta
2938
+ });
2939
+ contentBlock.input += delta;
2940
+ contentBlock.firstDelta = false;
2923
2941
  }
2924
- default: {
2925
- const _exhaustiveCheck = deltaType;
2926
- throw new Error(
2927
- `Unsupported delta type: ${_exhaustiveCheck}`
2928
- );
2942
+ return;
2943
+ }
2944
+ case "citations_delta": {
2945
+ const citation = value.delta.citation;
2946
+ const source = createCitationSource(
2947
+ citation,
2948
+ citationDocuments,
2949
+ generateId3
2950
+ );
2951
+ if (source) {
2952
+ controller.enqueue(source);
2929
2953
  }
2954
+ return;
2955
+ }
2956
+ default: {
2957
+ const _exhaustiveCheck = deltaType;
2958
+ throw new Error(
2959
+ `Unsupported delta type: ${_exhaustiveCheck}`
2960
+ );
2930
2961
  }
2931
2962
  }
2932
- case "message_start": {
2933
- usage.inputTokens = value.message.usage.input_tokens;
2934
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2935
- rawUsage = {
2936
- ...value.message.usage
2937
- };
2938
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2939
- controller.enqueue({
2940
- type: "response-metadata",
2941
- id: (_d = value.message.id) != null ? _d : void 0,
2942
- modelId: (_e = value.message.model) != null ? _e : void 0
2943
- });
2944
- return;
2945
- }
2946
- case "message_delta": {
2947
- usage.outputTokens = value.usage.output_tokens;
2948
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2949
- finishReason = mapAnthropicStopReason({
2950
- finishReason: value.delta.stop_reason,
2951
- isJsonResponseFromTool
2952
- });
2953
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2954
- container = value.delta.container != null ? {
2955
- expiresAt: value.delta.container.expires_at,
2956
- id: value.delta.container.id,
2957
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2958
- type: skill.type,
2959
- skillId: skill.skill_id,
2960
- version: skill.version
2961
- }))) != null ? _j : null
2962
- } : null;
2963
- rawUsage = {
2964
- ...rawUsage,
2965
- ...value.usage
2966
- };
2967
- return;
2968
- }
2969
- case "message_stop": {
2970
- controller.enqueue({
2971
- type: "finish",
2972
- finishReason,
2973
- usage,
2974
- providerMetadata: {
2975
- anthropic: {
2976
- usage: rawUsage != null ? rawUsage : null,
2977
- cacheCreationInputTokens,
2978
- stopSequence,
2979
- container
2980
- }
2963
+ }
2964
+ case "message_start": {
2965
+ usage.inputTokens = value.message.usage.input_tokens;
2966
+ usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2967
+ rawUsage = {
2968
+ ...value.message.usage
2969
+ };
2970
+ cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2971
+ controller.enqueue({
2972
+ type: "response-metadata",
2973
+ id: (_d = value.message.id) != null ? _d : void 0,
2974
+ modelId: (_e = value.message.model) != null ? _e : void 0
2975
+ });
2976
+ return;
2977
+ }
2978
+ case "message_delta": {
2979
+ usage.outputTokens = value.usage.output_tokens;
2980
+ usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2981
+ finishReason = mapAnthropicStopReason({
2982
+ finishReason: value.delta.stop_reason,
2983
+ isJsonResponseFromTool
2984
+ });
2985
+ stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2986
+ container = value.delta.container != null ? {
2987
+ expiresAt: value.delta.container.expires_at,
2988
+ id: value.delta.container.id,
2989
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2990
+ type: skill.type,
2991
+ skillId: skill.skill_id,
2992
+ version: skill.version
2993
+ }))) != null ? _j : null
2994
+ } : null;
2995
+ rawUsage = {
2996
+ ...rawUsage,
2997
+ ...value.usage
2998
+ };
2999
+ return;
3000
+ }
3001
+ case "message_stop": {
3002
+ controller.enqueue({
3003
+ type: "finish",
3004
+ finishReason,
3005
+ usage,
3006
+ providerMetadata: {
3007
+ anthropic: {
3008
+ usage: rawUsage != null ? rawUsage : null,
3009
+ cacheCreationInputTokens,
3010
+ stopSequence,
3011
+ container
2981
3012
  }
2982
- });
2983
- return;
2984
- }
2985
- case "error": {
2986
- controller.enqueue({ type: "error", error: value.error });
2987
- return;
2988
- }
2989
- default: {
2990
- const _exhaustiveCheck = value;
2991
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2992
- }
3013
+ }
3014
+ });
3015
+ return;
3016
+ }
3017
+ case "error": {
3018
+ controller.enqueue({ type: "error", error: value.error });
3019
+ return;
3020
+ }
3021
+ default: {
3022
+ const _exhaustiveCheck = value;
3023
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2993
3024
  }
2994
3025
  }
2995
- })
2996
- ),
2997
- request: { body },
2998
- response: { headers: responseHeaders }
2999
- };
3026
+ }
3027
+ })
3028
+ );
3029
+ const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
3030
+ stream = streamForConsumer;
3031
+ const reader = streamForFirstChunk.getReader();
3032
+ (async () => {
3033
+ try {
3034
+ const { done } = await reader.read();
3035
+ if (!done) {
3036
+ await reader.cancel();
3037
+ }
3038
+ } catch (error) {
3039
+ try {
3040
+ await reader.cancel();
3041
+ } catch (e) {
3042
+ }
3043
+ } finally {
3044
+ reader.releaseLock();
3045
+ }
3046
+ })();
3047
+ return returnPromise.promise;
3000
3048
  }
3001
3049
  };
3002
3050
  function getModelCapabilities(modelId) {