"""skill 正文直接写进系统提示
skill 总共几 KB,每次调用都会命中提示缓存;如果让模型自己 read_file,每个请求要多 2 次串行调用,
每次几秒,比多带这几 KB 贵得多。需要附件(references/)的 skill 出现后,再改回按需读取
"""
from pathlib import Path
SKILLS_ROOT = Path(__file__).resolve().parents[2] / "skills"
def _split_frontmatter(text: str) -> tuple[dict[str, str], str]:
_, header, body = text.split("---", 2)
meta = {}
for line in header.strip().splitlines():
key, _, value = line.partition(":")
meta[key.strip()] = value.strip()
return meta, body.strip()
def skills_prompt(root: Path = SKILLS_ROOT) -> str:
"""每个 skill 一段:description 说明什么时候用,正文是做法;任务符合描述时按正文做,一个请求可以用到多个"""
sections = []
for path in sorted(root.glob("*/SKILL.md")):
meta, body = _split_frontmatter(path.read_text(encoding="utf-8"))
sections.append(
f'\n适用:{meta["description"]}\n\n{body}\n'
)
return (
"\n以下 skill 提供特定任务的做法,任务符合“适用”时按正文做,一个请求可以用到多个\n\n"
+ "\n\n".join(sections)
+ "\n"
)