@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/internal/index.js
CHANGED
|
@@ -249,12 +249,28 @@ function prepareTools({
|
|
|
249
249
|
// src/convert-to-anthropic-messages-prompt.ts
|
|
250
250
|
var import_provider2 = require("@ai-sdk/provider");
|
|
251
251
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
252
|
+
function convertToString(data) {
|
|
253
|
+
if (typeof data === "string") {
|
|
254
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
255
|
+
}
|
|
256
|
+
if (data instanceof Uint8Array) {
|
|
257
|
+
return new TextDecoder().decode(data);
|
|
258
|
+
}
|
|
259
|
+
if (data instanceof URL) {
|
|
260
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
261
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
|
265
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
266
|
+
});
|
|
267
|
+
}
|
|
252
268
|
async function convertToAnthropicMessagesPrompt({
|
|
253
269
|
prompt,
|
|
254
270
|
sendReasoning,
|
|
255
271
|
warnings
|
|
256
272
|
}) {
|
|
257
|
-
var _a, _b, _c, _d;
|
|
273
|
+
var _a, _b, _c, _d, _e;
|
|
258
274
|
const betas = /* @__PURE__ */ new Set();
|
|
259
275
|
const blocks = groupIntoBlocks(prompt);
|
|
260
276
|
let system = void 0;
|
|
@@ -361,6 +377,30 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
361
377
|
},
|
|
362
378
|
cache_control: cacheControl
|
|
363
379
|
});
|
|
380
|
+
} else if (part.mediaType === "text/plain") {
|
|
381
|
+
const enableCitations = await shouldEnableCitations(
|
|
382
|
+
part.providerOptions
|
|
383
|
+
);
|
|
384
|
+
const metadata = await getDocumentMetadata(
|
|
385
|
+
part.providerOptions
|
|
386
|
+
);
|
|
387
|
+
anthropicContent.push({
|
|
388
|
+
type: "document",
|
|
389
|
+
source: part.data instanceof URL ? {
|
|
390
|
+
type: "url",
|
|
391
|
+
url: part.data.toString()
|
|
392
|
+
} : {
|
|
393
|
+
type: "text",
|
|
394
|
+
media_type: "text/plain",
|
|
395
|
+
data: convertToString(part.data)
|
|
396
|
+
},
|
|
397
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
398
|
+
...metadata.context && { context: metadata.context },
|
|
399
|
+
...enableCitations && {
|
|
400
|
+
citations: { enabled: true }
|
|
401
|
+
},
|
|
402
|
+
cache_control: cacheControl
|
|
403
|
+
});
|
|
364
404
|
} else {
|
|
365
405
|
throw new import_provider2.UnsupportedFunctionalityError({
|
|
366
406
|
functionality: `media type: ${part.mediaType}`
|
|
@@ -376,7 +416,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
376
416
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
377
417
|
const part = content[i2];
|
|
378
418
|
const isLastPart = i2 === content.length - 1;
|
|
379
|
-
const cacheControl = (
|
|
419
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
380
420
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
381
421
|
var _a2;
|
|
382
422
|
switch (part2.type) {
|
|
@@ -426,7 +466,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
426
466
|
for (let k = 0; k < content.length; k++) {
|
|
427
467
|
const part = content[k];
|
|
428
468
|
const isLastContentPart = k === content.length - 1;
|
|
429
|
-
const cacheControl = (
|
|
469
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
430
470
|
switch (part.type) {
|
|
431
471
|
case "text": {
|
|
432
472
|
anthropicContent.push({
|
|
@@ -575,8 +615,42 @@ function mapAnthropicStopReason({
|
|
|
575
615
|
}
|
|
576
616
|
|
|
577
617
|
// src/anthropic-messages-language-model.ts
|
|
578
|
-
|
|
579
|
-
|
|
618
|
+
var citationSchemas = {
|
|
619
|
+
webSearchResult: import_zod3.z.object({
|
|
620
|
+
type: import_zod3.z.literal("web_search_result_location"),
|
|
621
|
+
cited_text: import_zod3.z.string(),
|
|
622
|
+
url: import_zod3.z.string(),
|
|
623
|
+
title: import_zod3.z.string(),
|
|
624
|
+
encrypted_index: import_zod3.z.string()
|
|
625
|
+
}),
|
|
626
|
+
pageLocation: import_zod3.z.object({
|
|
627
|
+
type: import_zod3.z.literal("page_location"),
|
|
628
|
+
cited_text: import_zod3.z.string(),
|
|
629
|
+
document_index: import_zod3.z.number(),
|
|
630
|
+
document_title: import_zod3.z.string().nullable(),
|
|
631
|
+
start_page_number: import_zod3.z.number(),
|
|
632
|
+
end_page_number: import_zod3.z.number()
|
|
633
|
+
}),
|
|
634
|
+
charLocation: import_zod3.z.object({
|
|
635
|
+
type: import_zod3.z.literal("char_location"),
|
|
636
|
+
cited_text: import_zod3.z.string(),
|
|
637
|
+
document_index: import_zod3.z.number(),
|
|
638
|
+
document_title: import_zod3.z.string().nullable(),
|
|
639
|
+
start_char_index: import_zod3.z.number(),
|
|
640
|
+
end_char_index: import_zod3.z.number()
|
|
641
|
+
})
|
|
642
|
+
};
|
|
643
|
+
var citationSchema = import_zod3.z.discriminatedUnion("type", [
|
|
644
|
+
citationSchemas.webSearchResult,
|
|
645
|
+
citationSchemas.pageLocation,
|
|
646
|
+
citationSchemas.charLocation
|
|
647
|
+
]);
|
|
648
|
+
var documentCitationSchema = import_zod3.z.discriminatedUnion("type", [
|
|
649
|
+
citationSchemas.pageLocation,
|
|
650
|
+
citationSchemas.charLocation
|
|
651
|
+
]);
|
|
652
|
+
function processCitation(citation, citationDocuments, generateId2, onSource) {
|
|
653
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
580
654
|
const source = createCitationSource(
|
|
581
655
|
citation,
|
|
582
656
|
citationDocuments,
|
|
@@ -593,6 +667,15 @@ function createCitationSource(citation, citationDocuments, generateId2) {
|
|
|
593
667
|
if (!documentInfo) {
|
|
594
668
|
return null;
|
|
595
669
|
}
|
|
670
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
671
|
+
citedText: citation.cited_text,
|
|
672
|
+
startPageNumber: citation.start_page_number,
|
|
673
|
+
endPageNumber: citation.end_page_number
|
|
674
|
+
} : {
|
|
675
|
+
citedText: citation.cited_text,
|
|
676
|
+
startCharIndex: citation.start_char_index,
|
|
677
|
+
endCharIndex: citation.end_char_index
|
|
678
|
+
};
|
|
596
679
|
return {
|
|
597
680
|
type: "source",
|
|
598
681
|
sourceType: "document",
|
|
@@ -601,11 +684,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
|
|
|
601
684
|
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
602
685
|
filename: documentInfo.filename,
|
|
603
686
|
providerMetadata: {
|
|
604
|
-
anthropic:
|
|
605
|
-
citedText: citation.cited_text,
|
|
606
|
-
startPageNumber: citation.start_page_number,
|
|
607
|
-
endPageNumber: citation.end_page_number
|
|
608
|
-
}
|
|
687
|
+
anthropic: providerMetadata
|
|
609
688
|
}
|
|
610
689
|
};
|
|
611
690
|
}
|
|
@@ -807,15 +886,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
807
886
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
808
887
|
}
|
|
809
888
|
extractCitationDocuments(prompt) {
|
|
810
|
-
const
|
|
889
|
+
const isCitationPart = (part) => {
|
|
811
890
|
var _a, _b;
|
|
891
|
+
if (part.type !== "file") {
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
812
897
|
const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
813
898
|
const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
|
|
814
899
|
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
815
900
|
};
|
|
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) => {
|
|
901
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
819
902
|
var _a;
|
|
820
903
|
const filePart = part;
|
|
821
904
|
return {
|
|
@@ -852,7 +935,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
852
935
|
content.push({ type: "text", text: part.text });
|
|
853
936
|
if (part.citations) {
|
|
854
937
|
for (const citation of part.citations) {
|
|
855
|
-
|
|
938
|
+
processCitation(
|
|
856
939
|
citation,
|
|
857
940
|
citationDocuments,
|
|
858
941
|
this.generateId,
|
|
@@ -1155,7 +1238,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1155
1238
|
}
|
|
1156
1239
|
case "citations_delta": {
|
|
1157
1240
|
const citation = value.delta.citation;
|
|
1158
|
-
|
|
1241
|
+
processCitation(
|
|
1159
1242
|
citation,
|
|
1160
1243
|
citationDocuments,
|
|
1161
1244
|
generateId2,
|
|
@@ -1230,25 +1313,7 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
1230
1313
|
import_zod3.z.object({
|
|
1231
1314
|
type: import_zod3.z.literal("text"),
|
|
1232
1315
|
text: import_zod3.z.string(),
|
|
1233
|
-
citations: import_zod3.z.array(
|
|
1234
|
-
import_zod3.z.discriminatedUnion("type", [
|
|
1235
|
-
import_zod3.z.object({
|
|
1236
|
-
type: import_zod3.z.literal("web_search_result_location"),
|
|
1237
|
-
cited_text: import_zod3.z.string(),
|
|
1238
|
-
url: import_zod3.z.string(),
|
|
1239
|
-
title: import_zod3.z.string(),
|
|
1240
|
-
encrypted_index: import_zod3.z.string()
|
|
1241
|
-
}),
|
|
1242
|
-
import_zod3.z.object({
|
|
1243
|
-
type: import_zod3.z.literal("page_location"),
|
|
1244
|
-
cited_text: import_zod3.z.string(),
|
|
1245
|
-
document_index: import_zod3.z.number(),
|
|
1246
|
-
document_title: import_zod3.z.string().nullable(),
|
|
1247
|
-
start_page_number: import_zod3.z.number(),
|
|
1248
|
-
end_page_number: import_zod3.z.number()
|
|
1249
|
-
})
|
|
1250
|
-
])
|
|
1251
|
-
).optional()
|
|
1316
|
+
citations: import_zod3.z.array(citationSchema).optional()
|
|
1252
1317
|
}),
|
|
1253
1318
|
import_zod3.z.object({
|
|
1254
1319
|
type: import_zod3.z.literal("thinking"),
|
|
@@ -1387,23 +1452,7 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1387
1452
|
}),
|
|
1388
1453
|
import_zod3.z.object({
|
|
1389
1454
|
type: import_zod3.z.literal("citations_delta"),
|
|
1390
|
-
citation:
|
|
1391
|
-
import_zod3.z.object({
|
|
1392
|
-
type: import_zod3.z.literal("web_search_result_location"),
|
|
1393
|
-
cited_text: import_zod3.z.string(),
|
|
1394
|
-
url: import_zod3.z.string(),
|
|
1395
|
-
title: import_zod3.z.string(),
|
|
1396
|
-
encrypted_index: import_zod3.z.string()
|
|
1397
|
-
}),
|
|
1398
|
-
import_zod3.z.object({
|
|
1399
|
-
type: import_zod3.z.literal("page_location"),
|
|
1400
|
-
cited_text: import_zod3.z.string(),
|
|
1401
|
-
document_index: import_zod3.z.number(),
|
|
1402
|
-
document_title: import_zod3.z.string().nullable(),
|
|
1403
|
-
start_page_number: import_zod3.z.number(),
|
|
1404
|
-
end_page_number: import_zod3.z.number()
|
|
1405
|
-
})
|
|
1406
|
-
])
|
|
1455
|
+
citation: citationSchema
|
|
1407
1456
|
})
|
|
1408
1457
|
])
|
|
1409
1458
|
}),
|