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