Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(conversation): render mixed assistant blocks in natural order (#15810)

Arvin Xu97708c3fbb55
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx b/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx
index 2442685a664..0619ec52be0 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx
@@ -140,11 +140,14 @@ describe('Group', () => {
mockIsGenerating = false;
});
- it('keeps long structured mixed content visible after the single inline tool', () => {
+ it('keeps a long mixed single-tool block inline in its natural order', () => {
+ // No promotion/relocation: a block carrying both a tool call and long prose
+ // renders as ONE inline unit (content above its tool inside ContentBlock),
+ // never split into a tool-first / text-after layout.
const longContent =
'后宫番 + 实际项目中的状态管理问题,这个组合挺有意思的!\n\n对于实际项目中的状态管理,你目前遇到的具体问题是什么?比如:\n- 不知道什么时候该用 useState,什么时候该用 Context\n- 组件间状态传递变得混乱\n- 性能问题(不必要的重渲染)';
- const { container } = render(
+ render(
<Group
id="assistant-1"
messageIndex={0}
@@ -158,86 +161,20 @@ describe('Group', () => {
/>,
);
- const sequence = Array.from(container.querySelectorAll('[data-testid]')).map((node) =>
- node.getAttribute('data-testid'),
- );
-
- expect(sequence).toEqual(['answer-segment', 'answer-segment']);
+ expect(screen.queryByTestId('workflow-segment')).not.toBeInTheDocument();
expect(parseAnswerSegments()).toEqual([
- {
- content: '',
- contentOverride: '',
- disableMarkdownStreaming: false,
- domId: 'block-1__workflow',
- hasError: false,
- hasToolsOverride: true,
- id: 'block-1',
- isFirstBlock: false,
- toolCount: 1,
- },
{
content: longContent,
- contentOverride: longContent,
+ contentOverride: undefined,
disableMarkdownStreaming: false,
- domId: 'block-1__answer',
+ domId: undefined,
hasError: false,
- hasToolsOverride: false,
+ hasToolsOverride: undefined,
id: 'block-1',
isFirstBlock: false,
- toolCount: 0,
- },
- ]);
- });
-
- it('keeps a final-looking mixed block after a folded multi-tool workflow', () => {
- const finalSummary =
- '我已经完成改动和验证,准备汇总。\n\n- targeted ESLint 通过\n- targeted Prettier check 通过\n- git diff --check 通过';
-
- const { container } = render(
- <Group
- id="assistant-1"
- messageIndex={0}
- blocks={[
- blk({
- content: finalSummary,
- id: 'block-1',
- tools: [
- { apiName: 'command_execution', id: 'tool-1' } as any,
- { apiName: 'command_execution', id: 'tool-2' } as any,
- { apiName: 'command_execution', id: 'tool-3' } as any,
- ],
- }),
- ]}
- />,
- );
-
- const sequence = Array.from(container.querySelectorAll('[data-testid]')).map((node) =>
- node.getAttribute('data-testid'),
- );
-
- expect(sequence).toEqual(['workflow-segment', 'answer-segment']);
- expect(parseWorkflowSegment()).toEqual([
- {
- content: '',
- contentOverride: '',
- disableMarkdownStreaming: false,
- domId: 'block-1__workflow',
- hasError: false,
- hasToolsOverride: true,
- toolCount: 3,
+ toolCount: 1,
},
]);
- expect(parseAnswerSegment()).toEqual({
- content: finalSummary,
- contentOverride: finalSummary,
- disableMarkdownStreaming: false,
- domId: 'block-1__answer',
- hasError: false,
- hasToolsOverride: false,
- id: 'block-1',
- isFirstBlock: false,
- toolCount: 0,
- });
});
it('keeps a short mixed status block inline when there is only one tool call', () => {
@@ -269,7 +206,10 @@ describe('Group', () => {
]);
});
- it('promotes the first sentence before folding a multi-tool workflow', () => {
+ it('keeps a mixed block full preamble visible above a folded multi-tool workflow', () => {
+ // The whole preamble (every sentence) stays in a visible answer segment; the
+ // workflow fold holds only the tools. Otherwise the prose past the first
+ // sentence would be hidden inside the collapsed-by-default workflow body.
const { container } = render(
<Group
id="assistant-1"
@@ -295,8 +235,8 @@ describe('Group', () => {
expect(sequence).toEqual(['answer-segment', 'workflow-segment']);
expect(parseAnswerSegment()).toEqual({
- content: '我先帮你查一下。',
- contentOverride: '我先帮你查一下。',
+ content: '我先帮你查一下。接下来我会继续整理结果。',
+ contentOverride: '我先帮你查一下。接下来我会继续整理结果。',
disableMarkdownStreaming: true,
domId: 'block-1__answer',
hasError: false,
@@ -307,8 +247,8 @@ describe('Group', () => {
});
expect(parseWorkflowSegment()).toEqual([
{
- content: '接下来我会继续整理结果。',
- contentOverride: '接下来我会继续整理结果。',
+ content: '',
+ contentOverride: '',
disableMarkdownStreaming: true,
domId: 'block-1__workflow',
hasError: false,
diff --git a/src/features/Conversation/Messages/AssistantGroup/components/Group.tsx b/src/features/Conversation/Messages/AssistantGroup/components/Group.tsx
index 0542d31fa51..bb40fcab509 100644
--- a/src/features/Conversation/Messages/AssistantGroup/components/Group.tsx
+++ b/src/features/Conversation/Messages/AssistantGroup/components/Group.tsx
@@ -9,12 +9,7 @@ import { type AssistantContentBlock } from '@/types/index';
import { messageStateSelectors, useConversationStore } from '../../../store';
import { MessageAggregationContext } from '../../Contexts/MessageAggregationContext';
-import { POST_TOOL_FINAL_ANSWER_SCORE_THRESHOLD } from '../constants';
-import {
- areWorkflowToolsComplete,
- getPostToolAnswerSplitIndex,
- scorePostToolBlockAsFinalAnswer,
-} from '../toolDisplayNames';
+import { areWorkflowToolsComplete, getPostToolAnswerSplitIndex } from '../toolDisplayNames';
import { CollapsedMessage } from './CollapsedMessage';
import GroupItem from './GroupItem';
import type { RenderableAssistantContentBlock } from './types';
@@ -58,11 +53,6 @@ interface PartitionedBlocks {
segments: GroupRenderSegment[];
}
-interface LeadingSentenceSplit {
- lead: string;
- remainder: string;
-}
-
const ANSWER_DOM_ID_SUFFIX = '__answer';
const WORKFLOW_DOM_ID_SUFFIX = '__workflow';
@@ -88,55 +78,6 @@ const hasReasoningContent = (block: AssistantContentBlock): boolean => {
return !!block.reasoning?.content?.trim();
};
-const isSentenceBoundary = (content: string, index: number): boolean => {
- const char = content[index];
- if (!char) return false;
- if (char === '。' || char === '!' || char === '?' || char === '!' || char === '?') return true;
- if (char !== '.') return false;
-
- const prev = content[index - 1] ?? '';
- const next = content[index + 1] ?? '';
- if (/[a-z\d]/i.test(prev) && /[a-z\d]/i.test(next)) return false;
- if (/\d/.test(prev) && /\d/.test(next)) return false;
-
- return true;
-};
-
-const extractLeadingSentenceSplit = (block: AssistantContentBlock): LeadingSentenceSplit | null => {
- const content = block.content ?? '';
- const trimmed = content.trim();
-
- if (!trimmed || trimmed === LOADING_FLAT) return null;
-
- let splitIndex = -1;
-
- for (let i = 0; i < content.length; i++) {
- if (!isSentenceBoundary(content, i)) continue;
- splitIndex = i + 1;
- break;
- }
-
- if (splitIndex === -1) {
- const paragraphBreak = content.search(/\n\s*\n/);
- if (paragraphBreak >= 0) splitIndex = paragraphBreak;
- }
-
- if (splitIndex === -1) {
- const firstLineBreak = content.indexOf('\n');
- if (firstLineBreak >= 0) splitIndex = firstLineBreak;
- }
-
- if (splitIndex === -1) return null;
-
- const lead = content.slice(0, splitIndex).trim();
- const remainder = content.slice(splitIndex).trimStart();
-
- if (!lead) return null;
- if (!remainder && !hasTools(block) && !hasReasoningContent(block) && !block.error) return null;
-
- return { lead, remainder };
-};
-
const isTrailingReasoningCandidate = (block: AssistantContentBlock): boolean => {
return hasReasoningContent(block) && !hasTools(block) && !block.error;
};
@@ -196,19 +137,10 @@ const appendWorkflowBlock = (
segments.push({ blocks: [block], kind: 'workflow' });
};
-const shouldPromoteMixedBlockContent = (block: AssistantContentBlock): boolean => {
- if (!hasTools(block) || !hasSubstantiveContent(block)) return false;
-
- return (
- scorePostToolBlockAsFinalAnswer({ ...block, tools: undefined }) >=
- POST_TOOL_FINAL_ANSWER_SCORE_THRESHOLD
- );
-};
-
const appendWorkflowRangeBlock = (
segments: GroupRenderSegment[],
block: AssistantContentBlock,
- allowLeadingSentencePromotion = false,
+ collapsesIntoWorkflow = false,
) => {
if (block.error) {
if (hasTools(block)) {
@@ -235,51 +167,37 @@ const appendWorkflowRangeBlock = (
return;
}
- if (!shouldPromoteMixedBlockContent(block)) {
- const leadingSentenceSplit =
- allowLeadingSentencePromotion && segments.length === 0 && hasTools(block)
- ? extractLeadingSentenceSplit(block)
- : null;
-
- if (leadingSentenceSplit) {
- appendAnswerBlock(
- segments,
- createAnswerRenderBlock(block, {
- content: leadingSentenceSplit.lead,
- error: undefined,
- imageList: undefined,
- reasoning: undefined,
- tools: undefined,
- }),
- );
- appendWorkflowBlock(
- segments,
- createWorkflowRenderBlock(block, {
- content: leadingSentenceSplit.remainder,
- }),
- );
- return;
- }
-
- appendWorkflowBlock(segments, block);
+ // A block that carries both tool calls and prose keeps its NATURAL order
+ // (content above the tool). Within one assistant message the model's text
+ // always precedes its tool_use — tool_use ends the turn, so any post-tool
+ // prose lands in a separate, tool-less block. A mixed block's content is
+ // therefore always a preamble, never a post-tool summary.
+ //
+ // In a single-tool turn the block renders inline as-is (content above its
+ // tool). In a multi-tool turn the tools collapse into a WorkflowCollapse that
+ // defaults to folded once complete, so we lift the full preamble into its own
+ // visible answer segment first and leave only the tool(s) in the fold —
+ // otherwise the explanation would be hidden inside the collapsed body.
+ if (collapsesIntoWorkflow && hasTools(block) && hasSubstantiveContent(block)) {
+ appendAnswerBlock(
+ segments,
+ createAnswerRenderBlock(block, {
+ error: undefined,
+ tools: undefined,
+ }),
+ );
+ appendWorkflowBlock(
+ segments,
+ createWorkflowRenderBlock(block, {
+ content: '',
+ imageList: undefined,
+ reasoning: undefined,
+ }),
+ );
return;
}
- appendWorkflowBlock(
- segments,
- createWorkflowRenderBlock(block, {
- content: '',
- imageList: undefined,
- }),
- );
- appendAnswerBlock(
- segments,
- createAnswerRenderBlock(block, {
- error: undefined,
- reasoning: undefined,
- tools: undefined,
- }),
- );
+ appendWorkflowBlock(segments, block);
};
const appendPostToolBlocks = (