diff --git a/server/routers/chat_router.py b/server/routers/chat_router.py index 24ecc964..7d5e9ba6 100644 --- a/server/routers/chat_router.py +++ b/server/routers/chat_router.py @@ -882,6 +882,34 @@ async def get_agent_history( raise HTTPException(status_code=500, detail=f"获取智能体历史消息出错: {str(e)}") +@chat.get("/agent/{agent_id}/state") +async def get_agent_state( + agent_id: str, + thread_id: str, + current_user: User = Depends(get_required_user), + db: Session = Depends(get_db), +): + try: + if not agent_manager.get_agent(agent_id): + raise HTTPException(status_code=404, detail=f"智能体 {agent_id} 不存在") + + conv_manager = ConversationManager(db) + _require_user_conversation(conv_manager, thread_id, str(current_user.id)) + + agent = agent_manager.get_agent(agent_id) + graph = await agent.get_graph() + langgraph_config = {"configurable": {"user_id": str(current_user.id), "thread_id": thread_id}} + state = await graph.aget_state(langgraph_config) + agent_state = _extract_agent_state(getattr(state, "values", {})) if state else {} + + return {"agent_state": agent_state} + except HTTPException: + raise + except Exception as e: + logger.error(f"获取AgentState出错: {e}, {traceback.format_exc()}") + raise HTTPException(status_code=500, detail=f"获取AgentState出错: {str(e)}") + + @chat.get("/agent/{agent_id}/config") async def get_agent_config(agent_id: str, current_user: User = Depends(get_required_user)): """从YAML文件加载智能体配置(需要登录)""" diff --git a/web/src/apis/agent_api.js b/web/src/apis/agent_api.js index 86b70607..e4ed4dc9 100644 --- a/web/src/apis/agent_api.js +++ b/web/src/apis/agent_api.js @@ -73,6 +73,14 @@ export const agentApi = { */ getAgentHistory: (agentId, threadId) => apiGet(`/api/chat/agent/${agentId}/history?thread_id=${threadId}`), + /** + * 获取指定会话的 AgentState + * @param {string} agentId - 智能体ID + * @param {string} threadId - 会话ID + * @returns {Promise} - AgentState + */ + getAgentState: (agentId, threadId) => apiGet(`/api/chat/agent/${agentId}/state?thread_id=${threadId}`), + /** * Submit feedback for a message * @param {number} messageId - Message ID diff --git a/web/src/components/AgentChatComponent.vue b/web/src/components/AgentChatComponent.vue index 15c09a50..cefa2896 100644 --- a/web/src/components/AgentChatComponent.vue +++ b/web/src/components/AgentChatComponent.vue @@ -45,8 +45,10 @@