"use strict";
// src/orchestrator/story/HostInvoker.ts
// v1.17.0 — Pluggable adapter interface for spawning a host CLI
// (claude-cli, gemini-cli, codex) with a prompt and capturing structured
// output. Per-provider adapters live under ./adapters/.
Object.defineProperty(exports, "__esModule", { value: true });
exports.HostInvoker = void 0;
class AdapterRegistry {
    adapters = new Map();
    register(adapter) {
        this.adapters.set(adapter.name, adapter);
    }
    get(name) {
        return this.adapters.get(name) || null;
    }
    list() {
        return Array.from(this.adapters.keys());
    }
    /**
     * Lazy-load default adapters. Idempotent and non-destructive: only
     * registers a default if the registry is missing it — never overwrites
     * an adapter that a caller already registered (so tests can inject
     * fakes).
     */
    ensureDefaults() {
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { ClaudeAdapter } = require('./adapters/ClaudeAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { GeminiAdapter } = require('./adapters/GeminiAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { CodexAdapter } = require('./adapters/CodexAdapter');
        // eslint-disable-next-line @typescript-eslint/no-var-requires
        const { MockAdapter } = require('./adapters/MockAdapter');
        if (!this.adapters.has('claude'))
            this.register(new ClaudeAdapter());
        if (!this.adapters.has('gemini'))
            this.register(new GeminiAdapter());
        if (!this.adapters.has('codex'))
            this.register(new CodexAdapter());
        if (!this.adapters.has('mock'))
            this.register(new MockAdapter());
    }
}
const registry = new AdapterRegistry();
exports.HostInvoker = {
    /** Manually register an adapter (used by tests). */
    register(adapter) {
        registry.register(adapter);
    },
    /** Invoke via the named provider, or auto-route via OPENLIFE_HOST_ADAPTER env. */
    async invoke(prompt, opts = {}) {
        registry.ensureDefaults();
        const name = opts.provider || process.env.OPENLIFE_HOST_ADAPTER || 'claude';
        const adapter = registry.get(name);
        if (!adapter) {
            return {
                ok: false,
                text: '',
                exitCode: -1,
                durationMs: 0,
                adapter: name,
                error: `adapter_not_registered: ${name}`,
            };
        }
        if (!(await adapter.isAvailable())) {
            return {
                ok: false,
                text: '',
                exitCode: -2,
                durationMs: 0,
                adapter: name,
                error: `adapter_unavailable: ${name} (CLI not on PATH or env missing)`,
            };
        }
        return adapter.invoke(prompt, opts);
    },
    /** Exposed for tests + diagnostics. */
    _listRegistered() {
        registry.ensureDefaults();
        return registry.list();
    },
    _resetForTests() {
        // Force re-load on next ensureDefaults() — used between test scenarios.
        registry['adapters'].clear();
    },
};
