From f8be0cc465123299f794f21154baecedfecc3970 Mon Sep 17 00:00:00 2001 From: Cats Juice Date: Mon, 7 Jul 2025 20:32:13 +0800 Subject: [PATCH] fix(core): chat history not show in independent page (#13069) ## Summary by CodeRabbit * **New Features** * Added the ability to reload the current AI chat session, clearing and reinitializing the chat context. * Introduced a function to close the chat history menu in the AI chat toolbar. * Enabled seamless opening of specific chat sessions from the chat toolbar, with improved loading state handling and error feedback. --- .../ai-chat-content/ai-chat-content.ts | 5 +++ .../ai-chat-toolbar/ai-chat-toolbar.ts | 4 +++ .../desktop/pages/workspace/chat/index.tsx | 34 ++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/ai-chat-content.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/ai-chat-content.ts index 9d9817e077..d9a2a21da3 100644 --- a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/ai-chat-content.ts +++ b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/ai-chat-content.ts @@ -308,6 +308,11 @@ export class AIChatContent extends SignalWatcher( this.closePreviewPanel(true); } + public reloadSession() { + this.reset(); + this.initChatContent().catch(console.error); + } + public openPreviewPanel(content?: TemplateResult<1>) { this.showPreviewPanel = true; if (content) this.previewPanelContent = content; diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-toolbar/ai-chat-toolbar.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-toolbar/ai-chat-toolbar.ts index fed17e468e..ebeef8456e 100644 --- a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-toolbar/ai-chat-toolbar.ts +++ b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-toolbar/ai-chat-toolbar.ts @@ -186,4 +186,8 @@ export class AIChatToolbar extends WithDisposable(ShadowlessElement) { closeOnClickAway: true, }); }; + + public closeHistoryMenu() { + this.abortController?.abort(); + } } diff --git a/packages/frontend/core/src/desktop/pages/workspace/chat/index.tsx b/packages/frontend/core/src/desktop/pages/workspace/chat/index.tsx index f02fb97814..f72c5323f1 100644 --- a/packages/frontend/core/src/desktop/pages/workspace/chat/index.tsx +++ b/packages/frontend/core/src/desktop/pages/workspace/chat/index.tsx @@ -59,6 +59,7 @@ export const Component = () => { null ); const [isTogglingPin, setIsTogglingPin] = useState(false); + const [isOpeningSession, setIsOpeningSession] = useState(false); const chatContainerRef = useRef(null); const chatToolContainerRef = useRef(null); const widthSignalRef = useRef>(signal(0)); @@ -109,6 +110,25 @@ export const Component = () => { } }, [client, createSession, currentSession, isTogglingPin, workspaceId]); + const onOpenSession = useCallback( + (sessionId: string) => { + if (isOpeningSession) return; + setIsOpeningSession(true); + client + .getSession(workspaceId, sessionId) + .then(session => { + setCurrentSession(session); + chatContent?.reloadSession(); + chatTool?.closeHistoryMenu(); + }) + .catch(console.error) + .finally(() => { + setIsOpeningSession(false); + }); + }, + [chatContent, chatTool, client, isOpeningSession, workspaceId] + ); + // create a temp doc/host for ai-chat-content useEffect(() => { let tempDoc: Doc | null = null; @@ -187,6 +207,9 @@ export const Component = () => { } tool.session = currentSession; + tool.workspaceId = workspaceId; + tool.docDisplayConfig = docDisplayConfig; + tool.onOpenSession = onOpenSession; tool.onNewSession = () => { if (!currentSession) return; @@ -204,7 +227,16 @@ export const Component = () => { chatToolContainerRef.current.append(tool); setChatTool(tool); } - }, [chatContent, chatTool, currentSession, isHeaderProvided, togglePin]); + }, [ + chatContent, + chatTool, + currentSession, + docDisplayConfig, + isHeaderProvided, + onOpenSession, + togglePin, + workspaceId, + ]); const onChatContainerRef = useCallback((node: HTMLDivElement) => { if (node) {