mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-10 18:39:22 +08:00
Properly reload the LLM chat when swapping between LLM Chat Notes
This commit is contained in:
parent
5d3cfcd0fc
commit
86096ee5b1
@ -125,9 +125,9 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
|
|
||||||
this.initializeEventListeners();
|
this.initializeEventListeners();
|
||||||
|
|
||||||
// Create a session when first loaded
|
// Don't create a session here - wait for refresh
|
||||||
this.createChatSession();
|
// This prevents the wrong session from being created for the wrong note
|
||||||
|
|
||||||
return this.$widget;
|
return this.$widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,7 +153,12 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await this.onGetData();
|
const data = await this.onGetData();
|
||||||
console.log("Loaded chat data:", data);
|
console.log(`Loading chat data for noteId: ${this.currentNoteId}`, data);
|
||||||
|
|
||||||
|
// Make sure we're loading data for the correct note
|
||||||
|
if (data && data.noteId && data.noteId !== this.currentNoteId) {
|
||||||
|
console.warn(`Data noteId ${data.noteId} doesn't match current noteId ${this.currentNoteId}`);
|
||||||
|
}
|
||||||
|
|
||||||
if (data && data.messages && Array.isArray(data.messages)) {
|
if (data && data.messages && Array.isArray(data.messages)) {
|
||||||
// Clear existing messages in the UI
|
// Clear existing messages in the UI
|
||||||
@ -171,11 +176,12 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
|
|
||||||
// Scroll to bottom
|
// Scroll to bottom
|
||||||
this.chatContainer.scrollTop = this.chatContainer.scrollHeight;
|
this.chatContainer.scrollTop = this.chatContainer.scrollHeight;
|
||||||
|
console.log(`Successfully loaded ${data.messages.length} messages for noteId: ${this.currentNoteId}`);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Error loading saved chat data:", e);
|
console.error(`Error loading saved chat data for noteId: ${this.currentNoteId}:`, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -191,13 +197,16 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Include the current note ID for tracking purposes
|
||||||
await this.onSaveData({
|
await this.onSaveData({
|
||||||
messages: this.messages,
|
messages: this.messages,
|
||||||
lastUpdated: new Date()
|
lastUpdated: new Date(),
|
||||||
|
noteId: this.currentNoteId // Include the note ID to help with debugging
|
||||||
});
|
});
|
||||||
|
console.log(`Saved chat data for noteId: ${this.currentNoteId} with ${this.messages.length} messages`);
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Error saving chat data:", e);
|
console.error(`Error saving chat data for noteId: ${this.currentNoteId}:`, e);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,12 +220,26 @@ export default class LlmChatPanel extends BasicWidget {
|
|||||||
await this.validateEmbeddingProviders();
|
await this.validateEmbeddingProviders();
|
||||||
|
|
||||||
// Get current note context if needed
|
// Get current note context if needed
|
||||||
this.currentNoteId = appContext.tabManager.getActiveContext()?.note?.noteId || null;
|
const currentActiveNoteId = appContext.tabManager.getActiveContext()?.note?.noteId || null;
|
||||||
|
|
||||||
// Try to load saved data
|
// If we're switching to a different note, we need to reset
|
||||||
|
if (this.currentNoteId !== currentActiveNoteId) {
|
||||||
|
console.log(`Note ID changed from ${this.currentNoteId} to ${currentActiveNoteId}, resetting chat panel`);
|
||||||
|
|
||||||
|
// Reset the UI and data
|
||||||
|
this.noteContextChatMessages.innerHTML = '';
|
||||||
|
this.messages = [];
|
||||||
|
this.sessionId = null;
|
||||||
|
this.hideSources(); // Hide any sources from previous note
|
||||||
|
|
||||||
|
// Update our current noteId
|
||||||
|
this.currentNoteId = currentActiveNoteId;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always try to load saved data for the current note
|
||||||
const hasSavedData = await this.loadSavedData();
|
const hasSavedData = await this.loadSavedData();
|
||||||
|
|
||||||
// Only create a new session if we don't have saved data
|
// Only create a new session if we don't have a session or saved data
|
||||||
if (!this.sessionId || !hasSavedData) {
|
if (!this.sessionId || !hasSavedData) {
|
||||||
// Create a new chat session
|
// Create a new chat session
|
||||||
await this.createChatSession();
|
await this.createChatSession();
|
||||||
|
@ -32,15 +32,37 @@ export default class AiChatTypeWidget extends TypeWidget {
|
|||||||
return this.$widget;
|
return this.$widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Override the refreshWithNote method to ensure we get note changes
|
||||||
|
async refreshWithNote(note: FNote | null | undefined) {
|
||||||
|
console.log("refreshWithNote called for note:", note?.noteId);
|
||||||
|
|
||||||
|
// Always force a refresh when the note changes
|
||||||
|
if (this.note?.noteId !== note?.noteId) {
|
||||||
|
console.log(`Note ID changed from ${this.note?.noteId} to ${note?.noteId}, forcing reset`);
|
||||||
|
this.isInitialized = false;
|
||||||
|
this.initPromise = null;
|
||||||
|
|
||||||
|
// Force refresh the chat panel with the new note
|
||||||
|
if (note) {
|
||||||
|
this.llmChatPanel.currentNoteId = note.noteId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Continue with regular doRefresh
|
||||||
|
await this.doRefresh(note);
|
||||||
|
}
|
||||||
|
|
||||||
async doRefresh(note: FNote | null | undefined) {
|
async doRefresh(note: FNote | null | undefined) {
|
||||||
try {
|
try {
|
||||||
|
console.log("doRefresh called for note:", note?.noteId);
|
||||||
|
|
||||||
// If we're already initializing, wait for that to complete
|
// If we're already initializing, wait for that to complete
|
||||||
if (this.initPromise) {
|
if (this.initPromise) {
|
||||||
await this.initPromise;
|
await this.initPromise;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only initialize once
|
// Initialize once or when note changes
|
||||||
if (!this.isInitialized) {
|
if (!this.isInitialized) {
|
||||||
console.log("Initializing AI Chat Panel for note:", note?.noteId);
|
console.log("Initializing AI Chat Panel for note:", note?.noteId);
|
||||||
|
|
||||||
@ -53,7 +75,8 @@ export default class AiChatTypeWidget extends TypeWidget {
|
|||||||
// Initialize with empty chat history
|
// Initialize with empty chat history
|
||||||
await this.saveData({
|
await this.saveData({
|
||||||
messages: [],
|
messages: [],
|
||||||
title: note.title
|
title: note.title,
|
||||||
|
noteId: note.noteId // Store the note ID in the data
|
||||||
});
|
});
|
||||||
console.log("Initialized empty chat history for new note");
|
console.log("Initialized empty chat history for new note");
|
||||||
} else {
|
} else {
|
||||||
@ -67,6 +90,10 @@ export default class AiChatTypeWidget extends TypeWidget {
|
|||||||
// Create a promise to track initialization
|
// Create a promise to track initialization
|
||||||
this.initPromise = (async () => {
|
this.initPromise = (async () => {
|
||||||
try {
|
try {
|
||||||
|
// Reset the UI before refreshing
|
||||||
|
this.llmChatPanel.noteContextChatMessages.innerHTML = '';
|
||||||
|
this.llmChatPanel.messages = [];
|
||||||
|
|
||||||
// This will load saved data via the getData callback
|
// This will load saved data via the getData callback
|
||||||
await this.llmChatPanel.refresh();
|
await this.llmChatPanel.refresh();
|
||||||
this.isInitialized = true;
|
this.isInitialized = true;
|
||||||
@ -89,24 +116,61 @@ export default class AiChatTypeWidget extends TypeWidget {
|
|||||||
// We don't need to refresh on entities reloaded for the chat
|
// We don't need to refresh on entities reloaded for the chat
|
||||||
}
|
}
|
||||||
|
|
||||||
async activeContextChangedEvent(data: EventData<"activeContextChanged">) {
|
async noteSwitched() {
|
||||||
// Only initialize if this becomes active and we're not initialized yet
|
console.log("Note switched to:", this.noteId);
|
||||||
if (this.isActive() && !this.isInitialized && !this.initPromise) {
|
|
||||||
try {
|
// Force a full reset when switching notes
|
||||||
this.initPromise = (async () => {
|
this.isInitialized = false;
|
||||||
try {
|
this.initPromise = null;
|
||||||
await this.llmChatPanel.refresh();
|
|
||||||
this.isInitialized = true;
|
if (this.note) {
|
||||||
} catch (e) {
|
// Update the chat panel with the new note ID before refreshing
|
||||||
console.error("Error initializing LlmChatPanel:", e);
|
this.llmChatPanel.currentNoteId = this.note.noteId;
|
||||||
}
|
|
||||||
})();
|
// Reset the chat panel UI
|
||||||
|
this.llmChatPanel.noteContextChatMessages.innerHTML = '';
|
||||||
|
this.llmChatPanel.messages = [];
|
||||||
|
this.llmChatPanel.sessionId = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the parent method to refresh
|
||||||
|
await super.noteSwitched();
|
||||||
|
}
|
||||||
|
|
||||||
await this.initPromise;
|
async activeContextChangedEvent(data: EventData<"activeContextChanged">) {
|
||||||
this.initPromise = null;
|
if (!this.isActive()) {
|
||||||
} catch (e) {
|
return;
|
||||||
console.error("Error in activeContextChangedEvent:", e);
|
}
|
||||||
|
|
||||||
|
console.log("Active context changed, refreshing AI Chat Panel");
|
||||||
|
|
||||||
|
// Always refresh when we become active - this ensures we load the correct note data
|
||||||
|
try {
|
||||||
|
// Reset initialization flag to force a refresh
|
||||||
|
this.isInitialized = false;
|
||||||
|
|
||||||
|
// Make sure the chat panel has the current note ID
|
||||||
|
if (this.note) {
|
||||||
|
this.llmChatPanel.currentNoteId = this.note.noteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.initPromise = (async () => {
|
||||||
|
try {
|
||||||
|
// Reset the UI before refreshing
|
||||||
|
this.llmChatPanel.noteContextChatMessages.innerHTML = '';
|
||||||
|
this.llmChatPanel.messages = [];
|
||||||
|
|
||||||
|
await this.llmChatPanel.refresh();
|
||||||
|
this.isInitialized = true;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error refreshing LlmChatPanel:", e);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
await this.initPromise;
|
||||||
|
this.initPromise = null;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error in activeContextChangedEvent:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user