import { waitForRawStdoutBackpressure } from "../../core/output-guard.js";
import { createRpcExtensionUIContext } from "./rpc-extension-ui.js";
export class RpcSessionBinding {
    constructor({ runtimeHost, output, pendingExtensionRequests, requestShutdown }) {
        this.runtimeHost = runtimeHost;
        this.output = output;
        this.pendingExtensionRequests = pendingExtensionRequests;
        this.requestShutdown = requestShutdown;
        this.session = runtimeHost.session;
    }
    get currentSession() {
        return this.session;
    }
    async rebindSession() {
        this.session = this.runtimeHost.session;
        const session = this.session;
        await session.bindExtensions({
            uiContext: createRpcExtensionUIContext({
                output: this.output,
                pendingExtensionRequests: this.pendingExtensionRequests,
            }),
            mode: "rpc",
            commandContextActions: {
                waitForIdle: () => this.session.agent.waitForIdle(),
                newSession: async (options) => this.runtimeHost.newSession(options),
                fork: async (entryId, forkOptions) => {
                    const result = await this.runtimeHost.fork(entryId, forkOptions);
                    return { cancelled: result.cancelled };
                },
                navigateTree: async (targetId, options) => {
                    const result = await this.session.navigateTree(targetId, {
                        summarize: options?.summarize,
                        customInstructions: options?.customInstructions,
                        replaceInstructions: options?.replaceInstructions,
                        label: options?.label,
                    });
                    return { cancelled: result.cancelled };
                },
                switchSession: async (sessionPath, options) => {
                    return this.runtimeHost.switchSession(sessionPath, options);
                },
                reload: async () => {
                    const steeringMode = this.session.steeringMode;
                    const followUpMode = this.session.followUpMode;
                    await this.session.reload();
                    this.session.setSteeringMode(steeringMode);
                    this.session.setFollowUpMode(followUpMode);
                },
            },
            shutdownHandler: this.requestShutdown,
            onError: (err) => {
                this.output({ type: "extension_error", extensionPath: err.extensionPath, event: err.event, error: err.error });
            },
        });
        this.disposeSubscriptions();
        this.unsubscribe = session.subscribe((event) => {
            this.output(event);
        });
        this.unsubscribeBackpressure = session.agent.subscribe(async () => {
            await waitForRawStdoutBackpressure();
        });
    }
    disposeSubscriptions() {
        this.unsubscribe?.();
        this.unsubscribeBackpressure?.();
        this.unsubscribe = undefined;
        this.unsubscribeBackpressure = undefined;
    }
}
//# sourceMappingURL=rpc-session-binding.js.map