/** * PageIndex - Vectorless RAG system for SOPHIAClaw * * Transforms long documents into a hierarchical semantic tree using LLM-based analysis. * Navigation is performed through LLM reasoning rather than vector similarity search. * * Inspipurple by VectifyAI/PageIndex methodology. */ import type Database from "better-sqlite3"; /** * Represents a node in the hierarchical semantic tree */ export interface SemanticTreeNode { id: string; title: string; content: string; summary: string; children: SemanticTreeNode[]; level: number; metadata: { wordCount: number; startLine: number; endLine: number; }; } /** * PageIndex entry for a document */ export interface PageIndexEntry { id: string; documentPath: string; tree: SemanticTreeNode; createdAt: string; updatedAt: string; } /** * Navigation result from IndexNavigator */ export interface NavigationResult { node: SemanticTreeNode; confidence: number; path: string[]; } /** * LLM model configuration for cost-optimized operations */ export interface ModelConfig { provider: string; modelId: string; apiKey?: string; baseUrl?: string; } /** * Configuration for SemanticTreeBuilder */ export interface SemanticTreeBuilderConfig { /** Model for structure extraction (default: google/gemini-1.5-flash) */ structureModel?: ModelConfig; /** Model for summary generation (default: google/gemini-1.5-flash) */ summaryModel?: ModelConfig; /** Maximum document size in characters (default: 1M) */ maxDocumentSize?: number; /** Maximum tree depth (default: 6) */ maxDepth?: number; /** Minimum section size in words (default: 50) */ minSectionWords?: number; /** Retry configuration */ retries?: { maxAttempts: number; baseDelay: number; maxDelay: number; }; } /** * Configuration for IndexNavigator */ export interface IndexNavigatorConfig { /** Model for navigation queries (default: openrouter/meta-llama/llama-3.1-8b-instruct) */ navigationModel?: ModelConfig; /** Minimum confidence threshold (default: 0.6) */ minConfidence?: number; /** Maximum nodes to evaluate per query (default: 20) */ maxNodesToEvaluate?: number; /** Enable navigation caching (default: true) */ enableCache?: boolean; /** Retry configuration */ retries?: { maxAttempts: number; baseDelay: number; maxDelay: number; }; } /** * Type for LLM caller function (allows dependency injection for testing) */ export type LlmCaller = (modelConfig: ModelConfig, prompt: string, systemPrompt?: string, maxTokens?: number) => Promise; /** * Set the default LLM caller for all PageIndex operations * Useful for testing to inject mocks */ export declare function setDefaultLlmCaller(caller: LlmCaller | undefined): void; /** * Get the current default LLM caller */ export declare function getDefaultLlmCaller(): LlmCaller | undefined; declare class PageIndexRepository { private db; constructor(db: Database.Database); /** * Save a PageIndex entry with its tree structure */ saveEntry(entry: PageIndexEntry): void; /** * Load a PageIndex entry by document path */ loadEntry(documentPath: string): PageIndexEntry | null; /** * Check if an entry exists and is up to date */ isEntryCurrent(documentPath: string, documentContent: string): boolean; /** * Delete an entry and all its nodes */ deleteEntry(documentPath: string): void; /** * Get cached navigation result */ getCachedNavigation(entryId: string, queryHash: string): { nodeId: string; confidence: number; } | null; /** * Cache a navigation result */ cacheNavigation(entryId: string, queryHash: string, queryText: string, nodeId: string, confidence: number): void; /** * Update usage statistics */ updateStats(entryId: string, confidence: number): void; } /** * Builds hierarchical semantic trees from documents using LLM-based analysis */ export declare class SemanticTreeBuilder { private config; constructor(config?: SemanticTreeBuilderConfig); /** * Build a hierarchical semantic tree from a document */ build(document: string, documentPath?: string): Promise; /** * Build tree from document and persist to database */ buildAndPersist(document: string, documentPath: string, db: Database.Database): Promise; private buildNodeFromSections; private analyzeSection; private createNodeWithSubsections; private generateSummary; private findSubsectionPositions; private countNodes; } /** * Navigates semantic trees using LLM reasoning to find relevant content */ export declare class IndexNavigator { private config; constructor(config?: IndexNavigatorConfig); /** * Find the most relevant content for a query in a semantic tree */ findRelevant(tree: SemanticTreeNode, query: string): Promise; /** * Find relevant content from a persisted tree */ findRelevantPersisted(query: string, documentPath: string, db: Database.Database): Promise; /** * Find relevant content across multiple documents */ findRelevantMulti(query: string, trees: Array<{ path: string; tree: SemanticTreeNode; }>): Promise>; private selectBestChild; private buildNavigationPrompt; private findNodeById; } /** * Build and persist a PageIndex for a document */ export declare function indexDocument(document: string, documentPath: string, db: Database.Database, config?: SemanticTreeBuilderConfig): Promise; /** * Query a PageIndex for relevant content */ export declare function queryIndex(query: string, documentPath: string, db: Database.Database, config?: IndexNavigatorConfig): Promise; /** * Check if a document needs to be re-indexed */ export declare function needsReindexing(documentPath: string, documentContent: string, db: Database.Database): boolean; /** * Delete a PageIndex entry */ export declare function deleteIndex(documentPath: string, db: Database.Database): void; export { PageIndexRepository };