| 1 | import { ConfigHandler } from "core/config/ConfigHandler"; |
| 2 | import { DataLogger } from "core/data/log"; |
| 3 | import { EDIT_MODE_STREAM_ID } from "core/edit/constants"; |
| 4 | import { |
| 5 | FromCoreProtocol, |
| 6 | FromWebviewProtocol, |
| 7 | ToCoreProtocol, |
| 8 | } from "core/protocol"; |
| 9 | import { ToWebviewFromCoreProtocol } from "core/protocol/coreWebview"; |
| 10 | import { ToIdeFromWebviewOrCoreProtocol } from "core/protocol/ide"; |
| 11 | import { ToIdeFromCoreProtocol } from "core/protocol/ideCore"; |
| 12 | import { InProcessMessenger, Message } from "core/protocol/messenger"; |
| 13 | import { |
| 14 | CORE_TO_WEBVIEW_PASS_THROUGH, |
| 15 | WEBVIEW_TO_CORE_PASS_THROUGH, |
| 16 | } from "core/protocol/passThrough"; |
| 17 | import { stripImages } from "core/util/messageContent"; |
| 18 | import * as vscode from "vscode"; |
| 19 | |
| 20 | import { ApplyManager } from "../apply"; |
| 21 | import { VerticalDiffManager } from "../diff/vertical/manager"; |
| 22 | import { addCurrentSelectionToEdit } from "../quickEdit/AddCurrentSelection"; |
| 23 | import EditDecorationManager from "../quickEdit/EditDecorationManager"; |
| 24 | import { |
| 25 | getControlPlaneSessionInfo, |
| 26 | WorkOsAuthProvider, |
| 27 | } from "../stubs/WorkOsAuthProvider"; |
| 28 | import { handleLLMError } from "../util/errorHandling"; |
| 29 | import { showTutorial } from "../util/tutorial"; |
| 30 | import { getExtensionUri } from "../util/vscode"; |
| 31 | import { VsCodeIde } from "../VsCodeIde"; |
| 32 | import { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 33 | |
| 34 | import { VsCodeExtension } from "./VsCodeExtension"; |
| 35 | |
| 36 | type ToIdeOrWebviewFromCoreProtocol = ToIdeFromCoreProtocol & |
| 37 | ToWebviewFromCoreProtocol; |
| 38 | |
| 39 | /** |
| 40 | * A shared messenger class between Core and Webview |
| 41 | * so we don't have to rewrite some of the handlers |
| 42 | */ |
| 43 | export class VsCodeMessenger { |
| 44 | onWebview<T extends keyof FromWebviewProtocol>( |
| 45 | messageType: T, |
| 46 | handler: ( |
| 47 | message: Message<FromWebviewProtocol[T][0]>, |
| 48 | ) => Promise<FromWebviewProtocol[T][1]> | FromWebviewProtocol[T][1], |
| 49 | ): void { |
| 50 | void this.webviewProtocol.on(messageType, handler); |
| 51 | } |
| 52 | |
| 53 | onCore<T extends keyof ToIdeOrWebviewFromCoreProtocol>( |
| 54 | messageType: T, |
| 55 | handler: ( |
| 56 | message: Message<ToIdeOrWebviewFromCoreProtocol[T][0]>, |
| 57 | ) => |
| 58 | | Promise<ToIdeOrWebviewFromCoreProtocol[T][1]> |
| 59 | | ToIdeOrWebviewFromCoreProtocol[T][1], |
| 60 | ): void { |
| 61 | this.inProcessMessenger.externalOn(messageType, handler); |
| 62 | } |
| 63 | |
| 64 | onWebviewOrCore<T extends keyof ToIdeFromWebviewOrCoreProtocol>( |
| 65 | messageType: T, |
| 66 | handler: ( |
| 67 | message: Message<ToIdeFromWebviewOrCoreProtocol[T][0]>, |
| 68 | ) => |
| 69 | | Promise<ToIdeFromWebviewOrCoreProtocol[T][1]> |
| 70 | | ToIdeFromWebviewOrCoreProtocol[T][1], |
| 71 | ): void { |
| 72 | this.onWebview(messageType, handler); |
| 73 | this.onCore(messageType, handler); |
| 74 | } |
| 75 | |
| 76 | constructor( |
| 77 | private readonly inProcessMessenger: InProcessMessenger< |
| 78 | ToCoreProtocol, |
| 79 | FromCoreProtocol |
| 80 | >, |
| 81 | private readonly webviewProtocol: VsCodeWebviewProtocol, |
| 82 | private readonly ide: VsCodeIde, |
| 83 | private readonly verticalDiffManagerPromise: Promise<VerticalDiffManager>, |
| 84 | private readonly configHandlerPromise: Promise<ConfigHandler>, |
| 85 | private readonly workOsAuthProvider: WorkOsAuthProvider, |
| 86 | private readonly editDecorationManager: EditDecorationManager, |
| 87 | private readonly context: vscode.ExtensionContext, |
| 88 | private readonly vsCodeExtension: VsCodeExtension, |
| 89 | ) { |
| 90 | /** WEBVIEW ONLY LISTENERS **/ |
| 91 | this.onWebview("showFile", (msg) => { |
| 92 | this.ide.openFile(msg.data.filepath); |
| 93 | }); |
| 94 | |
| 95 | this.onWebview("vscode/openMoveRightMarkdown", (msg) => { |
| 96 | vscode.commands.executeCommand( |
| 97 | "markdown.showPreview", |
| 98 | vscode.Uri.joinPath( |
| 99 | getExtensionUri(), |
| 100 | "media", |
| 101 | "move-chat-panel-right.md", |
| 102 | ), |
| 103 | ); |
| 104 | }); |
| 105 | |
| 106 | this.onWebview("toggleDevTools", (msg) => { |
| 107 | vscode.commands.executeCommand("continue.viewLogs"); |
| 108 | }); |
| 109 | |
| 110 | this.onWebview("reloadWindow", (msg) => { |
| 111 | vscode.commands.executeCommand("workbench.action.reloadWindow"); |
| 112 | }); |
| 113 | this.onWebview("focusEditor", (msg) => { |
| 114 | vscode.commands.executeCommand("workbench.action.focusActiveEditorGroup"); |
| 115 | }); |
| 116 | this.onWebview("toggleFullScreen", (msg) => { |
| 117 | vscode.commands.executeCommand("continue.openInNewWindow"); |
| 118 | }); |
| 119 | |
| 120 | this.onWebview("acceptDiff", async ({ data: { filepath, streamId } }) => { |
| 121 | await vscode.commands.executeCommand( |
| 122 | "continue.acceptDiff", |
| 123 | filepath, |
| 124 | streamId, |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | this.onWebview("rejectDiff", async ({ data: { filepath, streamId } }) => { |
| 129 | await vscode.commands.executeCommand( |
| 130 | "continue.rejectDiff", |
| 131 | filepath, |
| 132 | streamId, |
| 133 | ); |
| 134 | }); |
| 135 | |
| 136 | this.onWebview("applyToFile", async ({ data }) => { |
| 137 | const [verticalDiffManager, configHandler] = await Promise.all([ |
| 138 | verticalDiffManagerPromise, |
| 139 | configHandlerPromise, |
| 140 | ]); |
| 141 | |
| 142 | const applyManager = new ApplyManager( |
| 143 | this.ide, |
| 144 | webviewProtocol, |
| 145 | verticalDiffManager, |
| 146 | configHandler, |
| 147 | ); |
| 148 | |
| 149 | await applyManager.applyToFile(data); |
| 150 | }); |
| 151 | |
| 152 | this.onWebview("showTutorial", async (msg) => { |
| 153 | await showTutorial(this.ide); |
| 154 | }); |
| 155 | |
| 156 | this.onWebview( |
| 157 | "overwriteFile", |
| 158 | async ({ data: { prevFileContent, filepath } }) => { |
| 159 | if (prevFileContent === null) { |
| 160 | // TODO: Delete the file |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | await this.ide.openFile(filepath); |
| 165 | |
| 166 | // Get active text editor |
| 167 | const editor = vscode.window.activeTextEditor; |
| 168 | |
| 169 | if (!editor) { |
| 170 | vscode.window.showErrorMessage("No active editor to apply edits to"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | editor.edit((builder) => |
| 175 | builder.replace( |
| 176 | new vscode.Range( |
| 177 | editor.document.positionAt(0), |
| 178 | editor.document.positionAt(editor.document.getText().length), |
| 179 | ), |
| 180 | prevFileContent, |
| 181 | ), |
| 182 | ); |
| 183 | }, |
| 184 | ); |
| 185 | |
| 186 | this.onWebview("insertAtCursor", async (msg) => { |
| 187 | const editor = vscode.window.activeTextEditor; |
| 188 | if (editor === undefined || !editor.selection) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | editor.edit((editBuilder) => { |
| 193 | editBuilder.replace( |
| 194 | new vscode.Range(editor.selection.start, editor.selection.end), |
| 195 | msg.data.text, |
| 196 | ); |
| 197 | }); |
| 198 | }); |
| 199 | this.onWebview("edit/addCurrentSelection", async (msg) => { |
| 200 | const verticalDiffManager = await this.verticalDiffManagerPromise; |
| 201 | await addCurrentSelectionToEdit({ |
| 202 | args: undefined, |
| 203 | editDecorationManager, |
| 204 | webviewProtocol: this.webviewProtocol, |
| 205 | verticalDiffManager, |
| 206 | }); |
| 207 | }); |
| 208 | this.onWebview("edit/sendPrompt", async (msg) => { |
| 209 | const prompt = msg.data.prompt; |
| 210 | const { start, end } = msg.data.range.range; |
| 211 | const verticalDiffManager = await verticalDiffManagerPromise; |
| 212 | |
| 213 | const configHandler = await configHandlerPromise; |
| 214 | const { config } = await configHandler.loadConfig(); |
| 215 | |
| 216 | if (!config) { |
| 217 | throw new Error("Edit: Failed to load config"); |
| 218 | } |
| 219 | |
| 220 | const model = |
| 221 | config?.selectedModelByRole.edit ?? config?.selectedModelByRole.chat; |
| 222 | |
| 223 | if (!model) { |
| 224 | throw new Error("No Edit or Chat model selected"); |
| 225 | } |
| 226 | |
| 227 | const fileAfterEdit = await verticalDiffManager.streamEdit({ |
| 228 | input: stripImages(prompt), |
| 229 | llm: model, |
| 230 | streamId: EDIT_MODE_STREAM_ID, |
| 231 | range: new vscode.Range( |
| 232 | new vscode.Position(start.line, start.character), |
| 233 | new vscode.Position(end.line, end.character), |
| 234 | ), |
| 235 | rulesToInclude: config.rules, |
| 236 | isApply: false, |
| 237 | }); |
| 238 | |
| 239 | // Log dev data |
| 240 | await DataLogger.getInstance().logDevData({ |
| 241 | name: "editInteraction", |
| 242 | data: { |
| 243 | prompt: stripImages(prompt), |
| 244 | completion: fileAfterEdit ?? "", |
| 245 | modelProvider: model.underlyingProviderName, |
| 246 | modelName: model.title ?? "", |
| 247 | modelTitle: model.title ?? "", |
| 248 | filepath: msg.data.range.filepath, |
| 249 | }, |
| 250 | }); |
| 251 | |
| 252 | return fileAfterEdit; |
| 253 | }); |
| 254 | |
| 255 | this.onWebview("edit/clearDecorations", async (msg) => { |
| 256 | editDecorationManager.clear(); |
| 257 | }); |
| 258 | |
| 259 | this.onWebview("session/share", async (msg) => { |
| 260 | await vscode.commands.executeCommand( |
| 261 | "continue.shareSession", |
| 262 | msg.data.sessionId, |
| 263 | ); |
| 264 | }); |
| 265 | |
| 266 | /** PASS THROUGH FROM WEBVIEW TO CORE AND BACK **/ |
| 267 | WEBVIEW_TO_CORE_PASS_THROUGH.forEach((messageType) => { |
| 268 | this.onWebview(messageType, async (msg) => { |
| 269 | return await this.inProcessMessenger.externalRequest( |
| 270 | messageType, |
| 271 | msg.data, |
| 272 | msg.messageId, |
| 273 | ); |
| 274 | }); |
| 275 | }); |
| 276 | |
| 277 | /** PASS THROUGH FROM CORE TO WEBVIEW AND BACK **/ |
| 278 | CORE_TO_WEBVIEW_PASS_THROUGH.forEach((messageType) => { |
| 279 | this.onCore(messageType, async (msg) => { |
| 280 | return this.webviewProtocol.request(messageType, msg.data); |
| 281 | }); |
| 282 | }); |
| 283 | |
| 284 | /** CORE ONLY LISTENERS **/ |
| 285 | // None right now |
| 286 | |
| 287 | /** BOTH CORE AND WEBVIEW **/ |
| 288 | this.onWebviewOrCore("readRangeInFile", async (msg) => { |
| 289 | return await vscode.workspace |
| 290 | .openTextDocument(msg.data.filepath) |
| 291 | .then((document) => { |
| 292 | const start = new vscode.Position(0, 0); |
| 293 | const end = new vscode.Position(5, 0); |
| 294 | const range = new vscode.Range(start, end); |
| 295 | |
| 296 | const contents = document.getText(range); |
| 297 | return contents; |
| 298 | }); |
| 299 | }); |
| 300 | |
| 301 | this.onWebviewOrCore("getIdeSettings", async (msg) => { |
| 302 | return ide.getIdeSettings(); |
| 303 | }); |
| 304 | this.onWebviewOrCore("getDiff", async (msg) => { |
| 305 | return ide.getDiff(msg.data.includeUnstaged); |
| 306 | }); |
| 307 | this.onWebviewOrCore("getTerminalContents", async (msg) => { |
| 308 | return ide.getTerminalContents(); |
| 309 | }); |
| 310 | this.onWebviewOrCore("getDebugLocals", async (msg) => { |
| 311 | return ide.getDebugLocals(Number(msg.data.threadIndex)); |
| 312 | }); |
| 313 | this.onWebviewOrCore("getAvailableThreads", async (msg) => { |
| 314 | return ide.getAvailableThreads(); |
| 315 | }); |
| 316 | this.onWebviewOrCore("getTopLevelCallStackSources", async (msg) => { |
| 317 | return ide.getTopLevelCallStackSources( |
| 318 | msg.data.threadIndex, |
| 319 | msg.data.stackDepth, |
| 320 | ); |
| 321 | }); |
| 322 | this.onWebviewOrCore("getWorkspaceDirs", async (msg) => { |
| 323 | return ide.getWorkspaceDirs(); |
| 324 | }); |
| 325 | this.onWebviewOrCore("writeFile", async (msg) => { |
| 326 | return ide.writeFile(msg.data.path, msg.data.contents); |
| 327 | }); |
| 328 | this.onWebviewOrCore("showVirtualFile", async (msg) => { |
| 329 | return ide.showVirtualFile(msg.data.name, msg.data.content); |
| 330 | }); |
| 331 | this.onWebviewOrCore("openFile", async (msg) => { |
| 332 | return ide.openFile(msg.data.path); |
| 333 | }); |
| 334 | this.onWebviewOrCore("runCommand", async (msg) => { |
| 335 | await ide.runCommand(msg.data.command); |
| 336 | }); |
| 337 | this.onWebviewOrCore("getSearchResults", async (msg) => { |
| 338 | return ide.getSearchResults(msg.data.query, msg.data.maxResults); |
| 339 | }); |
| 340 | this.onWebviewOrCore("getFileResults", async (msg) => { |
| 341 | return ide.getFileResults(msg.data.pattern, msg.data.maxResults); |
| 342 | }); |
| 343 | this.onWebviewOrCore("subprocess", async (msg) => { |
| 344 | return ide.subprocess(msg.data.command, msg.data.cwd); |
| 345 | }); |
| 346 | this.onWebviewOrCore("getProblems", async (msg) => { |
| 347 | return ide.getProblems(msg.data.filepath); |
| 348 | }); |
| 349 | this.onWebviewOrCore("getBranch", async (msg) => { |
| 350 | const { dir } = msg.data; |
| 351 | return ide.getBranch(dir); |
| 352 | }); |
| 353 | this.onWebviewOrCore("getOpenFiles", async (msg) => { |
| 354 | return ide.getOpenFiles(); |
| 355 | }); |
| 356 | this.onWebviewOrCore("getCurrentFile", async () => { |
| 357 | return ide.getCurrentFile(); |
| 358 | }); |
| 359 | this.onWebviewOrCore("getPinnedFiles", async (msg) => { |
| 360 | return ide.getPinnedFiles(); |
| 361 | }); |
| 362 | this.onWebviewOrCore("showLines", async (msg) => { |
| 363 | const { filepath, startLine, endLine } = msg.data; |
| 364 | return ide.showLines(filepath, startLine, endLine); |
| 365 | }); |
| 366 | this.onWebviewOrCore("showToast", (msg) => { |
| 367 | this.ide.showToast(...msg.data); |
| 368 | }); |
| 369 | this.onWebviewOrCore("getControlPlaneSessionInfo", async (msg) => { |
| 370 | return getControlPlaneSessionInfo( |
| 371 | msg.data.silent, |
| 372 | msg.data.useOnboarding, |
| 373 | ); |
| 374 | }); |
| 375 | this.onWebviewOrCore("logoutOfControlPlane", async (msg) => { |
| 376 | const sessions = await this.workOsAuthProvider.getSessions(); |
| 377 | await Promise.all( |
| 378 | sessions.map((session) => workOsAuthProvider.removeSession(session.id)), |
| 379 | ); |
| 380 | vscode.commands.executeCommand( |
| 381 | "setContext", |
| 382 | "continue.isSignedInToControlPlane", |
| 383 | false, |
| 384 | ); |
| 385 | }); |
| 386 | this.onWebviewOrCore("saveFile", async (msg) => { |
| 387 | return await ide.saveFile(msg.data.filepath); |
| 388 | }); |
| 389 | this.onWebviewOrCore("readFile", async (msg) => { |
| 390 | return await ide.readFile(msg.data.filepath); |
| 391 | }); |
| 392 | this.onWebviewOrCore("openUrl", (msg) => { |
| 393 | vscode.env.openExternal(vscode.Uri.parse(msg.data)); |
| 394 | }); |
| 395 | |
| 396 | this.onWebviewOrCore("fileExists", async (msg) => { |
| 397 | return await ide.fileExists(msg.data.filepath); |
| 398 | }); |
| 399 | |
| 400 | this.onWebviewOrCore("gotoDefinition", async (msg) => { |
| 401 | return await ide.gotoDefinition(msg.data.location); |
| 402 | }); |
| 403 | |
| 404 | this.onWebviewOrCore("getReferences", async (msg) => { |
| 405 | return await ide.getReferences(msg.data.location); |
| 406 | }); |
| 407 | |
| 408 | this.onWebviewOrCore("getDocumentSymbols", async (msg) => { |
| 409 | return await ide.getDocumentSymbols(msg.data.textDocumentIdentifier); |
| 410 | }); |
| 411 | |
| 412 | this.onWebviewOrCore("getFileStats", async (msg) => { |
| 413 | return await ide.getFileStats(msg.data.files); |
| 414 | }); |
| 415 | |
| 416 | this.onWebviewOrCore("getGitRootPath", async (msg) => { |
| 417 | return await ide.getGitRootPath(msg.data.dir); |
| 418 | }); |
| 419 | |
| 420 | this.onWebviewOrCore("listDir", async (msg) => { |
| 421 | return await ide.listDir(msg.data.dir); |
| 422 | }); |
| 423 | |
| 424 | this.onWebviewOrCore("getRepoName", async (msg) => { |
| 425 | return await ide.getRepoName(msg.data.dir); |
| 426 | }); |
| 427 | |
| 428 | this.onWebviewOrCore("getTags", async (msg) => { |
| 429 | return await ide.getTags(msg.data); |
| 430 | }); |
| 431 | |
| 432 | this.onWebviewOrCore("getIdeInfo", async (msg) => { |
| 433 | return await ide.getIdeInfo(); |
| 434 | }); |
| 435 | |
| 436 | this.onWebviewOrCore("isTelemetryEnabled", async (msg) => { |
| 437 | return await ide.isTelemetryEnabled(); |
| 438 | }); |
| 439 | |
| 440 | this.onWebviewOrCore("getUniqueId", async (msg) => { |
| 441 | return await ide.getUniqueId(); |
| 442 | }); |
| 443 | |
| 444 | this.onWebviewOrCore("reportError", async (msg) => { |
| 445 | await handleLLMError(msg.data); |
| 446 | }); |
| 447 | } |
| 448 | } |