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