@ai-sdk/anthropic 2.0.52 → 2.0.53

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.
@@ -7,7 +7,6 @@ import {
7
7
  combineHeaders,
8
8
  createEventSourceResponseHandler,
9
9
  createJsonResponseHandler,
10
- DelayedPromise,
11
10
  generateId,
12
11
  parseProviderOptions as parseProviderOptions2,
13
12
  postJsonToApi,
@@ -2319,6 +2318,7 @@ var AnthropicMessagesLanguageModel = class {
2319
2318
  };
2320
2319
  }
2321
2320
  async doStream(options) {
2321
+ var _a, _b;
2322
2322
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2323
2323
  ...options,
2324
2324
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
@@ -2350,25 +2350,13 @@ var AnthropicMessagesLanguageModel = class {
2350
2350
  let container = null;
2351
2351
  let blockType = void 0;
2352
2352
  const generateId2 = this.generateId;
2353
- let isFirstChunk = true;
2354
- let stream = void 0;
2355
- const returnPromise = new DelayedPromise();
2356
2353
  const transformedStream = response.pipeThrough(
2357
2354
  new TransformStream({
2358
2355
  start(controller) {
2359
2356
  controller.enqueue({ type: "stream-start", warnings });
2360
2357
  },
2361
- async flush() {
2362
- if (returnPromise.isPending()) {
2363
- returnPromise.resolve({
2364
- stream,
2365
- request: { body },
2366
- response: { headers: responseHeaders }
2367
- });
2368
- }
2369
- },
2370
2358
  transform(chunk, controller) {
2371
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2359
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
2372
2360
  if (options.includeRawChunks) {
2373
2361
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2374
2362
  }
@@ -2376,29 +2364,6 @@ var AnthropicMessagesLanguageModel = class {
2376
2364
  controller.enqueue({ type: "error", error: chunk.error });
2377
2365
  return;
2378
2366
  }
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;
2394
- }
2395
- isFirstChunk = false;
2396
- returnPromise.resolve({
2397
- stream,
2398
- request: { body },
2399
- response: { headers: responseHeaders }
2400
- });
2401
- }
2402
2367
  const value = chunk.value;
2403
2368
  switch (value.type) {
2404
2369
  case "ping": {
@@ -2530,11 +2495,11 @@ var AnthropicMessagesLanguageModel = class {
2530
2495
  toolCallId: part.tool_use_id,
2531
2496
  toolName: "web_search",
2532
2497
  result: part.content.map((result) => {
2533
- var _a2;
2498
+ var _a3;
2534
2499
  return {
2535
2500
  url: result.url,
2536
2501
  title: result.title,
2537
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2502
+ pageAge: (_a3 = result.page_age) != null ? _a3 : null,
2538
2503
  encryptedContent: result.encrypted_content,
2539
2504
  type: result.type
2540
2505
  };
@@ -2550,7 +2515,7 @@ var AnthropicMessagesLanguageModel = class {
2550
2515
  title: result.title,
2551
2516
  providerMetadata: {
2552
2517
  anthropic: {
2553
- pageAge: (_a = result.page_age) != null ? _a : null
2518
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null
2554
2519
  }
2555
2520
  }
2556
2521
  });
@@ -2753,7 +2718,7 @@ var AnthropicMessagesLanguageModel = class {
2753
2718
  }
2754
2719
  case "message_start": {
2755
2720
  usage.inputTokens = value.message.usage.input_tokens;
2756
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2721
+ usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
2757
2722
  rawUsage = {
2758
2723
  ...value.message.usage
2759
2724
  };
@@ -2817,22 +2782,35 @@ var AnthropicMessagesLanguageModel = class {
2817
2782
  })
2818
2783
  );
2819
2784
  const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
2820
- stream = streamForConsumer;
2821
2785
  const firstChunkReader = streamForFirstChunk.getReader();
2822
2786
  try {
2823
- const { done } = await firstChunkReader.read();
2824
- if (!done) {
2825
- firstChunkReader.cancel();
2787
+ await firstChunkReader.read();
2788
+ let result = await firstChunkReader.read();
2789
+ if (((_a = result.value) == null ? void 0 : _a.type) === "raw") {
2790
+ result = await firstChunkReader.read();
2826
2791
  }
2827
- } catch (error) {
2828
- try {
2829
- firstChunkReader.cancel();
2830
- } catch (e) {
2792
+ if (((_b = result.value) == null ? void 0 : _b.type) === "error") {
2793
+ const error = result.value.error;
2794
+ throw new APICallError({
2795
+ message: error.message,
2796
+ url,
2797
+ requestBodyValues: body,
2798
+ statusCode: error.type === "overloaded_error" ? 529 : 500,
2799
+ responseHeaders,
2800
+ responseBody: JSON.stringify(error),
2801
+ isRetryable: error.type === "overloaded_error"
2802
+ });
2831
2803
  }
2832
2804
  } finally {
2805
+ firstChunkReader.cancel().catch(() => {
2806
+ });
2833
2807
  firstChunkReader.releaseLock();
2834
2808
  }
2835
- return returnPromise.promise;
2809
+ return {
2810
+ stream: streamForConsumer,
2811
+ request: { body },
2812
+ response: { headers: responseHeaders }
2813
+ };
2836
2814
  }
2837
2815
  };
2838
2816
  function getModelCapabilities(modelId) {