File size: 15,525 Bytes
f7359ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | import unittest
import json
import asyncio
from unittest.mock import patch, MagicMock
from app import (
parse_tool_definitions,
build_tool_system_prompt,
prepare_messages_for_tools,
extract_tool_calls,
normalize_upstream_messages,
build_upstream_payload,
app,
)
from fastapi.testclient import TestClient
client = TestClient(app)
class TestToolEmulation(unittest.TestCase):
def setUp(self):
self.sample_tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather in city",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Calculate math expression",
"parameters": {
"type": "object",
"properties": {
"expr": {"type": "string"}
},
"required": ["expr"]
}
}
}
]
def test_parse_tool_definitions(self):
tool_defs, valid_names = parse_tool_definitions(self.sample_tools)
self.assertEqual(valid_names, ["get_weather", "calculate"])
self.assertEqual(len(tool_defs), 2)
# Legacy functions support
legacy_functions = [{"name": "search", "description": "web search"}]
tool_defs2, valid_names2 = parse_tool_definitions(None, functions=legacy_functions)
self.assertEqual(valid_names2, ["search"])
def test_build_tool_system_prompt(self):
tool_defs, _ = parse_tool_definitions(self.sample_tools)
prompt = build_tool_system_prompt(tool_defs, tool_choice="auto")
self.assertIn("## Tools", prompt)
self.assertIn("get_weather", prompt)
self.assertIn("```json", prompt)
# Required choice
req_prompt = build_tool_system_prompt(tool_defs, tool_choice="required")
self.assertIn("REQUIRED to call at least one", req_prompt)
# Specific function choice
fn_prompt = build_tool_system_prompt(tool_defs, tool_choice={"type": "function", "function": {"name": "calculate"}})
self.assertIn("REQUIRED to call the function 'calculate'", fn_prompt)
def test_prepare_messages_for_tools(self):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What's the weather in Tokyo?"},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"location\":\"Tokyo\"}"}
}
]
},
{"role": "tool", "tool_call_id": "call_123", "name": "get_weather", "content": "{\"temp\": 22}"}
]
tool_prompt = "TOOL_PROMPT_MOCK"
transformed = prepare_messages_for_tools(messages, tool_prompt=tool_prompt)
self.assertEqual(len(transformed), 5)
self.assertEqual(transformed[-1]["role"], "system")
self.assertEqual(transformed[-1]["content"], "TOOL_PROMPT_MOCK")
def test_normalize_upstream_messages(self):
messages = [
{"role": "system", "content": "You are a coding agent."},
{"role": "user", "content": "Write a python script."},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_abc",
"type": "function",
"function": {"name": "bash", "arguments": "{\"cmd\": \"ls\"}"}
}
]
},
{"role": "tool", "tool_call_id": "call_abc", "content": "file1.txt\nfile2.txt"},
{"role": "system", "content": "Additional tool schema"}
]
norm = normalize_upstream_messages(messages)
# 1. No system role allowed in upstream
roles = [m["role"] for m in norm]
self.assertNotIn("system", roles)
self.assertNotIn("tool", roles)
# 2. System prompt prepended to the first user message
first_user = norm[0]
self.assertEqual(first_user["role"], "user")
self.assertIn("[System Instructions]", first_user["content"])
self.assertIn("You are a coding agent.", first_user["content"])
self.assertIn("Additional tool schema", first_user["content"])
self.assertIn("Write a python script.", first_user["content"])
# 3. Assistant tool calls formatted as JSON block for consistent in-context learning
self.assertIn('"name": "bash"', norm[1]["content"])
# 4. Tool result flattened to user
self.assertEqual(norm[2]["role"], "user")
self.assertIn("[Tool Execution Result for `call_abc`]:", norm[2]["content"])
# 5. Last message must be user
self.assertEqual(norm[-1]["role"], "user")
def test_build_upstream_payload_message_id_binding(self):
messages = [
{"role": "system", "content": "sys prompt"},
{"role": "user", "content": "hello"}
]
payload = build_upstream_payload(messages, reasoning_effort="high", web_search=True)
self.assertEqual(payload["reasoningEffort"], "high")
self.assertTrue(payload["webSearchEnabled"])
self.assertEqual(len(payload["messages"]), 1)
self.assertEqual(payload["messages"][0]["role"], "user")
# messageId must strictly equal the id of the last message in messages array
last_id = payload["messages"][-1]["id"]
self.assertEqual(payload["messageId"], last_id)
def test_extract_tool_calls_markdown(self):
text = """I will check the weather for you.
```json
{
"name": "get_weather",
"arguments": {
"location": "Tokyo"
}
}
```
"""
tool_calls, remaining = extract_tool_calls(text, ["get_weather", "calculate"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 1)
self.assertEqual(tool_calls[0]["function"]["name"], "get_weather")
args = json.loads(tool_calls[0]["function"]["arguments"])
self.assertEqual(args.get("location"), "Tokyo")
self.assertEqual(remaining, "I will check the weather for you.")
def test_extract_tool_calls_raw_json(self):
text = '{"name": "calculate", "arguments": {"expr": "12 * 8"}}'
tool_calls, remaining = extract_tool_calls(text, ["get_weather", "calculate"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 1)
self.assertEqual(tool_calls[0]["function"]["name"], "calculate")
self.assertEqual(remaining, "")
def test_extract_tool_calls_multiple(self):
text = """```json
[
{"name": "get_weather", "arguments": {"location": "Beijing"}},
{"name": "get_weather", "arguments": {"location": "Shanghai"}}
]
```"""
tool_calls, remaining = extract_tool_calls(text, ["get_weather"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 2)
self.assertEqual(tool_calls[0]["function"]["name"], "get_weather")
self.assertEqual(tool_calls[1]["function"]["name"], "get_weather")
def test_extract_tool_calls_user_reported_case(self):
# Exact reproduction of user issue: natural calling text + prefix
text = """✅ `todo` 正常(#1-#6 已创建)。
开始任务 #1,标记为进行中:
Calling tool `todo` with arguments: {"action":"update","id":1,"status":"in_progress","activeForm":"测试基础工具"}"""
tool_calls, remaining = extract_tool_calls(text, ["todo", "bash"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 1)
self.assertEqual(tool_calls[0]["function"]["name"], "todo")
args = json.loads(tool_calls[0]["function"]["arguments"])
self.assertEqual(args["action"], "update")
self.assertEqual(args["id"], 1)
self.assertEqual(args["status"], "in_progress")
self.assertIn("✅ `todo` 正常", remaining)
self.assertIn("开始任务 #1,标记为进行中:", remaining)
def test_extract_tool_calls_action_format(self):
text = """Action: bash
Action Input: {"command": "ls -la"}"""
tool_calls, remaining = extract_tool_calls(text, ["bash"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 1)
self.assertEqual(tool_calls[0]["function"]["name"], "bash")
args = json.loads(tool_calls[0]["function"]["arguments"])
self.assertEqual(args["command"], "ls -la")
def test_extract_tool_calls_multiple_independent_blocks(self):
text = """准备查看文件列表:
```json
{"name": "bash", "arguments": {"command": "ls"}}
```
同时查看当前路径:
```json
{"name": "bash", "arguments": {"command": "pwd"}}
```
操作准备完毕。"""
tool_calls, remaining = extract_tool_calls(text, ["bash"])
self.assertIsNotNone(tool_calls)
self.assertEqual(len(tool_calls), 2)
self.assertEqual(json.loads(tool_calls[0]["function"]["arguments"])["command"], "ls")
self.assertEqual(json.loads(tool_calls[1]["function"]["arguments"])["command"], "pwd")
self.assertIn("准备查看文件列表:", remaining)
self.assertIn("同时查看当前路径:", remaining)
self.assertIn("操作准备完毕。", remaining)
def test_compact_tool_definitions(self):
tools = [
{
"type": "function",
"function": {
"name": f"tool_{i}",
"description": "Very long description " * 20,
"parameters": {
"type": "object",
"properties": {
f"arg_{j}": {"type": "string", "description": "arg desc " * 20}
for j in range(5)
}
}
}
}
for i in range(70)
]
tool_defs, valid_names = parse_tool_definitions(tools)
prompt = build_tool_system_prompt(tool_defs)
self.assertLess(len(prompt), 35000)
def test_extract_tool_calls_no_match(self):
text = "Hello! Tokyo is a great city. No tools needed."
tool_calls, remaining = extract_tool_calls(text, ["get_weather"])
self.assertIsNone(tool_calls)
self.assertEqual(remaining, text)
def test_extract_tool_calls_unknown_function(self):
text = '```json\n{"name": "unknown_tool", "arguments": {}}\n```'
tool_calls, remaining = extract_tool_calls(text, ["get_weather"])
self.assertIsNone(tool_calls)
class TestChatCompletionEndpointWithTools(unittest.TestCase):
@patch("app.engine.chat_events")
def test_chat_completions_non_stream_tool_call(self, mock_chat_events):
async def fake_events(*args, **kwargs):
yield ("reasoning", "Thinking about what tool to use...")
yield ("text", '```json\n{"name": "get_weather", "arguments": {"location": "Paris"}}\n```')
mock_chat_events.side_effect = fake_events
req_payload = {
"model": "mercury-3",
"messages": [{"role": "user", "content": "What is the weather in Paris?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather",
"parameters": {"type": "object", "properties": {"location": {"type": "string"}}}
}
}
],
"stream": False
}
response = client.post("/v1/chat/completions", json=req_payload)
self.assertEqual(response.status_code, 200)
data = response.json()
choice = data["choices"][0]
self.assertEqual(choice["finish_reason"], "tool_calls")
self.assertIn("tool_calls", choice["message"])
self.assertEqual(choice["message"]["tool_calls"][0]["function"]["name"], "get_weather")
self.assertIn("reasoning_content", choice["message"])
self.assertEqual(choice["message"]["reasoning_content"], "Thinking about what tool to use...")
@patch("app.engine.chat_events")
def test_chat_completions_stream_tool_call(self, mock_chat_events):
async def fake_events(*args, **kwargs):
yield ("reasoning", "Thinking...")
yield ("text", '```json\n{"name": "get_weather", "arguments": {"location": "London"}}\n```')
mock_chat_events.side_effect = fake_events
req_payload = {
"model": "mercury-3",
"messages": [{"role": "user", "content": "What is the weather in London?"}],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather",
"parameters": {"type": "object", "properties": {"location": {"type": "string"}}}
}
}
],
"stream": True
}
response = client.post("/v1/chat/completions", json=req_payload)
self.assertEqual(response.status_code, 200)
chunks = []
for line in response.iter_lines():
if line.startswith("data: ") and line != "data: [DONE]":
chunks.append(json.loads(line[6:]))
# Verify stream chunk formats
self.assertTrue(any("reasoning_content" in c["choices"][0]["delta"] for c in chunks))
tool_call_chunks = [c for c in chunks if "tool_calls" in c["choices"][0]["delta"]]
self.assertTrue(len(tool_call_chunks) > 0)
self.assertEqual(tool_call_chunks[0]["choices"][0]["delta"]["tool_calls"][0]["function"]["name"], "get_weather")
end_chunks = [c for c in chunks if c["choices"][0].get("finish_reason") == "tool_calls"]
self.assertEqual(len(end_chunks), 1)
@patch("app.engine.chat_events")
def test_chat_completions_normal_chat_without_tools(self, mock_chat_events):
async def fake_events(*args, **kwargs):
yield ("text", "Hello there!")
mock_chat_events.side_effect = fake_events
req_payload = {
"model": "mercury-3",
"messages": [{"role": "user", "content": "Hi"}],
"stream": False
}
response = client.post("/v1/chat/completions", json=req_payload)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data["choices"][0]["finish_reason"], "stop")
self.assertEqual(data["choices"][0]["message"]["content"], "Hello there!")
self.assertNotIn("tool_calls", data["choices"][0]["message"])
if __name__ == "__main__":
unittest.main()
|