技能系统架构
每个技能是一个独立进程,通过 JSON-RPC 2.0 over HTTP 与 OpenClaw Gateway 通信。Gateway 作为 AI 大脑,在适当时机自动调用技能并将结果融入对话。
1
用户对话
用户向 AI 提出需要技能能力的请求
2
Gateway 路由
AI 判断需要调用哪个技能,Gateway 转发请求
3
技能执行
技能进程处理请求,返回结构化结果
4
结果整合
AI 将技能结果融入最终回复返回用户
SKILL.md
技能声明文件。描述技能的元信息、输入/输出 Schema、调用示例。Gateway 读取此文件了解如何调用技能。
必须
Handler 实现
任意语言实现的 HTTP 服务,监听指定端口,响应 Gateway 的 JSON-RPC 调用请求。
必须
start.sh / start.ps1
技能启动脚本。Gateway 通过此脚本启动技能进程,也可手动运行进行调试。
推荐
tests/
技能单元测试和集成测试。发布前需通过所有测试,市场审核时也会运行此目录。
发布必须
5 分钟快速开始
使用 CLI 工具快速搭建技能脚手架,然后实现你的业务逻辑。
以下以 Node.js 为例。Python 和 .NET 版本的脚手架代码风格相似,仅语言语法不同。
# 1. 生成技能脚手架
openclaw skill new my-skill --lang node
# 2. 进入目录
cd my-skill && npm install
# 3. 启动技能进程(监听 18800)
npm start
# 另一个终端:注册到 Gateway
openclaw skill install ./
# 验证注册成功
openclaw skill invoke my-skill '{"input":"test"}'
openclaw skill new my-skill --lang node
# 2. 进入目录
cd my-skill && npm install
# 3. 启动技能进程(监听 18800)
npm start
# 另一个终端:注册到 Gateway
openclaw skill install ./
# 验证注册成功
openclaw skill invoke my-skill '{"input":"test"}'
SKILL.md 文件结构
SKILL.md 是一个增强的 Markdown 文件,包含 YAML Front Matter(机器可读)和 Markdown 正文(人类可读)。两部分都有实际作用,缺一不可。
---
id: web-search
name: 网页搜索
version: 2.1.0
author: your-name
description: 实时搜索互联网内容,返回摘要和引用链接
license: MIT
tags: [search, web, realtime]
runtime:
language: node
entry: src/index.js
port: 18800
input:
type: object
properties:
query:
type: string
description: 搜索关键词或完整问题
maxResults:
type: integer
default: 5
required: [query]
output:
type: object
properties:
results: {type: array}
summary: {type: string}
examples:
- input: {query: "PithClaw 最新版本"}
output: {summary: "PithClaw 最新版本为 v1.2..."}
---
# 网页搜索技能
实时搜索互联网内容,支持自然语言查询。
## 使用场景
- 获取最新新闻和事件
- 查找技术文档和教程
- 验证实时数据(价格、天气等)
id: web-search
name: 网页搜索
version: 2.1.0
author: your-name
description: 实时搜索互联网内容,返回摘要和引用链接
license: MIT
tags: [search, web, realtime]
runtime:
language: node
entry: src/index.js
port: 18800
input:
type: object
properties:
query:
type: string
description: 搜索关键词或完整问题
maxResults:
type: integer
default: 5
required: [query]
output:
type: object
properties:
results: {type: array}
summary: {type: string}
examples:
- input: {query: "PithClaw 最新版本"}
output: {summary: "PithClaw 最新版本为 v1.2..."}
---
# 网页搜索技能
实时搜索互联网内容,支持自然语言查询。
## 使用场景
- 获取最新新闻和事件
- 查找技术文档和教程
- 验证实时数据(价格、天气等)
Front Matter 字段参考
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | string | 必填 | 全局唯一 ID,kebab-case,用于 API 路由和注册表 |
| name | string | 必填 | 技能显示名称,支持中文 |
| version | semver | 必填 | 遵循 SemVer,格式 x.y.z |
| description | string | 必填 | 一句话描述技能功能,AI 用此决定何时调用 |
| runtime.language | string | 必填 | node / python / dotnet / go / any |
| runtime.port | integer | 必填 | 技能进程监听端口,18800+ 段,不得与其他技能冲突 |
| input | JSON Schema | 必填 | 输入参数的 JSON Schema 定义,Gateway 用于参数校验 |
| output | JSON Schema | 必填 | 输出结构的 JSON Schema,AI 用于理解返回值含义 |
| tags | string[] | 可选 | 分类标签,用于技能市场搜索和过滤 |
| examples | array | 推荐 | 输入/输出示例对,有助于 AI 更准确地调用技能 |
| permissions | string[] | 可选 | 声明所需权限,如 network / filesystem / subprocess |
JSON-RPC 通信协议
Gateway 通过标准 JSON-RPC 2.0 over HTTP POST 调用技能。技能进程只需实现一个 HTTP 端点即可。
// 请求: POST http://localhost:18800/invoke
{
"jsonrpc": "2.0",
"method": "invoke",
"id": "call-001",
"params": {
"skillId": "web-search",
"input": { "query": "PithClaw 最新版本" },
"context": { "sessionId": "sess-xyz" }
}
}
// 成功响应
{
"jsonrpc": "2.0",
"id": "call-001",
"result": {
"summary": "PithClaw 最新版本为 v1.2.0,发布于...",
"results": [...]
}
}
// 错误响应
{
"jsonrpc": "2.0",
"id": "call-001",
"error": { "code": -32603, "message": "Search API timeout" }
}
{
"jsonrpc": "2.0",
"method": "invoke",
"id": "call-001",
"params": {
"skillId": "web-search",
"input": { "query": "PithClaw 最新版本" },
"context": { "sessionId": "sess-xyz" }
}
}
// 成功响应
{
"jsonrpc": "2.0",
"id": "call-001",
"result": {
"summary": "PithClaw 最新版本为 v1.2.0,发布于...",
"results": [...]
}
}
// 错误响应
{
"jsonrpc": "2.0",
"id": "call-001",
"error": { "code": -32603, "message": "Search API timeout" }
}
Node.js Handler 最小实现
const http = require('http');
const PORT = process.env.SKILL_PORT ?? 18800;
async function invoke(input, context) {
// 在这里实现你的技能逻辑
const { query } = input;
// ... 调用外部 API、处理数据 ...
return {
summary: `搜索 "${query}" 的结果:...`,
results: []
};
}
// JSON-RPC 2.0 HTTP 处理器
http.createServer(async (req, res) => {
if (req.method !== 'POST') {
res.writeHead(405); res.end(); return;
}
let body = '';
req.on('data', d => body += d);
req.on('end', async () => {
const rpc = JSON.parse(body);
try {
const result = await invoke(rpc.params.input, rpc.params.context);
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify({jsonrpc:'2.0',id:rpc.id,result}));
} catch (e) {
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify({jsonrpc:'2.0',id:rpc.id,
error:{code:-32603,message:e.message}}));
}
});
}).listen(PORT, () => console.log(`Skill listening on :${PORT}`));
const PORT = process.env.SKILL_PORT ?? 18800;
async function invoke(input, context) {
// 在这里实现你的技能逻辑
const { query } = input;
// ... 调用外部 API、处理数据 ...
return {
summary: `搜索 "${query}" 的结果:...`,
results: []
};
}
// JSON-RPC 2.0 HTTP 处理器
http.createServer(async (req, res) => {
if (req.method !== 'POST') {
res.writeHead(405); res.end(); return;
}
let body = '';
req.on('data', d => body += d);
req.on('end', async () => {
const rpc = JSON.parse(body);
try {
const result = await invoke(rpc.params.input, rpc.params.context);
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify({jsonrpc:'2.0',id:rpc.id,result}));
} catch (e) {
res.writeHead(200, {'Content-Type':'application/json'});
res.end(JSON.stringify({jsonrpc:'2.0',id:rpc.id,
error:{code:-32603,message:e.message}}));
}
});
}).listen(PORT, () => console.log(`Skill listening on :${PORT}`));
本地调试工具
CLI 提供完整的调试工具链,无需部署即可验证技能行为。
# 验证 SKILL.md 格式
openclaw skill validate ./SKILL.md
# 启动技能并进入交互调试模式
openclaw skill debug ./ --watch
# 手动发送测试请求
openclaw skill invoke web-search '{"query":"test"}' --verbose
# 运行技能自带测试
openclaw skill test ./
# 查看技能日志
openclaw skill logs web-search --follow
openclaw skill validate ./SKILL.md
# 启动技能并进入交互调试模式
openclaw skill debug ./ --watch
# 手动发送测试请求
openclaw skill invoke web-search '{"query":"test"}' --verbose
# 运行技能自带测试
openclaw skill test ./
# 查看技能日志
openclaw skill logs web-search --follow
发布到技能市场
技能市场目前处于内测阶段,发布需申请开发者权限。请通过 Gitee Issue 联系团队获取
SKILL_PUBLISH_KEY。
# 1. 确保所有测试通过
openclaw skill test ./
# 2. 打包技能
openclaw skill pack ./ --output ./dist/web-search-2.1.0.tar.gz
# 3. 登录开发者账号
openclaw auth login
# 4. 发布到市场
openclaw skill publish ./dist/web-search-2.1.0.tar.gz
# 5. 查看发布状态(审核通常在 24h 内完成)
openclaw skill publish-status web-search
openclaw skill test ./
# 2. 打包技能
openclaw skill pack ./ --output ./dist/web-search-2.1.0.tar.gz
# 3. 登录开发者账号
openclaw auth login
# 4. 发布到市场
openclaw skill publish ./dist/web-search-2.1.0.tar.gz
# 5. 查看发布状态(审核通常在 24h 内完成)
openclaw skill publish-status web-search