/** * Guardian Interceptor * * Wraps LLM clients to intercept and validate all inputs/outputs. * Acts as middleware between the agent and the LLM. */ import type { GenerateOptions, GenerateResult, ModelClient } from '@artemiskit/core'; import type { GuardianEventHandler, GuardrailResult, InterceptedRequest, Violation } from './types'; /** * Guardrail function signature */ export type GuardrailFn = (content: string, context?: Record) => Promise; /** * Interceptor configuration */ export interface InterceptorConfig { /** Enable input validation */ validateInput?: boolean; /** Enable output validation */ validateOutput?: boolean; /** Input guardrails to run */ inputGuardrails?: GuardrailFn[]; /** Output guardrails to run */ outputGuardrails?: GuardrailFn[]; /** Whether to block on validation failure (flat boolean, see shouldBlockViolation for finer control) */ blockOnFailure?: boolean; /** * Callback to determine if a specific violation should cause blocking. * Use this for mode-aware blocking (e.g., selective mode blocks only high-severity). * If provided, this callback is used instead of flat blockOnFailure for per-violation decisions. * The callback receives a violation and should return true if it should block. */ shouldBlockViolation?: (violation: Violation) => boolean; /** Event handlers */ onEvent?: GuardianEventHandler; /** Log violations */ logViolations?: boolean; /** * Callback to check if requests should be allowed (e.g., circuit breaker check). * Called before each LLM request. Return false to block the request. */ shouldAllow?: () => { allowed: boolean; reason?: string; }; } /** * Interceptor statistics */ export interface InterceptorStats { totalRequests: number; blockedRequests: number; totalViolations: number; averageLatencyMs: number; inputViolations: number; outputViolations: number; } /** * Guardian Interceptor wraps a ModelClient to add validation */ export declare class GuardianInterceptor implements ModelClient { readonly provider: string; private client; private config; private stats; private requestHistory; constructor(client: ModelClient, config?: InterceptorConfig); /** * Generate with guardrail validation */ generate(options: GenerateOptions): Promise; /** * Get model capabilities (pass-through) */ capabilities(): Promise; /** * Stream generation with guardrail validation * * IMPORTANT: Streaming is currently not supported by GuardianInterceptor. * This method throws an error to prevent bypassing Guardian protections. * * Streaming validation is complex because: * 1. Input validation must complete before streaming starts * 2. Output validation requires the complete response * 3. Circuit breaker must be checked per-request * * For protected LLM access, use the non-streaming `generate()` method. * If you need streaming, use the underlying client directly (unprotected). * * @throws {Error} Always throws - streaming not supported in Guardian */ stream?(_options: GenerateOptions, _onChunk: (chunk: string) => void): AsyncIterable; /** * Embed text (pass-through) */ embed?(text: string, model?: string): Promise; /** * Close the client */ close?(): Promise; /** * Get interceptor statistics */ getStats(): InterceptorStats; /** * Reset statistics */ resetStats(): void; /** * Get request history */ getRequestHistory(): InterceptedRequest[]; /** * Clear request history */ clearHistory(): void; /** * Run guardrails and aggregate results */ private runGuardrails; /** * Extract text from prompt (string or messages array) */ private extractPromptText; /** * Emit an event to the handler */ private emitEvent; } /** * Error thrown when a request is blocked by guardrails */ export declare class GuardianBlockedError extends Error { readonly violations: Violation[]; constructor(message: string, violations: Violation[]); } /** * Create an interceptor wrapping a model client */ export declare function createInterceptor(client: ModelClient, config?: InterceptorConfig): GuardianInterceptor; //# sourceMappingURL=interceptor.d.ts.map