मुख्य सामग्री पर जाएं

title: उदाहरण description: “Claude Code और Agents SDK के लिए hooks कैसे सेट करें” icon: book-open

सामान्य परिदृश्यों के लिए तैयार उदाहरण। प्रत्येक बताता है कि कैसे इंस्टॉल करें और क्या उम्मीद करें।

Claude Code के लिए hooks सेट करना

Failproof AI Claude Code के साथ अपने hooks system के माध्यम से एकीकृत होता है। जब आप failproofai policies --install चलाते हैं, तो यह Claude Code के settings.json में hook commands को पंजीकृत करता है जो हर tool call पर चलते हैं।
1

failproofai इंस्टॉल करें

npm install -g failproofai
2

सभी built-in policies को सक्षम करें

failproofai policies --install
3

सत्यापित करें कि hooks पंजीकृत हैं

cat ~/.claude/settings.json | grep failproofai
आपको PreToolUse, PostToolUse, Notification, और Stop events के लिए hook entries दिखनी चाहिए।
4

Claude Code चलाएं

claude
Policies अब हर tool call पर स्वचालित रूप से चलती हैं। Claude को sudo rm -rf / चलाने के लिए कहने का प्रयास करें - यह अवरुद्ध होगा।

Agents SDK के लिए hooks सेट करना

यदि आप Agents SDK के साथ बना रहे हैं, तो आप समान hook system को प्रोग्राम के अनुसार उपयोग कर सकते हैं।
1

अपने प्रोजेक्ट में failproofai इंस्टॉल करें

npm install failproofai
2

अपने agent में hooks कॉन्फ़िगर करें

अपने agent process बनाते समय hook commands पास करें। Hooks उसी तरह चलते हैं जैसे Claude Code में - stdin/stdout JSON के माध्यम से:
failproofai --hook PreToolUse   # हर tool से पहले कहा जाता है
failproofai --hook PostToolUse  # हर tool के बाद कहा जाता है
3

अपने agent के लिए एक custom policy लिखें

import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "limit-to-project-dir",
  description: "Agent को project directory के अंदर रखें",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    const path = String(ctx.toolInput?.file_path ?? "");
    if (path.startsWith("/") && !path.startsWith(ctx.session?.cwd ?? "")) {
      return deny("Agent project directory तक सीमित है");
    }
    return allow();
  },
});
4

custom policy को इंस्टॉल करें

failproofai policies --install --custom ./my-agent-policies.js

विनाशकारी commands को ब्लॉक करें

सबसे सामान्य सेटअप - agents को अपरिवर्तनीय नुकसान करने से रोकें।
failproofai policies --install block-sudo block-rm-rf block-force-push block-curl-pipe-sh
यह क्या करता है:
  • block-sudo - सभी sudo commands को अवरुद्ध करता है
  • block-rm-rf - recursive file deletion को अवरुद्ध करता है
  • block-force-push - git push --force को अवरुद्ध करता है
  • block-curl-pipe-sh - remote scripts को shell में pipe करने को अवरुद्ध करता है

Secret leakage को रोकें

Agents को tool output में credentials देखने या leak करने से रोकें।
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
ये PostToolUse पर चलती हैं - एक tool चलने के बाद, वे output को agent के देखने से पहले साफ करती हैं।

Agents को attention की आवश्यकता होने पर Slack alerts प्राप्त करें

Notification hook का उपयोग करके idle alerts को Slack में भेजें।
import { customPolicies, allow, instruct } from "failproofai";

customPolicies.add({
  name: "slack-on-idle",
  description: "जब agent input की प्रतीक्षा में हो तो Slack को alert करें",
  match: { events: ["Notification"] },
  fn: async (ctx) => {
    const webhookUrl = process.env.SLACK_WEBHOOK_URL;
    if (!webhookUrl) return allow();

    const message = String(ctx.payload?.message ?? "Agent प्रतीक्षा में है");
    const project = ctx.session?.cwd ?? "unknown";

    try {
      await fetch(webhookUrl, {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          text: `*${message}*\nProject: \`${project}\``,
        }),
        signal: AbortSignal.timeout(5000),
      });
    } catch {
      // अगर Slack अप्राप्य है तो कभी भी agent को block न करें
    }

    return allow();
  },
});
इसे इंस्टॉल करें:
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js

Agents को एक branch पर रखें

Agents को branches को switch करने या protected branches में push करने से रोकें।
import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "stay-on-branch",
  description: "Agent को अन्य branches check out करने से रोकें",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    const cmd = String(ctx.toolInput?.command ?? "");
    if (/git\s+checkout\s+(?!-b)/.test(cmd)) {
      return deny("वर्तमान branch पर रहें। यदि आवश्यक हो तो -b के साथ एक नई branch बनाएं।");
    }
    return allow();
  },
});

Commits से पहले tests की आवश्यकता करें

Agents को commit करने से पहले tests चलाने के लिए remind करें।
import { customPolicies, allow, instruct } from "failproofai";

customPolicies.add({
  name: "test-before-commit",
  description: "Agent को commit करने से पहले tests चलाने के लिए remind करें",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    const cmd = String(ctx.toolInput?.command ?? "");
    if (/git\s+commit/.test(cmd)) {
      return instruct("Commit करने से पहले tests चलाएं। पहले `npm test` या `bun test` का उपयोग करें।");
    }
    return allow();
  },
});

Production repo को lock down करें

एक project-level config को commit करें ताकि आपकी team के हर developer को समान policies मिलें। अपने repo में .failproofai/policies-config.json बनाएं:
{
  "enabledPolicies": [
    "block-sudo",
    "block-rm-rf",
    "block-force-push",
    "block-push-master",
    "block-env-files",
    "sanitize-api-keys",
    "sanitize-jwt"
  ],
  "policyParams": {
    "block-push-master": {
      "protectedBranches": ["main", "release", "production"]
    }
  }
}
फिर इसे commit करें:
git add .failproofai/policies-config.json
git commit -m "Add failproofai team policies"
जिस हर team member के पास failproofai installed है वह स्वचालित रूप से इन नियमों को उठा लेगा।

Convention policies के साथ एक org-wide quality standard बनाएं

सबसे प्रभावशाली सेटअप: .failproofai/policies/ को अपने repo में commit करें अपने project के अनुरूप policies के साथ। हर team member को स्वचालित रूप से ये मिलते हैं — कोई install commands नहीं, कोई config changes नहीं।
1

Directory बनाएं और policies जोड़ें

mkdir -p .failproofai/policies
// .failproofai/policies/team-policies.mjs
import { customPolicies, allow, deny, instruct } from "failproofai";

// अपनी team की preferred package manager को enforce करें
// (या इसके बजाय built-in prefer-package-manager policy को enable करें)
customPolicies.add({
  name: "enforce-bun",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    const cmd = String(ctx.toolInput?.command ?? "");
    if (/\bnpm\b/.test(cmd)) return deny("npm की बजाय bun का उपयोग करें।");
    return allow();
  },
});

// Agent को commit करने से पहले tests चलाने के लिए remind करें
customPolicies.add({
  name: "test-before-commit",
  match: { events: ["PreToolUse"] },
  fn: async (ctx) => {
    if (ctx.toolName !== "Bash") return allow();
    if (/git\s+commit/.test(ctx.toolInput?.command ?? "")) {
      return instruct("Commit करने से पहले tests चलाएं।");
    }
    return allow();
  },
});
2

Git में commit करें

git add .failproofai/policies/
git commit -m "Add team quality policies"
3

Improvement जारी रखें

जैसे-जैसे आपकी team को नए failure modes का सामना करता है, policies जोड़ें और push करें। हर कोई अपने अगले git pull पर update प्राप्त करता है। ये policies एक living quality standard बन जाती हैं जो आपकी team के साथ बढ़ती हैं।

अधिक उदाहरण

Repo में examples/ directory में शामिल हैं:
Fileयह क्या दिखाता है
policies-basic.jsStarter policies - production writes, force-push, piped scripts को block करें
policies-notification.jsIdle notifications और session end के लिए Slack alerts
policies-advanced/index.jsTransitive imports, async hooks, PostToolUse output scrubbing, Stop event handling