From 4181490794d48e0aa4494df2c03dc8cc91683ce1 Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Mon, 11 Mar 2024 22:26:43 +0800 Subject: [PATCH] added updateSubmenuItems request --- core/web/webviewProtocol.ts | 20 +++++--- extensions/vscode/src/debug/debug.ts | 26 ++++++++-- .../vscode/src/extension/vscodeExtension.ts | 40 +++++++-------- extensions/vscode/src/util/ideUtils.ts | 49 +++++++++---------- gui/src/hooks/useSubmenuContextProviders.tsx | 21 ++++++++ 5 files changed, 101 insertions(+), 55 deletions(-) diff --git a/core/web/webviewProtocol.ts b/core/web/webviewProtocol.ts index 469c9822b..85741b085 100644 --- a/core/web/webviewProtocol.ts +++ b/core/web/webviewProtocol.ts @@ -1,5 +1,6 @@ import { ContextItemWithId, + ContextSubmenuItem, ContinueRcJson, DiffLine, Problem, @@ -23,7 +24,7 @@ export type IdeProtocol = { readFile: [{ filepath: string }, string]; showDiff: [ { filepath: string; newContents: string; stepIndex: number }, - void, + void ]; diffLine: [ { @@ -32,7 +33,7 @@ export type IdeProtocol = { startLine: number; endLine: number; }, - void, + void ]; getProblems: [{ filepath: string }, Problem[]]; getBranch: [{ dir: string }, string]; @@ -43,8 +44,11 @@ export type IdeProtocol = { getDiff: [undefined, string]; getWorkspaceConfigs: [undefined, ContinueRcJson[]]; getTerminalContents: [undefined, string]; - getDebugLocals: [{threadIndex: Number}, string]; - getTopLevelCallStackSources: [{threadIndex: number, stackDepth: number}, string[]]; + getDebugLocals: [{ threadIndex: Number }, string]; + getTopLevelCallStackSources: [ + { threadIndex: number; stackDepth: number }, + string[] + ]; getAvailableThreads: [undefined, string[]]; isTelemetryEnabled: [undefined, boolean]; getUniqueId: [undefined, string]; @@ -60,7 +64,7 @@ export type WebviewProtocol = Protocol & workspacePaths: string[]; vscMachineId: string; vscMediaUrl: string; - }, + } ]; errorPopup: [{ message: string }, void]; @@ -86,7 +90,11 @@ export type ReverseWebviewProtocol = { historyIndex: number; item: ContextItemWithId; }, - void, + void + ]; + updateSubmenuItems: [ + { provider: string; submenuItems: ContextSubmenuItem[] }, + void ]; getDefaultModelTitle: [undefined, string]; newSessionWithPrompt: [{ prompt: string }, void]; diff --git a/extensions/vscode/src/debug/debug.ts b/extensions/vscode/src/debug/debug.ts index fdab0d945..623ea5c5a 100644 --- a/extensions/vscode/src/debug/debug.ts +++ b/extensions/vscode/src/debug/debug.ts @@ -1,14 +1,18 @@ import * as vscode from "vscode"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; +import { VsCodeIde } from "../ideProtocol"; export const threadStopped: Map = new Map(); // Arrays has better perf but you probably won't have thousands of threads in a single debug session -export function registerDebugTracker(webviewProtocol: VsCodeWebviewProtocol) { +export function registerDebugTracker( + webviewProtocol: VsCodeWebviewProtocol, + ide: VsCodeIde +) { vscode.debug.registerDebugAdapterTrackerFactory("*", { createDebugAdapterTracker(session: vscode.DebugSession) { return { - onDidSendMessage(message: any) { + async onDidSendMessage(message: any) { if (message.type == "event") { switch (message.event) { case "continued": @@ -29,7 +33,21 @@ export function registerDebugTracker(webviewProtocol: VsCodeWebviewProtocol) { threadStopped.set(key, false) ); - webviewProtocol?.request("refreshSubmenuItems", undefined); + webviewProtocol?.request("updateSubmenuItems", { + provider: "locals", + submenuItems: (await ide.getAvailableThreads()).map( + (thread, threadIndex) => { + const [threadId, threadName] = thread + .split(",") + .map((str) => str.trimStart()); + return { + id: `${threadIndex}`, + title: threadName, + description: threadId, + }; + } + ), + }); break; case "thread": @@ -45,7 +63,7 @@ export function registerDebugTracker(webviewProtocol: VsCodeWebviewProtocol) { break; } } - } + }, }; }, }); diff --git a/extensions/vscode/src/extension/vscodeExtension.ts b/extensions/vscode/src/extension/vscodeExtension.ts index c9aadac01..81c031a52 100644 --- a/extensions/vscode/src/extension/vscodeExtension.ts +++ b/extensions/vscode/src/extension/vscodeExtension.ts @@ -36,7 +36,7 @@ export class VsCodeExtension { // Config Handler with output channel const outputChannel = vscode.window.createOutputChannel( - "Continue - LLM Prompt/Completion", + "Continue - LLM Prompt/Completion" ); this.configHandler = new ConfigHandler( this.ide, @@ -45,19 +45,19 @@ export class VsCodeExtension { : new URL(remoteUrl), async (log: string) => { outputChannel.appendLine( - "==========================================================================", + "==========================================================================" ); outputChannel.appendLine( - "==========================================================================", + "==========================================================================" ); outputChannel.append(log); }, - () => this.webviewProtocol?.request("configUpdate", undefined), + () => this.webviewProtocol?.request("configUpdate", undefined) ); this.configHandler.reloadConfig(); this.verticalDiffManager = new VerticalPerLineDiffManager( - this.configHandler, + this.configHandler ); this.extensionContext = context; this.tabAutocompleteModel = new TabAutocompleteModel(this.configHandler); @@ -67,11 +67,11 @@ export class VsCodeExtension { this.ide, this.windowId, this.extensionContext, - this.verticalDiffManager, + this.verticalDiffManager ); try { const configSync = new RemoteConfigSync( - this.configHandler.reloadConfig.bind(this.configHandler), + this.configHandler.reloadConfig.bind(this.configHandler) ); configSync.setup(); } catch (e) { @@ -85,14 +85,14 @@ export class VsCodeExtension { this.sidebar, { webviewOptions: { retainContextWhenHidden: true }, - }, - ), + } + ) ); this.webviewProtocol = this.sidebar.webviewProtocol; // Indexing + pause token const indexingPauseToken = new PauseToken( - context.globalState.get("continue.indexingPaused") === true, + context.globalState.get("continue.indexingPaused") === true ); this.webviewProtocol.on("index/setPaused", (msg) => { context.globalState.update("continue.indexingPaused", msg.data); @@ -103,14 +103,14 @@ export class VsCodeExtension { this.indexer = new CodebaseIndexer( this.configHandler, this.ide, - indexingPauseToken, + indexingPauseToken ); // CodeLens registerAllCodeLensProviders( context, this.diffManager, - this.verticalDiffManager.editorToVerticalDiffCodeLens, + this.verticalDiffManager.editorToVerticalDiffCodeLens ); // Tab autocomplete @@ -125,9 +125,9 @@ export class VsCodeExtension { new ContinueCompletionProvider( this.configHandler, this.ide, - this.tabAutocompleteModel, - ), - ), + this.tabAutocompleteModel + ) + ) ); // Commands @@ -138,10 +138,10 @@ export class VsCodeExtension { this.sidebar, this.configHandler, this.diffManager, - this.verticalDiffManager, + this.verticalDiffManager ); - registerDebugTracker(this.webviewProtocol); + registerDebugTracker(this.webviewProtocol, this.ide); // Indexing this.ide.getWorkspaceDirs().then((dirs) => this.refreshCodebaseIndex(dirs)); @@ -189,7 +189,7 @@ export class VsCodeExtension { } }); } - }), + }) ); // Register a content provider for the readonly virtual documents @@ -207,8 +207,8 @@ export class VsCodeExtension { context.subscriptions.push( vscode.workspace.registerTextDocumentContentProvider( VsCodeExtension.continueVirtualDocumentScheme, - documentContentProvider, - ), + documentContentProvider + ) ); } diff --git a/extensions/vscode/src/util/ideUtils.ts b/extensions/vscode/src/util/ideUtils.ts index 000b22562..953bf3c25 100644 --- a/extensions/vscode/src/util/ideUtils.ts +++ b/extensions/vscode/src/util/ideUtils.ts @@ -25,12 +25,12 @@ export class VsCodeIdeUtils { async gotoDefinition( filepath: string, - position: vscode.Position, + position: vscode.Position ): Promise { const locations: vscode.Location[] = await vscode.commands.executeCommand( "vscode.executeDefinitionProvider", uriFromFilePath(filepath), - position, + position ); return locations; } @@ -38,25 +38,25 @@ export class VsCodeIdeUtils { async documentSymbol(filepath: string): Promise { return await vscode.commands.executeCommand( "vscode.executeDocumentSymbolProvider", - uriFromFilePath(filepath), + uriFromFilePath(filepath) ); } async references( filepath: string, - position: vscode.Position, + position: vscode.Position ): Promise { return await vscode.commands.executeCommand( "vscode.executeReferenceProvider", uriFromFilePath(filepath), - position, + position ); } async foldingRanges(filepath: string): Promise { return await vscode.commands.executeCommand( "vscode.executeFoldingRangeProvider", - uriFromFilePath(filepath), + uriFromFilePath(filepath) ); } @@ -80,12 +80,12 @@ export class VsCodeIdeUtils { rangeInFile.range.start.line, rangeInFile.range.start.character, rangeInFile.range.end.line, - rangeInFile.range.end.character, + rangeInFile.range.end.character ); const editor = await openEditorAndRevealRange( rangeInFile.filepath, range, - vscode.ViewColumn.One, + vscode.ViewColumn.One ); if (editor) { const decorationType = vscode.window.createTextEditorDecorationType({ @@ -100,7 +100,7 @@ export class VsCodeIdeUtils { cursorDisposable.dispose(); editor.setDecorations(decorationType, []); } - }, + } ); setTimeout(() => { @@ -123,9 +123,9 @@ export class VsCodeIdeUtils { edit.range.start.line, edit.range.start.character, edit.range.end.line, - edit.range.end.character, + edit.range.end.character ), - edit.replacement, + edit.replacement ); } @@ -134,7 +134,7 @@ export class VsCodeIdeUtils { const panel = vscode.window.createWebviewPanel( "continue.continueGUIView", "Continue", - vscode.ViewColumn.One, + vscode.ViewColumn.One ); // panel.webview.html = this.sidebar.getSidebarContent( // extensionContext, @@ -165,8 +165,8 @@ export class VsCodeIdeUtils { vscode.Uri.parse( `${ VsCodeExtension.continueVirtualDocumentScheme - }:${encodeURIComponent(name)}?${encodeURIComponent(contents)}`, - ), + }:${encodeURIComponent(name)}?${encodeURIComponent(contents)}` + ) ) .then((doc) => { vscode.window.showTextDocument(doc, { preview: false }); @@ -248,7 +248,7 @@ export class VsCodeIdeUtils { async getDirectoryContents( directory: string, - recursive: boolean, + recursive: boolean ): Promise { if (!recursive) { return ( @@ -273,7 +273,7 @@ export class VsCodeIdeUtils { gitRoot ?? directory, [], true, - gitRoot === directory ? undefined : onlyThisDirectory, + gitRoot === directory ? undefined : onlyThisDirectory )) { allFiles.push(file); } @@ -297,14 +297,14 @@ export class VsCodeIdeUtils { if (typeof contents === "undefined") { try { const fileStats = await vscode.workspace.fs.stat( - uriFromFilePath(filepath), + uriFromFilePath(filepath) ); if (fileStats.size > 10 * MAX_BYTES) { return ""; } const bytes = await vscode.workspace.fs.readFile( - uriFromFilePath(filepath), + uriFromFilePath(filepath) ); // Truncate the buffer to the first MAX_BYTES @@ -319,10 +319,10 @@ export class VsCodeIdeUtils { async readRangeInFile( filepath: string, - range: vscode.Range, + range: vscode.Range ): Promise { const contents = new TextDecoder().decode( - await vscode.workspace.fs.readFile(vscode.Uri.file(filepath)), + await vscode.workspace.fs.readFile(vscode.Uri.file(filepath)) ); const lines = contents.split("\n"); return ( @@ -336,20 +336,20 @@ export class VsCodeIdeUtils { const tempCopyBuffer = await vscode.env.clipboard.readText(); if (commands < 0) { await vscode.commands.executeCommand( - "workbench.action.terminal.selectAll", + "workbench.action.terminal.selectAll" ); } else { for (let i = 0; i < commands; i++) { await vscode.commands.executeCommand( - "workbench.action.terminal.selectToPreviousCommand", + "workbench.action.terminal.selectToPreviousCommand" ); } } await vscode.commands.executeCommand( - "workbench.action.terminal.copySelection", + "workbench.action.terminal.copySelection" ); await vscode.commands.executeCommand( - "workbench.action.terminal.clearSelection", + "workbench.action.terminal.clearSelection" ); const terminalContents = await vscode.env.clipboard.readText(); await vscode.env.clipboard.writeText(tempCopyBuffer); @@ -361,7 +361,6 @@ export class VsCodeIdeUtils { return terminalContents; } - private async _getThreads(session: vscode.DebugSession) { const threadsResponse = await session.customRequest("threads"); const threads = threadsResponse.threads.filter((thread: any) => diff --git a/gui/src/hooks/useSubmenuContextProviders.tsx b/gui/src/hooks/useSubmenuContextProviders.tsx index c4b1072e3..b0097f3ce 100644 --- a/gui/src/hooks/useSubmenuContextProviders.tsx +++ b/gui/src/hooks/useSubmenuContextProviders.tsx @@ -45,6 +45,27 @@ function useSubmenuContextProviders() { setLoaded(false); }); + useWebviewListener("updateSubmenuItems", async (data) => { + const minisearch = new MiniSearch({ + fields: ["title", "description"], + storeFields: ["id", "title", "description"], + }); + + minisearch.addAll(data.submenuItems); + + setMinisearches((prev) => ({ ...prev, [data.provider]: minisearch })); + + if (data.provider === "file") { + const openFiles = await getOpenFileItems(); + setFallbackResults((prev) => ({ ...prev, file: openFiles })); + } else { + setFallbackResults((prev) => ({ + ...prev, + [data.provider]: data.submenuItems.slice(0, 6), + })); + } + }); + function addItem(providerTitle: string, item: ContextSubmenuItem) { if (!minisearches[providerTitle]) { return;