/** * SQLite 辅助工具 — 结构化记忆存储 * * 所有表都包含 agent_id 列以支持动态 Agent 隔离。 * 每个 WeCom 用户/群组对应一个独立的 agentId,数据完全隔离。 */ import type Database from "better-sqlite3"; import type { MemoryEntry, MemoryCategory, FlamePet, FlameColor, FlameStats, Notification, NotificationLevel, NotificationSource, TodoEntry, TodoStatus, ChapterMark } from "../types.js"; export type { Database }; /** * 同步初始化数据库连接(仅在插件启动时由 index.ts 调用一次)。 * v5.7.17: sync 化以适配 openclaw plugin loader 对 register() 同步返回的硬约束。 * 用 createRequire 加载 better-sqlite3,失败可以被 try/catch 截获。 */ export declare function initDb(openclawDir: string, extDir?: string, log?: { warn: (msg: string) => void; info?: (msg: string) => void; }): Database.Database; /** * 同步获取已初始化的数据库实例。 * 必须在 initDb() 成功后调用;否则抛出明确的错误。 * 供各模块在注册期使用(此时 DB 已由 index.ts 初始化完毕)。 */ export declare function getDb(): Database.Database; /** v5.7.2: 暴露给运维 / enhance_memory_purge 调用的清理函数 */ export declare function purgeOldSafetyLogs(db: Database.Database, retentionDays?: number): { deleted: number; }; export declare function storeMemory(db: Database.Database, agentId: string, category: MemoryCategory, content: string, tags?: string, importance?: number, sessionId?: string, extras?: { why?: string; howToApply?: string; }): MemoryEntry; export declare function searchMemories(db: Database.Database, agentId: string, opts?: { category?: MemoryCategory; keyword?: string; limit?: number; since?: string; }): MemoryEntry[]; export declare function getRecentMemories(db: Database.Database, agentId: string, limit?: number): MemoryEntry[]; export declare function deleteMemory(db: Database.Database, agentId: string, id: number): boolean; /** * 按 tag / category / contentLike 批量清理记忆(agentId 隔离)。 * dry_run=true 时只 SELECT 不 DELETE,返回匹配条数(v5.7.1 hot-fix 用)。 */ export declare function purgeMemories(db: Database.Database, agentId: string, opts: { tag?: string; category?: MemoryCategory; contentLike?: string; dryRun?: boolean; }): { matched: number; }; export declare function getMemoryStats(db: Database.Database, agentId?: string): Record; /** 获取所有已知的 agentId 列表 */ export declare function getAllAgentIds(db: Database.Database): string[]; export declare function logSafetyEvent(db: Database.Database, agentId: string, tool: string, params: string, action: string, rule?: string, reason?: string): void; export declare function getRecentSafetyEvents(db: Database.Database, agentId?: string, limit?: number): Array<{ id: number; agent_id: string; tool: string; params: string; action: string; rule: string; reason: string; created_at: string; }>; export declare function getSafetyStats(db: Database.Database, agentId?: string): { total: number; blocked: number; logged: number; }; export declare function getOrCreatePet(db: Database.Database, agentId: string, defaultName?: string, defaultColor?: FlameColor): FlamePet; export declare function addPetXp(db: Database.Database, agentId: string, xpGain: number, statBoosts?: Partial): { pet: FlamePet; leveledUp: boolean; oldLevel: number; }; export declare function renamePet(db: Database.Database, agentId: string, name: string): void; export declare function setPetColor(db: Database.Database, agentId: string, color: FlameColor): void; export declare function emitNotification(db: Database.Database, agentId: string, level: NotificationLevel, source: NotificationSource, title: string, detail?: string): void; export declare function getRecentNotifications(db: Database.Database, agentId?: string, limit?: number): Notification[]; export declare function getUnreadNotificationCount(db: Database.Database, agentId?: string): number; export declare function markNotificationRead(db: Database.Database, id: number): void; export declare function pruneNotifications(db: Database.Database, maxRetained: number): void; export interface TodoInput { content: string; activeForm: string; status?: TodoStatus; } export declare function replaceTodos(db: Database.Database, agentId: string, sessionId: string, todos: TodoInput[]): TodoEntry[]; export declare function listTodos(db: Database.Database, agentId: string, sessionId?: string): TodoEntry[]; export declare function updateTodoStatus(db: Database.Database, agentId: string, sessionId: string, position: number, status: TodoStatus): TodoEntry | null; export declare function getLatestTodos(db: Database.Database, agentId: string): TodoEntry[]; export declare function addChapter(db: Database.Database, agentId: string, sessionId: string, title: string, summary?: string): ChapterMark; export declare function listChapters(db: Database.Database, agentId: string, sessionId?: string, limit?: number): ChapterMark[]; export interface ScheduledTaskBinding { id: number; agent_id: string; name: string; cron_ref: string; instructions: string; enabled: number; last_fired_at: string | null; created_at: string; } export declare function upsertScheduledBinding(db: Database.Database, agentId: string, name: string, cronRef: string, instructions: string): ScheduledTaskBinding; export declare function listScheduledBindings(db: Database.Database, agentId?: string): ScheduledTaskBinding[]; export declare function disableScheduledBinding(db: Database.Database, agentId: string, name: string): boolean; export declare function touchScheduledBindingFired(db: Database.Database, id: number): void;