agent.py 9.3 KB

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