Add message history to chat panel

- Create MessageHistory class in src/util/messageHistory.ts
- Add message history to sendMessage function in src/handler/sendMessage.ts
- Add message history to ChatPanel class in src/panel/chatPanel.ts
- Update parent hash handling in sendMessage function
This commit is contained in:
bobo.yang 2023-05-09 16:05:49 +08:00
parent 6fe67255dc
commit 36bef1c8ea
3 changed files with 64 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import DevChat, { ChatResponse } from '../toolwrapper/devchat';
import CommandManager from '../command/commandManager'; import CommandManager from '../command/commandManager';
import { logger } from '../util/logger'; import { logger } from '../util/logger';
import { MessageHandler } from './messageHandler'; import { MessageHandler } from './messageHandler';
import messageHistory from '../util/messageHistory';
// Add this function to messageHandler.ts // Add this function to messageHandler.ts
@ -71,7 +72,8 @@ function getInstructionFiles(): string[] {
const devChat = new DevChat(); const devChat = new DevChat();
// message: { command: 'sendMessage', text: 'xxx', parent_hash: 'xxx'}
// message: { command: 'sendMessage', text: 'xxx', hash: 'xxx'}
// return message: // return message:
// { command: 'receiveMessage', text: 'xxxx', hash: 'xxx', user: 'xxx', date: 'xxx'} // { command: 'receiveMessage', text: 'xxxx', hash: 'xxx', user: 'xxx', date: 'xxx'}
// { command: 'receiveMessagePartial', text: 'xxxx', user: 'xxx', date: 'xxx'} // { command: 'receiveMessagePartial', text: 'xxxx', user: 'xxx', date: 'xxx'}
@ -80,10 +82,19 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro
const parsedMessage = parseMessage(newText2); const parsedMessage = parseMessage(newText2);
const chatOptions: any = {}; const chatOptions: any = {};
logger.channel()?.info(`parent_hash: ${message.parent_hash}`) let parent_hash = undefined;
if (message.parent_hash) { logger.channel()?.info(`request message hash: ${message.hash}`)
chatOptions.parent = message.parent_hash; if (message.hash) {
const hmessage = messageHistory.find(panel, message.hash);
parent_hash = hmessage ? message.parent_hash : undefined;
} else {
const hmessage = messageHistory.findLast(panel);
parent_hash = hmessage ? hmessage.hash : undefined;
} }
if (parent_hash) {
chatOptions.parent = parent_hash;
}
logger.channel()?.info(`parent hash: ${parent_hash}`);
if (parsedMessage.context.length > 0) { if (parsedMessage.context.length > 0) {
chatOptions.context = parsedMessage.context; chatOptions.context = parsedMessage.context;
@ -104,6 +115,10 @@ export async function sendMessage(message: any, panel: vscode.WebviewPanel): Pro
const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData); const chatResponse = await devChat.chat(parsedMessage.text, chatOptions, onData);
if (!chatResponse.isError) {
messageHistory.add(panel, {request: message.text, text: parsedMessage.text, parent_hash, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date });
}
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: chatResponse.response, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date, isError: chatResponse.isError }); MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: chatResponse.response, hash: chatResponse['prompt-hash'], user: chatResponse.user, date: chatResponse.date, isError: chatResponse.isError });
return; return;
} }

View File

@ -5,6 +5,8 @@ import '../handler/loadHandlers';
import handleMessage from '../handler/messageHandler'; import handleMessage from '../handler/messageHandler';
import WebviewManager from './webviewManager'; import WebviewManager from './webviewManager';
import messageHistory from '../util/messageHistory';
export default class ChatPanel { export default class ChatPanel {
private static _instance: ChatPanel | undefined; private static _instance: ChatPanel | undefined;
private readonly _panel: vscode.WebviewPanel; private readonly _panel: vscode.WebviewPanel;
@ -16,6 +18,9 @@ export default class ChatPanel {
ChatPanel._instance._panel.reveal(); ChatPanel._instance._panel.reveal();
} else { } else {
const panel = ChatPanel.createWebviewPanel(extensionUri); const panel = ChatPanel.createWebviewPanel(extensionUri);
panel.onDidDispose(() => {
messageHistory.remove(panel);
});
ChatPanel._instance = new ChatPanel(panel, extensionUri); ChatPanel._instance = new ChatPanel(panel, extensionUri);
} }
} }

View File

@ -0,0 +1,40 @@
import * as vscode from 'vscode';
class MessageHistory {
private history: WeakMap<vscode.WebviewPanel, any[]>;
private lastmessage: WeakMap<vscode.WebviewPanel, any>;
constructor() {
this.history = new WeakMap();
this.lastmessage = new WeakMap();
}
add(panel: vscode.WebviewPanel, message: any) {
if (!this.history.has(panel)) {
this.history.set(panel, []);
}
this.history.get(panel)!.push(message);
this.lastmessage.set(panel, message);
}
find(panel: vscode.WebviewPanel, hash: string) {
if (!this.history.has(panel)) {
return null;
}
return this.history.get(panel)!.find(message => message.hash === hash);
}
findLast(panel: vscode.WebviewPanel) {
if (!this.history.has(panel)) {
return null;
}
return this.lastmessage.get(panel);
}
remove(panel: vscode.WebviewPanel) {
this.history.delete(panel);
this.lastmessage.delete(panel);
}
}
const messageHistory = new MessageHistory();
export default messageHistory;