import { buildPiLaunch } from "../runtime.js";
export class FakePi {
    constructor(command = ["pi"]) {
        this.recordedLaunches = [];
        this.sessions = [];
        this.queuedCommands = [];
        this.command = command;
    }
    async startSession(input) {
        const launch = buildPiLaunch({
            command: this.command,
            session: input,
        });
        this.recordedLaunches.push(launch);
        const session = new FakePiSession(launch);
        session.commands = this.queuedCommands.shift() ?? [];
        this.sessions.push(session);
        return session;
    }
    queueCommands(commands) {
        this.queuedCommands.push(commands);
    }
    latestSession() {
        const session = this.sessions.at(-1);
        if (!session) {
            throw new Error("FakePi has no sessions");
        }
        return session;
    }
}
export class FakePiSession {
    constructor(launch) {
        this.prompts = [];
        this.setModelRequests = [];
        this.setThinkingLevelRequests = [];
        this.treeNavigationRequests = [];
        this.capturedUserEntries = [];
        this.abortRequested = false;
        this.canceledExtensionUiRequests = [];
        this.extensionUiResponses = [];
        this.setModelResult = null;
        this.models = [];
        this.messages = [];
        this.stats = {
            tokens: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
            cost: 0,
        };
        this.commands = [];
        this.subscribers = new Set();
        this.state = {
            model: null,
            thinkingLevel: "medium",
            isStreaming: false,
            isCompacting: false,
            sessionFile: launch.session ?? "/tmp/pi-session",
            sessionId: "pi-session-1",
            messageCount: 0,
            pendingMessageCount: 0,
        };
    }
    onEvent(callback) {
        this.subscribers.add(callback);
        return () => {
            this.subscribers.delete(callback);
        };
    }
    async prompt(message, images) {
        this.prompts.push({ message, imageCount: images?.length ?? 0 });
        this.handleTreeNavigationCommand(message);
        this.handleEntryCaptureCommand(message);
    }
    async abort() {
        this.abortRequested = true;
    }
    async getState() {
        return this.state;
    }
    async getMessages() {
        return this.messages;
    }
    async getAvailableModels() {
        return this.models;
    }
    async setModel(provider, modelId) {
        this.setModelRequests.push({ provider, modelId });
        if (!this.setModelResult) {
            throw new Error("FakePi setModel requires setModelResult to be scripted");
        }
        return this.setModelResult;
    }
    async setThinkingLevel(level) {
        this.setThinkingLevelRequests.push(level);
    }
    async getSessionStats() {
        return this.stats;
    }
    async getCommands() {
        return this.commands;
    }
    respondToExtensionUiRequest(id, response) {
        this.extensionUiResponses.push({ id, response });
    }
    cancelExtensionUiRequest(id) {
        this.canceledExtensionUiRequests.push(id);
        this.respondToExtensionUiRequest(id, { cancelled: true });
    }
    async close() { }
    emit(event) {
        for (const subscriber of this.subscribers) {
            subscriber(event);
        }
    }
    finishTurn(message = { role: "assistant", content: [] }) {
        this.messages = [...this.messages, message];
        this.emit({ type: "agent_end", messages: this.messages });
    }
    handleTreeNavigationCommand(message) {
        const prefix = "/paseo_tree ";
        if (!message.startsWith(prefix)) {
            return;
        }
        const payload = JSON.parse(Buffer.from(message.slice(prefix.length), "base64url").toString("utf8"));
        if (typeof payload.targetId !== "string" || typeof payload.requestId !== "string") {
            return;
        }
        this.treeNavigationRequests.push(payload.targetId);
        this.emitEntryCapture(undefined, "tree_navigation");
        this.emitExtensionCommandResult(payload.requestId, { ok: true, result: {} });
    }
    handleEntryCaptureCommand(message) {
        const prefix = "/paseo_capture_entries ";
        if (!message.startsWith(prefix)) {
            return;
        }
        const payload = JSON.parse(Buffer.from(message.slice(prefix.length), "base64url").toString("utf8"));
        if (typeof payload.requestId !== "string") {
            return;
        }
        this.emitEntryCapture(payload.requestId, typeof payload.reason === "string" ? payload.reason : "command");
    }
    emitEntryCapture(requestId, reason = "test") {
        this.emit({
            type: "extension_ui_request",
            id: `capture-${requestId ?? reason}`,
            method: "notify",
            message: `PASEO_ENTRY_CAPTURE ${JSON.stringify({
                reason,
                requestId,
                entries: this.capturedUserEntries,
            })}`,
        });
    }
    emitExtensionCommandResult(requestId, result) {
        this.emit({
            type: "extension_ui_request",
            id: `command-${requestId}`,
            method: "notify",
            message: `PASEO_COMMAND_RESULT ${JSON.stringify({ requestId, ...result })}`,
        });
    }
}
//# sourceMappingURL=fake-pi.js.map