mirror of
https://gitea.ingwaz.work/Ingwaz/openbrain-mcp.git
synced 2026-03-31 14:49:06 +00:00
Add TTL expiry for transient facts
This commit is contained in:
@@ -73,11 +73,45 @@ Install pgvector for your active PostgreSQL major version, then run: CREATE EXTE
|
||||
embedding vector(384) NOT NULL,
|
||||
keywords TEXT[] DEFAULT '{}',
|
||||
metadata JSONB DEFAULT '{}',
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
expires_at TIMESTAMPTZ
|
||||
);
|
||||
ALTER TABLE memories ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ;
|
||||
ALTER TABLE memories ADD COLUMN IF NOT EXISTS tsv tsvector;
|
||||
CREATE OR REPLACE FUNCTION memories_tsv_trigger()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
BEGIN
|
||||
NEW.tsv :=
|
||||
setweight(to_tsvector('pg_catalog.english', COALESCE(NEW.content, '')), 'A') ||
|
||||
setweight(
|
||||
to_tsvector('pg_catalog.english', COALESCE(array_to_string(NEW.keywords, ' '), '')),
|
||||
'B'
|
||||
);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
UPDATE memories
|
||||
SET tsv =
|
||||
setweight(to_tsvector('pg_catalog.english', COALESCE(content, '')), 'A') ||
|
||||
setweight(
|
||||
to_tsvector('pg_catalog.english', COALESCE(array_to_string(keywords, ' '), '')),
|
||||
'B'
|
||||
)
|
||||
WHERE tsv IS NULL;
|
||||
DROP TRIGGER IF EXISTS memories_tsv_update ON memories;
|
||||
CREATE TRIGGER memories_tsv_update
|
||||
BEFORE INSERT OR UPDATE OF content, keywords ON memories
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION memories_tsv_trigger();
|
||||
CREATE INDEX IF NOT EXISTS idx_memories_agent ON memories(agent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_memories_embedding ON memories
|
||||
USING hnsw (embedding vector_cosine_ops);
|
||||
CREATE INDEX IF NOT EXISTS idx_memories_tsv ON memories
|
||||
USING GIN (tsv);
|
||||
CREATE INDEX IF NOT EXISTS idx_memories_expires_at ON memories (expires_at)
|
||||
WHERE expires_at IS NOT NULL;
|
||||
"#,
|
||||
)
|
||||
.await
|
||||
|
||||
Reference in New Issue
Block a user