mcpField
L
llm-gate

llm-gate

github.com/rohitg00测试与质量2.9k142 次安装4 周前更新
代码仓库

关于 llm-gate

llm-gate 是用于 Claude Code 的技能,可为工具操作添加 LLM 检查。它通过提示词或 Agent 钩子验证操作,并按条件阻止不合规的调用。

主要功能

  1. 01通过提示词钩子检查质量
  2. 02使用 Agent 钩子进行复杂审查
  3. 03按条件匹配操作
  4. 04配置模型和超时
  5. 05不合规时阻止工具调用

适用场景

  1. 01验证 Conventional Commits 提交信息
  2. 02标记有风险的破坏性命令
  3. 03检查文件写入中的泄露密钥

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rohitg00/pro-workflow --skill llm-gate

安装后在 Claude Code 中输入/llm-gate确认已加载。

详细介绍

LLM Gate

使用 Claude Code 的 type: "prompt" 钩子创建智能质量门,通过 AI 验证操作。

触发场景

适用于:

  • 设置提交信息验证
  • 强制执行超出代码检查工具检查范围的代码规范
  • 为特定操作创建智能防护机制

提示词钩子的工作方式

Claude Code 支持使用 type: "prompt" 钩子,通过小型大模型(默认使用 Haiku)验证条件:

{
  "PreToolUse": [{
    "matcher": "Bash",
    "hooks": [{
      "type": "prompt",
      "if": "Bash(git commit*)",
      "prompt": "Check if this git commit follows conventional commit format (<type>(<scope>): <summary>). The commit command is: $ARGUMENTS. Return {\"ok\": true} if valid, {\"ok\": false, \"reason\": \"...\"} if not.",
      "model": "haiku",
      "timeout": 15
    }]
  }]
}

该钩子会:

  1. 将 $ARGUMENTS 替换为 JSON 钩子输入
  2. 发送给 Haiku(速度快、成本低)
  3. 期待返回 {"ok": true} 或 {"ok": false, "reason": "..."}
  4. 如果结果不是 ok → 使用原因阻止工具调用

示例质量门

常规提交信息验证器
{
  "type": "prompt",
  "if": "Bash(git commit*)",
  "prompt": "Verify this git commit follows conventional commits: type(scope): summary. Types: feat,fix,refactor,test,docs,chore,perf,ci. Summary under 72 chars. Input: $ARGUMENTS",
  "model": "haiku"
}
破坏性命令防护
{
  "type": "prompt",
  "if": "Bash(rm *)",
  "prompt": "Check if this rm command is safe. Flag if it uses -rf on important directories (src/, node_modules/, .git/). Input: $ARGUMENTS",
  "model": "haiku"
}
API 密钥泄漏防护
{
  "type": "prompt",
  "matcher": "Write",
  "prompt": "Check if this file write contains hardcoded API keys, secrets, passwords, or tokens. Input: $ARGUMENTS. Return ok:false if secrets found.",
  "model": "haiku"
}

Agent 钩子

对于复杂验证,请使用 type: "agent"(运行完整 Agent):

{
  "type": "agent",
  "if": "Bash(git push*)",
  "prompt": "Review all staged changes for security issues before pushing. Check for: hardcoded secrets, SQL injection, XSS vulnerabilities, exposed internal URLs.",
  "model": "haiku",
  "timeout": 60
}

设置指南

  1. 选择要设置质量门的操作
  2. 编写提示词(保持精简,不超过 100 个单词)
  3. 选择模型(追求速度选 haiku,追求准确性选 sonnet)
  4. 设置超时(提示词为 15s,Agent 为 60s)
  5. 将其添加到 hooks.json 中对应的事件下

规则

  • 简单检查使用 Haiku(速度快、成本低)
  • 仅在复杂分析时使用 Sonnet
  • 提示词保持在 100 个单词以内,以确保可靠性
  • 始终包含 if 条件,避免每次工具调用都运行
  • 设置合理的超时时间(提示词 15s,Agent 60s)
  • 部署前测试钩子,避免阻塞工作流

评论

支持 Markdown