refactor: 重构聊天模块支持 SSE 断线重连及用户上下文隔离
This commit is contained in:
@@ -70,7 +70,6 @@ def _router_rules(llm_config: SystemAgentLLMConfig | None) -> list[str]:
|
||||
"- Return key_entities and constraints that are execution-relevant; low confidence -> omit rather than guess.",
|
||||
"- Set execution_mode by complexity: onestep / tool_assisted / multistep.",
|
||||
"- Set result_typing.primary to the most suitable response shape; use clarification_request only when required info is missing.",
|
||||
"- Set ui.ui_mode and ui.ui_decision_reason based on whether structured UI improves actionability.",
|
||||
f"- task_typing.primary must use one TaskType enum: {_enum_values(TaskType)}.",
|
||||
f"- task_typing.secondary max 3 enums: {_enum_values(TaskType)}.",
|
||||
f"- result_typing.primary must use one ResultType enum: {_enum_values(ResultType)}.",
|
||||
|
||||
@@ -279,7 +279,7 @@ class AgentScopeRunner:
|
||||
runtime_mode: RuntimeMode,
|
||||
work_memory: WorkProfileContent | None,
|
||||
) -> WorkerAgentOutputLite:
|
||||
worker_output_model = resolve_worker_output_model(router_output.ui.ui_mode)
|
||||
worker_output_model = resolve_worker_output_model(router_output.execution_mode)
|
||||
await self._emit_step_event(
|
||||
pipeline=pipeline,
|
||||
run_input=run_input,
|
||||
|
||||
@@ -51,7 +51,10 @@ def _status_badge_needed(intent: UiHintIntent, status: UiHintStatus) -> bool:
|
||||
|
||||
|
||||
def _status_label(status: str) -> str:
|
||||
return status.upper()
|
||||
normalized = status.strip().lower()
|
||||
if not normalized:
|
||||
return "ui.status.info"
|
||||
return f"ui.status.{normalized}"
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
||||
@@ -14,13 +14,11 @@ from schemas.agent.runtime_models import (
|
||||
ResultTyping,
|
||||
ResultType,
|
||||
RouterAgentOutput,
|
||||
RouterUiDecision,
|
||||
RunStatus,
|
||||
TaskType,
|
||||
TaskTyping,
|
||||
ToolAgentOutput,
|
||||
ToolStatus,
|
||||
UiMode,
|
||||
WorkerAgentOutputLite,
|
||||
WorkerAgentOutputRich,
|
||||
resolve_worker_output_model,
|
||||
@@ -47,7 +45,6 @@ __all__ = [
|
||||
"ClientTimeContext",
|
||||
"ResultType",
|
||||
"RouterAgentOutput",
|
||||
"RouterUiDecision",
|
||||
"RunStatus",
|
||||
"RuntimeMode",
|
||||
"TaskType",
|
||||
@@ -56,7 +53,6 @@ __all__ = [
|
||||
"SystemVisibilityBit",
|
||||
"ToolAgentOutput",
|
||||
"ToolStatus",
|
||||
"UiMode",
|
||||
"UiHintAction",
|
||||
"UiHintIntent",
|
||||
"UiHintSection",
|
||||
|
||||
@@ -62,11 +62,6 @@ class ExecutionMode(str, Enum):
|
||||
MULTISTEP = "multistep"
|
||||
|
||||
|
||||
class UiMode(str, Enum):
|
||||
NONE = "none"
|
||||
RICH = "rich"
|
||||
|
||||
|
||||
class RunStatus(str, Enum):
|
||||
SUCCESS = "success"
|
||||
PARTIAL_SUCCESS = "partial_success"
|
||||
@@ -114,13 +109,6 @@ class NormalizedTaskInput(BaseModel):
|
||||
context_summary: str = Field(default="", max_length=2000)
|
||||
|
||||
|
||||
class RouterUiDecision(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
ui_mode: UiMode
|
||||
ui_decision_reason: str
|
||||
|
||||
|
||||
class RouterAgentOutput(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
|
||||
@@ -130,7 +118,6 @@ class RouterAgentOutput(BaseModel):
|
||||
task_typing: TaskTyping
|
||||
execution_mode: ExecutionMode
|
||||
result_typing: ResultTyping
|
||||
ui: RouterUiDecision
|
||||
|
||||
|
||||
class ErrorInfo(BaseModel):
|
||||
@@ -175,7 +162,9 @@ class AgentOutput(WorkerAgentOutputRich):
|
||||
WorkerAgentOutput = WorkerAgentOutputLite | WorkerAgentOutputRich
|
||||
|
||||
|
||||
def resolve_worker_output_model(ui_mode: UiMode) -> type[WorkerAgentOutputLite]:
|
||||
if ui_mode == UiMode.RICH:
|
||||
return WorkerAgentOutputRich
|
||||
return WorkerAgentOutputLite
|
||||
def resolve_worker_output_model(
|
||||
execution_mode: ExecutionMode,
|
||||
) -> type[WorkerAgentOutputLite]:
|
||||
if execution_mode == ExecutionMode.ONESTEP:
|
||||
return WorkerAgentOutputLite
|
||||
return WorkerAgentOutputRich
|
||||
|
||||
@@ -595,11 +595,12 @@ def build_status_panel(
|
||||
secondary_button: UiButtonNode | None = None,
|
||||
node_id: str | None = None,
|
||||
) -> UiStackNode:
|
||||
status_label = f"ui.status.{status.value}"
|
||||
children: list[UiNode] = [
|
||||
build_stack(
|
||||
[
|
||||
build_text(title, role=TextRole.TITLE),
|
||||
build_badge(label=status.value.upper(), status=status),
|
||||
build_badge(label=status_label, status=status),
|
||||
],
|
||||
direction=LayoutDirection.HORIZONTAL,
|
||||
gap=8,
|
||||
|
||||
@@ -48,6 +48,7 @@ from v1.users.dependencies import get_current_user
|
||||
router = APIRouter(prefix="/agent", tags=["agent"])
|
||||
logger = get_logger("v1.agent.router")
|
||||
_LAST_EVENT_ID_RE = re.compile(r"^\d+-\d+$")
|
||||
_RUN_ID_RE = re.compile(r"^[A-Za-z0-9_-]{1,128}$")
|
||||
_MAX_SSE_CONNECTIONS_PER_USER = 3
|
||||
_SSE_SLOT_TTL_SECONDS = 15 * 60
|
||||
_TERMINAL_RUN_EVENT_TYPES = {"RUN_FINISHED", "RUN_ERROR"}
|
||||
@@ -120,6 +121,11 @@ def _is_terminal_run_event(event: dict[str, object]) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _is_target_run_event(event: dict[str, object], *, target_run_id: str) -> bool:
|
||||
run_id = event.get("runId")
|
||||
return isinstance(run_id, str) and run_id == target_run_id
|
||||
|
||||
|
||||
@router.post(
|
||||
"/runs", response_model=TaskAcceptedResponse, status_code=status.HTTP_202_ACCEPTED
|
||||
)
|
||||
@@ -188,9 +194,19 @@ async def stream_events(
|
||||
thread_id: str,
|
||||
service: Annotated[AgentService, Depends(get_agent_service)],
|
||||
current_user: Annotated[CurrentUser, Depends(get_current_user)],
|
||||
run_id: str | None = Query(default=None, alias="runId"),
|
||||
last_event_id: str | None = Header(default=None, alias="Last-Event-ID"),
|
||||
idle_limit: int = Query(default=300, ge=1, le=3600),
|
||||
) -> StreamingResponse:
|
||||
if run_id is None or _RUN_ID_RE.fullmatch(run_id) is None:
|
||||
raise ApiProblemError(
|
||||
status_code=422,
|
||||
detail=problem_payload(
|
||||
code="AGENT_INVALID_RUN_ID",
|
||||
detail="Invalid runId",
|
||||
),
|
||||
)
|
||||
|
||||
if last_event_id is not None and (
|
||||
len(last_event_id) > 32 or _LAST_EVENT_ID_RE.fullmatch(last_event_id) is None
|
||||
):
|
||||
@@ -255,6 +271,8 @@ async def stream_events(
|
||||
if not row_id or not isinstance(event, dict):
|
||||
continue
|
||||
cursor = row_id
|
||||
if not _is_target_run_event(event, target_run_id=run_id):
|
||||
continue
|
||||
yield to_sse_event(row_id, event)
|
||||
if _is_terminal_run_event(event):
|
||||
terminal_event_reached = True
|
||||
|
||||
@@ -77,7 +77,7 @@ def test_create_schedule_item_returns_201() -> None:
|
||||
source_type=ScheduleItemSourceType.MANUAL,
|
||||
created_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
permission=7,
|
||||
permission=15,
|
||||
is_owner=True,
|
||||
)
|
||||
|
||||
@@ -110,7 +110,7 @@ def test_list_schedule_items_returns_200() -> None:
|
||||
source_type=ScheduleItemSourceType.MANUAL,
|
||||
created_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
permission=7,
|
||||
permission=15,
|
||||
is_owner=True,
|
||||
)
|
||||
|
||||
@@ -145,7 +145,7 @@ def test_get_schedule_item_returns_200() -> None:
|
||||
source_type=ScheduleItemSourceType.MANUAL,
|
||||
created_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
permission=7,
|
||||
permission=15,
|
||||
is_owner=True,
|
||||
)
|
||||
|
||||
@@ -173,7 +173,7 @@ def test_update_schedule_item_returns_200() -> None:
|
||||
source_type=ScheduleItemSourceType.MANUAL,
|
||||
created_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
permission=7,
|
||||
permission=15,
|
||||
is_owner=True,
|
||||
)
|
||||
|
||||
@@ -204,7 +204,7 @@ def test_delete_schedule_item_returns_204() -> None:
|
||||
source_type=ScheduleItemSourceType.MANUAL,
|
||||
created_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
updated_at=datetime(2026, 2, 27, 10, 0, 0, tzinfo=timezone.utc),
|
||||
permission=7,
|
||||
permission=15,
|
||||
is_owner=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -154,6 +154,65 @@ class _TerminalStreamAgentService(_FakeAgentService):
|
||||
return []
|
||||
|
||||
|
||||
class _MixedRunStreamAgentService(_FakeAgentService):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.stream_calls = 0
|
||||
|
||||
async def stream_events(
|
||||
self,
|
||||
*,
|
||||
thread_id: str,
|
||||
last_event_id: str | None,
|
||||
current_user: CurrentUser,
|
||||
) -> list[dict[str, object]]:
|
||||
del thread_id, last_event_id, current_user
|
||||
self.stream_calls += 1
|
||||
if self.stream_calls == 1:
|
||||
return [
|
||||
{
|
||||
"id": "11-0",
|
||||
"event": {
|
||||
"type": "RUN_FINISHED",
|
||||
"threadId": "00000000-0000-0000-0000-000000000001",
|
||||
"runId": "run-old",
|
||||
},
|
||||
},
|
||||
{
|
||||
"id": "12-0",
|
||||
"event": {
|
||||
"type": "RUN_STARTED",
|
||||
"threadId": "00000000-0000-0000-0000-000000000001",
|
||||
"runId": "run-1",
|
||||
},
|
||||
},
|
||||
]
|
||||
if self.stream_calls == 2:
|
||||
return [
|
||||
{
|
||||
"id": "13-0",
|
||||
"event": {
|
||||
"type": "STEP_STARTED",
|
||||
"threadId": "00000000-0000-0000-0000-000000000001",
|
||||
"runId": "run-1",
|
||||
"stepName": "router",
|
||||
},
|
||||
}
|
||||
]
|
||||
if self.stream_calls == 3:
|
||||
return [
|
||||
{
|
||||
"id": "14-0",
|
||||
"event": {
|
||||
"type": "RUN_FINISHED",
|
||||
"threadId": "00000000-0000-0000-0000-000000000001",
|
||||
"runId": "run-1",
|
||||
},
|
||||
}
|
||||
]
|
||||
return []
|
||||
|
||||
|
||||
def test_run_requires_auth_and_returns_202_task_id() -> None:
|
||||
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
|
||||
client = TestClient(app)
|
||||
@@ -168,7 +227,7 @@ def test_run_requires_auth_and_returns_202_task_id() -> None:
|
||||
"messages": [{"id": "u1", "role": "user", "content": "hello"}],
|
||||
"tools": [],
|
||||
"context": [],
|
||||
"forwardedProps": {"agent_type": "worker"},
|
||||
"forwardedProps": {"runtime_mode": "chat"},
|
||||
},
|
||||
)
|
||||
assert unauthorized.status_code == 401
|
||||
@@ -185,7 +244,7 @@ def test_run_requires_auth_and_returns_202_task_id() -> None:
|
||||
"messages": [{"id": "u1", "role": "user", "content": "hello"}],
|
||||
"tools": [],
|
||||
"context": [],
|
||||
"forwardedProps": {"agent_type": "worker"},
|
||||
"forwardedProps": {"runtime_mode": "chat"},
|
||||
},
|
||||
)
|
||||
assert authorized.status_code == 202
|
||||
@@ -219,7 +278,7 @@ def test_stream_reads_from_last_event_id() -> None:
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?idle_limit=1",
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1&idle_limit=1",
|
||||
headers={"Last-Event-ID": "1-0"},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
@@ -255,7 +314,7 @@ def test_stream_handles_stream_backend_errors_without_connection_crash() -> None
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?idle_limit=1"
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1&idle_limit=1"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("text/event-stream")
|
||||
@@ -288,7 +347,7 @@ def test_stream_stops_after_terminal_run_event() -> None:
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?idle_limit=3"
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1&idle_limit=3"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("text/event-stream")
|
||||
@@ -309,7 +368,7 @@ def test_stream_rejects_invalid_last_event_id() -> None:
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events",
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1",
|
||||
headers={"Last-Event-ID": "bad-id"},
|
||||
)
|
||||
assert response.status_code == 422
|
||||
@@ -320,6 +379,68 @@ def test_stream_rejects_invalid_last_event_id() -> None:
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_stream_filters_non_target_run_and_waits_target_terminal() -> None:
|
||||
service = _MixedRunStreamAgentService()
|
||||
app.dependency_overrides[get_agent_service] = lambda: service
|
||||
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
|
||||
id=uuid4(), phone="+8613812345678"
|
||||
)
|
||||
client = TestClient(app)
|
||||
original_acquire = agent_router._acquire_sse_slot
|
||||
original_release = agent_router._release_sse_slot
|
||||
|
||||
async def _allow_slot(*, user_id: str) -> bool:
|
||||
del user_id
|
||||
return True
|
||||
|
||||
async def _noop_release(*, user_id: str) -> None:
|
||||
del user_id
|
||||
return None
|
||||
|
||||
agent_router._acquire_sse_slot = _allow_slot # type: ignore[assignment]
|
||||
agent_router._release_sse_slot = _noop_release # type: ignore[assignment]
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1&idle_limit=3"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["content-type"].startswith("text/event-stream")
|
||||
assert '"runId":"run-old"' not in response.text
|
||||
assert '"runId":"run-1"' in response.text
|
||||
assert "event: RUN_STARTED" in response.text
|
||||
assert "event: STEP_STARTED" in response.text
|
||||
assert "event: RUN_FINISHED" in response.text
|
||||
assert service.stream_calls == 3
|
||||
finally:
|
||||
agent_router._acquire_sse_slot = original_acquire # type: ignore[assignment]
|
||||
agent_router._release_sse_slot = original_release # type: ignore[assignment]
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_stream_rejects_invalid_or_missing_run_id() -> None:
|
||||
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
|
||||
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
|
||||
id=uuid4(), phone="+8613812345678"
|
||||
)
|
||||
client = TestClient(app)
|
||||
|
||||
try:
|
||||
invalid = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=bad%20id"
|
||||
)
|
||||
assert invalid.status_code == 422
|
||||
assert invalid.json()["code"] == "AGENT_INVALID_RUN_ID"
|
||||
|
||||
missing = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events"
|
||||
)
|
||||
assert missing.status_code == 422
|
||||
assert missing.json()["code"] == "AGENT_INVALID_RUN_ID"
|
||||
finally:
|
||||
app.dependency_overrides = {}
|
||||
|
||||
|
||||
def test_stream_rejects_when_sse_connection_limit_exceeded() -> None:
|
||||
app.dependency_overrides[get_agent_service] = lambda: _FakeAgentService()
|
||||
app.dependency_overrides[get_current_user] = lambda: CurrentUser(
|
||||
@@ -336,7 +457,7 @@ def test_stream_rejects_when_sse_connection_limit_exceeded() -> None:
|
||||
|
||||
try:
|
||||
response = client.get(
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events"
|
||||
"/api/v1/agent/runs/00000000-0000-0000-0000-000000000001/events?runId=run-1"
|
||||
)
|
||||
assert response.status_code == 429
|
||||
payload = response.json()
|
||||
@@ -587,7 +708,7 @@ def test_asr_transcribe_returns_sync_transcript(monkeypatch) -> None:
|
||||
return "这是测试转写结果"
|
||||
|
||||
monkeypatch.setattr(
|
||||
"v1.agent.service.asr_service.transcribe_file",
|
||||
"v1.agent.router.asr_service.transcribe_file",
|
||||
mock_transcribe_file,
|
||||
)
|
||||
|
||||
|
||||
@@ -12,10 +12,8 @@ from schemas.agent.runtime_models import (
|
||||
ResultType,
|
||||
ResultTyping,
|
||||
RouterAgentOutput,
|
||||
RouterUiDecision,
|
||||
TaskType,
|
||||
TaskTyping,
|
||||
UiMode,
|
||||
WorkerAgentOutputLite,
|
||||
)
|
||||
from schemas.agent.system_agent import AgentType
|
||||
@@ -65,10 +63,6 @@ def test_build_worker_input_messages_only_contains_router_contract() -> None:
|
||||
task_typing=TaskTyping(primary=TaskType.SCHEDULING),
|
||||
execution_mode=ExecutionMode.TOOL_ASSISTED,
|
||||
result_typing=ResultTyping(primary=ResultType.EXECUTION_REPORT),
|
||||
ui=RouterUiDecision(
|
||||
ui_mode=UiMode.NONE,
|
||||
ui_decision_reason="单一执行任务,文本输出足够",
|
||||
),
|
||||
)
|
||||
|
||||
input_messages = runner._build_worker_input_messages(router_output=router_output)
|
||||
@@ -239,10 +233,6 @@ async def test_execute_runs_router_then_worker(
|
||||
task_typing=TaskTyping(primary=TaskType.SCHEDULING),
|
||||
execution_mode=ExecutionMode.TOOL_ASSISTED,
|
||||
result_typing=ResultTyping(primary=ResultType.EXECUTION_REPORT),
|
||||
ui=RouterUiDecision(
|
||||
ui_mode=UiMode.NONE,
|
||||
ui_decision_reason="单任务",
|
||||
),
|
||||
)
|
||||
|
||||
async def _fake_execute_worker_step(**kwargs: object) -> WorkerAgentOutputLite:
|
||||
@@ -308,10 +298,6 @@ async def test_execute_raises_cancelled_error_before_worker_when_cancel_requeste
|
||||
task_typing=TaskTyping(primary=TaskType.SCHEDULING),
|
||||
execution_mode=ExecutionMode.TOOL_ASSISTED,
|
||||
result_typing=ResultTyping(primary=ResultType.EXECUTION_REPORT),
|
||||
ui=RouterUiDecision(
|
||||
ui_mode=UiMode.NONE,
|
||||
ui_decision_reason="单任务",
|
||||
),
|
||||
)
|
||||
|
||||
async def _fake_execute_worker_step(**kwargs: object) -> WorkerAgentOutputLite:
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from core.agentscope.runtime.ui_compiler import compile as compile_ui
|
||||
from schemas.agent.ui_schema import UiStatus, build_status_panel
|
||||
from schemas.agent.ui_hints import UiHintsPayload
|
||||
|
||||
|
||||
def _collect_badge_labels(node: Any) -> list[str]:
|
||||
labels: list[str] = []
|
||||
if isinstance(node, dict):
|
||||
if node.get("type") == "badge" and isinstance(node.get("label"), str):
|
||||
labels.append(node["label"])
|
||||
for value in node.values():
|
||||
labels.extend(_collect_badge_labels(value))
|
||||
elif isinstance(node, list):
|
||||
for item in node:
|
||||
labels.extend(_collect_badge_labels(item))
|
||||
return labels
|
||||
|
||||
|
||||
def test_compile_status_badge_uses_stable_status_token() -> None:
|
||||
hints = UiHintsPayload.model_validate(
|
||||
{
|
||||
"intent": "status",
|
||||
"status": "success",
|
||||
"title": "日程已创建",
|
||||
"body": "已为您创建提醒。",
|
||||
}
|
||||
)
|
||||
|
||||
schema = compile_ui(hints)
|
||||
badge_labels = _collect_badge_labels(schema)
|
||||
|
||||
assert "ui.status.success" in badge_labels
|
||||
|
||||
|
||||
def test_compile_status_badge_does_not_emit_uppercase_legacy_label() -> None:
|
||||
hints = UiHintsPayload.model_validate(
|
||||
{
|
||||
"intent": "status",
|
||||
"status": "error",
|
||||
"title": "创建失败",
|
||||
"body": "请稍后重试。",
|
||||
}
|
||||
)
|
||||
|
||||
schema = compile_ui(hints)
|
||||
badge_labels = _collect_badge_labels(schema)
|
||||
|
||||
assert "ERROR" not in badge_labels
|
||||
assert "ui.status.error" in badge_labels
|
||||
|
||||
|
||||
def test_build_status_panel_uses_stable_status_token_label() -> None:
|
||||
panel = build_status_panel(
|
||||
title="已创建",
|
||||
message="提醒创建成功",
|
||||
status=UiStatus.SUCCESS,
|
||||
)
|
||||
|
||||
badge_labels = _collect_badge_labels(panel)
|
||||
assert "ui.status.success" in badge_labels
|
||||
@@ -27,10 +27,6 @@ def test_router_agent_output_coerces_key_entity_value_to_string() -> None:
|
||||
"primary": "summary",
|
||||
"secondary": [],
|
||||
},
|
||||
"ui": {
|
||||
"ui_mode": "none",
|
||||
"ui_decision_reason": "test",
|
||||
},
|
||||
}
|
||||
|
||||
model = RouterAgentOutput.model_validate(payload)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
from v1.agent.router import _is_target_run_event, _is_terminal_run_event
|
||||
|
||||
|
||||
def test_is_target_run_event_matches_expected_run_id() -> None:
|
||||
event: dict[str, object] = {"type": "STEP_STARTED", "runId": "run_123"}
|
||||
assert _is_target_run_event(event, target_run_id="run_123") is True
|
||||
|
||||
|
||||
def test_is_target_run_event_rejects_other_run_id() -> None:
|
||||
event: dict[str, object] = {"type": "STEP_STARTED", "runId": "run_999"}
|
||||
assert _is_target_run_event(event, target_run_id="run_123") is False
|
||||
|
||||
|
||||
def test_is_target_run_event_rejects_missing_run_id() -> None:
|
||||
event: dict[str, object] = {"type": "STEP_STARTED"}
|
||||
assert _is_target_run_event(event, target_run_id="run_123") is False
|
||||
|
||||
|
||||
def test_is_terminal_run_event_only_accepts_terminal_types() -> None:
|
||||
assert _is_terminal_run_event({"type": "RUN_FINISHED"}) is True
|
||||
assert _is_terminal_run_event({"type": "RUN_ERROR"}) is True
|
||||
assert _is_terminal_run_event({"type": "STEP_STARTED"}) is False
|
||||
Reference in New Issue
Block a user