"Chat with Notes" launcher works

This commit is contained in:
perf3ct 2025-03-28 20:16:16 +00:00
parent a05013c8f4
commit 921a243b0d
No known key found for this signature in database
GPG Key ID: 569C4EEC436F5232
3 changed files with 68 additions and 28 deletions

View File

@ -3,10 +3,12 @@ import LlmChatPanel from "../llm_chat_panel.js";
import { type EventData } from "../../components/app_context.js"; import { type EventData } from "../../components/app_context.js";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import server from "../../services/server.js"; import server from "../../services/server.js";
import toastService from "../../services/toast.js";
export default class AiChatTypeWidget extends TypeWidget { export default class AiChatTypeWidget extends TypeWidget {
private llmChatPanel: LlmChatPanel; private llmChatPanel: LlmChatPanel;
private isInitialized: boolean = false; private isInitialized: boolean = false;
private initPromise: Promise<void> | null = null;
constructor() { constructor() {
super(); super();
@ -25,11 +27,30 @@ export default class AiChatTypeWidget extends TypeWidget {
} }
async doRefresh(note: FNote | null | undefined) { async doRefresh(note: FNote | null | undefined) {
// Initialize the chat panel if not already done try {
// If we're already initializing, wait for that to complete
if (this.initPromise) {
await this.initPromise;
return;
}
// Only initialize once
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);
// Create a promise to track initialization
this.initPromise = (async () => {
try {
await this.llmChatPanel.refresh(); await this.llmChatPanel.refresh();
this.isInitialized = true; this.isInitialized = true;
} catch (e) {
console.error("Error initializing LlmChatPanel:", e);
toastService.showError("Failed to initialize chat panel. Try reloading.");
}
})();
await this.initPromise;
this.initPromise = null;
} }
// If this is a newly created note, we can initialize the content // If this is a newly created note, we can initialize the content
@ -48,6 +69,10 @@ export default class AiChatTypeWidget extends TypeWidget {
console.error("Error initializing AI Chat note content:", e); console.error("Error initializing AI Chat note content:", e);
} }
} }
} catch (e) {
console.error("Error in doRefresh:", e);
toastService.showError("Error refreshing chat. Please try again.");
}
} }
async entitiesReloadedEvent(data: EventData<"entitiesReloaded">) { async entitiesReloadedEvent(data: EventData<"entitiesReloaded">) {
@ -55,10 +80,23 @@ export default class AiChatTypeWidget extends TypeWidget {
} }
async activeContextChangedEvent(data: EventData<"activeContextChanged">) { async activeContextChangedEvent(data: EventData<"activeContextChanged">) {
// Only refresh when this becomes active and we're not initialized yet // Only initialize if this becomes active and we're not initialized yet
if (this.isActive() && !this.isInitialized) { if (this.isActive() && !this.isInitialized && !this.initPromise) {
try {
this.initPromise = (async () => {
try {
await this.llmChatPanel.refresh(); await this.llmChatPanel.refresh();
this.isInitialized = true; this.isInitialized = true;
} catch (e) {
console.error("Error initializing LlmChatPanel:", e);
}
})();
await this.initPromise;
this.initPromise = null;
} catch (e) {
console.error("Error in activeContextChangedEvent:", e);
}
} }
} }
@ -74,6 +112,7 @@ export default class AiChatTypeWidget extends TypeWidget {
}); });
} catch (e) { } catch (e) {
console.error("Error saving AI Chat data:", e); console.error("Error saving AI Chat data:", e);
toastService.showError("Failed to save chat data");
} }
} }

View File

@ -70,10 +70,10 @@ export default function buildLaunchBarConfig() {
{ id: "_lbNoteMap", title: t("hidden-subtree.note-map-title"), type: "launcher", targetNoteId: "_globalNoteMap", icon: "bx bxs-network-chart" }, { id: "_lbNoteMap", title: t("hidden-subtree.note-map-title"), type: "launcher", targetNoteId: "_globalNoteMap", icon: "bx bxs-network-chart" },
{ {
id: "_lbLlmChat", id: "_lbLlmChat",
title: t("hidden-subtree.ai-chat-title"), title: t("hidden-subtree.llm-chat-title"),
type: "launcher", type: "launcher",
command: "createAiChat",
icon: "bx bx-bot", icon: "bx bx-bot",
builtinWidget: "aiChatLauncher",
attributes: [ attributes: [
{ type: "label", name: "desktopOnly" } { type: "label", name: "desktopOnly" }
] ]

View File

@ -247,7 +247,8 @@
"advanced-title": "Advanced", "advanced-title": "Advanced",
"visible-launchers-title": "Visible Launchers", "visible-launchers-title": "Visible Launchers",
"user-guide": "User Guide", "user-guide": "User Guide",
"localization": "Language & Region" "localization": "Language & Region",
"inbox-title": "Inbox"
}, },
"notes": { "notes": {
"new-note": "New note", "new-note": "New note",