Files
tw/core/engine.py

64 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
from typing import Optional, Any
from core.schema import StandardMessage, StandardResponse
from core.events.event_bus import bus
logger = logging.getLogger("cs_agent")
class BusinessEngine:
"""
业务逻辑中枢:
1. 接收 StandardMessage。
2. 决定由哪个 AI 工具或流程处理。
3. 返回 StandardResponse。
4. 对外广播异步事件。
"""
def __init__(self, agent_instance: Any = None):
"""
:param agent_instance: 核心 AI Agent 的实例(比如重构后的 CustomerServiceAgent
"""
self.agent = agent_instance
async def handle_message(self, msg: StandardMessage) -> StandardResponse:
"""
大脑的思考主入口
"""
logger.info(f"[Engine] 收到来自 {msg.platform} 的消息: {msg.user_id} -> {msg.content[:50]}")
# TODO: 这里将接入重构后的 Single Agent + Tool Calling
# 目前模拟一个简单的规则响应,展示 StandardResponse 的用法
if "报价" in msg.content or msg.image_urls:
return StandardResponse(
reply_content="正在为你查看图片,请稍等...",
metadata={"acc_id": msg.acc_id, "acc_type": msg.acc_type}
)
if "转人工" in msg.content:
return StandardResponse(
reply_content="正在为你转接设计师...",
need_transfer=True,
metadata={"acc_id": msg.acc_id, "acc_type": msg.acc_type}
)
# 兜底回复
return StandardResponse(
reply_content="你好我是AI助手有什么可以帮你的",
metadata={"acc_id": msg.acc_id, "acc_type": msg.acc_type}
)
async def emit_image_result(self, user_id: str, platform: str, url: str, acc_id: str):
"""
这是一个业务触发器示例:当图片处理完成时,由 Engine 主动发广播。
"""
await bus.emit(
"MESSAGE_OUTBOUND",
user_id=user_id,
platform=platform,
response=StandardResponse(
reply_content=url,
msg_type=1, # 图片
metadata={"acc_id": acc_id}
)
)