feat(agent): control reasoning visibility
This commit is contained in:
@@ -30,6 +30,7 @@ from app.services.chat_queue_runtime import (
|
||||
)
|
||||
from app.services.chat_queue_service import load_chat_queue_config
|
||||
from app.services.chat_stream_service import ChatStreamService
|
||||
from app.services.reasoning_policy_service import ReasoningPolicyService
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -54,7 +55,14 @@ def history(
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> dict:
|
||||
messages = ChatService.get_history(db, current_user, sessionId)
|
||||
return api_success([ChatMessageRead.model_validate(message).model_dump(mode="json") for message in messages])
|
||||
reasoning_visible = ReasoningPolicyService.is_visible(db)
|
||||
result = []
|
||||
for message in messages:
|
||||
item = ChatMessageRead.model_validate(message).model_dump(mode="json")
|
||||
if message.role == "assistant" and not reasoning_visible:
|
||||
item["content"] = ReasoningPolicyService.strip_reasoning(item["content"])
|
||||
result.append(item)
|
||||
return api_success(result)
|
||||
|
||||
|
||||
@router.put("/session/title")
|
||||
@@ -152,14 +160,20 @@ async def _chat_stream(payload: ChatCompletionRequest, db: Session, current_user
|
||||
return
|
||||
|
||||
try:
|
||||
reasoning_visible = ReasoningPolicyService.is_visible(db)
|
||||
yield _sse_event(
|
||||
"generating",
|
||||
message="已进入生成队列,正在生成回答。",
|
||||
message="思考中",
|
||||
activeCount=queue_request.active_count,
|
||||
waitingCount=queue_request.waiting_count,
|
||||
reasoningVisible=reasoning_visible,
|
||||
)
|
||||
async for chunk in ChatStreamService.stream_answer_async(db, current_user, payload.sessionId, payload.message):
|
||||
yield _sse_event("content", content=chunk)
|
||||
chunks = ChatStreamService.stream_answer_async(db, current_user, payload.sessionId, payload.message)
|
||||
async for segment in ReasoningPolicyService.iter_segments(chunks):
|
||||
if segment.kind == "content":
|
||||
yield _sse_event("content", content=segment.content)
|
||||
elif reasoning_visible:
|
||||
yield _sse_event("reasoning", content=segment.content)
|
||||
except HTTPException as exc:
|
||||
yield _sse_event("error", message=str(exc.detail))
|
||||
except asyncio.CancelledError:
|
||||
|
||||
Reference in New Issue
Block a user