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