@agentscope-ai/agentscope 0.0.5 → 0.0.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/agent/index.d.mts +4 -4
- package/dist/agent/index.d.ts +4 -4
- package/dist/agent/index.js +34 -7
- package/dist/agent/index.js.map +1 -1
- package/dist/agent/index.mjs +34 -7
- package/dist/agent/index.mjs.map +1 -1
- package/dist/{base-qmU135_k.d.ts → base-13VLaOvY.d.ts} +2 -2
- package/dist/{base-BI5s2ksj.d.mts → base-Bc3GkNS7.d.mts} +1 -1
- package/dist/{base-DHtZCg94.d.ts → base-CJkm56kB.d.ts} +1 -1
- package/dist/{base-CFDeoJRe.d.ts → base-Dfizi3RB.d.ts} +1 -1
- package/dist/{base-BDyDUIhj.d.mts → base-L72wZVx8.d.mts} +2 -2
- package/dist/{base-BB9eTlit.d.mts → base-Ps8E0j1_.d.mts} +1 -1
- package/dist/event/index.d.mts +2 -2
- package/dist/event/index.d.ts +2 -2
- package/dist/event/index.js.map +1 -1
- package/dist/event/index.mjs.map +1 -1
- package/dist/formatter/index.d.mts +2 -2
- package/dist/formatter/index.d.ts +2 -2
- package/dist/formatter/index.js +16 -0
- package/dist/formatter/index.js.map +1 -1
- package/dist/formatter/index.mjs +16 -0
- package/dist/formatter/index.mjs.map +1 -1
- package/dist/message/index.d.mts +1 -1
- package/dist/message/index.d.ts +1 -1
- package/dist/message/index.js +50 -14
- package/dist/message/index.js.map +1 -1
- package/dist/message/index.mjs +50 -14
- package/dist/message/index.mjs.map +1 -1
- package/dist/{message-D-LObC06.d.mts → message-COpNEf0G.d.mts} +10 -7
- package/dist/{message-DU0_qm3u.d.ts → message-DbCMy5tM.d.ts} +10 -7
- package/dist/model/index.d.mts +4 -4
- package/dist/model/index.d.ts +4 -4
- package/dist/model/index.js +16 -0
- package/dist/model/index.js.map +1 -1
- package/dist/model/index.mjs +16 -0
- package/dist/model/index.mjs.map +1 -1
- package/dist/storage/index.d.mts +2 -2
- package/dist/storage/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/agent/agent.ts +20 -7
- package/src/event/index.ts +2 -2
- package/src/message/append-event.test.ts +17 -11
- package/src/message/message.test.ts +2 -2
- package/src/message/message.ts +77 -14
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/message/message.ts","../../src/formatter/base.ts","../../src/formatter/dashscope-chat-formatter.ts","../../src/formatter/deepseek-chat-formatter.ts","../../src/formatter/ollama-chat-formatter.ts","../../src/formatter/openai-chat-formatter.ts"],"sourcesContent":["import { JSONSerializableObject } from '../type';\nimport {\n ContentBlock,\n TextBlock,\n ThinkingBlock,\n ToolResultBlock,\n ToolCallBlock,\n DataBlock,\n Base64Source,\n URLSource,\n} from './block';\nimport { AgentEvent, EventType } from '../event';\n\n/** A chat message exchanged between agents or between an agent and a model. */\nexport interface Msg {\n /** Unique identifier for the message. */\n id: string;\n /** Display name of the message sender. */\n name: string;\n /** Conversation role of the sender. */\n role: 'user' | 'assistant' | 'system';\n /** Message body. */\n content: ContentBlock[];\n /** Arbitrary key-value metadata attached to the message. */\n metadata: Record<string, JSONSerializableObject>;\n /** ISO-8601 creation timestamp. */\n created_at: string;\n /** ISO-8601 finished timestamp. */\n finished_at?: string | null;\n /** Usage information for the message, such as token counts. */\n usage?: {\n inputTokens: number;\n outputTokens: number;\n };\n}\n\n/**\n * Create a new {@link Msg} object, filling in `id` and `created_at` when omitted.\n * A plain string `content` is automatically wrapped in a single {@link TextBlock}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object.\n */\nexport function createMsg({\n name,\n content,\n role,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n usage,\n}: Omit<Msg, 'id' | 'created_at' | 'metadata' | 'content'> &\n Partial<Pick<Msg, 'id' | 'created_at' | 'metadata'>> & {\n content: string | ContentBlock[];\n }): Msg {\n const contentBlocks: ContentBlock[] =\n typeof content === 'string'\n ? [{ id: crypto.randomUUID(), type: 'text', text: content } as TextBlock]\n : content;\n return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };\n}\n\n/**\n * Create a user {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @returns A Msg object with role 'user'.\n */\nexport function UserMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n}): Msg {\n return createMsg({ name, content, role: 'user', metadata, id, created_at });\n}\n\n/**\n * Create an assistant {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.usage\n * @returns A Msg object with role 'assistant'.\n */\nexport function AssistantMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n usage,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n usage?: Msg['usage'];\n}): Msg {\n return createMsg({ name, content, role: 'assistant', metadata, id, created_at, usage });\n}\n\n/**\n * Create a system {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @returns A Msg object with role 'system'.\n */\nexport function SystemMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n}): Msg {\n return createMsg({ name, content, role: 'system', metadata, id, created_at });\n}\n\n/**\n * Extract the plain-text content from a message.\n *\n * When `content` is a string it is returned as-is. When it is an array of\n * content blocks, all {@link TextBlock} texts are joined with `separator`.\n *\n * @param msg - The message to read.\n * @param separator - String inserted between consecutive text blocks. Defaults to `'\\n'`.\n * @returns The concatenated text, or `null` when no text blocks are present.\n */\nexport function getTextContent(msg: Msg, separator: string = '\\n'): string | null {\n const textBlocks = msg.content.filter(block => block.type === 'text');\n if (textBlocks.length === 0) return null;\n return textBlocks.map(block => (block as TextBlock).text).join(separator);\n}\n\n/**\n * Return all content blocks from a message, regardless of type.\n *\n * When `content` is a plain string it is wrapped in a single {@link TextBlock}.\n *\n * @param msg - The message to read.\n * @returns An array of all {@link ContentBlock} objects.\n */\nexport function getContentBlocks(msg: Msg): ContentBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'text'): TextBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'thinking'): ThinkingBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'data'): DataBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_call'): ToolCallBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_result'): ToolResultBlock[];\nexport function getContentBlocks(\n msg: Msg,\n blockType?: 'text' | 'thinking' | 'data' | 'tool_call' | 'tool_result'\n): ContentBlock[] {\n if (!blockType) return msg.content;\n return msg.content.filter(block => block.type === blockType);\n}\n\n/**\n * Find a content block by type and id within a message.\n * @param msg\n * @param blockType\n * @param blockId\n * @returns The matching {@link ContentBlock}, or `undefined` if not found.\n */\nfunction findBlock(msg: Msg, blockType: string, blockId: string): ContentBlock | undefined {\n return msg.content.find(block => block.type === blockType && block.id === blockId);\n}\n\n/**\n * Apply a streaming {@link AgentEvent} to a {@link Msg}, mutating it in place.\n *\n * Only `content` and `finished_at` are ever modified. Events whose\n * `reply_id` does not match `msg.id` are skipped with a warning.\n * @param msg\n * @param event\n * @returns The mutated {@link Msg} object.\n */\nexport function appendEvent(msg: Msg, event: AgentEvent): Msg {\n if (!('reply_id' in event)) return msg;\n if (event.reply_id !== msg.id) {\n console.warn(\n `Event reply_id \"${event.reply_id}\" does not match message id \"${msg.id}\", skipping.`\n );\n return msg;\n }\n\n switch (event.type) {\n case EventType.REPLY_END:\n msg.finished_at = event.created_at;\n break;\n\n case EventType.TEXT_BLOCK_START:\n msg.content.push({ type: 'text', id: event.block_id, text: '' });\n break;\n\n case EventType.TEXT_BLOCK_DELTA: {\n const block = findBlock(msg, 'text', event.block_id);\n if (!block) {\n console.warn(`TextBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as TextBlock).text += event.delta;\n }\n break;\n }\n\n case EventType.TEXT_BLOCK_END:\n break;\n\n case EventType.THINKING_BLOCK_START:\n msg.content.push({ type: 'thinking', id: event.block_id, thinking: '' });\n break;\n\n case EventType.THINKING_BLOCK_DELTA: {\n const block = findBlock(msg, 'thinking', event.block_id);\n if (!block) {\n console.warn(`ThinkingBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as ThinkingBlock).thinking += event.delta;\n }\n break;\n }\n\n case EventType.THINKING_BLOCK_END:\n break;\n\n case EventType.DATA_BLOCK_START:\n msg.content.push({\n type: 'data',\n id: event.block_id,\n source: { type: 'base64', data: '', media_type: event.media_type },\n });\n break;\n\n case EventType.DATA_BLOCK_DELTA: {\n const block = findBlock(msg, 'data', event.block_id);\n if (!block) {\n console.warn(`DataBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n ((block as DataBlock).source as Base64Source).data += event.data;\n }\n break;\n }\n\n case EventType.DATA_BLOCK_END:\n break;\n\n case EventType.TOOL_CALL_START:\n msg.content.push({\n type: 'tool_call',\n id: event.tool_call_id,\n name: event.tool_call_name,\n input: '',\n state: 'pending',\n });\n break;\n\n case EventType.TOOL_CALL_DELTA: {\n const block = findBlock(msg, 'tool_call', event.tool_call_id);\n if (!block) {\n console.warn(`ToolCallBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolCallBlock).input += event.delta;\n }\n break;\n }\n\n case EventType.TOOL_CALL_END:\n break;\n\n case EventType.TOOL_RESULT_START:\n msg.content.push({\n type: 'tool_result',\n id: event.tool_call_id,\n name: event.tool_call_name,\n output: [],\n state: 'running',\n });\n break;\n\n case EventType.TOOL_RESULT_TEXT_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const last = trb.output[trb.output.length - 1];\n if (!last || last.type !== 'text') {\n trb.output.push({\n type: 'text',\n id: event.block_id ?? crypto.randomUUID(),\n text: event.delta,\n });\n } else {\n (last as TextBlock).text += event.delta;\n }\n }\n break;\n }\n\n case EventType.TOOL_RESULT_DATA_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const source: Base64Source | URLSource =\n event.data != null\n ? { type: 'base64', data: event.data, media_type: event.media_type }\n : { type: 'url', url: event.url!, media_type: event.media_type };\n trb.output.push({ type: 'data', id: event.block_id, source });\n }\n break;\n }\n\n case EventType.TOOL_RESULT_END: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolResultBlock).state = event.state;\n }\n break;\n }\n\n case EventType.MODEL_CALL_END:\n // Accumulated the input and output tokens here.\n if (msg.usage) {\n msg.usage.inputTokens += event.input_tokens;\n msg.usage.outputTokens += event.output_tokens;\n } else {\n msg.usage = {\n inputTokens: event.input_tokens,\n outputTokens: event.output_tokens,\n };\n }\n break;\n\n case EventType.REQUIRE_USER_CONFIRM:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) {\n (b as ToolCallBlock).state = 'asking';\n if (tc.suggested_rules !== undefined) {\n (b as ToolCallBlock).suggested_rules = tc.suggested_rules;\n }\n }\n }\n break;\n\n case EventType.USER_CONFIRM_RESULT:\n for (const result of event.confirm_results) {\n const b = findBlock(msg, 'tool_call', result.tool_call.id);\n if (b) {\n (b as ToolCallBlock).state = result.confirmed ? 'allowed' : 'finished';\n }\n }\n break;\n\n case EventType.REQUIRE_EXTERNAL_EXECUTION:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) (b as ToolCallBlock).state = 'submitted';\n }\n break;\n\n case EventType.EXTERNAL_EXECUTION_RESULT:\n for (const result of event.execution_results) {\n msg.content.push(result);\n }\n break;\n }\n\n return msg;\n}\n","import { Msg, TextBlock, DataBlock, createMsg } from '../message';\n\n/**\n * Base class for message formatters.\n */\nexport abstract class FormatterBase {\n /**\n * Format the input message objects into the required format by the API.\n *\n * @param root0\n * @param root0.msgs - An array of message objects to be formatted.\n * @returns A promise that resolves to an array of formatted message objects.\n */\n abstract format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]>;\n\n /**\n * Convert the tool output to string format for the LLM APIs that only accept text input. If\n * `promoteMultimodalToolResult` is true, the multimodal content will be promoted to be a user message with\n * \"<system-info></system-info>\" tags. Otherwise, the multimodal content will be saved to a storage and a URL link\n * will be provided in the text output.\n *\n * @param output - The tool output, which can be a string or an array of content blocks.\n * @param promoteMultimodalToolResult - Whether to promote the multimodal content to the prompt messages.\n * @returns An object containing the text output and an optional promoted message.\n */\n convertToolOutputToString(\n output: string | (TextBlock | DataBlock)[],\n promoteMultimodalToolResult: boolean | { image?: boolean; audio?: boolean; video?: boolean }\n ) {\n if (typeof output === 'string') return { text: output, promotedMsg: null };\n\n let textualOutput = [];\n\n const promotedData: { id: string; block: DataBlock }[] = [];\n\n for (const block of output) {\n switch (block.type) {\n case 'text':\n textualOutput.push(block.text);\n break;\n default:\n const type = block.source.media_type.split('/')[0];\n if (type !== 'image' && type !== 'audio' && type !== 'video') {\n console.log(\n `Unsupported media type '${block.source.media_type}' in tool output. Only image, audio and video are supported.`\n );\n break;\n }\n if (block.source.type === 'url') {\n textualOutput.push(\n `<system-info>One returned ${type} can be found at: ${block.source.url}</system-info>`\n );\n } else {\n // If we should promote the multimodal content to the prompt messages\n const shouldPromote =\n promoteMultimodalToolResult === true ||\n (typeof promoteMultimodalToolResult === 'object' &&\n promoteMultimodalToolResult[type]);\n\n if (shouldPromote) {\n // Create an ID for the multimodal content first, which should less than 10 characters\n const dataID = Math.random().toString(36).substring(2, 10);\n textualOutput.push(\n `<system-info>One returned ${type} is embedded with ID '${dataID}' and will be attached within '<system-info></system-info>' tags later.</system-info>`\n );\n\n // Record the promoted data\n promotedData.push({ id: dataID, block });\n } else {\n // TODO: save locally\n\n // Save to storage and provide URL link\n textualOutput.push(`The returned ${block.type} is stored locally.`);\n }\n }\n }\n }\n\n // Attach prefix and suffix system-info tags if there are promoted blocks\n const promotedBlocks: (TextBlock | DataBlock)[] = [];\n promotedData.forEach(({ id, block }) => {\n const type = block.source.media_type.split('/')[0];\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `<${type}_data id='${id}'>`,\n });\n promotedBlocks.push(block);\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `</${type}_data>\\n`,\n });\n });\n\n if (promotedBlocks.length > 0) {\n // The prefix\n const prefix =\n '<system-info>The multimodal contents returned from the tool call are as follows:\\n';\n\n if (promotedBlocks[0].type === 'text') {\n promotedBlocks[0].text = `${prefix}${promotedBlocks[0].text}`;\n } else {\n promotedBlocks.unshift({\n id: crypto.randomUUID(),\n type: 'text',\n text: `${prefix}`,\n });\n }\n\n // The suffix\n const lastBlock = promotedBlocks[promotedBlocks.length - 1];\n if (lastBlock.type === 'text') {\n promotedBlocks[promotedBlocks.length - 1] = {\n id: crypto.randomUUID(),\n type: 'text',\n text: `${lastBlock.text}</system-info>`,\n };\n } else {\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `</system-info>`,\n });\n }\n }\n\n return {\n text: textualOutput.join('\\n'),\n promotedMsg: createMsg({ name: 'user', content: promotedBlocks, role: 'user' }),\n };\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, TextBlock, getContentBlocks } from '../message';\nimport { DataBlock } from '../message';\n\ninterface DashScopeFormatterOptions {\n /**\n * Since DashScope API doesn't support multimodal tool outputs, this option indicates whether to\n * promote the multimodal tool results to the prompt messages, so that LLMs can see them.\n * Note you should ensure your model supports the corresponding modalities.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n *\n */\nexport class DashScopeChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initialize a DashScopeChatFormatter instance.\n *\n * @param promoteMultimodalToolResult - Since DashScope API doesn't support multimodal tool outputs, this option\n * indicates whether to promote the multimodal tool results to the prompt messages, so that LLMs can see them.\n * Note you should ensure your model supports the corresponding modalities.\n * @param promoteMultimodalToolResult.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: DashScopeFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input message objects into the required format by DashScope API.\n *\n * @param msgs - An array of Msg instances to be formatted.\n * @param msgs.msgs\n * @returns A promise that resolves to an array of formatted message objects.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n content: Record<string, unknown>[];\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n content: [],\n };\n\n // The cached messages that should be pushed after the current message, to keep the order of messages correct.\n const cachedMsgs = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n formattedMsg.content.push(this._formatTextBlock(block));\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg) {\n // Insert the promoted message into the array as the next message to be processed\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n formattedMsg.content.push(...this._formatMultimodalBlock(block));\n break;\n }\n }\n if (formattedMsg.content.length > 0 || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n // Process next message\n index++;\n }\n return formattedMsgs;\n }\n\n /**\n * Format a text content block into the required format.\n *\n * @param block - The text content block to format.\n * @returns An object representing the formatted text content.\n */\n _formatTextBlock(block: TextBlock) {\n return { text: block.text };\n }\n\n /**\n * Format a multimodal data block into the required format.\n * In DashScope API, the local file paths should be prefixed with \"file://\". URLs are kept unchanged.\n *\n * @param block - The multimodal content block to format.\n * @returns An object representing the formatted multimodal content.\n */\n _formatMultimodalBlock(block: DataBlock) {\n const type = block.source.media_type.split('/')[0];\n\n if (!['image', 'audio', 'video'].includes(type)) {\n console.log(\n `Skip unsupported media type ${block.source.media_type} in DashScopeChatFormatter. Only image, audio and video are supported.`\n );\n return [];\n }\n\n if (block.source.type === 'url') {\n return [{ [type]: block.source.url }];\n }\n\n return [\n {\n [type]: `data:${block.source.media_type};base64,${block.source.data}`,\n },\n ];\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, getContentBlocks } from '../message';\n\ninterface DeepSeekFormatterOptions {\n /**\n * Most LLM APIs don't support multimodal tool outputs, this option controls whether to\n * promote multimodal tool results to follow-up user messages.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n * Format AgentScope message objects into DeepSeek Chat Completions message format.\n */\nexport class DeepSeekChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initializes a new instance of the DeepSeekChatFormatter class.\n * @param root0\n * @param root0.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: DeepSeekFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input messages into the structure expected by DeepSeek Chat Completions API.\n * @param root0\n * @param root0.msgs\n * @returns An array of formatted message objects ready to be sent to the DeepSeek API.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n name: string;\n content: Record<string, unknown>[] | null;\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n name: msg.name,\n content: null,\n };\n const content: Record<string, unknown>[] = [];\n\n // Cache tool-result messages to keep the sequence right after current message.\n const cachedMsgs: Record<string, unknown>[] = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n content.push({\n type: 'text',\n text: block.text,\n });\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg?.content.length) {\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n console.warn(\n `DeepSeek models don't support multimodal data for now (2026-03), skip the data block in message content.`\n );\n break;\n }\n }\n\n if (content.length > 0) {\n formattedMsg.content = content;\n }\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n index++;\n }\n\n return formattedMsgs;\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, getContentBlocks, getTextContent } from '../message';\n\n/**\n * Format AgentScope message objects into Ollama Chat message format.\n * Ollama expects simple string content, not the multimodal array format.\n */\nexport class OllamaChatFormatter extends FormatterBase {\n // eslint-disable-next-line jsdoc/require-returns\n /**\n * Format messages for Ollama API\n * @param root0\n * @param root0.msgs\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n\n for (const msg of msgs) {\n const formattedMsg: {\n role: string;\n content: string;\n tool_calls?: {\n function: {\n name: string;\n arguments: Record<string, unknown>;\n };\n }[];\n } = {\n role: msg.role,\n content: '',\n };\n\n // Extract text content\n const textContent = getTextContent(msg);\n if (textContent) {\n formattedMsg.content = textContent;\n }\n\n // Handle tool calls\n const toolCalls = getContentBlocks(msg, 'tool_call');\n if (toolCalls.length > 0) {\n formattedMsg.tool_calls = toolCalls.map(toolCall => ({\n function: {\n name: toolCall.name,\n arguments: JSON.parse(toolCall.input),\n },\n }));\n }\n\n // Handle tool results\n const toolResults = getContentBlocks(msg, 'tool_result');\n for (const toolResult of toolResults) {\n const resultText = this.convertToolOutputToString(toolResult.output, false);\n formattedMsgs.push({\n role: 'tool',\n content: resultText.text,\n });\n }\n\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n }\n\n return formattedMsgs;\n }\n}\n","import { existsSync } from 'fs';\nimport { readFile } from 'fs/promises';\nimport { extname } from 'path';\nimport { fileURLToPath } from 'url';\n\nimport { FormatterBase } from './base';\nimport { DataBlock, Msg, TextBlock, getContentBlocks } from '../message';\n\ninterface OpenAIFormatterOptions {\n /**\n * Most LLM APIs don't support multimodal tool outputs, this option controls whether to\n * promote multimodal tool results to follow-up user messages.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n * Format AgentScope message objects into OpenAI Chat Completions message format.\n */\nexport class OpenAIChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initializes a new instance of the OpenAIChatFormatter class.\n * @param root0\n * @param root0.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: OpenAIFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input messages into OpenAI Chat Completions message format.\n * @param root0\n * @param root0.msgs\n * @returns An array of formatted messages compatible with OpenAI Chat Completions API.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n name: string;\n content: Record<string, unknown>[] | null;\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n name: msg.name,\n content: null,\n };\n const content: Record<string, unknown>[] = [];\n\n // Cache tool-result messages to keep the sequence right after current message.\n const cachedMsgs: Record<string, unknown>[] = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n content.push(this._formatTextBlock(block));\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg?.content.length) {\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n content.push(\n ...(await this._formatMultimodalBlock({ block, role: msg.role }))\n );\n break;\n }\n }\n\n if (content.length > 0) {\n formattedMsg.content = content;\n }\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n index++;\n }\n\n return formattedMsgs;\n }\n\n /**\n * Format a text block into OpenAI Chat Completions message content format.\n * @param block\n * @returns An object representing the formatted text block.\n */\n _formatTextBlock(block: TextBlock) {\n return {\n type: 'text',\n text: block.text,\n };\n }\n\n /**\n * Format a multimodal data block into OpenAI Chat Completions message content format.\n * @param root0\n * @param root0.block\n * @param root0.role\n * @returns The formatted content blocks\n */\n async _formatMultimodalBlock({\n block,\n role,\n }: {\n block: DataBlock;\n role: Msg['role'];\n }): Promise<Record<string, unknown>[]> {\n const type = block.source.media_type.split('/')[0];\n if (type === 'image') {\n return [\n {\n type: 'image_url',\n image_url: {\n url: await this._toOpenAIImageURL(block),\n },\n },\n ];\n }\n\n if (type === 'audio') {\n // Skip assistant output audio to avoid carrying generated audio back into next request.\n if (role === 'assistant') {\n return [];\n }\n return [\n {\n type: 'input_audio',\n input_audio: await this._toOpenAIAudioData(block),\n },\n ];\n }\n\n console.log(\n `Skip unsupported media type ${block.source.media_type} in OpenAIChatFormatter. Only image and audio are supported.`\n );\n return [];\n }\n\n /**\n * Convert the data block to an OpenAI compatible image URL.\n * @param block\n * @returns A promise that resolves to a string representing the image URL in a format compatible with OpenAI Chat Completions API.\n */\n protected async _toOpenAIImageURL(block: DataBlock): Promise<string> {\n if (block.source.type === 'base64') {\n return `data:${block.source.media_type};base64,${block.source.data}`;\n }\n\n const sourceUrl = block.source.url;\n if (sourceUrl.startsWith('http://') || sourceUrl.startsWith('https://')) {\n return sourceUrl;\n }\n if (sourceUrl.startsWith('data:')) {\n return sourceUrl;\n }\n\n const localPath = this._toLocalPath(sourceUrl);\n if (!localPath || !existsSync(localPath)) {\n throw new Error(`Image path not found: ${sourceUrl}`);\n }\n\n const ext = extname(localPath).toLowerCase();\n const supportedImageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp'];\n if (!supportedImageExtensions.includes(ext)) {\n throw new TypeError(\n `Unsupported image extension: ${ext}. Supported: ${supportedImageExtensions.join(', ')}`\n );\n }\n\n const file = await readFile(localPath);\n const mime = block.source.media_type || `image/${ext.slice(1)}`;\n return `data:${mime};base64,${file.toString('base64')}`;\n }\n\n /**\n * Converts a data block to OpenAI compatible audio data format.\n *\n * @param block - The data block containing audio information.\n * @returns A promise that resolves to an object with audio data and format.\n */\n protected async _toOpenAIAudioData(\n block: DataBlock\n ): Promise<{ data: string; format: 'wav' | 'mp3' }> {\n const supportedMediaTypes = new Map<string, 'wav' | 'mp3'>([\n ['audio/wav', 'wav'],\n ['audio/mp3', 'mp3'],\n ['audio/mpeg', 'mp3'],\n ]);\n\n if (block.source.type === 'base64') {\n const format = supportedMediaTypes.get(block.source.media_type);\n if (!format) {\n throw new TypeError(\n `Unsupported audio media type: ${block.source.media_type}, only audio/wav and audio/mp3 are supported.`\n );\n }\n return { data: block.source.data, format };\n }\n\n const sourceUrl = block.source.url;\n const localPath = this._toLocalPath(sourceUrl);\n let data: string;\n\n if (localPath && existsSync(localPath)) {\n const file = await readFile(localPath);\n data = file.toString('base64');\n } else if (sourceUrl.startsWith('http://') || sourceUrl.startsWith('https://')) {\n const response = await fetch(sourceUrl);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch audio from URL: ${sourceUrl} (${response.status})`\n );\n }\n const arr = await response.arrayBuffer();\n data = Buffer.from(arr).toString('base64');\n } else {\n throw new Error(\n `Unsupported audio source: ${sourceUrl}, it should be a local file path, file URL, or an HTTP URL.`\n );\n }\n\n const ext = extname(localPath || sourceUrl).toLowerCase();\n const extToFormat = new Map<string, 'wav' | 'mp3'>([\n ['.wav', 'wav'],\n ['.mp3', 'mp3'],\n ]);\n const format = extToFormat.get(ext);\n if (!format) {\n throw new TypeError(`Unsupported audio extension: ${ext}, wav and mp3 are supported.`);\n }\n\n return { data, format };\n }\n\n /**\n * Converts a URL or path to a local file path.\n *\n * @param urlOrPath - The URL or path to convert.\n * @returns The local file path, or null if not a local path.\n */\n protected _toLocalPath(urlOrPath: string) {\n if (urlOrPath.startsWith('file://')) {\n return fileURLToPath(urlOrPath);\n }\n if (!urlOrPath.includes('://')) {\n return urlOrPath;\n }\n return null;\n }\n}\n"],"mappings":";AAkDO,SAAS,UAAU;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AAAA,EACA;AACJ,GAGY;AACR,QAAM,gBACF,OAAO,YAAY,WACb,CAAC,EAAE,IAAI,OAAO,WAAW,GAAG,MAAM,QAAQ,MAAM,QAAQ,CAAc,IACtE;AACV,SAAO,EAAE,IAAI,MAAM,MAAM,SAAS,eAAe,UAAU,YAAY,aAAa,MAAM;AAC9F;AA6FO,SAAS,eAAe,KAAU,YAAoB,MAAqB;AAC9E,QAAM,aAAa,IAAI,QAAQ,OAAO,WAAS,MAAM,SAAS,MAAM;AACpE,MAAI,WAAW,WAAW,EAAG,QAAO;AACpC,SAAO,WAAW,IAAI,WAAU,MAAoB,IAAI,EAAE,KAAK,SAAS;AAC5E;AAgBO,SAAS,iBACZ,KACA,WACc;AACd,MA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AS,GAAG;AACpC,YAAM,OAAO,MAAM,SAAS,SAAS;AACrC,aAAO,KAAK,SAAS,QAAQ;AAAA,IACjC,WAAW,UAAU,WAAW,SAAS,KAAK,UAAU,WAAW,UAAU,GAAG;AAC5E,YAAM,WAAW,MAAM,MAAM,SAAS;AACtC,UAAI,CAAC,SAAS,IAAI;AACd,cAAM,IAAI;AAAA,UACN,mCAAmC,SAAS,KAAK,SAAS,MAAM;AAAA,QACpE;AAAA,MACJ;AACA,YAAM,MAAM,MAAM,SAAS,YAAY;AACvC,aAAO,OAAO,KAAK,GAAG,EAAE,SAAS,QAAQ;AAAA,IAC7C,OAAO;AACH,YAAM,IAAI;AAAA,QACN,6BAA6B,SAAS;AAAA,MAC1C;AAAA,IACJ;AAEA,UAAM,MAAM,QAAQ,aAAa,SAAS,EAAE,YAAY;AACxD,UAAM,cAAc,oBAAI,IAA2B;AAAA,MAC/C,CAAC,QAAQ,KAAK;AAAA,MACd,CAAC,QAAQ,KAAK;AAAA,IAClB,CAAC;AACD,UAAM,SAAS,YAAY,IAAI,GAAG;AAClC,QAAI,CAAC,QAAQ;AACT,YAAM,IAAI,UAAU,gCAAgC,GAAG,8BAA8B;AAAA,IACzF;AAEA,WAAO,EAAE,MAAM,OAAO;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,aAAa,WAAmB;AACtC,QAAI,UAAU,WAAW,SAAS,GAAG;AACjC,aAAO,cAAc,SAAS;AAAA,IAClC;AACA,QAAI,CAAC,UAAU,SAAS,KAAK,GAAG;AAC5B,aAAO;AAAA,IACX;AACA,WAAO;AAAA,EACX;AACJ;","names":["format"]}
|
|
1
|
+
{"version":3,"sources":["../../src/message/message.ts","../../src/formatter/base.ts","../../src/formatter/dashscope-chat-formatter.ts","../../src/formatter/deepseek-chat-formatter.ts","../../src/formatter/ollama-chat-formatter.ts","../../src/formatter/openai-chat-formatter.ts"],"sourcesContent":["import { JSONSerializableObject } from '../type';\nimport {\n ContentBlock,\n TextBlock,\n ThinkingBlock,\n ToolResultBlock,\n ToolCallBlock,\n DataBlock,\n Base64Source,\n URLSource,\n} from './block';\nimport { AgentEvent, EventType } from '../event';\n\n/** A chat message exchanged between agents or between an agent and a model. */\nexport interface Msg {\n /** Unique identifier for the message. */\n id: string;\n /** Display name of the message sender. */\n name: string;\n /** Conversation role of the sender. */\n role: 'user' | 'assistant' | 'system';\n /** Message body. */\n content: ContentBlock[];\n /** Arbitrary key-value metadata attached to the message. */\n metadata: Record<string, JSONSerializableObject>;\n /** ISO-8601 creation timestamp. */\n created_at: string;\n /** ISO-8601 finished timestamp. */\n finished_at?: string | null;\n /** Usage information for the message, such as token counts. */\n usage?: {\n input_tokens: number;\n output_tokens: number;\n };\n}\n\n/**\n * Create a new {@link Msg} object, filling in `id` and `created_at` when omitted.\n * A plain string `content` is automatically wrapped in a single {@link TextBlock}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object.\n */\n/**\n * Validate that content blocks are allowed for the given role.\n *\n * Mirrors the Python `_assert_user_content_blocks` / `_assert_system_content_blocks`\n * guards: user messages may only contain text or data blocks; system messages\n * may only contain text blocks; assistant messages accept any block type.\n * @param role\n * @param content\n */\nfunction assertContentBlocksForRole(role: Msg['role'], content: ContentBlock[]): void {\n if (role === 'user') {\n for (const block of content) {\n if (block.type !== 'text' && block.type !== 'data') {\n throw new Error('User message can only contain text blocks or data blocks.');\n }\n }\n } else if (role === 'system') {\n for (const block of content) {\n if (block.type !== 'text') {\n throw new Error('System message can only contain text blocks.');\n }\n }\n }\n}\n\n/**\n * createMsg is a low-level utility for constructing Msg objects with proper defaults and validation.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object with the specified properties, and defaults for any omitted fields.\n */\nexport function createMsg({\n name,\n content,\n role,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n usage,\n}: Omit<Msg, 'id' | 'created_at' | 'metadata' | 'content'> &\n Partial<Pick<Msg, 'id' | 'created_at' | 'metadata'>> & {\n content: string | ContentBlock[];\n }): Msg {\n const contentBlocks: ContentBlock[] =\n typeof content === 'string'\n ? [{ id: crypto.randomUUID(), type: 'text', text: content } as TextBlock]\n : content;\n assertContentBlocksForRole(role, contentBlocks);\n return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };\n}\n\n/**\n * Create a user {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @returns A Msg object with role 'user'.\n */\nexport function UserMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n finished_at?: string | null;\n}): Msg {\n return createMsg({\n name,\n content,\n role: 'user',\n metadata,\n id,\n created_at,\n finished_at: finished_at ?? created_at,\n });\n}\n\n/**\n * Create an assistant {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.usage\n * @returns A Msg object with role 'assistant'.\n */\nexport function AssistantMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n usage,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n usage?: Msg['usage'];\n}): Msg {\n return createMsg({ name, content, role: 'assistant', metadata, id, created_at, usage });\n}\n\n/**\n * Create a system {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @returns A Msg object with role 'system'.\n */\nexport function SystemMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n finished_at?: string | null;\n}): Msg {\n return createMsg({\n name,\n content,\n role: 'system',\n metadata,\n id,\n created_at,\n finished_at: finished_at ?? created_at,\n });\n}\n\n/**\n * Extract the plain-text content from a message.\n *\n * When `content` is a string it is returned as-is. When it is an array of\n * content blocks, all {@link TextBlock} texts are joined with `separator`.\n *\n * @param msg - The message to read.\n * @param separator - String inserted between consecutive text blocks. Defaults to `'\\n'`.\n * @returns The concatenated text, or `null` when no text blocks are present.\n */\nexport function getTextContent(msg: Msg, separator: string = '\\n'): string | null {\n const textBlocks = msg.content.filter(block => block.type === 'text');\n if (textBlocks.length === 0) return null;\n return textBlocks.map(block => (block as TextBlock).text).join(separator);\n}\n\n/**\n * Return all content blocks from a message, regardless of type.\n *\n * When `content` is a plain string it is wrapped in a single {@link TextBlock}.\n *\n * @param msg - The message to read.\n * @returns An array of all {@link ContentBlock} objects.\n */\nexport function getContentBlocks(msg: Msg): ContentBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'text'): TextBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'thinking'): ThinkingBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'data'): DataBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_call'): ToolCallBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_result'): ToolResultBlock[];\nexport function getContentBlocks(\n msg: Msg,\n blockType?: 'text' | 'thinking' | 'data' | 'tool_call' | 'tool_result'\n): ContentBlock[] {\n if (!blockType) return msg.content;\n return msg.content.filter(block => block.type === blockType);\n}\n\n/**\n * Find a content block by type and id within a message.\n * @param msg\n * @param blockType\n * @param blockId\n * @returns The matching {@link ContentBlock}, or `undefined` if not found.\n */\nfunction findBlock(msg: Msg, blockType: string, blockId: string): ContentBlock | undefined {\n return msg.content.find(block => block.type === blockType && block.id === blockId);\n}\n\n/**\n * Apply a streaming {@link AgentEvent} to a {@link Msg}, mutating it in place.\n *\n * Only `content` and `finished_at` are ever modified. Events whose\n * `reply_id` does not match `msg.id` are skipped with a warning.\n * @param msg\n * @param event\n * @returns The mutated {@link Msg} object.\n */\nexport function appendEvent(msg: Msg, event: AgentEvent): Msg {\n if (!('reply_id' in event)) return msg;\n if (event.reply_id !== msg.id) {\n console.warn(\n `Event reply_id \"${event.reply_id}\" does not match message id \"${msg.id}\", skipping.`\n );\n return msg;\n }\n\n switch (event.type) {\n case EventType.REPLY_END:\n msg.finished_at = event.created_at;\n break;\n\n case EventType.TEXT_BLOCK_START:\n msg.content.push({ type: 'text', id: event.block_id, text: '' });\n break;\n\n case EventType.TEXT_BLOCK_DELTA: {\n const block = findBlock(msg, 'text', event.block_id);\n if (!block) {\n console.warn(`TextBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as TextBlock).text += event.delta;\n }\n break;\n }\n\n case EventType.TEXT_BLOCK_END:\n break;\n\n case EventType.THINKING_BLOCK_START:\n msg.content.push({ type: 'thinking', id: event.block_id, thinking: '' });\n break;\n\n case EventType.THINKING_BLOCK_DELTA: {\n const block = findBlock(msg, 'thinking', event.block_id);\n if (!block) {\n console.warn(`ThinkingBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as ThinkingBlock).thinking += event.delta;\n }\n break;\n }\n\n case EventType.THINKING_BLOCK_END:\n break;\n\n case EventType.DATA_BLOCK_START:\n msg.content.push({\n type: 'data',\n id: event.block_id,\n source: { type: 'base64', data: '', media_type: event.media_type },\n });\n break;\n\n case EventType.DATA_BLOCK_DELTA: {\n const block = findBlock(msg, 'data', event.block_id);\n if (!block) {\n console.warn(`DataBlock \"${event.block_id}\" not found, skipping.`);\n } else if (event.data) {\n ((block as DataBlock).source as Base64Source).data += event.data;\n }\n break;\n }\n\n case EventType.DATA_BLOCK_END:\n break;\n\n case EventType.TOOL_CALL_START:\n msg.content.push({\n type: 'tool_call',\n id: event.tool_call_id,\n name: event.tool_call_name,\n input: '',\n state: 'pending',\n });\n break;\n\n case EventType.TOOL_CALL_DELTA: {\n const block = findBlock(msg, 'tool_call', event.tool_call_id);\n if (!block) {\n console.warn(`ToolCallBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolCallBlock).input += event.delta;\n }\n break;\n }\n\n case EventType.TOOL_CALL_END:\n break;\n\n case EventType.TOOL_RESULT_START:\n msg.content.push({\n type: 'tool_result',\n id: event.tool_call_id,\n name: event.tool_call_name,\n output: [],\n state: 'running',\n });\n break;\n\n case EventType.TOOL_RESULT_TEXT_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const last = trb.output[trb.output.length - 1];\n if (!last || last.type !== 'text') {\n trb.output.push({\n type: 'text',\n id: crypto.randomUUID(),\n text: event.delta,\n });\n } else {\n (last as TextBlock).text += event.delta;\n }\n }\n break;\n }\n\n case EventType.TOOL_RESULT_DATA_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const source: Base64Source | URLSource =\n event.data != null\n ? { type: 'base64', data: event.data, media_type: event.media_type }\n : { type: 'url', url: event.url!, media_type: event.media_type };\n trb.output.push({\n type: 'data',\n id: event.block_id ?? crypto.randomUUID(),\n source,\n });\n }\n break;\n }\n\n case EventType.TOOL_RESULT_END: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolResultBlock).state = event.state;\n }\n break;\n }\n\n case EventType.MODEL_CALL_END:\n // Accumulated the input and output tokens here.\n if (msg.usage) {\n msg.usage.input_tokens += event.input_tokens;\n msg.usage.output_tokens += event.output_tokens;\n } else {\n msg.usage = {\n input_tokens: event.input_tokens,\n output_tokens: event.output_tokens,\n };\n }\n break;\n\n case EventType.REQUIRE_USER_CONFIRM:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) {\n (b as ToolCallBlock).state = 'asking';\n (b as ToolCallBlock).suggested_rules = tc.suggested_rules || [];\n }\n }\n break;\n\n case EventType.USER_CONFIRM_RESULT:\n for (const result of event.confirm_results) {\n const b = findBlock(msg, 'tool_call', result.tool_call.id);\n if (b) {\n (b as ToolCallBlock).state = result.confirmed ? 'allowed' : 'finished';\n }\n }\n break;\n\n case EventType.REQUIRE_EXTERNAL_EXECUTION:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) (b as ToolCallBlock).state = 'submitted';\n }\n break;\n\n case EventType.EXTERNAL_EXECUTION_RESULT:\n for (const result of event.execution_results) {\n msg.content.push(result);\n }\n break;\n }\n\n return msg;\n}\n","import { Msg, TextBlock, DataBlock, createMsg } from '../message';\n\n/**\n * Base class for message formatters.\n */\nexport abstract class FormatterBase {\n /**\n * Format the input message objects into the required format by the API.\n *\n * @param root0\n * @param root0.msgs - An array of message objects to be formatted.\n * @returns A promise that resolves to an array of formatted message objects.\n */\n abstract format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]>;\n\n /**\n * Convert the tool output to string format for the LLM APIs that only accept text input. If\n * `promoteMultimodalToolResult` is true, the multimodal content will be promoted to be a user message with\n * \"<system-info></system-info>\" tags. Otherwise, the multimodal content will be saved to a storage and a URL link\n * will be provided in the text output.\n *\n * @param output - The tool output, which can be a string or an array of content blocks.\n * @param promoteMultimodalToolResult - Whether to promote the multimodal content to the prompt messages.\n * @returns An object containing the text output and an optional promoted message.\n */\n convertToolOutputToString(\n output: string | (TextBlock | DataBlock)[],\n promoteMultimodalToolResult: boolean | { image?: boolean; audio?: boolean; video?: boolean }\n ) {\n if (typeof output === 'string') return { text: output, promotedMsg: null };\n\n let textualOutput = [];\n\n const promotedData: { id: string; block: DataBlock }[] = [];\n\n for (const block of output) {\n switch (block.type) {\n case 'text':\n textualOutput.push(block.text);\n break;\n default:\n const type = block.source.media_type.split('/')[0];\n if (type !== 'image' && type !== 'audio' && type !== 'video') {\n console.log(\n `Unsupported media type '${block.source.media_type}' in tool output. Only image, audio and video are supported.`\n );\n break;\n }\n if (block.source.type === 'url') {\n textualOutput.push(\n `<system-info>One returned ${type} can be found at: ${block.source.url}</system-info>`\n );\n } else {\n // If we should promote the multimodal content to the prompt messages\n const shouldPromote =\n promoteMultimodalToolResult === true ||\n (typeof promoteMultimodalToolResult === 'object' &&\n promoteMultimodalToolResult[type]);\n\n if (shouldPromote) {\n // Create an ID for the multimodal content first, which should less than 10 characters\n const dataID = Math.random().toString(36).substring(2, 10);\n textualOutput.push(\n `<system-info>One returned ${type} is embedded with ID '${dataID}' and will be attached within '<system-info></system-info>' tags later.</system-info>`\n );\n\n // Record the promoted data\n promotedData.push({ id: dataID, block });\n } else {\n // TODO: save locally\n\n // Save to storage and provide URL link\n textualOutput.push(`The returned ${block.type} is stored locally.`);\n }\n }\n }\n }\n\n // Attach prefix and suffix system-info tags if there are promoted blocks\n const promotedBlocks: (TextBlock | DataBlock)[] = [];\n promotedData.forEach(({ id, block }) => {\n const type = block.source.media_type.split('/')[0];\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `<${type}_data id='${id}'>`,\n });\n promotedBlocks.push(block);\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `</${type}_data>\\n`,\n });\n });\n\n if (promotedBlocks.length > 0) {\n // The prefix\n const prefix =\n '<system-info>The multimodal contents returned from the tool call are as follows:\\n';\n\n if (promotedBlocks[0].type === 'text') {\n promotedBlocks[0].text = `${prefix}${promotedBlocks[0].text}`;\n } else {\n promotedBlocks.unshift({\n id: crypto.randomUUID(),\n type: 'text',\n text: `${prefix}`,\n });\n }\n\n // The suffix\n const lastBlock = promotedBlocks[promotedBlocks.length - 1];\n if (lastBlock.type === 'text') {\n promotedBlocks[promotedBlocks.length - 1] = {\n id: crypto.randomUUID(),\n type: 'text',\n text: `${lastBlock.text}</system-info>`,\n };\n } else {\n promotedBlocks.push({\n id: crypto.randomUUID(),\n type: 'text',\n text: `</system-info>`,\n });\n }\n }\n\n return {\n text: textualOutput.join('\\n'),\n promotedMsg: createMsg({ name: 'user', content: promotedBlocks, role: 'user' }),\n };\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, TextBlock, getContentBlocks } from '../message';\nimport { DataBlock } from '../message';\n\ninterface DashScopeFormatterOptions {\n /**\n * Since DashScope API doesn't support multimodal tool outputs, this option indicates whether to\n * promote the multimodal tool results to the prompt messages, so that LLMs can see them.\n * Note you should ensure your model supports the corresponding modalities.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n *\n */\nexport class DashScopeChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initialize a DashScopeChatFormatter instance.\n *\n * @param promoteMultimodalToolResult - Since DashScope API doesn't support multimodal tool outputs, this option\n * indicates whether to promote the multimodal tool results to the prompt messages, so that LLMs can see them.\n * Note you should ensure your model supports the corresponding modalities.\n * @param promoteMultimodalToolResult.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: DashScopeFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input message objects into the required format by DashScope API.\n *\n * @param msgs - An array of Msg instances to be formatted.\n * @param msgs.msgs\n * @returns A promise that resolves to an array of formatted message objects.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n content: Record<string, unknown>[];\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n content: [],\n };\n\n // The cached messages that should be pushed after the current message, to keep the order of messages correct.\n const cachedMsgs = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n formattedMsg.content.push(this._formatTextBlock(block));\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg) {\n // Insert the promoted message into the array as the next message to be processed\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n formattedMsg.content.push(...this._formatMultimodalBlock(block));\n break;\n }\n }\n if (formattedMsg.content.length > 0 || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n // Process next message\n index++;\n }\n return formattedMsgs;\n }\n\n /**\n * Format a text content block into the required format.\n *\n * @param block - The text content block to format.\n * @returns An object representing the formatted text content.\n */\n _formatTextBlock(block: TextBlock) {\n return { text: block.text };\n }\n\n /**\n * Format a multimodal data block into the required format.\n * In DashScope API, the local file paths should be prefixed with \"file://\". URLs are kept unchanged.\n *\n * @param block - The multimodal content block to format.\n * @returns An object representing the formatted multimodal content.\n */\n _formatMultimodalBlock(block: DataBlock) {\n const type = block.source.media_type.split('/')[0];\n\n if (!['image', 'audio', 'video'].includes(type)) {\n console.log(\n `Skip unsupported media type ${block.source.media_type} in DashScopeChatFormatter. Only image, audio and video are supported.`\n );\n return [];\n }\n\n if (block.source.type === 'url') {\n return [{ [type]: block.source.url }];\n }\n\n return [\n {\n [type]: `data:${block.source.media_type};base64,${block.source.data}`,\n },\n ];\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, getContentBlocks } from '../message';\n\ninterface DeepSeekFormatterOptions {\n /**\n * Most LLM APIs don't support multimodal tool outputs, this option controls whether to\n * promote multimodal tool results to follow-up user messages.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n * Format AgentScope message objects into DeepSeek Chat Completions message format.\n */\nexport class DeepSeekChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initializes a new instance of the DeepSeekChatFormatter class.\n * @param root0\n * @param root0.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: DeepSeekFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input messages into the structure expected by DeepSeek Chat Completions API.\n * @param root0\n * @param root0.msgs\n * @returns An array of formatted message objects ready to be sent to the DeepSeek API.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n name: string;\n content: Record<string, unknown>[] | null;\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n name: msg.name,\n content: null,\n };\n const content: Record<string, unknown>[] = [];\n\n // Cache tool-result messages to keep the sequence right after current message.\n const cachedMsgs: Record<string, unknown>[] = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n content.push({\n type: 'text',\n text: block.text,\n });\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg?.content.length) {\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n console.warn(\n `DeepSeek models don't support multimodal data for now (2026-03), skip the data block in message content.`\n );\n break;\n }\n }\n\n if (content.length > 0) {\n formattedMsg.content = content;\n }\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n index++;\n }\n\n return formattedMsgs;\n }\n}\n","import { FormatterBase } from './base';\nimport { Msg, getContentBlocks, getTextContent } from '../message';\n\n/**\n * Format AgentScope message objects into Ollama Chat message format.\n * Ollama expects simple string content, not the multimodal array format.\n */\nexport class OllamaChatFormatter extends FormatterBase {\n // eslint-disable-next-line jsdoc/require-returns\n /**\n * Format messages for Ollama API\n * @param root0\n * @param root0.msgs\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n\n for (const msg of msgs) {\n const formattedMsg: {\n role: string;\n content: string;\n tool_calls?: {\n function: {\n name: string;\n arguments: Record<string, unknown>;\n };\n }[];\n } = {\n role: msg.role,\n content: '',\n };\n\n // Extract text content\n const textContent = getTextContent(msg);\n if (textContent) {\n formattedMsg.content = textContent;\n }\n\n // Handle tool calls\n const toolCalls = getContentBlocks(msg, 'tool_call');\n if (toolCalls.length > 0) {\n formattedMsg.tool_calls = toolCalls.map(toolCall => ({\n function: {\n name: toolCall.name,\n arguments: JSON.parse(toolCall.input),\n },\n }));\n }\n\n // Handle tool results\n const toolResults = getContentBlocks(msg, 'tool_result');\n for (const toolResult of toolResults) {\n const resultText = this.convertToolOutputToString(toolResult.output, false);\n formattedMsgs.push({\n role: 'tool',\n content: resultText.text,\n });\n }\n\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n }\n\n return formattedMsgs;\n }\n}\n","import { existsSync } from 'fs';\nimport { readFile } from 'fs/promises';\nimport { extname } from 'path';\nimport { fileURLToPath } from 'url';\n\nimport { FormatterBase } from './base';\nimport { DataBlock, Msg, TextBlock, getContentBlocks } from '../message';\n\ninterface OpenAIFormatterOptions {\n /**\n * Most LLM APIs don't support multimodal tool outputs, this option controls whether to\n * promote multimodal tool results to follow-up user messages.\n */\n promoteMultimodalToolResult?:\n | {\n image?: boolean;\n audio?: boolean;\n video?: boolean;\n }\n | boolean;\n}\n\n/**\n * Format AgentScope message objects into OpenAI Chat Completions message format.\n */\nexport class OpenAIChatFormatter extends FormatterBase {\n private promoteMultimodalToolResult:\n | { image?: boolean; audio?: boolean; video?: boolean }\n | boolean;\n\n /**\n * Initializes a new instance of the OpenAIChatFormatter class.\n * @param root0\n * @param root0.promoteMultimodalToolResult\n */\n constructor({ promoteMultimodalToolResult = false }: OpenAIFormatterOptions = {}) {\n super();\n this.promoteMultimodalToolResult = promoteMultimodalToolResult;\n }\n\n /**\n * Format the input messages into OpenAI Chat Completions message format.\n * @param root0\n * @param root0.msgs\n * @returns An array of formatted messages compatible with OpenAI Chat Completions API.\n */\n async format({ msgs }: { msgs: Array<Msg> }): Promise<Record<string, unknown>[]> {\n const formattedMsgs: Array<Record<string, unknown>> = [];\n let index = 0;\n\n while (index < msgs.length) {\n const msg = msgs[index];\n const formattedMsg: {\n role: string;\n name: string;\n content: Record<string, unknown>[] | null;\n tool_calls?: {\n id: string;\n type: 'function';\n function: {\n name: string;\n arguments: string;\n };\n }[];\n } = {\n role: msg.role,\n name: msg.name,\n content: null,\n };\n const content: Record<string, unknown>[] = [];\n\n // Cache tool-result messages to keep the sequence right after current message.\n const cachedMsgs: Record<string, unknown>[] = [];\n for (const block of getContentBlocks(msg)) {\n switch (block.type) {\n case 'text':\n content.push(this._formatTextBlock(block));\n break;\n case 'thinking':\n break;\n case 'tool_call':\n if (!formattedMsg.tool_calls) {\n formattedMsg.tool_calls = [];\n }\n formattedMsg.tool_calls.push({\n id: block.id,\n type: 'function',\n function: {\n name: block.name,\n arguments: block.input,\n },\n });\n break;\n case 'tool_result':\n const formattedToolResult = this.convertToolOutputToString(\n block.output,\n this.promoteMultimodalToolResult\n );\n cachedMsgs.push({\n role: 'tool',\n tool_call_id: block.id,\n name: block.name,\n content: formattedToolResult.text,\n });\n if (formattedToolResult.promotedMsg?.content.length) {\n msgs.splice(index + 1, 0, formattedToolResult.promotedMsg);\n }\n break;\n case 'data':\n content.push(\n ...(await this._formatMultimodalBlock({ block, role: msg.role }))\n );\n break;\n }\n }\n\n if (content.length > 0) {\n formattedMsg.content = content;\n }\n if (formattedMsg.content || formattedMsg.tool_calls) {\n formattedMsgs.push(formattedMsg);\n }\n if (cachedMsgs.length > 0) {\n formattedMsgs.push(...cachedMsgs);\n }\n\n index++;\n }\n\n return formattedMsgs;\n }\n\n /**\n * Format a text block into OpenAI Chat Completions message content format.\n * @param block\n * @returns An object representing the formatted text block.\n */\n _formatTextBlock(block: TextBlock) {\n return {\n type: 'text',\n text: block.text,\n };\n }\n\n /**\n * Format a multimodal data block into OpenAI Chat Completions message content format.\n * @param root0\n * @param root0.block\n * @param root0.role\n * @returns The formatted content blocks\n */\n async _formatMultimodalBlock({\n block,\n role,\n }: {\n block: DataBlock;\n role: Msg['role'];\n }): Promise<Record<string, unknown>[]> {\n const type = block.source.media_type.split('/')[0];\n if (type === 'image') {\n return [\n {\n type: 'image_url',\n image_url: {\n url: await this._toOpenAIImageURL(block),\n },\n },\n ];\n }\n\n if (type === 'audio') {\n // Skip assistant output audio to avoid carrying generated audio back into next request.\n if (role === 'assistant') {\n return [];\n }\n return [\n {\n type: 'input_audio',\n input_audio: await this._toOpenAIAudioData(block),\n },\n ];\n }\n\n console.log(\n `Skip unsupported media type ${block.source.media_type} in OpenAIChatFormatter. Only image and audio are supported.`\n );\n return [];\n }\n\n /**\n * Convert the data block to an OpenAI compatible image URL.\n * @param block\n * @returns A promise that resolves to a string representing the image URL in a format compatible with OpenAI Chat Completions API.\n */\n protected async _toOpenAIImageURL(block: DataBlock): Promise<string> {\n if (block.source.type === 'base64') {\n return `data:${block.source.media_type};base64,${block.source.data}`;\n }\n\n const sourceUrl = block.source.url;\n if (sourceUrl.startsWith('http://') || sourceUrl.startsWith('https://')) {\n return sourceUrl;\n }\n if (sourceUrl.startsWith('data:')) {\n return sourceUrl;\n }\n\n const localPath = this._toLocalPath(sourceUrl);\n if (!localPath || !existsSync(localPath)) {\n throw new Error(`Image path not found: ${sourceUrl}`);\n }\n\n const ext = extname(localPath).toLowerCase();\n const supportedImageExtensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp'];\n if (!supportedImageExtensions.includes(ext)) {\n throw new TypeError(\n `Unsupported image extension: ${ext}. Supported: ${supportedImageExtensions.join(', ')}`\n );\n }\n\n const file = await readFile(localPath);\n const mime = block.source.media_type || `image/${ext.slice(1)}`;\n return `data:${mime};base64,${file.toString('base64')}`;\n }\n\n /**\n * Converts a data block to OpenAI compatible audio data format.\n *\n * @param block - The data block containing audio information.\n * @returns A promise that resolves to an object with audio data and format.\n */\n protected async _toOpenAIAudioData(\n block: DataBlock\n ): Promise<{ data: string; format: 'wav' | 'mp3' }> {\n const supportedMediaTypes = new Map<string, 'wav' | 'mp3'>([\n ['audio/wav', 'wav'],\n ['audio/mp3', 'mp3'],\n ['audio/mpeg', 'mp3'],\n ]);\n\n if (block.source.type === 'base64') {\n const format = supportedMediaTypes.get(block.source.media_type);\n if (!format) {\n throw new TypeError(\n `Unsupported audio media type: ${block.source.media_type}, only audio/wav and audio/mp3 are supported.`\n );\n }\n return { data: block.source.data, format };\n }\n\n const sourceUrl = block.source.url;\n const localPath = this._toLocalPath(sourceUrl);\n let data: string;\n\n if (localPath && existsSync(localPath)) {\n const file = await readFile(localPath);\n data = file.toString('base64');\n } else if (sourceUrl.startsWith('http://') || sourceUrl.startsWith('https://')) {\n const response = await fetch(sourceUrl);\n if (!response.ok) {\n throw new Error(\n `Failed to fetch audio from URL: ${sourceUrl} (${response.status})`\n );\n }\n const arr = await response.arrayBuffer();\n data = Buffer.from(arr).toString('base64');\n } else {\n throw new Error(\n `Unsupported audio source: ${sourceUrl}, it should be a local file path, file URL, or an HTTP URL.`\n );\n }\n\n const ext = extname(localPath || sourceUrl).toLowerCase();\n const extToFormat = new Map<string, 'wav' | 'mp3'>([\n ['.wav', 'wav'],\n ['.mp3', 'mp3'],\n ]);\n const format = extToFormat.get(ext);\n if (!format) {\n throw new TypeError(`Unsupported audio extension: ${ext}, wav and mp3 are supported.`);\n }\n\n return { data, format };\n }\n\n /**\n * Converts a URL or path to a local file path.\n *\n * @param urlOrPath - The URL or path to convert.\n * @returns The local file path, or null if not a local path.\n */\n protected _toLocalPath(urlOrPath: string) {\n if (urlOrPath.startsWith('file://')) {\n return fileURLToPath(urlOrPath);\n }\n if (!urlOrPath.includes('://')) {\n return urlOrPath;\n }\n return null;\n }\n}\n"],"mappings":";AA2DA,SAAS,2BAA2B,MAAmB,SAA+B;AAClF,MAAI,SAAS,QAAQ;AACjB,eAAW,SAAS,SAAS;AACzB,UAAI,MAAM,SAAS,UAAU,MAAM,SAAS,QAAQ;AAChD,cAAM,IAAI,MAAM,2DAA2D;AAAA,MAC/E;AAAA,IACJ;AAAA,EACJ,WAAW,SAAS,UAAU;AAC1B,eAAW,SAAS,SAAS;AACzB,UAAI,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SAAS,EAAE,YAAY;AACxD,UAAM,cAAc,oBAAI,IAA2B;AAAA,MAC/C,CAAC,QAAQ,KAAK;AAAA,MACd,CAAC,QAAQ,KAAK;AAAA,IAClB,CAAC;AACD,UAAM,SAAS,YAAY,IAAI,GAAG;AAClC,QAAI,CAAC,QAAQ;AACT,YAAM,IAAI,UAAU,gCAAgC,GAAG,8BAA8B;AAAA,IACzF;AAEA,WAAO,EAAE,MAAM,OAAO;AAAA,EAC1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQU,aAAa,WAAmB;AACtC,QAAI,UAAU,WAAW,SAAS,GAAG;AACjC,aAAO,cAAc,SAAS;AAAA,IAClC;AACA,QAAI,CAAC,UAAU,SAAS,KAAK,GAAG;AAC5B,aAAO;AAAA,IACX;AACA,WAAO;AAAA,EACX;AACJ;","names":["format"]}
|
package/dist/message/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AssistantMsg, M as Msg, S as SystemMsg, U as UserMsg, a as appendEvent, c as createMsg, g as getContentBlocks, b as getTextContent } from '../message-
|
|
1
|
+
export { A as AssistantMsg, M as Msg, S as SystemMsg, U as UserMsg, a as appendEvent, c as createMsg, g as getContentBlocks, b as getTextContent } from '../message-COpNEf0G.mjs';
|
|
2
2
|
export { B as Base64Source, C as ContentBlock, D as DataBlock, T as TextBlock, a as ThinkingBlock, b as ToolCallBlock, c as ToolCallState, d as ToolResultBlock, e as ToolResultState, U as URLSource } from '../block-BqWf-Qcb.mjs';
|
|
3
3
|
import '../index-CAxQAkiP.mjs';
|
|
4
4
|
import '../event/index.mjs';
|
package/dist/message/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AssistantMsg, M as Msg, S as SystemMsg, U as UserMsg, a as appendEvent, c as createMsg, g as getContentBlocks, b as getTextContent } from '../message-
|
|
1
|
+
export { A as AssistantMsg, M as Msg, S as SystemMsg, U as UserMsg, a as appendEvent, c as createMsg, g as getContentBlocks, b as getTextContent } from '../message-DbCMy5tM.js';
|
|
2
2
|
export { B as Base64Source, C as ContentBlock, D as DataBlock, T as TextBlock, a as ThinkingBlock, b as ToolCallBlock, c as ToolCallState, d as ToolResultBlock, e as ToolResultState, U as URLSource } from '../block-BqWf-Qcb.js';
|
|
3
3
|
import '../index-CAxQAkiP.js';
|
|
4
4
|
import '../event/index.js';
|
package/dist/message/index.js
CHANGED
|
@@ -32,6 +32,21 @@ __export(message_exports, {
|
|
|
32
32
|
module.exports = __toCommonJS(message_exports);
|
|
33
33
|
|
|
34
34
|
// src/message/message.ts
|
|
35
|
+
function assertContentBlocksForRole(role, content) {
|
|
36
|
+
if (role === "user") {
|
|
37
|
+
for (const block of content) {
|
|
38
|
+
if (block.type !== "text" && block.type !== "data") {
|
|
39
|
+
throw new Error("User message can only contain text blocks or data blocks.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else if (role === "system") {
|
|
43
|
+
for (const block of content) {
|
|
44
|
+
if (block.type !== "text") {
|
|
45
|
+
throw new Error("System message can only contain text blocks.");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
35
50
|
function createMsg({
|
|
36
51
|
name,
|
|
37
52
|
content,
|
|
@@ -43,6 +58,7 @@ function createMsg({
|
|
|
43
58
|
usage
|
|
44
59
|
}) {
|
|
45
60
|
const contentBlocks = typeof content === "string" ? [{ id: crypto.randomUUID(), type: "text", text: content }] : content;
|
|
61
|
+
assertContentBlocksForRole(role, contentBlocks);
|
|
46
62
|
return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };
|
|
47
63
|
}
|
|
48
64
|
function UserMsg({
|
|
@@ -50,9 +66,18 @@ function UserMsg({
|
|
|
50
66
|
content,
|
|
51
67
|
metadata = {},
|
|
52
68
|
id = crypto.randomUUID(),
|
|
53
|
-
created_at = (/* @__PURE__ */ new Date()).toISOString()
|
|
69
|
+
created_at = (/* @__PURE__ */ new Date()).toISOString(),
|
|
70
|
+
finished_at
|
|
54
71
|
}) {
|
|
55
|
-
return createMsg({
|
|
72
|
+
return createMsg({
|
|
73
|
+
name,
|
|
74
|
+
content,
|
|
75
|
+
role: "user",
|
|
76
|
+
metadata,
|
|
77
|
+
id,
|
|
78
|
+
created_at,
|
|
79
|
+
finished_at: finished_at ?? created_at
|
|
80
|
+
});
|
|
56
81
|
}
|
|
57
82
|
function AssistantMsg({
|
|
58
83
|
name,
|
|
@@ -69,9 +94,18 @@ function SystemMsg({
|
|
|
69
94
|
content,
|
|
70
95
|
metadata = {},
|
|
71
96
|
id = crypto.randomUUID(),
|
|
72
|
-
created_at = (/* @__PURE__ */ new Date()).toISOString()
|
|
97
|
+
created_at = (/* @__PURE__ */ new Date()).toISOString(),
|
|
98
|
+
finished_at
|
|
73
99
|
}) {
|
|
74
|
-
return createMsg({
|
|
100
|
+
return createMsg({
|
|
101
|
+
name,
|
|
102
|
+
content,
|
|
103
|
+
role: "system",
|
|
104
|
+
metadata,
|
|
105
|
+
id,
|
|
106
|
+
created_at,
|
|
107
|
+
finished_at: finished_at ?? created_at
|
|
108
|
+
});
|
|
75
109
|
}
|
|
76
110
|
function getTextContent(msg, separator = "\n") {
|
|
77
111
|
const textBlocks = msg.content.filter((block) => block.type === "text");
|
|
@@ -136,7 +170,7 @@ function appendEvent(msg, event) {
|
|
|
136
170
|
const block = findBlock(msg, "data", event.block_id);
|
|
137
171
|
if (!block) {
|
|
138
172
|
console.warn(`DataBlock "${event.block_id}" not found, skipping.`);
|
|
139
|
-
} else {
|
|
173
|
+
} else if (event.data) {
|
|
140
174
|
block.source.data += event.data;
|
|
141
175
|
}
|
|
142
176
|
break;
|
|
@@ -185,7 +219,7 @@ function appendEvent(msg, event) {
|
|
|
185
219
|
if (!last || last.type !== "text") {
|
|
186
220
|
trb.output.push({
|
|
187
221
|
type: "text",
|
|
188
|
-
id:
|
|
222
|
+
id: crypto.randomUUID(),
|
|
189
223
|
text: event.delta
|
|
190
224
|
});
|
|
191
225
|
} else {
|
|
@@ -204,7 +238,11 @@ function appendEvent(msg, event) {
|
|
|
204
238
|
trb.output = [{ type: "text", id: crypto.randomUUID(), text: trb.output }];
|
|
205
239
|
}
|
|
206
240
|
const source = event.data != null ? { type: "base64", data: event.data, media_type: event.media_type } : { type: "url", url: event.url, media_type: event.media_type };
|
|
207
|
-
trb.output.push({
|
|
241
|
+
trb.output.push({
|
|
242
|
+
type: "data",
|
|
243
|
+
id: event.block_id ?? crypto.randomUUID(),
|
|
244
|
+
source
|
|
245
|
+
});
|
|
208
246
|
}
|
|
209
247
|
break;
|
|
210
248
|
}
|
|
@@ -219,12 +257,12 @@ function appendEvent(msg, event) {
|
|
|
219
257
|
}
|
|
220
258
|
case "MODEL_CALL_END" /* MODEL_CALL_END */:
|
|
221
259
|
if (msg.usage) {
|
|
222
|
-
msg.usage.
|
|
223
|
-
msg.usage.
|
|
260
|
+
msg.usage.input_tokens += event.input_tokens;
|
|
261
|
+
msg.usage.output_tokens += event.output_tokens;
|
|
224
262
|
} else {
|
|
225
263
|
msg.usage = {
|
|
226
|
-
|
|
227
|
-
|
|
264
|
+
input_tokens: event.input_tokens,
|
|
265
|
+
output_tokens: event.output_tokens
|
|
228
266
|
};
|
|
229
267
|
}
|
|
230
268
|
break;
|
|
@@ -233,9 +271,7 @@ function appendEvent(msg, event) {
|
|
|
233
271
|
const b = findBlock(msg, "tool_call", tc.id);
|
|
234
272
|
if (b) {
|
|
235
273
|
b.state = "asking";
|
|
236
|
-
|
|
237
|
-
b.suggested_rules = tc.suggested_rules;
|
|
238
|
-
}
|
|
274
|
+
b.suggested_rules = tc.suggested_rules || [];
|
|
239
275
|
}
|
|
240
276
|
}
|
|
241
277
|
break;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/message/index.ts","../../src/message/message.ts","../../src/message/enums.ts"],"sourcesContent":["export {\n Msg,\n createMsg,\n UserMsg,\n AssistantMsg,\n SystemMsg,\n getTextContent,\n getContentBlocks,\n appendEvent,\n} from './message';\nexport {\n TextBlock,\n ThinkingBlock,\n ToolCallBlock,\n ToolCallState,\n ToolResultBlock,\n ToolResultState,\n ContentBlock,\n Base64Source,\n URLSource,\n DataBlock,\n} from './block';\nexport { GenerateReason } from './enums';\n","import { JSONSerializableObject } from '../type';\nimport {\n ContentBlock,\n TextBlock,\n ThinkingBlock,\n ToolResultBlock,\n ToolCallBlock,\n DataBlock,\n Base64Source,\n URLSource,\n} from './block';\nimport { AgentEvent, EventType } from '../event';\n\n/** A chat message exchanged between agents or between an agent and a model. */\nexport interface Msg {\n /** Unique identifier for the message. */\n id: string;\n /** Display name of the message sender. */\n name: string;\n /** Conversation role of the sender. */\n role: 'user' | 'assistant' | 'system';\n /** Message body. */\n content: ContentBlock[];\n /** Arbitrary key-value metadata attached to the message. */\n metadata: Record<string, JSONSerializableObject>;\n /** ISO-8601 creation timestamp. */\n created_at: string;\n /** ISO-8601 finished timestamp. */\n finished_at?: string | null;\n /** Usage information for the message, such as token counts. */\n usage?: {\n inputTokens: number;\n outputTokens: number;\n };\n}\n\n/**\n * Create a new {@link Msg} object, filling in `id` and `created_at` when omitted.\n * A plain string `content` is automatically wrapped in a single {@link TextBlock}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object.\n */\nexport function createMsg({\n name,\n content,\n role,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n usage,\n}: Omit<Msg, 'id' | 'created_at' | 'metadata' | 'content'> &\n Partial<Pick<Msg, 'id' | 'created_at' | 'metadata'>> & {\n content: string | ContentBlock[];\n }): Msg {\n const contentBlocks: ContentBlock[] =\n typeof content === 'string'\n ? [{ id: crypto.randomUUID(), type: 'text', text: content } as TextBlock]\n : content;\n return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };\n}\n\n/**\n * Create a user {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @returns A Msg object with role 'user'.\n */\nexport function UserMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n}): Msg {\n return createMsg({ name, content, role: 'user', metadata, id, created_at });\n}\n\n/**\n * Create an assistant {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.usage\n * @returns A Msg object with role 'assistant'.\n */\nexport function AssistantMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n usage,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n usage?: Msg['usage'];\n}): Msg {\n return createMsg({ name, content, role: 'assistant', metadata, id, created_at, usage });\n}\n\n/**\n * Create a system {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @returns A Msg object with role 'system'.\n */\nexport function SystemMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n}): Msg {\n return createMsg({ name, content, role: 'system', metadata, id, created_at });\n}\n\n/**\n * Extract the plain-text content from a message.\n *\n * When `content` is a string it is returned as-is. When it is an array of\n * content blocks, all {@link TextBlock} texts are joined with `separator`.\n *\n * @param msg - The message to read.\n * @param separator - String inserted between consecutive text blocks. Defaults to `'\\n'`.\n * @returns The concatenated text, or `null` when no text blocks are present.\n */\nexport function getTextContent(msg: Msg, separator: string = '\\n'): string | null {\n const textBlocks = msg.content.filter(block => block.type === 'text');\n if (textBlocks.length === 0) return null;\n return textBlocks.map(block => (block as TextBlock).text).join(separator);\n}\n\n/**\n * Return all content blocks from a message, regardless of type.\n *\n * When `content` is a plain string it is wrapped in a single {@link TextBlock}.\n *\n * @param msg - The message to read.\n * @returns An array of all {@link ContentBlock} objects.\n */\nexport function getContentBlocks(msg: Msg): ContentBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'text'): TextBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'thinking'): ThinkingBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'data'): DataBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_call'): ToolCallBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_result'): ToolResultBlock[];\nexport function getContentBlocks(\n msg: Msg,\n blockType?: 'text' | 'thinking' | 'data' | 'tool_call' | 'tool_result'\n): ContentBlock[] {\n if (!blockType) return msg.content;\n return msg.content.filter(block => block.type === blockType);\n}\n\n/**\n * Find a content block by type and id within a message.\n * @param msg\n * @param blockType\n * @param blockId\n * @returns The matching {@link ContentBlock}, or `undefined` if not found.\n */\nfunction findBlock(msg: Msg, blockType: string, blockId: string): ContentBlock | undefined {\n return msg.content.find(block => block.type === blockType && block.id === blockId);\n}\n\n/**\n * Apply a streaming {@link AgentEvent} to a {@link Msg}, mutating it in place.\n *\n * Only `content` and `finished_at` are ever modified. Events whose\n * `reply_id` does not match `msg.id` are skipped with a warning.\n * @param msg\n * @param event\n * @returns The mutated {@link Msg} object.\n */\nexport function appendEvent(msg: Msg, event: AgentEvent): Msg {\n if (!('reply_id' in event)) return msg;\n if (event.reply_id !== msg.id) {\n console.warn(\n `Event reply_id \"${event.reply_id}\" does not match message id \"${msg.id}\", skipping.`\n );\n return msg;\n }\n\n switch (event.type) {\n case EventType.REPLY_END:\n msg.finished_at = event.created_at;\n break;\n\n case EventType.TEXT_BLOCK_START:\n msg.content.push({ type: 'text', id: event.block_id, text: '' });\n break;\n\n case EventType.TEXT_BLOCK_DELTA: {\n const block = findBlock(msg, 'text', event.block_id);\n if (!block) {\n console.warn(`TextBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as TextBlock).text += event.delta;\n }\n break;\n }\n\n case EventType.TEXT_BLOCK_END:\n break;\n\n case EventType.THINKING_BLOCK_START:\n msg.content.push({ type: 'thinking', id: event.block_id, thinking: '' });\n break;\n\n case EventType.THINKING_BLOCK_DELTA: {\n const block = findBlock(msg, 'thinking', event.block_id);\n if (!block) {\n console.warn(`ThinkingBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as ThinkingBlock).thinking += event.delta;\n }\n break;\n }\n\n case EventType.THINKING_BLOCK_END:\n break;\n\n case EventType.DATA_BLOCK_START:\n msg.content.push({\n type: 'data',\n id: event.block_id,\n source: { type: 'base64', data: '', media_type: event.media_type },\n });\n break;\n\n case EventType.DATA_BLOCK_DELTA: {\n const block = findBlock(msg, 'data', event.block_id);\n if (!block) {\n console.warn(`DataBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n ((block as DataBlock).source as Base64Source).data += event.data;\n }\n break;\n }\n\n case EventType.DATA_BLOCK_END:\n break;\n\n case EventType.TOOL_CALL_START:\n msg.content.push({\n type: 'tool_call',\n id: event.tool_call_id,\n name: event.tool_call_name,\n input: '',\n state: 'pending',\n });\n break;\n\n case EventType.TOOL_CALL_DELTA: {\n const block = findBlock(msg, 'tool_call', event.tool_call_id);\n if (!block) {\n console.warn(`ToolCallBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolCallBlock).input += event.delta;\n }\n break;\n }\n\n case EventType.TOOL_CALL_END:\n break;\n\n case EventType.TOOL_RESULT_START:\n msg.content.push({\n type: 'tool_result',\n id: event.tool_call_id,\n name: event.tool_call_name,\n output: [],\n state: 'running',\n });\n break;\n\n case EventType.TOOL_RESULT_TEXT_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const last = trb.output[trb.output.length - 1];\n if (!last || last.type !== 'text') {\n trb.output.push({\n type: 'text',\n id: event.block_id ?? crypto.randomUUID(),\n text: event.delta,\n });\n } else {\n (last as TextBlock).text += event.delta;\n }\n }\n break;\n }\n\n case EventType.TOOL_RESULT_DATA_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const source: Base64Source | URLSource =\n event.data != null\n ? { type: 'base64', data: event.data, media_type: event.media_type }\n : { type: 'url', url: event.url!, media_type: event.media_type };\n trb.output.push({ type: 'data', id: event.block_id, source });\n }\n break;\n }\n\n case EventType.TOOL_RESULT_END: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolResultBlock).state = event.state;\n }\n break;\n }\n\n case EventType.MODEL_CALL_END:\n // Accumulated the input and output tokens here.\n if (msg.usage) {\n msg.usage.inputTokens += event.input_tokens;\n msg.usage.outputTokens += event.output_tokens;\n } else {\n msg.usage = {\n inputTokens: event.input_tokens,\n outputTokens: event.output_tokens,\n };\n }\n break;\n\n case EventType.REQUIRE_USER_CONFIRM:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) {\n (b as ToolCallBlock).state = 'asking';\n if (tc.suggested_rules !== undefined) {\n (b as ToolCallBlock).suggested_rules = tc.suggested_rules;\n }\n }\n }\n break;\n\n case EventType.USER_CONFIRM_RESULT:\n for (const result of event.confirm_results) {\n const b = findBlock(msg, 'tool_call', result.tool_call.id);\n if (b) {\n (b as ToolCallBlock).state = result.confirmed ? 'allowed' : 'finished';\n }\n }\n break;\n\n case EventType.REQUIRE_EXTERNAL_EXECUTION:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) (b as ToolCallBlock).state = 'submitted';\n }\n break;\n\n case EventType.EXTERNAL_EXECUTION_RESULT:\n for (const result of event.execution_results) {\n msg.content.push(result);\n }\n break;\n }\n\n return msg;\n}\n","export enum GenerateReason {\n AWAITING_TOOL_RESULT = 'AWAITING_TOOL_RESULT',\n AWAITING_USER_CONFIRMATION = 'AWAITING_USER_CONFIRMATION',\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkDO,SAAS,UAAU;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AAAA,EACA;AACJ,GAGY;AACR,QAAM,gBACF,OAAO,YAAY,WACb,CAAC,EAAE,IAAI,OAAO,WAAW,GAAG,MAAM,QAAQ,MAAM,QAAQ,CAAc,IACtE;AACV,SAAO,EAAE,IAAI,MAAM,MAAM,SAAS,eAAe,UAAU,YAAY,aAAa,MAAM;AAC9F;AAYO,SAAS,QAAQ;AAAA,EACpB;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,cAAa,oBAAI,KAAK,GAAE,YAAY;AACxC,GAMQ;AACJ,SAAO,UAAU,EAAE,MAAM,SAAS,MAAM,QAAQ,UAAU,IAAI,WAAW,CAAC;AAC9E;AAaO,SAAS,aAAa;AAAA,EACzB;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AACJ,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;AAAA,IAEJ;AACI,iBAAW,UAAU,MAAM,mBAAmB;AAC1C,YAAI,QAAQ,KAAK,MAAM;AAAA,MAC3B;AACA;AAAA,EACR;AAEA,SAAO;AACX;;;AC3ZO,IAAK,iBAAL,kBAAKA,oBAAL;AACH,EAAAA,gBAAA,0BAAuB;AACvB,EAAAA,gBAAA,gCAA6B;AAFrB,SAAAA;AAAA,GAAA;","names":["GenerateReason"]}
|
|
1
|
+
{"version":3,"sources":["../../src/message/index.ts","../../src/message/message.ts","../../src/message/enums.ts"],"sourcesContent":["export {\n Msg,\n createMsg,\n UserMsg,\n AssistantMsg,\n SystemMsg,\n getTextContent,\n getContentBlocks,\n appendEvent,\n} from './message';\nexport {\n TextBlock,\n ThinkingBlock,\n ToolCallBlock,\n ToolCallState,\n ToolResultBlock,\n ToolResultState,\n ContentBlock,\n Base64Source,\n URLSource,\n DataBlock,\n} from './block';\nexport { GenerateReason } from './enums';\n","import { JSONSerializableObject } from '../type';\nimport {\n ContentBlock,\n TextBlock,\n ThinkingBlock,\n ToolResultBlock,\n ToolCallBlock,\n DataBlock,\n Base64Source,\n URLSource,\n} from './block';\nimport { AgentEvent, EventType } from '../event';\n\n/** A chat message exchanged between agents or between an agent and a model. */\nexport interface Msg {\n /** Unique identifier for the message. */\n id: string;\n /** Display name of the message sender. */\n name: string;\n /** Conversation role of the sender. */\n role: 'user' | 'assistant' | 'system';\n /** Message body. */\n content: ContentBlock[];\n /** Arbitrary key-value metadata attached to the message. */\n metadata: Record<string, JSONSerializableObject>;\n /** ISO-8601 creation timestamp. */\n created_at: string;\n /** ISO-8601 finished timestamp. */\n finished_at?: string | null;\n /** Usage information for the message, such as token counts. */\n usage?: {\n input_tokens: number;\n output_tokens: number;\n };\n}\n\n/**\n * Create a new {@link Msg} object, filling in `id` and `created_at` when omitted.\n * A plain string `content` is automatically wrapped in a single {@link TextBlock}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object.\n */\n/**\n * Validate that content blocks are allowed for the given role.\n *\n * Mirrors the Python `_assert_user_content_blocks` / `_assert_system_content_blocks`\n * guards: user messages may only contain text or data blocks; system messages\n * may only contain text blocks; assistant messages accept any block type.\n * @param role\n * @param content\n */\nfunction assertContentBlocksForRole(role: Msg['role'], content: ContentBlock[]): void {\n if (role === 'user') {\n for (const block of content) {\n if (block.type !== 'text' && block.type !== 'data') {\n throw new Error('User message can only contain text blocks or data blocks.');\n }\n }\n } else if (role === 'system') {\n for (const block of content) {\n if (block.type !== 'text') {\n throw new Error('System message can only contain text blocks.');\n }\n }\n }\n}\n\n/**\n * createMsg is a low-level utility for constructing Msg objects with proper defaults and validation.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.role\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @param root0.usage\n * @returns A Msg object with the specified properties, and defaults for any omitted fields.\n */\nexport function createMsg({\n name,\n content,\n role,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n usage,\n}: Omit<Msg, 'id' | 'created_at' | 'metadata' | 'content'> &\n Partial<Pick<Msg, 'id' | 'created_at' | 'metadata'>> & {\n content: string | ContentBlock[];\n }): Msg {\n const contentBlocks: ContentBlock[] =\n typeof content === 'string'\n ? [{ id: crypto.randomUUID(), type: 'text', text: content } as TextBlock]\n : content;\n assertContentBlocksForRole(role, contentBlocks);\n return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };\n}\n\n/**\n * Create a user {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @returns A Msg object with role 'user'.\n */\nexport function UserMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n finished_at?: string | null;\n}): Msg {\n return createMsg({\n name,\n content,\n role: 'user',\n metadata,\n id,\n created_at,\n finished_at: finished_at ?? created_at,\n });\n}\n\n/**\n * Create an assistant {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.usage\n * @returns A Msg object with role 'assistant'.\n */\nexport function AssistantMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n usage,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n usage?: Msg['usage'];\n}): Msg {\n return createMsg({ name, content, role: 'assistant', metadata, id, created_at, usage });\n}\n\n/**\n * Create a system {@link Msg}.\n * @param root0\n * @param root0.name\n * @param root0.content\n * @param root0.metadata\n * @param root0.id\n * @param root0.created_at\n * @param root0.finished_at\n * @returns A Msg object with role 'system'.\n */\nexport function SystemMsg({\n name,\n content,\n metadata = {},\n id = crypto.randomUUID(),\n created_at = new Date().toISOString(),\n finished_at,\n}: {\n name: string;\n content: string | ContentBlock[];\n metadata?: Record<string, JSONSerializableObject>;\n id?: string;\n created_at?: string;\n finished_at?: string | null;\n}): Msg {\n return createMsg({\n name,\n content,\n role: 'system',\n metadata,\n id,\n created_at,\n finished_at: finished_at ?? created_at,\n });\n}\n\n/**\n * Extract the plain-text content from a message.\n *\n * When `content` is a string it is returned as-is. When it is an array of\n * content blocks, all {@link TextBlock} texts are joined with `separator`.\n *\n * @param msg - The message to read.\n * @param separator - String inserted between consecutive text blocks. Defaults to `'\\n'`.\n * @returns The concatenated text, or `null` when no text blocks are present.\n */\nexport function getTextContent(msg: Msg, separator: string = '\\n'): string | null {\n const textBlocks = msg.content.filter(block => block.type === 'text');\n if (textBlocks.length === 0) return null;\n return textBlocks.map(block => (block as TextBlock).text).join(separator);\n}\n\n/**\n * Return all content blocks from a message, regardless of type.\n *\n * When `content` is a plain string it is wrapped in a single {@link TextBlock}.\n *\n * @param msg - The message to read.\n * @returns An array of all {@link ContentBlock} objects.\n */\nexport function getContentBlocks(msg: Msg): ContentBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'text'): TextBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'thinking'): ThinkingBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'data'): DataBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_call'): ToolCallBlock[];\nexport function getContentBlocks(msg: Msg, blockType: 'tool_result'): ToolResultBlock[];\nexport function getContentBlocks(\n msg: Msg,\n blockType?: 'text' | 'thinking' | 'data' | 'tool_call' | 'tool_result'\n): ContentBlock[] {\n if (!blockType) return msg.content;\n return msg.content.filter(block => block.type === blockType);\n}\n\n/**\n * Find a content block by type and id within a message.\n * @param msg\n * @param blockType\n * @param blockId\n * @returns The matching {@link ContentBlock}, or `undefined` if not found.\n */\nfunction findBlock(msg: Msg, blockType: string, blockId: string): ContentBlock | undefined {\n return msg.content.find(block => block.type === blockType && block.id === blockId);\n}\n\n/**\n * Apply a streaming {@link AgentEvent} to a {@link Msg}, mutating it in place.\n *\n * Only `content` and `finished_at` are ever modified. Events whose\n * `reply_id` does not match `msg.id` are skipped with a warning.\n * @param msg\n * @param event\n * @returns The mutated {@link Msg} object.\n */\nexport function appendEvent(msg: Msg, event: AgentEvent): Msg {\n if (!('reply_id' in event)) return msg;\n if (event.reply_id !== msg.id) {\n console.warn(\n `Event reply_id \"${event.reply_id}\" does not match message id \"${msg.id}\", skipping.`\n );\n return msg;\n }\n\n switch (event.type) {\n case EventType.REPLY_END:\n msg.finished_at = event.created_at;\n break;\n\n case EventType.TEXT_BLOCK_START:\n msg.content.push({ type: 'text', id: event.block_id, text: '' });\n break;\n\n case EventType.TEXT_BLOCK_DELTA: {\n const block = findBlock(msg, 'text', event.block_id);\n if (!block) {\n console.warn(`TextBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as TextBlock).text += event.delta;\n }\n break;\n }\n\n case EventType.TEXT_BLOCK_END:\n break;\n\n case EventType.THINKING_BLOCK_START:\n msg.content.push({ type: 'thinking', id: event.block_id, thinking: '' });\n break;\n\n case EventType.THINKING_BLOCK_DELTA: {\n const block = findBlock(msg, 'thinking', event.block_id);\n if (!block) {\n console.warn(`ThinkingBlock \"${event.block_id}\" not found, skipping.`);\n } else {\n (block as ThinkingBlock).thinking += event.delta;\n }\n break;\n }\n\n case EventType.THINKING_BLOCK_END:\n break;\n\n case EventType.DATA_BLOCK_START:\n msg.content.push({\n type: 'data',\n id: event.block_id,\n source: { type: 'base64', data: '', media_type: event.media_type },\n });\n break;\n\n case EventType.DATA_BLOCK_DELTA: {\n const block = findBlock(msg, 'data', event.block_id);\n if (!block) {\n console.warn(`DataBlock \"${event.block_id}\" not found, skipping.`);\n } else if (event.data) {\n ((block as DataBlock).source as Base64Source).data += event.data;\n }\n break;\n }\n\n case EventType.DATA_BLOCK_END:\n break;\n\n case EventType.TOOL_CALL_START:\n msg.content.push({\n type: 'tool_call',\n id: event.tool_call_id,\n name: event.tool_call_name,\n input: '',\n state: 'pending',\n });\n break;\n\n case EventType.TOOL_CALL_DELTA: {\n const block = findBlock(msg, 'tool_call', event.tool_call_id);\n if (!block) {\n console.warn(`ToolCallBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolCallBlock).input += event.delta;\n }\n break;\n }\n\n case EventType.TOOL_CALL_END:\n break;\n\n case EventType.TOOL_RESULT_START:\n msg.content.push({\n type: 'tool_result',\n id: event.tool_call_id,\n name: event.tool_call_name,\n output: [],\n state: 'running',\n });\n break;\n\n case EventType.TOOL_RESULT_TEXT_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const last = trb.output[trb.output.length - 1];\n if (!last || last.type !== 'text') {\n trb.output.push({\n type: 'text',\n id: crypto.randomUUID(),\n text: event.delta,\n });\n } else {\n (last as TextBlock).text += event.delta;\n }\n }\n break;\n }\n\n case EventType.TOOL_RESULT_DATA_DELTA: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n const trb = block as ToolResultBlock;\n if (typeof trb.output === 'string') {\n trb.output = [{ type: 'text', id: crypto.randomUUID(), text: trb.output }];\n }\n const source: Base64Source | URLSource =\n event.data != null\n ? { type: 'base64', data: event.data, media_type: event.media_type }\n : { type: 'url', url: event.url!, media_type: event.media_type };\n trb.output.push({\n type: 'data',\n id: event.block_id ?? crypto.randomUUID(),\n source,\n });\n }\n break;\n }\n\n case EventType.TOOL_RESULT_END: {\n const block = findBlock(msg, 'tool_result', event.tool_call_id);\n if (!block) {\n console.warn(`ToolResultBlock \"${event.tool_call_id}\" not found, skipping.`);\n } else {\n (block as ToolResultBlock).state = event.state;\n }\n break;\n }\n\n case EventType.MODEL_CALL_END:\n // Accumulated the input and output tokens here.\n if (msg.usage) {\n msg.usage.input_tokens += event.input_tokens;\n msg.usage.output_tokens += event.output_tokens;\n } else {\n msg.usage = {\n input_tokens: event.input_tokens,\n output_tokens: event.output_tokens,\n };\n }\n break;\n\n case EventType.REQUIRE_USER_CONFIRM:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) {\n (b as ToolCallBlock).state = 'asking';\n (b as ToolCallBlock).suggested_rules = tc.suggested_rules || [];\n }\n }\n break;\n\n case EventType.USER_CONFIRM_RESULT:\n for (const result of event.confirm_results) {\n const b = findBlock(msg, 'tool_call', result.tool_call.id);\n if (b) {\n (b as ToolCallBlock).state = result.confirmed ? 'allowed' : 'finished';\n }\n }\n break;\n\n case EventType.REQUIRE_EXTERNAL_EXECUTION:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) (b as ToolCallBlock).state = 'submitted';\n }\n break;\n\n case EventType.EXTERNAL_EXECUTION_RESULT:\n for (const result of event.execution_results) {\n msg.content.push(result);\n }\n break;\n }\n\n return msg;\n}\n","export enum GenerateReason {\n AWAITING_TOOL_RESULT = 'AWAITING_TOOL_RESULT',\n AWAITING_USER_CONFIRMATION = 'AWAITING_USER_CONFIRMATION',\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC2DA,SAAS,2BAA2B,MAAmB,SAA+B;AAClF,MAAI,SAAS,QAAQ;AACjB,eAAW,SAAS,SAAS;AACzB,UAAI,MAAM,SAAS,UAAU,MAAM,SAAS,QAAQ;AAChD,cAAM,IAAI,MAAM,2DAA2D;AAAA,MAC/E;AAAA,IACJ;AAAA,EACJ,WAAW,SAAS,UAAU;AAC1B,eAAW,SAAS,SAAS;AACzB,UAAI,MAAM,SAAS,QAAQ;AACvB,cAAM,IAAI,MAAM,8CAA8C;AAAA,MAClE;AAAA,IACJ;AAAA,EACJ;AACJ;AAeO,SAAS,UAAU;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,cAAa,oBAAI,KAAK,GAAE,YAAY;AAAA,EACpC;AAAA,EACA;AACJ,GAGY;AACR,QAAM,gBACF,OAAO,YAAY,WACb,CAAC,EAAE,IAAI,OAAO,WAAW,GAAG,MAAM,QAAQ,MAAM,QAAQ,CAAc,IACtE;AACV,6BAA2B,MAAM,aAAa;AAC9C,SAAO,EAAE,IAAI,MAAM,MAAM,SAAS,eAAe,UAAU,YAAY,aAAa,MAAM;AAC9F;AAaO,SAAS,QAAQ;AAAA,EACpB;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,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