Skip to content

Commit b1058e5

Browse files
committed
feat: Prioritize frequently-changed files for AI context
Based on git history analysis, always include priority files first: - index.js (84 changes in 6 months) - ai-handler.js, discord.js, spotify-async.js - Admin files, help templates, etc. Then include remaining files up to 600KB limit (~150K tokens). This ensures most relevant code is always available to AI.
1 parent da04554 commit b1058e5

1 file changed

Lines changed: 54 additions & 9 deletions

File tree

.github/agent/agent.mjs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,27 @@ try {
171171
}
172172

173173
// Include all code files while excluding docs, images, and large files
174-
// This provides complete context while staying under 200K token limit
174+
// Prioritize frequently-changed files to stay under 200K token limit
175175
let codebaseContent = "";
176176
const fileList = files.split("\n").filter(f => f.trim());
177177

178+
// Most frequently modified files (from git history analysis)
179+
// These are always included first
180+
const priorityFiles = [
181+
'index.js',
182+
'ai-handler.js',
183+
'discord.js',
184+
'spotify-async.js',
185+
'soundcraft-handler.js',
186+
'slack.js',
187+
'lib/spotify-validator.js',
188+
'lib/webauthn-handler.js',
189+
'public/setup/admin.js',
190+
'public/setup/admin.html',
191+
'templates/help/helpTextAdmin.txt',
192+
'templates/help/helpText.txt',
193+
];
194+
178195
// Include code files, exclude non-code
179196
const includedExtensions = ['.js', '.mjs', '.json', '.txt', '.html', '.css', '.yml', '.yaml'];
180197
const excludedPaths = [
@@ -184,18 +201,40 @@ const excludedPaths = [
184201
'coverage/',
185202
'dist/',
186203
'build/',
187-
'docs/', // Exclude documentation
188-
'README.md', // Exclude large README
189-
'CHANGELOG.md', // Exclude changelog
190-
'.github/workflows/', // Exclude workflow files (too verbose)
191-
'test/', // Exclude tests
204+
'docs/',
205+
'README.md',
206+
'CHANGELOG.md',
207+
'.github/workflows/',
208+
'test/',
209+
'build.txt',
192210
];
193211

194212
console.log("[AGENT] Loading codebase files...");
195213
let filesIncluded = 0;
196214
let totalSize = 0;
215+
const includedFiles = new Set();
216+
217+
// First pass: Include priority files
218+
for (const filePath of priorityFiles) {
219+
if (!fileList.includes(filePath)) continue;
220+
221+
try {
222+
const stats = fs.statSync(filePath);
223+
const content = fs.readFileSync(filePath, "utf8");
224+
codebaseContent += `\n\n=== ${filePath} ===\n${content}`;
225+
filesIncluded++;
226+
totalSize += stats.size;
227+
includedFiles.add(filePath);
228+
console.log(`[AGENT] Priority: ${filePath} (${Math.round(stats.size / 1024)} KB)`);
229+
} catch (e) {
230+
// Skip if can't read
231+
}
232+
}
197233

234+
// Second pass: Include other code files (skip if exceeds limit)
198235
for (const filePath of fileList) {
236+
if (includedFiles.has(filePath)) continue;
237+
199238
// Skip excluded paths
200239
if (excludedPaths.some(excluded => filePath.includes(excluded))) {
201240
continue;
@@ -209,12 +248,18 @@ for (const filePath of fileList) {
209248
try {
210249
const stats = fs.statSync(filePath);
211250

212-
// Skip very large files (>80KB), except index.js which is critical
213-
if (stats.size > 80000 && filePath !== 'index.js') {
251+
// Skip very large files
252+
if (stats.size > 80000) {
214253
console.log(`[AGENT] Skipping large file: ${filePath} (${stats.size} bytes)`);
215254
continue;
216255
}
217256

257+
// Stop if we're approaching token limit (~600KB ≈ 150K tokens)
258+
if (totalSize > 600000) {
259+
console.log(`[AGENT] Reached size limit, skipping remaining files`);
260+
break;
261+
}
262+
218263
const content = fs.readFileSync(filePath, "utf8");
219264
codebaseContent += `\n\n=== ${filePath} ===\n${content}`;
220265
filesIncluded++;
@@ -224,7 +269,7 @@ for (const filePath of fileList) {
224269
}
225270
}
226271

227-
console.log(`[AGENT] Included ${filesIncluded} code files (${Math.round(totalSize / 1024)} KB)`);
272+
console.log(`[AGENT] Included ${filesIncluded} code files (${Math.round(totalSize / 1024)} KB total)`);
228273

229274
// Build specialized prompt for SlackONOS
230275
const prompt = `You are an autonomous coding agent for SlackONOS, a democratic music bot for Discord and Slack that controls Sonos speakers.

0 commit comments

Comments
 (0)