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

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

verify करें कि 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 / चलाने के लिए कहने का प्रयास करें - यह block हो जाएगा।

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

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

अपनी project में failproofai इंस्टॉल करें

npm install failproofai
2

अपने agent में hooks configure करें

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

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

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

customPolicies.add({
  name: "limit-to-project-dir",
  description: "Keep the agent inside the 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 is restricted to the project directory");
    }
    return allow();
  },
});
4

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

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

विनाशकारी commands को block करें

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

secret leakage को रोकें

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

जब agents को ध्यान देने की आवश्यकता हो तो Slack alerts प्राप्त करें

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

customPolicies.add({
  name: "slack-on-idle",
  description: "Alert Slack when the agent is waiting for input",
  match: { events: ["Notification"] },
  fn: async (ctx) => {
    const webhookUrl = process.env.SLACK_WEBHOOK_URL;
    if (!webhookUrl) return allow();

    const message = String(ctx.payload?.message ?? "Agent is waiting");
    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 ones में push करने से रोकें।
import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "stay-on-branch",
  description: "Prevent the agent from checking out other branches",
  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("Stay on the current branch. Create a new branch with -b if needed.");
    }
    return allow();
  },
});

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

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

customPolicies.add({
  name: "test-before-commit",
  description: "Remind the agent to run tests before committing",
  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("Run tests before committing. Use `npm test` or `bun test` first.");
    }
    return allow();
  },
});

एक production repo को lock down करें

एक project-level config को commit करें ताकि आपकी टीम के हर 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"
जिस भी टीम member के पास failproofai installed है वह स्वचालित रूप से ये rules को pick up करेगा।

अधिक उदाहरण

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