/**
 * Search tools — local full-text search via SQLite FTS5 over documents and memory.
 */
import { getDb } from "../db.js";
export const searchTools = [
    {
        name: "quickSearch",
        description: "Execute a quick search across local documents and memory. Returns ranked results.",
        inputSchema: {
            type: "object",
            properties: {
                query: { type: "string", description: "Search query" },
                maxResults: {
                    type: "number",
                    description: "Maximum number of results (default 10)",
                },
            },
            required: ["query"],
        },
        handler: async (args) => {
            const db = getDb();
            const maxResults = args.maxResults ?? 10;
            const query = args.query;
            // Search documents via FTS5
            const docResults = db.prepare(`
        SELECT d.id, d.title, 'document' as source,
               snippet(documents_fts, 1, '**', '**', '...', 48) as snippet,
               rank
        FROM documents_fts
        JOIN documents d ON d.rowid = documents_fts.rowid
        WHERE documents_fts MATCH ?
          AND d.is_archived = 0
        ORDER BY rank
        LIMIT ?
      `).all(query, maxResults);
            // Search memory via FTS5
            const memResults = db.prepare(`
        SELECT m.id, m.key as title, 'memory' as source,
               snippet(memory_fts, 1, '**', '**', '...', 48) as snippet,
               rank
        FROM memory_fts
        JOIN memory m ON m.id = memory_fts.rowid
        WHERE memory_fts MATCH ?
        ORDER BY rank
        LIMIT ?
      `).all(query, maxResults);
            // Merge and sort by rank
            const combined = [...docResults, ...memResults]
                .sort((a, b) => a.rank - b.rank)
                .slice(0, maxResults);
            return {
                query,
                count: combined.length,
                results: combined.map((r) => ({
                    id: r.id,
                    title: r.title,
                    source: r.source,
                    snippet: r.snippet,
                })),
            };
        },
    },
    {
        name: "fusionSearch",
        description: "Multi-source search with mode control. Modes: fast (documents only), balanced (documents + memory), comprehensive (documents + memory + plans).",
        inputSchema: {
            type: "object",
            properties: {
                query: { type: "string", description: "Search query" },
                mode: {
                    type: "string",
                    enum: ["fast", "balanced", "comprehensive"],
                    description: "Search mode (default: balanced)",
                },
            },
            required: ["query"],
        },
        handler: async (args) => {
            const db = getDb();
            const mode = args.mode ?? "balanced";
            const query = args.query;
            const results = [];
            // Always search documents
            const docResults = db.prepare(`
        SELECT d.id, d.title, 'document' as source,
               snippet(documents_fts, 1, '**', '**', '...', 48) as snippet,
               rank
        FROM documents_fts
        JOIN documents d ON d.rowid = documents_fts.rowid
        WHERE documents_fts MATCH ?
          AND d.is_archived = 0
        ORDER BY rank
        LIMIT 20
      `).all(query);
            results.push(...docResults);
            // Balanced and comprehensive: also search memory
            if (mode === "balanced" || mode === "comprehensive") {
                const memResults = db.prepare(`
          SELECT m.id, m.key as title, 'memory' as source,
                 snippet(memory_fts, 1, '**', '**', '...', 48) as snippet,
                 rank
          FROM memory_fts
          JOIN memory m ON m.id = memory_fts.rowid
          WHERE memory_fts MATCH ?
          ORDER BY rank
          LIMIT 20
        `).all(query);
                results.push(...memResults);
            }
            // Comprehensive: also search plans (keyword match, no FTS)
            if (mode === "comprehensive") {
                const queryLower = query.toLowerCase();
                const plans = db.prepare("SELECT id, goal, steps FROM plans").all();
                for (const plan of plans) {
                    const text = `${plan.goal} ${plan.steps}`.toLowerCase();
                    if (text.includes(queryLower)) {
                        results.push({
                            id: plan.id,
                            title: plan.goal,
                            source: "plan",
                            snippet: plan.goal,
                            rank: -0.5, // lower priority than FTS matches
                        });
                    }
                }
            }
            // Sort by rank and dedupe
            const sorted = results
                .sort((a, b) => a.rank - b.rank)
                .slice(0, 30);
            return {
                query,
                mode,
                count: sorted.length,
                results: sorted.map((r) => ({
                    id: r.id,
                    title: r.title,
                    source: r.source,
                    snippet: r.snippet,
                })),
            };
        },
    },
];
//# sourceMappingURL=searchTools.js.map