refactor: split order handling and ai routing flow from agent
This commit is contained in:
59
core/order_flow.py
Normal file
59
core/order_flow.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.pydantic_ai_agent import AgentResponse, ConversationState, CustomerMessage, CustomerServiceAgent
|
||||
|
||||
|
||||
async def handle_order_notification(
|
||||
agent: "CustomerServiceAgent",
|
||||
*,
|
||||
message: "CustomerMessage",
|
||||
state: "ConversationState",
|
||||
) -> Optional["AgentResponse"]:
|
||||
"""Handle system order notifications before normal AI dialogue."""
|
||||
from core.pydantic_ai_agent import AgentResponse
|
||||
|
||||
if "系统订单信息" not in message.msg and "订单状态" not in message.msg:
|
||||
return None
|
||||
|
||||
_, order_block = agent._split_customer_text(message.msg)
|
||||
customer_text, _ = agent._split_customer_text(message.msg)
|
||||
order = agent._parse_order_info(order_block or message.msg)
|
||||
pay_status = order.get("pay_status", "")
|
||||
order_status = order.get("order_status", "")
|
||||
|
||||
paid_keywords = ["等待发货", "已付款", "付款成功", "买家已付款"]
|
||||
is_paid = any(kw in pay_status or kw in order_status for kw in paid_keywords)
|
||||
|
||||
if is_paid:
|
||||
asyncio.create_task(agent._check_order_amount(message.from_id, order, message.acc_id))
|
||||
asyncio.create_task(
|
||||
agent._record_deal_success(
|
||||
message.from_id,
|
||||
message.from_name,
|
||||
message.acc_id,
|
||||
message.acc_type,
|
||||
order,
|
||||
state,
|
||||
)
|
||||
)
|
||||
try:
|
||||
from core.workflow import workflow
|
||||
|
||||
asyncio.create_task(
|
||||
workflow.trigger_processing_on_payment(
|
||||
customer_id=message.from_id,
|
||||
acc_id=message.acc_id,
|
||||
acc_type=message.acc_type,
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"[Agent] 触发作图失败: {e}")
|
||||
elif not customer_text:
|
||||
print(f"[Agent] 订单通知静默({pay_status or order_status}),跳过回复")
|
||||
return AgentResponse(reply="", should_reply=False, need_transfer=False)
|
||||
|
||||
return None
|
||||
Reference in New Issue
Block a user