|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import json |
| 5 | +from collections.abc import AsyncIterator |
| 6 | +from contextlib import asynccontextmanager |
| 7 | +from pathlib import Path |
| 8 | +from typing import cast |
| 9 | + |
| 10 | +import aiosqlite |
| 11 | + |
| 12 | +from ...items import TResponseInputItem |
| 13 | +from ...memory import SessionABC |
| 14 | + |
| 15 | + |
| 16 | +class AsyncSQLiteSession(SessionABC): |
| 17 | + """Async SQLite-based implementation of session storage. |
| 18 | +
|
| 19 | + This implementation stores conversation history in a SQLite database. |
| 20 | + By default, uses an in-memory database that is lost when the process ends. |
| 21 | + For persistent storage, provide a file path. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__( |
| 25 | + self, |
| 26 | + session_id: str, |
| 27 | + db_path: str | Path = ":memory:", |
| 28 | + sessions_table: str = "agent_sessions", |
| 29 | + messages_table: str = "agent_messages", |
| 30 | + ): |
| 31 | + """Initialize the async SQLite session. |
| 32 | +
|
| 33 | + Args: |
| 34 | + session_id: Unique identifier for the conversation session |
| 35 | + db_path: Path to the SQLite database file. Defaults to ':memory:' (in-memory database) |
| 36 | + sessions_table: Name of the table to store session metadata. Defaults to |
| 37 | + 'agent_sessions' |
| 38 | + messages_table: Name of the table to store message data. Defaults to 'agent_messages' |
| 39 | + """ |
| 40 | + self.session_id = session_id |
| 41 | + self.db_path = db_path |
| 42 | + self.sessions_table = sessions_table |
| 43 | + self.messages_table = messages_table |
| 44 | + self._connection: aiosqlite.Connection | None = None |
| 45 | + self._lock = asyncio.Lock() |
| 46 | + self._init_lock = asyncio.Lock() |
| 47 | + |
| 48 | + async def _init_db_for_connection(self, conn: aiosqlite.Connection) -> None: |
| 49 | + """Initialize the database schema for a specific connection.""" |
| 50 | + await conn.execute( |
| 51 | + f""" |
| 52 | + CREATE TABLE IF NOT EXISTS {self.sessions_table} ( |
| 53 | + session_id TEXT PRIMARY KEY, |
| 54 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 55 | + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 56 | + ) |
| 57 | + """ |
| 58 | + ) |
| 59 | + |
| 60 | + await conn.execute( |
| 61 | + f""" |
| 62 | + CREATE TABLE IF NOT EXISTS {self.messages_table} ( |
| 63 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 64 | + session_id TEXT NOT NULL, |
| 65 | + message_data TEXT NOT NULL, |
| 66 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 67 | + FOREIGN KEY (session_id) REFERENCES {self.sessions_table} (session_id) |
| 68 | + ON DELETE CASCADE |
| 69 | + ) |
| 70 | + """ |
| 71 | + ) |
| 72 | + |
| 73 | + await conn.execute( |
| 74 | + f""" |
| 75 | + CREATE INDEX IF NOT EXISTS idx_{self.messages_table}_session_id |
| 76 | + ON {self.messages_table} (session_id, id) |
| 77 | + """ |
| 78 | + ) |
| 79 | + |
| 80 | + await conn.commit() |
| 81 | + |
| 82 | + async def _get_connection(self) -> aiosqlite.Connection: |
| 83 | + """Get or create a database connection.""" |
| 84 | + if self._connection is not None: |
| 85 | + return self._connection |
| 86 | + |
| 87 | + async with self._init_lock: |
| 88 | + if self._connection is None: |
| 89 | + self._connection = await aiosqlite.connect(str(self.db_path)) |
| 90 | + await self._connection.execute("PRAGMA journal_mode=WAL") |
| 91 | + await self._init_db_for_connection(self._connection) |
| 92 | + |
| 93 | + return self._connection |
| 94 | + |
| 95 | + @asynccontextmanager |
| 96 | + async def _locked_connection(self) -> AsyncIterator[aiosqlite.Connection]: |
| 97 | + """Provide a connection under the session lock.""" |
| 98 | + async with self._lock: |
| 99 | + conn = await self._get_connection() |
| 100 | + yield conn |
| 101 | + |
| 102 | + async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]: |
| 103 | + """Retrieve the conversation history for this session. |
| 104 | +
|
| 105 | + Args: |
| 106 | + limit: Maximum number of items to retrieve. If None, retrieves all items. |
| 107 | + When specified, returns the latest N items in chronological order. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + List of input items representing the conversation history |
| 111 | + """ |
| 112 | + |
| 113 | + async with self._locked_connection() as conn: |
| 114 | + if limit is None: |
| 115 | + cursor = await conn.execute( |
| 116 | + f""" |
| 117 | + SELECT message_data FROM {self.messages_table} |
| 118 | + WHERE session_id = ? |
| 119 | + ORDER BY id ASC |
| 120 | + """, |
| 121 | + (self.session_id,), |
| 122 | + ) |
| 123 | + else: |
| 124 | + cursor = await conn.execute( |
| 125 | + f""" |
| 126 | + SELECT message_data FROM {self.messages_table} |
| 127 | + WHERE session_id = ? |
| 128 | + ORDER BY id DESC |
| 129 | + LIMIT ? |
| 130 | + """, |
| 131 | + (self.session_id, limit), |
| 132 | + ) |
| 133 | + |
| 134 | + rows = list(await cursor.fetchall()) |
| 135 | + await cursor.close() |
| 136 | + |
| 137 | + if limit is not None: |
| 138 | + rows = rows[::-1] |
| 139 | + |
| 140 | + items: list[TResponseInputItem] = [] |
| 141 | + for (message_data,) in rows: |
| 142 | + try: |
| 143 | + item = json.loads(message_data) |
| 144 | + items.append(item) |
| 145 | + except json.JSONDecodeError: |
| 146 | + continue |
| 147 | + |
| 148 | + return items |
| 149 | + |
| 150 | + async def add_items(self, items: list[TResponseInputItem]) -> None: |
| 151 | + """Add new items to the conversation history. |
| 152 | +
|
| 153 | + Args: |
| 154 | + items: List of input items to add to the history |
| 155 | + """ |
| 156 | + if not items: |
| 157 | + return |
| 158 | + |
| 159 | + async with self._locked_connection() as conn: |
| 160 | + await conn.execute( |
| 161 | + f""" |
| 162 | + INSERT OR IGNORE INTO {self.sessions_table} (session_id) VALUES (?) |
| 163 | + """, |
| 164 | + (self.session_id,), |
| 165 | + ) |
| 166 | + |
| 167 | + message_data = [(self.session_id, json.dumps(item)) for item in items] |
| 168 | + await conn.executemany( |
| 169 | + f""" |
| 170 | + INSERT INTO {self.messages_table} (session_id, message_data) VALUES (?, ?) |
| 171 | + """, |
| 172 | + message_data, |
| 173 | + ) |
| 174 | + |
| 175 | + await conn.execute( |
| 176 | + f""" |
| 177 | + UPDATE {self.sessions_table} |
| 178 | + SET updated_at = CURRENT_TIMESTAMP |
| 179 | + WHERE session_id = ? |
| 180 | + """, |
| 181 | + (self.session_id,), |
| 182 | + ) |
| 183 | + |
| 184 | + await conn.commit() |
| 185 | + |
| 186 | + async def pop_item(self) -> TResponseInputItem | None: |
| 187 | + """Remove and return the most recent item from the session. |
| 188 | +
|
| 189 | + Returns: |
| 190 | + The most recent item if it exists, None if the session is empty |
| 191 | + """ |
| 192 | + async with self._locked_connection() as conn: |
| 193 | + cursor = await conn.execute( |
| 194 | + f""" |
| 195 | + DELETE FROM {self.messages_table} |
| 196 | + WHERE id = ( |
| 197 | + SELECT id FROM {self.messages_table} |
| 198 | + WHERE session_id = ? |
| 199 | + ORDER BY id DESC |
| 200 | + LIMIT 1 |
| 201 | + ) |
| 202 | + RETURNING message_data |
| 203 | + """, |
| 204 | + (self.session_id,), |
| 205 | + ) |
| 206 | + |
| 207 | + result = await cursor.fetchone() |
| 208 | + await cursor.close() |
| 209 | + await conn.commit() |
| 210 | + |
| 211 | + if result: |
| 212 | + message_data = result[0] |
| 213 | + try: |
| 214 | + return cast(TResponseInputItem, json.loads(message_data)) |
| 215 | + except json.JSONDecodeError: |
| 216 | + return None |
| 217 | + |
| 218 | + return None |
| 219 | + |
| 220 | + async def clear_session(self) -> None: |
| 221 | + """Clear all items for this session.""" |
| 222 | + async with self._locked_connection() as conn: |
| 223 | + await conn.execute( |
| 224 | + f"DELETE FROM {self.messages_table} WHERE session_id = ?", |
| 225 | + (self.session_id,), |
| 226 | + ) |
| 227 | + await conn.execute( |
| 228 | + f"DELETE FROM {self.sessions_table} WHERE session_id = ?", |
| 229 | + (self.session_id,), |
| 230 | + ) |
| 231 | + await conn.commit() |
| 232 | + |
| 233 | + async def close(self) -> None: |
| 234 | + """Close the database connection.""" |
| 235 | + if self._connection is None: |
| 236 | + return |
| 237 | + async with self._lock: |
| 238 | + await self._connection.close() |
| 239 | + self._connection = None |
0 commit comments