UNPKG

cortexweaver

Version:

CortexWeaver is a command-line interface (CLI) tool that orchestrates a swarm of specialized AI agents, powered by Claude Code and Gemini CLI, to assist in software development. It transforms a high-level project plan (plan.md) into a series of coordinate

184 lines 7.77 kB
"use strict"; /** * Context Primer Core Module * * Contains the main context priming orchestration logic */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ContextPrimerCore = void 0; const analysis_1 = require("./analysis"); class ContextPrimerCore { constructor(canvas, workspace, contractsPath = './contracts') { this.canvas = canvas; this.workspace = workspace; this.analysis = new analysis_1.ContextAnalysis(contractsPath); } async primeContext(task, agentType, projectId, options = {}) { const defaults = { maxCodeModules: 10, maxPheromones: 5, maxSimilarTasks: 3, maxWorkspaceFiles: 15, maxContractSnippets: 8, includeTests: true, includeDocs: true }; const opts = { ...defaults, ...options }; // Fetch context data in parallel for performance const [architecturalDecisions, allCodeModules, allContracts, allPheromones, dependencies, workspaceFiles, contractSnippets] = await Promise.all([ this.getArchitecturalDecisions(projectId), this.getCodeModules(projectId), this.getContracts(projectId), this.getRelevantPheromones(task, agentType, opts.maxPheromones), this.getTaskDependencies(task.id), this.getRelevantWorkspaceFiles(task, agentType, opts), this.getRelevantContractSnippets(task, agentType, opts.maxContractSnippets) ]); // Filter and prioritize code modules based on relevance to task and agent type const codeModules = this.analysis.prioritizeCodeModules(allCodeModules, task, agentType, opts.maxCodeModules); // Filter contracts based on relevance const contracts = this.analysis.prioritizeContracts(allContracts, task, agentType); // Find similar tasks for learning const similarTasks = await this.findSimilarTasks(task, opts.maxSimilarTasks); return { architecturalDecisions, codeModules, contracts, pheromones: allPheromones, dependencies, similarTasks, workspaceFiles, contractSnippets }; } async getArchitecturalDecisions(projectId) { try { return await this.canvas.getArchitecturalDecisionsByProject(projectId); } catch (error) { console.warn('Failed to fetch architectural decisions:', error); return []; } } async getCodeModules(projectId) { try { return await this.canvas.getCodeModulesByProject(projectId); } catch (error) { console.warn('Failed to fetch code modules:', error); return []; } } async getContracts(projectId) { try { return await this.canvas.getContractsByProject(projectId); } catch (error) { console.warn('Failed to fetch contracts:', error); return []; } } async getRelevantPheromones(task, agentType, maxCount) { try { // Use the enhanced pheromone system to get context-relevant pheromones const taskComplexity = this.analysis.estimateTaskComplexity(task); const taskContext = task.title + ' ' + task.description; const contextPheromones = await this.canvas.getContextPheromones(agentType, taskContext, taskComplexity); // Combine guides and warnings, prioritizing guides but including warnings for balance const allPheromones = [ ...contextPheromones.guides, ...contextPheromones.warnings.slice(0, Math.max(1, Math.floor(maxCount * 0.3))) // Max 30% warnings ]; // If we don't have enough from context search, fall back to legacy types for backwards compatibility if (allPheromones.length < maxCount) { const legacyTypes = this.analysis.getPheromoneTypesForAgent(agentType); const legacyPheromones = []; for (const type of legacyTypes) { const pheromones = await this.canvas.getPheromonesByType(type); legacyPheromones.push(...pheromones); } // Add legacy pheromones that aren't already included const existingIds = new Set(allPheromones.map(p => p.id)); const newLegacyPheromones = legacyPheromones .filter(p => !existingIds.has(p.id)) .slice(0, maxCount - allPheromones.length); allPheromones.push(...newLegacyPheromones); } // Sort by strength and relevance, then limit return allPheromones .sort((a, b) => b.strength - a.strength) .slice(0, maxCount); } catch (error) { console.warn('Failed to fetch pheromones:', error); return []; } } async getTaskDependencies(taskId) { try { return await this.canvas.getTaskDependencies(taskId); } catch (error) { console.warn('Failed to fetch task dependencies:', error); return []; } } async findSimilarTasks(task, maxCount) { try { // Extract keywords from task title and description const keywords = this.analysis.extractKeywords(task.title + ' ' + task.description); return await this.canvas.findSimilarTasks(task.id, keywords); } catch (error) { console.warn('Failed to find similar tasks:', error); return []; } } async getRelevantWorkspaceFiles(task, agentType, options) { try { const projectRoot = this.workspace.getProjectRoot(); const files = await this.analysis.scanWorkspaceFiles(projectRoot); // Filter and score files based on relevance to task and agent type const relevantFiles = files .map(file => ({ ...file, relevanceScore: this.analysis.calculateFileRelevance(file, task, agentType) })) .filter(file => { if (!options.includeTests && file.type === 'test') return false; if (!options.includeDocs && file.type === 'documentation') return false; return file.relevanceScore > 0.1; // Minimum relevance threshold }) .sort((a, b) => b.relevanceScore - a.relevanceScore) .slice(0, options.maxWorkspaceFiles || 15); return relevantFiles; } catch (error) { console.warn('Failed to scan workspace files:', error); return []; } } async getRelevantContractSnippets(task, agentType, maxCount) { try { const snippets = await this.analysis.extractContractSnippets(); // Score and filter snippets based on relevance const relevantSnippets = snippets .map(snippet => ({ ...snippet, relevanceScore: this.analysis.calculateContractRelevance(snippet, task, agentType) })) .filter(snippet => snippet.relevanceScore > 0.1) .sort((a, b) => b.relevanceScore - a.relevanceScore) .slice(0, maxCount); return relevantSnippets; } catch (error) { console.warn('Failed to extract contract snippets:', error); return []; } } } exports.ContextPrimerCore = ContextPrimerCore; //# sourceMappingURL=core.js.map