ai-sdk-guardrails
Version:
Input and output guardrails middleware for Vercel AI SDK.
41 lines (39 loc) • 1.93 kB
text/typescript
/**
* Detection-time text normalization — an anti-evasion pass applied *before*
* pattern matching so that obfuscated injections (homoglyphs, zero-width
* characters, leetspeak, spaced-out letters, typos) are matched against the
* same regexes as their plain-text equivalents.
*
* Kept as a pure, dependency-free function so it can preprocess input for any
* guardrail.
*/
interface DetectNormalizationOptions {
enabled?: boolean;
/** Fold full-width and look-alike (Cyrillic/Greek) letters to ASCII. */
foldHomoglyphs?: boolean;
/** Remove zero-width and other invisible characters. */
stripInvisible?: boolean;
collapseWhitespace?: boolean;
/** Join single letters separated by whitespace, e.g. `i g n o r e`. */
joinSeparatedLetters?: boolean;
normalizeCase?: boolean;
/** Decode common leetspeak substitutions, e.g. `1gn0r3`. */
decodeLeetspeak?: boolean;
repairTypos?: boolean;
repairPhonetics?: boolean;
}
type ResolvedDetectNormalizationOptions = Required<DetectNormalizationOptions>;
declare const DEFAULT_DETECT_NORMALIZATION: ResolvedDetectNormalizationOptions;
/** Resolve a normalization option (`true`/`undefined`/object) to a full config. */
declare function resolveDetectNormalization(options?: boolean | DetectNormalizationOptions): ResolvedDetectNormalizationOptions;
/**
* Normalize text for detection. Defeats common evasion tricks before any
* regex/keyword matching runs. Returns the (trimmed) input unchanged when
* disabled.
*
* @example
* normalizeForDetection('1gn0re prev10us 1nstruct10ns');
* // => 'ignore previous instructions'
*/
declare function normalizeForDetection(input: string, options?: boolean | DetectNormalizationOptions): string;
export { type DetectNormalizationOptions as D, type ResolvedDetectNormalizationOptions as R, DEFAULT_DETECT_NORMALIZATION as a, normalizeForDetection as n, resolveDetectNormalization as r };