← 返回 Skills 市场
hanxiao-bot

Hook Examples

作者 hanxiao-bot · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
114
总下载
0
收藏
1
当前安装
1
版本数
在 OpenClaw 中安装
/install hook-examples
功能描述
Provides code examples demonstrating uses of various OpenClaw hooks to intercept, modify, validate, or block operations at different execution stages.
使用说明 (SKILL.md)

Hook Examples - OpenClaw Hook Usage Examples

Overview

OpenClaw supports 16+ Hook types that can intercept and modify behavior at various stages.

Available Hooks

Hook Timing Can Block?
before_model_resolve Before model resolution
before_prompt_build Before prompt building
before_tool_call Before tool call
after_tool_call After tool call
message_sending Before message sending ✅ cancel
message_sent After message sent
subagent_spawning Before subagent spawn
subagent_ended After subagent ended

Example 1: Tool Audit Log

Record all tool calls to a file:

api.registerHook("before_tool_call", async ({ event, ctx }) => {
  const log = {
    time: new Date().toISOString(),
    tool: event.tool.name,
    params: event.tool.params,
    session: ctx.sessionKey
  };
  // Write to log file
  console.log("[TOOL_AUDIT]", JSON.stringify(log));
  return {}; // Don't block, continue execution
});

Example 2: Dangerous Tool Interception

Block dangerous tools in non-elevated sessions:

api.registerHook("before_tool_call", async ({ event, ctx }) => {
  const dangerous = ["gateway", "cron", "nodes"];
  if (dangerous.includes(event.tool.name) && !ctx.session.elevated) {
    return { 
      block: true, 
      reason: `Tool '${event.tool.name}' requires elevated permissions` 
    };
  }
  return {};
});

Example 3: Parameter Validation

Validate dangerous commands in exec tool:

api.registerHook("before_tool_call", async ({ event, ctx }) => {
  if (event.tool.name === "exec") {
    const cmd = event.tool.params.command || "";
    const dangerous = ["rm -rf", "dd if=", "mkfs", ":(){:|:&}:"];
    for (const d of dangerous) {
      if (cmd.includes(d)) {
        return { 
          block: true, 
          reason: `Dangerous command pattern detected: ${d}` 
        };
      }
    }
  }
  return {};
});

Example 4: Dynamic Model Switching

Switch models based on task type:

api.registerHook("before_model_resolve", async ({ event, ctx }) => {
  const msg = event.messages?.[0]?.content || "";
  if (msg.includes("write code") || msg.includes("debug")) {
    return { 
      model: "ollama/deepseek-r1:70b",
      provider: "ollama"
    };
  }
  if (msg.includes("document") || msg.includes("summary")) {
    return { 
      model: "ollama/qwen3:14b",
      provider: "ollama"
    };
  }
  return {};
});

Example 5: Subagent Result Routing

Custom subagent result delivery:

api.registerHook("subagent_ended", async ({ event, ctx }) => {
  // Do extra processing here
  console.log("[SUBAGENT_ENDED]", event.result);
  return {}; 
});

Registration

Register in the plugin's register(api):

export default definePluginEntry({
  id: "my-hook-plugin",
  name: "My Hook Plugin",
  register(api) {
    api.registerHook("before_tool_call", myHandler);
  }
});

Notes

  • Returning { block: true } from a hook blocks the operation
  • before_model_resolve can return { model, provider } to override
  • Hooks are synchronous; avoid long-running operations
  • Multiple hooks execute in priority order
安全使用建议
This is a coherent examples-only skill — it doesn't install code or ask for secrets. Before using any snippet in your environment: (1) avoid logging raw tool params or session data (they may include secrets); sanitize or redact sensitive fields before writing logs; (2) test blocking rules carefully to avoid unintended denial of legitimate operations; (3) be cautious with dynamic model/provider switching (ensure you trust the target provider and model); and (4) don't paste these snippets into production unchanged — add input validation, rate-limiting/timeouts, and error handling as needed.
功能分析
Type: OpenClaw Skill Name: hook-examples Version: 1.0.0 The bundle consists of documentation and code examples for implementing OpenClaw hooks. The examples provided in SKILL.md are educational and focus on security-enhancing practices, such as auditing tool calls, blocking dangerous tools (e.g., cron, gateway), and sanitizing shell commands against destructive patterns like 'rm -rf'.
能力评估
Purpose & Capability
Name/description (hook examples) match the provided content: the SKILL.md contains multiple hook examples (before_tool_call, before_model_resolve, subagent_ended, etc.). No unrelated binaries, env vars, or install steps are requested.
Instruction Scope
Instructions stay within the domain of hook usage, but examples include logging full tool params and session context and demonstrate blocking tools and switching provider/models. Logging tool parameters may capture sensitive data (e.g., commands or credentials passed to tools) — the docs show console.log and a comment about writing to a file, so users should avoid copying those snippets into production without sanitization.
Install Mechanism
Instruction-only skill with no install spec or code files. This is the lowest-risk presentation — nothing is downloaded or written to disk by the skill itself.
Credentials
The skill declares no required environment variables, credentials, or config paths. The examples reference ctx.session and event payloads that are expected in hook handlers — there is no disproportionate credential access requested.
Persistence & Privilege
always is false and the skill is user-invocable. There is no evidence this skill attempts to modify other skills or request persistent elevated privileges.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install hook-examples
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /hook-examples 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
- Initial release with example usage of 16+ OpenClaw Hook types. - Provides code samples for tool audit logging, blocking dangerous tools, parameter validation, dynamic model switching, and subagent result routing. - Includes hook registration and behavioral control documentation. - Outlines timing and blocking capabilities for supported hooks.
元数据
Slug hook-examples
版本 1.0.0
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 1
常见问题

Hook Examples 是什么?

Provides code examples demonstrating uses of various OpenClaw hooks to intercept, modify, validate, or block operations at different execution stages. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 114 次。

如何安装 Hook Examples?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install hook-examples」即可一键安装,无需额外配置。

Hook Examples 是免费的吗?

是的,Hook Examples 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Hook Examples 支持哪些平台?

Hook Examples 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Hook Examples?

由 hanxiao-bot(@hanxiao-bot)开发并维护,当前版本 v1.0.0。

💬 留言讨论