From cd884312eaeb92e1baacd121101d0d19da6eaac5 Mon Sep 17 00:00:00 2001 From: akumatus Date: Thu, 6 Mar 2025 07:00:57 +0000 Subject: [PATCH] fix(core): missing clean up subscription (#10636) --- .../src/blocksuite/ai/chat-panel/index.ts | 37 ++++++++++++------- .../src/blocksuite/ai/provider/request.ts | 11 ++---- .../pages/workspace/detail-page/tabs/chat.tsx | 2 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts index e8ae0de01e..aa69eaa94d 100644 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts +++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts @@ -10,6 +10,7 @@ import { import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/utils'; import type { Store } from '@blocksuite/affine/store'; import { HelpIcon, InformationIcon } from '@blocksuite/icons/lit'; +import { type Signal, signal } from '@preact/signals-core'; import { css, html, type PropertyValues } from 'lit'; import { property, state } from 'lit/decorators.js'; import { createRef, type Ref, ref } from 'lit/directives/ref.js'; @@ -271,6 +272,10 @@ export class ChatPanel extends SignalWatcher( private _chatContextId: string | null | undefined = null; + private _isOpen: Signal = signal(false); + + private _width: Signal = signal(undefined); + private readonly _scrollToEnd = () => { if (!this._wheelTriggered) { this._chatMessages.value?.scrollToEnd(); @@ -306,8 +311,7 @@ export class ChatPanel extends SignalWatcher( private readonly _initPanel = async () => { try { - const isOpen = !!this.appSidebarConfig.isOpen().signal.value; - if (!isOpen) return; + if (!this._isOpen.value) return; const userId = (await AIProvider.userInfo)?.id; if (!userId) return; @@ -383,14 +387,6 @@ export class ChatPanel extends SignalWatcher( }) .catch(console.error); } - - this._disposables.add( - this.appSidebarConfig.isOpen().signal.subscribe(isOpen => { - if (isOpen && this.isLoading) { - this._initPanel().catch(console.error); - } - }) - ); } override connectedCallback() { @@ -422,6 +418,22 @@ export class ChatPanel extends SignalWatcher( } }) ); + + const isOpen = this.appSidebarConfig.isOpen(); + this._isOpen = isOpen.signal; + this._disposables.add(isOpen.cleanup); + + const width = this.appSidebarConfig.getWidth(); + this._width = width.signal; + this._disposables.add(width.cleanup); + + this._disposables.add( + this._isOpen.subscribe(isOpen => { + if (isOpen && this.isLoading) { + this._initPanel().catch(console.error); + } + }) + ); } updateContext = (context: Partial) => { @@ -440,10 +452,9 @@ export class ChatPanel extends SignalWatcher( }; override render() { - const panelWidth = this.appSidebarConfig.getWidth().signal.value; + const width = this._width.value || 0; const style = styleMap({ - padding: - panelWidth && panelWidth > 540 ? '8px 24px 0 24px' : '8px 12px 0 12px', + padding: width > 540 ? '8px 24px 0 24px' : '8px 12px 0 12px', }); return html`
diff --git a/packages/frontend/core/src/blocksuite/ai/provider/request.ts b/packages/frontend/core/src/blocksuite/ai/provider/request.ts index 039dae2136..5590faae5c 100644 --- a/packages/frontend/core/src/blocksuite/ai/provider/request.ts +++ b/packages/frontend/core/src/blocksuite/ai/provider/request.ts @@ -70,8 +70,10 @@ async function createSessionMessage({ sessionId: providedSessionId, attachments, params, - retry = false, -}: TextToTextOptions) { +}: TextToTextOptions): Promise<{ + sessionId: string; + messageId: string; +}> { if (!promptName && !providedSessionId) { throw new Error('promptName or sessionId is required'); } @@ -107,10 +109,6 @@ async function createSessionMessage({ ) ).filter(Boolean) as File[]; } - if (retry) - return { - sessionId, - }; const messageId = await client.createMessage(options); return { @@ -157,7 +155,6 @@ export function textToText({ attachments, params, sessionId, - retry, }); _sessionId = message.sessionId; _messageId = message.messageId; diff --git a/packages/frontend/core/src/desktop/pages/workspace/detail-page/tabs/chat.tsx b/packages/frontend/core/src/desktop/pages/workspace/detail-page/tabs/chat.tsx index bb8c30f0c2..b4674c8468 100644 --- a/packages/frontend/core/src/desktop/pages/workspace/detail-page/tabs/chat.tsx +++ b/packages/frontend/core/src/desktop/pages/workspace/detail-page/tabs/chat.tsx @@ -54,7 +54,6 @@ export const EditorChatPanel = forwardRef(function EditorChatPanel( chatPanelRef.current = new ChatPanel(); chatPanelRef.current.host = editor.host; chatPanelRef.current.doc = editor.doc; - containerRef.current?.append(chatPanelRef.current); const searchService = framework.get(AINetworkSearchService); const docDisplayMetaService = framework.get(DocDisplayMetaService); const workspaceService = framework.get(WorkspaceService); @@ -101,6 +100,7 @@ export const EditorChatPanel = forwardRef(function EditorChatPanel( SpecProvider._.getSpec('preview:page') ); chatPanelRef.current.previewSpecBuilder = previewSpecBuilder; + containerRef.current?.append(chatPanelRef.current); } else { chatPanelRef.current.host = editor.host; chatPanelRef.current.doc = editor.doc;