Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): isolate AI sidebar failures from notes

tianma-if9a325158e868
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 800d7da2..40b39c2b 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -111,6 +111,7 @@ import {
import { memoShareQueryKey, ShareMemoDialog } from "./dialogs/ShareMemoDialog";
import { ShareNoteImageDialog } from "./dialogs/ShareNoteImageDialog";
import { AiSidebar, readAiSidebarOpen, readAiSidebarWidth, writeAiSidebarOpen } from "./ai-sidebar/AiSidebar";
+import { AiSidebarErrorBoundary } from "./ai-sidebar/AiSidebarErrorBoundary";
import { api } from "@/lib/api";
import { isDesktopResourceRuntime, stageDesktopResource, toDesktopResourceDownloadUrl, toDesktopResourceUrl } from "@/lib/desktop-resources";
import { contentReferencesStagedResourceUrl, findMatchingMemoResource, repairMemoStagedResourceUrls, repairTiptapStagedResourceUrls } from "@/lib/staged-resource-repair";
@@ -152,6 +153,7 @@ import {
getMemoSaveConflictInfoFromQueueItem,
} from "@/lib/memo-save-conflict";
import { copyTextToClipboard } from "@/lib/clipboard";
+import { createClientUuid } from "@/lib/client-id";
import { isLocalMemoId, remapLocalDraftMemoId } from "@/lib/local-mirror";
import { shouldAcceptRemoteMemoDetail } from "@/lib/memo-detail-freshness";
import type { EdgeEverRepository } from "@/lib/repository";
@@ -2046,7 +2048,7 @@ const RichEditorPane = ({
}
if (!pin) return;
setSelectionPin(pin);
- setSelectionRequest({ id: crypto.randomUUID(), kind });
+ setSelectionRequest({ id: createClientUuid(), kind });
setAiSidebarOpen(true);
}, [editor, effectiveReadOnly, setAiSidebarOpen, useMarkdownSourceEditor, useMobilePlainTextEditor]);
@@ -4373,24 +4375,26 @@ const RichEditorPane = ({
/>
)}
</div>
- <AiSidebar
- open={aiAssistantOpen}
- onOpenChange={setAiSidebarOpen}
- companionAvailable={companionAvailable}
- selectionMarkdown={selectionPin?.memoId === memo.id ? selectionPin.sentText : ""}
- selectionPin={selectionPin?.memoId === memo.id ? selectionPin : null}
- selectionRequest={selectionRequest}
- onDismissSelectionPin={() => setSelectionPin(null)}
- onReplaceSelection={replacePinnedSelection}
- contentMarkdown={currentMarkdownForAi}
- memoId={memo.id}
- notebookId={memo.notebookId}
- notebookTitle={notebookOptions.find((notebook) => notebook.id === memo.notebookId)?.name}
- noteTitle={title}
- beforeCompanionApply={beforeCompanionApply}
- onCompanionNotesChanged={onCompanionNotesChanged}
- onOpenCompanionNote={onOpenCompanionNote}
- />
+ <AiSidebarErrorBoundary open={aiAssistantOpen} onOpenChange={setAiSidebarOpen}>
+ <AiSidebar
+ open={aiAssistantOpen}
+ onOpenChange={setAiSidebarOpen}
+ companionAvailable={companionAvailable}
+ selectionMarkdown={selectionPin?.memoId === memo.id ? selectionPin.sentText : ""}
+ selectionPin={selectionPin?.memoId === memo.id ? selectionPin : null}
+ selectionRequest={selectionRequest}
+ onDismissSelectionPin={() => setSelectionPin(null)}
+ onReplaceSelection={replacePinnedSelection}
+ contentMarkdown={currentMarkdownForAi}
+ memoId={memo.id}
+ notebookId={memo.notebookId}
+ notebookTitle={notebookOptions.find((notebook) => notebook.id === memo.notebookId)?.name}
+ noteTitle={title}
+ beforeCompanionApply={beforeCompanionApply}
+ onCompanionNotesChanged={onCompanionNotesChanged}
+ onOpenCompanionNote={onOpenCompanionNote}
+ />
+ </AiSidebarErrorBoundary>
</div>
);
};
diff --git a/apps/web/src/components/ai-sidebar/AiSidebar.tsx b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
index 7cd663d3..7d9bcfce 100644
--- a/apps/web/src/components/ai-sidebar/AiSidebar.tsx
+++ b/apps/web/src/components/ai-sidebar/AiSidebar.tsx
@@ -36,6 +36,7 @@ import {
import { Reasoning, ReasoningContent, ReasoningTrigger } from "@/components/ai-elements/reasoning";
import { api, ApiRequestError } from "@/lib/api";
import { copyTextToClipboard } from "@/lib/clipboard";
+import { createClientUuid } from "@/lib/client-id";
import {
AI_ATTACHMENT_ACCEPT,
AiAttachmentError,
@@ -733,7 +734,7 @@ function AiSidebarSession({
const [turns, setTurns] = useState<CompanionTurn[]>([]);
const [actions, setActions] = useState<CompanionAction[]>([]);
const [localTurns, setLocalTurns] = useState<LocalTurn[]>([]);
- const [threadId, setThreadId] = useState<string>(() => readAiSidebarThread() ?? crypto.randomUUID());
+ const [threadId, setThreadId] = useState<string>(() => readAiSidebarThread() ?? createClientUuid());
const [attachments, setAttachments] = useState<PendingAttachment[]>([]);
const [attachmentError, setAttachmentError] = useState<string | null>(null);
const [conflicts, setConflicts] = useState<Record<string, string>>({});
@@ -831,7 +832,7 @@ function AiSidebarSession({
if (event.type === "tool") {
setLocalTurns((previous) => previous.map((turn) => {
if (turn.id !== turnId || turn.status === "cancelled") return turn;
- const row = { id: crypto.randomUUID(), name: event.name, status: event.status, title: event.title };
+ const row = { id: createClientUuid(), name: event.name, status: event.status, title: event.title };
let index = -1;
for (let toolIndex = turn.tools.length - 1; toolIndex >= 0; toolIndex -= 1) {
const item = turn.tools[toolIndex];
@@ -995,7 +996,7 @@ function AiSidebarSession({
throw new Error("unavailable");
}
const controller = new AbortController();
- const id = crypto.randomUUID();
+ const id = createClientUuid();
const snapshot = attachments;
attachmentSnapshot.current = snapshot;
locked.current = true;
@@ -1214,7 +1215,7 @@ function AiSidebarSession({
existing = [...existing, prepared];
added.push({
...prepared,
- localId: crypto.randomUUID(),
+ localId: createClientUuid(),
previewUrl: prepared.mediaType.startsWith("image/") ? `data:${prepared.mediaType};base64,${prepared.base64Data}` : null,
});
} catch (cause) {
@@ -1382,7 +1383,7 @@ function AiSidebarSession({
title={threadTitle}
onSelect={rememberThread}
onCreate={() => {
- if (threads.some((thread) => thread.id === threadId)) rememberThread(crypto.randomUUID());
+ if (threads.some((thread) => thread.id === threadId)) rememberThread(createClientUuid());
}}
/>
) : (
diff --git a/apps/web/src/components/ai-sidebar/AiSidebarErrorBoundary.tsx b/apps/web/src/components/ai-sidebar/AiSidebarErrorBoundary.tsx
new file mode 100644
index 00000000..9634c9f9
--- /dev/null
+++ b/apps/web/src/components/ai-sidebar/AiSidebarErrorBoundary.tsx
@@ -0,0 +1,56 @@
+import React, { type ErrorInfo, type ReactNode } from "react";
+
+type Props = {
+ children: ReactNode;
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+};
+
+type State = { failed: boolean };
+
+export class AiSidebarErrorBoundary extends React.Component<Props, State> {
+ state: State = { failed: false };
+
+ static getDerivedStateFromError(): State {
+ return { failed: true };
+ }
+
+ componentDidCatch(error: Error, info: ErrorInfo) {
+ console.error("Caught AI sidebar error", error, info.componentStack);
+ }
+
+ render() {
+ if (!this.state.failed) return this.props.children;
+ if (!this.props.open) return null;
+ const zh = navigator.language.toLowerCase().startsWith("zh");
+ return (
+ <aside
+ className="relative h-full w-[min(100vw,380px)] shrink-0 border-l border-slate-200 bg-card max-md:fixed max-md:inset-y-0 max-md:right-0 max-md:z-50"
+ role="alert"
+ >
+ <div className="flex h-full flex-col items-start justify-center gap-3 p-6 text-slate-900">
+ <h2 className="text-base font-semibold">{zh ? "AI 侧栏暂时无法打开" : "The AI sidebar cannot open"}</h2>
+ <p className="text-sm text-slate-600">
+ {zh ? "笔记仍可查看和编辑。你可以重试侧栏,或关闭后继续使用笔记。" : "You can still view and edit your note. Retry the sidebar or close it to continue."}
+ </p>
+ <div className="flex gap-2">
+ <button
+ type="button"
+ className="rounded-lg bg-emerald-500 px-3 py-2 text-sm font-medium text-white hover:bg-emerald-600"
+ onClick={() => this.setState({ failed: false })}
+ >
+ {zh ? "重试侧栏" : "Retry sidebar"}
+ </button>
+ <button
+ type="button"
+ className="rounded-lg border border-slate-300 px-3 py-2 text-sm font-medium hover:bg-slate-50"
+ onClick={() => this.props.onOpenChange(false)}
+ >
+ {zh ? "关闭" : "Close"}
+ </button>
+ </div>
+ </div>
+ </aside>
+ );
+ }
+}