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