@ai-sdk/anthropic 2.0.0-canary.5 → 2.0.0-canary.7

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.
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(internal_exports);
27
27
 
28
28
  // src/anthropic-messages-language-model.ts
29
29
  var import_provider3 = require("@ai-sdk/provider");
30
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
30
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
31
31
  var import_zod2 = require("zod");
32
32
 
33
33
  // src/anthropic-error.ts
@@ -171,6 +171,7 @@ function prepareTools({
171
171
 
172
172
  // src/convert-to-anthropic-messages-prompt.ts
173
173
  var import_provider2 = require("@ai-sdk/provider");
174
+ var import_provider_utils2 = require("@ai-sdk/provider-utils");
174
175
  function convertToAnthropicMessagesPrompt({
175
176
  prompt,
176
177
  sendReasoning,
@@ -234,7 +235,7 @@ function convertToAnthropicMessagesPrompt({
234
235
  } : {
235
236
  type: "base64",
236
237
  media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
237
- data: part.data
238
+ data: (0, import_provider_utils2.convertToBase64)(part.data)
238
239
  },
239
240
  cache_control: cacheControl
240
241
  });
@@ -248,7 +249,7 @@ function convertToAnthropicMessagesPrompt({
248
249
  } : {
249
250
  type: "base64",
250
251
  media_type: "application/pdf",
251
- data: part.data
252
+ data: (0, import_provider_utils2.convertToBase64)(part.data)
252
253
  },
253
254
  cache_control: cacheControl
254
255
  });
@@ -465,7 +466,7 @@ var AnthropicMessagesLanguageModel = class {
465
466
  }
466
467
  async getArgs({
467
468
  prompt,
468
- maxTokens = 4096,
469
+ maxOutputTokens = 4096,
469
470
  // 4096: max model output tokens TODO update default in v5
470
471
  temperature,
471
472
  topP,
@@ -511,7 +512,7 @@ var AnthropicMessagesLanguageModel = class {
511
512
  sendReasoning: (_a = this.settings.sendReasoning) != null ? _a : true,
512
513
  warnings
513
514
  });
514
- const anthropicOptions = (0, import_provider_utils2.parseProviderOptions)({
515
+ const anthropicOptions = (0, import_provider_utils3.parseProviderOptions)({
515
516
  provider: "anthropic",
516
517
  providerOptions,
517
518
  schema: anthropicProviderOptionsSchema
@@ -522,7 +523,7 @@ var AnthropicMessagesLanguageModel = class {
522
523
  // model id:
523
524
  model: this.modelId,
524
525
  // standardized settings:
525
- max_tokens: maxTokens,
526
+ max_tokens: maxOutputTokens,
526
527
  temperature,
527
528
  top_k: topK,
528
529
  top_p: topP,
@@ -565,7 +566,7 @@ var AnthropicMessagesLanguageModel = class {
565
566
  details: "topP is not supported when thinking is enabled"
566
567
  });
567
568
  }
568
- baseArgs.max_tokens = maxTokens + thinkingBudget;
569
+ baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
569
570
  }
570
571
  const {
571
572
  tools: anthropicTools2,
@@ -587,8 +588,8 @@ var AnthropicMessagesLanguageModel = class {
587
588
  betas,
588
589
  headers
589
590
  }) {
590
- return (0, import_provider_utils2.combineHeaders)(
591
- await (0, import_provider_utils2.resolve)(this.config.headers),
591
+ return (0, import_provider_utils3.combineHeaders)(
592
+ await (0, import_provider_utils3.resolve)(this.config.headers),
592
593
  betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
593
594
  headers
594
595
  );
@@ -608,12 +609,12 @@ var AnthropicMessagesLanguageModel = class {
608
609
  responseHeaders,
609
610
  value: response,
610
611
  rawValue: rawResponse
611
- } = await (0, import_provider_utils2.postJsonToApi)({
612
+ } = await (0, import_provider_utils3.postJsonToApi)({
612
613
  url: this.buildRequestUrl(false),
613
614
  headers: await this.getHeaders({ betas, headers: options.headers }),
614
615
  body: this.transformRequestBody(args),
615
616
  failedResponseHandler: anthropicFailedResponseHandler,
616
- successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
617
+ successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(
617
618
  anthropicMessagesResponseSchema
618
619
  ),
619
620
  abortSignal: options.abortSignal,
@@ -632,6 +633,7 @@ var AnthropicMessagesLanguageModel = class {
632
633
  for (const content of response.content) {
633
634
  if (content.type === "tool_use") {
634
635
  toolCalls.push({
636
+ type: "tool-call",
635
637
  toolCallType: "function",
636
638
  toolCallId: content.id,
637
639
  toolName: content.name,
@@ -640,26 +642,35 @@ var AnthropicMessagesLanguageModel = class {
640
642
  }
641
643
  }
642
644
  }
643
- const reasoning = response.content.filter(
644
- (content) => content.type === "redacted_thinking" || content.type === "thinking"
645
- ).map(
646
- (content) => content.type === "thinking" ? {
647
- type: "text",
648
- text: content.thinking,
649
- signature: content.signature
650
- } : {
651
- type: "redacted",
652
- data: content.data
645
+ const reasoning = [];
646
+ for (const content of response.content) {
647
+ if (content.type === "redacted_thinking") {
648
+ reasoning.push({
649
+ type: "reasoning",
650
+ reasoningType: "redacted",
651
+ data: content.data
652
+ });
653
+ } else if (content.type === "thinking") {
654
+ reasoning.push({
655
+ type: "reasoning",
656
+ reasoningType: "text",
657
+ text: content.thinking
658
+ });
659
+ reasoning.push({
660
+ type: "reasoning",
661
+ reasoningType: "signature",
662
+ signature: content.signature
663
+ });
653
664
  }
654
- );
665
+ }
655
666
  return {
656
- text,
667
+ text: text != null ? { type: "text", text } : void 0,
657
668
  reasoning: reasoning.length > 0 ? reasoning : void 0,
658
669
  toolCalls,
659
670
  finishReason: mapAnthropicStopReason(response.stop_reason),
660
671
  usage: {
661
- promptTokens: response.usage.input_tokens,
662
- completionTokens: response.usage.output_tokens
672
+ inputTokens: response.usage.input_tokens,
673
+ outputTokens: response.usage.output_tokens
663
674
  },
664
675
  request: { body: args },
665
676
  response: {
@@ -680,22 +691,21 @@ var AnthropicMessagesLanguageModel = class {
680
691
  async doStream(options) {
681
692
  const { args, warnings, betas } = await this.getArgs(options);
682
693
  const body = { ...args, stream: true };
683
- const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
694
+ const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
684
695
  url: this.buildRequestUrl(true),
685
696
  headers: await this.getHeaders({ betas, headers: options.headers }),
686
697
  body: this.transformRequestBody(body),
687
698
  failedResponseHandler: anthropicFailedResponseHandler,
688
- successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
699
+ successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(
689
700
  anthropicMessagesChunkSchema
690
701
  ),
691
702
  abortSignal: options.abortSignal,
692
703
  fetch: this.config.fetch
693
704
  });
694
- const { messages: rawPrompt, ...rawSettings } = args;
695
705
  let finishReason = "unknown";
696
706
  const usage = {
697
- promptTokens: Number.NaN,
698
- completionTokens: Number.NaN
707
+ inputTokens: void 0,
708
+ outputTokens: void 0
699
709
  };
700
710
  const toolCallContentBlocks = {};
701
711
  let providerMetadata = void 0;
@@ -724,7 +734,8 @@ var AnthropicMessagesLanguageModel = class {
724
734
  }
725
735
  case "redacted_thinking": {
726
736
  controller.enqueue({
727
- type: "redacted-reasoning",
737
+ type: "reasoning",
738
+ reasoningType: "redacted",
728
739
  data: value.content_block.data
729
740
  });
730
741
  return;
@@ -765,22 +776,24 @@ var AnthropicMessagesLanguageModel = class {
765
776
  switch (deltaType) {
766
777
  case "text_delta": {
767
778
  controller.enqueue({
768
- type: "text-delta",
769
- textDelta: value.delta.text
779
+ type: "text",
780
+ text: value.delta.text
770
781
  });
771
782
  return;
772
783
  }
773
784
  case "thinking_delta": {
774
785
  controller.enqueue({
775
786
  type: "reasoning",
776
- textDelta: value.delta.thinking
787
+ reasoningType: "text",
788
+ text: value.delta.thinking
777
789
  });
778
790
  return;
779
791
  }
780
792
  case "signature_delta": {
781
793
  if (blockType === "thinking") {
782
794
  controller.enqueue({
783
- type: "reasoning-signature",
795
+ type: "reasoning",
796
+ reasoningType: "signature",
784
797
  signature: value.delta.signature
785
798
  });
786
799
  }
@@ -807,8 +820,8 @@ var AnthropicMessagesLanguageModel = class {
807
820
  }
808
821
  }
809
822
  case "message_start": {
810
- usage.promptTokens = value.message.usage.input_tokens;
811
- usage.completionTokens = value.message.usage.output_tokens;
823
+ usage.inputTokens = value.message.usage.input_tokens;
824
+ usage.outputTokens = value.message.usage.output_tokens;
812
825
  providerMetadata = {
813
826
  anthropic: {
814
827
  cacheCreationInputTokens: (_a = value.message.usage.cache_creation_input_tokens) != null ? _a : null,
@@ -823,7 +836,7 @@ var AnthropicMessagesLanguageModel = class {
823
836
  return;
824
837
  }
825
838
  case "message_delta": {
826
- usage.completionTokens = value.usage.output_tokens;
839
+ usage.outputTokens = value.usage.output_tokens;
827
840
  finishReason = mapAnthropicStopReason(value.delta.stop_reason);
828
841
  return;
829
842
  }