UNPKG

@langchain/core

Version:
1 lines 2.23 kB
{"version":3,"file":"format.cjs","names":[],"sources":["../../src/utils/format.ts"],"sourcesContent":["/**\n * A function that converts data from one format to another.\n *\n * This is commonly used for transforming message content blocks between different\n * provider-specific formats and standardized internal representations.\n *\n * @template T - The input type to convert from\n * @template U - The output type to convert to\n *\n * @param input - The data to convert\n * @returns The converted data in the target format\n *\n * @example\n * ```typescript\n * // Convert from OpenAI format to standard format\n * const converter: Converter<OpenAIBlock, ContentBlock.Standard> = (block) => {\n * return { type: \"text\", text: block.text };\n * };\n * ```\n */\nexport type Converter<T, U> = (input: T) => U;\n\n/**\n * A pair of bidirectional conversion functions for transforming data between two formats.\n *\n * This type is used throughout the message system to enable conversion between\n * provider-specific message formats (like OpenAI, Anthropic, Google, etc.) and\n * standardized internal content block representations. The `encode` function\n * typically converts from a standard format to a provider-specific format, while\n * `decode` converts from a provider-specific format back to the standard format.\n *\n * @template T - The first format (typically the standard/internal format)\n * @template U - The second format (typically the provider-specific format)\n *\n * @property encode - Converts from format T to format U\n * @property decode - Converts from format U back to format T\n *\n * @example\n * ```typescript\n * // Converter pair for OpenAI message blocks\n * const openAIConverter: ConverterPair<ContentBlock.Standard, OpenAIBlock> = {\n * encode: (standard) => ({ text: standard.text }),\n * decode: (openai) => ({ type: \"text\", text: openai.text })\n * };\n *\n * // Usage\n * const standardBlock = { type: \"text\", text: \"Hello\" };\n * const openaiBlock = openAIConverter.encode(standardBlock);\n * const backToStandard = openAIConverter.decode(openaiBlock);\n * ```\n */\nexport type ConverterPair<T, U> = {\n encode: Converter<T, U>;\n decode: Converter<U, T>;\n};\n"],"mappings":""}