返回顶部
o

openclaw-plugin-dev OpenClaw插件开发

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.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
159
下载量
免费
免费
1
收藏
概述
安装方式
版本历史

openclaw-plugin-dev

OpenClaw 插件开发指南

核心概念

钩子机制

OpenClaw 提供多种钩子用于拦截和处理事件:

钩子触发时机用途
llminput在发送 LLM 请求之前捕获请求(提示词、系统提示词、历史消息)
llmoutput
在 LLM 响应完成后 | 捕获响应(助手文本、用量) |
| agent_start | 当 Agent 会话开始时 | 初始化会话状态 |
| agent_end | 当 Agent 会话结束时 | 清理资源并记录统计信息 |

请求-响应关联

使用 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 };

注意事项

钩子生命周期

  • - llmoutput 依赖于正常的会话终止
  • 如果会话被中断(网关重启、用户发送新消息),llmoutput 可能不会触发
  • 需针对中断场景进行设计,避免资源泄漏

调试方法

bash

检查网关日志以确认钩子触发


grep llm_output ~/.openclaw/logs/gateway.log

检查插件加载

grep plugin-name ~/.openclaw/logs/gateway.log

配置位置

在 openclaw.json 中启用插件:

json
{
plugins: {
entries: {
plugin-name: {
enabled: true,
config: {
option1: value1
}
}
}
}
}

示例:LLM API 日志记录器

完整示例请访问 https://github.com/cicadaFang/openclaw-llm-api-logger

功能特性:

  • - 记录所有 LLM API 请求和响应
  • JSONL 格式日志,按日期分割
  • 使用 runId 关联请求和响应
  • 记录 durationMs 和 usage 等指标

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 openclaw-plugin-dev-1775940900 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 openclaw-plugin-dev-1775940900 技能

通过命令行安装

skillhub install openclaw-plugin-dev-1775940900

下载

⬇ 下载 openclaw-plugin-dev v1.0.0(免费)

文件大小: 2.39 KB | 发布时间: 2026-4-12 10:50

v1.0.0 最新 2026-4-12 10:50
Initial release of the OpenClaw Plugin Development Guide.

- Introduces hook mechanisms for plugin development, including llm_input, llm_output, agent_start, and agent_end.
- Documents plugin structure and manifest requirements.
- Provides code templates for request-response correlation, logging, and configuration management.
- Includes notes on hook lifecycle and debugging methods.
- Offers example configuration and a reference LLM API logger plugin.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部