{"version":3,"file":"tool-call-engine/utils.mjs","sources":["webpack://@multimodal/agent/./src/tool-call-engine/utils.ts"],"sourcesContent":["/*\n * Copyright (c) 2025 Bytedance, Inc. and its affiliates.\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport { getLogger } from '../utils/logger';\nimport {\n  ChatCompletionContentPart,\n  ChatCompletionMessageParam,\n  MultimodalToolCallResult,\n} from '@multimodal/agent-interface';\n\nconst logger = getLogger('ToolCallEngine');\n\n/**\n * Builds chat completion messages from tool call results.\n *\n * This shared utility handles the conversion of tool call results into properly\n * formatted messages for different LLM engine types.\n *\n * For native engines (using OpenAI's tool protocol):\n * - Text content goes into a 'tool' role message\n * - Non-text content (like images) goes into a separate 'user' role message\n *\n * For non-native engines (Prompt Engineering & Structured Outputs):\n * - When only text is present: A single 'user' message with text content\n * - When multimodal content exists: A single 'user' message with an array of content parts,\n *   combining text and non-text elements (images, etc.)\n *\n * @param toolCallResults - Array of multimodal tool call results to process\n * @param isNativeEngine - Whether to format for native tool protocol (OpenAI)\n * @returns Array of properly formatted chat completion messages\n */\nexport function buildToolCallResultMessages(\n  toolCallResults: MultimodalToolCallResult[],\n  isNativeEngine = false,\n): ChatCompletionMessageParam[] {\n  if (toolCallResults.length === 0) {\n    return [];\n  }\n\n  logger.debug(`Building ${toolCallResults.length} tool call result messages`);\n\n  // Select appropriate processing strategy based on engine type upfront\n  // rather than checking in each iteration\n  return isNativeEngine\n    ? buildNativeEngineMessages(toolCallResults)\n    : buildNonNativeEngineMessages(toolCallResults);\n}\n\n/**\n * Processes tool call results for native engines (OpenAI tool protocol).\n * Separates text content (tool role) from non-text content (user role).\n */\nfunction buildNativeEngineMessages(\n  toolCallResults: MultimodalToolCallResult[],\n): ChatCompletionMessageParam[] {\n  const messages: ChatCompletionMessageParam[] = [];\n\n  for (const result of toolCallResults) {\n    // Extract text content\n    const textContent = result.content\n      .filter((part) => part.type === 'text')\n      .map((part) => (part as { text: string }).text)\n      .join('');\n\n    // Add standard tool result message (text content only)\n    messages.push({\n      role: 'tool',\n      tool_call_id: result.toolCallId,\n      content: textContent,\n    });\n\n    // If non-text content exists (images, etc.), add as a separate user message\n    const nonTextParts = result.content.filter((part) => part.type !== 'text');\n    if (nonTextParts.length > 0) {\n      logger.debug(`Adding non-text content message for tool result: ${result.toolName}`);\n      messages.push({\n        role: 'user',\n        content: nonTextParts,\n      });\n    }\n  }\n\n  return messages;\n}\n\n/**\n * Processes tool call results for non-native engines (Prompt Engineering & Structured Outputs).\n * Combines text and non-text content into single messages with appropriate formatting.\n */\nfunction buildNonNativeEngineMessages(\n  toolCallResults: MultimodalToolCallResult[],\n): ChatCompletionMessageParam[] {\n  const messages: ChatCompletionMessageParam[] = [];\n\n  for (const result of toolCallResults) {\n    const hasNonTextContent = result.content.some((part) => part.type !== 'text');\n\n    if (hasNonTextContent) {\n      // Multimodal content - create a single message with all content parts\n      const textParts = result.content\n        .filter((part) => part.type === 'text')\n        .map((part) => (part as { text: string }).text)\n        .join('');\n\n      // Create combined content array\n      const combinedContent: ChatCompletionContentPart[] = [\n        {\n          type: 'text',\n          text: `Tool: ${result.toolName}\\nResult:\\n${textParts}`,\n        },\n        ...result.content.filter((part) => part.type !== 'text'),\n      ];\n\n      messages.push({\n        role: 'user',\n        content: combinedContent,\n      });\n    } else {\n      // Text-only content - use simple string format\n      const textContent = result.content\n        .filter((part) => part.type === 'text')\n        .map((part) => (part as { text: string }).text)\n        .join('');\n\n      messages.push({\n        role: 'user',\n        content: `Tool: ${result.toolName}\\nResult:\\n${textContent}`,\n      });\n    }\n  }\n\n  return messages;\n}\n"],"names":["logger","getLogger","buildToolCallResultMessages","toolCallResults","isNativeEngine","buildNativeEngineMessages","buildNonNativeEngineMessages","messages","result","textContent","part","nonTextParts","hasNonTextContent","textParts","combinedContent"],"mappings":";;;;;AAYA,MAAMA,SAASC,UAAU;AAqBlB,SAASC,4BACdC,eAA2C,EAC3CC,iBAAiB,KAAK;IAEtB,IAAID,AAA2B,MAA3BA,gBAAg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