/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { uiTelemetryService, triggerSessionEndHook, triggerSessionStartHook, SessionEndReason, SessionStartSource, } from '@vybestack/llxprt-code-core';
import { CommandKind } from './types.js';
import { getCliRuntimeServices } from '../../runtime/runtimeSettings.js';
function resolveForegroundGeminiClient(context) {
    if (context.services.config) {
        return context.services.config.getGeminiClient();
    }
    try {
        return getCliRuntimeServices().config.getGeminiClient();
    }
    catch {
        return null;
    }
}
export const clearCommand = {
    name: 'clear',
    description: 'clear the screen and conversation history',
    kind: CommandKind.BUILT_IN,
    autoExecute: true,
    action: async (context, _args) => {
        const geminiClient = resolveForegroundGeminiClient(context);
        if (geminiClient) {
            context.ui.setDebugMessage('Clearing terminal and resetting chat.');
            // Trigger SessionEnd hook before clearing (fail-open)
            if (context.services.config) {
                try {
                    await triggerSessionEndHook(context.services.config, SessionEndReason.Clear);
                }
                catch {
                    // Hooks are fail-open - continue even if hook fails
                }
            }
            await geminiClient.resetChat();
            // Trigger SessionStart hook after clearing (fail-open)
            if (context.services.config) {
                try {
                    await triggerSessionStartHook(context.services.config, SessionStartSource.Clear);
                }
                catch {
                    // Hooks are fail-open - continue even if hook fails
                }
            }
        }
        else {
            context.ui.setDebugMessage('Clearing terminal.');
        }
        uiTelemetryService.setLastPromptTokenCount(0);
        context.ui.updateHistoryTokenCount(0);
        context.ui.clear();
    },
};
//# sourceMappingURL=clearCommand.js.map