AgentDetector

The AgentDetector is the main entry point for interacting with the Superline Agent Detection library. It manages the initialization, configuration, and detection process.

You need to create an instance of this class, providing necessary providers.

Constructor

Creates a new agent detector instance.

new AgentDetector(
  metadataProvider: MetadataPort,
  eventsProvider: EventPort,
  eventStorage?: IEventStorage, // Optional, defaults to BrowserEventStorage
  throttleConfig?: ThrottleConfig // Optional, defaults to DEFAULT_THROTTLE_CONFIG
)
import AgentDetector from '@superline-ai/agent-detection';
import { BrowserMetadataProvider, BrowserEventProvider } from '@superline-ai/agent-detection/providers'; // Assuming provider paths

const metadataProvider = new BrowserMetadataProvider();
const eventsProvider = new BrowserEventProvider();

const detector = new AgentDetector(metadataProvider, eventsProvider);

Instance Methods

init

Initializes the agent detector instance with the provided configuration.

init(options?: AgentDetectorInitOptions): AgentDetector
// detector instance created as shown in constructor example

// Initialize with default options
AgentDetector.init();

startDetection

Starts the detection process, initializing all extractors and beginning data collection.

startDetection(): AgentDetector
// detector instance created and initialized (with autoStart: false)
AgentDetector.init({ autoStart: false });

// Start collection manually
AgentDetector.startDetection();

getCurrentDetectionResult

Gets the current detection result based on data collected so far. Does not stop the detection process or remove event listeners.

getCurrentDetectionResult(): Promise<DetectionResult>
// detector instance created and initialized
AgentDetector.init();
// (wait for some interaction)

async function checkCurrentStatus() {
  const result = await AgentDetector.getCurrentDetectionResult();
  console.log('Current isAgent:', result.isAgent);
  console.log('Current Confidence:', result.confidence);
}

// Call periodically or on demand
setInterval(checkCurrentStatus, 10000); 

finalizeDetection

Finalizes the detection process, computes a final result based on collected data, and cleans up listeners. Stops collecting new events but keeps the current session data.

finalizeDetection(): Promise<DetectionResult>
// detector instance created and initialized
AgentDetector.init();

// Later, get final detection result
async function checkAgent() {
  // Ensure sufficient time for data collection
  await new Promise(resolve => setTimeout(resolve, 5000)); 
  
  const result = await AgentDetector.finalizeDetection();
  console.log('Final isAgent:', result.isAgent);
  console.log('Final Confidence:', result.confidence);
  
  // Note: Detection is stopped after this call
}

checkAgent();

cleanup

Cleans up all resources used by the agent detector instance. Stops all event listeners and clears stored session events.

cleanup(): void
// detector instance created and initialized
AgentDetector.init();

// When the detector is no longer needed (e.g., component unmount)
AgentDetector.cleanup();

cleanupOldEvents

Cleans up old events from storage based on age. Helps manage storage size.

cleanupOldEvents(maxAgeMs?: number): Promise<void>
// detector instance created

// Clean up events older than 1 week
AgentDetector.cleanupOldEvents(7 * 24 * 60 * 60 * 1000);

Next Steps