@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.
@@ -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 = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
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 = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
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
- function processPageLocationCitation(citation, citationDocuments, generateId2, onSource) {
567
- if (citation.type === "page_location") {
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 isCitationEnabled = (part) => {
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
- processPageLocationCitation(
926
+ processCitation(
844
927
  citation,
845
928
  citationDocuments,
846
929
  this.generateId,
@@ -1143,7 +1226,7 @@ var AnthropicMessagesLanguageModel = class {
1143
1226
  }
1144
1227
  case "citations_delta": {
1145
1228
  const citation = value.delta.citation;
1146
- processPageLocationCitation(
1229
+ processCitation(
1147
1230
  citation,
1148
1231
  citationDocuments,
1149
1232
  generateId2,
@@ -1218,25 +1301,7 @@ var anthropicMessagesResponseSchema = z3.object({
1218
1301
  z3.object({
1219
1302
  type: z3.literal("text"),
1220
1303
  text: z3.string(),
1221
- citations: z3.array(
1222
- z3.discriminatedUnion("type", [
1223
- z3.object({
1224
- type: z3.literal("web_search_result_location"),
1225
- cited_text: z3.string(),
1226
- url: z3.string(),
1227
- title: z3.string(),
1228
- encrypted_index: z3.string()
1229
- }),
1230
- z3.object({
1231
- type: z3.literal("page_location"),
1232
- cited_text: z3.string(),
1233
- document_index: z3.number(),
1234
- document_title: z3.string().nullable(),
1235
- start_page_number: z3.number(),
1236
- end_page_number: z3.number()
1237
- })
1238
- ])
1239
- ).optional()
1304
+ citations: z3.array(citationSchema).optional()
1240
1305
  }),
1241
1306
  z3.object({
1242
1307
  type: z3.literal("thinking"),
@@ -1375,23 +1440,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1375
1440
  }),
1376
1441
  z3.object({
1377
1442
  type: z3.literal("citations_delta"),
1378
- citation: z3.discriminatedUnion("type", [
1379
- z3.object({
1380
- type: z3.literal("web_search_result_location"),
1381
- cited_text: z3.string(),
1382
- url: z3.string(),
1383
- title: z3.string(),
1384
- encrypted_index: z3.string()
1385
- }),
1386
- z3.object({
1387
- type: z3.literal("page_location"),
1388
- cited_text: z3.string(),
1389
- document_index: z3.number(),
1390
- document_title: z3.string().nullable(),
1391
- start_page_number: z3.number(),
1392
- end_page_number: z3.number()
1393
- })
1394
- ])
1443
+ citation: citationSchema
1395
1444
  })
1396
1445
  ])
1397
1446
  }),