oclaw/svc/config/database.py
oliver d14e9d3596 feat(persistence): PostgreSQL assistant store, chat persist fixes, gateway scripts
- Add SQLAlchemy Core repos, pg adapter/compat, assistant_store factory, Alembic bootstrap and migration/cutover scripts.

- Harden chat_message writes (NUL scrub for PG), turn_uuid on attempt failure, WS turn_runner fallbacks and gateway executed_turn_uuid init.

- start_gateway: log paths, PS7 stderr handling via cmd, background stdout/stderr redirect; runtime assistant_runtime_log_dir export.

- Ops: clear_all_chat_sessions with PG-only --postgresql and env-gated wipe; clear_postgres_chat_sessions.ps1.

- Tests: SA repos, pg compat, persist fallback, smoke env isolation; CI and docs touch-ups.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-14 09:33:50 +08:00

57 lines
1.9 KiB
Python

"""Assistant database backend selection (SQLite default, PostgreSQL opt-in via env)."""
from __future__ import annotations
import os
def assistant_db_backend() -> str:
"""Return ``sqlite`` (default) or ``postgresql``."""
raw = (
os.getenv("AIA_ASSISTANT_DB_BACKEND")
or os.getenv("OPS_ASSISTANT_DB_BACKEND")
or "sqlite"
).strip().lower()
if raw in ("sqlite", ""):
return "sqlite"
if raw in ("pg", "postgres", "postgresql"):
return "postgresql"
raise ValueError(
f"Invalid assistant DB backend {raw!r}. "
"Use sqlite (default) or postgresql (aliases: pg, postgres)."
)
def assistant_sqlalchemy_url() -> str:
"""SQLAlchemy URL for the assistant store (sqlite or postgresql+psycopg)."""
if assistant_db_backend() == "postgresql":
raw = assistant_postgres_dsn()
if raw.startswith("postgresql+") or raw.startswith("postgres+"):
return raw
if raw.startswith("postgresql://") or raw.startswith("postgres://"):
return "postgresql+psycopg://" + raw.split("://", 1)[1]
return raw
from svc.config.paths import db_path
p = db_path().replace("\\", "/")
return f"sqlite+pysqlite:///{p}"
def assistant_postgres_dsn() -> str:
"""PostgreSQL connection URI for the assistant store (psycopg/libpq format)."""
url = (
os.getenv("AIA_ASSISTANT_DATABASE_URL")
or os.getenv("OPS_ASSISTANT_DATABASE_URL")
or os.getenv("AIA_ASSISTANT_PG_DSN")
or os.getenv("OPS_ASSISTANT_PG_DSN")
or ""
).strip()
if not url:
raise ValueError(
"PostgreSQL backend requires AIA_ASSISTANT_DATABASE_URL (or OPS_ASSISTANT_DATABASE_URL) "
"to a libpq connection string, e.g. postgresql://user:pass@127.0.0.1:5432/oclaw"
)
return url
__all__ = ["assistant_db_backend", "assistant_postgres_dsn", "assistant_sqlalchemy_url"]