Skip to content

Build your personal knowledge base with Trilium Notes

13.8 KB
Raw Blame
/**
 * Minimal Agent Client Protocol (ACP) client: newline-delimited JSON-RPC 2.0
 * over a subprocess's stdio, as spoken by `copilot --acp`, Google's
 * `agy_acp_server` and other ACP agents (see https://agentclientprotocol.com/).
 *
 * Deliberately dependency-free and transport-only: protocol semantics
 * (initialize, session/new, session/prompt, permission policy) live in the
 * provider. The client handles framing, request/response correlation,
 * agent→client requests, and the agent's lifecycle, whether it runs as a
 * subprocess or as a Node script in a worker thread.
 */

import { getLog } from "@triliumnext/core";
import { type ChildProcessWithoutNullStreams, spawn } from "child_process";
import { createInterface } from "readline";
import type { Readable, Writable } from "stream";
import { Worker } from "worker_threads";

/** How long a disposed agent has to exit on its own before it is killed. */
const DISPOSE_GRACE_MS = 5_000;

interface JsonRpcMessage {
    jsonrpc: "2.0";
    id?: number | string;
    method?: string;
    params?: unknown;
    result?: unknown;
    error?: { code: number; message: string; data?: unknown };
}

export class AcpError extends Error {
    constructor(
        public readonly code: number,
        message: string,
        public readonly data?: unknown
    ) {
        super(message);
        this.name = "AcpError";
    }
}

export interface AcpClientOptions {
    cwd: string;
    /**
     * CLI arguments, passed as-is. Each agent names its ACP mode differently
     * (`copilot --acp`; Google's `agy_acp_server` speaks nothing else).
     */
    args?: string[];
    /** Variables set on top of the server's own environment. */
    env?: Record<string, string>;
    /**
     * Launch through a shell (required for npm `.cmd` shims on Windows). The
     * binary path is quoted by the client when set.
     */
    shell?: boolean;
    /** Called for every notification (no `id`) the agent sends. */
    onNotification?: (method: string, params: unknown) => void;
    /**
     * Called for every agent→client *request* (has an `id`; e.g.
     * `session/request_permission`). The returned value is sent as the
     * response result; a thrown error becomes a JSON-RPC error response.
     * When no handler is set, requests are answered with "method not found".
     */
    onAgentRequest?: (method: string, params: unknown) => Promise<unknown> | unknown;
    /**
     * Called once when the subprocess dies on its own (crash, kill, agent
     * exit) — never for a deliberate {@link AcpClient.dispose}. A pooled
     * client uses this to evict itself so the next turn starts a fresh agent
     * instead of handing out a corpse.
     */
    onExit?: (error: Error) => void;
}

/** Where the agent runs: a subprocess, or a script in a worker thread of this process. */
interface AgentHost {
    stdin: Writable;
    stdout: Readable;
    stderr: Readable;
    /** Called when the agent cannot start, or its worker throws. */
    onError(listener: (err: Error) => void): void;
    /** Called when the agent ends, with how it ended (`SIGTERM`, `code 1`). */
    onExit(listener: (how: string) => void): void;
    kill(): void;
}

export class AcpClient {
    private nextId = 1;
    private readonly pending = new Map<number, { resolve: (msg: JsonRpcMessage) => void; reject: (err: Error) => void }>();
    private exitError: Error | undefined;
    private disposed = false;
    private exited = false;

    private constructor(
        private readonly proc: AgentHost,
        private readonly options: AcpClientOptions
    ) {
        const rl = createInterface({ input: proc.stdout });
        rl.on("line", line => this.handleLine(line));

        proc.stderr.on("data", (data: Buffer) => {
            const text = data.toString().trim();
            if (text) {
                getLog().info(`ACP agent stderr: ${text}`);
            }
        });

        // Writing to a subprocess that already exited (a late session/cancel, or
        // an agent-request reply resumed after dispose()) emits EPIPE on stdin.
        // An unhandled stream "error" event would take the server down, and
        // failAll() has already rejected everything in flight — there is nothing
        // left to report.
        proc.stdin.on("error", () => {});

        proc.onError(err => this.die(new Error(`Failed to start the ACP agent: ${err.message}`)));
        proc.onExit(how => {
            this.exited = true;
            // A deliberate dispose() ends the subprocess — that exit is expected
            // and must not surface as an error for in-flight (cancelled) requests.
            if (!this.disposed) {
                this.die(new Error(`The ACP agent exited unexpectedly (${how}).`));
            }
        });
    }

    /**
     * Run the agent as a subprocess. Outside Windows it leads its own process
     * group, so a Ctrl+C in the server's terminal does not reach it: the server
     * ends it through {@link dispose} or {@link killLiveAgents}, which kill the
     * whole group, including the processes the agent started.
     */
    static start(binary: string, options: AcpClientOptions): AcpClient {
        const proc = spawn(
            options.shell ? `"${binary}"` : binary,
            options.args ?? [],
            {
                cwd: options.cwd,
                shell: options.shell ?? false,
                detached: process.platform !== "win32",
                stdio: ["pipe", "pipe", "pipe"],
                env: options.env ? { ...process.env, ...options.env } : process.env
            }
        );
        trackLiveAgent(proc);
        return new AcpClient({
            stdin: proc.stdin,
            stdout: proc.stdout,
            stderr: proc.stderr,
            onError: listener => proc.on("error", listener),
            onExit: listener => proc.on("exit", (code, sig) => listener(sig ?? `code ${code}`)),
            kill: () => killProcessTree(proc)
        }, options);
    }

    /**
     * Run an agent that ships as a Node script in a worker thread of this
     * process, speaking ACP over the worker's stdio. It needs no Node binary on
     * the host, which a packaged desktop build cannot provide: its `RunAsNode`
     * fuse is off. The worker shares this process's working directory, so
     * `cwd` and `shell` do not apply.
     */
    static startWorker(script: string, options: AcpClientOptions): AcpClient {
        const worker = new Worker(script, {
            argv: options.args ?? [],
            env: { ...process.env, ...options.env },
            stdin: true,
            stdout: true,
            stderr: true
        });
        const stdin = worker.stdin;
        if (!stdin) {
            throw new Error("The ACP agent's worker has no stdin.");
        }
        return new AcpClient({
            stdin,
            stdout: worker.stdout,
            stderr: worker.stderr,
            onError: listener => worker.on("error", listener),
            onExit: listener => worker.on("exit", code => listener(`code ${code}`)),
            kill: () => void worker.terminate()
        }, options);
    }

    /** Send a request and await its response result (rejects with {@link AcpError} on error responses). */
    async request<T = unknown>(method: string, params: unknown, timeoutMs = 120_000): Promise<T> {
        if (this.exitError) {
            throw this.exitError;
        }
        const id = this.nextId++;
        const response = await new Promise<JsonRpcMessage>((resolve, reject) => {
            const timer = setTimeout(() => {
                this.pending.delete(id);
                reject(new Error(`ACP request "${method}" timed out after ${Math.round(timeoutMs / 1000)} s.`));
            }, timeoutMs);
            this.pending.set(id, {
                resolve: msg => {
                    clearTimeout(timer);
                    resolve(msg);
                },
                reject: err => {
                    clearTimeout(timer);
                    reject(err);
                }
            });
            try {
                this.send({ jsonrpc: "2.0", id, method, params });
            } catch (err) {
                // A synchronous write failure would otherwise leave the timer
                // armed and the id stranded in `pending` until it elapses.
                clearTimeout(timer);
                this.pending.delete(id);
                reject(err instanceof Error ? err : new Error(String(err)));
            }
        });
        if (response.error) {
            throw new AcpError(response.error.code, response.error.message, response.error.data);
        }
        return response.result as T;
    }

    /** Send a notification (fire-and-forget, e.g. `session/cancel`). */
    notify(method: string, params: unknown): void {
        this.send({ jsonrpc: "2.0", method, params });
    }

    /**
     * Reject anything still in flight and end the subprocess: close its stdin so
     * it can exit on its own, and kill it only if it is still running after
     * {@link DISPOSE_GRACE_MS}. The kill is a SIGKILL, because an agent that is
     * still running after the grace period might not act on a SIGTERM.
     */
    dispose(): void {
        if (this.disposed) {
            return;
        }
        this.disposed = true;
        this.failAll(new Error("The ACP client was disposed."));
        this.proc.stdin.end();
        const killTimer = setTimeout(() => {
            if (!this.exited) {
                this.proc.kill();
            }
        }, DISPOSE_GRACE_MS);
        killTimer.unref();
    }

    private send(message: JsonRpcMessage): void {
        this.proc.stdin.write(`${JSON.stringify(message)}\n`);
    }

    private handleLine(line: string): void {
        if (!line.trim()) {
            return;
        }
        let message: JsonRpcMessage;
        try {
            const parsed: unknown = JSON.parse(line);
            if (!parsed || typeof parsed !== "object") {
                // Valid JSON, but not a protocol message (a bare `null` would
                // otherwise blow up on the property reads below).
                return;
            }
            message = parsed as JsonRpcMessage;
        } catch {
            // Not part of the protocol stream (e.g. a stray banner) — ignore.
            return;
        }

        if (message.id !== undefined && message.method === undefined) {
            // Response to one of our requests.
            const entry = this.pending.get(message.id as number);
            if (entry) {
                this.pending.delete(message.id as number);
                entry.resolve(message);
            }
        } else if (message.method !== undefined && message.id !== undefined) {
            void this.answerAgentRequest(message.id, message.method, message.params);
        } else if (message.method !== undefined) {
            this.options.onNotification?.(message.method, message.params);
        }
    }

    private async answerAgentRequest(id: number | string, method: string, params: unknown): Promise<void> {
        const handler = this.options.onAgentRequest;
        if (!handler) {
            this.send({ jsonrpc: "2.0", id, error: { code: -32601, message: `Client does not support "${method}".` } });
            return;
        }
        try {
            const result = await handler(method, params);
            this.send({ jsonrpc: "2.0", id, result });
        } catch (err) {
            const text = err instanceof Error ? err.message : String(err);
            this.send({ jsonrpc: "2.0", id, error: { code: -32603, message: text } });
        }
    }

    /** Whether the subprocess is still usable (not disposed, not exited). */
    get alive(): boolean {
        return !this.disposed && this.exitError === undefined;
    }

    /**
     * The subprocess died on its own. Fails everything in flight, then tells
     * the owner exactly once so a pooled client can evict itself.
     */
    private die(error: Error): void {
        const alreadyDead = this.exitError !== undefined;
        this.failAll(error);
        if (!alreadyDead) {
            this.options.onExit?.(error);
        }
    }

    private failAll(error: Error): void {
        this.exitError = error;
        for (const entry of this.pending.values()) {
            entry.reject(error);
        }
        this.pending.clear();
    }
}

/** Subprocess agents that have not exited yet, killed by {@link killLiveAgents} when the server exits. */
const liveAgents = new Set<ChildProcessWithoutNullStreams>();

function trackLiveAgent(proc: ChildProcessWithoutNullStreams): void {
    if (!process.listeners("exit").includes(killLiveAgents)) {
        process.on("exit", killLiveAgents);
    }
    liveAgents.add(proc);
    proc.on("exit", () => liveAgents.delete(proc));
}

/**
 * Kill every subprocess agent that is still running. The server's `exit`
 * handler calls it, so an agent cannot outlive the server.
 */
export function killLiveAgents(): void {
    for (const proc of liveAgents) {
        killProcessTree(proc);
    }
}

/**
 * End the agent and every process it started. On Windows `proc.kill()`
 * ends only the spawned process, while the agent can run in a child of it:
 * the PyInstaller build of `agy_acp_server` starts its server that way, and
 * a `.cmd` shim runs the CLI under `cmd.exe`. `taskkill /T` ends the tree.
 * Elsewhere the agent leads its own process group, and the negative PID
 * signals the whole group.
 */
function killProcessTree(proc: ChildProcessWithoutNullStreams): void {
    if (proc.pid === undefined) {
        return;
    }
    if (process.platform === "win32") {
        const taskkill = spawn("taskkill", [ "/pid", String(proc.pid), "/T", "/F" ], { stdio: "ignore", windowsHide: true });
        taskkill.on("error", err => getLog().error(`Failed to end the ACP agent's process tree: ${err.message}`));
        return;
    }
    try {
        process.kill(-proc.pid, "SIGKILL");
    } catch {
        // ESRCH: the group exited between the check and the signal.
    }
}