@ai-sdk/anthropic 3.0.0-beta.67 → 3.0.0-beta.69

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,13 +1,12 @@
1
1
  // src/anthropic-messages-language-model.ts
2
2
  import {
3
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3,
4
- APICallError
3
+ APICallError,
4
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
5
5
  } from "@ai-sdk/provider";
6
6
  import {
7
7
  combineHeaders,
8
8
  createEventSourceResponseHandler,
9
9
  createJsonResponseHandler,
10
- DelayedPromise,
11
10
  generateId,
12
11
  parseProviderOptions as parseProviderOptions2,
13
12
  postJsonToApi,
@@ -2491,6 +2490,7 @@ var AnthropicMessagesLanguageModel = class {
2491
2490
  };
2492
2491
  }
2493
2492
  async doStream(options) {
2493
+ var _a, _b;
2494
2494
  const {
2495
2495
  args: body,
2496
2496
  warnings,
@@ -2529,25 +2529,13 @@ var AnthropicMessagesLanguageModel = class {
2529
2529
  let isJsonResponseFromTool = false;
2530
2530
  let blockType = void 0;
2531
2531
  const generateId2 = this.generateId;
2532
- let isFirstChunk = true;
2533
- let stream = void 0;
2534
- const returnPromise = new DelayedPromise();
2535
2532
  const transformedStream = response.pipeThrough(
2536
2533
  new TransformStream({
2537
2534
  start(controller) {
2538
2535
  controller.enqueue({ type: "stream-start", warnings });
2539
2536
  },
2540
- async flush() {
2541
- if (returnPromise.isPending()) {
2542
- returnPromise.resolve({
2543
- stream,
2544
- request: { body },
2545
- response: { headers: responseHeaders }
2546
- });
2547
- }
2548
- },
2549
2537
  transform(chunk, controller) {
2550
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2538
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
2551
2539
  if (options.includeRawChunks) {
2552
2540
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2553
2541
  }
@@ -2555,29 +2543,6 @@ var AnthropicMessagesLanguageModel = class {
2555
2543
  controller.enqueue({ type: "error", error: chunk.error });
2556
2544
  return;
2557
2545
  }
2558
- if (isFirstChunk) {
2559
- if (chunk.value.type === "error") {
2560
- returnPromise.reject(
2561
- new APICallError({
2562
- message: chunk.value.error.message,
2563
- url,
2564
- requestBodyValues: body,
2565
- statusCode: chunk.value.error.type === "overloaded_error" ? 529 : 500,
2566
- responseHeaders,
2567
- responseBody: JSON.stringify(chunk.value.error),
2568
- isRetryable: chunk.value.error.type === "overloaded_error"
2569
- })
2570
- );
2571
- controller.terminate();
2572
- return;
2573
- }
2574
- isFirstChunk = false;
2575
- returnPromise.resolve({
2576
- stream,
2577
- request: { body },
2578
- response: { headers: responseHeaders }
2579
- });
2580
- }
2581
2546
  const value = chunk.value;
2582
2547
  switch (value.type) {
2583
2548
  case "ping": {
@@ -2717,11 +2682,11 @@ var AnthropicMessagesLanguageModel = class {
2717
2682
  toolCallId: part.tool_use_id,
2718
2683
  toolName: "web_search",
2719
2684
  result: part.content.map((result) => {
2720
- var _a2;
2685
+ var _a3;
2721
2686
  return {
2722
2687
  url: result.url,
2723
2688
  title: result.title,
2724
- pageAge: (_a2 = result.page_age) != null ? _a2 : null,
2689
+ pageAge: (_a3 = result.page_age) != null ? _a3 : null,
2725
2690
  encryptedContent: result.encrypted_content,
2726
2691
  type: result.type
2727
2692
  };
@@ -2736,7 +2701,7 @@ var AnthropicMessagesLanguageModel = class {
2736
2701
  title: result.title,
2737
2702
  providerMetadata: {
2738
2703
  anthropic: {
2739
- pageAge: (_a = result.page_age) != null ? _a : null
2704
+ pageAge: (_a2 = result.page_age) != null ? _a2 : null
2740
2705
  }
2741
2706
  }
2742
2707
  });
@@ -2862,7 +2827,7 @@ var AnthropicMessagesLanguageModel = class {
2862
2827
  type: "tool-call",
2863
2828
  toolCallId: contentBlock.toolCallId,
2864
2829
  toolName,
2865
- input: contentBlock.input,
2830
+ input: contentBlock.input === "" ? "{}" : contentBlock.input,
2866
2831
  providerExecuted: contentBlock.providerExecuted
2867
2832
  });
2868
2833
  }
@@ -2964,7 +2929,7 @@ var AnthropicMessagesLanguageModel = class {
2964
2929
  }
2965
2930
  case "message_start": {
2966
2931
  usage.inputTokens = value.message.usage.input_tokens;
2967
- usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
2932
+ usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
2968
2933
  rawUsage = {
2969
2934
  ...value.message.usage
2970
2935
  };
@@ -3028,22 +2993,35 @@ var AnthropicMessagesLanguageModel = class {
3028
2993
  })
3029
2994
  );
3030
2995
  const [streamForFirstChunk, streamForConsumer] = transformedStream.tee();
3031
- stream = streamForConsumer;
3032
2996
  const firstChunkReader = streamForFirstChunk.getReader();
3033
2997
  try {
3034
- const { done } = await firstChunkReader.read();
3035
- if (!done) {
3036
- firstChunkReader.cancel();
2998
+ await firstChunkReader.read();
2999
+ let result = await firstChunkReader.read();
3000
+ if (((_a = result.value) == null ? void 0 : _a.type) === "raw") {
3001
+ result = await firstChunkReader.read();
3037
3002
  }
3038
- } catch (error) {
3039
- try {
3040
- firstChunkReader.cancel();
3041
- } catch (e) {
3003
+ if (((_b = result.value) == null ? void 0 : _b.type) === "error") {
3004
+ const error = result.value.error;
3005
+ throw new APICallError({
3006
+ message: error.message,
3007
+ url,
3008
+ requestBodyValues: body,
3009
+ statusCode: error.type === "overloaded_error" ? 529 : 500,
3010
+ responseHeaders,
3011
+ responseBody: JSON.stringify(error),
3012
+ isRetryable: error.type === "overloaded_error"
3013
+ });
3042
3014
  }
3043
3015
  } finally {
3016
+ firstChunkReader.cancel().catch(() => {
3017
+ });
3044
3018
  firstChunkReader.releaseLock();
3045
3019
  }
3046
- return returnPromise.promise;
3020
+ return {
3021
+ stream: streamForConsumer,
3022
+ request: { body },
3023
+ response: { headers: responseHeaders }
3024
+ };
3047
3025
  }
3048
3026
  };
3049
3027
  function getModelCapabilities(modelId) {