/**
 * Memory tools — persistent key-value store backed by local SQLite.
 */
import { getDb } from "../db.js";
export const memoryTools = [
    {
        name: "writeAgentMemory",
        description: "Store intermediate results or data for later retrieval. Use this to avoid context window overflow.",
        inputSchema: {
            type: "object",
            properties: {
                key: {
                    type: "string",
                    description: "Unique key to store the data under (e.g., 'research_results')",
                },
                content: {
                    type: "string",
                    description: "Content to store (text, JSON string, markdown, etc.)",
                },
                metadata: {
                    type: "object",
                    description: "Optional metadata about the stored content",
                    properties: {
                        type: { type: "string", description: "Type of content" },
                        source: { type: "string", description: "Source of the data" },
                        timestamp: { type: "string", description: "When the data was created" },
                    },
                },
            },
            required: ["key", "content"],
        },
        handler: async (args) => {
            const { key, content, metadata = {} } = args;
            if (!key || !content)
                throw new Error("Key and content are required");
            const db = getDb();
            const now = new Date().toISOString();
            const metaJson = JSON.stringify({ ...metadata, createdAt: now });
            db.prepare(`
        INSERT INTO memory (key, content, metadata, created_at, updated_at)
        VALUES (?, ?, ?, ?, ?)
        ON CONFLICT(key) DO UPDATE SET
          content = excluded.content,
          metadata = excluded.metadata,
          updated_at = excluded.updated_at
      `).run(key, content, metaJson, now, now);
            const row = db.prepare("SELECT id FROM memory WHERE key = ?").get(key);
            return {
                success: true,
                memoryId: row?.id,
                key,
                message: `Stored ${content.length} characters under key '${key}'`,
            };
        },
    },
    {
        name: "readAgentMemory",
        description: "Retrieve previously stored data by key.",
        inputSchema: {
            type: "object",
            properties: {
                key: { type: "string", description: "Key of the data to retrieve" },
            },
            required: ["key"],
        },
        handler: async (args) => {
            const db = getDb();
            const row = db.prepare("SELECT * FROM memory WHERE key = ?").get(args.key);
            if (!row)
                throw new Error(`No memory found for key: ${args.key}`);
            let metadata = {};
            try {
                metadata = JSON.parse(row.metadata || "{}");
            }
            catch { /* ignore */ }
            return {
                success: true,
                key: row.key,
                content: row.content,
                metadata,
            };
        },
    },
    {
        name: "listAgentMemory",
        description: "List all stored memory keys with their metadata.",
        inputSchema: {
            type: "object",
            properties: {
                filter: {
                    type: "string",
                    description: "Optional filter to match keys (substring match)",
                },
            },
        },
        handler: async (args) => {
            const db = getDb();
            const filter = args.filter ?? "";
            const rows = filter
                ? db.prepare("SELECT id, key, content, metadata FROM memory WHERE key LIKE ?").all(`%${filter}%`)
                : db.prepare("SELECT id, key, content, metadata FROM memory").all();
            const list = rows.map((e) => {
                let metadata = {};
                try {
                    metadata = JSON.parse(e.metadata || "{}");
                }
                catch { /* ignore */ }
                return {
                    key: e.key,
                    memoryId: e.id,
                    contentLength: (e.content ?? "").length,
                    metadata,
                };
            });
            return { success: true, count: list.length, memories: list };
        },
    },
    {
        name: "deleteAgentMemory",
        description: "Delete stored memory by key.",
        inputSchema: {
            type: "object",
            properties: {
                key: { type: "string", description: "Key of the memory to delete" },
            },
            required: ["key"],
        },
        handler: async (args) => {
            const db = getDb();
            const result = db.prepare("DELETE FROM memory WHERE key = ?").run(args.key);
            if (result.changes === 0)
                throw new Error(`No memory found for key: ${args.key}`);
            return { success: true, message: `Deleted memory for key '${args.key}'` };
        },
    },
];
//# sourceMappingURL=memoryTools.js.map