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

הגדרת 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, אתה יכול להשתמש באותה מערכת hooks בצורה תכנותית.
1

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

npm install failproofai
2

הגדר hooks ב-agent שלך

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

כתוב מדיניות מותאמת אישית עבור ה-agent שלך

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

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

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

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

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

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

השתמש בה-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

שמור agents בענף

מנע מ-agents להחליף ענפים או לדחוף לענפים מוגנים.
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.
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();
  },
});

נעל ריפוזיטוריום ייצור

בצע 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 מותקן יבחרו באופן אוטומטי את הכללים הללו.

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

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