# -*- coding: utf-8 -*- """系统提示词分层装配 - 端到端测试""" import sys, os, io, json, requests sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") BASE = "http://localhost:8038/api/v1" def login(): r = requests.post(f"{BASE}/auth/login", data={"username": "admin", "password": "123456"}) r.raise_for_status() return r.json()["access_token"] def test_prompt_sections(): token = login() headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"} print("=== 系统提示词分层装配测试 ===\n") # Test 1: Chat with prompt sections enabled (default) print("1. 对话(分层装配 开启)…") r1 = requests.post(f"{BASE}/agent-chat/bare", headers=headers, json={"message": "你好,请用简短回答介绍一下自己", "prompt_sections_enabled": True}, timeout=120) r1.raise_for_status() d1 = r1.json() print(f" 会话 ID: {d1['session_id']}") print(f" 回复: {d1['content'][:200]}…") print(f" 迭代: {d1['iterations_used']}, 工具调用: {d1['tool_calls_made']}") ok1 = d1["iterations_used"] >= 1 print(f" {'✓ 通过' if ok1 else '✗ 失败'}\n") # Test 2: Chat with prompt sections disabled print("2. 对话(分层装配 关闭)…") r2 = requests.post(f"{BASE}/agent-chat/bare", headers=headers, json={"message": "你好,请用简短回答介绍一下自己", "prompt_sections_enabled": False}, timeout=120) r2.raise_for_status() d2 = r2.json() print(f" 会话 ID: {d2['session_id']}") print(f" 回复: {d2['content'][:200]}…") print(f" 迭代: {d2['iterations_used']}, 工具调用: {d2['tool_calls_made']}") ok2 = d2["iterations_used"] >= 1 print(f" {'✓ 通过' if ok2 else '✗ 失败'}\n") # Test 3: Agent chat with prompt sections print("3. Agent 对话(分层装配 开启)…") # First find an agent r_agents = requests.get(f"{BASE}/agents", headers=headers, params={"limit": 5}, timeout=10) r_agents.raise_for_status() agents_data = r_agents.json() agents = agents_data if isinstance(agents_data, list) else agents_data.get("items", []) if agents: agent_id = agents[0]["id"] r3 = requests.post(f"{BASE}/agent-chat/{agent_id}", headers=headers, json={"message": "你好", "prompt_sections_enabled": True}, timeout=120) r3.raise_for_status() d3 = r3.json() print(f" Agent: {agents[0].get('name', 'N/A')}") print(f" 回复: {d3['content'][:200]}…") ok3 = d3["iterations_used"] >= 1 print(f" {'✓ 通过' if ok3 else '✗ 失败'}") else: print(" ⚠ 跳过(无可用 Agent)") ok3 = True print() # Test 4: Verify system prompt composition via health endpoint print("4. 验证分层装配模块加载…") r4 = requests.get(f"http://localhost:8038/health", timeout=10) r4.raise_for_status() d4 = r4.json() print(f" 状态: {d4['status']}") print(f" 工具数: {d4.get('builtin_tools', {}).get('count', 'N/A')}") ok4 = d4["status"] == "healthy" print(f" {'✓ 通过' if ok4 else '✗ 失败'}\n") all_ok = ok1 and ok2 and ok3 and ok4 print(f"=== {'全部测试通过' if all_ok else '部分测试失败'} ===") return all_ok if __name__ == "__main__": test_prompt_sections()