/** * DetectionBackend - Pluggable detection classifier * * Allows users to plug in ML-based detection alongside the built-in regex guards. * Default: regex-only (zero dependencies, <5ms). * Optional: any async classifier (embedding similarity, external API, custom ML). * * Why this exists: Research shows regex-only detection is bypassed at >90% ASR * by adaptive attacks (JBFuzz 99%, AutoDAN 88%, PAIR adaptive). This interface * lets users add ML-based detection without forcing dependencies on all users. */ /** Context about what is being classified */ export interface DetectionContext { type: "user_input" | "tool_result" | "llm_output" | "system_context" | "rag_document"; sessionId?: string; metadata?: Record; } /** Result from a detection classifier */ export interface DetectionResult { safe: boolean; confidence: number; threats: DetectionThreat[]; } export interface DetectionThreat { category: string; severity: "low" | "medium" | "high" | "critical"; description: string; } /** * Detection classifier callback type. * * Can be sync (for regex/local ML) or async (for API calls). * Users implement this as a function, closure, or class method. * * @example * // Sync classifier (fast, local) * const myClassifier: DetectionClassifier = (input, ctx) => ({ * safe: !input.includes("hack"), * confidence: 0.9, * threats: [] * }); * * @example * // Async classifier (ML API) * const mlClassifier: DetectionClassifier = async (input, ctx) => { * const res = await fetch('https://my-ml-api/classify', { * method: 'POST', * body: JSON.stringify({ text: input, type: ctx.type }) * }); * const data = await res.json(); * return { safe: data.score < 0.5, confidence: data.score, threats: data.threats }; * }; */ export type DetectionClassifier = (input: string, context: DetectionContext) => DetectionResult | Promise; /** * Create a built-in regex classifier that wraps InputSanitizer + EncodingDetector. * * Useful as a baseline or fallback classifier. */ export declare function createRegexClassifier(config?: { threshold?: number; detectPAP?: boolean; }): DetectionClassifier; /** * Merge two detection results (used when combining regex + ML backends) * * Policy: if EITHER result is unsafe, the merged result is unsafe. * Confidence: take the lower confidence (most conservative). */ export declare function mergeDetectionResults(a: DetectionResult, b: DetectionResult): DetectionResult;