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