forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodb_session.py
More file actions
338 lines (271 loc) · 12 KB
/
mongodb_session.py
File metadata and controls
338 lines (271 loc) · 12 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
"""MongoDB-powered Session backend.
Requires ``pymongo>=4.13``, which ships the native async API
(``AsyncMongoClient``). Install it with::
pip install openai-agents[mongodb]
Usage::
from agents.extensions.memory import MongoDBSession
# Create from MongoDB URI
session = MongoDBSession.from_uri(
session_id="user-123",
uri="mongodb://localhost:27017",
database="agents",
)
# Or pass an existing AsyncMongoClient that your application already manages
from pymongo.asynchronous.mongo_client import AsyncMongoClient
client = AsyncMongoClient("mongodb://localhost:27017")
session = MongoDBSession(
session_id="user-123",
client=client,
database="agents",
)
await Runner.run(agent, "Hello", session=session)
"""
from __future__ import annotations
import asyncio
import json
from typing import Any
try:
from importlib.metadata import version as _get_version
_VERSION: str | None = _get_version("openai-agents")
except Exception:
_VERSION = None
try:
from pymongo.asynchronous.collection import AsyncCollection
from pymongo.asynchronous.mongo_client import AsyncMongoClient
from pymongo.driver_info import DriverInfo
except ImportError as e:
raise ImportError(
"MongoDBSession requires the 'pymongo' package (>=4.13). "
"Install it with: pip install openai-agents[mongodb]"
) from e
from ...items import TResponseInputItem
from ...memory.session import SessionABC
from ...memory.session_settings import SessionSettings, resolve_session_limit
# Identifies this library in the MongoDB handshake for server-side telemetry.
_DRIVER_INFO = DriverInfo(name="openai-agents", version=_VERSION)
class MongoDBSession(SessionABC):
"""MongoDB implementation of :pyclass:`agents.memory.session.Session`.
Conversation items are stored as individual documents in a ``messages``
collection. A lightweight ``sessions`` collection tracks metadata
(creation time, last-updated time) for each session.
Indexes are created once per ``(database, sessions_collection,
messages_collection)`` combination on the first call to any of the
session protocol methods. Subsequent calls skip the setup entirely.
"""
# Class-level registry so index creation only runs once per unique key.
_initialized_keys: set[tuple[str, str, str]] = set()
_init_locks: dict[tuple[str, str, str], asyncio.Lock] = {}
_init_locks_guard: asyncio.Lock = asyncio.Lock()
session_settings: SessionSettings | None = None
def __init__(
self,
session_id: str,
*,
client: AsyncMongoClient,
database: str = "agents",
sessions_collection: str = "agent_sessions",
messages_collection: str = "agent_messages",
session_settings: SessionSettings | None = None,
):
"""Initialize a new MongoDBSession.
Args:
session_id: Unique identifier for the conversation.
client: A pre-configured ``AsyncMongoClient`` instance.
database: Name of the MongoDB database to use.
Defaults to ``"agents"``.
sessions_collection: Name of the collection that stores session
metadata. Defaults to ``"agent_sessions"``.
messages_collection: Name of the collection that stores individual
conversation items. Defaults to ``"agent_messages"``.
session_settings: Optional session configuration. When ``None`` a
default :class:`~agents.memory.session_settings.SessionSettings`
is used (no item limit).
"""
self.session_id = session_id
self.session_settings = session_settings or SessionSettings()
self._client = client
self._owns_client = False
# Annotate an externally-supplied client with library metadata.
# append_metadata is available in PyMongo >=4.14; guard for older installs.
if hasattr(client, "append_metadata"):
client.append_metadata(_DRIVER_INFO)
db = client[database]
self._sessions: AsyncCollection = db[sessions_collection]
self._messages: AsyncCollection = db[messages_collection]
self._init_key = (database, sessions_collection, messages_collection)
# ------------------------------------------------------------------
# Convenience constructors
# ------------------------------------------------------------------
@classmethod
def from_uri(
cls,
session_id: str,
*,
uri: str,
database: str = "agents",
client_kwargs: dict[str, Any] | None = None,
session_settings: SessionSettings | None = None,
**kwargs: Any,
) -> MongoDBSession:
"""Create a session from a MongoDB URI string.
Args:
session_id: Conversation ID.
uri: MongoDB connection URI,
e.g. ``"mongodb://localhost:27017"`` or
``"mongodb+srv://user:pass@cluster.example.com"``.
database: Name of the MongoDB database to use.
client_kwargs: Additional keyword arguments forwarded to
:class:`pymongo.asynchronous.mongo_client.AsyncMongoClient`.
session_settings: Optional session configuration settings.
**kwargs: Additional keyword arguments forwarded to the main
constructor (e.g. ``sessions_collection``,
``messages_collection``).
Returns:
A :class:`MongoDBSession` connected to the specified MongoDB server.
"""
client_kwargs = client_kwargs or {}
client_kwargs.setdefault("driver", _DRIVER_INFO)
client: AsyncMongoClient = AsyncMongoClient(uri, **client_kwargs)
session = cls(
session_id,
client=client,
database=database,
session_settings=session_settings,
**kwargs,
)
session._owns_client = True
return session
# ------------------------------------------------------------------
# Index initialisation
# ------------------------------------------------------------------
async def _get_init_lock(self) -> asyncio.Lock:
"""Return (creating if necessary) the per-init-key asyncio Lock."""
async with self._init_locks_guard:
lock = self._init_locks.get(self._init_key)
if lock is None:
lock = asyncio.Lock()
self._init_locks[self._init_key] = lock
return lock
async def _ensure_indexes(self) -> None:
"""Create required indexes the first time this key is accessed."""
if self._init_key in self._initialized_keys:
return
lock = await self._get_init_lock()
async with lock:
# Double-checked locking: another coroutine may have finished first.
if self._init_key in self._initialized_keys:
return
# sessions: unique index on session_id.
await self._sessions.create_index("session_id", unique=True)
# messages: compound index for efficient per-session retrieval and sorting.
await self._messages.create_index([("session_id", 1), ("_id", 1)])
self._initialized_keys.add(self._init_key)
# ------------------------------------------------------------------
# Serialization helpers
# ------------------------------------------------------------------
async def _serialize_item(self, item: TResponseInputItem) -> str:
"""Serialize an item to a JSON string. Can be overridden by subclasses."""
return json.dumps(item, separators=(",", ":"))
async def _deserialize_item(self, raw: str) -> TResponseInputItem:
"""Deserialize a JSON string to an item. Can be overridden by subclasses."""
return json.loads(raw) # type: ignore[no-any-return]
# ------------------------------------------------------------------
# Session protocol implementation
# ------------------------------------------------------------------
async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
"""Retrieve the conversation history for this session.
Args:
limit: Maximum number of items to retrieve. When ``None``, the
effective limit is taken from :attr:`session_settings`.
If that is also ``None``, all items are returned.
The returned list is always in chronological (oldest-first)
order.
Returns:
List of input items representing the conversation history.
"""
await self._ensure_indexes()
session_limit = resolve_session_limit(limit, self.session_settings)
if session_limit is not None and session_limit <= 0:
return []
query = {"session_id": self.session_id}
if session_limit is None:
cursor = self._messages.find(query).sort("_id", 1)
docs = await cursor.to_list()
else:
# Fetch the latest N documents in reverse order, then reverse the
# list to restore chronological order.
cursor = self._messages.find(query).sort("_id", -1).limit(session_limit)
docs = await cursor.to_list()
docs.reverse()
items: list[TResponseInputItem] = []
for doc in docs:
try:
items.append(await self._deserialize_item(doc["message_data"]))
except (json.JSONDecodeError, KeyError):
# Skip corrupted or malformed documents.
continue
return items
async def add_items(self, items: list[TResponseInputItem]) -> None:
"""Add new items to the conversation history.
Args:
items: List of input items to append to the session.
"""
if not items:
return
await self._ensure_indexes()
# Upsert the session metadata document.
await self._sessions.update_one(
{"session_id": self.session_id},
{"$setOnInsert": {"session_id": self.session_id}},
upsert=True,
)
payload = [
{
"session_id": self.session_id,
"message_data": await self._serialize_item(item),
}
for item in items
]
await self._messages.insert_many(payload, ordered=True)
async def pop_item(self) -> TResponseInputItem | None:
"""Remove and return the most recent item from the session.
Returns:
The most recent item if it exists, ``None`` if the session is empty.
"""
await self._ensure_indexes()
doc = await self._messages.find_one_and_delete(
{"session_id": self.session_id},
sort=[("_id", -1)],
)
if doc is None:
return None
try:
return await self._deserialize_item(doc["message_data"])
except (json.JSONDecodeError, KeyError):
return None
async def clear_session(self) -> None:
"""Clear all items for this session."""
await self._ensure_indexes()
await self._messages.delete_many({"session_id": self.session_id})
await self._sessions.delete_one({"session_id": self.session_id})
# ------------------------------------------------------------------
# Lifecycle helpers
# ------------------------------------------------------------------
async def close(self) -> None:
"""Close the underlying MongoDB connection.
Only closes the client if this session owns it (i.e. it was created
via :meth:`from_uri`). If the client was injected externally the
caller is responsible for managing its lifecycle.
"""
if self._owns_client:
await self._client.aclose()
async def ping(self) -> bool:
"""Test MongoDB connectivity.
Returns:
``True`` if the server is reachable, ``False`` otherwise.
"""
try:
await self._client.admin.command("ping")
return True
except Exception:
return False