UNPKG

mnemos-coder

Version:

CLI-based coding agent with graph-based execution loop and terminal UI

137 lines (131 loc) 5.68 kB
/** * General Purpose Subagent * Fallback agent with access to all tools for handling uncategorized tasks * Acts as the catch-all when specialized agents don't match */ import { BaseSubagent } from '../BaseSubagent.js'; export class GeneralPurpose extends BaseSubagent { constructor(name, config, sharedMCPClient) { const actualConfig = config || { name: 'general-purpose', description: 'General-purpose agent for any task that doesn\'t match specialized agents', tools: [ // File operations 'file-server_read_file', 'file-server_write_file', 'file-server_delete_file', 'file-server_get_file_info', 'unified-diff-editor_apply_unified_diff', // Shell operations 'shell-commands_execute_command', 'shell-commands_shell_output', 'shell-commands_shell_kill', 'shell-commands_shell_list', // Search operations 'grep-search_search_content', 'glob-finder_find_files', // Todo operations 'todos_create_todo', 'todos_update_todo_status', // Other operations 'syntax-validator_validate_file_syntax', 'linter_validate_file' ], systemPrompt: `You are a general-purpose development assistant. Handle tasks that don't fit specialized categories. IMPORTANT: To create files, use file-server_write_file with parameters: - filePath: the path to the file - content: the content to write To execute shell commands, use shell-commands_execute_command with parameter: - command: the command to execute`, maxIterations: 20, priority: 1, // Lowest priority - only used when no specialized agent matches enabled: true, tags: ['general', 'fallback', 'uncategorized'] }; super(name || 'general-purpose', actualConfig, sharedMCPClient); } async canHandle(task, context) { // Always returns a low score so specialized agents are preferred // But never returns 0, so it can act as a fallback return 0.1; } async *executeInternal(messages) { // Process messages and execute task let iteration = 0; const maxIterations = this.config.maxIterations || 20; while (iteration < maxIterations) { iteration++; // Call LLM with messages const response = await this.llmClient.chat(messages); yield { type: 'llm_response', content: response, iteration }; // Parse and execute tool calls const toolCalls = this.parseToolCalls(response); if (toolCalls.length === 0) { // No more tool calls, task complete yield { type: 'completion', content: 'Task completed', iteration }; break; } // Execute tool calls for (const toolCall of toolCalls) { yield { type: 'tool_call', content: toolCall, iteration }; try { const result = await this.executeTool(toolCall.name, toolCall.arguments || {}); yield { type: 'tool_response', content: result, iteration }; // Add tool result to messages messages.push({ role: 'assistant', content: response }); messages.push({ role: 'user', content: `Tool result: ${JSON.stringify(result)}` }); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); this.log(`Tool execution failed for ${toolCall.name}: ${errorMessage}`); yield { type: 'tool_error', content: errorMessage, iteration }; // Add error to message history so LLM knows the tool failed messages.push({ role: 'assistant', content: response }); messages.push({ role: 'user', content: `Tool execution failed for ${toolCall.name}: ${errorMessage}\nThe tool ${toolCall.name} is not available or parameters are incorrect. Please use one of the available tools with correct parameters.` }); } } } } buildPrompt(task, context) { return `You are a general-purpose development assistant handling a task that doesn't fit specialized categories. Task: ${task} Instructions: 1. Analyze what needs to be done 2. Use appropriate tools to complete the task 3. Ensure thorough completion 4. Provide clear results Since you're the fallback agent, the task might be: - Exploratory (searching, understanding codebase) - Administrative (file organization, documentation) - Mixed (requiring multiple different operations) - Unclear (needs interpretation and best-effort execution) Approach the task systematically and use all available tools as needed.`; } } //# sourceMappingURL=GeneralPurpose.js.map