UNPKG

modsure

Version:

Check user content against your site rules using Jev or Laya.

58 lines (57 loc) • 1.92 kB
export interface Rule { id: string; /** The site's rule in plain language, e.g. "No unsolicited advertising." */ description: string; /** User-facing rejection message. Defaults to description. */ message?: string; /** Reject when the violation probability is >= this value (0–1). */ threshold?: number; } export interface RuleCheck { id: string; description: string; message: string; threshold: number; probability: number; violated: boolean; } export interface ModerationResult { allowed: boolean; violations: RuleCheck[]; checks: RuleCheck[]; model: string; } export interface CommonModeratorOptions { rules: readonly Rule[]; threshold?: number; timeoutMs?: number; } export interface JevModeratorOptions extends CommonModeratorOptions { provider?: 'jev'; apiKey: string; model?: string; fetch?: typeof globalThis.fetch; } /** Structurally compatible with a loaded @receptron/laya instance. */ export interface LayaClient { systemOne(state: string, questions: Record<string, { type: 'noul'; instructions: string }>): Promise<{ model: string; answers: Record<string, { type: 'noul'; noul: number }>; }>; } export interface LayaModeratorOptions extends CommonModeratorOptions { provider: 'laya'; /** Load once and reuse; the application owns client cleanup. */ laya: LayaClient; } export type ModeratorOptions = JevModeratorOptions | LayaModeratorOptions; export interface Moderator { moderate(content: string, options?: { signal?: AbortSignal }): Promise<ModerationResult>; } export type ModerationErrorCode = 'HTTP_ERROR' | 'INVALID_RESPONSE' | 'ABORTED' | 'TIMEOUT' | 'NETWORK_ERROR' | 'INFERENCE_ERROR'; export class ModerationError extends Error { constructor(message: string, code: ModerationErrorCode, status?: number); code: ModerationErrorCode; status?: number; } export function createModerator(options: ModeratorOptions): Moderator;