Skip to content

Commit 5caaf82

Browse files
ndemiancclaude
andcommitted
Atom++ AI: inline tab-completion (ghost text)
- inlineComplete.js: register an InlineCompletionItemProvider over all files. Builds fill-in-the-middle context (code before/after the cursor), debounces, ties the request's AbortController to VS Code's cancellation token (typing cancels in-flight calls), strips stray markdown fences, and skips suggestions that duplicate text after the cursor. Status-bar toggle + command. - providers.js: add non-streaming completeClaude() / completeOllama(). - extension.js: register inline completions with a SILENT key lookup so it never prompts mid-typing (no key -> no ghost text). - Settings: completions.enabled / completions.model (default Haiku) / completions.debounce (default 150ms, tuned for snappiness). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1b8d647 commit 5caaf82

4 files changed

Lines changed: 240 additions & 3 deletions

File tree

‎extensions/atom-ai/extension.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
const vscode = require('vscode');
1212
const path = require('path');
1313
const fs = require('fs');
14-
const { streamClaude, streamOllama, listOllamaModels } = require('./providers');
14+
const { streamClaude, streamOllama, completeClaude, completeOllama, listOllamaModels } = require('./providers');
1515
const { registerAiEdit } = require('./aiEdit');
1616
const { registerLmProvider } = require('./lmProvider');
17+
const { registerInlineComplete } = require('./inlineComplete');
1718

1819
const CLAUDE_MODELS = [
1920
{ label: 'Claude Opus 4.8', id: 'claude-opus-4-8', detail: 'Most capable' },
@@ -277,6 +278,14 @@ function activate(context) {
277278
return key;
278279
});
279280

281+
// Inline (ghost-text) tab-completion. Silent key lookup — it must never prompt mid-typing.
282+
registerInlineComplete(context, {
283+
aiConfig,
284+
getKeySilent: () => context.secrets.get(SECRET_KEY),
285+
completeClaude,
286+
completeOllama
287+
});
288+
280289
// AI-first: reveal the chat (in the secondary side bar) on first launch. We do this once
281290
// and then defer to VS Code's own per-workspace layout persistence, so if the user later
282291
// closes the panel we don't keep forcing it back open.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Atom++ — AI (M2, feature 2: inline tab-completion)
3+
*
4+
* Ghost-text completions as you type, accepted with Tab. Uses VS Code's stable
5+
* InlineCompletionItemProvider API (no core patches). Requests go directly to the
6+
* provider (Claude / Ollama) — debounced, and cancelled the moment you keep typing.
7+
*--------------------------------------------------------------------------------------------*/
8+
// @ts-check
9+
'use strict';
10+
11+
const vscode = require('vscode');
12+
13+
const PREFIX_MAX = 2500; // chars of context before the cursor (smaller = faster first token)
14+
const SUFFIX_MAX = 1000; // chars of context after the cursor
15+
const MAX_TOKENS = 160; // keep completions short & snappy
16+
17+
const SYSTEM_PROMPT =
18+
'You are a code completion engine inside the Atom++ editor. You are given the code ' +
19+
'before the cursor and the code after it, with the insertion point marked <CURSOR>. ' +
20+
'Reply with ONLY the raw code that should be inserted at <CURSOR> to continue the code ' +
21+
'naturally. Do NOT repeat code that already exists before or after the cursor. Do NOT ' +
22+
'wrap your answer in markdown fences. Do NOT add explanations. If no completion is ' +
23+
'appropriate, reply with an empty string.';
24+
25+
/** @param {vscode.CancellationToken} token */
26+
function sleep(ms, token) {
27+
return new Promise((resolve) => {
28+
const t = setTimeout(resolve, ms);
29+
token.onCancellationRequested(() => { clearTimeout(t); resolve(undefined); });
30+
});
31+
}
32+
33+
/** Build prefix/suffix context windows around the cursor. */
34+
function buildContext(document, position) {
35+
const start = new vscode.Position(0, 0);
36+
const lastLine = document.lineAt(document.lineCount - 1);
37+
const end = lastLine.range.end;
38+
const pre = document.getText(new vscode.Range(start, position));
39+
const suf = document.getText(new vscode.Range(position, end));
40+
return { prefix: pre.slice(-PREFIX_MAX), suffix: suf.slice(0, SUFFIX_MAX) };
41+
}
42+
43+
/** Strip accidental markdown fences / leading language tags the model may add. */
44+
function clean(text) {
45+
if (!text) { return ''; }
46+
let t = text;
47+
const fence = String.fromCharCode(96, 96, 96);
48+
if (t.indexOf(fence) >= 0) {
49+
// Take the content of the first fenced block if the whole thing got wrapped.
50+
const m = t.match(new RegExp(fence + '[a-zA-Z0-9_+-]*\\n?([\\s\\S]*?)' + fence));
51+
if (m) { t = m[1]; }
52+
else { t = t.split(fence).join(''); }
53+
}
54+
return t;
55+
}
56+
57+
/**
58+
* @param {vscode.ExtensionContext} context
59+
* @param {{ aiConfig: () => vscode.WorkspaceConfiguration,
60+
* getKeySilent: () => Thenable<string|undefined>,
61+
* completeClaude: Function, completeOllama: Function }} deps
62+
*/
63+
function registerInlineComplete(context, deps) {
64+
const { aiConfig, getKeySilent, completeClaude, completeOllama } = deps;
65+
66+
// --- status bar toggle ---------------------------------------------------
67+
const status = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 50);
68+
status.command = 'atompp.ai.toggleCompletions';
69+
function refreshStatus() {
70+
const on = aiConfig().get('completions.enabled', true);
71+
status.text = on ? '$(sparkle) AI' : '$(circle-slash) AI';
72+
status.tooltip = on
73+
? 'Atom++ AI inline completions: ON (click to disable)'
74+
: 'Atom++ AI inline completions: OFF (click to enable)';
75+
status.show();
76+
}
77+
refreshStatus();
78+
79+
context.subscriptions.push(
80+
status,
81+
vscode.commands.registerCommand('atompp.ai.toggleCompletions', async () => {
82+
const cfg = aiConfig();
83+
const next = !cfg.get('completions.enabled', true);
84+
await cfg.update('completions.enabled', next, vscode.ConfigurationTarget.Global);
85+
refreshStatus();
86+
vscode.window.setStatusBarMessage('Atom++ AI completions ' + (next ? 'enabled' : 'disabled'), 2000);
87+
}),
88+
vscode.workspace.onDidChangeConfiguration((e) => {
89+
if (e.affectsConfiguration('atompp.ai.completions')) { refreshStatus(); }
90+
})
91+
);
92+
93+
// --- the provider --------------------------------------------------------
94+
/** @type {vscode.InlineCompletionItemProvider} */
95+
const provider = {
96+
async provideInlineCompletionItems(document, position, ctx, token) {
97+
const cfg = aiConfig();
98+
if (!cfg.get('completions.enabled', true)) { return null; }
99+
if (document.uri.scheme !== 'file' && document.uri.scheme !== 'untitled') { return null; }
100+
101+
// Debounce: if the user keeps typing, this token is cancelled and we bail.
102+
const debounce = Math.max(0, cfg.get('completions.debounce', 150));
103+
await sleep(debounce, token);
104+
if (token.isCancellationRequested) { return null; }
105+
106+
const provider2 = cfg.get('provider', 'claude');
107+
const { prefix, suffix } = buildContext(document, position);
108+
if (!prefix.trim() && !suffix.trim()) { return null; }
109+
110+
const userContent =
111+
'Language: ' + (document.languageId || 'plaintext') + '\n' +
112+
'Insert code at <CURSOR>. Reply with ONLY the text to insert.\n\n' +
113+
prefix + '<CURSOR>' + suffix;
114+
115+
const ac = new AbortController();
116+
const sub = token.onCancellationRequested(() => ac.abort());
117+
118+
let text = '';
119+
try {
120+
if (provider2 === 'claude') {
121+
const key = await getKeySilent();
122+
if (!key || token.isCancellationRequested) { return null; }
123+
text = await completeClaude({
124+
apiKey: key,
125+
model: cfg.get('completions.model', 'claude-haiku-4-5-20251001'),
126+
maxTokens: MAX_TOKENS,
127+
system: SYSTEM_PROMPT,
128+
messages: [{ role: 'user', content: userContent }],
129+
signal: ac.signal
130+
});
131+
} else {
132+
text = await completeOllama({
133+
url: cfg.get('ollama.url', 'http://localhost:11434'),
134+
model: cfg.get('ollama.model', 'llama3.1'),
135+
system: SYSTEM_PROMPT,
136+
messages: [{ role: 'user', content: userContent }],
137+
signal: ac.signal
138+
});
139+
}
140+
} catch (e) {
141+
// Network/abort/key errors are silent — inline completion must never nag.
142+
return null;
143+
} finally {
144+
sub.dispose();
145+
}
146+
147+
if (token.isCancellationRequested) { return null; }
148+
const insert = clean(text);
149+
if (!insert.trim()) { return null; }
150+
// Avoid suggesting text that just duplicates what's already after the cursor.
151+
if (suffix.startsWith(insert)) { return null; }
152+
153+
const item = new vscode.InlineCompletionItem(insert, new vscode.Range(position, position));
154+
return [item];
155+
}
156+
};
157+
158+
context.subscriptions.push(
159+
vscode.languages.registerInlineCompletionItemProvider({ pattern: '**' }, provider)
160+
);
161+
}
162+
163+
module.exports = { registerInlineComplete };

‎extensions/atom-ai/package.json‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
{ "command": "atompp.ai.applyEdit", "title": "Keep AI Edit", "category": "Atom++", "icon": "$(check)" },
4545
{ "command": "atompp.ai.discardEdit", "title": "Discard AI Edit", "category": "Atom++", "icon": "$(close)" },
4646
{ "command": "atompp.ai.setApiKey", "title": "AI: Set Anthropic API Key…", "category": "Atom++", "icon": "$(key)" },
47-
{ "command": "atompp.ai.clearApiKey", "title": "AI: Clear Anthropic API Key", "category": "Atom++" }
47+
{ "command": "atompp.ai.clearApiKey", "title": "AI: Clear Anthropic API Key", "category": "Atom++" },
48+
{ "command": "atompp.ai.toggleCompletions", "title": "AI: Toggle Inline Completions", "category": "Atom++" }
4849
],
4950
"menus": {
5051
"view/title": [
@@ -84,6 +85,24 @@
8485
"default": true,
8586
"description": "Automatically send the currently open file to the AI as context with each chat message."
8687
},
88+
"atompp.ai.completions.enabled": {
89+
"type": "boolean",
90+
"default": true,
91+
"description": "Show inline AI code completions (ghost text) as you type. Accept with Tab."
92+
},
93+
"atompp.ai.completions.model": {
94+
"type": "string",
95+
"enum": ["claude-haiku-4-5-20251001", "claude-sonnet-4-6", "claude-opus-4-8"],
96+
"enumDescriptions": ["Claude Haiku 4.5 (fastest — recommended)", "Claude Sonnet 4.6 (balanced)", "Claude Opus 4.8 (most capable, slower)"],
97+
"default": "claude-haiku-4-5-20251001",
98+
"description": "Claude model used for inline completions. A fast model is recommended for low latency."
99+
},
100+
"atompp.ai.completions.debounce": {
101+
"type": "number",
102+
"default": 150,
103+
"minimum": 0,
104+
"description": "Idle time in milliseconds before requesting an inline completion (lower = snappier but more requests)."
105+
},
87106
"atompp.ai.claude.model": {
88107
"type": "string",
89108
"enum": ["claude-opus-4-8", "claude-sonnet-4-6", "claude-haiku-4-5-20251001"],

‎extensions/atom-ai/providers.js‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,52 @@ async function streamOllama(opts) {
9595
});
9696
}
9797

98+
/**
99+
* Non-streaming single completion from Claude. Returns the full text response.
100+
* Used for inline (ghost-text) completions where we need the whole insertion at once.
101+
* @param {{apiKey:string, model:string, maxTokens:number, system:string,
102+
* messages:{role:string,content:string}[], stop?:string[], signal?:AbortSignal}} opts
103+
* @returns {Promise<string>}
104+
*/
105+
async function completeClaude(opts) {
106+
const body = {
107+
model: opts.model,
108+
max_tokens: opts.maxTokens,
109+
system: opts.system,
110+
messages: opts.messages
111+
};
112+
if (opts.stop && opts.stop.length) { body.stop_sequences = opts.stop; }
113+
const res = await fetch('https://api.anthropic.com/v1/messages', {
114+
method: 'POST',
115+
headers: {
116+
'content-type': 'application/json',
117+
'x-api-key': opts.apiKey,
118+
'anthropic-version': '2023-06-01'
119+
},
120+
body: JSON.stringify(body),
121+
signal: opts.signal
122+
});
123+
if (!res.ok) {
124+
const text = await res.text().catch(() => '');
125+
throw new Error(`Anthropic API ${res.status}: ${text || res.statusText}`);
126+
}
127+
const data = await res.json();
128+
const parts = Array.isArray(data.content) ? data.content : [];
129+
return parts.filter((p) => p && p.type === 'text').map((p) => p.text).join('');
130+
}
131+
132+
/**
133+
* Non-streaming single completion from Ollama (collects the streamed chat response).
134+
* @param {{url:string, model:string, system:string,
135+
* messages:{role:string,content:string}[], signal?:AbortSignal}} opts
136+
* @returns {Promise<string>}
137+
*/
138+
async function completeOllama(opts) {
139+
let out = '';
140+
await streamOllama({ ...opts, onDelta: (t) => { out += t; } });
141+
return out;
142+
}
143+
98144
/** Best-effort list of locally available Ollama models. Returns [] if unreachable. */
99145
async function listOllamaModels(url) {
100146
try {
@@ -108,4 +154,4 @@ async function listOllamaModels(url) {
108154
}
109155
}
110156

111-
module.exports = { streamClaude, streamOllama, listOllamaModels };
157+
module.exports = { streamClaude, streamOllama, completeClaude, completeOllama, listOllamaModels };

0 commit comments

Comments
 (0)