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