/** * Guardrails - Pattern-based detection of risky operations * * Guardrails scan tool inputs/commands before execution, match against * known dangerous patterns, and emit warnings or block execution. */ /** * Action to take when a guardrail is triggered * - warn: Log warning but proceed with execution * - confirm: Require confirmation before proceeding * - block: Prevent execution entirely */ export type GuardrailAction = 'warn' | 'confirm' | 'block'; /** * A guardrail definition */ export interface Guardrail { /** * Unique identifier for this guardrail */ id: string; /** * Human-readable name */ name: string; /** * Description of what this guardrail protects against */ description: string; /** * Patterns to match against tool inputs */ patterns: RegExp[]; /** * Action to take when triggered */ action: GuardrailAction; /** * Message to display when triggered */ message: string; /** * Which tools this guardrail applies to (empty = all tools) */ scope?: string[]; /** * Whether this guardrail is enabled (default: true) */ enabled?: boolean; /** * Tags for grouping/filtering guardrails */ tags?: string[]; } /** * Input for adding a custom guardrail */ export interface GuardrailInput { id: string; name: string; description: string; patterns: RegExp[]; action: GuardrailAction; message: string; scope?: string[]; enabled?: boolean; tags?: string[]; } /** * Result of checking a guardrail */ export interface GuardrailResult { /** * Whether any guardrail was triggered */ triggered: boolean; /** * The guardrail that was triggered (if any) */ guardrail?: Guardrail; /** * The matched pattern (if any) */ match?: string; /** * The action to take */ action?: GuardrailAction; /** * The tool that triggered the guardrail */ toolName?: string; /** * The input that triggered the guardrail */ input?: unknown; /** * The specific subcommand that triggered (compound commands only) */ subcommand?: string; /** * 0-based index of the subcommand in the compound command */ subcommandIndex?: number; } /** * Context passed to guardrail handlers */ export interface GuardrailContext { /** * The tool being called */ toolName: string; /** * The tool input */ input: unknown; /** * The stringified input used for pattern matching */ inputString: string; } /** * Handler called when a guardrail is triggered * * @param result - The guardrail check result * @param context - Context about the tool call * @returns true to proceed with execution, false to block */ export type GuardrailTriggeredHandler = (result: GuardrailResult, context: GuardrailContext) => boolean | Promise; /** * Configuration for the GuardrailManager */ export interface GuardrailManagerOptions { /** * Enable guardrails (default: true) */ enabled?: boolean; /** * Custom guardrails to add */ custom?: GuardrailInput[]; /** * Include built-in guardrails (default: true) */ includeDefaults?: boolean; /** * Handler called when a guardrail is triggered */ onTriggered?: GuardrailTriggeredHandler; } /** * Event types for guardrail operations */ export type GuardrailEventType = 'guardrail:triggered' | 'guardrail:warning' | 'guardrail:blocked' | 'guardrail:confirmed' | 'guardrail:cancelled'; /** * Guardrail event payload */ export interface GuardrailEvent { type: GuardrailEventType; result: GuardrailResult; context: GuardrailContext; } /** * Event handler for guardrail events */ export type GuardrailEventHandler = (event: GuardrailEvent) => void;