import { ProviderCredentialCommandService } from '../../../core/auth/index.js';
import { ChatSessionRecords } from '../../../core/chat/engine/sessions/records/index.js';
import { FileHeartbeatTaskService } from '../../../core/heartbeat/index.js';
import { controlPlaneSessionRuntimeContextService } from './session-runtime-context-service.js';
/**
 * Composes core slash-command ports from resolved control-plane runtime context.
 */
export class ControlPlaneSlashCommandExecutionContextService {
    create(args, hints) {
        const resolved = controlPlaneSessionRuntimeContextService.resolve(args);
        const { runtimeContext, sessions } = resolved;
        const heartbeatTasks = new FileHeartbeatTaskService({ stateRoot: args.stateRoot });
        return {
            model: {
                active: () => runtimeContext.model,
                setActive: (model) => {
                    sessions.updateSettings(args.sessionId, { model });
                },
                activeReasoningEffort: () => runtimeContext.reasoningEffort,
                setReasoningEffort: (reasoningEffort) => {
                    sessions.updateSettings(args.sessionId, { reasoningEffort });
                },
                credentialSource: () => runtimeContext.credentialSource,
            },
            auth: {
                status: () => ProviderCredentialCommandService.formatStatusMessage(args.credentialStorePath),
                login: (provider) => ProviderCredentialCommandService.loginProviderWithOAuth(provider, {
                    storePath: args.credentialStorePath,
                }),
                logout: (provider) => ProviderCredentialCommandService.logoutProvider(provider, args.credentialStorePath),
            },
            compaction: {
                compactActive: args.compactActive,
            },
            drift: {
                status: () => ({ enabled: runtimeContext.driftEnabled }),
                setEnabled: (enabled) => {
                    sessions.setDriftEnabled(args.sessionId, enabled);
                },
            },
            session: {
                all: () => sessions.listExisting(),
                recent: () => this.recentSessions(sessions.listExisting()),
                recentListMessage: () => this.recentSessionMessages(sessions.listExisting()),
                create: (name) => sessions.create({
                    name,
                    model: runtimeContext.model,
                    workspaceId: args.workspaceId,
                }),
                switch: (id) => {
                    sessions.require(id);
                },
                rename: (name) => {
                    sessions.rename(args.sessionId, name);
                },
                remove: (id) => {
                    sessions.delete(id);
                },
                clear: () => {
                    sessions.resetConversation(args.sessionId);
                },
                summarize: ChatSessionRecords.summarize,
            },
            heartbeat: {
                listTasks: async () => await heartbeatTasks.listTasks(),
                listRunRecords: async (options) => await heartbeatTasks.listRunRecords(options),
                loadRunRecord: async (id) => await heartbeatTasks.loadRunRecord(id),
            },
            help: {
                message: () => this.formatHelpMessage(hints),
            },
        };
    }
    recentSessions(sessions) {
        return [...sessions].sort((left, right) => (right.updatedAt ?? '').localeCompare(left.updatedAt ?? '')).slice(0, 10);
    }
    recentSessionMessages(sessions) {
        return this.recentSessions(sessions).map((session, index) => (`${index + 1}. ${session.id} (${session.name}) - ${ChatSessionRecords.summarize(session)}`));
    }
    formatHelpMessage(hints) {
        return [
            'Slash commands',
            '',
            ...hints.flatMap((hint) => [hint.command, capitalizeFirst(hint.description), '']),
        ].join('\n').trimEnd();
    }
}
export const controlPlaneSlashCommandExecutionContextService = new ControlPlaneSlashCommandExecutionContextService();
function capitalizeFirst(value) {
    return value.length > 0 ? `${value[0]?.toUpperCase()}${value.slice(1)}.` : value;
}
//# sourceMappingURL=slash-command-execution-context-service.js.map