@@ -35,6 +35,8 @@ let activeWebview;
3535let conversation = [ ] ;
3636/** @type {string | null } */
3737let 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 } */
3941let 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+
78145function 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 ) ;
0 commit comments