# plugins/postgres_memory.py
from gitagent.plugins import MemoryBackend
import psycopg2
class PostgresMemory(MemoryBackend):
def __init__(self, dsn: str):
self.conn = psycopg2.connect(dsn)
def read(self, key: str) -> str | None:
with self.conn.cursor() as cur:
cur.execute("SELECT value FROM agent_memory WHERE key = %s", (key,))
row = cur.fetchone()
return row[0] if row else None
def write(self, key: str, value: str) -> None:
with self.conn.cursor() as cur:
cur.execute(
"INSERT INTO agent_memory (key, value) VALUES (%s, %s) "
"ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value",
(key, value)
)
self.conn.commit()
def delete(self, key: str) -> None:
with self.conn.cursor() as cur:
cur.execute("DELETE FROM agent_memory WHERE key = %s", (key,))
self.conn.commit()