@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.mjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
// src/anthropic-messages-language-model.ts
|
|
2
2
|
import {
|
|
3
|
+
APICallError,
|
|
3
4
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError3
|
|
4
5
|
} from "@ai-sdk/provider";
|
|
5
6
|
import {
|
|
6
7
|
combineHeaders,
|
|
7
8
|
createEventSourceResponseHandler,
|
|
8
9
|
createJsonResponseHandler,
|
|
10
|
+
generateId,
|
|
9
11
|
parseProviderOptions as parseProviderOptions2,
|
|
10
12
|
postJsonToApi,
|
|
11
13
|
resolve
|
|
@@ -29,6 +31,36 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
|
29
31
|
|
|
30
32
|
// src/anthropic-messages-options.ts
|
|
31
33
|
import { z as z2 } from "zod";
|
|
34
|
+
var webSearchLocationSchema = z2.object({
|
|
35
|
+
type: z2.literal("approximate"),
|
|
36
|
+
city: z2.string().optional(),
|
|
37
|
+
region: z2.string().optional(),
|
|
38
|
+
country: z2.string(),
|
|
39
|
+
timezone: z2.string().optional()
|
|
40
|
+
});
|
|
41
|
+
var anthropicFilePartProviderOptions = z2.object({
|
|
42
|
+
/**
|
|
43
|
+
* Citation configuration for this document.
|
|
44
|
+
* When enabled, this document will generate citations in the response.
|
|
45
|
+
*/
|
|
46
|
+
citations: z2.object({
|
|
47
|
+
/**
|
|
48
|
+
* Enable citations for this document
|
|
49
|
+
*/
|
|
50
|
+
enabled: z2.boolean()
|
|
51
|
+
}).optional(),
|
|
52
|
+
/**
|
|
53
|
+
* Custom title for the document.
|
|
54
|
+
* If not provided, the filename will be used.
|
|
55
|
+
*/
|
|
56
|
+
title: z2.string().optional(),
|
|
57
|
+
/**
|
|
58
|
+
* Context about the document that will be passed to the model
|
|
59
|
+
* but not used towards cited content.
|
|
60
|
+
* Useful for storing document metadata as text or stringified JSON.
|
|
61
|
+
*/
|
|
62
|
+
context: z2.string().optional()
|
|
63
|
+
});
|
|
32
64
|
var anthropicProviderOptions = z2.object({
|
|
33
65
|
/**
|
|
34
66
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -39,6 +71,31 @@ var anthropicProviderOptions = z2.object({
|
|
|
39
71
|
thinking: z2.object({
|
|
40
72
|
type: z2.union([z2.literal("enabled"), z2.literal("disabled")]),
|
|
41
73
|
budgetTokens: z2.number().optional()
|
|
74
|
+
}).optional(),
|
|
75
|
+
/**
|
|
76
|
+
* Web search tool configuration for Claude models that support it.
|
|
77
|
+
* When provided, automatically adds the web search tool to the request.
|
|
78
|
+
*/
|
|
79
|
+
webSearch: z2.object({
|
|
80
|
+
/**
|
|
81
|
+
* Limit the number of searches per request (optional)
|
|
82
|
+
* Defaults to 5 if not specified
|
|
83
|
+
*/
|
|
84
|
+
maxUses: z2.number().min(1).max(20).optional(),
|
|
85
|
+
/**
|
|
86
|
+
* Only include results from these domains (optional)
|
|
87
|
+
* Cannot be used with blockedDomains
|
|
88
|
+
*/
|
|
89
|
+
allowedDomains: z2.array(z2.string()).optional(),
|
|
90
|
+
/**
|
|
91
|
+
* Never include results from these domains (optional)
|
|
92
|
+
* Cannot be used with allowedDomains
|
|
93
|
+
*/
|
|
94
|
+
blockedDomains: z2.array(z2.string()).optional(),
|
|
95
|
+
/**
|
|
96
|
+
* Localize search results based on user location (optional)
|
|
97
|
+
*/
|
|
98
|
+
userLocation: webSearchLocationSchema.optional()
|
|
42
99
|
}).optional()
|
|
43
100
|
});
|
|
44
101
|
|
|
@@ -46,6 +103,9 @@ var anthropicProviderOptions = z2.object({
|
|
|
46
103
|
import {
|
|
47
104
|
UnsupportedFunctionalityError
|
|
48
105
|
} from "@ai-sdk/provider";
|
|
106
|
+
function isWebSearchTool(tool) {
|
|
107
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
108
|
+
}
|
|
49
109
|
function prepareTools({
|
|
50
110
|
tools,
|
|
51
111
|
toolChoice
|
|
@@ -58,6 +118,10 @@ function prepareTools({
|
|
|
58
118
|
}
|
|
59
119
|
const anthropicTools2 = [];
|
|
60
120
|
for (const tool of tools) {
|
|
121
|
+
if (isWebSearchTool(tool)) {
|
|
122
|
+
anthropicTools2.push(tool);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
61
125
|
switch (tool.type) {
|
|
62
126
|
case "function":
|
|
63
127
|
anthropicTools2.push({
|
|
@@ -178,7 +242,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
178
242
|
sendReasoning,
|
|
179
243
|
warnings
|
|
180
244
|
}) {
|
|
181
|
-
var _a, _b, _c;
|
|
245
|
+
var _a, _b, _c, _d;
|
|
182
246
|
const betas = /* @__PURE__ */ new Set();
|
|
183
247
|
const blocks = groupIntoBlocks(prompt);
|
|
184
248
|
let system = void 0;
|
|
@@ -189,6 +253,26 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
189
253
|
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
190
254
|
return cacheControlValue;
|
|
191
255
|
}
|
|
256
|
+
async function shouldEnableCitations(providerMetadata) {
|
|
257
|
+
var _a2, _b2;
|
|
258
|
+
const anthropicOptions = await parseProviderOptions({
|
|
259
|
+
provider: "anthropic",
|
|
260
|
+
providerOptions: providerMetadata,
|
|
261
|
+
schema: anthropicFilePartProviderOptions
|
|
262
|
+
});
|
|
263
|
+
return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
|
|
264
|
+
}
|
|
265
|
+
async function getDocumentMetadata(providerMetadata) {
|
|
266
|
+
const anthropicOptions = await parseProviderOptions({
|
|
267
|
+
provider: "anthropic",
|
|
268
|
+
providerOptions: providerMetadata,
|
|
269
|
+
schema: anthropicFilePartProviderOptions
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
title: anthropicOptions == null ? void 0 : anthropicOptions.title,
|
|
273
|
+
context: anthropicOptions == null ? void 0 : anthropicOptions.context
|
|
274
|
+
};
|
|
275
|
+
}
|
|
192
276
|
for (let i = 0; i < blocks.length; i++) {
|
|
193
277
|
const block = blocks[i];
|
|
194
278
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -242,6 +326,12 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
242
326
|
});
|
|
243
327
|
} else if (part.mediaType === "application/pdf") {
|
|
244
328
|
betas.add("pdfs-2024-09-25");
|
|
329
|
+
const enableCitations = await shouldEnableCitations(
|
|
330
|
+
part.providerOptions
|
|
331
|
+
);
|
|
332
|
+
const metadata = await getDocumentMetadata(
|
|
333
|
+
part.providerOptions
|
|
334
|
+
);
|
|
245
335
|
anthropicContent.push({
|
|
246
336
|
type: "document",
|
|
247
337
|
source: part.data instanceof URL ? {
|
|
@@ -252,6 +342,11 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
252
342
|
media_type: "application/pdf",
|
|
253
343
|
data: convertToBase64(part.data)
|
|
254
344
|
},
|
|
345
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
346
|
+
...metadata.context && { context: metadata.context },
|
|
347
|
+
...enableCitations && {
|
|
348
|
+
citations: { enabled: true }
|
|
349
|
+
},
|
|
255
350
|
cache_control: cacheControl
|
|
256
351
|
});
|
|
257
352
|
} else {
|
|
@@ -269,7 +364,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
269
364
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
270
365
|
const part = content[i2];
|
|
271
366
|
const isLastPart = i2 === content.length - 1;
|
|
272
|
-
const cacheControl = (
|
|
367
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
273
368
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
274
369
|
var _a2;
|
|
275
370
|
switch (part2.type) {
|
|
@@ -319,7 +414,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
319
414
|
for (let k = 0; k < content.length; k++) {
|
|
320
415
|
const part = content[k];
|
|
321
416
|
const isLastContentPart = k === content.length - 1;
|
|
322
|
-
const cacheControl = (
|
|
417
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
323
418
|
switch (part.type) {
|
|
324
419
|
case "text": {
|
|
325
420
|
anthropicContent.push({
|
|
@@ -450,13 +545,16 @@ function groupIntoBlocks(prompt) {
|
|
|
450
545
|
}
|
|
451
546
|
|
|
452
547
|
// src/map-anthropic-stop-reason.ts
|
|
453
|
-
function mapAnthropicStopReason(
|
|
548
|
+
function mapAnthropicStopReason({
|
|
549
|
+
finishReason,
|
|
550
|
+
isJsonResponseFromTool
|
|
551
|
+
}) {
|
|
454
552
|
switch (finishReason) {
|
|
455
553
|
case "end_turn":
|
|
456
554
|
case "stop_sequence":
|
|
457
555
|
return "stop";
|
|
458
556
|
case "tool_use":
|
|
459
|
-
return "tool-calls";
|
|
557
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
460
558
|
case "max_tokens":
|
|
461
559
|
return "length";
|
|
462
560
|
default:
|
|
@@ -465,11 +563,47 @@ function mapAnthropicStopReason(finishReason) {
|
|
|
465
563
|
}
|
|
466
564
|
|
|
467
565
|
// src/anthropic-messages-language-model.ts
|
|
566
|
+
function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
|
|
567
|
+
if (citation.type === "page_location") {
|
|
568
|
+
const source = createCitationSource(
|
|
569
|
+
citation,
|
|
570
|
+
citationDocuments,
|
|
571
|
+
generateId2
|
|
572
|
+
);
|
|
573
|
+
if (source) {
|
|
574
|
+
onSource(source);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function createCitationSource(citation, citationDocuments, generateId2) {
|
|
579
|
+
var _a;
|
|
580
|
+
const documentInfo = citationDocuments[citation.document_index];
|
|
581
|
+
if (!documentInfo) {
|
|
582
|
+
return null;
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
type: "source",
|
|
586
|
+
sourceType: "document",
|
|
587
|
+
id: generateId2(),
|
|
588
|
+
mediaType: documentInfo.mediaType,
|
|
589
|
+
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
590
|
+
filename: documentInfo.filename,
|
|
591
|
+
providerMetadata: {
|
|
592
|
+
anthropic: {
|
|
593
|
+
citedText: citation.cited_text,
|
|
594
|
+
startPageNumber: citation.start_page_number,
|
|
595
|
+
endPageNumber: citation.end_page_number
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
}
|
|
468
600
|
var AnthropicMessagesLanguageModel = class {
|
|
469
601
|
constructor(modelId, config) {
|
|
470
602
|
this.specificationVersion = "v2";
|
|
603
|
+
var _a;
|
|
471
604
|
this.modelId = modelId;
|
|
472
605
|
this.config = config;
|
|
606
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
473
607
|
}
|
|
474
608
|
supportsUrl(url) {
|
|
475
609
|
return url.protocol === "https:";
|
|
@@ -517,13 +651,27 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
517
651
|
setting: "seed"
|
|
518
652
|
});
|
|
519
653
|
}
|
|
520
|
-
if (responseFormat
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
654
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
655
|
+
if (responseFormat.schema == null) {
|
|
656
|
+
warnings.push({
|
|
657
|
+
type: "unsupported-setting",
|
|
658
|
+
setting: "responseFormat",
|
|
659
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
660
|
+
});
|
|
661
|
+
} else if (tools != null) {
|
|
662
|
+
warnings.push({
|
|
663
|
+
type: "unsupported-setting",
|
|
664
|
+
setting: "tools",
|
|
665
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
666
|
+
});
|
|
667
|
+
}
|
|
526
668
|
}
|
|
669
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
670
|
+
type: "function",
|
|
671
|
+
name: "json",
|
|
672
|
+
description: "Respond with a JSON object.",
|
|
673
|
+
parameters: responseFormat.schema
|
|
674
|
+
} : void 0;
|
|
527
675
|
const anthropicOptions = await parseProviderOptions2({
|
|
528
676
|
provider: "anthropic",
|
|
529
677
|
providerOptions,
|
|
@@ -585,12 +733,38 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
585
733
|
}
|
|
586
734
|
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
587
735
|
}
|
|
736
|
+
let modifiedTools = tools;
|
|
737
|
+
let modifiedToolChoice = toolChoice;
|
|
738
|
+
if (anthropicOptions == null ? void 0 : anthropicOptions.webSearch) {
|
|
739
|
+
const webSearchTool = {
|
|
740
|
+
type: "web_search_20250305",
|
|
741
|
+
name: "web_search",
|
|
742
|
+
max_uses: anthropicOptions.webSearch.maxUses,
|
|
743
|
+
allowed_domains: anthropicOptions.webSearch.allowedDomains,
|
|
744
|
+
blocked_domains: anthropicOptions.webSearch.blockedDomains,
|
|
745
|
+
...anthropicOptions.webSearch.userLocation && {
|
|
746
|
+
user_location: {
|
|
747
|
+
type: anthropicOptions.webSearch.userLocation.type,
|
|
748
|
+
country: anthropicOptions.webSearch.userLocation.country,
|
|
749
|
+
city: anthropicOptions.webSearch.userLocation.city,
|
|
750
|
+
region: anthropicOptions.webSearch.userLocation.region,
|
|
751
|
+
timezone: anthropicOptions.webSearch.userLocation.timezone
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
modifiedTools = tools ? [...tools, webSearchTool] : [webSearchTool];
|
|
756
|
+
}
|
|
588
757
|
const {
|
|
589
758
|
tools: anthropicTools2,
|
|
590
759
|
toolChoice: anthropicToolChoice,
|
|
591
760
|
toolWarnings,
|
|
592
761
|
betas: toolsBetas
|
|
593
|
-
} = prepareTools(
|
|
762
|
+
} = prepareTools(
|
|
763
|
+
jsonResponseTool != null ? {
|
|
764
|
+
tools: [jsonResponseTool],
|
|
765
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name }
|
|
766
|
+
} : { tools: modifiedTools, toolChoice: modifiedToolChoice }
|
|
767
|
+
);
|
|
594
768
|
return {
|
|
595
769
|
args: {
|
|
596
770
|
...baseArgs,
|
|
@@ -598,7 +772,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
598
772
|
tool_choice: anthropicToolChoice
|
|
599
773
|
},
|
|
600
774
|
warnings: [...warnings, ...toolWarnings],
|
|
601
|
-
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas])
|
|
775
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
776
|
+
jsonResponseTool
|
|
602
777
|
};
|
|
603
778
|
}
|
|
604
779
|
async getHeaders({
|
|
@@ -619,9 +794,29 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
619
794
|
var _a, _b, _c;
|
|
620
795
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
621
796
|
}
|
|
797
|
+
extractCitationDocuments(prompt) {
|
|
798
|
+
const isCitationEnabled = (part) => {
|
|
799
|
+
var _a, _b;
|
|
800
|
+
const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
801
|
+
const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
|
|
802
|
+
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
803
|
+
};
|
|
804
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
|
|
805
|
+
(part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
|
|
806
|
+
).map((part) => {
|
|
807
|
+
var _a;
|
|
808
|
+
const filePart = part;
|
|
809
|
+
return {
|
|
810
|
+
title: (_a = filePart.filename) != null ? _a : "Untitled Document",
|
|
811
|
+
filename: filePart.filename,
|
|
812
|
+
mediaType: filePart.mediaType
|
|
813
|
+
};
|
|
814
|
+
});
|
|
815
|
+
}
|
|
622
816
|
async doGenerate(options) {
|
|
623
|
-
var _a, _b, _c, _d;
|
|
624
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
817
|
+
var _a, _b, _c, _d, _e;
|
|
818
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
819
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
625
820
|
const {
|
|
626
821
|
responseHeaders,
|
|
627
822
|
value: response,
|
|
@@ -641,7 +836,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
641
836
|
for (const part of response.content) {
|
|
642
837
|
switch (part.type) {
|
|
643
838
|
case "text": {
|
|
644
|
-
|
|
839
|
+
if (jsonResponseTool == null) {
|
|
840
|
+
content.push({ type: "text", text: part.text });
|
|
841
|
+
if (part.citations) {
|
|
842
|
+
for (const citation of part.citations) {
|
|
843
|
+
processPageLocationCitation(
|
|
844
|
+
citation,
|
|
845
|
+
citationDocuments,
|
|
846
|
+
this.generateId,
|
|
847
|
+
(source) => content.push(source)
|
|
848
|
+
);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
}
|
|
645
852
|
break;
|
|
646
853
|
}
|
|
647
854
|
case "thinking": {
|
|
@@ -669,43 +876,85 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
669
876
|
break;
|
|
670
877
|
}
|
|
671
878
|
case "tool_use": {
|
|
672
|
-
content.push(
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
879
|
+
content.push(
|
|
880
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
881
|
+
jsonResponseTool != null ? {
|
|
882
|
+
type: "text",
|
|
883
|
+
text: JSON.stringify(part.input)
|
|
884
|
+
} : {
|
|
885
|
+
type: "tool-call",
|
|
886
|
+
toolCallType: "function",
|
|
887
|
+
toolCallId: part.id,
|
|
888
|
+
toolName: part.name,
|
|
889
|
+
args: JSON.stringify(part.input)
|
|
890
|
+
}
|
|
891
|
+
);
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
case "server_tool_use": {
|
|
895
|
+
continue;
|
|
896
|
+
}
|
|
897
|
+
case "web_search_tool_result": {
|
|
898
|
+
if (Array.isArray(part.content)) {
|
|
899
|
+
for (const result of part.content) {
|
|
900
|
+
if (result.type === "web_search_result") {
|
|
901
|
+
content.push({
|
|
902
|
+
type: "source",
|
|
903
|
+
sourceType: "url",
|
|
904
|
+
id: this.generateId(),
|
|
905
|
+
url: result.url,
|
|
906
|
+
title: result.title,
|
|
907
|
+
providerMetadata: {
|
|
908
|
+
anthropic: {
|
|
909
|
+
encryptedContent: result.encrypted_content,
|
|
910
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
} else if (part.content.type === "web_search_tool_result_error") {
|
|
917
|
+
throw new APICallError({
|
|
918
|
+
message: `Web search failed: ${part.content.error_code}`,
|
|
919
|
+
url: "web_search_api",
|
|
920
|
+
requestBodyValues: { tool_use_id: part.tool_use_id },
|
|
921
|
+
data: { error_code: part.content.error_code }
|
|
922
|
+
});
|
|
923
|
+
}
|
|
679
924
|
break;
|
|
680
925
|
}
|
|
681
926
|
}
|
|
682
927
|
}
|
|
683
928
|
return {
|
|
684
929
|
content,
|
|
685
|
-
finishReason: mapAnthropicStopReason(
|
|
930
|
+
finishReason: mapAnthropicStopReason({
|
|
931
|
+
finishReason: response.stop_reason,
|
|
932
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
933
|
+
}),
|
|
686
934
|
usage: {
|
|
687
935
|
inputTokens: response.usage.input_tokens,
|
|
688
936
|
outputTokens: response.usage.output_tokens,
|
|
689
937
|
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
690
|
-
cachedInputTokens: (
|
|
938
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
691
939
|
},
|
|
692
940
|
request: { body: args },
|
|
693
941
|
response: {
|
|
694
|
-
id: (
|
|
695
|
-
modelId: (
|
|
942
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
943
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
696
944
|
headers: responseHeaders,
|
|
697
945
|
body: rawResponse
|
|
698
946
|
},
|
|
699
947
|
warnings,
|
|
700
948
|
providerMetadata: {
|
|
701
949
|
anthropic: {
|
|
702
|
-
cacheCreationInputTokens: (
|
|
950
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
703
951
|
}
|
|
704
952
|
}
|
|
705
953
|
};
|
|
706
954
|
}
|
|
707
955
|
async doStream(options) {
|
|
708
|
-
const { args, warnings, betas } = await this.getArgs(options);
|
|
956
|
+
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
957
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
709
958
|
const body = { ...args, stream: true };
|
|
710
959
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
711
960
|
url: this.buildRequestUrl(true),
|
|
@@ -727,6 +976,8 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
727
976
|
const toolCallContentBlocks = {};
|
|
728
977
|
let providerMetadata = void 0;
|
|
729
978
|
let blockType = void 0;
|
|
979
|
+
const config = this.config;
|
|
980
|
+
const generateId2 = this.generateId;
|
|
730
981
|
return {
|
|
731
982
|
stream: response.pipeThrough(
|
|
732
983
|
new TransformStream({
|
|
@@ -734,7 +985,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
734
985
|
controller.enqueue({ type: "stream-start", warnings });
|
|
735
986
|
},
|
|
736
987
|
transform(chunk, controller) {
|
|
737
|
-
var _a, _b, _c, _d, _e, _f;
|
|
988
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
738
989
|
if (!chunk.success) {
|
|
739
990
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
740
991
|
return;
|
|
@@ -773,6 +1024,40 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
773
1024
|
};
|
|
774
1025
|
return;
|
|
775
1026
|
}
|
|
1027
|
+
case "server_tool_use": {
|
|
1028
|
+
return;
|
|
1029
|
+
}
|
|
1030
|
+
case "web_search_tool_result": {
|
|
1031
|
+
if (Array.isArray(value.content_block.content)) {
|
|
1032
|
+
for (const result of value.content_block.content) {
|
|
1033
|
+
if (result.type === "web_search_result") {
|
|
1034
|
+
controller.enqueue({
|
|
1035
|
+
type: "source",
|
|
1036
|
+
sourceType: "url",
|
|
1037
|
+
id: generateId2(),
|
|
1038
|
+
url: result.url,
|
|
1039
|
+
title: result.title,
|
|
1040
|
+
providerMetadata: {
|
|
1041
|
+
anthropic: {
|
|
1042
|
+
encryptedContent: result.encrypted_content,
|
|
1043
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
} else if (value.content_block.content.type === "web_search_tool_result_error") {
|
|
1050
|
+
controller.enqueue({
|
|
1051
|
+
type: "error",
|
|
1052
|
+
error: {
|
|
1053
|
+
type: "web-search-error",
|
|
1054
|
+
message: `Web search failed: ${value.content_block.content.error_code}`,
|
|
1055
|
+
code: value.content_block.content.error_code
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
return;
|
|
1060
|
+
}
|
|
776
1061
|
default: {
|
|
777
1062
|
const _exhaustiveCheck = contentBlockType;
|
|
778
1063
|
throw new Error(
|
|
@@ -784,13 +1069,15 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
784
1069
|
case "content_block_stop": {
|
|
785
1070
|
if (toolCallContentBlocks[value.index] != null) {
|
|
786
1071
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
1072
|
+
if (jsonResponseTool == null) {
|
|
1073
|
+
controller.enqueue({
|
|
1074
|
+
type: "tool-call",
|
|
1075
|
+
toolCallType: "function",
|
|
1076
|
+
toolCallId: contentBlock.toolCallId,
|
|
1077
|
+
toolName: contentBlock.toolName,
|
|
1078
|
+
args: contentBlock.jsonText
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
794
1081
|
delete toolCallContentBlocks[value.index];
|
|
795
1082
|
}
|
|
796
1083
|
blockType = void 0;
|
|
@@ -800,6 +1087,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
800
1087
|
const deltaType = value.delta.type;
|
|
801
1088
|
switch (deltaType) {
|
|
802
1089
|
case "text_delta": {
|
|
1090
|
+
if (jsonResponseTool != null) {
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
803
1093
|
controller.enqueue({
|
|
804
1094
|
type: "text",
|
|
805
1095
|
text: value.delta.text
|
|
@@ -830,16 +1120,34 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
830
1120
|
}
|
|
831
1121
|
case "input_json_delta": {
|
|
832
1122
|
const contentBlock = toolCallContentBlocks[value.index];
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
1123
|
+
if (!contentBlock) {
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
controller.enqueue(
|
|
1127
|
+
jsonResponseTool != null ? {
|
|
1128
|
+
type: "text",
|
|
1129
|
+
text: value.delta.partial_json
|
|
1130
|
+
} : {
|
|
1131
|
+
type: "tool-call-delta",
|
|
1132
|
+
toolCallType: "function",
|
|
1133
|
+
toolCallId: contentBlock.toolCallId,
|
|
1134
|
+
toolName: contentBlock.toolName,
|
|
1135
|
+
argsTextDelta: value.delta.partial_json
|
|
1136
|
+
}
|
|
1137
|
+
);
|
|
840
1138
|
contentBlock.jsonText += value.delta.partial_json;
|
|
841
1139
|
return;
|
|
842
1140
|
}
|
|
1141
|
+
case "citations_delta": {
|
|
1142
|
+
const citation = value.delta.citation;
|
|
1143
|
+
processPageLocationCitation(
|
|
1144
|
+
citation,
|
|
1145
|
+
citationDocuments,
|
|
1146
|
+
generateId2,
|
|
1147
|
+
(source) => controller.enqueue(source)
|
|
1148
|
+
);
|
|
1149
|
+
return;
|
|
1150
|
+
}
|
|
843
1151
|
default: {
|
|
844
1152
|
const _exhaustiveCheck = deltaType;
|
|
845
1153
|
throw new Error(
|
|
@@ -850,23 +1158,26 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
850
1158
|
}
|
|
851
1159
|
case "message_start": {
|
|
852
1160
|
usage.inputTokens = value.message.usage.input_tokens;
|
|
853
|
-
usage.cachedInputTokens = (
|
|
1161
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
854
1162
|
providerMetadata = {
|
|
855
1163
|
anthropic: {
|
|
856
|
-
cacheCreationInputTokens: (
|
|
1164
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
857
1165
|
}
|
|
858
1166
|
};
|
|
859
1167
|
controller.enqueue({
|
|
860
1168
|
type: "response-metadata",
|
|
861
|
-
id: (
|
|
862
|
-
modelId: (
|
|
1169
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1170
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
863
1171
|
});
|
|
864
1172
|
return;
|
|
865
1173
|
}
|
|
866
1174
|
case "message_delta": {
|
|
867
1175
|
usage.outputTokens = value.usage.output_tokens;
|
|
868
|
-
usage.totalTokens = ((
|
|
869
|
-
finishReason = mapAnthropicStopReason(
|
|
1176
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1177
|
+
finishReason = mapAnthropicStopReason({
|
|
1178
|
+
finishReason: value.delta.stop_reason,
|
|
1179
|
+
isJsonResponseFromTool: jsonResponseTool != null
|
|
1180
|
+
});
|
|
870
1181
|
return;
|
|
871
1182
|
}
|
|
872
1183
|
case "message_stop": {
|
|
@@ -903,7 +1214,26 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
903
1214
|
z3.discriminatedUnion("type", [
|
|
904
1215
|
z3.object({
|
|
905
1216
|
type: z3.literal("text"),
|
|
906
|
-
text: z3.string()
|
|
1217
|
+
text: z3.string(),
|
|
1218
|
+
citations: z3.array(
|
|
1219
|
+
z3.discriminatedUnion("type", [
|
|
1220
|
+
z3.object({
|
|
1221
|
+
type: z3.literal("web_search_result_location"),
|
|
1222
|
+
cited_text: z3.string(),
|
|
1223
|
+
url: z3.string(),
|
|
1224
|
+
title: z3.string(),
|
|
1225
|
+
encrypted_index: z3.string()
|
|
1226
|
+
}),
|
|
1227
|
+
z3.object({
|
|
1228
|
+
type: z3.literal("page_location"),
|
|
1229
|
+
cited_text: z3.string(),
|
|
1230
|
+
document_index: z3.number(),
|
|
1231
|
+
document_title: z3.string().nullable(),
|
|
1232
|
+
start_page_number: z3.number(),
|
|
1233
|
+
end_page_number: z3.number()
|
|
1234
|
+
})
|
|
1235
|
+
])
|
|
1236
|
+
).optional()
|
|
907
1237
|
}),
|
|
908
1238
|
z3.object({
|
|
909
1239
|
type: z3.literal("thinking"),
|
|
@@ -919,6 +1249,31 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
919
1249
|
id: z3.string(),
|
|
920
1250
|
name: z3.string(),
|
|
921
1251
|
input: z3.unknown()
|
|
1252
|
+
}),
|
|
1253
|
+
z3.object({
|
|
1254
|
+
type: z3.literal("server_tool_use"),
|
|
1255
|
+
id: z3.string(),
|
|
1256
|
+
name: z3.string(),
|
|
1257
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1258
|
+
}),
|
|
1259
|
+
z3.object({
|
|
1260
|
+
type: z3.literal("web_search_tool_result"),
|
|
1261
|
+
tool_use_id: z3.string(),
|
|
1262
|
+
content: z3.union([
|
|
1263
|
+
z3.array(
|
|
1264
|
+
z3.object({
|
|
1265
|
+
type: z3.literal("web_search_result"),
|
|
1266
|
+
url: z3.string(),
|
|
1267
|
+
title: z3.string(),
|
|
1268
|
+
encrypted_content: z3.string(),
|
|
1269
|
+
page_age: z3.string().nullish()
|
|
1270
|
+
})
|
|
1271
|
+
),
|
|
1272
|
+
z3.object({
|
|
1273
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1274
|
+
error_code: z3.string()
|
|
1275
|
+
})
|
|
1276
|
+
])
|
|
922
1277
|
})
|
|
923
1278
|
])
|
|
924
1279
|
),
|
|
@@ -927,7 +1282,10 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
927
1282
|
input_tokens: z3.number(),
|
|
928
1283
|
output_tokens: z3.number(),
|
|
929
1284
|
cache_creation_input_tokens: z3.number().nullish(),
|
|
930
|
-
cache_read_input_tokens: z3.number().nullish()
|
|
1285
|
+
cache_read_input_tokens: z3.number().nullish(),
|
|
1286
|
+
server_tool_use: z3.object({
|
|
1287
|
+
web_search_requests: z3.number()
|
|
1288
|
+
}).nullish()
|
|
931
1289
|
})
|
|
932
1290
|
});
|
|
933
1291
|
var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
@@ -964,6 +1322,31 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
964
1322
|
z3.object({
|
|
965
1323
|
type: z3.literal("redacted_thinking"),
|
|
966
1324
|
data: z3.string()
|
|
1325
|
+
}),
|
|
1326
|
+
z3.object({
|
|
1327
|
+
type: z3.literal("server_tool_use"),
|
|
1328
|
+
id: z3.string(),
|
|
1329
|
+
name: z3.string(),
|
|
1330
|
+
input: z3.record(z3.unknown()).nullish()
|
|
1331
|
+
}),
|
|
1332
|
+
z3.object({
|
|
1333
|
+
type: z3.literal("web_search_tool_result"),
|
|
1334
|
+
tool_use_id: z3.string(),
|
|
1335
|
+
content: z3.union([
|
|
1336
|
+
z3.array(
|
|
1337
|
+
z3.object({
|
|
1338
|
+
type: z3.literal("web_search_result"),
|
|
1339
|
+
url: z3.string(),
|
|
1340
|
+
title: z3.string(),
|
|
1341
|
+
encrypted_content: z3.string(),
|
|
1342
|
+
page_age: z3.string().nullish()
|
|
1343
|
+
})
|
|
1344
|
+
),
|
|
1345
|
+
z3.object({
|
|
1346
|
+
type: z3.literal("web_search_tool_result_error"),
|
|
1347
|
+
error_code: z3.string()
|
|
1348
|
+
})
|
|
1349
|
+
])
|
|
967
1350
|
})
|
|
968
1351
|
])
|
|
969
1352
|
}),
|
|
@@ -986,6 +1369,26 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
986
1369
|
z3.object({
|
|
987
1370
|
type: z3.literal("signature_delta"),
|
|
988
1371
|
signature: z3.string()
|
|
1372
|
+
}),
|
|
1373
|
+
z3.object({
|
|
1374
|
+
type: z3.literal("citations_delta"),
|
|
1375
|
+
citation: z3.discriminatedUnion("type", [
|
|
1376
|
+
z3.object({
|
|
1377
|
+
type: z3.literal("web_search_result_location"),
|
|
1378
|
+
cited_text: z3.string(),
|
|
1379
|
+
url: z3.string(),
|
|
1380
|
+
title: z3.string(),
|
|
1381
|
+
encrypted_index: z3.string()
|
|
1382
|
+
}),
|
|
1383
|
+
z3.object({
|
|
1384
|
+
type: z3.literal("page_location"),
|
|
1385
|
+
cited_text: z3.string(),
|
|
1386
|
+
document_index: z3.number(),
|
|
1387
|
+
document_title: z3.string().nullable(),
|
|
1388
|
+
start_page_number: z3.number(),
|
|
1389
|
+
end_page_number: z3.number()
|
|
1390
|
+
})
|
|
1391
|
+
])
|
|
989
1392
|
})
|
|
990
1393
|
])
|
|
991
1394
|
}),
|