UNPKG

@intellectronica/ruler

Version:

Ruler — apply the same rules to all coding agents

157 lines (156 loc) 7.07 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CodexCliAgent = void 0; const path = __importStar(require("path")); /* eslint-disable @typescript-eslint/no-explicit-any */ const fs_1 = require("fs"); const toml = __importStar(require("toml")); const toml_1 = require("@iarna/toml"); const FileSystemUtils_1 = require("../core/FileSystemUtils"); /** * OpenAI Codex CLI agent adapter. */ class CodexCliAgent { getIdentifier() { return 'codex'; } getName() { return 'OpenAI Codex CLI'; } async applyRulerConfig(concatenatedRules, projectRoot, rulerMcpJson, agentConfig) { // Get default paths const defaults = this.getDefaultOutputPath(projectRoot); // Determine the instructions file path const instructionsPath = agentConfig?.outputPath ?? agentConfig?.outputPathInstructions ?? defaults.instructions; // Write the instructions file await (0, FileSystemUtils_1.backupFile)(instructionsPath); await (0, FileSystemUtils_1.writeGeneratedFile)(instructionsPath, concatenatedRules); // Handle MCP configuration if enabled const mcpEnabled = agentConfig?.mcp?.enabled ?? true; if (mcpEnabled && rulerMcpJson) { // Determine the config file path const configPath = agentConfig?.outputPathConfig ?? defaults.config; // Ensure the parent directory exists await fs_1.promises.mkdir(path.dirname(configPath), { recursive: true }); // Get the merge strategy const strategy = agentConfig?.mcp?.strategy ?? 'merge'; // Extract MCP servers from ruler config const rulerServers = rulerMcpJson.mcpServers || {}; // Read existing TOML config if it exists let existingConfig = {}; try { const existingContent = await fs_1.promises.readFile(configPath, 'utf8'); existingConfig = toml.parse(existingContent); } catch { // File doesn't exist or can't be parsed, use empty config } // Create the updated config const updatedConfig = { ...existingConfig }; // Initialize mcp_servers if it doesn't exist if (!updatedConfig.mcp_servers) { updatedConfig.mcp_servers = {}; } if (strategy === 'overwrite') { // For overwrite strategy, replace the entire mcp_servers section updatedConfig.mcp_servers = {}; } // Add the ruler servers for (const [serverName, serverConfig] of Object.entries(rulerServers)) { // Create a properly formatted MCP server entry const mcpServer = { command: serverConfig.command, args: serverConfig.args, }; // Format env as an inline table if (serverConfig.env) { mcpServer.env = serverConfig.env; } updatedConfig.mcp_servers[serverName] = mcpServer; } // Convert to TOML with special handling for env to ensure it's an inline table let tomlContent = ''; // Handle non-mcp_servers sections first const configWithoutMcpServers = { ...updatedConfig }; delete configWithoutMcpServers.mcp_servers; if (Object.keys(configWithoutMcpServers).length > 0) { tomlContent += (0, toml_1.stringify)(configWithoutMcpServers); } // Now handle mcp_servers with special formatting for env if (updatedConfig.mcp_servers && Object.keys(updatedConfig.mcp_servers).length > 0) { for (const [serverName, serverConfigRaw] of Object.entries(updatedConfig.mcp_servers)) { const serverConfig = serverConfigRaw; tomlContent += `\n[mcp_servers.${serverName}]\n`; // Add command if (serverConfig.command) { tomlContent += `command = "${serverConfig.command}"\n`; } // Add args if present if (serverConfig.args && Array.isArray(serverConfig.args)) { const argsStr = JSON.stringify(serverConfig.args) .replace(/"/g, '"') .replace(/,/g, ', '); tomlContent += `args = ${argsStr}\n`; } // Add env as inline table if present if (serverConfig.env && Object.keys(serverConfig.env).length > 0) { tomlContent += `env = { `; const entries = Object.entries(serverConfig.env); for (let i = 0; i < entries.length; i++) { const [key, value] = entries[i]; tomlContent += `${key} = "${value}"`; if (i < entries.length - 1) { tomlContent += ', '; } } tomlContent += ` }\n`; } } } await (0, FileSystemUtils_1.writeGeneratedFile)(configPath, tomlContent); } } getDefaultOutputPath(projectRoot) { return { instructions: path.join(projectRoot, 'AGENTS.md'), config: path.join(projectRoot, '.codex', 'config.toml'), }; } } exports.CodexCliAgent = CodexCliAgent;