import type { AgentRole, AgentDefinition, AgentConfig } from './agents' const REPLACED_BUILTIN_AGENTS = ['build', 'plan'] const ENHANCED_BUILTIN_AGENTS: Record }> = { plan: { tools: { 'memory-read': true, }, }, } const PLUGIN_COMMANDS: Record = { review: { description: 'Run a code review on current changes', agent: 'auditor', subtask: true, template: 'Review the current code changes. $ARGUMENTS', }, 'ralph-loop': { description: 'Start a Ralph iterative development loop in a worktree', agent: 'code', subtask: false, template: 'Use the memory-plan-ralph tool to start a Ralph loop. Use the following as the plan: $ARGUMENTS. Derive a short title from the plan.', }, 'cancel-ralph': { description: 'Cancel the active Ralph loop', agent: 'code', subtask: false, template: 'Use the ralph-cancel tool to cancel the active Ralph loop. $ARGUMENTS', }, } export function createConfigHandler(agents: Record) { return async (config: Record) => { const agentConfigs = createAgentConfigs(agents) const userAgentConfigs = config.agent as Record | undefined const mergedAgents = { ...agentConfigs } if (userAgentConfigs) { for (const [name, userConfig] of Object.entries(userAgentConfigs)) { if (mergedAgents[name]) { mergedAgents[name] = { ...mergedAgents[name], ...userConfig } } else { mergedAgents[name] = userConfig } } } for (const name of REPLACED_BUILTIN_AGENTS) { mergedAgents[name] = { ...mergedAgents[name], hidden: true } } for (const [name, enhancement] of Object.entries(ENHANCED_BUILTIN_AGENTS)) { const existing = mergedAgents[name] as AgentConfig | undefined const existingTools = existing?.tools ?? {} mergedAgents[name] = { ...existing, tools: { ...existingTools, ...enhancement.tools }, } as AgentConfig } config.agent = mergedAgents config.default_agent = 'code' const userCommands = config.command as Record | undefined const mergedCommands: Record = { ...PLUGIN_COMMANDS } if (userCommands) { for (const [name, userCommand] of Object.entries(userCommands)) { mergedCommands[name] = userCommand } } config.command = mergedCommands } } function createAgentConfigs(agents: Record): Record { const result: Record = {} for (const agent of Object.values(agents)) { const tools: Record = {} if (agent.tools?.exclude) { for (const tool of agent.tools.exclude) { tools[tool] = false } } result[agent.displayName] = { description: agent.description, model: agent.defaultModel ?? '', prompt: agent.systemPrompt ?? '', mode: agent.mode ?? 'subagent', ...(Object.keys(tools).length > 0 ? { tools } : {}), ...(agent.variant ? { variant: agent.variant } : {}), ...(agent.temperature !== undefined ? { temperature: agent.temperature } : {}), ...(agent.steps !== undefined ? { steps: agent.steps } : {}), ...(agent.hidden ? { hidden: agent.hidden } : {}), ...(agent.color ? { color: agent.color } : {}), ...(agent.permission ? { permission: agent.permission } : {}), } } return result }