- 将多智能体/子智能体prompt从代码提取到prompts/multi_agent/和prompts/sub_agent/ - 多智能体模式添加可用的子智能体动态prompt并冻结 - 重启后自动从conversation.json恢复多智能体idle任务 - 修复新对话侧边栏显示其他对话子智能体的问题 - 简化子智能体弹窗输出样式 - sleep工具在多智能体模式下移除wait_sub_agent_ids参数
76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
"""子智能体提示词构建。
|
|
|
|
所有 prompt 正文均从 prompts/sub_agent/ 下的文本文件加载,避免在代码中硬编码。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import platform
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
try:
|
|
from config import PROMPTS_DIR
|
|
except ImportError:
|
|
import sys
|
|
|
|
project_root = Path(__file__).resolve().parents[2]
|
|
if str(project_root) not in sys.path:
|
|
sys.path.insert(0, str(project_root))
|
|
from config import PROMPTS_DIR
|
|
|
|
|
|
_SUB_AGENT_PROMPTS_DIR = Path(PROMPTS_DIR) / "sub_agent"
|
|
_TEMPLATE_CACHE: dict[str, str] = {}
|
|
|
|
|
|
def _load_template(name: str) -> str:
|
|
"""从 prompts/sub_agent/<name>.txt 加载模板,带缓存。"""
|
|
cached = _TEMPLATE_CACHE.get(name)
|
|
if cached is not None:
|
|
return cached
|
|
|
|
template_path = _SUB_AGENT_PROMPTS_DIR / f"{name}.txt"
|
|
if not template_path.exists():
|
|
raise FileNotFoundError(f"子智能体 prompt 模板缺失: {template_path}")
|
|
|
|
content = template_path.read_text(encoding="utf-8")
|
|
_TEMPLATE_CACHE[name] = content
|
|
return content
|
|
|
|
|
|
def _format_template(name: str, **kwargs) -> str:
|
|
"""加载模板并用 str.format 填充占位符。"""
|
|
template = _load_template(name)
|
|
return template.format(**kwargs)
|
|
|
|
|
|
def build_user_message(
|
|
agent_id: int,
|
|
summary: str,
|
|
task: str,
|
|
deliverables_path: str,
|
|
timeout_seconds: int,
|
|
) -> str:
|
|
"""构建发送给子智能体的用户消息。"""
|
|
return _format_template(
|
|
"user_message",
|
|
agent_id=agent_id,
|
|
summary=summary,
|
|
task=task,
|
|
deliverables_path=deliverables_path,
|
|
timeout_seconds=timeout_seconds,
|
|
)
|
|
|
|
|
|
def build_system_prompt(workspace_path: str) -> str:
|
|
"""构建子智能体的系统提示。"""
|
|
system_info = f"{platform.system()} {platform.release()}"
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
return _format_template(
|
|
"system",
|
|
workspace_path=workspace_path,
|
|
system_info=system_info,
|
|
current_time=current_time,
|
|
)
|