Skip to content

Commit 9681738

Browse files
committed
refactor: Remove chat messages cache
- Removed messageHistory utility and related functions - Updated loadTopicHistoryFromCurrentMessageHistory to fetch logs directly - Removed addMessageToHistory and message deletion from messageHistory
1 parent 933952b commit 9681738

File tree

4 files changed

+10
-139
lines changed

4 files changed

+10
-139
lines changed

src/handler/historyMessagesBase.ts

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
2-
31
import DevChat, { LogEntry, LogOptions } from '../toolwrapper/devchat';
4-
import messageHistory from '../util/messageHistory';
5-
import { ApiKeyManager } from '../util/apiKey';
6-
72

83
export interface LoadHistoryMessages {
94
command: string;
@@ -62,57 +57,19 @@ export async function loadTopicHistoryLogs(topicId: string | undefined): Promise
6257
return logEntries;
6358
}
6459

65-
export function updateCurrentMessageHistory(topicId: string, logEntries: Array<LogEntry>): void {
66-
messageHistory.clear();
67-
messageHistory.setTopic(topicId);
6860

69-
for (let i = 0; i < logEntries.length; i++) {
70-
let entryOld = logEntries[i];
71-
let entryNew = {
72-
date: entryOld.date,
73-
hash: entryOld.hash,
74-
request: entryOld.request,
75-
text: entryOld.response,
76-
user: entryOld.user,
77-
parentHash: entryOld.parent,
78-
context: entryOld.context,
79-
};
80-
messageHistory.add(entryNew);
81-
82-
}
83-
}
84-
85-
export function loadTopicHistoryFromCurrentMessageHistory(skip: number, count: number): LoadHistoryMessages {
86-
const logEntries = messageHistory.getList();
87-
const newEntries = logEntries.map((entry) => {
61+
export async function loadTopicHistoryFromCurrentMessageHistory(topicId: string, skip: number, count: number): Promise< LoadHistoryMessages > {
62+
const logEntries = await loadTopicHistoryLogs(topicId);
63+
if (!logEntries) {
8864
return {
89-
hash: entry.hash,
90-
parent: entry.parentHash,
91-
user: entry.user,
92-
date: entry.date,
93-
request: entry.request,
94-
response: entry.text,
95-
context: entry.context,
96-
} as LogEntry;
97-
});
98-
99-
const logEntriesFlat = newEntries.reverse().slice(skip, skip + count).reverse();
100-
return {
101-
command: 'loadHistoryMessages',
102-
entries: logEntriesFlat,
103-
} as LoadHistoryMessages;
104-
}
105-
106-
export async function historyMessagesBase(topicId: string): Promise<LoadHistoryMessages | undefined> {
107-
const logEntriesFlat = await loadTopicHistoryLogs(topicId);
108-
if (!logEntriesFlat) {
109-
return undefined;
65+
command: 'loadHistoryMessages',
66+
entries: [],
67+
} as LoadHistoryMessages;
11068
}
11169

112-
updateCurrentMessageHistory(topicId, logEntriesFlat);
113-
70+
const logEntriesFlat = logEntries.reverse().slice(skip, skip + count).reverse();
11471
return {
11572
command: 'loadHistoryMessages',
116-
entries: logEntriesFlat.length > 0 ? logEntriesFlat : [],
73+
entries: logEntriesFlat,
11774
} as LoadHistoryMessages;
11875
}

src/handler/historyMessagesHandler.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as vscode from 'vscode';
22
import { MessageHandler } from './messageHandler';
33
import { regInMessage, regOutMessage } from '../util/reg_messages';
4-
import { historyMessagesBase, LoadHistoryMessages, loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
5-
import { UiUtilWrapper } from '../util/uiUtil';
4+
import { loadTopicHistoryFromCurrentMessageHistory } from './historyMessagesBase';
65
import { DevChatConfig } from '../util/config';
76

87

@@ -15,12 +14,7 @@ export async function getHistoryMessages(message: {command: string, topicId: str
1514
const skip = maxCount * (message.page ? message.page : 0);
1615
const topicId = message.topicId;
1716

18-
const historyMessageAll = await historyMessagesBase(topicId);
19-
if (!historyMessageAll?.entries.length || historyMessageAll?.entries.length < maxCount) {
20-
MessageHandler.sendMessage(panel, historyMessageAll!);
21-
}
22-
23-
const historyMessage = loadTopicHistoryFromCurrentMessageHistory(skip, maxCount);
17+
const historyMessage = await loadTopicHistoryFromCurrentMessageHistory(topicId, skip, maxCount);
2418
if (historyMessage) {
2519
MessageHandler.sendMessage(panel, historyMessage);
2620
}

src/handler/sendMessageBase.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import DevChat, { ChatOptions, ChatResponse } from '../toolwrapper/devchat';
22
import { logger } from '../util/logger';
3-
import messageHistory from '../util/messageHistory';
43
import { assertValue } from '../util/check';
54

65

@@ -119,25 +118,6 @@ export async function parseMessageAndSetOptions(message: any): Promise<[{ contex
119118
return [parsedMessage, chatOptions];
120119
}
121120

122-
/**
123-
* Adds a message to the message history.
124-
*
125-
* @param {any} message - The message object.
126-
* @param {ChatResponse} chatResponse - The chat response object.
127-
* @param {string | undefined} parentHash - The hash of the parent message, if any.
128-
* @returns {void}
129-
*/
130-
export async function addMessageToHistory(message: any, chatResponse: ChatResponse, parentHash: string | undefined): Promise<void> {
131-
messageHistory.add({
132-
request: message.text,
133-
text: chatResponse.response,
134-
parentHash,
135-
hash: chatResponse['prompt-hash'],
136-
user: chatResponse.user,
137-
date: chatResponse.date
138-
});
139-
}
140-
141121
/**
142122
* Processes the chat response by replacing certain patterns in the response text.
143123
*
@@ -179,8 +159,6 @@ export async function sendMessageBase(message: any, handlePartialData: (data: {
179159

180160
assertValue(UserStopHandler.isUserInteractionStopped(), "User Stopped");
181161

182-
await addMessageToHistory(message, chatResponse, message.parent_hash);
183-
184162
return {
185163
command: 'receiveMessage',
186164
text: processChatResponse(chatResponse),
@@ -221,8 +199,6 @@ export async function stopDevChatBase(message: any): Promise<void> {
221199
export async function deleteChatMessageBase(message:{'hash': string}): Promise<boolean> {
222200
try {
223201
assertValue(!message.hash, 'Message hash is required');
224-
// delete the message from messageHistory
225-
messageHistory.delete(message.hash);
226202

227203
// delete the message by devchat
228204
const bSuccess = await devChat.delete(message.hash);

src/util/messageHistory.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)