feat: add topic summarize endpoint
This commit is contained in:
@@ -9,8 +9,10 @@ from src.backend.db.database import (
|
||||
get_topic,
|
||||
update_topic,
|
||||
delete_topic,
|
||||
list_requests,
|
||||
)
|
||||
from src.backend.models.api import TopicCreate, TopicUpdate, ok, fail
|
||||
from src.backend.services.worker_client import WorkerClient
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/topics",
|
||||
@@ -91,3 +93,38 @@ async def delete(topic_id: str, db=Depends(_get_db)):
|
||||
content=fail("NOT_FOUND", "Topic not found or has active requests"),
|
||||
)
|
||||
return ok({"deleted": True})
|
||||
|
||||
|
||||
@router.post("/{topic_id}/summarize", summary="Regenerate topic summary")
|
||||
async def summarize_topic(topic_id: str, db=Depends(_get_db)):
|
||||
topic = await get_topic(db, topic_id)
|
||||
if not topic:
|
||||
return JSONResponse(
|
||||
status_code=404,
|
||||
content=fail("NOT_FOUND", "Topic not found"),
|
||||
)
|
||||
|
||||
completed = await list_requests(db, status="completed", topic_id=topic_id)
|
||||
if not completed:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=fail("NO_DATA", "No completed requests to summarize"),
|
||||
)
|
||||
|
||||
exchanges = [
|
||||
{"prompt": r["prompt"], "result": r["result"]}
|
||||
for r in completed
|
||||
if r["result"]
|
||||
]
|
||||
|
||||
worker = WorkerClient()
|
||||
result = await worker.summarize(exchanges=exchanges, mode="complete")
|
||||
|
||||
if not result.get("success"):
|
||||
return JSONResponse(
|
||||
status_code=502,
|
||||
content=fail("WORKER_ERROR", "Worker failed to generate summary"),
|
||||
)
|
||||
|
||||
updated = await update_topic(db, topic_id, summary=result["summary"])
|
||||
return ok(updated)
|
||||
|
||||
Reference in New Issue
Block a user