🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(conversation): split workflow runs at mid-turn narration (#23053)
Mocha0163ffb6d77c
diff --git a/packages/conversation-flow/src/__tests__/assistantGroupContent.test.ts b/packages/conversation-flow/src/__tests__/assistantGroupContent.test.tsindex df5747602e2..407624c580d 100644--- a/packages/conversation-flow/src/__tests__/assistantGroupContent.test.ts+++ b/packages/conversation-flow/src/__tests__/assistantGroupContent.test.ts@@ -64,28 +64,128 @@ describe('isAssistantGroupStatusText', () => {});describe('partitionAssistantGroupBlocks', () => {- it('keeps a single-tool mixed block in workflow and the earlier answer final', () => {- const answer = block('answer', 'This is the visible final answer.');- const mixedTool = block(- 'mixed-tool',- 'This long tool narration has multiple sentences. It remains workflow content.',- { tools: [tool('only-tool')] },- );- const { segments } = partitionAssistantGroupBlocks([answer, mixedTool], {- isGenerating: false,- });+ const narration = [+ '正在读 0.7 版和 cc-switch 的额度查询实现,对照出 Claude、Codex、OpenCode 三家各自的取数接口,然后定实现方案。',+ '代码结构已摸清,正在核对 OpenCode 凭据的存放形式,随后把实现拆成工作包交给 Weaver 派发。',+ ];+ const realSteps = () => [+ block('step-1', '', { tools: [tool('a'), tool('b'), tool('c')] }),+ block('step-2', '', { tools: [tool('d'), tool('e')] }),+ block('step-3', narration[0]!, { tools: [tool('f'), tool('g')] }),+ block('step-4', '', { tools: [tool('h'), tool('i')] }),+ block('step-5', narration[1]!, { tools: [tool('j')] }),+ ];++ it.each([true, false])(+ 'splits real tool narration into answers (generating=%s)',+ (isGenerating) => {+ const final = block(+ 'step-6',+ '## 实现完成\n\n已接入三家额度接口。\n\n- Claude\n- Codex\n- OpenCode',+ );+ const { segments } = partitionAssistantGroupBlocks([...realSteps(), final], {+ isGenerating,+ toolsPhaseComplete: true,+ });+ expect(segments.map(({ kind }) => kind)).toEqual([+ 'workflow',+ 'answer',+ 'workflow',+ 'answer',+ 'workflow',+ 'answer',+ ]);+ expect(+ segments.flatMap((segment) =>+ segment.kind === 'workflow'+ ? [segment.blocks.reduce((sum, entry) => sum + (entry.tools?.length ?? 0), 0)]+ : [],+ ),+ ).toEqual([5, 4, 1]);+ expect(+ segments.flatMap((segment) => (segment.kind === 'answer' ? [segment.block.content] : [])),+ ).toEqual([...narration, final.content]);+ },+ );- expect(segments.map(({ kind }) => kind)).toEqual(['answer', 'workflow']);- expect(segments[1]).toMatchObject({- blocks: [{ content: mixedTool.content, id: mixedTool.id }],- kind: 'workflow',- });- expect(splitAssistantGroupFinalAnswer(segments).finalSegments).toEqual([- { block: answer, kind: 'answer' },- ]);+ it.each([true, false])(+ 'uses the last narration as final when a turn ends on tools (generating=%s)',+ (isGenerating) => {+ const { postToolTailPromoted, segments } = partitionAssistantGroupBlocks(realSteps(), {+ isGenerating,+ toolsPhaseComplete: true,+ });+ expect(postToolTailPromoted).toBe(false);+ expect(segments.map(({ kind }) => kind)).toEqual([+ 'workflow',+ 'answer',+ 'workflow',+ 'answer',+ 'workflow',+ ]);+ expect(splitAssistantGroupFinalAnswer(segments).finalSegments).toEqual([+ expect.objectContaining({+ block: expect.objectContaining({ content: narration[1] }),+ kind: 'answer',+ }),+ ]);+ },+ );++ it.each([true, false])(+ 'promotes long pure prose between tools without an empty workflow (generating=%s)',+ (isGenerating) => {+ const prose =+ 'The implementation has been inspected. '.repeat(5) + '\n\nNext, verify the credentials.';+ const { segments } = partitionAssistantGroupBlocks(+ [+ block('first', '', { tools: [tool('a')] }),+ block('prose', prose, { reasoning: { content: 'Inspecting credentials' } }),+ block('last', '', { tools: [tool('b')] }),+ ],+ { isGenerating },+ );+ expect(segments.map(({ kind }) => kind)).toEqual(['workflow', 'answer', 'workflow']);+ expect(segments[1]).toMatchObject({+ block: { content: prose, tools: undefined, error: undefined },+ });+ expect(segments[2]).toMatchObject({ blocks: [{ id: 'last' }] });+ },+ );++ it.each(['', '...', ' '])('does not promote a placeholder %j', (content) => {+ const { segments } = partitionAssistantGroupBlocks(+ [block('first', content, { tools: [tool('a'), tool('b')] })],+ { isGenerating: true },+ );+ expect(segments.map(({ kind }) => kind)).toEqual(['workflow']);});- it('projects answer-like mixed content out of a multi-tool workflow', () => {+ it.each([true, false])(+ 'keeps a single-tool mixed block in workflow and the earlier answer final (generating=%s)',+ (isGenerating) => {+ const answer = block('answer', 'This is the visible final answer.');+ const mixedTool = block(+ 'mixed-tool',+ 'This long tool narration has multiple sentences. It remains workflow content.',+ { tools: [tool('only-tool')] },+ );+ const { segments } = partitionAssistantGroupBlocks([answer, mixedTool], {+ isGenerating,+ });++ expect(segments.map(({ kind }) => kind)).toEqual(['answer', 'workflow']);+ expect(segments[1]).toMatchObject({+ blocks: [{ content: mixedTool.content, id: mixedTool.id }],+ kind: 'workflow',+ });+ expect(splitAssistantGroupFinalAnswer(segments).finalSegments).toEqual([+ { block: answer, kind: 'answer' },+ ]);+ },+ );++ it('projects short narration and answer-like mixed content out of a multi-tool workflow', () => {const detailedAnswer ='The migration is complete. All affected paths now share the same invariant.';const { segments } = partitionAssistantGroupBlocks(@@ -96,8 +196,12 @@ describe('partitionAssistantGroupBlocks', () => {{ isGenerating: false },);- expect(segments.map(({ kind }) => kind)).toEqual(['workflow', 'answer', 'workflow']);- expect(segments[1]).toMatchObject({+ expect(segments.map(({ kind }) => kind)).toEqual(['answer', 'workflow', 'answer', 'workflow']);+ expect(segments[0]).toMatchObject({+ block: { content: 'I will inspect the code.', tools: undefined },+ kind: 'answer',+ });+ expect(segments[2]).toMatchObject({block: {content: detailedAnswer,id: 'answer-with-tool',@@ -106,7 +210,7 @@ describe('partitionAssistantGroupBlocks', () => {},kind: 'answer',});- expect(segments[2]).toMatchObject({+ expect(segments[3]).toMatchObject({blocks: [{content: '',@@ -146,21 +250,43 @@ describe('partitionAssistantGroupBlocks', () => {{ isBreakoutTool, isGenerating: false },);- expect(segments.map(({ kind }) => kind)).toEqual(['workflow', 'workflow', 'workflow']);+ expect(segments.map(({ kind }) => kind)).toEqual([+ 'answer',+ 'workflow',+ 'answer',+ 'workflow',+ 'workflow',+ 'workflow',+ ]);expect(segments[0]).toMatchObject({+ block: { content: 'Looking.', tools: undefined },+ kind: 'answer',+ });+ expect(segments[1]).toMatchObject({+ blocks: [{ id: 'step-1', content: '' }],+ kind: 'workflow',+ });+ expect(segments[2]).toMatchObject({+ block: {+ content: 'Checking the screenshot.',+ reasoning: { content: 'thinking' },+ tools: undefined,+ },+ kind: 'answer',+ });+ expect(segments[3]).toMatchObject({blocks: [- { id: 'step-1' },{- content: 'Checking the screenshot.',+ content: '',id: 'step-2',projection: 'workflow',- reasoning: { content: 'thinking' },+ reasoning: undefined,tools: [expect.objectContaining({ id: 'bash-2' })],},],kind: 'workflow',});- expect(segments[1]).toMatchObject({+ expect(segments[4]).toMatchObject({blocks: [{content: '',@@ -173,7 +299,7 @@ describe('partitionAssistantGroupBlocks', () => {kind: 'workflow',standalone: true,});- expect(segments[2]).toMatchObject({+ expect(segments[5]).toMatchObject({blocks: [{content: '',@@ -223,17 +349,37 @@ describe('partitionAssistantGroupBlocks', () => {]);});- it('keeps a short post-tool status in workflow while generating', () => {+ it('keeps short post-tool status inside a single-tool workflow while generating', () => {const { postToolTailPromoted, segments } = partitionAssistantGroupBlocks([- block('tool-step', 'Searching.', { tools: [tool('search-call')] }),+ block('tool', 'Searching.', { tools: [tool('search')] }),+ block('status', 'Now I will summarize the results.'),+ ],+ { isGenerating: true, toolsPhaseComplete: true },+ );+ expect(postToolTailPromoted).toBe(false);+ expect(segments).toEqual([+ {+ kind: 'workflow',+ blocks: [+ block('tool', 'Searching.', { tools: [tool('search')] }),+ block('status', 'Now I will summarize the results.'),+ ],+ },+ ]);+ });++ it('shows short post-tool narration without marking the final answer promoted while generating', () => {+ const { postToolTailPromoted, segments } = partitionAssistantGroupBlocks(+ [+ block('tool-step', '', { tools: [tool('search-call'), tool('read-call')] }),block('status', 'Now I will summarize the results.'),],{ isGenerating: true, toolsPhaseComplete: true },);expect(postToolTailPromoted).toBe(false);- expect(segments.map(({ kind }) => kind)).toEqual(['workflow']);+ expect(segments.map(({ kind }) => kind)).toEqual(['workflow', 'answer']);});});diff --git a/packages/conversation-flow/src/assistantGroupContent.ts b/packages/conversation-flow/src/assistantGroupContent.tsindex bcece90e22f..0fb6ffd1ea3 100644--- a/packages/conversation-flow/src/assistantGroupContent.ts+++ b/packages/conversation-flow/src/assistantGroupContent.ts@@ -168,9 +168,6 @@ export const isAssistantGroupStatusText = (content?: string | null): boolean =>return sentenceCount <= 1;};-const shouldPromoteMixedBlockContent = (block: AssistantContentBlock): boolean =>- hasTools(block) && hasSubstantiveContent(block) && !isAssistantGroupStatusText(block.content);-const appendWorkflowRangeBlock = (segments: AssistantGroupSegment[],block: AssistantContentBlock,@@ -207,7 +204,7 @@ const appendWorkflowRangeBlock = (return;}- if (collapsesIntoWorkflow && shouldPromoteMixedBlockContent(block)) {+ if (collapsesIntoWorkflow && hasSubstantiveContent(block)) {appendAnswerBlock(segments,createProjectedBlock(block, 'answer', {@@ -215,13 +212,15 @@ const appendWorkflowRangeBlock = (tools: undefined,}),);- appendWorkflowRest(- createProjectedBlock(block, 'workflow', {- content: '',- imageList: undefined,- reasoning: undefined,- }),- );+ if (hasTools(block)) {+ appendWorkflowRest(+ createProjectedBlock(block, 'workflow', {+ content: '',+ imageList: undefined,+ reasoning: undefined,+ }),+ );+ }return;}diff --git a/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx b/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsxindex d6ab3f79bc8..5e78afe07f2 100644--- a/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx+++ b/src/features/Conversation/Messages/AssistantGroup/components/Group.test.tsx@@ -343,7 +343,7 @@ describe('Group', () => {]);});- it('folds consecutive short mixed single-tool blocks into one workflow segment', () => {+ it('shows short narration between consecutive single-tool workflow steps', () => {const { container } = render(<GroupisLatestItem@@ -368,27 +368,57 @@ describe('Group', () => {node.getAttribute('data-testid'),);- expect(sequence).toEqual(['workflow-segment']);- expect(screen.queryByTestId('answer-segment')).not.toBeInTheDocument();- expect(parseWorkflowSegment()).toEqual([- {+ expect(sequence).toEqual([+ 'answer-segment',+ 'answer-segment',+ 'answer-segment',+ 'answer-segment',+ ]);+ expect(screen.queryByTestId('workflow-segment')).not.toBeInTheDocument();+ const rendered = screen+ .getAllByTestId('answer-segment')+ .map((node) => JSON.parse(node.getAttribute('data-block') || '{}'));+ expect(rendered).toEqual([+ expect.objectContaining({content: 'Let me inspect the package scripts.',+ id: 'block-1',+ domId: 'block-1__answer',+ toolCount: 0,+ hasToolsOverride: false,disableMarkdownStreaming: true,- domId: undefined,hasError: false,+ }),+ expect.objectContaining({+ content: '',+ id: 'block-1',+ domId: 'block-1__workflow',toolCount: 1,- },- {+ hasToolsOverride: true,+ disableMarkdownStreaming: true,+ hasError: false,+ }),+ expect.objectContaining({content: 'Now let me read the source file.',+ id: 'block-2',+ domId: 'block-2__answer',+ toolCount: 0,+ hasToolsOverride: false,disableMarkdownStreaming: false,- domId: undefined,hasError: false,+ }),+ expect.objectContaining({+ content: '',+ id: 'block-2',+ domId: 'block-2__workflow',toolCount: 1,- },+ hasToolsOverride: true,+ disableMarkdownStreaming: false,+ hasError: false,+ }),]);});- it('breaks an image-bearing tool out between two workflow folds', () => {+ it('breaks an image-bearing tool out and shows narration between workflow runs', () => {const { container } = render(<GroupisLatestItem@@ -426,25 +456,31 @@ describe('Group', () => {const sequence = Array.from(container.querySelectorAll('[data-testid]')).map((node) =>node.getAttribute('data-testid'),);- expect(sequence).toEqual(['workflow-segment', 'answer-segment', 'workflow-segment']);-- const [first, second] = screen- .getAllByTestId('workflow-segment')- .map((node) => JSON.parse(node.getAttribute('data-blocks') || '[]'));- expect(first).toEqual([+ expect(sequence).toEqual([+ 'answer-segment',+ 'workflow-segment',+ 'answer-segment',+ 'answer-segment',+ 'answer-segment',+ 'answer-segment',+ ]);+ expect(parseWorkflowSegment()).toEqual([expect.objectContaining({- contentOverride: 'Inspecting.',+ contentOverride: '',domId: 'block-1__tool-0__workflow',toolCount: 2,}),]);- expect(second).toEqual([- expect.objectContaining({ domId: 'block-1__tool-3__workflow', toolCount: 1 }),- expect.objectContaining({ content: 'Continuing.', toolCount: 1 }),- ]);- expect(parseAnswerSegment()).toEqual(+ const answers = screen+ .getAllByTestId('answer-segment')+ .map((node) => JSON.parse(node.getAttribute('data-block') || '{}'));+ expect(answers).toEqual([+ expect.objectContaining({ content: 'Inspecting.', domId: 'block-1__answer', toolCount: 0 }),expect.objectContaining({ domId: 'block-1__tool-2__workflow', id: 'block-1', toolCount: 1 }),- );+ expect.objectContaining({ content: '', domId: 'block-1__tool-3__workflow', toolCount: 1 }),+ expect.objectContaining({ content: 'Continuing.', domId: 'block-2__answer', toolCount: 0 }),+ expect.objectContaining({ content: '', domId: 'block-2__workflow', toolCount: 1 }),+ ]);});it('does not fold the latest process behind a non-renderable final answer placeholder', () => {@@ -470,7 +506,9 @@ describe('Group', () => {expect(screen.queryByTestId('process-fold')).not.toBeInTheDocument();expect(screen.getByTestId('workflow-segment')).toBeInTheDocument();- expect(screen.queryByTestId('answer-segment')).not.toBeInTheDocument();+ expect(parseAnswerSegment()).toEqual(+ expect.objectContaining({ content: 'I will run the checks.', toolCount: 0 }),+ );});it('keeps a non-latest finished turn’s final answer visible outside the fold', () => {@@ -488,7 +526,11 @@ describe('Group', () => {);const fold = screen.getByTestId('process-fold');- const answer = screen.getByTestId('answer-segment');+ const [narration, answer] = screen.getAllByTestId('answer-segment');+ expect(fold.contains(narration)).toBe(true);+ expect(JSON.parse(answer.getAttribute('data-block') || '{}').content).toBe(+ 'Here is the final answer.',+ );// Folding only ever collapses the process; the final answer stays a visible// sibling for every turn, latest or not — never swallowed into the fold.expect(fold.contains(answer)).toBe(false);@@ -552,7 +594,11 @@ describe('Group', () => {);const fold = screen.getByTestId('process-fold');- const answer = screen.getByTestId('answer-segment');+ const [narration, answer] = screen.getAllByTestId('answer-segment');+ expect(fold.contains(narration)).toBe(true);+ expect(JSON.parse(answer.getAttribute('data-block') || '{}').content).toBe(+ 'Here is the final answer.',+ );// Latest turn: the answer stays out of the fold so it reads without expanding.expect(fold.contains(answer)).toBe(false);expect(fold.contains(screen.getByTestId('workflow-segment'))).toBe(true);diff --git a/src/features/Conversation/Messages/AssistantGroup/components/groupChain.test.ts b/src/features/Conversation/Messages/AssistantGroup/components/groupChain.test.tsindex 7e8b2c9cd66..1906467e8b5 100644--- a/src/features/Conversation/Messages/AssistantGroup/components/groupChain.test.ts+++ b/src/features/Conversation/Messages/AssistantGroup/components/groupChain.test.ts@@ -120,3 +120,51 @@ describe('buildChainView MCP App views', () => {expect(viewKey([appTool, nextTool])).toBe(alone);});});++describe('buildChainView answer keys', () => {+ const tool = (id: string) => ({+ apiName: 'read',+ arguments: '{}',+ id,+ identifier: 'test',+ result: { content: 'done', id: `${id}-result` },+ type: 'default' as const,+ });+ const answers = (blocks: AssistantContentBlock[], isGenerating: boolean) =>+ buildChainView(+ { blocks, id: 'group' },+ { hasActiveOperation: isGenerating, isGenerating },+ ).segments.flatMap((segment) =>+ segment.kind === 'answer'+ ? [{ content: segment.block.content, key: segment.block.renderKey }]+ : [],+ );++ it.each([+ 'I am inspecting the credentials.',+ '## Inspection\n\nI am inspecting the credential storage.',+ ])('preserves the streamed prose key when the same step gains tool calls: %s', (content) => {+ const first = { content: '', id: 'first', tools: [tool('a'), tool('b')] };+ const prose = { content, id: 'prose' };+ const before = answers([first, prose], true);+ expect(before).toEqual([{ content: prose.content, key: 'prose' }]);+ expect(answers([first, { ...prose, tools: [tool('c')] }], true)).toEqual(before);+ });++ it.each(['A short summary.', '## Summary\n\nThe implementation is complete.'])(+ 'preserves narration and final answer keys when generation ends: %s',+ (content) => {+ const blocks = [+ { content: '', id: 'first', tools: [tool('a'), tool('b')] },+ { content: 'Inspecting credentials.', id: 'middle', tools: [tool('c')] },+ { content, id: 'final' },+ ];+ const streaming = answers(blocks, true);+ expect(streaming).toEqual([+ { content: 'Inspecting credentials.', key: 'middle' },+ { content, key: 'final' },+ ]);+ expect(answers(blocks, false)).toEqual(streaming);+ },+ );+});diff --git a/src/features/Conversation/Messages/AssistantGroup/components/groupChain.ts b/src/features/Conversation/Messages/AssistantGroup/components/groupChain.tsindex e1354909876..e20bc968972 100644--- a/src/features/Conversation/Messages/AssistantGroup/components/groupChain.ts+++ b/src/features/Conversation/Messages/AssistantGroup/components/groupChain.ts@@ -237,7 +237,14 @@ const toRenderSegments = (markTaskCompletionNotices(segments.map((segment) =>segment.kind === 'answer'- ? { block: toRenderableBlock(segment.block, () => false), kind: 'answer' }+ ? {+ block: {+ ...toRenderableBlock(segment.block, () => false),+ // Prose keeps its identity when tools arrive or its projection ends.+ renderKey: segment.block.id,+ },+ kind: 'answer',+ }: {...segment,blocks: segment.blocks.map((block) => toRenderableBlock(block, isBreakout)),