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-flow): fold toolless turn-head into the tool chain group (#16084)

Arvin Xubb3e68905365
diff --git a/packages/conversation-flow/src/__tests__/parse.test.ts b/packages/conversation-flow/src/__tests__/parse.test.ts
index 11101d08061..ef1030e9834 100644
--- a/packages/conversation-flow/src/__tests__/parse.test.ts
+++ b/packages/conversation-flow/src/__tests__/parse.test.ts
@@ -442,6 +442,149 @@ describe('parse', () => {
expect(serializeParseResult(result)).toEqual(outputs.assistantGroup.toolsWithBranches);
});
+
+ // Regression: a hetero-agent (e.g. Claude Code) turn often opens with a
+ // TOOLLESS narration step streamed in reply to the user BEFORE the first
+ // tool call, with the tool-using step as its direct child. Previously the
+ // toolless head fell through to "Priority 4: regular message" and rendered
+ // as its own standalone bubble, while the tool step opened a SEPARATE
+ // assistantGroup β€” so the UI showed two disconnected assistant cards (a
+ // visually broken chain). The head must instead seed the single
+ // assistantGroup.
+ it('should fold a toolless turn-head into the following tool chain (single group)', () => {
+ const messages: Message[] = [
+ {
+ content: 'Can you check the build status?',
+ createdAt: 0,
+ id: 'u1',
+ role: 'user',
+ updatedAt: 0,
+ },
+ {
+ content: 'Sure β€” let me first find where the CI config lives.',
+ createdAt: 1,
+ id: 'a-head', // toolless narration, parent is the user message
+ parentId: 'u1',
+ role: 'assistant',
+ updatedAt: 1,
+ },
+ {
+ content: 'Found it. Reading the workflow file now.',
+ createdAt: 2,
+ id: 'a-tool',
+ parentId: 'a-head', // direct child of the toolless head
+ role: 'assistant',
+ tools: [
+ { apiName: 'readFile', arguments: '{}', id: 't1', identifier: 'fs', type: 'default' },
+ ],
+ updatedAt: 2,
+ },
+ {
+ content: 'name: CI\non: [push]',
+ createdAt: 3,
+ id: 't1',
+ parentId: 'a-tool',
+ role: 'tool',
+ tool_call_id: 't1',
+ updatedAt: 3,
+ },
+ {
+ content: 'The build runs on every push and is currently green.',
+ createdAt: 4,
+ id: 'a-final',
+ parentId: 't1',
+ role: 'assistant',
+ updatedAt: 4,
+ },
+ ] as Message[];
+
+ const result = parse(messages);
+
+ // user + ONE assistantGroup (not user + standalone assistant + group)
+ expect(result.flatList).toHaveLength(2);
+ expect(result.flatList[0].id).toBe('u1');
+ expect(result.flatList[1].role).toBe('assistantGroup');
+
+ // The toolless head is the first child of the group, with its prose intact,
+ // followed by the tool step and the final answer β€” all in one card.
+ const children = (result.flatList[1] as any).children;
+ expect(children.map((c: any) => c.id)).toEqual(['a-head', 'a-tool', 'a-final']);
+ expect(children[0].content).toBe('Sure β€” let me first find where the CI config lives.');
+ expect(children[0].tools).toBeUndefined();
+ expect(children[1].tools[0].result_msg_id).toBe('t1');
+ });
+
+ // Guard for the narrow scope above: when MORE than one toolless prose step
+ // precedes the first tool call, the head is NOT folded. collectAssistantChain
+ // stops at the first toolless continuation, so folding here would emit a
+ // tools-less assistantGroup and still leave the tool step split. The multi-
+ // prose prelude must instead stay as plain assistant bubbles β€” and crucially
+ // we must never produce an assistantGroup with no tools.
+ it('should not fold a multi-step toolless prelude (no tools-less assistantGroup)', () => {
+ const messages: Message[] = [
+ {
+ content: 'Can you check the build status?',
+ createdAt: 0,
+ id: 'u1',
+ role: 'user',
+ updatedAt: 0,
+ },
+ {
+ content: 'Sure, let me think about where to look.',
+ createdAt: 1,
+ id: 'a-head', // toolless, parent is the user message
+ parentId: 'u1',
+ role: 'assistant',
+ updatedAt: 1,
+ },
+ {
+ content: 'The CI config is probably under .github/workflows.',
+ createdAt: 2,
+ id: 'a-prose', // SECOND toolless prose step before any tool
+ parentId: 'a-head',
+ role: 'assistant',
+ updatedAt: 2,
+ },
+ {
+ content: 'Reading it now.',
+ createdAt: 3,
+ id: 'a-tool',
+ parentId: 'a-prose',
+ role: 'assistant',
+ tools: [
+ { apiName: 'readFile', arguments: '{}', id: 't1', identifier: 'fs', type: 'default' },
+ ],
+ updatedAt: 3,
+ },
+ {
+ content: 'name: CI',
+ createdAt: 4,
+ id: 't1',
+ parentId: 'a-tool',
+ role: 'tool',
+ tool_call_id: 't1',
+ updatedAt: 4,
+ },
+ ] as Message[];
+
+ const result = parse(messages);
+
+ // No assistantGroup may exist without tools in any of its children.
+ const toollessGroups = result.flatList.filter(
+ (m) =>
+ m.role === 'assistantGroup' &&
+ ((m as any).children ?? []).every((c: any) => !c.tools || c.tools.length === 0),
+ );
+ expect(toollessGroups).toHaveLength(0);
+
+ // The two prose steps render as plain assistant bubbles; the tool step
+ // forms its own (well-formed) assistantGroup.
+ const head = result.flatList.find((m) => m.id === 'a-head');
+ expect(head?.role).toBe('assistant');
+ const toolGroup = result.flatList.find((m) => m.id === 'a-tool');
+ expect(toolGroup?.role).toBe('assistantGroup');
+ expect((toolGroup as any).children[0].tools[0].result_msg_id).toBe('t1');
+ });
});
describe('Agent Group Scenarios', () => {
diff --git a/packages/conversation-flow/src/transformation/FlatListBuilder.ts b/packages/conversation-flow/src/transformation/FlatListBuilder.ts
index 2d48c9e3101..d3f70e48397 100644
--- a/packages/conversation-flow/src/transformation/FlatListBuilder.ts
+++ b/packages/conversation-flow/src/transformation/FlatListBuilder.ts
@@ -210,8 +210,15 @@ export class FlatListBuilder {
continue;
}
- // Priority 2: AssistantGroup (assistant + tools)
- if (message.role === 'assistant' && message.tools && message.tools.length > 0) {
+ // Priority 2: AssistantGroup (assistant + tools), or the toolless
+ // narration step that heads a tool-using chain β€” the latter must seed the
+ // group instead of splitting into its own standalone bubble. Supervisors
+ // are excluded from the toolless-head path so they still fall to 2b.
+ if (
+ message.role === 'assistant' &&
+ ((message.tools && message.tools.length > 0) ||
+ (!message.metadata?.isSupervisor && this.messageCollector.isToolChainHead(message)))
+ ) {
// Collect the entire assistant group chain
const assistantChain: Message[] = [];
const allToolMessages: Message[] = [];
diff --git a/packages/conversation-flow/src/transformation/MessageCollector.ts b/packages/conversation-flow/src/transformation/MessageCollector.ts
index b0012cd2f63..9950096028a 100644
--- a/packages/conversation-flow/src/transformation/MessageCollector.ts
+++ b/packages/conversation-flow/src/transformation/MessageCollector.ts
@@ -107,6 +107,52 @@ export class MessageCollector {
return collected;
}
+ /**
+ * True when a TOOLLESS assistant is the head of a turn whose very next step
+ * calls tools β€” the narration the LLM streams in reply to the user
+ * immediately before its first tool call, with the tool-using step as its
+ * direct child. `collectAssistantChain` already walks correctly from such a
+ * head, but the flat-list dispatcher only opens an AssistantGroup when the
+ * message itself carries tools β€” so without this check the toolless head is
+ * emitted as its own standalone bubble and visually splits off from the group
+ * that starts at the first tool step (looks like a broken chain).
+ *
+ * Deliberately narrow:
+ * - Only a turn head (parent is a `user` message). A toolless step after a
+ * tool result is a mid-chain answer handled elsewhere; folding those would
+ * change unrelated grouping (and the contextTree path).
+ * - The immediate continuation must ALREADY carry tools. We intentionally do
+ * not walk through intermediate toolless prose steps: `collectAssistantChain`
+ * stops at the first toolless continuation (it only recurses through
+ * tool-using steps), so classifying a multi-prose head would yield a
+ * tools-less AssistantGroup and still leave the tool step split off. Such a
+ * multi-prose prelude therefore falls back to standalone bubbles rather than
+ * a malformed group.
+ * - A fork (>1 same-agent non-signal continuation) returns false so branch
+ * handling stays untouched.
+ */
+ isToolChainHead(assistant: Message): boolean {
+ if (assistant.role !== 'assistant') return false;
+ if (assistant.tools && assistant.tools.length > 0) return false;
+
+ const parent = assistant.parentId ? this.messageMap.get(assistant.parentId) : undefined;
+ if (parent?.role !== 'user') return false;
+
+ // A toolless step owns no tool results, so its only continuation candidates
+ // are its own non-signal, same-agent assistant children (mirrors
+ // findFlatChainContinuation for that case).
+ const groupAgentId = assistant.agentId;
+ const candidates = (this.childrenMap.get(assistant.id) ?? [])
+ .map((id) => this.messageMap.get(id))
+ .filter(
+ (m): m is Message =>
+ !!m && m.role === 'assistant' && m.agentId === groupAgentId && !getMessageSignal(m),
+ );
+ if (candidates.length !== 1) return false; // no continuation, or a fork β†’ defer to branch logic
+ const next = candidates[0];
+ return !!(next.tools && next.tools.length > 0); // only when the very next step calls tools
+ }
+
/**
* Recursively collect the entire assistant chain
* (assistant -> tools -> assistant -> tools -> ...)