@ai-sdk/anthropic 2.0.49 → 2.0.51

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