Skip to content

Commit 8796953

Browse files
committed
Fix edit message
1 parent 64cf0a0 commit 8796953

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

src/aiChat.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { confirmClearData, tryRecoverJSON } from "./helpers/aiChatHelper.jsx";
3030

3131
import { SearchInChats } from "./components/aiChat/searchInChats.jsx";
3232

33-
import { format_chat_to_prompt, MessagePair, pairs_to_messages } from "./classes/message.js";
33+
import { format_chat_to_prompt, Message, MessagePair, pairs_to_messages } from "./classes/message.js";
3434

3535
import { getChatResponse, getChatResponseSync } from "./api/gpt.jsx";
3636
import * as providers from "./api/providers.js";
@@ -783,7 +783,7 @@ export default function Chat({ launchContext }) {
783783
if (newMessage?.files) {
784784
currentChatData.messages[idx].files = newMessage.files;
785785
// Process and cache files immediately after assignment
786-
currentChatData.messages[idx].processAndCacheFiles();
786+
currentChatData.messages[idx] = Message.processFiles(currentChatData.messages[idx]);
787787
}
788788

789789
currentChatData.messages[idx].second.content = "";
@@ -944,7 +944,7 @@ export default function Chat({ launchContext }) {
944944
currentChatData.messages.unshift(newMessagePair);
945945

946946
// Process and cache files in the message that's now in the chat
947-
currentChatData.messages[0].processAndCacheFiles();
947+
currentChatData.messages[0] = Message.processFiles(currentChatData.messages[0]);
948948

949949
setCurrentChatData(currentChatData); // possibly redundant, put here for safety and consistency
950950
await flushUpdateChat(currentChatData);

src/classes/message.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ export class Message {
2828
this.files = processFiles(this.files);
2929
}
3030
}
31+
32+
static processFiles(m) {
33+
m = structuredClone(m); // avoid mutating original
34+
let message = new Message(m);
35+
message.processAndCacheFiles();
36+
return message;
37+
}
3138
}
3239

3340
export class MessagePair {
@@ -91,8 +98,8 @@ export const pairs_to_messages = (pairs, query = null) => {
9198
if (!messagePair.first.content && !messagePair.files) continue;
9299

93100
// Process and cache files in the original MessagePair if needed
94-
if (messagePair.files && messagePair.processAndCacheFiles) {
95-
messagePair.processAndCacheFiles();
101+
if (messagePair.files) {
102+
messagePair = Message.processFiles(messagePair);
96103
}
97104

98105
chat.push(
@@ -173,18 +180,7 @@ export const messages_to_json = (chat, { readFiles = true, provider = null } = {
173180
console.assert(msg.role === "user", "Only user messages can have files");
174181

175182
// Process and cache files in the original chat object for memoization
176-
if (chat[i].processAndCacheFiles) {
177-
chat[i].processAndCacheFiles();
178-
// Update our copy with the processed files
179-
msg.files = chat[i].files;
180-
} else {
181-
// Fallback for plain objects that aren't Message instances
182-
// Process files and update both the original object and our copy for memoization
183-
console.log(`Processing files for plain object (no processAndCacheFiles method)`);
184-
const processedFiles = processFiles(msg.files);
185-
chat[i].files = processedFiles; // Update original for memoization
186-
msg.files = processedFiles; // Update copy for current use
187-
}
183+
msg.files = Message.processFiles(msg.files);
188184

189185
for (const file of msg.files) {
190186
const content = file.content || file; // backward compatibility

0 commit comments

Comments
 (0)