Prima qmem_store falliva se il gateway non era raggiungibile: la conoscenza andava persa. Ora il record entra in una coda locale persistente e viene inviato automaticamente quando la connessione torna. Core (extensions/local-db.ts): - tabella `pending` (local_id, payload JSON, attempts, last_error, status, remote_id) + colonna `records.pending` (migrazione automatica dei DB esistenti) - queueStore(): accoda e crea subito il placeholder locale ricercabile (⏳) - flushQueue(): POST /v1/memories con Idempotency-Key = local_id (retry senza duplicati), FIFO, pacing sotto il rate limit, timeout 12s per richiesta - esiti: synced (il record locale adotta l'ID remoto, niente duplicati) · duplicate (409: registra l'ID del match e NON sovrascrive il testo locale autorevole) · failed (4xx di validazione, non ritentato) · 0/429/5xx: resta in coda e il flush si ferma - supersede offline: supersedes_id che punta a un local_id viene rimappato al remote_id al flush (se il genitore non è sincronizzato → failed esplicito) - submitOrQueue(): online → gateway + indicizzazione locale; offline → coda - maybeBackgroundFlush() (single-flight) e flushQueueIfPending() per session_start - stato/report: queued/synced/duplicate/failed, più vecchio, ultimo errore, last_flush, record pendenti in indice Estensione: - qmem_store: gateway giù → accoda e risponde con id locale, dimensione coda e spiegazione (details.queued/local_id/queue_size) - fallback offline di session_start: flush in background (non blocca l'avvio) - /qmem:local queue|flush; status con la coda; marker "⏳ in coda" nei risultati locali di qmem_search/qmem_get - regole e skill: un record in coda NON è ancora nella memoria condivisa CLI: store [--queue-only], queue, flush (+ status con la coda). Test: scripts/test-local.mjs ora copre anche outbox → 24 controlli (flush con 2 sync + 1 duplicato 409 + 1 fallito 422, Idempotency-Key, rimappatura del supersede, ricerca del record con l'ID remoto dopo il sync). Verifiche: 24/24 test superati; demo reale su DB temporaneo: store accodato, queue con local_id, flush con gateway giù → "fermato: HTTP 0" e voce che resta in coda con l'errore registrato.
90 lines
3.8 KiB
TypeScript
90 lines
3.8 KiB
TypeScript
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
import { Type } from "typebox";
|
|
import { gatewayRequest, loadConfig } from "../shared.ts";
|
|
import { localDbPath, localGet } from "../local-db.ts";
|
|
|
|
export function registerQmemGet(pi: ExtensionAPI) {
|
|
pi.registerTool({
|
|
name: "qmem_get",
|
|
label: "Qmem memory get by ID",
|
|
description:
|
|
"Fetch one record exactly by UUID, including superseded records. Use qmem_search when the UUID is unknown.",
|
|
parameters: Type.Object({
|
|
memory_id: Type.String({ description: "Record UUID." }),
|
|
}),
|
|
async execute(toolCallId, params, signal, onUpdate, ctx) {
|
|
const cfg = loadConfig();
|
|
if (!cfg.apiKey) {
|
|
return {
|
|
content: [{ type: "text", text: "Config mancante: esegui /qmem:config per impostare url e apiKey." }],
|
|
details: { error: "missing_config" },
|
|
};
|
|
}
|
|
const p = params as any;
|
|
onUpdate?.({ content: [{ type: "text", text: `qmem: recupero record ${p.memory_id}...` }] });
|
|
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.pending ? ", ⏳ creato offline: non ancora sul gateway" : ""}${local.superseded_by ? `, ⚠️ superseduto da ${local.superseded_by}` : ""}${local.remote_id ? `, sincronizzato come ${local.remote_id}` : ""}\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: [
|
|
{
|
|
type: "text",
|
|
text: notFound
|
|
? `Record ${p.memory_id} non trovato (404). Verifica l'ID (qmem_search include_superseded=true per la lineage) oppure l'URL gateway.`
|
|
: `Errore ${status}: ${JSON.stringify(data)}`,
|
|
},
|
|
],
|
|
details: { error: notFound ? "not_found" : "gateway_error", status, memory_id: p.memory_id },
|
|
};
|
|
}
|
|
const r = data ?? {};
|
|
const meta = [
|
|
`[${r.kind ?? "?"}/${r.scope ?? "?"}${r.level ? ` ${r.level}` : ""}${r.topic ? ` (${r.topic})` : ""}${r.confidence ? ` conf=${r.confidence}` : ""}]`,
|
|
`project: ${r.project_id ?? "?"}`,
|
|
`agente: ${r.agent_id ?? "?"}, creato: ${r.created_at ?? "?"}${r.source ? `, fonte: ${r.source}` : ""}`,
|
|
];
|
|
if (r.parent_id) meta.push(`parent: ${r.parent_id}`);
|
|
if (r.links && r.links.length > 0) meta.push(`links: ${r.links.length}`);
|
|
if (r.supersedes_id) meta.push(`supersede ${r.supersedes_id}`);
|
|
if (r.superseded_by) meta.push(`⚠️ SUPERSEDUTO da ${r.superseded_by}`);
|
|
if (r.expires_at) meta.push(`scade: ${r.expires_at}`);
|
|
const lines = [`memory_id: ${r.memory_id ?? p.memory_id}`, meta.join(" | "), "", r.text ?? "(nessun testo)"];
|
|
return {
|
|
content: [{ type: "text", text: lines.join("\n") }],
|
|
details: {
|
|
memory_id: r.memory_id ?? p.memory_id,
|
|
kind: r.kind,
|
|
scope: r.scope,
|
|
project_id: r.project_id,
|
|
parent_id: r.parent_id,
|
|
level: r.level,
|
|
topic: r.topic,
|
|
superseded_by: r.superseded_by ?? null,
|
|
},
|
|
};
|
|
},
|
|
});
|
|
|
|
}
|