Skip to content

Commit 5aac439

Browse files
ndemiancclaude
andcommitted
Atom++ AI: multi-file (codebase-wide) chat context
- Pin any workspace files as chat context via a searchable multi-select picker (unified "+ Add context": current selection + all files, build/vendor dirs excluded). - Pinned files show as removable chips (language badge + ✕) in the composer; their contents are sent with each message alongside the active file, capped at 80KB/file and 250KB total to protect the context window. - New chat clears pinned files. Added "AI: Add Files to Chat…" command + title-bar button. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 5caaf82 commit 5aac439

3 files changed

Lines changed: 101 additions & 7 deletions

File tree

‎extensions/atom-ai/extension.js‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ let activeWebview;
3535
let conversation = [];
3636
/** @type {string | null} */
3737
let pendingContext = null;
38+
/** Files pinned as chat context (whole codebase-wide context). @type {{id:string,uri:vscode.Uri,name:string,rel:string}[]} */
39+
let contextFiles = [];
3840
/** @type {AbortController | null} */
3941
let abort = null;
4042

@@ -75,11 +77,78 @@ function addSelection() {
7577
post({ type: 'context', label: sel.label });
7678
}
7779

80+
function postContextFiles() {
81+
post({ type: 'contextFiles', files: contextFiles.map((f) => ({ id: f.id, name: f.name, rel: f.rel })) });
82+
}
83+
84+
function removeFileContext(id) {
85+
contextFiles = contextFiles.filter((f) => f.id !== id);
86+
postContextFiles();
87+
}
88+
89+
/** Unified "+ Add context" picker: choose the current selection and/or files from the workspace. */
90+
async function addContext() {
91+
/** @type {any[]} */
92+
const items = [];
93+
const sel = captureSelection();
94+
if (sel) { items.push({ label: '$(selection) Selected code', description: sel.label, _kind: 'sel', _sel: sel }); }
95+
96+
const excludes = '{**/node_modules/**,**/.git/**,**/out/**,**/dist/**,**/.vscode-test/**,**/*.map}';
97+
const uris = await vscode.workspace.findFiles('**/*', excludes, 5000);
98+
if (sel && uris.length) { items.push({ label: 'Workspace files', kind: vscode.QuickPickItemKind.Separator }); }
99+
for (const u of uris) {
100+
items.push({ label: '$(file) ' + path.basename(u.fsPath), description: vscode.workspace.asRelativePath(u), _kind: 'file', _uri: u });
101+
}
102+
if (!items.length) { vscode.window.showInformationMessage('Atom++ AI: no workspace files to add.'); return; }
103+
104+
const picks = await vscode.window.showQuickPick(items, {
105+
canPickMany: true,
106+
matchOnDescription: true,
107+
placeHolder: 'Add context — pick the selection and/or files (type to filter)'
108+
});
109+
if (!picks || !picks.length) { return; }
110+
111+
for (const p of picks) {
112+
if (p._kind === 'sel') {
113+
pendingContext = p._sel.block;
114+
post({ type: 'context', label: p._sel.label });
115+
} else if (p._kind === 'file') {
116+
const id = p._uri.fsPath;
117+
if (!contextFiles.find((f) => f.id === id)) {
118+
contextFiles.push({ id, uri: p._uri, name: path.basename(id), rel: vscode.workspace.asRelativePath(p._uri) });
119+
}
120+
}
121+
}
122+
vscode.commands.executeCommand('atomAi.chat.focus');
123+
postContextFiles();
124+
}
125+
126+
/** Read pinned context files as content blocks, capped to protect the context window. */
127+
async function contextFileBlocks() {
128+
const PER_FILE = 80 * 1024;
129+
const TOTAL = 250 * 1024;
130+
const blocks = [];
131+
let used = 0;
132+
for (const f of contextFiles) {
133+
try {
134+
const doc = await vscode.workspace.openTextDocument(f.uri);
135+
let body = doc.getText();
136+
if (body.length > PER_FILE) { body = body.slice(0, PER_FILE) + '\n…(file truncated for context)…'; }
137+
if (used + body.length > TOTAL) { blocks.push('…(some pinned files omitted to fit the context window)…'); break; }
138+
used += body.length;
139+
blocks.push('File `' + f.rel + '`:\n```' + (doc.languageId || '') + '\n' + body + '\n```');
140+
} catch { /* unreadable/binary file — skip */ }
141+
}
142+
return blocks;
143+
}
144+
78145
function newChat() {
79146
conversation = [];
80147
pendingContext = null;
148+
contextFiles = [];
81149
if (abort) { abort.abort(); }
82150
post({ type: 'reset' });
151+
postContextFiles();
83152
}
84153

85154
/** The currently open file as a context block (capped), or null. */
@@ -113,6 +182,7 @@ async function handleSend(text) {
113182
const blocks = [];
114183
const fileBlock = activeFileBlock();
115184
if (fileBlock) { blocks.push(fileBlock); }
185+
blocks.push(...await contextFileBlocks());
116186
if (pendingContext) { blocks.push(pendingContext); }
117187
const userContent = blocks.length ? (blocks.join('\n\n') + '\n\n' + text) : text;
118188
conversation.push({ role: 'user', content: userContent });
@@ -214,10 +284,12 @@ class ChatViewProvider {
214284
view.webview.html = getHtml();
215285
view.webview.onDidReceiveMessage(async (msg) => {
216286
switch (msg.type) {
217-
case 'ready': sendConfigToWebview(); postActiveFile(); break;
287+
case 'ready': sendConfigToWebview(); postActiveFile(); postContextFiles(); break;
218288
case 'send': await handleSend(msg.text); break;
219289
case 'stop': if (abort) { abort.abort(); } break;
220290
case 'addSelection': addSelection(); break;
291+
case 'addContext': await addContext(); break;
292+
case 'removeContext': removeFileContext(msg.id); break;
221293
case 'newChat': newChat(); break;
222294
case 'setKey': await promptForKey(); break;
223295
case 'pickModel': await pickModel(); break;
@@ -249,6 +321,7 @@ function activate(context) {
249321
vscode.commands.registerCommand('atompp.ai.newChat', newChat),
250322
vscode.commands.registerCommand('atompp.ai.pickModel', pickModel),
251323
vscode.commands.registerCommand('atompp.ai.addSelection', addSelection),
324+
vscode.commands.registerCommand('atompp.ai.addFileContext', addContext),
252325
vscode.commands.registerCommand('atompp.ai.setApiKey', promptForKey),
253326
vscode.commands.registerCommand('atompp.ai.clearApiKey', async () => {
254327
await context.secrets.delete(SECRET_KEY);

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
padding: 2px 4px; border-radius: 4px;
6262
background: var(--accent); color: #fff; opacity: .9;
6363
}
64+
.chip.pinned { padding-right: 4px; }
65+
.chip .x {
66+
display: inline-flex; align-items: center; justify-content: center;
67+
width: 14px; height: 14px; margin-left: 1px; cursor: pointer;
68+
border-radius: 50%; font-size: 9px; opacity: .55;
69+
}
70+
.chip .x:hover { opacity: 1; background: var(--vscode-toolbar-hoverBackground, rgba(127,127,127,.35)); }
6471
.addchip {
6572
display: inline-flex; align-items: center; gap: 5px;
6673
font-size: 11px; line-height: 1; padding: 4px 9px; cursor: pointer;
@@ -167,6 +174,7 @@
167174
let streaming = false;
168175
let activeFileLabel = null;
169176
let selLabel = null;
177+
let ctxFiles = []; // files pinned as context: {id,name,rel}
170178

171179
function esc(s){ return s.replace(/[&<>]/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;'}[c])); }
172180

@@ -185,12 +193,22 @@
185193
function selChip(label){
186194
return '<span class="chip" title="Selected code, sent as context">📎 <span class="name">' + esc(label) + '</span></span>';
187195
}
196+
function pinnedChip(f){
197+
return '<span class="chip pinned" title="' + esc(f.rel) + ' — pinned as context">' +
198+
'<span class="badge">' + esc(badgeFor(f.name)) + '</span>' +
199+
'<span class="name">' + esc(f.name) + '</span>' +
200+
'<span class="x" data-id="' + esc(f.id) + '" title="Remove">✕</span></span>';
201+
}
188202
function renderChips(){
189-
let h = '<button class="addchip" id="addctx" title="Add selected code as context (⌥⌘A)"><span class="plus">+</span> Add context</button>';
203+
let h = '<button class="addchip" id="addctx" title="Add files or selection as context"><span class="plus">+</span> Add context</button>';
190204
if (activeFileLabel) { h += fileChip(activeFileLabel); }
205+
for (const f of ctxFiles) { h += pinnedChip(f); }
191206
if (selLabel) { h += selChip(selLabel); }
192207
chipsEl.innerHTML = h;
193-
document.getElementById('addctx').onclick = () => vscode.postMessage({ type: 'addSelection' });
208+
document.getElementById('addctx').onclick = () => vscode.postMessage({ type: 'addContext' });
209+
chipsEl.querySelectorAll('.x').forEach(x => {
210+
x.onclick = (e) => { e.stopPropagation(); vscode.postMessage({ type: 'removeContext', id: x.getAttribute('data-id') }); };
211+
});
194212
}
195213

196214
function render(md){
@@ -247,7 +265,7 @@
247265
}
248266

249267
sendBtn.onclick = doSend;
250-
document.getElementById('attach').onclick = () => vscode.postMessage({ type: 'addSelection' });
268+
document.getElementById('attach').onclick = () => vscode.postMessage({ type: 'addContext' });
251269
document.getElementById('model').onclick = () => vscode.postMessage({ type: 'pickModel' });
252270
document.getElementById('settings').onclick = () => vscode.postMessage({ type: 'openSettings' });
253271

@@ -275,11 +293,12 @@
275293
current = null; setStreaming(false);
276294
}
277295
else if (m.type === 'activeFile'){ activeFileLabel = m.label; renderChips(); }
296+
else if (m.type === 'contextFiles'){ ctxFiles = m.files || []; renderChips(); }
278297
else if (m.type === 'context'){ selLabel = m.label; renderChips(); }
279298
else if (m.type === 'clearContext'){ selLabel = null; renderChips(); }
280299
else if (m.type === 'reset'){
281300
log.innerHTML = '<div id="empty"><div class="big">New chat</div>Ask about your code, or describe what to build.</div>';
282-
selLabel = null; renderChips();
301+
selLabel = null; ctxFiles = []; renderChips();
283302
pending = ''; shown = ''; doneSignaled = false; flushAll = false; current = null; setStreaming(false);
284303
}
285304
});

‎extensions/atom-ai/package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
{ "command": "atompp.ai.newChat", "title": "AI: New Chat", "category": "Atom++", "icon": "$(add)" },
4141
{ "command": "atompp.ai.pickModel", "title": "AI: Select Model…", "category": "Atom++" },
4242
{ "command": "atompp.ai.addSelection", "title": "AI: Add Selection to Chat", "category": "Atom++" },
43+
{ "command": "atompp.ai.addFileContext", "title": "AI: Add Files to Chat…", "category": "Atom++", "icon": "$(new-file)" },
4344
{ "command": "atompp.ai.editSelection", "title": "AI: Edit Selection…", "category": "Atom++", "icon": "$(sparkle)" },
4445
{ "command": "atompp.ai.applyEdit", "title": "Keep AI Edit", "category": "Atom++", "icon": "$(check)" },
4546
{ "command": "atompp.ai.discardEdit", "title": "Discard AI Edit", "category": "Atom++", "icon": "$(close)" },
@@ -49,8 +50,9 @@
4950
],
5051
"menus": {
5152
"view/title": [
52-
{ "command": "atompp.ai.newChat", "when": "view == atomAi.chat", "group": "navigation@1" },
53-
{ "command": "atompp.ai.setApiKey", "when": "view == atomAi.chat", "group": "navigation@2" }
53+
{ "command": "atompp.ai.addFileContext", "when": "view == atomAi.chat", "group": "navigation@1" },
54+
{ "command": "atompp.ai.newChat", "when": "view == atomAi.chat", "group": "navigation@2" },
55+
{ "command": "atompp.ai.setApiKey", "when": "view == atomAi.chat", "group": "navigation@3" }
5456
],
5557
"editor/context": [
5658
{ "command": "atompp.ai.editSelection", "when": "editorHasSelection", "group": "z_atompp@1" },

0 commit comments

Comments
 (0)