@ai-sdk/anthropic 2.0.49 → 2.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -11,16 +11,18 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "2.0.49" : "0.0.0-test";
14
+ var VERSION = true ? "2.0.50" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
18
+ APICallError,
18
19
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
19
20
  } from "@ai-sdk/provider";
20
21
  import {
21
22
  combineHeaders,
22
23
  createEventSourceResponseHandler,
23
24
  createJsonResponseHandler,
25
+ DelayedPromise,
24
26
  generateId,
25
27
  parseProviderOptions as parseProviderOptions2,
26
28
  postJsonToApi,
@@ -2338,8 +2340,9 @@ var AnthropicMessagesLanguageModel = class {
2338
2340
  });
2339
2341
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2340
2342
  const body = { ...args, stream: true };
2343
+ const url = this.buildRequestUrl(true);
2341
2344
  const { responseHeaders, value: response } = await postJsonToApi({
2342
- url: this.buildRequestUrl(true),
2345
+ url,
2343
2346
  headers: await this.getHeaders({ betas, headers: options.headers }),
2344
2347
  body: this.transformRequestBody(body),
2345
2348
  failedResponseHandler: anthropicFailedResponseHandler,
@@ -2362,441 +2365,491 @@ var AnthropicMessagesLanguageModel = class {
2362
2365
  let container = null;
2363
2366
  let blockType = void 0;
2364
2367
  const generateId3 = this.generateId;
2365
- return {
2366
- stream: response.pipeThrough(
2367
- new TransformStream({
2368
- start(controller) {
2369
- controller.enqueue({ type: "stream-start", warnings });
2370
- },
2371
- transform(chunk, controller) {
2372
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2373
- if (options.includeRawChunks) {
2374
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2368
+ let isFirstChunk = true;
2369
+ let stream = void 0;
2370
+ const returnPromise = new DelayedPromise();
2371
+ const transformedStream = response.pipeThrough(
2372
+ new TransformStream({
2373
+ start(controller) {
2374
+ controller.enqueue({ type: "stream-start", warnings });
2375
+ },
2376
+ async flush() {
2377
+ if (returnPromise.isPending()) {
2378
+ returnPromise.resolve({
2379
+ stream,
2380
+ request: { body },
2381
+ response: { headers: responseHeaders }
2382
+ });
2383
+ }
2384
+ },
2385
+ transform(chunk, controller) {
2386
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2387
+ if (options.includeRawChunks) {
2388
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2389
+ }
2390
+ if (!chunk.success) {
2391
+ controller.enqueue({ type: "error", error: chunk.error });
2392
+ return;
2393
+ }
2394
+ if (isFirstChunk) {
2395
+ if (chunk.value.type === "error") {
2396
+ returnPromise.reject(
2397
+ new APICallError({
2398
+ message: chunk.value.error.message,
2399
+ url,
2400
+ requestBodyValues: body,
2401
+ statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
2402
+ responseHeaders,
2403
+ responseBody: JSON.stringify(chunk.value.error),
2404
+ isRetryable: chunk.value.error.type === "overloaded_error"
2405
+ })
2406
+ );
2407
+ controller.terminate();
2408
+ return;
2375
2409
  }
2376
- if (!chunk.success) {
2377
- controller.enqueue({ type: "error", error: chunk.error });
2410
+ isFirstChunk = false;
2411
+ returnPromise.resolve({
2412
+ stream,
2413
+ request: { body },
2414
+ response: { headers: responseHeaders }
2415
+ });
2416
+ }
2417
+ const value = chunk.value;
2418
+ switch (value.type) {
2419
+ case "ping": {
2378
2420
  return;
2379
2421
  }
2380
- const value = chunk.value;
2381
- switch (value.type) {
2382
- case "ping": {
2383
- return;
2384
- }
2385
- case "content_block_start": {
2386
- const contentBlockType = value.content_block.type;
2387
- blockType = contentBlockType;
2388
- switch (contentBlockType) {
2389
- case "text": {
2390
- contentBlocks[value.index] = { type: "text" };
2391
- controller.enqueue({
2392
- type: "text-start",
2393
- id: String(value.index)
2394
- });
2395
- return;
2396
- }
2397
- case "thinking": {
2398
- contentBlocks[value.index] = { type: "reasoning" };
2399
- controller.enqueue({
2400
- type: "reasoning-start",
2401
- id: String(value.index)
2402
- });
2403
- return;
2404
- }
2405
- case "redacted_thinking": {
2406
- contentBlocks[value.index] = { type: "reasoning" };
2407
- controller.enqueue({
2408
- type: "reasoning-start",
2409
- id: String(value.index),
2410
- providerMetadata: {
2411
- anthropic: {
2412
- redactedData: value.content_block.data
2413
- }
2422
+ case "content_block_start": {
2423
+ const contentBlockType = value.content_block.type;
2424
+ blockType = contentBlockType;
2425
+ switch (contentBlockType) {
2426
+ case "text": {
2427
+ contentBlocks[value.index] = { type: "text" };
2428
+ controller.enqueue({
2429
+ type: "text-start",
2430
+ id: String(value.index)
2431
+ });
2432
+ return;
2433
+ }
2434
+ case "thinking": {
2435
+ contentBlocks[value.index] = { type: "reasoning" };
2436
+ controller.enqueue({
2437
+ type: "reasoning-start",
2438
+ id: String(value.index)
2439
+ });
2440
+ return;
2441
+ }
2442
+ case "redacted_thinking": {
2443
+ contentBlocks[value.index] = { type: "reasoning" };
2444
+ controller.enqueue({
2445
+ type: "reasoning-start",
2446
+ id: String(value.index),
2447
+ providerMetadata: {
2448
+ anthropic: {
2449
+ redactedData: value.content_block.data
2414
2450
  }
2415
- });
2416
- return;
2417
- }
2418
- case "tool_use": {
2419
- contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2451
+ }
2452
+ });
2453
+ return;
2454
+ }
2455
+ case "tool_use": {
2456
+ contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2457
+ type: "tool-call",
2458
+ toolCallId: value.content_block.id,
2459
+ toolName: value.content_block.name,
2460
+ input: "",
2461
+ firstDelta: true
2462
+ };
2463
+ controller.enqueue(
2464
+ usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2465
+ type: "tool-input-start",
2466
+ id: value.content_block.id,
2467
+ toolName: value.content_block.name
2468
+ }
2469
+ );
2470
+ return;
2471
+ }
2472
+ case "server_tool_use": {
2473
+ if ([
2474
+ "web_fetch",
2475
+ "web_search",
2476
+ // code execution 20250825:
2477
+ "code_execution",
2478
+ // code execution 20250825 text editor:
2479
+ "text_editor_code_execution",
2480
+ // code execution 20250825 bash:
2481
+ "bash_code_execution"
2482
+ ].includes(value.content_block.name)) {
2483
+ contentBlocks[value.index] = {
2420
2484
  type: "tool-call",
2421
2485
  toolCallId: value.content_block.id,
2422
2486
  toolName: value.content_block.name,
2423
2487
  input: "",
2488
+ providerExecuted: true,
2424
2489
  firstDelta: true
2425
2490
  };
2426
- controller.enqueue(
2427
- usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2428
- type: "tool-input-start",
2429
- id: value.content_block.id,
2430
- toolName: value.content_block.name
2431
- }
2432
- );
2433
- return;
2434
- }
2435
- case "server_tool_use": {
2436
- if ([
2437
- "web_fetch",
2438
- "web_search",
2439
- // code execution 20250825:
2440
- "code_execution",
2441
- // code execution 20250825 text editor:
2442
- "text_editor_code_execution",
2443
- // code execution 20250825 bash:
2444
- "bash_code_execution"
2445
- ].includes(value.content_block.name)) {
2446
- contentBlocks[value.index] = {
2447
- type: "tool-call",
2448
- toolCallId: value.content_block.id,
2449
- toolName: value.content_block.name,
2450
- input: "",
2451
- providerExecuted: true,
2452
- firstDelta: true
2453
- };
2454
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2455
- controller.enqueue({
2456
- type: "tool-input-start",
2457
- id: value.content_block.id,
2458
- toolName: mappedToolName,
2459
- providerExecuted: true
2460
- });
2461
- }
2462
- return;
2491
+ const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2492
+ controller.enqueue({
2493
+ type: "tool-input-start",
2494
+ id: value.content_block.id,
2495
+ toolName: mappedToolName,
2496
+ providerExecuted: true
2497
+ });
2463
2498
  }
2464
- case "web_fetch_tool_result": {
2465
- const part = value.content_block;
2466
- if (part.content.type === "web_fetch_result") {
2467
- controller.enqueue({
2468
- type: "tool-result",
2469
- toolCallId: part.tool_use_id,
2470
- toolName: "web_fetch",
2471
- result: {
2472
- type: "web_fetch_result",
2473
- url: part.content.url,
2474
- retrievedAt: part.content.retrieved_at,
2475
- content: {
2476
- type: part.content.content.type,
2477
- title: part.content.content.title,
2478
- citations: part.content.content.citations,
2479
- source: {
2480
- type: part.content.content.source.type,
2481
- mediaType: part.content.content.source.media_type,
2482
- data: part.content.content.source.data
2483
- }
2499
+ return;
2500
+ }
2501
+ case "web_fetch_tool_result": {
2502
+ const part = value.content_block;
2503
+ if (part.content.type === "web_fetch_result") {
2504
+ controller.enqueue({
2505
+ type: "tool-result",
2506
+ toolCallId: part.tool_use_id,
2507
+ toolName: "web_fetch",
2508
+ result: {
2509
+ type: "web_fetch_result",
2510
+ url: part.content.url,
2511
+ retrievedAt: part.content.retrieved_at,
2512
+ content: {
2513
+ type: part.content.content.type,
2514
+ title: part.content.content.title,
2515
+ citations: part.content.content.citations,
2516
+ source: {
2517
+ type: part.content.content.source.type,
2518
+ mediaType: part.content.content.source.media_type,
2519
+ data: part.content.content.source.data
2484
2520
  }
2485
- },
2486
- providerExecuted: true
2487
- });
2488
- } else if (part.content.type === "web_fetch_tool_result_error") {
2489
- controller.enqueue({
2490
- type: "tool-result",
2491
- toolCallId: part.tool_use_id,
2492
- toolName: "web_fetch",
2493
- isError: true,
2494
- result: {
2495
- type: "web_fetch_tool_result_error",
2496
- errorCode: part.content.error_code
2497
- },
2498
- providerExecuted: true
2499
- });
2500
- }
2501
- return;
2521
+ }
2522
+ },
2523
+ providerExecuted: true
2524
+ });
2525
+ } else if (part.content.type === "web_fetch_tool_result_error") {
2526
+ controller.enqueue({
2527
+ type: "tool-result",
2528
+ toolCallId: part.tool_use_id,
2529
+ toolName: "web_fetch",
2530
+ isError: true,
2531
+ result: {
2532
+ type: "web_fetch_tool_result_error",
2533
+ errorCode: part.content.error_code
2534
+ },
2535
+ providerExecuted: true
2536
+ });
2502
2537
  }
2503
- case "web_search_tool_result": {
2504
- const part = value.content_block;
2505
- if (Array.isArray(part.content)) {
2506
- controller.enqueue({
2507
- type: "tool-result",
2508
- toolCallId: part.tool_use_id,
2509
- toolName: "web_search",
2510
- result: part.content.map((result) => {
2511
- var _a2;
2512
- return {
2513
- url: result.url,
2514
- title: result.title,
2515
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2516
- encryptedContent: result.encrypted_content,
2517
- type: result.type
2518
- };
2519
- }),
2520
- providerExecuted: true
2521
- });
2522
- for (const result of part.content) {
2523
- controller.enqueue({
2524
- type: "source",
2525
- sourceType: "url",
2526
- id: generateId3(),
2538
+ return;
2539
+ }
2540
+ case "web_search_tool_result": {
2541
+ const part = value.content_block;
2542
+ if (Array.isArray(part.content)) {
2543
+ controller.enqueue({
2544
+ type: "tool-result",
2545
+ toolCallId: part.tool_use_id,
2546
+ toolName: "web_search",
2547
+ result: part.content.map((result) => {
2548
+ var _a2;
2549
+ return {
2527
2550
  url: result.url,
2528
2551
  title: result.title,
2529
- providerMetadata: {
2530
- anthropic: {
2531
- pageAge: (_a = result.page_age) != null ? _a : null
2532
- }
2533
- }
2534
- });
2535
- }
2536
- } else {
2537
- controller.enqueue({
2538
- type: "tool-result",
2539
- toolCallId: part.tool_use_id,
2540
- toolName: "web_search",
2541
- isError: true,
2542
- result: {
2543
- type: "web_search_tool_result_error",
2544
- errorCode: part.content.error_code
2545
- },
2546
- providerExecuted: true
2547
- });
2548
- }
2549
- return;
2550
- }
2551
- // code execution 20250522:
2552
- case "code_execution_tool_result": {
2553
- const part = value.content_block;
2554
- if (part.content.type === "code_execution_result") {
2555
- controller.enqueue({
2556
- type: "tool-result",
2557
- toolCallId: part.tool_use_id,
2558
- toolName: "code_execution",
2559
- result: {
2560
- type: part.content.type,
2561
- stdout: part.content.stdout,
2562
- stderr: part.content.stderr,
2563
- return_code: part.content.return_code
2564
- },
2565
- providerExecuted: true
2566
- });
2567
- } else if (part.content.type === "code_execution_tool_result_error") {
2552
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2553
+ encryptedContent: result.encrypted_content,
2554
+ type: result.type
2555
+ };
2556
+ }),
2557
+ providerExecuted: true
2558
+ });
2559
+ for (const result of part.content) {
2568
2560
  controller.enqueue({
2569
- type: "tool-result",
2570
- toolCallId: part.tool_use_id,
2571
- toolName: "code_execution",
2572
- isError: true,
2573
- result: {
2574
- type: "code_execution_tool_result_error",
2575
- errorCode: part.content.error_code
2576
- },
2577
- providerExecuted: true
2561
+ type: "source",
2562
+ sourceType: "url",
2563
+ id: generateId3(),
2564
+ url: result.url,
2565
+ title: result.title,
2566
+ providerMetadata: {
2567
+ anthropic: {
2568
+ pageAge: (_a = result.page_age) != null ? _a : null
2569
+ }
2570
+ }
2578
2571
  });
2579
2572
  }
2580
- return;
2573
+ } else {
2574
+ controller.enqueue({
2575
+ type: "tool-result",
2576
+ toolCallId: part.tool_use_id,
2577
+ toolName: "web_search",
2578
+ isError: true,
2579
+ result: {
2580
+ type: "web_search_tool_result_error",
2581
+ errorCode: part.content.error_code
2582
+ },
2583
+ providerExecuted: true
2584
+ });
2581
2585
  }
2582
- // code execution 20250825:
2583
- case "bash_code_execution_tool_result":
2584
- case "text_editor_code_execution_tool_result": {
2585
- const part = value.content_block;
2586
+ return;
2587
+ }
2588
+ // code execution 20250522:
2589
+ case "code_execution_tool_result": {
2590
+ const part = value.content_block;
2591
+ if (part.content.type === "code_execution_result") {
2586
2592
  controller.enqueue({
2587
2593
  type: "tool-result",
2588
2594
  toolCallId: part.tool_use_id,
2589
2595
  toolName: "code_execution",
2590
- result: part.content,
2596
+ result: {
2597
+ type: part.content.type,
2598
+ stdout: part.content.stdout,
2599
+ stderr: part.content.stderr,
2600
+ return_code: part.content.return_code
2601
+ },
2602
+ providerExecuted: true
2603
+ });
2604
+ } else if (part.content.type === "code_execution_tool_result_error") {
2605
+ controller.enqueue({
2606
+ type: "tool-result",
2607
+ toolCallId: part.tool_use_id,
2608
+ toolName: "code_execution",
2609
+ isError: true,
2610
+ result: {
2611
+ type: "code_execution_tool_result_error",
2612
+ errorCode: part.content.error_code
2613
+ },
2591
2614
  providerExecuted: true
2592
2615
  });
2593
- return;
2594
- }
2595
- default: {
2596
- const _exhaustiveCheck = contentBlockType;
2597
- throw new Error(
2598
- `Unsupported content block type: ${_exhaustiveCheck}`
2599
- );
2600
2616
  }
2617
+ return;
2618
+ }
2619
+ // code execution 20250825:
2620
+ case "bash_code_execution_tool_result":
2621
+ case "text_editor_code_execution_tool_result": {
2622
+ const part = value.content_block;
2623
+ controller.enqueue({
2624
+ type: "tool-result",
2625
+ toolCallId: part.tool_use_id,
2626
+ toolName: "code_execution",
2627
+ result: part.content,
2628
+ providerExecuted: true
2629
+ });
2630
+ return;
2631
+ }
2632
+ default: {
2633
+ const _exhaustiveCheck = contentBlockType;
2634
+ throw new Error(
2635
+ `Unsupported content block type: ${_exhaustiveCheck}`
2636
+ );
2601
2637
  }
2602
2638
  }
2603
- case "content_block_stop": {
2604
- if (contentBlocks[value.index] != null) {
2605
- const contentBlock = contentBlocks[value.index];
2606
- switch (contentBlock.type) {
2607
- case "text": {
2639
+ }
2640
+ case "content_block_stop": {
2641
+ if (contentBlocks[value.index] != null) {
2642
+ const contentBlock = contentBlocks[value.index];
2643
+ switch (contentBlock.type) {
2644
+ case "text": {
2645
+ controller.enqueue({
2646
+ type: "text-end",
2647
+ id: String(value.index)
2648
+ });
2649
+ break;
2650
+ }
2651
+ case "reasoning": {
2652
+ controller.enqueue({
2653
+ type: "reasoning-end",
2654
+ id: String(value.index)
2655
+ });
2656
+ break;
2657
+ }
2658
+ case "tool-call":
2659
+ if (!usesJsonResponseTool) {
2608
2660
  controller.enqueue({
2609
- type: "text-end",
2610
- id: String(value.index)
2661
+ type: "tool-input-end",
2662
+ id: contentBlock.toolCallId
2611
2663
  });
2612
- break;
2613
- }
2614
- case "reasoning": {
2664
+ const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2615
2665
  controller.enqueue({
2616
- type: "reasoning-end",
2617
- id: String(value.index)
2666
+ type: "tool-call",
2667
+ toolCallId: contentBlock.toolCallId,
2668
+ toolName,
2669
+ input: contentBlock.input,
2670
+ providerExecuted: contentBlock.providerExecuted
2618
2671
  });
2619
- break;
2620
2672
  }
2621
- case "tool-call":
2622
- if (!usesJsonResponseTool) {
2623
- controller.enqueue({
2624
- type: "tool-input-end",
2625
- id: contentBlock.toolCallId
2626
- });
2627
- const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2628
- controller.enqueue({
2629
- type: "tool-call",
2630
- toolCallId: contentBlock.toolCallId,
2631
- toolName,
2632
- input: contentBlock.input,
2633
- providerExecuted: contentBlock.providerExecuted
2634
- });
2635
- }
2636
- break;
2637
- }
2638
- delete contentBlocks[value.index];
2673
+ break;
2639
2674
  }
2640
- blockType = void 0;
2641
- return;
2675
+ delete contentBlocks[value.index];
2642
2676
  }
2643
- case "content_block_delta": {
2644
- const deltaType = value.delta.type;
2645
- switch (deltaType) {
2646
- case "text_delta": {
2647
- if (usesJsonResponseTool) {
2648
- return;
2649
- }
2650
- controller.enqueue({
2651
- type: "text-delta",
2652
- id: String(value.index),
2653
- delta: value.delta.text
2654
- });
2677
+ blockType = void 0;
2678
+ return;
2679
+ }
2680
+ case "content_block_delta": {
2681
+ const deltaType = value.delta.type;
2682
+ switch (deltaType) {
2683
+ case "text_delta": {
2684
+ if (usesJsonResponseTool) {
2655
2685
  return;
2656
2686
  }
2657
- case "thinking_delta": {
2687
+ controller.enqueue({
2688
+ type: "text-delta",
2689
+ id: String(value.index),
2690
+ delta: value.delta.text
2691
+ });
2692
+ return;
2693
+ }
2694
+ case "thinking_delta": {
2695
+ controller.enqueue({
2696
+ type: "reasoning-delta",
2697
+ id: String(value.index),
2698
+ delta: value.delta.thinking
2699
+ });
2700
+ return;
2701
+ }
2702
+ case "signature_delta": {
2703
+ if (blockType === "thinking") {
2658
2704
  controller.enqueue({
2659
2705
  type: "reasoning-delta",
2660
2706
  id: String(value.index),
2661
- delta: value.delta.thinking
2707
+ delta: "",
2708
+ providerMetadata: {
2709
+ anthropic: {
2710
+ signature: value.delta.signature
2711
+ }
2712
+ }
2662
2713
  });
2663
- return;
2664
2714
  }
2665
- case "signature_delta": {
2666
- if (blockType === "thinking") {
2667
- controller.enqueue({
2668
- type: "reasoning-delta",
2669
- id: String(value.index),
2670
- delta: "",
2671
- providerMetadata: {
2672
- anthropic: {
2673
- signature: value.delta.signature
2674
- }
2675
- }
2676
- });
2677
- }
2715
+ return;
2716
+ }
2717
+ case "input_json_delta": {
2718
+ const contentBlock = contentBlocks[value.index];
2719
+ let delta = value.delta.partial_json;
2720
+ if (delta.length === 0) {
2678
2721
  return;
2679
2722
  }
2680
- case "input_json_delta": {
2681
- const contentBlock = contentBlocks[value.index];
2682
- let delta = value.delta.partial_json;
2683
- if (delta.length === 0) {
2723
+ if (usesJsonResponseTool) {
2724
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2684
2725
  return;
2685
2726
  }
2686
- if (usesJsonResponseTool) {
2687
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2688
- return;
2689
- }
2690
- controller.enqueue({
2691
- type: "text-delta",
2692
- id: String(value.index),
2693
- delta
2694
- });
2695
- } else {
2696
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2697
- return;
2698
- }
2699
- if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2700
- delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2701
- }
2702
- controller.enqueue({
2703
- type: "tool-input-delta",
2704
- id: contentBlock.toolCallId,
2705
- delta
2706
- });
2707
- contentBlock.input += delta;
2708
- contentBlock.firstDelta = false;
2727
+ controller.enqueue({
2728
+ type: "text-delta",
2729
+ id: String(value.index),
2730
+ delta
2731
+ });
2732
+ } else {
2733
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2734
+ return;
2709
2735
  }
2710
- return;
2711
- }
2712
- case "citations_delta": {
2713
- const citation = value.delta.citation;
2714
- const source = createCitationSource(
2715
- citation,
2716
- citationDocuments,
2717
- generateId3
2718
- );
2719
- if (source) {
2720
- controller.enqueue(source);
2736
+ if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2737
+ delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2721
2738
  }
2722
- return;
2739
+ controller.enqueue({
2740
+ type: "tool-input-delta",
2741
+ id: contentBlock.toolCallId,
2742
+ delta
2743
+ });
2744
+ contentBlock.input += delta;
2745
+ contentBlock.firstDelta = false;
2723
2746
  }
2724
- default: {
2725
- const _exhaustiveCheck = deltaType;
2726
- throw new Error(
2727
- `Unsupported delta type: ${_exhaustiveCheck}`
2728
- );
2747
+ return;
2748
+ }
2749
+ case "citations_delta": {
2750
+ const citation = value.delta.citation;
2751
+ const source = createCitationSource(
2752
+ citation,
2753
+ citationDocuments,
2754
+ generateId3
2755
+ );
2756
+ if (source) {
2757
+ controller.enqueue(source);
2729
2758
  }
2759
+ return;
2760
+ }
2761
+ default: {
2762
+ const _exhaustiveCheck = deltaType;
2763
+ throw new Error(
2764
+ `Unsupported delta type: ${_exhaustiveCheck}`
2765
+ );
2730
2766
  }
2731
2767
  }
2732
- case "message_start": {
2733
- usage.inputTokens = value.message.usage.input_tokens;
2734
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2735
- rawUsage = {
2736
- ...value.message.usage
2737
- };
2738
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2739
- controller.enqueue({
2740
- type: "response-metadata",
2741
- id: (_d = value.message.id) != null ? _d : void 0,
2742
- modelId: (_e = value.message.model) != null ? _e : void 0
2743
- });
2744
- return;
2745
- }
2746
- case "message_delta": {
2747
- usage.outputTokens = value.usage.output_tokens;
2748
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2749
- finishReason = mapAnthropicStopReason({
2750
- finishReason: value.delta.stop_reason,
2751
- isJsonResponseFromTool: usesJsonResponseTool
2752
- });
2753
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2754
- container = value.delta.container != null ? {
2755
- expiresAt: value.delta.container.expires_at,
2756
- id: value.delta.container.id,
2757
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2758
- type: skill.type,
2759
- skillId: skill.skill_id,
2760
- version: skill.version
2761
- }))) != null ? _j : null
2762
- } : null;
2763
- rawUsage = {
2764
- ...rawUsage,
2765
- ...value.usage
2766
- };
2767
- return;
2768
- }
2769
- case "message_stop": {
2770
- controller.enqueue({
2771
- type: "finish",
2772
- finishReason,
2773
- usage,
2774
- providerMetadata: {
2775
- anthropic: {
2776
- usage: rawUsage != null ? rawUsage : null,
2777
- cacheCreationInputTokens,
2778
- stopSequence,
2779
- container
2780
- }
2768
+ }
2769
+ case "message_start": {
2770
+ usage.inputTokens = value.message.usage.input_tokens;
2771
+ usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2772
+ rawUsage = {
2773
+ ...value.message.usage
2774
+ };
2775
+ cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2776
+ controller.enqueue({
2777
+ type: "response-metadata",
2778
+ id: (_d = value.message.id) != null ? _d : void 0,
2779
+ modelId: (_e = value.message.model) != null ? _e : void 0
2780
+ });
2781
+ return;
2782
+ }
2783
+ case "message_delta": {
2784
+ usage.outputTokens = value.usage.output_tokens;
2785
+ usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2786
+ finishReason = mapAnthropicStopReason({
2787
+ finishReason: value.delta.stop_reason,
2788
+ isJsonResponseFromTool: usesJsonResponseTool
2789
+ });
2790
+ stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2791
+ container = value.delta.container != null ? {
2792
+ expiresAt: value.delta.container.expires_at,
2793
+ id: value.delta.container.id,
2794
+ skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2795
+ type: skill.type,
2796
+ skillId: skill.skill_id,
2797
+ version: skill.version
2798
+ }))) != null ? _j : null
2799
+ } : null;
2800
+ rawUsage = {
2801
+ ...rawUsage,
2802
+ ...value.usage
2803
+ };
2804
+ return;
2805
+ }
2806
+ case "message_stop": {
2807
+ controller.enqueue({
2808
+ type: "finish",
2809
+ finishReason,
2810
+ usage,
2811
+ providerMetadata: {
2812
+ anthropic: {
2813
+ usage: rawUsage != null ? rawUsage : null,
2814
+ cacheCreationInputTokens,
2815
+ stopSequence,
2816
+ container
2781
2817
  }
2782
- });
2783
- return;
2784
- }
2785
- case "error": {
2786
- controller.enqueue({ type: "error", error: value.error });
2787
- return;
2788
- }
2789
- default: {
2790
- const _exhaustiveCheck = value;
2791
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2792
- }
2818
+ }
2819
+ });
2820
+ return;
2821
+ }
2822
+ case "error": {
2823
+ controller.enqueue({ type: "error", error: value.error });
2824
+ return;
2825
+ }
2826
+ default: {
2827
+ const _exhaustiveCheck = value;
2828
+ throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2793
2829
  }
2794
2830
  }
2795
- })
2796
- ),
2797
- request: { body },
2798
- response: { headers: responseHeaders }
2799
- };
2831
+ }
2832
+ })
2833
+ );
2834
+ const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
2835
+ stream = streamForConsumer;
2836
+ const reader = streamForFirstChunk.getReader();
2837
+ (async () => {
2838
+ try {
2839
+ const { done } = await reader.read();
2840
+ if (!done) {
2841
+ await reader.cancel();
2842
+ }
2843
+ } catch (error) {
2844
+ try {
2845
+ await reader.cancel();
2846
+ } catch (e) {
2847
+ }
2848
+ } finally {
2849
+ reader.releaseLock();
2850
+ }
2851
+ })();
2852
+ return returnPromise.promise;
2800
2853
  }
2801
2854
  };
2802
2855
  function getModelCapabilities(modelId) {