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