> ## Documentation Index
> Fetch the complete documentation index at: https://docs.befailproof.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# उदाहरण

> Claude Code और Agents SDK के लिए hooks सेट अप करने का तरीका

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

***

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

Failproof AI Claude Code के साथ इसके [hooks system](https://docs.anthropic.com/en/docs/claude-code/hooks) के माध्यम से एकीकृत होता है। जब आप `failproofai policies --install` चलाते हैं, तो यह Claude Code के `settings.json` में hook कमांड्स को रजिस्टर करता है जो हर tool कॉल पर चलते हैं।

<Steps>
  <Step title="failproofai इंस्टॉल करें">
    ```bash theme={null}
    npm install -g failproofai
    ```
  </Step>

  <Step title="सभी built-in policies सक्षम करें">
    ```bash theme={null}
    failproofai policies --install
    ```
  </Step>

  <Step title="Hooks रजिस्टर हैं यह सत्यापित करें">
    ```bash theme={null}
    cat ~/.claude/settings.json | grep failproofai
    ```

    आपको `PreToolUse`, `PostToolUse`, `Notification`, और `Stop` events के लिए hook entries दिखाई देने चाहिए।
  </Step>

  <Step title="Claude Code चलाएं">
    ```bash theme={null}
    claude
    ```

    Policies अब हर tool कॉल पर स्वचालित रूप से चलते हैं। Claude से `sudo rm -rf /` चलाने के लिए कहने का प्रयास करें - यह ब्लॉक किया जाएगा।
  </Step>
</Steps>

***

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

यदि आप [Agents SDK](https://docs.anthropic.com/en/docs/agents-sdk) के साथ निर्माण कर रहे हैं, तो आप समान hook system को प्रोग्रामेटिक रूप से उपयोग कर सकते हैं।

<Steps>
  <Step title="अपने प्रोजेक्ट में failproofai इंस्टॉल करें">
    ```bash theme={null}
    npm install failproofai
    ```
  </Step>

  <Step title="अपने agent में hooks कॉन्फ़िगर करें">
    अपने agent process को बनाते समय hook कमांड्स पास करें। Hooks Claude Code की तरह ही काम करते हैं - stdin/stdout JSON के माध्यम से:

    ```bash theme={null}
    failproofai --hook PreToolUse   # प्रत्येक tool से पहले कॉल किया जाता है
    failproofai --hook PostToolUse  # प्रत्येक tool के बाद कॉल किया जाता है
    ```
  </Step>

  <Step title="अपने agent के लिए एक custom policy लिखें">
    ```javascript theme={null}
    import { customPolicies, allow, deny } from "failproofai";

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

  <Step title="Custom policy इंस्टॉल करें">
    ```bash theme={null}
    failproofai policies --install --custom ./my-agent-policies.js
    ```
  </Step>
</Steps>

***

## विनाशकारी कमांड्स को ब्लॉक करें

सबसे सामान्य सेटअप - agents को अपरिवर्तनीय नुकसान से बचाएं।

```bash theme={null}
failproofai policies --install block-sudo block-rm-rf block-force-push block-curl-pipe-sh
```

यह क्या करता है:

* `block-sudo` - सभी `sudo` कमांड्स को ब्लॉक करता है
* `block-rm-rf` - recursive file deletion को ब्लॉक करता है
* `block-force-push` - `git push --force` को ब्लॉक करता है
* `block-curl-pipe-sh` - दूरस्थ scripts को shell में piping करने को ब्लॉक करता है

***

## Secret leakage को रोकें

Agents को tool output में credentials को देखने या leak करने से रोकें।

```bash theme={null}
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 में भेजें।

```javascript theme={null}
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 को ब्लॉक न करें
    }

    return allow();
  },
});
```

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

```bash theme={null}
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js
```

***

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

Agents को branches को switch करने या protected branches में push करने से रोकें।

```javascript theme={null}
import { customPolicies, allow, deny } from "failproofai";

customPolicies.add({
  name: "stay-on-branch",
  description: "Agent को अन्य branches को checkout करने से रोकें",
  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 चलाने के लिए याद दिलाएं।

```javascript theme={null}
import { customPolicies, allow, instruct } from "failproofai";

customPolicies.add({
  name: "test-before-commit",
  description: "Agent को commit से पहले tests चलाने के लिए याद दिलाएं",
  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` बनाएं:

```json theme={null}
{
  "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 करें:

```bash theme={null}
git add .failproofai/policies-config.json
git commit -m "Add failproofai team policies"
```

हर team member जिसके पास failproofai इंस्टॉल है, ये rules स्वचालित रूप से pick up करेगा।

***

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

सबसे प्रभावशाली सेटअप: अपने repo में `.failproofai/policies/` को commit करें जिसमें आपके project के लिए tailored policies हों। हर team member को वे स्वचालित रूप से मिलते हैं — कोई install कमांड नहीं, कोई config changes नहीं।

<Steps>
  <Step title="डायरेक्टरी बनाएं और policies जोड़ें">
    ```bash theme={null}
    mkdir -p .failproofai/policies
    ```

    ```js theme={null}
    // .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 चलाने के लिए याद दिलाएं
    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();
      },
    });
    ```
  </Step>

  <Step title="Git में commit करें">
    ```bash theme={null}
    git add .failproofai/policies/
    git commit -m "Add team quality policies"
    ```
  </Step>

  <Step title="सुधार करते रहें">
    जब आपकी team को नई failure modes का सामना करना पड़े, policies जोड़ें और push करें। हर कोई अपने अगले `git pull` पर update प्राप्त करेगा। ये policies एक living quality standard बन जाते हैं जो आपकी team के साथ बढ़ते हैं।
  </Step>
</Steps>

***

## और उदाहरण

Repo में [`examples/`](https://github.com/failproofai/failproofai/tree/main/examples) डायरेक्टरी में शामिल हैं:

| File                         | यह क्या दिखाता है                                                                  |
| ---------------------------- | ---------------------------------------------------------------------------------- |
| `policies-basic.js`          | Starter policies - production writes, force-push, piped scripts को block करें      |
| `policies-notification.js`   | Idle notifications और session end के लिए Slack alerts                              |
| `policies-advanced/index.js` | Transitive imports, async hooks, PostToolUse output scrubbing, Stop event handling |
