@ai-sdk/anthropic 3.0.0-beta.63 → 3.0.0-beta.65

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,
@@ -721,7 +723,6 @@ var textEditor_20250728InputSchema = lazySchema3(
721
723
  );
722
724
  var factory = createProviderDefinedToolFactory({
723
725
  id: "anthropic.text_editor_20250728",
724
- name: "str_replace_based_edit_tool",
725
726
  inputSchema: textEditor_20250728InputSchema
726
727
  });
727
728
  var textEditor_20250728 = (args = {}) => {
@@ -773,7 +774,6 @@ var webSearch_20250305InputSchema = lazySchema4(
773
774
  );
774
775
  var factory2 = createProviderDefinedToolFactoryWithOutputSchema({
775
776
  id: "anthropic.web_search_20250305",
776
- name: "web_search",
777
777
  inputSchema: webSearch_20250305InputSchema,
778
778
  outputSchema: webSearch_20250305OutputSchema
779
779
  });
@@ -834,7 +834,6 @@ var webFetch_20250910InputSchema = lazySchema5(
834
834
  );
835
835
  var factory3 = createProviderDefinedToolFactoryWithOutputSchema2({
836
836
  id: "anthropic.web_fetch_20250910",
837
- name: "web_fetch",
838
837
  inputSchema: webFetch_20250910InputSchema,
839
838
  outputSchema: webFetch_20250910OutputSchema
840
839
  });
@@ -1124,7 +1123,6 @@ var codeExecution_20250522InputSchema = lazySchema6(
1124
1123
  );
1125
1124
  var factory4 = createProviderDefinedToolFactoryWithOutputSchema3({
1126
1125
  id: "anthropic.code_execution_20250522",
1127
- name: "code_execution",
1128
1126
  inputSchema: codeExecution_20250522InputSchema,
1129
1127
  outputSchema: codeExecution_20250522OutputSchema
1130
1128
  });
@@ -1217,7 +1215,6 @@ var codeExecution_20250825InputSchema = lazySchema7(
1217
1215
  );
1218
1216
  var factory5 = createProviderDefinedToolFactoryWithOutputSchema4({
1219
1217
  id: "anthropic.code_execution_20250825",
1220
- name: "code_execution",
1221
1218
  inputSchema: codeExecution_20250825InputSchema,
1222
1219
  outputSchema: codeExecution_20250825OutputSchema
1223
1220
  });
@@ -2505,8 +2502,9 @@ var AnthropicMessagesLanguageModel = class {
2505
2502
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2506
2503
  });
2507
2504
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2505
+ const url = this.buildRequestUrl(true);
2508
2506
  const { responseHeaders, value: response } = await postJsonToApi({
2509
- url: this.buildRequestUrl(true),
2507
+ url,
2510
2508
  headers: await this.getHeaders({ betas, headers: options.headers }),
2511
2509
  body: this.transformRequestBody(body),
2512
2510
  failedResponseHandler: anthropicFailedResponseHandler,
@@ -2531,473 +2529,523 @@ var AnthropicMessagesLanguageModel = class {
2531
2529
  let isJsonResponseFromTool = false;
2532
2530
  let blockType = void 0;
2533
2531
  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 });
2532
+ let isFirstChunk = true;
2533
+ let stream = void 0;
2534
+ const returnPromise = new DelayedPromise();
2535
+ const transformedStream = response.pipeThrough(
2536
+ new TransformStream({
2537
+ start(controller) {
2538
+ controller.enqueue({ type: "stream-start", warnings });
2539
+ },
2540
+ async flush() {
2541
+ if (returnPromise.isPending()) {
2542
+ returnPromise.resolve({
2543
+ stream,
2544
+ request: { body },
2545
+ response: { headers: responseHeaders }
2546
+ });
2547
+ }
2548
+ },
2549
+ transform(chunk, controller) {
2550
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2551
+ if (options.includeRawChunks) {
2552
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2553
+ }
2554
+ if (!chunk.success) {
2555
+ controller.enqueue({ type: "error", error: chunk.error });
2556
+ return;
2557
+ }
2558
+ if (isFirstChunk) {
2559
+ if (chunk.value.type === "error") {
2560
+ returnPromise.reject(
2561
+ new APICallError({
2562
+ message: chunk.value.error.message,
2563
+ url,
2564
+ requestBodyValues: body,
2565
+ statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
2566
+ responseHeaders,
2567
+ responseBody: JSON.stringify(chunk.value.error),
2568
+ isRetryable: chunk.value.error.type === "overloaded_error"
2569
+ })
2570
+ );
2571
+ controller.terminate();
2572
+ return;
2544
2573
  }
2545
- if (!chunk.success) {
2546
- controller.enqueue({ type: "error", error: chunk.error });
2574
+ isFirstChunk = false;
2575
+ returnPromise.resolve({
2576
+ stream,
2577
+ request: { body },
2578
+ response: { headers: responseHeaders }
2579
+ });
2580
+ }
2581
+ const value = chunk.value;
2582
+ switch (value.type) {
2583
+ case "ping": {
2547
2584
  return;
2548
2585
  }
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;
2586
+ case "content_block_start": {
2587
+ const part = value.content_block;
2588
+ const contentBlockType = part.type;
2589
+ blockType = contentBlockType;
2590
+ switch (contentBlockType) {
2591
+ case "text": {
2592
+ if (usesJsonResponseTool) {
2593
+ return;
2594
+ }
2595
+ contentBlocks[value.index] = { type: "text" };
2596
+ controller.enqueue({
2597
+ type: "text-start",
2598
+ id: String(value.index)
2599
+ });
2600
+ return;
2601
+ }
2602
+ case "thinking": {
2603
+ contentBlocks[value.index] = { type: "reasoning" };
2604
+ controller.enqueue({
2605
+ type: "reasoning-start",
2606
+ id: String(value.index)
2607
+ });
2608
+ return;
2609
+ }
2610
+ case "redacted_thinking": {
2611
+ contentBlocks[value.index] = { type: "reasoning" };
2612
+ controller.enqueue({
2613
+ type: "reasoning-start",
2614
+ id: String(value.index),
2615
+ providerMetadata: {
2616
+ anthropic: {
2617
+ redactedData: part.data
2618
+ }
2562
2619
  }
2620
+ });
2621
+ return;
2622
+ }
2623
+ case "tool_use": {
2624
+ const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2625
+ if (isJsonResponseTool) {
2626
+ isJsonResponseFromTool = true;
2563
2627
  contentBlocks[value.index] = { type: "text" };
2564
2628
  controller.enqueue({
2565
2629
  type: "text-start",
2566
2630
  id: String(value.index)
2567
2631
  });
2568
- return;
2569
- }
2570
- case "thinking": {
2571
- contentBlocks[value.index] = { type: "reasoning" };
2632
+ } else {
2633
+ contentBlocks[value.index] = {
2634
+ type: "tool-call",
2635
+ toolCallId: part.id,
2636
+ toolName: part.name,
2637
+ input: "",
2638
+ firstDelta: true
2639
+ };
2572
2640
  controller.enqueue({
2573
- type: "reasoning-start",
2574
- id: String(value.index)
2641
+ type: "tool-input-start",
2642
+ id: part.id,
2643
+ toolName: part.name
2575
2644
  });
2576
- return;
2577
2645
  }
2578
- case "redacted_thinking": {
2579
- contentBlocks[value.index] = { type: "reasoning" };
2646
+ return;
2647
+ }
2648
+ case "server_tool_use": {
2649
+ if ([
2650
+ "web_fetch",
2651
+ "web_search",
2652
+ // code execution 20250825:
2653
+ "code_execution",
2654
+ // code execution 20250825 text editor:
2655
+ "text_editor_code_execution",
2656
+ // code execution 20250825 bash:
2657
+ "bash_code_execution"
2658
+ ].includes(part.name)) {
2659
+ contentBlocks[value.index] = {
2660
+ type: "tool-call",
2661
+ toolCallId: part.id,
2662
+ toolName: part.name,
2663
+ input: "",
2664
+ providerExecuted: true,
2665
+ firstDelta: true
2666
+ };
2667
+ const mappedToolName = part.name === "text_editor_code_execution" || part.name === "bash_code_execution" ? "code_execution" : part.name;
2580
2668
  controller.enqueue({
2581
- type: "reasoning-start",
2582
- id: String(value.index),
2583
- providerMetadata: {
2584
- anthropic: {
2585
- redactedData: part.data
2586
- }
2587
- }
2669
+ type: "tool-input-start",
2670
+ id: part.id,
2671
+ toolName: mappedToolName,
2672
+ providerExecuted: true
2588
2673
  });
2589
- return;
2590
2674
  }
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
- }
2675
+ return;
2676
+ }
2677
+ case "web_fetch_tool_result": {
2678
+ if (part.content.type === "web_fetch_result") {
2679
+ controller.enqueue({
2680
+ type: "tool-result",
2681
+ toolCallId: part.tool_use_id,
2682
+ toolName: "web_fetch",
2683
+ result: {
2684
+ type: "web_fetch_result",
2685
+ url: part.content.url,
2686
+ retrievedAt: part.content.retrieved_at,
2687
+ content: {
2688
+ type: part.content.content.type,
2689
+ title: part.content.content.title,
2690
+ citations: part.content.content.citations,
2691
+ source: {
2692
+ type: part.content.content.source.type,
2693
+ mediaType: part.content.content.source.media_type,
2694
+ data: part.content.content.source.data
2664
2695
  }
2665
2696
  }
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;
2697
+ }
2698
+ });
2699
+ } else if (part.content.type === "web_fetch_tool_result_error") {
2700
+ controller.enqueue({
2701
+ type: "tool-result",
2702
+ toolCallId: part.tool_use_id,
2703
+ toolName: "web_fetch",
2704
+ isError: true,
2705
+ result: {
2706
+ type: "web_fetch_tool_result_error",
2707
+ errorCode: part.content.error_code
2708
+ }
2709
+ });
2680
2710
  }
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(),
2711
+ return;
2712
+ }
2713
+ case "web_search_tool_result": {
2714
+ if (Array.isArray(part.content)) {
2715
+ controller.enqueue({
2716
+ type: "tool-result",
2717
+ toolCallId: part.tool_use_id,
2718
+ toolName: "web_search",
2719
+ result: part.content.map((result) => {
2720
+ var _a2;
2721
+ return {
2703
2722
  url: result.url,
2704
2723
  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") {
2724
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2725
+ encryptedContent: result.encrypted_content,
2726
+ type: result.type
2727
+ };
2728
+ })
2729
+ });
2730
+ for (const result of part.content) {
2741
2731
  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
2732
+ type: "source",
2733
+ sourceType: "url",
2734
+ id: generateId2(),
2735
+ url: result.url,
2736
+ title: result.title,
2737
+ providerMetadata: {
2738
+ anthropic: {
2739
+ pageAge: (_a = result.page_age) != null ? _a : null
2740
+ }
2749
2741
  }
2750
2742
  });
2751
2743
  }
2752
- return;
2753
- }
2754
- // code execution 20250825:
2755
- case "bash_code_execution_tool_result":
2756
- case "text_editor_code_execution_tool_result": {
2744
+ } else {
2757
2745
  controller.enqueue({
2758
2746
  type: "tool-result",
2759
2747
  toolCallId: part.tool_use_id,
2760
- toolName: "code_execution",
2761
- result: part.content
2748
+ toolName: "web_search",
2749
+ isError: true,
2750
+ result: {
2751
+ type: "web_search_tool_result_error",
2752
+ errorCode: part.content.error_code
2753
+ }
2762
2754
  });
2763
- return;
2764
2755
  }
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
- }
2756
+ return;
2757
+ }
2758
+ // code execution 20250522:
2759
+ case "code_execution_tool_result": {
2760
+ if (part.content.type === "code_execution_result") {
2761
+ controller.enqueue({
2762
+ type: "tool-result",
2763
+ toolCallId: part.tool_use_id,
2764
+ toolName: "code_execution",
2765
+ result: {
2766
+ type: part.content.type,
2767
+ stdout: part.content.stdout,
2768
+ stderr: part.content.stderr,
2769
+ return_code: part.content.return_code
2778
2770
  }
2779
- };
2780
- controller.enqueue(mcpToolCalls[part.id]);
2781
- return;
2782
- }
2783
- case "mcp_tool_result": {
2771
+ });
2772
+ } else if (part.content.type === "code_execution_tool_result_error") {
2784
2773
  controller.enqueue({
2785
2774
  type: "tool-result",
2786
2775
  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
2776
+ toolName: "code_execution",
2777
+ isError: true,
2778
+ result: {
2779
+ type: "code_execution_tool_result_error",
2780
+ errorCode: part.content.error_code
2781
+ }
2792
2782
  });
2793
- return;
2794
- }
2795
- default: {
2796
- const _exhaustiveCheck = contentBlockType;
2797
- throw new Error(
2798
- `Unsupported content block type: ${_exhaustiveCheck}`
2799
- );
2800
2783
  }
2784
+ return;
2785
+ }
2786
+ // code execution 20250825:
2787
+ case "bash_code_execution_tool_result":
2788
+ case "text_editor_code_execution_tool_result": {
2789
+ controller.enqueue({
2790
+ type: "tool-result",
2791
+ toolCallId: part.tool_use_id,
2792
+ toolName: "code_execution",
2793
+ result: part.content
2794
+ });
2795
+ return;
2796
+ }
2797
+ case "mcp_tool_use": {
2798
+ mcpToolCalls[part.id] = {
2799
+ type: "tool-call",
2800
+ toolCallId: part.id,
2801
+ toolName: part.name,
2802
+ input: JSON.stringify(part.input),
2803
+ providerExecuted: true,
2804
+ dynamic: true,
2805
+ providerMetadata: {
2806
+ anthropic: {
2807
+ type: "mcp-tool-use",
2808
+ serverName: part.server_name
2809
+ }
2810
+ }
2811
+ };
2812
+ controller.enqueue(mcpToolCalls[part.id]);
2813
+ return;
2814
+ }
2815
+ case "mcp_tool_result": {
2816
+ controller.enqueue({
2817
+ type: "tool-result",
2818
+ toolCallId: part.tool_use_id,
2819
+ toolName: mcpToolCalls[part.tool_use_id].toolName,
2820
+ isError: part.is_error,
2821
+ result: part.content,
2822
+ dynamic: true,
2823
+ providerMetadata: mcpToolCalls[part.tool_use_id].providerMetadata
2824
+ });
2825
+ return;
2826
+ }
2827
+ default: {
2828
+ const _exhaustiveCheck = contentBlockType;
2829
+ throw new Error(
2830
+ `Unsupported content block type: ${_exhaustiveCheck}`
2831
+ );
2801
2832
  }
2802
2833
  }
2803
- case "content_block_stop": {
2804
- if (contentBlocks[value.index] != null) {
2805
- const contentBlock = contentBlocks[value.index];
2806
- switch (contentBlock.type) {
2807
- case "text": {
2834
+ }
2835
+ case "content_block_stop": {
2836
+ if (contentBlocks[value.index] != null) {
2837
+ const contentBlock = contentBlocks[value.index];
2838
+ switch (contentBlock.type) {
2839
+ case "text": {
2840
+ controller.enqueue({
2841
+ type: "text-end",
2842
+ id: String(value.index)
2843
+ });
2844
+ break;
2845
+ }
2846
+ case "reasoning": {
2847
+ controller.enqueue({
2848
+ type: "reasoning-end",
2849
+ id: String(value.index)
2850
+ });
2851
+ break;
2852
+ }
2853
+ case "tool-call":
2854
+ const isJsonResponseTool = usesJsonResponseTool && contentBlock.toolName === "json";
2855
+ if (!isJsonResponseTool) {
2808
2856
  controller.enqueue({
2809
- type: "text-end",
2810
- id: String(value.index)
2857
+ type: "tool-input-end",
2858
+ id: contentBlock.toolCallId
2811
2859
  });
2812
- break;
2813
- }
2814
- case "reasoning": {
2860
+ const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2815
2861
  controller.enqueue({
2816
- type: "reasoning-end",
2817
- id: String(value.index)
2862
+ type: "tool-call",
2863
+ toolCallId: contentBlock.toolCallId,
2864
+ toolName,
2865
+ input: contentBlock.input,
2866
+ providerExecuted: contentBlock.providerExecuted
2818
2867
  });
2819
- break;
2820
2868
  }
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];
2869
+ break;
2840
2870
  }
2841
- blockType = void 0;
2842
- return;
2871
+ delete contentBlocks[value.index];
2843
2872
  }
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
- });
2873
+ blockType = void 0;
2874
+ return;
2875
+ }
2876
+ case "content_block_delta": {
2877
+ const deltaType = value.delta.type;
2878
+ switch (deltaType) {
2879
+ case "text_delta": {
2880
+ if (usesJsonResponseTool) {
2856
2881
  return;
2857
2882
  }
2858
- case "thinking_delta": {
2883
+ controller.enqueue({
2884
+ type: "text-delta",
2885
+ id: String(value.index),
2886
+ delta: value.delta.text
2887
+ });
2888
+ return;
2889
+ }
2890
+ case "thinking_delta": {
2891
+ controller.enqueue({
2892
+ type: "reasoning-delta",
2893
+ id: String(value.index),
2894
+ delta: value.delta.thinking
2895
+ });
2896
+ return;
2897
+ }
2898
+ case "signature_delta": {
2899
+ if (blockType === "thinking") {
2859
2900
  controller.enqueue({
2860
2901
  type: "reasoning-delta",
2861
2902
  id: String(value.index),
2862
- delta: value.delta.thinking
2903
+ delta: "",
2904
+ providerMetadata: {
2905
+ anthropic: {
2906
+ signature: value.delta.signature
2907
+ }
2908
+ }
2863
2909
  });
2864
- return;
2865
2910
  }
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
- }
2911
+ return;
2912
+ }
2913
+ case "input_json_delta": {
2914
+ const contentBlock = contentBlocks[value.index];
2915
+ let delta = value.delta.partial_json;
2916
+ if (delta.length === 0) {
2879
2917
  return;
2880
2918
  }
2881
- case "input_json_delta": {
2882
- const contentBlock = contentBlocks[value.index];
2883
- let delta = value.delta.partial_json;
2884
- if (delta.length === 0) {
2919
+ if (isJsonResponseFromTool) {
2920
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2885
2921
  return;
2886
2922
  }
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;
2923
+ controller.enqueue({
2924
+ type: "text-delta",
2925
+ id: String(value.index),
2926
+ delta
2927
+ });
2928
+ } else {
2929
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2930
+ return;
2910
2931
  }
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);
2932
+ if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2933
+ delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2922
2934
  }
2923
- return;
2935
+ controller.enqueue({
2936
+ type: "tool-input-delta",
2937
+ id: contentBlock.toolCallId,
2938
+ delta
2939
+ });
2940
+ contentBlock.input += delta;
2941
+ contentBlock.firstDelta = false;
2924
2942
  }
2925
- default: {
2926
- const _exhaustiveCheck = deltaType;
2927
- throw new Error(
2928
- `Unsupported delta type: ${_exhaustiveCheck}`
2929
- );
2943
+ return;
2944
+ }
2945
+ case "citations_delta": {
2946
+ const citation = value.delta.citation;
2947
+ const source = createCitationSource(
2948
+ citation,
2949
+ citationDocuments,
2950
+ generateId2
2951
+ );
2952
+ if (source) {
2953
+ controller.enqueue(source);
2930
2954
  }
2955
+ return;
2956
+ }
2957
+ default: {
2958
+ const _exhaustiveCheck = deltaType;
2959
+ throw new Error(
2960
+ `Unsupported delta type: ${_exhaustiveCheck}`
2961
+ );
2931
2962
  }
2932
2963
  }
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
- }
2964
+ }
2965
+ case "message_start": {
2966
+ usage.inputTokens = value.message.usage.input_tokens;
2967
+ usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2968
+ rawUsage = {
2969
+ ...value.message.usage
2970
+ };
2971
+ cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2972
+ controller.enqueue({
2973
+ type: "response-metadata",
2974
+ id: (_d = value.message.id) != null ? _d : void 0,
2975
+ modelId: (_e = value.message.model) != null ? _e : void 0
2976
+ });
2977
+ return;
2978
+ }
2979
+ case "message_delta": {
2980
+ usage.outputTokens = value.usage.output_tokens;
2981
+ usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2982
+ finishReason = mapAnthropicStopReason({
2983
+ finishReason: value.delta.stop_reason,
2984
+ isJsonResponseFromTool
2985
+ });
2986
+ stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2987
+ container = value.delta.container != null ? {
2988
+ expiresAt: value.delta.container.expires_at,
2989
+ id: value.delta.container.id,
2990
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2991
+ type: skill.type,
2992
+ skillId: skill.skill_id,
2993
+ version: skill.version
2994
+ }))) != null ? _j : null
2995
+ } : null;
2996
+ rawUsage = {
2997
+ ...rawUsage,
2998
+ ...value.usage
2999
+ };
3000
+ return;
3001
+ }
3002
+ case "message_stop": {
3003
+ controller.enqueue({
3004
+ type: "finish",
3005
+ finishReason,
3006
+ usage,
3007
+ providerMetadata: {
3008
+ anthropic: {
3009
+ usage: rawUsage != null ? rawUsage : null,
3010
+ cacheCreationInputTokens,
3011
+ stopSequence,
3012
+ container
2982
3013
  }
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
- }
3014
+ }
3015
+ });
3016
+ return;
3017
+ }
3018
+ case "error": {
3019
+ controller.enqueue({ type: "error", error: value.error });
3020
+ return;
3021
+ }
3022
+ default: {
3023
+ const _exhaustiveCheck = value;
3024
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2994
3025
  }
2995
3026
  }
2996
- })
2997
- ),
2998
- request: { body },
2999
- response: { headers: responseHeaders }
3000
- };
3027
+ }
3028
+ })
3029
+ );
3030
+ const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
3031
+ stream = streamForConsumer;
3032
+ const reader = streamForFirstChunk.getReader();
3033
+ (async () => {
3034
+ try {
3035
+ const { done } = await reader.read();
3036
+ if (!done) {
3037
+ await reader.cancel();
3038
+ }
3039
+ } catch (error) {
3040
+ try {
3041
+ await reader.cancel();
3042
+ } catch (e) {
3043
+ }
3044
+ } finally {
3045
+ reader.releaseLock();
3046
+ }
3047
+ })();
3048
+ return returnPromise.promise;
3001
3049
  }
3002
3050
  };
3003
3051
  function getModelCapabilities(modelId) {
@@ -3063,7 +3111,6 @@ var bash_20241022InputSchema = lazySchema8(
3063
3111
  );
3064
3112
  var bash_20241022 = createProviderDefinedToolFactory2({
3065
3113
  id: "anthropic.bash_20241022",
3066
- name: "bash",
3067
3114
  inputSchema: bash_20241022InputSchema
3068
3115
  });
3069
3116
 
@@ -3084,7 +3131,6 @@ var bash_20250124InputSchema = lazySchema9(
3084
3131
  );
3085
3132
  var bash_20250124 = createProviderDefinedToolFactory3({
3086
3133
  id: "anthropic.bash_20250124",
3087
- name: "bash",
3088
3134
  inputSchema: bash_20250124InputSchema
3089
3135
  });
3090
3136
 
@@ -3117,7 +3163,6 @@ var computer_20241022InputSchema = lazySchema10(
3117
3163
  );
3118
3164
  var computer_20241022 = createProviderDefinedToolFactory4({
3119
3165
  id: "anthropic.computer_20241022",
3120
- name: "computer",
3121
3166
  inputSchema: computer_20241022InputSchema
3122
3167
  });
3123
3168
 
@@ -3160,7 +3205,6 @@ var computer_20250124InputSchema = lazySchema11(
3160
3205
  );
3161
3206
  var computer_20250124 = createProviderDefinedToolFactory5({
3162
3207
  id: "anthropic.computer_20250124",
3163
- name: "computer",
3164
3208
  inputSchema: computer_20250124InputSchema
3165
3209
  });
3166
3210
 
@@ -3210,7 +3254,6 @@ var memory_20250818InputSchema = lazySchema12(
3210
3254
  );
3211
3255
  var memory_20250818 = createProviderDefinedToolFactory6({
3212
3256
  id: "anthropic.memory_20250818",
3213
- name: "memory",
3214
3257
  inputSchema: memory_20250818InputSchema
3215
3258
  });
3216
3259
 
@@ -3236,7 +3279,6 @@ var textEditor_20241022InputSchema = lazySchema13(
3236
3279
  );
3237
3280
  var textEditor_20241022 = createProviderDefinedToolFactory7({
3238
3281
  id: "anthropic.text_editor_20241022",
3239
- name: "str_replace_editor",
3240
3282
  inputSchema: textEditor_20241022InputSchema
3241
3283
  });
3242
3284
 
@@ -3262,7 +3304,6 @@ var textEditor_20250124InputSchema = lazySchema14(
3262
3304
  );
3263
3305
  var textEditor_20250124 = createProviderDefinedToolFactory8({
3264
3306
  id: "anthropic.text_editor_20250124",
3265
- name: "str_replace_editor",
3266
3307
  inputSchema: textEditor_20250124InputSchema
3267
3308
  });
3268
3309
 
@@ -3288,7 +3329,6 @@ var textEditor_20250429InputSchema = lazySchema15(
3288
3329
  );
3289
3330
  var textEditor_20250429 = createProviderDefinedToolFactory9({
3290
3331
  id: "anthropic.text_editor_20250429",
3291
- name: "str_replace_based_edit_tool",
3292
3332
  inputSchema: textEditor_20250429InputSchema
3293
3333
  });
3294
3334