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