import { debug } from "../../engine/core/logger.js";
import { hookCommand } from "../shared/hook_command.js";
function escapeTomlString(value) {
    return value
        .replace(/\\/g, "\\\\")
        .replace(/"/g, '\\"')
        .replace(/\n/g, "\\n")
        .replace(/\t/g, "\\t")
        .replace(/\r/g, "\\r");
}
function toTomlArray(items) {
    const escaped = items.map((s) => `"${escapeTomlString(s)}"`);
    return `[${escaped.join(", ")}]`;
}
/**
 * 生成 Codex MCP 服务器 TOML 配置。
 * @param projectPath - 项目路径
 * @returns TOML 格式的配置字符串
 */
export function generateCodexMcpConfig(projectPath) {
    debug("Codex配置", "生成 MCP 配置 —", projectPath);
    return `[mcp_servers.soloforge]
command = "soloforge"
args = ${toTomlArray(["mcp"])}

[mcp_servers.soloforge.env]
SOLOFORGE_PROJECT = "${escapeTomlString(projectPath)}"

[mcp_servers.playwright]
command = "npx"
args = ${toTomlArray(["@playwright/mcp@latest", "--headless"])}

[features]
hooks = true
`;
}
/**
 * 生成 Codex hooks 配置对象。
 * @returns hooks 配置对象
 */
export function generateCodexHooksConfig() {
    debug("Codex配置", "生成 Hooks 配置");
    return JSON.stringify({
        hooks: {
            PreToolUse: [
                {
                    matcher: "^(apply_patch|Edit|Write)$",
                    hooks: [
                        {
                            type: "command",
                            command: hookCommand("check-write"),
                            timeout: 15,
                        },
                    ],
                },
                {
                    matcher: "^Bash$",
                    hooks: [
                        {
                            type: "command",
                            command: hookCommand("check-bash"),
                            timeout: 15,
                        },
                    ],
                },
            ],
            PostToolUse: [
                {
                    matcher: "^Bash$",
                    hooks: [
                        {
                            type: "command",
                            command: hookCommand("post-bash"),
                            timeout: 10,
                        },
                    ],
                },
            ],
        },
    }, null, 2);
}
//# sourceMappingURL=codex_config.js.map