代码提交
This commit is contained in:
1 parent
d93cc62806
commit
ddf83f7728
25 files changed
+4086
-424
No files matched your search
@@ -82,8 +82,18 @@ def _user_ctx(request: Request) -> tuple[str, bool]:
|
||||
# ── REST endpoints (Phase 1) ──────────────────────────────────────────────
|
||||
|
||||
|
||||
def _check_stream_ready(request: Request) -> None:
|
||||
stream_ready = getattr(request.app.state, "maf_conversation_stream_ready", True)
|
||||
if not stream_ready:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=getattr(request.app.state, "maf_conversation_stream_status", "stream_not_ready"),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/conversations", response_model=ConversationResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_conversation(body: CreateConversationRequest, request: Request):
|
||||
_check_stream_ready(request)
|
||||
svc = _get_conversation_service(request)
|
||||
user_id, _ = _user_ctx(request)
|
||||
try:
|
||||
@@ -142,6 +152,7 @@ async def list_messages(
|
||||
|
||||
@router.post("/conversations/{conversation_id}/messages", response_model=SendMessageResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def send_message(conversation_id: str, body: SendMessageRequest, request: Request):
|
||||
_check_stream_ready(request)
|
||||
svc = _get_conversation_service(request)
|
||||
user_id, _ = _user_ctx(request)
|
||||
try:
|
||||
@@ -198,53 +209,44 @@ def _is_terminal(event: dict[str, Any]) -> bool:
|
||||
return event.get("type") in _TERMINAL_EVENT_TYPES
|
||||
|
||||
|
||||
@router.get("/conversations/{conversation_id}/stream")
|
||||
async def stream_conversation(
|
||||
conversation_id: str,
|
||||
def build_conversation_event_generator(
|
||||
*,
|
||||
service: MafConversationService,
|
||||
hub: ConversationEventHub,
|
||||
request: Request,
|
||||
after_seq: int = Query(default=0, ge=0),
|
||||
):
|
||||
"""SSE stream for one conversation (§4.2).
|
||||
conversation_id: str,
|
||||
after_seq: int = 0,
|
||||
) -> Any:
|
||||
"""Extracted async generator for SSE streaming — testable without HTTP.
|
||||
|
||||
Single fetch-reader connection per conversation. The ordering is:
|
||||
The ordering is:
|
||||
|
||||
1. **Open subscription** (register before any DB query — P0-1 fix).
|
||||
2. **Replay** from DB: ``list_events(after_seq)``.
|
||||
3. **Stabilise**: re-query while the hub generation advanced during replay.
|
||||
4. **Snapshot** marker.
|
||||
5. **Real-time**: ``wait_for_change`` → query DB → yield events.
|
||||
1. **Reconcile** any missed projections for the active run (P0-1 auto).
|
||||
2. **Open subscription** (register before any DB query — P0-1 fix).
|
||||
3. **Replay** from DB: ``list_events(after_seq)``.
|
||||
4. **Stabilise**: re-query while the hub generation advanced during replay.
|
||||
5. **Snapshot** marker.
|
||||
6. **Real-time**: ``wait_for_change`` → query DB → yield events.
|
||||
|
||||
On terminal events (``run.completed`` / ``run.error`` / ``run.cancelled``)
|
||||
the stream sends the event and closes (P0-2 fix).
|
||||
|
||||
Heartbeat every 15 s. Disconnect detection via ``request.is_disconnected()``.
|
||||
On terminal events the stream sends the event and closes (P0-2 fix).
|
||||
Heartbeat every ``hub.heartbeat_interval`` seconds.
|
||||
"""
|
||||
svc = _get_conversation_service(request)
|
||||
hub = _get_event_hub(request)
|
||||
user_id, is_admin = _user_ctx(request)
|
||||
store = service._store # noqa: SLF001
|
||||
|
||||
if hub is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="SSE event hub not configured",
|
||||
)
|
||||
async def _gen():
|
||||
try:
|
||||
active_run_id = await _get_active_run_id(store, conversation_id)
|
||||
if active_run_id:
|
||||
try:
|
||||
await service.reconcile_conversation_events(run_id=active_run_id)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("SSE connect reconcile failed conv=%s", conversation_id, exc_info=True)
|
||||
except Exception: # noqa: BLE001
|
||||
logger.warning("SSE connect reconcile lookup failed conv=%s", conversation_id, exc_info=True)
|
||||
|
||||
# Ownership check before opening the stream.
|
||||
try:
|
||||
await svc.get_conversation(
|
||||
conversation_id=conversation_id, user_id=user_id, is_admin=is_admin,
|
||||
)
|
||||
except ConversationNotFoundError:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Conversation not found")
|
||||
|
||||
store = svc._store # noqa: SLF001 — router needs raw event queries for streaming
|
||||
|
||||
async def event_generator():
|
||||
# ── P0-1: Open subscription BEFORE replay ──
|
||||
sub = hub.open_subscription(conversation_id)
|
||||
last_seq = after_seq
|
||||
try:
|
||||
# ── Phase 1: Replay from DB ──
|
||||
try:
|
||||
replay_events = await store.conversation_events.list_events(
|
||||
conversation_id, after_seq=last_seq,
|
||||
@@ -254,7 +256,6 @@ async def stream_conversation(
|
||||
return
|
||||
yield _format_sse_event(evt)
|
||||
last_seq = max(last_seq, int(evt.get("seq", last_seq)))
|
||||
# P0-2: stop if replay already hit a terminal event.
|
||||
if _is_terminal(evt):
|
||||
return
|
||||
except Exception:
|
||||
@@ -269,7 +270,6 @@ async def stream_conversation(
|
||||
})
|
||||
return
|
||||
|
||||
# ── P0-1: Stabilise — catch events published during replay ──
|
||||
while sub.has_pending:
|
||||
sub.mark_seen()
|
||||
late_events = await store.conversation_events.list_events(
|
||||
@@ -285,7 +285,6 @@ async def stream_conversation(
|
||||
if _is_terminal(evt):
|
||||
return
|
||||
|
||||
# ── Send snapshot marker ──
|
||||
snapshot = {
|
||||
"seq": last_seq,
|
||||
"event_id": f"snapshot_{conversation_id}_{last_seq}",
|
||||
@@ -297,7 +296,6 @@ async def stream_conversation(
|
||||
if not await request.is_disconnected():
|
||||
yield _format_sse_event(snapshot)
|
||||
|
||||
# ── Phase 2: Real-time subscription ──
|
||||
heartbeat_interval = hub.heartbeat_interval
|
||||
while True:
|
||||
if hub.is_closed:
|
||||
@@ -307,13 +305,11 @@ async def stream_conversation(
|
||||
|
||||
changed = await sub.wait_for_change(timeout=heartbeat_interval)
|
||||
if not changed:
|
||||
# Timeout → heartbeat (or hub closed).
|
||||
if hub.is_closed:
|
||||
return
|
||||
yield _format_heartbeat()
|
||||
continue
|
||||
|
||||
# Generation advanced — fetch new events from DB.
|
||||
new_events = await store.conversation_events.list_events(
|
||||
conversation_id, after_seq=last_seq,
|
||||
)
|
||||
@@ -322,7 +318,6 @@ async def stream_conversation(
|
||||
return
|
||||
yield _format_sse_event(evt)
|
||||
last_seq = max(last_seq, int(evt.get("seq", last_seq)))
|
||||
# P0-2: terminal event → close stream after sending it.
|
||||
if _is_terminal(evt):
|
||||
return
|
||||
except asyncio.CancelledError:
|
||||
@@ -334,8 +329,63 @@ async def stream_conversation(
|
||||
finally:
|
||||
sub.close()
|
||||
|
||||
return _gen()
|
||||
|
||||
|
||||
async def _get_active_run_id(store: Any, conversation_id: str) -> str | None:
|
||||
from sqlalchemy import select
|
||||
|
||||
from deerflow.persistence.maf_roundtable.model import MafRoundtableConversationRow
|
||||
async with store.session_factory() as db:
|
||||
row = (
|
||||
await db.execute(
|
||||
select(MafRoundtableConversationRow.active_run_id).where(
|
||||
MafRoundtableConversationRow.id == conversation_id,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
return row[0] if row and row[0] else None
|
||||
|
||||
|
||||
@router.get("/conversations/{conversation_id}/stream")
|
||||
async def stream_conversation(
|
||||
conversation_id: str,
|
||||
request: Request,
|
||||
after_seq: int = Query(default=0, ge=0),
|
||||
):
|
||||
"""SSE stream for one conversation (§4.2)."""
|
||||
svc = _get_conversation_service(request)
|
||||
hub = _get_event_hub(request)
|
||||
user_id, is_admin = _user_ctx(request)
|
||||
|
||||
if hub is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail="SSE event hub not configured",
|
||||
)
|
||||
|
||||
stream_ready = getattr(request.app.state, "maf_conversation_stream_ready", True)
|
||||
if not stream_ready:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||
detail=getattr(request.app.state, "maf_conversation_stream_status", "stream_not_ready"),
|
||||
)
|
||||
|
||||
try:
|
||||
await svc.get_conversation(
|
||||
conversation_id=conversation_id, user_id=user_id, is_admin=is_admin,
|
||||
)
|
||||
except ConversationNotFoundError:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Conversation not found")
|
||||
|
||||
return StreamingResponse(
|
||||
event_generator(),
|
||||
build_conversation_event_generator(
|
||||
service=svc,
|
||||
hub=hub,
|
||||
request=request,
|
||||
conversation_id=conversation_id,
|
||||
after_seq=after_seq,
|
||||
),
|
||||
media_type="text/event-stream",
|
||||
headers=_SSE_HEADERS,
|
||||
)
|
||||
@@ -119,9 +119,20 @@ async def get_health(request: Request) -> HealthResponse:
|
||||
cfg = app_config.maf_roundtable
|
||||
svc = _get_service(request)
|
||||
store_available = svc is not None and svc.store is not None
|
||||
from app.maf_roundtable.dispatch_contracts import MafDispatchCatalog
|
||||
|
||||
dispatch_ready, dispatch_status = MafDispatchCatalog(cfg).readiness()
|
||||
readiness = get_maf_write_guard_readiness(app_config=app_config)
|
||||
stream_ready = bool(getattr(request.app.state, "maf_conversation_stream_ready", False))
|
||||
stream_status = str(getattr(request.app.state, "maf_conversation_stream_status", "unknown"))
|
||||
conv_backend = str(getattr(request.app.state, "maf_conversation_event_backend", "memory"))
|
||||
return HealthResponse(
|
||||
enabled=bool(cfg.enabled and svc is not None and svc.enabled),
|
||||
enabled=bool(cfg.enabled and dispatch_ready and svc is not None and svc.enabled),
|
||||
conversation_dispatch_ready=bool(cfg.enabled and dispatch_ready and svc is not None and svc.enabled),
|
||||
conversation_dispatch_status=dispatch_status,
|
||||
conversation_stream_ready=stream_ready,
|
||||
conversation_stream_status=stream_status,
|
||||
conversation_event_backend=conv_backend,
|
||||
store_available=store_available,
|
||||
default_mode=cfg.default_mode,
|
||||
default_write_policy=cfg.default_write_policy,
|
||||
|
||||
Reference in new issue
Block a user