feat(server): 暴露 /api/sessions/{sid}/ws 进度流端点(+websockets 依赖)
This commit is contained in:
@@ -11,14 +11,16 @@
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI, File, Form, HTTPException, UploadFile
|
||||
from fastapi import FastAPI, File, Form, HTTPException, UploadFile, WebSocket, WebSocketDisconnect
|
||||
from fastapi.responses import FileResponse, HTMLResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from genesis.inference.factory import build_inference_engine
|
||||
from genesis.server.hub import hub
|
||||
from genesis.server.service import FileTypeError, GenesisService, ServiceStepError
|
||||
from genesis.server.store import (
|
||||
ProjectConfigError, ProjectsStore, SessionNotFoundError, SessionStore,
|
||||
@@ -302,6 +304,22 @@ def create_app(
|
||||
raise _error(404, "RESULT_NOT_FOUND", "QA 报告不存在")
|
||||
return FileResponse(rec.qa_report_path, media_type="application/json", filename="qa-report.json")
|
||||
|
||||
# ---------- 进度流(WebSocket) ----------
|
||||
|
||||
@app.websocket("/api/sessions/{sid}/ws")
|
||||
async def session_progress_ws(ws: WebSocket, sid: str):
|
||||
await ws.accept()
|
||||
hub.register_loop(asyncio.get_running_loop())
|
||||
q = hub.subscribe(sid)
|
||||
try:
|
||||
while True:
|
||||
event = await q.get()
|
||||
await ws.send_json(event)
|
||||
except WebSocketDisconnect:
|
||||
pass
|
||||
finally:
|
||||
hub.unsubscribe(sid, q)
|
||||
|
||||
# ---------- 聊天 ----------
|
||||
|
||||
@app.post("/api/chat/{sid}/messages")
|
||||
|
||||
Reference in New Issue
Block a user