import fs from 'fs'; import path from 'path'; import os from 'os'; import yaml from 'yaml'; import stripJsonComments from 'strip-json-comments'; const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode'); const AGENT_DIR = path.join(OPENCODE_CONFIG_DIR, 'agent'); const COMMAND_DIR = path.join(OPENCODE_CONFIG_DIR, 'command'); const CONFIG_FILE = path.join(OPENCODE_CONFIG_DIR, 'opencode.json'); const PROMPT_FILE_PATTERN = /^\{file:(.+)\}$/i; // Command scope types const COMMAND_SCOPE = { USER: 'user', PROJECT: 'project' }; function ensureDirs() { if (!fs.existsSync(OPENCODE_CONFIG_DIR)) { fs.mkdirSync(OPENCODE_CONFIG_DIR, { recursive: true }); } if (!fs.existsSync(AGENT_DIR)) { fs.mkdirSync(AGENT_DIR, { recursive: true }); } if (!fs.existsSync(COMMAND_DIR)) { fs.mkdirSync(COMMAND_DIR, { recursive: true }); } } /** * Ensure project-level command directory exists */ function ensureProjectCommandDir(workingDirectory) { const projectCommandDir = path.join(workingDirectory, '.opencode', 'command'); if (!fs.existsSync(projectCommandDir)) { fs.mkdirSync(projectCommandDir, { recursive: true }); } return projectCommandDir; } /** * Get project-level command path */ function getProjectCommandPath(workingDirectory, commandName) { return path.join(workingDirectory, '.opencode', 'command', `${commandName}.md`); } /** * Get user-level command path */ function getUserCommandPath(commandName) { return path.join(COMMAND_DIR, `${commandName}.md`); } /** * Determine command scope based on where the .md file exists * Priority: project level > user level > null (built-in only) */ function getCommandScope(commandName, workingDirectory) { if (workingDirectory) { const projectPath = getProjectCommandPath(workingDirectory, commandName); if (fs.existsSync(projectPath)) { return { scope: COMMAND_SCOPE.PROJECT, path: projectPath }; } } const userPath = getUserCommandPath(commandName); if (fs.existsSync(userPath)) { return { scope: COMMAND_SCOPE.USER, path: userPath }; } return { scope: null, path: null }; } /** * Get the path where a command should be written based on scope */ function getCommandWritePath(commandName, workingDirectory, requestedScope) { // For updates: check existing location first (project takes precedence) const existing = getCommandScope(commandName, workingDirectory); if (existing.path) { return existing; } // For new commands or built-in overrides: use requested scope or default to user const scope = requestedScope || COMMAND_SCOPE.USER; if (scope === COMMAND_SCOPE.PROJECT && workingDirectory) { return { scope: COMMAND_SCOPE.PROJECT, path: getProjectCommandPath(workingDirectory, commandName) }; } return { scope: COMMAND_SCOPE.USER, path: getUserCommandPath(commandName) }; } function isPromptFileReference(value) { if (typeof value !== 'string') { return false; } return PROMPT_FILE_PATTERN.test(value.trim()); } function resolvePromptFilePath(reference) { const match = typeof reference === 'string' ? reference.trim().match(PROMPT_FILE_PATTERN) : null; if (!match) { return null; } let target = match[1].trim(); if (!target) { return null; } if (target.startsWith('./')) { target = target.slice(2); target = path.join(OPENCODE_CONFIG_DIR, target); } else if (!path.isAbsolute(target)) { target = path.join(OPENCODE_CONFIG_DIR, target); } return target; } function writePromptFile(filePath, content) { const dir = path.dirname(filePath); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(filePath, content ?? '', 'utf8'); console.log(`Updated prompt file: ${filePath}`); } function readConfig() { if (!fs.existsSync(CONFIG_FILE)) { return {}; } try { const content = fs.readFileSync(CONFIG_FILE, 'utf8'); const normalized = stripJsonComments(content).trim(); if (!normalized) { return {}; } return JSON.parse(normalized); } catch (error) { console.error('Failed to read config file:', error); throw new Error('Failed to read OpenCode configuration'); } } function writeConfig(config) { try { if (fs.existsSync(CONFIG_FILE)) { const backupFile = `${CONFIG_FILE}.openchamber.backup`; fs.copyFileSync(CONFIG_FILE, backupFile); console.log(`Created config backup: ${backupFile}`); } fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf8'); console.log('Successfully wrote config file'); } catch (error) { console.error('Failed to write config file:', error); throw new Error('Failed to write OpenCode configuration'); } } function parseMdFile(filePath) { try { const content = fs.readFileSync(filePath, 'utf8'); const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/); if (!match) { return { frontmatter: {}, body: content.trim() }; } const frontmatter = yaml.parse(match[1]) || {}; const body = match[2].trim(); return { frontmatter, body }; } catch (error) { console.error(`Failed to parse markdown file ${filePath}:`, error); throw new Error('Failed to parse agent markdown file'); } } function writeMdFile(filePath, frontmatter, body) { try { // Filter out null/undefined values - OpenCode expects keys to be omitted rather than set to null const cleanedFrontmatter = Object.fromEntries( Object.entries(frontmatter).filter(([, value]) => value != null) ); const yamlStr = yaml.stringify(cleanedFrontmatter); const content = `---\n${yamlStr}---\n\n${body}`; fs.writeFileSync(filePath, content, 'utf8'); console.log(`Successfully wrote markdown file: ${filePath}`); } catch (error) { console.error(`Failed to write markdown file ${filePath}:`, error); throw new Error('Failed to write agent markdown file'); } } function getAgentSources(agentName) { const mdPath = path.join(AGENT_DIR, `${agentName}.md`); const mdExists = fs.existsSync(mdPath); const config = readConfig(); const jsonSection = config.agent?.[agentName]; const sources = { md: { exists: mdExists, path: mdExists ? mdPath : null, fields: [] }, json: { exists: !!jsonSection, path: CONFIG_FILE, fields: [] } }; if (mdExists) { const { frontmatter, body } = parseMdFile(mdPath); sources.md.fields = Object.keys(frontmatter); if (body) { sources.md.fields.push('prompt'); } } if (jsonSection) { sources.json.fields = Object.keys(jsonSection); } return sources; } function createAgent(agentName, config) { ensureDirs(); const mdPath = path.join(AGENT_DIR, `${agentName}.md`); if (fs.existsSync(mdPath)) { throw new Error(`Agent ${agentName} already exists as .md file`); } const existingConfig = readConfig(); if (existingConfig.agent?.[agentName]) { throw new Error(`Agent ${agentName} already exists in opencode.json`); } const { prompt, ...frontmatter } = config; writeMdFile(mdPath, frontmatter, prompt || ''); console.log(`Created new agent: ${agentName}`); } function updateAgent(agentName, updates) { ensureDirs(); const mdPath = path.join(AGENT_DIR, `${agentName}.md`); const mdExists = fs.existsSync(mdPath); let mdData = mdExists ? parseMdFile(mdPath) : null; let config = readConfig(); const jsonSection = config.agent?.[agentName]; let mdModified = false; let jsonModified = false; for (const [field, value] of Object.entries(updates)) { if (field === 'prompt') { const normalizedValue = typeof value === 'string' ? value : (value == null ? '' : String(value)); if (mdExists) { mdData.body = normalizedValue; mdModified = true; } else if (isPromptFileReference(jsonSection?.prompt)) { const promptFilePath = resolvePromptFilePath(jsonSection.prompt); if (!promptFilePath) { throw new Error(`Invalid prompt file reference for agent ${agentName}`); } writePromptFile(promptFilePath, normalizedValue); } else if (isPromptFileReference(normalizedValue)) { if (!config.agent) config.agent = {}; if (!config.agent[agentName]) config.agent[agentName] = {}; config.agent[agentName].prompt = normalizedValue; jsonModified = true; } else { if (!config.agent) config.agent = {}; if (!config.agent[agentName]) config.agent[agentName] = {}; config.agent[agentName].prompt = normalizedValue; jsonModified = true; } continue; } const inMd = mdData?.frontmatter?.[field] !== undefined; const inJson = jsonSection?.[field] !== undefined; if (inMd) { mdData.frontmatter[field] = value; mdModified = true; } else if (inJson) { if (!config.agent) config.agent = {}; if (!config.agent[agentName]) config.agent[agentName] = {}; config.agent[agentName][field] = value; jsonModified = true; } else { if (mdExists && jsonSection) { if (!config.agent) config.agent = {}; if (!config.agent[agentName]) config.agent[agentName] = {}; config.agent[agentName][field] = value; jsonModified = true; } else if (mdExists) { mdData.frontmatter[field] = value; mdModified = true; } else { if (!config.agent) config.agent = {}; if (!config.agent[agentName]) config.agent[agentName] = {}; config.agent[agentName][field] = value; jsonModified = true; } } } if (mdModified) { writeMdFile(mdPath, mdData.frontmatter, mdData.body); } if (jsonModified) { writeConfig(config); } console.log(`Updated agent: ${agentName} (md: ${mdModified}, json: ${jsonModified})`); } function deleteAgent(agentName) { const mdPath = path.join(AGENT_DIR, `${agentName}.md`); let deleted = false; if (fs.existsSync(mdPath)) { fs.unlinkSync(mdPath); console.log(`Deleted agent .md file: ${mdPath}`); deleted = true; } const config = readConfig(); if (config.agent?.[agentName]) { delete config.agent[agentName]; writeConfig(config); console.log(`Removed agent from opencode.json: ${agentName}`); deleted = true; } if (!deleted) { if (!config.agent) config.agent = {}; config.agent[agentName] = { disable: true }; writeConfig(config); console.log(`Disabled built-in agent: ${agentName}`); } } function getCommandSources(commandName, workingDirectory) { // Check project level first (takes precedence) const projectPath = workingDirectory ? getProjectCommandPath(workingDirectory, commandName) : null; const projectExists = projectPath && fs.existsSync(projectPath); // Then check user level const userPath = getUserCommandPath(commandName); const userExists = fs.existsSync(userPath); // Determine which md file to use (project takes precedence) const mdPath = projectExists ? projectPath : (userExists ? userPath : null); const mdExists = !!mdPath; const mdScope = projectExists ? COMMAND_SCOPE.PROJECT : (userExists ? COMMAND_SCOPE.USER : null); const config = readConfig(); const jsonSection = config.command?.[commandName]; const sources = { md: { exists: mdExists, path: mdPath, scope: mdScope, fields: [] }, json: { exists: !!jsonSection, path: CONFIG_FILE, fields: [] }, // Additional info about both levels projectMd: { exists: projectExists, path: projectPath }, userMd: { exists: userExists, path: userPath } }; if (mdExists) { const { frontmatter, body } = parseMdFile(mdPath); sources.md.fields = Object.keys(frontmatter); if (body) { sources.md.fields.push('template'); } } if (jsonSection) { sources.json.fields = Object.keys(jsonSection); } return sources; } function createCommand(commandName, config, workingDirectory, scope) { ensureDirs(); // Check if command already exists at either level const projectPath = workingDirectory ? getProjectCommandPath(workingDirectory, commandName) : null; const userPath = getUserCommandPath(commandName); if (projectPath && fs.existsSync(projectPath)) { throw new Error(`Command ${commandName} already exists as project-level .md file`); } if (fs.existsSync(userPath)) { throw new Error(`Command ${commandName} already exists as user-level .md file`); } const existingConfig = readConfig(); if (existingConfig.command?.[commandName]) { throw new Error(`Command ${commandName} already exists in opencode.json`); } // Determine target path based on requested scope let targetPath; let targetScope; if (scope === COMMAND_SCOPE.PROJECT && workingDirectory) { ensureProjectCommandDir(workingDirectory); targetPath = projectPath; targetScope = COMMAND_SCOPE.PROJECT; } else { targetPath = userPath; targetScope = COMMAND_SCOPE.USER; } // Extract scope from config - it's only used for path determination, not written to file const { template, scope: _scopeFromConfig, ...frontmatter } = config; writeMdFile(targetPath, frontmatter, template || ''); console.log(`Created new command: ${commandName} (scope: ${targetScope}, path: ${targetPath})`); } function updateCommand(commandName, updates, workingDirectory) { ensureDirs(); // Determine correct path: project level takes precedence const { scope, path: mdPath } = getCommandWritePath(commandName, workingDirectory); const mdExists = mdPath && fs.existsSync(mdPath); // If no existing md file, we need to create one (for built-in command overrides) // Default to user level for built-in overrides let targetPath = mdPath; let targetScope = scope; if (!mdExists) { // No existing md file - this is a built-in override, create at user level targetPath = getUserCommandPath(commandName); targetScope = COMMAND_SCOPE.USER; } let mdData = mdExists ? parseMdFile(mdPath) : { frontmatter: {}, body: '' }; let config = readConfig(); const jsonSection = config.command?.[commandName]; let mdModified = false; let jsonModified = false; let creatingNewMd = !mdExists; for (const [field, value] of Object.entries(updates)) { if (field === 'template') { const normalizedValue = typeof value === 'string' ? value : (value == null ? '' : String(value)); if (mdExists || creatingNewMd) { mdData.body = normalizedValue; mdModified = true; } else if (isPromptFileReference(jsonSection?.template)) { const templateFilePath = resolvePromptFilePath(jsonSection.template); if (!templateFilePath) { throw new Error(`Invalid template file reference for command ${commandName}`); } writePromptFile(templateFilePath, normalizedValue); } else if (isPromptFileReference(normalizedValue)) { if (!config.command) config.command = {}; if (!config.command[commandName]) config.command[commandName] = {}; config.command[commandName].template = normalizedValue; jsonModified = true; } else { // Create new md file for the update mdData.body = normalizedValue; mdModified = true; creatingNewMd = true; } continue; } const inMd = mdData?.frontmatter?.[field] !== undefined; const inJson = jsonSection?.[field] !== undefined; if (inMd || creatingNewMd) { mdData.frontmatter[field] = value; mdModified = true; } else if (inJson) { if (!config.command) config.command = {}; if (!config.command[commandName]) config.command[commandName] = {}; config.command[commandName][field] = value; jsonModified = true; } else { // New field - add to md if it exists or we're creating one if (mdExists || creatingNewMd) { mdData.frontmatter[field] = value; mdModified = true; } else { if (!config.command) config.command = {}; if (!config.command[commandName]) config.command[commandName] = {}; config.command[commandName][field] = value; jsonModified = true; } } } if (mdModified) { writeMdFile(targetPath, mdData.frontmatter, mdData.body); } if (jsonModified) { writeConfig(config); } console.log(`Updated command: ${commandName} (scope: ${targetScope}, md: ${mdModified}, json: ${jsonModified})`); } function deleteCommand(commandName, workingDirectory) { let deleted = false; // Check project level first (takes precedence) if (workingDirectory) { const projectPath = getProjectCommandPath(workingDirectory, commandName); if (fs.existsSync(projectPath)) { fs.unlinkSync(projectPath); console.log(`Deleted project-level command .md file: ${projectPath}`); deleted = true; } } // Then check user level const userPath = getUserCommandPath(commandName); if (fs.existsSync(userPath)) { fs.unlinkSync(userPath); console.log(`Deleted user-level command .md file: ${userPath}`); deleted = true; } // Also check json config const config = readConfig(); if (config.command?.[commandName]) { delete config.command[commandName]; writeConfig(config); console.log(`Removed command from opencode.json: ${commandName}`); deleted = true; } if (!deleted) { throw new Error(`Command "${commandName}" not found`); } } export { getAgentSources, createAgent, updateAgent, deleteAgent, getCommandSources, getCommandScope, createCommand, updateCommand, deleteCommand, readConfig, writeConfig, AGENT_DIR, COMMAND_DIR, CONFIG_FILE, COMMAND_SCOPE };