-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi_tool_session_store.py
More file actions
200 lines (159 loc) · 6.86 KB
/
Copy pathapi_tool_session_store.py
File metadata and controls
200 lines (159 loc) · 6.86 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
"""Redis-backed session store for the API Tool Calling agentic loop."""
from typing import Any, Optional
from fastapi import HTTPException, Request, status
from src.loki_logger import LokiLogger
from redis import WatchError
from models.session_models import APIToolSession
from src.utils.redis_client import get_redis_client
logger = LokiLogger(service_name="api-tool-session-store")
_SESSION_KEY_PREFIX = "session:"
_SESSION_TTL_SECONDS = 1800 # 30 minutes, sliding
_UPDATE_MAX_RETRIES = 3
def _key(chat_id: str) -> str:
return f"{_SESSION_KEY_PREFIX}{chat_id}"
_VALID_SESSION_FIELDS = frozenset(APIToolSession.model_fields)
class APIToolSessionStore:
"""CRUD store for API tool agentic-loop sessions backed by Redis.
All operations are async and safe to call from FastAPI handlers.
The TTL is reset (sliding expiry) on every save() and update().
"""
async def get(self, chat_id: str) -> Optional[APIToolSession]:
"""Retrieve a session by chat_id.
Returns:
The deserialized session, or None if not found / Redis unavailable.
"""
client = get_redis_client()
if client is None:
logger.warning(f"[SessionStore] Redis unavailable - get({chat_id}) skipped")
return None
try:
raw = await client.get(_key(chat_id))
if raw is None:
return None
return APIToolSession.model_validate_json(raw)
except Exception as exc:
logger.error(f"[SessionStore] get({chat_id}) failed: {exc}")
return None
async def save(self, session: APIToolSession) -> None:
"""Persist a session (full replace) and reset the TTL.
Args:
session: The session object to persist.
"""
client = get_redis_client()
if client is None:
logger.warning(
f"[SessionStore] Redis unavailable - save({session.chat_id}) skipped"
)
return
try:
await client.set(
_key(session.chat_id),
session.model_dump_json(),
ex=_SESSION_TTL_SECONDS,
)
logger.debug(f"[SessionStore] Session saved for chat_id={session.chat_id}")
except Exception as exc:
logger.error(f"[SessionStore] save({session.chat_id}) failed: {exc}")
async def update(self, chat_id: str, **fields: Any) -> Optional[APIToolSession]:
"""Atomically update a session using optimistic locking (WATCH/MULTI/EXEC).
Uses Redis WATCH to detect concurrent modifications. If a conflicting
write is detected, the operation retries up to ``_UPDATE_MAX_RETRIES`` times.
Args:
chat_id: The conversation to update.
**fields: Field names and new values to merge into the session.
Returns:
The updated session, or None if the session does not exist or Redis is unavailable.
Raises:
ValueError: If any of the provided field names are not valid
``APIToolSession`` attributes.
"""
unknown = set(fields) - _VALID_SESSION_FIELDS
if unknown:
raise ValueError(f"Unknown session fields: {unknown}")
client = get_redis_client()
if client is None:
logger.warning(
f"[SessionStore] Redis unavailable - update({chat_id}) skipped"
)
return None
key = _key(chat_id)
for attempt in range(_UPDATE_MAX_RETRIES):
try:
async with client.pipeline(transaction=True) as pipe:
await pipe.watch(key)
raw = await pipe.get(key)
if raw is None:
await pipe.unwatch()
logger.warning(
f"[SessionStore] update({chat_id}) - session not found, skipping"
)
return None
session = APIToolSession.model_validate_json(raw)
updated = session.model_copy(update=fields)
pipe.multi()
pipe.set(key, updated.model_dump_json(), ex=_SESSION_TTL_SECONDS)
await pipe.execute()
logger.debug(
f"[SessionStore] Session updated for chat_id={chat_id}"
)
return updated
except WatchError:
logger.debug(
f"[SessionStore] update({chat_id}) - concurrent modification detected, "
f"retrying (attempt {attempt + 1}/{_UPDATE_MAX_RETRIES})"
)
continue
except Exception as exc:
logger.error(f"[SessionStore] update({chat_id}) failed: {exc}")
return None
logger.error(
f"[SessionStore] update({chat_id}) - exhausted {_UPDATE_MAX_RETRIES} retries due to concurrent writes"
)
return None
async def delete(self, chat_id: str) -> None:
"""Remove a session from Redis.
Args:
chat_id: The conversation whose session should be deleted.
"""
client = get_redis_client()
if client is None:
logger.warning(
f"[SessionStore] Redis unavailable - delete({chat_id}) skipped"
)
return
try:
await client.delete(_key(chat_id))
logger.debug(f"[SessionStore] Session deleted for chat_id={chat_id}")
except Exception as exc:
logger.error(f"[SessionStore] delete({chat_id}) failed: {exc}")
async def exists(self, chat_id: str) -> bool:
"""Check whether a session exists for the given chat_id.
Returns:
True if the session key exists in Redis, False otherwise.
"""
client = get_redis_client()
if client is None:
return False
try:
return bool(await client.exists(_key(chat_id)))
except Exception as exc:
logger.error(f"[SessionStore] exists({chat_id}) failed: {exc}")
return False
def require_session_store(request: Request) -> APIToolSessionStore:
"""FastAPI dependency that guarantees a live session store.
Use as a dependency on any endpoint that requires multi-turn session
state. Returns HTTP 503 immediately when Redis is unavailable instead
of letting the request silently degrade.
"""
store: Optional[APIToolSessionStore] = getattr(
request.app.state, "session_store", None
)
if store is None:
logger.error(
f"[SessionStore] Session store unavailable — returning 503 for {request.url.path}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Session store is currently unavailable. Please try again later.",
)
return store