דלג לתוכן הראשי

title: דוגמאות description: “כיצד להגדיר hooks עבור Claude Code ו-Agents SDK” icon: book-open

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

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

Failproof AI משתלבת עם Claude Code דרך מערכת ה-hooks שלו. כאשר אתה מריץ failproofai policies --install, היא רושמת פקודות hook ב-settings.json של Claude Code שמופעלות בכל קריאת כלי.
1

התקן את failproofai

npm install -g failproofai
2

הפעל את כל המדיניות המובנית

failproofai policies --install
3

אמת שהוקמו hooks

cat ~/.claude/settings.json | grep failproofai
אתה אמור לראות ערכי hook עבור אירועים PreToolUse, PostToolUse, Notification ו-Stop.
4

הרץ את Claude Code

claude
המדיניויות פועלות כעת באופן אוטומטי בכל קריאת כלי. נסה לבקש מ-Claude להריץ sudo rm -rf / - זה יחסום.

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

אם אתה בונה עם Agents SDK, אתה יכול להשתמש באותה מערכת hook בצורה תכנותית.
1

התקן את failproofai בפרויקט שלך

npm install failproofai
2

הגדר hooks בסוכן שלך

העבר פקודות hook כאשר אתה יוצר את תהליך הסוכן. ה-hooks מופעלים באותו אופן כמו ב-Claude Code - דרך stdin/stdout JSON:
failproofai --hook PreToolUse   # called before each tool
failproofai --hook PostToolUse  # called after each tool
3

כתוב מדיניות מותאמת עבור הסוכן שלך

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

התקן את המדיניות המותאמת

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

חסום פקודות הורסות

ההגדרה הנפוצה ביותר - מנע מסוכנים לגרום נזק בלתי הפיך.
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

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

עצור סוכנים מלראות או להדליף אישורים בפלט הכלי.
failproofai policies --install sanitize-api-keys sanitize-jwt sanitize-connection-strings sanitize-bearer-tokens
אלה מופעלים על PostToolUse - לאחר שכלי רץ, הם מנקים את הפלט לפני שהסוכן רואה אותו.

קבל התראות Slack כשסוכנים זקוקים לתשומת לב

השתמש ב-notification hook להעברת התראות צפי ל-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 {
      // never block the agent if Slack is unreachable
    }

    return allow();
  },
});
התקן זאת:
SLACK_WEBHOOK_URL=https://hooks.slack.com/... failproofai policies --install --custom ./slack-alerts.js

שמור סוכנים על ענף

מנע מסוכנים להחליף ענפים או לדחוף לענפים מוגנים.
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

תזכור לסוכנים להריץ בדיקות לפני ביצוע commit.
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 של קובץ הגדרות ברמת הפרויקט כדי שכל מפתח בצוות שלך יקבל אותן מדיניויות. צור .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"
כל חבר בצוות שיש לו failproofai מותקן יקטום את הכללים האלה באופן אוטומטי.

בנה תקן איכות בקנה מידה של ארגון עם מדיניות convention

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

צור את התיקייה והוסף מדיניויות

mkdir -p .failproofai/policies
// .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();
  },
});
2

בצע commit ל-git

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

המשך בשיפור

כאשר הצוות שלך נתקל בהצורות כשל חדשות, הוסף מדיניויות והדחף. כולם מקבלים את העדכון ב-git pull הבא שלהם. המדיניויות הללו הופכות לתקן איכות חי שגדל עם הצוות שלך.

דוגמאות נוספות

תיקייה examples/ בריפו מכילה:
קובץמה הוא מציג
policies-basic.jsמדיניויות התחלות - חסום כתיבה production, force-push, סקריפטים מובילים
policies-notification.jsהתראות Slack עבור הודעות צפי וסיום הפעלה
policies-advanced/index.jsיבואות טרנזיטיביות, hook אסינכרוני, ניקוי פלט PostToolUse, טיפול באירוע Stop