refactor: split gateway and pi-qmem extension into modules
- gateway: separate config, models, state, audit, guardrail, embeddings, store, metrics, cleanup and routes; keep main.py as FastAPI bootstrap - extension: split client/config, six tools, config command and rules; preserve jiti entrypoint and registrations - Dockerfile copies the complete gateway module set - tests: update monkeypatch boundaries for modular config/state
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
||||
import { Type } from "typebox";
|
||||
import { gatewayRequest, loadConfig } from "../shared";
|
||||
|
||||
export function registerQmemGet(pi: ExtensionAPI) {
|
||||
pi.registerTool({
|
||||
name: "qmem_get",
|
||||
label: "Qmem memory get by ID",
|
||||
description:
|
||||
"Recupera un record di memoria per UUID (recupero deterministico, non semantico). " +
|
||||
"Usalo quando conosci gia' l'ID di un record (es. citato da un puntatore, dal playbook o da un altro record): " +
|
||||
"qmem_search non puo' garantire di trovare il record giusto, qmem_get lo restituisce esattamente. " +
|
||||
"Restituisce anche i record superseduti (utile per lineage/audit). " +
|
||||
"Per trovare record senza conoscerne l'ID usa qmem_search.",
|
||||
promptGuidelines: [
|
||||
"qmem_get: se un record cita un ID (es. 'record e526b65a'), usa qmem_get con quell'ID per recuperarlo esattamente — non tentare di indovinarlo con qmem_search.",
|
||||
],
|
||||
parameters: Type.Object({
|
||||
memory_id: Type.String({ description: "UUID del record da recuperare (es. dalla risposta di qmem_search o da un puntatore)." }),
|
||||
}),
|
||||
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";
|
||||
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,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user