feat: add unified strategy project workflow
This commit is contained in:
1 parent
67d12929fc
commit
a26fd32eba
28 files changed
+3382
-191
No files matched your search
@@ -13,9 +13,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from contextlib import asynccontextmanager
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
import app.gateway.roundtable_model_fallback as mf
|
||||
import app.gateway.routers.parallel_agents as parallel_agents
|
||||
from app.gateway.roundtable_model_fallback import (
|
||||
fallback_model_chain,
|
||||
is_llm_error_message,
|
||||
@@ -56,6 +61,181 @@ def test_is_llm_error_message_normal_is_none() -> None:
|
||||
assert is_llm_error_message({"type": "ai", "content": ""}) is None
|
||||
|
||||
|
||||
def test_parallel_agent_run_raises_instead_of_completing_llm_fallback(monkeypatch) -> None:
|
||||
fallback_message = {
|
||||
"type": "ai",
|
||||
"content": "大模型本次调用未能成功完成,请稍后再试或更换模型。",
|
||||
}
|
||||
|
||||
async def fake_stream_upstream(*_args, **_kwargs):
|
||||
yield None, [f"data: {json.dumps({'messages': [fallback_message]}, ensure_ascii=False)}"]
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
|
||||
|
||||
async def collect() -> list[object]:
|
||||
return [
|
||||
item
|
||||
async for item in parallel_agents._run_one_agent(
|
||||
None, # type: ignore[arg-type]
|
||||
"http://gateway.test",
|
||||
{"Authorization": "Bearer test"},
|
||||
"strategy-selection-intent",
|
||||
"thread-id",
|
||||
"test task",
|
||||
"",
|
||||
)
|
||||
]
|
||||
|
||||
try:
|
||||
asyncio.run(collect())
|
||||
except RuntimeError as exc:
|
||||
assert "模型调用失败" in str(exc)
|
||||
else:
|
||||
raise AssertionError("LLM fallback message must not be emitted as agent_done")
|
||||
|
||||
|
||||
def test_parallel_agent_run_rejects_punctuation_only_delivery(monkeypatch) -> None:
|
||||
invalid_message = {"type": "ai", "content": "!" * 500}
|
||||
|
||||
async def fake_stream_upstream(*_args, **_kwargs):
|
||||
yield None, [f"data: {json.dumps({'messages': [invalid_message]}, ensure_ascii=False)}"]
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
|
||||
|
||||
async def collect() -> list[object]:
|
||||
return [
|
||||
item
|
||||
async for item in parallel_agents._run_one_agent(
|
||||
None, # type: ignore[arg-type]
|
||||
"http://gateway.test",
|
||||
{"Authorization": "Bearer test"},
|
||||
"strategy-selection-angle-security",
|
||||
"thread-id",
|
||||
"test task",
|
||||
"",
|
||||
)
|
||||
]
|
||||
|
||||
try:
|
||||
asyncio.run(collect())
|
||||
except RuntimeError as exc:
|
||||
assert "未生成有效正文" in str(exc)
|
||||
else:
|
||||
raise AssertionError("Punctuation-only output must not be emitted as agent_done")
|
||||
|
||||
|
||||
def test_parallel_agent_run_allows_empty_content_clarification(monkeypatch) -> None:
|
||||
clarification_message = {
|
||||
"type": "ai",
|
||||
"content": "",
|
||||
"tool_calls": [
|
||||
{
|
||||
"id": "clarify-1",
|
||||
"name": "ask_clarification",
|
||||
"args": {
|
||||
"question": "请确认策略时间范围。",
|
||||
"clarification_type": "single_choice",
|
||||
"options": ["短期", "长期"],
|
||||
"allow_custom": True,
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
async def fake_stream_upstream(*_args, **_kwargs):
|
||||
yield None, [
|
||||
f"data: {json.dumps({'messages': [clarification_message]}, ensure_ascii=False)}"
|
||||
]
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_stream_upstream", fake_stream_upstream)
|
||||
|
||||
async def collect() -> list[object]:
|
||||
return [
|
||||
item
|
||||
async for item in parallel_agents._run_one_agent(
|
||||
None, # type: ignore[arg-type]
|
||||
"http://gateway.test",
|
||||
{"Authorization": "Bearer test"},
|
||||
"strategy-selection-intent",
|
||||
"thread-id",
|
||||
"test task",
|
||||
"",
|
||||
)
|
||||
]
|
||||
|
||||
events = asyncio.run(collect())
|
||||
|
||||
assert len(events) == 1
|
||||
event = events[0]
|
||||
assert isinstance(event, dict)
|
||||
assert event["type"] == "clarification"
|
||||
assert event["content"] == ""
|
||||
assert event["clarification"]["question"] == "请确认策略时间范围。"
|
||||
|
||||
|
||||
def test_parallel_agent_substantive_content_accepts_chinese_and_english() -> None:
|
||||
assert parallel_agents._has_substantive_content("这是一份包含目标、前置条件、阶段路径和验收标准的完整策略。")
|
||||
assert parallel_agents._has_substantive_content(
|
||||
"A complete strategy with objectives, prerequisites, milestones, and measurable outcomes."
|
||||
)
|
||||
assert not parallel_agents._has_substantive_content("<think>internal reasoning only</think>!!!")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("buffered_content", "expected_error"),
|
||||
[
|
||||
("大模型本次调用未能成功完成,请稍后再试或更换模型。", "模型调用失败"),
|
||||
("", "未生成有效正文"),
|
||||
("<think>internal reasoning only</think>", "未生成有效正文"),
|
||||
("!" * 500, "未生成有效正文"),
|
||||
],
|
||||
)
|
||||
def test_parallel_agent_truncated_stream_rejects_invalid_buffer(
|
||||
monkeypatch,
|
||||
buffered_content: str,
|
||||
expected_error: str,
|
||||
) -> None:
|
||||
async def fake_run_one_agent(*_args, **_kwargs):
|
||||
yield "event: messages"
|
||||
yield f"data: {json.dumps([{'type': 'ai', 'content': buffered_content}], ensure_ascii=False)}"
|
||||
|
||||
@asynccontextmanager
|
||||
async def fake_client(*_args, **_kwargs):
|
||||
yield object()
|
||||
|
||||
async def fake_create_thread(*_args, **_kwargs):
|
||||
return "thread-id"
|
||||
|
||||
monkeypatch.setattr(parallel_agents, "_run_one_agent", fake_run_one_agent)
|
||||
monkeypatch.setattr(parallel_agents, "_make_loopback_client", fake_client)
|
||||
monkeypatch.setattr(parallel_agents, "_create_thread", fake_create_thread)
|
||||
monkeypatch.setattr(parallel_agents, "_loopback_base", lambda _request: "http://gateway.test")
|
||||
monkeypatch.setattr(parallel_agents, "_auth_headers", lambda _request: {})
|
||||
|
||||
async def collect() -> list[dict[str, object]]:
|
||||
response = await parallel_agents.run_parallel_agents_stream(
|
||||
SimpleNamespace(),
|
||||
{
|
||||
"task_name": "test task",
|
||||
"brief": "test brief",
|
||||
"agent_ids": ["strategy-selection-intent"],
|
||||
},
|
||||
)
|
||||
events: list[dict[str, object]] = []
|
||||
async for chunk in response.body_iterator:
|
||||
text = chunk.decode() if isinstance(chunk, bytes) else chunk
|
||||
for line in text.splitlines():
|
||||
if line.startswith("data:"):
|
||||
events.append(json.loads(line[5:].strip()))
|
||||
return events
|
||||
|
||||
events = asyncio.run(collect())
|
||||
assert not any(event.get("type") == "agent_done" for event in events)
|
||||
errors = [event for event in events if event.get("type") == "agent_error"]
|
||||
assert len(errors) == 1
|
||||
assert expected_error in str(errors[0].get("message"))
|
||||
|
||||
|
||||
# ── 中间件给兜底 AIMessage 打标记 ────────────────────────────────────────────
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user