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