Skip to content

Commit 5529509

Browse files
committed
feat: poc for cli integration
1 parent 97bef44 commit 5529509

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

packages/core/src/codewhisperer/activation.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import { syncSecurityIssueWebview } from './views/securityIssue/securityIssueWeb
8585
import { detectCommentAboveLine } from '../shared/utilities/commentUtils'
8686
import { activateEditTracking } from './nextEditPrediction/activation'
8787
import { notifySelectDeveloperProfile } from './region/utils'
88+
import { initializeQCliTerminal } from './ui/codeWhispererNodes'
8889

8990
let localize: nls.LocalizeFunc
9091

@@ -484,6 +485,9 @@ export async function activate(context: ExtContext): Promise<void> {
484485
}
485486

486487
activateEditTracking(context)
488+
489+
// Initialize the Q CLI terminal with automatic opening
490+
initializeQCliTerminal()
487491
}
488492

489493
export async function shutdown() {

packages/core/src/codewhisperer/ui/codeWhispererNodes.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,33 @@ import { focusAmazonQPanel } from '../../codewhispererChat/commands/registerComm
3030
import { isWeb } from '../../shared/extensionGlobals'
3131
import { getLogger } from '../../shared/logger/logger'
3232

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+
3360
export function createAutoSuggestions(running: boolean): DataQuickPickItem<'autoSuggestions'> {
3461
const labelResume = localize('AWS.codewhisperer.resumeCodeWhispererNode.label', 'Resume Auto-Suggestions')
3562
const iconResume = getIcon('vscode-debug-start')
@@ -258,6 +285,71 @@ export function switchToAmazonQNode(): DataQuickPickItem<'openChatPanel'> {
258285
}
259286
}
260287

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+
261353
export function createSignIn(): DataQuickPickItem<'signIn'> {
262354
const label = localize('AWS.codewhisperer.signInNode.label', 'Sign in to get started')
263355
const icon = getIcon('vscode-account')

packages/core/src/codewhisperer/ui/statusBarMenu.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
switchToAmazonQNode,
2424
createSecurityScan,
2525
createSelectRegionProfileNode,
26+
createOpenQCli,
2627
} from './codeWhispererNodes'
2728
import { hasVendedIamCredentials, hasVendedCredentialsFromMetadata } from '../../auth/auth'
2829
import { AuthUtil } from '../util/authUtil'
@@ -82,6 +83,7 @@ function getAmazonQCodeWhispererNodes() {
8283
? [createSelectCustomization()]
8384
: []),
8485
switchToAmazonQNode(),
86+
createOpenQCli(),
8587
]
8688
}
8789

0 commit comments

Comments
 (0)