feat: live agent answers (SSE streaming) + «Чат» tab on phones
CI / ruff · pytest (pull_request) Successful in 2m43s
CI / ruff · pytest (pull_request) Successful in 2m43s
- POST /dialog/{token}/events/stream: Server-Sent Events with the agent's steps («Смотрю вашу карту ЕМИАС…»,
«Ищу в клинических рекомендациях…»), the answer as the model writes it (OpenRouter stream=true), then the
final replies (same as /events, incl. the forbidden-output filter and sources). X-Accel-Buffering: no.
- Web: free-text messages use the stream — a live bubble with steps, typing dots and a cursor; falls back to
the plain request if the stream breaks.
- Phones: the bottom tab bar gets «Чат» (it had no way back to the chat).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -8,9 +8,14 @@ disclosed — the dashboard polls every 2–3 s), `ack_alert` (in `repo.ack_aler
|
||||
`app/api/fixtures.py` is kept for tests and as the demo cabinet reference (doctor id).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import secrets
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.api import demo as console
|
||||
@@ -42,10 +47,12 @@ from app.db.identity import Patient
|
||||
from app.db.session import get_clinical_session, get_identity_session
|
||||
from app.dialog.contracts import Event, get_engine
|
||||
from app.integrations.base import integrations
|
||||
from app.llm.agent import stream as agent_stream
|
||||
from app.scheduler import jobs as scheduler
|
||||
from app.services.anonymous_care import display_name as anonymous_name
|
||||
from app.services.monitoring import refresh_status
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/api/v1")
|
||||
router.include_router(max_notifications.router)
|
||||
router.include_router(questionnaire_router)
|
||||
@@ -219,6 +226,49 @@ async def dialog_event(token: str, body: DialogIn) -> DialogOut:
|
||||
return DialogOut(replies=await get_engine().handle(token, body))
|
||||
|
||||
|
||||
@router.post("/dialog/{token}/events/stream", tags=["dialog"])
|
||||
async def dialog_event_stream(token: str, body: DialogIn) -> StreamingResponse:
|
||||
"""Same as /events, as Server-Sent Events: `status` (agent steps), `delta` (answer as it is written),
|
||||
`reset`, then `done` with the final replies (identical to /events). `error` if the engine failed."""
|
||||
await _require_patient(token)
|
||||
queue: asyncio.Queue[tuple[str, Any]] = asyncio.Queue()
|
||||
marker = agent_stream.STREAM.set(queue) # copied into the task's context below
|
||||
try:
|
||||
|
||||
async def run() -> None:
|
||||
try:
|
||||
replies = await get_engine().handle(token, body)
|
||||
queue.put_nowait(("done", [r.model_dump() for r in replies]))
|
||||
except Exception: # noqa: BLE001 — reported to the client, which falls back to /events
|
||||
log.exception("dialog stream failed")
|
||||
queue.put_nowait(("error", None))
|
||||
|
||||
task = asyncio.create_task(run())
|
||||
finally:
|
||||
agent_stream.STREAM.reset(marker)
|
||||
|
||||
async def events():
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
kind, payload = await asyncio.wait_for(queue.get(), timeout=15)
|
||||
except TimeoutError:
|
||||
yield ": keepalive\n\n"
|
||||
continue
|
||||
yield f"event: {kind}\ndata: {json.dumps(payload, ensure_ascii=False)}\n\n"
|
||||
if kind in ("done", "error"):
|
||||
return
|
||||
finally:
|
||||
if not task.done():
|
||||
await task
|
||||
|
||||
return StreamingResponse(
|
||||
events(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}, # nginx: do not buffer SSE
|
||||
)
|
||||
|
||||
|
||||
@router.get("/dialog/{token}/outbox", response_model=DialogOut, tags=["dialog"])
|
||||
async def dialog_outbox(token: str) -> DialogOut:
|
||||
"""System-initiated replies (reminders, check-ins) waiting for the web console."""
|
||||
|
||||
Reference in New Issue
Block a user