fix(pheby): migrate bridge to the new persistence repository API
The Hermes update replaced SessionDB with the persistence layer; raw SQL (_read_all), create_session/append_message/delete_sessions, get_messages_as_conversation and set_session_yolo are gone. Use sessions.search_sessions (client-side session_key filter), messages .read_conversation(ConversationRequest) with as_openai_message rows, sessions.create/append/delete/get/set_yolo instead. Update test fakes and open the storage interface directly in the cron delivery tests.
This commit is contained in:
@@ -206,13 +206,24 @@ async def list_conversations(server: Any = None) -> List[Dict[str, Any]]:
|
||||
db = _session_db()
|
||||
if db is not None:
|
||||
try:
|
||||
deliveries = await asyncio.to_thread(
|
||||
db._read_all,
|
||||
"SELECT s.chat_id, s.id, MAX(m.timestamp) AS last_active "
|
||||
"FROM sessions s JOIN messages m ON m.session_id = s.id "
|
||||
"WHERE s.source = ? GROUP BY s.id, s.chat_id",
|
||||
("pheby_delivery",),
|
||||
# The persistence layer no longer exposes raw SQL; the sessions
|
||||
# repository's source search returns every row (archived included)
|
||||
# with its computed last_active — the same recency the old
|
||||
# MAX(messages.timestamp) query produced.
|
||||
from persistence.conversation.sessions import SessionWindow
|
||||
entries = await asyncio.to_thread(
|
||||
db.sessions.search_sessions,
|
||||
sources=("pheby_delivery",),
|
||||
window=SessionWindow(limit=None),
|
||||
)
|
||||
deliveries = [
|
||||
{
|
||||
"chat_id": getattr(e.session, "chat_id", None),
|
||||
"id": str(getattr(e.session, "session_id", "")),
|
||||
"last_active": getattr(e, "last_active", None),
|
||||
}
|
||||
for e in entries
|
||||
]
|
||||
by_id = {item["conversation_id"]: item for item in out}
|
||||
for row in deliveries:
|
||||
cid = str(row["chat_id"] or "")
|
||||
@@ -285,12 +296,16 @@ async def persist_cron_delivery(conversation_id: str, content: str,
|
||||
timestamp = time.time()
|
||||
|
||||
def _persist() -> int:
|
||||
db.create_session(sid, "pheby_delivery", session_key=key,
|
||||
chat_id=conversation_id, chat_type="dm")
|
||||
return db.append_message(
|
||||
sid, "assistant", content, timestamp=timestamp,
|
||||
from persistence.conversation.messages import NewMessage
|
||||
from persistence.conversation.sessions import NewSession, SessionId
|
||||
db.sessions.create(NewSession(
|
||||
session_id=SessionId(sid), source="pheby_delivery", session_key=key,
|
||||
chat_id=conversation_id, chat_type="dm"))
|
||||
msg_id = db.messages.append(sid, NewMessage(
|
||||
role="assistant", content=content, timestamp=timestamp,
|
||||
display_kind="cron_delivery",
|
||||
display_metadata={"job_id": str(metadata["job_id"])})
|
||||
display_metadata={"job_id": str(metadata["job_id"])}))
|
||||
return int(msg_id)
|
||||
|
||||
row_id = await asyncio.to_thread(_persist)
|
||||
server = _current_server()
|
||||
@@ -334,24 +349,40 @@ async def conversation_history(conversation_id: str, limit: int,
|
||||
db = _session_db()
|
||||
if db is not None:
|
||||
try:
|
||||
# The routing index only knows the current generation. Query the
|
||||
# The routing index only knows the current generation. Search the
|
||||
# persisted key to include resets even when parent_session_id is
|
||||
# absent. Hermes's own decoder handles archived/compacted rows.
|
||||
ids = await asyncio.to_thread(
|
||||
db._read_all,
|
||||
"SELECT id FROM sessions WHERE session_key = ? AND source IN (?, ?) ORDER BY started_at, id",
|
||||
(session_key, "pheby", "pheby_delivery"),
|
||||
# absent. Raw SQL is gone from the persistence layer; the sessions
|
||||
# repository's source search (no archived filter) plus a
|
||||
# client-side session_key filter is the equivalent lookup.
|
||||
from persistence.conversation.sessions import SessionWindow
|
||||
entries = await asyncio.to_thread(
|
||||
db.sessions.search_sessions,
|
||||
sources=("pheby", "pheby_delivery"),
|
||||
window=SessionWindow(limit=None),
|
||||
)
|
||||
session_ids = [str(row["id"]) for row in ids]
|
||||
keyed = {
|
||||
str(e.session.session_id):
|
||||
(float(getattr(e.session, "started_at", 0.0) or 0.0),
|
||||
str(e.session.session_id))
|
||||
for e in entries
|
||||
if str(getattr(e.session, "session_key", "") or "") == str(session_key)
|
||||
}
|
||||
# Chronological order (the old SQL ordered by started_at, id):
|
||||
# older generations must page before the current one.
|
||||
session_ids = [sid for sid, _ in sorted(keyed.items(), key=lambda kv: kv[1])]
|
||||
if session_id and session_id not in session_ids:
|
||||
session_ids.append(session_id)
|
||||
if session_ids:
|
||||
found = True
|
||||
rows = []
|
||||
from persistence.conversation.messages import ConversationRequest, TranscriptView
|
||||
for sid in session_ids:
|
||||
rows.extend(await asyncio.to_thread(
|
||||
db.get_messages_as_conversation, sid,
|
||||
include_row_ids=True, include_compacted=True))
|
||||
for msg in await asyncio.to_thread(
|
||||
db.messages.read_conversation, sid,
|
||||
ConversationRequest(include_row_ids=True,
|
||||
view=TranscriptView.DISPLAY)):
|
||||
row = msg.as_openai_message()
|
||||
rows.append(row)
|
||||
rows.sort(key=lambda row: int(row.get("_row_id") or 0))
|
||||
visible = []
|
||||
seen_replays = set()
|
||||
@@ -453,18 +484,33 @@ async def delete_conversation(conversation_id: str) -> bool:
|
||||
|
||||
if db is not None:
|
||||
try:
|
||||
rows = await asyncio.to_thread(
|
||||
db._read_all,
|
||||
"SELECT id FROM sessions WHERE session_key = ? AND source IN (?, ?) ORDER BY started_at, id",
|
||||
(session_key, "pheby", "pheby_delivery"),
|
||||
from persistence.conversation.sessions import SessionWindow
|
||||
entries = await asyncio.to_thread(
|
||||
db.sessions.search_sessions,
|
||||
sources=("pheby", "pheby_delivery"),
|
||||
window=SessionWindow(limit=None),
|
||||
)
|
||||
ids = [str(row["id"]) for row in rows]
|
||||
ids = [
|
||||
str(e.session.session_id) for e in entries
|
||||
if str(getattr(e.session, "session_key", "") or "") == str(session_key)
|
||||
]
|
||||
if session_id and session_id not in ids:
|
||||
ids.append(str(session_id))
|
||||
if ids:
|
||||
deleted = await asyncio.to_thread(db.delete_sessions, ids)
|
||||
if deleted != len(ids):
|
||||
return False
|
||||
from gateway.session_ingress_media import native_inputs_root
|
||||
removed = set()
|
||||
for target in ids:
|
||||
deleted = await asyncio.to_thread(
|
||||
db.sessions.delete, target,
|
||||
media_root=str(native_inputs_root()))
|
||||
removed.update(str(i) for i in deleted)
|
||||
# Every requested id must be gone afterwards (delete() also
|
||||
# removes lineage children, so removed can be a superset).
|
||||
for target in ids:
|
||||
if target in removed:
|
||||
continue
|
||||
if await asyncio.to_thread(db.sessions.get, target) is not None:
|
||||
return False
|
||||
except Exception:
|
||||
logger.error("[pheby] session db delete failed", exc_info=True)
|
||||
return False
|
||||
@@ -897,9 +943,13 @@ async def yolo_snapshot(conversation_id: str) -> Dict[str, Any]:
|
||||
db = _session_db()
|
||||
if db is not None:
|
||||
try:
|
||||
meta = await asyncio.to_thread(db.get_session, str(session_id))
|
||||
from hermes_state import SessionDB
|
||||
if SessionDB.session_yolo_enabled(meta):
|
||||
from persistence.conversation.sessions import (
|
||||
SessionId,
|
||||
session_yolo_enabled,
|
||||
)
|
||||
record = await asyncio.to_thread(
|
||||
db.sessions.get, SessionId(str(session_id)))
|
||||
if record is not None and session_yolo_enabled(record.model_config):
|
||||
enable_session_yolo(session_key)
|
||||
enabled = True
|
||||
except Exception:
|
||||
@@ -927,7 +977,9 @@ async def set_yolo(conversation_id: str, enabled: bool) -> Dict[str, Any]:
|
||||
db = _session_db()
|
||||
if db is not None:
|
||||
try:
|
||||
await asyncio.to_thread(db.set_session_yolo, str(session_id), bool(enabled))
|
||||
from persistence.conversation.sessions import SessionId
|
||||
await asyncio.to_thread(
|
||||
db.sessions.set_yolo, SessionId(str(session_id)), bool(enabled))
|
||||
except Exception:
|
||||
logger.warning("[pheby] yolo persistence failed", exc_info=True)
|
||||
return {"ok": True, "enabled": bool(enabled), "scope": "conversation",
|
||||
|
||||
Reference in New Issue
Block a user