| 1 | /* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | import * as fs from "node:fs"; |
| 3 | |
| 4 | import { ContextMenuConfig, ILLM, ModelInstaller } from "core"; |
| 5 | import { CompletionProvider } from "core/autocomplete/CompletionProvider"; |
| 6 | import { ConfigHandler } from "core/config/ConfigHandler"; |
| 7 | import { EXTENSION_NAME } from "core/control-plane/env"; |
| 8 | import { Core } from "core/core"; |
| 9 | import { walkDirAsync } from "core/indexing/walkDir"; |
| 10 | import { isModelInstaller } from "core/llm"; |
| 11 | import { startLocalLemonade } from "core/util/lemonadeHelper"; |
| 12 | import { startLocalOllama } from "core/util/ollamaHelper"; |
| 13 | import { getConfigJsonPath, getConfigYamlPath } from "core/util/paths"; |
| 14 | import { Telemetry } from "core/util/posthog"; |
| 15 | import * as vscode from "vscode"; |
| 16 | import * as YAML from "yaml"; |
| 17 | |
| 18 | import { convertJsonToYamlConfig } from "../../../packages/config-yaml/dist"; |
| 19 | |
| 20 | import { NextEditLoggingService } from "core/nextEdit/NextEditLoggingService"; |
| 21 | import { |
| 22 | getAutocompleteStatusBarDescription, |
| 23 | getAutocompleteStatusBarTitle, |
| 24 | getNextEditMenuItems, |
| 25 | getStatusBarStatus, |
| 26 | getStatusBarStatusFromQuickPickItemLabel, |
| 27 | handleNextEditToggle, |
| 28 | isNextEditToggleLabel, |
| 29 | quickPickStatusText, |
| 30 | setupStatusBar, |
| 31 | StatusBarStatus, |
| 32 | } from "./autocomplete/statusBar"; |
| 33 | import { ContinueConsoleWebviewViewProvider } from "./ContinueConsoleWebviewViewProvider"; |
| 34 | import { ContinueGUIWebviewViewProvider } from "./ContinueGUIWebviewViewProvider"; |
| 35 | import { processDiff } from "./diff/processDiff"; |
| 36 | import { VerticalDiffManager } from "./diff/vertical/manager"; |
| 37 | import EditDecorationManager from "./quickEdit/EditDecorationManager"; |
| 38 | import { QuickEdit, QuickEditShowParams } from "./quickEdit/QuickEditQuickPick"; |
| 39 | import { |
| 40 | addCodeToContextFromRange, |
| 41 | addEntireFileToContext, |
| 42 | addHighlightedCodeToContext, |
| 43 | } from "./util/addCode"; |
| 44 | import { Battery } from "./util/battery"; |
| 45 | import { getMetaKeyLabel } from "./util/util"; |
| 46 | import { openEditorAndRevealRange } from "./util/vscode"; |
| 47 | import { VsCodeIde } from "./VsCodeIde"; |
| 48 | |
| 49 | let fullScreenPanel: vscode.WebviewPanel | undefined; |
| 50 | |
| 51 | function getFullScreenTab() { |
| 52 | const tabs = vscode.window.tabGroups.all.flatMap((tabGroup) => tabGroup.tabs); |
| 53 | return tabs.find((tab) => |
| 54 | (tab.input as any)?.viewType?.endsWith("continue.continueGUIView"), |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | type TelemetryCaptureParams = Parameters<typeof Telemetry.capture>; |
| 59 | |
| 60 | /** |
| 61 | * Helper method to add the `isCommandEvent` to all telemetry captures |
| 62 | */ |
| 63 | function captureCommandTelemetry( |
| 64 | commandName: TelemetryCaptureParams[0], |
| 65 | properties: TelemetryCaptureParams[1] = {}, |
| 66 | ) { |
| 67 | Telemetry.capture(commandName, { isCommandEvent: true, ...properties }); |
| 68 | } |
| 69 | |
| 70 | function focusGUI() { |
| 71 | const fullScreenTab = getFullScreenTab(); |
| 72 | if (fullScreenTab) { |
| 73 | // focus fullscreen |
| 74 | fullScreenPanel?.reveal(); |
| 75 | } else { |
| 76 | // focus sidebar |
| 77 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 78 | // vscode.commands.executeCommand("workbench.action.focusAuxiliaryBar"); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function hideGUI() { |
| 83 | const fullScreenTab = getFullScreenTab(); |
| 84 | if (fullScreenTab) { |
| 85 | // focus fullscreen |
| 86 | fullScreenPanel?.dispose(); |
| 87 | } else { |
| 88 | // focus sidebar |
| 89 | vscode.commands.executeCommand("workbench.action.closeAuxiliaryBar"); |
| 90 | // vscode.commands.executeCommand("workbench.action.toggleAuxiliaryBar"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | function waitForSidebarReady( |
| 95 | sidebar: ContinueGUIWebviewViewProvider, |
| 96 | timeout: number, |
| 97 | interval: number, |
| 98 | ): Promise<boolean> { |
| 99 | return new Promise((resolve) => { |
| 100 | const startTime = Date.now(); |
| 101 | |
| 102 | const checkReadyState = () => { |
| 103 | if (sidebar.isReady) { |
| 104 | resolve(true); |
| 105 | } else if (Date.now() - startTime >= timeout) { |
| 106 | resolve(false); // Timed out |
| 107 | } else { |
| 108 | setTimeout(checkReadyState, interval); |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | checkReadyState(); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | // Copy everything over from extension.ts |
| 117 | const getCommandsMap: ( |
| 118 | ide: VsCodeIde, |
| 119 | extensionContext: vscode.ExtensionContext, |
| 120 | sidebar: ContinueGUIWebviewViewProvider, |
| 121 | consoleView: ContinueConsoleWebviewViewProvider, |
| 122 | configHandler: ConfigHandler, |
| 123 | verticalDiffManager: VerticalDiffManager, |
| 124 | battery: Battery, |
| 125 | quickEdit: QuickEdit, |
| 126 | core: Core, |
| 127 | editDecorationManager: EditDecorationManager, |
| 128 | ) => { [command: string]: (...args: any) => any } = ( |
| 129 | ide, |
| 130 | extensionContext, |
| 131 | sidebar, |
| 132 | consoleView, |
| 133 | configHandler, |
| 134 | verticalDiffManager, |
| 135 | battery, |
| 136 | quickEdit, |
| 137 | core, |
| 138 | editDecorationManager, |
| 139 | ) => { |
| 140 | /** |
| 141 | * Streams an inline edit to the vertical diff manager. |
| 142 | * |
| 143 | * This function retrieves the configuration, determines the appropriate model title, |
| 144 | * increments the FTC count, and then streams an edit to the |
| 145 | * vertical diff manager. |
| 146 | * |
| 147 | * @param promptName - The key for the prompt in the context menu configuration. |
| 148 | * @param fallbackPrompt - The prompt to use if the configured prompt is not available. |
| 149 | * @param [range] - Optional. The range to edit if provided. |
| 150 | * @returns |
| 151 | */ |
| 152 | async function streamInlineEdit( |
| 153 | promptName: keyof ContextMenuConfig, |
| 154 | fallbackPrompt: string, |
| 155 | range?: vscode.Range, |
| 156 | ) { |
| 157 | const { config } = await configHandler.loadConfig(); |
| 158 | if (!config) { |
| 159 | throw new Error("Config not loaded"); |
| 160 | } |
| 161 | |
| 162 | const llm = |
| 163 | config.selectedModelByRole.edit ?? config.selectedModelByRole.chat; |
| 164 | |
| 165 | if (!llm) { |
| 166 | throw new Error("No edit or chat model selected"); |
| 167 | } |
| 168 | |
| 169 | void sidebar.webviewProtocol.request("incrementFtc", undefined); |
| 170 | |
| 171 | await verticalDiffManager.streamEdit({ |
| 172 | input: |
| 173 | config.experimental?.contextMenuPrompts?.[promptName] ?? fallbackPrompt, |
| 174 | llm, |
| 175 | range, |
| 176 | rulesToInclude: config.rules, |
| 177 | isApply: false, |
| 178 | }); |
| 179 | } |
| 180 | |
| 181 | return { |
| 182 | "continue.acceptDiff": async (newFileUri?: string, streamId?: string) => { |
| 183 | captureCommandTelemetry("acceptDiff"); |
| 184 | void processDiff( |
| 185 | "accept", |
| 186 | sidebar, |
| 187 | ide, |
| 188 | core, |
| 189 | verticalDiffManager, |
| 190 | newFileUri, |
| 191 | streamId, |
| 192 | ); |
| 193 | }, |
| 194 | |
| 195 | "continue.rejectDiff": async (newFileUri?: string, streamId?: string) => { |
| 196 | captureCommandTelemetry("rejectDiff"); |
| 197 | void processDiff( |
| 198 | "reject", |
| 199 | sidebar, |
| 200 | ide, |
| 201 | core, |
| 202 | verticalDiffManager, |
| 203 | newFileUri, |
| 204 | streamId, |
| 205 | ); |
| 206 | }, |
| 207 | "continue.acceptVerticalDiffBlock": (fileUri?: string, index?: number) => { |
| 208 | captureCommandTelemetry("acceptVerticalDiffBlock"); |
| 209 | verticalDiffManager.acceptRejectVerticalDiffBlock(true, fileUri, index); |
| 210 | }, |
| 211 | "continue.rejectVerticalDiffBlock": (fileUri?: string, index?: number) => { |
| 212 | captureCommandTelemetry("rejectVerticalDiffBlock"); |
| 213 | verticalDiffManager.acceptRejectVerticalDiffBlock(false, fileUri, index); |
| 214 | }, |
| 215 | "continue.quickFix": async ( |
| 216 | range: vscode.Range, |
| 217 | diagnosticMessage: string, |
| 218 | ) => { |
| 219 | captureCommandTelemetry("quickFix"); |
| 220 | |
| 221 | const prompt = `Please explain the cause of this error and how to solve it: ${diagnosticMessage}`; |
| 222 | |
| 223 | addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt); |
| 224 | |
| 225 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 226 | }, |
| 227 | // Passthrough for telemetry purposes |
| 228 | "continue.defaultQuickAction": async (args: QuickEditShowParams) => { |
| 229 | captureCommandTelemetry("defaultQuickAction"); |
| 230 | vscode.commands.executeCommand("continue.focusEdit", args); |
| 231 | }, |
| 232 | "continue.customQuickActionSendToChat": async ( |
| 233 | prompt: string, |
| 234 | range: vscode.Range, |
| 235 | ) => { |
| 236 | captureCommandTelemetry("customQuickActionSendToChat"); |
| 237 | |
| 238 | addCodeToContextFromRange(range, sidebar.webviewProtocol, prompt); |
| 239 | |
| 240 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 241 | }, |
| 242 | "continue.customQuickActionStreamInlineEdit": async ( |
| 243 | prompt: string, |
| 244 | range: vscode.Range, |
| 245 | ) => { |
| 246 | captureCommandTelemetry("customQuickActionStreamInlineEdit"); |
| 247 | |
| 248 | streamInlineEdit("docstring", prompt, range); |
| 249 | }, |
| 250 | "continue.codebaseForceReIndex": async () => { |
| 251 | core.invoke("index/forceReIndex", undefined); |
| 252 | }, |
| 253 | "continue.rebuildCodebaseIndex": async () => { |
| 254 | core.invoke("index/forceReIndex", { shouldClearIndexes: true }); |
| 255 | }, |
| 256 | "continue.docsIndex": async () => { |
| 257 | core.invoke("context/indexDocs", { reIndex: false }); |
| 258 | }, |
| 259 | "continue.docsReIndex": async () => { |
| 260 | core.invoke("context/indexDocs", { reIndex: true }); |
| 261 | }, |
| 262 | "continue.focusContinueInput": async () => { |
| 263 | const isContinueInputFocused = await sidebar.webviewProtocol.request( |
| 264 | "isContinueInputFocused", |
| 265 | undefined, |
| 266 | false, |
| 267 | ); |
| 268 | |
| 269 | // This is a temporary fix—sidebar.webviewProtocol.request is blocking |
| 270 | // when the GUI hasn't yet been setup and we should instead be |
| 271 | // immediately throwing an error, or returning a Result object |
| 272 | focusGUI(); |
| 273 | if (!sidebar.isReady) { |
| 274 | const isReady = await waitForSidebarReady(sidebar, 5000, 100); |
| 275 | if (!isReady) { |
| 276 | return; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | const historyLength = await sidebar.webviewProtocol.request( |
| 281 | "getWebviewHistoryLength", |
| 282 | undefined, |
| 283 | false, |
| 284 | ); |
| 285 | |
| 286 | if (isContinueInputFocused) { |
| 287 | if (historyLength === 0) { |
| 288 | hideGUI(); |
| 289 | } else { |
| 290 | void sidebar.webviewProtocol?.request( |
| 291 | "focusContinueInputWithNewSession", |
| 292 | undefined, |
| 293 | false, |
| 294 | ); |
| 295 | } |
| 296 | } else { |
| 297 | focusGUI(); |
| 298 | sidebar.webviewProtocol?.request( |
| 299 | "focusContinueInputWithNewSession", |
| 300 | undefined, |
| 301 | false, |
| 302 | ); |
| 303 | void addHighlightedCodeToContext(sidebar.webviewProtocol); |
| 304 | } |
| 305 | }, |
| 306 | "continue.focusContinueInputWithoutClear": async () => { |
| 307 | const isContinueInputFocused = await sidebar.webviewProtocol.request( |
| 308 | "isContinueInputFocused", |
| 309 | undefined, |
| 310 | false, |
| 311 | ); |
| 312 | |
| 313 | // This is a temporary fix—sidebar.webviewProtocol.request is blocking |
| 314 | // when the GUI hasn't yet been setup and we should instead be |
| 315 | // immediately throwing an error, or returning a Result object |
| 316 | focusGUI(); |
| 317 | if (!sidebar.isReady) { |
| 318 | const isReady = await waitForSidebarReady(sidebar, 5000, 100); |
| 319 | if (!isReady) { |
| 320 | return; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | if (isContinueInputFocused) { |
| 325 | hideGUI(); |
| 326 | } else { |
| 327 | focusGUI(); |
| 328 | |
| 329 | sidebar.webviewProtocol?.request( |
| 330 | "focusContinueInputWithoutClear", |
| 331 | undefined, |
| 332 | ); |
| 333 | |
| 334 | void addHighlightedCodeToContext(sidebar.webviewProtocol); |
| 335 | } |
| 336 | }, |
| 337 | // QuickEditShowParams are passed from CodeLens, temp fix |
| 338 | // until we update to new params specific to Edit |
| 339 | "continue.focusEdit": async (args?: QuickEditShowParams) => { |
| 340 | captureCommandTelemetry("focusEdit"); |
| 341 | focusGUI(); |
| 342 | sidebar.webviewProtocol?.request("focusEdit", undefined); |
| 343 | }, |
| 344 | "continue.exitEditMode": async () => { |
| 345 | captureCommandTelemetry("exitEditMode"); |
| 346 | editDecorationManager.clear(); |
| 347 | void sidebar.webviewProtocol?.request("exitEditMode", undefined); |
| 348 | }, |
| 349 | "continue.generateRule": async () => { |
| 350 | captureCommandTelemetry("generateRule"); |
| 351 | focusGUI(); |
| 352 | void sidebar.webviewProtocol?.request("generateRule", undefined); |
| 353 | }, |
| 354 | "continue.writeCommentsForCode": async () => { |
| 355 | captureCommandTelemetry("writeCommentsForCode"); |
| 356 | |
| 357 | streamInlineEdit( |
| 358 | "comment", |
| 359 | "Write comments for this code. Do not change anything about the code itself.", |
| 360 | ); |
| 361 | }, |
| 362 | "continue.writeDocstringForCode": async () => { |
| 363 | captureCommandTelemetry("writeDocstringForCode"); |
| 364 | |
| 365 | void streamInlineEdit( |
| 366 | "docstring", |
| 367 | "Write a docstring for this code. Do not change anything about the code itself.", |
| 368 | ); |
| 369 | }, |
| 370 | "continue.fixCode": async () => { |
| 371 | captureCommandTelemetry("fixCode"); |
| 372 | |
| 373 | streamInlineEdit( |
| 374 | "fix", |
| 375 | "Fix this code. If it is already 100% correct, simply rewrite the code.", |
| 376 | ); |
| 377 | }, |
| 378 | "continue.optimizeCode": async () => { |
| 379 | captureCommandTelemetry("optimizeCode"); |
| 380 | streamInlineEdit("optimize", "Optimize this code"); |
| 381 | }, |
| 382 | "continue.fixGrammar": async () => { |
| 383 | captureCommandTelemetry("fixGrammar"); |
| 384 | streamInlineEdit( |
| 385 | "fixGrammar", |
| 386 | "If there are any grammar or spelling mistakes in this writing, fix them. Do not make other large changes to the writing.", |
| 387 | ); |
| 388 | }, |
| 389 | "continue.clearConsole": async () => { |
| 390 | consoleView.clearLog(); |
| 391 | }, |
| 392 | "continue.viewLogs": async () => { |
| 393 | captureCommandTelemetry("viewLogs"); |
| 394 | vscode.commands.executeCommand("workbench.action.toggleDevTools"); |
| 395 | }, |
| 396 | "continue.debugTerminal": async () => { |
| 397 | captureCommandTelemetry("debugTerminal"); |
| 398 | |
| 399 | const terminalContents = await ide.getTerminalContents(); |
| 400 | |
| 401 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 402 | |
| 403 | sidebar.webviewProtocol?.request("userInput", { |
| 404 | input: `I got the following error, can you please help explain how to fix it?\n\n${terminalContents.trim()}`, |
| 405 | }); |
| 406 | }, |
| 407 | "continue.hideInlineTip": () => { |
| 408 | vscode.workspace |
| 409 | .getConfiguration(EXTENSION_NAME) |
| 410 | .update("showInlineTip", false, vscode.ConfigurationTarget.Global); |
| 411 | }, |
| 412 | |
| 413 | // Commands without keyboard shortcuts |
| 414 | "continue.addModel": () => { |
| 415 | captureCommandTelemetry("addModel"); |
| 416 | |
| 417 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 418 | sidebar.webviewProtocol?.request("addModel", undefined); |
| 419 | }, |
| 420 | "continue.newSession": () => { |
| 421 | sidebar.webviewProtocol?.request("newSession", undefined); |
| 422 | }, |
| 423 | |
| 424 | "continue.shareSession": async (sessionId: string | undefined) => { |
| 425 | if (!sessionId) { |
| 426 | sessionId = await sidebar.webviewProtocol?.request( |
| 427 | "getCurrentSessionId", |
| 428 | undefined, |
| 429 | ); |
| 430 | } |
| 431 | if (!sessionId) { |
| 432 | void vscode.window.showErrorMessage( |
| 433 | "No session ID found. Please start a new session first.", |
| 434 | ); |
| 435 | return; |
| 436 | } |
| 437 | //let user select the destination folder |
| 438 | const destinationFolder = await vscode.window.showOpenDialog({ |
| 439 | canSelectFolders: true, |
| 440 | canSelectFiles: false, |
| 441 | canSelectMany: false, |
| 442 | openLabel: "Select Destination Folder", |
| 443 | }); |
| 444 | if (!destinationFolder || destinationFolder.length === 0) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | try { |
| 449 | // despite core.invoke not being async, we still need to await it, because the 'history/share' command is async |
| 450 | // if not awaited, then errors will not be caught. |
| 451 | await core.invoke("history/share", { |
| 452 | id: sessionId, |
| 453 | outputDir: destinationFolder[0].fsPath, |
| 454 | }); |
| 455 | } catch (error) { |
| 456 | const errorMessage = `Failed to save session: ${error instanceof Error ? error.message : String(error)}`; |
| 457 | void vscode.window.showErrorMessage(errorMessage); |
| 458 | } |
| 459 | }, |
| 460 | "continue.viewHistory": () => { |
| 461 | vscode.commands.executeCommand("continue.navigateTo", "/history", true); |
| 462 | }, |
| 463 | "continue.focusContinueSessionId": async ( |
| 464 | sessionId: string | undefined, |
| 465 | ) => { |
| 466 | if (!sessionId) { |
| 467 | sessionId = await vscode.window.showInputBox({ |
| 468 | prompt: "Enter the Session ID", |
| 469 | }); |
| 470 | } |
| 471 | void sidebar.webviewProtocol?.request("focusContinueSessionId", { |
| 472 | sessionId, |
| 473 | }); |
| 474 | }, |
| 475 | "continue.applyCodeFromChat": () => { |
| 476 | void sidebar.webviewProtocol.request("applyCodeFromChat", undefined); |
| 477 | }, |
| 478 | "continue.openConfigPage": () => { |
| 479 | vscode.commands.executeCommand("continue.navigateTo", "/config", false); |
| 480 | }, |
| 481 | "continue.selectFilesAsContext": async ( |
| 482 | firstUri: vscode.Uri, |
| 483 | uris: vscode.Uri[], |
| 484 | ) => { |
| 485 | if (uris === undefined) { |
| 486 | throw new Error("No files were selected"); |
| 487 | } |
| 488 | |
| 489 | vscode.commands.executeCommand("continue.continueGUIView.focus"); |
| 490 | |
| 491 | for (const uri of uris) { |
| 492 | // If it's a folder, add the entire folder contents recursively by using walkDir (to ignore ignored files) |
| 493 | const isDirectory = await vscode.workspace.fs |
| 494 | .stat(uri) |
| 495 | ?.then((stat) => stat.type === vscode.FileType.Directory); |
| 496 | if (isDirectory) { |
| 497 | for await (const fileUri of walkDirAsync(uri.toString(), ide, { |
| 498 | source: "vscode continue.selectFilesAsContext command", |
| 499 | })) { |
| 500 | await addEntireFileToContext( |
| 501 | vscode.Uri.parse(fileUri), |
| 502 | sidebar.webviewProtocol, |
| 503 | ide.ideUtils, |
| 504 | ); |
| 505 | } |
| 506 | } else { |
| 507 | await addEntireFileToContext( |
| 508 | uri, |
| 509 | sidebar.webviewProtocol, |
| 510 | ide.ideUtils, |
| 511 | ); |
| 512 | } |
| 513 | } |
| 514 | }, |
| 515 | "continue.logAutocompleteOutcome": ( |
| 516 | completionId: string, |
| 517 | completionProvider: CompletionProvider, |
| 518 | ) => { |
| 519 | completionProvider.accept(completionId); |
| 520 | }, |
| 521 | "continue.logNextEditOutcomeAccept": ( |
| 522 | completionId: string, |
| 523 | nextEditLoggingService: NextEditLoggingService, |
| 524 | ) => { |
| 525 | nextEditLoggingService.accept(completionId); |
| 526 | }, |
| 527 | "continue.logNextEditOutcomeReject": ( |
| 528 | completionId: string, |
| 529 | nextEditLoggingService: NextEditLoggingService, |
| 530 | ) => { |
| 531 | nextEditLoggingService.reject(completionId); |
| 532 | }, |
| 533 | "continue.toggleTabAutocompleteEnabled": () => { |
| 534 | captureCommandTelemetry("toggleTabAutocompleteEnabled"); |
| 535 | |
| 536 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 537 | const enabled = config.get("enableTabAutocomplete"); |
| 538 | const pauseOnBattery = config.get<boolean>( |
| 539 | "pauseTabAutocompleteOnBattery", |
| 540 | ); |
| 541 | if (!pauseOnBattery || battery.isACConnected()) { |
| 542 | config.update( |
| 543 | "enableTabAutocomplete", |
| 544 | !enabled, |
| 545 | vscode.ConfigurationTarget.Global, |
| 546 | ); |
| 547 | } else { |
| 548 | if (enabled) { |
| 549 | const paused = getStatusBarStatus() === StatusBarStatus.Paused; |
| 550 | if (paused) { |
| 551 | setupStatusBar(StatusBarStatus.Enabled); |
| 552 | } else { |
| 553 | config.update( |
| 554 | "enableTabAutocomplete", |
| 555 | false, |
| 556 | vscode.ConfigurationTarget.Global, |
| 557 | ); |
| 558 | } |
| 559 | } else { |
| 560 | setupStatusBar(StatusBarStatus.Paused); |
| 561 | config.update( |
| 562 | "enableTabAutocomplete", |
| 563 | true, |
| 564 | vscode.ConfigurationTarget.Global, |
| 565 | ); |
| 566 | } |
| 567 | } |
| 568 | }, |
| 569 | "continue.forceAutocomplete": async () => { |
| 570 | captureCommandTelemetry("forceAutocomplete"); |
| 571 | |
| 572 | // 1. Explicitly hide any existing suggestion. This clears VS Code's cache for the current position. |
| 573 | await vscode.commands.executeCommand("editor.action.inlineSuggest.hide"); |
| 574 | |
| 575 | // 2. Now trigger a new one. VS Code has no cached suggestion, so it's forced to call our provider. |
| 576 | await vscode.commands.executeCommand( |
| 577 | "editor.action.inlineSuggest.trigger", |
| 578 | ); |
| 579 | }, |
| 580 | |
| 581 | "continue.openTabAutocompleteConfigMenu": async () => { |
| 582 | captureCommandTelemetry("openTabAutocompleteConfigMenu"); |
| 583 | |
| 584 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 585 | const quickPick = vscode.window.createQuickPick(); |
| 586 | |
| 587 | const { config: continueConfig } = await configHandler.loadConfig(); |
| 588 | const autocompleteModels = |
| 589 | continueConfig?.modelsByRole.autocomplete ?? []; |
| 590 | const selected = |
| 591 | continueConfig?.selectedModelByRole?.autocomplete?.title ?? undefined; |
| 592 | |
| 593 | // Toggle between Disabled, Paused, and Enabled |
| 594 | const pauseOnBattery = |
| 595 | config.get<boolean>("pauseTabAutocompleteOnBattery") && |
| 596 | !battery.isACConnected(); |
| 597 | const currentStatus = getStatusBarStatus(); |
| 598 | |
| 599 | let targetStatus: StatusBarStatus | undefined; |
| 600 | if (pauseOnBattery) { |
| 601 | // Cycle from Disabled -> Paused -> Enabled |
| 602 | targetStatus = |
| 603 | currentStatus === StatusBarStatus.Paused |
| 604 | ? StatusBarStatus.Enabled |
| 605 | : currentStatus === StatusBarStatus.Disabled |
| 606 | ? StatusBarStatus.Paused |
| 607 | : StatusBarStatus.Disabled; |
| 608 | } else { |
| 609 | // Toggle between Disabled and Enabled |
| 610 | targetStatus = |
| 611 | currentStatus === StatusBarStatus.Disabled |
| 612 | ? StatusBarStatus.Enabled |
| 613 | : StatusBarStatus.Disabled; |
| 614 | } |
| 615 | |
| 616 | const nextEditEnabled = config.get<boolean>("enableNextEdit") ?? false; |
| 617 | |
| 618 | quickPick.items = [ |
| 619 | { |
| 620 | label: "$(gear) Open settings", |
| 621 | }, |
| 622 | { |
| 623 | label: "$(comment) Open chat", |
| 624 | description: getMetaKeyLabel() + " + L", |
| 625 | }, |
| 626 | { |
| 627 | label: "$(screen-full) Open full screen chat", |
| 628 | description: |
| 629 | getMetaKeyLabel() + " + K, " + getMetaKeyLabel() + " + M", |
| 630 | }, |
| 631 | { |
| 632 | label: quickPickStatusText(targetStatus), |
| 633 | description: |
| 634 | getMetaKeyLabel() + " + K, " + getMetaKeyLabel() + " + A", |
| 635 | }, |
| 636 | ...getNextEditMenuItems(currentStatus, nextEditEnabled), |
| 637 | { |
| 638 | kind: vscode.QuickPickItemKind.Separator, |
| 639 | label: "Switch model", |
| 640 | }, |
| 641 | ...autocompleteModels.map((model) => ({ |
| 642 | label: getAutocompleteStatusBarTitle(selected, model), |
| 643 | description: getAutocompleteStatusBarDescription(selected, model), |
| 644 | })), |
| 645 | ]; |
| 646 | quickPick.onDidAccept(() => { |
| 647 | const selectedOption = quickPick.selectedItems[0].label; |
| 648 | const targetStatus = |
| 649 | getStatusBarStatusFromQuickPickItemLabel(selectedOption); |
| 650 | |
| 651 | if (targetStatus !== undefined) { |
| 652 | setupStatusBar(targetStatus); |
| 653 | config.update( |
| 654 | "enableTabAutocomplete", |
| 655 | targetStatus === StatusBarStatus.Enabled, |
| 656 | vscode.ConfigurationTarget.Global, |
| 657 | ); |
| 658 | } else if (isNextEditToggleLabel(selectedOption)) { |
| 659 | handleNextEditToggle(selectedOption, config); |
| 660 | } else if ( |
| 661 | autocompleteModels.some((model) => model.title === selectedOption) |
| 662 | ) { |
| 663 | if (core.configHandler.currentProfile?.profileDescription.id) { |
| 664 | core.invoke("config/updateSelectedModel", { |
| 665 | profileId: |
| 666 | core.configHandler.currentProfile?.profileDescription.id, |
| 667 | role: "autocomplete", |
| 668 | title: selectedOption, |
| 669 | }); |
| 670 | } |
| 671 | } else if (selectedOption === "$(comment) Open chat") { |
| 672 | vscode.commands.executeCommand("continue.focusContinueInput"); |
| 673 | } else if (selectedOption === "$(screen-full) Open full screen chat") { |
| 674 | vscode.commands.executeCommand("continue.openInNewWindow"); |
| 675 | } else if (selectedOption === "$(gear) Open settings") { |
| 676 | vscode.commands.executeCommand("continue.navigateTo", "/config"); |
| 677 | } |
| 678 | |
| 679 | quickPick.dispose(); |
| 680 | }); |
| 681 | quickPick.show(); |
| 682 | }, |
| 683 | "continue.navigateTo": (path: string, toggle: boolean) => { |
| 684 | sidebar.webviewProtocol?.request("navigateTo", { path, toggle }); |
| 685 | focusGUI(); |
| 686 | }, |
| 687 | "continue.startLocalOllama": () => { |
| 688 | startLocalOllama(ide); |
| 689 | }, |
| 690 | "continue.startLocalLemonade": () => { |
| 691 | startLocalLemonade(ide); |
| 692 | }, |
| 693 | "continue.installModel": async ( |
| 694 | modelName: string, |
| 695 | llmProvider: ILLM | undefined, |
| 696 | ) => { |
| 697 | try { |
| 698 | if (!isModelInstaller(llmProvider)) { |
| 699 | const msg = llmProvider |
| 700 | ? `LLM provider '${llmProvider.providerName}' does not support installing models` |
| 701 | : "Missing LLM Provider"; |
| 702 | throw new Error(msg); |
| 703 | } |
| 704 | await installModelWithProgress(modelName, llmProvider); |
| 705 | } catch (e) { |
| 706 | const message = e instanceof Error ? e.message : String(e); |
| 707 | vscode.window.showErrorMessage( |
| 708 | `Failed to install '${modelName}': ${message}`, |
| 709 | ); |
| 710 | } |
| 711 | }, |
| 712 | "continue.convertConfigJsonToConfigYaml": async () => { |
| 713 | const configJson = fs.readFileSync(getConfigJsonPath(), "utf-8"); |
| 714 | const parsed = JSON.parse(configJson); |
| 715 | const configYaml = convertJsonToYamlConfig(parsed); |
| 716 | |
| 717 | const configYamlPath = getConfigYamlPath(); |
| 718 | fs.writeFileSync(configYamlPath, YAML.stringify(configYaml)); |
| 719 | |
| 720 | // Open config.yaml |
| 721 | await openEditorAndRevealRange( |
| 722 | vscode.Uri.file(configYamlPath), |
| 723 | undefined, |
| 724 | undefined, |
| 725 | false, |
| 726 | ); |
| 727 | |
| 728 | void vscode.window |
| 729 | .showInformationMessage( |
| 730 | "Your config.json has been converted to the new config.yaml format. If you need to switch back to config.json, you can delete or rename config.yaml.", |
| 731 | "Read the docs", |
| 732 | ) |
| 733 | .then(async (selection) => { |
| 734 | if (selection === "Read the docs") { |
| 735 | await vscode.env.openExternal( |
| 736 | vscode.Uri.parse("https://docs.continue.dev/yaml-migration"), |
| 737 | ); |
| 738 | } |
| 739 | }); |
| 740 | }, |
| 741 | "continue.enterEnterpriseLicenseKey": async () => { |
| 742 | captureCommandTelemetry("enterEnterpriseLicenseKey"); |
| 743 | |
| 744 | const licenseKey = await vscode.window.showInputBox({ |
| 745 | prompt: "Enter your enterprise license key", |
| 746 | password: true, |
| 747 | ignoreFocusOut: true, |
| 748 | placeHolder: "License key", |
| 749 | }); |
| 750 | |
| 751 | if (!licenseKey) { |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | try { |
| 756 | const isValid = core.invoke("mdm/setLicenseKey", { |
| 757 | licenseKey, |
| 758 | }); |
| 759 | |
| 760 | if (isValid) { |
| 761 | void vscode.window.showInformationMessage( |
| 762 | "Enterprise license key successfully validated and saved. Reloading window.", |
| 763 | ); |
| 764 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 765 | await vscode.commands.executeCommand("workbench.action.reloadWindow"); |
| 766 | } else { |
| 767 | void vscode.window.showErrorMessage( |
| 768 | "Invalid license key. Please check your license key and try again.", |
| 769 | ); |
| 770 | } |
| 771 | } catch (error) { |
| 772 | void vscode.window.showErrorMessage( |
| 773 | `Failed to set enterprise license key: ${error instanceof Error ? error.message : String(error)}`, |
| 774 | ); |
| 775 | } |
| 776 | }, |
| 777 | "continue.toggleNextEditEnabled": async () => { |
| 778 | captureCommandTelemetry("toggleNextEditEnabled"); |
| 779 | |
| 780 | const config = vscode.workspace.getConfiguration(EXTENSION_NAME); |
| 781 | const tabAutocompleteEnabled = config.get<boolean>( |
| 782 | "enableTabAutocomplete", |
| 783 | ); |
| 784 | |
| 785 | if (!tabAutocompleteEnabled) { |
| 786 | vscode.window.showInformationMessage( |
| 787 | "Please enable tab autocomplete first to use Next Edit", |
| 788 | ); |
| 789 | return; |
| 790 | } |
| 791 | |
| 792 | const nextEditEnabled = config.get<boolean>("enableNextEdit") ?? false; |
| 793 | |
| 794 | // updateNextEditState in VsCodeExtension.ts will handle the validation. |
| 795 | config.update( |
| 796 | "enableNextEdit", |
| 797 | !nextEditEnabled, |
| 798 | vscode.ConfigurationTarget.Global, |
| 799 | ); |
| 800 | }, |
| 801 | "continue.openInNewWindow": async () => { |
| 802 | focusGUI(); |
| 803 | |
| 804 | const sessionId = await sidebar.webviewProtocol.request( |
| 805 | "getCurrentSessionId", |
| 806 | undefined, |
| 807 | ); |
| 808 | // Check if full screen is already open by checking open tabs |
| 809 | const fullScreenTab = getFullScreenTab(); |
| 810 | |
| 811 | if (fullScreenTab && fullScreenPanel) { |
| 812 | // Full screen open, but not focused - focus it |
| 813 | fullScreenPanel.reveal(); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | // Clear the sidebar to prevent overwriting changes made in fullscreen |
| 818 | vscode.commands.executeCommand("continue.newSession"); |
| 819 | |
| 820 | // Full screen not open - open it |
| 821 | captureCommandTelemetry("openInNewWindow"); |
| 822 | |
| 823 | // Create the full screen panel |
| 824 | let panel = vscode.window.createWebviewPanel( |
| 825 | "continue.continueGUIView", |
| 826 | "Continue", |
| 827 | vscode.ViewColumn.One, |
| 828 | { |
| 829 | retainContextWhenHidden: true, |
| 830 | enableScripts: true, |
| 831 | }, |
| 832 | ); |
| 833 | fullScreenPanel = panel; |
| 834 | |
| 835 | // Add content to the panel |
| 836 | panel.webview.html = sidebar.getSidebarContent( |
| 837 | extensionContext, |
| 838 | panel, |
| 839 | undefined, |
| 840 | undefined, |
| 841 | true, |
| 842 | ); |
| 843 | |
| 844 | const sessionLoader = panel.onDidChangeViewState(() => { |
| 845 | vscode.commands.executeCommand("continue.newSession"); |
| 846 | if (sessionId) { |
| 847 | vscode.commands.executeCommand( |
| 848 | "continue.focusContinueSessionId", |
| 849 | sessionId, |
| 850 | ); |
| 851 | } |
| 852 | panel.reveal(); |
| 853 | sessionLoader.dispose(); |
| 854 | }); |
| 855 | |
| 856 | // When panel closes, reset the webview and focus |
| 857 | panel.onDidDispose( |
| 858 | () => { |
| 859 | sidebar.resetWebviewProtocolWebview(); |
| 860 | vscode.commands.executeCommand("continue.focusContinueInput"); |
| 861 | }, |
| 862 | null, |
| 863 | extensionContext.subscriptions, |
| 864 | ); |
| 865 | |
| 866 | vscode.commands.executeCommand("workbench.action.copyEditorToNewWindow"); |
| 867 | vscode.commands.executeCommand("workbench.action.closeAuxiliaryBar"); |
| 868 | }, |
| 869 | "continue.forceNextEdit": async () => { |
| 870 | captureCommandTelemetry("forceNextEdit"); |
| 871 | |
| 872 | // This is basically the same logic as forceAutocomplete. |
| 873 | // I'm writing a new command KV pair here in case we diverge in features. |
| 874 | |
| 875 | await vscode.commands.executeCommand("editor.action.inlineSuggest.hide"); |
| 876 | |
| 877 | await vscode.commands.executeCommand( |
| 878 | "editor.action.inlineSuggest.trigger", |
| 879 | ); |
| 880 | }, |
| 881 | }; |
| 882 | }; |
| 883 | |
| 884 | async function installModelWithProgress( |
| 885 | modelName: string, |
| 886 | modelInstaller: ModelInstaller, |
| 887 | ) { |
| 888 | return vscode.window.withProgress( |
| 889 | { |
| 890 | location: vscode.ProgressLocation.Notification, |
| 891 | title: `Installing model '${modelName}'`, |
| 892 | cancellable: true, |
| 893 | }, |
| 894 | async (windowProgress, token) => { |
| 895 | let currentProgress: number = 0; |
| 896 | const progressWrapper = ( |
| 897 | details: string, |
| 898 | worked?: number, |
| 899 | total?: number, |
| 900 | ) => { |
| 901 | let increment = 0; |
| 902 | if (worked && total) { |
| 903 | const progressValue = Math.round((worked / total) * 100); |
| 904 | increment = progressValue - currentProgress; |
| 905 | currentProgress = progressValue; |
| 906 | } |
| 907 | windowProgress.report({ message: details, increment }); |
| 908 | }; |
| 909 | const abortController = new AbortController(); |
| 910 | token.onCancellationRequested(() => { |
| 911 | console.log(`Pulling ${modelName} model was cancelled`); |
| 912 | abortController.abort(); |
| 913 | }); |
| 914 | await modelInstaller.installModel( |
| 915 | modelName, |
| 916 | abortController.signal, |
| 917 | progressWrapper, |
| 918 | ); |
| 919 | }, |
| 920 | ); |
| 921 | } |
| 922 | |
| 923 | export function registerAllCommands( |
| 924 | context: vscode.ExtensionContext, |
| 925 | ide: VsCodeIde, |
| 926 | extensionContext: vscode.ExtensionContext, |
| 927 | sidebar: ContinueGUIWebviewViewProvider, |
| 928 | consoleView: ContinueConsoleWebviewViewProvider, |
| 929 | configHandler: ConfigHandler, |
| 930 | verticalDiffManager: VerticalDiffManager, |
| 931 | battery: Battery, |
| 932 | quickEdit: QuickEdit, |
| 933 | core: Core, |
| 934 | editDecorationManager: EditDecorationManager, |
| 935 | ) { |
| 936 | for (const [command, callback] of Object.entries( |
| 937 | getCommandsMap( |
| 938 | ide, |
| 939 | extensionContext, |
| 940 | sidebar, |
| 941 | consoleView, |
| 942 | configHandler, |
| 943 | verticalDiffManager, |
| 944 | battery, |
| 945 | quickEdit, |
| 946 | core, |
| 947 | editDecorationManager, |
| 948 | ), |
| 949 | )) { |
| 950 | context.subscriptions.push( |
| 951 | vscode.commands.registerCommand(command, callback), |
| 952 | ); |
| 953 | } |
| 954 | } |