/** * API Pattern Learner * * A generalized system for learning API patterns from successful extractions * and applying them to new, similar sites. * * This component: * 1. Defines pattern templates extracted from existing handlers * 2. Stores learned patterns in a registry * 3. Matches URLs to known patterns * 4. Applies patterns to extract content */ import type { ApiDomainGroup, ApiExtractionSuccess, ApiPatternTemplate, BootstrapPattern, ContentMapping, LearnedApiPattern, PatternLearningListener, PatternMatch, PatternRegistryConfig, PatternRegistryStats, PatternTemplateType, PatternTransferOptions, PatternTransferResult, PatternValidation, SiteSimilarityScore } from '../types/api-patterns.js'; /** * Pattern templates representing the 5 major API pattern types * identified from the 8 existing handlers. */ export declare const PATTERN_TEMPLATES: ApiPatternTemplate[]; export declare const API_DOMAIN_GROUPS: ApiDomainGroup[]; /** * Bootstrap patterns extracted from the 8 existing handlers. * These are used to seed the pattern registry with known-working patterns. */ export declare const BOOTSTRAP_PATTERNS: BootstrapPattern[]; /** * API Pattern Registry - stores and manages learned patterns */ export declare class ApiPatternRegistry { private patterns; private domainIndex; private templateIndex; private listeners; private store; private config; private initialized; constructor(config?: Partial); /** * Initialize the registry, loading persisted patterns and bootstrapping */ initialize(): Promise; /** * Ensure all bootstrap patterns are loaded, adding any that are missing. * This is called on every initialization to ensure bootstrap patterns * are always available, even when persisted learned patterns exist. */ private ensureBootstrapPatterns; /** * Bootstrap the registry with known patterns from existing handlers * @deprecated Use ensureBootstrapPatterns() instead */ private bootstrap; /** * Add a pattern to the internal indexes */ private addToIndexes; /** * Persist patterns to disk */ private persist; /** * Emit a learning event to all listeners */ private emit; /** * Subscribe to pattern learning events */ subscribe(listener: PatternLearningListener): () => void; /** * Get a pattern by ID */ getPattern(id: string): LearnedApiPattern | undefined; /** * Get all patterns for a domain */ getPatternsForDomain(domain: string): LearnedApiPattern[]; /** * Get all patterns of a specific template type */ getPatternsByType(type: PatternTemplateType): LearnedApiPattern[]; /** * Find patterns that match a URL * Optimized to check domain-indexed patterns first before falling back to all patterns */ findMatchingPatterns(url: string): PatternMatch[]; /** * Try to match a URL against a pattern */ private tryMatch; /** * Extract a variable from a URL using an extractor definition */ private extractVariable; /** * Update pattern metrics after an application attempt */ updatePatternMetrics(patternId: string, success: boolean, domain: string, responseTime?: number, failureReason?: string): Promise; /** * Learn a new pattern from a successful extraction */ learnPattern(templateType: PatternTemplateType, sourceUrl: string, apiEndpoint: string, contentMapping: ContentMapping, validation: PatternValidation): Promise; /** * Learn from a successful API extraction event * Called by ContentIntelligence when an API strategy succeeds */ learnFromExtraction(event: ApiExtractionSuccess): Promise; /** * Infer the template type from a strategy name and URLs */ private inferTemplateType; /** * Infer content mapping from extracted content * Searches for extracted values within the structured response to find their JSON paths */ private inferContentMapping; /** * Recursively search for a value within an object and return its JSON path * Returns null if the value is not found */ private findValuePath; /** * Infer variable extractors from URL and endpoint comparison * Returns both the extractors and the actual values that were extracted */ private inferExtractors; /** * Create a URL pattern from a source URL */ private createUrlPattern; /** * Create an endpoint template from an API endpoint and extracted values * Replaces actual values with {varName} placeholders */ private createEndpointTemplate; /** * Get registry statistics */ getStats(): PatternRegistryStats; /** * Clean up stale patterns */ cleanup(): Promise; /** * Force persist immediately */ flush(): Promise; /** * Get the API domain group for a domain */ getApiDomainGroup(domain: string): ApiDomainGroup | null; /** * Calculate similarity score between a source pattern and a target domain */ calculateSimilarity(sourcePattern: LearnedApiPattern, targetDomain: string): SiteSimilarityScore; /** * Find patterns that can potentially be transferred to a target domain * Returns patterns from similar sites that might work for the target */ findTransferablePatterns(targetDomain: string, options?: PatternTransferOptions): Array<{ pattern: LearnedApiPattern; similarity: SiteSimilarityScore; }>; /** * Transfer a pattern to a new target domain * Creates a new pattern derived from the source with reduced confidence */ transferPattern(sourcePatternId: string, targetDomain: string, targetUrlPattern: string, options?: PatternTransferOptions): Promise; /** * Automatically transfer applicable patterns to a new domain * Called when visiting a domain without existing patterns */ autoTransferPatterns(targetDomain: string, targetUrl: string, options?: PatternTransferOptions): Promise; /** * Generate a URL pattern for a new domain based on the source pattern */ private generateUrlPatternForDomain; /** * Record the outcome of using a transferred pattern * Boosts confidence on success, reduces on failure */ recordTransferOutcome(patternId: string, success: boolean, domain: string, responseTime: number, failureReason?: string): Promise; /** * Get all API domain groups */ getApiDomainGroups(): ApiDomainGroup[]; /** * Discover and learn patterns from OpenAPI/Swagger specification * Probes common locations for specs and generates patterns from found endpoints */ discoverFromOpenAPI(domain: string, options?: import('../types/api-patterns.js').OpenAPIDiscoveryOptions): Promise; /** * Check if we have OpenAPI-derived patterns for a domain */ hasOpenAPIPatterns(domain: string): boolean; /** * Get all OpenAPI-derived patterns for a domain */ getOpenAPIPatterns(domain: string): LearnedApiPattern[]; /** Anti-patterns learned from repeated failures */ private antiPatterns; /** * Secondary index for O(1) anti-pattern lookup by pattern+category * Maps `${patternId}:${category}` to anti-pattern ID */ private antiPatternIndex; /** * Record a pattern failure with classification * Returns the failure classification for use in retry logic */ recordPatternFailure(patternId: string, domain: string, attemptedUrl: string, statusCode: number | undefined, errorMessage: string, responseTime?: number): Promise; /** * Update extended failure tracking for a pattern */ private updatePatternFailureTracking; /** * Check if an anti-pattern should be created and create it */ private maybeCreateAntiPattern; /** * Check if a URL matches any active anti-patterns * Returns matching anti-patterns if found */ checkAntiPatterns(url: string): Promise; /** * Get retry strategy for a failed pattern */ getRetryStrategy(patternId: string, attemptNumber: number): Promise<{ shouldRetry: boolean; waitMs: number; strategy: import('../types/api-patterns.js').RetryStrategy; }>; /** * Analyze pattern health based on failure history */ analyzePatternHealth(patternId: string): Promise<{ isHealthy: boolean; dominantFailureType: import('../types/api-patterns.js').FailureCategory | null; suggestedAction: import('../types/api-patterns.js').RetryStrategy; reason: string; }>; /** * Get all active anti-patterns */ getActiveAntiPatterns(): import('../types/api-patterns.js').AntiPattern[]; /** * Clear an anti-pattern (e.g., after user provides authentication) */ clearAntiPattern(antiPatternId: string): Promise; /** * Import an anti-pattern from external source (e.g., LearningEngine persistence). * This adds the anti-pattern to the registry without emitting an event. * @param antiPattern The anti-pattern to import * @returns true if imported, false if already exists or expired */ importAntiPattern(antiPattern: import('../types/api-patterns.js').AntiPattern): boolean; /** * Get failure summary for a pattern */ getPatternFailureSummary(patternId: string): Promise; } /** * Get the template for a given type */ export declare function getPatternTemplate(type: PatternTemplateType): ApiPatternTemplate | undefined; /** * Suggest a template type for a URL based on indicators */ export declare function suggestTemplateType(url: string): PatternTemplateType | null; //# sourceMappingURL=api-pattern-learner.d.ts.map