nanobot 源码解析(五):Skills 系统——让 AI 秒变专家
在传统的 AI 框架中,如果你想让 Agent 学会使用一个新工具(比如查询天气、操作 GitHub),通常需要写复杂的 Python 类、定义 JSON Schema,还要处理各种回调。
但在 nanobot 中,你只需要写一个 Markdown 文件。
今天我们就来拆解 nanobot 的 Skills(技能)系统,看看它是如何实现“Markdown 即代码”的。
1. 技能的本质:提示词工程
在 nanobot 中,一个技能就是一个文件夹,核心是里面的 SKILL.md。
nanobot内置的skills放在project_path/nanobot/skills目录下,用户自定义的skills放在workspace/.nanobot/skills目录下
以 weather 技能为例:
- 路径:
nanobot/skills/weather/SKILL.md - 内容:它并不包含复杂的 Python 逻辑,而是直接告诉 AI:“如果你想查天气,可以运行
curl wttr.in/城市名”。
这种设计非常聪明:既然大模型已经非常擅长理解文档和编写代码,为什么不直接给它一份文档,让它自己去执行命令呢?
2. 核心机制:渐进式加载(Progressive Loading)
如果把所有技能的详细说明都塞进 Prompt,AI 的上下文窗口很快就会爆掉。nanobot 采用了渐进式加载策略:
第一阶段:技能索引
当 Agent 启动时,SkillsLoader 会扫描所有技能,提取 SKILL.md 顶部的 YAML 元数据(前后两个---包裹),其中下面两个字段最重要:
- name: 技能名
- description: 技能描述,简要说明技能是做什么的
将workspace与built-in skills所有的元数据进行合并,生成一个技能索引(xml语法),添加到大模型的system prompt中:
(... 系统提示词中的其它内容,身份、角色、记忆等)
# Skills
The following skills extend your capabilities. To use a skill, read its SKILL.md file using the read_file tool.
Skills with available="false" need dependencies installed first - you can try installing them with apt/brew.
<skills>
<skill available="true">
<name>baoyu-xhs-images</name>
<description>Generates Xiaohongshu (Little Red Book) infographic series with 11 visual styles and 8 layouts. Breaks content into 1-10 cartoon-style images optimized for XHS engagement. Use when user mentions "小红书图片", "XHS images", "RedNote infographics", "小红书种草", or wants social media infographics for Chinese platforms.</description>
<location>.nanobot/skills/baoyu-xhs-images/SKILL.md</location>
</skill>
<skill available="true">
<name>memory</name>
<description>Two-layer memory system with grep-based recall.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/memory/SKILL.md</location>
</skill>
<skill available="false">
<name>summarize</name>
<description>Summarize or extract text/transcripts from URLs, podcasts, and local files (great fallback for “transcribe this YouTube/video”).</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/summarize/SKILL.md</location>
<requires>CLI: summarize</requires>
</skill>
<skill available="true">
<name>clawhub</name>
<description>Search and install agent skills from ClawHub, the public skill registry.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/clawhub/SKILL.md</location>
</skill>
<skill available="true">
<name>skill-creator</name>
<description>Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/skill-creator/SKILL.md</location>
</skill>
<skill available="false">
<name>github</name>
<description>Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/github/SKILL.md</location>
<requires>CLI: gh</requires>
</skill>
<skill available="false">
<name>tmux</name>
<description>Remote-control tmux sessions for interactive CLIs by sending keystrokes and scraping pane output.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/tmux/SKILL.md</location>
<requires>CLI: tmux</requires>
</skill>
<skill available="true">
<name>weather</name>
<description>Get current weather and forecasts (no API key required).</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/weather/SKILL.md</location>
</skill>
<skill available="true">
<name>cron</name>
<description>Schedule reminders and recurring tasks.</description>
<location>/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/cron/SKILL.md</location>
</skill>
</skills>
第二阶段:按需读取
我们结合代码来梳理skills的加载和运行逻辑
主 Agent 在思考时,只会看到这个索引。如果它发现用户问了天气问题,它会意识到:“哦,我有一个 weather 技能可以用”。
此时,它会调用 read_file 工具去读取那个 SKILL.md 的完整内容。读完之后,它就学会了如何使用 curl 去查天气,然后立即执行。

2026-03-17 15:06:44.196 | INFO | nanobot.agent.loop:_process_message:357 - Processing message from cli:user: 上海今天天气
2026-03-17 15:14:27.222 | INFO | nanobot.agent.loop:_run_agent_loop:230 - Tool call: list_dir({"path": "/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/weather"})
2026-03-17 15:18:53.296 | INFO | nanobot.agent.loop:_run_agent_loop:230 - Tool call: read_file({"path": "/Users/chaoxu.ren/PycharmProjects/nanobot/nanobot/skills/weather/SKILL.md"})
2026-03-17 15:22:42.715 | INFO | nanobot.agent.loop:_run_agent_loop:230 - Tool call: exec({"command": "curl -s \"wttr.in/Shanghai?format=3\""})
2026-03-17 15:27:02.195 | INFO | nanobot.agent.loop:_run_agent_loop:230 - Tool call: exec({"command": "curl -s \"https://api.open-meteo.com/v1/forecast?latitude=31.23&longitude=121.47¤t_weather=true\""})
2026-03-17 15:36:59.862 | INFO | nanobot.agent.loop:_process_message:449 - Response to cli:user: 上海今天的天气目前气温约为 10.6°C,天气状况显示为小雨(WMO 代码 61)。风速约为 10.8 km/h。
3. 技能的“门槛”:依赖检查
有些技能需要特定的环境(比如 github 技能需要安装 gh 命令行工具)。nanobot 在元数据中定义了这些依赖:
# SKILL.md 顶部
metadata: {"nanobot":{"requires":{"bins":["curl"]}}}
SkillsLoader 会在加载前自动检查系统路径中是否存在 curl。如果不存在,这个技能就会被标记为“不可用”,并告诉 AI 缺少什么。这种自愈式的设计大大降低了配置出错的概率。当然,有些 agent 可以自己执行命令去下载对应的依赖。
4. skills的这种设计真的很高级
- 极低的学习成本:
任何会写 Markdown 和简单脚本的人,都能为 nanobot 开发新技能。 - 极致的灵活性:
你可以随时在~/.nanobot/workspace/skills下新建一个文件夹,丢进一个SKILL.md,Agent 瞬间就获得了新能力,无需重启。 - 跨语言能力:
因为技能本质上是教 AI 运行命令,所以你可以教它运行 Python、Node.js、Go 甚至一段复杂的 Shell 脚本。
5. 开发者视角:如何创造一个新技能?
如果你想让 nanobot 学会某个新本事,只需三步:
- 在
skills目录下建个文件夹。 - 写个
SKILL.md,顶部写好name和description。 - 在正文中用 Markdown 告诉 AI 怎么操作(最好带上代码示例)。
⚠️ 可以使用 skill-creator这种skill帮助写新skill**
6. 获取和分享技能:Skills 社区与资源
如果你不想从零开始写,也可以直接从社区获取现成的技能,或者寻找灵感:
- ClawHub:
这是主要的公开 Agent 技能注册中心(Registry)。它支持自然语言搜索,可以让你非常轻松地为你的 Agent 找到并安装各种实用工具。 - SkillsMP:这是一个技能市场平台,提供了丰富的技能供用户选择和使用。
- GitHub:GitHub 上有很多开源的技能项目,你可以根据自己的需求进行选择和使用。
总结
nanobot 的 Skills 系统再次体现了其“以文档为中心”的设计哲学。它充分信任大模型的理解能力,将复杂的逻辑抽象为简单的文档阅读与命令执行。
访问feifeixu 查看更多nanobot源码解析系列文章。

浙公网安备 33010602011771号