/** * Vercel AI SDK Integration for llm-trust-guard * * Provides middleware and wrappers for securing applications built with the * Vercel AI SDK (@vercel/ai / ai package). Works with any provider (OpenAI, * Anthropic, Google, Mistral, etc.) through the language model middleware API. * * Zero extra dependencies — `ai` is never imported directly. * * @example * ```typescript * import { openai } from '@ai-sdk/openai'; * import { generateText, streamText } from 'ai'; * import { wrapWithTrustGuard } from 'llm-trust-guard/integrations/vercel-ai-sdk'; * * const model = wrapWithTrustGuard(openai('gpt-4o')); * * // Input is validated, output is filtered — automatic * const { text } = await generateText({ * model, * messages: [{ role: 'user', content: userMessage }], * }); * ``` */ import { InputSanitizer, EncodingDetector, OutputFilter, MemoryGuard, ToolChainValidator } from "../index.js"; export interface TrustGuardAIConfig { /** Enable InputSanitizer + EncodingDetector on user messages (default: true) */ validateInput?: boolean; /** Enable OutputFilter on model responses (default: true) */ filterOutput?: boolean; /** Enable ToolChainValidator on tool calls (default: true) */ validateTools?: boolean; /** Throw error instead of returning blocked status (default: false) */ throwOnViolation?: boolean; /** Custom violation handler: (type, details) => void */ onViolation?: (type: string, details: unknown) => void; /** Forwarded to InputSanitizer constructor */ sanitizerConfig?: ConstructorParameters[0]; /** Forwarded to OutputFilter constructor */ outputConfig?: ConstructorParameters[0]; } export interface InputValidationResult { allowed: boolean; violations: string[]; sanitizedText?: string; } export interface AIOutputFilterResult { allowed: boolean; filteredText: string; piiDetected: number; secretsDetected: number; } export declare class TrustGuardAI { readonly inputSanitizer: InputSanitizer; readonly encodingDetector: EncodingDetector; readonly outputFilter: OutputFilter; readonly memoryGuard: MemoryGuard; readonly toolChainValidator: ToolChainValidator; private readonly config; constructor(config?: TrustGuardAIConfig); /** * Validate user input text. Returns the sanitized text if allowed. */ validateInput(text: string, requestId?: string): InputValidationResult; /** * Filter LLM output. Always returns a (possibly masked) string. */ filterOutput(text: string, requestId?: string): AIOutputFilterResult; /** * Validate all user messages in an AI SDK message array in place. * Returns { allowed, messages, violations }. */ validateMessages(messages: Array<{ role: string; content: string | unknown; }>, requestId?: string): { allowed: boolean; messages: typeof messages; violations: string[]; }; /** * Validate a tool call before execution. */ validateToolCall(toolName: string, args: Record, sessionId: string): { allowed: boolean; violations: string[]; }; private _handleViolation; } export declare class TrustGuardAIViolationError extends Error { readonly violationType: string; readonly details: unknown; constructor(type: string, details: unknown); } /** * Build a Vercel AI SDK LanguageModelV1Middleware object. * * Pass the returned object to `wrapLanguageModel` (or * `experimental_wrapLanguageModel` in older SDK versions): * * ```typescript * import { wrapLanguageModel } from 'ai'; * import { openai } from '@ai-sdk/openai'; * import { createTrustGuardMiddleware } from 'llm-trust-guard/integrations/vercel-ai-sdk'; * * const model = wrapLanguageModel({ * model: openai('gpt-4o'), * middleware: createTrustGuardMiddleware({ throwOnViolation: true }), * }); * ``` */ export declare function createTrustGuardMiddleware(config?: TrustGuardAIConfig): { wrapGenerate: (options: { doGenerate: () => Promise; params: { messages?: Array<{ role: string; content: unknown; }>; }; }) => Promise; wrapStream: (options: { doStream: () => Promise<{ stream: AsyncIterable; } & Record>; params: { messages?: Array<{ role: string; content: unknown; }>; }; }) => Promise<{ stream: AsyncIterable; } & Record>; }; /** * Wrap a Vercel AI SDK language model with trust guard middleware. * * This is a convenience wrapper for `wrapLanguageModel` from the `ai` package. * It requires `ai` >= 3.1 to be installed in the host project. * * ```typescript * import { openai } from '@ai-sdk/openai'; * import { wrapWithTrustGuard } from 'llm-trust-guard/integrations/vercel-ai-sdk'; * * // Drop-in replacement for openai('gpt-4o') * const model = wrapWithTrustGuard(openai('gpt-4o'), { * validateInput: true, * filterOutput: true, * throwOnViolation: true, * }); * * const { text } = await generateText({ model, prompt: userMessage }); * ``` */ export declare function wrapWithTrustGuard(model: T, config?: TrustGuardAIConfig): T; /** * Create a simple validate-then-generate helper. * * ```typescript * import { generateText } from 'ai'; * import { openai } from '@ai-sdk/openai'; * import { createSecureGenerate } from 'llm-trust-guard/integrations/vercel-ai-sdk'; * * const secureGenerate = createSecureGenerate(generateText, { * throwOnViolation: true, * }); * * const { text } = await secureGenerate({ * model: openai('gpt-4o'), * messages: [{ role: 'user', content: userInput }], * }); * ``` */ export declare function createSecureGenerate Promise<{ text?: string; }>>(generateFn: T, config?: TrustGuardAIConfig): T;