refactor: 重构 Agent 模块为 AgentScope,删除旧版 CrewAI/LiteLLM 实现
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from core.agentscope.schemas.agui_input import (
|
||||
MAX_MESSAGES,
|
||||
MAX_RUN_ID_LENGTH,
|
||||
MAX_RUN_INPUT_BYTES,
|
||||
MAX_TEXT_CHARS,
|
||||
extract_latest_tool_result,
|
||||
parse_run_input,
|
||||
validate_run_request_messages_contract,
|
||||
)
|
||||
|
||||
|
||||
def _base_payload() -> dict[str, object]:
|
||||
return {
|
||||
"threadId": "00000000-0000-0000-0000-000000000001",
|
||||
"runId": "run-1",
|
||||
"state": {},
|
||||
"messages": [{"id": "u1", "role": "user", "content": "hello"}],
|
||||
"tools": [],
|
||||
"context": [],
|
||||
"forwardedProps": {},
|
||||
}
|
||||
|
||||
|
||||
def test_parse_run_input_rejects_invalid_uuid() -> None:
|
||||
payload = _base_payload()
|
||||
payload["threadId"] = "bad-uuid"
|
||||
|
||||
with pytest.raises(ValueError, match="threadId must be a valid UUID"):
|
||||
parse_run_input(payload)
|
||||
|
||||
|
||||
def test_parse_run_input_rejects_message_count_over_limit() -> None:
|
||||
payload = _base_payload()
|
||||
payload["messages"] = [
|
||||
{"id": f"u{i}", "role": "user", "content": "x"} for i in range(MAX_MESSAGES + 1)
|
||||
]
|
||||
|
||||
with pytest.raises(ValueError, match="RunAgentInput.messages exceeds limit"):
|
||||
parse_run_input(payload)
|
||||
|
||||
|
||||
def test_parse_run_input_rejects_user_text_over_limit() -> None:
|
||||
payload = _base_payload()
|
||||
payload["messages"] = [
|
||||
{"id": "u1", "role": "user", "content": "x" * (MAX_TEXT_CHARS + 1)}
|
||||
]
|
||||
|
||||
with pytest.raises(
|
||||
ValueError, match="RunAgentInput user message text exceeds limit"
|
||||
):
|
||||
parse_run_input(payload)
|
||||
|
||||
|
||||
def test_parse_run_input_rejects_payload_over_limit() -> None:
|
||||
payload = _base_payload()
|
||||
payload["forwardedProps"] = {"blob": "x" * MAX_RUN_INPUT_BYTES}
|
||||
|
||||
with pytest.raises(ValueError, match="RunAgentInput payload exceeds size limit"):
|
||||
parse_run_input(payload)
|
||||
|
||||
|
||||
def test_parse_run_input_rejects_run_id_over_limit() -> None:
|
||||
payload = _base_payload()
|
||||
payload["runId"] = "r" * (MAX_RUN_ID_LENGTH + 1)
|
||||
|
||||
with pytest.raises(ValueError, match="runId exceeds length limit"):
|
||||
parse_run_input(payload)
|
||||
|
||||
|
||||
def test_extract_latest_tool_result_requires_tool_call_id() -> None:
|
||||
run_input = parse_run_input(_base_payload())
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="RunAgentInput.messages requires a tool message with toolCallId for resume",
|
||||
):
|
||||
extract_latest_tool_result(run_input)
|
||||
|
||||
|
||||
def test_validate_run_request_messages_contract_requires_single_user_message() -> None:
|
||||
payload = _base_payload()
|
||||
payload["messages"] = [
|
||||
{"id": "u1", "role": "user", "content": "hello"},
|
||||
{"id": "u2", "role": "user", "content": "again"},
|
||||
]
|
||||
run_input = parse_run_input(payload)
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="RunAgentInput.messages must contain exactly one user message",
|
||||
):
|
||||
validate_run_request_messages_contract(run_input)
|
||||
Reference in New Issue
Block a user