Skip to content

Commit 92532ba

Browse files
ndemiancclaude
andcommitted
Atom++ AI: automatic context retrieval for chat (codebase-wide Phase 2)
On each chat message, auto-discover and include workspace files relevant to the question — no manual pinning needed. A muted "Auto-context: …" line shows which files were pulled in (transparency). Retrieval signals, scoped to the active file's sub-project so unrelated sibling trees in the same workspace don't pollute results: - ripgrep CONTENT search (primary) using the editor's bundled rg (resolves both the dev and packaged paths); respects .gitignore, so the vscode/ checkout is skipped. - filename matches and workspace symbols as secondary signals. - specificity weighting (long/CamelCase identifiers count more) + a minimum-score threshold so lone common-word matches don't add noise. Settings: chat.autoContext (default on), chat.autoContextMaxFiles (4), chat.scopeToActiveProject (on), chat.workspaceMap (off). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5aac439 commit 92532ba

3 files changed

Lines changed: 219 additions & 2 deletions

File tree

‎extensions/atom-ai/extension.js‎

Lines changed: 188 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
const vscode = require('vscode');
1212
const path = require('path');
1313
const fs = require('fs');
14+
const cp = require('child_process');
1415
const { streamClaude, streamOllama, completeClaude, completeOllama, listOllamaModels } = require('./providers');
1516
const { registerAiEdit } = require('./aiEdit');
1617
const { registerLmProvider } = require('./lmProvider');
@@ -23,6 +24,8 @@ const CLAUDE_MODELS = [
2324
];
2425

2526
const SECRET_KEY = 'atompp.ai.anthropicKey';
27+
const FILE_EXCLUDES = '{**/node_modules/**,**/.git/**,**/out/**,**/dist/**,**/.vscode-test/**,**/*.map}';
28+
const STOPWORDS = new Set(['the','and','for','with','that','this','how','does','what','where','when','why','from','into','your','you','are','was','were','will','can','could','should','would','about','have','has','its','it','the','file','code','function','please','show','tell','explain','using','use','used','there','their','then','than','they','them','some','any','all','not','but','get','set']);
2629
const SYSTEM_PROMPT =
2730
'You are Atom++\'s built-in AI assistant, helping the user write and understand code inside their editor. ' +
2831
'Be concise and practical. Use Markdown and fenced code blocks. When given a code selection as context, focus your answer on it.';
@@ -93,8 +96,7 @@ async function addContext() {
9396
const sel = captureSelection();
9497
if (sel) { items.push({ label: '$(selection) Selected code', description: sel.label, _kind: 'sel', _sel: sel }); }
9598

96-
const excludes = '{**/node_modules/**,**/.git/**,**/out/**,**/dist/**,**/.vscode-test/**,**/*.map}';
97-
const uris = await vscode.workspace.findFiles('**/*', excludes, 5000);
99+
const uris = await vscode.workspace.findFiles('**/*', FILE_EXCLUDES, 5000);
98100
if (sel && uris.length) { items.push({ label: 'Workspace files', kind: vscode.QuickPickItemKind.Separator }); }
99101
for (const u of uris) {
100102
items.push({ label: '$(file) ' + path.basename(u.fsPath), description: vscode.workspace.asRelativePath(u), _kind: 'file', _uri: u });
@@ -142,6 +144,177 @@ async function contextFileBlocks() {
142144
return blocks;
143145
}
144146

147+
/** List workspace files once (cached per send), respecting the standard excludes. */
148+
async function listWorkspaceFiles() {
149+
if (!vscode.workspace.workspaceFolders || !vscode.workspace.workspaceFolders.length) { return []; }
150+
return vscode.workspace.findFiles('**/*', FILE_EXCLUDES, 5000);
151+
}
152+
153+
/** Identifiers/keywords from a question, minus common stop-words. */
154+
function extractKeywords(text) {
155+
const raw = text.match(/[A-Za-z_][A-Za-z0-9_]{2,}/g) || [];
156+
const seen = new Set();
157+
const out = [];
158+
for (const w of raw) {
159+
const lw = w.toLowerCase();
160+
if (STOPWORDS.has(lw) || seen.has(lw)) { continue; }
161+
seen.add(lw);
162+
out.push(w);
163+
if (out.length >= 12) { break; }
164+
}
165+
return out;
166+
}
167+
168+
/** Resolve the ripgrep binary bundled with the editor (dev and packaged paths differ). */
169+
let _rgPath; // cached: string | null
170+
function rgPath() {
171+
if (_rgPath !== undefined) { return _rgPath; }
172+
const root = vscode.env.appRoot;
173+
const candidates = [
174+
path.join(root, 'node_modules', '@vscode', 'ripgrep', 'bin', 'rg'),
175+
path.join(root, 'node_modules', '@vscode', 'ripgrep-universal', 'bin', process.platform + '-' + process.arch, 'rg'),
176+
path.join(root, 'node_modules.asar.unpacked', '@vscode', 'ripgrep', 'bin', 'rg')
177+
];
178+
_rgPath = candidates.find((c) => { try { return fs.existsSync(c); } catch { return false; } }) || null;
179+
return _rgPath;
180+
}
181+
182+
/** Files (absolute paths) under cwd whose CONTENT contains the literal term. Empty on any failure. */
183+
function rgFiles(term, cwd) {
184+
return new Promise((resolve) => {
185+
const bin = rgPath();
186+
if (!bin || !cwd) { resolve([]); return; }
187+
const args = [
188+
'--files-with-matches', '--no-messages', '--no-config', '-i', '-F',
189+
'--max-filesize', '1M', '--max-count', '1',
190+
'-g', '!**/node_modules/**', '-g', '!**/.git/**', '-g', '!**/out/**',
191+
'-g', '!**/dist/**', '-g', '!**/*.map', '-g', '!**/*.min.*',
192+
'-e', term, '.'
193+
];
194+
let out = '';
195+
let done = false;
196+
const finish = (paths) => { if (!done) { done = true; resolve(paths); } };
197+
try {
198+
const child = cp.spawn(bin, args, { cwd });
199+
const timer = setTimeout(() => { try { child.kill(); } catch { /* noop */ } finish([]); }, 4000);
200+
child.stdout.on('data', (d) => { out += d.toString(); });
201+
child.on('error', () => { clearTimeout(timer); finish([]); });
202+
child.on('close', () => {
203+
clearTimeout(timer);
204+
finish(out.split('\n').map((s) => s.trim()).filter(Boolean).map((rel) => path.resolve(cwd, rel)));
205+
});
206+
} catch { finish([]); }
207+
});
208+
}
209+
210+
/** Scope retrieval to the active file's top-level sub-project, so unrelated sibling trees
211+
* (e.g. a vendored source dump) don't pollute results. Returns the search dir + rel prefix. */
212+
function activeProjectScope() {
213+
const folders = vscode.workspace.workspaceFolders || [];
214+
const fallback = { dir: folders.length ? folders[0].uri.fsPath : '', prefix: '' };
215+
if (!aiConfig().get('chat.scopeToActiveProject', true)) { return fallback; }
216+
const ed = vscode.window.activeTextEditor;
217+
if (!ed || ed.document.uri.scheme !== 'file') { return fallback; }
218+
const wsFolder = vscode.workspace.getWorkspaceFolder(ed.document.uri);
219+
if (!wsFolder) { return fallback; }
220+
const rel = path.relative(wsFolder.uri.fsPath, ed.document.uri.fsPath);
221+
if (!rel || rel.startsWith('..')) { return fallback; }
222+
const top = rel.split(path.sep)[0];
223+
const topPath = path.join(wsFolder.uri.fsPath, top);
224+
try { if (!fs.statSync(topPath).isDirectory()) { return fallback; } } catch { return fallback; }
225+
return { dir: topPath, prefix: top + '/' };
226+
}
227+
228+
/** A specific identifier (long or camelCase) is a much stronger relevance signal than a common word. */
229+
function contentWeight(kw) {
230+
return (kw.length >= 8 || /[A-Z]/.test(kw.slice(1))) ? 6 : 3;
231+
}
232+
233+
/**
234+
* Auto-discover files relevant to the question via ripgrep content search (primary),
235+
* filename matches, and workspace symbols — scoped to the active sub-project. Returns their
236+
* contents as context blocks (capped). Skips the active & pinned files; thresholds out noise.
237+
* @returns {Promise<{blocks:string[], names:string[]}>}
238+
*/
239+
async function gatherAutoContext(question, allFiles) {
240+
const cfg = aiConfig();
241+
const keywords = extractKeywords(question);
242+
if (!keywords.length) { return { blocks: [], names: [] }; }
243+
const scope = activeProjectScope();
244+
const inScope = (uri) => !scope.prefix || vscode.workspace.asRelativePath(uri).startsWith(scope.prefix);
245+
246+
/** @type {Map<string,{uri:vscode.Uri,score:number}>} */
247+
const score = new Map();
248+
const bump = (uri, s) => {
249+
const k = uri.fsPath;
250+
const cur = score.get(k);
251+
if (cur) { cur.score += s; } else { score.set(k, { uri, score: s }); }
252+
};
253+
254+
// 1. CONTENT search via ripgrep (primary signal — finds files by what's inside them).
255+
const terms = keywords.slice(0, 6);
256+
const hits = await Promise.all(terms.map((kw) => rgFiles(kw, scope.dir)));
257+
for (let i = 0; i < terms.length; i++) {
258+
for (const abs of hits[i]) { bump(vscode.Uri.file(abs), contentWeight(terms[i])); }
259+
}
260+
261+
// 2. filename matches (scoped).
262+
for (const u of allFiles) {
263+
if (!inScope(u)) { continue; }
264+
const base = path.basename(u.fsPath).toLowerCase();
265+
for (const kw of keywords) { if (kw.length >= 3 && base.includes(kw.toLowerCase())) { bump(u, 4); } }
266+
}
267+
268+
// 3. workspace symbols (scoped; uses language-server indexes when present).
269+
for (const kw of terms) {
270+
let syms = [];
271+
try { syms = await vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider', kw) || []; } catch { syms = []; }
272+
for (const sym of syms.slice(0, 20)) {
273+
const uri = sym && sym.location && sym.location.uri;
274+
if (!uri || !inScope(uri)) { continue; }
275+
const exact = sym.name && sym.name.toLowerCase() === kw.toLowerCase();
276+
bump(uri, exact ? 5 : 2);
277+
}
278+
}
279+
280+
const MIN_SCORE = 4; // a single common-word match (3) is not enough on its own.
281+
const activeUri = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri.fsPath : null;
282+
const pinned = new Set(contextFiles.map((f) => f.id));
283+
const max = Math.max(0, cfg.get('chat.autoContextMaxFiles', 4));
284+
const ranked = [...score.values()]
285+
.filter((e) => e.score >= MIN_SCORE && e.uri.fsPath !== activeUri && !pinned.has(e.uri.fsPath))
286+
.sort((a, b) => b.score - a.score)
287+
.slice(0, max);
288+
289+
const blocks = [];
290+
const names = [];
291+
let used = 0;
292+
const PER = 60 * 1024, TOTAL = 180 * 1024;
293+
for (const e of ranked) {
294+
try {
295+
const doc = await vscode.workspace.openTextDocument(e.uri);
296+
let body = doc.getText();
297+
if (body.length > PER) { body = body.slice(0, PER) + '\n…(truncated)…'; }
298+
if (used + body.length > TOTAL) { break; }
299+
used += body.length;
300+
const rel = vscode.workspace.asRelativePath(e.uri);
301+
blocks.push('Possibly relevant file `' + rel + '`:\n```' + (doc.languageId || '') + '\n' + body + '\n```');
302+
names.push(rel);
303+
} catch { /* unreadable/binary — skip */ }
304+
}
305+
return { blocks, names };
306+
}
307+
308+
/** A compact list of project file paths, so the model knows the repo structure. */
309+
function workspaceMapBlock(allFiles) {
310+
if (!allFiles.length) { return null; }
311+
const rels = allFiles.map((u) => vscode.workspace.asRelativePath(u)).sort();
312+
const CAP = 400;
313+
let list = rels.slice(0, CAP).join('\n');
314+
if (rels.length > CAP) { list += '\n…(' + (rels.length - CAP) + ' more files)…'; }
315+
return 'Project files (paths only, for orientation):\n```\n' + list + '\n```';
316+
}
317+
145318
function newChat() {
146319
conversation = [];
147320
pendingContext = null;
@@ -180,13 +353,26 @@ async function handleSend(text) {
180353
const provider = cfg.get('provider', 'claude');
181354

182355
const blocks = [];
356+
357+
// Workspace-derived context (auto-retrieval + optional file map) — one file listing, reused.
358+
const wantAuto = cfg.get('chat.autoContext', true);
359+
const wantMap = cfg.get('chat.workspaceMap', false);
360+
let allFiles = [];
361+
if (wantAuto || wantMap) { allFiles = await listWorkspaceFiles(); }
362+
if (wantMap) { const mb = workspaceMapBlock(allFiles); if (mb) { blocks.push(mb); } }
363+
183364
const fileBlock = activeFileBlock();
184365
if (fileBlock) { blocks.push(fileBlock); }
185366
blocks.push(...await contextFileBlocks());
367+
368+
const auto = wantAuto ? await gatherAutoContext(text, allFiles) : { blocks: [], names: [] };
369+
blocks.push(...auto.blocks);
370+
186371
if (pendingContext) { blocks.push(pendingContext); }
187372
const userContent = blocks.length ? (blocks.join('\n\n') + '\n\n' + text) : text;
188373
conversation.push({ role: 'user', content: userContent });
189374
post({ type: 'userMessage', text });
375+
if (auto.names.length) { post({ type: 'autoContext', names: auto.names }); }
190376
pendingContext = null;
191377
post({ type: 'clearContext' });
192378
post({ type: 'assistantStart' });

‎extensions/atom-ai/media/chat.html‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
.err { color: var(--vscode-errorForeground); }
3434
pre { background: var(--vscode-textCodeBlock-background, rgba(127,127,127,.15)); padding: 9px 10px; border-radius: 8px; overflow-x: auto; margin: 8px 0; }
3535
code { font-family: var(--vscode-editor-font-family, ui-monospace, monospace); font-size: .92em; }
36+
.autoctx { font-size: 11px; opacity: .55; margin: -4px 2px 2px; }
37+
.autoctx .files { font-family: var(--vscode-editor-font-family, ui-monospace, monospace); }
3638
#empty { margin: auto; max-width: 280px; text-align: center; opacity: .65; line-height: 1.55; padding: 20px; }
3739
#empty .big { font-size: 1.15em; opacity: .9; margin-bottom: 6px; }
3840

@@ -232,6 +234,13 @@
232234
d.innerHTML = '<div class="role">' + (role === 'user' ? 'You' : 'Atom++ AI') + '</div><div class="body">' + html + '</div>';
233235
log.appendChild(d); log.scrollTop = log.scrollHeight; return d.querySelector('.body');
234236
}
237+
function addAutoCtx(names){
238+
if (!names || !names.length) { return; }
239+
clearEmpty();
240+
const d = document.createElement('div'); d.className = 'autoctx';
241+
d.innerHTML = '🔎 Auto-context: <span class="files">' + names.map(esc).join(', ') + '</span>';
242+
log.appendChild(d); log.scrollTop = log.scrollHeight;
243+
}
235244

236245
// Typewriter reveal: deltas land in `pending`; this loop moves them into `shown`
237246
// at a steady pace so output flows in naturally instead of dumping in blocks.
@@ -294,6 +303,7 @@
294303
}
295304
else if (m.type === 'activeFile'){ activeFileLabel = m.label; renderChips(); }
296305
else if (m.type === 'contextFiles'){ ctxFiles = m.files || []; renderChips(); }
306+
else if (m.type === 'autoContext'){ addAutoCtx(m.names); }
297307
else if (m.type === 'context'){ selLabel = m.label; renderChips(); }
298308
else if (m.type === 'clearContext'){ selLabel = null; renderChips(); }
299309
else if (m.type === 'reset'){

‎extensions/atom-ai/package.json‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@
8787
"default": true,
8888
"description": "Automatically send the currently open file to the AI as context with each chat message."
8989
},
90+
"atompp.ai.chat.autoContext": {
91+
"type": "boolean",
92+
"default": true,
93+
"description": "Automatically find and include workspace files relevant to your chat question (via filename + symbol search)."
94+
},
95+
"atompp.ai.chat.autoContextMaxFiles": {
96+
"type": "number",
97+
"default": 4,
98+
"minimum": 0,
99+
"description": "Maximum number of files auto-included as context per chat message."
100+
},
101+
"atompp.ai.chat.scopeToActiveProject": {
102+
"type": "boolean",
103+
"default": true,
104+
"description": "Limit auto-context search to the active file's top-level sub-folder, so unrelated sibling projects in the same workspace don't pollute results."
105+
},
106+
"atompp.ai.chat.workspaceMap": {
107+
"type": "boolean",
108+
"default": false,
109+
"description": "Include a list of all project file paths with each chat message, so the AI knows the repo structure. Uses more tokens."
110+
},
90111
"atompp.ai.completions.enabled": {
91112
"type": "boolean",
92113
"default": true,

0 commit comments

Comments
 (0)