agent.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. """
  2. find_agent 工厂 — 组装 Agent 实例。
  3. 每个业务 Agent 都应提供 create_xxx_agent() 工厂函数,
  4. 统一注册本 Agent 的工具 + 共享基础设施工具。
  5. """
  6. from __future__ import annotations
  7. import json
  8. import re
  9. from pathlib import Path
  10. from supply_agent import Agent
  11. from supply_agent.config import Settings
  12. from supply_agent.types import Message, Role
  13. from agents.find_agent.tools import register_all_tools
  14. _PROMPT_PATH = Path(__file__).parent / "prompt" / "system_prompt.md"
  15. FIND_AGENT_SYSTEM_PROMPT = _PROMPT_PATH.read_text(encoding="utf-8")
  16. _EVIDENCE_TOOLS = {
  17. "douyin_detail",
  18. "get_content_fans_portrait",
  19. "get_account_fans_portrait",
  20. "batch_fetch_portraits",
  21. "normalize_age_portraits",
  22. "qwen_video_analyze",
  23. }
  24. _VIDEO_ID_PATTERN = re.compile(r"(?<!\d)\d{15,22}(?!\d)")
  25. def _report_section(
  26. content: str,
  27. start_marker: str,
  28. end_markers: tuple[str, ...],
  29. ) -> str | None:
  30. start = content.find(start_marker)
  31. if start < 0:
  32. return None
  33. ends = [
  34. position
  35. for marker in end_markers
  36. if (position := content.find(marker, start + len(start_marker))) >= 0
  37. ]
  38. end = min(ends) if ends else len(content)
  39. return content[start:end]
  40. def _validate_report_buckets(
  41. content: str,
  42. state: dict[str, object],
  43. ) -> str | None:
  44. candidates = state.get("candidates")
  45. if not isinstance(candidates, list):
  46. return "最终状态缺少 candidates,无法校验报告分池"
  47. bucket_by_id = {
  48. str(item.get("aweme_id")): str(item.get("decision_bucket"))
  49. for item in candidates
  50. if isinstance(item, dict) and item.get("aweme_id")
  51. }
  52. run = state.get("run")
  53. run_state = run if isinstance(run, dict) else {}
  54. primary_section = _report_section(
  55. content,
  56. "主推荐",
  57. ("补充推荐",),
  58. )
  59. backup_section = _report_section(
  60. content,
  61. "补充推荐",
  62. ("最有竞争力", "搜索树", "缺失数据", "总结"),
  63. )
  64. if primary_section is None or backup_section is None:
  65. return "最终报告必须分别包含“主推荐”和“补充推荐”段"
  66. primary_ids = set(_VIDEO_ID_PATTERN.findall(primary_section))
  67. backup_ids = set(_VIDEO_ID_PATTERN.findall(backup_section))
  68. wrong_primary = sorted(
  69. video_id
  70. for video_id in primary_ids
  71. if bucket_by_id.get(video_id) != "primary"
  72. )
  73. wrong_backup = sorted(
  74. video_id
  75. for video_id in backup_ids
  76. if bucket_by_id.get(video_id) != "backup"
  77. )
  78. if wrong_primary:
  79. return (
  80. "主推荐段包含非 primary 候选: "
  81. + ", ".join(wrong_primary[:5])
  82. + "。必须按数据库 decision_bucket 输出"
  83. )
  84. if wrong_backup:
  85. return (
  86. "补充推荐段包含非 backup 候选: "
  87. + ", ".join(wrong_backup[:5])
  88. + "。这些条目应移到淘汰候选,不得冒充 Agent 保留结果"
  89. )
  90. primary_count = int(run_state.get("primary_count") or 0)
  91. backup_count = int(run_state.get("backup_count") or 0)
  92. if primary_count > 0 and not primary_ids:
  93. return "数据库存在 primary 候选,但主推荐段没有输出 aweme_id"
  94. if primary_count == 0 and backup_count > 0 and not backup_ids:
  95. return "数据库只有 backup 保留候选,补充推荐段至少应输出一条"
  96. return None
  97. def _successful_tool_events(
  98. messages: list[Message],
  99. ) -> list[tuple[int, str, dict[str, object]]]:
  100. events: list[tuple[int, str, dict[str, object]]] = []
  101. for index, message in enumerate(messages):
  102. if message.role != Role.TOOL or not message.name or not message.content:
  103. continue
  104. try:
  105. payload = json.loads(message.content)
  106. except (TypeError, json.JSONDecodeError):
  107. continue
  108. if not isinstance(payload, dict) or payload.get("error"):
  109. continue
  110. events.append((index, message.name, payload))
  111. return events
  112. def find_agent_completion_guard(messages: list[Message]) -> str | None:
  113. """Reject final text until persisted state and the final audit prove completion."""
  114. events = _successful_tool_events(messages)
  115. if not any(name == "create_video_discovery_run" for _, name, _ in events):
  116. return "尚未成功创建视频发现运行"
  117. record_indexes = [
  118. index
  119. for index, name, _ in events
  120. if name == "record_video_search_page"
  121. ]
  122. if not record_indexes:
  123. return "尚未持久化任何搜索页"
  124. evaluation_events = [
  125. (index, payload)
  126. for index, name, payload in events
  127. if name == "batch_save_video_candidate_evaluations"
  128. ]
  129. if not evaluation_events:
  130. return "尚未保存候选评估"
  131. evaluation_index, evaluation = evaluation_events[-1]
  132. if evaluation_index < max(record_indexes):
  133. return "最后一次搜索发生在候选评估之后,新增候选尚未重新评估"
  134. if evaluation.get("status") != "finished":
  135. return "最后一次候选保存尚未把运行状态设置为 finished"
  136. evidence_indexes = [
  137. index for index, name, _ in events if name in _EVIDENCE_TOOLS
  138. ]
  139. if evidence_indexes and evaluation_index < max(evidence_indexes):
  140. return "最后一次证据获取发生在候选评估之后,证据尚未重新保存"
  141. audit_events = [
  142. (index, payload)
  143. for index, name, payload in events
  144. if name in {
  145. "audit_video_discovery_process",
  146. "audit_video_discovery_run",
  147. }
  148. ]
  149. if not audit_events:
  150. return "尚未执行完成审计"
  151. audit_index, audit = audit_events[-1]
  152. if audit_index < evaluation_index:
  153. post_audit_evaluations = [
  154. payload
  155. for index, payload in evaluation_events
  156. if index > audit_index
  157. ]
  158. if not post_audit_evaluations or any(
  159. payload.get("audit_relevant_changed") is not False
  160. for payload in post_audit_evaluations
  161. ):
  162. return (
  163. "最后一次候选评估改变了审计相关状态,尚未重新审计;"
  164. "下一步只调用 audit_video_discovery_run,"
  165. "不要再次保存或查询"
  166. )
  167. if audit_index < max(record_indexes):
  168. return "最后一次搜索页保存尚未重新审计"
  169. if evidence_indexes and audit_index < max(evidence_indexes):
  170. return "最后一次证据获取尚未重新审计"
  171. if audit.get("can_finish") is not True:
  172. violations = audit.get("critical_violations")
  173. if isinstance(violations, list) and violations:
  174. summary = ";".join(str(item) for item in violations[:5])
  175. return f"最后一次审计未通过:{summary}"
  176. return "最后一次审计未通过"
  177. state_events = [
  178. (index, payload)
  179. for index, name, payload in events
  180. if name == "query_video_discovery_state"
  181. ]
  182. if not state_events:
  183. return "尚未查询最终数据库状态"
  184. state_index, state = state_events[-1]
  185. last_changed_evaluation_index = max(
  186. (
  187. index
  188. for index, payload in evaluation_events
  189. if payload.get("audit_relevant_changed") is not False
  190. ),
  191. default=evaluation_index,
  192. )
  193. if state_index < last_changed_evaluation_index:
  194. return "最终数据库状态早于最后一次有效候选变更,请重新查询"
  195. last_assistant = next(
  196. (
  197. message
  198. for message in reversed(messages)
  199. if message.role == Role.ASSISTANT and not message.tool_calls
  200. ),
  201. None,
  202. )
  203. if last_assistant is None or not (last_assistant.content or "").strip():
  204. return "最终回答为空"
  205. report_error = _validate_report_buckets(last_assistant.content, state)
  206. if report_error:
  207. return report_error
  208. return None
  209. def create_find_agent(
  210. settings: Settings | None = None,
  211. *,
  212. model: str | None = None,
  213. ) -> Agent:
  214. """创建 find_agent 实例,注册所有相关工具。"""
  215. agent = Agent(
  216. settings=settings,
  217. name="find_agent",
  218. model="google/gemini-3-flash-preview",
  219. system_prompt=FIND_AGENT_SYSTEM_PROMPT,
  220. max_iterations=60,
  221. temperature=0.2,
  222. # 暂停启用完成守卫;函数保留,便于后续按需恢复。
  223. completion_guard=None,
  224. tool_call_budgets={
  225. "search": (
  226. {
  227. "douyin_search",
  228. "douyin_search_tikhub",
  229. "douyin_user_videos",
  230. },
  231. 10,
  232. ),
  233. "detail": ({"douyin_detail"}, 2),
  234. "portrait": (
  235. {
  236. "get_content_fans_portrait",
  237. "get_account_fans_portrait",
  238. "batch_fetch_portraits",
  239. },
  240. 3,
  241. ),
  242. "video_analysis": ({"qwen_video_analyze"}, 3),
  243. "evaluation_save": (
  244. {"batch_save_video_candidate_evaluations"},
  245. 6,
  246. ),
  247. "state_query": ({"query_video_discovery_state"}, 8),
  248. },
  249. tool_repeat_requires_change={
  250. "query_video_discovery_state": {
  251. "record_video_search_page",
  252. "batch_save_video_candidate_evaluations",
  253. },
  254. },
  255. )
  256. # 本 Agent 专属工具
  257. register_all_tools(agent.tools)
  258. return agent