From 1f5636520e2ca63a42720b4af2c97eb0aa2f20f3 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Fri, 13 Feb 2026 22:29:20 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E7=BB=9F=E4=B8=80=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=92=8C=20import=20=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整 Python 文件 import 顺序,遵循标准库→第三方库→项目模块顺序 - 统一字符串引号风格为双引号 - 优化 Vue 组件 console.log 格式 - 压缩 import 语句为单行 - 清理不必要的空行 --- src/agents/chatbot/graph.py | 5 +- src/services/conversation_service.py | 4 +- test/api/test_attachment_and_agent_state.py | 53 +++++++++---------- web/src/components/AgentChatComponent.vue | 20 ++++++- web/src/components/AgentPanel.vue | 9 +--- .../dashboard/CallStatsComponent.vue | 1 - 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/agents/chatbot/graph.py b/src/agents/chatbot/graph.py index 0e4ee619..99740bc1 100644 --- a/src/agents/chatbot/graph.py +++ b/src/agents/chatbot/graph.py @@ -1,8 +1,7 @@ -from langchain.agents import create_agent -from langchain.agents.middleware import ModelRetryMiddleware - from deepagents.backends import StateBackend from deepagents.middleware.filesystem import FilesystemMiddleware +from langchain.agents import create_agent +from langchain.agents.middleware import ModelRetryMiddleware from src.agents.common import BaseAgent, load_chat_model from src.agents.common.middlewares import ( diff --git a/src/services/conversation_service.py b/src/services/conversation_service.py index 5e7cabdb..642bce47 100644 --- a/src/services/conversation_service.py +++ b/src/services/conversation_service.py @@ -31,9 +31,9 @@ def _make_attachment_path(file_name: str) -> str: """ # 提取不带扩展名的部分 base_name = file_name - for ext in ['.docx', '.txt', '.html', '.htm', '.pdf', '.md']: + for ext in [".docx", ".txt", ".html", ".htm", ".pdf", ".md"]: if file_name.lower().endswith(ext): - base_name = file_name[:-len(ext)] + base_name = file_name[: -len(ext)] break # 替换路径分隔符 diff --git a/test/api/test_attachment_and_agent_state.py b/test/api/test_attachment_and_agent_state.py index 72f2bc88..33169765 100644 --- a/test/api/test_attachment_and_agent_state.py +++ b/test/api/test_attachment_and_agent_state.py @@ -13,19 +13,18 @@ import asyncio import contextlib import os import sys +import uuid from pathlib import Path +import httpx +from dotenv import load_dotenv + # 添加项目根目录到 Python 路径 PROJECT_ROOT = Path(__file__).parent.parent.parent sys.path.insert(0, str(PROJECT_ROOT)) -# 加载 .env 文件 -from dotenv import load_dotenv load_dotenv(PROJECT_ROOT / ".env") -import httpx -import uuid - # API 配置 API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:5050") @@ -58,9 +57,9 @@ class APITester: async def login(self) -> bool: """登录获取 token""" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"1. 正在登录: {self.username}") - print(f"{'='*60}") + print(f"{'=' * 60}") async with self._client() as client: response = await client.post( @@ -82,9 +81,9 @@ class APITester: async def create_thread(self, agent_id: str) -> str: """创建对话线程""" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"2. 创建对话线程 (agent_id: {agent_id})") - print(f"{'='*60}") + print(f"{'=' * 60}") async with self._client() as client: response = await client.post( @@ -104,9 +103,9 @@ class APITester: async def upload_attachment(self, thread_id: str, file_path: str) -> dict | None: """上传附件""" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"3. 上传附件: {file_path}") - print(f"{'='*60}") + print(f"{'=' * 60}") if not os.path.exists(file_path): print(f" ✗ 文件不存在: {file_path}") @@ -123,7 +122,7 @@ class APITester: if response.status_code == 200: attachment = response.json() - print(f" ✓ 上传成功!") + print(" ✓ 上传成功!") print(f" file_id: {attachment.get('file_id')}") print(f" file_name: {attachment.get('file_name')}") print(f" status: {attachment.get('status')}") @@ -134,9 +133,9 @@ class APITester: async def list_attachments(self, thread_id: str) -> list[dict]: """列出附件""" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"4. 列出附件 (thread_id: {thread_id})") - print(f"{'='*60}") + print(f"{'=' * 60}") async with self._client() as client: response = await client.get( @@ -157,9 +156,9 @@ class APITester: async def get_agent_state(self, agent_id: str, thread_id: str) -> dict | None: """获取 agent state""" - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(f"5. 获取 Agent State (agent_id: {agent_id}, thread_id: {thread_id})") - print(f"{'='*60}") + print(f"{'=' * 60}") async with self._client() as client: response = await client.get( @@ -171,11 +170,11 @@ class APITester: if response.status_code == 200: state = response.json() agent_state = state.get("agent_state", {}) - print(f" ✓ 获取成功!") + print(" ✓ 获取成功!") print(f" files: {len(agent_state.get('files', {}))} 个") print(f" todos: {len(agent_state.get('todos', []))} 个") if agent_state.get("files"): - print(f" 文件列表:") + print(" 文件列表:") for path in agent_state["files"]: file_info = agent_state["files"][path] print(f" - {path}: {len(file_info.get('content', []))} 行") @@ -186,9 +185,9 @@ class APITester: async def send_chat_message(self, agent_id: str, thread_id: str, query: str) -> bool: """发送聊天消息(流式)""" - print(f"\n{'='*60}") - print(f"6. 发送聊天消息") - print(f"{'='*60}") + print(f"\n{'=' * 60}") + print("6. 发送聊天消息") + print(f"{'=' * 60}") print(f" Query: {query}") print(f" Thread ID: {thread_id}") @@ -200,7 +199,7 @@ class APITester: headers=self.headers, ) as response: print(f"\n 响应状态: {response.status_code}") - print(f" 响应内容:") + print(" 响应内容:") async for chunk in response.aiter_lines(): if chunk: print(f" {chunk[:150]}...") @@ -264,11 +263,7 @@ def hello(): await tester.get_agent_state(agent_id, thread_id) # 7. 发送聊天消息测试 - await tester.send_chat_message( - agent_id, - thread_id, - "你好,请简单介绍一下你自己。" - ) + await tester.send_chat_message(agent_id, thread_id, "你好,请简单介绍一下你自己。") # 8. 再次获取 agent state (验证 todos 等状态) await asyncio.sleep(1) @@ -279,9 +274,9 @@ def hello(): os.remove(test_file_path) print(f"\n 测试文件已清理: {test_file_path}") - print(f"\n{'='*60}") + print(f"\n{'=' * 60}") print(" 测试完成!") - print(f"{'='*60}\n") + print(f"{'=' * 60}\n") if __name__ == "__main__": diff --git a/web/src/components/AgentChatComponent.vue b/web/src/components/AgentChatComponent.vue index fcf20cc1..d777dca2 100644 --- a/web/src/components/AgentChatComponent.vue +++ b/web/src/components/AgentChatComponent.vue @@ -671,7 +671,16 @@ const fetchAgentState = async (agentId, threadId) => { // 确保更新 currentChatId 对应的 state,因为 currentAgentState 依赖它 // 如果 currentChatId 为 null,使用传入的 threadId const targetChatId = currentChatId.value || threadId - console.log('[fetchAgentState] agentId:', agentId, 'threadId:', threadId, 'targetChatId:', targetChatId, 'agent_state:', JSON.stringify(res.agent_state || {})?.slice(0, 200)) + console.log( + '[fetchAgentState] agentId:', + agentId, + 'threadId:', + threadId, + 'targetChatId:', + targetChatId, + 'agent_state:', + JSON.stringify(res.agent_state || {})?.slice(0, 200) + ) const ts = getThreadState(targetChatId) if (ts) { ts.agentState = res.agent_state || null @@ -1088,7 +1097,14 @@ const handleAgentStateRefresh = async (threadId = null) => { if (!currentAgentId.value) return // 优先使用传入的 threadId,否则使用当前的 currentChatId let chatId = threadId || currentChatId.value - console.log('[handleAgentStateRefresh] input threadId:', threadId, 'currentChatId:', currentChatId.value, 'final chatId:', chatId) + console.log( + '[handleAgentStateRefresh] input threadId:', + threadId, + 'currentChatId:', + currentChatId.value, + 'final chatId:', + chatId + ) if (!chatId) return await fetchAgentState(currentAgentId.value, chatId) } diff --git a/web/src/components/AgentPanel.vue b/web/src/components/AgentPanel.vue index a78e4ee3..5a42ab4c 100644 --- a/web/src/components/AgentPanel.vue +++ b/web/src/components/AgentPanel.vue @@ -151,14 +151,7 @@