-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
331 lines (277 loc) · 11.3 KB
/
context.ts
File metadata and controls
331 lines (277 loc) · 11.3 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
import * as fs from 'fs';
import * as path from 'path';
import { CCSIndex, ContextOutput, ContextBlock, FileSection, SearchResult } from './types';
import { searchIndex, expandWithDependencies } from './searcher';
import { MAX_CONTEXT_FILES, MAX_CONTEXT_LINES, MAX_SECTION_LINES } from './constants';
// ── Context Builder ─────────────────────────────────────────────────────────
// The core of the engine. Takes a query + index, returns the minimal context
// Claude needs to answer accurately. Zero exploration, zero wasted tokens.
export function buildContext(
query: string,
index: CCSIndex,
options: { maxFiles?: number; maxLines?: number } = {}
): ContextOutput {
const maxFiles = options.maxFiles ?? MAX_CONTEXT_FILES;
const maxLines = options.maxLines ?? MAX_CONTEXT_LINES;
// 1. Search the index for matching files
let results = searchIndex(query, index);
// 2. Expand with dependency context
results = expandWithDependencies(results, index);
// 3. Take top results (within limits)
const topResults = results.slice(0, maxFiles);
// 4. For each result, extract the relevant sections
const blocks: ContextBlock[] = [];
let totalLines = 0;
for (const result of topResults) {
if (totalLines >= maxLines) break;
const remaining = maxLines - totalLines;
const block = buildBlock(result, index, query, remaining);
if (block && block.sections.length > 0) {
blocks.push(block);
totalLines += block.sections.reduce((sum, s) => sum + (s.endLine - s.startLine + 1), 0);
}
}
return {
query,
files: blocks,
conventions: index.conventions,
totalLines,
totalFiles: blocks.length,
};
}
// ── Block Builder ───────────────────────────────────────────────────────────
// For a single search result, extract the relevant code sections.
function buildBlock(
result: SearchResult,
index: CCSIndex,
query: string,
maxLines: number
): ContextBlock | null {
const absPath = path.join(index.root, result.file);
if (!fs.existsSync(absPath)) return null;
let content: string;
try {
content = fs.readFileSync(absPath, 'utf8');
} catch {
return null;
}
const allLines = content.split('\n');
const sections: FileSection[] = [];
let linesUsed = 0;
// Strategy depends on match type
if (result.matchType === 'symbol') {
// Extract the matched symbol's definition
const symbolName = extractSymbolFromDetail(result.matchDetail);
if (symbolName) {
const symbolSections = extractSymbolContext(allLines, symbolName, result.node, maxLines);
for (const section of symbolSections) {
if (linesUsed + (section.endLine - section.startLine + 1) > maxLines) break;
sections.push(section);
linesUsed += section.endLine - section.startLine + 1;
}
}
}
if (sections.length === 0) {
// Fallback: extract the most important sections
// Start with imports (first 5 lines) + exports + key symbols
const importSection = extractSection(allLines, 1, Math.min(5, allLines.length), 'imports');
if (importSection) {
sections.push(importSection);
linesUsed += importSection.endLine - importSection.startLine + 1;
}
// Add exported symbol definitions
for (const sym of result.node.symbols.filter(s => s.exported)) {
if (linesUsed >= maxLines) break;
const endLine = sym.endLine ?? Math.min(sym.line + 15, allLines.length);
const sectionLines = endLine - sym.line + 1;
if (linesUsed + sectionLines > maxLines) continue;
const section = extractSection(
allLines,
sym.line,
endLine,
`${sym.type} ${sym.name}`
);
if (section) {
sections.push(section);
linesUsed += sectionLines;
}
}
}
// If still no sections, take the first N lines (header)
if (sections.length === 0) {
const headerLines = Math.min(MAX_SECTION_LINES, maxLines, allLines.length);
const section = extractSection(allLines, 1, headerLines, 'file header');
if (section) sections.push(section);
}
return {
file: result.file,
sections,
rank: result.node.rank,
importerCount: result.node.importedBy.length,
role: result.node.role,
};
}
// ── Section Extraction ──────────────────────────────────────────────────────
function extractSection(
lines: string[],
startLine: number,
endLine: number,
reason: string
): FileSection | null {
const start = Math.max(1, startLine);
const end = Math.min(lines.length, endLine);
if (start > end) return null;
const content = lines.slice(start - 1, end).join('\n');
return { startLine: start, endLine: end, content, reason };
}
function extractSymbolContext(
lines: string[],
symbolName: string,
node: any,
maxLines: number
): FileSection[] {
const sections: FileSection[] = [];
// Find the symbol in the node's symbol list
const symbols = node.symbols?.filter((s: any) =>
s.name.toLowerCase() === symbolName.toLowerCase()
) ?? [];
if (symbols.length === 0) {
// Fall back to searching line content
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes(symbolName)) {
const start = Math.max(1, i + 1 - 2); // 2 lines before
const end = Math.min(lines.length, i + 1 + MAX_SECTION_LINES);
sections.push(extractSection(lines, start, end, `contains "${symbolName}"`)!);
break;
}
}
return sections.filter(Boolean);
}
let linesUsed = 0;
for (const sym of symbols) {
if (linesUsed >= maxLines) break;
// Determine end of symbol (use endLine if available, else scan for block end)
let endLine = sym.endLine;
if (!endLine) {
endLine = findBlockEnd(lines, sym.line - 1);
}
endLine = Math.min(endLine, sym.line + MAX_SECTION_LINES - 1);
const sectionLen = endLine - sym.line + 1;
if (linesUsed + sectionLen > maxLines) continue;
const section = extractSection(lines, sym.line, endLine, `${sym.type} ${sym.name}`);
if (section) {
sections.push(section);
linesUsed += sectionLen;
}
}
return sections;
}
/**
* Find the end of a code block starting at a given line.
* Tracks brace/indent depth to find the closing brace.
*/
function findBlockEnd(lines: string[], startIdx: number): number {
let depth = 0;
let foundOpen = false;
for (let i = startIdx; i < lines.length && i < startIdx + MAX_SECTION_LINES; i++) {
const line = lines[i];
for (const ch of line) {
if (ch === '{' || ch === '(') { depth++; foundOpen = true; }
if (ch === '}' || ch === ')') depth--;
}
if (foundOpen && depth <= 0) return i + 1;
}
// For Python (indent-based), find where indentation returns to start level
if (!foundOpen && startIdx < lines.length) {
const startIndent = lines[startIdx].length - lines[startIdx].trimStart().length;
for (let i = startIdx + 1; i < lines.length && i < startIdx + MAX_SECTION_LINES; i++) {
const line = lines[i];
if (line.trim() === '') continue;
const indent = line.length - line.trimStart().length;
if (indent <= startIndent && i > startIdx + 1) return i;
}
}
return Math.min(startIdx + MAX_SECTION_LINES, lines.length);
}
function extractSymbolFromDetail(detail: string): string | null {
const match = detail.match(/Symbol "(\w+)"/i);
return match ? match[1] : null;
}
// ── Format Context Output ───────────────────────────────────────────────────
// Formats the context as a compact string for injection into Claude's prompt.
export function formatContext(context: ContextOutput): string {
const parts: string[] = [];
parts.push(`## Context for: ${context.query}`);
parts.push(`> ${context.totalFiles} files, ${context.totalLines} lines of relevant code`);
parts.push('');
for (const block of context.files) {
const meta = [
`Rank: ${block.rank}`,
`${block.importerCount} importers`,
`Role: ${block.role}`,
].join(', ');
parts.push(`### ${block.file} [${meta}]`);
for (const section of block.sections) {
parts.push(`Lines ${section.startLine}-${section.endLine}: ${section.reason}`);
parts.push('```');
// Add line numbers to each line
const sectionLines = section.content.split('\n');
for (let i = 0; i < sectionLines.length; i++) {
parts.push(`${section.startLine + i}| ${sectionLines[i]}`);
}
parts.push('```');
parts.push('');
}
}
// Add conventions summary (compact)
if (context.conventions) {
parts.push('## Conventions');
parts.push(`- Naming: files=${context.conventions.naming.files}, vars=${context.conventions.naming.variables}, classes=${context.conventions.naming.classes}`);
parts.push(`- Architecture: ${context.conventions.architecture}`);
if (context.conventions.testing.framework !== 'unknown') {
parts.push(`- Testing: ${context.conventions.testing.framework} (${context.conventions.testing.pattern})`);
}
parts.push('');
}
return parts.join('\n');
}
// ── Project Summary ─────────────────────────────────────────────────────────
// Generates a compact project summary for CLAUDE.md / SessionStart injection.
// Designed to be stable across sessions → maximizes prompt cache hits.
export function generateSummary(index: CCSIndex): string {
const parts: string[] = [];
parts.push('## Codebase Summary (auto-generated by CCS engine)');
parts.push('');
parts.push(`- **Files:** ${index.stats.totalFiles} | **Lines:** ${index.stats.totalLines.toLocaleString()} | **Dirs:** ${index.stats.totalDirs}`);
if (index.stats.techStack.length > 0) {
parts.push(`- **Stack:** ${index.stats.techStack.join(', ')}`);
}
if (index.stats.languages.length > 0) {
const topLangs = index.stats.languages.slice(0, 5)
.map(l => `${l.language} (${l.percentage}%)`)
.join(', ');
parts.push(`- **Languages:** ${topLangs}`);
}
parts.push(`- **Architecture:** ${index.conventions.architecture}`);
if (index.stats.entryPoints.length > 0) {
parts.push(`- **Entry points:** ${index.stats.entryPoints.join(', ')}`);
}
// Top 10 most important files (S and A rank)
const topFiles = Object.values(index.files)
.filter(f => f.rank === 'S' || f.rank === 'A')
.sort((a, b) => b.centrality - a.centrality)
.slice(0, 10);
if (topFiles.length > 0) {
parts.push('');
parts.push('### Key Files');
parts.push('| File | Rank | Role | Importers | Exports |');
parts.push('|------|------|------|-----------|---------|');
for (const f of topFiles) {
parts.push(`| ${f.path} | ${f.rank} | ${f.role} | ${f.importedBy.length} | ${f.exports.slice(0, 3).join(', ')}${f.exports.length > 3 ? '...' : ''} |`);
}
}
parts.push('');
parts.push('> Use `ccs context "<query>"` to get precise context for any task.');
parts.push(`> Index: .ccs/index.json (${new Date(index.timestamp).toLocaleDateString()})`);
return parts.join('\n');
}