/** * OpenAI Integration for llm-trust-guard * * Provides wrappers and utilities for securing OpenAI API calls. * Works with both the official OpenAI SDK and direct API calls. */ import { InputSanitizer, OutputFilter } from "../index.js"; export interface SecureOpenAIConfig { /** Enable input validation */ validateInput?: boolean; /** Enable output filtering */ filterOutput?: boolean; /** Enable function/tool call validation */ validateFunctions?: boolean; /** Throw error on violation */ throwOnViolation?: boolean; /** Custom violation handler */ onViolation?: (type: string, details: any) => void; /** InputSanitizer configuration */ sanitizerConfig?: ConstructorParameters[0]; /** OutputFilter configuration */ outputConfig?: ConstructorParameters[0]; } export interface ValidationResult { allowed: boolean; violations: string[]; sanitized?: string; details?: any; } export interface SecureMessage { role: "system" | "user" | "assistant" | "function" | "tool"; content: string | null; name?: string; function_call?: any; tool_calls?: any[]; } /** * Secure wrapper for OpenAI API calls * * @example * ```typescript * import OpenAI from 'openai'; * import { SecureOpenAI } from 'llm-trust-guard/integrations/openai'; * * const openai = new OpenAI(); * const secure = new SecureOpenAI({ * validateInput: true, * filterOutput: true, * throwOnViolation: true * }); * * // Validate messages before sending * const messages = [ * { role: 'system', content: 'You are a helpful assistant.' }, * { role: 'user', content: userInput } * ]; * * const validatedMessages = secure.validateMessages(messages, sessionId); * if (!validatedMessages.allowed) { * throw new Error(`Blocked: ${validatedMessages.violations.join(', ')}`); * } * * // Make the API call * const completion = await openai.chat.completions.create({ * model: 'gpt-4', * messages: validatedMessages.messages * }); * * // Filter the response * const safeResponse = secure.filterResponse(completion); * ``` */ export declare class SecureOpenAI { private inputSanitizer; private encodingDetector; private memoryGuard; private outputFilter; private toolChainValidator; private config; constructor(config?: SecureOpenAIConfig); /** * Validate a single message content */ validateContent(content: string, requestId?: string): ValidationResult; /** * Validate an array of chat messages */ validateMessages(messages: SecureMessage[], sessionId: string, requestId?: string): { allowed: boolean; messages: SecureMessage[]; violations: string[]; }; /** * Validate function/tool definitions */ validateFunctions(functions: Array<{ name: string; description?: string; parameters?: any; }>, sessionId: string): ValidationResult; /** * Validate a function/tool call before execution */ validateFunctionCall(name: string, args: Record, sessionId: string): ValidationResult; /** * Filter the response from OpenAI */ filterResponse(response: { choices?: Array<{ message?: { content?: string | null; function_call?: any; tool_calls?: any[]; }; text?: string; }>; }, requestId?: string): typeof response; /** * Create a secure chat completion wrapper */ createSecureChat(sessionId: string): { /** * Prepare messages for API call */ prepareMessages: (messages: SecureMessage[]) => { allowed: boolean; messages: SecureMessage[]; violations: string[]; }; /** * Validate function call before execution */ validateFunctionCall: (name: string, args: any) => ValidationResult; /** * Filter response before returning */ filterResponse: (response: any) => { choices?: Array<{ message?: { content?: string | null; function_call?: any; tool_calls?: any[]; }; text?: string; }>; }; }; private handleViolation; } /** * Error thrown on security violations */ export declare class OpenAISecurityError extends Error { violations: string[]; constructor(message: string, violations: string[]); } /** * Create a simple wrapper function for validating OpenAI messages * * @example * ```typescript * const validate = createMessageValidator(); * * const userMessage = await getUserInput(); * const result = validate(userMessage); * * if (!result.allowed) { * console.log('Blocked:', result.violations); * return; * } * * // Use result.sanitized in your API call * ``` */ export declare function createMessageValidator(config?: ConstructorParameters[0]): (content: string) => { allowed: boolean; sanitized: string; violations: string[]; }; /** * Middleware-style wrapper for OpenAI client * * @example * ```typescript * import OpenAI from 'openai'; * import { wrapOpenAIClient } from 'llm-trust-guard/integrations/openai'; * * const openai = new OpenAI(); * const secureOpenAI = wrapOpenAIClient(openai, { * validateInput: true, * filterOutput: true * }); * * // Use secureOpenAI.chat.completions.create() as normal * // Input will be validated, output will be filtered * ``` */ export declare function wrapOpenAIClient(client: T, config?: SecureOpenAIConfig): T;