优化目录结构

This commit is contained in:
2026-07-03 20:49:36 +08:00
parent 04fefcf488
commit 8073dd63a7
10 changed files with 220 additions and 238 deletions
+31 -13
View File
@@ -58,20 +58,26 @@ def test_agent_executes_tool_and_persists_full_history(tmp_path: Path) -> None:
[
completion(
content=None,
tool_calls=[tool_call("call-1", "calculate", '{"expression":"2+2"}')],
tool_calls=[
tool_call(
"call-1",
"get_current_time",
'{"timezone":"UTC"}',
)
],
),
completion(content="结果是 4", prompt_tokens=20, completion_tokens=3),
completion(content="当前时间已查询", prompt_tokens=20, completion_tokens=3),
]
)
storage = JsonSessionStorage(tmp_path)
session = await storage.create("Use tools when needed.")
service = ChatService(storage, provider, "test-model") # type: ignore[arg-type]
response = await service.generate_response(session.session_id, "2+2 等于多少")
response = await service.generate_response(session.session_id, "现在几点")
assert response.message.content == "结果是 4"
assert response.message.content == "当前时间已查询"
assert "tool_calls" not in response.message.model_dump()
assert response.tools_use == ["calculate"]
assert response.tools_use == ["get_current_time"]
assert response.usage is not None
assert response.usage.model_dump() == {
"prompt_tokens": 30,
@@ -86,7 +92,7 @@ def test_agent_executes_tool_and_persists_full_history(tmp_path: Path) -> None:
"assistant",
"tool",
]
assert json.loads(second_messages[-1]["content"])["result"] == 4
assert json.loads(second_messages[-1]["content"])["timezone"] == "UTC"
saved = await storage.read(session.session_id)
assert [message.role for message in saved.messages] == [
@@ -108,11 +114,15 @@ def test_agent_executes_multiple_tools_in_order(tmp_path: Path) -> None:
completion(
content=None,
tool_calls=[
tool_call("call-1", "calculate", '{"expression":"6*7"}'),
tool_call(
"call-1",
"get_current_time",
'{"timezone":"UTC"}',
),
tool_call(
"call-2",
"get_current_time",
'{"timezone":"UTC"}',
'{"timezone":"Asia/Shanghai"}',
),
],
),
@@ -130,7 +140,7 @@ def test_agent_executes_multiple_tools_in_order(tmp_path: Path) -> None:
"call-1",
"call-2",
]
assert response.tools_use == ["calculate", "get_current_time"]
assert response.tools_use == ["get_current_time"]
asyncio.run(scenario())
@@ -141,7 +151,7 @@ def test_tool_error_is_returned_to_model(tmp_path: Path) -> None:
[
completion(
content=None,
tool_calls=[tool_call("bad-1", "calculate", "not-json")],
tool_calls=[tool_call("bad-1", "get_current_time", "not-json")],
),
completion(content="参数无效。"),
]
@@ -164,13 +174,21 @@ def test_tool_error_is_returned_to_model(tmp_path: Path) -> None:
@pytest.mark.parametrize(
("max_rounds", "max_calls", "calls"),
[
(1, 10, [tool_call("call-1", "calculate", '{"expression":"1+1"}')]),
(
1,
10,
[tool_call("call-1", "get_current_time", '{"timezone":"UTC"}')],
),
(
5,
1,
[
tool_call("call-1", "calculate", '{"expression":"1+1"}'),
tool_call("call-2", "calculate", '{"expression":"2+2"}'),
tool_call("call-1", "get_current_time", '{"timezone":"UTC"}'),
tool_call(
"call-2",
"get_current_time",
'{"timezone":"Asia/Shanghai"}',
),
],
),
],