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