Stateless mode: in-memory sessions, delete per request; README: document stateless behavior

This commit is contained in:
2025-08-09 16:18:16 +05:30
parent 57cbf155ea
commit 92ea439fc5
2 changed files with 26 additions and 9 deletions

View File

@@ -31,4 +31,9 @@ curl -s -X POST http://localhost:8000/process_data \
``` ```
Returns `{ "emails": "..." }`. Returns `{ "emails": "..." }`.
### Stateless mode
- Sessions are ephemeral and in-memory only; no SQLite persistence.
- Each request creates a new session and it is deleted after completion.
- Web UI works, but conversation history resets on refresh/restart.

30
main.py
View File

@@ -37,7 +37,8 @@ if os.getenv("GEMINI_API_KEY") and not os.getenv("GOOGLE_GENAI_API_KEY"):
os.environ["GOOGLE_GENAI_API_KEY"] = os.getenv("GEMINI_API_KEY") os.environ["GOOGLE_GENAI_API_KEY"] = os.getenv("GEMINI_API_KEY")
AGENTS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "agents") AGENTS_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "agents")
SESSION_SERVICE_URI = "sqlite:///./sessions.db" # Stateless: use in-memory session service (no SQLite persistence)
SESSION_SERVICE_URI = None
ALLOWED_ORIGINS = ["*"] ALLOWED_ORIGINS = ["*"]
SERVE_WEB_INTERFACE = True SERVE_WEB_INTERFACE = True
@@ -62,14 +63,25 @@ def process_data(data: dict = Body(...)):
) )
final_text = None final_text = None
for event in _api_runner.run(user_id="api-user", session_id=session.id, new_message=content): try:
if getattr(event, "content", None): for event in _api_runner.run(user_id="api-user", session_id=session.id, new_message=content):
for part in event.content.parts or []: if getattr(event, "content", None):
text = getattr(part, "text", None) for part in event.content.parts or []:
if text: text = getattr(part, "text", None)
final_text = text if text:
final_text = text
return {"emails": final_text or ""} return {"emails": final_text or ""}
finally:
# Ephemeral: delete session after each request
try:
_api_runner.session_service.delete_session_sync(
app_name=_api_runner.app_name,
user_id="api-user",
session_id=session.id,
)
except Exception:
# Best-effort cleanup
pass
if __name__ == "__main__": if __name__ == "__main__":