From 6cf182190c478e8e4d035d4691caef8d60efca5c Mon Sep 17 00:00:00 2001 From: akumatus Date: Thu, 3 Apr 2025 14:53:50 +0000 Subject: [PATCH] refactor(core): remove useless chat-input components (#11426) Close [BS-2583](https://linear.app/affine-design/issue/BS-2583). --- .../ai/chat-panel/chat-panel-input.ts | 108 ------------- .../src/blocksuite/ai/chat-panel/index.ts | 5 +- .../components/ai-chat-input/ai-chat-input.ts | 146 ++++++++++++++++-- .../core/src/blocksuite/ai/effects.ts | 6 +- .../ai/peek-view/chat-block-input.ts | 119 -------------- .../ai/peek-view/chat-block-peek-view.ts | 22 +-- .../e2e/chat-with/network.spec.ts | 2 +- .../e2e/utils/chat-panel-utils.ts | 2 +- 8 files changed, 155 insertions(+), 255 deletions(-) delete mode 100644 packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-input.ts delete mode 100644 packages/frontend/core/src/blocksuite/ai/peek-view/chat-block-input.ts diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-input.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-input.ts deleted file mode 100644 index d970b81a6a..0000000000 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/chat-panel-input.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { AIChatInput } from '../components/ai-chat-input'; -import type { ChatMessage } from '../components/ai-chat-messages'; -import { type AIError, AIProvider } from '../provider'; -import { readBlobAsURL } from '../utils/image'; - -export class ChatPanelInput extends AIChatInput { - send = async (text: string) => { - const { status, markdown, images } = this.chatContextValue; - if (status === 'loading' || status === 'transmitting') return; - if (!text) return; - - try { - const { doc } = this.host; - const promptName = this.getPromptName(); - - this.updateContext({ - images: [], - status: 'loading', - error: null, - quote: '', - markdown: '', - }); - - const attachments = await Promise.all( - images?.map(image => readBlobAsURL(image)) - ); - - const userInput = (markdown ? `${markdown}\n` : '') + text; - this.updateContext({ - messages: [ - ...this.chatContextValue.messages, - { - id: '', - role: 'user', - content: userInput, - createdAt: new Date().toISOString(), - attachments, - }, - { - id: '', - role: 'assistant', - content: '', - createdAt: new Date().toISOString(), - }, - ], - }); - - // must update prompt name after local chat message is updated - // otherwise, the unauthorized error can not be rendered properly - await this.updatePromptName(promptName); - - const abortController = new AbortController(); - const sessionId = await this.getSessionId(); - if (!sessionId) return; - - const contexts = await this.getMatchedContexts(userInput); - const stream = AIProvider.actions.chat?.({ - sessionId, - input: userInput, - contexts, - docId: doc.id, - attachments: images, - workspaceId: doc.workspace.id, - host: this.host, - stream: true, - signal: abortController.signal, - where: 'chat-panel', - control: 'chat-send', - isRootSession: true, - }); - - if (stream) { - this.updateContext({ abortController }); - - for await (const text of stream) { - const messages = [...this.chatContextValue.messages]; - const last = messages[messages.length - 1] as ChatMessage; - last.content += text; - this.updateContext({ messages, status: 'transmitting' }); - } - - this.updateContext({ status: 'success' }); - - const { messages } = this.chatContextValue; - const last = messages[messages.length - 1] as ChatMessage; - if (!last.id) { - const historyIds = await AIProvider.histories?.ids( - doc.workspace.id, - doc.id, - { sessionId } - ); - if (!historyIds || !historyIds[0]) return; - last.id = historyIds[0].messages.at(-1)?.id ?? ''; - } - } - } catch (error) { - this.updateContext({ status: 'error', error: error as AIError }); - } finally { - this.updateContext({ abortController: null }); - } - }; -} - -declare global { - interface HTMLElementTagNameMap { - 'chat-panel-input': ChatPanelInput; - } -} 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 616fd417d0..6926832e3c 100644 --- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts +++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts @@ -1,4 +1,3 @@ -import './chat-panel-input'; import './chat-panel-messages'; import type { @@ -640,7 +639,7 @@ export class ChatPanel extends SignalWatcher( .docDisplayConfig=${this.docDisplayConfig} .searchMenuConfig=${this.searchMenuConfig} > - + > - + .onChatSuccess=${this._onChatSuccess} + .trackOptions=${{ + where: 'ai-chat-block', + control: 'chat-send', + }} + >