/** * Message Preparation Utilities for LLM Processing * * Features: * - Filters client.* tool calls from messages prepared for LLM input * - Two-layer architecture: storage (agent.memory) vs processing (LLM input) * - Maintains complete message history while providing clean LLM context * - Enhanced string protocol: Parses JSON strings with __type markers into OpenAI format * * Implementation: * - Removes client.* tool calls from LLM context * - No side-effect state updates while preparing messages * - parseMessageContent() converts enhanced string format to OpenAI ChatMessage * - Used by utils.ts prepareMessagesForLLM() - high-level function applies this filter * - NOT used directly by llm-manager.ts (receives pre-filtered messages from utils.ts) * * Changes: * - 2026-03-06: Unwrap persisted tool execution envelopes so only canonical `result` content flows back into LLM continuation. * - 2026-02-28: Added canonical `llm.prep` category emission while preserving legacy `llm.message-prep` logs for migration compatibility. * - 2026-02-11: Added unresolved-tool-call cleanup. * - Assistant `tool_calls` are pruned to only IDs that have matching tool-result messages. * - Prevents OpenAI 400 errors from legacy/incomplete tool-call history. * - 2026-02-08: Removed stale manual tool-intervention terminology from message prep docs * - 2026-02-08: Fixed OpenAI API validation error - now filters orphaned tool messages * - Tool messages referencing removed client.* tool_call_ids are now properly filtered * - Tracks removed tool_call_ids to prevent "tool must follow tool_calls" errors * - 2025-11-04: Simplified from message-filter.ts, removed legacy tool decision cache logic * - 2025-11-06: Added parseMessageContent() for enhanced string protocol support * - 2025-11-06: Consolidated with utils.ts - renamed to filterClientSideMessages, added alias */ import type { ChatMessage } from './types.js'; /** * Parse message content to detect enhanced string format and convert to OpenAI ChatMessage. * * Enhanced String Protocol: * - Transport Layer: JSON strings with __type markers (e.g., {"__type": "tool_result", ...}) * - Storage Layer: OpenAI ChatMessage format (e.g., {role: "tool", tool_call_id: "...", ...}) * * Supported __type values: * - "tool_result": Converts to OpenAI tool message with role: "tool" * * AgentId Handling: * - If agentId is present in JSON, returns { message, targetAgentId } * - Caller should prepend @mention before publishing * * Backward Compatibility: * - Regular text strings pass through unchanged * - Invalid JSON strings pass through unchanged * - Missing __type passes through unchanged * * @param content - String content (may be plain text or JSON with __type marker) * @param defaultRole - Role to use if content is not enhanced format (default: "user") * @returns Object with ChatMessage and optional targetAgentId * * @example * // Tool result with agentId (enhanced format) * parseMessageContent('{"__type":"tool_result","tool_call_id":"call_123","agentId":"a1","content":"..."}') * // → {message: {role: "tool", ...}, targetAgentId: "a1"} * * // Regular text (backward compatible) * parseMessageContent("Hello world") * // → {message: {role: "user", content: "Hello world", ...}, targetAgentId: undefined} */ export declare function parseMessageContent(content: string, defaultRole?: 'user' | 'assistant'): { message: ChatMessage; targetAgentId?: string; }; /** * Filter client-side messages and tool calls from message array. * * This function creates a clean copy of messages suitable for LLM processing: * - Removes messages marked as clientOnly * - Removes client.* tool calls from assistant messages * - Removes orphaned tool messages (those referencing removed client.* tool calls) * - Removes orphaned tool messages with missing tool_call_ids (invalid data) * - Removes tool messages that don't have a preceding assistant message with matching tool_call * - Drops messages that become empty after filtering * * @param messages - All messages from agent memory * @returns Filtered messages ready for LLM */ export declare function filterClientSideMessages(messages: ChatMessage[]): ChatMessage[];