refactor: unify core pipeline logging with cs_agent logger

This commit is contained in:
2026-03-01 16:29:52 +08:00
parent a6b7bf1982
commit 8dd5a11b4b
11 changed files with 82 additions and 55 deletions

View File

@@ -22,15 +22,10 @@ from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider
from dotenv import load_dotenv
from utils.metrics_tracker import emit as metrics_emit
from utils.observability import emit_activity, build_trace_id
from utils.observability import emit_activity
from core.quote_state_machine import QuoteStateMachine
from services.risk_service import RiskService
from core.agent_pre_rules import AgentPreRuleService
from core.find_image_flow import handle_find_image_batch_flow
from core.order_flow import handle_order_notification
from core.ai_reply_flow import execute_ai_turn
from core.reply_finalize_flow import finalize_ai_reply
from core.prompt_flow import build_prompt_bundle
from core.order_helpers import parse_order_info, order_instruction as build_order_instruction
from core.collection_intent_helpers import (
append_requirement,
@@ -81,7 +76,6 @@ load_dotenv()
from services.service_tuhui_upload import upload_to_tuhui
from core.workflow_router import get_workflow_router
from core.workflow_router import get_workflow_router
# ========== 企业微信通知 ==========
_WECHAT_WEBHOOK = os.getenv("WECHAT_WEBHOOK", "")
@@ -91,7 +85,7 @@ logger = logging.getLogger("cs_agent")
async def _notify_wechat(content: str, tag: str = "通知"):
"""发送企业微信 markdown 通知,任何异常都发"""
if not _WECHAT_WEBHOOK:
print(f"[{tag}] 未配置 WECHAT_WEBHOOK跳过推送")
logger.info("[%s] 未配置 WECHAT_WEBHOOK跳过推送", tag)
return
try:
import httpx
@@ -102,11 +96,11 @@ async def _notify_wechat(content: str, tag: str = "通知"):
})
data = resp.json()
if data.get("errcode") == 0:
print(f"[{tag}] 企业微信推送成功")
logger.info("[%s] 企业微信推送成功", tag)
else:
print(f"[{tag}] 企业微信推送失败: {data}")
logger.warning("[%s] 企业微信推送失败: %s", tag, data)
except Exception as e:
print(f"[{tag}] 企业微信发送异常: {e}")
logger.exception("[%s] 企业微信发送异常: %s", tag, e)
async def _notify_wechat_overdue():
@@ -246,7 +240,7 @@ def load_skill_map(skills_dir: str = "skills") -> Dict[str, str]:
else:
skill_map[skill_name] = content
except Exception as e:
print(f"警告: 读取 {skill_file} 失败: {e}")
logger.warning("读取技能文件失败: %s | err=%s", skill_file, e)
return skill_map
@@ -441,9 +435,7 @@ class CustomerServiceAgent:
@staticmethod
def _log_block(title: str, content: str):
"""统一的控制台分层日志输出。"""
print(f"{CustomerServiceAgent.C_PROMPT}[{title}]{CustomerServiceAgent.C_RESET}")
print(content)
print(f"{CustomerServiceAgent.C_MUTED}────────────────────{CustomerServiceAgent.C_RESET}")
logger.info("[%s]\n%s\n--------------------", title, content)
@staticmethod
def _normalize_reply_text(text: Optional[str]) -> str:
@@ -783,7 +775,7 @@ class CustomerServiceAgent:
return
paid = float(m.group())
print(f"[Agent] 订单金额核查:报价 {quoted}元 vs 实付 {paid}元(客户 {customer_id}")
logger.info("[Agent] 订单金额核查:报价 %s元 vs 实付 %s元(客户 %s", quoted, paid, customer_id)
# 实付金额明显低于报价(低于报价的 60%)才预警
if paid < quoted * 0.6:
@@ -795,10 +787,10 @@ class CustomerServiceAgent:
f"实付:{paid}\n"
f"差额:{quoted - paid:.1f}元 — 请人工核查"
)
print(f"[Agent] {msg}")
logger.warning("[Agent] %s", msg)
await self._notify_wechat(msg)
except Exception as e:
print(f"[Agent] 订单金额核查失败: {e}")
logger.exception("[Agent] 订单金额核查失败: %s", e)
def _extract_image_url(self, msg: str) -> str:
"""从消息中提取图片URL兼容纯URL和 text#*#url 两种格式"""
@@ -887,7 +879,7 @@ class CustomerServiceAgent:
quality=r.get("quality", ""),
)
except Exception as e:
print(f"[Agent] Workflow 批量任务创建失败: {e}")
logger.exception("[Agent] Workflow 批量任务创建失败: %s", e)
_calc_requirement_surcharge = staticmethod(calc_requirement_surcharge)
_build_batch_quote_reply = staticmethod(build_batch_quote_reply)
@@ -945,7 +937,7 @@ class CustomerServiceAgent:
raise RuntimeError(str(link))
links.append(link)
except Exception as e:
print(f"[Agent] 找图自动处理失败,回退需求澄清: {e}")
logger.exception("[Agent] 找图自动处理失败,回退需求澄清: %s", e)
return {
"reply": "这种可以做类似款。你先说下具体需求:要几张、是否改字、尺寸比例、交付格式(单图/打包链接),我按需求给你直接做。",
"need_transfer": False,
@@ -1064,7 +1056,7 @@ async def test_agent():
)
response = await agent.process_message(test_msg)
print(f"回复内容: {response.reply}")
logger.info("回复内容: %s", response.reply)
if __name__ == "__main__":