> ## 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.

# דוגמאות

> כיצד להגדיר hooks עבור Claude Code ו-Agents SDK

דוגמאות מוכנות לשימוש עבור תרחישים נפוצים. כל אחת מהן מציגה כיצד להתקין ומה לצפות.

***

## הגדרת hooks עבור Claude Code

Failproof AI משתלב עם Claude Code דרך [מערכת ה-hooks](https://docs.anthropic.com/en/docs/claude-code/hooks) שלו. כאשר אתה מריץ `failproofai policies --install`, זה רושם פקודות hook ב-`settings.json` של Claude Code שמופעלות בכל קריאת tool.

<Steps>
  <Step title="התקן את failproofai">
    ```bash theme={null}
    npm install -g failproofai
    ```
  </Step>

  <Step title="הפעל את כל ה-policies המובנים">
    ```bash theme={null}
    failproofai policies --install
    ```
  </Step>

  <Step title="אימות שה-hooks רשומים">
    ```bash theme={null}
    cat ~/.claude/settings.json | grep failproofai
    ```

    אתה אמור לראות רשומות hook עבור אירועים `PreToolUse`, `PostToolUse`, `Notification`, ו-`Stop`.
  </Step>

  <Step title="הרץ את Claude Code">
    ```bash theme={null}
    claude
    ```

    Policies מתבצעים כעת באופן אוטומטי בכל קריאת tool. נסה לבקש מ-Claude להריץ `sudo rm -rf /` - זה ייחסם.
  </Step>
</Steps>

***

## הגדרת hooks עבור Agents SDK

אם אתה בונה עם [Agents SDK](https://docs.anthropic.com/en/docs/agents-sdk), אתה יכול להשתמש באותה מערכת hook באופן תכנותי.

<Steps>
  <Step title="התקן את failproofai בפרויקט שלך">
    ```bash theme={null}
    npm install failproofai
    ```
  </Step>

  <Step title="הגדר hooks בסוכן שלך">
    העבר פקודות hook בעת יצירת תהליך ה-agent שלך. ה-hooks מתבצעים באותו אופן כמו ב-Claude Code - דרך stdin/stdout JSON:

    ```bash theme={null}
    failproofai --hook PreToolUse   # נקרא לפני כל tool
    failproofai --hook PostToolUse  # נקרא אחרי כל tool
    ```
  </Step>

  <Step title="כתוב policy מותאם אישית עבור ה-agent שלך">
    ```javascript theme={null}
    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();
      },
    });
    ```
  </Step>

  <Step title="התקן את ה-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` - חוסם מחיקת קבצים רקורסיבית
* `block-force-push` - חוסם `git push --force`
* `block-curl-pipe-sh` - חוסם הנחת סקריפטים מרוחוקים אל shell

***

## מנע דליפת סודות

עצור agents מלראות או דליפה של אישורים בפלט tool.

```bash theme={null}
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
```

אלה מתבצעים על `PostToolUse` - אחרי שתוכנית מתבצעת, הם מנקים את הפלט לפני שה-agent רואה זאת.

***

## קבל התראות Slack כאשר agents צריכים תשומת לב

השתמש בה-notification hook כדי להעביר התראות מהמתנה ל-Slack.

```javascript theme={null}
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 {
      // never block the agent if Slack is unreachable
    }

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

התקן זאת:

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

***

## שמור על agents בענף

מנע מ-agents לשנות ענפים או לדחוף לענפים מוגנים.

```javascript theme={null}
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

הזכר ל-agents להריץ בדיקות לפני ביצוע commit.

```javascript theme={null}
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

בצע commit של קונפיגורציה ברמת פרויקט כדי שכל מפתח בצוות שלך יקבל את אותם policies.

צור `.failproofai/policies-config.json` ב-repo שלך:

```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"
```

כל חברי הצוות שיש להם failproofai מותקן ילקטו את הכללים הללו באופן אוטומטי.

***

## בנה תקן איכות ברמת הארגון עם convention policies

ההגדרה בעלת ההשפעה הרבה ביותר: בצע commit של `.failproofai/policies/` ל-repo שלך עם policies המותאמים לפרויקט שלך. כל חברי הצוות מקבלים אותם באופן אוטומטי — ללא פקודות התקנה, ללא שינויי הגדרות.

<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";

    // Enforce your team's preferred package manager
    // (or enable the built-in prefer-package-manager policy instead)
    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("Use bun instead of npm.");
        return allow();
      },
    });

    // Remind the agent to run tests before committing
    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("Run tests before committing.");
        }
        return allow();
      },
    });
    ```
  </Step>

  <Step title="בצע commit ל-git">
    ```bash theme={null}
    git add .failproofai/policies/
    git commit -m "Add team quality policies"
    ```
  </Step>

  <Step title="המשך בשיפור">
    כאשר הצוות שלך נתקל באופני כשל חדשים, הוסף policies ודחוף. כולם מקבלים את העדכון ב-`git pull` הבא שלהם. Policies אלה הופכים לתקן איכות חי שגדל עם הצוות שלך.
  </Step>
</Steps>

***

## עוד דוגמאות

התיקייה [`examples/`](https://github.com/failproofai/failproofai/tree/main/examples) ב-repo מכילה:

| קובץ                         | מה זה מציג                                                                   |
| ---------------------------- | ---------------------------------------------------------------------------- |
| `policies-basic.js`          | Starter policies - חסום כתיבה לייצור, force-push, סקריפטים מנובעים           |
| `policies-notification.js`   | התראות Slack להודעות מהמתנה וסיום הסשן                                       |
| `policies-advanced/index.js` | יבוא טרנזיטיבי, async hooks, PostToolUse output scrubbing, טיפול באירוע Stop |
