"use strict";
// =============================================================================
// Pure Agent - Checkpoint Tool
// Snapshots and restores conversation state
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
const tool_registry_1 = require("../tool-registry");
const result_types_1 = require("../result-types");
const utils_1 = require("../utils");
const _checkpoints = new Map();
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
async function checkpointHandler(args, context) {
    const { action, checkpoint_id, description } = args;
    switch (action) {
        case 'save': {
            const id = `ckpt_${(0, utils_1.generateId)()}`;
            const checkpoint = {
                id,
                sessionId: context.sessionId,
                description: description || `Checkpoint at ${new Date().toISOString()}`,
                messages: [], // Will be populated by the conversation loop
                createdAt: new Date().toISOString(),
            };
            _checkpoints.set(id, checkpoint);
            return (0, result_types_1.toolResult)({
                success: true,
                action: 'saved',
                checkpoint_id: id,
                description: checkpoint.description,
                instruction: 'Checkpoint saved. Use this ID to restore later if needed.',
            });
        }
        case 'list': {
            const sessionCheckpoints = Array.from(_checkpoints.values())
                .filter(c => c.sessionId === context.sessionId)
                .map(c => ({
                id: c.id,
                description: c.description,
                created_at: c.createdAt,
                message_count: c.messages.length,
            }));
            return (0, result_types_1.toolResult)({
                success: true,
                checkpoints: sessionCheckpoints,
                count: sessionCheckpoints.length,
            });
        }
        case 'restore': {
            if (!checkpoint_id)
                return (0, result_types_1.toolError)('Missing checkpoint_id for restore');
            const checkpoint = _checkpoints.get(checkpoint_id);
            if (!checkpoint)
                return (0, result_types_1.toolError)(`Checkpoint not found: ${checkpoint_id}`);
            return (0, result_types_1.toolResult)({
                success: true,
                action: 'restored',
                checkpoint_id: checkpoint.id,
                description: checkpoint.description,
                messages: checkpoint.messages,
                instruction: 'Checkpoint restored. The conversation state has been reverted.',
            });
        }
        case 'delete': {
            if (!checkpoint_id)
                return (0, result_types_1.toolError)('Missing checkpoint_id for delete');
            const existed = _checkpoints.delete(checkpoint_id);
            return (0, result_types_1.toolResult)({
                success: existed,
                action: 'deleted',
                checkpoint_id,
            });
        }
        default:
            return (0, result_types_1.toolError)(`Unknown action: ${action}. Valid: save, list, restore, delete`);
    }
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
tool_registry_1.registry.register({
    name: 'checkpoint',
    toolset: 'checkpoint',
    schema: {
        description: 'Manage conversation checkpoints. Save snapshots of conversation state, list existing checkpoints, restore to a previous checkpoint, or delete old ones. Use before risky operations to enable rollback.',
        parameters: {
            type: 'object',
            properties: {
                action: {
                    type: 'string',
                    enum: ['save', 'list', 'restore', 'delete'],
                    description: 'Action to perform',
                },
                checkpoint_id: {
                    type: 'string',
                    description: 'Checkpoint ID (for restore/delete)',
                },
                description: {
                    type: 'string',
                    description: 'Description for the checkpoint (for save)',
                },
            },
            required: ['action'],
        },
    },
    handler: checkpointHandler,
    isAsync: true,
    description: 'Conversation checkpoints',
    emoji: '📌',
});
//# sourceMappingURL=checkpoint.js.map