@langchain/anthropic
Version:
Anthropic integrations for LangChain.js
1 lines • 12.9 kB
Source Map (JSON)
{"version":3,"file":"content.cjs","names":["block: unknown","standardContentBlockConverter: StandardContentBlockConverter<{\n text: Anthropic.Messages.TextBlockParam;\n image: Anthropic.Messages.ImageBlockParam;\n file: Anthropic.Messages.DocumentBlockParam;\n}>"],"sources":["../../src/utils/content.ts"],"sourcesContent":["import type Anthropic from \"@anthropic-ai/sdk\";\nimport {\n parseBase64DataUrl,\n type StandardContentBlockConverter,\n} from \"@langchain/core/messages\";\n\nexport function _isAnthropicThinkingBlock(\n block: unknown\n): block is Anthropic.Messages.ThinkingBlockParam {\n return (\n typeof block === \"object\" &&\n block !== null &&\n \"type\" in block &&\n block.type === \"thinking\"\n );\n}\n\nexport function _isAnthropicRedactedThinkingBlock(\n block: unknown\n): block is Anthropic.Messages.RedactedThinkingBlockParam {\n return (\n typeof block === \"object\" &&\n block !== null &&\n \"type\" in block &&\n block.type === \"redacted_thinking\"\n );\n}\n\nexport function _isAnthropicSearchResultBlock(\n block: unknown\n): block is Anthropic.Beta.BetaSearchResultBlockParam {\n return (\n typeof block === \"object\" &&\n block !== null &&\n \"type\" in block &&\n block.type === \"search_result\"\n );\n}\n\nexport function _isAnthropicImageBlockParam(\n block: unknown\n): block is Anthropic.Messages.ImageBlockParam {\n if (typeof block !== \"object\" || block == null) {\n return false;\n }\n if (!(\"type\" in block) || block.type !== \"image\") {\n return false;\n }\n\n if (\n !(\"source\" in block) ||\n typeof block.source !== \"object\" ||\n block.source == null\n ) {\n return false;\n }\n\n if (!(\"type\" in block.source)) {\n return false;\n }\n\n if (block.source.type === \"base64\") {\n if (!(\"media_type\" in block.source)) {\n return false;\n }\n\n if (typeof block.source.media_type !== \"string\") {\n return false;\n }\n\n if (!(\"data\" in block.source)) {\n return false;\n }\n\n if (typeof block.source.data !== \"string\") {\n return false;\n }\n\n return true;\n }\n\n if (block.source.type === \"url\") {\n if (!(\"url\" in block.source)) {\n return false;\n }\n\n if (typeof block.source.url !== \"string\") {\n return false;\n }\n\n return true;\n }\n\n return false;\n}\n\nexport const standardContentBlockConverter: StandardContentBlockConverter<{\n text: Anthropic.Messages.TextBlockParam;\n image: Anthropic.Messages.ImageBlockParam;\n file: Anthropic.Messages.DocumentBlockParam;\n}> = {\n providerName: \"anthropic\",\n\n fromStandardTextBlock(block): Anthropic.Messages.TextBlockParam {\n return {\n type: \"text\",\n text: block.text,\n ...(\"citations\" in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as Anthropic.Messages.TextBlockParam;\n },\n\n fromStandardImageBlock(block): Anthropic.Messages.ImageBlockParam {\n if (block.source_type === \"url\") {\n const data = parseBase64DataUrl({\n dataUrl: block.url,\n asTypedArray: false,\n });\n if (data) {\n return {\n type: \"image\",\n source: {\n type: \"base64\",\n data: data.data,\n media_type: data.mime_type,\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as Anthropic.Messages.ImageBlockParam;\n } else {\n return {\n type: \"image\",\n source: {\n type: \"url\",\n url: block.url,\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as Anthropic.Messages.ImageBlockParam;\n }\n } else {\n if (block.source_type === \"base64\") {\n return {\n type: \"image\",\n source: {\n type: \"base64\",\n data: block.data,\n media_type: block.mime_type ?? \"\",\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n } as Anthropic.Messages.ImageBlockParam;\n } else {\n throw new Error(`Unsupported image source type: ${block.source_type}`);\n }\n }\n },\n\n fromStandardFileBlock(block): Anthropic.Messages.DocumentBlockParam {\n const mime_type = (block.mime_type ?? \"\").split(\";\")[0];\n\n if (block.source_type === \"url\") {\n if (mime_type === \"application/pdf\" || mime_type === \"\") {\n return {\n type: \"document\",\n source: {\n type: \"url\",\n url: block.url,\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...(\"citations\" in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...(\"context\" in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...(\"title\" in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n }\n throw new Error(\n `Unsupported file mime type for file url source: ${block.mime_type}`\n );\n } else if (block.source_type === \"text\") {\n if (mime_type === \"text/plain\" || mime_type === \"\") {\n return {\n type: \"document\",\n source: {\n type: \"text\",\n data: block.text,\n media_type: block.mime_type ?? \"\",\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...(\"citations\" in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...(\"context\" in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...(\"title\" in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file text source: ${block.mime_type}`\n );\n }\n } else if (block.source_type === \"base64\") {\n if (mime_type === \"application/pdf\" || mime_type === \"\") {\n return {\n type: \"document\",\n source: {\n type: \"base64\",\n data: block.data,\n media_type: \"application/pdf\",\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...(\"citations\" in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...(\"context\" in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...(\"title\" in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n } else if (\n [\"image/jpeg\", \"image/png\", \"image/gif\", \"image/webp\"].includes(\n mime_type\n )\n ) {\n return {\n type: \"document\",\n source: {\n type: \"content\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n data: block.data,\n media_type: mime_type as\n | \"image/jpeg\"\n | \"image/png\"\n | \"image/gif\"\n | \"image/webp\",\n },\n },\n ],\n },\n ...(\"cache_control\" in (block.metadata ?? {})\n ? { cache_control: block.metadata!.cache_control }\n : {}),\n ...(\"citations\" in (block.metadata ?? {})\n ? { citations: block.metadata!.citations }\n : {}),\n ...(\"context\" in (block.metadata ?? {})\n ? { context: block.metadata!.context }\n : {}),\n ...(\"title\" in (block.metadata ?? {})\n ? { title: block.metadata!.title }\n : {}),\n } as Anthropic.Messages.DocumentBlockParam;\n } else {\n throw new Error(\n `Unsupported file mime type for file base64 source: ${block.mime_type}`\n );\n }\n } else {\n throw new Error(`Unsupported file source type: ${block.source_type}`);\n }\n },\n};\n"],"mappings":";;;;AAMA,SAAgB,0BACdA,OACgD;AAChD,QACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAElB;AAED,SAAgB,kCACdA,OACwD;AACxD,QACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAElB;AAED,SAAgB,8BACdA,OACoD;AACpD,QACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,MAAM,SAAS;AAElB;AAED,SAAgB,4BACdA,OAC6C;AAC7C,KAAI,OAAO,UAAU,YAAY,SAAS,KACxC,QAAO;AAET,KAAI,EAAE,UAAU,UAAU,MAAM,SAAS,QACvC,QAAO;AAGT,KACE,EAAE,YAAY,UACd,OAAO,MAAM,WAAW,YACxB,MAAM,UAAU,KAEhB,QAAO;AAGT,KAAI,EAAE,UAAU,MAAM,QACpB,QAAO;AAGT,KAAI,MAAM,OAAO,SAAS,UAAU;AAClC,MAAI,EAAE,gBAAgB,MAAM,QAC1B,QAAO;AAGT,MAAI,OAAO,MAAM,OAAO,eAAe,SACrC,QAAO;AAGT,MAAI,EAAE,UAAU,MAAM,QACpB,QAAO;AA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