feat: add streaming unified review assistant
This commit is contained in:
@@ -56,6 +56,7 @@ async function mockApplication(page, authSession = session()) {
|
||||
else if (url.pathname === "/api/watchlist" || url.pathname === "/api/notes") payload = { items: [] };
|
||||
else if (url.pathname === "/api/alerts") payload = { items: [], unread_count: 0 };
|
||||
else if (url.pathname === "/api/trades") payload = { items: [], summary: {} };
|
||||
else if (url.pathname === "/api/assistant/messages") payload = { items: [] };
|
||||
else if (url.pathname === "/api/search") payload = { groups: { stocks: [], sectors: [], themes: [], indices: [] } };
|
||||
else if (url.pathname === "/api/dragon-tiger") {
|
||||
payload = {
|
||||
@@ -93,6 +94,9 @@ test("admin shell opens every primary workspace and global search", async ({ pag
|
||||
await page.locator("#alertButton").click();
|
||||
await expect(page.locator("#alertsDialog")).toBeVisible();
|
||||
await page.locator("#closeAlertsDialog").click();
|
||||
await page.locator("#assistantButton").click();
|
||||
await expect(page.locator("#assistantDialog")).toBeVisible();
|
||||
await page.locator("#closeAssistantDialog").click();
|
||||
|
||||
const views = [
|
||||
"sentimentCycleView", "limitPool", "brokenView", "downView", "yesterdayView",
|
||||
|
||||
@@ -12,12 +12,15 @@ class ApiAccessPolicyTests(unittest.TestCase):
|
||||
("GET", "/api/screener/tracking"): "member",
|
||||
("GET", "/api/mentors/messages"): "member",
|
||||
("GET", "/api/heaven/setup"): "member",
|
||||
("GET", "/api/assistant/messages"): "member",
|
||||
("POST", "/api/screener/run"): "member",
|
||||
("POST", "/api/screener/tracking/refresh"): "member",
|
||||
("POST", "/api/mentors/chat"): "member",
|
||||
("POST", "/api/heaven/interpret"): "member",
|
||||
("POST", "/api/assistant/chat"): "member",
|
||||
("DELETE", "/api/screener/strategies/42"): "member",
|
||||
("DELETE", "/api/mentors/messages"): "member",
|
||||
("DELETE", "/api/assistant/messages"): "member",
|
||||
}
|
||||
for (method, path), role in cases.items():
|
||||
with self.subTest(method=method, path=path):
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from assistant_agent import ReviewAssistantError, stream_review_assistant
|
||||
from database import ReviewDatabase
|
||||
from server import RequestHandler
|
||||
|
||||
|
||||
class StreamingResponse:
|
||||
def __init__(self, lines: list[bytes]):
|
||||
self.lines = lines
|
||||
|
||||
def __enter__(self):
|
||||
return iter(self.lines)
|
||||
|
||||
def __exit__(self, exc_type, exc, traceback):
|
||||
return False
|
||||
|
||||
|
||||
class ReviewAssistantStreamingTests(unittest.TestCase):
|
||||
def test_openai_compatible_sse_is_yielded_in_order(self):
|
||||
response = StreamingResponse(
|
||||
[
|
||||
b'data: {"choices":[{"delta":{"content":"first"}}]}\n',
|
||||
b'data: {"choices":[{"delta":{"content":" second"}}]}\n',
|
||||
b'data: [DONE]\n',
|
||||
]
|
||||
)
|
||||
with patch("assistant_agent.urllib.request.urlopen", return_value=response):
|
||||
chunks = list(
|
||||
stream_review_assistant({}, "question", [], "key", "https://example.test/v1", "model")
|
||||
)
|
||||
self.assertEqual(chunks, ["first", " second"])
|
||||
|
||||
def test_empty_stream_is_rejected(self):
|
||||
response = StreamingResponse([b"data: [DONE]\n"])
|
||||
with patch("assistant_agent.urllib.request.urlopen", return_value=response):
|
||||
with self.assertRaises(ReviewAssistantError):
|
||||
list(
|
||||
stream_review_assistant({}, "question", [], "key", "https://example.test/v1", "model")
|
||||
)
|
||||
|
||||
def test_ndjson_event_writer_flushes_complete_line(self):
|
||||
handler = RequestHandler.__new__(RequestHandler)
|
||||
handler.wfile = io.BytesIO()
|
||||
RequestHandler._write_stream_event(handler, {"type": "delta", "content": "片段"})
|
||||
self.assertEqual(
|
||||
handler.wfile.getvalue().decode("utf-8"),
|
||||
'{"type":"delta","content":"片段"}\n',
|
||||
)
|
||||
|
||||
|
||||
class ReviewAssistantHistoryTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.database = ReviewDatabase(Path(self.temp.name) / "review.db")
|
||||
self.owner = self.database.create_user("assistant_owner", "salt", "hash")
|
||||
self.other = self.database.create_user("assistant_other", "salt", "hash")
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temp.cleanup()
|
||||
|
||||
def test_history_is_private_and_clear_is_scoped(self):
|
||||
self.database.save_assistant_exchange(
|
||||
self.owner["id"], "今天怎么看?", "先看承接。", "20260722"
|
||||
)
|
||||
owner_messages = self.database.list_assistant_messages(self.owner["id"])
|
||||
self.assertEqual([item["role"] for item in owner_messages], ["user", "assistant"])
|
||||
self.assertEqual(self.database.list_assistant_messages(self.other["id"]), [])
|
||||
self.assertEqual(self.database.delete_assistant_messages(self.other["id"]), 0)
|
||||
self.assertEqual(self.database.delete_assistant_messages(self.owner["id"]), 2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user