@ai-sdk/anthropic 3.0.0-beta.63 → 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.63" : "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,
@@ -2520,8 +2522,9 @@ var AnthropicMessagesLanguageModel = class {
2520
2522
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2521
2523
  });
2522
2524
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2525
+ const url = this.buildRequestUrl(true);
2523
2526
  const { responseHeaders, value: response } = await postJsonToApi({
2524
- url: this.buildRequestUrl(true),
2527
+ url,
2525
2528
  headers: await this.getHeaders({ betas, headers: options.headers }),
2526
2529
  body: this.transformRequestBody(body),
2527
2530
  failedResponseHandler: anthropicFailedResponseHandler,
@@ -2546,473 +2549,523 @@ var AnthropicMessagesLanguageModel = class {
2546
2549
  let isJsonResponseFromTool = false;
2547
2550
  let blockType = void 0;
2548
2551
  const generateId3 = this.generateId;
2549
- return {
2550
- stream: response.pipeThrough(
2551
- new TransformStream({
2552
- start(controller) {
2553
- controller.enqueue({ type: "stream-start", warnings });
2554
- },
2555
- transform(chunk, controller) {
2556
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2557
- if (options.includeRawChunks) {
2558
- 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;
2559
2593
  }
2560
- if (!chunk.success) {
2561
- 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": {
2562
2604
  return;
2563
2605
  }
2564
- const value = chunk.value;
2565
- switch (value.type) {
2566
- case "ping": {
2567
- return;
2568
- }
2569
- case "content_block_start": {
2570
- const part = value.content_block;
2571
- const contentBlockType = part.type;
2572
- blockType = contentBlockType;
2573
- switch (contentBlockType) {
2574
- case "text": {
2575
- if (usesJsonResponseTool) {
2576
- 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
+ }
2577
2639
  }
2640
+ });
2641
+ return;
2642
+ }
2643
+ case "tool_use": {
2644
+ const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2645
+ if (isJsonResponseTool) {
2646
+ isJsonResponseFromTool = true;
2578
2647
  contentBlocks[value.index] = { type: "text" };
2579
2648
  controller.enqueue({
2580
2649
  type: "text-start",
2581
2650
  id: String(value.index)
2582
2651
  });
2583
- return;
2584
- }
2585
- case "thinking": {
2586
- 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
+ };
2587
2660
  controller.enqueue({
2588
- type: "reasoning-start",
2589
- id: String(value.index)
2661
+ type: "tool-input-start",
2662
+ id: part.id,
2663
+ toolName: part.name
2590
2664
  });
2591
- return;
2592
2665
  }
2593
- case "redacted_thinking": {
2594
- 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;
2595
2688
  controller.enqueue({
2596
- type: "reasoning-start",
2597
- id: String(value.index),
2598
- providerMetadata: {
2599
- anthropic: {
2600
- redactedData: part.data
2601
- }
2602
- }
2689
+ type: "tool-input-start",
2690
+ id: part.id,
2691
+ toolName: mappedToolName,
2692
+ providerExecuted: true
2603
2693
  });
2604
- return;
2605
2694
  }
2606
- case "tool_use": {
2607
- const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2608
- if (isJsonResponseTool) {
2609
- isJsonResponseFromTool = true;
2610
- contentBlocks[value.index] = { type: "text" };
2611
- controller.enqueue({
2612
- type: "text-start",
2613
- id: String(value.index)
2614
- });
2615
- } else {
2616
- contentBlocks[value.index] = {
2617
- type: "tool-call",
2618
- toolCallId: part.id,
2619
- toolName: part.name,
2620
- input: "",
2621
- firstDelta: true
2622
- };
2623
- controller.enqueue({
2624
- type: "tool-input-start",
2625
- id: part.id,
2626
- toolName: part.name
2627
- });
2628
- }
2629
- return;
2630
- }
2631
- case "server_tool_use": {
2632
- if ([
2633
- "web_fetch",
2634
- "web_search",
2635
- // code execution 20250825:
2636
- "code_execution",
2637
- // code execution 20250825 text editor:
2638
- "text_editor_code_execution",
2639
- // code execution 20250825 bash:
2640
- "bash_code_execution"
2641
- ].includes(part.name)) {
2642
- contentBlocks[value.index] = {
2643
- type: "tool-call",
2644
- toolCallId: part.id,
2645
- toolName: part.name,
2646
- input: "",
2647
- providerExecuted: true,
2648
- firstDelta: true
2649
- };
2650
- const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2651
- controller.enqueue({
2652
- type: "tool-input-start",
2653
- id: part.id,
2654
- toolName: mappedToolName,
2655
- providerExecuted: true
2656
- });
2657
- }
2658
- return;
2659
- }
2660
- case "web_fetch_tool_result": {
2661
- if (part.content.type === "web_fetch_result") {
2662
- controller.enqueue({
2663
- type: "tool-result",
2664
- toolCallId: part.tool_use_id,
2665
- toolName: "web_fetch",
2666
- result: {
2667
- type: "web_fetch_result",
2668
- url: part.content.url,
2669
- retrievedAt: part.content.retrieved_at,
2670
- content: {
2671
- type: part.content.content.type,
2672
- title: part.content.content.title,
2673
- citations: part.content.content.citations,
2674
- source: {
2675
- type: part.content.content.source.type,
2676
- mediaType: part.content.content.source.media_type,
2677
- data: part.content.content.source.data
2678
- }
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
2679
2715
  }
2680
2716
  }
2681
- });
2682
- } else if (part.content.type === "web_fetch_tool_result_error") {
2683
- controller.enqueue({
2684
- type: "tool-result",
2685
- toolCallId: part.tool_use_id,
2686
- toolName: "web_fetch",
2687
- isError: true,
2688
- result: {
2689
- type: "web_fetch_tool_result_error",
2690
- errorCode: part.content.error_code
2691
- }
2692
- });
2693
- }
2694
- 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
+ });
2695
2730
  }
2696
- case "web_search_tool_result": {
2697
- if (Array.isArray(part.content)) {
2698
- controller.enqueue({
2699
- type: "tool-result",
2700
- toolCallId: part.tool_use_id,
2701
- toolName: "web_search",
2702
- result: part.content.map((result) => {
2703
- var _a2;
2704
- return {
2705
- url: result.url,
2706
- title: result.title,
2707
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2708
- encryptedContent: result.encrypted_content,
2709
- type: result.type
2710
- };
2711
- })
2712
- });
2713
- for (const result of part.content) {
2714
- controller.enqueue({
2715
- type: "source",
2716
- sourceType: "url",
2717
- 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 {
2718
2742
  url: result.url,
2719
2743
  title: result.title,
2720
- providerMetadata: {
2721
- anthropic: {
2722
- pageAge: (_a = result.page_age) != null ? _a : null
2723
- }
2724
- }
2725
- });
2726
- }
2727
- } else {
2728
- controller.enqueue({
2729
- type: "tool-result",
2730
- toolCallId: part.tool_use_id,
2731
- toolName: "web_search",
2732
- isError: true,
2733
- result: {
2734
- type: "web_search_tool_result_error",
2735
- errorCode: part.content.error_code
2736
- }
2737
- });
2738
- }
2739
- return;
2740
- }
2741
- // code execution 20250522:
2742
- case "code_execution_tool_result": {
2743
- if (part.content.type === "code_execution_result") {
2744
- controller.enqueue({
2745
- type: "tool-result",
2746
- toolCallId: part.tool_use_id,
2747
- toolName: "code_execution",
2748
- result: {
2749
- type: part.content.type,
2750
- stdout: part.content.stdout,
2751
- stderr: part.content.stderr,
2752
- return_code: part.content.return_code
2753
- }
2754
- });
2755
- } 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) {
2756
2751
  controller.enqueue({
2757
- type: "tool-result",
2758
- toolCallId: part.tool_use_id,
2759
- toolName: "code_execution",
2760
- isError: true,
2761
- result: {
2762
- type: "code_execution_tool_result_error",
2763
- 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
+ }
2764
2761
  }
2765
2762
  });
2766
2763
  }
2767
- return;
2768
- }
2769
- // code execution 20250825:
2770
- case "bash_code_execution_tool_result":
2771
- case "text_editor_code_execution_tool_result": {
2764
+ } else {
2772
2765
  controller.enqueue({
2773
2766
  type: "tool-result",
2774
2767
  toolCallId: part.tool_use_id,
2775
- toolName: "code_execution",
2776
- 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
+ }
2777
2774
  });
2778
- return;
2779
2775
  }
2780
- case "mcp_tool_use": {
2781
- mcpToolCalls[part.id] = {
2782
- type: "tool-call",
2783
- toolCallId: part.id,
2784
- toolName: part.name,
2785
- input: JSON.stringify(part.input),
2786
- providerExecuted: true,
2787
- dynamic: true,
2788
- providerMetadata: {
2789
- anthropic: {
2790
- type: "mcp-tool-use",
2791
- serverName: part.server_name
2792
- }
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
2793
2790
  }
2794
- };
2795
- controller.enqueue(mcpToolCalls[part.id]);
2796
- return;
2797
- }
2798
- case "mcp_tool_result": {
2791
+ });
2792
+ } else if (part.content.type === "code_execution_tool_result_error") {
2799
2793
  controller.enqueue({
2800
2794
  type: "tool-result",
2801
2795
  toolCallId: part.tool_use_id,
2802
- toolName: mcpToolCalls[part.tool_use_id].toolName,
2803
- isError: part.is_error,
2804
- result: part.content,
2805
- dynamic: true,
2806
- 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
+ }
2807
2802
  });
2808
- return;
2809
- }
2810
- default: {
2811
- const _exhaustiveCheck = contentBlockType;
2812
- throw new Error(
2813
- `Unsupported content block type: ${_exhaustiveCheck}`
2814
- );
2815
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
+ );
2816
2852
  }
2817
2853
  }
2818
- case "content_block_stop": {
2819
- if (contentBlocks[value.index] != null) {
2820
- const contentBlock = contentBlocks[value.index];
2821
- switch (contentBlock.type) {
2822
- 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) {
2823
2876
  controller.enqueue({
2824
- type: "text-end",
2825
- id: String(value.index)
2877
+ type: "tool-input-end",
2878
+ id: contentBlock.toolCallId
2826
2879
  });
2827
- break;
2828
- }
2829
- case "reasoning": {
2880
+ const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2830
2881
  controller.enqueue({
2831
- type: "reasoning-end",
2832
- id: String(value.index)
2882
+ type: "tool-call",
2883
+ toolCallId: contentBlock.toolCallId,
2884
+ toolName,
2885
+ input: contentBlock.input,
2886
+ providerExecuted: contentBlock.providerExecuted
2833
2887
  });
2834
- break;
2835
2888
  }
2836
- case "tool-call":
2837
- const isJsonResponseTool = usesJsonResponseTool && contentBlock.toolName === "json";
2838
- if (!isJsonResponseTool) {
2839
- controller.enqueue({
2840
- type: "tool-input-end",
2841
- id: contentBlock.toolCallId
2842
- });
2843
- const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2844
- controller.enqueue({
2845
- type: "tool-call",
2846
- toolCallId: contentBlock.toolCallId,
2847
- toolName,
2848
- input: contentBlock.input,
2849
- providerExecuted: contentBlock.providerExecuted
2850
- });
2851
- }
2852
- break;
2853
- }
2854
- delete contentBlocks[value.index];
2889
+ break;
2855
2890
  }
2856
- blockType = void 0;
2857
- return;
2891
+ delete contentBlocks[value.index];
2858
2892
  }
2859
- case "content_block_delta": {
2860
- const deltaType = value.delta.type;
2861
- switch (deltaType) {
2862
- case "text_delta": {
2863
- if (usesJsonResponseTool) {
2864
- return;
2865
- }
2866
- controller.enqueue({
2867
- type: "text-delta",
2868
- id: String(value.index),
2869
- delta: value.delta.text
2870
- });
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) {
2871
2901
  return;
2872
2902
  }
2873
- 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") {
2874
2920
  controller.enqueue({
2875
2921
  type: "reasoning-delta",
2876
2922
  id: String(value.index),
2877
- delta: value.delta.thinking
2923
+ delta: "",
2924
+ providerMetadata: {
2925
+ anthropic: {
2926
+ signature: value.delta.signature
2927
+ }
2928
+ }
2878
2929
  });
2879
- return;
2880
2930
  }
2881
- case "signature_delta": {
2882
- if (blockType === "thinking") {
2883
- controller.enqueue({
2884
- type: "reasoning-delta",
2885
- id: String(value.index),
2886
- delta: "",
2887
- providerMetadata: {
2888
- anthropic: {
2889
- signature: value.delta.signature
2890
- }
2891
- }
2892
- });
2893
- }
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) {
2894
2937
  return;
2895
2938
  }
2896
- case "input_json_delta": {
2897
- const contentBlock = contentBlocks[value.index];
2898
- let delta = value.delta.partial_json;
2899
- if (delta.length === 0) {
2939
+ if (isJsonResponseFromTool) {
2940
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2900
2941
  return;
2901
2942
  }
2902
- if (isJsonResponseFromTool) {
2903
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2904
- return;
2905
- }
2906
- controller.enqueue({
2907
- type: "text-delta",
2908
- id: String(value.index),
2909
- delta
2910
- });
2911
- } else {
2912
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2913
- return;
2914
- }
2915
- if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2916
- delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2917
- }
2918
- controller.enqueue({
2919
- type: "tool-input-delta",
2920
- id: contentBlock.toolCallId,
2921
- delta
2922
- });
2923
- contentBlock.input += delta;
2924
- 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;
2925
2951
  }
2926
- return;
2927
- }
2928
- case "citations_delta": {
2929
- const citation = value.delta.citation;
2930
- const source = createCitationSource(
2931
- citation,
2932
- citationDocuments,
2933
- generateId3
2934
- );
2935
- if (source) {
2936
- 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)}`;
2937
2954
  }
2938
- return;
2955
+ controller.enqueue({
2956
+ type: "tool-input-delta",
2957
+ id: contentBlock.toolCallId,
2958
+ delta
2959
+ });
2960
+ contentBlock.input += delta;
2961
+ contentBlock.firstDelta = false;
2939
2962
  }
2940
- default: {
2941
- const _exhaustiveCheck = deltaType;
2942
- throw new Error(
2943
- `Unsupported delta type: ${_exhaustiveCheck}`
2944
- );
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);
2945
2974
  }
2975
+ return;
2976
+ }
2977
+ default: {
2978
+ const _exhaustiveCheck = deltaType;
2979
+ throw new Error(
2980
+ `Unsupported delta type: ${_exhaustiveCheck}`
2981
+ );
2946
2982
  }
2947
2983
  }
2948
- case "message_start": {
2949
- usage.inputTokens = value.message.usage.input_tokens;
2950
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2951
- rawUsage = {
2952
- ...value.message.usage
2953
- };
2954
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2955
- controller.enqueue({
2956
- type: "response-metadata",
2957
- id: (_d = value.message.id) != null ? _d : void 0,
2958
- modelId: (_e = value.message.model) != null ? _e : void 0
2959
- });
2960
- return;
2961
- }
2962
- case "message_delta": {
2963
- usage.outputTokens = value.usage.output_tokens;
2964
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2965
- finishReason = mapAnthropicStopReason({
2966
- finishReason: value.delta.stop_reason,
2967
- isJsonResponseFromTool
2968
- });
2969
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2970
- container = value.delta.container != null ? {
2971
- expiresAt: value.delta.container.expires_at,
2972
- id: value.delta.container.id,
2973
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2974
- type: skill.type,
2975
- skillId: skill.skill_id,
2976
- version: skill.version
2977
- }))) != null ? _j : null
2978
- } : null;
2979
- rawUsage = {
2980
- ...rawUsage,
2981
- ...value.usage
2982
- };
2983
- return;
2984
- }
2985
- case "message_stop": {
2986
- controller.enqueue({
2987
- type: "finish",
2988
- finishReason,
2989
- usage,
2990
- providerMetadata: {
2991
- anthropic: {
2992
- usage: rawUsage != null ? rawUsage : null,
2993
- cacheCreationInputTokens,
2994
- stopSequence,
2995
- container
2996
- }
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
2997
3033
  }
2998
- });
2999
- return;
3000
- }
3001
- case "error": {
3002
- controller.enqueue({ type: "error", error: value.error });
3003
- return;
3004
- }
3005
- default: {
3006
- const _exhaustiveCheck = value;
3007
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
3008
- }
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}`);
3009
3045
  }
3010
3046
  }
3011
- })
3012
- ),
3013
- request: { body },
3014
- response: { headers: responseHeaders }
3015
- };
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;
3016
3069
  }
3017
3070
  };
3018
3071
  function getModelCapabilities(modelId) {