Skip to content

Commit c2d5c3f

Browse files
authored
Merge pull request #249 from eharris128/broken-history
Fixes chat history Chat history was not being updated correctly when users were regenerating responses causing only the initial response to show when user refreshed UI. Updated History handler to remove assistant message from history to be replaced by regenerated message.
2 parents 3076398 + 1c496c1 commit c2d5c3f

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

src/History/HistoryHandler.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HistoryItem, Message } from "Types/types";
1+
import { HistoryItem, Message } from "Types/types";
22
import LLMPlugin from "main";
33

44
export class History {
@@ -19,17 +19,22 @@ export class History {
1919
}
2020
}
2121

22+
update(index: number, messages: Message[]) {
23+
this.plugin.settings.promptHistory[index].messages = messages;
24+
this.plugin.saveSettings();
25+
}
26+
2227
reset() {
2328
this.plugin.settings.promptHistory = [];
2429
this.plugin.saveSettings();
2530
}
2631

27-
//take in an index from the selected chat history
28-
//overwrite history with new prompt/additional prompt
32+
//take in an index from the selected chat history
33+
//overwrite history with new prompt/additional prompt
2934
overwriteHistory(messages: Message[], index: number) {
30-
const historyItem = this.plugin.settings.promptHistory[index]
35+
const historyItem = this.plugin.settings.promptHistory[index];
3136
historyItem.messages = messages;
32-
this.plugin.settings.promptHistory[index] = historyItem
33-
this.plugin.saveSettings()
37+
this.plugin.settings.promptHistory[index] = historyItem;
38+
this.plugin.saveSettings();
3439
}
3540
}

src/Plugin/Components/ChatContainer.ts

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,44 @@ export class ChatContainer {
6363
this.messageStore.subscribe(this.updateMessages.bind(this));
6464
}
6565

66-
private updateMessages(message: Message[]) {
67-
const currentIndex = this.plugin.settings.currentIndex;
68-
const fabIndex = this.plugin.settings.fabSettings.historyIndex;
69-
const widgetIndex = this.plugin.settings.widgetSettings.historyIndex;
70-
71-
if (currentIndex > -1) {
72-
message = this.plugin.settings.promptHistory[currentIndex].messages;
73-
}
74-
75-
// Always update the current view
76-
if (this.viewType === this.plugin.settings.currentView) {
77-
this.resetChat();
78-
this.generateIMLikeMessages(message);
79-
return;
80-
}
81-
82-
// Update FAB view if it's showing the same history item
83-
if (this.viewType === "floating-action-button" && fabIndex === currentIndex && currentIndex > -1) {
84-
this.resetChat();
85-
this.generateIMLikeMessages(message);
86-
return;
87-
}
88-
89-
// Update Widget view if it's showing the same history item
90-
if (this.viewType === "widget" && widgetIndex === currentIndex && currentIndex > -1) {
91-
this.resetChat();
92-
this.generateIMLikeMessages(message);
93-
return;
94-
}
95-
}
66+
private updateMessages(message: Message[]) {
67+
const currentIndex = this.plugin.settings.currentIndex;
68+
const fabIndex = this.plugin.settings.fabSettings.historyIndex;
69+
const widgetIndex = this.plugin.settings.widgetSettings.historyIndex;
70+
71+
if (currentIndex > -1) {
72+
message = this.plugin.settings.promptHistory[currentIndex].messages;
73+
}
74+
75+
// Always update the current view
76+
if (this.viewType === this.plugin.settings.currentView) {
77+
this.resetChat();
78+
this.generateIMLikeMessages(message);
79+
return;
80+
}
81+
82+
// Update FAB view if it's showing the same history item
83+
if (
84+
this.viewType === "floating-action-button" &&
85+
fabIndex === currentIndex &&
86+
currentIndex > -1
87+
) {
88+
this.resetChat();
89+
this.generateIMLikeMessages(message);
90+
return;
91+
}
92+
93+
// Update Widget view if it's showing the same history item
94+
if (
95+
this.viewType === "widget" &&
96+
widgetIndex === currentIndex &&
97+
currentIndex > -1
98+
) {
99+
this.resetChat();
100+
this.generateIMLikeMessages(message);
101+
return;
102+
}
103+
}
96104

97105
getMessages() {
98106
return this.messageStore.getMessages();
@@ -792,6 +800,7 @@ private updateMessages(message: Message[]) {
792800
messages.pop();
793801
this.messageStore.setMessages(messages);
794802
this.historyMessages.lastElementChild?.remove();
803+
this.plugin.history.update(this.plugin.settings.currentIndex, messages);
795804
}
796805

797806
removeMessage(header: Header, modelName: string) {

0 commit comments

Comments
 (0)