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