mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-05-21 15:36:48 +00:00
2b33bfd78f
Apply the require_permission decorator to all 28 routes that take a
{thread_id} path parameter. Combined with the strict middleware
(previous commit), this gives the double-layer protection that
AUTH_TEST_PLAN test 7.5.9 documents:
Layer 1 (AuthMiddleware): cookie + JWT validation, rejects junk
cookies and stamps request.state.user
Layer 2 (@require_permission with owner_check=True): per-resource
ownership verification via
ThreadMetaStore.check_access — returns
404 if a different user owns the thread
The decorator's owner_check branch is rewritten to use the SQL
thread_meta_repo (the 2.0-rc persistence layer) instead of the
LangGraph store path that PR #1728 used (_store_get / get_store
in routers/threads.py). The inject_record convenience is dropped
— no caller in 2.0 needs the LangGraph blob, and the SQL repo has
a different shape.
Routes decorated (28 total):
- threads.py: delete, patch, get, get-state, post-state, post-history
- thread_runs.py: post-runs, post-runs-stream, post-runs-wait,
list_runs, get_run, cancel_run, join_run, stream_existing_run,
list_thread_messages, list_run_messages, list_run_events,
thread_token_usage
- feedback.py: create, list, stats, delete
- uploads.py: upload (added Request param), list, delete
- artifacts.py: get_artifact
- suggestions.py: generate (renamed body parameter to avoid
conflict with FastAPI Request)
Test fixes:
- test_suggestions_router.py: bypass the decorator via __wrapped__
(the unit tests cover parsing logic, not auth — no point spinning
up a thread_meta_repo just to test JSON unwrapping)
- test_auth_middleware.py 4 fake-cookie tests: already updated in
the previous commit (745bf432)
Tests: 293 passed (auth + persistence + isolation + suggestions)
Lint: clean
133 lines
4.4 KiB
Python
133 lines
4.4 KiB
Python
"""Feedback endpoints — create, list, stats, delete.
|
|
|
|
Allows users to submit thumbs-up/down feedback on runs,
|
|
optionally scoped to a specific message.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, HTTPException, Request
|
|
from pydantic import BaseModel, Field
|
|
|
|
from app.gateway.authz import require_permission
|
|
from app.gateway.deps import get_current_user, get_feedback_repo, get_run_store
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter(prefix="/api/threads", tags=["feedback"])
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Request / response models
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class FeedbackCreateRequest(BaseModel):
|
|
rating: int = Field(..., description="Feedback rating: +1 (positive) or -1 (negative)")
|
|
comment: str | None = Field(default=None, description="Optional text feedback")
|
|
message_id: str | None = Field(default=None, description="Optional: scope feedback to a specific message")
|
|
|
|
|
|
class FeedbackResponse(BaseModel):
|
|
feedback_id: str
|
|
run_id: str
|
|
thread_id: str
|
|
owner_id: str | None = None
|
|
message_id: str | None = None
|
|
rating: int
|
|
comment: str | None = None
|
|
created_at: str = ""
|
|
|
|
|
|
class FeedbackStatsResponse(BaseModel):
|
|
run_id: str
|
|
total: int = 0
|
|
positive: int = 0
|
|
negative: int = 0
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Endpoints
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
@router.post("/{thread_id}/runs/{run_id}/feedback", response_model=FeedbackResponse)
|
|
@require_permission("threads", "write", owner_check=True)
|
|
async def create_feedback(
|
|
thread_id: str,
|
|
run_id: str,
|
|
body: FeedbackCreateRequest,
|
|
request: Request,
|
|
) -> dict[str, Any]:
|
|
"""Submit feedback (thumbs-up/down) for a run."""
|
|
if body.rating not in (1, -1):
|
|
raise HTTPException(status_code=400, detail="rating must be +1 or -1")
|
|
|
|
user_id = await get_current_user(request)
|
|
|
|
# Validate run exists and belongs to thread
|
|
run_store = get_run_store(request)
|
|
run = await run_store.get(run_id)
|
|
if run is None:
|
|
raise HTTPException(status_code=404, detail=f"Run {run_id} not found")
|
|
if run.get("thread_id") != thread_id:
|
|
raise HTTPException(status_code=404, detail=f"Run {run_id} not found in thread {thread_id}")
|
|
|
|
feedback_repo = get_feedback_repo(request)
|
|
return await feedback_repo.create(
|
|
run_id=run_id,
|
|
thread_id=thread_id,
|
|
rating=body.rating,
|
|
owner_id=user_id,
|
|
message_id=body.message_id,
|
|
comment=body.comment,
|
|
)
|
|
|
|
|
|
@router.get("/{thread_id}/runs/{run_id}/feedback", response_model=list[FeedbackResponse])
|
|
@require_permission("threads", "read", owner_check=True)
|
|
async def list_feedback(
|
|
thread_id: str,
|
|
run_id: str,
|
|
request: Request,
|
|
) -> list[dict[str, Any]]:
|
|
"""List all feedback for a run."""
|
|
feedback_repo = get_feedback_repo(request)
|
|
return await feedback_repo.list_by_run(thread_id, run_id)
|
|
|
|
|
|
@router.get("/{thread_id}/runs/{run_id}/feedback/stats", response_model=FeedbackStatsResponse)
|
|
@require_permission("threads", "read", owner_check=True)
|
|
async def feedback_stats(
|
|
thread_id: str,
|
|
run_id: str,
|
|
request: Request,
|
|
) -> dict[str, Any]:
|
|
"""Get aggregated feedback stats (positive/negative counts) for a run."""
|
|
feedback_repo = get_feedback_repo(request)
|
|
return await feedback_repo.aggregate_by_run(thread_id, run_id)
|
|
|
|
|
|
@router.delete("/{thread_id}/runs/{run_id}/feedback/{feedback_id}")
|
|
@require_permission("threads", "delete", owner_check=True)
|
|
async def delete_feedback(
|
|
thread_id: str,
|
|
run_id: str,
|
|
feedback_id: str,
|
|
request: Request,
|
|
) -> dict[str, bool]:
|
|
"""Delete a feedback record."""
|
|
feedback_repo = get_feedback_repo(request)
|
|
# Verify feedback belongs to the specified thread/run before deleting
|
|
existing = await feedback_repo.get(feedback_id)
|
|
if existing is None:
|
|
raise HTTPException(status_code=404, detail=f"Feedback {feedback_id} not found")
|
|
if existing.get("thread_id") != thread_id or existing.get("run_id") != run_id:
|
|
raise HTTPException(status_code=404, detail=f"Feedback {feedback_id} not found in run {run_id}")
|
|
deleted = await feedback_repo.delete(feedback_id)
|
|
if not deleted:
|
|
raise HTTPException(status_code=404, detail=f"Feedback {feedback_id} not found")
|
|
return {"success": True}
|