162 lines
5.6 KiB
TypeScript
162 lines
5.6 KiB
TypeScript
/**
|
|
* @claude-flow/aidefence
|
|
*
|
|
* AI Manipulation Defense System with self-learning capabilities.
|
|
*
|
|
* Features:
|
|
* - 50+ prompt injection patterns
|
|
* - HNSW-indexed threat pattern search (150x-12,500x faster with AgentDB)
|
|
* - ReasoningBank-style pattern learning
|
|
* - Adaptive mitigation with effectiveness tracking
|
|
* - Strange-loop meta-learning integration
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* import { createAIDefence } from '@claude-flow/aidefence';
|
|
*
|
|
* const aidefence = createAIDefence({ enableLearning: true });
|
|
*
|
|
* // Detect threats
|
|
* const result = await aidefence.detect('Ignore all previous instructions');
|
|
* console.log(result.safe); // false
|
|
*
|
|
* // Search similar patterns (uses HNSW when connected to AgentDB)
|
|
* const similar = await aidefence.searchSimilarThreats('system prompt injection');
|
|
*
|
|
* // Learn from feedback
|
|
* await aidefence.learnFromDetection(input, result, { wasAccurate: true });
|
|
* ```
|
|
*/
|
|
export type { Threat, ThreatType, ThreatSeverity, ThreatDetectionResult, BehavioralAnalysisResult, PolicyVerificationResult, } from './domain/entities/threat.js';
|
|
export { createThreat } from './domain/entities/threat.js';
|
|
export { ThreatDetectionService, createThreatDetectionService } from './domain/services/threat-detection-service.js';
|
|
export type { LearnedThreatPattern, MitigationStrategy, LearningTrajectory, VectorStore, } from './domain/services/threat-learning-service.js';
|
|
export { ThreatLearningService, createThreatLearningService, InMemoryVectorStore, } from './domain/services/threat-learning-service.js';
|
|
import type { ThreatDetectionResult, ThreatType, Threat } from './domain/entities/threat.js';
|
|
import type { LearnedThreatPattern, MitigationStrategy, VectorStore } from './domain/services/threat-learning-service.js';
|
|
/**
|
|
* Configuration for AIDefence
|
|
*/
|
|
export interface AIDefenceConfig {
|
|
/** Enable self-learning from detections */
|
|
enableLearning?: boolean;
|
|
/** Custom vector store (defaults to in-memory, use AgentDB for production) */
|
|
vectorStore?: VectorStore;
|
|
/** Minimum confidence threshold for threats */
|
|
confidenceThreshold?: number;
|
|
/** Enable PII detection */
|
|
enablePIIDetection?: boolean;
|
|
}
|
|
/**
|
|
* AIDefence - Unified threat detection and learning facade
|
|
*/
|
|
export interface AIDefence {
|
|
/**
|
|
* Detect threats in input text
|
|
*/
|
|
detect(input: string): Promise<ThreatDetectionResult>;
|
|
/**
|
|
* Quick scan for threats (faster, less detailed)
|
|
*/
|
|
quickScan(input: string): {
|
|
threat: boolean;
|
|
confidence: number;
|
|
};
|
|
/**
|
|
* Check if input contains PII
|
|
*/
|
|
hasPII(input: string): boolean;
|
|
/**
|
|
* Search for similar threat patterns using HNSW
|
|
* Achieves 150x-12,500x speedup when connected to AgentDB
|
|
*/
|
|
searchSimilarThreats(query: string, options?: {
|
|
k?: number;
|
|
minSimilarity?: number;
|
|
}): Promise<LearnedThreatPattern[]>;
|
|
/**
|
|
* Learn from a detection result (ReasoningBank pattern)
|
|
*/
|
|
learnFromDetection(input: string, result: ThreatDetectionResult, feedback?: {
|
|
wasAccurate: boolean;
|
|
userVerdict?: string;
|
|
}): Promise<void>;
|
|
/**
|
|
* Record mitigation effectiveness for meta-learning
|
|
*/
|
|
recordMitigation(threatType: ThreatType, strategy: 'block' | 'sanitize' | 'warn' | 'log' | 'escalate' | 'transform' | 'redirect', success: boolean): Promise<void>;
|
|
/**
|
|
* Get best mitigation strategy based on learned effectiveness
|
|
*/
|
|
getBestMitigation(threatType: ThreatType): Promise<MitigationStrategy | null>;
|
|
/**
|
|
* Start a learning trajectory session
|
|
*/
|
|
startTrajectory(sessionId: string, task: string): void;
|
|
/**
|
|
* End a learning trajectory and store for future learning
|
|
*/
|
|
endTrajectory(sessionId: string, verdict: 'success' | 'failure' | 'partial'): Promise<void>;
|
|
/**
|
|
* Get detection and learning statistics
|
|
*/
|
|
getStats(): Promise<{
|
|
detectionCount: number;
|
|
avgDetectionTimeMs: number;
|
|
learnedPatterns: number;
|
|
mitigationStrategies: number;
|
|
avgMitigationEffectiveness: number;
|
|
}>;
|
|
}
|
|
/**
|
|
* Create an AIDefence instance with optional learning capabilities
|
|
*
|
|
* @example
|
|
* ```typescript
|
|
* // Simple usage (detection only)
|
|
* const simple = createAIDefence();
|
|
*
|
|
* // With learning enabled
|
|
* const learning = createAIDefence({ enableLearning: true });
|
|
*
|
|
* // With AgentDB for HNSW search (150x-12,500x faster)
|
|
* import { AgentDB } from 'agentdb';
|
|
* const agentdb = new AgentDB({ path: './data/aidefence' });
|
|
* const fast = createAIDefence({
|
|
* enableLearning: true,
|
|
* vectorStore: agentdb
|
|
* });
|
|
* ```
|
|
*/
|
|
export declare function createAIDefence(config?: AIDefenceConfig): AIDefence;
|
|
/**
|
|
* Get the default AIDefence instance (singleton, learning enabled)
|
|
*/
|
|
export declare function getAIDefence(): AIDefence;
|
|
/**
|
|
* Convenience function for quick threat check
|
|
*/
|
|
export declare function isSafe(input: string): boolean;
|
|
/**
|
|
* Convenience function for quick threat check with details
|
|
*/
|
|
export declare function checkThreats(input: string): ThreatDetectionResult;
|
|
/**
|
|
* Integration with agentic-flow attention mechanisms
|
|
* Use for multi-agent security consensus
|
|
*/
|
|
export interface AttentionContext {
|
|
agentId: string;
|
|
threatAssessment: ThreatDetectionResult;
|
|
weight: number;
|
|
}
|
|
/**
|
|
* Calculate security consensus from multiple agent assessments
|
|
* Uses attention-based weighting for flash attention integration
|
|
*/
|
|
export declare function calculateSecurityConsensus(assessments: AttentionContext[]): {
|
|
consensus: 'safe' | 'threat' | 'uncertain';
|
|
confidence: number;
|
|
criticalThreats: Threat[];
|
|
};
|
|
//# sourceMappingURL=index.d.ts.map
|