Skip to content

Commit 6008f90

Browse files
committed
Refactor ChatEditor to track the current conversation ID and guard
message callbacks * Added a new member `m_viewingConvId` to `ChatEditor` to keep the ID of the conversation currently being displayed. * Updated the `currentEditorChanged` signal handler to set `m_viewingConvId` and use it instead of a local `convId`. The handler now early‑returns when the signal originates from a different editor instance. * Updated all callbacks that receive a `Message` (`onMessageAppended`, `onPendingMessageChanged`) and the follow‑up widget creation to ignore messages whose `convId` does not match `m_viewingConvId`. These changes ensure that each editor instance only processes messages for its own conversation, preventing cross‑talk between editors when the user switches tabs. This also makes the code clearer by explicitly storing the conversation context.
1 parent db38ea2 commit 6008f90

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

llamachateditor.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,25 @@ ChatEditor::ChatEditor()
156156
&EditorManager::currentEditorChanged,
157157
this,
158158
[this](Core::IEditor *editor) {
159-
if (editor == this) {
160-
const QString convId = QString::fromUtf8(m_document->contents());
161-
ViewingChat chat = ChatManager::instance().getViewingChat(convId);
162-
163-
// A new conversation has one root message
164-
if (chat.messages.size() > 1) {
165-
refreshMessages(chat.messages, chat.conv.currNode);
166-
} else {
167-
ChatManager::instance().refreshServerProps();
168-
}
169-
170-
ChatManager::instance().setCurrentConversation(convId);
171-
m_document->setPreferredDisplayName(chat.conv.name);
159+
if (editor != this)
160+
return;
172161

173-
EditorManager::instance()->updateWindowTitles();
162+
m_viewingConvId = QString::fromUtf8(m_document->contents());
163+
ViewingChat chat = ChatManager::instance().getViewingChat(m_viewingConvId);
174164

175-
m_input->setFocus();
165+
// A new conversation has one root message
166+
if (chat.messages.size() > 1) {
167+
refreshMessages(chat.messages, chat.conv.currNode);
168+
} else {
169+
ChatManager::instance().refreshServerProps();
176170
}
171+
172+
ChatManager::instance().setCurrentConversation(m_viewingConvId);
173+
m_document->setPreferredDisplayName(chat.conv.name);
174+
175+
EditorManager::instance()->updateWindowTitles();
176+
177+
m_input->setFocus();
177178
});
178179

179180
// Make sure we have the markdown content before saving
@@ -288,6 +289,9 @@ void ChatEditor::createFollowUpWidget(const QString &convId,
288289
qint64 leafNodeId,
289290
const QStringList &questions)
290291
{
292+
if (convId != m_viewingConvId)
293+
return;
294+
291295
if (m_followUpWidget) {
292296
m_followUpWidget->deleteLater();
293297
m_followUpWidget = nullptr;
@@ -426,6 +430,9 @@ void ChatEditor::refreshMessages(const QVector<Message> &messages, qint64 leafNo
426430

427431
void ChatEditor::onMessageAppended(const Message &msg, qint64 pendingId)
428432
{
433+
if (msg.convId != m_viewingConvId)
434+
return;
435+
429436
ViewingChat chat = ChatManager::instance().getViewingChat(msg.convId);
430437
if (pendingId < 0) {
431438
refreshMessages(chat.messages, msg.id);
@@ -513,6 +520,9 @@ void ChatEditor::onMessageAppended(const Message &msg, qint64 pendingId)
513520

514521
void ChatEditor::onPendingMessageChanged(const Message &pm)
515522
{
523+
if (pm.convId != m_viewingConvId)
524+
return;
525+
516526
ChatMessage *w = nullptr;
517527

518528
auto it = std::find_if(m_messageWidgets.begin(),

llamachateditor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public slots:
9191
bool m_searchActive{false};
9292

9393
SearchToolbar *m_searchToolbar{nullptr};
94+
QString m_viewingConvId;
9495
};
9596

9697
void setupChatEditor();

0 commit comments

Comments
 (0)