@langchain/anthropic 1.4.0 → 1.4.1
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 +9 -0
- package/dist/chat_models.cjs +4 -1
- package/dist/chat_models.cjs.map +1 -1
- package/dist/chat_models.d.cts.map +1 -1
- package/dist/chat_models.d.ts.map +1 -1
- package/dist/chat_models.js +4 -1
- package/dist/chat_models.js.map +1 -1
- package/dist/output_parsers.cjs.map +1 -1
- package/dist/output_parsers.js.map +1 -1
- package/dist/utils/message_inputs.cjs.map +1 -1
- package/dist/utils/message_inputs.js.map +1 -1
- package/dist/utils/message_outputs.cjs.map +1 -1
- package/dist/utils/message_outputs.js.map +1 -1
- package/dist/utils/params.cjs +23 -9
- package/dist/utils/params.cjs.map +1 -1
- package/dist/utils/params.js +23 -9
- package/dist/utils/params.js.map +1 -1
- package/dist/utils/prompts.cjs.map +1 -1
- package/dist/utils/prompts.js.map +1 -1
- package/dist/utils/standard.cjs.map +1 -1
- package/dist/utils/standard.js.map +1 -1
- package/dist/utils/stream_events.cjs.map +1 -1
- package/dist/utils/stream_events.js.map +1 -1
- package/package.json +8 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"standard.js","names":[],"sources":["../../src/utils/standard.ts"],"sourcesContent":["import type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n BaseMessage,\n ContentBlock,\n ResponseMetadata,\n} from \"@langchain/core/messages\";\nimport { iife } from \"./index.js\";\n\nfunction _isStandardAnnotation(\n annotation: unknown\n): annotation is ContentBlock.Citation {\n return (\n typeof annotation === \"object\" &&\n annotation !== null &&\n \"type\" in annotation &&\n annotation.type === \"citation\"\n );\n}\n\nfunction _formatStandardCitations(\n annotations: ContentBlock.Citation[]\n): Anthropic.Beta.BetaTextCitation[] {\n function* iterateAnnotations() {\n for (const annotation of annotations) {\n if (_isStandardAnnotation(annotation)) {\n if (annotation.source === \"char\") {\n yield {\n type: \"char_location\" as const,\n file_id: annotation.url ?? \"\",\n start_char_index: annotation.startIndex ?? 0,\n end_char_index: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"page\") {\n yield {\n type: \"page_location\" as const,\n file_id: annotation.url ?? \"\",\n start_page_number: annotation.startIndex ?? 0,\n end_page_number: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"block\") {\n yield {\n type: \"content_block_location\" as const,\n file_id: annotation.url ?? \"\",\n start_block_index: annotation.startIndex ?? 0,\n end_block_index: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"url\") {\n yield {\n type: \"web_search_result_location\" as const,\n url: annotation.url ?? \"\",\n title: annotation.title ?? null,\n encrypted_index: String(annotation.startIndex ?? 0),\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"search\") {\n yield {\n type: \"search_result_location\" as const,\n title: annotation.title ?? null,\n start_block_index: annotation.startIndex ?? 0,\n end_block_index: annotation.endIndex ?? 0,\n search_result_index: 0,\n source: annotation.source ?? \"\",\n cited_text: annotation.citedText ?? \"\",\n };\n }\n }\n }\n }\n return Array.from(iterateAnnotations());\n}\n\nfunction _formatBase64Data(data: string | Uint8Array): string {\n if (typeof data === \"string\") return data;\n else return _encodeUint8Array(data);\n}\n\nfunction _encodeUint8Array(data: Uint8Array): string {\n const output = [];\n for (let i = 0, { length } = data; i < length; i++) {\n output.push(String.fromCharCode(data[i]));\n }\n return btoa(output.join(\"\"));\n}\n\nfunction _normalizeMimeType(mimeType?: string | null): string {\n return (mimeType ?? \"\").split(\";\")[0].toLowerCase();\n}\n\nfunction _extractMetadataValue<T>(\n metadata: unknown,\n key: string\n): T | undefined {\n if (\n metadata !== undefined &&\n metadata !== null &&\n typeof metadata === \"object\" &&\n key in metadata\n ) {\n return (metadata as Record<string, unknown>)[key] as T;\n }\n return undefined;\n}\n\nfunction _applyDocumentMetadata(\n block: Anthropic.Beta.BetaRequestDocumentBlock,\n metadata: unknown\n): Anthropic.Beta.BetaRequestDocumentBlock {\n const cacheControl =\n _extractMetadataValue<Anthropic.Beta.BetaCacheControlEphemeral | null>(\n metadata,\n \"cache_control\"\n );\n if (cacheControl !== undefined) {\n block.cache_control = cacheControl;\n }\n const citations =\n _extractMetadataValue<Anthropic.Beta.BetaCitationsConfigParam | null>(\n metadata,\n \"citations\"\n );\n if (citations !== undefined) {\n block.citations = citations;\n }\n const context = _extractMetadataValue<string | null>(metadata, \"context\");\n if (context !== undefined) {\n block.context = context;\n }\n const title = _extractMetadataValue<string | null>(metadata, \"title\");\n if (title !== undefined) {\n block.title = title;\n }\n return block;\n}\n\nfunction _applyImageMetadata(\n block: Anthropic.Beta.BetaImageBlockParam,\n metadata: unknown\n): Anthropic.Beta.BetaImageBlockParam {\n const cacheControl =\n _extractMetadataValue<Anthropic.Beta.BetaCacheControlEphemeral | null>(\n metadata,\n \"cache_control\"\n );\n if (cacheControl !== undefined) {\n block.cache_control = cacheControl;\n }\n return block;\n}\n\nfunction _hasAllowedImageMimeType(\n mimeType: string\n): mimeType is \"image/jpeg\" | \"image/png\" | \"image/gif\" | \"image/webp\" {\n const ALLOWED_IMAGE_MIME_TYPES = new Set([\n \"image/jpeg\",\n \"image/png\",\n \"image/gif\",\n \"image/webp\",\n ]);\n return ALLOWED_IMAGE_MIME_TYPES.has(mimeType);\n}\n\nexport function _formatStandardContent(\n message: BaseMessage\n): Anthropic.Beta.BetaContentBlockParam[] {\n const result: Anthropic.Beta.BetaContentBlockParam[] = [];\n const responseMetadata = message.response_metadata as ResponseMetadata;\n const isAnthropicMessage =\n \"model_provider\" in responseMetadata &&\n responseMetadata?.model_provider === \"anthropic\";\n for (const block of message.contentBlocks) {\n if (block.type === \"text\") {\n if (block.annotations) {\n result.push({\n type: \"text\",\n text: block.text,\n citations: _formatStandardCitations(\n block.annotations as ContentBlock.Citation[]\n ),\n });\n } else {\n result.push({\n type: \"text\",\n text: block.text,\n });\n }\n } else if (block.type === \"tool_call\") {\n result.push({\n type: \"tool_use\",\n id: block.id ?? \"\",\n name: block.name,\n input: block.args,\n });\n } else if (block.type === \"tool_call_chunk\") {\n const input = iife(() => {\n if (typeof block.args !== \"string\") {\n return block.args;\n }\n try {\n return JSON.parse(block.args);\n } catch {\n return {};\n }\n });\n result.push({\n type: \"tool_use\",\n id: block.id ?? \"\",\n name: block.name ?? \"\",\n input,\n });\n } else if (block.type === \"reasoning\" && isAnthropicMessage) {\n result.push({\n type: \"thinking\",\n thinking: block.reasoning,\n signature: String(block.signature),\n });\n } else if (block.type === \"server_tool_call\" && isAnthropicMessage) {\n if (block.name === \"web_search\") {\n result.push({\n type: \"server_tool_use\",\n name: block.name,\n id: block.id ?? \"\",\n input: block.args,\n });\n } else if (block.name === \"code_execution\") {\n result.push({\n type: \"server_tool_use\",\n name: block.name,\n id: block.id ?? \"\",\n input: block.args,\n });\n }\n } else if (block.type === \"server_tool_call_result\" && isAnthropicMessage) {\n if (block.name === \"web_search\" && Array.isArray(block.output.urls)) {\n const content = block.output.urls.map((url) => ({\n type: \"web_search_result\" as const,\n title: \"\",\n encrypted_content: \"\",\n url,\n }));\n result.push({\n type: \"web_search_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n content,\n });\n } else if (block.name === \"code_execution\") {\n result.push({\n type: \"code_execution_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n content: block.output as any,\n });\n } else if (block.name === \"mcp_tool_result\") {\n result.push({\n type: \"mcp_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n content: block.output as any,\n });\n }\n } else if (block.type === \"audio\") {\n throw new Error(\"Anthropic does not support audio content blocks.\");\n } else if (block.type === \"file\") {\n const metadata = block.metadata;\n if (block.fileId) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"file\",\n file_id: block.fileId,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.url) {\n const mimeType = _normalizeMimeType(block.mimeType);\n if (mimeType === \"application/pdf\" || mimeType === \"\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"url\",\n url: block.url,\n },\n },\n metadata\n )\n );\n continue;\n }\n }\n if (block.data) {\n const mimeType = _normalizeMimeType(block.mimeType);\n if (mimeType === \"\" || mimeType === \"application/pdf\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: \"application/pdf\",\n },\n },\n metadata\n )\n );\n } else if (mimeType === \"text/plain\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"text\",\n data: _formatBase64Data(block.data),\n media_type: \"text/plain\",\n },\n },\n metadata\n )\n );\n } else {\n if (_hasAllowedImageMimeType(mimeType)) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"content\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: mimeType,\n },\n },\n ],\n },\n },\n metadata\n )\n );\n } else {\n throw new Error(\n `Unsupported file mime type for Anthropic base64 source: ${mimeType}`\n );\n }\n }\n continue;\n }\n throw new Error(\n \"File content block must include a fileId, url, or data property.\"\n );\n } else if (block.type === \"image\") {\n const metadata = block.metadata;\n if (block.fileId) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"file\",\n file_id: block.fileId,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.url) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"url\",\n url: block.url,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.data) {\n const mimeType = _normalizeMimeType(block.mimeType) || \"image/png\";\n if (_hasAllowedImageMimeType(mimeType)) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: mimeType,\n },\n },\n metadata\n )\n );\n }\n continue;\n }\n throw new Error(\n \"Image content block must include a fileId, url, or data property.\"\n );\n } else if (block.type === \"video\") {\n // no-op\n } else if (block.type === \"text-plain\") {\n if (block.data) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"text\",\n data: _formatBase64Data(block.data),\n media_type: \"text/plain\",\n },\n },\n block.metadata\n )\n );\n }\n } else if (block.type === \"non_standard\" && isAnthropicMessage) {\n result.push(block.value as Anthropic.Beta.BetaContentBlockParam);\n }\n }\n return result;\n}\n"],"mappings":";;AAQA,SAAS,sBACP,YACqC;AACrC,QACE,OAAO,eAAe,YACtB,eAAe,QACf,UAAU,cACV,WAAW,SAAS;;AAIxB,SAAS,yBACP,aACmC;CACnC,UAAU,qBAAqB;AAC7B,OAAK,MAAM,cAAc,YACvB,KAAI,sBAAsB,WAAW;OAC/B,WAAW,WAAW,OACxB,OAAM;IACJ,MAAM;IACN,SAAS,WAAW,OAAO;IAC3B,kBAAkB,WAAW,cAAc;IAC3C,gBAAgB,WAAW,YAAY;IACvC,gBAAgB,WAAW,SAAS;IACpC,gBAAgB;IAChB,YAAY,WAAW,aAAa;IACrC;YACQ,WAAW,WAAW,OAC/B,OAAM;IACJ,MAAM;IACN,SAAS,WAAW,OAAO;IAC3B,mBAAmB,WAAW,cAAc;IAC5C,iBAAiB,WAAW,YAAY;IACxC,gBAAgB,WAAW,SAAS;IACpC,gBAAgB;IAChB,YAAY,WAAW,aAAa;IACrC;YACQ,WAAW,WAAW,QAC/B,OAAM;IACJ,MAAM;IACN,SAAS,WAAW,OAAO;IAC3B,mBAAmB,WAAW,cAAc;IAC5C,iBAAiB,WAAW,YAAY;IACxC,gBAAgB,WAAW,SAAS;IACpC,gBAAgB;IAChB,YAAY,WAAW,aA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AA8C;AAGpE,QAAO"}
|
|
1
|
+
{"version":3,"file":"standard.js","names":[],"sources":["../../src/utils/standard.ts"],"sourcesContent":["import type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n BaseMessage,\n ContentBlock,\n ResponseMetadata,\n} from \"@langchain/core/messages\";\nimport { iife } from \"./index.js\";\n\nfunction _isStandardAnnotation(\n annotation: unknown\n): annotation is ContentBlock.Citation {\n return (\n typeof annotation === \"object\" &&\n annotation !== null &&\n \"type\" in annotation &&\n annotation.type === \"citation\"\n );\n}\n\nfunction _formatStandardCitations(\n annotations: ContentBlock.Citation[]\n): Anthropic.Beta.BetaTextCitation[] {\n function* iterateAnnotations() {\n for (const annotation of annotations) {\n if (_isStandardAnnotation(annotation)) {\n if (annotation.source === \"char\") {\n yield {\n type: \"char_location\" as const,\n file_id: annotation.url ?? \"\",\n start_char_index: annotation.startIndex ?? 0,\n end_char_index: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"page\") {\n yield {\n type: \"page_location\" as const,\n file_id: annotation.url ?? \"\",\n start_page_number: annotation.startIndex ?? 0,\n end_page_number: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"block\") {\n yield {\n type: \"content_block_location\" as const,\n file_id: annotation.url ?? \"\",\n start_block_index: annotation.startIndex ?? 0,\n end_block_index: annotation.endIndex ?? 0,\n document_title: annotation.title ?? null,\n document_index: 0,\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"url\") {\n yield {\n type: \"web_search_result_location\" as const,\n url: annotation.url ?? \"\",\n title: annotation.title ?? null,\n encrypted_index: String(annotation.startIndex ?? 0),\n cited_text: annotation.citedText ?? \"\",\n };\n } else if (annotation.source === \"search\") {\n yield {\n type: \"search_result_location\" as const,\n title: annotation.title ?? null,\n start_block_index: annotation.startIndex ?? 0,\n end_block_index: annotation.endIndex ?? 0,\n search_result_index: 0,\n source: annotation.source ?? \"\",\n cited_text: annotation.citedText ?? \"\",\n };\n }\n }\n }\n }\n return Array.from(iterateAnnotations());\n}\n\nfunction _formatBase64Data(data: string | Uint8Array): string {\n if (typeof data === \"string\") return data;\n else return _encodeUint8Array(data);\n}\n\nfunction _encodeUint8Array(data: Uint8Array): string {\n const output = [];\n for (let i = 0, { length } = data; i < length; i++) {\n output.push(String.fromCharCode(data[i]));\n }\n return btoa(output.join(\"\"));\n}\n\nfunction _normalizeMimeType(mimeType?: string | null): string {\n return (mimeType ?? \"\").split(\";\")[0].toLowerCase();\n}\n\nfunction _extractMetadataValue<T>(\n metadata: unknown,\n key: string\n): T | undefined {\n if (\n metadata !== undefined &&\n metadata !== null &&\n typeof metadata === \"object\" &&\n key in metadata\n ) {\n return (metadata as Record<string, unknown>)[key] as T;\n }\n return undefined;\n}\n\nfunction _applyDocumentMetadata(\n block: Anthropic.Beta.BetaRequestDocumentBlock,\n metadata: unknown\n): Anthropic.Beta.BetaRequestDocumentBlock {\n const cacheControl =\n _extractMetadataValue<Anthropic.Beta.BetaCacheControlEphemeral | null>(\n metadata,\n \"cache_control\"\n );\n if (cacheControl !== undefined) {\n block.cache_control = cacheControl;\n }\n const citations =\n _extractMetadataValue<Anthropic.Beta.BetaCitationsConfigParam | null>(\n metadata,\n \"citations\"\n );\n if (citations !== undefined) {\n block.citations = citations;\n }\n const context = _extractMetadataValue<string | null>(metadata, \"context\");\n if (context !== undefined) {\n block.context = context;\n }\n const title = _extractMetadataValue<string | null>(metadata, \"title\");\n if (title !== undefined) {\n block.title = title;\n }\n return block;\n}\n\nfunction _applyImageMetadata(\n block: Anthropic.Beta.BetaImageBlockParam,\n metadata: unknown\n): Anthropic.Beta.BetaImageBlockParam {\n const cacheControl =\n _extractMetadataValue<Anthropic.Beta.BetaCacheControlEphemeral | null>(\n metadata,\n \"cache_control\"\n );\n if (cacheControl !== undefined) {\n block.cache_control = cacheControl;\n }\n return block;\n}\n\nfunction _hasAllowedImageMimeType(\n mimeType: string\n): mimeType is \"image/jpeg\" | \"image/png\" | \"image/gif\" | \"image/webp\" {\n const ALLOWED_IMAGE_MIME_TYPES = new Set([\n \"image/jpeg\",\n \"image/png\",\n \"image/gif\",\n \"image/webp\",\n ]);\n return ALLOWED_IMAGE_MIME_TYPES.has(mimeType);\n}\n\nexport function _formatStandardContent(\n message: BaseMessage\n): Anthropic.Beta.BetaContentBlockParam[] {\n const result: Anthropic.Beta.BetaContentBlockParam[] = [];\n const responseMetadata = message.response_metadata as ResponseMetadata;\n const isAnthropicMessage =\n \"model_provider\" in responseMetadata &&\n responseMetadata?.model_provider === \"anthropic\";\n for (const block of message.contentBlocks) {\n if (block.type === \"text\") {\n if (block.annotations) {\n result.push({\n type: \"text\",\n text: block.text,\n citations: _formatStandardCitations(\n block.annotations as ContentBlock.Citation[]\n ),\n });\n } else {\n result.push({\n type: \"text\",\n text: block.text,\n });\n }\n } else if (block.type === \"tool_call\") {\n result.push({\n type: \"tool_use\",\n id: block.id ?? \"\",\n name: block.name,\n input: block.args,\n });\n } else if (block.type === \"tool_call_chunk\") {\n const input = iife(() => {\n if (typeof block.args !== \"string\") {\n return block.args;\n }\n try {\n return JSON.parse(block.args);\n } catch {\n return {};\n }\n });\n result.push({\n type: \"tool_use\",\n id: block.id ?? \"\",\n name: block.name ?? \"\",\n input,\n });\n } else if (block.type === \"reasoning\" && isAnthropicMessage) {\n result.push({\n type: \"thinking\",\n thinking: block.reasoning,\n signature: String(block.signature),\n });\n } else if (block.type === \"server_tool_call\" && isAnthropicMessage) {\n if (block.name === \"web_search\") {\n result.push({\n type: \"server_tool_use\",\n name: block.name,\n id: block.id ?? \"\",\n input: block.args,\n });\n } else if (block.name === \"code_execution\") {\n result.push({\n type: \"server_tool_use\",\n name: block.name,\n id: block.id ?? \"\",\n input: block.args,\n });\n }\n } else if (block.type === \"server_tool_call_result\" && isAnthropicMessage) {\n if (block.name === \"web_search\" && Array.isArray(block.output.urls)) {\n const content = block.output.urls.map((url) => ({\n type: \"web_search_result\" as const,\n title: \"\",\n encrypted_content: \"\",\n url,\n }));\n result.push({\n type: \"web_search_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n content,\n });\n } else if (block.name === \"code_execution\") {\n result.push({\n type: \"code_execution_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n content: block.output as any,\n });\n } else if (block.name === \"mcp_tool_result\") {\n result.push({\n type: \"mcp_tool_result\",\n tool_use_id: block.toolCallId ?? \"\",\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n content: block.output as any,\n });\n }\n } else if (block.type === \"audio\") {\n throw new Error(\"Anthropic does not support audio content blocks.\");\n } else if (block.type === \"file\") {\n const metadata = block.metadata;\n if (block.fileId) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"file\",\n file_id: block.fileId,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.url) {\n const mimeType = _normalizeMimeType(block.mimeType);\n if (mimeType === \"application/pdf\" || mimeType === \"\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"url\",\n url: block.url,\n },\n },\n metadata\n )\n );\n continue;\n }\n }\n if (block.data) {\n const mimeType = _normalizeMimeType(block.mimeType);\n if (mimeType === \"\" || mimeType === \"application/pdf\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: \"application/pdf\",\n },\n },\n metadata\n )\n );\n } else if (mimeType === \"text/plain\") {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"text\",\n data: _formatBase64Data(block.data),\n media_type: \"text/plain\",\n },\n },\n metadata\n )\n );\n } else {\n if (_hasAllowedImageMimeType(mimeType)) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"content\",\n content: [\n {\n type: \"image\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: mimeType,\n },\n },\n ],\n },\n },\n metadata\n )\n );\n } else {\n throw new Error(\n `Unsupported file mime type for Anthropic base64 source: ${mimeType}`\n );\n }\n }\n continue;\n }\n throw new Error(\n \"File content block must include a fileId, url, or data property.\"\n );\n } else if (block.type === \"image\") {\n const metadata = block.metadata;\n if (block.fileId) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"file\",\n file_id: block.fileId,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.url) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"url\",\n url: block.url,\n },\n },\n metadata\n )\n );\n continue;\n }\n if (block.data) {\n const mimeType = _normalizeMimeType(block.mimeType) || \"image/png\";\n if (_hasAllowedImageMimeType(mimeType)) {\n result.push(\n _applyImageMetadata(\n {\n type: \"image\",\n source: {\n type: \"base64\",\n data: _formatBase64Data(block.data),\n media_type: mimeType,\n },\n },\n metadata\n )\n );\n }\n continue;\n }\n throw new Error(\n \"Image content block must include a fileId, url, or data property.\"\n );\n } else if (block.type === \"video\") {\n // no-op\n } else if (block.type === \"text-plain\") {\n if (block.data) {\n result.push(\n _applyDocumentMetadata(\n {\n type: \"document\",\n source: {\n type: \"text\",\n data: _formatBase64Data(block.data),\n media_type: \"text/plain\",\n },\n },\n block.metadata\n )\n );\n }\n } else if (block.type === \"non_standard\" && isAnthropicMessage) {\n result.push(block.value as Anthropic.Beta.BetaContentBlockParam);\n }\n }\n return result;\n}\n"],"mappings":";;AAQA,SAAS,sBACP,YACqC;AACrC,QACE,OAAO,eAAe,YACtB,eAAe,QACf,UAAU,cACV,WAAW,SAAS;;AAIxB,SAAS,yBACP,aACmC;CACnC,UAAU,qBAAqB;AAC7B,OAAK,MAAM,cAAc,YACvB,KAAI,sBAAsB,WAAW;OAC/B,WAAW,WAAW,OACxB,OAAM;IACJ,MAAM;IACN,SAAS,WAAW,OAAO;IA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ACF;AACD;;AAEF,MAAI,MAAM,MAAM;GACd,MAAM,WAAW,mBAAmB,MAAM,SAAS,IAAI;AACvD,OAAI,yBAAyB,SAAS,CACpC,QAAO,KACL,oBACE;IACE,MAAM;IACN,QAAQ;KACN,MAAM;KACN,MAAM,kBAAkB,MAAM,KAAK;KACnC,YAAY;KACb;IACF,EACD,SACD,CACF;AAEH;;AAEF,QAAM,IAAI,MACR,oEACD;YACQ,MAAM,SAAS,SAAS,YAExB,MAAM,SAAS;MACpB,MAAM,KACR,QAAO,KACL,uBACE;GACE,MAAM;GACN,QAAQ;IACN,MAAM;IACN,MAAM,kBAAkB,MAAM,KAAK;IACnC,YAAY;IACb;GACF,EACD,MAAM,SACP,CACF;YAEM,MAAM,SAAS,kBAAkB,mBAC1C,QAAO,KAAK,MAAM,MAA8C;AAGpE,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream_events.cjs","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock } from \"@langchain/core/messages/content\";\nimport type { UsageMetadata } from \"@langchain/core/messages/metadata\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AA0BA,gBAAuB,uBACrB,QACA,UAAyC,EAAE,EACL;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,KAI3B;CACH,IAAI;CACJ,IAAI,aAA4B;AAEhC,YAAW,MAAM,QAAQ,OACvB,SAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;AAClC,OAAI,SAAS,kBACX,iBAAgB,mBAAmB,MAAM;AAE3C,SAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,eAAe,GAAG,EAAE;IAClD;AACD,SAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,SAAS;KAAE;KAAO;KAAI;IACvB;AACD;;EAGF,KAAK;AACH,gBAAa,KAAK,MAAM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AY;IAClB,MAAM,YAAY;IAClB,OAAO;IACR;;AAEH,SAAO;GACL,MAAM;GACN,IAAI,YAAY;GAChB,MAAM,YAAY;GAClB,MAAM;GAEP;;CAGH,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;AACnC,QAAO"}
|
|
1
|
+
{"version":3,"file":"stream_events.cjs","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock } from \"@langchain/core/messages/content\";\nimport type { UsageMetadata } from \"@langchain/core/messages/metadata\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AA0BA,gBAAuB,uBACrB,QACA,UAAyC,EAAE,EACL;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,KAI3B;CACH,IAAI;CACJ,IAAI,aAA4B;AAEhC,YAAW,MAAM,QAAQ,OACvB,SAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;AAClC,OAAI,SAAS,kBACX,iBAAgB,mBAAmB,MAAM;AAE3C,SAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,eAAe,GAAG,EAAE;IAClD;AACD,SAAM;IACJ,OAAO;IACP,UAAU;IACV,MAAM;IACN,SAAS;KAAE;KAAO;KAAI;IACvB;AACD;;EAGF,KAAK;AACH,gBAAa,KAAK,MAAM;AACxB,OAAI,qBAAqB,KAAK,OAAO;AACnC,QAAI,CAAC,cACH,iBAAgB;KACd,cAAc;KACd,eAAe,KAAK,MAAM;KAC1B,cAAc,KAAK,MAAM;KAC1B;QAED,iBAAgB;KACd,GAAG;KACH,eACE,cAAc,gBAAgB,KAAK,MAAM;KAC3C,cACE,cAAc,eACd,cAAc,gBACd,KAAK,MAAM;KACd;AAEH,UAAM;KAAE,OAAO;KAAkB,OAAO;KAAe;;AAEzD,OACE,wBAAwB,KAAK,SAC7B,KAAK,MAAM,mBAEX,OAAM;IACJ,OAAO;IACP,UAAU;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AAS;AACxE,UAAO;IACL,cAAc;KACZ,MAAM;KACN,QAAQ;MACN,MAAM,YAAY;MAClB;MACD;KACF;IACD,aAAa;KACX,GAAG;KACH;KACD;IACF;;EAGH,KAAK,kBACH,QAAO;GACL,cAAc;IACZ,MAAM;IACN,QAAQ;KAAE,MAAM,YAAY;KAAM,WAAW,MAAM;KAAW;IAC/D;GACD,aAAa;IAAE,GAAG;IAAa,WAAW,MAAM;IAAW;GAC5D;EAEH,KAAK,mBACH,QAAO;GACL,cAAc;IACZ,MAAM;IACN,QAAQ;KACN,MAAM;KACN,OAAO;MAAE,GAAI,YAAY,SAAS,EAAE;MAAG,YAAY;MAAO;KAC3D;IACF;GACD,aAAa;IACX,GAAG;IACH,OAAO;KAAE,GAAI,YAAY,SAAS,EAAE;KAAG,YAAY;KAAO;IAC3D;GACF;EAEH,QACE,QAAO;GACL,cAAc;IACZ,MAAM;IACN,QAAQ;KAAE,MAAM,YAAY;KAAM,GAAG;KAAO;IAC7C;GACD;GACD;;;AAIP,SAAS,cAEP,aACc;AACd,KACE,YAAY,SAAS,qBACrB,YAAY,SAAS,0BACrB;EACA,MAAM,YACJ,YAAY,SAAS,oBAChB,cACA;EACP,IAAI;AACJ,MAAI;AACF,gBAAa,KAAK,MAAM,YAAY,QAAQ,KAAK;UAC3C;AACN,UAAO;IACL,MAAM;IACN,IAAI,YAAY;IAChB,MAAM,YAAY;IAClB,MAAM,YAAY;IAClB,OAAO;IACR;;AAEH,SAAO;GACL,MAAM;GACN,IAAI,YAAY;GAChB,MAAM,YAAY;GAClB,MAAM;GAEP;;CAGH,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;AACnC,QAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream_events.js","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock } from \"@langchain/core/messages/content\";\nimport type { UsageMetadata } from \"@langchain/core/messages/metadata\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;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AM;KACN,OAAO;MAAE,GAAI,YAAY,SAAS,EAAE;MAAG,YAAY;MAAO;KAC3D;IACF;GACD,aAAa;IACX,GAAG;IACH,OAAO;KAAE,GAAI,YAAY,SAAS,EAAE;KAAG,YAAY;KAAO;IAC3D;GACF;EAEH,QACE,QAAO;GACL,cAAc;IACZ,MAAM;IACN,QAAQ;KAAE,MAAM,YAAY;KAAM,GAAG;KAAO;IAC7C;GACD;GACD;;;AAIP,SAAS,cAEP,aACc;AACd,KACE,YAAY,SAAS,qBACrB,YAAY,SAAS,0BACrB;EACA,MAAM,YACJ,YAAY,SAAS,oBAChB,cACA;EACP,IAAI;AACJ,MAAI;AACF,gBAAa,KAAK,MAAM,YAAY,QAAQ,KAAK;UAC3C;AACN,UAAO;IACL,MAAM;IACN,IAAI,YAAY;IAChB,MAAM,YAAY;IAClB,MAAM,YAAY;IAClB,OAAO;IACR;;AAEH,SAAO;GACL,MAAM;GACN,IAAI,YAAY;GAChB,MAAM,YAAY;GAClB,MAAM;GAEP;;CAGH,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;AACnC,QAAO"}
|
|
1
|
+
{"version":3,"file":"stream_events.js","names":[],"sources":["../../src/utils/stream_events.ts"],"sourcesContent":["/**\n * Converts a raw Anthropic SSE event stream into LangChain ChatModelStreamEvents.\n *\n * @module\n */\n\nimport type Anthropic from \"@anthropic-ai/sdk\";\nimport type {\n ChatModelStreamEvent,\n ContentBlockDelta,\n FinishReason,\n} from \"@langchain/core/language_models/event\";\nimport type { ContentBlock } from \"@langchain/core/messages/content\";\nimport type { UsageMetadata } from \"@langchain/core/messages/metadata\";\nimport type { AnthropicMessageStreamEvent } from \"../types.js\";\n\n// ─── Public API ─────────────────────────────────────────────────\n\nexport interface ConvertAnthropicStreamOptions {\n streamUsage?: boolean;\n}\n\n/**\n * Convert an async iterable of raw Anthropic stream events into\n * LangChain `ChatModelStreamEvent`s with typed deltas.\n */\nexport async function* convertAnthropicStream(\n source: AsyncIterable<AnthropicMessageStreamEvent>,\n options: ConvertAnthropicStreamOptions = {}\n): AsyncGenerator<ChatModelStreamEvent> {\n const shouldStreamUsage = options.streamUsage ?? true;\n\n // Track accumulated state per content block (for finalization)\n const blockAccumulators = new Map<\n number,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n Record<string, any>\n >();\n let usageSnapshot: UsageMetadata | undefined;\n let stopReason: string | null = null;\n\n for await (const data of source) {\n switch (data.type) {\n // ── Message lifecycle ──────────────────────────────────\n case \"message_start\": {\n const { usage, id, model } = data.message;\n if (usage && shouldStreamUsage) {\n usageSnapshot = buildUsageSnapshot(usage);\n }\n yield {\n event: \"message-start\" as const,\n id,\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n };\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"message_start\",\n payload: { model, id },\n };\n break;\n }\n\n case \"message_delta\": {\n stopReason = data.delta.stop_reason;\n if (shouldStreamUsage && data.usage) {\n if (!usageSnapshot) {\n usageSnapshot = {\n input_tokens: 0,\n output_tokens: data.usage.output_tokens,\n total_tokens: data.usage.output_tokens,\n };\n } else {\n usageSnapshot = {\n ...usageSnapshot,\n output_tokens:\n usageSnapshot.output_tokens + data.usage.output_tokens,\n total_tokens:\n usageSnapshot.input_tokens +\n usageSnapshot.output_tokens +\n data.usage.output_tokens,\n };\n }\n yield { event: \"usage\" as const, usage: usageSnapshot };\n }\n if (\n \"context_management\" in data.delta &&\n data.delta.context_management\n ) {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: \"context_management\",\n payload: data.delta.context_management,\n };\n }\n break;\n }\n\n case \"message_stop\": {\n yield {\n event: \"message-finish\" as const,\n reason: mapStopReason(stopReason),\n ...(usageSnapshot ? { usage: usageSnapshot } : {}),\n metadata: { model_provider: \"anthropic\" },\n };\n break;\n }\n\n // ── Content block lifecycle ───────────────────────────\n case \"content_block_start\": {\n const { index, content_block } = data;\n const mapped = mapBlockToContentBlock(content_block, index);\n blockAccumulators.set(index, { ...mapped });\n yield {\n event: \"content-block-start\" as const,\n index,\n content: mapped,\n };\n break;\n }\n\n case \"content_block_delta\": {\n const { index, delta } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const { contentDelta, accumulated } = applyAnthropicDelta(acc, delta);\n blockAccumulators.set(index, accumulated);\n\n yield {\n event: \"content-block-delta\" as const,\n index,\n delta: contentDelta,\n };\n break;\n }\n\n case \"content_block_stop\": {\n const { index } = data;\n const acc = blockAccumulators.get(index);\n if (!acc) break;\n\n const finalized = finalizeBlock(acc);\n yield {\n event: \"content-block-finish\" as const,\n index,\n content: finalized,\n };\n blockAccumulators.delete(index);\n break;\n }\n\n // ── Unhandled → provider passthrough ───────────────────\n default: {\n yield {\n event: \"provider\" as const,\n provider: \"anthropic\",\n name: data.type,\n payload: data,\n };\n break;\n }\n }\n }\n}\n\n// ─── Internal helpers ───────────────────────────────────────────\n\nfunction mapStopReason(stopReason: string | null | undefined): FinishReason {\n switch (stopReason) {\n case \"end_turn\":\n case \"stop_sequence\":\n return \"stop\";\n case \"tool_use\":\n return \"tool_use\";\n case \"max_tokens\":\n return \"length\";\n default:\n return \"stop\";\n }\n}\n\nfunction buildUsageSnapshot(\n usage: Anthropic.Messages.Usage | Record<string, number>\n): UsageMetadata {\n const cacheCreation =\n (usage as Record<string, number>).cache_creation_input_tokens ?? 0;\n const cacheRead =\n (usage as Record<string, number>).cache_read_input_tokens ?? 0;\n const totalInput = usage.input_tokens + cacheCreation + cacheRead;\n return {\n input_tokens: totalInput,\n output_tokens: usage.output_tokens,\n total_tokens: totalInput + usage.output_tokens,\n input_token_details: {\n cache_creation: cacheCreation,\n cache_read: cacheRead,\n },\n };\n}\n\nfunction mapBlockToContentBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n block: any,\n index: number\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n switch (block.type) {\n case \"text\":\n return { type: \"text\" as const, text: block.text ?? \"\", index };\n case \"thinking\":\n return {\n type: \"reasoning\" as const,\n reasoning: block.thinking ?? \"\",\n index,\n };\n case \"redacted_thinking\":\n return { type: \"non_standard\" as const, value: { ...block }, index };\n case \"tool_use\":\n return {\n type: \"tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n case \"server_tool_use\":\n return {\n type: \"server_tool_call_chunk\" as const,\n id: block.id,\n name: block.name,\n args: \"\",\n index,\n };\n default:\n return { type: \"non_standard\" as const, value: { ...block }, index };\n }\n}\n\n/**\n * Map an Anthropic content_block_delta to a content block delta\n * and update the accumulated state.\n */\nfunction applyAnthropicDelta(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n delta: any\n): {\n contentDelta: ContentBlockDelta;\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>;\n} {\n switch (delta.type) {\n case \"text_delta\":\n return {\n contentDelta: { type: \"text-delta\" as const, text: delta.text },\n accumulated: {\n ...accumulated,\n text: (accumulated.text ?? \"\") + delta.text,\n },\n };\n\n case \"thinking_delta\":\n return {\n contentDelta: {\n type: \"reasoning-delta\" as const,\n reasoning: delta.thinking,\n },\n accumulated: {\n ...accumulated,\n reasoning: (accumulated.reasoning ?? \"\") + delta.thinking,\n },\n };\n\n case \"input_json_delta\": {\n const newArgs = (accumulated.args ?? \"\") + delta.partial_json;\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, args: newArgs },\n },\n accumulated: { ...accumulated, args: newArgs },\n };\n }\n\n case \"citations_delta\": {\n const annotations = [...(accumulated.annotations ?? []), delta.citation];\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: accumulated.type,\n annotations,\n },\n },\n accumulated: {\n ...accumulated,\n annotations,\n },\n };\n }\n\n case \"signature_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, signature: delta.signature },\n },\n accumulated: { ...accumulated, signature: delta.signature },\n };\n\n case \"compaction_delta\":\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: {\n type: \"non_standard\",\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n },\n accumulated: {\n ...accumulated,\n value: { ...(accumulated.value ?? {}), compaction: delta },\n },\n };\n\n default:\n return {\n contentDelta: {\n type: \"block-delta\" as const,\n fields: { type: accumulated.type, ...delta },\n },\n accumulated,\n };\n }\n}\n\nfunction finalizeBlock(\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n accumulated: Record<string, any>\n): ContentBlock {\n if (\n accumulated.type === \"tool_call_chunk\" ||\n accumulated.type === \"server_tool_call_chunk\"\n ) {\n const finalType =\n accumulated.type === \"tool_call_chunk\"\n ? (\"tool_call\" as const)\n : (\"server_tool_call\" as const);\n let parsedArgs: unknown;\n try {\n parsedArgs = JSON.parse(accumulated.args || \"{}\");\n } catch {\n return {\n type: \"invalid_tool_call\" as const,\n id: accumulated.id,\n name: accumulated.name,\n args: accumulated.args,\n error: \"Failed to parse tool call arguments as JSON\",\n } as ContentBlock.Tools.InvalidToolCall;\n }\n return {\n type: finalType,\n id: accumulated.id,\n name: accumulated.name,\n args: parsedArgs,\n // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n } as any;\n }\n\n const { index: _index, ...rest } = accumulated;\n return rest as ContentBlock;\n}\n"],"mappings":";;;;;AA0BA,gBAAuB,uBACrB,QACA,UAAyC,EAAE,EACL;CACtC,MAAM,oBAAoB,QAAQ,eAAe;CAGjD,MAAM,oCAAoB,IAAI,KAI3B;CACH,IAAI;CACJ,IAAI,aAA4B;AAEhC,YAAW,MAAM,QAAQ,OACvB,SAAQ,KAAK,MAAb;EAEE,KAAK,iBAAiB;GACpB,MAAM,EAAE,OAAO,IAAI,UAAU,KAAK;AAClC,OAAI,SAAS,kBACX,iBAAgB,mBAAmB,MAAM;AAE3C,SAAM;IACJ,OAAO;IACP;IACA,GAAI,gBAAgB,EAAE,OAAO,eAA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S,oBAChB,cACA;EACP,IAAI;AACJ,MAAI;AACF,gBAAa,KAAK,MAAM,YAAY,QAAQ,KAAK;UAC3C;AACN,UAAO;IACL,MAAM;IACN,IAAI,YAAY;IAChB,MAAM,YAAY;IAClB,MAAM,YAAY;IAClB,OAAO;IACR;;AAEH,SAAO;GACL,MAAM;GACN,IAAI,YAAY;GAChB,MAAM,YAAY;GAClB,MAAM;GAEP;;CAGH,MAAM,EAAE,OAAO,QAAQ,GAAG,SAAS;AACnC,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/anthropic",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Anthropic integrations for LangChain.js",
|
|
5
5
|
"author": "LangChain",
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,23 +14,23 @@
|
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-anthropic/",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@anthropic-ai/sdk": "^0.
|
|
17
|
+
"@anthropic-ai/sdk": "^0.103.0",
|
|
18
18
|
"zod": "^3.25.76 || ^4"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@langchain/core": "^1.1.
|
|
21
|
+
"@langchain/core": "^1.1.49"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@anthropic-ai/vertex-sdk": "^0.
|
|
24
|
+
"@anthropic-ai/vertex-sdk": "^0.16.1",
|
|
25
25
|
"@cfworker/json-schema": "^4.1.1",
|
|
26
26
|
"@tsconfig/recommended": "^1.0.10",
|
|
27
|
-
"@vitest/coverage-v8": "^
|
|
27
|
+
"@vitest/coverage-v8": "^4.1.8",
|
|
28
28
|
"dotenv": "^17.4.0",
|
|
29
29
|
"dpdm": "^3.14.0",
|
|
30
30
|
"rimraf": "^6.1.3",
|
|
31
|
-
"typescript": "~
|
|
32
|
-
"vitest": "^4.1.
|
|
33
|
-
"@langchain/core": "^1.1.
|
|
31
|
+
"typescript": "~6.0.3",
|
|
32
|
+
"vitest": "^4.1.8",
|
|
33
|
+
"@langchain/core": "^1.1.49",
|
|
34
34
|
"@langchain/standard-tests": "0.0.23",
|
|
35
35
|
"@langchain/tsconfig": "0.0.1"
|
|
36
36
|
},
|