From e9a439a31338bb4a246dad462bf5ba88faa32710 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Sat, 6 Sep 2025 15:07:26 +0000 Subject: [PATCH] feat: add repository context caching system for improved LLM performance - Implement RepositoryContextManager to maintain complete project context - Add configuration options for context management (enabled, maxFiles, includeFileContent, etc.) - Integrate cached context into environment details sent to LLM - Update system prompt to mention repository context feature - Add comprehensive test suite for RepositoryContextManager - Automatically update context on file changes with debouncing - Support smart file selection based on relevance and patterns - Extract code patterns (imports, definitions, APIs) for better context understanding This addresses issue #7735 by providing a complete and updated repository context for every LLM call, improving accuracy and reducing token consumption. --- packages/types/src/global-settings.ts | 13 + src/core/context/RepositoryContextManager.ts | 502 ++++++++++++++++++ .../RepositoryContextManager.spec.ts | 287 ++++++++++ src/core/environment/getEnvironmentDetails.ts | 10 + src/core/prompts/sections/capabilities.ts | 2 +- src/core/task/Task.ts | 26 + src/shared/ExtensionMessage.ts | 1 + 7 files changed, 840 insertions(+), 1 deletion(-) create mode 100644 src/core/context/RepositoryContextManager.ts create mode 100644 src/core/context/__tests__/RepositoryContextManager.spec.ts diff --git a/packages/types/src/global-settings.ts b/packages/types/src/global-settings.ts index f1c4b81c48..1379b697de 100644 --- a/packages/types/src/global-settings.ts +++ b/packages/types/src/global-settings.ts @@ -152,6 +152,19 @@ export const globalSettingsSchema = z.object({ hasOpenedModeSelector: z.boolean().optional(), lastModeExportPath: z.string().optional(), lastModeImportPath: z.string().optional(), + + // Repository context configuration + repositoryContext: z + .object({ + enabled: z.boolean(), + maxFileSize: z.number(), + maxFiles: z.number(), + includeFileContent: z.boolean(), + excludePatterns: z.array(z.string()), + updateInterval: z.number(), + smartSelection: z.boolean(), + }) + .optional(), }) export type GlobalSettings = z.infer diff --git a/src/core/context/RepositoryContextManager.ts b/src/core/context/RepositoryContextManager.ts new file mode 100644 index 0000000000..7f6d335d8d --- /dev/null +++ b/src/core/context/RepositoryContextManager.ts @@ -0,0 +1,502 @@ +import * as vscode from "vscode" +import * as path from "path" +import * as fs from "fs/promises" +import { glob } from "glob" +import crypto from "crypto" + +import { RooIgnoreController } from "../ignore/RooIgnoreController" +import { formatResponse } from "../prompts/responses" + +export interface FileContext { + path: string + content?: string + hash?: string + lastModified?: number + size?: number + isDirectory: boolean +} + +export interface RepositoryContext { + files: Map + lastUpdated: number + projectStructure: string + relevantFiles: string[] + codePatterns: Map // pattern -> file paths +} + +export interface RepositoryContextConfig { + enabled: boolean + maxFileSize: number // in bytes + maxFiles: number + includeFileContent: boolean + excludePatterns: string[] + updateInterval: number // in milliseconds + smartSelection: boolean // intelligently select relevant files +} + +const DEFAULT_CONFIG: RepositoryContextConfig = { + enabled: false, + maxFileSize: 100 * 1024, // 100KB + maxFiles: 500, + includeFileContent: false, + excludePatterns: ["node_modules/**", ".git/**", "dist/**", "build/**", "*.log", "*.lock"], + updateInterval: 60000, // 1 minute + smartSelection: true, +} + +export class RepositoryContextManager { + private context: RepositoryContext | null = null + private config: RepositoryContextConfig + private cwd: string + private rooIgnoreController?: RooIgnoreController + private updateTimer?: NodeJS.Timeout + private fileWatcher?: vscode.FileSystemWatcher + private isUpdating: boolean = false + + constructor(cwd: string, config: Partial = {}, rooIgnoreController?: RooIgnoreController) { + this.cwd = cwd + this.config = { ...DEFAULT_CONFIG, ...config } + this.rooIgnoreController = rooIgnoreController + } + + /** + * Initialize the repository context manager + */ + async initialize(): Promise { + if (!this.config.enabled) { + return + } + + // Initial context build + await this.updateContext() + + // Set up file watcher for automatic updates + this.setupFileWatcher() + + // Set up periodic updates + if (this.config.updateInterval > 0) { + this.updateTimer = setInterval(() => { + this.updateContext().catch(console.error) + }, this.config.updateInterval) + } + } + + /** + * Get the current repository context + */ + getContext(): RepositoryContext | null { + return this.context + } + + /** + * Update the repository context + */ + async updateContext(): Promise { + if (this.isUpdating) { + return // Prevent concurrent updates + } + + this.isUpdating = true + try { + const files = new Map() + const codePatterns = new Map() + + // Get all files in the repository + const allFiles = await this.getAllFiles() + + // Filter files based on configuration + const filteredFiles = await this.filterFiles(allFiles) + + // Process each file + for (const filePath of filteredFiles) { + const fileContext = await this.processFile(filePath) + if (fileContext) { + files.set(filePath, fileContext) + + // Extract code patterns if content is available + if (fileContext.content && this.config.smartSelection) { + this.extractCodePatterns(filePath, fileContext.content, codePatterns) + } + } + } + + // Generate project structure + const projectStructure = this.generateProjectStructure(files) + + // Identify relevant files based on patterns and usage + const relevantFiles = this.identifyRelevantFiles(files, codePatterns) + + this.context = { + files, + lastUpdated: Date.now(), + projectStructure, + relevantFiles, + codePatterns, + } + } finally { + this.isUpdating = false + } + } + + /** + * Get all files in the repository + */ + private async getAllFiles(): Promise { + const pattern = "**/*" + const options = { + cwd: this.cwd, + ignore: this.config.excludePatterns, + nodir: false, + dot: true, + } + + try { + const files = await glob(pattern, options) + return files.map((f) => path.relative(this.cwd, path.join(this.cwd, f))) + } catch (error) { + console.error("Error getting files:", error) + return [] + } + } + + /** + * Filter files based on configuration and .rooignore + */ + private async filterFiles(files: string[]): Promise { + let filtered = files + + // Apply .rooignore if available + if (this.rooIgnoreController) { + // filterPaths returns an array of allowed paths + filtered = this.rooIgnoreController.filterPaths(files) + } + + // Apply max files limit + if (filtered.length > this.config.maxFiles) { + // Prioritize source code files + const prioritized = this.prioritizeFiles(filtered) + filtered = prioritized.slice(0, this.config.maxFiles) + } + + return filtered + } + + /** + * Process a single file to create FileContext + */ + private async processFile(filePath: string): Promise { + const fullPath = path.join(this.cwd, filePath) + + try { + const stats = await fs.stat(fullPath) + const fileContext: FileContext = { + path: filePath, + isDirectory: stats.isDirectory(), + lastModified: stats.mtimeMs, + size: stats.size, + } + + // Include file content if configured and file is not too large + if (this.config.includeFileContent && !stats.isDirectory() && stats.size <= this.config.maxFileSize) { + try { + const content = await fs.readFile(fullPath, "utf-8") + fileContext.content = content + fileContext.hash = this.hashContent(content) + } catch (error) { + // File might be binary or unreadable + console.debug(`Could not read file ${filePath}:`, error) + } + } + + return fileContext + } catch (error) { + console.error(`Error processing file ${filePath}:`, error) + return null + } + } + + /** + * Generate a hash for file content + */ + private hashContent(content: string): string { + return crypto.createHash("sha256").update(content).digest("hex").substring(0, 8) + } + + /** + * Extract code patterns from file content + */ + private extractCodePatterns(filePath: string, content: string, patterns: Map): void { + // Extract imports/requires + const importPattern = /(?:import|require)\s*\(?['"]([^'"]+)['"]\)?/g + let match + while ((match = importPattern.exec(content)) !== null) { + const pattern = `import:${match[1]}` + if (!patterns.has(pattern)) { + patterns.set(pattern, []) + } + patterns.get(pattern)!.push(filePath) + } + + // Extract function/class definitions + const definitionPattern = /(?:function|class|interface|type|const|let|var)\s+([A-Z][A-Za-z0-9_]*)/g + while ((match = definitionPattern.exec(content)) !== null) { + const pattern = `definition:${match[1]}` + if (!patterns.has(pattern)) { + patterns.set(pattern, []) + } + patterns.get(pattern)!.push(filePath) + } + + // Extract API endpoints + const apiPattern = /(?:get|post|put|delete|patch)\s*\(\s*['"]([^'"]+)['"]/gi + while ((match = apiPattern.exec(content)) !== null) { + const pattern = `api:${match[1]}` + if (!patterns.has(pattern)) { + patterns.set(pattern, []) + } + patterns.get(pattern)!.push(filePath) + } + } + + /** + * Generate project structure representation + */ + private generateProjectStructure(files: Map): string { + const tree: any = {} + + // Build tree structure + for (const [filePath, context] of files) { + const parts = filePath.split(path.sep) + let current = tree + + for (let i = 0; i < parts.length; i++) { + const part = parts[i] + if (i === parts.length - 1) { + // Leaf node + current[part] = context.isDirectory ? {} : null + } else { + // Directory node + if (!current[part]) { + current[part] = {} + } + current = current[part] + } + } + } + + // Convert tree to string representation + return this.treeToString(tree) + } + + /** + * Convert tree structure to string + */ + private treeToString(tree: any, prefix: string = "", isLast: boolean = true): string { + let result = "" + const entries = Object.entries(tree) + + entries.forEach(([name, value], index) => { + const isLastEntry = index === entries.length - 1 + const connector = isLastEntry ? "└── " : "├── " + const extension = isLastEntry ? " " : "│ " + + result += prefix + connector + name + + if (value === null) { + // File + result += "\n" + } else if (typeof value === "object" && Object.keys(value).length > 0) { + // Directory with contents + result += "/\n" + result += this.treeToString(value, prefix + extension, isLastEntry) + } else { + // Empty directory + result += "/\n" + } + }) + + return result + } + + /** + * Prioritize files based on importance + */ + private prioritizeFiles(files: string[]): string[] { + const priorities: { [key: string]: number } = { + // Configuration files + "package.json": 1, + "tsconfig.json": 1, + ".env": 1, + config: 2, + + // Source code + ".ts": 3, + ".tsx": 3, + ".js": 3, + ".jsx": 3, + ".py": 3, + ".java": 3, + ".go": 3, + ".rs": 3, + + // Documentation + ".md": 4, + README: 2, + + // Tests + ".spec": 5, + ".test": 5, + + // Assets and others + ".css": 6, + ".scss": 6, + ".html": 6, + ".json": 7, + ".yaml": 7, + ".yml": 7, + } + + return files.sort((a, b) => { + const getPriority = (file: string): number => { + for (const [pattern, priority] of Object.entries(priorities)) { + if (file.includes(pattern)) { + return priority + } + } + return 999 + } + + return getPriority(a) - getPriority(b) + }) + } + + /** + * Identify the most relevant files for the current context + */ + private identifyRelevantFiles(files: Map, patterns: Map): string[] { + const relevance = new Map() + + // Initialize relevance scores + for (const filePath of files.keys()) { + relevance.set(filePath, 0) + } + + // Boost relevance for files with many connections + for (const [pattern, filePaths] of patterns) { + for (const filePath of filePaths) { + relevance.set(filePath, (relevance.get(filePath) || 0) + 1) + } + } + + // Boost relevance for recently modified files + const now = Date.now() + for (const [filePath, context] of files) { + if (context.lastModified) { + const age = now - context.lastModified + const dayInMs = 24 * 60 * 60 * 1000 + if (age < dayInMs) { + relevance.set(filePath, (relevance.get(filePath) || 0) + 10) + } else if (age < 7 * dayInMs) { + relevance.set(filePath, (relevance.get(filePath) || 0) + 5) + } + } + } + + // Sort by relevance and return top files + const sorted = Array.from(relevance.entries()).sort((a, b) => b[1] - a[1]) + + return sorted.slice(0, 100).map(([filePath]) => filePath) + } + + /** + * Set up file system watcher for automatic updates + */ + private setupFileWatcher(): void { + const pattern = new vscode.RelativePattern(this.cwd, "**/*") + this.fileWatcher = vscode.workspace.createFileSystemWatcher(pattern) + + // Debounce updates to avoid too frequent refreshes + let updateTimeout: NodeJS.Timeout | undefined + + const scheduleUpdate = () => { + if (updateTimeout) { + clearTimeout(updateTimeout) + } + updateTimeout = setTimeout(() => { + this.updateContext().catch(console.error) + }, 1000) // Wait 1 second after last change + } + + this.fileWatcher.onDidCreate(scheduleUpdate) + this.fileWatcher.onDidChange(scheduleUpdate) + this.fileWatcher.onDidDelete(scheduleUpdate) + } + + /** + * Format context for inclusion in environment details + */ + formatForEnvironment(): string { + if (!this.context) { + return "" + } + + let details = "\n\n# Repository Context" + details += `\nLast Updated: ${new Date(this.context.lastUpdated).toISOString()}` + details += `\nTotal Files: ${this.context.files.size}` + + if (this.context.projectStructure) { + details += "\n\n## Project Structure\n```" + details += "\n" + this.context.projectStructure + details += "\n```" + } + + if (this.context.relevantFiles.length > 0) { + details += "\n\n## Most Relevant Files" + for (const file of this.context.relevantFiles.slice(0, 20)) { + const fileContext = this.context.files.get(file) + if (fileContext) { + details += `\n- ${file}` + if (fileContext.hash) { + details += ` (hash: ${fileContext.hash})` + } + } + } + } + + if (this.config.includeFileContent && this.context.relevantFiles.length > 0) { + details += "\n\n## File Contents (Top Relevant)" + let contentCount = 0 + for (const file of this.context.relevantFiles) { + if (contentCount >= 5) break // Limit to 5 files + const fileContext = this.context.files.get(file) + if (fileContext?.content) { + details += `\n\n### ${file}\n\`\`\`\n${fileContext.content.slice(0, 500)}` + if (fileContext.content.length > 500) { + details += "\n... (truncated)" + } + details += "\n```" + contentCount++ + } + } + } + + return details + } + + /** + * Clean up resources + */ + dispose(): void { + if (this.updateTimer) { + clearInterval(this.updateTimer) + this.updateTimer = undefined + } + + if (this.fileWatcher) { + this.fileWatcher.dispose() + this.fileWatcher = undefined + } + + this.context = null + } +} diff --git a/src/core/context/__tests__/RepositoryContextManager.spec.ts b/src/core/context/__tests__/RepositoryContextManager.spec.ts new file mode 100644 index 0000000000..c3f97828ed --- /dev/null +++ b/src/core/context/__tests__/RepositoryContextManager.spec.ts @@ -0,0 +1,287 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" +import * as vscode from "vscode" +import * as fs from "fs/promises" +import * as path from "path" +import { RepositoryContextManager, RepositoryContextConfig } from "../RepositoryContextManager" +import { RooIgnoreController } from "../../ignore/RooIgnoreController" + +// Mock vscode +vi.mock("vscode", () => ({ + workspace: { + createFileSystemWatcher: vi.fn(() => ({ + onDidCreate: vi.fn(), + onDidChange: vi.fn(), + onDidDelete: vi.fn(), + dispose: vi.fn(), + })), + }, + RelativePattern: vi.fn((base, pattern) => ({ base, pattern })), +})) + +// Mock glob module +vi.mock("glob", () => ({ + glob: vi.fn(() => Promise.resolve(["file1.ts", "file2.js", "dir/file3.py"])), +})) + +// Mock fs/promises +vi.mock("fs/promises", () => ({ + default: { + stat: vi.fn(() => + Promise.resolve({ + isDirectory: () => false, + mtimeMs: Date.now(), + size: 1000, + }), + ), + readFile: vi.fn(() => Promise.resolve("file content")), + }, +})) + +describe("RepositoryContextManager", () => { + let manager: RepositoryContextManager + let mockRooIgnoreController: RooIgnoreController + const testCwd = "/test/project" + + beforeEach(() => { + // Create mock RooIgnoreController + mockRooIgnoreController = { + filterPaths: vi.fn((paths) => paths), + validateAccess: vi.fn(() => true), + dispose: vi.fn(), + } as any + + // Reset all mocks + vi.clearAllMocks() + }) + + afterEach(() => { + if (manager) { + manager.dispose() + } + }) + + describe("initialization", () => { + it("should not initialize when disabled", async () => { + const config: Partial = { + enabled: false, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + expect(manager.getContext()).toBeNull() + }) + + it("should initialize and build context when enabled", async () => { + const config: Partial = { + enabled: true, + maxFiles: 100, + includeFileContent: false, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const context = manager.getContext() + expect(context).not.toBeNull() + expect(context?.files.size).toBeGreaterThan(0) + expect(context?.lastUpdated).toBeDefined() + expect(context?.projectStructure).toBeDefined() + }) + + it("should set up file watcher when enabled", async () => { + const config: Partial = { + enabled: true, + updateInterval: 0, // Disable periodic updates for testing + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + expect(vscode.workspace.createFileSystemWatcher).toHaveBeenCalled() + }) + }) + + describe("context building", () => { + it("should respect maxFiles limit", async () => { + const config: Partial = { + enabled: true, + maxFiles: 2, + includeFileContent: false, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const context = manager.getContext() + expect(context?.files.size).toBeLessThanOrEqual(2) + }) + + it("should include file content when configured", async () => { + const config: Partial = { + enabled: true, + maxFiles: 10, + includeFileContent: true, + maxFileSize: 10000, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const context = manager.getContext() + const firstFile = context?.files.values().next().value + expect(firstFile?.content).toBeDefined() + expect(firstFile?.hash).toBeDefined() + }) + + it("should apply rooignore filtering", async () => { + const config: Partial = { + enabled: true, + maxFiles: 100, + } + + // Mock rooIgnoreController to filter out some files + mockRooIgnoreController.filterPaths = vi.fn((paths) => paths.slice(0, 2)) + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + expect(mockRooIgnoreController.filterPaths).toHaveBeenCalled() + const context = manager.getContext() + expect(context?.files.size).toBe(2) + }) + }) + + describe("formatForEnvironment", () => { + it("should format context for environment details", async () => { + const config: Partial = { + enabled: true, + maxFiles: 10, + includeFileContent: true, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const formatted = manager.formatForEnvironment() + expect(formatted).toContain("# Repository Context") + expect(formatted).toContain("Last Updated:") + expect(formatted).toContain("Total Files:") + expect(formatted).toContain("## Project Structure") + }) + + it("should return empty string when context is null", () => { + const config: Partial = { + enabled: false, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + const formatted = manager.formatForEnvironment() + expect(formatted).toBe("") + }) + + it("should include file contents when configured", async () => { + const config: Partial = { + enabled: true, + maxFiles: 10, + includeFileContent: true, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const formatted = manager.formatForEnvironment() + expect(formatted).toContain("## File Contents") + }) + }) + + describe("disposal", () => { + it("should clean up resources on dispose", async () => { + const config: Partial = { + enabled: true, + updateInterval: 1000, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + const fileWatcherDispose = vi.fn() + const mockWatcher = { + onDidCreate: vi.fn(), + onDidChange: vi.fn(), + onDidDelete: vi.fn(), + dispose: fileWatcherDispose, + } + ;(vscode.workspace.createFileSystemWatcher as any).mockReturnValue(mockWatcher) + + manager.dispose() + + expect(manager.getContext()).toBeNull() + }) + }) + + describe("code pattern extraction", () => { + it("should extract import patterns from code", async () => { + const config: Partial = { + enabled: true, + includeFileContent: true, + smartSelection: true, + } + + // Mock file content with imports + const { glob } = await import("glob") + const fs = await import("fs/promises") + ;(glob as any).mockResolvedValue(["file1.ts"]) + ;(fs.default.readFile as any).mockResolvedValue(` +import React from 'react' +import { useState } from 'react' +const Component = () => {} +`) + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const context = manager.getContext() + expect(context?.codePatterns.has("import:react")).toBe(true) + }) + + it("should identify relevant files based on patterns", async () => { + const config: Partial = { + enabled: true, + includeFileContent: true, + smartSelection: true, + } + + manager = new RepositoryContextManager(testCwd, config, mockRooIgnoreController) + await manager.initialize() + + // Wait for async operations + await new Promise((resolve) => setTimeout(resolve, 100)) + + const context = manager.getContext() + expect(context?.relevantFiles).toBeDefined() + expect(Array.isArray(context?.relevantFiles)).toBe(true) + }) + }) +}) diff --git a/src/core/environment/getEnvironmentDetails.ts b/src/core/environment/getEnvironmentDetails.ts index c0139649ab..6d92821f6a 100644 --- a/src/core/environment/getEnvironmentDetails.ts +++ b/src/core/environment/getEnvironmentDetails.ts @@ -20,6 +20,7 @@ import { formatResponse } from "../prompts/responses" import { Task } from "../task/Task" import { formatReminderSection } from "./reminder" +import { RepositoryContextManager } from "../context/RepositoryContextManager" export async function getEnvironmentDetails(cline: Task, includeFileDetails: boolean = false) { let details = "" @@ -268,6 +269,15 @@ export async function getEnvironmentDetails(cline: Task, includeFileDetails: boo } } + // Add repository context if available and enabled + const repositoryContextManager = cline.repositoryContextManager + if (repositoryContextManager) { + const repoContext = repositoryContextManager.formatForEnvironment() + if (repoContext) { + details += repoContext + } + } + const todoListEnabled = state && typeof state.apiConfiguration?.todoListEnabled === "boolean" ? state.apiConfiguration.todoListEnabled diff --git a/src/core/prompts/sections/capabilities.ts b/src/core/prompts/sections/capabilities.ts index e2d27db5bb..44b7c50d59 100644 --- a/src/core/prompts/sections/capabilities.ts +++ b/src/core/prompts/sections/capabilities.ts @@ -16,7 +16,7 @@ CAPABILITIES - You have access to tools that let you execute CLI commands on the user's computer, list files, view source code definitions, regex search${ supportsComputerUse ? ", use the browser" : "" }, read and write files, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more. -- When the user initially gives you a task, a recursive list of all filepaths in the current workspace directory ('${cwd}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current workspace directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.${ +- When the user initially gives you a task, a recursive list of all filepaths in the current workspace directory ('${cwd}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). Additionally, if repository context caching is enabled, you'll receive a comprehensive view of the project structure, relevant files, and code patterns that is automatically maintained and updated. This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current workspace directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop.${ codeIndexManager && codeIndexManager.isFeatureEnabled && codeIndexManager.isFeatureConfigured && diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index c5be865731..b81b6f7107 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -112,6 +112,7 @@ import { processUserContentMentions } from "../mentions/processUserContentMentio import { getMessagesSinceLastSummary, summarizeConversation } from "../condense" import { Gpt5Metadata, ClineMessageWithMetadata } from "./types" import { MessageQueueService } from "../message-queue/MessageQueueService" +import { RepositoryContextManager, RepositoryContextConfig } from "../context/RepositoryContextManager" import { AutoApprovalHandler } from "./AutoApprovalHandler" @@ -237,6 +238,7 @@ export class Task extends EventEmitter implements TaskLike { fileContextTracker: FileContextTracker urlContentFetcher: UrlContentFetcher terminalProcess?: RooTerminalProcess + repositoryContextManager?: RepositoryContextManager // Computer User browserSession: BrowserSession @@ -348,6 +350,21 @@ export class Task extends EventEmitter implements TaskLike { console.error("Failed to initialize RooIgnoreController:", error) }) + // Initialize repository context manager if enabled + provider.getState().then((state) => { + const repoContextConfig = state?.repositoryContext + if (repoContextConfig?.enabled) { + this.repositoryContextManager = new RepositoryContextManager( + this.cwd, + repoContextConfig, + this.rooIgnoreController, + ) + this.repositoryContextManager.initialize().catch((error) => { + console.error("Failed to initialize RepositoryContextManager:", error) + }) + } + }) + this.apiConfiguration = apiConfiguration this.api = buildApiHandler(apiConfiguration) this.autoApprovalHandler = new AutoApprovalHandler() @@ -1577,6 +1594,15 @@ export class Task extends EventEmitter implements TaskLike { console.error("Error disposing file context tracker:", error) } + try { + if (this.repositoryContextManager) { + this.repositoryContextManager.dispose() + this.repositoryContextManager = undefined + } + } catch (error) { + console.error("Error disposing repository context manager:", error) + } + try { // If we're not streaming then `abortStream` won't be called. if (this.isStreaming && this.diffViewProvider.isEditing) { diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index d08c66e36b..1ad2fce849 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -281,6 +281,7 @@ export type ExtensionState = Pick< | "remoteControlEnabled" | "openRouterImageGenerationSelectedModel" | "includeTaskHistoryInEnhance" + | "repositoryContext" > & { version: string clineMessages: ClineMessage[]