Spaces:
Running
Running
File size: 7,155 Bytes
691dc7c e99df76 691dc7c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | from __future__ import annotations
"""Transactional messaging records in the application-owned FCC database."""
import sqlite3
from datetime import UTC, datetime
from free_claude_code.messaging.models import MessageScope
from free_claude_code.messaging.trees import (
ConversationSnapshot,
MessagingStorageError,
TreeIdentity,
TreeSnapshot,
normalize_tree_snapshot,
)
from .sqlite_database import SQLiteDatabase
_FIELDS = (
"node_id",
"status_message_id",
"state",
"parent_id",
"parent_reference_id",
"session_id",
)
def write_tree(connection: sqlite3.Connection, snapshot: TreeSnapshot) -> None:
"""Update only changed rows, inserting new parents before their children."""
normalized = normalize_tree_snapshot(snapshot)
scope = (snapshot.scope.platform, snapshot.scope.chat_id)
identity = (*scope, snapshot.root_id)
connection.execute(
"INSERT INTO messaging_trees VALUES (?,?,?) ON CONFLICT DO NOTHING", identity
)
previous = {
row["node_id"]: dict(row)
for row in connection.execute(
"SELECT * FROM messaging_nodes WHERE platform=? AND chat_id=? AND root_id=?",
identity,
)
}
for node_id in previous.keys() - normalized.nodes.keys():
connection.execute(
"DELETE FROM messaging_nodes WHERE platform=? AND chat_id=? AND node_id=?",
(*scope, node_id),
)
for node_id in normalized.nodes:
node = normalized.nodes[node_id]
before = previous.get(node_id)
if before is not None and all(
before[field] == node.get(field) for field in _FIELDS
):
continue
connection.execute(
"INSERT INTO messaging_nodes (platform,chat_id,root_id,node_id,status_message_id,state,parent_id,parent_reference_id,session_id) "
"VALUES (?,?,?,?,?,?,?,?,?) ON CONFLICT(platform,chat_id,node_id) DO UPDATE SET "
"root_id=excluded.root_id,status_message_id=excluded.status_message_id,state=excluded.state,"
"parent_id=excluded.parent_id,parent_reference_id=excluded.parent_reference_id,session_id=excluded.session_id",
(*identity, *(node.get(field) for field in _FIELDS)),
)
class SQLiteMessagingStore:
def __init__(
self, database: SQLiteDatabase, *, managed_message_cap: int | None = None
) -> None:
self.database = database
self._cap = managed_message_cap
async def load_conversation_snapshot(self) -> ConversationSnapshot:
def load(connection: sqlite3.Connection) -> ConversationSnapshot:
trees: dict[TreeIdentity, TreeSnapshot] = {}
for row in connection.execute(
"SELECT * FROM messaging_nodes ORDER BY rowid"
):
scope = MessageScope(platform=row["platform"], chat_id=row["chat_id"])
identity = TreeIdentity(scope=scope, root_id=row["root_id"])
snapshot = trees.setdefault(
identity, TreeSnapshot(scope, identity.root_id, {})
)
snapshot.nodes[row["node_id"]] = {
field: row[field] for field in _FIELDS
}
return ConversationSnapshot(trees)
return await self.database.run(load, write=False)
async def commit_trees(
self,
snapshots: tuple[TreeSnapshot, ...],
*,
removed: tuple[TreeIdentity, ...] = (),
clear_scope: MessageScope | None = None,
) -> None:
def commit(connection: sqlite3.Connection) -> None:
if clear_scope is not None:
scope = (clear_scope.platform, clear_scope.chat_id)
connection.execute(
"DELETE FROM messaging_trees WHERE platform=? AND chat_id=?", scope
)
connection.execute(
"DELETE FROM messaging_managed_messages WHERE platform=? AND chat_id=?",
scope,
)
for identity in removed:
connection.execute(
"DELETE FROM messaging_trees WHERE platform=? AND chat_id=? AND root_id=?",
(identity.scope.platform, identity.scope.chat_id, identity.root_id),
)
for snapshot in snapshots:
write_tree(connection, snapshot)
try:
await self.database.run(commit)
except sqlite3.Error as exc:
raise MessagingStorageError(
"Messaging history could not be saved."
) from exc
def _trim(
self, connection: sqlite3.Connection, platform: str, chat_id: str
) -> None:
if self._cap is not None and self._cap > 0:
connection.execute(
"DELETE FROM messaging_managed_messages WHERE sequence IN ("
"SELECT sequence FROM messaging_managed_messages WHERE platform=? AND chat_id=? "
"ORDER BY sequence DESC LIMIT -1 OFFSET ?)",
(platform, chat_id, self._cap),
)
async def trim(self) -> None:
def trim(connection: sqlite3.Connection) -> None:
for row in connection.execute(
"SELECT DISTINCT platform,chat_id FROM messaging_managed_messages"
).fetchall():
self._trim(connection, row[0], row[1])
await self.database.run(trim)
async def record_message_id(
self, platform: str, chat_id: str, message_id: str, direction: str, kind: str
) -> None:
def record(connection: sqlite3.Connection) -> None:
connection.execute(
"INSERT INTO messaging_managed_messages(platform,chat_id,message_id,ts,direction,kind) "
"VALUES (?,?,?,?,?,?) ON CONFLICT(platform,chat_id,message_id) DO NOTHING",
(
str(platform),
str(chat_id),
str(message_id),
datetime.now(UTC).isoformat(),
direction,
kind,
),
)
self._trim(connection, platform, chat_id)
await self.database.run(record)
async def get_tracked_message_ids_for_chat(
self, platform: str, chat_id: str
) -> list[str]:
return await self.database.run(
lambda connection: [
row[0]
for row in connection.execute(
"SELECT message_id FROM messaging_managed_messages WHERE platform=? AND chat_id=? ORDER BY sequence",
(platform, chat_id),
)
],
write=False,
)
async def forget_tracked_message_ids(
self, platform: str, chat_id: str, message_ids: set[str]
) -> None:
def forget(connection: sqlite3.Connection) -> None:
connection.executemany(
"DELETE FROM messaging_managed_messages WHERE platform=? AND chat_id=? AND message_id=?",
[(platform, chat_id, item) for item in message_ids],
)
await self.database.run(forget)
|