@ai-sdk/anthropic 2.0.0-alpha.12 → 2.0.0-alpha.13
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 +10 -0
- package/dist/index.js +101 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +101 -52
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +101 -52
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +101 -52
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -247,12 +247,28 @@ import {
|
|
|
247
247
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
248
248
|
} from "@ai-sdk/provider";
|
|
249
249
|
import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
250
|
+
function convertToString(data) {
|
|
251
|
+
if (typeof data === "string") {
|
|
252
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
253
|
+
}
|
|
254
|
+
if (data instanceof Uint8Array) {
|
|
255
|
+
return new TextDecoder().decode(data);
|
|
256
|
+
}
|
|
257
|
+
if (data instanceof URL) {
|
|
258
|
+
throw new UnsupportedFunctionalityError2({
|
|
259
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
throw new UnsupportedFunctionalityError2({
|
|
263
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
264
|
+
});
|
|
265
|
+
}
|
|
250
266
|
async function convertToAnthropicMessagesPrompt({
|
|
251
267
|
prompt,
|
|
252
268
|
sendReasoning,
|
|
253
269
|
warnings
|
|
254
270
|
}) {
|
|
255
|
-
var _a, _b, _c, _d;
|
|
271
|
+
var _a, _b, _c, _d, _e;
|
|
256
272
|
const betas = /* @__PURE__ */ new Set();
|
|
257
273
|
const blocks = groupIntoBlocks(prompt);
|
|
258
274
|
let system = void 0;
|
|
@@ -359,6 +375,30 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
359
375
|
},
|
|
360
376
|
cache_control: cacheControl
|
|
361
377
|
});
|
|
378
|
+
} else if (part.mediaType === "text/plain") {
|
|
379
|
+
const enableCitations = await shouldEnableCitations(
|
|
380
|
+
part.providerOptions
|
|
381
|
+
);
|
|
382
|
+
const metadata = await getDocumentMetadata(
|
|
383
|
+
part.providerOptions
|
|
384
|
+
);
|
|
385
|
+
anthropicContent.push({
|
|
386
|
+
type: "document",
|
|
387
|
+
source: part.data instanceof URL ? {
|
|
388
|
+
type: "url",
|
|
389
|
+
url: part.data.toString()
|
|
390
|
+
} : {
|
|
391
|
+
type: "text",
|
|
392
|
+
media_type: "text/plain",
|
|
393
|
+
data: convertToString(part.data)
|
|
394
|
+
},
|
|
395
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
396
|
+
...metadata.context && { context: metadata.context },
|
|
397
|
+
...enableCitations && {
|
|
398
|
+
citations: { enabled: true }
|
|
399
|
+
},
|
|
400
|
+
cache_control: cacheControl
|
|
401
|
+
});
|
|
362
402
|
} else {
|
|
363
403
|
throw new UnsupportedFunctionalityError2({
|
|
364
404
|
functionality: `media type: ${part.mediaType}`
|
|
@@ -374,7 +414,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
374
414
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
375
415
|
const part = content[i2];
|
|
376
416
|
const isLastPart = i2 === content.length - 1;
|
|
377
|
-
const cacheControl = (
|
|
417
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
378
418
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
379
419
|
var _a2;
|
|
380
420
|
switch (part2.type) {
|
|
@@ -424,7 +464,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
424
464
|
for (let k = 0; k < content.length; k++) {
|
|
425
465
|
const part = content[k];
|
|
426
466
|
const isLastContentPart = k === content.length - 1;
|
|
427
|
-
const cacheControl = (
|
|
467
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
428
468
|
switch (part.type) {
|
|
429
469
|
case "text": {
|
|
430
470
|
anthropicContent.push({
|
|
@@ -573,8 +613,42 @@ function mapAnthropicStopReason({
|
|
|
573
613
|
}
|
|
574
614
|
|
|
575
615
|
// src/anthropic-messages-language-model.ts
|
|
576
|
-
|
|
577
|
-
|
|
616
|
+
var citationSchemas = {
|
|
617
|
+
webSearchResult: z3.object({
|
|
618
|
+
type: z3.literal("web_search_result_location"),
|
|
619
|
+
cited_text: z3.string(),
|
|
620
|
+
url: z3.string(),
|
|
621
|
+
title: z3.string(),
|
|
622
|
+
encrypted_index: z3.string()
|
|
623
|
+
}),
|
|
624
|
+
pageLocation: z3.object({
|
|
625
|
+
type: z3.literal("page_location"),
|
|
626
|
+
cited_text: z3.string(),
|
|
627
|
+
document_index: z3.number(),
|
|
628
|
+
document_title: z3.string().nullable(),
|
|
629
|
+
start_page_number: z3.number(),
|
|
630
|
+
end_page_number: z3.number()
|
|
631
|
+
}),
|
|
632
|
+
charLocation: z3.object({
|
|
633
|
+
type: z3.literal("char_location"),
|
|
634
|
+
cited_text: z3.string(),
|
|
635
|
+
document_index: z3.number(),
|
|
636
|
+
document_title: z3.string().nullable(),
|
|
637
|
+
start_char_index: z3.number(),
|
|
638
|
+
end_char_index: z3.number()
|
|
639
|
+
})
|
|
640
|
+
};
|
|
641
|
+
var citationSchema = z3.discriminatedUnion("type", [
|
|
642
|
+
citationSchemas.webSearchResult,
|
|
643
|
+
citationSchemas.pageLocation,
|
|
644
|
+
citationSchemas.charLocation
|
|
645
|
+
]);
|
|
646
|
+
var documentCitationSchema = z3.discriminatedUnion("type", [
|
|
647
|
+
citationSchemas.pageLocation,
|
|
648
|
+
citationSchemas.charLocation
|
|
649
|
+
]);
|
|
650
|
+
function processCitation(citation, citationDocuments, generateId3, onSource) {
|
|
651
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
578
652
|
const source = createCitationSource(
|
|
579
653
|
citation,
|
|
580
654
|
citationDocuments,
|
|
@@ -591,6 +665,15 @@ function createCitationSource(citation, citationDocuments, generateId3) {
|
|
|
591
665
|
if (!documentInfo) {
|
|
592
666
|
return null;
|
|
593
667
|
}
|
|
668
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
669
|
+
citedText: citation.cited_text,
|
|
670
|
+
startPageNumber: citation.start_page_number,
|
|
671
|
+
endPageNumber: citation.end_page_number
|
|
672
|
+
} : {
|
|
673
|
+
citedText: citation.cited_text,
|
|
674
|
+
startCharIndex: citation.start_char_index,
|
|
675
|
+
endCharIndex: citation.end_char_index
|
|
676
|
+
};
|
|
594
677
|
return {
|
|
595
678
|
type: "source",
|
|
596
679
|
sourceType: "document",
|
|
@@ -599,11 +682,7 @@ function createCitationSource(citation, citationDocuments, generateId3) {
|
|
|
599
682
|
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
600
683
|
filename: documentInfo.filename,
|
|
601
684
|
providerMetadata: {
|
|
602
|
-
anthropic:
|
|
603
|
-
citedText: citation.cited_text,
|
|
604
|
-
startPageNumber: citation.start_page_number,
|
|
605
|
-
endPageNumber: citation.end_page_number
|
|
606
|
-
}
|
|
685
|
+
anthropic: providerMetadata
|
|
607
686
|
}
|
|
608
687
|
};
|
|
609
688
|
}
|
|
@@ -805,15 +884,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
805
884
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
806
885
|
}
|
|
807
886
|
extractCitationDocuments(prompt) {
|
|
808
|
-
const
|
|
887
|
+
const isCitationPart = (part) => {
|
|
809
888
|
var _a, _b;
|
|
889
|
+
if (part.type !== "file") {
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
810
895
|
const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
811
896
|
const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
|
|
812
897
|
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
813
898
|
};
|
|
814
|
-
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(
|
|
815
|
-
(part) => part.type === "file" && part.mediaType === "application/pdf" && isCitationEnabled(part)
|
|
816
|
-
).map((part) => {
|
|
899
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
817
900
|
var _a;
|
|
818
901
|
const filePart = part;
|
|
819
902
|
return {
|
|
@@ -850,7 +933,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
850
933
|
content.push({ type: "text", text: part.text });
|
|
851
934
|
if (part.citations) {
|
|
852
935
|
for (const citation of part.citations) {
|
|
853
|
-
|
|
936
|
+
processCitation(
|
|
854
937
|
citation,
|
|
855
938
|
citationDocuments,
|
|
856
939
|
this.generateId,
|
|
@@ -1153,7 +1236,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1153
1236
|
}
|
|
1154
1237
|
case "citations_delta": {
|
|
1155
1238
|
const citation = value.delta.citation;
|
|
1156
|
-
|
|
1239
|
+
processCitation(
|
|
1157
1240
|
citation,
|
|
1158
1241
|
citationDocuments,
|
|
1159
1242
|
generateId3,
|
|
@@ -1228,25 +1311,7 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
1228
1311
|
z3.object({
|
|
1229
1312
|
type: z3.literal("text"),
|
|
1230
1313
|
text: z3.string(),
|
|
1231
|
-
citations: z3.array(
|
|
1232
|
-
z3.discriminatedUnion("type", [
|
|
1233
|
-
z3.object({
|
|
1234
|
-
type: z3.literal("web_search_result_location"),
|
|
1235
|
-
cited_text: z3.string(),
|
|
1236
|
-
url: z3.string(),
|
|
1237
|
-
title: z3.string(),
|
|
1238
|
-
encrypted_index: z3.string()
|
|
1239
|
-
}),
|
|
1240
|
-
z3.object({
|
|
1241
|
-
type: z3.literal("page_location"),
|
|
1242
|
-
cited_text: z3.string(),
|
|
1243
|
-
document_index: z3.number(),
|
|
1244
|
-
document_title: z3.string().nullable(),
|
|
1245
|
-
start_page_number: z3.number(),
|
|
1246
|
-
end_page_number: z3.number()
|
|
1247
|
-
})
|
|
1248
|
-
])
|
|
1249
|
-
).optional()
|
|
1314
|
+
citations: z3.array(citationSchema).optional()
|
|
1250
1315
|
}),
|
|
1251
1316
|
z3.object({
|
|
1252
1317
|
type: z3.literal("thinking"),
|
|
@@ -1385,23 +1450,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1385
1450
|
}),
|
|
1386
1451
|
z3.object({
|
|
1387
1452
|
type: z3.literal("citations_delta"),
|
|
1388
|
-
citation:
|
|
1389
|
-
z3.object({
|
|
1390
|
-
type: z3.literal("web_search_result_location"),
|
|
1391
|
-
cited_text: z3.string(),
|
|
1392
|
-
url: z3.string(),
|
|
1393
|
-
title: z3.string(),
|
|
1394
|
-
encrypted_index: z3.string()
|
|
1395
|
-
}),
|
|
1396
|
-
z3.object({
|
|
1397
|
-
type: z3.literal("page_location"),
|
|
1398
|
-
cited_text: z3.string(),
|
|
1399
|
-
document_index: z3.number(),
|
|
1400
|
-
document_title: z3.string().nullable(),
|
|
1401
|
-
start_page_number: z3.number(),
|
|
1402
|
-
end_page_number: z3.number()
|
|
1403
|
-
})
|
|
1404
|
-
])
|
|
1453
|
+
citation: citationSchema
|
|
1405
1454
|
})
|
|
1406
1455
|
])
|
|
1407
1456
|
}),
|