From dacd9fb291f389e30d7e157caee1a9c9281d5031 Mon Sep 17 00:00:00 2001 From: commandblock2 Date: Mon, 11 Mar 2024 23:01:03 +0800 Subject: [PATCH] refactor: changed the interface of threads --- core/context/providers/LocalsProvider.ts | 21 ++++++------- core/index.d.ts | 31 +++++++++++-------- core/util/filesystem.ts | 13 ++++---- core/util/messageIde.ts | 12 ++++---- core/web/webviewProtocol.ts | 3 +- extensions/vscode/src/debug/debug.ts | 26 +++++++++------- extensions/vscode/src/ideProtocol.ts | 38 ++++++++++++++---------- extensions/vscode/src/util/ideUtils.ts | 32 ++++++++------------ 8 files changed, 94 insertions(+), 82 deletions(-) diff --git a/core/context/providers/LocalsProvider.ts b/core/context/providers/LocalsProvider.ts index c385ecc93..b25ecde5a 100644 --- a/core/context/providers/LocalsProvider.ts +++ b/core/context/providers/LocalsProvider.ts @@ -22,7 +22,9 @@ class LocalsProvider extends BaseContextProvider { // Assuming that the query is a number const localVariables = await extras.ide.getDebugLocals(Number(query)); const threadIndex = Number(query); - const thread = (await extras.ide.getAvailableThreads())[threadIndex]; + const thread = (await extras.ide.getAvailableThreads()).find( + (thread) => thread.id == threadIndex + ); const callStacksSources = await extras.ide.getTopLevelCallStackSources( threadIndex, this.options?.stackDepth || 3 @@ -36,7 +38,7 @@ class LocalsProvider extends BaseContextProvider { { description: "The value, name and possibly type of the local variables", content: - `This is a paused thread: ${thread.split(",")[1].trimStart()}\n` + + `This is a paused thread: ${thread?.name}` + `Current local variable contents:\n\n${localVariables}.\n` + `Current top level call stacks: \n\n${callStackContents}`, name: "Locals", @@ -49,16 +51,11 @@ class LocalsProvider extends BaseContextProvider { ): Promise { const threads = await args.ide.getAvailableThreads(); - return threads.map((thread, threadIndex) => { - const [threadId, threadName] = thread - .split(",") - .map((str) => str.trimStart()); - return { - id: `${threadIndex}`, - title: threadName, - description: threadId, - }; - }); + return threads.map((thread) => ({ + id: `${thread.id}`, + title: thread.name, + description: `${thread.id}`, + })); } } diff --git a/core/index.d.ts b/core/index.d.ts index c6473248f..55d4f02b0 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -16,7 +16,7 @@ declare global { postIntellijMessage?: ( messageType: string, data: any, - messageIde: string, + messageIde: string ) => void; } } @@ -73,17 +73,17 @@ export interface ILLM extends LLMOptions { streamComplete( prompt: string, - options?: LLMFullCompletionOptions, + options?: LLMFullCompletionOptions ): AsyncGenerator; streamChat( messages: ChatMessage[], - options?: LLMFullCompletionOptions, + options?: LLMFullCompletionOptions ): AsyncGenerator; chat( messages: ChatMessage[], - options?: LLMFullCompletionOptions, + options?: LLMFullCompletionOptions ): Promise; countTokens(text: string): number; @@ -123,10 +123,10 @@ export interface CustomContextProvider { type?: ContextProviderType; getContextItems( query: string, - extras: ContextProviderExtras, + extras: ContextProviderExtras ): Promise; loadSubmenuItems?: ( - args: LoadSubmenuItemsArgs, + args: LoadSubmenuItemsArgs ) => Promise; } @@ -141,7 +141,7 @@ export interface IContextProvider { getContextItems( query: string, - extras: ContextProviderExtras, + extras: ContextProviderExtras ): Promise; loadSubmenuItems(args: LoadSubmenuItemsArgs): Promise; @@ -287,15 +287,15 @@ export interface CustomLLMWithOptionals { streamCompletion?: ( prompt: string, options: CompletionOptions, - fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise, + fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise ) => AsyncGenerator; streamChat?: ( messages: ChatMessage[], options: CompletionOptions, - fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise, + fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise ) => AsyncGenerator; listModels?: ( - fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise, + fetch: (input: RequestInfo | URL, init?: RequestInit) => Promise ) => Promise; } @@ -320,6 +320,11 @@ export class Problem { message: string; } +export class Thread { + name: string; + id: number; +} + export type IdeType = "vscode" | "jetbrains"; export interface IdeInfo { ideType: IdeType; @@ -339,7 +344,7 @@ export interface IDE { threadIndex: number, stackDepth: number ): Promise; - getAvailableThreads(): Promise; + getAvailableThreads(): Promise; listWorkspaceContents(directory?: string): Promise; listFolders(): Promise; getWorkspaceDirs(): Promise; @@ -355,12 +360,12 @@ export interface IDE { showLines( filepath: string, startLine: number, - endLine: number, + endLine: number ): Promise; showDiff( filepath: string, newContents: string, - stepIndex: number, + stepIndex: number ): Promise; getOpenFiles(): Promise; getPinnedFiles(): Promise; diff --git a/core/util/filesystem.ts b/core/util/filesystem.ts index bc237f94e..069e4ffb5 100644 --- a/core/util/filesystem.ts +++ b/core/util/filesystem.ts @@ -1,5 +1,5 @@ import * as fs from "fs"; -import { ContinueRcJson, IDE, IdeInfo, Problem, Range } from ".."; +import { ContinueRcJson, IDE, IdeInfo, Problem, Range, Thread } from ".."; import { getContinueGlobalPath } from "./paths"; class FileSystemIde implements IDE { @@ -36,16 +36,19 @@ class FileSystemIde implements IDE { async getDebugLocals(threadIndex: number): Promise { return Promise.resolve(""); } - async getTopLevelCallStackSources(threadIndex: number, stackDepth: number): Promise { + async getTopLevelCallStackSources( + threadIndex: number, + stackDepth: number + ): Promise { return Promise.resolve([]); } - async getAvailableThreads(): Promise { + async getAvailableThreads(): Promise { return Promise.resolve([]); } showLines( filepath: string, startLine: number, - endLine: number, + endLine: number ): Promise { return Promise.resolve(); } @@ -110,7 +113,7 @@ class FileSystemIde implements IDE { showDiff( filepath: string, newContents: string, - stepIndex: number, + stepIndex: number ): Promise { return Promise.resolve(); } diff --git a/core/util/messageIde.ts b/core/util/messageIde.ts index 01568f1d0..0b6ca26e5 100644 --- a/core/util/messageIde.ts +++ b/core/util/messageIde.ts @@ -1,8 +1,8 @@ -import { ContinueRcJson, IDE, IdeInfo, Problem, Range } from ".."; +import { ContinueRcJson, IDE, IdeInfo, Problem, Range, Thread } from ".."; export class MessageIde implements IDE { constructor( - private readonly request: (messageType: string, data: any) => Promise, + private readonly request: (messageType: string, data: any) => Promise ) {} getDebugLocals(threadIndex: number): Promise { return this.request("getDebugLocals", { threadIndex }); @@ -16,8 +16,8 @@ export class MessageIde implements IDE { stackDepth, }); } - getAvailableThreads(): Promise { - return this.request("getTopLevelCallStackSources", undefined); + getAvailableThreads(): Promise { + return this.request("getAvailableThreads", undefined); } getIdeInfo(): Promise { return this.request("getIdeInfo", undefined); @@ -60,7 +60,7 @@ export class MessageIde implements IDE { async showLines( filepath: string, startLine: number, - endLine: number, + endLine: number ): Promise { return await this.request("showLines", { filepath, startLine, endLine }); } @@ -105,7 +105,7 @@ export class MessageIde implements IDE { async showDiff( filepath: string, newContents: string, - stepIndex: number, + stepIndex: number ): Promise { await this.request("showDiff", { filepath, newContents, stepIndex }); } diff --git a/core/web/webviewProtocol.ts b/core/web/webviewProtocol.ts index 85741b085..4b7ba3672 100644 --- a/core/web/webviewProtocol.ts +++ b/core/web/webviewProtocol.ts @@ -5,6 +5,7 @@ import { DiffLine, Problem, Range, + Thread, } from ".."; import { RangeInFileWithContents } from "../commands/util"; import { Protocol } from "../protocol"; @@ -49,7 +50,7 @@ export type IdeProtocol = { { threadIndex: number; stackDepth: number }, string[] ]; - getAvailableThreads: [undefined, string[]]; + getAvailableThreads: [undefined, Thread[]]; isTelemetryEnabled: [undefined, boolean]; getUniqueId: [undefined, string]; }; diff --git a/extensions/vscode/src/debug/debug.ts b/extensions/vscode/src/debug/debug.ts index 623ea5c5a..6637ae045 100644 --- a/extensions/vscode/src/debug/debug.ts +++ b/extensions/vscode/src/debug/debug.ts @@ -12,6 +12,17 @@ export function registerDebugTracker( vscode.debug.registerDebugAdapterTrackerFactory("*", { createDebugAdapterTracker(session: vscode.DebugSession) { return { + async onWillStopSession() { + threadStopped.clear(); + webviewProtocol?.request("updateSubmenuItems", { + provider: "locals", + submenuItems: (await ide.getAvailableThreads()).map((thread) => ({ + id: `${thread.id}`, + title: thread.name, + description: `${thread.id}`, + })), + }); + }, async onDidSendMessage(message: any) { if (message.type == "event") { switch (message.event) { @@ -36,16 +47,11 @@ export function registerDebugTracker( 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, - }; - } + (thread) => ({ + id: `${thread.id}`, + title: thread.name, + description: `${thread.id}`, + }) ), }); break; diff --git a/extensions/vscode/src/ideProtocol.ts b/extensions/vscode/src/ideProtocol.ts index edf605f5f..a59899211 100644 --- a/extensions/vscode/src/ideProtocol.ts +++ b/extensions/vscode/src/ideProtocol.ts @@ -4,7 +4,7 @@ import * as path from "path"; import * as vscode from "vscode"; import * as child_process from "child_process"; -import { ContinueRcJson, IDE, IdeInfo, Problem, Range } from "core"; +import { ContinueRcJson, IDE, IdeInfo, Problem, Range, Thread } from "core"; import { DiffManager } from "./diff/horizontal"; import { VsCodeIdeUtils } from "./util/ideUtils"; import { traverseDirectory } from "./util/traverseDirectory"; @@ -33,8 +33,8 @@ class VsCodeIde implements IDE { filepath, new vscode.Range( new vscode.Position(range.start.line, range.start.character), - new vscode.Position(range.end.line, range.end.character), - ), + new vscode.Position(range.end.line, range.end.character) + ) ); } @@ -46,7 +46,7 @@ class VsCodeIde implements IDE { files.map(async (file) => { let stat = await vscode.workspace.fs.stat(uriFromFilePath(file)); pathToLastModified[file] = stat.mtime; - }), + }) ); return pathToLastModified; @@ -79,10 +79,16 @@ class VsCodeIde implements IDE { return await this.ideUtils.getDebugLocals(threadIndex); } - async getTopLevelCallStackSources(threadIndex: number, stackDepth: number): Promise { - return await this.ideUtils.getTopLevelCallStackSources(threadIndex, stackDepth); + async getTopLevelCallStackSources( + threadIndex: number, + stackDepth: number + ): Promise { + return await this.ideUtils.getTopLevelCallStackSources( + threadIndex, + stackDepth + ); } - async getAvailableThreads(): Promise { + async getAvailableThreads(): Promise { return await this.ideUtils.getAvailableThreads(); } @@ -93,7 +99,7 @@ class VsCodeIde implements IDE { const contents = await Promise.all( this.ideUtils .getWorkspaceDirectories() - .map((dir) => this.ideUtils.getDirectoryContents(dir, true)), + .map((dir) => this.ideUtils.getDirectoryContents(dir, true)) ); return contents.flat(); } @@ -108,7 +114,7 @@ class VsCodeIde implements IDE { for (const [filename, type] of files) { if (type === vscode.FileType.File && filename === ".continuerc.json") { const contents = await this.ideUtils.readFile( - vscode.Uri.joinPath(workspaceDir, filename).fsPath, + vscode.Uri.joinPath(workspaceDir, filename).fsPath ); configs.push(JSON.parse(contents)); } @@ -126,7 +132,7 @@ class VsCodeIde implements IDE { directory, [], false, - undefined, + undefined )) { allDirs.push(dir); } @@ -146,7 +152,7 @@ class VsCodeIde implements IDE { async writeFile(path: string, contents: string): Promise { await vscode.workspace.fs.writeFile( vscode.Uri.file(path), - Buffer.from(contents), + Buffer.from(contents) ); } @@ -161,11 +167,11 @@ class VsCodeIde implements IDE { async showLines( filepath: string, startLine: number, - endLine: number, + endLine: number ): Promise { const range = new vscode.Range( new vscode.Position(startLine, 0), - new vscode.Position(endLine, 0), + new vscode.Position(endLine, 0) ); openEditorAndRevealRange(filepath, range).then(() => { // TODO: Highlight lines @@ -199,7 +205,7 @@ class VsCodeIde implements IDE { async showDiff( filepath: string, newContents: string, - stepIndex: number, + stepIndex: number ): Promise { await this.diffManager.writeDiff(filepath, newContents, stepIndex); } @@ -224,10 +230,10 @@ class VsCodeIde implements IDE { "@vscode", "ripgrep", "bin", - "rg", + "rg" ), ["-i", "-C", "2", `"${query}"`, "."], - { cwd: dir }, + { cwd: dir } ); let output = ""; diff --git a/extensions/vscode/src/util/ideUtils.ts b/extensions/vscode/src/util/ideUtils.ts index 953bf3c25..0cfb29227 100644 --- a/extensions/vscode/src/util/ideUtils.ts +++ b/extensions/vscode/src/util/ideUtils.ts @@ -1,4 +1,4 @@ -import { FileEdit, RangeInFile } from "core"; +import { FileEdit, RangeInFile, Thread } from "core"; import path from "path"; import * as vscode from "vscode"; import { VsCodeExtension } from "../extension/vscodeExtension"; @@ -372,14 +372,12 @@ export class VsCodeIdeUtils { return threadsResponse; } - async getAvailableThreads(): Promise { + async getAvailableThreads(): Promise { const session = vscode.debug.activeDebugSession; if (!session) return []; const threadsResponse = await this._getThreads(session); - return threadsResponse.threads.map( - (thread: { id: number; name: string }) => `${thread.id}, ${thread.name}` - ); + return threadsResponse.threads; } async getDebugLocals(threadIndex: number = 0): Promise { @@ -392,13 +390,11 @@ export class VsCodeIdeUtils { return ""; } - const variablesResponse = await this._getThreads(session) - .then((threadsResponse) => - session.customRequest("stackTrace", { - threadId: threadsResponse.threads[threadIndex].id, - startFrame: 0, - }) - ) + const variablesResponse = await session + .customRequest("stackTrace", { + threadId: threadIndex, + startFrame: 0, + }) .then((traceResponse) => session.customRequest("scopes", { frameId: traceResponse.stackFrames[0].id, @@ -429,13 +425,11 @@ export class VsCodeIdeUtils { const session = vscode.debug.activeDebugSession; if (!session) return []; - const sourcesPromises = await this._getThreads(session) - .then((threadsResponse) => - session.customRequest("stackTrace", { - threadId: threadsResponse.threads[threadIndex].id, - startFrame: 0, - }) - ) + const sourcesPromises = await session + .customRequest("stackTrace", { + threadId: threadIndex, + startFrame: 0, + }) .then((traceResponse) => traceResponse.stackFrames .slice(0, stackDepth)