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