import { execFile } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import { providerCapability, providerDefaultEffort, providerDefaultModel } from './agent-provider-catalog.js';
function version(command, args) {
    return new Promise((resolve) => {
        execFile(command, args, { timeout: 3000 }, (error, stdout, stderr) => {
            if (error) {
                resolve(null);
                return;
            }
            resolve((stdout || stderr).trim() || null);
        });
    });
}
class CliProviderAdapter {
    async detect() {
        const detected = await version(this.command, ['--version']);
        return {
            ...providerCapability(this.id),
            label: this.label,
            command: this.command,
            available: detected !== null,
            version: detected
        };
    }
}
export class ClaudeCliAdapter extends CliProviderAdapter {
    id = 'claude';
    label = 'Claude Agent SDK';
    command = 'claude';
    buildCommand(session, _cwd, prompt, context) {
        const providerSessionId = session.providerSessionId || randomUUID();
        const model = session.model?.trim() || providerDefaultModel(session.provider);
        const effort = session.effort?.trim() || providerDefaultEffort(session.provider, model);
        const args = [
            '-p',
            '--verbose',
            '--output-format',
            'stream-json',
            '--include-partial-messages'
        ];
        if (session.providerSessionId) {
            args.push('--resume', providerSessionId);
        }
        else {
            args.push('--session-id', providerSessionId);
        }
        if (model)
            args.push('--model', model);
        if (effort)
            args.push('--effort', effort);
        const permissionMode = session.interactionMode === 'plan' ? 'plan' : session.permissionMode;
        if (permissionMode)
            args.push('--permission-mode', permissionMode);
        if (context?.extensions && context.projectId && context.projectPath) {
            const overrides = context.extensions.buildClaudeSpawnArgs(session.id, context.projectId, context.projectPath);
            if (overrides.settingsArg) {
                args.push('--settings', overrides.settingsArg);
            }
            if (overrides.mcpConfigFile) {
                args.push('--mcp-config', overrides.mcpConfigFile);
                if (overrides.strict)
                    args.push('--strict-mcp-config');
            }
        }
        args.push(prompt);
        return { command: this.command, args, mode: 'cli', providerSessionId };
    }
}
export class CodexCliAdapter extends CliProviderAdapter {
    id = 'codex';
    label = 'OpenAI Codex';
    command = 'codex';
    buildCommand(session, _cwd, _prompt, context) {
        const args = ['app-server'];
        if (context?.extensions && context.projectId) {
            const extras = context.extensions.buildCodexSpawnArgs(session.id, context.projectId);
            args.push(...extras);
        }
        return { command: this.command, args, mode: 'app-server' };
    }
}
const adapters = {
    claude: new ClaudeCliAdapter(),
    codex: new CodexCliAdapter()
};
export function providerAdapters() {
    return Object.values(adapters);
}
export function providerAdapter(provider) {
    return adapters[provider];
}
//# sourceMappingURL=agent-provider-adapters.js.map