@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.mjs
CHANGED
|
@@ -237,12 +237,28 @@ import {
|
|
|
237
237
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
238
238
|
} from "@ai-sdk/provider";
|
|
239
239
|
import { convertToBase64, parseProviderOptions } from "@ai-sdk/provider-utils";
|
|
240
|
+
function convertToString(data) {
|
|
241
|
+
if (typeof data === "string") {
|
|
242
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
243
|
+
}
|
|
244
|
+
if (data instanceof Uint8Array) {
|
|
245
|
+
return new TextDecoder().decode(data);
|
|
246
|
+
}
|
|
247
|
+
if (data instanceof URL) {
|
|
248
|
+
throw new UnsupportedFunctionalityError2({
|
|
249
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
throw new UnsupportedFunctionalityError2({
|
|
253
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
254
|
+
});
|
|
255
|
+
}
|
|
240
256
|
async function convertToAnthropicMessagesPrompt({
|
|
241
257
|
prompt,
|
|
242
258
|
sendReasoning,
|
|
243
259
|
warnings
|
|
244
260
|
}) {
|
|
245
|
-
var _a, _b, _c, _d;
|
|
261
|
+
var _a, _b, _c, _d, _e;
|
|
246
262
|
const betas = /* @__PURE__ */ new Set();
|
|
247
263
|
const blocks = groupIntoBlocks(prompt);
|
|
248
264
|
let system = void 0;
|
|
@@ -349,6 +365,30 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
349
365
|
},
|
|
350
366
|
cache_control: cacheControl
|
|
351
367
|
});
|
|
368
|
+
} else if (part.mediaType === "text/plain") {
|
|
369
|
+
const enableCitations = await shouldEnableCitations(
|
|
370
|
+
part.providerOptions
|
|
371
|
+
);
|
|
372
|
+
const metadata = await getDocumentMetadata(
|
|
373
|
+
part.providerOptions
|
|
374
|
+
);
|
|
375
|
+
anthropicContent.push({
|
|
376
|
+
type: "document",
|
|
377
|
+
source: part.data instanceof URL ? {
|
|
378
|
+
type: "url",
|
|
379
|
+
url: part.data.toString()
|
|
380
|
+
} : {
|
|
381
|
+
type: "text",
|
|
382
|
+
media_type: "text/plain",
|
|
383
|
+
data: convertToString(part.data)
|
|
384
|
+
},
|
|
385
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
386
|
+
...metadata.context && { context: metadata.context },
|
|
387
|
+
...enableCitations && {
|
|
388
|
+
citations: { enabled: true }
|
|
389
|
+
},
|
|
390
|
+
cache_control: cacheControl
|
|
391
|
+
});
|
|
352
392
|
} else {
|
|
353
393
|
throw new UnsupportedFunctionalityError2({
|
|
354
394
|
functionality: `media type: ${part.mediaType}`
|
|
@@ -364,7 +404,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
364
404
|
for (let i2 = 0; i2 < content.length; i2++) {
|
|
365
405
|
const part = content[i2];
|
|
366
406
|
const isLastPart = i2 === content.length - 1;
|
|
367
|
-
const cacheControl = (
|
|
407
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
368
408
|
const toolResultContent = part.content != null ? part.content.map((part2) => {
|
|
369
409
|
var _a2;
|
|
370
410
|
switch (part2.type) {
|
|
@@ -414,7 +454,7 @@ async function convertToAnthropicMessagesPrompt({
|
|
|
414
454
|
for (let k = 0; k < content.length; k++) {
|
|
415
455
|
const part = content[k];
|
|
416
456
|
const isLastContentPart = k === content.length - 1;
|
|
417
|
-
const cacheControl = (
|
|
457
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
418
458
|
switch (part.type) {
|
|
419
459
|
case "text": {
|
|
420
460
|
anthropicContent.push({
|
|
@@ -563,8 +603,42 @@ function mapAnthropicStopReason({
|
|
|
563
603
|
}
|
|
564
604
|
|
|
565
605
|
// src/anthropic-messages-language-model.ts
|
|
566
|
-
|
|
567
|
-
|
|
606
|
+
var citationSchemas = {
|
|
607
|
+
webSearchResult: z3.object({
|
|
608
|
+
type: z3.literal("web_search_result_location"),
|
|
609
|
+
cited_text: z3.string(),
|
|
610
|
+
url: z3.string(),
|
|
611
|
+
title: z3.string(),
|
|
612
|
+
encrypted_index: z3.string()
|
|
613
|
+
}),
|
|
614
|
+
pageLocation: z3.object({
|
|
615
|
+
type: z3.literal("page_location"),
|
|
616
|
+
cited_text: z3.string(),
|
|
617
|
+
document_index: z3.number(),
|
|
618
|
+
document_title: z3.string().nullable(),
|
|
619
|
+
start_page_number: z3.number(),
|
|
620
|
+
end_page_number: z3.number()
|
|
621
|
+
}),
|
|
622
|
+
charLocation: z3.object({
|
|
623
|
+
type: z3.literal("char_location"),
|
|
624
|
+
cited_text: z3.string(),
|
|
625
|
+
document_index: z3.number(),
|
|
626
|
+
document_title: z3.string().nullable(),
|
|
627
|
+
start_char_index: z3.number(),
|
|
628
|
+
end_char_index: z3.number()
|
|
629
|
+
})
|
|
630
|
+
};
|
|
631
|
+
var citationSchema = z3.discriminatedUnion("type", [
|
|
632
|
+
citationSchemas.webSearchResult,
|
|
633
|
+
citationSchemas.pageLocation,
|
|
634
|
+
citationSchemas.charLocation
|
|
635
|
+
]);
|
|
636
|
+
var documentCitationSchema = z3.discriminatedUnion("type", [
|
|
637
|
+
citationSchemas.pageLocation,
|
|
638
|
+
citationSchemas.charLocation
|
|
639
|
+
]);
|
|
640
|
+
function processCitation(citation, citationDocuments, generateId2, onSource) {
|
|
641
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
568
642
|
const source = createCitationSource(
|
|
569
643
|
citation,
|
|
570
644
|
citationDocuments,
|
|
@@ -581,6 +655,15 @@ function createCitationSource(citation, citationDocuments, generateId2) {
|
|
|
581
655
|
if (!documentInfo) {
|
|
582
656
|
return null;
|
|
583
657
|
}
|
|
658
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
659
|
+
citedText: citation.cited_text,
|
|
660
|
+
startPageNumber: citation.start_page_number,
|
|
661
|
+
endPageNumber: citation.end_page_number
|
|
662
|
+
} : {
|
|
663
|
+
citedText: citation.cited_text,
|
|
664
|
+
startCharIndex: citation.start_char_index,
|
|
665
|
+
endCharIndex: citation.end_char_index
|
|
666
|
+
};
|
|
584
667
|
return {
|
|
585
668
|
type: "source",
|
|
586
669
|
sourceType: "document",
|
|
@@ -589,11 +672,7 @@ function createCitationSource(citation, citationDocuments, generateId2) {
|
|
|
589
672
|
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
590
673
|
filename: documentInfo.filename,
|
|
591
674
|
providerMetadata: {
|
|
592
|
-
anthropic:
|
|
593
|
-
citedText: citation.cited_text,
|
|
594
|
-
startPageNumber: citation.start_page_number,
|
|
595
|
-
endPageNumber: citation.end_page_number
|
|
596
|
-
}
|
|
675
|
+
anthropic: providerMetadata
|
|
597
676
|
}
|
|
598
677
|
};
|
|
599
678
|
}
|
|
@@ -795,15 +874,19 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
795
874
|
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
796
875
|
}
|
|
797
876
|
extractCitationDocuments(prompt) {
|
|
798
|
-
const
|
|
877
|
+
const isCitationPart = (part) => {
|
|
799
878
|
var _a, _b;
|
|
879
|
+
if (part.type !== "file") {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
800
885
|
const anthropic = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
801
886
|
const citationsConfig = anthropic == null ? void 0 : anthropic.citations;
|
|
802
887
|
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
803
888
|
};
|
|
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) => {
|
|
889
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
807
890
|
var _a;
|
|
808
891
|
const filePart = part;
|
|
809
892
|
return {
|
|
@@ -840,7 +923,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
840
923
|
content.push({ type: "text", text: part.text });
|
|
841
924
|
if (part.citations) {
|
|
842
925
|
for (const citation of part.citations) {
|
|
843
|
-
|
|
926
|
+
processCitation(
|
|
844
927
|
citation,
|
|
845
928
|
citationDocuments,
|
|
846
929
|
this.generateId,
|
|
@@ -986,6 +1069,9 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
986
1069
|
},
|
|
987
1070
|
transform(chunk, controller) {
|
|
988
1071
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
1072
|
+
if (options.includeRawChunks) {
|
|
1073
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1074
|
+
}
|
|
989
1075
|
if (!chunk.success) {
|
|
990
1076
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
991
1077
|
return;
|
|
@@ -1140,7 +1226,7 @@ var AnthropicMessagesLanguageModel = class {
|
|
|
1140
1226
|
}
|
|
1141
1227
|
case "citations_delta": {
|
|
1142
1228
|
const citation = value.delta.citation;
|
|
1143
|
-
|
|
1229
|
+
processCitation(
|
|
1144
1230
|
citation,
|
|
1145
1231
|
citationDocuments,
|
|
1146
1232
|
generateId2,
|
|
@@ -1215,25 +1301,7 @@ var anthropicMessagesResponseSchema = z3.object({
|
|
|
1215
1301
|
z3.object({
|
|
1216
1302
|
type: z3.literal("text"),
|
|
1217
1303
|
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()
|
|
1304
|
+
citations: z3.array(citationSchema).optional()
|
|
1237
1305
|
}),
|
|
1238
1306
|
z3.object({
|
|
1239
1307
|
type: z3.literal("thinking"),
|
|
@@ -1372,23 +1440,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
|
|
|
1372
1440
|
}),
|
|
1373
1441
|
z3.object({
|
|
1374
1442
|
type: z3.literal("citations_delta"),
|
|
1375
|
-
citation:
|
|
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
|
-
])
|
|
1443
|
+
citation: citationSchema
|
|
1392
1444
|
})
|
|
1393
1445
|
])
|
|
1394
1446
|
}),
|