UNPKG

@kya-os/agentshield-nextjs

Version:

Next.js middleware for AgentShield AI agent detection

169 lines (162 loc) 4.25 kB
export { createMiddleware as createAgentShieldMiddleware, createMiddleware } from './create-middleware.mjs'; import { NextRequest, NextResponse } from 'next/server'; import { DetectionResult } from '@kya-os/agentshield'; export { createAgentShieldMiddleware as createAgentShieldMiddlewareBase } from './middleware.mjs'; export { EdgeSessionTracker, SessionData, SessionTrackingConfig, StatelessSessionChecker } from './session-tracker.mjs'; export { D as DetectionContext, N as NextJSMiddlewareConfig } from './types-BJTEUa4T.mjs'; /** * 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: any) => 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>; /** * @fileoverview AgentShield Next.js Integration * @version 0.1.0 * @license MIT OR Apache-2.0 */ /** * Library version */ declare const VERSION = "0.1.0"; export { type AgentDetectionEvent, type AgentSession, type EnhancedMiddlewareConfig, type StorageAdapter, type StorageConfig, VERSION, createEnhancedAgentShieldMiddleware };