mirror of
https://github.com/shareAI-lab/analysis_claude_code.git
synced 2026-03-22 10:25:41 +08:00
- s03: inject reminder into tool_result instead of mutating history (#37) - s05: SkillLoader uses rglob("SKILL.md") + frontmatter name priority, matching Agent Skills standard (#30, PR #34) - CI: upgrade actions/checkout and actions/setup-node to v6 (#36) - docs: update s05 skill directory structure in all 3 languages
109 lines
3.6 KiB
Markdown
109 lines
3.6 KiB
Markdown
# s05: Skills (技能加载)
|
|
|
|
`s01 > s02 > s03 > s04 > [ s05 ] s06 | s07 > s08 > s09 > s10 > s11 > s12`
|
|
|
|
> *"用到什么知识, 临时加载什么知识"* -- 通过 tool_result 注入, 不塞 system prompt。
|
|
|
|
## 问题
|
|
|
|
你希望智能体遵循特定领域的工作流: git 约定、测试模式、代码审查清单。全塞进系统提示太浪费 -- 10 个技能, 每个 2000 token, 就是 20,000 token, 大部分跟当前任务毫无关系。
|
|
|
|
## 解决方案
|
|
|
|
```
|
|
System prompt (Layer 1 -- always present):
|
|
+--------------------------------------+
|
|
| You are a coding agent. |
|
|
| Skills available: |
|
|
| - git: Git workflow helpers | ~100 tokens/skill
|
|
| - test: Testing best practices |
|
|
+--------------------------------------+
|
|
|
|
When model calls load_skill("git"):
|
|
+--------------------------------------+
|
|
| tool_result (Layer 2 -- on demand): |
|
|
| <skill name="git"> |
|
|
| Full git workflow instructions... | ~2000 tokens
|
|
| Step 1: ... |
|
|
| </skill> |
|
|
+--------------------------------------+
|
|
```
|
|
|
|
第一层: 系统提示中放技能名称 (低成本)。第二层: tool_result 中按需放完整内容。
|
|
|
|
## 工作原理
|
|
|
|
1. 每个技能是一个目录, 包含 `SKILL.md` 文件和 YAML frontmatter。
|
|
|
|
```
|
|
skills/
|
|
pdf/
|
|
SKILL.md # ---\n name: pdf\n description: Process PDF files\n ---\n ...
|
|
code-review/
|
|
SKILL.md # ---\n name: code-review\n description: Review code\n ---\n ...
|
|
```
|
|
|
|
2. SkillLoader 递归扫描 `SKILL.md` 文件, 用目录名作为技能标识。
|
|
|
|
```python
|
|
class SkillLoader:
|
|
def __init__(self, skills_dir: Path):
|
|
self.skills = {}
|
|
for f in sorted(skills_dir.rglob("SKILL.md")):
|
|
text = f.read_text()
|
|
meta, body = self._parse_frontmatter(text)
|
|
name = meta.get("name", f.parent.name)
|
|
self.skills[name] = {"meta": meta, "body": body}
|
|
|
|
def get_descriptions(self) -> str:
|
|
lines = []
|
|
for name, skill in self.skills.items():
|
|
desc = skill["meta"].get("description", "")
|
|
lines.append(f" - {name}: {desc}")
|
|
return "\n".join(lines)
|
|
|
|
def get_content(self, name: str) -> str:
|
|
skill = self.skills.get(name)
|
|
if not skill:
|
|
return f"Error: Unknown skill '{name}'."
|
|
return f"<skill name=\"{name}\">\n{skill['body']}\n</skill>"
|
|
```
|
|
|
|
3. 第一层写入系统提示。第二层不过是 dispatch map 中的又一个工具。
|
|
|
|
```python
|
|
SYSTEM = f"""You are a coding agent at {WORKDIR}.
|
|
Skills available:
|
|
{SKILL_LOADER.get_descriptions()}"""
|
|
|
|
TOOL_HANDLERS = {
|
|
# ...base tools...
|
|
"load_skill": lambda **kw: SKILL_LOADER.get_content(kw["name"]),
|
|
}
|
|
```
|
|
|
|
模型知道有哪些技能 (便宜), 需要时再加载完整内容 (贵)。
|
|
|
|
## 相对 s04 的变更
|
|
|
|
| 组件 | 之前 (s04) | 之后 (s05) |
|
|
|----------------|------------------|--------------------------------|
|
|
| Tools | 5 (基础 + task) | 5 (基础 + load_skill) |
|
|
| 系统提示 | 静态字符串 | + 技能描述列表 |
|
|
| 知识库 | 无 | skills/\*/SKILL.md 文件 |
|
|
| 注入方式 | 无 | 两层 (系统提示 + result) |
|
|
|
|
## 试一试
|
|
|
|
```sh
|
|
cd learn-claude-code
|
|
python agents/s05_skill_loading.py
|
|
```
|
|
|
|
试试这些 prompt (英文 prompt 对 LLM 效果更好, 也可以用中文):
|
|
|
|
1. `What skills are available?`
|
|
2. `Load the agent-builder skill and follow its instructions`
|
|
3. `I need to do a code review -- load the relevant skill first`
|
|
4. `Build an MCP server using the mcp-builder skill`
|