src / core / RAGEngine.ts
import { KnowledgeTriple, DecomposedQuery, CompressedContextPayload } from '../schema/contextInterfaces';
// FIX APPLIED HERE: Using explicit relative paths to force module resolution consistency.
import { QueryPlanner } from './core/QueryPlanner';
import { GraphRetriever } from '../graph/GraphRetriever';
/**
* Core orchestration engine for the advanced RAG plugin.
* This module manages the entire process flow: Query Planning -> Retrieval -> Compilation.
* It accepts initialized client objects, making it fully testable and independent of the environment setup.
*/
export class RAGEngine {
private planner: QueryPlanner;
private graphretriever: GraphRetriever;
// The constructor now requires ALL necessary services/clients to be passed in.
constructor(llmClient: any, dbConnection: any) {
this.planner = new QueryPlanner(llmClient);
this.graphretriever = new GraphRetriever(dbConnection);
}
/**
* Processes a raw user query into a maximally compressed context payload using injected clients.
* @param rawQuery The original question from the user.
* @returns A structured payload ready to be injected into the prompt.
*/
public async processQuery(rawQuery: string): Promise<CompressedContextPayload> {
console.log(`\n========================================================`);
console.log(`[ENGINE START] Processing query: "${rawQuery}"`);
// Step 1: Query Decomposition & Planning
const plan = await this.planner.planDecomposition(rawQuery);
let graphTriples: KnowledgeTriple[] = [];
let synthesizedNotes: string = "";
if (plan) {
console.log(`[ENGINE] Plan generated. Retrieving structured knowledge from Graph.`);
// Step 2: Multi-modal Retrieval using the Plan
graphTriples = await this.graphretriever.retrieveTriplesByPlan(plan);
synthesizedNotes = "Knowledge derived from the following specific plan:";
} else {
console.warn("[ENGINE] No complex plan needed or detected. Falling back to general context search.");
// Fallback logic remains abstract, awaiting a real simple-search client implementation
graphTriples = [];
synthesizedNotes = "General, foundational knowledge was retrieved from standard document sources.";
}
// Step 3: Final Compilation - The token compression payoff!
const finalPayload: CompressedContextPayload = {
sourceType: plan ? 'DECOMPOSED_SEARCH' : 'GRAPH_KNOWLEDGE',
triples: graphTriples,
plan: plan,
summaryNotes: synthesizedNotes,
};
console.log("[ENGINE] Context payload successfully compiled.");
return finalPayload;
}
}