/**
 * RPC Client for programmatic access to the coding agent.
 *
 * Spawns the agent in RPC mode and provides a typed API for all operations.
 */
import { spawn } from "node:child_process";
import { attachJsonlLineReader, serializeJsonLine } from "./jsonl.js";
// ============================================================================
// RPC Client
// ============================================================================
export class RpcClient {
    constructor(options = {}) {
        this.process = null;
        this.stopReadingStdout = null;
        this.eventListeners = [];
        this.pendingRequests = new Map();
        this.requestId = 0;
        this.stderr = "";
        this.exitError = null;
        this.options = options;
    }
    /** Start the RPC agent process. */
    async start() {
        if (this.process) {
            throw new Error("Client already started");
        }
        const cliPath = this.options.cliPath ?? "dist/cli.js";
        const args = ["--mode", "rpc"];
        if (this.options.provider) {
            args.push("--provider", this.options.provider);
        }
        if (this.options.model) {
            args.push("--model", this.options.model);
        }
        if (this.options.contextWindow !== undefined) {
            args.push("--context-window", String(this.options.contextWindow));
        }
        if (this.options.args) {
            args.push(...this.options.args);
        }
        this.exitError = null;
        const childProcess = spawn("node", [cliPath, ...args], {
            cwd: this.options.cwd,
            env: { ...process.env, ...this.options.env },
            stdio: ["pipe", "pipe", "pipe"],
        });
        this.process = childProcess;
        childProcess.once("exit", (code, signal) => {
            const error = this.createProcessExitError(code, signal);
            this.exitError = error;
            this.rejectPendingRequests(error);
        });
        childProcess.once("error", (error) => {
            this.exitError = error;
            this.rejectPendingRequests(error);
        });
        childProcess.stdin?.on("error", (error) => {
            this.exitError = error;
            this.rejectPendingRequests(error);
        });
        // Collect stderr for debugging
        childProcess.stderr?.on("data", (data) => {
            this.stderr += data.toString();
            process.stderr.write(data);
        });
        // Set up strict JSONL reader for stdout.
        this.stopReadingStdout = attachJsonlLineReader(childProcess.stdout, (line) => {
            this.handleLine(line);
        });
        // Wait a moment for process to initialize
        await new Promise((resolve) => setTimeout(resolve, 100));
        if (this.process.exitCode !== null) {
            throw new Error(`Agent process exited immediately with code ${this.process.exitCode}. Stderr: ${this.stderr}`);
        }
    }
    /** Stop the RPC agent process. */
    async stop() {
        if (!this.process)
            return;
        this.stopReadingStdout?.();
        this.stopReadingStdout = null;
        this.process.kill("SIGTERM");
        // Wait for process to exit
        await new Promise((resolve) => {
            const timeout = setTimeout(() => {
                this.process?.kill("SIGKILL");
                resolve();
            }, 1000);
            this.process?.on("exit", () => {
                clearTimeout(timeout);
                resolve();
            });
        });
        this.process = null;
        this.pendingRequests.clear();
    }
    /** Subscribe to agent events. */
    onEvent(listener) {
        this.eventListeners.push(listener);
        return () => {
            const index = this.eventListeners.indexOf(listener);
            if (index !== -1) {
                this.eventListeners.splice(index, 1);
            }
        };
    }
    /** Get collected stderr output (useful for debugging). */
    getStderr() {
        return this.stderr;
    }
    // =========================================================================
    // Command Methods
    // =========================================================================
    /**
     * Send a prompt to the agent.
     * Returns immediately after sending; use onEvent() to receive streaming events.
     * Use waitForIdle() to wait for completion.
     */
    async prompt(message, images) {
        await this.send({ type: "prompt", message, images });
    }
    /** Queue a steering message to interrupt the agent mid-run. */
    async steer(message, images) {
        await this.send({ type: "steer", message, images });
    }
    /** Queue a follow-up message to be processed after the agent finishes. */
    async followUp(message, images) {
        await this.send({ type: "follow_up", message, images });
    }
    /** Abort current operation. */
    async abort() {
        await this.send({ type: "abort" });
    }
    /**
     * Start a new session, optionally with parent tracking.
     * @param parentSession - Optional parent session path for lineage tracking
     * @returns Object with `cancelled: true` if an extension cancelled the new session
     */
    async newSession(parentSession) {
        const response = await this.send({ type: "new_session", parentSession });
        return this.getData(response);
    }
    /** Get current session state. */
    async getState() {
        const response = await this.send({ type: "get_state" });
        return this.getData(response);
    }
    /** Set model by provider and ID. */
    async setModel(provider, modelId) {
        const response = await this.send({ type: "set_model", provider, modelId });
        return this.getData(response);
    }
    /** Cycle to next model. */
    async cycleModel() {
        const response = await this.send({ type: "cycle_model" });
        return this.getData(response);
    }
    /** Get list of available models. */
    async getAvailableModels() {
        const response = await this.send({ type: "get_available_models" });
        return this.getData(response).models;
    }
    /** Set thinking level. */
    async setThinkingLevel(level) {
        await this.send({ type: "set_thinking_level", level });
    }
    /** Cycle thinking level. */
    async cycleThinkingLevel() {
        const response = await this.send({ type: "cycle_thinking_level" });
        return this.getData(response);
    }
    /**
     * Set the active context-window token budget for the current model.
     * This is a runtime selection and does not persist context-window defaults.
     */
    async setContextWindow(contextWindow) {
        const response = await this.send({ type: "set_context_window", contextWindow });
        this.assertSuccess(response);
    }
    /** Get selectable context-window token budgets for the current model. */
    async getAvailableContextWindows() {
        const response = await this.send({ type: "get_available_context_windows" });
        return this.getData(response);
    }
    /** Set steering mode. */
    async setSteeringMode(mode) {
        await this.send({ type: "set_steering_mode", mode });
    }
    /** Set follow-up mode. */
    async setFollowUpMode(mode) {
        await this.send({ type: "set_follow_up_mode", mode });
    }
    /** Compact session context with deletion-only verbatim context compaction. */
    async compact() {
        const response = await this.send({ type: "compact" });
        return this.getData(response);
    }
    /** @deprecated Use compact() instead. */
    async contextCompact() {
        const response = await this.send({ type: "context_compact" });
        return this.getData(response);
    }
    async setAutoCompaction(enabled) {
        await this.send({ type: "set_auto_compaction", enabled });
    }
    async setAutoRetry(enabled) {
        await this.send({ type: "set_auto_retry", enabled });
    }
    async abortRetry() {
        await this.send({ type: "abort_retry" });
    }
    async bash(command) {
        const response = await this.send({ type: "bash", command });
        return this.getData(response);
    }
    async abortBash() {
        await this.send({ type: "abort_bash" });
    }
    async getSessionStats() {
        const response = await this.send({ type: "get_session_stats" });
        return this.getData(response);
    }
    async exportHtml(outputPath) {
        const response = await this.send({ type: "export_html", outputPath });
        return this.getData(response);
    }
    async switchSession(sessionPath) {
        const response = await this.send({ type: "switch_session", sessionPath });
        return this.getData(response);
    }
    async fork(entryId) {
        const response = await this.send({ type: "fork", entryId });
        return this.getData(response);
    }
    async clone() {
        const response = await this.send({ type: "clone" });
        return this.getData(response);
    }
    async getForkMessages() {
        const response = await this.send({ type: "get_fork_messages" });
        return this.getData(response).messages;
    }
    /** Session entries in append order, optionally only those after the `since` entry id. */
    async getEntries(since) {
        const response = await this.send({ type: "get_entries", since });
        return this.getData(response);
    }
    /** The session entry tree. */
    async getTree() {
        const response = await this.send({ type: "get_tree" });
        return this.getData(response);
    }
    async getLastAssistantText() {
        const response = await this.send({ type: "get_last_assistant_text" });
        return this.getData(response).text;
    }
    async setSessionName(name) {
        await this.send({ type: "set_session_name", name });
    }
    async getMessages() {
        const response = await this.send({ type: "get_messages" });
        return this.getData(response).messages;
    }
    async getCommands() {
        const response = await this.send({ type: "get_commands" });
        return this.getData(response).commands;
    }
    waitForIdle(timeout = 60000) {
        return new Promise((resolve, reject) => {
            const timer = setTimeout(() => {
                unsubscribe();
                reject(new Error(`Timeout waiting for agent to become idle. Stderr: ${this.stderr}`));
            }, timeout);
            const unsubscribe = this.onEvent((event) => {
                if (event.type === "agent_end") {
                    clearTimeout(timer);
                    unsubscribe();
                    resolve();
                }
            });
        });
    }
    collectEvents(timeout = 60000) {
        return new Promise((resolve, reject) => {
            const events = [];
            const timer = setTimeout(() => {
                unsubscribe();
                reject(new Error(`Timeout collecting events. Stderr: ${this.stderr}`));
            }, timeout);
            const unsubscribe = this.onEvent((event) => {
                events.push(event);
                if (event.type === "agent_end") {
                    clearTimeout(timer);
                    unsubscribe();
                    resolve(events);
                }
            });
        });
    }
    async promptAndWait(message, images, timeout = 60000) {
        const eventsPromise = this.collectEvents(timeout);
        await this.prompt(message, images);
        return eventsPromise;
    }
    handleLine(line) {
        try {
            const data = JSON.parse(line);
            if (data.type === "response" && data.id && this.pendingRequests.has(data.id)) {
                const pending = this.pendingRequests.get(data.id);
                this.pendingRequests.delete(data.id);
                pending.resolve(data);
                return;
            }
            for (const listener of this.eventListeners) {
                listener(data);
            }
        }
        catch {
        }
    }
    createProcessExitError(code, signal) {
        return new Error(`Agent process exited (code=${code} signal=${signal}). Stderr: ${this.stderr}`);
    }
    rejectPendingRequests(error) {
        for (const { reject } of this.pendingRequests.values()) {
            reject(error);
        }
        this.pendingRequests.clear();
    }
    async send(command) {
        const childProcess = this.process;
        const stdin = childProcess?.stdin;
        if (!childProcess || !stdin) {
            throw new Error("Client not started");
        }
        if (this.exitError) {
            throw this.exitError;
        }
        if (childProcess.exitCode !== null) {
            const error = this.createProcessExitError(childProcess.exitCode, childProcess.signalCode);
            this.exitError = error;
            throw error;
        }
        if (stdin.destroyed || !stdin.writable) {
            throw new Error("Agent process stdin is not writable");
        }
        const id = `req_${++this.requestId}`;
        const fullCommand = { ...command, id };
        return new Promise((resolve, reject) => {
            const timeout = setTimeout(() => {
                this.pendingRequests.delete(id);
                reject(new Error(`Timeout waiting for response to ${command.type}. Stderr: ${this.stderr}`));
            }, 30000);
            this.pendingRequests.set(id, {
                resolve: (response) => {
                    clearTimeout(timeout);
                    resolve(response);
                },
                reject: (error) => {
                    clearTimeout(timeout);
                    reject(error);
                },
            });
            try {
                stdin.write(serializeJsonLine(fullCommand));
            }
            catch (error) {
                this.pendingRequests.delete(id);
                clearTimeout(timeout);
                reject(error instanceof Error ? error : new Error(String(error)));
            }
        });
    }
    assertSuccess(response) {
        if (!response.success) {
            throw new Error(response.error);
        }
    }
    getData(response) {
        this.assertSuccess(response);
        const successResponse = response;
        return successResponse.data;
    }
}
//# sourceMappingURL=rpc-client.js.map