import * as acp from "@agentclientprotocol/sdk";
export class AcpClientRuntime {
    options;
    connection;
    sessionId;
    constructor(options) {
        this.options = options;
        this.connection = new acp.ClientSideConnection(() => ({
            requestPermission: async (params) => this.requestPermission(params),
            sessionUpdate: async (params) => {
                await this.options.onSessionUpdate?.(params);
            },
        }), options.stream);
    }
    async start() {
        await this.connection.initialize({
            protocolVersion: acp.PROTOCOL_VERSION,
            clientCapabilities: {
                fs: {
                    readTextFile: false,
                    writeTextFile: false,
                },
                terminal: false,
            },
        });
        const session = await this.connection.newSession({
            cwd: this.options.cwd,
            mcpServers: [],
        });
        this.sessionId = session.sessionId;
    }
    async prompt(text) {
        if (!this.sessionId) {
            throw new Error("ACP session is not started.");
        }
        return this.connection.prompt({
            sessionId: this.sessionId,
            prompt: [{ type: "text", text }],
        });
    }
    async cancel() {
        if (!this.sessionId) {
            return;
        }
        await this.connection.cancel({ sessionId: this.sessionId });
    }
    async stop() {
        await this.cancel().catch(() => { });
    }
    async requestPermission(params) {
        const allow = params.options.find((option) => option.kind === "allow_always") ??
            params.options.find((option) => option.kind === "allow_once");
        if (allow) {
            return { outcome: { outcome: "selected", optionId: allow.optionId } };
        }
        const reject = params.options.find((option) => option.kind === "reject_once") ??
            params.options.find((option) => option.kind === "reject_always");
        if (reject) {
            return { outcome: { outcome: "selected", optionId: reject.optionId } };
        }
        return { outcome: { outcome: "cancelled" } };
    }
}
//# sourceMappingURL=acp-client-runtime.js.map