@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/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 = (_c = getCacheControl(part.providerOptions)) != null ? _c : isLastPart ? getCacheControl(message.providerOptions) : void 0;
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 = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
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
- function processPageLocationCitation(citation, citationDocuments, generateId3, onSource) {
577
- if (citation.type === "page_location") {
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 isCitationEnabled = (part) => {
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
- processPageLocationCitation(
936
+ processCitation(
854
937
  citation,
855
938
  citationDocuments,
856
939
  this.generateId,
@@ -996,6 +1079,9 @@ var AnthropicMessagesLanguageModel = class {
996
1079
  },
997
1080
  transform(chunk, controller) {
998
1081
  var _a, _b, _c, _d, _e, _f, _g;
1082
+ if (options.includeRawChunks) {
1083
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1084
+ }
999
1085
  if (!chunk.success) {
1000
1086
  controller.enqueue({ type: "error", error: chunk.error });
1001
1087
  return;
@@ -1150,7 +1236,7 @@ var AnthropicMessagesLanguageModel = class {
1150
1236
  }
1151
1237
  case "citations_delta": {
1152
1238
  const citation = value.delta.citation;
1153
- processPageLocationCitation(
1239
+ processCitation(
1154
1240
  citation,
1155
1241
  citationDocuments,
1156
1242
  generateId3,
@@ -1225,25 +1311,7 @@ var anthropicMessagesResponseSchema = z3.object({
1225
1311
  z3.object({
1226
1312
  type: z3.literal("text"),
1227
1313
  text: z3.string(),
1228
- citations: z3.array(
1229
- z3.discriminatedUnion("type", [
1230
- z3.object({
1231
- type: z3.literal("web_search_result_location"),
1232
- cited_text: z3.string(),
1233
- url: z3.string(),
1234
- title: z3.string(),
1235
- encrypted_index: z3.string()
1236
- }),
1237
- z3.object({
1238
- type: z3.literal("page_location"),
1239
- cited_text: z3.string(),
1240
- document_index: z3.number(),
1241
- document_title: z3.string().nullable(),
1242
- start_page_number: z3.number(),
1243
- end_page_number: z3.number()
1244
- })
1245
- ])
1246
- ).optional()
1314
+ citations: z3.array(citationSchema).optional()
1247
1315
  }),
1248
1316
  z3.object({
1249
1317
  type: z3.literal("thinking"),
@@ -1382,23 +1450,7 @@ var anthropicMessagesChunkSchema = z3.discriminatedUnion("type", [
1382
1450
  }),
1383
1451
  z3.object({
1384
1452
  type: z3.literal("citations_delta"),
1385
- citation: z3.discriminatedUnion("type", [
1386
- z3.object({
1387
- type: z3.literal("web_search_result_location"),
1388
- cited_text: z3.string(),
1389
- url: z3.string(),
1390
- title: z3.string(),
1391
- encrypted_index: z3.string()
1392
- }),
1393
- z3.object({
1394
- type: z3.literal("page_location"),
1395
- cited_text: z3.string(),
1396
- document_index: z3.number(),
1397
- document_title: z3.string().nullable(),
1398
- start_page_number: z3.number(),
1399
- end_page_number: z3.number()
1400
- })
1401
- ])
1453
+ citation: citationSchema
1402
1454
  })
1403
1455
  ])
1404
1456
  }),