OpenClaw Plugin Development Guide. For creating OpenClaw plugins, including hook mechanisms, logging, configuration management, etc. Trigger words: develop plugin, create plugin, plugin development, llm_input hook, llm_output hook, wrapStreamFn.
OpenClaw 提供多种钩子用于拦截和处理事件:
| 钩子 | 触发时机 | 用途 |
|---|---|---|
| llminput | 在发送 LLM 请求之前 | 捕获请求(提示词、系统提示词、历史消息) |
| llmoutput |
使用 runId 关联请求和响应:
typescript
const inFlightRequests = new Map
api.on(llm_input, (event: any) => {
const runId = event.runId;
inFlightRequests.set(runId, { timestamp: Date.now(), input: event });
});
api.on(llm_output, (event: any) => {
const runId = event.runId;
const request = inFlightRequests.get(runId);
// 成功配对,记录完整的请求-响应
inFlightRequests.delete(runId);
});
~/.openclaw/extensions/plugin-name/
├── openclaw.plugin.json # 插件清单
├── index.ts # 主入口
├── logger.ts # 工具模块(可选)
└── README.md # 文档(可选)
json
{
id: plugin-name,
name: 插件名称,
version: 1.0.0,
main: index.ts,
description: 插件描述
}
typescript
type OpenClawPluginApi = {
config?: any;
pluginConfig?: unknown;
logger: { info: (msg: string) => void; warn: (msg: string) => void; error: (msg: string) => void };
on: (hookName: string, handler: (event: any, ctx?: any) => void, opts?: { priority?: number }) => void;
};
const plugin = {
id: plugin-name,
name: 插件名称,
description: 插件描述,
register(api: OpenClawPluginApi) {
// 获取配置
const config = api.config?.plugins?.entries?.[plugin-name]?.config ?? {};
// 注册钩子
api.on(llm_input, (event) => { / 处理请求 / });
api.on(llm_output, (event) => { / 处理响应 / });
},
};
export default plugin;
typescript
// JSONL 格式,按日期分割文件
const logPath = path.join(basePath, ${new Date().toISOString().split(T)[0]}.jsonl);
fs.appendFileSync(logPath, JSON.stringify(entry) + \n);
typescript
const DEFAULT_CONFIG = {
enabled: true,
logPath: ~/.openclaw/logs/plugin-name,
};
const config = { ...DEFAULT_CONFIG, ...rawConfig };
bash
在 openclaw.json 中启用插件:
json
{
plugins: {
entries: {
plugin-name: {
enabled: true,
config: {
option1: value1
}
}
}
}
}
完整示例请访问 https://github.com/cicadaFang/openclaw-llm-api-logger
功能特性:
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 openclaw-plugin-dev-1775940900 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 openclaw-plugin-dev-1775940900 技能
skillhub install openclaw-plugin-dev-1775940900
文件大小: 2.39 KB | 发布时间: 2026-4-12 10:50