/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { CommandKind, } from './types.js';
import { getRuntimeApi } from '../contexts/RuntimeContext.js';
import * as os from 'node:os';
import * as path from 'node:path';
const validModes = ['now', 'status', 'on', 'error', 'off'];
function isValidMode(mode) {
    return validModes.includes(mode);
}
/**
 * Schema for /dumpcontext command argument completion
 */
const dumpcontextSchema = [
    {
        kind: 'literal',
        value: 'now',
        description: 'Dump context on next request only',
    },
    {
        kind: 'literal',
        value: 'status',
        description: 'Show current dump status (default)',
    },
    {
        kind: 'literal',
        value: 'on',
        description: 'Dump context before every request',
    },
    {
        kind: 'literal',
        value: 'error',
        description: 'Dump context only on errors',
    },
    {
        kind: 'literal',
        value: 'off',
        description: 'Disable context dumping',
    },
];
export const dumpcontextCommand = {
    name: 'dumpcontext',
    description: 'Control context dumping: now, status, on, error, off (default: status)',
    kind: CommandKind.BUILT_IN,
    schema: dumpcontextSchema,
    action: async (context, args) => {
        try {
            const runtime = getRuntimeApi();
            const mode = args?.trim().toLowerCase() || 'status';
            if (!isValidMode(mode)) {
                return {
                    type: 'message',
                    messageType: 'error',
                    content: `Invalid mode '${mode}'. Valid modes are: ${validModes.join(', ')}`,
                };
            }
            const dumpDir = path.join(os.homedir(), '.llxprt', 'dumps');
            // Handle status command
            if (mode === 'status') {
                const currentMode = runtime.getEphemeralSetting('dumpcontext') || 'off';
                return {
                    type: 'message',
                    messageType: 'info',
                    content: `Context dumping: ${currentMode}\n\nDump directory: ${dumpDir}\n\nAvailable modes:\n- now: Dump context on next request only\n- on: Dump context before every request\n- error: Dump context only on errors\n- off: Disable context dumping\n- status: Show current status (default)`,
                };
            }
            // Handle mode changes
            runtime.setEphemeralSetting('dumpcontext', mode);
            const messages = {
                now: `Context will be dumped on next request.\nDump directory: ${dumpDir}`,
                on: `Context dumping enabled for all requests.\nDumps will be saved to: ${dumpDir}`,
                error: `Context dumping enabled for errors only.\nDumps will be saved to: ${dumpDir}`,
                off: 'Context dumping disabled.',
                status: '', // Already handled above
            };
            return {
                type: 'message',
                messageType: 'info',
                content: messages[mode],
            };
        }
        catch (error) {
            return {
                type: 'message',
                messageType: 'error',
                content: `Failed to manage dumpcontext: ${error instanceof Error ? error.message : String(error)}`,
            };
        }
    },
};
//# sourceMappingURL=dumpcontextCommand.js.map