"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.grokOrchestrator = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_os_1 = __importDefault(require("node:os"));
const node_path_1 = __importDefault(require("node:path"));
const GROK_CONFIG_DIR = node_path_1.default.join(node_os_1.default.homedir(), '.grok');
const GROK_BIN_PATH = node_path_1.default.join(GROK_CONFIG_DIR, 'bin', 'grok');
/**
 * Grok Build CLI (xAI) — pickup-via-Claude orchestrator.
 *
 * Verified 2026-05-15: Grok Build reads `~/.claude.json` natively and
 * shows chorus under its merged MCP server list (`grok inspect` →
 * "chorus (stdio) config"). It does NOT need its own `grok mcp add`
 * call — the entry registered by the claude orchestrator is reused.
 *
 * Implications:
 *   - When the user has already run `chorus connect claude`, Grok
 *     auto-picks chorus from the same file. Zero additional config.
 *   - When the user hasn't connected claude, Grok won't see chorus.
 *     Solution: tell them to connect claude first.
 *
 * This orchestrator therefore reports `connected = true` when it
 * detects the grok binary AND chorus is present in `~/.claude.json`
 * (under either top-level `mcpServers` or any project-scoped
 * `projects.*.mcpServers`). `supported = true` so the /connect card
 * shows in the normal section. `connect()` is a no-op that just
 * tells the user to wire claude — no duplicate MCP entry needed.
 *
 * Authentication: `grok login` (browser) or `GROK_DEPLOYMENT_KEY`
 * env var. Headless `grok -p` invocation needs SuperGrok Heavy.
 * Like every other CLI, unauthenticated grok surfaces as
 * `auth_missing` at dispatch time — handled by the existing health
 * + voice auto-disable machinery, no shim-specific code needed.
 */
function hasChorusInClaudeJson() {
    const claudeJson = node_path_1.default.join(node_os_1.default.homedir(), '.claude.json');
    if (!node_fs_1.default.existsSync(claudeJson))
        return false;
    try {
        const config = JSON.parse(node_fs_1.default.readFileSync(claudeJson, 'utf-8'));
        if (config.mcpServers && 'chorus' in config.mcpServers)
            return true;
        for (const project of Object.values(config.projects ?? {})) {
            if (project?.mcpServers && 'chorus' in project.mcpServers)
                return true;
        }
        return false;
    }
    catch {
        return false;
    }
}
function getGrokStatus() {
    const detected = node_fs_1.default.existsSync(GROK_BIN_PATH) || node_fs_1.default.existsSync(GROK_CONFIG_DIR);
    const connected = detected && hasChorusInClaudeJson();
    return {
        name: 'grok',
        label: 'Grok Build',
        connected,
        approvedTools: connected ? 1 : 0,
        totalTools: 1,
        note: connected
            ? 'Two-way wired: (1) Grok Build reads ~/.claude.json automatically, so it can call chorus.* tools; (2) chorus dispatches to grok-build as a reviewer via the new shim. SuperGrok Heavy subscription required for invocation — free-tier returns 403 cleanly.'
            : 'Grok Build picks up chorus from ~/.claude.json. Run `chorus connect claude` first; Grok will then see chorus automatically. Chorus can also dispatch to grok-build as a reviewer (SuperGrok Heavy subscription required).',
        supported: detected,
        firstCallBehavior: 'inherits_global',
    };
}
exports.grokOrchestrator = {
    name: 'grok',
    label: 'Grok Build',
    getStatus: getGrokStatus,
    detect: () => node_fs_1.default.existsSync(GROK_BIN_PATH) || node_fs_1.default.existsSync(GROK_CONFIG_DIR),
    connect: async (_opts) => {
        // No-op: Grok Build auto-discovers chorus from ~/.claude.json via
        // its config merge. Tell the user to wire claude (if not already)
        // and they're done.
        if (!hasChorusInClaudeJson()) {
            throw new Error('Grok Build reads chorus MCP from ~/.claude.json — but no chorus ' +
                'entry exists there yet. Run `chorus connect claude` first, then ' +
                'Grok will see chorus automatically.');
        }
        return {
            registered: false,
            toolsAdded: 0,
            slashCommand: 'skipped',
            full: {
                added: [],
                alreadyPresent: ['mcpServers.chorus (via ~/.claude.json)'],
                configPath: node_path_1.default.join(node_os_1.default.homedir(), '.claude.json'),
                slashCommand: 'skipped',
                slashCommandPath: '',
            },
        };
    },
};
//# sourceMappingURL=grok.js.map