test_approval_replay.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """
  2. 审批回放测试 — 复用 validated_decisions_20260507.csv 反复发审批。
  3. 每次启动跑一轮:发审批表 → 阻塞等您在飞书回复 → Agent 根据回复选择后续工具链 → 发回执。
  4. 您可在飞书群里换不同回复(通过/拒绝/通过广告 X/降幅改小一点/为什么 pause 这条 X)测试 Agent 的动作和回执文案。
  5. 用法:
  6. cd /Users/liulidong/project/agent/Agent
  7. .venv/bin/python3 examples/auto_put_ad_mini/test_approval_replay.py [validated_csv_path]
  8. """
  9. import asyncio
  10. import os
  11. import sys
  12. from pathlib import Path
  13. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  14. from dotenv import load_dotenv
  15. load_dotenv()
  16. from agent.core.runner import AgentRunner
  17. from agent.trace import FileSystemTraceStore
  18. from agent.llm import create_openrouter_llm_call
  19. from agent.utils import setup_logging
  20. from examples.auto_put_ad_mini.config import (
  21. MAIN_CONFIG, SKILLS_DIR, TRACE_STORE_PATH, LOG_LEVEL, LOG_FILE,
  22. )
  23. # 触发 @tool 注册
  24. from examples.auto_put_ad_mini.tools.data_query import fetch_creative_data, merge_creative_data # noqa
  25. from examples.auto_put_ad_mini.tools.roi_calculator import calculate_roi_metrics # noqa
  26. from examples.auto_put_ad_mini.tools.portfolio_metrics import calculate_portfolio_summary # noqa
  27. from examples.auto_put_ad_mini.tools.ad_decision import ( # noqa
  28. get_ads_for_review, apply_decisions, query_ad_detail, modify_decisions,
  29. )
  30. from examples.auto_put_ad_mini.tools.report_generator import generate_report # noqa
  31. from examples.auto_put_ad_mini.tools.guardrails import validate_decisions # noqa
  32. from examples.auto_put_ad_mini.tools.execution_engine import execute_decisions, check_execution_feedback # noqa
  33. from examples.auto_put_ad_mini.tools.im_approval import ( # noqa
  34. send_approval_request, check_approval_status, send_feishu_text_message,
  35. )
  36. def _load_system_prompt(base_dir: Path) -> str:
  37. p = base_dir / "prompts" / "system.prompt"
  38. return p.read_text(encoding="utf-8") if p.exists() else ""
  39. async def main():
  40. setup_logging(level=LOG_LEVEL, file=LOG_FILE)
  41. base_dir = Path(__file__).parent
  42. validated_csv = sys.argv[1] if len(sys.argv) > 1 else str(
  43. base_dir / "outputs" / "reports" / "validated_decisions_20260507.csv"
  44. )
  45. if not Path(validated_csv).exists():
  46. print(f"❌ 找不到 validated CSV: {validated_csv}")
  47. sys.exit(1)
  48. print(f"▶ 使用决策 CSV: {validated_csv}")
  49. print("▶ 启动 Agent,将在飞书发审批表并阻塞等待回复...")
  50. print("▶ 在飞书群里 @机器人 + 回复任意以下内容测试:")
  51. print(" - 通过 / 同意 / 批准")
  52. print(" - 拒绝 / 驳回")
  53. print(" - 通过广告 12345678901")
  54. print(" - 通过 12345678901, 拒绝 22345678901")
  55. print(" - 降幅改小一点")
  56. print(" - 为什么 pause 这条 12345678901?")
  57. print("─" * 60)
  58. store = FileSystemTraceStore(base_path=TRACE_STORE_PATH)
  59. runner = AgentRunner(
  60. trace_store=store,
  61. llm_call=create_openrouter_llm_call(model=MAIN_CONFIG.model),
  62. skills_dir=SKILLS_DIR if Path(SKILLS_DIR).exists() else None,
  63. logger_name="agents.auto_put_ad_mini.replay",
  64. )
  65. config = MAIN_CONFIG
  66. system_prompt = _load_system_prompt(base_dir)
  67. if system_prompt:
  68. config.system_prompt = system_prompt
  69. user_msg = (
  70. f"复用已验证决策 CSV `{validated_csv}` 直接走审批协商流程:\n"
  71. "1. 调用 `send_approval_request(validated_csv='{path}', wait_for_reply=True)` 发审批表并阻塞等回复\n"
  72. "2. 收到回复后,严格按 system prompt 的反馈类型识别表选择后续工具链:\n"
  73. " - 全部通过 → execute_decisions + send_feishu_text_message 摘要\n"
  74. " - 全部拒绝 → 仅 send_feishu_text_message 简短回执,不执行\n"
  75. " - 部分批准型 → execute_decisions(filter_ad_ids=approved_ids) + send_feishu_text_message 回执,**严禁重审**\n"
  76. " - 策略型/方向型 → modify_decisions → validate_decisions → send_approval_request 重审\n"
  77. " - 质疑型 → query_ad_detail + send_feishu_text_message 解释\n"
  78. "3. 全程使用第二人称「您」,禁用【】公文头\n"
  79. ).format(path=validated_csv)
  80. messages = [
  81. {"role": "system", "content": system_prompt},
  82. {"role": "user", "content": user_msg},
  83. ]
  84. async for item in runner.run(messages=messages, config=config):
  85. # 流式打印 Agent 每一步动作
  86. cls_name = type(item).__name__
  87. if hasattr(item, "tool_name") and getattr(item, "tool_name", None):
  88. print(f" → tool: {item.tool_name}")
  89. elif hasattr(item, "role") and getattr(item, "role", None) == "assistant":
  90. content = getattr(item, "content", "")
  91. if isinstance(content, str) and content.strip():
  92. snippet = content[:500]
  93. print(f"\n[assistant] {snippet}")
  94. elif isinstance(content, list):
  95. for blk in content:
  96. if isinstance(blk, dict) and blk.get("type") == "text":
  97. print(f"\n[assistant text] {str(blk.get('text', ''))[:500]}")
  98. else:
  99. print(f" · {cls_name}")
  100. print("─" * 60)
  101. print("▶ Agent 终止")
  102. if __name__ == "__main__":
  103. asyncio.run(main())