From 7a6d8191298b60a8332c84667dbdeed5816a71c9 Mon Sep 17 00:00:00 2001 From: wgqqqqq Date: Sat, 31 Oct 2026 11:12:32 +0800 Subject: [PATCH] fix(harmonyos): hide copy action while assistant is streaming --- .../pages/components/ChatMessageBubble.ets | 16 +++++++-- .../ConversationMessageRenderPolicy.ets | 18 ++++++++++ ...assistant-message-copy-visibility.test.cjs | 33 +++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 src/apps/mobile/harmonyos/tools/tests/assistant-message-copy-visibility.test.cjs diff --git a/src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ChatMessageBubble.ets b/src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ChatMessageBubble.ets index 273033ef9e..645dac0963 100644 --- a/src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ChatMessageBubble.ets +++ b/src/apps/mobile/harmonyos/entry/src/main/ets/pages/components/ChatMessageBubble.ets @@ -177,11 +177,12 @@ export struct ChatMessageBubble { * Retry keeps the exact condition the previous `else if` used — including its * exclusion by the errored branch above, which draws its own retry action — * so moving it into this row changes no failed-state behavior. Copy is drawn - * whenever there is a body to write. + * only after the assistant turn has settled; streamed text is not an export + * target while it is still changing. */ @Builder ActionRow() { - if (this.showsRetryAction() || this.copySourceText().length > 0) { + if (this.showsRetryAction() || this.canCopyAssistantText()) { Row({ space: 10 }) { if (this.showsRetryAction()) { ChatMessageRetryAction({ @@ -190,7 +191,7 @@ export struct ChatMessageBubble { onRetry: this.onRetryMessage }) } - if (this.copySourceText().length > 0) { + if (this.canCopyAssistantText()) { AssistantMessageCopyAction({ source: this.copySourceText(), onCopyScoped: this.onCopyMessageScoped @@ -689,6 +690,15 @@ export struct ChatMessageBubble { return this.finalMessageText(this.item); } + private canCopyAssistantText(): boolean { + return ConversationMessageRenderPolicy.canCopyAssistant( + this.item, + this.isStreamingAssistant(), + this.isFinalizing, + this.copySourceText().length + ); + } + private finalMessageText(item: ConversationUiMessage): string { const structuredText = this.lastTopLevelText(item.items || []); if (structuredText.length > 0) { diff --git a/src/apps/mobile/harmonyos/entry/src/main/ets/pages/policy/ConversationMessageRenderPolicy.ets b/src/apps/mobile/harmonyos/entry/src/main/ets/pages/policy/ConversationMessageRenderPolicy.ets index b2e802f38a..57d79e5f21 100644 --- a/src/apps/mobile/harmonyos/entry/src/main/ets/pages/policy/ConversationMessageRenderPolicy.ets +++ b/src/apps/mobile/harmonyos/entry/src/main/ets/pages/policy/ConversationMessageRenderPolicy.ets @@ -40,6 +40,24 @@ export class ConversationMessageRenderPolicy { return status === 'active' || status === 'failed'; } + /** + * Copy is available only for a settled assistant answer. Streamed text may + * already be non-empty while the turn is still changing. + */ + static canCopyAssistant( + item: ConversationUiMessage, + isStreaming: boolean, + isFinalizing: boolean, + sourceTextLength: number + ): boolean { + if ((item.role || '').toLowerCase() === 'user' || isStreaming || isFinalizing || sourceTextLength <= 0) { + return false; + } + const status = (item.status || '').toLowerCase(); + return status === '' || status === 'done' || status === 'completed' || + status === 'success' || status === 'sent'; + } + static hasRenderableItems(items: ConversationUiMessageItem[]): boolean { return items.some((entry: ConversationUiMessageItem) => { if ((entry.content || '').trim().length > 0 || !!entry.tool) { diff --git a/src/apps/mobile/harmonyos/tools/tests/assistant-message-copy-visibility.test.cjs b/src/apps/mobile/harmonyos/tools/tests/assistant-message-copy-visibility.test.cjs new file mode 100644 index 0000000000..dc882ebd2c --- /dev/null +++ b/src/apps/mobile/harmonyos/tools/tests/assistant-message-copy-visibility.test.cjs @@ -0,0 +1,33 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const ts = require('typescript'); + +const source = fs.readFileSync(path.join(__dirname, + '../../entry/src/main/ets/pages/policy/ConversationMessageRenderPolicy.ets'), 'utf8'); +const compiled = ts.transpileModule(source, { compilerOptions: { + target: ts.ScriptTarget.ES2022, + module: ts.ModuleKind.CommonJS, +} }).outputText; +const exported = {}; +new Function('require', 'exports', compiled)(() => ({}), exported); +const { ConversationMessageRenderPolicy } = exported; + +function message(status, role = 'assistant') { + return { id: 'message', role, text: 'answer', status }; +} + +test('copy stays hidden while an answer is streaming or finalizing', () => { + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message('active'), true, false, 6), false); + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message('completed'), false, true, 6), false); + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message('processing'), false, false, 6), false); +}); + +test('copy appears for settled assistant answers with content', () => { + for (const status of ['', 'done', 'completed', 'success', 'sent']) { + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message(status), false, false, 6), true, status); + } + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message('completed'), false, false, 0), false); + assert.equal(ConversationMessageRenderPolicy.canCopyAssistant(message('completed', 'user'), false, false, 6), false); +});