@ai-sdk/anthropic 2.0.0-alpha.10 → 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 +9 -0
- package/dist/index.js +166 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +166 -11
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +1 -0
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +166 -11
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +166 -11
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/internal/index.mjs
CHANGED
|
@@ -38,6 +38,29 @@ var webSearchLocationSchema = z2.object({
|
|
|
38
38
|
country: z2.string(),
|
|
39
39
|
timezone: z2.string().optional()
|
|
40
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
|
+
});
|
|
41
64
|
var anthropicProviderOptions = z2.object({
|
|
42
65
|
/**
|
|
43
66
|
Include reasoning content in requests sent to the model. Defaults to `true`.
|
|
@@ -219,7 +242,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
219
242
|
sendReasoning,
|
|
220
243
|
warnings
|
|
221
244
|
}) {
|
|
222
|
-
var _a, _b, _c;
|
|
245
|
+
var _a, _b, _c, _d;
|
|
223
246
|
const betas = /* @__PURE__ */ new Set();
|
|
224
247
|
const blocks = groupIntoBlocks(prompt);
|
|
225
248
|
let system = void 0;
|
|
@@ -230,6 +253,26 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
230
253
|
const cacheControlValue = (_a2 = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a2 : anthropic == null ? void 0 : anthropic.cache_control;
|
|
231
254
|
return cacheControlValue;
|
|
232
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
|
+
}
|
|
233
276
|
for (let i = 0; i < blocks.length; i++) {
|
|
234
277
|
const block = blocks[i];
|
|
235
278
|
const isLastBlock = i === blocks.length - 1;
|
|
@@ -283,6 +326,12 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
283
326
|
});
|
|
284
327
|
} else if (part.mediaType === "application/pdf") {
|
|
285
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
|
+
);
|
|
286
335
|
anthropicContent.push({
|
|
287
336
|
type: "document",
|
|
288
337
|
source: part.data instanceof URL ? {
|
|
@@ -293,6 +342,11 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
293
342
|
media_type: "application/pdf",
|
|
294
343
|
data: convertToBase64(part.data)
|
|
295
344
|
},
|
|
345
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
346
|
+
...metadata.context && { context: metadata.context },
|
|
347
|
+
...enableCitations && {
|
|
348
|
+
citations: { enabled: true }
|
|
349
|
+
},
|
|
296
350
|
cache_control: cacheControl
|
|
297
351
|
});
|
|
298
352
|
} else {
|
|
@@ -310,7 +364,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
310
364
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
311
365
|
const part = content[i2];
|
|
312
366
|
const isLastPart = i2 === content.length - 1;
|
|
313
|
-
const cacheControl = (
|
|
367
|
+
const cacheControl = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
314
368
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
315
369
|
var _a2;
|
|
316
370
|
switch (part2.type) {
|
|
@@ -360,7 +414,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
360
414
|
for (let k = 0; k < content.length; k++) {
|
|
361
415
|
const part = content[k];
|
|
362
416
|
const isLastContentPart = k === content.length - 1;
|
|
363
|
-
const cacheControl = (
|
|
417
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
364
418
|
switch (part.type) {
|
|
365
419
|
case "text": {
|
|
366
420
|
anthropicContent.push({
|
|
@@ -509,6 +563,40 @@ function mapAnthropicStopReason({
|
|
|
509
563
|
}
|
|
510
564
|
|
|
511
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
|
+
}
|
|
512
600
|
var AnthropicMessagesLanguageModel = class {
|
|
513
601
|
constructor(modelId, config) {
|
|
514
602
|
this.specificationVersion = "v2";
|
|
@@ -706,9 +794,29 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
706
794
|
var _a, _b, _c;
|
|
707
795
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
708
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
|
+
}
|
|
709
816
|
async doGenerate(options) {
|
|
710
817
|
var _a, _b, _c, _d, _e;
|
|
711
818
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
819
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
712
820
|
const {
|
|
713
821
|
responseHeaders,
|
|
714
822
|
value: response,
|
|
@@ -730,6 +838,16 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
730
838
|
case "text": {
|
|
731
839
|
if (jsonResponseTool == null) {
|
|
732
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
|
+
}
|
|
733
851
|
}
|
|
734
852
|
break;
|
|
735
853
|
}
|
|
@@ -836,6 +954,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
836
954
|
}
|
|
837
955
|
async doStream(options) {
|
|
838
956
|
const { args, warnings, betas, jsonResponseTool } = await this.getArgs(options);
|
|
957
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
839
958
|
const body = { ...args, stream: true };
|
|
840
959
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
841
960
|
url: this.buildRequestUrl(true),
|
|
@@ -1020,6 +1139,13 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1020
1139
|
return;
|
|
1021
1140
|
}
|
|
1022
1141
|
case "citations_delta": {
|
|
1142
|
+
const citation = value.delta.citation;
|
|
1143
|
+
processPageLocationCitation(
|
|
1144
|
+
citation,
|
|
1145
|
+
citationDocuments,
|
|
1146
|
+
generateId2,
|
|
1147
|
+
(source) => controller.enqueue(source)
|
|
1148
|
+
);
|
|
1023
1149
|
return;
|
|
1024
1150
|
}
|
|
1025
1151
|
default: {
|
|
@@ -1088,7 +1214,26 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
1088
1214
|
z3.discriminatedUnion("type", [
|
|
1089
1215
|
z3.object({
|
|
1090
1216
|
type: z3.literal("text"),
|
|
1091
|
-
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()
|
|
1092
1237
|
}),
|
|
1093
1238
|
z3.object({
|
|
1094
1239
|
type: z3.literal("thinking"),
|
|
@@ -1227,13 +1372,23 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1227
1372
|
}),
|
|
1228
1373
|
z3.object({
|
|
1229
1374
|
type: z3.literal("citations_delta"),
|
|
1230
|
-
citation: z3.
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
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
|
+
])
|
|
1237
1392
|
})
|
|
1238
1393
|
])
|
|
1239
1394
|
}),
|