/** * LangChain Integration for llm-trust-guard * * Provides callbacks, wrappers, and utilities for securing * LangChain-based applications. */ import { InputSanitizer, OutputFilter } from "../index.js"; import type { PAPSanitizerResult } from "../guards/input-sanitizer.js"; export interface TrustGuardCallbackConfig { /** Enable input validation */ validateInput?: boolean; /** Enable output filtering */ filterOutput?: boolean; /** Enable tool chain validation */ validateTools?: boolean; /** Throw error on violation (otherwise just log) */ throwOnViolation?: boolean; /** Custom violation handler */ onViolation?: (type: string, details: any) => void; /** InputSanitizer configuration */ sanitizerConfig?: ConstructorParameters[0]; /** OutputFilter configuration */ outputConfig?: ConstructorParameters[0]; } /** * Security result from guard checks */ export interface SecurityCheckResult { allowed: boolean; guard: string; violations: string[]; sanitizedInput?: string; details?: any; } /** * TrustGuard wrapper for LangChain * * @example * ```typescript * import { ChatOpenAI } from '@langchain/openai'; * import { TrustGuardLangChain } from 'llm-trust-guard/integrations/langchain'; * * const guard = new TrustGuardLangChain({ * validateInput: true, * filterOutput: true, * throwOnViolation: true * }); * * // Validate before sending to LLM * const result = guard.validateInput(userMessage); * if (!result.allowed) { * throw new Error(`Blocked: ${result.violations.join(', ')}`); * } * * // Use with LangChain * const llm = new ChatOpenAI(); * const response = await llm.invoke(result.sanitizedInput || userMessage); * * // Filter output before returning to user * const filtered = guard.filterOutput(response.content); * ``` */ export declare class TrustGuardLangChain { private inputSanitizer; private encodingDetector; private memoryGuard; private toolChainValidator; private outputFilter; private config; constructor(config?: TrustGuardCallbackConfig); /** * Validate user input before sending to LLM */ validateInput(input: string, requestId?: string): SecurityCheckResult; /** * Validate context/memory before injection */ validateContext(context: string | string[], sessionId: string, requestId?: string): SecurityCheckResult; /** * Validate RAG documents before context injection */ validateDocuments(documents: Array<{ content: string; metadata?: any; }>, sessionId: string): SecurityCheckResult; /** * Validate tool calls before execution */ validateToolCall(toolName: string, toolArgs: Record, sessionId: string): SecurityCheckResult; /** * Filter LLM output before returning to user */ filterOutput(output: string, requestId?: string): string; /** * Create a secure message processor */ createSecureProcessor(sessionId: string): { /** * Process user message with full validation */ processUserMessage: (message: string) => { allowed: boolean; message: string; violations: string[]; }; /** * Process context/RAG content */ processContext: (context: string[]) => { allowed: boolean; violations: string[]; }; /** * Process tool call */ processToolCall: (tool: string, args: any) => { allowed: boolean; violations: string[]; }; /** * Process LLM output */ processOutput: (output: string) => string; }; private handleViolation; } /** * Error thrown when throwOnViolation is true */ export declare class TrustGuardViolationError extends Error { type: string; details: any; constructor(type: string, details: any); } /** * Create a simple input validator function for use with LangChain * * @example * ```typescript * const validateInput = createInputValidator(); * * // In your chain * const chain = RunnableSequence.from([ * new RunnableLambda({ func: (input) => { * const result = validateInput(input.message); * if (!result.allowed) throw new Error('Blocked'); * return { ...input, message: result.sanitized }; * }}), * prompt, * llm, * outputParser * ]); * ``` */ export declare function createInputValidator(config?: ConstructorParameters[0]): (input: string) => { allowed: boolean; sanitized: string; violations: string[]; pap?: PAPSanitizerResult["pap"]; }; /** * Create an output filter function for use with LangChain */ export declare function createOutputFilter(config?: ConstructorParameters[0]): (output: string) => string;