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
@@ -8,7 +8,17 @@ from app.main import app
|
||||
async def tenant_id(async_client: AsyncClient) -> str:
|
||||
"""Create a tenant and return its ID."""
|
||||
resp = await async_client.post("/api/v1/tenants", json={"name": "Test Tenant", "slug": "test-tenant"})
|
||||
return resp.json()["id"]
|
||||
tid = resp.json()["id"]
|
||||
# Configure tenant with LLM provider (mock settings)
|
||||
await async_client.put(
|
||||
f"/api/v1/tenants/{tid}/config",
|
||||
json={
|
||||
"llm_provider": "openai",
|
||||
"llm_api_key": "test-api-key",
|
||||
"llm_model": "gpt-4",
|
||||
},
|
||||
)
|
||||
return tid
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -186,3 +196,101 @@ class TestMessageOperations:
|
||||
assert resp.status_code == 200
|
||||
# The response may or may not include messages depending on schema
|
||||
# For now, we just verify the conversation is accessible
|
||||
|
||||
|
||||
class TestSSEStreaming:
|
||||
"""Test SSE streaming message responses."""
|
||||
|
||||
@pytest.fixture
|
||||
async def conversation_id(self, async_client: AsyncClient, tenant_id: str, employee_id: str) -> str:
|
||||
"""Create a conversation and return its ID."""
|
||||
resp = await async_client.post(
|
||||
"/api/v1/conversations",
|
||||
json={
|
||||
"tenant_id": tenant_id,
|
||||
"employee_id": employee_id,
|
||||
"user_id": "user-001",
|
||||
},
|
||||
)
|
||||
return resp.json()["id"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_stream_events(self, async_client: AsyncClient, conversation_id: str):
|
||||
"""Test SSE stream returns properly formatted events."""
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
# Mock provider that yields tokens
|
||||
async def mock_stream(messages):
|
||||
for token in ["Hello", " ", "world", "!"]:
|
||||
yield token
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.chat_stream = mock_stream
|
||||
|
||||
with patch(
|
||||
"app.services.conversation_service.get_provider_for_tenant",
|
||||
AsyncMock(return_value=mock_provider)
|
||||
):
|
||||
async with async_client.stream(
|
||||
"POST",
|
||||
f"/api/v1/conversations/{conversation_id}/messages/stream",
|
||||
json={"content": "Hello"},
|
||||
) as response:
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"] == "text/event-stream; charset=utf-8"
|
||||
|
||||
events = []
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith("data: "):
|
||||
events.append(line[6:]) # Remove "data: " prefix
|
||||
|
||||
# Should have at least message_start, some tokens, and message_end
|
||||
assert len(events) >= 2
|
||||
|
||||
# First event should be message_start
|
||||
import json
|
||||
first_event = json.loads(events[0])
|
||||
assert first_event["type"] == "message_start"
|
||||
|
||||
# Last event should be message_end
|
||||
last_event = json.loads(events[-1])
|
||||
assert last_event["type"] == "message_end"
|
||||
assert "token_count" in last_event
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_sse_stream_persists_messages(
|
||||
self, async_client: AsyncClient, conversation_id: str
|
||||
):
|
||||
"""Test that SSE stream persists both user and assistant messages."""
|
||||
from unittest.mock import AsyncMock, patch, MagicMock
|
||||
|
||||
# Mock provider that yields tokens
|
||||
async def mock_stream(messages):
|
||||
for token in ["Test", " ", "response"]:
|
||||
yield token
|
||||
|
||||
mock_provider = MagicMock()
|
||||
mock_provider.chat_stream = mock_stream
|
||||
|
||||
with patch(
|
||||
"app.services.conversation_service.get_provider_for_tenant",
|
||||
AsyncMock(return_value=mock_provider)
|
||||
):
|
||||
async with async_client.stream(
|
||||
"POST",
|
||||
f"/api/v1/conversations/{conversation_id}/messages/stream",
|
||||
json={"content": "Test question"},
|
||||
) as response:
|
||||
# Consume the stream
|
||||
async for _ in response.aiter_lines():
|
||||
pass
|
||||
|
||||
# Verify messages were persisted
|
||||
resp = await async_client.get(f"/api/v1/conversations/{conversation_id}/messages")
|
||||
messages = resp.json()
|
||||
|
||||
# Should have user message and assistant message
|
||||
assert len(messages) >= 2
|
||||
assert messages[0]["role"] == "user"
|
||||
assert messages[0]["content"] == "Test question"
|
||||
assert messages[1]["role"] == "assistant"
|
||||
Reference in new issue
Block a user