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