@agentscope-ai/agentscope 0.0.2 → 0.0.4

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.
Files changed (90) hide show
  1. package/LICENSE +202 -0
  2. package/dist/agent/index.d.mts +10 -10
  3. package/dist/agent/index.d.ts +10 -10
  4. package/dist/agent/index.js +104 -93
  5. package/dist/agent/index.js.map +1 -1
  6. package/dist/agent/index.mjs +104 -93
  7. package/dist/agent/index.mjs.map +1 -1
  8. package/dist/{base-BOx3UzOl.d.mts → base-1YVBgB4n.d.mts} +2 -2
  9. package/dist/{base-DYlBMCy_.d.mts → base-B_MQMHWr.d.mts} +3 -3
  10. package/dist/{base-Cwi4bjze.d.ts → base-BherSLRs.d.ts} +3 -3
  11. package/dist/{base-NX-knWOv.d.ts → base-CY4DMBH1.d.ts} +1 -1
  12. package/dist/{base-BoIps2RL.d.ts → base-ChWjyzPL.d.ts} +2 -2
  13. package/dist/{base-C7jwyH4Z.d.mts → base-ClilytRZ.d.mts} +1 -1
  14. package/dist/{block-VsnHrllL.d.mts → block-B72uPF1H.d.mts} +7 -5
  15. package/dist/{block-VsnHrllL.d.ts → block-B72uPF1H.d.ts} +7 -5
  16. package/dist/event/index.d.mts +105 -89
  17. package/dist/event/index.d.ts +105 -89
  18. package/dist/event/index.js +8 -8
  19. package/dist/event/index.js.map +1 -1
  20. package/dist/event/index.mjs +8 -8
  21. package/dist/event/index.mjs.map +1 -1
  22. package/dist/formatter/index.d.mts +4 -3
  23. package/dist/formatter/index.d.ts +4 -3
  24. package/dist/formatter/index.js +17 -17
  25. package/dist/formatter/index.js.map +1 -1
  26. package/dist/formatter/index.mjs +17 -17
  27. package/dist/formatter/index.mjs.map +1 -1
  28. package/dist/{index-BcatlwXQ.d.ts → index-BNfyKbQN.d.ts} +1 -1
  29. package/dist/{index-BTJDlKvQ.d.mts → index-UQCwdfet.d.mts} +1 -1
  30. package/dist/mcp/index.d.mts +2 -2
  31. package/dist/mcp/index.d.ts +2 -2
  32. package/dist/mcp/index.js +1 -1
  33. package/dist/mcp/index.js.map +1 -1
  34. package/dist/mcp/index.mjs +1 -1
  35. package/dist/mcp/index.mjs.map +1 -1
  36. package/dist/message/index.d.mts +3 -2
  37. package/dist/message/index.d.ts +3 -2
  38. package/dist/message/index.js +204 -5
  39. package/dist/message/index.js.map +1 -1
  40. package/dist/message/index.mjs +200 -5
  41. package/dist/message/index.mjs.map +1 -1
  42. package/dist/message-CPZd0NIc.d.ts +133 -0
  43. package/dist/message-DgpfAaHK.d.mts +133 -0
  44. package/dist/model/index.d.mts +6 -5
  45. package/dist/model/index.d.ts +6 -5
  46. package/dist/model/index.js +39 -28
  47. package/dist/model/index.js.map +1 -1
  48. package/dist/model/index.mjs +39 -28
  49. package/dist/model/index.mjs.map +1 -1
  50. package/dist/storage/index.d.mts +4 -3
  51. package/dist/storage/index.d.ts +4 -3
  52. package/dist/storage/index.js +4 -4
  53. package/dist/storage/index.js.map +1 -1
  54. package/dist/storage/index.mjs +4 -4
  55. package/dist/storage/index.mjs.map +1 -1
  56. package/dist/tool/index.d.mts +4 -4
  57. package/dist/tool/index.d.ts +4 -4
  58. package/dist/{toolkit-CEpulFi0.d.ts → toolkit-DeOlul5Y.d.ts} +2 -2
  59. package/dist/{toolkit-CGEZSZPa.d.mts → toolkit-jwe7NmVJ.d.mts} +2 -2
  60. package/package.json +87 -87
  61. package/src/agent/agent.test.ts +104 -71
  62. package/src/agent/agent.ts +112 -104
  63. package/src/agent/test-compression.ts +1 -1
  64. package/src/event/index.ts +96 -98
  65. package/src/formatter/base.ts +3 -3
  66. package/src/formatter/dashscope-chat-formatter.test.ts +11 -8
  67. package/src/formatter/dashscope-chat-formatter.ts +3 -3
  68. package/src/formatter/openai-chat-formatter.test.ts +13 -5
  69. package/src/formatter/openai-chat-formatter.ts +6 -6
  70. package/src/mcp/base.ts +1 -1
  71. package/src/mcp/http.test.ts +2 -0
  72. package/src/mcp/stdio.test.ts +1 -0
  73. package/src/message/append-event.test.ts +783 -0
  74. package/src/message/block.ts +8 -4
  75. package/src/message/index.ts +12 -1
  76. package/src/message/message.test.ts +3 -1
  77. package/src/message/message.ts +310 -47
  78. package/src/model/dashscope-model.test.ts +4 -0
  79. package/src/model/dashscope-model.ts +3 -0
  80. package/src/model/deepseek-model.test.ts +2 -0
  81. package/src/model/deepseek-model.ts +3 -0
  82. package/src/model/ollama-model.test.ts +1 -0
  83. package/src/model/ollama-model.ts +2 -0
  84. package/src/model/openai-model.ts +3 -0
  85. package/src/permission/index.ts +13 -0
  86. package/src/storage/file-system.test.ts +4 -3
  87. package/src/storage/file-system.ts +4 -4
  88. package/src/tool/toolkit.test.ts +12 -0
  89. package/dist/message-CkN21KaY.d.mts +0 -99
  90. package/dist/message-CzLeTlua.d.ts +0 -99
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/formatter/index.ts","../../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":["export { FormatterBase } from './base';\nexport { DashScopeChatFormatter } from './dashscope-chat-formatter';\nexport { DeepSeekChatFormatter } from './deepseek-chat-formatter';\nexport { OllamaChatFormatter } from './ollama-chat-formatter';\nexport { OpenAIChatFormatter } from './openai-chat-formatter';\n","import { JSONSerializableObject } from '../type';\nimport {\n ContentBlock,\n TextBlock,\n ThinkingBlock,\n ToolResultBlock,\n ToolCallBlock,\n DataBlock,\n} from './block';\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 timestamp: string;\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 `timestamp` when omitted.\n *\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.timestamp\n * @param root0.usage\n * @returns A fully-populated {@link Msg} object.\n */\nexport function createMsg({\n name,\n content,\n role,\n metadata = {},\n id = crypto.randomUUID(),\n timestamp = new Date().toISOString(),\n usage,\n}: Omit<Msg, 'id' | 'timestamp' | 'metadata'> &\n Partial<Pick<Msg, 'id' | 'timestamp' | 'metadata'>>): Msg {\n return { id, name, role, content, metadata, timestamp, usage } as Msg;\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) {\n return null;\n }\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[];\n/**\n * Return all {@link TextBlock} objects from a message.\n *\n * @param msg - The message to read.\n * @param blockType - `'text'`\n * @returns An array of {@link TextBlock} objects.\n */\nexport function getContentBlocks(msg: Msg, blockType: 'text'): TextBlock[];\n/**\n * Return all {@link ThinkingBlock} objects from a message.\n *\n * @param msg - The message to read.\n * @param blockType - `'thinking'`\n * @returns An array of {@link ThinkingBlock} objects.\n */\nexport function getContentBlocks(msg: Msg, blockType: 'thinking'): ThinkingBlock[];\n/**\n * Return all {@link DataBlock} objects from a message.\n *\n * @param msg - The message to read.\n * @param blockType - `'video'`\n * @returns An array of {@link DataBlock} objects.\n */\nexport function getContentBlocks(msg: Msg, blockType: 'data'): DataBlock[];\n/**\n * Return all {@link ToolCallBlock} objects from a message.\n *\n * @param msg - The message to read.\n * @param blockType - `'tool_call'`\n * @returns An array of {@link ToolCallBlock} objects.\n */\nexport function getContentBlocks(msg: Msg, blockType: 'tool_call'): ToolCallBlock[];\n/**\n * Return all {@link ToolResultBlock} objects from a message.\n *\n * @param msg - The message to read.\n * @param blockType - `'tool_result'`\n * @returns An array of {@link ToolResultBlock} objects.\n */\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","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.mediaType.split('/')[0];\n if (type !== 'image' && type !== 'audio' && type !== 'video') {\n console.log(\n `Unsupported media type '${block.source.mediaType}' 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.mediaType.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.mediaType.split('/')[0];\n\n if (!['image', 'audio', 'video'].includes(type)) {\n console.log(\n `Skip unsupported media type ${block.source.mediaType} 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.mediaType};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.mediaType.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.mediaType} 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.mediaType};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.mediaType || `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.mediaType);\n if (!format) {\n throw new TypeError(\n `Unsupported audio media type: ${block.source.mediaType}, 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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC4CO,SAAS,UAAU;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW,CAAC;AAAA,EACZ,KAAK,OAAO,WAAW;AAAA,EACvB,aAAY,oBAAI,KAAK,GAAE,YAAY;AAAA,EACnC;AACJ,GAC8D;AAC1D,SAAO,EAAE,IAAI,MAAM,MAAM,SAAS,UAAU,WAAW,MAAM;AACjE;AAYO,SAAS,eAAe,KAAU,YAAoB,MAAqB;AAC9E,QAAM,aAAa,IAAI,QAAQ,OAAO,WAAS,MAAM,SAAS,MAAM;AACpE,MAAI,WAAW,WAAW,GAAG;AACzB,WAAO;AAAA,EACX;AACA,SAAO,WAAW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AM,YAAY,KAAK,aAAa,SAAS;AAC7C,QAAI;AAEJ,QAAI,iBAAa,sBAAW,SAAS,GAAG;AACpC,YAAM,OAAO,UAAM,0BAAS,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,UAAM,qBAAQ,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,iBAAO,0BAAc,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/formatter/index.ts","../../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":["export { FormatterBase } from './base';\nexport { DashScopeChatFormatter } from './dashscope-chat-formatter';\nexport { DeepSeekChatFormatter } from './deepseek-chat-formatter';\nexport { OllamaChatFormatter } from './ollama-chat-formatter';\nexport { OpenAIChatFormatter } from './openai-chat-formatter';\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.REQUIRE_USER_CONFIRM:\n for (const tc of event.tool_calls) {\n const b = findBlock(msg, 'tool_call', tc.id);\n if (b) (b as ToolCallBlock).state = 'asking';\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":";;;;;;;;;;;;;;;;;;;;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;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,MAAI,CAAC,UAAW,QAAO,IAAI;AAC3B,SAAO,IAAI,QAAQ,OAAO,WAAS,MAAM,SAAS,SAAS;AAC/D;;;ACtLO,IAAe,gBAAf,MAA6B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBhC,0BACI,QACA,6BACF;AACE,QAAI,OAAO,WAAW,SAAU,QAAO,EAAE,MAAM,QAAQ,aAAa,KAAK;AAEzE,QAAI,gBAAgB,CAAC;A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,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,UAAM,qBAAQ,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,iBAAO,0BAAc,SAAS;AAAA,IAClC;AACA,QAAI,CAAC,UAAU,SAAS,KAAK,GAAG;AAC5B,aAAO;AAAA,IACX;AACA,WAAO;AAAA,EACX;AACJ;","names":["format"]}
@@ -5,16 +5,16 @@ function createMsg({
5
5
  role,
6
6
  metadata = {},
7
7
  id = crypto.randomUUID(),
8
- timestamp = (/* @__PURE__ */ new Date()).toISOString(),
8
+ created_at = (/* @__PURE__ */ new Date()).toISOString(),
9
+ finished_at,
9
10
  usage
10
11
  }) {
11
- return { id, name, role, content, metadata, timestamp, usage };
12
+ const contentBlocks = typeof content === "string" ? [{ id: crypto.randomUUID(), type: "text", text: content }] : content;
13
+ return { id, name, role, content: contentBlocks, metadata, created_at, finished_at, usage };
12
14
  }
13
15
  function getTextContent(msg, separator = "\n") {
14
16
  const textBlocks = msg.content.filter((block) => block.type === "text");
15
- if (textBlocks.length === 0) {
16
- return null;
17
- }
17
+ if (textBlocks.length === 0) return null;
18
18
  return textBlocks.map((block) => block.text).join(separator);
19
19
  }
20
20
  function getContentBlocks(msg, blockType) {
@@ -44,10 +44,10 @@ var FormatterBase = class {
44
44
  textualOutput.push(block.text);
45
45
  break;
46
46
  default:
47
- const type = block.source.mediaType.split("/")[0];
47
+ const type = block.source.media_type.split("/")[0];
48
48
  if (type !== "image" && type !== "audio" && type !== "video") {
49
49
  console.log(
50
- `Unsupported media type '${block.source.mediaType}' in tool output. Only image, audio and video are supported.`
50
+ `Unsupported media type '${block.source.media_type}' in tool output. Only image, audio and video are supported.`
51
51
  );
52
52
  break;
53
53
  }
@@ -71,7 +71,7 @@ var FormatterBase = class {
71
71
  }
72
72
  const promotedBlocks = [];
73
73
  promotedData.forEach(({ id, block }) => {
74
- const type = block.source.mediaType.split("/")[0];
74
+ const type = block.source.media_type.split("/")[0];
75
75
  promotedBlocks.push({
76
76
  id: crypto.randomUUID(),
77
77
  type: "text",
@@ -217,10 +217,10 @@ var DashScopeChatFormatter = class extends FormatterBase {
217
217
  * @returns An object representing the formatted multimodal content.
218
218
  */
219
219
  _formatMultimodalBlock(block) {
220
- const type = block.source.mediaType.split("/")[0];
220
+ const type = block.source.media_type.split("/")[0];
221
221
  if (!["image", "audio", "video"].includes(type)) {
222
222
  console.log(
223
- `Skip unsupported media type ${block.source.mediaType} in DashScopeChatFormatter. Only image, audio and video are supported.`
223
+ `Skip unsupported media type ${block.source.media_type} in DashScopeChatFormatter. Only image, audio and video are supported.`
224
224
  );
225
225
  return [];
226
226
  }
@@ -229,7 +229,7 @@ var DashScopeChatFormatter = class extends FormatterBase {
229
229
  }
230
230
  return [
231
231
  {
232
- [type]: `data:${block.source.mediaType};base64,${block.source.data}`
232
+ [type]: `data:${block.source.media_type};base64,${block.source.data}`
233
233
  }
234
234
  ];
235
235
  }
@@ -480,7 +480,7 @@ var OpenAIChatFormatter = class extends FormatterBase {
480
480
  block,
481
481
  role
482
482
  }) {
483
- const type = block.source.mediaType.split("/")[0];
483
+ const type = block.source.media_type.split("/")[0];
484
484
  if (type === "image") {
485
485
  return [
486
486
  {
@@ -503,7 +503,7 @@ var OpenAIChatFormatter = class extends FormatterBase {
503
503
  ];
504
504
  }
505
505
  console.log(
506
- `Skip unsupported media type ${block.source.mediaType} in OpenAIChatFormatter. Only image and audio are supported.`
506
+ `Skip unsupported media type ${block.source.media_type} in OpenAIChatFormatter. Only image and audio are supported.`
507
507
  );
508
508
  return [];
509
509
  }
@@ -514,7 +514,7 @@ var OpenAIChatFormatter = class extends FormatterBase {
514
514
  */
515
515
  async _toOpenAIImageURL(block) {
516
516
  if (block.source.type === "base64") {
517
- return `data:${block.source.mediaType};base64,${block.source.data}`;
517
+ return `data:${block.source.media_type};base64,${block.source.data}`;
518
518
  }
519
519
  const sourceUrl = block.source.url;
520
520
  if (sourceUrl.startsWith("http://") || sourceUrl.startsWith("https://")) {
@@ -535,7 +535,7 @@ var OpenAIChatFormatter = class extends FormatterBase {
535
535
  );
536
536
  }
537
537
  const file = await readFile(localPath);
538
- const mime = block.source.mediaType || `image/${ext.slice(1)}`;
538
+ const mime = block.source.media_type || `image/${ext.slice(1)}`;
539
539
  return `data:${mime};base64,${file.toString("base64")}`;
540
540
  }
541
541
  /**
@@ -551,10 +551,10 @@ var OpenAIChatFormatter = class extends FormatterBase {
551
551
  ["audio/mpeg", "mp3"]
552
552
  ]);
553
553
  if (block.source.type === "base64") {
554
- const format2 = supportedMediaTypes.get(block.source.mediaType);
554
+ const format2 = supportedMediaTypes.get(block.source.media_type);
555
555
  if (!format2) {
556
556
  throw new TypeError(
557
- `Unsupported audio media type: ${block.source.mediaType}, only audio/wav and audio/mp3 are supported.`
557
+ `Unsupported audio media type: ${block.source.media_type}, only audio/wav and audio/mp3 are supported.`
558
558
  );
559
559
  }
560
560
  return { data: block.source.data, format: format2 };