@ai-sdk/anthropic 2.0.0-alpha.11 → 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 +19 -0
- package/dist/index.js +104 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +104 -52
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +104 -52
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +104 -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,
|
|
@@ -998,6 +1081,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
998
1081
|
},
|
|
999
1082
|
transform(chunk, controller) {
|
|
1000
1083
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
1084
|
+
if (options.includeRawChunks) {
|
|
1085
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1086
|
+
}
|
|
1001
1087
|
if (!chunk.success) {
|
|
1002
1088
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
1003
1089
|
return;
|
|
@@ -1152,7 +1238,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1152
1238
|
}
|
|
1153
1239
|
case "citations_delta": {
|
|
1154
1240
|
const citation = value.delta.citation;
|
|
1155
|
-
|
|
1241
|
+
processCitation(
|
|
1156
1242
|
citation,
|
|
1157
1243
|
citationDocuments,
|
|
1158
1244
|
generateId2,
|
|
@@ -1227,25 +1313,7 @@ var anthropicMessagesResponseSchema = import_zod3.z.object({
|
|
|
1227
1313
|
import_zod3.z.object({
|
|
1228
1314
|
type: import_zod3.z.literal("text"),
|
|
1229
1315
|
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()
|
|
1316
|
+
citations: import_zod3.z.array(citationSchema).optional()
|
|
1249
1317
|
}),
|
|
1250
1318
|
import_zod3.z.object({
|
|
1251
1319
|
type: import_zod3.z.literal("thinking"),
|
|
@@ -1384,23 +1452,7 @@ var anthropicMessagesChunkSchema = import_zod3.z.discriminatedUnion("type", [
|
|
|
1384
1452
|
}),
|
|
1385
1453
|
import_zod3.z.object({
|
|
1386
1454
|
type: import_zod3.z.literal("citations_delta"),
|
|
1387
|
-
citation:
|
|
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
|
-
])
|
|
1455
|
+
citation: citationSchema
|
|
1404
1456
|
})
|
|
1405
1457
|
])
|
|
1406
1458
|
}),
|