Skip to content

Commit ec1ab78

Browse files
committed
feat: Include complete codebase content for AI agent
- Changed from keyword-based file selection to full codebase - Reads all source files (JS, JSON, HTML, CSS, YAML, MD, TXT) - Excludes node_modules, large files, and build artifacts - Increased max_tokens from 4096 to 8192 for larger context - AI now has complete accurate view of all files This ensures the AI always generates diffs with correct line numbers, eliminating patch application failures.
1 parent ea426cc commit ec1ab78

1 file changed

Lines changed: 40 additions & 31 deletions

File tree

.github/agent/agent.mjs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -170,37 +170,48 @@ try {
170170
console.log("[AGENT] No .cursorrules file found");
171171
}
172172

173-
// Try to identify and read relevant files based on the task
174-
// This helps the AI generate accurate diffs with correct line numbers
175-
let relevantFiles = "";
176-
const taskLower = task.toLowerCase();
177-
178-
// Common file patterns to check based on task keywords
179-
const filePatterns = [
180-
{ keywords: ["help", "admin", "command"], files: ["templates/help/helpTextAdmin.txt", "templates/help/helpText.txt"] },
181-
{ keywords: ["config", "setting"], files: ["config/config.json", "src/config-handler.js"] },
182-
{ keywords: ["slack", "message", "notification"], files: ["src/slack-handler.js", "src/notification-handler.js"] },
183-
{ keywords: ["discord"], files: ["src/discord-handler.js"] },
184-
{ keywords: ["sonos", "speaker", "playback"], files: ["src/sonos-handler.js"] },
185-
{ keywords: ["route", "endpoint", "web", "admin panel"], files: ["src/webserver.js", "public/admin.html"] },
186-
{ keywords: ["queue", "track"], files: ["src/queue-handler.js"] },
187-
{ keywords: ["vote", "voting"], files: ["src/voting-handler.js"] },
188-
];
173+
// Read all relevant source files for complete context
174+
// This ensures the AI has accurate file content for generating diffs
175+
let codebaseContent = "";
176+
const fileList = files.split("\n").filter(f => f.trim());
177+
178+
// Define which files to include (exclude large binaries, dependencies, etc.)
179+
const includedExtensions = ['.js', '.mjs', '.json', '.txt', '.html', '.css', '.md', '.yml', '.yaml'];
180+
const excludedPaths = ['node_modules/', 'package-lock.json', '.git/', 'coverage/', 'dist/', 'build/'];
181+
182+
console.log("[AGENT] Reading codebase files...");
183+
let filesIncluded = 0;
184+
185+
for (const filePath of fileList) {
186+
// Skip if file is in excluded paths
187+
if (excludedPaths.some(excluded => filePath.includes(excluded))) {
188+
continue;
189+
}
189190

190-
for (const pattern of filePatterns) {
191-
if (pattern.keywords.some(keyword => taskLower.includes(keyword))) {
192-
for (const filePath of pattern.files) {
193-
try {
194-
const content = fs.readFileSync(filePath, "utf8");
195-
relevantFiles += `\n\n=== ${filePath} ===\n${content}`;
196-
console.log(`[AGENT] Including content of ${filePath} for context`);
197-
} catch (e) {
198-
// File doesn't exist, skip it
199-
}
191+
// Skip if file extension is not in allowed list
192+
if (!includedExtensions.some(ext => filePath.endsWith(ext))) {
193+
continue;
194+
}
195+
196+
try {
197+
const stats = fs.statSync(filePath);
198+
// Skip files larger than 100KB to avoid token limits
199+
if (stats.size > 100000) {
200+
console.log(`[AGENT] Skipping large file: ${filePath} (${stats.size} bytes)`);
201+
continue;
200202
}
203+
204+
const content = fs.readFileSync(filePath, "utf8");
205+
codebaseContent += `\n\n=== ${filePath} ===\n${content}`;
206+
filesIncluded++;
207+
} catch (e) {
208+
// File read error, skip it
209+
console.log(`[AGENT] Could not read ${filePath}: ${e.message}`);
201210
}
202211
}
203212

213+
console.log(`[AGENT] Included ${filesIncluded} files for AI context`);
214+
204215
// Build specialized prompt for SlackONOS
205216
const prompt = `You are an autonomous coding agent for SlackONOS, a democratic music bot for Discord and Slack that controls Sonos speakers.
206217
@@ -219,13 +230,11 @@ CODEBASE CONTEXT:
219230
Project Rules and Conventions:
220231
${cursorRules}
221232
222-
Repository Files:
223-
${files}
224-
225233
Recent Commits:
226234
${recentCommits}
227235
228-
${relevantFiles ? `RELEVANT FILE CONTENTS (use these for accurate line numbers):\n${relevantFiles}` : ''}
236+
COMPLETE CODEBASE CONTENT (use these exact contents for accurate diffs):
237+
${codebaseContent}
229238
230239
TASK FROM ADMIN (${requester}):
231240
${task}
@@ -255,7 +264,7 @@ async function callAI(promptText) {
255264
if (provider === "claude") {
256265
const response = await aiClient.messages.create({
257266
model: aiModel,
258-
max_tokens: 4096,
267+
max_tokens: 8192,
259268
temperature: 0.2,
260269
messages: [{ role: "user", content: promptText }],
261270
});

0 commit comments

Comments
 (0)