import { createControlPlaneRequestContext } from '../../../client-shared/api/links.js';
export const SLASH_COMMAND_REQUEST_TIMEOUT_MS = 5 * 60_000;
/**
 * Owns cli-v2 control-plane API calls that require terminal runtime defaults.
 */
export class ControlPlaneSessionApiService {
    client;
    defaults;
    constructor(options) {
        this.client = options.client;
        this.defaults = {
            defaultModel: options.defaultModel,
            maxSteps: options.maxSteps,
            searchIgnoreDirs: options.searchIgnoreDirs,
            systemContext: options.systemContext,
            apiKey: options.apiKey,
            preferApiKey: options.preferApiKey,
        };
    }
    async resolveWorkspaceId(workspaceId) {
        const state = await this.client.controlPlane.state.query(workspaceId ? { workspaceId } : undefined);
        const resolvedWorkspaceId = workspaceId ?? state.activeWorkspaceId;
        if (!resolvedWorkspaceId) {
            throw new Error('No active Heddle workspace is available from the control-plane API.');
        }
        return resolvedWorkspaceId;
    }
    async listSessions(workspaceId) {
        const result = await this.client.controlPlane.sessions.query({ workspaceId });
        return result.workspaceId === workspaceId ? result.sessions : [];
    }
    async getModelOptions() {
        return this.client.controlPlane.modelOptions.query();
    }
    async createSession(workspaceId, input = {}) {
        return this.client.controlPlane.sessionCreate.mutate({
            ...input,
            workspaceId,
            model: input.model ?? this.defaults.defaultModel,
        });
    }
    async getSession(workspaceId, sessionId) {
        return this.client.controlPlane.session.query({ id: sessionId, workspaceId });
    }
    async getRunning(workspaceId, sessionId) {
        return this.client.controlPlane.sessionRunning.query({ id: sessionId, workspaceId });
    }
    async getRunState(workspaceId, sessionId) {
        return this.client.controlPlane.sessionRunState.query({ id: sessionId, workspaceId });
    }
    async getRuntimeContext(workspaceId, sessionId) {
        return this.client.controlPlane.sessionRuntimeContext.query({ sessionId, workspaceId });
    }
    async getPendingApproval(workspaceId, sessionId) {
        return this.client.controlPlane.sessionPendingApproval.query({ id: sessionId, workspaceId });
    }
    async getSlashCommandCatalog(workspaceId) {
        return this.client.controlPlane.slashCommandCatalog.query({ workspaceId });
    }
    async updatePermissionMode(workspaceId, mode) {
        return this.client.controlPlane.workspacePermissionModeUpdate.mutate({ workspaceId, mode });
    }
    async searchWorkspaceFiles(input) {
        return this.client.controlPlane.workspaceFileSearch.query(input);
    }
    async sendPrompt(input) {
        return this.client.controlPlane.sessionSendPrompt.mutate({
            ...input,
            maxSteps: this.defaults.maxSteps,
            searchIgnoreDirs: this.defaults.searchIgnoreDirs,
            apiKey: this.defaults.apiKey,
            preferApiKey: this.defaults.preferApiKey,
            systemContext: this.defaults.systemContext,
        });
    }
    async sendPromptAsync(input) {
        return this.client.controlPlane.sessionSendPromptAsync.mutate({
            ...input,
            maxSteps: this.defaults.maxSteps,
            searchIgnoreDirs: this.defaults.searchIgnoreDirs,
            apiKey: this.defaults.apiKey,
            preferApiKey: this.defaults.preferApiKey,
            systemContext: this.defaults.systemContext,
        });
    }
    async preflightDirectShell(input) {
        return this.client.controlPlane.sessionDirectShellPreflight.query(input);
    }
    async runDirectShellAsync(input) {
        return this.client.controlPlane.sessionDirectShellAsync.mutate({
            ...input,
            apiKey: this.defaults.apiKey,
            preferApiKey: this.defaults.preferApiKey,
            systemContext: this.defaults.systemContext,
        });
    }
    async executeSlashCommand(input) {
        return this.client.controlPlane.slashCommandExecute.mutate(input, {
            context: createControlPlaneRequestContext({
                timeoutMs: SLASH_COMMAND_REQUEST_TIMEOUT_MS,
            }),
        });
    }
    async continueSession(workspaceId, sessionId) {
        return this.client.controlPlane.sessionContinue.mutate({
            id: sessionId,
            workspaceId,
            apiKey: this.defaults.apiKey,
            preferApiKey: this.defaults.preferApiKey,
        });
    }
    async cancelRun(workspaceId, sessionId) {
        return this.client.controlPlane.sessionCancel.mutate({ id: sessionId, workspaceId });
    }
    async resolvePendingApproval(workspaceId, sessionId, decision) {
        return this.client.controlPlane.sessionResolveApproval.mutate({
            workspaceId,
            sessionId,
            decision,
        });
    }
}
//# sourceMappingURL=control-plane-session-api-service.js.map