الانتقال إلى المحتوى الرئيسي

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، يمكنك استخدام نفس نظام hooks بشكل برمجي.
1

تثبيت failproofai في مشروعك

npm install failproofai
2

تكوين hooks في وكيلك

مرر أوامر hook عند إنشاء عملية الوكيل الخاصة بك. تعمل hooks بنفس الطريقة كما هي في Claude Code - عبر JSON على stdin/stdout:
failproofai --hook PreToolUse   # يتم استدعاؤها قبل كل أداة
failproofai --hook PostToolUse  # يتم استدعاؤها بعد كل أداة
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 عندما يحتاج الوكلاء إلى الاهتمام

استخدم 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();
  },
});

طلب اختبارات قبل الالتزام

ذكّر الوكلاء بتشغيل الاختبارات قبل الالتزام.
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();
  },
});

قفل مستودع الإنتاج

التزم بتكوين على مستوى المشروع بحيث يحصل كل مطور في فريقك على نفس السياسات. أنشئ .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"]
    }
  }
}
ثم التزم به:
git add .failproofai/policies-config.json
git commit -m "Add failproofai team policies"
سيلتقط كل عضو في الفريق لديه failproofai مثبتاً هذه القواعد تلقائياً.

المزيد من الأمثلة

يحتوي دليل examples/ في المستودع على:
الملفما يعرضه
policies-basic.jsالسياسات الأساسية - منع عمليات الكتابة في الإنتاج والدفع القسري والبرامج النصية المنقولة
policies-notification.jsتنبيهات Slack لإشعارات السكون ونهاية الجلسة
policies-advanced/index.jsالاستيرادات المتعددة والخطافات غير المتزامنة وتنقية مخرجات PostToolUse ومعالجة حدث Stop