feat: 增强 Agent 系统和完善项目结构
主要改进: - Agent 增强: 订单查询、售后支持、客服路由等功能优化 - 新增语言检测和 Token 管理模块 - 改进 Chatwoot webhook 处理和用户标识 - MCP 服务器增强: 订单 MCP 和 Strapi MCP 功能扩展 - 新增商城客户端、知识库、缓存和同步模块 - 添加多语言提示词系统 (YAML) - 完善项目结构: 整理文档、脚本和测试文件 - 新增调试和测试工具脚本 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,11 +7,7 @@ import httpx
|
||||
from langgraph.graph import StateGraph, END
|
||||
|
||||
from .state import AgentState, ConversationState, mark_finished, add_tool_result, set_response
|
||||
from agents.router import classify_intent, route_by_intent
|
||||
from agents.customer_service import customer_service_agent
|
||||
from agents.order import order_agent
|
||||
from agents.aftersale import aftersale_agent
|
||||
from agents.product import product_agent
|
||||
# 延迟导入以避免循环依赖
|
||||
from config import settings
|
||||
from utils.logger import get_logger
|
||||
|
||||
@@ -197,20 +193,36 @@ async def handle_error(state: AgentState) -> AgentState:
|
||||
|
||||
def should_call_tools(state: AgentState) -> Literal["call_tools", "send_response", "back_to_agent"]:
|
||||
"""Determine if tools need to be called"""
|
||||
|
||||
|
||||
logger.debug(
|
||||
"Checking if tools should be called",
|
||||
conversation_id=state.get("conversation_id"),
|
||||
has_tool_calls=bool(state.get("tool_calls")),
|
||||
tool_calls_count=len(state.get("tool_calls", [])),
|
||||
has_response=bool(state.get("response")),
|
||||
state_value=state.get("state")
|
||||
)
|
||||
|
||||
# If there are pending tool calls, execute them
|
||||
if state.get("tool_calls"):
|
||||
logger.info(
|
||||
"Routing to tool execution",
|
||||
tool_count=len(state["tool_calls"])
|
||||
)
|
||||
return "call_tools"
|
||||
|
||||
|
||||
# If we have a response ready, send it
|
||||
if state.get("response"):
|
||||
logger.debug("Routing to send_response (has response)")
|
||||
return "send_response"
|
||||
|
||||
|
||||
# If we're waiting for info, send the question
|
||||
if state.get("state") == ConversationState.AWAITING_INFO.value:
|
||||
logger.debug("Routing to send_response (awaiting info)")
|
||||
return "send_response"
|
||||
|
||||
|
||||
# Otherwise, something went wrong
|
||||
logger.warning("Unexpected state, routing to send_response", state=state.get("state"))
|
||||
return "send_response"
|
||||
|
||||
|
||||
@@ -255,13 +267,20 @@ def check_completion(state: AgentState) -> Literal["continue", "end", "error"]:
|
||||
|
||||
def create_agent_graph() -> StateGraph:
|
||||
"""Create the main agent workflow graph
|
||||
|
||||
|
||||
Returns:
|
||||
Compiled LangGraph workflow
|
||||
"""
|
||||
# 延迟导入以避免循环依赖
|
||||
from agents.router import classify_intent, route_by_intent
|
||||
from agents.customer_service import customer_service_agent
|
||||
from agents.order import order_agent
|
||||
from agents.aftersale import aftersale_agent
|
||||
from agents.product import product_agent
|
||||
|
||||
# Create graph with AgentState
|
||||
graph = StateGraph(AgentState)
|
||||
|
||||
|
||||
# Add nodes
|
||||
graph.add_node("receive", receive_message)
|
||||
graph.add_node("classify", classify_intent)
|
||||
@@ -347,10 +366,11 @@ async def process_message(
|
||||
account_id: str,
|
||||
message: str,
|
||||
history: list[dict] = None,
|
||||
context: dict = None
|
||||
context: dict = None,
|
||||
user_token: str = None
|
||||
) -> AgentState:
|
||||
"""Process a user message through the agent workflow
|
||||
|
||||
|
||||
Args:
|
||||
conversation_id: Chatwoot conversation ID
|
||||
user_id: User identifier
|
||||
@@ -358,12 +378,13 @@ async def process_message(
|
||||
message: User's message
|
||||
history: Previous conversation history
|
||||
context: Existing conversation context
|
||||
|
||||
user_token: User JWT token for API calls
|
||||
|
||||
Returns:
|
||||
Final agent state with response
|
||||
"""
|
||||
from .state import create_initial_state
|
||||
|
||||
|
||||
# Create initial state
|
||||
initial_state = create_initial_state(
|
||||
conversation_id=conversation_id,
|
||||
@@ -371,7 +392,8 @@ async def process_message(
|
||||
account_id=account_id,
|
||||
current_message=message,
|
||||
messages=history,
|
||||
context=context
|
||||
context=context,
|
||||
user_token=user_token
|
||||
)
|
||||
|
||||
# Get compiled graph
|
||||
|
||||
Reference in New Issue
Block a user