From 6e9487a9e166044402b8e60938fa66af92cd5667 Mon Sep 17 00:00:00 2001 From: Wu Yue Date: Tue, 1 Jul 2025 19:20:20 +0800 Subject: [PATCH] feat(core): remove chat-panel component's dependency on doc (#12975) Close [AI-259](https://linear.app/affine-design/issue/AI-259) Close [AI-243](https://linear.app/affine-design/issue/AI-243) ## Summary by CodeRabbit * **New Features** * Introduced a unified AI chat content component to manage and display chat interactions. * Added new chat block message components for improved chat message rendering. * **Refactor** * Simplified and unified session management across all AI chat components, now passing full session objects instead of session IDs. * Updated component and property names for clarity and consistency (e.g., chat message and block message components). * Consolidated chat history and actions retrieval for a more streamlined chat experience. * Removed redundant session ID getters and replaced them with direct session object usage. * Streamlined chat panel and composer components by removing internal message and context state management. * **Bug Fixes** * Improved handling of chat session state and loading, reducing redundant state properties. * Enhanced event handling to prevent errors when chat parameters are missing. * **Tests** * Removed outdated chat clearing test cases to align with new chat state management. * **Chores** * Updated import paths and reorganized module exports for better maintainability. --- .../ai/_common/chat-actions-handle.ts | 18 +- .../core/src/blocksuite/ai/actions/types.ts | 10 +- .../ai/blocks/ai-chat-block/ai-chat-block.ts | 4 +- .../components/ai-chat-messages.ts | 12 +- .../src/blocksuite/ai/chat-panel/index.ts | 471 +++++------------- .../ai/chat-panel/message/assistant.ts | 14 +- .../ai-chat-composer/ai-chat-composer.ts | 90 +--- .../ai-chat-content/ai-chat-content.ts | 321 ++++++++++++ .../ai/components/ai-chat-content/index.ts | 2 + .../ai-chat-content/type.ts} | 7 +- .../components/ai-chat-input/ai-chat-input.ts | 34 +- .../ai-chat-input/preference-popup.ts | 2 +- .../ai-chat-messages/ai-chat-messages.ts} | 60 +-- .../ai/components/ai-chat-messages/index.ts | 1 + .../ai-history-clear/ai-history-clear.ts | 12 +- .../ai/components/chat-action-list.ts | 5 +- .../src/blocksuite/ai/components/copy-more.ts | 7 +- .../ai/components/playground/chat.ts | 74 ++- .../ai/components/playground/content.ts | 2 +- .../core/src/blocksuite/ai/effects.ts | 14 +- .../ai/peek-view/chat-block-peek-view.ts | 96 ++-- .../src/blocksuite/ai/provider/ai-provider.ts | 8 +- .../blocksuite/ai/provider/setup-provider.tsx | 16 +- .../core/src/blocksuite/ai/utils/extract.ts | 2 +- .../workspace/detail-page/detail-page.tsx | 7 +- .../view/doc-preview/doc-peek-view.tsx | 7 +- .../e2e/basic/chat.spec.ts | 19 - .../e2e/settings/embedding.spec.ts | 3 - 28 files changed, 669 insertions(+), 649 deletions(-) create mode 100644 packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/ai-chat-content.ts create mode 100644 packages/frontend/core/src/blocksuite/ai/components/ai-chat-content/index.ts rename packages/frontend/core/src/blocksuite/ai/{chat-panel/chat-context.ts => components/ai-chat-content/type.ts} (74%) rename packages/frontend/core/src/blocksuite/ai/{chat-panel/chat-panel-messages.ts => components/ai-chat-messages/ai-chat-messages.ts} (90%) diff --git a/packages/frontend/core/src/blocksuite/ai/_common/chat-actions-handle.ts b/packages/frontend/core/src/blocksuite/ai/_common/chat-actions-handle.ts index 54189fc47d..585c095fa7 100644 --- a/packages/frontend/core/src/blocksuite/ai/_common/chat-actions-handle.ts +++ b/packages/frontend/core/src/blocksuite/ai/_common/chat-actions-handle.ts @@ -1,4 +1,3 @@ -import { ChatHistoryOrder } from '@affine/graphql'; import { EdgelessCRUDIdentifier } from '@blocksuite/affine/blocks/surface'; import { Bound, @@ -69,14 +68,15 @@ export type ChatAction = { export async function queryHistoryMessages( workspaceId: string, - docId: string, - forkSessionId: string + forkSessionId: string, + docId?: string ) { // Get fork session messages - const histories = await AIProvider.histories?.chats(workspaceId, docId, { - sessionId: forkSessionId, - messageOrder: ChatHistoryOrder.asc, - }); + const histories = await AIProvider.histories?.chats( + workspaceId, + forkSessionId, + docId + ); if (!histories || !histories.length) { return []; @@ -117,8 +117,8 @@ export async function constructRootChatBlockMessages( const userInfo = await AIProvider.userInfo; const forkMessages = (await queryHistoryMessages( doc.workspace.id, - doc.id, - forkSessionId + forkSessionId, + doc.id )) as ChatMessage[]; return constructUserInfoWithMessages(forkMessages, userInfo); } diff --git a/packages/frontend/core/src/blocksuite/ai/actions/types.ts b/packages/frontend/core/src/blocksuite/ai/actions/types.ts index d0e31668ad..9904cf3a5f 100644 --- a/packages/frontend/core/src/blocksuite/ai/actions/types.ts +++ b/packages/frontend/core/src/blocksuite/ai/actions/types.ts @@ -1,5 +1,4 @@ import type { - ChatHistoryOrder, ContextMatchedDocChunk, ContextMatchedFileChunk, ContextWorkspaceEmbeddingStatus, @@ -400,15 +399,12 @@ declare global { // non chat histories actions: ( workspaceId: string, - docId?: string + docId: string ) => Promise; chats: ( workspaceId: string, - docId?: string, - options?: { - sessionId?: string; - messageOrder?: ChatHistoryOrder; - } + sessionId: string, + docId?: string ) => Promise; cleanup: ( workspaceId: string, diff --git a/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/ai-chat-block.ts b/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/ai-chat-block.ts index dc89139c2d..3813de947c 100644 --- a/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/ai-chat-block.ts +++ b/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/ai-chat-block.ts @@ -49,12 +49,12 @@ export class AIChatBlockComponent extends BlockComponent { return html`
- + >
${ChatWithAIIcon} AI chat block diff --git a/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/components/ai-chat-messages.ts b/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/components/ai-chat-messages.ts index 7f697cbf45..2b73668eb6 100644 --- a/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/components/ai-chat-messages.ts +++ b/packages/frontend/core/src/blocksuite/ai/blocks/ai-chat-block/components/ai-chat-messages.ts @@ -11,7 +11,7 @@ import { } from '../../../components/ai-chat-messages'; import { UserInfoTemplate } from './user-info'; -export class AIChatMessage extends LitElement { +export class AIChatBlockMessage extends LitElement { static override styles = css` .ai-chat-message { display: flex; @@ -99,7 +99,7 @@ export class AIChatMessage extends LitElement { accessor textRendererOptions: TextRendererOptions = {}; } -export class AIChatMessages extends LitElement { +export class AIChatBlockMessages extends LitElement { static override styles = css` :host { width: 100%; @@ -123,11 +123,11 @@ export class AIChatMessages extends LitElement { message => message.id || message.createdAt, message => { return html` - + > `; } )} @@ -146,7 +146,7 @@ export class AIChatMessages extends LitElement { declare global { interface HTMLElementTagNameMap { - 'ai-chat-message': AIChatMessage; - 'ai-chat-messages': AIChatMessages; + 'ai-chat-block-message': AIChatBlockMessage; + 'ai-chat-block-messages': AIChatBlockMessages; } } 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 b7a3c3c7c3..dd64218997 100644 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts +++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts @@ -1,5 +1,3 @@ -import './chat-panel-messages'; - import type { WorkspaceDialogService } from '@affine/core/modules/dialogs'; import type { FeatureFlagService } from '@affine/core/modules/feature-flag'; import type { ContextEmbedStatus, CopilotSessionType } from '@affine/graphql'; @@ -12,9 +10,8 @@ import { CenterPeekIcon } from '@blocksuite/icons/lit'; import { type Signal, signal } from '@preact/signals-core'; import { css, html, nothing, type PropertyValues } from 'lit'; import { property, state } from 'lit/decorators.js'; -import { createRef, type Ref, ref } from 'lit/directives/ref.js'; +import { keyed } from 'lit/directives/keyed.js'; import { styleMap } from 'lit/directives/style-map.js'; -import { throttle } from 'lodash-es'; import type { DocDisplayConfig, @@ -25,31 +22,9 @@ import type { AIPlaygroundConfig, AIReasoningConfig, } from '../components/ai-chat-input'; -import { - type ChatAction, - type ChatMessage, - type HistoryMessage, -} from '../components/ai-chat-messages'; import { createPlaygroundModal } from '../components/playground/modal'; import { AIProvider } from '../provider'; -import { extractSelectedContent } from '../utils/extract'; -import { - getSelectedImagesAsBlobs, - getSelectedTextContent, -} from '../utils/selection-utils'; import type { AppSidebarConfig } from './chat-config'; -import type { ChatContextValue } from './chat-context'; -import type { ChatPanelMessages } from './chat-panel-messages'; - -const DEFAULT_CHAT_CONTEXT_VALUE: ChatContextValue = { - quote: '', - images: [], - abortController: null, - messages: [], - status: 'idle', - error: null, - markdown: '', -}; export class ChatPanel extends SignalWatcher( WithDisposable(ShadowlessElement) @@ -58,24 +33,10 @@ export class ChatPanel extends SignalWatcher( chat-panel { width: 100%; user-select: text; - } - .chat-panel-container { - display: flex; - flex-direction: column; - height: 100%; - } - - .chat-panel-title { - background: var(--affine-background-primary-color); - position: relative; - padding: 8px 0px; - width: 100%; - height: 36px; - display: flex; - justify-content: space-between; - align-items: center; - z-index: 1; + .chat-panel-container { + height: 100%; + } .chat-panel-title-text { font-size: 14px; @@ -83,153 +44,40 @@ export class ChatPanel extends SignalWatcher( color: var(--affine-text-secondary-color); } - svg { - width: 18px; - height: 18px; - color: var(--affine-text-secondary-color); + .chat-panel-playground { + cursor: pointer; + padding: 2px; + margin-left: 8px; + margin-right: auto; + display: flex; + justify-content: center; + align-items: center; + } + + .chat-panel-playground:hover svg { + color: ${unsafeCSSVarV2('icon/activated')}; } } - - chat-panel-messages { - flex: 1; - overflow-y: hidden; - } - - .chat-panel-hints { - margin: 0 4px; - padding: 8px 12px; - border-radius: 8px; - border: 1px solid var(--affine-border-color); - font-size: 14px; - font-weight: 500; - cursor: pointer; - } - - .chat-panel-hints :first-child { - color: var(--affine-text-primary-color); - } - - .chat-panel-hints :nth-child(2) { - color: var(--affine-text-secondary-color); - } - - .chat-panel-playground { - cursor: pointer; - padding: 2px; - margin-left: 8px; - margin-right: auto; - display: flex; - justify-content: center; - align-items: center; - } - - .chat-panel-playground:hover svg { - color: ${unsafeCSSVarV2('icon/activated')}; - } `; - private readonly _chatMessagesRef: Ref = - createRef(); - - // request counter to track the latest request - private _updateHistoryCounter = 0; - - private _wheelTriggered = false; - - private readonly _updateHistory = async () => { - const { doc } = this; - - const currentRequest = ++this._updateHistoryCounter; - - const [histories, actions] = await Promise.all([ - AIProvider.histories?.chats(doc.workspace.id, doc.id), - AIProvider.histories?.actions(doc.workspace.id, doc.id), - ]); - - // Check if this is still the latest request - if (currentRequest !== this._updateHistoryCounter) { - return; - } - - const chatActions = (actions || []) as ChatAction[]; - const messages: HistoryMessage[] = chatActions; - - const sessionId = await this._getSessionId(); - const history = histories?.find(history => history.sessionId === sessionId); - if (history) { - const chatMessages = (history.messages || []) as ChatMessage[]; - messages.push(...chatMessages); - } - - this.chatContextValue = { - ...this.chatContextValue, - messages: messages.sort( - (a, b) => - new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime() - ), - }; - - this._scrollToEnd(); - }; - - private readonly _updateEmbeddingProgress = ( - count: Record - ) => { - const total = count.finished + count.processing + count.failed; - this.embeddingProgress = [count.finished, total]; - }; - - private readonly _getSessionId = async () => { - if (this.session) { - return this.session.id; - } - const sessions = ( - (await AIProvider.session?.getSessions( - this.doc.workspace.id, - this.doc.id, - { action: false } - )) || [] - ).filter(session => !session.parentSessionId); - this.session = sessions.at(-1); - return this.session?.id; - }; - - private readonly _createSessionId = async () => { - if (this.session) { - return this.session.id; - } - const sessionId = await AIProvider.session?.createSession({ - docId: this.doc.id, - workspaceId: this.doc.workspace.id, - promptName: 'Chat With AFFiNE AI', - }); - if (sessionId) { - this.session = await AIProvider.session?.getSession( - this.doc.workspace.id, - sessionId - ); - } - return sessionId; - }; - @property({ attribute: false }) accessor host!: EditorHost; @property({ attribute: false }) accessor doc!: Store; - @property({ attribute: false }) - accessor networkSearchConfig!: AINetworkSearchConfig; - - @property({ attribute: false }) - accessor reasoningConfig!: AIReasoningConfig; - @property({ attribute: false }) accessor playgroundConfig!: AIPlaygroundConfig; @property({ attribute: false }) accessor appSidebarConfig!: AppSidebarConfig; + @property({ attribute: false }) + accessor networkSearchConfig!: AINetworkSearchConfig; + + @property({ attribute: false }) + accessor reasoningConfig!: AIReasoningConfig; + @property({ attribute: false }) accessor searchMenuConfig!: SearchMenuConfig; @@ -246,56 +94,74 @@ export class ChatPanel extends SignalWatcher( accessor affineWorkspaceDialogService!: WorkspaceDialogService; @state() - accessor isLoading = false; - - @state() - accessor chatContextValue: ChatContextValue = DEFAULT_CHAT_CONTEXT_VALUE; + accessor session: CopilotSessionType | null | undefined; @state() accessor embeddingProgress: [number, number] = [0, 0]; - @state() - accessor session: CopilotSessionType | undefined = undefined; + private isSidebarOpen: Signal = signal(false); - private _isInitialized = false; + private sidebarWidth: Signal = signal(undefined); - private _isSidebarOpen: Signal = signal(false); - - private _sidebarWidth: Signal = signal(undefined); - - private readonly _scrollToEnd = () => { - if (!this._wheelTriggered) { - this._chatMessagesRef.value?.scrollToEnd(); + private readonly initSession = async () => { + if (this.session) { + return this.session; } + const sessions = ( + (await AIProvider.session?.getSessions( + this.doc.workspace.id, + this.doc.id, + { action: false } + )) || [] + ).filter(session => !session.parentSessionId); + const session = sessions.at(-1); + this.session = session ?? null; + return session; }; - private readonly _throttledScrollToEnd = throttle(this._scrollToEnd, 600); + private readonly createSession = async () => { + if (this.session) { + return this.session; + } + const sessionId = await AIProvider.session?.createSession({ + docId: this.doc.id, + workspaceId: this.doc.workspace.id, + promptName: 'Chat With AFFiNE AI', + }); + if (sessionId) { + const session = await AIProvider.session?.getSession( + this.doc.workspace.id, + sessionId + ); + this.session = session ?? null; + } + return this.session; + }; - private readonly _initPanel = async () => { + private readonly initPanel = async () => { try { - if (!this._isSidebarOpen.value) return; - if (this.isLoading) return; - const userId = (await AIProvider.userInfo)?.id; - if (!userId) return; - - this.isLoading = true; - await this._updateHistory(); - this.isLoading = false; - this._isInitialized = true; + if (!this.isSidebarOpen.value) { + return; + } + await this.initSession(); } catch (error) { console.error(error); } }; - private readonly _resetPanel = () => { + private readonly resetPanel = () => { this.session = undefined; - this.chatContextValue = DEFAULT_CHAT_CONTEXT_VALUE; - this.isLoading = false; - this._isInitialized = false; this.embeddingProgress = [0, 0]; }; - private readonly _openPlayground = () => { + private readonly updateEmbeddingProgress = ( + count: Record + ) => { + const total = count.finished + count.processing + count.failed; + this.embeddingProgress = [count.finished, total]; + }; + + private readonly openPlayground = () => { const playgroundContent = html` { - await this._initPanel(); - }); - } - } - - protected override updated(_changedProperties: PropertyValues) { - if (this.chatContextValue.status === 'loading') { - // reset the wheel triggered flag when the status is loading - this._wheelTriggered = false; - } - - if ( - _changedProperties.has('chatContextValue') && - (this.chatContextValue.status === 'loading' || - this.chatContextValue.status === 'error' || - this.chatContextValue.status === 'success') - ) { - setTimeout(this._scrollToEnd, 500); - } - - if ( - _changedProperties.has('chatContextValue') && - this.chatContextValue.status === 'transmitting' - ) { - this._throttledScrollToEnd(); - } - } - - protected override firstUpdated(): void { - const chatMessages = this._chatMessagesRef.value; - if (chatMessages) { - chatMessages.updateComplete - .then(() => { - chatMessages.getScrollContainer()?.addEventListener('wheel', () => { - this._wheelTriggered = true; - }); - }) - .catch(console.error); + protected override updated(changedProperties: PropertyValues) { + if (changedProperties.has('doc')) { + this.resetPanel(); + this.initPanel().catch(console.error); } } @@ -363,135 +191,80 @@ export class ChatPanel extends SignalWatcher( super.connectedCallback(); if (!this.doc) throw new Error('doc is required'); - this._disposables.add( - AIProvider.slots.actions.subscribe(({ event }) => { - const { status } = this.chatContextValue; - if ( - event === 'finished' && - (status === 'idle' || status === 'success') - ) { - this._updateHistory().catch(console.error); - } - }) - ); this._disposables.add( AIProvider.slots.userInfo.subscribe(() => { - this._initPanel().catch(console.error); - }) - ); - this._disposables.add( - AIProvider.slots.requestOpenWithChat.subscribe(({ host }) => { - if (this.host === host) { - extractSelectedContent(host) - .then(context => { - if (!context) return; - this.updateContext(context); - }) - .catch(console.error); - } + this.resetPanel(); + this.initPanel().catch(console.error); }) ); const isOpen = this.appSidebarConfig.isOpen(); - this._isSidebarOpen = isOpen.signal; + this.isSidebarOpen = isOpen.signal; this._disposables.add(isOpen.cleanup); const width = this.appSidebarConfig.getWidth(); - this._sidebarWidth = width.signal; + this.sidebarWidth = width.signal; this._disposables.add(width.cleanup); this._disposables.add( - this._isSidebarOpen.subscribe(isOpen => { - if (isOpen && !this._isInitialized) { - this._initPanel().catch(console.error); + this.isSidebarOpen.subscribe(() => { + if (this.session === undefined) { + this.initPanel().catch(console.error); } }) ); } - updateContext = (context: Partial) => { - this.chatContextValue = { ...this.chatContextValue, ...context }; - }; - - continueInChat = async () => { - const text = await getSelectedTextContent(this.host, 'plain-text'); - const markdown = await getSelectedTextContent(this.host, 'markdown'); - const images = await getSelectedImagesAsBlobs(this.host); - this.updateContext({ - quote: text, - markdown, - images, - }); - }; - override render() { - const width = this._sidebarWidth.value || 0; + const isInitialized = this.session !== undefined; + if (!isInitialized) { + return nothing; + } + + const width = this.sidebarWidth.value || 0; const style = styleMap({ padding: width > 540 ? '8px 24px 0 24px' : '8px 12px 0 12px', }); const [done, total] = this.embeddingProgress; const isEmbedding = total > 0 && done < total; + const title = html` +
+ ${isEmbedding + ? html`Embedding ${done}/${total}` + : 'AFFiNE AI'} +
+ ${this.playgroundConfig.visible.value + ? html` +
+ ${CenterPeekIcon()} +
+ ` + : nothing} + `; return html`
-
-
- ${isEmbedding - ? html`Embedding ${done}/${total}` - : 'AFFiNE AI'} -
- ${this.playgroundConfig.visible.value - ? html` -
- ${CenterPeekIcon()} -
- ` - : nothing} - -
- - + .session=${this.session} + .createSession=${this.createSession} + .workspaceId=${this.doc.workspace.id} + .docId=${this.doc.id} + .networkSearchConfig=${this.networkSearchConfig} + .reasoningConfig=${this.reasoningConfig} + .searchMenuConfig=${this.searchMenuConfig} + .docDisplayConfig=${this.docDisplayConfig} + .extensions=${this.extensions} + .affineFeatureFlagService=${this.affineFeatureFlagService} + .affineWorkspaceDialogService=${this.affineWorkspaceDialogService} + .updateEmbeddingProgress=${this.updateEmbeddingProgress} + .width=${this.sidebarWidth} + >` + )}
`; } } diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/message/assistant.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/message/assistant.ts index 8400d0be80..afa7ba80bf 100644 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/message/assistant.ts +++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/message/assistant.ts @@ -1,4 +1,5 @@ import type { FeatureFlagService } from '@affine/core/modules/feature-flag'; +import type { CopilotSessionType } from '@affine/graphql'; import { WithDisposable } from '@blocksuite/affine/global/lit'; import { isInsidePageEditor } from '@blocksuite/affine/shared/utils'; import type { EditorHost } from '@blocksuite/affine/std'; @@ -53,7 +54,7 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { accessor affineFeatureFlagService!: FeatureFlagService; @property({ attribute: false }) - accessor getSessionId!: () => Promise; + accessor session!: CopilotSessionType | null | undefined; @property({ attribute: false }) accessor retry!: () => void; @@ -62,7 +63,7 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { accessor testId = 'chat-message-assistant'; @property({ attribute: false }) - accessor panelWidth!: Signal; + accessor width: Signal | undefined; get state() { const { isLast, status } = this; @@ -117,7 +118,7 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { .answer=${answer} .host=${this.host} .state=${this.state} - .width=${this.panelWidth} + .width=${this.width} .extensions=${this.extensions} .affineFeatureFlagService=${this.affineFeatureFlagService} >`; @@ -134,7 +135,7 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { } private renderEditorActions() { - const { item, isLast, status } = this; + const { item, isLast, status, host, session } = this; if (!isChatMessage(item) || item.role !== 'assistant') return nothing; @@ -146,7 +147,6 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { ) return nothing; - const { host } = this; const { content, streamObjects, id: messageId } = item; const markdown = streamObjects?.length ? mergeStreamContent(streamObjects) @@ -159,10 +159,10 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { return html` this.retry()} @@ -171,8 +171,8 @@ export class ChatMessageAssistant extends WithDisposable(ShadowlessElement) { ? html`` diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-composer/ai-chat-composer.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-composer/ai-chat-composer.ts index 7e1cdfb33b..93a840bbf9 100644 --- a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-composer/ai-chat-composer.ts +++ b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-composer/ai-chat-composer.ts @@ -12,9 +12,7 @@ import type { import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit'; import type { EditorHost } from '@blocksuite/affine/std'; import { ShadowlessElement } from '@blocksuite/affine/std'; -import type { Store } from '@blocksuite/affine/store'; -import { type Signal, signal } from '@preact/signals-core'; -import { css, html, type PropertyValues } from 'lit'; +import { css, html } from 'lit'; import { property, state } from 'lit/decorators.js'; import { AIProvider } from '../../provider'; @@ -56,16 +54,13 @@ export class AIChatComposer extends SignalWatcher( accessor host!: EditorHost; @property({ attribute: false }) - accessor doc!: Store; + accessor workspaceId!: string; @property({ attribute: false }) - accessor session!: CopilotSessionType | undefined; + accessor session!: CopilotSessionType | null | undefined; @property({ attribute: false }) - accessor getSessionId!: () => Promise; - - @property({ attribute: false }) - accessor createSessionId!: () => Promise; + accessor createSession!: () => Promise; @property({ attribute: false }) accessor chatContextValue!: AIChatInputContext; @@ -73,9 +68,6 @@ export class AIChatComposer extends SignalWatcher( @property({ attribute: false }) accessor updateContext!: (context: Partial) => void; - @property({ attribute: false }) - accessor isVisible: Signal = signal(false); - @property({ attribute: false }) accessor updateEmbeddingProgress!: ( count: Record @@ -102,9 +94,6 @@ export class AIChatComposer extends SignalWatcher( @property({ attribute: false }) accessor portalContainer: HTMLElement | null = null; - @property({ attribute: false }) - accessor panelWidth: Signal = signal(undefined); - @property({ attribute: false }) accessor affineWorkspaceDialogService!: WorkspaceDialogService; @@ -114,10 +103,6 @@ export class AIChatComposer extends SignalWatcher( @state() accessor embeddingCompleted = false; - private _isInitialized = false; - - private _isLoading = false; - private _contextId: string | undefined = undefined; private _pollAbortController: AbortController | null = null; @@ -141,9 +126,7 @@ export class AIChatComposer extends SignalWatcher( .host=${this.host} .chips=${this.chips} .session=${this.session} - .getSessionId=${this.getSessionId} - .createSessionId=${this.createSessionId} - .getContextId=${this._getContextId} + .createSession=${this.createSession} .chatContextValue=${this.chatContextValue} .updateContext=${this.updateContext} .networkSearchConfig=${this.networkSearchConfig} @@ -151,7 +134,6 @@ export class AIChatComposer extends SignalWatcher( .docDisplayConfig=${this.docDisplayConfig} .onChatSuccess=${this.onChatSuccess} .trackOptions=${this.trackOptions} - .panelWidth=${this.panelWidth} .addImages=${this.addImages} >