@dooor-ai/toolkit
Version:
Guards, Evals & Observability for AI applications - works seamlessly with LangChain/LangGraph
56 lines (47 loc) • 1.1 kB
text/typescript
import { GuardResult, GuardConfig } from "../core/types";
/**
* Abstract base class for all guards
*/
export abstract class Guard {
protected config: GuardConfig;
constructor(config: GuardConfig = {}) {
this.config = {
threshold: 0.8,
blockOnDetection: true,
enabled: true,
...config,
};
}
/**
* Get the name of this guard
*/
abstract get name(): string;
/**
* Validate the input
* @param input - The input text to validate
* @param metadata - Additional metadata (e.g., user info, session)
* @returns GuardResult indicating if input passed
*/
abstract validate(
input: string,
metadata?: Record<string, any>
): Promise<GuardResult> | GuardResult;
/**
* Check if this guard is enabled
*/
isEnabled(): boolean {
return this.config.enabled ?? true;
}
/**
* Check if guard should block on detection
*/
shouldBlock(): boolean {
return this.config.blockOnDetection ?? true;
}
/**
* Get the threshold
*/
getThreshold(): number {
return this.config.threshold ?? 0.8;
}
}