From 81c0e8be73b402a1b21f63877b67351c7e552b4e Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Mon, 5 Feb 2024 11:19:01 +0800 Subject: [PATCH] refactor: Simplify topic interaction in UI - Remove topic-related views, commands, and handlers from the codebase - Refactor code to align with the new unified topic interaction method - Streamline UI by eliminating unnecessary elements and consolidating views --- package.json | 73 ---------------------------- src/contributes/commands.ts | 61 ----------------------- src/contributes/views.ts | 7 --- src/extension.ts | 13 +---- src/handler/sendMessageBase.ts | 52 -------------------- src/panel/devchatView.ts | 8 --- test/handler/sendMessageBase.test.ts | 23 +-------- 7 files changed, 2 insertions(+), 235 deletions(-) diff --git a/package.json b/package.json index 986c7e7..72340fa 100644 --- a/package.json +++ b/package.json @@ -548,39 +548,10 @@ "type": "webview", "id": "devchat-view", "name": "DevChat" - }, - { - "type": "tree", - "id": "devchat-topicview", - "name": "DevChat-Topic" } ] }, "commands": [ - { - "command": "devchat-topicview.reloadTopic", - "title": "Reload Topics", - "icon": "$(refresh)" - }, - { - "command": "devchat-topicview.selectTopic", - "title": "Select Topic", - "icon": "$(add)" - }, - { - "command": "devchat-topicview.addTopic", - "title": "Add Topic", - "icon": "$(add)" - }, - { - "command": "devchat-topicview.deleteSelectedTopic", - "title": "Delete Selected Topic", - "icon": "$(trash)" - }, - { - "command": "devchat-topicview.deleteTopic", - "title": "Delete topic" - }, { "command": "devchat.applyDiffResult", "title": "Apply Diff", @@ -655,30 +626,6 @@ } ], "menus": { - "view/item/context": [ - { - "command": "devchat-topicview.deleteTopic", - "when": "view == devchat-topicview", - "group": "1_modification" - } - ], - "view/title": [ - { - "command": "devchat-topicview.addTopic", - "when": "view == devchat-topicview", - "group": "navigation" - }, - { - "command": "devchat-topicview.deleteSelectedTopic", - "when": "view == devchat-topicview", - "group": "navigation" - }, - { - "command": "devchat-topicview.reloadTopic", - "when": "view == devchat-topicview", - "group": "navigation" - } - ], "editor/title": [ { "command": "devchat.applyDiffResult", @@ -687,26 +634,6 @@ } ], "commandPalette": [ - { - "command": "devchat-topicview.reloadTopic", - "when": "false" - }, - { - "command": "devchat-topicview.selectTopic", - "when": "false" - }, - { - "command": "devchat-topicview.addTopic", - "when": "false" - }, - { - "command": "devchat-topicview.deleteSelectedTopic", - "when": "false" - }, - { - "command": "devchat-topicview.deleteTopic", - "when": "false" - }, { "command": "devchat.applyDiffResult", "when": "false" diff --git a/src/contributes/commands.ts b/src/contributes/commands.ts index 4bf3d46..99e051f 100644 --- a/src/contributes/commands.ts +++ b/src/contributes/commands.ts @@ -162,67 +162,6 @@ export function registerStatusBarItemClickCommand(context: vscode.ExtensionConte ); } -const topicDeleteCallback = async (item: TopicTreeItem) => { - const confirm = 'Delete'; - const label = typeof item.label === 'string' ? item.label : item.label!.label; - const truncatedLabel = label.substring(0, 20) + (label.length > 20 ? '...' : ''); - const result = await vscode.window.showWarningMessage( - `Are you sure you want to delete the topic "${truncatedLabel}"?`, - { modal: true }, - confirm - ); - - if (result === confirm) { - TopicManager.getInstance().deleteTopic(item.id); - } -}; - - -export function regTopicDeleteCommand(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand('devchat-topicview.deleteTopic', topicDeleteCallback) - ); -} - -export function regAddTopicCommand(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand('devchat-topicview.addTopic', () => { - const topic = TopicManager.getInstance().createTopic(); - TopicManager.getInstance().setCurrentTopic(topic.topicId); - }) - ); -} - -export function regDeleteSelectTopicCommand(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand('devchat-topicview.deleteSelectedTopic', () => { - const selectedItem = TopicTreeDataProvider.getInstance().selectedItem; - if (selectedItem) { - topicDeleteCallback(selectedItem); - } else { - vscode.window.showErrorMessage('No item selected'); - } - }) - ); -} - -export function regSelectTopicCommand(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand('devchat-topicview.selectTopic', (item: TopicTreeItem) => { - TopicTreeDataProvider.getInstance().setSelectedItem(item); - TopicManager.getInstance().setCurrentTopic(item.id); - }) - ); -} - -export function regReloadTopicCommand(context: vscode.ExtensionContext) { - context.subscriptions.push( - vscode.commands.registerCommand('devchat-topicview.reloadTopic', async () => { - TopicManager.getInstance().loadTopics(); - }) - ); -} - export function regPythonPathCommand(context: vscode.ExtensionContext) { context.subscriptions.push( vscode.commands.registerCommand('devchat.PythonPath', async () => { diff --git a/src/contributes/views.ts b/src/contributes/views.ts index 1b92b3f..11956ce 100644 --- a/src/contributes/views.ts +++ b/src/contributes/views.ts @@ -12,10 +12,3 @@ export function regDevChatView(context: vscode.ExtensionContext) { }) ); } - -export function regTopicView(context: vscode.ExtensionContext) { - const yourTreeView = vscode.window.createTreeView('devchat-topicview', { - treeDataProvider: TopicTreeDataProvider.getInstance(), - }); - context.subscriptions.push(yourTreeView); -} \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 998cbb9..a3bdc3c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,11 +6,6 @@ import { registerAskForCodeCommand, registerAskForFileCommand, registerAccessKeySettingCommand, - regTopicDeleteCommand, - regAddTopicCommand, - regDeleteSelectTopicCommand, - regSelectTopicCommand, - regReloadTopicCommand, regApplyDiffResultCommand, registerStatusBarItemClickCommand, regPythonPathCommand, @@ -21,7 +16,7 @@ import { registerHandleUri, } from './contributes/commands'; import { regLanguageContext } from './contributes/context'; -import { regDevChatView, regTopicView } from './contributes/views'; +import { regDevChatView } from './contributes/views'; import { ExtensionContextHolder } from './util/extensionContext'; import { logger } from './util/logger'; @@ -294,7 +289,6 @@ async function activate(context: vscode.ExtensionContext) { regLanguageContext(); regDevChatView(context); - regTopicView(context); registerAccessKeySettingCommand(context); registerOpenChatPanelCommand(context); @@ -309,11 +303,6 @@ async function activate(context: vscode.ExtensionContext) { createStatusBarItem(context); - regTopicDeleteCommand(context); - regAddTopicCommand(context); - regDeleteSelectTopicCommand(context); - regSelectTopicCommand(context); - regReloadTopicCommand(context); regApplyDiffResultCommand(context); regPythonPathCommand(context); diff --git a/src/handler/sendMessageBase.ts b/src/handler/sendMessageBase.ts index deadb23..83a9337 100644 --- a/src/handler/sendMessageBase.ts +++ b/src/handler/sendMessageBase.ts @@ -5,57 +5,6 @@ import { TopicManager } from '../topic/topicManager'; import { assertValue } from '../util/check'; -/** - * Class to handle topic updates. - */ -export class TopicUpdateHandler { - /** - * Flag to indicate if the topic is being updated from the webview. - */ - private static isTopicUpdatingFromWebview: boolean = false; - - /** - * Checks if the topic change was triggered by the webview. - * - * @returns {boolean} - Returns true if the topic change was triggered by the webview, false otherwise. - */ - public static isTopicChangeTriggeredByWebview(): boolean { - return TopicUpdateHandler.isTopicUpdatingFromWebview; - } - - /** - * Processes the topic change after a chat. - * - * @param {string | undefined} parentHash - The hash of the parent message, if any. - * @param {any} message - The message object. - * @param {ChatResponse} chatResponse - The chat response object. - * @returns {Promise} - A Promise that resolves when the topic change has been processed. - */ - public static async processTopicChangeAfterChat(parentHash:string | undefined, message: any, chatResponse: ChatResponse): Promise { - TopicUpdateHandler.isTopicUpdatingFromWebview = true; - try { - if (!chatResponse.isError) { - let topicId = TopicManager.getInstance().currentTopicId; - if (!topicId) { - // create new topic - const topic = TopicManager.getInstance().createTopic(); - topicId = topic.topicId; - } - - TopicManager.getInstance().updateTopic( - topicId!, - chatResponse['prompt-hash'], - parseDateStringToTimestamp(chatResponse.date), - message.text, - chatResponse.response - ); - } - } finally { - TopicUpdateHandler.isTopicUpdatingFromWebview = false; - } - } -} - /** * Class to handle user interaction stop events. */ @@ -232,7 +181,6 @@ export async function sendMessageBase(message: any, handlePartialData: (data: { assertValue(UserStopHandler.isUserInteractionStopped(), "User Stopped"); await addMessageToHistory(message, chatResponse, message.parent_hash); - await TopicUpdateHandler.processTopicChangeAfterChat(message.parent_hash, message, chatResponse); return { command: 'receiveMessage', diff --git a/src/panel/devchatView.ts b/src/panel/devchatView.ts index 43c1856..b50c18b 100644 --- a/src/panel/devchatView.ts +++ b/src/panel/devchatView.ts @@ -9,7 +9,6 @@ import { ExtensionContextHolder } from '../util/extensionContext'; import { TopicManager } from '../topic/topicManager'; import { UiUtilWrapper } from '../util/uiUtil'; import { ChatContextManager } from '../context/contextManager'; -import { TopicUpdateHandler } from '../handler/sendMessageBase'; export class DevChatViewProvider implements vscode.WebviewViewProvider { @@ -64,13 +63,6 @@ export class DevChatViewProvider implements vscode.WebviewViewProvider { null, this._context.subscriptions ); - - TopicManager.getInstance().addOnCurrentTopicIdChangeListener((topicId) => { - if (topicId && TopicUpdateHandler.isTopicChangeTriggeredByWebview()) { - return; - } - this.reloadWebview(); - }); } private onDidChangeWorkspaceFolders(event: vscode.WorkspaceFoldersChangeEvent): void { diff --git a/test/handler/sendMessageBase.test.ts b/test/handler/sendMessageBase.test.ts index 171dc7c..3fb8f17 100644 --- a/test/handler/sendMessageBase.test.ts +++ b/test/handler/sendMessageBase.test.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; import sinon from 'sinon'; import * as path from 'path'; -import { parseMessage, parseMessageAndSetOptions, TopicUpdateHandler, processChatResponse, sendMessageBase, stopDevChatBase } from '../../src/handler/sendMessageBase'; +import { parseMessage, parseMessageAndSetOptions, processChatResponse, sendMessageBase, stopDevChatBase } from '../../src/handler/sendMessageBase'; import { ChatResponse } from '../../src/toolwrapper/devchat'; import { UiUtilWrapper } from '../../src/util/uiUtil'; @@ -56,27 +56,6 @@ describe('sendMessageBase', () => { }); }); - - describe('TopicUpdateHandler.processTopicChangeAfterChat', () => { - it('should handle topic correctly', async () => { - const parentHash = 'somehash'; - const message = { - text: 'Hello, world!' - }; - const chatResponse: ChatResponse = { - "finish_reason": "", - response: 'Hello, user!', - isError: false, - user: 'user', - date: '2022-01-01T00:00:00.000Z', - 'prompt-hash': 'responsehash' - }; - - await TopicUpdateHandler.processTopicChangeAfterChat(parentHash, message, chatResponse); - // Check if the topic was updated correctly - }); - }); - describe('processChatResponse', () => { it('should handle response text correctly when isError is false', async () => { const partialDataText = 'Partial data';