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."""
|
||||
|
||||
@@ -21,6 +21,7 @@ from pydantic import BaseModel
|
||||
|
||||
from app.config import Settings, get_settings
|
||||
from app.core.types import ScrubbedText
|
||||
from app.llm.agent import stream
|
||||
from app.llm.agent.prompts import SYSTEM_PROMPT
|
||||
from app.llm.agent.providers import (
|
||||
GigaChatToolProvider,
|
||||
@@ -127,6 +128,7 @@ async def answer_chat(
|
||||
return None
|
||||
if is_small_talk(question):
|
||||
return AgentAnswer(text=SMALL_TALK_REPLY, sources=[], tools=[], provider="small_talk")
|
||||
stream.emit("status", "Думаю над вопросом…")
|
||||
ctx = ToolContext(token=token)
|
||||
messages: list[dict[str, Any]] = [
|
||||
{"role": "system", "content": SYSTEM_PROMPT + await _patient_brief(ctx)},
|
||||
@@ -159,6 +161,7 @@ async def answer_chat(
|
||||
}
|
||||
)
|
||||
for c in turn.tool_calls[:MAX_TOOL_CALLS_PER_TURN]:
|
||||
stream.emit("status", stream.TOOL_STATUS.get(c.name, "Проверяю данные…"))
|
||||
result = await run_tool(ctx, c.name, c.arguments)
|
||||
messages.append({"role": "tool", "tool_call_id": c.id, "name": c.name, "content": result})
|
||||
log.warning("chat agent: step limit reached tools=%s", ",".join(ctx.used))
|
||||
|
||||
@@ -16,6 +16,7 @@ import httpx
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.config import Settings
|
||||
from app.llm.agent import stream
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -53,6 +54,8 @@ class OpenRouterProvider:
|
||||
self._client = client
|
||||
|
||||
async def chat(self, messages: list[dict[str, Any]], tools: list[dict[str, Any]]) -> AssistantTurn:
|
||||
if stream.active():
|
||||
return await self._chat_stream(messages, tools)
|
||||
body: dict[str, Any] = {"model": self._model, "messages": messages, "tools": tools, "temperature": 0.1}
|
||||
body["max_tokens"] = 900
|
||||
if not any(m["role"] == "tool" for m in messages):
|
||||
@@ -174,6 +177,78 @@ class GigaChatToolProvider:
|
||||
return AssistantTurn(content=msg.content or None, tool_calls=calls)
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------------------------ streaming
|
||||
|
||||
|
||||
async def _iter_sse(resp: httpx.Response):
|
||||
async for line in resp.aiter_lines():
|
||||
if not line.startswith("data:"):
|
||||
continue # comments («: OPENROUTER PROCESSING») and blank lines
|
||||
data = line[5:].strip()
|
||||
if data == "[DONE]":
|
||||
return
|
||||
try:
|
||||
yield json.loads(data)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
|
||||
async def _openrouter_stream(self: "OpenRouterProvider", messages: list[dict[str, Any]], tools: list[dict[str, Any]]):
|
||||
"""Same request with `stream: true`: content deltas go to the live channel, tool calls are assembled."""
|
||||
body: dict[str, Any] = {
|
||||
"model": self._model,
|
||||
"messages": messages,
|
||||
"tools": tools,
|
||||
"temperature": 0.1,
|
||||
"max_tokens": 900,
|
||||
"stream": True,
|
||||
}
|
||||
if not any(m["role"] == "tool" for m in messages):
|
||||
body["tool_choice"] = "required"
|
||||
headers = {"Authorization": f"Bearer {self._key}", "X-Title": "PTSRaiAgent prototype (synthetic data)"}
|
||||
content: list[str] = []
|
||||
calls: dict[int, dict[str, Any]] = {}
|
||||
try:
|
||||
client = self._client or httpx.AsyncClient(timeout=self._timeout, proxy=self._proxy)
|
||||
try:
|
||||
async with client.stream("POST", self._url, json=body, headers=headers, timeout=self._timeout) as resp:
|
||||
if resp.status_code != 200:
|
||||
await resp.aread()
|
||||
raise ProviderError(f"http {resp.status_code}")
|
||||
async for chunk in _iter_sse(resp):
|
||||
choice = (chunk.get("choices") or [{}])[0]
|
||||
delta = choice.get("delta") or {}
|
||||
if delta.get("content"):
|
||||
content.append(delta["content"])
|
||||
stream.emit("delta", delta["content"])
|
||||
for tc in delta.get("tool_calls") or []:
|
||||
slot = calls.setdefault(tc.get("index", 0), {"id": "", "name": "", "args": ""})
|
||||
slot["id"] = tc.get("id") or slot["id"]
|
||||
fn = tc.get("function") or {}
|
||||
slot["name"] += fn.get("name") or ""
|
||||
slot["args"] += fn.get("arguments") or ""
|
||||
finally:
|
||||
if self._client is None:
|
||||
await client.aclose()
|
||||
except httpx.HTTPError as exc:
|
||||
raise ProviderError(type(exc).__name__) from exc
|
||||
out = []
|
||||
for i in sorted(calls):
|
||||
c = calls[i]
|
||||
try:
|
||||
args = json.loads(c["args"] or "{}")
|
||||
except json.JSONDecodeError:
|
||||
args = {}
|
||||
out.append(ToolCall(id=c["id"] or c["name"] or f"call{i}", name=c["name"], arguments=args))
|
||||
if out and content:
|
||||
stream.emit("reset", None) # a preface before tool calls is not the answer
|
||||
log.info("openrouter stream ok calls=%d chars=%d", len(out), sum(map(len, content)))
|
||||
return AssistantTurn(content="".join(content) or None, tool_calls=out)
|
||||
|
||||
|
||||
OpenRouterProvider._chat_stream = _openrouter_stream # type: ignore[attr-defined]
|
||||
|
||||
|
||||
# ------------------------------------------------------------------------------------------------ offline mock
|
||||
|
||||
_DRUG_WORDS = re.compile(
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Live progress of the chat agent for streaming channels (web SSE).
|
||||
|
||||
The SSE endpoint sets a queue in the context; the agent pushes events while it works:
|
||||
status — a tool step in plain Russian («Смотрю вашу карту ЕМИАС…»)
|
||||
delta — a chunk of the answer as the model writes it
|
||||
reset — the streamed text so far was a preface before tool calls: clear it
|
||||
Channels without a queue (Telegram, tests) are unaffected. Deltas are the raw model text: the final reply
|
||||
(forbidden-output filter, sources footer) always comes in the closing `done` event and replaces it.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from contextvars import ContextVar
|
||||
from typing import Any
|
||||
|
||||
STREAM: ContextVar[asyncio.Queue[tuple[str, Any]] | None] = ContextVar("agent_stream", default=None)
|
||||
|
||||
TOOL_STATUS = {
|
||||
"get_patient_record": "Смотрю вашу карту ЕМИАС…",
|
||||
"check_drug_for_patient": "Проверяю препарат по вашей карте: болезни, непереносимости, другие лекарства…",
|
||||
"get_drug_info": "Открываю инструкцию к препарату (ГРЛС)…",
|
||||
"search_guidelines": "Ищу в клинических рекомендациях Минздрава…",
|
||||
}
|
||||
|
||||
|
||||
def active() -> bool:
|
||||
return STREAM.get() is not None
|
||||
|
||||
|
||||
def emit(kind: str, payload: Any) -> None:
|
||||
q = STREAM.get()
|
||||
if q is not None:
|
||||
q.put_nowait((kind, payload))
|
||||
Reference in New Issue
Block a user