@ai-sdk/anthropic 2.0.0-alpha.7 → 2.0.0-alpha.8
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.
- package/CHANGELOG.md +11 -0
- package/dist/index.js +83 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +83 -36
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +83 -36
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +83 -36
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/internal/index.mjs
CHANGED
|
@@ -450,13 +450,16 @@ function groupIntoBlocks(prompt) {
|
|
|
450
450
|
}
|
|
451
451
|
|
|
452
452
|
// src/map-anthropic-stop-reason.ts
|
|
453
|
-
function mapAnthropicStopReason(
|
|
453
|
+
function mapAnthropicStopReason({
|
|
454
|
+
finishReason,
|
|
455
|
+
isJsonResponseFromTool
|
|
456
|
+
}) {
|
|
454
457
|
switch (finishReason) {
|
|
455
458
|
case "end_turn":
|
|
456
459
|
case "stop_sequence":
|
|
457
460
|
return "stop";
|
|
458
461
|
case "tool_use":
|
|
459
|
-
return "tool-calls";
|
|
462
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
460
463
|
case "max_tokens":
|
|
461
464
|
return "length";
|
|
462
465
|
default:
|
|
@@ -517,13 +520,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
517
520
|
setting: "seed"
|
|
518
521
|
});
|
|
519
522
|
}
|
|
520
|
-
if (responseFormat
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
523
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
524
|
+
if (responseFormat.schema == null) {
|
|
525
|
+
warnings.push({
|
|
526
|
+
type: "unsupported-setting",
|
|
527
|
+
setting: "responseFormat",
|
|
528
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
529
|
+
});
|
|
530
|
+
} else if (tools != null) {
|
|
531
|
+
warnings.push({
|
|
532
|
+
type: "unsupported-setting",
|
|
533
|
+
setting: "tools",
|
|
534
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
535
|
+
});
|
|
536
|
+
}
|
|
526
537
|
}
|
|
538
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
539
|
+
type: "function",
|
|
540
|
+
name: "json",
|
|
541
|
+
description: "Respond with a JSON object.",
|
|
542
|
+
parameters: responseFormat.schema
|
|
543
|
+
} : void 0;
|
|
527
544
|
const anthropicOptions = await parseProviderOptions2({
|
|
528
545
|
provider: "anthropic",
|
|
529
546
|
providerOptions,
|
|
@@ -590,7 +607,12 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
590
607
|
toolChoice: anthropicToolChoice,
|
|
591
608
|
toolWarnings,
|
|
592
609
|
betas: toolsBetas
|
|
593
|
-
} = prepareTools(
|
|
610
|
+
} = prepareTools(
|
|
611
|
+
jsonResponseTool != null ? {
|
|
612
|
+
tools: [jsonResponseTool],
|
|
613
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
614
|
+
} : { tools, toolChoice }
|
|
615
|
+
);
|
|
594
616
|
return {
|
|
595
617
|
args: {
|
|
596
618
|
...baseArgs,
|
|
@@ -598,7 +620,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
598
620
|
tool_choice: anthropicToolChoice
|
|
599
621
|
},
|
|
600
622
|
warnings: [...warnings, ...toolWarnings],
|
|
601
|
-
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
623
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
624
|
+
jsonResponseTool
|
|
602
625
|
};
|
|
603
626
|
}
|
|
604
627
|
async getHeaders({
|
|
@@ -621,7 +644,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
621
644
|
}
|
|
622
645
|
async doGenerate(options) {
|
|
623
646
|
var _a, _b, _c, _d;
|
|
624
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
647
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
625
648
|
const {
|
|
626
649
|
responseHeaders,
|
|
627
650
|
value: response,
|
|
@@ -641,7 +664,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
641
664
|
for (const part of response.content) {
|
|
642
665
|
switch (part.type) {
|
|
643
666
|
case "text": {
|
|
644
|
-
|
|
667
|
+
if (jsonResponseTool == null) {
|
|
668
|
+
content.push({ type: "text", text: part.text });
|
|
669
|
+
}
|
|
645
670
|
break;
|
|
646
671
|
}
|
|
647
672
|
case "thinking": {
|
|
@@ -669,20 +694,29 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
669
694
|
break;
|
|
670
695
|
}
|
|
671
696
|
case "tool_use": {
|
|
672
|
-
content.push(
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
697
|
+
content.push(
|
|
698
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
699
|
+
jsonResponseTool != null ? {
|
|
700
|
+
type: "text",
|
|
701
|
+
text: JSON.stringify(part.input)
|
|
702
|
+
} : {
|
|
703
|
+
type: "tool-call",
|
|
704
|
+
toolCallType: "function",
|
|
705
|
+
toolCallId: part.id,
|
|
706
|
+
toolName: part.name,
|
|
707
|
+
args: JSON.stringify(part.input)
|
|
708
|
+
}
|
|
709
|
+
);
|
|
679
710
|
break;
|
|
680
711
|
}
|
|
681
712
|
}
|
|
682
713
|
}
|
|
683
714
|
return {
|
|
684
715
|
content,
|
|
685
|
-
finishReason: mapAnthropicStopReason(
|
|
716
|
+
finishReason: mapAnthropicStopReason({
|
|
717
|
+
finishReason: response.stop_reason,
|
|
718
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
719
|
+
}),
|
|
686
720
|
usage: {
|
|
687
721
|
inputTokens: response.usage.input_tokens,
|
|
688
722
|
outputTokens: response.usage.output_tokens,
|
|
@@ -705,7 +739,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
705
739
|
};
|
|
706
740
|
}
|
|
707
741
|
async doStream(options) {
|
|
708
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
742
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
709
743
|
const body = { ...args, stream: true };
|
|
710
744
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
711
745
|
url: this.buildRequestUrl(true),
|
|
@@ -784,13 +818,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
784
818
|
case "content_block_stop": {
|
|
785
819
|
if (toolCallContentBlocks[value.index] != null) {
|
|
786
820
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
821
|
+
if (jsonResponseTool == null) {
|
|
822
|
+
controller.enqueue({
|
|
823
|
+
type: "tool-call",
|
|
824
|
+
toolCallType: "function",
|
|
825
|
+
toolCallId: contentBlock.toolCallId,
|
|
826
|
+
toolName: contentBlock.toolName,
|
|
827
|
+
args: contentBlock.jsonText
|
|
828
|
+
});
|
|
829
|
+
}
|
|
794
830
|
delete toolCallContentBlocks[value.index];
|
|
795
831
|
}
|
|
796
832
|
blockType = void 0;
|
|
@@ -800,6 +836,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
800
836
|
const deltaType = value.delta.type;
|
|
801
837
|
switch (deltaType) {
|
|
802
838
|
case "text_delta": {
|
|
839
|
+
if (jsonResponseTool != null) {
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
803
842
|
controller.enqueue({
|
|
804
843
|
type: "text",
|
|
805
844
|
text: value.delta.text
|
|
@@ -830,13 +869,18 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
830
869
|
}
|
|
831
870
|
case "input_json_delta": {
|
|
832
871
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
833
|
-
controller.enqueue(
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
872
|
+
controller.enqueue(
|
|
873
|
+
jsonResponseTool != null ? {
|
|
874
|
+
type: "text",
|
|
875
|
+
text: value.delta.partial_json
|
|
876
|
+
} : {
|
|
877
|
+
type: "tool-call-delta",
|
|
878
|
+
toolCallType: "function",
|
|
879
|
+
toolCallId: contentBlock.toolCallId,
|
|
880
|
+
toolName: contentBlock.toolName,
|
|
881
|
+
argsTextDelta: value.delta.partial_json
|
|
882
|
+
}
|
|
883
|
+
);
|
|
840
884
|
contentBlock.jsonText += value.delta.partial_json;
|
|
841
885
|
return;
|
|
842
886
|
}
|
|
@@ -866,7 +910,10 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
866
910
|
case "message_delta": {
|
|
867
911
|
usage.outputTokens = value.usage.output_tokens;
|
|
868
912
|
usage.totalTokens = ((_e = usage.inputTokens) != null ? _e : 0) + ((_f = value.usage.output_tokens) != null ? _f : 0);
|
|
869
|
-
finishReason = mapAnthropicStopReason(
|
|
913
|
+
finishReason = mapAnthropicStopReason({
|
|
914
|
+
finishReason: value.delta.stop_reason,
|
|
915
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
916
|
+
});
|
|
870
917
|
return;
|
|
871
918
|
}
|
|
872
919
|
case "message_stop": {
|