refactor: Remove chat messages cache
- Removed messageHistory utility and related functions - Updated loadTopicHistoryFromCurrentMessageHistory to fetch logs directly - Removed addMessageToHistory and message deletion from messageHistory
This commit is contained in:
parent
933952b0c7
commit
9681738eca
@ -1,9 +1,4 @@
|
|||||||
|
|
||||||
|
|
||||||
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
|
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
|
||||||
import messageHistory from '../util/messageHistory';
|
|
||||||
import { ApiKeyManager } from '../util/apiKey';
|
|
||||||
|
|
||||||
|
|
||||||
export interface LoadHistoryMessages {
|
export interface LoadHistoryMessages {
|
||||||
command: string;
|
command: string;
|
||||||
@ -62,57 +57,19 @@ export async function loadTopicHistoryLogs(topicId: string | undefined): Promise
|
|||||||
return logEntries;
|
return logEntries;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateCurrentMessageHistory(topicId: string, logEntries: Array<LogEntry>): void {
|
|
||||||
messageHistory.clear();
|
|
||||||
messageHistory.setTopic(topicId);
|
|
||||||
|
|
||||||
for (let i = 0; i < logEntries.length; i++) {
|
export async function loadTopicHistoryFromCurrentMessageHistory(topicId: string, skip: number, count: number): Promise< LoadHistoryMessages > {
|
||||||
let entryOld = logEntries[i];
|
const logEntries = await loadTopicHistoryLogs(topicId);
|
||||||
let entryNew = {
|
if (!logEntries) {
|
||||||
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) => {
|
|
||||||
return {
|
return {
|
||||||
hash: entry.hash,
|
command: 'loadHistoryMessages',
|
||||||
parent: entry.parentHash,
|
entries: [],
|
||||||
user: entry.user,
|
} as LoadHistoryMessages;
|
||||||
date: entry.date,
|
}
|
||||||
request: entry.request,
|
|
||||||
response: entry.text,
|
|
||||||
context: entry.context,
|
|
||||||
} as LogEntry;
|
|
||||||
});
|
|
||||||
|
|
||||||
const logEntriesFlat = newEntries.reverse().slice(skip, skip + count).reverse();
|
const logEntriesFlat = logEntries.reverse().slice(skip, skip + count).reverse();
|
||||||
return {
|
return {
|
||||||
command: 'loadHistoryMessages',
|
command: 'loadHistoryMessages',
|
||||||
entries: logEntriesFlat,
|
entries: logEntriesFlat,
|
||||||
} as LoadHistoryMessages;
|
} 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 * as vscode from 'vscode';
|
||||||
import { MessageHandler } from './messageHandler';
|
import { MessageHandler } from './messageHandler';
|
||||||
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
import { regInMessage, regOutMessage } from '../util/reg_messages';
|
||||||
import { historyMessagesBase, LoadHistoryMessages, loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
|
import { loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
|
||||||
import { UiUtilWrapper } from '../util/uiUtil';
|
|
||||||
import { DevChatConfig } from '../util/config';
|
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 skip = maxCount * (message.page ? message.page : 0);
|
||||||
const topicId = message.topicId;
|
const topicId = message.topicId;
|
||||||
|
|
||||||
const historyMessageAll = await historyMessagesBase(topicId);
|
const historyMessage = await loadTopicHistoryFromCurrentMessageHistory(topicId, skip, maxCount);
|
||||||
if (!historyMessageAll?.entries.length || historyMessageAll?.entries.length < maxCount) {
|
|
||||||
MessageHandler.sendMessage(panel, historyMessageAll!);
|
|
||||||
}
|
|
||||||
|
|
||||||
const historyMessage = loadTopicHistoryFromCurrentMessageHistory(skip, maxCount);
|
|
||||||
if (historyMessage) {
|
if (historyMessage) {
|
||||||
MessageHandler.sendMessage(panel, historyMessage);
|
MessageHandler.sendMessage(panel, historyMessage);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import DevChat, { ChatOptions, ChatResponse } from '../toolwrapper/devchat';
|
import DevChat, { ChatOptions, ChatResponse } from '../toolwrapper/devchat';
|
||||||
import { logger } from '../util/logger';
|
import { logger } from '../util/logger';
|
||||||
import messageHistory from '../util/messageHistory';
|
|
||||||
import { assertValue } from '../util/check';
|
import { assertValue } from '../util/check';
|
||||||
|
|
||||||
|
|
||||||
@ -119,25 +118,6 @@ export async function parseMessageAndSetOptions(message: any): Promise<[{ contex
|
|||||||
return [parsedMessage, chatOptions];
|
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.
|
* 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");
|
assertValue(UserStopHandler.isUserInteractionStopped(), "User Stopped");
|
||||||
|
|
||||||
await addMessageToHistory(message, chatResponse, message.parent_hash);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
command: 'receiveMessage',
|
command: 'receiveMessage',
|
||||||
text: processChatResponse(chatResponse),
|
text: processChatResponse(chatResponse),
|
||||||
@ -221,8 +199,6 @@ export async function stopDevChatBase(message: any): Promise<void> {
|
|||||||
export async function deleteChatMessageBase(message:{'hash': string}): Promise<boolean> {
|
export async function deleteChatMessageBase(message:{'hash': string}): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
assertValue(!message.hash, 'Message hash is required');
|
assertValue(!message.hash, 'Message hash is required');
|
||||||
// delete the message from messageHistory
|
|
||||||
messageHistory.delete(message.hash);
|
|
||||||
|
|
||||||
// delete the message by devchat
|
// delete the message by devchat
|
||||||
const bSuccess = await devChat.delete(message.hash);
|
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