@ai-sdk/anthropic 2.0.48 → 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.48" : "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,
@@ -1787,6 +1789,7 @@ var AnthropicMessagesLanguageModel = class {
1787
1789
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
1788
1790
  }
1789
1791
  async getArgs({
1792
+ userSuppliedBetas,
1790
1793
  prompt,
1791
1794
  maxOutputTokens,
1792
1795
  temperature,
@@ -2004,7 +2007,7 @@ var AnthropicMessagesLanguageModel = class {
2004
2007
  tool_choice: anthropicToolChoice
2005
2008
  },
2006
2009
  warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
2007
- betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
2010
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas, ...userSuppliedBetas]),
2008
2011
  usesJsonResponseTool: jsonResponseTool != null
2009
2012
  };
2010
2013
  }
@@ -2014,8 +2017,20 @@ var AnthropicMessagesLanguageModel = class {
2014
2017
  }) {
2015
2018
  return combineHeaders(
2016
2019
  await resolve(this.config.headers),
2017
- betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
2018
- headers
2020
+ headers,
2021
+ betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {}
2022
+ );
2023
+ }
2024
+ async getBetasFromHeaders(requestHeaders) {
2025
+ var _a, _b;
2026
+ const configHeaders = await resolve(this.config.headers);
2027
+ const configBetaHeader = (_a = configHeaders["anthropic-beta"]) != null ? _a : "";
2028
+ const requestBetaHeader = (_b = requestHeaders == null ? void 0 : requestHeaders["anthropic-beta"]) != null ? _b : "";
2029
+ return new Set(
2030
+ [
2031
+ ...configBetaHeader.toLowerCase().split(","),
2032
+ ...requestBetaHeader.toLowerCase().split(",")
2033
+ ].map((beta) => beta.trim()).filter((beta) => beta !== "")
2019
2034
  );
2020
2035
  }
2021
2036
  buildRequestUrl(isStreaming) {
@@ -2051,7 +2066,10 @@ var AnthropicMessagesLanguageModel = class {
2051
2066
  }
2052
2067
  async doGenerate(options) {
2053
2068
  var _a, _b, _c, _d, _e, _f, _g, _h;
2054
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2069
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2070
+ ...options,
2071
+ userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2072
+ });
2055
2073
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2056
2074
  const {
2057
2075
  responseHeaders,
@@ -2316,11 +2334,15 @@ var AnthropicMessagesLanguageModel = class {
2316
2334
  };
2317
2335
  }
2318
2336
  async doStream(options) {
2319
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2337
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2338
+ ...options,
2339
+ userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2340
+ });
2320
2341
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2321
2342
  const body = { ...args, stream: true };
2343
+ const url = this.buildRequestUrl(true);
2322
2344
  const { responseHeaders, value: response } = await postJsonToApi({
2323
- url: this.buildRequestUrl(true),
2345
+ url,
2324
2346
  headers: await this.getHeaders({ betas, headers: options.headers }),
2325
2347
  body: this.transformRequestBody(body),
2326
2348
  failedResponseHandler: anthropicFailedResponseHandler,
@@ -2343,441 +2365,491 @@ var AnthropicMessagesLanguageModel = class {
2343
2365
  let container = null;
2344
2366
  let blockType = void 0;
2345
2367
  const generateId3 = this.generateId;
2346
- return {
2347
- stream: response.pipeThrough(
2348
- new TransformStream({
2349
- start(controller) {
2350
- controller.enqueue({ type: "stream-start", warnings });
2351
- },
2352
- transform(chunk, controller) {
2353
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2354
- if (options.includeRawChunks) {
2355
- 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;
2356
2409
  }
2357
- if (!chunk.success) {
2358
- 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": {
2359
2420
  return;
2360
2421
  }
2361
- const value = chunk.value;
2362
- switch (value.type) {
2363
- case "ping": {
2364
- return;
2365
- }
2366
- case "content_block_start": {
2367
- const contentBlockType = value.content_block.type;
2368
- blockType = contentBlockType;
2369
- switch (contentBlockType) {
2370
- case "text": {
2371
- contentBlocks[value.index] = { type: "text" };
2372
- controller.enqueue({
2373
- type: "text-start",
2374
- id: String(value.index)
2375
- });
2376
- return;
2377
- }
2378
- case "thinking": {
2379
- contentBlocks[value.index] = { type: "reasoning" };
2380
- controller.enqueue({
2381
- type: "reasoning-start",
2382
- id: String(value.index)
2383
- });
2384
- return;
2385
- }
2386
- case "redacted_thinking": {
2387
- contentBlocks[value.index] = { type: "reasoning" };
2388
- controller.enqueue({
2389
- type: "reasoning-start",
2390
- id: String(value.index),
2391
- providerMetadata: {
2392
- anthropic: {
2393
- redactedData: value.content_block.data
2394
- }
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
2395
2450
  }
2396
- });
2397
- return;
2398
- }
2399
- case "tool_use": {
2400
- 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] = {
2401
2484
  type: "tool-call",
2402
2485
  toolCallId: value.content_block.id,
2403
2486
  toolName: value.content_block.name,
2404
2487
  input: "",
2488
+ providerExecuted: true,
2405
2489
  firstDelta: true
2406
2490
  };
2407
- controller.enqueue(
2408
- usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
2409
- type: "tool-input-start",
2410
- id: value.content_block.id,
2411
- toolName: value.content_block.name
2412
- }
2413
- );
2414
- return;
2415
- }
2416
- case "server_tool_use": {
2417
- if ([
2418
- "web_fetch",
2419
- "web_search",
2420
- // code execution 20250825:
2421
- "code_execution",
2422
- // code execution 20250825 text editor:
2423
- "text_editor_code_execution",
2424
- // code execution 20250825 bash:
2425
- "bash_code_execution"
2426
- ].includes(value.content_block.name)) {
2427
- contentBlocks[value.index] = {
2428
- type: "tool-call",
2429
- toolCallId: value.content_block.id,
2430
- toolName: value.content_block.name,
2431
- input: "",
2432
- providerExecuted: true,
2433
- firstDelta: true
2434
- };
2435
- const mappedToolName = value.content_block.name === "text_editor_code_execution" || value.content_block.name === "bash_code_execution" ? "code_execution" : value.content_block.name;
2436
- controller.enqueue({
2437
- type: "tool-input-start",
2438
- id: value.content_block.id,
2439
- toolName: mappedToolName,
2440
- providerExecuted: true
2441
- });
2442
- }
2443
- 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
+ });
2444
2498
  }
2445
- case "web_fetch_tool_result": {
2446
- const part = value.content_block;
2447
- if (part.content.type === "web_fetch_result") {
2448
- controller.enqueue({
2449
- type: "tool-result",
2450
- toolCallId: part.tool_use_id,
2451
- toolName: "web_fetch",
2452
- result: {
2453
- type: "web_fetch_result",
2454
- url: part.content.url,
2455
- retrievedAt: part.content.retrieved_at,
2456
- content: {
2457
- type: part.content.content.type,
2458
- title: part.content.content.title,
2459
- citations: part.content.content.citations,
2460
- source: {
2461
- type: part.content.content.source.type,
2462
- mediaType: part.content.content.source.media_type,
2463
- data: part.content.content.source.data
2464
- }
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
2465
2520
  }
2466
- },
2467
- providerExecuted: true
2468
- });
2469
- } else if (part.content.type === "web_fetch_tool_result_error") {
2470
- controller.enqueue({
2471
- type: "tool-result",
2472
- toolCallId: part.tool_use_id,
2473
- toolName: "web_fetch",
2474
- isError: true,
2475
- result: {
2476
- type: "web_fetch_tool_result_error",
2477
- errorCode: part.content.error_code
2478
- },
2479
- providerExecuted: true
2480
- });
2481
- }
2482
- 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
+ });
2483
2537
  }
2484
- case "web_search_tool_result": {
2485
- const part = value.content_block;
2486
- if (Array.isArray(part.content)) {
2487
- controller.enqueue({
2488
- type: "tool-result",
2489
- toolCallId: part.tool_use_id,
2490
- toolName: "web_search",
2491
- result: part.content.map((result) => {
2492
- var _a2;
2493
- return {
2494
- url: result.url,
2495
- title: result.title,
2496
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2497
- encryptedContent: result.encrypted_content,
2498
- type: result.type
2499
- };
2500
- }),
2501
- providerExecuted: true
2502
- });
2503
- for (const result of part.content) {
2504
- controller.enqueue({
2505
- type: "source",
2506
- sourceType: "url",
2507
- 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 {
2508
2550
  url: result.url,
2509
2551
  title: result.title,
2510
- providerMetadata: {
2511
- anthropic: {
2512
- pageAge: (_a = result.page_age) != null ? _a : null
2513
- }
2514
- }
2515
- });
2516
- }
2517
- } else {
2518
- controller.enqueue({
2519
- type: "tool-result",
2520
- toolCallId: part.tool_use_id,
2521
- toolName: "web_search",
2522
- isError: true,
2523
- result: {
2524
- type: "web_search_tool_result_error",
2525
- errorCode: part.content.error_code
2526
- },
2527
- providerExecuted: true
2528
- });
2529
- }
2530
- return;
2531
- }
2532
- // code execution 20250522:
2533
- case "code_execution_tool_result": {
2534
- const part = value.content_block;
2535
- if (part.content.type === "code_execution_result") {
2536
- controller.enqueue({
2537
- type: "tool-result",
2538
- toolCallId: part.tool_use_id,
2539
- toolName: "code_execution",
2540
- result: {
2541
- type: part.content.type,
2542
- stdout: part.content.stdout,
2543
- stderr: part.content.stderr,
2544
- return_code: part.content.return_code
2545
- },
2546
- providerExecuted: true
2547
- });
2548
- } 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) {
2549
2560
  controller.enqueue({
2550
- type: "tool-result",
2551
- toolCallId: part.tool_use_id,
2552
- toolName: "code_execution",
2553
- isError: true,
2554
- result: {
2555
- type: "code_execution_tool_result_error",
2556
- errorCode: part.content.error_code
2557
- },
2558
- 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
+ }
2559
2571
  });
2560
2572
  }
2561
- 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
+ });
2562
2585
  }
2563
- // code execution 20250825:
2564
- case "bash_code_execution_tool_result":
2565
- case "text_editor_code_execution_tool_result": {
2566
- 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") {
2567
2592
  controller.enqueue({
2568
2593
  type: "tool-result",
2569
2594
  toolCallId: part.tool_use_id,
2570
2595
  toolName: "code_execution",
2571
- 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
+ },
2572
2614
  providerExecuted: true
2573
2615
  });
2574
- return;
2575
- }
2576
- default: {
2577
- const _exhaustiveCheck = contentBlockType;
2578
- throw new Error(
2579
- `Unsupported content block type: ${_exhaustiveCheck}`
2580
- );
2581
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
+ );
2582
2637
  }
2583
2638
  }
2584
- case "content_block_stop": {
2585
- if (contentBlocks[value.index] != null) {
2586
- const contentBlock = contentBlocks[value.index];
2587
- switch (contentBlock.type) {
2588
- 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) {
2589
2660
  controller.enqueue({
2590
- type: "text-end",
2591
- id: String(value.index)
2661
+ type: "tool-input-end",
2662
+ id: contentBlock.toolCallId
2592
2663
  });
2593
- break;
2594
- }
2595
- case "reasoning": {
2664
+ const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2596
2665
  controller.enqueue({
2597
- type: "reasoning-end",
2598
- id: String(value.index)
2666
+ type: "tool-call",
2667
+ toolCallId: contentBlock.toolCallId,
2668
+ toolName,
2669
+ input: contentBlock.input,
2670
+ providerExecuted: contentBlock.providerExecuted
2599
2671
  });
2600
- break;
2601
2672
  }
2602
- case "tool-call":
2603
- if (!usesJsonResponseTool) {
2604
- controller.enqueue({
2605
- type: "tool-input-end",
2606
- id: contentBlock.toolCallId
2607
- });
2608
- const toolName = contentBlock.toolName === "text_editor_code_execution" || contentBlock.toolName === "bash_code_execution" ? "code_execution" : contentBlock.toolName;
2609
- controller.enqueue({
2610
- type: "tool-call",
2611
- toolCallId: contentBlock.toolCallId,
2612
- toolName,
2613
- input: contentBlock.input,
2614
- providerExecuted: contentBlock.providerExecuted
2615
- });
2616
- }
2617
- break;
2618
- }
2619
- delete contentBlocks[value.index];
2673
+ break;
2620
2674
  }
2621
- blockType = void 0;
2622
- return;
2675
+ delete contentBlocks[value.index];
2623
2676
  }
2624
- case "content_block_delta": {
2625
- const deltaType = value.delta.type;
2626
- switch (deltaType) {
2627
- case "text_delta": {
2628
- if (usesJsonResponseTool) {
2629
- return;
2630
- }
2631
- controller.enqueue({
2632
- type: "text-delta",
2633
- id: String(value.index),
2634
- delta: value.delta.text
2635
- });
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) {
2636
2685
  return;
2637
2686
  }
2638
- 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") {
2639
2704
  controller.enqueue({
2640
2705
  type: "reasoning-delta",
2641
2706
  id: String(value.index),
2642
- delta: value.delta.thinking
2707
+ delta: "",
2708
+ providerMetadata: {
2709
+ anthropic: {
2710
+ signature: value.delta.signature
2711
+ }
2712
+ }
2643
2713
  });
2644
- return;
2645
2714
  }
2646
- case "signature_delta": {
2647
- if (blockType === "thinking") {
2648
- controller.enqueue({
2649
- type: "reasoning-delta",
2650
- id: String(value.index),
2651
- delta: "",
2652
- providerMetadata: {
2653
- anthropic: {
2654
- signature: value.delta.signature
2655
- }
2656
- }
2657
- });
2658
- }
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) {
2659
2721
  return;
2660
2722
  }
2661
- case "input_json_delta": {
2662
- const contentBlock = contentBlocks[value.index];
2663
- let delta = value.delta.partial_json;
2664
- if (delta.length === 0) {
2723
+ if (usesJsonResponseTool) {
2724
+ if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2665
2725
  return;
2666
2726
  }
2667
- if (usesJsonResponseTool) {
2668
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
2669
- return;
2670
- }
2671
- controller.enqueue({
2672
- type: "text-delta",
2673
- id: String(value.index),
2674
- delta
2675
- });
2676
- } else {
2677
- if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
2678
- return;
2679
- }
2680
- if (contentBlock.firstDelta && (contentBlock.toolName === "bash_code_execution" || contentBlock.toolName === "text_editor_code_execution")) {
2681
- delta = `{"type": "${contentBlock.toolName}",${delta.substring(1)}`;
2682
- }
2683
- controller.enqueue({
2684
- type: "tool-input-delta",
2685
- id: contentBlock.toolCallId,
2686
- delta
2687
- });
2688
- contentBlock.input += delta;
2689
- 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;
2690
2735
  }
2691
- return;
2692
- }
2693
- case "citations_delta": {
2694
- const citation = value.delta.citation;
2695
- const source = createCitationSource(
2696
- citation,
2697
- citationDocuments,
2698
- generateId3
2699
- );
2700
- if (source) {
2701
- 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)}`;
2702
2738
  }
2703
- return;
2739
+ controller.enqueue({
2740
+ type: "tool-input-delta",
2741
+ id: contentBlock.toolCallId,
2742
+ delta
2743
+ });
2744
+ contentBlock.input += delta;
2745
+ contentBlock.firstDelta = false;
2704
2746
  }
2705
- default: {
2706
- const _exhaustiveCheck = deltaType;
2707
- throw new Error(
2708
- `Unsupported delta type: ${_exhaustiveCheck}`
2709
- );
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);
2710
2758
  }
2759
+ return;
2760
+ }
2761
+ default: {
2762
+ const _exhaustiveCheck = deltaType;
2763
+ throw new Error(
2764
+ `Unsupported delta type: ${_exhaustiveCheck}`
2765
+ );
2711
2766
  }
2712
2767
  }
2713
- case "message_start": {
2714
- usage.inputTokens = value.message.usage.input_tokens;
2715
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2716
- rawUsage = {
2717
- ...value.message.usage
2718
- };
2719
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
2720
- controller.enqueue({
2721
- type: "response-metadata",
2722
- id: (_d = value.message.id) != null ? _d : void 0,
2723
- modelId: (_e = value.message.model) != null ? _e : void 0
2724
- });
2725
- return;
2726
- }
2727
- case "message_delta": {
2728
- usage.outputTokens = value.usage.output_tokens;
2729
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
2730
- finishReason = mapAnthropicStopReason({
2731
- finishReason: value.delta.stop_reason,
2732
- isJsonResponseFromTool: usesJsonResponseTool
2733
- });
2734
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
2735
- container = value.delta.container != null ? {
2736
- expiresAt: value.delta.container.expires_at,
2737
- id: value.delta.container.id,
2738
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
2739
- type: skill.type,
2740
- skillId: skill.skill_id,
2741
- version: skill.version
2742
- }))) != null ? _j : null
2743
- } : null;
2744
- rawUsage = {
2745
- ...rawUsage,
2746
- ...value.usage
2747
- };
2748
- return;
2749
- }
2750
- case "message_stop": {
2751
- controller.enqueue({
2752
- type: "finish",
2753
- finishReason,
2754
- usage,
2755
- providerMetadata: {
2756
- anthropic: {
2757
- usage: rawUsage != null ? rawUsage : null,
2758
- cacheCreationInputTokens,
2759
- stopSequence,
2760
- container
2761
- }
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
2762
2817
  }
2763
- });
2764
- return;
2765
- }
2766
- case "error": {
2767
- controller.enqueue({ type: "error", error: value.error });
2768
- return;
2769
- }
2770
- default: {
2771
- const _exhaustiveCheck = value;
2772
- throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
2773
- }
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}`);
2774
2829
  }
2775
2830
  }
2776
- })
2777
- ),
2778
- request: { body },
2779
- response: { headers: responseHeaders }
2780
- };
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;
2781
2853
  }
2782
2854
  };
2783
2855
  function getModelCapabilities(modelId) {