This repository was archived by the owner on May 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase-content.ts
More file actions
338 lines (279 loc) · 11.1 KB
/
Copy pathcodebase-content.ts
File metadata and controls
338 lines (279 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// ── Codebase Content Collector ────────────────────────────────────────
// Collects source file contents relevant to the current prompt and formats
// them for system prompt injection. Only injects files that match the prompt
// context — not a blind dump. Agents should NOT re-read injected files.
import { readdirSync, readFileSync, existsSync, statSync } from "node:fs";
import { join, extname, basename, dirname } from "node:path";
// ── Types ────────────────────────────────────────────────────────────
export interface CollectedFile {
path: string;
content: string;
lines: number;
truncated: boolean;
size: number;
relevance: number;
}
export interface CodebaseContent {
files: CollectedFile[];
totalChars: number;
allPaths: string[];
}
// ── Constants ────────────────────────────────────────────────────────
const MAX_LINES_PER_FILE = 500;
const MAX_FILE_SIZE = 50 * 1024; // 50KB
const RELEVANT_CHAR_BUDGET = 80_000; // ~23K tokens for relevant files only
const CONFIG_BUDGET = 10_000; // separate budget for config files
const INCLUDE_EXTENSIONS = new Set([
".ts", ".tsx", ".js", ".jsx",
".py", ".go", ".rs", ".java", ".rb",
".css", ".scss", ".html",
".json", ".yaml", ".yml", ".toml",
".md",
]);
const IGNORE_DIRS = new Set([
"node_modules", ".git", "dist", "build", ".next", ".nuxt",
"__pycache__", ".venv", "venv", "target", "vendor",
".DS_Store", ".cache", ".turbo", ".parcel-cache",
"coverage", ".nyc_output", ".svelte-kit", ".output",
".vercel", ".netlify", "out", ".expo",
]);
const IGNORE_PATTERNS = [
/\.lock$/,
/\.map$/,
/\.min\./,
/\.d\.ts$/,
/\.d\.mts$/,
/\.d\.cts$/,
];
const CONFIG_FILES = new Set([
"package.json", "tsconfig.json", "tsconfig.base.json",
"biome.json", "biome.jsonc",
".eslintrc.json", ".eslintrc.js", ".eslintrc.cjs",
".prettierrc", ".prettierrc.json",
"vite.config.ts", "vite.config.js",
"next.config.js", "next.config.mjs", "next.config.ts",
"tailwind.config.ts", "tailwind.config.js",
"drizzle.config.ts",
"vitest.config.ts",
"deno.json",
]);
const ALLOWED_MD_NAMES = new Set([
"readme.md", "claude.md", "agents.md", "conventions.md", "contributing.md",
]);
// Words too common to be meaningful for relevance
const STOP_WORDS = new Set([
"the", "a", "an", "is", "are", "was", "were", "be", "been",
"do", "does", "did", "will", "would", "could", "should",
"have", "has", "had", "not", "but", "and", "or", "if", "then",
"this", "that", "it", "its", "in", "on", "at", "to", "for",
"of", "with", "from", "by", "as", "so", "no", "yes",
"make", "use", "add", "get", "set", "new", "all", "my",
"please", "want", "need", "like", "just", "also",
"만들어", "해줘", "수정", "변경", "추가", "삭제", "확인",
"이", "그", "저", "것", "들", "를", "을", "에", "의", "로",
]);
// ── Collector ────────────────────────────────────────────────────────
export class CodebaseContentCollector {
private projectDir: string;
private cachedPaths: string[] | null = null;
private cachedGitHead: string | null = null;
constructor(projectDir: string) {
this.projectDir = projectDir;
}
/** Collect files relevant to the given prompt. Config files always included. */
collect(prompt?: string): CodebaseContent {
const currentHead = this.readGitHead();
// Cache the file list (not content) — invalidate on git HEAD change
if (!this.cachedPaths || this.cachedGitHead !== currentHead) {
this.cachedPaths = this.walkFiles(this.projectDir, "", 0);
this.cachedGitHead = currentHead;
}
const allPaths = this.cachedPaths;
const keywords = prompt ? this.extractKeywords(prompt) : [];
// Score each file by relevance to the prompt
const scored: Array<{ path: string; score: number; isConfig: boolean }> = [];
for (const filePath of allPaths) {
const name = basename(filePath);
const isConfig = CONFIG_FILES.has(name);
const score = isConfig ? 100 : this.scoreRelevance(filePath, keywords);
scored.push({ path: filePath, score, isConfig });
}
// Separate config files from content files
const configFiles = scored.filter(f => f.isConfig);
const contentFiles = scored.filter(f => !f.isConfig && f.score > 0);
// Sort content files by relevance (highest first)
contentFiles.sort((a, b) => b.score - a.score);
// Collect config files first (separate budget)
const files: CollectedFile[] = [];
let configChars = 0;
for (const { path: filePath } of configFiles) {
const collected = this.readFile(filePath, 100);
if (!collected) continue;
if (configChars + collected.content.length > CONFIG_BUDGET) continue;
files.push(collected);
configChars += collected.content.length;
}
// Collect relevant content files
let contentChars = 0;
for (const { path: filePath, score } of contentFiles) {
const collected = this.readFile(filePath, score);
if (!collected) continue;
if (contentChars + collected.content.length > RELEVANT_CHAR_BUDGET) break;
files.push(collected);
contentChars += collected.content.length;
}
return {
files,
totalChars: configChars + contentChars,
allPaths,
};
}
formatForPrompt(content: CodebaseContent): string {
if (content.files.length === 0 && content.allPaths.length === 0) return "";
const parts: string[] = [];
if (content.files.length > 0) {
parts.push(`[CODEBASE CONTENT — ${content.files.length} files injected]`);
parts.push("IMPORTANT: These files are already in your context. Do NOT use Read, Bash, or Glob to re-read them.");
parts.push("Only use tools for files NOT listed here.\n");
for (const file of content.files) {
parts.push(`=== ${file.path} ===`);
parts.push(file.content);
}
}
// List remaining paths (no content) so agent knows what exists
const injectedPaths = new Set(content.files.map(f => f.path));
const otherPaths = content.allPaths.filter(p => !injectedPaths.has(p));
if (otherPaths.length > 0) {
parts.push(`\n[OTHER FILES — ${otherPaths.length} files available via Read tool]`);
parts.push(otherPaths.join("\n"));
}
parts.push("[END CODEBASE CONTENT]");
return parts.join("\n");
}
invalidateCache(): void {
this.cachedPaths = null;
this.cachedGitHead = null;
}
// ── Private helpers ──────────────────────────────────────────────
private readFile(filePath: string, relevance: number): CollectedFile | null {
const absPath = join(this.projectDir, filePath);
let stat;
try {
stat = statSync(absPath);
} catch {
return null;
}
if (stat.size > MAX_FILE_SIZE) return null;
let raw: string;
try {
raw = readFileSync(absPath, "utf-8");
} catch {
return null;
}
const lines = raw.split("\n");
let content: string;
let truncated = false;
if (lines.length > MAX_LINES_PER_FILE) {
content = lines.slice(0, MAX_LINES_PER_FILE).join("\n") + `\n... truncated (${lines.length} lines total)`;
truncated = true;
} else {
content = raw;
}
return { path: filePath, content, lines: lines.length, truncated, size: stat.size, relevance };
}
/** Extract meaningful keywords from a prompt for file relevance scoring. */
private extractKeywords(prompt: string): string[] {
// Split on whitespace, punctuation, camelCase, and path separators
const raw = prompt
.replace(/([a-z])([A-Z])/g, "$1 $2") // camelCase split
.replace(/[^\w가-힣\s/.-]/g, " ")
.toLowerCase()
.split(/[\s/]+/)
.filter(w => w.length > 1);
const unique = new Set<string>();
for (const w of raw) {
if (!STOP_WORDS.has(w)) unique.add(w);
}
// Also extract file paths or component names mentioned directly
const pathMatches = prompt.match(/[\w\-./]+\.[\w]+/g) ?? [];
for (const p of pathMatches) {
unique.add(p.toLowerCase());
// Also add the stem without extension
const stem = basename(p, extname(p)).toLowerCase();
if (stem.length > 1) unique.add(stem);
}
return [...unique];
}
/** Score a file path's relevance to the keywords. 0 = not relevant. */
private scoreRelevance(filePath: string, keywords: string[]): number {
if (keywords.length === 0) {
// No prompt: return base score by depth (shallow = more relevant)
const depth = filePath.split("/").length;
return Math.max(1, 10 - depth);
}
const lower = filePath.toLowerCase();
const name = basename(lower, extname(lower));
const dir = dirname(lower);
let score = 0;
for (const kw of keywords) {
// Exact filename match (strongest signal)
if (name === kw) { score += 50; continue; }
// Filename contains keyword
if (name.includes(kw)) { score += 30; continue; }
// Directory path contains keyword
if (dir.includes(kw)) { score += 20; continue; }
// Full path contains keyword
if (lower.includes(kw)) { score += 10; continue; }
}
return score;
}
private walkFiles(dir: string, relPath: string, depth: number): string[] {
if (depth > 8) return [];
let entries;
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
return [];
}
const results: string[] = [];
for (const entry of entries) {
if (entry.name.startsWith(".") && entry.name !== ".claude") continue;
if (IGNORE_DIRS.has(entry.name)) continue;
const entryRel = relPath ? `${relPath}/${entry.name}` : entry.name;
if (entry.isDirectory() && !entry.isSymbolicLink()) {
results.push(...this.walkFiles(join(dir, entry.name), entryRel, depth + 1));
} else if (entry.isFile()) {
if (this.shouldInclude(entry.name)) {
results.push(entryRel);
}
}
}
return results;
}
private shouldInclude(fileName: string): boolean {
const ext = extname(fileName).toLowerCase();
if (!INCLUDE_EXTENSIONS.has(ext)) return false;
for (const pattern of IGNORE_PATTERNS) {
if (pattern.test(fileName)) return false;
}
if (ext === ".md") {
return ALLOWED_MD_NAMES.has(fileName.toLowerCase());
}
return true;
}
private readGitHead(): string {
try {
const headPath = join(this.projectDir, ".git", "HEAD");
const content = readFileSync(headPath, "utf-8").trim();
if (content.startsWith("ref: ")) {
const refPath = join(this.projectDir, ".git", content.slice(5));
if (existsSync(refPath)) {
return readFileSync(refPath, "utf-8").trim().slice(0, 12);
}
}
return content.slice(0, 12);
} catch {
return "no-git";
}
}
}