@ai-sdk/anthropic 2.0.0-alpha.1 → 2.0.0-alpha.11
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 +78 -0
- package/dist/index.d.mts +90 -1
- package/dist/index.d.ts +90 -1
- package/dist/index.js +466 -61
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +469 -61
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +4 -1
- package/dist/internal/index.d.ts +4 -1
- package/dist/internal/index.js +453 -52
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +455 -52
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/internal/index.js
CHANGED
|
@@ -47,6 +47,36 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
|
|
|
47
47
|
|
|
48
48
|
// src/anthropic-messages-options.ts
|
|
49
49
|
var import_zod2 = require("zod");
|
|
50
|
+
var webSearchLocationSchema = import_zod2.z.object({
|
|
51
|
+
type: import_zod2.z.literal("approximate"),
|
|
52
|
+
city: import_zod2.z.string().optional(),
|
|
53
|
+
region: import_zod2.z.string().optional(),
|
|
54
|
+
country: import_zod2.z.string(),
|
|
55
|
+
timezone: import_zod2.z.string().optional()
|
|
56
|
+
});
|
|
57
|
+
var anthropicFilePartProviderOptions = import_zod2.z.object({
|
|
58
|
+
/**
|
|
59
|
+
* Citation configuration for this document.
|
|
60
|
+
* When enabled, this document will generate citations in the response.
|
|
61
|
+
*/
|
|
62
|
+
citations: import_zod2.z.object({
|
|
63
|
+
/**
|
|
64
|
+
* Enable citations for this document
|
|
65
|
+
*/
|
|
66
|
+
enabled: import_zod2.z.boolean()
|
|
67
|
+
}).optional(),
|
|
68
|
+
/**
|
|
69
|
+
* Custom title for the document.
|
|
70
|
+
* If not provided, the filename will be used.
|
|
71
|
+
*/
|
|
72
|
+
title: import_zod2.z.string().optional(),
|
|
73
|
+
/**
|
|
74
|
+
* Context about the document that will be passed to the model
|
|
75
|
+
* but not used towards cited content.
|
|
76
|
+
* Useful for storing document metadata as text or stringified JSON.
|
|
77
|
+
*/
|
|
78
|
+
context: import_zod2.z.string().optional()
|
|
79
|
+
});
|
|
50
80
|
var anthropicProviderOptions = import_zod2.z.object({
|
|
51
81
|
/**
|
|
52
82
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -57,11 +87,39 @@ var anthropicProviderOptions = import_zod2.z.object({
|
|
|
57
87
|
thinking: import_zod2.z.object({
|
|
58
88
|
type: import_zod2.z.union([import_zod2.z.literal("enabled"), import_zod2.z.literal("disabled")]),
|
|
59
89
|
budgetTokens: import_zod2.z.number().optional()
|
|
90
|
+
}).optional(),
|
|
91
|
+
/**
|
|
92
|
+
* Web search tool configuration for Claude models that support it.
|
|
93
|
+
* When provided, automatically adds the web search tool to the request.
|
|
94
|
+
*/
|
|
95
|
+
webSearch: import_zod2.z.object({
|
|
96
|
+
/**
|
|
97
|
+
* Limit the number of searches per request (optional)
|
|
98
|
+
* Defaults to 5 if not specified
|
|
99
|
+
*/
|
|
100
|
+
maxUses: import_zod2.z.number().min(1).max(20).optional(),
|
|
101
|
+
/**
|
|
102
|
+
* Only include results from these domains (optional)
|
|
103
|
+
* Cannot be used with blockedDomains
|
|
104
|
+
*/
|
|
105
|
+
allowedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
106
|
+
/**
|
|
107
|
+
* Never include results from these domains (optional)
|
|
108
|
+
* Cannot be used with allowedDomains
|
|
109
|
+
*/
|
|
110
|
+
blockedDomains: import_zod2.z.array(import_zod2.z.string()).optional(),
|
|
111
|
+
/**
|
|
112
|
+
* Localize search results based on user location (optional)
|
|
113
|
+
*/
|
|
114
|
+
userLocation: webSearchLocationSchema.optional()
|
|
60
115
|
}).optional()
|
|
61
116
|
});
|
|
62
117
|
|
|
63
118
|
// src/anthropic-prepare-tools.ts
|
|
64
119
|
var import_provider = require("@ai-sdk/provider");
|
|
120
|
+
function isWebSearchTool(tool) {
|
|
121
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
122
|
+
}
|
|
65
123
|
function prepareTools({
|
|
66
124
|
tools,
|
|
67
125
|
toolChoice
|
|
@@ -74,6 +132,10 @@ function prepareTools({
|
|
|
74
132
|
}
|
|
75
133
|
const anthropicTools2 = [];
|
|
76
134
|
for (const tool of tools) {
|
|
135
|
+
if (isWebSearchTool(tool)) {
|
|
136
|
+
anthropicTools2.push(tool);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
77
139
|
switch (tool.type) {
|
|
78
140
|
case "function":
|
|
79
141
|
anthropicTools2.push({
|
|
@@ -192,7 +254,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
192
254
|
sendReasoning,
|
|
193
255
|
warnings
|
|
194
256
|
}) {
|
|
195
|
-
var _a, _b, _c;
|
|
257
|
+
var _a, _b, _c, _d;
|
|
196
258
|
const betas = /* @__PURE__ */ new Set();
|
|
197
259
|
const blocks = groupIntoBlocks(prompt);
|
|
198
260
|
let system = void 0;
|
|
@@ -203,6 +265,26 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
203
265
|
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
204
266
|
return cacheControlValue;
|
|
205
267
|
}
|
|
268
|
+
async function shouldEnableCitations(providerMetadata) {
|
|
269
|
+
var _a2, _b2;
|
|
270
|
+
const anthropicOptions = await (0, import_provider_utils2.parseProviderOptions)({
|
|
271
|
+
provider: "anthropic",
|
|
272
|
+
providerOptions: providerMetadata,
|
|
273
|
+
schema: anthropicFilePartProviderOptions
|
|
274
|
+
});
|
|
275
|
+
return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
|
|
276
|
+
}
|
|
277
|
+
async function getDocumentMetadata(providerMetadata) {
|
|
278
|
+
const anthropicOptions = await (0, import_provider_utils2.parseProviderOptions)({
|
|
279
|
+
provider: "anthropic",
|
|
280
|
+
providerOptions: providerMetadata,
|
|
281
|
+
schema: anthropicFilePartProviderOptions
|
|
282
|
+
});
|
|
283
|
+
return {
|
|
284
|
+
title: anthropicOptions == null ? void 0 : anthropicOptions.title,
|
|
285
|
+
context: anthropicOptions == null ? void 0 : anthropicOptions.context
|
|
286
|
+
};
|
|
287
|
+
}
|
|
206
288
|
for (let i = 0; i < blocks.length; i++) {
|
|
207
289
|
const block = blocks[i];
|
|
208
290
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -256,6 +338,12 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
256
338
|
});
|
|
257
339
|
} else if (part.mediaType === "application/pdf") {
|
|
258
340
|
betas.add("pdfs-2024-09-25");
|
|
341
|
+
const enableCitations = await shouldEnableCitations(
|
|
342
|
+
part.providerOptions
|
|
343
|
+
);
|
|
344
|
+
const metadata = await getDocumentMetadata(
|
|
345
|
+
part.providerOptions
|
|
346
|
+
);
|
|
259
347
|
anthropicContent.push({
|
|
260
348
|
type: "document",
|
|
261
349
|
source: part.data instanceof URL ? {
|
|
@@ -266,6 +354,11 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
266
354
|
media_type: "application/pdf",
|
|
267
355
|
data: (0, import_provider_utils2.convertToBase64)(part.data)
|
|
268
356
|
},
|
|
357
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
358
|
+
...metadata.context && { context: metadata.context },
|
|
359
|
+
...enableCitations && {
|
|
360
|
+
citations: { enabled: true }
|
|
361
|
+
},
|
|
269
362
|
cache_control: cacheControl
|
|
270
363
|
});
|
|
271
364
|
} else {
|
|
@@ -283,7 +376,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
283
376
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
284
377
|
const part = content[i2];
|
|
285
378
|
const isLastPart = i2 === content.length - 1;
|
|
286
|
-
const cacheControl = (
|
|
379
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
287
380
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
288
381
|
var _a2;
|
|
289
382
|
switch (part2.type) {
|
|
@@ -333,7 +426,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
333
426
|
for (let k = 0; k < content.length; k++) {
|
|
334
427
|
const part = content[k];
|
|
335
428
|
const isLastContentPart = k === content.length - 1;
|
|
336
|
-
const cacheControl = (
|
|
429
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
337
430
|
switch (part.type) {
|
|
338
431
|
case "text": {
|
|
339
432
|
anthropicContent.push({
|
|
@@ -464,13 +557,16 @@ function groupIntoBlocks(prompt) {
|
|
|
464
557
|
}
|
|
465
558
|
|
|
466
559
|
// src/map-anthropic-stop-reason.ts
|
|
467
|
-
function mapAnthropicStopReason(
|
|
560
|
+
function mapAnthropicStopReason({
|
|
561
|
+
finishReason,
|
|
562
|
+
isJsonResponseFromTool
|
|
563
|
+
}) {
|
|
468
564
|
switch (finishReason) {
|
|
469
565
|
case "end_turn":
|
|
470
566
|
case "stop_sequence":
|
|
471
567
|
return "stop";
|
|
472
568
|
case "tool_use":
|
|
473
|
-
return "tool-calls";
|
|
569
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
474
570
|
case "max_tokens":
|
|
475
571
|
return "length";
|
|
476
572
|
default:
|
|
@@ -479,11 +575,47 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
479
575
|
}
|
|
480
576
|
|
|
481
577
|
// src/anthropic-messages-language-model.ts
|
|
578
|
+
function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
|
|
579
|
+
if (citation.type === "page_location") {
|
|
580
|
+
const source = createCitationSource(
|
|
581
|
+
citation,
|
|
582
|
+
citationDocuments,
|
|
583
|
+
generateId2
|
|
584
|
+
);
|
|
585
|
+
if (source) {
|
|
586
|
+
onSource(source);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
function createCitationSource(citation, citationDocuments, generateId2) {
|
|
591
|
+
var _a;
|
|
592
|
+
const documentInfo = citationDocuments[citation.document_index];
|
|
593
|
+
if (!documentInfo) {
|
|
594
|
+
return null;
|
|
595
|
+
}
|
|
596
|
+
return {
|
|
597
|
+
type: "source",
|
|
598
|
+
sourceType: "document",
|
|
599
|
+
id: generateId2(),
|
|
600
|
+
mediaType: documentInfo.mediaType,
|
|
601
|
+
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
602
|
+
filename: documentInfo.filename,
|
|
603
|
+
providerMetadata: {
|
|
604
|
+
anthropic: {
|
|
605
|
+
citedText: citation.cited_text,
|
|
606
|
+
startPageNumber: citation.start_page_number,
|
|
607
|
+
endPageNumber: citation.end_page_number
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
};
|
|
611
|
+
}
|
|
482
612
|
var AnthropicMessagesLanguageModel = class {
|
|
483
613
|
constructor(modelId, config) {
|
|
484
614
|
this.specificationVersion = "v2";
|
|
615
|
+
var _a;
|
|
485
616
|
this.modelId = modelId;
|
|
486
617
|
this.config = config;
|
|
618
|
+
this.generateId = (_a = config.generateId) != null ? _a : import_provider_utils3.generateId;
|
|
487
619
|
}
|
|
488
620
|
supportsUrl(url) {
|
|
489
621
|
return url.protocol === "https:";
|
|
@@ -531,13 +663,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
531
663
|
setting: "seed"
|
|
532
664
|
});
|
|
533
665
|
}
|
|
534
|
-
if (responseFormat
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
666
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
667
|
+
if (responseFormat.schema == null) {
|
|
668
|
+
warnings.push({
|
|
669
|
+
type: "unsupported-setting",
|
|
670
|
+
setting: "responseFormat",
|
|
671
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
672
|
+
});
|
|
673
|
+
} else if (tools != null) {
|
|
674
|
+
warnings.push({
|
|
675
|
+
type: "unsupported-setting",
|
|
676
|
+
setting: "tools",
|
|
677
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
678
|
+
});
|
|
679
|
+
}
|
|
540
680
|
}
|
|
681
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
682
|
+
type: "function",
|
|
683
|
+
name: "json",
|
|
684
|
+
description: "Respond with a JSON object.",
|
|
685
|
+
parameters: responseFormat.schema
|
|
686
|
+
} : void 0;
|
|
541
687
|
const anthropicOptions = await (0, import_provider_utils3.parseProviderOptions)({
|
|
542
688
|
provider: "anthropic",
|
|
543
689
|
providerOptions,
|
|
@@ -599,12 +745,38 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
599
745
|
}
|
|
600
746
|
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
601
747
|
}
|
|
748
|
+
let modifiedTools = tools;
|
|
749
|
+
let modifiedToolChoice = toolChoice;
|
|
750
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
751
|
+
const webSearchTool = {
|
|
752
|
+
type: "web_search_20250305",
|
|
753
|
+
name: "web_search",
|
|
754
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
755
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
756
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
757
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
758
|
+
user_location: {
|
|
759
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
760
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
761
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
762
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
763
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
768
|
+
}
|
|
602
769
|
const {
|
|
603
770
|
tools: anthropicTools2,
|
|
604
771
|
toolChoice: anthropicToolChoice,
|
|
605
772
|
toolWarnings,
|
|
606
773
|
betas: toolsBetas
|
|
607
|
-
} = prepareTools(
|
|
774
|
+
} = prepareTools(
|
|
775
|
+
jsonResponseTool != null ? {
|
|
776
|
+
tools: [jsonResponseTool],
|
|
777
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
778
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
779
|
+
);
|
|
608
780
|
return {
|
|
609
781
|
args: {
|
|
610
782
|
...baseArgs,
|
|
@@ -612,7 +784,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
612
784
|
tool_choice: anthropicToolChoice
|
|
613
785
|
},
|
|
614
786
|
warnings: [...warnings, ...toolWarnings],
|
|
615
|
-
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
787
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
788
|
+
jsonResponseTool
|
|
616
789
|
};
|
|
617
790
|
}
|
|
618
791
|
async getHeaders({
|
|
@@ -633,9 +806,29 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
633
806
|
var _a, _b, _c;
|
|
634
807
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
635
808
|
}
|
|
809
|
+
extractCitationDocuments(prompt) {
|
|
810
|
+
const isCitationEnabled = (part) => {
|
|
811
|
+
var _a, _b;
|
|
812
|
+
const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
813
|
+
const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
|
|
814
|
+
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
815
|
+
};
|
|
816
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
|
|
817
|
+
(part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
|
|
818
|
+
).map((part) => {
|
|
819
|
+
var _a;
|
|
820
|
+
const filePart = part;
|
|
821
|
+
return {
|
|
822
|
+
title: (_a = filePart.filename) != null ? _a : "Untitled Document",
|
|
823
|
+
filename: filePart.filename,
|
|
824
|
+
mediaType: filePart.mediaType
|
|
825
|
+
};
|
|
826
|
+
});
|
|
827
|
+
}
|
|
636
828
|
async doGenerate(options) {
|
|
637
|
-
var _a, _b, _c, _d;
|
|
638
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
829
|
+
var _a, _b, _c, _d, _e;
|
|
830
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
831
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
639
832
|
const {
|
|
640
833
|
responseHeaders,
|
|
641
834
|
value: response,
|
|
@@ -655,7 +848,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
655
848
|
for (const part of response.content) {
|
|
656
849
|
switch (part.type) {
|
|
657
850
|
case "text": {
|
|
658
|
-
|
|
851
|
+
if (jsonResponseTool == null) {
|
|
852
|
+
content.push({ type: "text", text: part.text });
|
|
853
|
+
if (part.citations) {
|
|
854
|
+
for (const citation of part.citations) {
|
|
855
|
+
processPageLocationCitation(
|
|
856
|
+
citation,
|
|
857
|
+
citationDocuments,
|
|
858
|
+
this.generateId,
|
|
859
|
+
(source) => content.push(source)
|
|
860
|
+
);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
659
864
|
break;
|
|
660
865
|
}
|
|
661
866
|
case "thinking": {
|
|
@@ -683,43 +888,85 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
683
888
|
break;
|
|
684
889
|
}
|
|
685
890
|
case "tool_use": {
|
|
686
|
-
content.push(
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
891
|
+
content.push(
|
|
892
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
893
|
+
jsonResponseTool != null ? {
|
|
894
|
+
type: "text",
|
|
895
|
+
text: JSON.stringify(part.input)
|
|
896
|
+
} : {
|
|
897
|
+
type: "tool-call",
|
|
898
|
+
toolCallType: "function",
|
|
899
|
+
toolCallId: part.id,
|
|
900
|
+
toolName: part.name,
|
|
901
|
+
args: JSON.stringify(part.input)
|
|
902
|
+
}
|
|
903
|
+
);
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
case "server_tool_use": {
|
|
907
|
+
continue;
|
|
908
|
+
}
|
|
909
|
+
case "web_search_tool_result": {
|
|
910
|
+
if (Array.isArray(part.content)) {
|
|
911
|
+
for (const result of part.content) {
|
|
912
|
+
if (result.type === "web_search_result") {
|
|
913
|
+
content.push({
|
|
914
|
+
type: "source",
|
|
915
|
+
sourceType: "url",
|
|
916
|
+
id: this.generateId(),
|
|
917
|
+
url: result.url,
|
|
918
|
+
title: result.title,
|
|
919
|
+
providerMetadata: {
|
|
920
|
+
anthropic: {
|
|
921
|
+
encryptedContent: result.encrypted_content,
|
|
922
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
929
|
+
throw new import_provider3.APICallError({
|
|
930
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
931
|
+
url: "web_search_api",
|
|
932
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
933
|
+
data: { error_code: part.content.error_code }
|
|
934
|
+
});
|
|
935
|
+
}
|
|
693
936
|
break;
|
|
694
937
|
}
|
|
695
938
|
}
|
|
696
939
|
}
|
|
697
940
|
return {
|
|
698
941
|
content,
|
|
699
|
-
finishReason: mapAnthropicStopReason(
|
|
942
|
+
finishReason: mapAnthropicStopReason({
|
|
943
|
+
finishReason: response.stop_reason,
|
|
944
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
945
|
+
}),
|
|
700
946
|
usage: {
|
|
701
947
|
inputTokens: response.usage.input_tokens,
|
|
702
948
|
outputTokens: response.usage.output_tokens,
|
|
703
949
|
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
704
|
-
cachedInputTokens: (
|
|
950
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
705
951
|
},
|
|
706
952
|
request: { body: args },
|
|
707
953
|
response: {
|
|
708
|
-
id: (
|
|
709
|
-
modelId: (
|
|
954
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
955
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
710
956
|
headers: responseHeaders,
|
|
711
957
|
body: rawResponse
|
|
712
958
|
},
|
|
713
959
|
warnings,
|
|
714
960
|
providerMetadata: {
|
|
715
961
|
anthropic: {
|
|
716
|
-
cacheCreationInputTokens: (
|
|
962
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
717
963
|
}
|
|
718
964
|
}
|
|
719
965
|
};
|
|
720
966
|
}
|
|
721
967
|
async doStream(options) {
|
|
722
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
968
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
969
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
723
970
|
const body = { ...args, stream: true };
|
|
724
971
|
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
|
725
972
|
url: this.buildRequestUrl(true),
|
|
@@ -741,6 +988,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
741
988
|
const toolCallContentBlocks = {};
|
|
742
989
|
let providerMetadata = void 0;
|
|
743
990
|
let blockType = void 0;
|
|
991
|
+
const config = this.config;
|
|
992
|
+
const generateId2 = this.generateId;
|
|
744
993
|
return {
|
|
745
994
|
stream: response.pipeThrough(
|
|
746
995
|
new TransformStream({
|
|
@@ -748,7 +997,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
748
997
|
controller.enqueue({ type: "stream-start", warnings });
|
|
749
998
|
},
|
|
750
999
|
transform(chunk, controller) {
|
|
751
|
-
var _a, _b, _c, _d, _e, _f;
|
|
1000
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
752
1001
|
if (!chunk.success) {
|
|
753
1002
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
754
1003
|
return;
|
|
@@ -787,6 +1036,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
787
1036
|
};
|
|
788
1037
|
return;
|
|
789
1038
|
}
|
|
1039
|
+
case "server_tool_use": {
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
case "web_search_tool_result": {
|
|
1043
|
+
if (Array.isArray(value.content_block.content)) {
|
|
1044
|
+
for (const result of value.content_block.content) {
|
|
1045
|
+
if (result.type === "web_search_result") {
|
|
1046
|
+
controller.enqueue({
|
|
1047
|
+
type: "source",
|
|
1048
|
+
sourceType: "url",
|
|
1049
|
+
id: generateId2(),
|
|
1050
|
+
url: result.url,
|
|
1051
|
+
title: result.title,
|
|
1052
|
+
providerMetadata: {
|
|
1053
|
+
anthropic: {
|
|
1054
|
+
encryptedContent: result.encrypted_content,
|
|
1055
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
});
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
1062
|
+
controller.enqueue({
|
|
1063
|
+
type: "error",
|
|
1064
|
+
error: {
|
|
1065
|
+
type: "web-search-error",
|
|
1066
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
1067
|
+
code: value.content_block.content.error_code
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
790
1073
|
default: {
|
|
791
1074
|
const _exhaustiveCheck = contentBlockType;
|
|
792
1075
|
throw new Error(
|
|
@@ -798,13 +1081,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
798
1081
|
case "content_block_stop": {
|
|
799
1082
|
if (toolCallContentBlocks[value.index] != null) {
|
|
800
1083
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
1084
|
+
if (jsonResponseTool == null) {
|
|
1085
|
+
controller.enqueue({
|
|
1086
|
+
type: "tool-call",
|
|
1087
|
+
toolCallType: "function",
|
|
1088
|
+
toolCallId: contentBlock.toolCallId,
|
|
1089
|
+
toolName: contentBlock.toolName,
|
|
1090
|
+
args: contentBlock.jsonText
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
808
1093
|
delete toolCallContentBlocks[value.index];
|
|
809
1094
|
}
|
|
810
1095
|
blockType = void 0;
|
|
@@ -814,6 +1099,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
814
1099
|
const deltaType = value.delta.type;
|
|
815
1100
|
switch (deltaType) {
|
|
816
1101
|
case "text_delta": {
|
|
1102
|
+
if (jsonResponseTool != null) {
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
817
1105
|
controller.enqueue({
|
|
818
1106
|
type: "text",
|
|
819
1107
|
text: value.delta.text
|
|
@@ -844,16 +1132,34 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
844
1132
|
}
|
|
845
1133
|
case "input_json_delta": {
|
|
846
1134
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
1135
|
+
if (!contentBlock) {
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
controller.enqueue(
|
|
1139
|
+
jsonResponseTool != null ? {
|
|
1140
|
+
type: "text",
|
|
1141
|
+
text: value.delta.partial_json
|
|
1142
|
+
} : {
|
|
1143
|
+
type: "tool-call-delta",
|
|
1144
|
+
toolCallType: "function",
|
|
1145
|
+
toolCallId: contentBlock.toolCallId,
|
|
1146
|
+
toolName: contentBlock.toolName,
|
|
1147
|
+
argsTextDelta: value.delta.partial_json
|
|
1148
|
+
}
|
|
1149
|
+
);
|
|
854
1150
|
contentBlock.jsonText += value.delta.partial_json;
|
|
855
1151
|
return;
|
|
856
1152
|
}
|
|
1153
|
+
case "citations_delta": {
|
|
1154
|
+
const citation = value.delta.citation;
|
|
1155
|
+
processPageLocationCitation(
|
|
1156
|
+
citation,
|
|
1157
|
+
citationDocuments,
|
|
1158
|
+
generateId2,
|
|
1159
|
+
(source) => controller.enqueue(source)
|
|
1160
|
+
);
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
857
1163
|
default: {
|
|
858
1164
|
const _exhaustiveCheck = deltaType;
|
|
859
1165
|
throw new Error(
|
|
@@ -864,23 +1170,26 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
864
1170
|
}
|
|
865
1171
|
case "message_start": {
|
|
866
1172
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
867
|
-
usage.cachedInputTokens = (
|
|
1173
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
868
1174
|
providerMetadata = {
|
|
869
1175
|
anthropic: {
|
|
870
|
-
cacheCreationInputTokens: (
|
|
1176
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
871
1177
|
}
|
|
872
1178
|
};
|
|
873
1179
|
controller.enqueue({
|
|
874
1180
|
type: "response-metadata",
|
|
875
|
-
id: (
|
|
876
|
-
modelId: (
|
|
1181
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1182
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
877
1183
|
});
|
|
878
1184
|
return;
|
|
879
1185
|
}
|
|
880
1186
|
case "message_delta": {
|
|
881
1187
|
usage.outputTokens = value.usage.output_tokens;
|
|
882
|
-
usage.totalTokens = ((
|
|
883
|
-
finishReason = mapAnthropicStopReason(
|
|
1188
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1189
|
+
finishReason = mapAnthropicStopReason({
|
|
1190
|
+
finishReason: value.delta.stop_reason,
|
|
1191
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
1192
|
+
});
|
|
884
1193
|
return;
|
|
885
1194
|
}
|
|
886
1195
|
case "message_stop": {
|
|
@@ -917,7 +1226,26 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
917
1226
|
import_zod3.z.discriminatedUnion("type", [
|
|
918
1227
|
import_zod3.z.object({
|
|
919
1228
|
type: import_zod3.z.literal("text"),
|
|
920
|
-
text: import_zod3.z.string()
|
|
1229
|
+
text: import_zod3.z.string(),
|
|
1230
|
+
citations: import_zod3.z.array(
|
|
1231
|
+
import_zod3.z.discriminatedUnion("type", [
|
|
1232
|
+
import_zod3.z.object({
|
|
1233
|
+
type: import_zod3.z.literal("web_search_result_location"),
|
|
1234
|
+
cited_text: import_zod3.z.string(),
|
|
1235
|
+
url: import_zod3.z.string(),
|
|
1236
|
+
title: import_zod3.z.string(),
|
|
1237
|
+
encrypted_index: import_zod3.z.string()
|
|
1238
|
+
}),
|
|
1239
|
+
import_zod3.z.object({
|
|
1240
|
+
type: import_zod3.z.literal("page_location"),
|
|
1241
|
+
cited_text: import_zod3.z.string(),
|
|
1242
|
+
document_index: import_zod3.z.number(),
|
|
1243
|
+
document_title: import_zod3.z.string().nullable(),
|
|
1244
|
+
start_page_number: import_zod3.z.number(),
|
|
1245
|
+
end_page_number: import_zod3.z.number()
|
|
1246
|
+
})
|
|
1247
|
+
])
|
|
1248
|
+
).optional()
|
|
921
1249
|
}),
|
|
922
1250
|
import_zod3.z.object({
|
|
923
1251
|
type: import_zod3.z.literal("thinking"),
|
|
@@ -933,6 +1261,31 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
933
1261
|
id: import_zod3.z.string(),
|
|
934
1262
|
name: import_zod3.z.string(),
|
|
935
1263
|
input: import_zod3.z.unknown()
|
|
1264
|
+
}),
|
|
1265
|
+
import_zod3.z.object({
|
|
1266
|
+
type: import_zod3.z.literal("server_tool_use"),
|
|
1267
|
+
id: import_zod3.z.string(),
|
|
1268
|
+
name: import_zod3.z.string(),
|
|
1269
|
+
input: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
1270
|
+
}),
|
|
1271
|
+
import_zod3.z.object({
|
|
1272
|
+
type: import_zod3.z.literal("web_search_tool_result"),
|
|
1273
|
+
tool_use_id: import_zod3.z.string(),
|
|
1274
|
+
content: import_zod3.z.union([
|
|
1275
|
+
import_zod3.z.array(
|
|
1276
|
+
import_zod3.z.object({
|
|
1277
|
+
type: import_zod3.z.literal("web_search_result"),
|
|
1278
|
+
url: import_zod3.z.string(),
|
|
1279
|
+
title: import_zod3.z.string(),
|
|
1280
|
+
encrypted_content: import_zod3.z.string(),
|
|
1281
|
+
page_age: import_zod3.z.string().nullish()
|
|
1282
|
+
})
|
|
1283
|
+
),
|
|
1284
|
+
import_zod3.z.object({
|
|
1285
|
+
type: import_zod3.z.literal("web_search_tool_result_error"),
|
|
1286
|
+
error_code: import_zod3.z.string()
|
|
1287
|
+
})
|
|
1288
|
+
])
|
|
936
1289
|
})
|
|
937
1290
|
])
|
|
938
1291
|
),
|
|
@@ -941,7 +1294,10 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
941
1294
|
input_tokens: import_zod3.z.number(),
|
|
942
1295
|
output_tokens: import_zod3.z.number(),
|
|
943
1296
|
cache_creation_input_tokens: import_zod3.z.number().nullish(),
|
|
944
|
-
cache_read_input_tokens: import_zod3.z.number().nullish()
|
|
1297
|
+
cache_read_input_tokens: import_zod3.z.number().nullish(),
|
|
1298
|
+
server_tool_use: import_zod3.z.object({
|
|
1299
|
+
web_search_requests: import_zod3.z.number()
|
|
1300
|
+
}).nullish()
|
|
945
1301
|
})
|
|
946
1302
|
});
|
|
947
1303
|
var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
@@ -978,6 +1334,31 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
978
1334
|
import_zod3.z.object({
|
|
979
1335
|
type: import_zod3.z.literal("redacted_thinking"),
|
|
980
1336
|
data: import_zod3.z.string()
|
|
1337
|
+
}),
|
|
1338
|
+
import_zod3.z.object({
|
|
1339
|
+
type: import_zod3.z.literal("server_tool_use"),
|
|
1340
|
+
id: import_zod3.z.string(),
|
|
1341
|
+
name: import_zod3.z.string(),
|
|
1342
|
+
input: import_zod3.z.record(import_zod3.z.unknown()).nullish()
|
|
1343
|
+
}),
|
|
1344
|
+
import_zod3.z.object({
|
|
1345
|
+
type: import_zod3.z.literal("web_search_tool_result"),
|
|
1346
|
+
tool_use_id: import_zod3.z.string(),
|
|
1347
|
+
content: import_zod3.z.union([
|
|
1348
|
+
import_zod3.z.array(
|
|
1349
|
+
import_zod3.z.object({
|
|
1350
|
+
type: import_zod3.z.literal("web_search_result"),
|
|
1351
|
+
url: import_zod3.z.string(),
|
|
1352
|
+
title: import_zod3.z.string(),
|
|
1353
|
+
encrypted_content: import_zod3.z.string(),
|
|
1354
|
+
page_age: import_zod3.z.string().nullish()
|
|
1355
|
+
})
|
|
1356
|
+
),
|
|
1357
|
+
import_zod3.z.object({
|
|
1358
|
+
type: import_zod3.z.literal("web_search_tool_result_error"),
|
|
1359
|
+
error_code: import_zod3.z.string()
|
|
1360
|
+
})
|
|
1361
|
+
])
|
|
981
1362
|
})
|
|
982
1363
|
])
|
|
983
1364
|
}),
|
|
@@ -1000,6 +1381,26 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1000
1381
|
import_zod3.z.object({
|
|
1001
1382
|
type: import_zod3.z.literal("signature_delta"),
|
|
1002
1383
|
signature: import_zod3.z.string()
|
|
1384
|
+
}),
|
|
1385
|
+
import_zod3.z.object({
|
|
1386
|
+
type: import_zod3.z.literal("citations_delta"),
|
|
1387
|
+
citation: import_zod3.z.discriminatedUnion("type", [
|
|
1388
|
+
import_zod3.z.object({
|
|
1389
|
+
type: import_zod3.z.literal("web_search_result_location"),
|
|
1390
|
+
cited_text: import_zod3.z.string(),
|
|
1391
|
+
url: import_zod3.z.string(),
|
|
1392
|
+
title: import_zod3.z.string(),
|
|
1393
|
+
encrypted_index: import_zod3.z.string()
|
|
1394
|
+
}),
|
|
1395
|
+
import_zod3.z.object({
|
|
1396
|
+
type: import_zod3.z.literal("page_location"),
|
|
1397
|
+
cited_text: import_zod3.z.string(),
|
|
1398
|
+
document_index: import_zod3.z.number(),
|
|
1399
|
+
document_title: import_zod3.z.string().nullable(),
|
|
1400
|
+
start_page_number: import_zod3.z.number(),
|
|
1401
|
+
end_page_number: import_zod3.z.number()
|
|
1402
|
+
})
|
|
1403
|
+
])
|
|
1003
1404
|
})
|
|
1004
1405
|
])
|
|
1005
1406
|
}),
|