@@ -30,6 +30,33 @@ import { focusAmazonQPanel } from '../../codewhispererChat/commands/registerComm
30
30
import { isWeb } from '../../shared/extensionGlobals'
31
31
import { getLogger } from '../../shared/logger/logger'
32
32
33
+ // Keep a reference to the active Q CLI terminal
34
+ let qCliTerminal : vscode . Terminal | undefined
35
+
36
+ // Function to initialize terminal persistence
37
+ export function initializeQCliTerminal ( ) : void {
38
+ // Auto-open the terminal when extension activates
39
+ setTimeout ( ( ) => {
40
+ try {
41
+ // Only open automatically if no terminal exists yet
42
+ if ( ! qCliTerminal ) {
43
+ const item = createOpenQCli ( )
44
+ if ( item && typeof item . onClick === 'function' ) {
45
+ item . onClick ( )
46
+ }
47
+ }
48
+ } catch ( error ) {
49
+ getLogger ( ) . error ( 'Failed to initialize Q CLI terminal: %s' , error )
50
+ }
51
+ } , 2000 )
52
+ // Listen for terminal close events to update state
53
+ vscode . window . onDidCloseTerminal ( ( terminal ) => {
54
+ if ( terminal === qCliTerminal ) {
55
+ qCliTerminal = undefined
56
+ }
57
+ } )
58
+ }
59
+
33
60
export function createAutoSuggestions ( running : boolean ) : DataQuickPickItem < 'autoSuggestions' > {
34
61
const labelResume = localize ( 'AWS.codewhisperer.resumeCodeWhispererNode.label' , 'Resume Auto-Suggestions' )
35
62
const iconResume = getIcon ( 'vscode-debug-start' )
@@ -258,6 +285,71 @@ export function switchToAmazonQNode(): DataQuickPickItem<'openChatPanel'> {
258
285
}
259
286
}
260
287
288
+ export function createOpenQCli (
289
+ customIconName ?: 'vscode-terminal' | 'vscode-code' | 'vscode-edit' | 'vscode-comment'
290
+ ) : DataQuickPickItem < 'openQCli' > {
291
+ const label = 'Open Q CLI'
292
+ const icon = getIcon ( 'vscode-comment' ) // Using comment icon as requested
293
+
294
+ return {
295
+ data : 'openQCli' ,
296
+ label : codicon `${ icon } ${ label } ` ,
297
+ // description: 'Open Q CLI with Chat',
298
+ onClick : ( ) => {
299
+ try {
300
+ // First create the terminal in the editor area
301
+ const terminal = vscode . window . createTerminal ( {
302
+ name : 'Amazon Q CLI' ,
303
+ location : vscode . TerminalLocation . Editor ,
304
+ message : `\x1b[1m\x1b[36m=== Welcome to Amazon Q CLI ===\x1b[0m\n` ,
305
+ iconPath : new vscode . ThemeIcon ( customIconName || 'vscode-comment' ) ,
306
+ } )
307
+
308
+ if ( ! terminal ) {
309
+ getLogger ( ) . error ( 'Failed to create terminal for Q CLI' )
310
+ return
311
+ }
312
+
313
+ // Store reference to the terminal
314
+ qCliTerminal = terminal
315
+
316
+ // Show the terminal
317
+ terminal . show ( )
318
+
319
+ // Split the editor and move terminal to right side, then run q chat when ready
320
+ void vscode . commands
321
+ . executeCommand ( 'workbench.action.moveEditorToRightGroup' )
322
+ // .then(() => vscode.commands.executeCommand('workbench.action.pinEditor')) // No need to pin the editor
323
+ . then ( ( ) => {
324
+ // Wait to ensure terminal is fully initialized and positioned
325
+ setTimeout ( ( ) => {
326
+ // Verify terminal still exists before sending command
327
+ if ( terminal ) {
328
+ try {
329
+ terminal . sendText ( 'q chat' )
330
+ getLogger ( ) . debug ( 'Successfully executed q chat in terminal' )
331
+ } catch ( cmdError : unknown ) {
332
+ getLogger ( ) . error (
333
+ 'Failed to execute command in terminal: %s' ,
334
+ cmdError instanceof Error ? cmdError . message : String ( cmdError )
335
+ )
336
+ }
337
+ }
338
+ } , 2000 ) // Increased delay for better reliability
339
+ } )
340
+ . then ( undefined , ( error : Error ) => {
341
+ getLogger ( ) . error ( 'Failed to position terminal: %s' , error )
342
+ } )
343
+ } catch ( error : unknown ) {
344
+ getLogger ( ) . error (
345
+ 'Failed to create terminal: %s' ,
346
+ error instanceof Error ? error . message : String ( error )
347
+ )
348
+ }
349
+ } ,
350
+ } as DataQuickPickItem < 'openQCli' >
351
+ }
352
+
261
353
export function createSignIn ( ) : DataQuickPickItem < 'signIn' > {
262
354
const label = localize ( 'AWS.codewhisperer.signInNode.label' , 'Sign in to get started' )
263
355
const icon = getIcon ( 'vscode-account' )
0 commit comments