UNPKG

@langchain/core

Version:
1 lines 5.11 kB
{"version":3,"file":"xai.cjs","names":["message: AIMessage","blocks: Array<ContentBlock.Standard>","_isObject","_isArray","_isString","ChatXAITranslator: StandardContentBlockTranslator"],"sources":["../../../src/messages/block_translators/xai.ts"],"sourcesContent":["import { AIMessage } from \"../ai.js\";\nimport { ContentBlock } from \"../content/index.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { _isArray, _isObject, _isString } from \"./utils.js\";\n\n/**\n * Converts an xAI AI message to an array of v1 standard content blocks.\n *\n * This function processes an AI message from xAI's API format\n * and converts it to the standardized v1 content block format. It handles\n * both the responses API (reasoning object with summary) and completions API\n * (reasoning_content string) formats.\n *\n * @param message - The AI message containing xAI-formatted content\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * // Responses API format\n * const message = new AIMessage({\n * content: \"The answer is 42\",\n * additional_kwargs: {\n * reasoning: {\n * id: \"reasoning_123\",\n * type: \"reasoning\",\n * summary: [{ type: \"summary_text\", text: \"Let me think...\" }]\n * }\n * }\n * });\n * const standardBlocks = convertToV1FromXAIMessage(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me think...\" },\n * // { type: \"text\", text: \"The answer is 42\" }\n * // ]\n * ```\n *\n * @example\n * ```typescript\n * // Completions API format\n * const message = new AIMessage({\n * content: \"The answer is 42\",\n * additional_kwargs: { reasoning_content: \"Let me think about this...\" }\n * });\n * const standardBlocks = convertToV1FromXAIMessage(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me think about this...\" },\n * // { type: \"text\", text: \"The answer is 42\" }\n * // ]\n * ```\n */\nexport function convertToV1FromXAIMessage(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n\n // Check for Responses API format: additional_kwargs.reasoning with summary array\n if (_isObject(message.additional_kwargs?.reasoning)) {\n const reasoning = message.additional_kwargs.reasoning;\n if (_isArray(reasoning.summary)) {\n const summaryText = reasoning.summary.reduce<string>((acc, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return `${acc}${item.text}`;\n }\n return acc;\n }, \"\");\n if (summaryText.length > 0) {\n blocks.push({\n type: \"reasoning\",\n reasoning: summaryText,\n });\n }\n }\n }\n\n // Check for Completions API format: additional_kwargs.reasoning_content\n const reasoningContent = message.additional_kwargs?.reasoning_content;\n if (_isString(reasoningContent) && reasoningContent.length > 0) {\n blocks.push({\n type: \"reasoning\",\n reasoning: reasoningContent,\n });\n }\n\n // Handle text content\n if (typeof message.content === \"string\") {\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n for (const block of message.content) {\n if (\n typeof block === \"object\" &&\n \"type\" in block &&\n block.type === \"text\" &&\n \"text\" in block &&\n _isString(block.text)\n ) {\n blocks.push({\n type: \"text\",\n text: block.text,\n });\n }\n }\n }\n\n // Add tool calls if present\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n\n return blocks;\n}\n\nexport const ChatXAITranslator: StandardContentBlockTranslator = {\n translateContent: convertToV1FromXAIMessage,\n translateContentChunk: convertToV1FromXAIMessage,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,SAAgB,0BACdA,SAC8B;CAC9B,MAAMC,SAAuC,CAAE;AAG/C,KAAIC,wBAAU,QAAQ,mBAAmB,UAAU,EAAE;EACnD,MAAM,YAAY,QAAQ,kBAAkB;AAC5C,MAAIC,uBAAS,UAAU,QAAQ,EAAE;GAC/B,MAAM,cAAc,UAAU,QAAQ,OAAe,CAAC,KAAK,SAAS;AAClE,QAAID,wBAAU,KAAK,IAAIE,wBAAU,KAAK,KAAK,CACzC,QAAO,GAAG,MAAM,KAAK,MAAM;AAE7B,WAAO;GACR,GAAE,GAAG;AACN,OAAI,YAAY,SAAS,GACvB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