agent_catalog.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Read-only catalog for the business Agents shown in the web console."""
  2. from __future__ import annotations
  3. from importlib import import_module
  4. from pathlib import Path
  5. from typing import Any
  6. from supply_infra.agent_prompt_injection import (
  7. get_agent_document_injection,
  8. save_agent_document_injection,
  9. )
  10. _PROJECT_ROOT = Path(__file__).resolve().parents[2]
  11. _AGENTS: dict[str, dict[str, str]] = {
  12. "demand_belong_category_agent": {
  13. "display_name": "分配 Agent",
  14. "subtitle": "语义归属与分类挂载",
  15. "prompt_path": "agents/demand_belong_category_agent/prompt/system_prompt.md",
  16. "tools_module": "agents.demand_belong_category_agent.tools",
  17. },
  18. "demand_grade_agent": {
  19. "display_name": "分等级 Agent",
  20. "subtitle": "证据融合与优先级判断",
  21. "prompt_path": "agents/demand_grade_agent/prompt/system_prompt.md",
  22. "tools_module": "agents.demand_grade_agent.tools",
  23. },
  24. "demand_video_expand_agent": {
  25. "display_name": "拓展 Agent",
  26. "subtitle": "视频点位与需求拓展",
  27. "prompt_path": "agents/demand_video_expand_agent/prompt/system_prompt.md",
  28. "tools_module": "agents.demand_video_expand_agent.tools",
  29. },
  30. }
  31. def get_agent_detail(agent_name: str) -> dict[str, Any] | None:
  32. """Return the current system prompt and registered tool definitions."""
  33. spec = _AGENTS.get(agent_name)
  34. if spec is None:
  35. return None
  36. prompt_path = _PROJECT_ROOT / spec["prompt_path"]
  37. prompt = prompt_path.read_text(encoding="utf-8")
  38. tools_module = import_module(spec["tools_module"])
  39. tools = []
  40. for tool_fn in tools_module.ALL_TOOLS:
  41. tools.append(
  42. {
  43. "name": getattr(tool_fn, "_tool_name", tool_fn.__name__),
  44. "description": getattr(
  45. tool_fn,
  46. "_tool_description",
  47. (tool_fn.__doc__ or "").strip(),
  48. ),
  49. "parameters": getattr(tool_fn, "_tool_parameters", {}),
  50. }
  51. )
  52. return {
  53. "name": agent_name,
  54. "display_name": spec["display_name"],
  55. "subtitle": spec["subtitle"],
  56. "system_prompt": prompt,
  57. "document_injection": get_agent_document_injection(agent_name),
  58. "tools": tools,
  59. }
  60. def update_agent_document_injection(
  61. agent_name: str,
  62. *,
  63. content: str,
  64. enabled: bool,
  65. ) -> dict[str, Any] | None:
  66. """Persist the editable document injection for an allow-listed Agent."""
  67. if agent_name not in _AGENTS:
  68. return None
  69. return save_agent_document_injection(
  70. agent_name,
  71. content=content,
  72. enabled=enabled,
  73. )