import { Guard, GuardResult, InputMapper } from "./model"; import { OnFailureHandler } from "./on-failure"; export interface GuardrailsOptions { /** * Called when any guard returns false. Can be: * - `"raise"`: Throw GuardValidationError (default) * - `"log"`: Log a warning and return the original result * - `"ignore"`: Return the original result silently (shadow/observe mode) * - Any other string: Return that string as a fallback value * - Callable: Custom OnFailureHandler receiving a GuardedResult */ onFailure?: string | OnFailureHandler; /** * If true, run all guards before handling failures. * If false (default), stop at the first failure (fail-fast). */ runAll?: boolean; /** * If true (default), run guards in parallel for lower latency. * If false, run guards sequentially — useful when guard order matters. */ parallel?: boolean; /** * Maps the guarded function's output to one input dict per guard. * Use this when the output is a structured object and different guards * need different fields. If omitted, the default mapper handles strings * and plain objects automatically. */ inputMapper?: InputMapper; } export interface GuardOptions { onFailure?: string | OnFailureHandler; parallel?: boolean; inputMapper?: InputMapper; } export interface ValidateOptions { parallel?: boolean; inputMapper?: InputMapper; } export interface ValidateResult { passed: boolean; results: GuardResult[]; } /** * Full-control guardrail runner with a fluent builder API. * * @param guards - Guard functions to run. Each receives its corresponding input dict * and returns a boolean: true = pass, false = fail. * @param options - Optional configuration (onFailure, name, runAll, parallel, inputMapper). * * @example * const g = new Guardrails([toxicityGuard(), piiGuard()], { name: "safety", onFailure: "log" }) * .parallel() * .runAll(); * * const result = await g.run(async () => callLLM(prompt)); */ export declare class Guardrails { private readonly guards; private readonly _onFailure; private readonly _runAll; private readonly _parallel; private readonly _inputMapper?; constructor(guards: Guard[], options?: GuardrailsOptions); parallel(): Guardrails; sequential(): Guardrails; runAll(): Guardrails; failFast(): Guardrails; raiseOnFailure(): Guardrails; logOnFailure(): Guardrails; ignoreOnFailure(): Guardrails; onFailure(handler: string | OnFailureHandler): Guardrails; private _clone; /** * Runs an async function (typically an LLM call), then evaluates its output * through all configured guards. * * The LLM function runs first and its result is captured. Guards then evaluate * that result. If any guard fails, the configured `onFailure` handler is invoked. * If a guard throws a real error (network failure, API error), a * `GuardExecutionError` is thrown regardless of `onFailure`. * * @param fn - The async function to run (e.g. your LLM call). Its return value * is passed to the guards as input. * @param args - Arguments forwarded to `fn`. * @returns The original return value of `fn` if all guards pass, or the * `onFailure` handler's return value if any guard fails. * * @example * const result = await g.run( * async (prompt) => openai.chat.completions.create(...), * userPrompt, * ); */ run(fn: (...args: unknown[]) => Promise, ...args: unknown[]): Promise>; /** * Runs guards directly against pre-mapped inputs without wrapping an async function. * * Use this when you already have the guard inputs constructed — for example when * using sequential/failFast execution and you want to pass specific field values * to each guard rather than relying on automatic mapping. * * Unlike the standalone `validate()` function, this method does NOT do any input * mapping — you are responsible for providing one input dict per guard in the * correct order. * * @param guardInputs - Array of input dicts, one per guard, in the same order as * the guards passed to the constructor. * @returns Per-guard results: `[{ name, passed, duration }, ...]` * * @example * // Check a user prompt for injection before calling the LLM * const g = new Guardrails([promptInjectionGuard(), piiGuard()]); * const results = await g.validate([ * { prompt: userPrompt }, * { text: userPrompt }, * ]); * if (results.every(r => r.passed)) { * const response = await callLLM(userPrompt); * } */ validate(guardInputs: Record[]): Promise; private _executeGuards; private _runSingleGuard; } /** * Wraps an async function with guardrails. Returns a new function with the same signature. * * @example * const safeGenerate = guard(generateResponse, [toxicityGuard(), piiGuard()], { * onFailure: "Sorry, blocked by content policy.", * }); * const result = await safeGenerate("Tell me a joke"); */ export declare function guard(fn: (...args: A) => Promise, guards: Guard[], options?: GuardOptions): (...args: A) => Promise>; /** * Validates a string or object against a set of guards without wrapping a function. * * @example — simple guards, no inputMapper required: * const result = await validateContent("LLM response text", [toxicityGuard(), piiGuard()]); * if (!result.passed) { * console.log("Failed:", result.results.filter(r => !r.passed)); * } * * @example — complex guard with typed input, inputMapper required: * const result = await validateContent(llmOutput, [semanticSimilarityGuard()], { * inputMapper: (output) => [{ text: output as string, reference: expectedAnswer }], * }); */ export declare function validateContent(output: string | Record, guards: Guard>[], options?: ValidateOptions): Promise; export declare function validateContent>(output: string | Record, guards: Guard[], options: ValidateOptions & { inputMapper: InputMapper, TInput>; }): Promise; /** * Decorator that wraps a class method with guardrails. * * @example * class MyService { * \@guardrail([toxicityGuard()], { onFailure: "Sorry, blocked." }) * async generateResponse(prompt: string): Promise { ... } * } */ export declare function guardrail(guards: Guard[], options?: GuardOptions): MethodDecorator; //# sourceMappingURL=guardrail.d.ts.map