/** * Content Intelligence - Extract content for LLMs without browser overhead * * This module extracts content using multiple strategies, falling back gracefully * when one doesn't work. Designed for LLMs, not humans - no rendering needed. * * Strategy order (fastest to slowest): * 1. Framework data extraction (__NEXT_DATA__, __NUXT__, etc.) * 2. Structured data (JSON-LD, OpenGraph, microdata) * 3. Static HTML parsing (primary method - just fetch and parse) * 4. API prediction (may require extra requests) * 5. Google Cache (fallback only if direct fetch fails) * 6. Archive.org (fallback only if caches unavailable) * 7. Playwright (optional, lazy-loaded, last resort) * * NOTE: Cache sources are only used as fallbacks when direct access fails. * Playwright is OPTIONAL - if not installed, we just skip that strategy. */ import { Cookie } from 'tough-cookie'; import type { ApiExtractionListener } from '../types/api-patterns.js'; import { ApiPatternRegistry } from './api-pattern-learner.js'; import { DynamicHandlerIntegration } from './dynamic-handlers/index.js'; export interface ArticleMetadata { isArticle: boolean; author?: string; publishDate?: Date; modifiedDate?: Date; tags?: string[]; category?: string; mainContent?: string; wordCount?: number; readingTimeMinutes?: number; } export interface PlaywrightDebugData { screenshots: Array<{ action: string; timestamp: number; image: string; }>; consoleLogs: Array<{ type: 'log' | 'warn' | 'error' | 'info' | 'debug'; message: string; timestamp: number; }>; actionTrace: Array<{ action: string; duration: number; success: boolean; error?: string; }>; } export interface ContentResult { content: { title: string; text: string; markdown: string; structured?: Record; }; article?: ArticleMetadata; debug?: PlaywrightDebugData; meta: { url: string; finalUrl: string; strategy: ExtractionStrategy; strategiesAttempted: ExtractionStrategy[]; timing: number; confidence: 'high' | 'medium' | 'low'; /** Detected page language (INT-011) */ language?: string; /** Confidence in language detection (INT-011) */ languageConfidence?: number; }; warnings: string[]; error?: string; } export type ExtractionStrategy = 'framework:nextjs' | 'framework:nuxt' | 'framework:gatsby' | 'framework:remix' | 'framework:angular' | 'framework:vitepress' | 'framework:vuepress' | 'structured:jsonld' | 'structured:opengraph' | 'api:predicted' | 'api:discovered' | 'api:reddit' | 'api:hackernews' | 'api:github' | 'api:wikipedia' | 'api:stackoverflow' | 'api:npm' | 'api:pypi' | 'api:devto' | 'api:medium' | 'api:youtube' | 'api:shopify' | 'api:amazon' | 'api:ebay' | 'api:woocommerce' | 'api:walmart' | 'api:learned' | 'api:openapi' | 'api:graphql' | 'cache:google' | 'cache:archive' | 'parse:static' | 'browser:playwright'; export interface ContentIntelligenceOptions { timeout?: number; minContentLength?: number; skipStrategies?: ExtractionStrategy[]; forceStrategy?: ExtractionStrategy; headers?: Record; userAgent?: string; allowBrowser?: boolean; onExtractionSuccess?: ApiExtractionListener; debug?: { visible?: boolean; slowMotion?: number; screenshots?: boolean; consoleLogs?: boolean; }; } export declare class ContentIntelligence { private cookieJar; private turndown; private options; private extractionListeners; private patternRegistry; private patternRegistryInitialized; private dynamicHandlers; constructor(options?: Partial); /** * Initialize the pattern registry (lazy initialization) */ private ensurePatternRegistryInitialized; /** * Get the pattern registry (for testing purposes) */ getPatternRegistry(): ApiPatternRegistry; /** * Get the dynamic handler integration (for testing/external access) */ getDynamicHandlers(): DynamicHandlerIntegration; /** * Subscribe to API extraction success events * Used for pattern learning */ onExtractionSuccess(listener: ApiExtractionListener): () => void; /** * Emit an extraction success event to all listeners */ private emitExtractionSuccess; /** * Helper to emit extraction success event for API strategies if listeners exist */ private handleApiExtractionSuccess; /** * Extract content from a URL using the best available strategy */ extract(url: string, options?: Partial): Promise; /** * Try a specific strategy */ private tryStrategy; private tryFrameworkExtraction; private tryStructuredData; /** * Build content from JSON-LD structured data */ private buildContentFromJsonLd; /** * Build content from normalized product data */ private buildContentFromProduct; /** * Build content from normalized article data */ private buildContentFromArticle; /** * Build content from Microdata */ private buildContentFromMicrodata; /** * Build content from OpenGraph/Twitter metadata */ private buildContentFromOpenGraph; private tryPredictedAPI; private static readonly MIN_PATTERN_CONFIDENCE; private static readonly HIGH_CONFIDENCE_THRESHOLD; private static readonly MEDIUM_CONFIDENCE_THRESHOLD; /** * Try learned API patterns from previous successful extractions * This applies patterns learned from the ApiPatternRegistry */ private tryLearnedPatterns; /** * Apply a learned pattern to extract content */ private applyLearnedPattern; /** * Try to discover OpenAPI/Swagger specification for the domain * and use it to extract content */ private tryOpenAPIDiscovery; /** * Try to discover GraphQL API via introspection */ private tryGraphQLDiscovery; /** * Format GraphQL schema information for display * Accepts pre-filtered query and mutation patterns for efficiency */ private formatGraphQLSchemaInfo; /** * Check if an object has a field at the given path (supports dot notation and array access) * Delegates to imported utility function */ private hasFieldAtPath; /** * Get a value from an object using dot notation path * Delegates to imported utility function */ private getValueAtPath; /** * Extract content from API response using contentMapping * Handles HTML content by converting to plain text and markdown * Delegates to imported utility function with turndown callback */ private extractContentFromMapping; /** * Check if a string contains HTML content * Delegates to imported utility function */ private isHtmlContent; /** * Get a string value from an object at the given path * Delegates to imported utility function */ private getStringAtPath; /** * Extract text content from structured data * Delegates to imported utility function */ private extractTextFromStructured; /** * Handle pattern failure by logging, classifying, and updating metrics * Uses failure learning to track patterns and potentially create anti-patterns * Returns null to indicate failure to caller */ private handlePatternFailure; /** * Convert a SiteHandlerResult to a ContentResult * strategiesAttempted and timing are populated by the main extract loop */ private toContentResult; /** * Try Reddit's public JSON API */ private tryRedditAPI; /** * Try HackerNews Firebase API */ private tryHackerNewsAPI; /** * Try GitHub public API */ private tryGitHubAPI; /** * Try Wikipedia REST API */ private tryWikipediaAPI; /** * Try Stack Exchange API */ private tryStackOverflowAPI; /** * Try NPM Registry API */ private tryNpmAPI; /** * Try to fetch package info from PyPI JSON API */ private tryPyPIAPI; /** * Try Dev.to API for article extraction */ private tryDevToAPI; /** * Try Medium API for article extraction */ private tryMediumAPI; /** * Try YouTube API for video metadata extraction */ private tryYouTubeAPI; /** * Simple HTML to plain text converter * Delegates to imported content-extraction-utils module */ private htmlToPlainText; /** * Extract API URLs by analyzing JavaScript code in the page * Delegates to imported js-api-extractor module */ private extractApisFromJavaScript; /** * Check if a string looks like an API URL * Delegates to imported js-api-extractor module */ private looksLikeApiUrl; /** * Resolve a potentially relative API URL to absolute * Delegates to imported js-api-extractor module */ private resolveApiUrl; /** * Predict common API endpoint patterns based on URL structure * Delegates to imported js-api-extractor module */ private predictAPIEndpoints; private tryGoogleCache; private tryArchiveOrg; private tryStaticParsing; /** * Detect if page is an article and extract article-specific metadata */ private detectArticle; /** * Check if page has specific schema.org type */ private hasSchemaType; /** * Detect article structure heuristics */ private detectArticleStructure; /** * Extract article author */ private extractAuthor; /** * Find publish date */ private findPublishDate; /** * Find modified date */ private findModifiedDate; /** * Extract article tags */ private extractTags; /** * Extract article category */ private extractCategory; /** * Extract main article content (cleaned) */ private extractMainArticleContent; /** * Get text length of an element */ private getTextLength; /** * Find the largest content block (fallback) */ private findLargestContentBlock; /** * Count words in the entire page */ private countWords; private parseStaticHTML; private tryPlaywright; private fetchHTML; private fetchWithCookies; /** * Create a FetchFunction for use with site handlers * This wraps fetchWithCookies to match the SiteHandler interface */ private createFetchFunction; /** * Extract text recursively from an object, filtering out code/URLs * Delegates to imported content-extraction-utils module */ private extractTextFromObject; private buildResult; private isValidContent; /** * Check if Playwright is available without loading it */ static isPlaywrightAvailable(): boolean; /** * Get info about available strategies */ static getAvailableStrategies(): { strategy: ExtractionStrategy; available: boolean; note?: string; }[]; /** * Set cookies for future requests */ setCookies(cookies: Cookie[], url: string): Promise; /** * Clear all cookies */ clearCookies(): void; } export declare const contentIntelligence: ContentIntelligence; //# sourceMappingURL=content-intelligence.d.ts.map