From 6033baeb86efe1f884d787f6cb5d6230d1045ca0 Mon Sep 17 00:00:00 2001 From: akumatus Date: Thu, 3 Apr 2025 14:53:49 +0000 Subject: [PATCH] refactor(core): add useAIChatConfig hook (#11424) Close [BS-2583](https://linear.app/affine-design/issue/BS-2583). --- .../ai-chat-chips/chat-panel-chips.ts | 7 +- .../ai/peek-view/chat-block-peek-view.ts | 34 ++++++ .../src/blocksuite/ai/peek-view/styles.ts | 4 +- .../hooks/affine/use-ai-chat-config.ts | 115 ++++++++++++++++++ .../pages/workspace/detail-page/tabs/chat.tsx | 100 +++------------ .../view/ai-chat-block-peek-view/index.tsx | 16 +-- 6 files changed, 175 insertions(+), 101 deletions(-) create mode 100644 packages/frontend/core/src/components/hooks/affine/use-ai-chat-config.ts diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-chips/chat-panel-chips.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-chips/chat-panel-chips.ts index 7e896be57..2bddd6f18 100644 --- a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-chips/chat-panel-chips.ts +++ b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-chips/chat-panel-chips.ts @@ -100,6 +100,9 @@ export class ChatPanelChips extends SignalWatcher( @property({ attribute: false }) accessor searchMenuConfig!: SearchMenuConfig; + @property({ attribute: false }) + accessor portalContainer: HTMLElement | null = null; + @property({ attribute: 'data-testid', reflect: true }) accessor testId = 'chat-panel-chips'; @@ -267,7 +270,7 @@ export class ChatPanelChips extends SignalWatcher( portalStyles: { zIndex: 'var(--affine-z-index-popover)', }, - container: document.body, + container: this.portalContainer ?? document.body, computePosition: { referenceElement: this.addButton, placement: 'top-start', @@ -306,7 +309,7 @@ export class ChatPanelChips extends SignalWatcher( portalStyles: { zIndex: 'var(--affine-z-index-popover)', }, - container: document.body, + container: this.portalContainer ?? document.body, computePosition: { referenceElement: this.moreCandidateButton, placement: 'top-start', diff --git a/packages/frontend/core/src/blocksuite/ai/peek-view/chat-block-peek-view.ts b/packages/frontend/core/src/blocksuite/ai/peek-view/chat-block-peek-view.ts index d27be9e86..e31d76b2a 100644 --- a/packages/frontend/core/src/blocksuite/ai/peek-view/chat-block-peek-view.ts +++ b/packages/frontend/core/src/blocksuite/ai/peek-view/chat-block-peek-view.ts @@ -23,6 +23,11 @@ import { queryHistoryMessages, } from '../_common/chat-actions-handle'; import { type AIChatBlockModel } from '../blocks'; +import type { + ChatChip, + DocDisplayConfig, + SearchMenuConfig, +} from '../components/ai-chat-chips'; import type { AINetworkSearchConfig } from '../components/ai-chat-input'; import type { ChatMessage } from '../components/ai-chat-messages'; import { ChatMessagesSchema } from '../components/ai-chat-messages'; @@ -155,6 +160,16 @@ export class AIChatBlockPeekView extends LitElement { }; private readonly _getContextId = async () => { + if (this._chatContextId) { + return this._chatContextId; + } + const sessionId = await this._getSessionId(); + if (sessionId) { + this._chatContextId = await AIProvider.context?.createContext( + this.host.doc.workspace.id, + sessionId + ); + } return this._chatContextId; }; @@ -273,6 +288,10 @@ export class AIChatBlockPeekView extends LitElement { this.chatContext = { ...this.chatContext, ...context }; }; + updateChips = (chips: ChatChip[]) => { + this.chips = chips; + }; + /** * Clean current chat messages and delete the newly created AI chat block */ @@ -524,6 +543,7 @@ export class AIChatBlockPeekView extends LitElement {