fix: auto-create session if not found in /run endpoint
This commit is contained in:
parent
1122af66d0
commit
7e1af38474
|
|
@ -1,7 +1,5 @@
|
|||
# agents/core-logic/app.py
|
||||
# OSVauco-NMTMD-GCOS — FastAPI HTTP entrypoint for Cloud Run
|
||||
# Wraps the ADK root_agent with a /run endpoint and a /health check.
|
||||
# Usage: uvicorn app:app --host 0.0.0.0 --port 8080
|
||||
|
||||
import os
|
||||
import logging
|
||||
|
|
@ -14,7 +12,6 @@ from pydantic import BaseModel
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# ── ADK imports ───────────────────────────────────────────────
|
||||
try:
|
||||
from google.adk.runners import Runner
|
||||
from google.adk.sessions import InMemorySessionService
|
||||
|
|
@ -24,13 +21,10 @@ except ImportError as e:
|
|||
logger.error(f"Failed to import ADK dependencies: {e}")
|
||||
raise
|
||||
|
||||
# ── Session service (in-memory for stateless Cloud Run) ───────────────
|
||||
session_service = InMemorySessionService()
|
||||
|
||||
APP_NAME = os.environ.get("CLOUD_RUN_SERVICE", "gcp-orchestrator")
|
||||
|
||||
|
||||
# ── Lifespan ───────────────────────────────────────────────────
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info(f"OSVauco agent '{APP_NAME}' starting up")
|
||||
|
|
@ -38,7 +32,6 @@ async def lifespan(app: FastAPI):
|
|||
logger.info(f"OSVauco agent '{APP_NAME}' shutting down")
|
||||
|
||||
|
||||
# ── App ────────────────────────────────────────────────────────
|
||||
app = FastAPI(
|
||||
title="OSVauco GCP Agent",
|
||||
description="ADK-based multi-agent orchestrator on Cloud Run",
|
||||
|
|
@ -47,7 +40,6 @@ app = FastAPI(
|
|||
)
|
||||
|
||||
|
||||
# ── Models ─────────────────────────────────────────────────────
|
||||
class RunRequest(BaseModel):
|
||||
user_id: str
|
||||
session_id: str
|
||||
|
|
@ -60,34 +52,27 @@ class RunResponse(BaseModel):
|
|||
response: str
|
||||
|
||||
|
||||
# ── Routes ─────────────────────────────────────────────────────
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
"""Cloud Run health check endpoint."""
|
||||
return JSONResponse({"status": "ok", "service": APP_NAME})
|
||||
|
||||
|
||||
@app.post("/run", response_model=RunResponse)
|
||||
async def run(req: RunRequest):
|
||||
"""
|
||||
Run the OSVauco ADK agent for a single turn.
|
||||
Creates (or reuses) a session, sends the user message,
|
||||
and returns the final agent response.
|
||||
"""
|
||||
try:
|
||||
# Create or reuse session
|
||||
try:
|
||||
session = await session_service.get_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=req.user_id,
|
||||
session_id=req.session_id,
|
||||
)
|
||||
except Exception:
|
||||
session = await session_service.create_session(
|
||||
# Always ensure session exists — create if missing
|
||||
session = session_service.get_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=req.user_id,
|
||||
session_id=req.session_id,
|
||||
)
|
||||
if session is None:
|
||||
session = session_service.create_session(
|
||||
app_name=APP_NAME,
|
||||
user_id=req.user_id,
|
||||
session_id=req.session_id,
|
||||
)
|
||||
logger.info(f"Created new session: {req.session_id}")
|
||||
|
||||
runner = Runner(
|
||||
agent=root_agent,
|
||||
|
|
@ -112,7 +97,7 @@ async def run(req: RunRequest):
|
|||
final_response += part.text
|
||||
|
||||
logger.info(
|
||||
f"[{req.user_id}/{req.session_id}] Agent response length: {len(final_response)}"
|
||||
f"[{req.user_id}/{req.session_id}] Response length: {len(final_response)}"
|
||||
)
|
||||
return RunResponse(
|
||||
user_id=req.user_id,
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user