|
| 1 | +""" |
| 2 | +调试 agent 在线状态问题 |
| 3 | +""" |
| 4 | +import asyncio |
| 5 | +import aiohttp |
| 6 | +import json |
| 7 | + |
| 8 | +SERVER_URL = "http://127.0.0.1:8000" |
| 9 | + |
| 10 | +async def debug_agent_status(): |
| 11 | + """检查 agent API 返回的数据""" |
| 12 | + print("=== 调试 Agent 在线状态 ===\n") |
| 13 | + |
| 14 | + async with aiohttp.ClientSession() as session: |
| 15 | + # 获取所有 agent |
| 16 | + async with session.get(f"{SERVER_URL}/api/agents") as resp: |
| 17 | + if resp.status != 200: |
| 18 | + print(f"❌ 获取 agent 列表失败: {resp.status}") |
| 19 | + return |
| 20 | + |
| 21 | + agents = await resp.json() |
| 22 | + print(f"找到 {len(agents)} 个 agent:\n") |
| 23 | + |
| 24 | + for i, agent in enumerate(agents, 1): |
| 25 | + print(f"Agent {i}:") |
| 26 | + print(f" ID: {agent.get('id')}") |
| 27 | + print(f" Name: {agent.get('name')}") |
| 28 | + print(f" Display Name: {agent.get('display_name')}") |
| 29 | + print(f" is_online: {agent.get('is_online')}") |
| 30 | + print(f" last_heartbeat: {agent.get('last_heartbeat')}") |
| 31 | + print(f" last_activity: {agent.get('last_activity')}") |
| 32 | + print(f" last_activity_time: {agent.get('last_activity_time')}") |
| 33 | + |
| 34 | + # 计算状态 |
| 35 | + activity_time = agent.get('last_activity_time') |
| 36 | + if activity_time: |
| 37 | + from datetime import datetime |
| 38 | + activity_dt = datetime.fromisoformat(activity_time.replace('Z', '+00:00')) |
| 39 | + now = datetime.now() |
| 40 | + seconds_ago = (now - activity_dt).total_seconds() |
| 41 | + print(f" 距离上次活动: {seconds_ago:.0f} 秒") |
| 42 | + |
| 43 | + # 模拟 getAgentState 逻辑 |
| 44 | + if agent.get('last_activity') == 'msg_wait' and seconds_ago < 60: |
| 45 | + state = 'Waiting' |
| 46 | + elif seconds_ago < 30: |
| 47 | + state = 'Active' |
| 48 | + elif seconds_ago < 300: |
| 49 | + state = 'Idle' |
| 50 | + else: |
| 51 | + state = 'Offline' if not agent.get('is_online') else 'Idle' |
| 52 | + else: |
| 53 | + state = 'Waiting' if agent.get('is_online') else 'Offline' |
| 54 | + |
| 55 | + print(f" 计算状态: {state}") |
| 56 | + print() |
| 57 | + |
| 58 | + # 检查过滤逻辑 |
| 59 | + print("=== 过滤逻辑测试 ===\n") |
| 60 | + offline_agents = [a for a in agents if agent.get('last_activity_time') is None or not agent.get('is_online')] |
| 61 | + print(f"可能被过滤为 Offline 的 agent 数量: {len(offline_agents)}") |
| 62 | + |
| 63 | + if offline_agents: |
| 64 | + print("\n这些 agent 可能被过滤:") |
| 65 | + for agent in offline_agents: |
| 66 | + print(f" - {agent.get('display_name') or agent.get('name')} (is_online={agent.get('is_online')}, last_activity_time={agent.get('last_activity_time')})") |
| 67 | + |
| 68 | +async def main(): |
| 69 | + try: |
| 70 | + await debug_agent_status() |
| 71 | + except aiohttp.ClientConnectorError: |
| 72 | + print("❌ 错误: 无法连接到服务器。请确保服务器正在运行 http://127.0.0.1:8000") |
| 73 | + except Exception as e: |
| 74 | + print(f"❌ 错误: {e}") |
| 75 | + import traceback |
| 76 | + traceback.print_exc() |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + asyncio.run(main()) |
0 commit comments