Merge pull request #568 from devchat-ai/remove_chat_messages_cache
Refactor: Remove chat messages cache
This commit is contained in:
commit
5a3547100d
@ -1,9 +1,4 @@
|
||||
|
||||
|
||||
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
|
||||
import messageHistory from '../util/messageHistory';
|
||||
import { ApiKeyManager } from '../util/apiKey';
|
||||
|
||||
|
||||
export interface LoadHistoryMessages {
|
||||
command: string;
|
||||
@ -62,57 +57,19 @@ export async function loadTopicHistoryLogs(topicId: string | undefined): Promise
|
||||
return logEntries;
|
||||
}
|
||||
|
||||
export function updateCurrentMessageHistory(topicId: string, logEntries: Array<LogEntry>): void {
|
||||
messageHistory.clear();
|
||||
messageHistory.setTopic(topicId);
|
||||
|
||||
for (let i = 0; i < logEntries.length; i++) {
|
||||
let entryOld = logEntries[i];
|
||||
let entryNew = {
|
||||
date: entryOld.date,
|
||||
hash: entryOld.hash,
|
||||
request: entryOld.request,
|
||||
text: entryOld.response,
|
||||
user: entryOld.user,
|
||||
parentHash: entryOld.parent,
|
||||
context: entryOld.context,
|
||||
};
|
||||
messageHistory.add(entryNew);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export function loadTopicHistoryFromCurrentMessageHistory(skip: number, count: number): LoadHistoryMessages {
|
||||
const logEntries = messageHistory.getList();
|
||||
const newEntries = logEntries.map((entry) => {
|
||||
export async function loadTopicHistoryFromCurrentMessageHistory(topicId: string, skip: number, count: number): Promise< LoadHistoryMessages > {
|
||||
const logEntries = await loadTopicHistoryLogs(topicId);
|
||||
if (!logEntries) {
|
||||
return {
|
||||
hash: entry.hash,
|
||||
parent: entry.parentHash,
|
||||
user: entry.user,
|
||||
date: entry.date,
|
||||
request: entry.request,
|
||||
response: entry.text,
|
||||
context: entry.context,
|
||||
} as LogEntry;
|
||||
});
|
||||
command: 'loadHistoryMessages',
|
||||
entries: [],
|
||||
} as LoadHistoryMessages;
|
||||
}
|
||||
|
||||
const logEntriesFlat = newEntries.reverse().slice(skip, skip + count).reverse();
|
||||
const logEntriesFlat = logEntries.reverse().slice(skip, skip + count).reverse();
|
||||
return {
|
||||
command: 'loadHistoryMessages',
|
||||
entries: logEntriesFlat,
|
||||
} as LoadHistoryMessages;
|
||||
}
|
||||
|
||||
export async function historyMessagesBase(topicId: string): Promise<LoadHistoryMessages | undefined> {
|
||||
const logEntriesFlat = await loadTopicHistoryLogs(topicId);
|
||||
if (!logEntriesFlat) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
updateCurrentMessageHistory(topicId, logEntriesFlat);
|
||||
|
||||
return {
|
||||
command: 'loadHistoryMessages',
|
||||
entries: logEntriesFlat.length > 0 ? logEntriesFlat : [],
|
||||
} as LoadHistoryMessages;
|
||||
}
|
||||
|
@ -1,8 +1,7 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { MessageHandler } from './messageHandler';
|
||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||
import { historyMessagesBase, LoadHistoryMessages, loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
|
||||
import { UiUtilWrapper } from '../util/uiUtil';
|
||||
import { loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
|
||||
import { DevChatConfig } from '../util/config';
|
||||
|
||||
|
||||
@ -15,12 +14,7 @@ export async function getHistoryMessages(message: {command: string, topicId: str
|
||||
const skip = maxCount * (message.page ? message.page : 0);
|
||||
const topicId = message.topicId;
|
||||
|
||||
const historyMessageAll = await historyMessagesBase(topicId);
|
||||
if (!historyMessageAll?.entries.length || historyMessageAll?.entries.length < maxCount) {
|
||||
MessageHandler.sendMessage(panel, historyMessageAll!);
|
||||
}
|
||||
|
||||
const historyMessage = loadTopicHistoryFromCurrentMessageHistory(skip, maxCount);
|
||||
const historyMessage = await loadTopicHistoryFromCurrentMessageHistory(topicId, skip, maxCount);
|
||||
if (historyMessage) {
|
||||
MessageHandler.sendMessage(panel, historyMessage);
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
import DevChat, { ChatOptions, ChatResponse } from '../toolwrapper/devchat';
|
||||
import { logger } from '../util/logger';
|
||||
import messageHistory from '../util/messageHistory';
|
||||
import { assertValue } from '../util/check';
|
||||
|
||||
|
||||
@ -119,25 +118,6 @@ export async function parseMessageAndSetOptions(message: any): Promise<[{ contex
|
||||
return [parsedMessage, chatOptions];
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message to the message history.
|
||||
*
|
||||
* @param {any} message - The message object.
|
||||
* @param {ChatResponse} chatResponse - The chat response object.
|
||||
* @param {string | undefined} parentHash - The hash of the parent message, if any.
|
||||
* @returns {void}
|
||||
*/
|
||||
export async function addMessageToHistory(message: any, chatResponse: ChatResponse, parentHash: string | undefined): Promise<void> {
|
||||
messageHistory.add({
|
||||
request: message.text,
|
||||
text: chatResponse.response,
|
||||
parentHash,
|
||||
hash: chatResponse['prompt-hash'],
|
||||
user: chatResponse.user,
|
||||
date: chatResponse.date
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes the chat response by replacing certain patterns in the response text.
|
||||
*
|
||||
@ -179,8 +159,6 @@ export async function sendMessageBase(message: any, handlePartialData: (data: {
|
||||
|
||||
assertValue(UserStopHandler.isUserInteractionStopped(), "User Stopped");
|
||||
|
||||
await addMessageToHistory(message, chatResponse, message.parent_hash);
|
||||
|
||||
return {
|
||||
command: 'receiveMessage',
|
||||
text: processChatResponse(chatResponse),
|
||||
@ -221,8 +199,6 @@ export async function stopDevChatBase(message: any): Promise<void> {
|
||||
export async function deleteChatMessageBase(message:{'hash': string}): Promise<boolean> {
|
||||
try {
|
||||
assertValue(!message.hash, 'Message hash is required');
|
||||
// delete the message from messageHistory
|
||||
messageHistory.delete(message.hash);
|
||||
|
||||
// delete the message by devchat
|
||||
const bSuccess = await devChat.delete(message.hash);
|
||||
|
@ -1,56 +0,0 @@
|
||||
|
||||
export class MessageHistory {
|
||||
private history: any[];
|
||||
private lastmessage: any | null;
|
||||
private topic: string | null;
|
||||
|
||||
constructor() {
|
||||
this.history = [];
|
||||
this.lastmessage = null;
|
||||
this.topic = null;
|
||||
}
|
||||
|
||||
setTopic(topic: string) {
|
||||
this.topic = topic;
|
||||
}
|
||||
|
||||
getTopic() {
|
||||
return this.topic;
|
||||
}
|
||||
|
||||
add(message: any) {
|
||||
this.history.push(message);
|
||||
this.lastmessage = message;
|
||||
}
|
||||
|
||||
getList() {
|
||||
return this.history;
|
||||
}
|
||||
|
||||
find(hash: string) {
|
||||
return this.history.find(message => message.hash === hash);
|
||||
}
|
||||
|
||||
delete(hash: string) {
|
||||
const index = this.history.findIndex(message => message.hash === hash);
|
||||
if (index >= 0) {
|
||||
this.history.splice(index, 1);
|
||||
}
|
||||
|
||||
if (this.lastmessage?.hash === hash) {
|
||||
this.lastmessage = null;
|
||||
}
|
||||
}
|
||||
|
||||
findLast() {
|
||||
return this.lastmessage;
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.history = [];
|
||||
this.lastmessage = null;
|
||||
}
|
||||
}
|
||||
|
||||
const messageHistory = new MessageHistory();
|
||||
export default messageHistory;
|
Loading…
x
Reference in New Issue
Block a user