There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
feat(core): chat-panel buttons ui (#11942)
akumatus3d2f0e7b5b8e
diff --git a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.tsindex ad1105a94e..0e4abc86c0 100644--- a/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts+++ b/packages/frontend/core/src/blocksuite/ai/chat-panel/index.ts@@ -5,7 +5,6 @@ import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';import type { EditorHost } from '@blocksuite/affine/std';import { ShadowlessElement } from '@blocksuite/affine/std';import type { ExtensionType, Store } from '@blocksuite/affine/store';-import { HelpIcon } 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';@@ -68,21 +67,12 @@ export class ChatPanel extends SignalWatcher(align-items: center;z-index: 1;- div:first-child {+ .chat-panel-title-text {font-size: 14px;font-weight: 500;color: var(--affine-text-secondary-color);}- div:last-child {- width: 24px;- height: 24px;- display: flex;- justify-content: center;- align-items: center;- cursor: pointer;- }-svg {width: 18px;height: 18px;@@ -385,20 +375,20 @@ export class ChatPanel extends SignalWatcher(return html`<div class="chat-panel-container" style=${style}><div class="chat-panel-title">- <div>+ <div class="chat-panel-title-text">${isEmbedding? html`<span data-testid="chat-panel-embedding-progress">Embedding ${done}/${total}</span>`: 'AFFiNE AI'}</div>- <div- @click=${() => {- AIProvider.toggleGeneralAIOnboarding?.(true);- }}- >- ${HelpIcon()}- </div>+ <ai-history-clear+ .host=${this.host}+ .doc=${this.doc}+ .getSessionId=${this._getSessionId}+ .onHistoryCleared=${this._updateHistory}+ .chatContextValue=${this.chatContextValue}+ ></ai-history-clear></div><chat-panel-messages${ref(this._chatMessagesRef)}@@ -418,7 +408,6 @@ export class ChatPanel extends SignalWatcher(.chatContextValue=${this.chatContextValue}.updateContext=${this.updateContext}.updateEmbeddingProgress=${this._updateEmbeddingProgress}- .onHistoryCleared=${this._updateHistory}.isVisible=${this._isSidebarOpen}.networkSearchConfig=${this.networkSearchConfig}.reasoningConfig=${this.reasoningConfig}@@ -428,6 +417,7 @@ export class ChatPanel extends SignalWatcher(where: 'chat-panel',control: 'chat-send',}}+ .sideBarWidth=${this._sidebarWidth}></ai-chat-composer></div>`;}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.tsindex 1536cdae3e..a8690e3c2b 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@@ -5,7 +5,6 @@ import type {CopilotDocType,} from '@affine/graphql';import { SignalWatcher, WithDisposable } from '@blocksuite/affine/global/lit';-import { NotificationProvider } from '@blocksuite/affine/shared/services';import type { EditorHost } from '@blocksuite/affine/std';import { ShadowlessElement } from '@blocksuite/affine/std';import type { Store } from '@blocksuite/affine/store';@@ -65,9 +64,6 @@ export class AIChatComposer extends SignalWatcher(@property({ attribute: false })accessor updateContext!: (context: Partial<AIChatInputContext>) => void;- @property({ attribute: false })- accessor onHistoryCleared: (() => void) | undefined;-@property({ attribute: false })accessor isVisible: Signal<boolean | undefined> = signal(false);@@ -97,6 +93,9 @@ export class AIChatComposer extends SignalWatcher(@property({ attribute: false })accessor portalContainer: HTMLElement | null = null;+ @property({ attribute: false })+ accessor sideBarWidth: Signal<number | undefined> = signal(undefined);+@state()accessor chips: ChatChip[] = [];@@ -131,9 +130,9 @@ export class AIChatComposer extends SignalWatcher(.networkSearchConfig=${this.networkSearchConfig}.reasoningConfig=${this.reasoningConfig}.docDisplayConfig=${this.docDisplayConfig}- .cleanupHistories=${this._cleanupHistories}.onChatSuccess=${this.onChatSuccess}.trackOptions=${this.trackOptions}+ .sideBarWidth=${this.sideBarWidth}></ai-chat-input><div class="chat-panel-footer">${InformationIcon()}@@ -354,36 +353,6 @@ export class AIChatComposer extends SignalWatcher(this._pollAbortController = null;};- private readonly _cleanupHistories = async () => {- const sessionId = await this.getSessionId();- const notification = this.host.std.getOptional(NotificationProvider);- if (!notification) return;- try {- if (- await notification.confirm({- title: 'Clear History',- message:- 'Are you sure you want to clear all history? This action will permanently delete all content, including all chat logs and data, and cannot be undone.',- confirmText: 'Confirm',- cancelText: 'Cancel',- })- ) {- const actionIds = this.chatContextValue.messages- .filter(item => 'sessionId' in item)- .map(item => item.sessionId);- await AIProvider.histories?.cleanup(- this.doc.workspace.id,- this.doc.id,- [...(sessionId ? [sessionId] : []), ...(actionIds || [])]- );- notification.toast('History cleared');- this.onHistoryCleared?.();- }- } catch {- notification.toast('Failed to clear history');- }- };-private readonly _initComposer = async () => {if (!this.isVisible.value) return;if (this._isLoading) return;diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-input/ai-chat-input.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-input/ai-chat-input.tsindex 7783648f05..b7a3334784 100644--- a/packages/frontend/core/src/blocksuite/ai/components/ai-chat-input/ai-chat-input.ts+++ b/packages/frontend/core/src/blocksuite/ai/components/ai-chat-input/ai-chat-input.ts@@ -4,12 +4,12 @@ import { unsafeCSSVarV2 } from '@blocksuite/affine/shared/theme';import { openFileOrFiles } from '@blocksuite/affine/shared/utils';import type { EditorHost } from '@blocksuite/affine/std';import {- BroomIcon,CloseIcon,ImageIcon,PublishIcon,ThinkingIcon,} from '@blocksuite/icons/lit';+import { type Signal, signal } from '@preact/signals-core';import { css, html, LitElement, nothing } from 'lit';import { property, query, state } from 'lit/decorators.js';import { repeat } from 'lit/directives/repeat.js';@@ -118,46 +118,55 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {gap: 8px;align-items: center;- div {- width: 24px;- height: 24px;+ .chat-input-icon {cursor: pointer;- }-- div:nth-child(2) {- margin-left: auto;- }-- .image-upload,- .chat-history-clear,- .chat-network-search {+ padding: 2px;display: flex;justify-content: center;align-items: center;+ border-radius: 4px;+svg {width: 20px;height: 20px;color: ${unsafeCSSVarV2('icon/primary')};}++ .chat-input-icon-label {+ font-size: 14px;+ line-height: 22px;+ font-weight: 500;+ color: ${unsafeCSSVarV2('icon/primary')};+ margin: 0 4px 0 4px;+ }}- .chat-history-clear svg {- color: var(--affine-text-secondary-color);+ .chat-input-icon:nth-child(2) {+ margin-left: auto;}- .chat-network-search[data-active='true'] svg {- color: ${unsafeCSSVarV2('icon/activated')};++ .chat-input-icon:hover {+ background-color: ${unsafeCSSVarV2('layer/background/hoverOverlay')};}- .chat-history-clear[aria-disabled='true'],- .image-upload[aria-disabled='true'],- .chat-network-search[aria-disabled='true'] {- cursor: not-allowed;+ .chat-input-icon[data-active='true'] {+ background-color: #1e96eb14;++ svg {+ color: ${unsafeCSSVarV2('icon/activated')};+ }++ .chat-input-icon-label {+ color: ${unsafeCSSVarV2('icon/activated')};+ }}- .chat-history-clear[aria-disabled='true'] svg,- .image-upload[aria-disabled='true'] svg,- .chat-network-search[aria-disabled='true'] svg {- color: var(--affine-text-disable-color) !important;+ .chat-input-icon[aria-disabled='true'] {+ cursor: not-allowed;++ svg {+ color: ${unsafeCSSVarV2('icon/secondary')} !important;+ }}}@@ -240,9 +249,6 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {@property({ attribute: false })accessor updateContext!: (context: Partial<AIChatInputContext>) => void;- @property({ attribute: false })- accessor cleanupHistories!: () => Promise<void>;-@property({ attribute: false })accessor networkSearchConfig!: AINetworkSearchConfig;@@ -264,6 +270,9 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {@property({ attribute: 'data-testid', reflect: true })accessor testId = 'chat-panel-input-container';+ @property({ attribute: false })+ accessor sideBarWidth: Signal<number | undefined> = signal(undefined);+private get _isNetworkActive() {return (!!this.networkSearchConfig.visible.value &&@@ -275,12 +284,8 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {return !!this.reasoningConfig.enabled.value;}- private get _isClearDisabled() {- return (- this.chatContextValue.status === 'loading' ||- this.chatContextValue.status === 'transmitting' ||- !this.chatContextValue.messages.length- );+ private get _isImageUploadDisabled() {+ return this.chatContextValue.images.length >= MaximumImageCount;}override connectedCallback() {@@ -303,6 +308,8 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {const { images, status } = this.chatContextValue;const hasImages = images.length > 0;const maxHeight = hasImages ? 272 + 2 : 200 + 2;+ const showLabel = this.sideBarWidth.value && this.sideBarWidth.value > 400;+return html` <divclass="chat-panel-input"data-if-focused=${this.focused}@@ -355,44 +362,48 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {></textarea><div class="chat-panel-input-actions"><div- class="chat-history-clear"- aria-disabled=${this._isClearDisabled}- @click=${this._handleClear}- data-testid="chat-panel-clear"+ class="chat-input-icon"+ data-testid="chat-panel-input-image-upload"+ aria-disabled=${this._isImageUploadDisabled}+ @click=${this._uploadImageFiles}>- ${BroomIcon()}+ ${ImageIcon()}+ <affine-tooltip>Upload</affine-tooltip></div>${this.networkSearchConfig.visible.value? html`<div- class="chat-network-search"+ class="chat-input-icon"data-testid="chat-network-search"data-active=${this._isNetworkActive}@click=${this._toggleNetworkSearch}@pointerdown=${stopPropagation}>${PublishIcon()}+ ${!showLabel+ ? html`<affine-tooltip>Search</affine-tooltip>`+ : nothing}+ ${showLabel+ ? html`<span class="chat-input-icon-label">Search</span>`+ : nothing}</div>`: nothing}<div- class="chat-network-search"+ class="chat-input-icon"data-testid="chat-reasoning"data-active=${this._isReasoningActive}@click=${this._toggleReasoning}@pointerdown=${stopPropagation}>${ThinkingIcon()}+ ${!showLabel+ ? html`<affine-tooltip>Reason</affine-tooltip>`+ : nothing}+ ${showLabel+ ? html`<span class="chat-input-icon-label">Reason</span>`+ : nothing}</div>- ${images.length < MaximumImageCount- ? html`<div- data-testid="chat-panel-input-image-upload"- class="image-upload"- @click=${this._uploadImageFiles}- >- ${ImageIcon()}- </div>`- : nothing}${status === 'transmitting'? html`<div @click=${this._handleAbort} data-testid="chat-panel-stop">${ChatAbortIcon}@@ -456,13 +467,6 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {reportResponse('aborted:stop');};- private readonly _handleClear = async () => {- if (this._isClearDisabled) {- return;- }- await this.cleanupHistories();- };-private readonly _toggleNetworkSearch = (e: MouseEvent) => {e.preventDefault();e.stopPropagation();@@ -493,6 +497,8 @@ export class AIChatInput extends SignalWatcher(WithDisposable(LitElement)) {};private readonly _uploadImageFiles = async (_e: MouseEvent) => {+ if (this._isImageUploadDisabled) return;+const images = await openFileOrFiles({acceptType: 'Images',multiple: true,diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/ai-history-clear.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/ai-history-clear.tsnew file mode 100644index 0000000000..184fdd7ae2--- /dev/null+++ b/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/ai-history-clear.ts@@ -0,0 +1,93 @@+import { WithDisposable } from '@blocksuite/affine/global/lit';+import { NotificationProvider } from '@blocksuite/affine/shared/services';+import { unsafeCSSVarV2 } from '@blocksuite/affine/shared/theme';+import type { EditorHost } from '@blocksuite/affine/std';+import { ShadowlessElement } from '@blocksuite/affine/std';+import type { Store } from '@blocksuite/affine/store';+import { css, html } from 'lit';+import { property } from 'lit/decorators.js';++import type { ChatContextValue } from '../../chat-panel/chat-context';+import { AIProvider } from '../../provider';++export class AIHistoryClear extends WithDisposable(ShadowlessElement) {+ @property({ attribute: false })+ accessor chatContextValue!: ChatContextValue;++ @property({ attribute: false })+ accessor getSessionId!: () => Promise<string | undefined>;++ @property({ attribute: false })+ accessor host!: EditorHost;++ @property({ attribute: false })+ accessor doc!: Store;++ @property({ attribute: false })+ accessor onHistoryCleared!: () => void;++ static override styles = css`+ .chat-history-clear {+ cursor: pointer;+ color: ${unsafeCSSVarV2('icon/primary')};+ }+ .chat-history-clear[aria-disabled='true'] {+ cursor: not-allowed;+ color: ${unsafeCSSVarV2('icon/secondary')};+ }+ `;++ private get _isHistoryClearDisabled() {+ return (+ this.chatContextValue.status === 'loading' ||+ this.chatContextValue.status === 'transmitting' ||+ !this.chatContextValue.messages.length+ );+ }++ private readonly _cleanupHistories = async () => {+ if (this._isHistoryClearDisabled) {+ return;+ }+ const sessionId = await this.getSessionId();+ const notification = this.host.std.getOptional(NotificationProvider);+ if (!notification) return;+ try {+ if (+ await notification.confirm({+ title: 'Clear History',+ message:+ 'Are you sure you want to clear all history? This action will permanently delete all content, including all chat logs and data, and cannot be undone.',+ confirmText: 'Confirm',+ cancelText: 'Cancel',+ })+ ) {+ const actionIds = this.chatContextValue.messages+ .filter(item => 'sessionId' in item)+ .map(item => item.sessionId);+ await AIProvider.histories?.cleanup(+ this.doc.workspace.id,+ this.doc.id,+ [...(sessionId ? [sessionId] : []), ...(actionIds || [])]+ );+ notification.toast('History cleared');+ this.onHistoryCleared?.();+ }+ } catch {+ notification.toast('Failed to clear history');+ }+ };++ override render() {+ return html`+ <div+ class="chat-history-clear"+ aria-disabled=${this._isHistoryClearDisabled}+ @click=${this._cleanupHistories}+ data-testid="chat-panel-clear"+ >+ Clear+ </div>+ `;+ }+}diff --git a/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/index.ts b/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/index.tsnew file mode 100644index 0000000000..3ee1326ece--- /dev/null+++ b/packages/frontend/core/src/blocksuite/ai/components/ai-history-clear/index.ts@@ -0,0 +1 @@+export * from './ai-history-clear';diff --git a/packages/frontend/core/src/blocksuite/ai/effects.ts b/packages/frontend/core/src/blocksuite/ai/effects.tsindex 34ba44bc3d..2b4c9bd847 100644--- a/packages/frontend/core/src/blocksuite/ai/effects.ts+++ b/packages/frontend/core/src/blocksuite/ai/effects.ts@@ -37,7 +37,8 @@ import { ChatPanelDocChip } from './components/ai-chat-chips/doc-chip';import { ChatPanelFileChip } from './components/ai-chat-chips/file-chip';import { ChatPanelTagChip } from './components/ai-chat-chips/tag-chip';import { AIChatComposer } from './components/ai-chat-composer';-import { AIChatInput } from './components/ai-chat-input/ai-chat-input';+import { AIChatInput } from './components/ai-chat-input';+import { AIHistoryClear } from './components/ai-history-clear';import { effects as componentAiItemEffects } from './components/ai-item';import { AIScrollableTextRenderer } from './components/ai-scrollable-text-renderer';import { AskAIButton } from './components/ask-ai-button';@@ -98,6 +99,7 @@ export function registerAIEffects() {customElements.define('ai-chat-input', AIChatInput);customElements.define('ai-chat-composer', AIChatComposer);customElements.define('chat-panel-chips', ChatPanelChips);+ customElements.define('ai-history-clear', AIHistoryClear);customElements.define('chat-panel-add-popover', ChatPanelAddPopover);customElements.define('chat-panel-candidates-popover',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.tsindex 5c1b8bf52b..7576a48fc0 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@@ -502,6 +502,15 @@ export class AIChatBlockPeekView extends LitElement {<div class="new-chat-messages-container">${this.CurrentMessages(currentChatMessages)}</div>+ <div class="history-clear-container">+ <ai-history-clear+ .host=${this.host}+ .doc=${this.host.doc}+ .getSessionId=${this._getSessionId}+ .onHistoryCleared=${this._onHistoryCleared}+ .chatContextValue=${chatContext}+ ></ai-history-clear>+ </div></div><ai-chat-composer.host=${host}@@ -510,7 +519,6 @@ export class AIChatBlockPeekView extends LitElement {.createSessionId=${this._createSessionId}.chatContextValue=${chatContext}.updateContext=${updateContext}- .onHistoryCleared=${this._onHistoryCleared}.isVisible=${this.isComposerVisible}.updateEmbeddingProgress=${this._updateEmbeddingProgress}.networkSearchConfig=${networkSearchConfig}diff --git a/packages/frontend/core/src/blocksuite/ai/peek-view/styles.ts b/packages/frontend/core/src/blocksuite/ai/peek-view/styles.tsindex bd9b6a37ad..4a8b689e74 100644--- a/packages/frontend/core/src/blocksuite/ai/peek-view/styles.ts+++ b/packages/frontend/core/src/blocksuite/ai/peek-view/styles.ts@@ -57,4 +57,11 @@ export const PeekViewStyles = css`flex-direction: column;gap: 8px;}++ .history-clear-container {+ display: flex;+ align-items: center;+ flex-direction: row-reverse;+ width: 100%;+ }`;diff --git a/tests/affine-cloud-copilot/e2e/chat-with/reasoning.spec.ts b/tests/affine-cloud-copilot/e2e/chat-with/reasoning.spec.tsnew file mode 100644index 0000000000..3a98e2db3c--- /dev/null+++ b/tests/affine-cloud-copilot/e2e/chat-with/reasoning.spec.ts@@ -0,0 +1,30 @@+import { test } from '../base/base-test';++test.describe('AIChatWith/Reasoning', () => {+ test.beforeEach(async ({ loggedInPage: page, utils }) => {+ await utils.testUtils.setupTestEnvironment(page);+ await utils.chatPanel.openChatPanel(page);+ });++ test('should support chat with reasoning', async ({+ loggedInPage: page,+ utils,+ }) => {+ await utils.chatPanel.enableReasoning(page);+ await utils.chatPanel.makeChat(+ page,+ 'How do you measure exactly 4 liters of water using a jug that only holds 3 and 5 liters?'+ );+ await utils.chatPanel.waitForHistory(page, [+ {+ role: 'user',+ content:+ 'How do you measure exactly 4 liters of water using a jug that only holds 3 and 5 liters?',+ },+ {+ role: 'assistant',+ status: 'success',+ },+ ]);+ });+});diff --git a/tests/affine-cloud-copilot/e2e/utils/chat-panel-utils.ts b/tests/affine-cloud-copilot/e2e/utils/chat-panel-utils.tsindex a6c11b9498..b8c50ebff6 100644--- a/tests/affine-cloud-copilot/e2e/utils/chat-panel-utils.ts+++ b/tests/affine-cloud-copilot/e2e/utils/chat-panel-utils.ts@@ -305,6 +305,20 @@ export class ChatPanelUtils {}}+ public static async enableReasoning(page: Page) {+ const reasoning = page.getByTestId('chat-reasoning');+ if ((await reasoning.getAttribute('data-active')) === 'false') {+ await reasoning.click();+ }+ }++ public static async disableReasoning(page: Page) {+ const reasoning = page.getByTestId('chat-reasoning');+ if ((await reasoning.getAttribute('data-active')) === 'true') {+ await reasoning.click();+ }+ }+public static async isNetworkSearchEnabled(page: Page) {const networkSearch = await page.getByTestId('chat-network-search');return (await networkSearch.getAttribute('aria-disabled')) === 'false';