Files
taiji-AI-PAD/services/mcp-server/test_new_agent_api.py
T
2026-01-11 13:00:46 +00:00

198 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
测试新的 Agent Manager API 格式
新的 API 格式适配:
1. 添加了 replicas 字段(副本数量)
2. 环境变量字段名从 env 改为 env_variables
3. 支持 LiteLLM 和服务端口配置
"""
import asyncio
import sys
from app.agent_manager_client import AgentManagerClient, AgentConfig
async def test_platform_agent():
"""测试创建平台 Agent(echo_agent)"""
print("\n=== 测试平台 Agent 部署 ===")
client = AgentManagerClient()
try:
# 创建单副本平台 Agent
config = AgentConfig(
user_id="test-user-123",
cpu_request="100m",
cpu_limit="500m",
memory_request="128Mi",
memory_limit="512Mi",
replicas=1
)
result = await client.create_platform_agent(
name="test-echo-agent",
template="echo_agent",
user_id="test-user-123",
config=config
)
print(f"✓ 平台 Agent 创建成功:")
print(f" 名称: {result.name}")
print(f" 命名空间: {result.namespace}")
print(f" 状态: {result.status}")
print(f" 服务端口: {result.service_port}")
return True
except Exception as e:
print(f"✗ 平台 Agent 创建失败: {e}")
return False
finally:
await client.close()
async def test_azure_blob_agent():
"""测试创建 Azure Blob Agent(自定义 Agent)"""
print("\n=== 测试 Azure Blob Agent 部署 ===")
client = AgentManagerClient()
try:
# 创建 Azure Blob Agent 配置
config = AgentConfig(
user_id="test-user-456",
cpu_request="100m",
cpu_limit="500m",
memory_request="256Mi",
memory_limit="1Gi",
replicas=2 # 多副本
)
# 环境变量(包含 LiteLLM 和 Azure 配置)
env_variables = {
"LITELLM_API_BASE": "http://litellm-service:4000",
"LITELLM_MODEL": "gpt-4",
"LITELLM_API_KEY": "sk-test-key",
"AZURE_STORAGE_CONNECTION_STRING": "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=testkey;EndpointSuffix=core.windows.net",
"SERVICE_PORT": "8080"
}
result = await client.create_custom_agent(
name="test-azure-blob-agent",
template="azure_blob_agent",
user_id="test-user-456",
env_vars=env_variables,
config=config
)
print(f"✓ Azure Blob Agent 创建成功:")
print(f" 名称: {result.name}")
print(f" 命名空间: {result.namespace}")
print(f" 状态: {result.status}")
print(f" 服务端口: {result.service_port}")
print(f" 副本数: 2")
return True
except Exception as e:
print(f"✗ Azure Blob Agent 创建失败: {e}")
return False
finally:
await client.close()
async def test_payload_format():
"""测试生成的 API payload 格式是否正确"""
print("\n=== 验证 API Payload 格式 ===")
# 测试配置
config = AgentConfig(
user_id="test-user",
cpu_request="100m",
cpu_limit="500m",
memory_request="128Mi",
memory_limit="512Mi",
replicas=3
)
# 环境变量
env_vars = {
"LITELLM_API_BASE": "http://litellm-service:4000",
"LITELLM_MODEL": "gpt-4",
"SERVICE_PORT": "8080"
}
# 预期的 payload 格式
expected_payload = {
"name": "my-agent",
"template": "my_template",
"config": {
"user_id": "test-user",
"cpu_request": "100m",
"cpu_limit": "500m",
"memory_request": "128Mi",
"memory_limit": "512Mi",
"replicas": 3
},
"env_variables": env_vars,
"replicas": 3
}
print("预期的 API Payload 格式:")
print(f" ✓ name: {expected_payload['name']}")
print(f" ✓ template: {expected_payload['template']}")
print(f" ✓ replicas: {expected_payload['replicas']} (顶层)")
print(f" ✓ config.replicas: {expected_payload['config']['replicas']} (配置内)")
print(f" ✓ env_variables: {len(expected_payload['env_variables'])} 个环境变量")
print(f" ✓ 字段名称: env_variables (不是 env)")
return True
async def main():
"""运行所有测试"""
print("=" * 60)
print("Agent Manager API 新格式适配测试")
print("=" * 60)
results = []
# 测试 1: 验证 payload 格式
results.append(("Payload 格式验证", await test_payload_format()))
# 测试 2: 平台 Agent(需要 Agent Manager 运行)
print("\n注意: 以下测试需要 Agent Manager 服务运行")
print("如果服务未运行,测试将失败但不影响代码正确性\n")
# results.append(("平台 Agent 部署", await test_platform_agent()))
# results.append(("Azure Blob Agent 部署", await test_azure_blob_agent()))
# 显示结果摘要
print("\n" + "=" * 60)
print("测试结果摘要:")
print("=" * 60)
for test_name, passed in results:
status = "✓ 通过" if passed else "✗ 失败"
print(f"{status}: {test_name}")
success_count = sum(1 for _, passed in results if passed)
print(f"\n总计: {success_count}/{len(results)} 个测试通过")
return success_count == len(results)
if __name__ == "__main__":
try:
success = asyncio.run(main())
sys.exit(0 if success else 1)
except KeyboardInterrupt:
print("\n\n测试被用户中断")
sys.exit(1)
except Exception as e:
print(f"\n\n测试执行出错: {e}")
import traceback
traceback.print_exc()
sys.exit(1)