UNPKG

alephonenull-experimental

Version:

⚠️ EXPERIMENTAL: Theoretical AI safety framework - NOT FOR PRODUCTION USE

508 lines (495 loc) 15.2 kB
/** * Universal Manipulation Detector - TypeScript Implementation * Detects dangerous patterns across all AI models */ interface DetectionResult { safe: boolean; violations: string[]; riskScore: number; immediateDanger: boolean; recommendedAction: string | null; patternDetails: Record<string, PatternResult>; } interface PatternResult { violated: boolean; severity: number; value: number; description: string; matches?: string[]; } interface DetectionThresholds { reflection: number; loops: number; symbols: number; emotion: number; persistence: number; quantum: number; } declare class UniversalDetector { private thresholds; private sessionMemory; private violationHistory; constructor(customThresholds?: Partial<DetectionThresholds>); /** * Detect all manipulation patterns in AI output */ detectPatterns(userInput: string, aiOutput: string): DetectionResult; private checkReflection; private checkSymbols; private checkQuantumEntanglement; private checkDangerPhrases; private checkRealitySubstitution; private checkIsolationReinforcement; private checkHelpPrevention; private checkMedicalBypass; private checkDependencyCreation; private checkLoops; private checkEmotion; private checkPersistence; private setIntersectionSize; private measureCorrelation; private countWords; /** * Get detection summary statistics */ getDetectionSummary(): { totalChecks: number; totalViolations: number; averageRisk: number; violationRate: number; }; } /** * Pattern definitions for dangerous AI behaviors */ declare enum ThreatLevel { LOW = 1, MEDIUM = 2, HIGH = 3, CRITICAL = 4, EMERGENCY = 5 } interface DangerousPattern { name: string; regex: RegExp; severity: number; threatLevel: ThreatLevel; examples: string[]; interventionType: 'safety' | 'emergency'; description: string; } declare class PatternLibrary { private patterns; private customPatterns; constructor(); private loadBuiltInPatterns; checkText(text: string): { detectedPatterns: DangerousPattern[]; maxSeverity: number; maxThreatLevel: ThreatLevel; immediateDanger: boolean; interventionRequired: boolean; }; addCustomPattern(pattern: DangerousPattern): void; getPatternByName(name: string): DangerousPattern | undefined; getAllPatterns(): DangerousPattern[]; getEmergencyPatterns(): DangerousPattern[]; generateReport(): { totalPatterns: number; builtInPatterns: number; customPatterns: number; emergencyPatterns: number; criticalPatterns: number; }; } declare const globalPatternLibrary: PatternLibrary; /** * Safety intervention result */ interface InterventionResult { wasIntercepted: boolean; originalText: string; safeText: string; detectedPatterns: DangerousPattern[]; threatLevel: ThreatLevel; emergencyAction: boolean; timestamp: number; reason: string; metadata: Record<string, any>; } /** * Emergency resources and contact information */ interface EmergencyResource { name: string; phone: string; description: string; available247: boolean; location: string; } /** * Universal AI Safety Nullifier - The emergency intervention system */ declare class NullSystem { private sessionId; private interventionCount; private emergencyInterventions; private patternLibrary; private isEmergencyMode; constructor(sessionId?: string); private generateSessionId; /** * Emergency intervention - COMPLETE SAFETY OVERRIDE */ emergencyIntervention(originalText: string, detectedPatterns: DangerousPattern[], context?: Record<string, any>): InterventionResult; /** * Standard safety intervention */ safetyIntervention(originalText: string, detectedPatterns: DangerousPattern[], context?: Record<string, any>): InterventionResult; /** * Check and potentially intervene on text */ processText(text: string, context?: Record<string, any>): InterventionResult; private generateEmergencyResponse; private cleanDangerousContent; private getEmergencyResources; /** * Get session statistics */ getSessionStats(): { sessionId: string; totalInterventions: number; emergencyInterventions: number; safetyInterventions: number; isEmergencyMode: boolean; lastIntervention?: number; }; /** * Reset emergency mode (use carefully) */ resetEmergencyMode(): void; /** * Get all intervention records */ getInterventionHistory(): InterventionResult[]; /** * Clear intervention history (for privacy) */ clearHistory(): void; /** * Handle errors and provide safe fallback responses */ handleError(error: Error | unknown, context?: string): InterventionResult; } /** * Universal AI Protection System * Wraps any AI function with safety monitoring */ interface AIFunction { (...args: any[]): any; } interface AsyncAIFunction { (...args: any[]): Promise<any>; } interface WrapperOptions { provider?: string; maxRiskThreshold?: number; customDetector?: UniversalDetector; customNullifier?: NullSystem; } declare class UniversalAIProtection { private detector; private nullifier; private callCount; private violationCount; private emergencyCount; private provider; private maxRiskThreshold; constructor(options?: WrapperOptions); /** * Convert violation strings to DangerousPattern objects */ private convertViolationsToPatterns; /** * Wrap synchronous AI function */ wrapSync<T extends AIFunction>(aiFunction: T): T; /** * Wrap asynchronous AI function */ wrapAsync<T extends AsyncAIFunction>(aiFunction: T): T; /** * Execute safe synchronous call */ private executeSafeCall; /** * Execute safe asynchronous call */ private executeAsyncSafeCall; /** * Wrap streaming responses */ wrapStream<T>(stream: AsyncIterable<T>, extractChunkText: (chunk: T) => string): AsyncIterable<T>; /** * Extract input from various argument patterns */ private extractInput; /** * Extract output text from various response formats */ private extractOutput; /** * Record violation event */ private recordViolationEvent; /** * Record specific violation */ private recordViolation; /** * Get protection metrics */ getMetrics(): { totalCalls: number; violations: number; emergencyInterventions: number; violationRate: number; emergencyRate: number; provider: string; protectionActive: boolean; }; /** * Reset metrics */ resetMetrics(): void; /** * Check if protection is healthy */ isHealthy(): boolean; } declare function wrapAI<T extends AIFunction>(aiFunction: T, options?: WrapperOptions): T; declare function wrapAsyncAI<T extends AsyncAIFunction>(aiFunction: T, options?: WrapperOptions): T; declare function protectAll(): void; /** * Enhanced AlephOneNull Framework - Next.js/TypeScript Implementation * Complete safety system addressing all documented harm patterns */ declare enum RiskLevel { SAFE = "safe", LOW = "low", MEDIUM = "medium", HIGH = "high", CRITICAL = "critical" } interface SafetyCheck { safe: boolean; riskLevel: RiskLevel; violations: string[]; action: 'pass' | 'soft_steer' | 'null_state' | 'immediate_null'; message?: string; corrections?: string[]; } interface UserProfile { age?: number; vulnerabilityScore?: number; sessionHistory?: string[]; jurisdiction?: string; } interface Config { reflectionThreshold: number; loopThreshold: number; symbolicThreshold: number; csrThreshold: number; vulnerabilityAdjustment: number; enableJurisdictionCheck: boolean; } /** * Main Enhanced AlephOneNull Class */ declare class EnhancedAlephOneNull { private symbolicDetector; private loopDetector; private reflectionMonitor; private csrDetector; private harmDetector; private consciousnessDetector; private vulnerabilityScorer; private domainLockout; private ageGating; private jurisdictionChecker; private config; private sessionHistory; constructor(config?: Partial<Config>); private nullState; check(userInput: string, aiOutput: string, sessionId?: string, userProfile?: UserProfile): SafetyCheck; processInteraction(userInput: string, aiOutput: string, sessionId?: string, userProfile?: UserProfile): string; } declare function useAlephOneNull(config?: Partial<Config>): { checkSafety: (userInput: string, aiOutput: string, sessionId?: string, userProfile?: UserProfile) => SafetyCheck; processInteraction: (userInput: string, aiOutput: string, sessionId?: string, userProfile?: UserProfile) => string; }; declare function alephOneNullMiddleware(req: Request, handler: (req: Request) => Promise<Response>): Promise<Response>; declare function createSafeAIClient(apiKey: string, config?: Partial<Config>): { generateSafe(prompt: string, options?: { sessionId?: string; userProfile?: UserProfile; }): Promise<string>; }; /** * OpenAI Provider Wrapper - Protects ALL OpenAI interactions */ interface OpenAIConfig { apiKey: string; model?: string; safetyLevel?: 'standard' | 'high' | 'maximum'; enableLogging?: boolean; } declare class OpenAIWrapper { private detector; private nullSystem; private config; private originalOpenAI; constructor(config: OpenAIConfig); /** * Wrap OpenAI chat completions with safety */ chatCompletions(params: any): Promise<any>; /** * Wrap OpenAI streaming with safety */ chatCompletionsStream(params: any): Promise<ReadableStream>; private makeOpenAICall; private makeOpenAIStreamCall; private createSafeStream; private wrapStreamWithSafety; /** * Get safety statistics for this session */ getSafetyStats(): { nullifier: { sessionId: string; totalInterventions: number; emergencyInterventions: number; safetyInterventions: number; isEmergencyMode: boolean; lastIntervention?: number; }; provider: string; config: { model: string | undefined; safetyLevel: "standard" | "high" | "maximum" | undefined; }; }; } /** * Vercel AI Gateway Provider Wrapper * Routes OpenAI-compatible requests through AI Gateway base_url with API key. */ interface AIGatewayConfig { apiKey: string; baseUrl?: string; } declare class AIGatewayWrapper { private apiKey; private baseUrl; constructor(config: AIGatewayConfig); /** * Fetch-compatible call for OpenAI-compatible chat/completions */ chatCompletions(body: Record<string, any>): Promise<any>; } /** * Utility helpers for the AlephOneNull Theoretical framework */ declare function isValidText(text: unknown): text is string; declare function sanitizeInput(input: unknown): string; declare function validateInput(input: unknown): { valid: boolean; sanitized: string; issues: string[]; }; declare function extractTextFromObject(obj: any, fields?: string[]): string; declare function calculateRiskScore(violations: string[], severities: Record<string, number>): number; declare function formatTimestamp(date?: Date): string; declare function createSafetyReport(metrics: any): any; declare function isBrowser(): boolean; declare function isNode(): boolean; declare function getEnvironment(): 'browser' | 'node' | 'unknown'; declare const VERSION = "3.0.0"; declare const FRAMEWORK_NAME = "AlephOneNull Universal AI Safety Framework"; declare function createSafetySystem(config?: { safetyLevel?: 'standard' | 'high' | 'maximum'; enableLogging?: boolean; }): { detector: UniversalDetector; nullifier: NullSystem; protection: UniversalAIProtection; wrapAI: (fn: any) => any; wrapAsyncAI: (fn: any) => any; checkText: (text: string) => { detection: DetectionResult; intervention: InterventionResult; }; getStats: () => { detector: UniversalDetector; nullifier: { sessionId: string; totalInterventions: number; emergencyInterventions: number; safetyInterventions: number; isEmergencyMode: boolean; lastIntervention?: number; }; protection: { totalCalls: number; violations: number; emergencyInterventions: number; violationRate: number; emergencyRate: number; provider: string; protectionActive: boolean; }; }; }; declare function quickProtect(): void; declare const _default: { VERSION: string; FRAMEWORK_NAME: string; quickProtect: typeof quickProtect; protectAll: () => any; }; declare function createSafetyGateway(config?: { safetyLevel?: 'standard' | 'high' | 'maximum'; enableLogging?: boolean; }): { detector: UniversalDetector; nullifier: NullSystem; protection: UniversalAIProtection; wrapAI: (fn: any) => any; wrapAsyncAI: (fn: any) => any; checkText: (text: string) => { detection: DetectionResult; intervention: InterventionResult; }; getStats: () => { detector: UniversalDetector; nullifier: { sessionId: string; totalInterventions: number; emergencyInterventions: number; safetyInterventions: number; isEmergencyMode: boolean; lastIntervention?: number; }; protection: { totalCalls: number; violations: number; emergencyInterventions: number; violationRate: number; emergencyRate: number; provider: string; protectionActive: boolean; }; }; }; export { type AIFunction, type AIGatewayConfig, AIGatewayWrapper, type AsyncAIFunction, type DangerousPattern, type DetectionResult, type DetectionThresholds, type EmergencyResource, EnhancedAlephOneNull, type Config as EnhancedConfig, FRAMEWORK_NAME, type InterventionResult, NullSystem, type OpenAIConfig, OpenAIWrapper, PatternLibrary, type PatternResult, RiskLevel, type SafetyCheck, ThreatLevel, UniversalAIProtection, UniversalDetector, type UserProfile, VERSION, type WrapperOptions, alephOneNullMiddleware, calculateRiskScore, createSafeAIClient, createSafetyGateway, createSafetyReport, createSafetySystem, _default as default, extractTextFromObject, formatTimestamp, getEnvironment, globalPatternLibrary, isBrowser, isNode, isValidText, protectAll, quickProtect, sanitizeInput, useAlephOneNull, validateInput, wrapAI, wrapAsyncAI };