-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathhandlers.js
More file actions
460 lines (398 loc) · 15.6 KB
/
Copy pathhandlers.js
File metadata and controls
460 lines (398 loc) · 15.6 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
'use strict';
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const CONTEXT_FILE = path.join('.github', 'copilot-instructions.md');
// Section header keywords in PROJECT_MAP.md
const MAP_SECTIONS = {
imports: '### Import graph',
classes: '### Class hierarchy',
routes: '### Route table',
};
/**
* read_context({ module? }) → string
*
* Returns the full context file, or just the sections whose file paths
* contain the given module substring.
*/
function readContext(args, cwd) {
const contextPath = path.join(cwd, CONTEXT_FILE);
if (!fs.existsSync(contextPath)) {
return 'No context file found. Run: node gen-context.js';
}
const content = fs.readFileSync(contextPath, 'utf8');
if (!args || !args.module) return content;
const mod = args.module.replace(/\\/g, '/').replace(/\/$/, '');
const lines = content.split('\n');
const result = [];
let capturing = false;
for (const line of lines) {
if (line.startsWith('### ')) {
const filePath = line.slice(4).trim().replace(/\\/g, '/');
// Match if file path starts with mod or contains /mod/ or /mod
capturing =
filePath === mod ||
filePath.startsWith(mod + '/') ||
filePath.includes('/' + mod + '/') ||
filePath.includes('/' + mod);
if (capturing) result.push(line);
continue;
}
if (capturing) result.push(line);
}
if (result.length === 0) return `No signatures found for module: ${mod}`;
return result.join('\n');
}
/**
* search_signatures({ query }) → string
*
* Case-insensitive search through all signature lines.
* Returns matching lines grouped by file path.
*/
function searchSignatures(args, cwd) {
if (!args || !args.query) return 'Missing required argument: query';
const contextPath = path.join(cwd, CONTEXT_FILE);
if (!fs.existsSync(contextPath)) {
return 'No context file found. Run: node gen-context.js';
}
const content = fs.readFileSync(contextPath, 'utf8');
const query = args.query.toLowerCase();
const lines = content.split('\n');
const result = [];
let currentFile = '';
let fileHeaderAdded = false;
for (const line of lines) {
if (line.startsWith('### ')) {
currentFile = line.slice(4).trim();
fileHeaderAdded = false;
continue;
}
// Skip markdown fences and top-level headers
if (line.startsWith('```') || line.startsWith('## ') || line.startsWith('# ') || line.startsWith('<!--')) {
continue;
}
if (line.toLowerCase().includes(query)) {
if (currentFile && !fileHeaderAdded) {
if (result.length > 0) result.push('');
result.push(`### ${currentFile}`);
fileHeaderAdded = true;
}
result.push(line);
}
}
if (result.length === 0) return `No signatures found matching: ${args.query}`;
return result.join('\n');
}
/**
* get_map({ type }) → string
*
* Returns a section from PROJECT_MAP.md.
* type: 'imports' | 'classes' | 'routes'
*/
function getMap(args, cwd) {
if (!args || !args.type) return 'Missing required argument: type';
const header = MAP_SECTIONS[args.type];
if (!header) {
return `Unknown map type: "${args.type}". Use: imports, classes, routes`;
}
const mapPath = path.join(cwd, 'PROJECT_MAP.md');
if (!fs.existsSync(mapPath)) {
return 'PROJECT_MAP.md not found. Run: node gen-project-map.js';
}
const content = fs.readFileSync(mapPath, 'utf8');
const idx = content.indexOf(header);
if (idx === -1) {
return `Section "${header}" not found in PROJECT_MAP.md`;
}
// Extract from this header to the next ### header
const after = content.slice(idx);
const nextMatch = after.slice(header.length).search(/\n###\s/);
return nextMatch === -1 ? after : after.slice(0, header.length + nextMatch);
}
/**
* create_checkpoint({ note? }) → string
*
* Returns a markdown checkpoint summarising current project state:
* - Timestamp and optional user note
* - Active git branch + last 5 commit messages
* - Token count of current context file
* - List of modules present in the context
* - Route count (if PROJECT_MAP.md exists)
*/
function createCheckpoint(args, cwd) {
const note = (args && args.note) ? args.note.trim() : '';
const now = new Date().toISOString().replace('T', ' ').slice(0, 19) + ' UTC';
const lines = [
'# SigMap Checkpoint',
`**Created:** ${now}`,
];
if (note) lines.push(`**Note:** ${note}`);
lines.push('');
// ── Git info ────────────────────────────────────────────────────────────
lines.push('## Git state');
try {
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
cwd, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'],
}).trim();
lines.push(`**Branch:** ${branch}`);
} catch (_) {
lines.push('**Branch:** (not a git repo)');
}
try {
const log = execSync(
'git log --oneline -5 --no-decorate 2>/dev/null',
{ cwd, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }
).trim();
if (log) {
lines.push('');
lines.push('**Recent commits:**');
for (const l of log.split('\n')) lines.push(`- ${l}`);
}
} catch (_) {} // ignore — not every project uses git
lines.push('');
// ── Context stats ────────────────────────────────────────────────────────
lines.push('## Context snapshot');
const contextPath = path.join(cwd, CONTEXT_FILE);
if (fs.existsSync(contextPath)) {
const content = fs.readFileSync(contextPath, 'utf8');
const tokens = Math.ceil(content.length / 4);
// Count modules (### headers are file paths)
const modules = content.split('\n').filter((l) => l.startsWith('### ')).map((l) => l.slice(4).trim());
lines.push(`**Token count:** ~${tokens}`);
lines.push(`**Modules in context:** ${modules.length}`);
if (modules.length > 0) {
lines.push('');
lines.push('**Modules:**');
for (const m of modules.slice(0, 20)) lines.push(`- ${m}`);
if (modules.length > 20) lines.push(`- … and ${modules.length - 20} more`);
}
} else {
lines.push('_No context file found. Run: node gen-context.js_');
}
lines.push('');
// ── Route summary ────────────────────────────────────────────────────────
const mapPath = path.join(cwd, 'PROJECT_MAP.md');
if (fs.existsSync(mapPath)) {
const mapContent = fs.readFileSync(mapPath, 'utf8');
const routeLines = mapContent.split('\n').filter((l) => l.startsWith('| ') && !l.startsWith('| Method') && !l.startsWith('|---'));
if (routeLines.length > 0) {
lines.push('## Routes');
lines.push(`**Total routes detected:** ${routeLines.length}`);
lines.push('');
for (const r of routeLines.slice(0, 10)) lines.push(r);
if (routeLines.length > 10) lines.push(`| … | +${routeLines.length - 10} more | |`);
lines.push('');
}
}
lines.push('---');
lines.push('_Generated by SigMap `create_checkpoint`_');
return lines.join('\n');
}
/**
* get_routing({}) → string
*
* Reads the current context file, classifies all indexed files by complexity,
* and returns a formatted markdown routing guide showing which files belong
* to the fast/balanced/powerful model tier.
*/
function getRouting(args, cwd) {
const contextPath = path.join(cwd, CONTEXT_FILE);
if (!fs.existsSync(contextPath)) {
return (
'_No context file found. Run `node gen-context.js --routing` first._\n\n' +
'This generates routing hints that map each file to a model tier:\n' +
'- **fast** (haiku/gpt-4o-mini) — config, markup, trivial utilities\n' +
'- **balanced** (sonnet/gpt-4o) — standard application code\n' +
'- **powerful** (opus/gpt-4-turbo) — complex, security-critical, or large modules'
);
}
// Parse file list from context (### headings are file paths)
const content = fs.readFileSync(contextPath, 'utf8');
const fileRels = content.split('\n')
.filter((l) => l.startsWith('### '))
.map((l) => l.slice(4).trim());
// Build synthetic fileEntries for the classifier
// We don't have live sig arrays here, so rebuild from the context blocks
const entries = [];
const blocks = content.split(/^### /m).slice(1); // slice past the header
for (const block of blocks) {
const firstLine = block.split('\n')[0].trim();
const codeBlock = block.match(/```\n([\s\S]*?)```/);
const sigs = codeBlock ? codeBlock[1].trim().split('\n').filter(Boolean) : [];
entries.push({ filePath: path.join(cwd, firstLine), sigs });
}
try {
const { classifyAll } = require('../../src/routing/classifier');
const { formatRoutingSection } = require('../../src/routing/hints');
const groups = classifyAll(entries, cwd);
return formatRoutingSection(groups);
} catch (err) {
return `_Routing classification failed: ${err.message}_`;
}
}
/**
* explain_file({ path }) → string
*
* Returns a file's signatures, its direct imports, and files that import it.
* path: relative path from project root (e.g. 'src/services/auth.ts')
*/
function explainFile(args, cwd) {
if (!args || !args.path) return 'Missing required argument: path';
const targetRel = args.path.replace(/\\/g, '/').replace(/^\//, '');
const targetAbs = path.resolve(cwd, targetRel);
const contextPath = path.join(cwd, CONTEXT_FILE);
const lines = ['# explain_file: ' + targetRel, ''];
// ── Signatures (from context file) ─────────────────────────────────────
lines.push('## Signatures');
let indexedFiles = [];
if (fs.existsSync(contextPath)) {
const ctxContent = fs.readFileSync(contextPath, 'utf8');
const ctxLines = ctxContent.split('\n');
let capturing = false;
const sigLines = [];
for (const line of ctxLines) {
if (line.startsWith('### ')) {
if (capturing) break; // already collected our block
const rel = line.slice(4).trim().replace(/\\/g, '/');
capturing = rel === targetRel || rel.endsWith('/' + targetRel) || targetRel.endsWith('/' + rel);
if (capturing) continue;
} else if (capturing) {
sigLines.push(line);
}
}
const sigs = sigLines.filter((l) => l !== '```' && l.trim() !== '');
if (sigs.length > 0) {
lines.push(...sigs);
} else {
lines.push('_No signatures indexed for this file. Run: node gen-context.js_');
}
indexedFiles = ctxContent
.split('\n')
.filter((l) => l.startsWith('### '))
.map((l) => path.resolve(cwd, l.slice(4).trim()));
} else {
lines.push('_No context file found. Run: node gen-context.js_');
}
if (!fs.existsSync(targetAbs)) {
lines.push('');
lines.push('> File not found on disk: ' + targetRel);
return lines.join('\n');
}
lines.push('');
// ── Direct imports ────────────────────────────────────────────────────────
lines.push('## Imports (direct dependencies)');
try {
const { extractImports } = require('../map/import-graph');
const fileContent = fs.readFileSync(targetAbs, 'utf8');
const fileSet = new Set(indexedFiles);
fileSet.add(targetAbs);
const imports = extractImports(targetAbs, fileContent, fileSet);
if (imports.length > 0) {
for (const imp of imports) lines.push('- ' + path.relative(cwd, imp).replace(/\\/g, '/'));
} else {
lines.push('_No resolvable relative imports found._');
}
} catch (err) {
lines.push('_Could not analyze imports: ' + err.message + '_');
}
lines.push('');
// ── Callers (reverse-import lookup) ──────────────────────────────────────
lines.push('## Callers (files that import this file)');
try {
const { extractImports } = require('../map/import-graph');
const fileSet = new Set(indexedFiles);
fileSet.add(targetAbs);
const callers = [];
for (const f of indexedFiles) {
if (f === targetAbs || !fs.existsSync(f)) continue;
try {
const fc = fs.readFileSync(f, 'utf8');
const imps = extractImports(f, fc, fileSet);
if (imps.includes(targetAbs)) callers.push(path.relative(cwd, f).replace(/\\/g, '/'));
} catch (_) {}
}
if (callers.length > 0) {
for (const c of callers) lines.push('- ' + c);
} else {
lines.push('_No indexed files import this file._');
}
} catch (err) {
lines.push('_Could not analyze callers: ' + err.message + '_');
}
return lines.join('\n');
}
/**
* list_modules({}) → string
*
* Lists all srcDir modules present in the context file, sorted by token count
* descending. Helps agents decide which module to query with read_context.
*/
function listModules(args, cwd) {
const contextPath = path.join(cwd, CONTEXT_FILE);
if (!fs.existsSync(contextPath)) {
return 'No context file found. Run: node gen-context.js';
}
const content = fs.readFileSync(contextPath, 'utf8');
const ctxLines = content.split('\n');
const groups = {}; // key: top-level dir, value: { fileCount, tokenCount }
let currentGroup = null;
let blockBuf = [];
function flushBlock() {
if (currentGroup === null || blockBuf.length === 0) return;
if (!groups[currentGroup]) groups[currentGroup] = { fileCount: 0, tokenCount: 0 };
groups[currentGroup].fileCount++;
groups[currentGroup].tokenCount += Math.ceil(blockBuf.join('\n').length / 4);
blockBuf = [];
}
for (const line of ctxLines) {
if (line.startsWith('### ')) {
flushBlock();
const rel = line.slice(4).trim().replace(/\\/g, '/');
const parts = rel.split('/');
currentGroup = parts.length > 1 ? parts[0] : '.';
} else if (currentGroup !== null) {
blockBuf.push(line);
}
}
flushBlock();
const sorted = Object.entries(groups)
.map(([mod, data]) => ({ module: mod, fileCount: data.fileCount, tokenCount: data.tokenCount }))
.sort((a, b) => b.tokenCount - a.tokenCount);
if (sorted.length === 0) return 'No modules found in context file.';
const total = sorted.reduce((s, m) => s + m.tokenCount, 0);
return [
'# Modules',
'',
'| Module | Files | Tokens |',
'|--------|-------|--------|',
...sorted.map((m) => `| ${m.module} | ${m.fileCount} | ~${m.tokenCount} |`),
'',
`**Total context tokens: ~${total}**`,
'',
'_Use `read_context({ module: "name" })` to get signatures for a specific module._',
].join('\n');
}
/**
* query_context({ query, topK? }) → string
*
* Ranks context-file entries by relevance to the query and returns the
* top-K most relevant files with their signatures and scores.
*/
function queryContext(args, cwd) {
if (!args || !args.query) return 'Missing required argument: query';
const contextPath = path.join(cwd, CONTEXT_FILE);
if (!fs.existsSync(contextPath)) {
return 'No context file found. Run: node gen-context.js';
}
try {
const { rank, buildSigIndex, formatRankTable } = require('../retrieval/ranker');
const index = buildSigIndex(cwd);
if (index.size === 0) return 'No signatures indexed. Run: node gen-context.js';
const topK = Math.min(Math.max(1, parseInt(args.topK, 10) || 10), 25);
const results = rank(args.query, index, { topK });
return formatRankTable(results, args.query);
} catch (err) {
return `_query_context failed: ${err.message}_`;
}
}
module.exports = { readContext, searchSignatures, getMap, createCheckpoint, getRouting, explainFile, listModules, queryContext };