1111const vscode = require ( 'vscode' ) ;
1212const path = require ( 'path' ) ;
1313const fs = require ( 'fs' ) ;
14+ const cp = require ( 'child_process' ) ;
1415const { streamClaude, streamOllama, completeClaude, completeOllama, listOllamaModels } = require ( './providers' ) ;
1516const { registerAiEdit } = require ( './aiEdit' ) ;
1617const { registerLmProvider } = require ( './lmProvider' ) ;
@@ -23,6 +24,8 @@ const CLAUDE_MODELS = [
2324] ;
2425
2526const 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' ] ) ;
2629const 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 - Z a - z _ ] [ A - Z a - z 0 - 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+
145318function 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' } ) ;
0 commit comments