@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.
@@ -2307,6 +2307,7 @@ var AnthropicMessagesLanguageModel = class {
2307
2307
  };
2308
2308
  }
2309
2309
  async doStream(options) {
2310
+ var _a, _b;
2310
2311
  const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2311
2312
  ...options,
2312
2313
  userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
@@ -2338,25 +2339,13 @@ var AnthropicMessagesLanguageModel = class {
2338
2339
  let container = null;
2339
2340
  let blockType = void 0;
2340
2341
  const generateId2 = this.generateId;
2341
- let isFirstChunk = true;
2342
- let stream = void 0;
2343
- const returnPromise = new import_provider_utils11.DelayedPromise();
2344
2342
  const transformedStream = response.pipeThrough(
2345
2343
  new TransformStream({
2346
2344
  start(controller) {
2347
2345
  controller.enqueue({ type: "stream-start", warnings });
2348
2346
  },
2349
- async flush() {
2350
- if (returnPromise.isPending()) {
2351
- returnPromise.resolve({
2352
- stream,
2353
- request: { body },
2354
- response: { headers: responseHeaders }
2355
- });
2356
- }
2357
- },
2358
2347
  transform(chunk, controller) {
2359
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2348
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
2360
2349
  if (options.includeRawChunks) {
2361
2350
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2362
2351
  }
@@ -2364,29 +2353,6 @@ var AnthropicMessagesLanguageModel = class {
2364
2353
  controller.enqueue({ type: "error", error: chunk.error });
2365
2354
  return;
2366
2355
  }
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;
2382
- }
2383
- isFirstChunk = false;
2384
- returnPromise.resolve({
2385
- stream,
2386
- request: { body },
2387
- response: { headers: responseHeaders }
2388
- });
2389
- }
2390
2356
  const value = chunk.value;
2391
2357
  switch (value.type) {
2392
2358
  case "ping": {
@@ -2518,11 +2484,11 @@ var AnthropicMessagesLanguageModel = class {
2518
2484
  toolCallId: part.tool_use_id,
2519
2485
  toolName: "web_search",
2520
2486
  result: part.content.map((result) => {
2521
- var _a2;
2487
+ var _a3;
2522
2488
  return {
2523
2489
  url: result.url,
2524
2490
  title: result.title,
2525
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2491
+ pageAge: (_a3 = result.page_age) != null ? _a3 : null,
2526
2492
  encryptedContent: result.encrypted_content,
2527
2493
  type: result.type
2528
2494
  };
@@ -2538,7 +2504,7 @@ var AnthropicMessagesLanguageModel = class {
2538
2504
  title: result.title,
2539
2505
  providerMetadata: {
2540
2506
  anthropic: {
2541
- pageAge: (_a = result.page_age) != null ? _a : null
2507
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null
2542
2508
  }
2543
2509
  }
2544
2510
  });
@@ -2741,7 +2707,7 @@ var AnthropicMessagesLanguageModel = class {
2741
2707
  }
2742
2708
  case "message_start": {
2743
2709
  usage.inputTokens = value.message.usage.input_tokens;
2744
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2710
+ usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
2745
2711
  rawUsage = {
2746
2712
  ...value.message.usage
2747
2713
  };
@@ -2805,22 +2771,35 @@ var AnthropicMessagesLanguageModel = class {
2805
2771
  })
2806
2772
  );
2807
2773
  const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
2808
- stream = streamForConsumer;
2809
2774
  const firstChunkReader = streamForFirstChunk.getReader();
2810
2775
  try {
2811
- const { done } = await firstChunkReader.read();
2812
- if (!done) {
2813
- firstChunkReader.cancel();
2776
+ await firstChunkReader.read();
2777
+ let result = await firstChunkReader.read();
2778
+ if (((_a = result.value) == null ? void 0 : _a.type) === "raw") {
2779
+ result = await firstChunkReader.read();
2814
2780
  }
2815
- } catch (error) {
2816
- try {
2817
- firstChunkReader.cancel();
2818
- } catch (e) {
2781
+ if (((_b = result.value) == null ? void 0 : _b.type) === "error") {
2782
+ const error = result.value.error;
2783
+ throw new import_provider3.APICallError({
2784
+ message: error.message,
2785
+ url,
2786
+ requestBodyValues: body,
2787
+ statusCode: error.type === "overloaded_error" ? 529 : 500,
2788
+ responseHeaders,
2789
+ responseBody: JSON.stringify(error),
2790
+ isRetryable: error.type === "overloaded_error"
2791
+ });
2819
2792
  }
2820
2793
  } finally {
2794
+ firstChunkReader.cancel().catch(() => {
2795
+ });
2821
2796
  firstChunkReader.releaseLock();
2822
2797
  }
2823
- return returnPromise.promise;
2798
+ return {
2799
+ stream: streamForConsumer,
2800
+ request: { body },
2801
+ response: { headers: responseHeaders }
2802
+ };
2824
2803
  }
2825
2804
  };
2826
2805
  function getModelCapabilities(modelId) {