"use strict";
// =============================================================================
// Pure Agent - Session Search Tool
// Ported from Hermes tools/session_search_tool.py
// Full-text search across past sessions via SQLite FTS5
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
const tool_registry_1 = require("../tool-registry");
const result_types_1 = require("../result-types");
const session_store_1 = require("../session-store");
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
async function sessionSearchHandler(args, context) {
    const { query, limit = 20, session_id, role_filter } = args;
    if (!query)
        return (0, result_types_1.toolError)('Missing required parameter: query');
    try {
        const store = (0, session_store_1.getSessionStore)();
        // Search messages using FTS5
        let results = store.searchMessages(query, limit);
        // Filter by session if specified
        if (session_id) {
            results = results.filter(r => r.sessionId === session_id);
        }
        // Filter by role if specified
        if (role_filter) {
            results = results.filter(r => r.role === role_filter);
        }
        return (0, result_types_1.toolResult)({
            success: true,
            query,
            results: results.map(r => ({
                session_id: r.sessionId,
                role: r.role,
                content: r.content.slice(0, 500), // Truncate for display
            })),
            count: results.length,
        });
    }
    catch (err) {
        return (0, result_types_1.toolError)(`Session search failed: ${err.message}`);
    }
}
// ---------------------------------------------------------------------------
// Registration
// ---------------------------------------------------------------------------
tool_registry_1.registry.register({
    name: 'session_search',
    toolset: 'session',
    schema: {
        description: 'Search across past conversation sessions using full-text search. Use this to recall information from previous conversations that the user may be referencing.',
        parameters: {
            type: 'object',
            properties: {
                query: {
                    type: 'string',
                    description: 'Search query (supports natural language)',
                },
                limit: {
                    type: 'number',
                    description: 'Maximum results (default: 20)',
                    default: 20,
                },
                session_id: {
                    type: 'string',
                    description: 'Filter to a specific session ID (optional)',
                },
                role_filter: {
                    type: 'string',
                    enum: ['user', 'assistant', 'tool'],
                    description: 'Filter by message role (optional)',
                },
            },
            required: ['query'],
        },
    },
    handler: sessionSearchHandler,
    isAsync: true,
    description: 'Search past conversations',
    emoji: '🔍',
});
//# sourceMappingURL=session-search.js.map