From 631c8b8145d30789db05d595238070face8ae901 Mon Sep 17 00:00:00 2001 From: akumatus Date: Fri, 14 Feb 2025 14:48:46 +0000 Subject: [PATCH] refactor(core): lazy create copilot session and context (#10170) --- .../ai/_common/components/chat-action-list.ts | 7 +- .../ai/_common/components/copy-more.ts | 7 +- .../presets/ai/chat-panel/chat-panel-chips.ts | 16 +- .../presets/ai/chat-panel/chat-panel-input.ts | 89 +++++----- .../ai/chat-panel/chat-panel-messages.ts | 11 +- .../ai/chat-panel/components/doc-chip.ts | 4 +- .../blocksuite/presets/ai/chat-panel/index.ts | 156 +++++++++++------- .../ai/peek-view/chat-block-peek-view.ts | 8 +- .../block-suite-editor/ai/request.ts | 31 +--- .../block-suite-editor/ai/setup-provider.tsx | 15 +- .../affine-cloud-copilot/e2e/copilot.spec.ts | 5 +- 11 files changed, 179 insertions(+), 170 deletions(-) diff --git a/packages/frontend/core/src/blocksuite/presets/ai/_common/components/chat-action-list.ts b/packages/frontend/core/src/blocksuite/presets/ai/_common/components/chat-action-list.ts index 70bde30114..fe713c8d6e 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/_common/components/chat-action-list.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/_common/components/chat-action-list.ts @@ -83,7 +83,7 @@ export class ChatActionList extends LitElement { accessor content: string = ''; @property({ attribute: false }) - accessor chatSessionId: string | undefined = undefined; + accessor getSessionId!: () => Promise; @property({ attribute: false }) accessor messageId: string | undefined = undefined; @@ -100,7 +100,7 @@ export class ChatActionList extends LitElement { return nothing; } - const { host, content, chatSessionId, messageId, layoutDirection } = this; + const { host, content, messageId, layoutDirection } = this; const classes = classMap({ 'actions-container': true, horizontal: layoutDirection === 'horizontal', @@ -138,11 +138,12 @@ export class ChatActionList extends LitElement { blocks: this._currentBlockSelections, images: this._currentImageSelections, }; + const sessionId = await this.getSessionId(); const success = await action.handler( host, content, currentSelections, - chatSessionId, + sessionId, messageId ); if (success) { diff --git a/packages/frontend/core/src/blocksuite/presets/ai/_common/components/copy-more.ts b/packages/frontend/core/src/blocksuite/presets/ai/_common/components/copy-more.ts index 82fea70677..851054289d 100644 --- a/packages/frontend/core/src/blocksuite/presets/ai/_common/components/copy-more.ts +++ b/packages/frontend/core/src/blocksuite/presets/ai/_common/components/copy-more.ts @@ -115,7 +115,7 @@ export class ChatCopyMore extends WithDisposable(LitElement) { accessor content!: string; @property({ attribute: false }) - accessor chatSessionId: string | undefined = undefined; + accessor getSessionId!: () => Promise; @property({ attribute: false }) accessor messageId: string | undefined = undefined; @@ -162,7 +162,7 @@ export class ChatCopyMore extends WithDisposable(LitElement) { } override render() { - const { host, content, isLast, messageId, chatSessionId, actions } = this; + const { host, content, isLast, messageId, actions } = this; return html`