@kya-os/agentshield-nextjs
Version:
Next.js middleware for AgentShield AI agent detection
154 lines (149 loc) • 3.69 kB
text/typescript
import { NextRequest, NextResponse } from 'next/server';
import { DetectionResult, DetectionInput } from '@kya-os/agentshield-shared';
/**
* Storage adapter types for AgentShield
*/
/**
* Agent detection event stored in the system
*/
interface AgentDetectionEvent {
eventId: string;
sessionId: string;
timestamp: string;
agentType: string;
agentName: string;
confidence: number;
path: string;
userAgent?: string;
ipAddress?: string;
method: string;
detectionReasons: string[];
verificationMethod: string;
detectionDetails?: {
patterns?: string[];
behaviors?: string[];
fingerprintMatches?: string[];
};
}
/**
* Agent session information
*/
interface AgentSession {
sessionId: string;
ipAddress?: string;
userAgent?: string;
agentType: string;
agentName?: string;
firstSeen: string;
lastSeen: string;
eventCount: number;
paths: string[];
averageConfidence: number;
verificationMethods: string[];
}
/**
* Storage adapter interface
* All methods should be async for compatibility with different backends
*/
interface StorageAdapter {
/**
* Store a detection event
*/
storeEvent(event: AgentDetectionEvent): Promise<void>;
/**
* Get recent events
*/
getRecentEvents(limit?: number): Promise<AgentDetectionEvent[]>;
/**
* Get events for a specific session
*/
getSessionEvents(sessionId: string): Promise<AgentDetectionEvent[]>;
/**
* Store or update a session
*/
storeSession(session: AgentSession): Promise<void>;
/**
* Get a session by ID
*/
getSession(sessionId: string): Promise<AgentSession | null>;
/**
* Get recent sessions
*/
getRecentSessions(limit?: number): Promise<AgentSession[]>;
/**
* Clean up old data (optional)
*/
cleanup?(olderThan: Date): Promise<void>;
}
/**
* Storage configuration options
*/
interface StorageConfig {
/**
* Type of storage to use
*/
type: 'redis' | 'memory' | 'custom';
/**
* Redis configuration (if type is 'redis')
*/
redis?: {
url: string;
token: string;
};
/**
* Custom storage adapter (if type is 'custom')
*/
custom?: StorageAdapter;
/**
* TTL for stored data in seconds
* Default: 86400 (24 hours)
*/
ttl?: number;
}
/**
* Enhanced Next.js middleware with all features built-in
*/
/**
* Enhanced configuration with built-in features
*/
interface EnhancedMiddlewareConfig {
/**
* Storage configuration
*/
storage?: StorageConfig;
/**
* Session tracking configuration
*/
sessionTracking?: {
enabled?: boolean;
ttl?: number;
};
/**
* Paths to skip detection
*/
skipPaths?: string[];
/**
* Action when agent detected
*/
onAgentDetected?: 'block' | 'log' | 'allow';
/**
* Custom handler for agent detection
*/
onDetection?: (result: DetectionResult, context: DetectionInput) => void | Promise<void>;
/**
* Confidence threshold
*/
confidenceThreshold?: number;
/**
* Response when blocking
*/
blockedResponse?: {
status?: number;
message?: string;
};
}
/**
* Create enhanced AgentShield middleware with all features
*/
declare function createEnhancedAgentShieldMiddleware(config?: EnhancedMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
export { type AgentDetectionEvent, type AgentSession, type EnhancedMiddlewareConfig, type StorageAdapter, type StorageConfig, createEnhancedAgentShieldMiddleware };