1313import contextlib
1414import os
1515import sys
16+ import uuid
1617from pathlib import Path
1718
19+ import httpx
20+ from dotenv import load_dotenv
21+
1822# 添加项目根目录到 Python 路径
1923PROJECT_ROOT = Path (__file__ ).parent .parent .parent
2024sys .path .insert (0 , str (PROJECT_ROOT ))
2125
22- # 加载 .env 文件
23- from dotenv import load_dotenv
2426load_dotenv (PROJECT_ROOT / ".env" )
2527
26- import httpx
27- import uuid
28-
2928
3029# API 配置
3130API_BASE_URL = os .getenv ("API_BASE_URL" , "http://localhost:5050" )
@@ -58,9 +57,9 @@ async def _client(self, timeout: float = 30.0):
5857
5958 async def login (self ) -> bool :
6059 """登录获取 token"""
61- print (f"\n { '=' * 60 } " )
60+ print (f"\n { '=' * 60 } " )
6261 print (f"1. 正在登录: { self .username } " )
63- print (f"{ '=' * 60 } " )
62+ print (f"{ '=' * 60 } " )
6463
6564 async with self ._client () as client :
6665 response = await client .post (
@@ -82,9 +81,9 @@ async def login(self) -> bool:
8281
8382 async def create_thread (self , agent_id : str ) -> str :
8483 """创建对话线程"""
85- print (f"\n { '=' * 60 } " )
84+ print (f"\n { '=' * 60 } " )
8685 print (f"2. 创建对话线程 (agent_id: { agent_id } )" )
87- print (f"{ '=' * 60 } " )
86+ print (f"{ '=' * 60 } " )
8887
8988 async with self ._client () as client :
9089 response = await client .post (
@@ -104,9 +103,9 @@ async def create_thread(self, agent_id: str) -> str:
104103
105104 async def upload_attachment (self , thread_id : str , file_path : str ) -> dict | None :
106105 """上传附件"""
107- print (f"\n { '=' * 60 } " )
106+ print (f"\n { '=' * 60 } " )
108107 print (f"3. 上传附件: { file_path } " )
109- print (f"{ '=' * 60 } " )
108+ print (f"{ '=' * 60 } " )
110109
111110 if not os .path .exists (file_path ):
112111 print (f" ✗ 文件不存在: { file_path } " )
@@ -123,7 +122,7 @@ async def upload_attachment(self, thread_id: str, file_path: str) -> dict | None
123122
124123 if response .status_code == 200 :
125124 attachment = response .json ()
126- print (f " ✓ 上传成功!" )
125+ print (" ✓ 上传成功!" )
127126 print (f" file_id: { attachment .get ('file_id' )} " )
128127 print (f" file_name: { attachment .get ('file_name' )} " )
129128 print (f" status: { attachment .get ('status' )} " )
@@ -134,9 +133,9 @@ async def upload_attachment(self, thread_id: str, file_path: str) -> dict | None
134133
135134 async def list_attachments (self , thread_id : str ) -> list [dict ]:
136135 """列出附件"""
137- print (f"\n { '=' * 60 } " )
136+ print (f"\n { '=' * 60 } " )
138137 print (f"4. 列出附件 (thread_id: { thread_id } )" )
139- print (f"{ '=' * 60 } " )
138+ print (f"{ '=' * 60 } " )
140139
141140 async with self ._client () as client :
142141 response = await client .get (
@@ -157,9 +156,9 @@ async def list_attachments(self, thread_id: str) -> list[dict]:
157156
158157 async def get_agent_state (self , agent_id : str , thread_id : str ) -> dict | None :
159158 """获取 agent state"""
160- print (f"\n { '=' * 60 } " )
159+ print (f"\n { '=' * 60 } " )
161160 print (f"5. 获取 Agent State (agent_id: { agent_id } , thread_id: { thread_id } )" )
162- print (f"{ '=' * 60 } " )
161+ print (f"{ '=' * 60 } " )
163162
164163 async with self ._client () as client :
165164 response = await client .get (
@@ -171,11 +170,11 @@ async def get_agent_state(self, agent_id: str, thread_id: str) -> dict | None:
171170 if response .status_code == 200 :
172171 state = response .json ()
173172 agent_state = state .get ("agent_state" , {})
174- print (f " ✓ 获取成功!" )
173+ print (" ✓ 获取成功!" )
175174 print (f" files: { len (agent_state .get ('files' , {}))} 个" )
176175 print (f" todos: { len (agent_state .get ('todos' , []))} 个" )
177176 if agent_state .get ("files" ):
178- print (f " 文件列表:" )
177+ print (" 文件列表:" )
179178 for path in agent_state ["files" ]:
180179 file_info = agent_state ["files" ][path ]
181180 print (f" - { path } : { len (file_info .get ('content' , []))} 行" )
@@ -186,9 +185,9 @@ async def get_agent_state(self, agent_id: str, thread_id: str) -> dict | None:
186185
187186 async def send_chat_message (self , agent_id : str , thread_id : str , query : str ) -> bool :
188187 """发送聊天消息(流式)"""
189- print (f"\n { '=' * 60 } " )
190- print (f "6. 发送聊天消息" )
191- print (f"{ '=' * 60 } " )
188+ print (f"\n { '=' * 60 } " )
189+ print ("6. 发送聊天消息" )
190+ print (f"{ '=' * 60 } " )
192191 print (f" Query: { query } " )
193192 print (f" Thread ID: { thread_id } " )
194193
@@ -200,7 +199,7 @@ async def send_chat_message(self, agent_id: str, thread_id: str, query: str) ->
200199 headers = self .headers ,
201200 ) as response :
202201 print (f"\n 响应状态: { response .status_code } " )
203- print (f " 响应内容:" )
202+ print (" 响应内容:" )
204203 async for chunk in response .aiter_lines ():
205204 if chunk :
206205 print (f" { chunk [:150 ]} ..." )
@@ -264,11 +263,7 @@ def hello():
264263 await tester .get_agent_state (agent_id , thread_id )
265264
266265 # 7. 发送聊天消息测试
267- await tester .send_chat_message (
268- agent_id ,
269- thread_id ,
270- "你好,请简单介绍一下你自己。"
271- )
266+ await tester .send_chat_message (agent_id , thread_id , "你好,请简单介绍一下你自己。" )
272267
273268 # 8. 再次获取 agent state (验证 todos 等状态)
274269 await asyncio .sleep (1 )
@@ -279,9 +274,9 @@ def hello():
279274 os .remove (test_file_path )
280275 print (f"\n 测试文件已清理: { test_file_path } " )
281276
282- print (f"\n { '=' * 60 } " )
277+ print (f"\n { '=' * 60 } " )
283278 print (" 测试完成!" )
284- print (f"{ '=' * 60 } \n " )
279+ print (f"{ '=' * 60 } \n " )
285280
286281
287282if __name__ == "__main__" :
0 commit comments