/** * Orchestrator Module * * Coordinates agent message processing, response generation, and turn management. * Provides high-level orchestration functions for agent behavior and LLM interaction. * * Features: * - Process agent messages with LLM response generation * - Determine if agent should respond based on mentions and turn limits * - Reset LLM call count for new conversation turns * - Turn limit enforcement with automatic handoff to human * - Enhanced tool call message formatting with parameters display * - SSE tool call data for web clients (streaming mode) * - Robust JSON parsing with detailed error logging for malformed tool arguments * * Implementation: * - Tool calls follow standard tool execution and LLM continuation flow * - Tool call messages show up to 3 parameters with truncation for readability * * Single tool: "Calling tool: shell_cmd (command: "ls", directory: "./")" * * Multiple tools: "Calling 2 tools: shell_cmd, read_file" * - In streaming mode, formatted tool call content with tool_calls data is sent via SSE * * Ensures web/Electron clients display complete tool call info with parameters * * Prevents incomplete display (e.g., "Calling tool: shell_cmd" without params) * - JSON parse errors include detailed logging (preview, length, error position) * * Helps diagnose LLM-generated malformed JSON in tool arguments * - JSON sanitization attempts to fix common LLM JSON issues before parsing * * Handles unterminated strings, trailing commas, truncation, unmatched braces * * Tries progressive fixes: trailing commas → close strings → truncate to valid * * Dependencies (Layer 5): * - types.ts (Layer 1) * - mention-logic.ts (Layer 2) * - publishers.ts (Layer 3) * - memory-manager.ts (Layer 4) * - utils.ts, logger.ts * - llm-manager.ts (runtime) * - storage (runtime) * * Changes: * - 2026-03-10: Publish one persisted chat-scoped `system` error event on terminal agent-turn failure so the transcript can retain a durable failure message across reloads. * - 2026-03-06: Required explicit `messageEvent.chatId` for agent-turn processing; removed `world.currentChatId` fallback from agent activity and turn-limit routing. * - 2026-03-06: Updated shell execution persistence to use explicit canonical failure reasons for shell validation/policy failures while keeping bounded-preview continuation output. * - 2026-03-06: Switched shell tool execution to one bounded-preview continuation mode and normalized persisted shell tool failures through the canonical shell-result formatter. * - 2026-02-28: Added canonical `turn.trace` diagnostics around per-message processing lifecycle. * - 2026-02-27: Passed explicit `chatId` when publishing `tool-execution` system events so event routing never falls back to `world.currentChatId`. * - 2026-02-27: Seeded continuation runs with already-loaded `load_skill` IDs from initial tool execution so immediate duplicate `load_skill` calls are suppressed in continuation. * - 2026-02-21: Passed `agentName` + minimal shell `llmResultMode` in initial tool-execution context so `shell_cmd` tool results stay status-only for LLM continuation and stdout assistant attribution remains consistent. * - 2026-02-16: Added `LOG_LLM_TOOL_BRIDGE` gate for LLM↔tool console bridge logs. * - 2026-02-16: Added explicit console debug logs for LLM↔tool request/result/error handoff payloads. * - 2026-02-14: Shell tool trusted cwd fallback now uses core default working directory (user home) when world `working_directory` is missing. * - 2026-02-13: Fixed shell_cmd mismatch handling by validating path targets in command parameters (e.g. `~/`) against world working_directory before execution. * - 2026-02-13: Added hard-stop guard for shell_cmd directory mismatches (LLM-requested `directory` must match world `working_directory`). * - 2026-02-13: Enriched displayed `shell_cmd` tool-call arguments with trusted world cwd so UI tool-call messages show the actual execution directory. * - 2026-02-13: Forced shell tool cwd to trusted world `working_directory`; mismatched LLM `directory` requests now stop execution with explicit error. * - 2026-02-13: Added chat-scoped `tool-start/tool-result/tool-error` event publishing so renderer session state stays accurate during tool execution. * - 2026-02-13: Added session processing-handle guards so stop requests abort active tool/continuation flow without spawning new LLM work. * - 2026-02-13: Propagated explicit `chatId` and stop abort-signal context through LLM/tool execution paths. * - 2026-02-11: Enhanced JSON parse error logging with rawArgs preview and suffix * - 2026-02-11: Fixed tool call display in Electron/web - send formatted content with tool_calls via SSE * - 2026-02-11: Fixed OpenAI tool-call protocol integrity. * - Persist only the first executable tool_call when agent execution is single-call. * - Route JSON parse/tool lookup failures through tool-error persistence so each persisted tool_call gets a matching tool message. * - 2026-02-10: Upgrade generic LLM tool-call text (e.g., "Calling tool: shell_cmd") to include parsed parameters * - 2026-02-10: Made tool-call argument parsing more robust for both JSON strings and object-like payloads * - 2026-02-08: Enhanced tool call message formatting to include parameters * - 2025-11-09: Extracted from events.ts for modular architecture */ import type { World, Agent, WorldMessageEvent } from '../types.js'; /** * Agent message processing with LLM response generation and auto-mention logic */ export declare function processAgentMessage(world: World, agent: Agent, messageEvent: WorldMessageEvent): Promise; /** * Enhanced message filtering logic with turn limits and mention detection */ export declare function shouldAgentRespond(world: World, agent: Agent, messageEvent: WorldMessageEvent): Promise;