@langchain/core 1.0.0-alpha.4 → 1.0.0-alpha.6
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/agents.d.ts.map +1 -1
- package/dist/caches/base.d.cts.map +1 -1
- package/dist/caches/base.d.ts.map +1 -1
- package/dist/callbacks/base.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.cts.map +1 -1
- package/dist/language_models/chat_models.d.ts.map +1 -1
- package/dist/load/import_map.cjs +1 -1
- package/dist/load/import_map.js +1 -1
- package/dist/messages/ai.cjs +19 -14
- package/dist/messages/ai.cjs.map +1 -1
- package/dist/messages/ai.js +19 -14
- package/dist/messages/ai.js.map +1 -1
- package/dist/messages/base.cjs +9 -2
- package/dist/messages/base.cjs.map +1 -1
- package/dist/messages/base.d.cts +2 -2
- package/dist/messages/base.d.cts.map +1 -1
- package/dist/messages/base.d.ts +2 -2
- package/dist/messages/base.d.ts.map +1 -1
- package/dist/messages/base.js +9 -2
- package/dist/messages/base.js.map +1 -1
- package/dist/messages/block_translators/bedrock_converse.cjs +213 -0
- package/dist/messages/block_translators/bedrock_converse.cjs.map +1 -0
- package/dist/messages/block_translators/bedrock_converse.js +213 -0
- package/dist/messages/block_translators/bedrock_converse.js.map +1 -0
- package/dist/messages/block_translators/google_genai.cjs +73 -0
- package/dist/messages/block_translators/google_genai.cjs.map +1 -0
- package/dist/messages/block_translators/google_genai.js +73 -0
- package/dist/messages/block_translators/google_genai.js.map +1 -0
- package/dist/messages/block_translators/google_vertexai.cjs +74 -0
- package/dist/messages/block_translators/google_vertexai.cjs.map +1 -0
- package/dist/messages/block_translators/google_vertexai.js +74 -0
- package/dist/messages/block_translators/google_vertexai.js.map +1 -0
- package/dist/messages/block_translators/index.cjs +10 -1
- package/dist/messages/block_translators/index.cjs.map +1 -1
- package/dist/messages/block_translators/index.js +10 -1
- package/dist/messages/block_translators/index.js.map +1 -1
- package/dist/messages/block_translators/utils.cjs +4 -0
- package/dist/messages/block_translators/utils.cjs.map +1 -1
- package/dist/messages/block_translators/utils.js +4 -1
- package/dist/messages/block_translators/utils.js.map +1 -1
- package/dist/messages/content/index.cjs.map +1 -1
- package/dist/messages/content/index.d.cts +20 -1
- package/dist/messages/content/index.d.cts.map +1 -1
- package/dist/messages/content/index.d.ts +20 -1
- package/dist/messages/content/index.d.ts.map +1 -1
- package/dist/messages/content/index.js.map +1 -1
- package/dist/messages/content/tools.cjs +3 -0
- package/dist/messages/content/tools.cjs.map +1 -1
- package/dist/messages/content/tools.d.cts +67 -6
- package/dist/messages/content/tools.d.cts.map +1 -1
- package/dist/messages/content/tools.d.ts +67 -6
- package/dist/messages/content/tools.d.ts.map +1 -1
- package/dist/messages/content/tools.js +3 -0
- package/dist/messages/content/tools.js.map +1 -1
- package/dist/messages/index.cjs +4 -0
- package/dist/messages/index.cjs.map +1 -1
- package/dist/messages/index.d.cts +2 -2
- package/dist/messages/index.d.ts +2 -2
- package/dist/messages/index.js +4 -2
- package/dist/messages/index.js.map +1 -1
- package/dist/messages/message.cjs.map +1 -1
- package/dist/messages/message.d.cts +6 -6
- package/dist/messages/message.d.cts.map +1 -1
- package/dist/messages/message.d.ts +6 -6
- package/dist/messages/message.d.ts.map +1 -1
- package/dist/messages/message.js.map +1 -1
- package/dist/messages/tool.cjs.map +1 -1
- package/dist/messages/tool.d.cts +3 -1
- package/dist/messages/tool.d.cts.map +1 -1
- package/dist/messages/tool.d.ts +3 -1
- package/dist/messages/tool.d.ts.map +1 -1
- package/dist/messages/tool.js.map +1 -1
- package/dist/utils/testing/chat_models.cjs +245 -0
- package/dist/utils/testing/chat_models.cjs.map +1 -0
- package/dist/utils/testing/chat_models.d.cts +134 -0
- package/dist/utils/testing/chat_models.d.cts.map +1 -0
- package/dist/utils/testing/chat_models.d.ts +134 -0
- package/dist/utils/testing/chat_models.d.ts.map +1 -0
- package/dist/utils/testing/chat_models.js +243 -0
- package/dist/utils/testing/chat_models.js.map +1 -0
- package/dist/utils/testing/embeddings.cjs +90 -0
- package/dist/utils/testing/embeddings.cjs.map +1 -0
- package/dist/utils/testing/embeddings.d.cts +58 -0
- package/dist/utils/testing/embeddings.d.cts.map +1 -0
- package/dist/utils/testing/embeddings.d.ts +58 -0
- package/dist/utils/testing/embeddings.d.ts.map +1 -0
- package/dist/utils/testing/embeddings.js +89 -0
- package/dist/utils/testing/embeddings.js.map +1 -0
- package/dist/utils/testing/index.cjs +42 -655
- package/dist/utils/testing/index.cjs.map +1 -1
- package/dist/utils/testing/index.d.cts +11 -379
- package/dist/utils/testing/index.d.ts +11 -379
- package/dist/utils/testing/index.js +10 -623
- package/dist/utils/testing/index.js.map +1 -1
- package/dist/utils/testing/llms.cjs +59 -0
- package/dist/utils/testing/llms.cjs.map +1 -0
- package/dist/utils/testing/llms.d.cts +31 -0
- package/dist/utils/testing/llms.d.cts.map +1 -0
- package/dist/utils/testing/llms.d.ts +31 -0
- package/dist/utils/testing/llms.d.ts.map +1 -0
- package/dist/utils/testing/llms.js +58 -0
- package/dist/utils/testing/llms.js.map +1 -0
- package/dist/utils/testing/message_history.cjs +67 -0
- package/dist/utils/testing/message_history.cjs.map +1 -0
- package/dist/utils/testing/message_history.d.cts +31 -0
- package/dist/utils/testing/message_history.d.cts.map +1 -0
- package/dist/utils/testing/message_history.d.ts +31 -0
- package/dist/utils/testing/message_history.d.ts.map +1 -0
- package/dist/utils/testing/message_history.js +65 -0
- package/dist/utils/testing/message_history.js.map +1 -0
- package/dist/utils/testing/output_parsers.cjs +20 -0
- package/dist/utils/testing/output_parsers.cjs.map +1 -0
- package/dist/utils/testing/output_parsers.d.cts +16 -0
- package/dist/utils/testing/output_parsers.d.cts.map +1 -0
- package/dist/utils/testing/output_parsers.d.ts +16 -0
- package/dist/utils/testing/output_parsers.d.ts.map +1 -0
- package/dist/utils/testing/output_parsers.js +20 -0
- package/dist/utils/testing/output_parsers.js.map +1 -0
- package/dist/utils/testing/retrievers.cjs +19 -0
- package/dist/utils/testing/retrievers.cjs.map +1 -0
- package/dist/utils/testing/retrievers.d.cts +17 -0
- package/dist/utils/testing/retrievers.d.cts.map +1 -0
- package/dist/utils/testing/retrievers.d.ts +17 -0
- package/dist/utils/testing/retrievers.d.ts.map +1 -0
- package/dist/utils/testing/retrievers.js +19 -0
- package/dist/utils/testing/retrievers.js.map +1 -0
- package/dist/utils/testing/runnables.cjs +19 -0
- package/dist/utils/testing/runnables.cjs.map +1 -0
- package/dist/utils/testing/runnables.d.cts +15 -0
- package/dist/utils/testing/runnables.d.cts.map +1 -0
- package/dist/utils/testing/runnables.d.ts +15 -0
- package/dist/utils/testing/runnables.d.ts.map +1 -0
- package/dist/utils/testing/runnables.js +19 -0
- package/dist/utils/testing/runnables.js.map +1 -0
- package/dist/utils/testing/tools.cjs +21 -0
- package/dist/utils/testing/tools.cjs.map +1 -0
- package/dist/utils/testing/tools.d.cts +21 -0
- package/dist/utils/testing/tools.d.cts.map +1 -0
- package/dist/utils/testing/tools.d.ts +21 -0
- package/dist/utils/testing/tools.d.ts.map +1 -0
- package/dist/utils/testing/tools.js +21 -0
- package/dist/utils/testing/tools.js.map +1 -0
- package/dist/utils/testing/tracers.cjs +25 -0
- package/dist/utils/testing/tracers.cjs.map +1 -0
- package/dist/utils/testing/tracers.d.cts +15 -0
- package/dist/utils/testing/tracers.d.cts.map +1 -0
- package/dist/utils/testing/tracers.d.ts +15 -0
- package/dist/utils/testing/tracers.d.ts.map +1 -0
- package/dist/utils/testing/tracers.js +25 -0
- package/dist/utils/testing/tracers.js.map +1 -0
- package/dist/utils/testing/vectorstores.cjs +129 -0
- package/dist/utils/testing/vectorstores.cjs.map +1 -0
- package/dist/utils/testing/vectorstores.d.cts +101 -0
- package/dist/utils/testing/vectorstores.d.cts.map +1 -0
- package/dist/utils/testing/vectorstores.d.ts +101 -0
- package/dist/utils/testing/vectorstores.d.ts.map +1 -0
- package/dist/utils/testing/vectorstores.js +129 -0
- package/dist/utils/testing/vectorstores.js.map +1 -0
- package/dist/utils/types/index.cjs +2 -0
- package/dist/utils/types/index.d.cts +2 -2
- package/dist/utils/types/index.d.ts +2 -2
- package/dist/utils/types/index.js +3 -2
- package/dist/utils/types/zod.cjs +39 -3
- package/dist/utils/types/zod.cjs.map +1 -1
- package/dist/utils/types/zod.d.cts +18 -1
- package/dist/utils/types/zod.d.cts.map +1 -1
- package/dist/utils/types/zod.d.ts +18 -1
- package/dist/utils/types/zod.d.ts.map +1 -1
- package/dist/utils/types/zod.js +39 -4
- package/dist/utils/types/zod.js.map +1 -1
- package/package.json +1 -1
- package/dist/utils/testing/index.d.cts.map +0 -1
- package/dist/utils/testing/index.d.ts.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","names":["firstContent: MessageContent","secondContent: MessageContent","left?: \"success\" | \"error\"","right?: \"success\" | \"error\"","obj: any","depthLimit: number","currentDepth: number","obj","result: Record<string, unknown>","arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>","fields: BaseMessageFields<TStructure, TRole>","blocks: Array<ContentBlock>","blocks","obj: unknown","value: string | undefined","depth: number | null","value?: unknown","left: Record<string, any>","right: Record<string, any>","left?: Content[]","right?: Content[]","left: T | undefined","right: T | undefined","x: BaseMessageLike","messageLike?: unknown"],"sources":["../../src/messages/base.ts"],"sourcesContent":["import { Serializable, SerializedConstructor } from \"../load/serializable.js\";\nimport { ContentBlock } from \"./content/index.js\";\nimport { isDataContentBlock } from \"./content/data.js\";\nimport { convertToV1FromAnthropicInput } from \"./block_translators/anthropic.js\";\nimport { convertToV1FromDataContent } from \"./block_translators/data.js\";\nimport { convertToV1FromChatCompletionsInput } from \"./block_translators/openai.js\";\nimport {\n $InferMessageContent,\n $InferResponseMetadata,\n MessageStructure,\n MessageType,\n isMessage,\n Message,\n} from \"./message.js\";\n\n/** @internal */\nconst MESSAGE_SYMBOL = Symbol.for(\"langchain.message\");\n\nexport interface StoredMessageData {\n content: string;\n role: string | undefined;\n name: string | undefined;\n tool_call_id: string | undefined;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n additional_kwargs?: Record<string, any>;\n /** Response metadata. For example: response headers, logprobs, token counts, model name. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n response_metadata?: Record<string, any>;\n id?: string;\n}\n\nexport interface StoredMessage {\n type: string;\n data: StoredMessageData;\n}\n\nexport interface StoredGeneration {\n text: string;\n message?: StoredMessage;\n}\n\nexport interface StoredMessageV1 {\n type: string;\n role: string | undefined;\n text: string;\n}\n\nexport type MessageContent = string | Array<ContentBlock>;\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n}\n\nexport type BaseMessageFields<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> = {\n id?: string;\n name?: string;\n content?: $InferMessageContent<TStructure, TRole>;\n contentBlocks?: Array<ContentBlock.Standard>;\n /** @deprecated */\n additional_kwargs?: {\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n function_call?: FunctionCall;\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n tool_calls?: OpenAIToolCall[];\n [key: string]: unknown;\n };\n response_metadata?: Partial<$InferResponseMetadata<TStructure, TRole>>;\n};\n\nexport function mergeContent(\n firstContent: MessageContent,\n secondContent: MessageContent\n): MessageContent {\n // If first content is a string\n if (typeof firstContent === \"string\") {\n if (firstContent === \"\") {\n return secondContent;\n }\n if (typeof secondContent === \"string\") {\n return firstContent + secondContent;\n } else if (\n Array.isArray(secondContent) &&\n secondContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n {\n type: \"text\",\n source_type: \"text\",\n text: firstContent,\n },\n ...secondContent,\n ];\n } else {\n return [{ type: \"text\", text: firstContent }, ...secondContent];\n }\n // If both are arrays\n } else if (Array.isArray(secondContent)) {\n return (\n _mergeLists(firstContent, secondContent) ?? [\n ...firstContent,\n ...secondContent,\n ]\n );\n } else {\n if (secondContent === \"\") {\n return firstContent;\n } else if (\n Array.isArray(firstContent) &&\n firstContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n ...firstContent,\n {\n type: \"file\",\n source_type: \"text\",\n text: secondContent,\n },\n ];\n } else {\n return [...firstContent, { type: \"text\", text: secondContent }];\n }\n }\n}\n\n/**\n * 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else\n * it will return 'success'.\n *\n * @param {\"success\" | \"error\" | undefined} left The existing value to 'merge' with the new value.\n * @param {\"success\" | \"error\" | undefined} right The new value to 'merge' with the existing value\n * @returns {\"success\" | \"error\"} The 'merged' value.\n */\nexport function _mergeStatus(\n left?: \"success\" | \"error\",\n right?: \"success\" | \"error\"\n): \"success\" | \"error\" | undefined {\n if (left === \"error\" || right === \"error\") {\n return \"error\";\n }\n return \"success\";\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction stringifyWithDepthLimit(obj: any, depthLimit: number): string {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n function helper(obj: any, currentDepth: number): any {\n if (typeof obj !== \"object\" || obj === null || obj === undefined) {\n return obj;\n }\n if (currentDepth >= depthLimit) {\n if (Array.isArray(obj)) {\n return \"[Array]\";\n }\n return \"[Object]\";\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => helper(item, currentDepth + 1));\n }\n\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(obj)) {\n result[key] = helper(obj[key], currentDepth + 1);\n }\n return result;\n }\n\n return JSON.stringify(helper(obj, 0), null, 2);\n}\n\n/**\n * Base class for all types of messages in a conversation. It includes\n * properties like `content`, `name`, and `additional_kwargs`. It also\n * includes methods like `toDict()` and `_getType()`.\n */\nexport abstract class BaseMessage<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n >\n extends Serializable\n implements Message<TStructure, TRole>\n{\n lc_namespace = [\"langchain_core\", \"messages\"];\n\n lc_serializable = true;\n\n get lc_aliases(): Record<string, string> {\n // exclude snake case conversion to pascal case\n return {\n additional_kwargs: \"additional_kwargs\",\n response_metadata: \"response_metadata\",\n };\n }\n\n readonly [MESSAGE_SYMBOL] = true as const;\n\n abstract readonly type: TRole;\n\n id?: string;\n\n name?: string;\n\n content: $InferMessageContent<TStructure, TRole>;\n\n additional_kwargs: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"additional_kwargs\"]\n >;\n\n response_metadata: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"response_metadata\"]\n >;\n\n /**\n * @deprecated Use .getType() instead or import the proper typeguard.\n * For example:\n *\n * ```ts\n * import { isAIMessage } from \"@langchain/core/messages\";\n *\n * const message = new AIMessage(\"Hello!\");\n * isAIMessage(message); // true\n * ```\n */\n _getType(): MessageType {\n return this.type;\n }\n\n /**\n * @deprecated Use .type instead\n * The type of the message.\n */\n getType(): MessageType {\n return this._getType();\n }\n\n constructor(\n arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>\n ) {\n const fields: BaseMessageFields<TStructure, TRole> =\n typeof arg === \"string\" || Array.isArray(arg) ? { content: arg } : arg;\n if (!fields.additional_kwargs) {\n fields.additional_kwargs = {};\n }\n if (!fields.response_metadata) {\n fields.response_metadata = {};\n }\n super(fields);\n this.name = fields.name;\n if (fields.content === undefined && fields.contentBlocks !== undefined) {\n this.content = fields.contentBlocks as $InferMessageContent<\n TStructure,\n TRole\n >;\n this.response_metadata = {\n output_version: \"v1\",\n ...fields.response_metadata,\n };\n } else if (fields.content !== undefined) {\n this.content = fields.content ?? [];\n this.response_metadata = fields.response_metadata;\n } else {\n this.content = [] as $InferMessageContent<TStructure, TRole>;\n this.response_metadata = fields.response_metadata;\n }\n this.additional_kwargs = fields.additional_kwargs;\n this.id = fields.id;\n }\n\n /** Get text content of the message. */\n get text(): string {\n if (typeof this.content === \"string\") {\n return this.content;\n }\n if (!Array.isArray(this.content)) return \"\";\n return this.content\n .map((c) => {\n if (typeof c === \"string\") return c;\n if (c.type === \"text\") return c.text;\n return \"\";\n })\n .join(\"\");\n }\n\n get contentBlocks(): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock> =\n typeof this.content === \"string\"\n ? [{ type: \"text\", text: this.content }]\n : this.content;\n const parsingSteps = [\n convertToV1FromDataContent,\n convertToV1FromChatCompletionsInput,\n convertToV1FromAnthropicInput,\n ];\n const parsedBlocks = parsingSteps.reduce(\n (blocks, step) => step(blocks),\n blocks\n );\n return parsedBlocks as Array<ContentBlock.Standard>;\n }\n\n toDict(): StoredMessage {\n return {\n type: this.getType(),\n data: (this.toJSON() as SerializedConstructor)\n .kwargs as StoredMessageData,\n };\n }\n\n static lc_name() {\n return \"BaseMessage\";\n }\n\n // Can't be protected for silly reasons\n get _printableFields(): Record<string, unknown> {\n return {\n id: this.id,\n content: this.content,\n name: this.name,\n additional_kwargs: this.additional_kwargs,\n response_metadata: this.response_metadata,\n };\n }\n\n static isInstance(obj: unknown): obj is BaseMessage {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n MESSAGE_SYMBOL in obj &&\n obj[MESSAGE_SYMBOL] === true &&\n isMessage(obj)\n );\n }\n\n // this private method is used to update the ID for the runtime\n // value as well as in lc_kwargs for serialisation\n _updateId(value: string | undefined) {\n this.id = value;\n\n // lc_attributes wouldn't work here, because jest compares the\n // whole object\n this.lc_kwargs.id = value;\n }\n\n get [Symbol.toStringTag]() {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.constructor as any).lc_name();\n }\n\n // Override the default behavior of console.log\n [Symbol.for(\"nodejs.util.inspect.custom\")](depth: number | null) {\n if (depth === null) {\n return this;\n }\n const printable = stringifyWithDepthLimit(\n this._printableFields,\n Math.max(4, depth)\n );\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return `${(this.constructor as any).lc_name()} ${printable}`;\n }\n}\n\n/**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\nexport type OpenAIToolCall = {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: FunctionCall;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: \"function\";\n\n index?: number;\n};\n\nexport function isOpenAIToolCallArray(\n value?: unknown\n): value is OpenAIToolCall[] {\n return (\n Array.isArray(value) &&\n value.every((v) => typeof (v as OpenAIToolCall).index === \"number\")\n );\n}\n\nexport function _mergeDicts(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n left: Record<string, any>,\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n right: Record<string, any>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n const merged = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (merged[key] == null) {\n merged[key] = value;\n } else if (value == null) {\n continue;\n } else if (\n typeof merged[key] !== typeof value ||\n Array.isArray(merged[key]) !== Array.isArray(value)\n ) {\n throw new Error(\n `field[${key}] already exists in the message chunk, but with a different type.`\n );\n } else if (typeof merged[key] === \"string\") {\n if (key === \"type\") {\n // Do not merge 'type' fields\n continue;\n } else if ([\"id\", \"output_version\", \"model_provider\"].includes(key)) {\n // Keep the incoming value for these fields\n merged[key] = value;\n } else {\n merged[key] += value;\n }\n } else if (typeof merged[key] === \"object\" && !Array.isArray(merged[key])) {\n merged[key] = _mergeDicts(merged[key], value);\n } else if (Array.isArray(merged[key])) {\n merged[key] = _mergeLists(merged[key], value);\n } else if (merged[key] === value) {\n continue;\n } else {\n console.warn(\n `field[${key}] already exists in this message chunk and value has unsupported type.`\n );\n }\n }\n return merged;\n}\n\nexport function _mergeLists<Content extends ContentBlock>(\n left?: Content[],\n right?: Content[]\n): Content[] | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n } else if (left === undefined || right === undefined) {\n return left || right;\n } else {\n const merged = [...left];\n for (const item of right) {\n if (\n typeof item === \"object\" &&\n item !== null &&\n \"index\" in item &&\n typeof item.index === \"number\"\n ) {\n const toMerge = merged.findIndex(\n (leftItem) =>\n leftItem !== null &&\n typeof leftItem === \"object\" &&\n \"index\" in leftItem &&\n leftItem.index === item.index &&\n // Only merge if IDs match (or both are undefined)\n (\"id\" in leftItem && \"id\" in item\n ? leftItem.id === item.id\n : !(\"id\" in leftItem) && !(\"id\" in item))\n );\n if (\n toMerge !== -1 &&\n typeof merged[toMerge] === \"object\" &&\n merged[toMerge] !== null\n ) {\n merged[toMerge] = _mergeDicts(\n merged[toMerge] as Record<string, unknown>,\n item as Record<string, unknown>\n ) as Content;\n } else {\n merged.push(item);\n }\n } else if (\n typeof item === \"object\" &&\n item !== null &&\n \"text\" in item &&\n item.text === \"\"\n ) {\n // No-op - skip empty text blocks\n continue;\n } else {\n merged.push(item);\n }\n }\n return merged;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function _mergeObj<T = any>(\n left: T | undefined,\n right: T | undefined\n): T {\n if (!left && !right) {\n throw new Error(\"Cannot merge two undefined objects.\");\n }\n if (!left || !right) {\n return left || (right as T);\n } else if (typeof left !== typeof right) {\n throw new Error(\n `Cannot merge objects of different types.\\nLeft ${typeof left}\\nRight ${typeof right}`\n );\n } else if (typeof left === \"string\" && typeof right === \"string\") {\n return (left + right) as T;\n } else if (Array.isArray(left) && Array.isArray(right)) {\n return _mergeLists(left, right) as T;\n } else if (typeof left === \"object\" && typeof right === \"object\") {\n return _mergeDicts(left, right) as T;\n } else if (left === right) {\n return left;\n } else {\n throw new Error(\n `Can not merge objects of different types.\\nLeft ${left}\\nRight ${right}`\n );\n }\n}\n\n/**\n * Represents a chunk of a message, which can be concatenated with other\n * message chunks. It includes a method `_merge_kwargs_dict()` for merging\n * additional keyword arguments from another `BaseMessageChunk` into this\n * one. It also overrides the `__add__()` method to support concatenation\n * of `BaseMessageChunk` instances.\n */\nexport abstract class BaseMessageChunk<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> extends BaseMessage<TStructure, TRole> {\n abstract concat(chunk: BaseMessageChunk): BaseMessageChunk<TStructure, TRole>;\n\n static isInstance(obj: unknown): obj is BaseMessageChunk {\n return (\n super.isInstance(obj) &&\n \"concat\" in obj &&\n typeof obj.concat === \"function\"\n );\n }\n}\n\nexport type MessageFieldWithRole = {\n role: MessageType;\n content: MessageContent;\n name?: string;\n} & Record<string, unknown>;\n\nexport function _isMessageFieldWithRole(\n x: BaseMessageLike\n): x is MessageFieldWithRole {\n return typeof (x as MessageFieldWithRole).role === \"string\";\n}\n\nexport type BaseMessageLike =\n | BaseMessage\n | MessageFieldWithRole\n | [MessageType, MessageContent]\n | string\n /**\n * @deprecated Specifying \"type\" is deprecated and will be removed in 0.4.0.\n */\n | ({\n type: MessageType | \"user\" | \"assistant\" | \"placeholder\";\n } & BaseMessageFields &\n Record<string, unknown>)\n | SerializedConstructor;\n\n/**\n * @deprecated Use {@link BaseMessage.isInstance} instead\n */\nexport function isBaseMessage(\n messageLike?: unknown\n): messageLike is BaseMessage {\n return typeof (messageLike as BaseMessage)?._getType === \"function\";\n}\n\n/**\n * @deprecated Use {@link BaseMessageChunk.isInstance} instead\n */\nexport function isBaseMessageChunk(\n messageLike?: unknown\n): messageLike is BaseMessageChunk {\n return (\n isBaseMessage(messageLike) &&\n typeof (messageLike as BaseMessageChunk).concat === \"function\"\n );\n}\n"],"mappings":";;;;;;;;;AAgBA,MAAM,iBAAiB,OAAO,IAAI,oBAAoB;AAuEtD,SAAgB,aACdA,cACAC,eACgB;AAEhB,KAAI,OAAO,iBAAiB,UAAU;AACpC,MAAI,iBAAiB,GACnB,QAAO;AAET,MAAI,OAAO,kBAAkB,SAC3B,QAAO,eAAe;WAEtB,MAAM,QAAQ,cAAc,IAC5B,cAAc,KAAK,CAAC,MAAM,mBAAmB,EAAE,CAAC,CAEhD,QAAO,CACL;GACE,MAAM;GACN,aAAa;GACb,MAAM;EACP,GACD,GAAG,aACJ;MAED,QAAO,CAAC;GAAE,MAAM;GAAQ,MAAM;EAAc,GAAE,GAAG,aAAc;CA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AAO,OAAO;UAE/E,OAAO,SAAS,YAAY,OAAO,UAAU,SACtD,QAAQ,OAAO;UACN,MAAM,QAAQ,KAAK,IAAI,MAAM,QAAQ,MAAM,CACpD,QAAO,YAAY,MAAM,MAAM;UACtB,OAAO,SAAS,YAAY,OAAO,UAAU,SACtD,QAAO,YAAY,MAAM,MAAM;UACtB,SAAS,MAClB,QAAO;KAEP,OAAM,IAAI,MACR,CAAC,gDAAgD,EAAE,KAAK,QAAQ,EAAE,OAAO;AAG9E;;;;;;;;AASD,IAAsB,mBAAtB,cAGU,YAA+B;CAGvC,OAAO,WAAWT,KAAuC;AACvD,SACE,MAAM,WAAW,IAAI,IACrB,YAAY,OACZ,OAAO,IAAI,WAAW;CAEzB;AACF;AAQD,SAAgB,wBACdU,GAC2B;AAC3B,QAAO,OAAQ,EAA2B,SAAS;AACpD;;;;AAmBD,SAAgB,cACdC,aAC4B;AAC5B,QAAO,OAAQ,aAA6B,aAAa;AAC1D;;;;AAKD,SAAgB,mBACdA,aACiC;AACjC,QACE,cAAc,YAAY,IAC1B,OAAQ,YAAiC,WAAW;AAEvD"}
|
|
1
|
+
{"version":3,"file":"base.js","names":["firstContent: MessageContent","secondContent: MessageContent","left?: \"success\" | \"error\"","right?: \"success\" | \"error\"","obj: any","depthLimit: number","currentDepth: number","obj","result: Record<string, unknown>","arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>","fields: BaseMessageFields<TStructure, TRole>","blocks: Array<ContentBlock>","blocks","obj: unknown","value: string | undefined","depth: number | null","value?: unknown","left: Record<string, any>","right: Record<string, any>","left?: Content[]","right?: Content[]","left: T | undefined","right: T | undefined","x: BaseMessageLike","messageLike?: unknown"],"sources":["../../src/messages/base.ts"],"sourcesContent":["import { Serializable, SerializedConstructor } from \"../load/serializable.js\";\nimport { ContentBlock } from \"./content/index.js\";\nimport { isDataContentBlock } from \"./content/data.js\";\nimport { convertToV1FromAnthropicInput } from \"./block_translators/anthropic.js\";\nimport { convertToV1FromDataContent } from \"./block_translators/data.js\";\nimport { convertToV1FromChatCompletionsInput } from \"./block_translators/openai.js\";\nimport {\n $InferMessageContent,\n $InferResponseMetadata,\n MessageStructure,\n MessageType,\n isMessage,\n Message,\n} from \"./message.js\";\n\n/** @internal */\nconst MESSAGE_SYMBOL = Symbol.for(\"langchain.message\");\n\nexport interface StoredMessageData {\n content: string;\n role: string | undefined;\n name: string | undefined;\n tool_call_id: string | undefined;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n additional_kwargs?: Record<string, any>;\n /** Response metadata. For example: response headers, logprobs, token counts, model name. */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n response_metadata?: Record<string, any>;\n id?: string;\n}\n\nexport interface StoredMessage {\n type: string;\n data: StoredMessageData;\n}\n\nexport interface StoredGeneration {\n text: string;\n message?: StoredMessage;\n}\n\nexport interface StoredMessageV1 {\n type: string;\n role: string | undefined;\n text: string;\n}\n\nexport type MessageContent = string | Array<ContentBlock>;\n\nexport interface FunctionCall {\n /**\n * The arguments to call the function with, as generated by the model in JSON\n * format. Note that the model does not always generate valid JSON, and may\n * hallucinate parameters not defined by your function schema. Validate the\n * arguments in your code before calling your function.\n */\n arguments: string;\n\n /**\n * The name of the function to call.\n */\n name: string;\n}\n\nexport type BaseMessageFields<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> = {\n id?: string;\n name?: string;\n content?: $InferMessageContent<TStructure, TRole>;\n contentBlocks?: Array<ContentBlock.Standard>;\n /** @deprecated */\n additional_kwargs?: {\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n function_call?: FunctionCall;\n /**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\n tool_calls?: OpenAIToolCall[];\n [key: string]: unknown;\n };\n response_metadata?: Partial<$InferResponseMetadata<TStructure, TRole>>;\n};\n\nexport function mergeContent(\n firstContent: MessageContent,\n secondContent: MessageContent\n): MessageContent {\n // If first content is a string\n if (typeof firstContent === \"string\") {\n if (firstContent === \"\") {\n return secondContent;\n }\n if (typeof secondContent === \"string\") {\n return firstContent + secondContent;\n } else if (\n Array.isArray(secondContent) &&\n secondContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n {\n type: \"text\",\n source_type: \"text\",\n text: firstContent,\n },\n ...secondContent,\n ];\n } else {\n return [{ type: \"text\", text: firstContent }, ...secondContent];\n }\n // If both are arrays\n } else if (Array.isArray(secondContent)) {\n return (\n _mergeLists(firstContent, secondContent) ?? [\n ...firstContent,\n ...secondContent,\n ]\n );\n } else {\n if (secondContent === \"\") {\n return firstContent;\n } else if (\n Array.isArray(firstContent) &&\n firstContent.some((c) => isDataContentBlock(c))\n ) {\n return [\n ...firstContent,\n {\n type: \"file\",\n source_type: \"text\",\n text: secondContent,\n },\n ];\n } else {\n return [...firstContent, { type: \"text\", text: secondContent }];\n }\n }\n}\n\n/**\n * 'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else\n * it will return 'success'.\n *\n * @param {\"success\" | \"error\" | undefined} left The existing value to 'merge' with the new value.\n * @param {\"success\" | \"error\" | undefined} right The new value to 'merge' with the existing value\n * @returns {\"success\" | \"error\"} The 'merged' value.\n */\nexport function _mergeStatus(\n left?: \"success\" | \"error\",\n right?: \"success\" | \"error\"\n): \"success\" | \"error\" | undefined {\n if (left === \"error\" || right === \"error\") {\n return \"error\";\n }\n return \"success\";\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction stringifyWithDepthLimit(obj: any, depthLimit: number): string {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n function helper(obj: any, currentDepth: number): any {\n if (typeof obj !== \"object\" || obj === null || obj === undefined) {\n return obj;\n }\n if (currentDepth >= depthLimit) {\n if (Array.isArray(obj)) {\n return \"[Array]\";\n }\n return \"[Object]\";\n }\n\n if (Array.isArray(obj)) {\n return obj.map((item) => helper(item, currentDepth + 1));\n }\n\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(obj)) {\n result[key] = helper(obj[key], currentDepth + 1);\n }\n return result;\n }\n\n return JSON.stringify(helper(obj, 0), null, 2);\n}\n\n/**\n * Base class for all types of messages in a conversation. It includes\n * properties like `content`, `name`, and `additional_kwargs`. It also\n * includes methods like `toDict()` and `_getType()`.\n */\nexport abstract class BaseMessage<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n >\n extends Serializable\n implements Message<TStructure, TRole>\n{\n lc_namespace = [\"langchain_core\", \"messages\"];\n\n lc_serializable = true;\n\n get lc_aliases(): Record<string, string> {\n // exclude snake case conversion to pascal case\n return {\n additional_kwargs: \"additional_kwargs\",\n response_metadata: \"response_metadata\",\n };\n }\n\n readonly [MESSAGE_SYMBOL] = true as const;\n\n abstract readonly type: TRole;\n\n id?: string;\n\n name?: string;\n\n content: $InferMessageContent<TStructure, TRole>;\n\n additional_kwargs: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"additional_kwargs\"]\n >;\n\n response_metadata: NonNullable<\n BaseMessageFields<TStructure, TRole>[\"response_metadata\"]\n >;\n\n /**\n * @deprecated Use .getType() instead or import the proper typeguard.\n * For example:\n *\n * ```ts\n * import { isAIMessage } from \"@langchain/core/messages\";\n *\n * const message = new AIMessage(\"Hello!\");\n * isAIMessage(message); // true\n * ```\n */\n _getType(): MessageType {\n return this.type;\n }\n\n /**\n * @deprecated Use .type instead\n * The type of the message.\n */\n getType(): MessageType {\n return this._getType();\n }\n\n constructor(\n arg:\n | $InferMessageContent<TStructure, TRole>\n | BaseMessageFields<TStructure, TRole>\n ) {\n const fields: BaseMessageFields<TStructure, TRole> =\n typeof arg === \"string\" || Array.isArray(arg) ? { content: arg } : arg;\n if (!fields.additional_kwargs) {\n fields.additional_kwargs = {};\n }\n if (!fields.response_metadata) {\n fields.response_metadata = {};\n }\n super(fields);\n this.name = fields.name;\n if (fields.content === undefined && fields.contentBlocks !== undefined) {\n this.content = fields.contentBlocks as $InferMessageContent<\n TStructure,\n TRole\n >;\n this.response_metadata = {\n output_version: \"v1\",\n ...fields.response_metadata,\n };\n } else if (fields.content !== undefined) {\n this.content = fields.content ?? [];\n this.response_metadata = fields.response_metadata;\n } else {\n this.content = [] as $InferMessageContent<TStructure, TRole>;\n this.response_metadata = fields.response_metadata;\n }\n this.additional_kwargs = fields.additional_kwargs;\n this.id = fields.id;\n }\n\n /** Get text content of the message. */\n get text(): string {\n if (typeof this.content === \"string\") {\n return this.content;\n }\n if (!Array.isArray(this.content)) return \"\";\n return this.content\n .map((c) => {\n if (typeof c === \"string\") return c;\n if (c.type === \"text\") return c.text;\n return \"\";\n })\n .join(\"\");\n }\n\n get contentBlocks(): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock> =\n typeof this.content === \"string\"\n ? [{ type: \"text\", text: this.content }]\n : this.content;\n const parsingSteps = [\n convertToV1FromDataContent,\n convertToV1FromChatCompletionsInput,\n convertToV1FromAnthropicInput,\n ];\n const parsedBlocks = parsingSteps.reduce(\n (blocks, step) => step(blocks),\n blocks\n );\n return parsedBlocks as Array<ContentBlock.Standard>;\n }\n\n toDict(): StoredMessage {\n return {\n type: this.getType(),\n data: (this.toJSON() as SerializedConstructor)\n .kwargs as StoredMessageData,\n };\n }\n\n static lc_name() {\n return \"BaseMessage\";\n }\n\n // Can't be protected for silly reasons\n get _printableFields(): Record<string, unknown> {\n return {\n id: this.id,\n content: this.content,\n name: this.name,\n additional_kwargs: this.additional_kwargs,\n response_metadata: this.response_metadata,\n };\n }\n\n static isInstance(obj: unknown): obj is BaseMessage {\n return (\n typeof obj === \"object\" &&\n obj !== null &&\n MESSAGE_SYMBOL in obj &&\n obj[MESSAGE_SYMBOL] === true &&\n isMessage(obj)\n );\n }\n\n // this private method is used to update the ID for the runtime\n // value as well as in lc_kwargs for serialisation\n _updateId(value: string | undefined) {\n this.id = value;\n\n // lc_attributes wouldn't work here, because jest compares the\n // whole object\n this.lc_kwargs.id = value;\n }\n\n get [Symbol.toStringTag]() {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return (this.constructor as any).lc_name();\n }\n\n // Override the default behavior of console.log\n [Symbol.for(\"nodejs.util.inspect.custom\")](depth: number | null) {\n if (depth === null) {\n return this;\n }\n const printable = stringifyWithDepthLimit(\n this._printableFields,\n Math.max(4, depth)\n );\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return `${(this.constructor as any).lc_name()} ${printable}`;\n }\n}\n\n/**\n * @deprecated Use \"tool_calls\" field on AIMessages instead\n */\nexport type OpenAIToolCall = {\n /**\n * The ID of the tool call.\n */\n id: string;\n\n /**\n * The function that the model called.\n */\n function: FunctionCall;\n\n /**\n * The type of the tool. Currently, only `function` is supported.\n */\n type: \"function\";\n\n index?: number;\n};\n\nexport function isOpenAIToolCallArray(\n value?: unknown\n): value is OpenAIToolCall[] {\n return (\n Array.isArray(value) &&\n value.every((v) => typeof (v as OpenAIToolCall).index === \"number\")\n );\n}\n\nexport function _mergeDicts(\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n left: Record<string, any> = {},\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n right: Record<string, any> = {}\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n): Record<string, any> {\n const merged = { ...left };\n for (const [key, value] of Object.entries(right)) {\n if (merged[key] == null) {\n merged[key] = value;\n } else if (value == null) {\n continue;\n } else if (\n typeof merged[key] !== typeof value ||\n Array.isArray(merged[key]) !== Array.isArray(value)\n ) {\n throw new Error(\n `field[${key}] already exists in the message chunk, but with a different type.`\n );\n } else if (typeof merged[key] === \"string\") {\n if (key === \"type\") {\n // Do not merge 'type' fields\n continue;\n } else if (\n [\"id\", \"name\", \"output_version\", \"model_provider\"].includes(key)\n ) {\n // Keep the incoming value for these fields\n merged[key] = value;\n } else {\n merged[key] += value;\n }\n } else if (typeof merged[key] === \"object\" && !Array.isArray(merged[key])) {\n merged[key] = _mergeDicts(merged[key], value);\n } else if (Array.isArray(merged[key])) {\n merged[key] = _mergeLists(merged[key], value);\n } else if (merged[key] === value) {\n continue;\n } else {\n console.warn(\n `field[${key}] already exists in this message chunk and value has unsupported type.`\n );\n }\n }\n return merged;\n}\n\nexport function _mergeLists<Content extends ContentBlock>(\n left?: Content[],\n right?: Content[]\n): Content[] | undefined {\n if (left === undefined && right === undefined) {\n return undefined;\n } else if (left === undefined || right === undefined) {\n return left || right;\n } else {\n const merged = [...left];\n for (const item of right) {\n if (\n typeof item === \"object\" &&\n item !== null &&\n \"index\" in item &&\n typeof item.index === \"number\"\n ) {\n const toMerge = merged.findIndex((leftItem) => {\n const isObject = typeof leftItem === \"object\";\n const indiciesMatch =\n \"index\" in leftItem && leftItem.index === item.index;\n const idsMatch =\n \"id\" in leftItem && \"id\" in item && leftItem?.id === item?.id;\n const eitherItemMissingID =\n !(\"id\" in leftItem) ||\n !leftItem?.id ||\n !(\"id\" in item) ||\n !item?.id;\n return isObject && indiciesMatch && (idsMatch || eitherItemMissingID);\n });\n if (\n toMerge !== -1 &&\n typeof merged[toMerge] === \"object\" &&\n merged[toMerge] !== null\n ) {\n merged[toMerge] = _mergeDicts(\n merged[toMerge] as Record<string, unknown>,\n item as Record<string, unknown>\n ) as Content;\n } else {\n merged.push(item);\n }\n } else if (\n typeof item === \"object\" &&\n item !== null &&\n \"text\" in item &&\n item.text === \"\"\n ) {\n // No-op - skip empty text blocks\n continue;\n } else {\n merged.push(item);\n }\n }\n return merged;\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function _mergeObj<T = any>(\n left: T | undefined,\n right: T | undefined\n): T {\n if (!left && !right) {\n throw new Error(\"Cannot merge two undefined objects.\");\n }\n if (!left || !right) {\n return left || (right as T);\n } else if (typeof left !== typeof right) {\n throw new Error(\n `Cannot merge objects of different types.\\nLeft ${typeof left}\\nRight ${typeof right}`\n );\n } else if (typeof left === \"string\" && typeof right === \"string\") {\n return (left + right) as T;\n } else if (Array.isArray(left) && Array.isArray(right)) {\n return _mergeLists(left, right) as T;\n } else if (typeof left === \"object\" && typeof right === \"object\") {\n return _mergeDicts(left, right) as T;\n } else if (left === right) {\n return left;\n } else {\n throw new Error(\n `Can not merge objects of different types.\\nLeft ${left}\\nRight ${right}`\n );\n }\n}\n\n/**\n * Represents a chunk of a message, which can be concatenated with other\n * message chunks. It includes a method `_merge_kwargs_dict()` for merging\n * additional keyword arguments from another `BaseMessageChunk` into this\n * one. It also overrides the `__add__()` method to support concatenation\n * of `BaseMessageChunk` instances.\n */\nexport abstract class BaseMessageChunk<\n TStructure extends MessageStructure = MessageStructure,\n TRole extends MessageType = MessageType\n> extends BaseMessage<TStructure, TRole> {\n abstract concat(chunk: BaseMessageChunk): BaseMessageChunk<TStructure, TRole>;\n\n static isInstance(obj: unknown): obj is BaseMessageChunk {\n return (\n super.isInstance(obj) &&\n \"concat\" in obj &&\n typeof obj.concat === \"function\"\n );\n }\n}\n\nexport type MessageFieldWithRole = {\n role: MessageType;\n content: MessageContent;\n name?: string;\n} & Record<string, unknown>;\n\nexport function _isMessageFieldWithRole(\n x: BaseMessageLike\n): x is MessageFieldWithRole {\n return typeof (x as MessageFieldWithRole).role === \"string\";\n}\n\nexport type BaseMessageLike =\n | BaseMessage\n | MessageFieldWithRole\n | [MessageType, MessageContent]\n | string\n /**\n * @deprecated Specifying \"type\" is deprecated and will be removed in 0.4.0.\n */\n | ({\n type: MessageType | \"user\" | \"assistant\" | \"placeholder\";\n } & BaseMessageFields &\n Record<string, unknown>)\n | SerializedConstructor;\n\n/**\n * @deprecated Use {@link BaseMessage.isInstance} instead\n */\nexport function isBaseMessage(\n messageLike?: unknown\n): messageLike is BaseMessage {\n return typeof (messageLike as BaseMessage)?._getType === \"function\";\n}\n\n/**\n * @deprecated Use {@link BaseMessageChunk.isInstance} instead\n */\nexport function isBaseMessageChunk(\n messageLike?: unknown\n): messageLike is BaseMessageChunk {\n return (\n isBaseMessage(messageLike) &&\n typeof (messageLike as BaseMessageChunk).concat === \"function\"\n );\n}\n"],"mappings":";;;;;;;;;AAgBA,MAAM,iBAAiB,OAAO,IAAI,oBAAoB;AAuEtD,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
|
+
const require_utils = require('./utils.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/messages/block_translators/bedrock_converse.ts
|
|
4
|
+
function convertFileFormatToMimeType(format) {
|
|
5
|
+
switch (format) {
|
|
6
|
+
case "csv": return "text/csv";
|
|
7
|
+
case "doc": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
8
|
+
case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
9
|
+
case "html": return "text/html";
|
|
10
|
+
case "md": return "text/markdown";
|
|
11
|
+
case "pdf": return "application/pdf";
|
|
12
|
+
case "txt": return "text/plain";
|
|
13
|
+
case "xls": return "application/vnd.ms-excel";
|
|
14
|
+
case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
15
|
+
case "gif": return "image/gif";
|
|
16
|
+
case "jpeg": return "image/jpeg";
|
|
17
|
+
case "jpg": return "image/jpeg";
|
|
18
|
+
case "png": return "image/png";
|
|
19
|
+
case "webp": return "image/webp";
|
|
20
|
+
case "flv": return "video/flv";
|
|
21
|
+
case "mkv": return "video/mkv";
|
|
22
|
+
case "mov": return "video/mov";
|
|
23
|
+
case "mp4": return "video/mp4";
|
|
24
|
+
case "mpeg": return "video/mpeg";
|
|
25
|
+
case "mpg": return "video/mpg";
|
|
26
|
+
case "three_gp": return "video/three_gp";
|
|
27
|
+
case "webm": return "video/webm";
|
|
28
|
+
case "wmv": return "video/wmv";
|
|
29
|
+
default: return "application/octet-stream";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function convertConverseDocumentBlock(block) {
|
|
33
|
+
if (require_utils._isObject(block.document) && require_utils._isObject(block.document.source)) {
|
|
34
|
+
const format = require_utils._isObject(block.document) && require_utils._isString(block.document.format) ? block.document.format : "";
|
|
35
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
36
|
+
if (require_utils._isObject(block.document.source)) {
|
|
37
|
+
if (require_utils._isObject(block.document.source.s3Location) && require_utils._isString(block.document.source.s3Location.uri)) return {
|
|
38
|
+
type: "file",
|
|
39
|
+
mimeType,
|
|
40
|
+
fileId: block.document.source.s3Location.uri
|
|
41
|
+
};
|
|
42
|
+
if (require_utils._isBytesArray(block.document.source.bytes)) return {
|
|
43
|
+
type: "file",
|
|
44
|
+
mimeType,
|
|
45
|
+
data: block.document.source.bytes
|
|
46
|
+
};
|
|
47
|
+
if (require_utils._isString(block.document.source.text)) return {
|
|
48
|
+
type: "file",
|
|
49
|
+
mimeType,
|
|
50
|
+
data: Buffer.from(block.document.source.text).toString("base64")
|
|
51
|
+
};
|
|
52
|
+
if (require_utils._isArray(block.document.source.content)) {
|
|
53
|
+
const data = block.document.source.content.reduce((acc, item) => {
|
|
54
|
+
if (require_utils._isObject(item) && require_utils._isString(item.text)) return acc + item.text;
|
|
55
|
+
return acc;
|
|
56
|
+
}, "");
|
|
57
|
+
return {
|
|
58
|
+
type: "file",
|
|
59
|
+
mimeType,
|
|
60
|
+
data
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: "non_standard",
|
|
67
|
+
value: block
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function convertConverseImageBlock(block) {
|
|
71
|
+
if (require_utils._isContentBlock(block, "image") && require_utils._isObject(block.image)) {
|
|
72
|
+
const format = require_utils._isObject(block.image) && require_utils._isString(block.image.format) ? block.image.format : "";
|
|
73
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
74
|
+
if (require_utils._isObject(block.image.source)) {
|
|
75
|
+
if (require_utils._isObject(block.image.source.s3Location) && require_utils._isString(block.image.source.s3Location.uri)) return {
|
|
76
|
+
type: "image",
|
|
77
|
+
mimeType,
|
|
78
|
+
fileId: block.image.source.s3Location.uri
|
|
79
|
+
};
|
|
80
|
+
if (require_utils._isBytesArray(block.image.source.bytes)) return {
|
|
81
|
+
type: "image",
|
|
82
|
+
mimeType,
|
|
83
|
+
data: block.image.source.bytes
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
type: "non_standard",
|
|
89
|
+
value: block
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function convertConverseVideoBlock(block) {
|
|
93
|
+
if (require_utils._isContentBlock(block, "video") && require_utils._isObject(block.video)) {
|
|
94
|
+
const format = require_utils._isObject(block.video) && require_utils._isString(block.video.format) ? block.video.format : "";
|
|
95
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
96
|
+
if (require_utils._isObject(block.video.source)) {
|
|
97
|
+
if (require_utils._isObject(block.video.source.s3Location) && require_utils._isString(block.video.source.s3Location.uri)) return {
|
|
98
|
+
type: "video",
|
|
99
|
+
mimeType,
|
|
100
|
+
fileId: block.video.source.s3Location.uri
|
|
101
|
+
};
|
|
102
|
+
if (require_utils._isBytesArray(block.video.source.bytes)) return {
|
|
103
|
+
type: "video",
|
|
104
|
+
mimeType,
|
|
105
|
+
data: block.video.source.bytes
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
type: "non_standard",
|
|
111
|
+
value: block
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function convertToV1FromChatBedrockConverseMessage(message) {
|
|
115
|
+
function* iterateContent() {
|
|
116
|
+
const content = typeof message.content === "string" ? [{
|
|
117
|
+
type: "text",
|
|
118
|
+
text: message.content
|
|
119
|
+
}] : message.content;
|
|
120
|
+
for (const block of content) {
|
|
121
|
+
if (require_utils._isContentBlock(block, "cache_point")) {
|
|
122
|
+
yield {
|
|
123
|
+
type: "non_standard",
|
|
124
|
+
value: block
|
|
125
|
+
};
|
|
126
|
+
continue;
|
|
127
|
+
} else if (require_utils._isContentBlock(block, "citations_content") && require_utils._isObject(block.citationsContent)) {
|
|
128
|
+
const text = require_utils._isArray(block.citationsContent.content) ? block.citationsContent.content.reduce((acc, item) => {
|
|
129
|
+
if (require_utils._isObject(item) && require_utils._isString(item.text)) return acc + item.text;
|
|
130
|
+
return acc;
|
|
131
|
+
}, "") : "";
|
|
132
|
+
const annotations = require_utils._isArray(block.citationsContent.citations) ? block.citationsContent.citations.reduce((acc, item) => {
|
|
133
|
+
if (require_utils._isObject(item)) {
|
|
134
|
+
const citedText = require_utils._isArray(item.sourceContent) ? item.sourceContent.reduce((acc$1, item$1) => {
|
|
135
|
+
if (require_utils._isObject(item$1) && require_utils._isString(item$1.text)) return acc$1 + item$1.text;
|
|
136
|
+
return acc$1;
|
|
137
|
+
}, "") : "";
|
|
138
|
+
const properties = require_utils.iife(() => {
|
|
139
|
+
if (require_utils._isObject(item.location)) {
|
|
140
|
+
const location = item.location.documentChar || item.location.documentPage || item.location.documentChunk;
|
|
141
|
+
if (require_utils._isObject(location)) return {
|
|
142
|
+
source: require_utils._isNumber(location.documentIndex) ? location.documentIndex.toString() : void 0,
|
|
143
|
+
startIndex: require_utils._isNumber(location.start) ? location.start : void 0,
|
|
144
|
+
endIndex: require_utils._isNumber(location.end) ? location.end : void 0
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {};
|
|
148
|
+
});
|
|
149
|
+
acc.push({
|
|
150
|
+
type: "citation",
|
|
151
|
+
citedText,
|
|
152
|
+
...properties
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return acc;
|
|
156
|
+
}, []) : [];
|
|
157
|
+
yield {
|
|
158
|
+
type: "text",
|
|
159
|
+
text,
|
|
160
|
+
annotations
|
|
161
|
+
};
|
|
162
|
+
continue;
|
|
163
|
+
} else if (require_utils._isContentBlock(block, "document") && require_utils._isObject(block.document)) {
|
|
164
|
+
yield convertConverseDocumentBlock(block);
|
|
165
|
+
continue;
|
|
166
|
+
} else if (require_utils._isContentBlock(block, "guard_content")) {
|
|
167
|
+
yield {
|
|
168
|
+
type: "non_standard",
|
|
169
|
+
value: block
|
|
170
|
+
};
|
|
171
|
+
continue;
|
|
172
|
+
} else if (require_utils._isContentBlock(block, "image") && require_utils._isObject(block.image)) {
|
|
173
|
+
yield convertConverseImageBlock(block);
|
|
174
|
+
continue;
|
|
175
|
+
} else if (require_utils._isContentBlock(block, "reasoning_content") && require_utils._isString(block.reasoningText)) {
|
|
176
|
+
yield {
|
|
177
|
+
type: "reasoning",
|
|
178
|
+
reasoning: block.reasoningText
|
|
179
|
+
};
|
|
180
|
+
continue;
|
|
181
|
+
} else if (require_utils._isContentBlock(block, "text") && require_utils._isString(block.text)) {
|
|
182
|
+
yield {
|
|
183
|
+
type: "text",
|
|
184
|
+
text: block.text
|
|
185
|
+
};
|
|
186
|
+
continue;
|
|
187
|
+
} else if (require_utils._isContentBlock(block, "tool_result")) {
|
|
188
|
+
yield {
|
|
189
|
+
type: "non_standard",
|
|
190
|
+
value: block
|
|
191
|
+
};
|
|
192
|
+
continue;
|
|
193
|
+
} else if (require_utils._isContentBlock(block, "tool_call")) continue;
|
|
194
|
+
else if (require_utils._isContentBlock(block, "video") && require_utils._isObject(block.video)) {
|
|
195
|
+
yield convertConverseVideoBlock(block);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
yield {
|
|
199
|
+
type: "non_standard",
|
|
200
|
+
value: block
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return Array.from(iterateContent());
|
|
205
|
+
}
|
|
206
|
+
const ChatBedrockConverseTranslator = {
|
|
207
|
+
translateContent: convertToV1FromChatBedrockConverseMessage,
|
|
208
|
+
translateContentChunk: convertToV1FromChatBedrockConverseMessage
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
exports.ChatBedrockConverseTranslator = ChatBedrockConverseTranslator;
|
|
213
|
+
//# sourceMappingURL=bedrock_converse.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock_converse.cjs","names":["format: string","block: ContentBlock","_isObject","_isString","_isBytesArray","_isArray","acc: string","_isContentBlock","message: AIMessage","acc: Array<ContentBlock.Citation>","item","acc","iife","_isNumber","ChatBedrockConverseTranslator: StandardContentBlockTranslator"],"sources":["../../../src/messages/block_translators/bedrock_converse.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport { KNOWN_BLOCK_TYPES } from \"../content/tools.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport {\n _isArray,\n _isBytesArray,\n _isContentBlock,\n _isNumber,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n// see `/libs/providers/langchain-aws/src/utils/compat.ts:convertFileFormatToMimeType`\nfunction convertFileFormatToMimeType(format: string): string {\n switch (format) {\n // DocumentBlock\n case \"csv\":\n return \"text/csv\";\n case \"doc\":\n return \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\";\n case \"docx\":\n return \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\";\n case \"html\":\n return \"text/html\";\n case \"md\":\n return \"text/markdown\";\n case \"pdf\":\n return \"application/pdf\";\n case \"txt\":\n return \"text/plain\";\n case \"xls\":\n return \"application/vnd.ms-excel\";\n case \"xlsx\":\n return \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\";\n // ImageBlock\n case \"gif\":\n return \"image/gif\";\n case \"jpeg\":\n return \"image/jpeg\";\n case \"jpg\":\n return \"image/jpeg\";\n case \"png\":\n return \"image/png\";\n case \"webp\":\n return \"image/webp\";\n // VideoBlock\n case \"flv\":\n return \"video/flv\";\n case \"mkv\":\n return \"video/mkv\";\n case \"mov\":\n return \"video/mov\";\n case \"mp4\":\n return \"video/mp4\";\n case \"mpeg\":\n return \"video/mpeg\";\n case \"mpg\":\n return \"video/mpg\";\n case \"three_gp\":\n return \"video/three_gp\";\n case \"webm\":\n return \"video/webm\";\n case \"wmv\":\n return \"video/wmv\";\n default:\n return \"application/octet-stream\";\n }\n}\n\nfunction convertConverseDocumentBlock(\n block: ContentBlock\n): ContentBlock.Standard {\n if (_isObject(block.document) && _isObject(block.document.source)) {\n const format =\n _isObject(block.document) && _isString(block.document.format)\n ? block.document.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.document.source)) {\n if (\n _isObject(block.document.source.s3Location) &&\n _isString(block.document.source.s3Location.uri)\n ) {\n return {\n type: \"file\",\n mimeType,\n fileId: block.document.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.document.source.bytes)) {\n return {\n type: \"file\",\n mimeType,\n data: block.document.source.bytes,\n };\n }\n if (_isString(block.document.source.text)) {\n return {\n type: \"file\",\n mimeType,\n data: Buffer.from(block.document.source.text).toString(\"base64\"),\n };\n }\n if (_isArray(block.document.source.content)) {\n const data = block.document.source.content.reduce(\n (acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n },\n \"\"\n );\n return {\n type: \"file\",\n mimeType,\n data,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nfunction convertConverseImageBlock(block: ContentBlock): ContentBlock.Standard {\n if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n const format =\n _isObject(block.image) && _isString(block.image.format)\n ? block.image.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.image.source)) {\n if (\n _isObject(block.image.source.s3Location) &&\n _isString(block.image.source.s3Location.uri)\n ) {\n return {\n type: \"image\",\n mimeType,\n fileId: block.image.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.image.source.bytes)) {\n return {\n type: \"image\",\n mimeType,\n data: block.image.source.bytes,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nfunction convertConverseVideoBlock(block: ContentBlock): ContentBlock.Standard {\n if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n const format =\n _isObject(block.video) && _isString(block.video.format)\n ? block.video.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.video.source)) {\n if (\n _isObject(block.video.source.s3Location) &&\n _isString(block.video.source.s3Location.uri)\n ) {\n return {\n type: \"video\",\n mimeType,\n fileId: block.video.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.video.source.bytes)) {\n return {\n type: \"video\",\n mimeType,\n data: block.video.source.bytes,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nexport function convertToV1FromChatBedrockConverseInput(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n const blocks = content.map((block) => {\n if (_isContentBlock(block, \"non_standard\") && \"value\" in block) {\n return block.value as ContentBlock;\n }\n return block;\n });\n for (const block of blocks) {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n yield { type: \"text\", text: block.text };\n continue;\n } else if (\n _isContentBlock(block, \"document\") &&\n _isObject(block.document)\n ) {\n yield convertConverseDocumentBlock(block);\n continue;\n } else if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n yield convertConverseImageBlock(block);\n continue;\n } else if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n yield convertConverseVideoBlock(block);\n continue;\n }\n if (KNOWN_BLOCK_TYPES.includes(block.type)) {\n yield block as ContentBlock.Standard;\n } else {\n yield { type: \"non_standard\", value: block };\n }\n }\n }\n return Array.from(iterateContent());\n}\n\nfunction convertToV1FromChatBedrockConverseMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n // see `/libs/providers/langchain-aws/src/utils/message_outputs.ts:convertConverseMessageToLangChainMessage`\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"cache_point\")) {\n yield { type: \"non_standard\", value: block };\n continue;\n } else if (\n _isContentBlock(block, \"citations_content\") &&\n _isObject(block.citationsContent)\n ) {\n const text = _isArray(block.citationsContent.content)\n ? block.citationsContent.content.reduce((acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n }, \"\")\n : \"\";\n const annotations = _isArray(block.citationsContent.citations)\n ? block.citationsContent.citations.reduce(\n (acc: Array<ContentBlock.Citation>, item) => {\n if (_isObject(item)) {\n const citedText = _isArray(item.sourceContent)\n ? item.sourceContent.reduce((acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n }, \"\")\n : \"\";\n const properties = iife(() => {\n if (_isObject(item.location)) {\n const location =\n item.location.documentChar ||\n item.location.documentPage ||\n item.location.documentChunk;\n if (_isObject(location)) {\n return {\n source: _isNumber(location.documentIndex)\n ? location.documentIndex.toString()\n : undefined,\n startIndex: _isNumber(location.start)\n ? location.start\n : undefined,\n endIndex: _isNumber(location.end)\n ? location.end\n : undefined,\n };\n }\n }\n return {};\n });\n acc.push({ type: \"citation\", citedText, ...properties });\n }\n return acc;\n },\n []\n )\n : [];\n yield { type: \"text\", text, annotations };\n continue;\n } else if (\n _isContentBlock(block, \"document\") &&\n _isObject(block.document)\n ) {\n yield convertConverseDocumentBlock(block);\n continue;\n } else if (_isContentBlock(block, \"guard_content\")) {\n yield {\n type: \"non_standard\",\n value: block,\n };\n continue;\n } else if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n yield convertConverseImageBlock(block);\n continue;\n } else if (\n _isContentBlock(block, \"reasoning_content\") &&\n _isString(block.reasoningText)\n ) {\n yield {\n type: \"reasoning\",\n reasoning: block.reasoningText,\n };\n continue;\n } else if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n yield { type: \"text\", text: block.text };\n continue;\n } else if (_isContentBlock(block, \"tool_result\")) {\n yield { type: \"non_standard\", value: block };\n continue;\n } else if (_isContentBlock(block, \"tool_call\")) {\n // no-op - filtered to tools\n continue;\n } else if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n yield convertConverseVideoBlock(block);\n continue;\n }\n yield { type: \"non_standard\", value: block };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatBedrockConverseTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatBedrockConverseMessage,\n translateContentChunk: convertToV1FromChatBedrockConverseMessage,\n};\n"],"mappings":";;;AAeA,SAAS,4BAA4BA,QAAwB;AAC3D,SAAQ,QAAR;EAEE,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,KACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,WACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,QACE,QAAO;CACV;AACF;AAED,SAAS,6BACPC,OACuB;AACvB,KAAIC,wBAAU,MAAM,SAAS,IAAIA,wBAAU,MAAM,SAAS,OAAO,EAAE;EACjE,MAAM,SACJA,wBAAU,MAAM,SAAS,IAAIC,wBAAU,MAAM,SAAS,OAAO,GACzD,MAAM,SAAS,SACf;EACN,MAAM,WAAW,4BAA4B,OAAO;AAEpD,MAAID,wBAAU,MAAM,SAAS,OAAO,EAAE;AACpC,OACEA,wBAAU,MAAM,SAAS,OAAO,WAAW,IAC3CC,wBAAU,MAAM,SAAS,OAAO,WAAW,IAAI,CAE/C,QAAO;IACL,MAAM;IACN;IACA,QAAQ,MAAM,SAAS,OAAO,WAAW;GAC1C;AAEH,OAAIC,4BAAc,MAAM,SAAS,OAAO,MAAM,CAC5C,QAAO;IACL,MAAM;IACN;IACA,MAAM,MAAM,SAAS,OAAO;GAC7B;AAEH,OAAID,wBAAU,MAAM,SAA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O,YAAY,CAE5C;YACSA,8BAAgB,OAAO,QAAQ,IAAIL,wBAAU,MAAM,MAAM,EAAE;IACpE,MAAM,0BAA0B,MAAM;AACtC;GACD;GACD,MAAM;IAAE,MAAM;IAAgB,OAAO;GAAO;EAC7C;CACF;AACD,QAAO,MAAM,KAAK,gBAAgB,CAAC;AACpC;AAED,MAAaY,gCAAgE;CAC3E,kBAAkB;CAClB,uBAAuB;AACxB"}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { _isArray, _isBytesArray, _isContentBlock, _isNumber, _isObject, _isString, iife } from "./utils.js";
|
|
2
|
+
|
|
3
|
+
//#region src/messages/block_translators/bedrock_converse.ts
|
|
4
|
+
function convertFileFormatToMimeType(format) {
|
|
5
|
+
switch (format) {
|
|
6
|
+
case "csv": return "text/csv";
|
|
7
|
+
case "doc": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
8
|
+
case "docx": return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
9
|
+
case "html": return "text/html";
|
|
10
|
+
case "md": return "text/markdown";
|
|
11
|
+
case "pdf": return "application/pdf";
|
|
12
|
+
case "txt": return "text/plain";
|
|
13
|
+
case "xls": return "application/vnd.ms-excel";
|
|
14
|
+
case "xlsx": return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
15
|
+
case "gif": return "image/gif";
|
|
16
|
+
case "jpeg": return "image/jpeg";
|
|
17
|
+
case "jpg": return "image/jpeg";
|
|
18
|
+
case "png": return "image/png";
|
|
19
|
+
case "webp": return "image/webp";
|
|
20
|
+
case "flv": return "video/flv";
|
|
21
|
+
case "mkv": return "video/mkv";
|
|
22
|
+
case "mov": return "video/mov";
|
|
23
|
+
case "mp4": return "video/mp4";
|
|
24
|
+
case "mpeg": return "video/mpeg";
|
|
25
|
+
case "mpg": return "video/mpg";
|
|
26
|
+
case "three_gp": return "video/three_gp";
|
|
27
|
+
case "webm": return "video/webm";
|
|
28
|
+
case "wmv": return "video/wmv";
|
|
29
|
+
default: return "application/octet-stream";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function convertConverseDocumentBlock(block) {
|
|
33
|
+
if (_isObject(block.document) && _isObject(block.document.source)) {
|
|
34
|
+
const format = _isObject(block.document) && _isString(block.document.format) ? block.document.format : "";
|
|
35
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
36
|
+
if (_isObject(block.document.source)) {
|
|
37
|
+
if (_isObject(block.document.source.s3Location) && _isString(block.document.source.s3Location.uri)) return {
|
|
38
|
+
type: "file",
|
|
39
|
+
mimeType,
|
|
40
|
+
fileId: block.document.source.s3Location.uri
|
|
41
|
+
};
|
|
42
|
+
if (_isBytesArray(block.document.source.bytes)) return {
|
|
43
|
+
type: "file",
|
|
44
|
+
mimeType,
|
|
45
|
+
data: block.document.source.bytes
|
|
46
|
+
};
|
|
47
|
+
if (_isString(block.document.source.text)) return {
|
|
48
|
+
type: "file",
|
|
49
|
+
mimeType,
|
|
50
|
+
data: Buffer.from(block.document.source.text).toString("base64")
|
|
51
|
+
};
|
|
52
|
+
if (_isArray(block.document.source.content)) {
|
|
53
|
+
const data = block.document.source.content.reduce((acc, item) => {
|
|
54
|
+
if (_isObject(item) && _isString(item.text)) return acc + item.text;
|
|
55
|
+
return acc;
|
|
56
|
+
}, "");
|
|
57
|
+
return {
|
|
58
|
+
type: "file",
|
|
59
|
+
mimeType,
|
|
60
|
+
data
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: "non_standard",
|
|
67
|
+
value: block
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function convertConverseImageBlock(block) {
|
|
71
|
+
if (_isContentBlock(block, "image") && _isObject(block.image)) {
|
|
72
|
+
const format = _isObject(block.image) && _isString(block.image.format) ? block.image.format : "";
|
|
73
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
74
|
+
if (_isObject(block.image.source)) {
|
|
75
|
+
if (_isObject(block.image.source.s3Location) && _isString(block.image.source.s3Location.uri)) return {
|
|
76
|
+
type: "image",
|
|
77
|
+
mimeType,
|
|
78
|
+
fileId: block.image.source.s3Location.uri
|
|
79
|
+
};
|
|
80
|
+
if (_isBytesArray(block.image.source.bytes)) return {
|
|
81
|
+
type: "image",
|
|
82
|
+
mimeType,
|
|
83
|
+
data: block.image.source.bytes
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
type: "non_standard",
|
|
89
|
+
value: block
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function convertConverseVideoBlock(block) {
|
|
93
|
+
if (_isContentBlock(block, "video") && _isObject(block.video)) {
|
|
94
|
+
const format = _isObject(block.video) && _isString(block.video.format) ? block.video.format : "";
|
|
95
|
+
const mimeType = convertFileFormatToMimeType(format);
|
|
96
|
+
if (_isObject(block.video.source)) {
|
|
97
|
+
if (_isObject(block.video.source.s3Location) && _isString(block.video.source.s3Location.uri)) return {
|
|
98
|
+
type: "video",
|
|
99
|
+
mimeType,
|
|
100
|
+
fileId: block.video.source.s3Location.uri
|
|
101
|
+
};
|
|
102
|
+
if (_isBytesArray(block.video.source.bytes)) return {
|
|
103
|
+
type: "video",
|
|
104
|
+
mimeType,
|
|
105
|
+
data: block.video.source.bytes
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
type: "non_standard",
|
|
111
|
+
value: block
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function convertToV1FromChatBedrockConverseMessage(message) {
|
|
115
|
+
function* iterateContent() {
|
|
116
|
+
const content = typeof message.content === "string" ? [{
|
|
117
|
+
type: "text",
|
|
118
|
+
text: message.content
|
|
119
|
+
}] : message.content;
|
|
120
|
+
for (const block of content) {
|
|
121
|
+
if (_isContentBlock(block, "cache_point")) {
|
|
122
|
+
yield {
|
|
123
|
+
type: "non_standard",
|
|
124
|
+
value: block
|
|
125
|
+
};
|
|
126
|
+
continue;
|
|
127
|
+
} else if (_isContentBlock(block, "citations_content") && _isObject(block.citationsContent)) {
|
|
128
|
+
const text = _isArray(block.citationsContent.content) ? block.citationsContent.content.reduce((acc, item) => {
|
|
129
|
+
if (_isObject(item) && _isString(item.text)) return acc + item.text;
|
|
130
|
+
return acc;
|
|
131
|
+
}, "") : "";
|
|
132
|
+
const annotations = _isArray(block.citationsContent.citations) ? block.citationsContent.citations.reduce((acc, item) => {
|
|
133
|
+
if (_isObject(item)) {
|
|
134
|
+
const citedText = _isArray(item.sourceContent) ? item.sourceContent.reduce((acc$1, item$1) => {
|
|
135
|
+
if (_isObject(item$1) && _isString(item$1.text)) return acc$1 + item$1.text;
|
|
136
|
+
return acc$1;
|
|
137
|
+
}, "") : "";
|
|
138
|
+
const properties = iife(() => {
|
|
139
|
+
if (_isObject(item.location)) {
|
|
140
|
+
const location = item.location.documentChar || item.location.documentPage || item.location.documentChunk;
|
|
141
|
+
if (_isObject(location)) return {
|
|
142
|
+
source: _isNumber(location.documentIndex) ? location.documentIndex.toString() : void 0,
|
|
143
|
+
startIndex: _isNumber(location.start) ? location.start : void 0,
|
|
144
|
+
endIndex: _isNumber(location.end) ? location.end : void 0
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {};
|
|
148
|
+
});
|
|
149
|
+
acc.push({
|
|
150
|
+
type: "citation",
|
|
151
|
+
citedText,
|
|
152
|
+
...properties
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return acc;
|
|
156
|
+
}, []) : [];
|
|
157
|
+
yield {
|
|
158
|
+
type: "text",
|
|
159
|
+
text,
|
|
160
|
+
annotations
|
|
161
|
+
};
|
|
162
|
+
continue;
|
|
163
|
+
} else if (_isContentBlock(block, "document") && _isObject(block.document)) {
|
|
164
|
+
yield convertConverseDocumentBlock(block);
|
|
165
|
+
continue;
|
|
166
|
+
} else if (_isContentBlock(block, "guard_content")) {
|
|
167
|
+
yield {
|
|
168
|
+
type: "non_standard",
|
|
169
|
+
value: block
|
|
170
|
+
};
|
|
171
|
+
continue;
|
|
172
|
+
} else if (_isContentBlock(block, "image") && _isObject(block.image)) {
|
|
173
|
+
yield convertConverseImageBlock(block);
|
|
174
|
+
continue;
|
|
175
|
+
} else if (_isContentBlock(block, "reasoning_content") && _isString(block.reasoningText)) {
|
|
176
|
+
yield {
|
|
177
|
+
type: "reasoning",
|
|
178
|
+
reasoning: block.reasoningText
|
|
179
|
+
};
|
|
180
|
+
continue;
|
|
181
|
+
} else if (_isContentBlock(block, "text") && _isString(block.text)) {
|
|
182
|
+
yield {
|
|
183
|
+
type: "text",
|
|
184
|
+
text: block.text
|
|
185
|
+
};
|
|
186
|
+
continue;
|
|
187
|
+
} else if (_isContentBlock(block, "tool_result")) {
|
|
188
|
+
yield {
|
|
189
|
+
type: "non_standard",
|
|
190
|
+
value: block
|
|
191
|
+
};
|
|
192
|
+
continue;
|
|
193
|
+
} else if (_isContentBlock(block, "tool_call")) continue;
|
|
194
|
+
else if (_isContentBlock(block, "video") && _isObject(block.video)) {
|
|
195
|
+
yield convertConverseVideoBlock(block);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
yield {
|
|
199
|
+
type: "non_standard",
|
|
200
|
+
value: block
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return Array.from(iterateContent());
|
|
205
|
+
}
|
|
206
|
+
const ChatBedrockConverseTranslator = {
|
|
207
|
+
translateContent: convertToV1FromChatBedrockConverseMessage,
|
|
208
|
+
translateContentChunk: convertToV1FromChatBedrockConverseMessage
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
//#endregion
|
|
212
|
+
export { ChatBedrockConverseTranslator };
|
|
213
|
+
//# sourceMappingURL=bedrock_converse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bedrock_converse.js","names":["format: string","block: ContentBlock","acc: string","message: AIMessage","acc: Array<ContentBlock.Citation>","item","acc","ChatBedrockConverseTranslator: StandardContentBlockTranslator"],"sources":["../../../src/messages/block_translators/bedrock_converse.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport { KNOWN_BLOCK_TYPES } from \"../content/tools.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport {\n _isArray,\n _isBytesArray,\n _isContentBlock,\n _isNumber,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n// see `/libs/providers/langchain-aws/src/utils/compat.ts:convertFileFormatToMimeType`\nfunction convertFileFormatToMimeType(format: string): string {\n switch (format) {\n // DocumentBlock\n case \"csv\":\n return \"text/csv\";\n case \"doc\":\n return \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\";\n case \"docx\":\n return \"application/vnd.openxmlformats-officedocument.wordprocessingml.document\";\n case \"html\":\n return \"text/html\";\n case \"md\":\n return \"text/markdown\";\n case \"pdf\":\n return \"application/pdf\";\n case \"txt\":\n return \"text/plain\";\n case \"xls\":\n return \"application/vnd.ms-excel\";\n case \"xlsx\":\n return \"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\";\n // ImageBlock\n case \"gif\":\n return \"image/gif\";\n case \"jpeg\":\n return \"image/jpeg\";\n case \"jpg\":\n return \"image/jpeg\";\n case \"png\":\n return \"image/png\";\n case \"webp\":\n return \"image/webp\";\n // VideoBlock\n case \"flv\":\n return \"video/flv\";\n case \"mkv\":\n return \"video/mkv\";\n case \"mov\":\n return \"video/mov\";\n case \"mp4\":\n return \"video/mp4\";\n case \"mpeg\":\n return \"video/mpeg\";\n case \"mpg\":\n return \"video/mpg\";\n case \"three_gp\":\n return \"video/three_gp\";\n case \"webm\":\n return \"video/webm\";\n case \"wmv\":\n return \"video/wmv\";\n default:\n return \"application/octet-stream\";\n }\n}\n\nfunction convertConverseDocumentBlock(\n block: ContentBlock\n): ContentBlock.Standard {\n if (_isObject(block.document) && _isObject(block.document.source)) {\n const format =\n _isObject(block.document) && _isString(block.document.format)\n ? block.document.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.document.source)) {\n if (\n _isObject(block.document.source.s3Location) &&\n _isString(block.document.source.s3Location.uri)\n ) {\n return {\n type: \"file\",\n mimeType,\n fileId: block.document.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.document.source.bytes)) {\n return {\n type: \"file\",\n mimeType,\n data: block.document.source.bytes,\n };\n }\n if (_isString(block.document.source.text)) {\n return {\n type: \"file\",\n mimeType,\n data: Buffer.from(block.document.source.text).toString(\"base64\"),\n };\n }\n if (_isArray(block.document.source.content)) {\n const data = block.document.source.content.reduce(\n (acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n },\n \"\"\n );\n return {\n type: \"file\",\n mimeType,\n data,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nfunction convertConverseImageBlock(block: ContentBlock): ContentBlock.Standard {\n if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n const format =\n _isObject(block.image) && _isString(block.image.format)\n ? block.image.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.image.source)) {\n if (\n _isObject(block.image.source.s3Location) &&\n _isString(block.image.source.s3Location.uri)\n ) {\n return {\n type: \"image\",\n mimeType,\n fileId: block.image.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.image.source.bytes)) {\n return {\n type: \"image\",\n mimeType,\n data: block.image.source.bytes,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nfunction convertConverseVideoBlock(block: ContentBlock): ContentBlock.Standard {\n if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n const format =\n _isObject(block.video) && _isString(block.video.format)\n ? block.video.format\n : \"\";\n const mimeType = convertFileFormatToMimeType(format);\n\n if (_isObject(block.video.source)) {\n if (\n _isObject(block.video.source.s3Location) &&\n _isString(block.video.source.s3Location.uri)\n ) {\n return {\n type: \"video\",\n mimeType,\n fileId: block.video.source.s3Location.uri,\n };\n }\n if (_isBytesArray(block.video.source.bytes)) {\n return {\n type: \"video\",\n mimeType,\n data: block.video.source.bytes,\n };\n }\n }\n }\n return { type: \"non_standard\", value: block };\n}\n\nexport function convertToV1FromChatBedrockConverseInput(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n const blocks = content.map((block) => {\n if (_isContentBlock(block, \"non_standard\") && \"value\" in block) {\n return block.value as ContentBlock;\n }\n return block;\n });\n for (const block of blocks) {\n if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n yield { type: \"text\", text: block.text };\n continue;\n } else if (\n _isContentBlock(block, \"document\") &&\n _isObject(block.document)\n ) {\n yield convertConverseDocumentBlock(block);\n continue;\n } else if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n yield convertConverseImageBlock(block);\n continue;\n } else if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n yield convertConverseVideoBlock(block);\n continue;\n }\n if (KNOWN_BLOCK_TYPES.includes(block.type)) {\n yield block as ContentBlock.Standard;\n } else {\n yield { type: \"non_standard\", value: block };\n }\n }\n }\n return Array.from(iterateContent());\n}\n\nfunction convertToV1FromChatBedrockConverseMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n // see `/libs/providers/langchain-aws/src/utils/message_outputs.ts:convertConverseMessageToLangChainMessage`\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"cache_point\")) {\n yield { type: \"non_standard\", value: block };\n continue;\n } else if (\n _isContentBlock(block, \"citations_content\") &&\n _isObject(block.citationsContent)\n ) {\n const text = _isArray(block.citationsContent.content)\n ? block.citationsContent.content.reduce((acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n }, \"\")\n : \"\";\n const annotations = _isArray(block.citationsContent.citations)\n ? block.citationsContent.citations.reduce(\n (acc: Array<ContentBlock.Citation>, item) => {\n if (_isObject(item)) {\n const citedText = _isArray(item.sourceContent)\n ? item.sourceContent.reduce((acc: string, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return acc + item.text;\n }\n return acc;\n }, \"\")\n : \"\";\n const properties = iife(() => {\n if (_isObject(item.location)) {\n const location =\n item.location.documentChar ||\n item.location.documentPage ||\n item.location.documentChunk;\n if (_isObject(location)) {\n return {\n source: _isNumber(location.documentIndex)\n ? location.documentIndex.toString()\n : undefined,\n startIndex: _isNumber(location.start)\n ? location.start\n : undefined,\n endIndex: _isNumber(location.end)\n ? location.end\n : undefined,\n };\n }\n }\n return {};\n });\n acc.push({ type: \"citation\", citedText, ...properties });\n }\n return acc;\n },\n []\n )\n : [];\n yield { type: \"text\", text, annotations };\n continue;\n } else if (\n _isContentBlock(block, \"document\") &&\n _isObject(block.document)\n ) {\n yield convertConverseDocumentBlock(block);\n continue;\n } else if (_isContentBlock(block, \"guard_content\")) {\n yield {\n type: \"non_standard\",\n value: block,\n };\n continue;\n } else if (_isContentBlock(block, \"image\") && _isObject(block.image)) {\n yield convertConverseImageBlock(block);\n continue;\n } else if (\n _isContentBlock(block, \"reasoning_content\") &&\n _isString(block.reasoningText)\n ) {\n yield {\n type: \"reasoning\",\n reasoning: block.reasoningText,\n };\n continue;\n } else if (_isContentBlock(block, \"text\") && _isString(block.text)) {\n yield { type: \"text\", text: block.text };\n continue;\n } else if (_isContentBlock(block, \"tool_result\")) {\n yield { type: \"non_standard\", value: block };\n continue;\n } else if (_isContentBlock(block, \"tool_call\")) {\n // no-op - filtered to tools\n continue;\n } else if (_isContentBlock(block, \"video\") && _isObject(block.video)) {\n yield convertConverseVideoBlock(block);\n continue;\n }\n yield { type: \"non_standard\", value: block };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatBedrockConverseTranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromChatBedrockConverseMessage,\n translateContentChunk: convertToV1FromChatBedrockConverseMessage,\n};\n"],"mappings":";;;AAeA,SAAS,4BAA4BA,QAAwB;AAC3D,SAAQ,QAAR;EAEE,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,KACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EAET,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,KAAK,WACH,QAAO;EACT,KAAK,OACH,QAAO;EACT,KAAK,MACH,QAAO;EACT,QACE,QAAO;CACV;AACF;AAED,SAAS,6BACPC,OACuB;AACvB,KAAI,UAAU,MAAM,SAAS,IAAI,UAAU,MAAM,SAAS,OAAO,EAAE;EACjE,MAAM,SACJ,UAAU,MAAM,SAAS,IAAI,UAAU,MAAM,SAAS,OAAO,GACzD,MAAM,SAAS,SACf;EACN,MAAM,WAAW,4BAA4B,OAAO;AAEpD,MAAI,UAAU,MAAM,SAAS,OAAO,EAAE;AACpC,OACE,UAAU,MAAM,SAAS,OAAO,WAAW,IAC3C,UAAU,MAAM,SAAS,OAAO,WAAW,IAAI,CAE/C,QAAO;IACL,MAAM;IACN;IACA,QAAQ,MAAM,SAAS,OAAO,WAAW;GAC1C;AAEH,OAAI,cAAc,MAAM,SAAS,OAAO,MAAM,CAC5C,QAAO;IACL,MAAM;IACN;IACA,MAAM,MAAM,SAAS,OAAO;GAC7B;AAEH,OAAI,UAAU,MAAM,SAAS,OAAO,KAAK,CACvC,QAAO;I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