feat(backend): B3 Task 3.2 - SSE 流式响应实现
- 添加 stream_chat 生成器函数处理 SSE 事件流 - 实现 message_start / token / message_end 事件格式 - 添加 messages/stream SSE 端点 - 构建 LLM 消息列表(system prompt + 历史 + 用户消息) - 持久化用户和 assistant 消息到数据库 - 添加 SSE 测试用 mock Provider Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
a0c2586487
commit
1d7946d98a
3 files changed
+193
-4
No files matched your search
@@ -1,4 +1,5 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.api.deps import get_db
|
||||
@@ -53,7 +54,6 @@ async def send_message(
|
||||
data: MessageCreate,
|
||||
session: AsyncSession = Depends(get_db),
|
||||
) -> MessageResponse:
|
||||
# For now, just create a user message (LLM response will come in Task 3.2)
|
||||
await conversation_service.get_conversation(session, conversation_id)
|
||||
msg = await conversation_service.create_message(
|
||||
session, conversation_id, role="user", content=data.content
|
||||
@@ -61,6 +61,19 @@ async def send_message(
|
||||
return MessageResponse.from_model(msg)
|
||||
|
||||
|
||||
@router.post("/{conversation_id}/messages/stream")
|
||||
async def send_message_stream(
|
||||
conversation_id: str,
|
||||
data: MessageCreate,
|
||||
session: AsyncSession = Depends(get_db),
|
||||
) -> StreamingResponse:
|
||||
"""SSE 流式响应端点"""
|
||||
return StreamingResponse(
|
||||
conversation_service.stream_chat(session, conversation_id, data.content),
|
||||
media_type="text/event-stream",
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{conversation_id}/messages", response_model=list[MessageResponse])
|
||||
async def list_messages(
|
||||
conversation_id: str, session: AsyncSession = Depends(get_db)
|
||||
|
||||
Reference in new issue
Block a user