Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
Loading