feat: indice locale SQLite/FTS5 + fallback testuale offline per qmem
Il gateway remoto non è sempre raggiungibile (VPN/nodi giù): finora le ricerche
fallivano e la conoscenza non era consultabile. Ora l'estensione mantiene un
indice locale testuale e vi degrada automaticamente.
Core (extensions/local-db.ts):
- schema SQLite con FTS5 (unicode61 remove_diacritics 2), trigger di sync,
tabella meta per cursori/stato; usa node:sqlite (Node >= 22.5, nessuna
dipendenza esterna), con soppressione del warning "experimental"
- import idempotente dalle sessioni pi (tutte le directory di progetto):
qmem_store/qmem_correct (ID + testo integrale), qmem_get (payload completo),
qmem_search (record osservati, anche creati da altri agenti)
- merge senza regressioni: le osservazioni povere (es. search senza
project_id) non azzerano i campi già noti; superseded_by monotono
- ricerca FTS5 con filtri (kind/project/scope/level/topic), esclusione di
superseduti e privati, ranking bm25, snippet, ripiego AND -> OR dichiarato
- enrich dal gateway (GET /v1/memories/{id}, pacing < rate limit, timeout 8s
per richiesta, stop al primo guasto) e pull da /v1/memories:export (endpoint
lato gateway previsto: se assente lo segnala senza errore)
- localGet per il recupero puntuale offline
Estensione:
- qmem_search: su 0/429/5xx degrada all'indice locale, risultati etichettati
"INDICE LOCALE, ricerca testuale non neurale" + details.fallback=local_sqlite
- qmem_get: fallback locale per UUID
- qmem_store: avviso esplicito che il record NON è salvato (nessuna coda)
- rendering arricchito con project_id e flag privato (anche per il gateway)
- comando /qmem:local status|import|find|enrich|pull
- regole e skill aggiornate: quando si usa l'indice locale non applicare le
soglie 0.45/0.60 (sono semantiche)
CLI standalone (stesso core): scripts/qmem-sqlite.mjs status|import|find|
enrich|pull (+ --json). Test: scripts/test-local.mjs (14 controlli, HOME
temporanea, sessioni sintetiche, gateway black-hole e stub HTTP).
Verifiche: 14/14 test superati; import reale 185 sessioni -> 1046 record unici
(1032 con testo, 986 attivi, 60 superseduti, 672 con project_id, 20 gruppi di
duplicati) in 2,8 MB; enrich con gateway giù si ferma in ~16s con messaggio
chiaro invece di restare appeso.
This commit is contained in:
@@ -13,6 +13,7 @@ import { registerQmemSearch } from "./tools/search";
|
||||
import { registerQmemStore } from "./tools/store";
|
||||
import { registerQmemTree } from "./tools/tree";
|
||||
import { registerQmemRules } from "./rules";
|
||||
import { registerQmemLocal } from "./local-command";
|
||||
|
||||
export default function qmemExtension(pi: ExtensionAPI) {
|
||||
registerQmemStore(pi);
|
||||
@@ -22,5 +23,6 @@ export default function qmemExtension(pi: ExtensionAPI) {
|
||||
registerQmemGet(pi);
|
||||
registerQmemTree(pi);
|
||||
registerQmemConfig(pi);
|
||||
registerQmemLocal(pi);
|
||||
registerQmemRules(pi);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* pi-qmem — comando /qmem:local: gestione dell'indice locale SQLite/FTS5.
|
||||
*
|
||||
* /qmem:local → stato (record, copertura, lag, duplicati)
|
||||
* /qmem:local import → ricostruisce/aggiorna l'indice dalle sessioni pi
|
||||
* /qmem:local find <query> → ricerca testuale locale (anche con gateway giù)
|
||||
* /qmem:local enrich [--all] → arricchisce dal gateway (GET /v1/memories/{id})
|
||||
* /qmem:local pull → pull incrementale dall'export del gateway
|
||||
*/
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import {
|
||||
enrichFromGateway,
|
||||
importFromSessions,
|
||||
localDbPath,
|
||||
localDbReport,
|
||||
localSearch,
|
||||
pullFromGatewayExport,
|
||||
} from "./local-db.ts";
|
||||
import { loadConfig } from "./shared.ts";
|
||||
|
||||
export function registerQmemLocal(pi: ExtensionAPI) {
|
||||
pi.registerCommand("qmem:local", {
|
||||
description: "Indice locale SQLite/FTS5: status | import | find <query> | enrich [--all] | pull",
|
||||
handler: async (args, ctx) => {
|
||||
const cfg = loadConfig();
|
||||
const dbFile = localDbPath(cfg);
|
||||
const [sub = "status", ...rest] = (args ?? "").trim().split(/\s+/);
|
||||
try {
|
||||
if (sub === "status") {
|
||||
const r = await localDbReport({ dbFile });
|
||||
if (!r.exists) {
|
||||
ctx.ui.notify(`Indice locale assente (${dbFile}): esegui /qmem:local import`, "warning");
|
||||
return;
|
||||
}
|
||||
const dup = r.duplicates.length ? ` | duplicati: ${r.duplicates.length} gruppi` : "";
|
||||
ctx.ui.notify(
|
||||
`Indice locale: ${r.total} record (${r.withText} con testo, ${r.active} attivi, ${r.superseded} superseduti, ${r.private} privati) | project_id ${r.withProject}/${r.total} | ${r.sizeKb} KB${dup}\n` +
|
||||
`ultimo import: ${r.lastImport ?? "-"} | enrich: ${r.lastEnrich ?? "-"} | export: ${r.lastExport ?? "-"}\n` +
|
||||
`DB: ${r.path}`,
|
||||
"info",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (sub === "import") {
|
||||
ctx.ui.setStatus("pi-qmem", "Import sessioni → indice locale...");
|
||||
const stats = await importFromSessions({ dbFile });
|
||||
const r = await localDbReport({ dbFile });
|
||||
ctx.ui.setStatus("pi-qmem", "");
|
||||
ctx.ui.notify(
|
||||
`Import completato: ${stats.files} sessioni, store=${stats.store} correct=${stats.correct} get=${stats.get} search_hit=${stats.searchHits} → ${stats.records} record unici.\n` +
|
||||
`Indice: ${r.total} record (${r.withText} con testo, ${r.active} attivi) in ${r.sizeKb} KB`,
|
||||
"info",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (sub === "find") {
|
||||
const query = rest.filter((a) => !a.startsWith("--")).join(" ").trim();
|
||||
if (!query) {
|
||||
ctx.ui.notify("Uso: /qmem:local find <query> [--all] [--kind K] [--project P]", "warning");
|
||||
return;
|
||||
}
|
||||
const has = (f: string) => rest.includes(`--${f}`);
|
||||
const val = (f: string) => {
|
||||
const i = rest.indexOf(`--${f}`);
|
||||
return i >= 0 && rest[i + 1] && !rest[i + 1].startsWith("--") ? rest[i + 1] : undefined;
|
||||
};
|
||||
const hits = await localSearch(
|
||||
{
|
||||
query,
|
||||
kind: val("kind"),
|
||||
project_id: val("project"),
|
||||
scope: val("scope"),
|
||||
include_superseded: has("all"),
|
||||
include_private: has("private"),
|
||||
top_k: Number(val("top")) || 5,
|
||||
},
|
||||
{ dbFile },
|
||||
);
|
||||
if (!hits.length) {
|
||||
ctx.ui.notify(`Nessun risultato locale per "${query}" (indice: ${dbFile})`, "warning");
|
||||
return;
|
||||
}
|
||||
const lines = hits.map(
|
||||
(h, i) =>
|
||||
`${i + 1}. [${h.kind ?? "?"}/${h.scope ?? "?"}${h.project_id ? ` project=${h.project_id}` : ""}${h.match_mode === "or" ? " OR" : ""}${h.superseded_by ? " superseduto" : ""}] ${h.snippet}\n (id: ${h.memory_id}, creato: ${h.created_at ?? "?"})`,
|
||||
);
|
||||
ctx.ui.notify(`Indice locale (ricerca testuale, non neurale) — ${hits.length} risultati:\n${lines.join("\n")}`, "info");
|
||||
return;
|
||||
}
|
||||
if (sub === "enrich") {
|
||||
ctx.ui.setStatus("pi-qmem", "Arricchimento dal gateway...");
|
||||
const stats = await enrichFromGateway(cfg, {
|
||||
dbFile,
|
||||
onlyIncomplete: !rest.includes("--all"),
|
||||
limit: 1000,
|
||||
});
|
||||
ctx.ui.setStatus("pi-qmem", "");
|
||||
ctx.ui.notify(
|
||||
stats.ok
|
||||
? `Arricchimento: ${stats.updated} record aggiornati (richiesti ${stats.requested}, falliti ${stats.failed})`
|
||||
: `Arricchimento non possibile: gateway non raggiungibile (${stats.errors[0] ?? "errore di rete"})`,
|
||||
stats.ok ? "info" : "warning",
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (sub === "pull") {
|
||||
ctx.ui.setStatus("pi-qmem", "Pull export dal gateway...");
|
||||
const res = await pullFromGatewayExport(cfg, { dbFile });
|
||||
ctx.ui.setStatus("pi-qmem", "");
|
||||
ctx.ui.notify(
|
||||
res.supported
|
||||
? `Pull export: ${res.fetched} record in ${res.pages} pagine`
|
||||
: `Pull export non disponibile: ${res.message ?? "endpoint assente sul gateway"}`,
|
||||
res.supported ? "info" : "warning",
|
||||
);
|
||||
return;
|
||||
}
|
||||
ctx.ui.notify("Uso: /qmem:local [status|import|find <query>|enrich [--all]|pull]", "warning");
|
||||
} catch (e) {
|
||||
ctx.ui.setStatus("pi-qmem", "");
|
||||
ctx.ui.notify(`Errore indice locale: ${e instanceof Error ? e.message : String(e)}`, "error");
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,709 @@
|
||||
/**
|
||||
* pi-qmem — indice locale SQLite/FTS5 (fallback testuale quando il gateway è giù).
|
||||
*
|
||||
* Perché: il gateway remoto (Qdrant + BGE-M3) non è sempre raggiungibile (VPN
|
||||
* giù, nodo offline). Questo modulo costruisce un indice **locale** dei record
|
||||
* di memoria a partire da due fonti:
|
||||
*
|
||||
* 1. le sessioni pi (~/.pi/agent/sessions/<cwd>/*.jsonl): ogni chiamata
|
||||
* `qmem_store`/`qmem_correct`/`qmem_get`/`qmem_search` contiene l'ID
|
||||
* fornito dal gateway e il testo integrale → storico ricostruibile offline;
|
||||
* 2. il gateway stesso, quando è raggiungibile (`enrich` via GET
|
||||
* /v1/memories/{id}, `pull` via GET /v1/memories:export quando esisterà).
|
||||
*
|
||||
* La ricerca è **lessicale** (FTS5 + BM25), non neurale: nessun embedding,
|
||||
* nessun modello, nessuna dipendenza esterna. È il fallback dichiarato di
|
||||
* `qmem_search` quando il gateway non risponde.
|
||||
*
|
||||
* DB di default: ~/.local/share/pi-qmem/qmem.sqlite (override: env QMEM_SQLITE
|
||||
* oppure `localDbPath` in ~/.config/pi-qmem/config.json).
|
||||
*
|
||||
* Nessuna dipendenza: usa `node:sqlite` (Node ≥ 22.5). Se il modulo non è
|
||||
* disponibile, le funzioni degradano con un errore esplicito.
|
||||
*/
|
||||
import * as fs from "node:fs";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { gatewayRequest, loadConfig, type MemoryConfig } from "./shared.ts";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Percorsi e apertura
|
||||
// ---------------------------------------------------------------------------
|
||||
export const DEFAULT_DB_FILE = path.join(os.homedir(), ".local", "share", "pi-qmem", "qmem.sqlite");
|
||||
|
||||
export function localDbPath(cfg?: MemoryConfig & { localDbPath?: string }): string {
|
||||
return process.env.QMEM_SQLITE ?? cfg?.localDbPath ?? DEFAULT_DB_FILE;
|
||||
}
|
||||
|
||||
export function sessionRoots(): string[] {
|
||||
const roots = process.env.QMEM_SESSIONS_DIR
|
||||
? [process.env.QMEM_SESSIONS_DIR]
|
||||
: [path.join(os.homedir(), ".pi", "agent", "sessions")];
|
||||
return roots.filter((r) => {
|
||||
try {
|
||||
return fs.statSync(r).isDirectory();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
type Db = any;
|
||||
|
||||
let sqliteModule: any | null | undefined;
|
||||
|
||||
/** Carica `node:sqlite` una volta sola, sopprimendo l'ExperimentalWarning. */
|
||||
async function loadSqlite(): Promise<any | null> {
|
||||
if (sqliteModule !== undefined) return sqliteModule;
|
||||
const originalEmit = process.emitWarning;
|
||||
try {
|
||||
// il warning "SQLite is an experimental feature" sporcherebbe la TUI
|
||||
(process as any).emitWarning = (warning: any, ...rest: any[]) => {
|
||||
const msg = typeof warning === "string" ? warning : String(warning?.message ?? "");
|
||||
const type = (rest[0] as any)?.type ?? rest[0];
|
||||
if (type === "ExperimentalWarning" && /sqlite/i.test(msg)) return;
|
||||
return (originalEmit as any).call(process, warning, ...rest);
|
||||
};
|
||||
sqliteModule = await import("node:sqlite");
|
||||
} catch {
|
||||
sqliteModule = null;
|
||||
} finally {
|
||||
(process as any).emitWarning = originalEmit;
|
||||
}
|
||||
return sqliteModule;
|
||||
}
|
||||
|
||||
export async function openLocalDb(dbFile?: string): Promise<Db> {
|
||||
const mod = await loadSqlite();
|
||||
if (!mod?.DatabaseSync) {
|
||||
throw new Error("node:sqlite non disponibile: serve Node >= 22.5 per l'indice locale");
|
||||
}
|
||||
const file = dbFile ?? localDbPath(loadConfig());
|
||||
if (file !== ":memory:") fs.mkdirSync(path.dirname(file), { recursive: true });
|
||||
const db = new mod.DatabaseSync(file);
|
||||
db.exec("PRAGMA journal_mode=WAL;");
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS records(
|
||||
rowid_ INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
memory_id TEXT UNIQUE NOT NULL,
|
||||
text TEXT, kind TEXT, project_id TEXT, scope TEXT, agent_id TEXT, confidence TEXT,
|
||||
importance REAL, created_at TEXT, supersedes_id TEXT, superseded_by TEXT,
|
||||
parent_id TEXT, topic TEXT, level TEXT, private INTEGER NOT NULL DEFAULT 0,
|
||||
text_hash TEXT, sources TEXT, synced_at TEXT, updated_at TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS records_project ON records(project_id);
|
||||
CREATE INDEX IF NOT EXISTS records_kind ON records(kind);
|
||||
CREATE INDEX IF NOT EXISTS records_hash ON records(text_hash);
|
||||
CREATE VIRTUAL TABLE IF NOT EXISTS records_fts USING fts5(
|
||||
text, kind, project_id, content='records', content_rowid='rowid_',
|
||||
tokenize="unicode61 remove_diacritics 2"
|
||||
);
|
||||
CREATE TRIGGER IF NOT EXISTS records_ai AFTER INSERT ON records BEGIN
|
||||
INSERT INTO records_fts(rowid, text, kind, project_id) VALUES (new.rowid_, new.text, new.kind, new.project_id);
|
||||
END;
|
||||
CREATE TRIGGER IF NOT EXISTS records_au AFTER UPDATE ON records BEGIN
|
||||
INSERT INTO records_fts(records_fts, rowid, text, kind, project_id) VALUES('delete', old.rowid_, old.text, old.kind, old.project_id);
|
||||
INSERT INTO records_fts(rowid, text, kind, project_id) VALUES (new.rowid_, new.text, new.kind, new.project_id);
|
||||
END;
|
||||
CREATE TRIGGER IF NOT EXISTS records_ad AFTER DELETE ON records BEGIN
|
||||
INSERT INTO records_fts(records_fts, rowid, text, kind, project_id) VALUES('delete', old.rowid_, old.text, old.kind, old.project_id);
|
||||
END;
|
||||
CREATE TABLE IF NOT EXISTS meta(k TEXT PRIMARY KEY, v TEXT);
|
||||
`);
|
||||
return db;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Upsert
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface LocalRecordInput {
|
||||
memory_id: string;
|
||||
text?: string | null;
|
||||
kind?: string | null;
|
||||
project_id?: string | null;
|
||||
scope?: string | null;
|
||||
agent_id?: string | null;
|
||||
confidence?: string | null;
|
||||
importance?: number | null;
|
||||
created_at?: string | null;
|
||||
supersedes_id?: string | null;
|
||||
superseded_by?: string | null;
|
||||
parent_id?: string | null;
|
||||
topic?: string | null;
|
||||
level?: string | null;
|
||||
private?: boolean | null;
|
||||
/** Fonte dell'osservazione: store | correct | get | search | export | enrich */
|
||||
source: string;
|
||||
}
|
||||
|
||||
/** Sorgenti ordinate per affidabilità del testo (maggiore = più completo). */
|
||||
const TEXT_PRIORITY: Record<string, number> = { export: 5, enrich: 5, get: 4, store: 3, correct: 3, search: 2 };
|
||||
|
||||
function normalizeText(t: string): string {
|
||||
return t.replace(/\s+/g, " ").trim().toLowerCase();
|
||||
}
|
||||
|
||||
export function textHashOf(text: string): string {
|
||||
return createHash("sha256").update(normalizeText(text)).digest("hex").slice(0, 32);
|
||||
}
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function upsertRecords(db: Db, records: LocalRecordInput[]): number {
|
||||
if (!records.length) return 0;
|
||||
const find = db.prepare("SELECT * FROM records WHERE memory_id = ?");
|
||||
const insert = db.prepare(`INSERT INTO records
|
||||
(memory_id, text, kind, project_id, scope, agent_id, confidence, importance, created_at,
|
||||
supersedes_id, superseded_by, parent_id, topic, level, private, text_hash, sources, synced_at, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`);
|
||||
const update = db.prepare(`UPDATE records SET
|
||||
text=?, kind=?, project_id=?, scope=?, agent_id=?, confidence=?, importance=?, created_at=?,
|
||||
supersedes_id=?, superseded_by=?, parent_id=?, topic=?, level=?, private=?, text_hash=?, sources=?, synced_at=?, updated_at=?
|
||||
WHERE rowid_=?`);
|
||||
const now = new Date().toISOString();
|
||||
let written = 0;
|
||||
for (const r of records) {
|
||||
if (!r.memory_id || !UUID_RE.test(r.memory_id)) continue;
|
||||
const existing = find.get(r.memory_id);
|
||||
const sources = new Set<string>((existing?.sources ?? "").split(",").filter(Boolean));
|
||||
sources.add(r.source);
|
||||
const prevText: string | null = existing?.text ?? null;
|
||||
const prevPri = Math.max(0, ...(existing?.sources ?? "").split(",").map((s: string) => TEXT_PRIORITY[s] ?? 0));
|
||||
const newPri = TEXT_PRIORITY[r.source] ?? 0;
|
||||
// il testo più affidabile vince; altrimenti si conserva quello esistente
|
||||
const text = prevText && newPri < prevPri ? prevText : (r.text ?? prevText);
|
||||
const supersededBy = r.superseded_by ?? existing?.superseded_by ?? null; // osservazione monotona
|
||||
const vals = [
|
||||
text,
|
||||
r.kind ?? null,
|
||||
r.project_id ?? null,
|
||||
r.scope ?? null,
|
||||
r.agent_id ?? null,
|
||||
r.confidence ?? null,
|
||||
typeof r.importance === "number" ? r.importance : null,
|
||||
r.created_at ?? null,
|
||||
r.supersedes_id ?? null,
|
||||
supersededBy,
|
||||
r.parent_id ?? null,
|
||||
r.topic ?? null,
|
||||
r.level ?? null,
|
||||
r.private ? 1 : 0,
|
||||
text ? textHashOf(text) : null,
|
||||
[...sources].sort().join(","),
|
||||
now,
|
||||
now,
|
||||
];
|
||||
if (existing) {
|
||||
// I campi già noti non vengono mai azzerati da osservazioni più povere
|
||||
// (es. un risultato di ricerca che non riporta project_id).
|
||||
const keys = ["text", "kind", "project_id", "scope", "agent_id", "confidence", "importance", "created_at",
|
||||
"supersedes_id", "superseded_by", "parent_id", "topic", "level", "private", "text_hash", "sources", "synced_at", "updated_at"];
|
||||
const merged = vals.map((v, i) => {
|
||||
const key = keys[i];
|
||||
const cur = (existing as any)[key];
|
||||
const curEmpty = cur === null || cur === undefined || cur === "" || cur === 0;
|
||||
if (key === "private") return cur === 1 ? 1 : v; // monotono: una volta privato resta privato
|
||||
if (key === "sources" || key === "synced_at" || key === "updated_at") return v;
|
||||
if (v === null || v === "" ) return curEmpty ? v : cur; // non azzerare
|
||||
return v;
|
||||
});
|
||||
update.run(...merged, existing.rowid_);
|
||||
} else {
|
||||
insert.run(r.memory_id, ...vals);
|
||||
}
|
||||
written++;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Import dalle sessioni pi
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface ImportStats {
|
||||
files: number;
|
||||
lines: number;
|
||||
store: number;
|
||||
correct: number;
|
||||
get: number;
|
||||
searchHits: number;
|
||||
unparsed: number;
|
||||
records: number;
|
||||
written: number;
|
||||
}
|
||||
|
||||
const ID_IN_LINE = /\(id:\s*([0-9a-f-]{36})([^)]*)\)/gi;
|
||||
|
||||
/** Estrae i record da un singolo file di sessione (JSONL). */
|
||||
function parseSessionFile(file: string, stats: ImportStats, out: LocalRecordInput[]): void {
|
||||
let raw: string;
|
||||
try {
|
||||
raw = fs.readFileSync(file, "utf8");
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
stats.files++;
|
||||
const calls = new Map<string, { name: string; args: any; ts?: string }>();
|
||||
for (const line of raw.split("\n")) {
|
||||
if (!line.trim()) continue;
|
||||
stats.lines++;
|
||||
let entry: any;
|
||||
try {
|
||||
entry = JSON.parse(line);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (entry?.type !== "message") continue;
|
||||
const msg = entry.message ?? {};
|
||||
const content = msg.content;
|
||||
if (msg.role === "assistant" && Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part?.type === "toolCall" && String(part.name ?? "").startsWith("qmem_")) {
|
||||
calls.set(part.id, { name: part.name, args: part.arguments ?? {}, ts: entry.timestamp });
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (msg.role !== "toolResult") continue;
|
||||
const name = String(msg.toolName ?? "");
|
||||
if (!name.startsWith("qmem_")) continue;
|
||||
const call = calls.get(msg.toolCallId) ?? { name, args: {}, ts: entry.timestamp };
|
||||
const text = Array.isArray(content)
|
||||
? content.filter((p: any) => p?.type === "text").map((p: any) => p.text ?? "").join(" ")
|
||||
: String(content ?? "");
|
||||
const det = msg.details ?? {};
|
||||
const args = call.args ?? {};
|
||||
if (call.name === "qmem_store" && det.memory_id) {
|
||||
stats.store++;
|
||||
out.push({
|
||||
memory_id: det.memory_id,
|
||||
text: args.text ?? null,
|
||||
kind: args.kind ?? null,
|
||||
project_id: args.project_id ?? null,
|
||||
scope: args.scope ?? null,
|
||||
agent_id: args.agent_id ?? null,
|
||||
confidence: args.confidence ?? null,
|
||||
importance: typeof args.importance === "number" ? args.importance : null,
|
||||
created_at: det.created_at ?? call.ts ?? null,
|
||||
supersedes_id: args.supersedes_id ?? null,
|
||||
parent_id: args.parent_id ?? null,
|
||||
topic: args.topic ?? null,
|
||||
level: args.level ?? null,
|
||||
private: args.private ?? null,
|
||||
source: "store",
|
||||
});
|
||||
} else if (call.name === "qmem_correct" && det.new_id) {
|
||||
stats.correct++;
|
||||
out.push({
|
||||
memory_id: det.new_id,
|
||||
text: args.text ?? null,
|
||||
kind: args.kind ?? null,
|
||||
project_id: args.project_id ?? null,
|
||||
scope: args.scope ?? null,
|
||||
agent_id: args.agent_id ?? null,
|
||||
created_at: call.ts ?? null,
|
||||
supersedes_id: det.superseded_id ?? null,
|
||||
topic: args.topic ?? null,
|
||||
level: args.level ?? null,
|
||||
source: "correct",
|
||||
});
|
||||
if (det.superseded_id) {
|
||||
out.push({ memory_id: det.superseded_id, superseded_by: det.new_id, source: "correct" });
|
||||
}
|
||||
} else if (call.name === "qmem_get" && det.memory_id) {
|
||||
stats.get++;
|
||||
// formato: "memory_id: <uuid>\n[kind/scope ..] | project: X | agente: Y, creato: Z ...\n\n<testo>"
|
||||
const parts = text.split("\n");
|
||||
const bodyStart = parts.findIndex((l, i) => i > 0 && l.trim() === "");
|
||||
const body = bodyStart >= 0 ? parts.slice(bodyStart + 1).join("\n").trim() : null;
|
||||
const project = /project:\s*([^|\n]+)/.exec(text)?.[1]?.trim();
|
||||
const created = /creato:\s*([^,|\n]+)/.exec(text)?.[1]?.trim();
|
||||
const agent = /agente:\s*([^,|\n]+)/.exec(text)?.[1]?.trim();
|
||||
out.push({
|
||||
memory_id: det.memory_id,
|
||||
text: body && body !== "(nessun testo)" ? body : null,
|
||||
kind: det.kind ?? null,
|
||||
project_id: det.project_id ?? (project && project !== "?" ? project : null),
|
||||
scope: det.scope ?? null,
|
||||
agent_id: agent && agent !== "?" ? agent : null,
|
||||
created_at: created && created !== "?" ? created : null,
|
||||
superseded_by: det.superseded_by ?? null,
|
||||
topic: det.topic ?? null,
|
||||
level: det.level ?? null,
|
||||
source: "get",
|
||||
});
|
||||
} else if (call.name === "qmem_search") {
|
||||
// formato: "N. [kind/scope ...] TESTO\n (id: <uuid>, agente: .., creato: ..)"
|
||||
const re = /^\s*\d+\.\s*\[([^\]/]+)\/([^\]\s]+)[^\]]*\]\s*([\s\S]*?)\n\s*\(id:\s*([0-9a-f-]{36})([^)]*)\)/gim;
|
||||
let m: RegExpExecArray | null;
|
||||
let found = 0;
|
||||
while ((m = re.exec(text))) {
|
||||
found++;
|
||||
const tail = m[5] ?? "";
|
||||
const sup = /supers[^\s]*\s+da\s+([0-9a-f-]{36})/i.exec(tail)?.[1] ?? null;
|
||||
const project = /project[=:]\s*([^,)\n]+)/i.exec(tail)?.[1]?.trim() ?? null;
|
||||
out.push({
|
||||
memory_id: m[4],
|
||||
text: m[3].trim(),
|
||||
kind: m[1].trim(),
|
||||
scope: m[2].trim(),
|
||||
project_id: project && project !== "?" ? project : null,
|
||||
created_at: /creato:\s*([^,)]+)/.exec(tail)?.[1]?.trim() ?? null,
|
||||
agent_id: /agente:\s*([^,)]+)/.exec(tail)?.[1]?.trim() ?? null,
|
||||
superseded_by: sup,
|
||||
source: "search",
|
||||
});
|
||||
}
|
||||
stats.searchHits += found;
|
||||
if (!found && /^\s*\d+\.\s*\[/.test(text)) stats.unparsed++;
|
||||
void ID_IN_LINE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Import idempotente dalle sessioni pi (tutte le directory di progetto). */
|
||||
export async function importFromSessions(opts?: { dbFile?: string; roots?: string[] }): Promise<ImportStats> {
|
||||
const db = await openLocalDb(opts?.dbFile);
|
||||
const stats: ImportStats = { files: 0, lines: 0, store: 0, correct: 0, get: 0, searchHits: 0, unparsed: 0, records: 0, written: 0 };
|
||||
const records: LocalRecordInput[] = [];
|
||||
for (const root of opts?.roots ?? sessionRoots()) {
|
||||
let dirs: string[] = [];
|
||||
try {
|
||||
dirs = fs.readdirSync(root).map((d) => path.join(root, d));
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const dir of dirs) {
|
||||
let files: string[] = [];
|
||||
try {
|
||||
files = fs
|
||||
.statSync(dir)
|
||||
.isDirectory()
|
||||
? fs.readdirSync(dir).filter((f) => f.endsWith(".jsonl")).map((f) => path.join(dir, f))
|
||||
: [dir];
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const file of files) parseSessionFile(file, stats, records);
|
||||
}
|
||||
}
|
||||
stats.records = new Set(records.map((r) => r.memory_id)).size;
|
||||
stats.written = upsertRecords(db, records);
|
||||
db.prepare("INSERT INTO meta(k,v) VALUES('last_import',?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(new Date().toISOString());
|
||||
db.prepare("INSERT INTO meta(k,v) VALUES('last_import_stats',?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(JSON.stringify(stats));
|
||||
db.close();
|
||||
return stats;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Ricerca lessicale (FTS5 + BM25)
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface LocalSearchParams {
|
||||
query: string;
|
||||
kind?: string;
|
||||
project_id?: string;
|
||||
scope?: string;
|
||||
level?: string;
|
||||
topic?: string;
|
||||
include_superseded?: boolean;
|
||||
include_private?: boolean;
|
||||
top_k?: number;
|
||||
/** true = match esatto della frase, senza espansione prefisso */
|
||||
exact?: boolean;
|
||||
}
|
||||
|
||||
export interface LocalSearchHit {
|
||||
memory_id: string;
|
||||
text: string | null;
|
||||
kind: string | null;
|
||||
project_id: string | null;
|
||||
scope: string | null;
|
||||
agent_id: string | null;
|
||||
created_at: string | null;
|
||||
superseded_by: string | null;
|
||||
sources: string | null;
|
||||
snippet: string;
|
||||
rank: number;
|
||||
/** "and" = tutti i termini presenti; "or" = match parziale (AND senza risultati) */
|
||||
match_mode?: "and" | "or";
|
||||
}
|
||||
|
||||
/** Converte una query utente in una MATCH di FTS5 senza rischi di sintassi. */
|
||||
export function toFtsMatch(query: string, exact = false, mode: "and" | "or" = "and"): string {
|
||||
const tokens = query
|
||||
.replace(/["'()*:^-]/g, " ")
|
||||
.split(/\s+/)
|
||||
.map((t) => t.trim())
|
||||
.filter((t) => t.length > 1);
|
||||
if (!tokens.length) return "";
|
||||
return tokens.map((t) => (exact ? `"${t}"` : `"${t}"*`)).join(mode === "and" ? " AND " : " OR ");
|
||||
}
|
||||
|
||||
async function runSearch(db: Db, params: LocalSearchParams, match: string): Promise<LocalSearchHit[]> {
|
||||
const where: string[] = ["records_fts MATCH ?"];
|
||||
const args: any[] = [match];
|
||||
if (params.kind) {
|
||||
where.push("r.kind = ?");
|
||||
args.push(params.kind);
|
||||
}
|
||||
if (params.project_id) {
|
||||
where.push("r.project_id = ?");
|
||||
args.push(params.project_id);
|
||||
}
|
||||
if (params.scope) {
|
||||
where.push("r.scope = ?");
|
||||
args.push(params.scope);
|
||||
}
|
||||
if (params.level) {
|
||||
where.push("r.level = ?");
|
||||
args.push(params.level);
|
||||
}
|
||||
if (params.topic) {
|
||||
where.push("r.topic = ?");
|
||||
args.push(params.topic);
|
||||
}
|
||||
if (!params.include_superseded) where.push("r.superseded_by IS NULL");
|
||||
if (!params.include_private) where.push("r.private = 0");
|
||||
args.push(Math.min(Math.max(params.top_k ?? 5, 1), 50));
|
||||
const sql = `SELECT r.memory_id, r.text, r.kind, r.project_id, r.scope, r.agent_id, r.created_at,
|
||||
r.superseded_by, r.sources, bm25(records_fts) AS rank,
|
||||
snippet(records_fts, 0, '«', '»', '…', 14) AS snippet
|
||||
FROM records_fts JOIN records r ON r.rowid_ = records_fts.rowid
|
||||
WHERE ${where.join(" AND ")} ORDER BY rank LIMIT ?`;
|
||||
return db.prepare(sql).all(...args) as LocalSearchHit[];
|
||||
}
|
||||
|
||||
export async function localSearch(params: LocalSearchParams, opts?: { dbFile?: string }): Promise<LocalSearchHit[]> {
|
||||
const match = toFtsMatch(params.query, params.exact);
|
||||
if (!match) return [];
|
||||
const db = await openLocalDb(opts?.dbFile);
|
||||
try {
|
||||
let hits = await runSearch(db, params, match);
|
||||
for (const h of hits) h.match_mode = "and";
|
||||
// AND senza risultati → ripiega su OR (match parziale) per non lasciare l'agente a mani vuote
|
||||
if (!hits.length && !params.exact) {
|
||||
const orMatch = toFtsMatch(params.query, false, "or");
|
||||
if (orMatch && orMatch !== match) {
|
||||
hits = await runSearch(db, params, orMatch);
|
||||
for (const h of hits) h.match_mode = "or";
|
||||
}
|
||||
}
|
||||
return hits;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** Recupero locale di un singolo record (fallback di qmem_get). */
|
||||
export async function localGet(memoryId: string, opts?: { dbFile?: string }): Promise<LocalSearchHit | null> {
|
||||
const db = await openLocalDb(opts?.dbFile);
|
||||
try {
|
||||
const row = db
|
||||
.prepare(
|
||||
`SELECT memory_id, text, kind, project_id, scope, agent_id, created_at, superseded_by, sources, 0 AS rank,
|
||||
COALESCE(substr(text,1,200),'') AS snippet
|
||||
FROM records WHERE memory_id = ?`,
|
||||
)
|
||||
.get(memoryId) as LocalSearchHit | undefined;
|
||||
return row ?? null;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Arricchimento dal gateway (quando torna raggiungibile)
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface EnrichStats {
|
||||
requested: number;
|
||||
ok: number;
|
||||
failed: number;
|
||||
updated: number;
|
||||
skipped: number;
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Arricchisce i record locali con i dati autorevoli del gateway
|
||||
* (GET /v1/memories/{id}): testo mancante, project_id, private, stato supersede.
|
||||
* Con `onlyIncomplete` (default) tocca solo i record con dati mancanti.
|
||||
*/
|
||||
export async function enrichFromGateway(
|
||||
cfg: MemoryConfig,
|
||||
opts?: { dbFile?: string; onlyIncomplete?: boolean; limit?: number; paceMs?: number },
|
||||
): Promise<EnrichStats> {
|
||||
const db = await openLocalDb(opts?.dbFile);
|
||||
const stats: EnrichStats = { requested: 0, ok: 0, failed: 0, updated: 0, skipped: 0, errors: [] };
|
||||
try {
|
||||
const only = opts?.onlyIncomplete ?? true;
|
||||
const sql = only
|
||||
? `SELECT memory_id FROM records
|
||||
WHERE text IS NULL OR project_id IS NULL OR synced_at IS NULL OR superseded_by IS NULL
|
||||
ORDER BY (text IS NULL) DESC, synced_at IS NULL DESC LIMIT ?`
|
||||
: "SELECT memory_id FROM records LIMIT ?";
|
||||
const ids = db.prepare(sql).all(opts?.limit ?? 1000).map((r: any) => r.memory_id as string);
|
||||
const pace = opts?.paceMs ?? 600; // ~100 richieste/min (< rate limit 120/min)
|
||||
// ogni GET è limitata a 8s (o al timeout configurato se più basso): evita di
|
||||
// restare appesi decine di secondi per record quando il gateway è giù
|
||||
const perRequestMs = Math.min(cfg.timeoutMs ?? 30_000, 8000);
|
||||
for (const id of ids) {
|
||||
stats.requested++;
|
||||
let res: { ok: boolean; status: number; data: any };
|
||||
try {
|
||||
res = await gatewayRequest(cfg, "GET", `/v1/memories/${id}`, undefined, AbortSignal.timeout(perRequestMs));
|
||||
} catch (e) {
|
||||
// timeout/abort: gateway non raggiungibile → inutile insistere sui record successivi
|
||||
stats.failed++;
|
||||
stats.errors.push(`${id}: ${e instanceof Error ? e.message : String(e)} (timeout ${perRequestMs}ms)`);
|
||||
break;
|
||||
}
|
||||
const { ok, status, data } = res;
|
||||
if (!ok || !data?.memory_id) {
|
||||
stats.failed++;
|
||||
if (stats.errors.length < 3) stats.errors.push(`${id}: HTTP ${status}`);
|
||||
// 0 (rete), 401 (chiave) o 5xx (nodo/upstream giù): il gateway non è utilizzabile
|
||||
if (status === 0 || status === 401 || status >= 500) break;
|
||||
continue;
|
||||
}
|
||||
stats.ok++;
|
||||
upsertRecords(db, [
|
||||
{
|
||||
memory_id: data.memory_id,
|
||||
text: data.text ?? null,
|
||||
kind: data.kind ?? null,
|
||||
project_id: data.project_id ?? null,
|
||||
scope: data.scope ?? null,
|
||||
agent_id: data.agent_id ?? null,
|
||||
confidence: data.confidence ?? null,
|
||||
importance: typeof data.importance === "number" ? data.importance : null,
|
||||
created_at: data.created_at ?? null,
|
||||
supersedes_id: data.supersedes_id ?? null,
|
||||
superseded_by: data.superseded_by ?? null,
|
||||
parent_id: data.parent_id ?? null,
|
||||
topic: data.topic ?? null,
|
||||
level: data.level ?? null,
|
||||
private: data.private ?? null,
|
||||
source: "enrich",
|
||||
},
|
||||
]);
|
||||
stats.updated++;
|
||||
if (pace > 0) await new Promise((r) => setTimeout(r, pace));
|
||||
}
|
||||
db.prepare("INSERT INTO meta(k,v) VALUES('last_enrich',?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(new Date().toISOString());
|
||||
return stats;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pull incrementale dal gateway (richiede `GET /v1/memories:export` sul
|
||||
* gateway: endpoint previsto ma non ancora deployato). Se assente, ritorna
|
||||
* `supported: false` senza errore.
|
||||
*/
|
||||
export async function pullFromGatewayExport(
|
||||
cfg: MemoryConfig,
|
||||
opts?: { dbFile?: string; limit?: number; maxPages?: number },
|
||||
): Promise<{ supported: boolean; pages: number; fetched: number; message?: string }> {
|
||||
const db = await openLocalDb(opts?.dbFile);
|
||||
try {
|
||||
let cursor = db.prepare("SELECT v FROM meta WHERE k='export_cursor'").get()?.v ?? "";
|
||||
const limit = opts?.limit ?? 500;
|
||||
const maxPages = opts?.maxPages ?? 20;
|
||||
let pages = 0;
|
||||
let fetched = 0;
|
||||
for (; pages < maxPages; pages++) {
|
||||
const route = `/v1/memories:export?limit=${limit}${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`;
|
||||
const { ok, status, data } = await gatewayRequest(
|
||||
cfg,
|
||||
"GET",
|
||||
route,
|
||||
undefined,
|
||||
AbortSignal.timeout(Math.min(cfg.timeoutMs ?? 30_000, 10_000)),
|
||||
);
|
||||
if (status === 404 || status === 405 || status === 400) {
|
||||
return { supported: false, pages, fetched, message: `endpoint di export non disponibile sul gateway (HTTP ${status})` };
|
||||
}
|
||||
if (!ok) return { supported: false, pages, fetched, message: `export fallito: HTTP ${status}` };
|
||||
const items = data?.results ?? data?.records ?? [];
|
||||
if (Array.isArray(items) && items.length) {
|
||||
upsertRecords(db, items.map((r: any) => ({ ...r, source: "export" })));
|
||||
fetched += items.length;
|
||||
}
|
||||
cursor = data?.next_cursor ?? "";
|
||||
if (!cursor || !items.length) break;
|
||||
db.prepare("INSERT INTO meta(k,v) VALUES('export_cursor',?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(String(cursor));
|
||||
}
|
||||
db.prepare("INSERT INTO meta(k,v) VALUES('last_export',?) ON CONFLICT(k) DO UPDATE SET v=excluded.v").run(new Date().toISOString());
|
||||
return { supported: true, pages, fetched };
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Report / stato
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface LocalDbReport {
|
||||
path: string;
|
||||
exists: boolean;
|
||||
sizeKb: number;
|
||||
total: number;
|
||||
withText: number;
|
||||
active: number;
|
||||
superseded: number;
|
||||
private: number;
|
||||
withProject: number;
|
||||
lastImport?: string;
|
||||
lastEnrich?: string;
|
||||
lastExport?: string;
|
||||
topProjects: Array<{ project_id: string | null; n: number }>;
|
||||
duplicates: Array<{ text_hash: string; n: number; ids: string[] }>;
|
||||
}
|
||||
|
||||
export async function localDbReport(opts?: { dbFile?: string }): Promise<LocalDbReport> {
|
||||
const file = opts?.dbFile ?? localDbPath(loadConfig());
|
||||
const exists = fs.existsSync(file);
|
||||
const report: LocalDbReport = {
|
||||
path: file,
|
||||
exists,
|
||||
sizeKb: 0,
|
||||
total: 0,
|
||||
withText: 0,
|
||||
active: 0,
|
||||
superseded: 0,
|
||||
private: 0,
|
||||
withProject: 0,
|
||||
topProjects: [],
|
||||
duplicates: [],
|
||||
};
|
||||
if (!exists) return report;
|
||||
for (const f of [file, `${file}-wal`, `${file}-shm`]) {
|
||||
try {
|
||||
report.sizeKb += Math.round(fs.statSync(f).size / 1024);
|
||||
} catch {
|
||||
/* assente */
|
||||
}
|
||||
}
|
||||
const db = await openLocalDb(file);
|
||||
try {
|
||||
const one = (sql: string) => db.prepare(sql).get()?.n ?? 0;
|
||||
report.total = one("SELECT COUNT(*) n FROM records");
|
||||
report.withText = one("SELECT COUNT(*) n FROM records WHERE text IS NOT NULL");
|
||||
report.active = one("SELECT COUNT(*) n FROM records WHERE superseded_by IS NULL");
|
||||
report.superseded = one("SELECT COUNT(*) n FROM records WHERE superseded_by IS NOT NULL");
|
||||
report.private = one("SELECT COUNT(*) n FROM records WHERE private = 1");
|
||||
report.withProject = one("SELECT COUNT(*) n FROM records WHERE project_id IS NOT NULL");
|
||||
report.lastImport = db.prepare("SELECT v FROM meta WHERE k='last_import'").get()?.v;
|
||||
report.lastEnrich = db.prepare("SELECT v FROM meta WHERE k='last_enrich'").get()?.v;
|
||||
report.lastExport = db.prepare("SELECT v FROM meta WHERE k='last_export'").get()?.v;
|
||||
report.topProjects = db.prepare("SELECT project_id, COUNT(*) n FROM records GROUP BY project_id ORDER BY n DESC LIMIT 8").all() as any;
|
||||
report.duplicates = db
|
||||
.prepare("SELECT text_hash, COUNT(*) n, GROUP_CONCAT(memory_id) ids FROM records WHERE text_hash IS NOT NULL GROUP BY text_hash HAVING n > 1 ORDER BY n DESC LIMIT 20")
|
||||
.all()
|
||||
.map((r: any) => ({ text_hash: r.text_hash, n: r.n, ids: String(r.ids).split(",") }));
|
||||
return report;
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,11 @@ MUST NOT:
|
||||
- Narrow search (scope/kind/project_id) without qmem_meta first.
|
||||
- Save without project_id or raw transcripts.
|
||||
Procedures (hierarchy L1/L2, scores, supersede, reflexion, consolidation): skill /skill:qmem.
|
||||
### Indice locale (fallback offline)
|
||||
- Quando il gateway non risponde, qmem_search degrada all'INDICE LOCALE SQLite/FTS5 (ricerca testuale, NON neurale: nessuno score 0.45/0.60). Il risultato è etichettato 'fallback: local_sqlite'.
|
||||
- I risultati locali sono osservazioni più vecchie del gateway: verificali prima dell'uso e non applicare le soglie di score del gateway.
|
||||
- qmem_store NON ha coda locale: a gateway giù il record non viene salvato → riprovalo quando torna raggiungibile.
|
||||
- Gestione: /qmem:local status | import | find <query> | enrich | pull (import = ricostruisce l'indice dalle sessioni pi; enrich/pull = allineamento dal gateway).
|
||||
### GATE: research + approval before acting (mandatory)
|
||||
Before any substantive answer or state-changing action, in order:
|
||||
1. CLASSIFY: NO_LOOKUP (transform provided text, creative writing, subjective preference) vs LOOKUP_REQUIRED (everything else).
|
||||
|
||||
@@ -31,6 +31,10 @@ export interface MemoryConfig {
|
||||
apiKey: string;
|
||||
timeoutMs?: number;
|
||||
correctMinScore?: number;
|
||||
/** Percorso del DB SQLite locale (default ~/.local/share/pi-qmem/qmem.sqlite). */
|
||||
localDbPath?: string;
|
||||
/** Usa l'indice locale come fallback quando il gateway non risponde (default true). */
|
||||
localFallback?: boolean;
|
||||
}
|
||||
|
||||
const CONFIG_DEFAULTS: MemoryConfig = {
|
||||
@@ -38,6 +42,7 @@ const CONFIG_DEFAULTS: MemoryConfig = {
|
||||
apiKey: "",
|
||||
timeoutMs: 30_000,
|
||||
correctMinScore: 0.6,
|
||||
localFallback: true,
|
||||
};
|
||||
|
||||
// Retry su errori transitori (429/5xx/timeout): backoff esponenziale + jitter
|
||||
|
||||
+23
-1
@@ -1,6 +1,7 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import { Type } from "typebox";
|
||||
import { gatewayRequest, loadConfig } from "../shared";
|
||||
import { gatewayRequest, loadConfig } from "../shared.ts";
|
||||
import { localDbPath, localGet } from "../local-db.ts";
|
||||
|
||||
export function registerQmemGet(pi: ExtensionAPI) {
|
||||
pi.registerTool({
|
||||
@@ -24,6 +25,27 @@ export function registerQmemGet(pi: ExtensionAPI) {
|
||||
const { ok, status, data } = await gatewayRequest(cfg, "GET", `/v1/memories/${p.memory_id}`, undefined, signal);
|
||||
if (!ok) {
|
||||
const notFound = status === 404 || data?.detail === "Memoria non trovata";
|
||||
if (!notFound) {
|
||||
// Gateway non raggiungibile: tentativo sull'indice locale (SQLite/FTS5)
|
||||
try {
|
||||
const local = await localGet(p.memory_id, { dbFile: localDbPath(cfg) });
|
||||
if (local) {
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`⚠️ Gateway non raggiungibile (HTTP ${status}): record dall'INDICE LOCALE (osservazione più vecchia del gateway, può essere incompleta).\n` +
|
||||
`memory_id: ${local.memory_id}\n[${local.kind ?? "?"}/${local.scope ?? "?"}${local.project_id ? ` project=${local.project_id}` : ""}] agente: ${local.agent_id ?? "?"}, creato: ${local.created_at ?? "?"}${local.superseded_by ? `, ⚠️ superseduto da ${local.superseded_by}` : ""}\n\n${local.text ?? "(nessun testo)"}`,
|
||||
},
|
||||
],
|
||||
details: { memory_id: local.memory_id, fallback: "local_sqlite", gateway_status: status },
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
/* indice locale non disponibile: si prosegue con l'errore del gateway */
|
||||
}
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import { Type } from "typebox";
|
||||
import { gatewayRequest, loadConfig } from "../shared";
|
||||
import { gatewayRequest, loadConfig } from "../shared.ts";
|
||||
import { localDbPath, localSearch, type LocalSearchHit } from "../local-db.ts";
|
||||
|
||||
export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
pi.registerTool({
|
||||
@@ -86,6 +87,60 @@ export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
signal,
|
||||
);
|
||||
if (!ok) {
|
||||
// Gateway non raggiungibile → fallback sull'indice locale SQLite/FTS5
|
||||
const canFallback = cfg.localFallback !== false && (status === 0 || status >= 500 || status === 429);
|
||||
if (canFallback) {
|
||||
const dbFile = localDbPath(cfg);
|
||||
try {
|
||||
const hits = await localSearch(
|
||||
{
|
||||
query: String(p.query ?? ""),
|
||||
kind: p.kind,
|
||||
project_id: p.project_id,
|
||||
scope: p.scope,
|
||||
level: p.level,
|
||||
topic: p.topic,
|
||||
include_superseded: p.include_superseded ?? false,
|
||||
include_private: p.include_private ?? false,
|
||||
top_k: p.top_k ?? 5,
|
||||
},
|
||||
{ dbFile },
|
||||
);
|
||||
if (hits.length) {
|
||||
const lines = hits.map(
|
||||
(h: LocalSearchHit, i: number) =>
|
||||
`${i + 1}. [${h.kind ?? "?"}/${h.scope ?? "?"}${h.project_id ? ` project=${h.project_id}` : ""} locale${h.match_mode === "or" ? " match-parziale(OR)" : ""}${h.superseded_by ? " ⚠️ superseduto" : ""}] ${h.snippet}\n (id: ${h.memory_id}, creato: ${h.created_at ?? "?"}, agente: ${h.agent_id ?? "?"}, fonti: ${h.sources ?? "?"})`,
|
||||
);
|
||||
const header =
|
||||
`⚠️ Gateway non raggiungibile (HTTP ${status}): risultati dall'INDICE LOCALE (SQLite/FTS5).\n` +
|
||||
`Ricerca TESTUALE, non neurale: nessuno score semantico, nessuna soglia 0.45/0.60 — verifica i risultati prima dell'uso.\n` +
|
||||
`DB: ${dbFile}`;
|
||||
return {
|
||||
content: [{ type: "text", text: `${header}\n${lines.join("\n")}` }],
|
||||
details: {
|
||||
fallback: "local_sqlite",
|
||||
hits: hits.length,
|
||||
gateway_status: status,
|
||||
match_mode: hits[0]?.match_mode ?? "and",
|
||||
},
|
||||
};
|
||||
}
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`Gateway non raggiungibile (HTTP ${status}) e nessun risultato nell'indice locale (${dbFile}).\n` +
|
||||
`Se il DB è assente o vecchio: /qmem:local import (ricostruisce l'indice dalle sessioni pi).`,
|
||||
},
|
||||
],
|
||||
details: { error: "gateway_error", status, fallback: "local_sqlite", hits: 0 },
|
||||
};
|
||||
} catch (e) {
|
||||
// nessun node:sqlite o DB illeggibile: si prosegue con l'errore del gateway
|
||||
void e;
|
||||
}
|
||||
}
|
||||
return {
|
||||
content: [{ type: "text", text: `Errore ${status}: ${JSON.stringify(data)}` }],
|
||||
details: { error: "gateway_error", status },
|
||||
@@ -109,7 +164,9 @@ export function registerQmemSearch(pi: ExtensionAPI) {
|
||||
const top = r.topic ? ` (${r.topic})` : "";
|
||||
const parent = r.parent_id ? `, parent: ${r.parent_id}` : "";
|
||||
const links = r.links && r.links.length > 0 ? `, links: ${r.links.length}` : "";
|
||||
return `${i + 1}. [${r.kind}/${r.scope}${lvl}${top} score=${r.score}${r.score < 0.6 ? " ⚠️" : ""}${r.rerank_score != null ? ` rerank=${r.rerank_score}` : ""}${r.composite_score != null ? ` composite=${r.composite_score}` : ""}${r.confidence ? ` conf=${r.confidence}` : ""}] ${r.text}\n (id: ${r.memory_id}${parent}${links}, agente: ${r.agent_id ?? "?"}, creato: ${r.created_at ?? "?"}${r.importance != null && r.importance !== 0.5 ? `, importanza: ${r.importance}` : ""}${r.source ? `, fonte: ${r.source}` : ""}${r.supersedes_id ? `, supersede ${r.supersedes_id}` : ""}${r.superseded_by ? `, ⚠️ superseduto da ${r.superseded_by}` : ""})`;
|
||||
const proj = ` project=${r.project_id ?? "?"}`;
|
||||
const priv = r.private ? ", 🔒 privato" : "";
|
||||
return `${i + 1}. [${r.kind}/${r.scope}${lvl}${top}${proj} score=${r.score}${r.score < 0.6 ? " ⚠️" : ""}${r.rerank_score != null ? ` rerank=${r.rerank_score}` : ""}${r.composite_score != null ? ` composite=${r.composite_score}` : ""}${r.confidence ? ` conf=${r.confidence}` : ""}] ${r.text}\n (id: ${r.memory_id}${parent}${links}, agente: ${r.agent_id ?? "?"}, creato: ${r.created_at ?? "?"}${r.importance != null && r.importance !== 0.5 ? `, importanza: ${r.importance}` : ""}${r.source ? `, fonte: ${r.source}` : ""}${r.supersedes_id ? `, supersede ${r.supersedes_id}` : ""}${r.superseded_by ? `, ⚠️ superseduto da ${r.superseded_by}` : ""}${priv})`;
|
||||
},
|
||||
);
|
||||
return {
|
||||
|
||||
@@ -108,8 +108,18 @@ export function registerQmemStore(pi: ExtensionAPI) {
|
||||
idemKey,
|
||||
);
|
||||
if (!ok) {
|
||||
const down = status === 0 || status >= 500 || status === 429;
|
||||
return {
|
||||
content: [{ type: "text", text: `Errore ${status}: ${JSON.stringify(data)}` }],
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text:
|
||||
`Errore ${status}: ${JSON.stringify(data)}` +
|
||||
(down
|
||||
? "\n⚠️ Il record NON è stato salvato: nessuna coda locale (il gateway è la fonte di verità). Riprova quando è raggiungibile, oppure annota il contenuto e usa /qmem:local import per l'indice testuale."
|
||||
: ""),
|
||||
},
|
||||
],
|
||||
details: { error: "gateway_error", status },
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user