π€― 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-flow): keep a group member's speak reply in the transcript (#20364)
Arvin Xu4532bb79e9c7
diff --git a/packages/conversation-flow/src/__tests__/groupMemberReply.test.ts b/packages/conversation-flow/src/__tests__/groupMemberReply.test.tsnew file mode 100644index 00000000000..3bbb21c0770--- /dev/null+++ b/packages/conversation-flow/src/__tests__/groupMemberReply.test.ts@@ -0,0 +1,185 @@+import { describe, expect, it } from 'vitest';++import { parse } from '../parse';+import type { Message } from '../types';++/**+ * Server-side `speak` parents the in-group member's reply to the SUPERVISOR+ * assistant that issued the call (a sibling of the tool result), while the+ * supervisor continues through the tool result. Once the supervisor resumes,+ * the speak turn becomes an intermediate step of the collected assistant+ * chain, and the member reply must still render (#19552).+ */+const message = (+ id: string,+ role: Message['role'],+ parentId: string | null,+ createdAt: number,+ extra: Partial<Message> = {},+): Message =>+ ({+ agentId: 'supervisor',+ content: id,+ createdAt,+ id,+ parentId,+ role,+ updatedAt: createdAt,+ ...extra,+ }) as Message;++const speakCall = (id: string, parentId: string, createdAt: number, resultId: string) =>+ message(id, 'assistant', parentId, createdAt, {+ metadata: { isSupervisor: true, orchestrationRole: 'supervisor' },+ tools: [+ {+ apiName: 'speak',+ arguments: '{"agentId":"member"}',+ id: `${id}-tool`,+ identifier: 'lobe-group-management',+ result_msg_id: resultId,+ type: 'builtin',+ },+ ],+ });++const memberReply = (+ id: string,+ parentId: string,+ createdAt: number,+ extra: Partial<Message> = {},+) =>+ message(id, 'assistant', parentId, createdAt, {+ agentId: 'member',+ content: 'SYNTHETIC_MEMBER_REPLY',+ metadata: { orchestrationRole: 'member' },+ ...extra,+ });++/** Fixture from the issue: user β speak call β (tool result β follow-up, member reply). */+const singleSpeak = (): Message[] => [+ message('user', 'user', null, 1),+ speakCall('supervisor-call', 'user', 2, 'tool-result'),+ message('tool-result', 'tool', 'supervisor-call', 3, {+ content: 'Member started',+ tool_call_id: 'supervisor-call-tool',+ }),+ memberReply('member-reply', 'supervisor-call', 4),+ message('supervisor-followup', 'assistant', 'tool-result', 5),+];++/** Same shape with an earlier supervisor tool step, so the speak turn is nested in the group. */+const nestedSpeak = (): Message[] => [+ message('user', 'user', null, 1),+ message('prep-call', 'assistant', 'user', 1.5, {+ tools: [+ {+ apiName: 'prepare',+ arguments: '{}',+ id: 'prep-tool',+ identifier: 'example',+ result_msg_id: 'prep-result',+ type: 'builtin',+ },+ ],+ }),+ message('prep-result', 'tool', 'prep-call', 2, { tool_call_id: 'prep-tool' }),+ speakCall('supervisor-call', 'prep-result', 4, 'tool-result'),+ message('tool-result', 'tool', 'supervisor-call', 5, { tool_call_id: 'supervisor-call-tool' }),+ memberReply('member-reply', 'supervisor-call', 6),+ message('supervisor-followup', 'assistant', 'tool-result', 7),+];++const countIds = (messages: Message[]) => {+ const counts = new Map<string, number>();+ const visit = (node: unknown) => {+ if (!node || typeof node !== 'object') return;+ if (Array.isArray(node)) return node.forEach(visit);+ const record = node as Record<string, unknown>;+ if (typeof record.id === 'string') counts.set(record.id, (counts.get(record.id) ?? 0) + 1);+ Object.values(record).forEach(visit);+ };+ visit(parse(messages).flatList);+ return counts;+};++describe('group member reply under an intermediate speak call (#19552)', () => {+ it.each([+ ['single speak', singleSpeak],+ ['speak nested after an earlier tool step', nestedSpeak],+ ])('renders the member reply exactly once (%s)', (_, build) => {+ const messages = build();+ const original = structuredClone(messages);+ const result = parse(messages);++ expect(result.messageMap['member-reply']).toBeDefined();+ const counts = countIds(messages);+ expect(counts.get('member-reply')).toBe(1);+ // The supervisor chain stays intact inside its group.+ expect(counts.get('supervisor-call')).toBeGreaterThan(0);+ expect(counts.get('supervisor-followup')).toBe(1);+ expect(result.flatList.at(-1)?.id).toBe('member-reply');+ expect(messages).toEqual(original);+ });++ it('keeps the member reply when the supervisor has not resumed yet', () => {+ const messages = singleSpeak().filter((m) => m.id !== 'supervisor-followup');+ expect(countIds(messages).get('member-reply')).toBe(1);+ });++ it('renders a multi-step member run and the turn that follows it', () => {+ const messages: Message[] = [+ ...singleSpeak().map((m) =>+ m.id === 'member-reply'+ ? memberReply('member-reply', 'supervisor-call', 4, {+ tools: [+ {+ apiName: 'search',+ arguments: '{}',+ id: 'member-tool',+ identifier: 'web',+ result_msg_id: 'member-tool-result',+ type: 'builtin',+ },+ ],+ })+ : m,+ ),+ message('member-tool-result', 'tool', 'member-reply', 4.5, {+ agentId: 'member',+ tool_call_id: 'member-tool',+ }),+ memberReply('member-final', 'member-tool-result', 4.8),+ message('next-user', 'user', 'supervisor-followup', 6),+ ];+ const counts = countIds(messages);+ for (const id of ['member-tool-result', 'member-final', 'next-user']) {+ expect(counts.get(id), id).toBe(1);+ }+ // The member's own tool run renders as one group (keyed by its first step),+ // before the user's next message rather than after it.+ expect(parse(messages).flatList.map((m) => m.id)).toEqual([+ 'user',+ 'supervisor-call',+ 'member-reply',+ 'next-user',+ ]);+ });++ it('does not surface a regenerated supervisor branch under the speak call', () => {+ const messages = [+ ...singleSpeak(),+ message('supervisor-retry', 'assistant', 'supervisor-call', 4.5),+ ];+ const counts = countIds(messages);+ expect(counts.get('member-reply')).toBe(1);+ expect(counts.has('supervisor-retry')).toBe(false);+ });++ it('does not pull an isolated (threaded) member run into the main transcript', () => {+ const messages = singleSpeak().map((m) =>+ m.id === 'member-reply' ? { ...m, threadId: 'thread-1' } : m,+ );+ expect(countIds(messages).has('member-reply')).toBe(false);+ });+});diff --git a/packages/conversation-flow/src/transformation/FlatListBuilder.ts b/packages/conversation-flow/src/transformation/FlatListBuilder.tsindex 64ca0cb5e04..977f8263ee4 100644--- a/packages/conversation-flow/src/transformation/FlatListBuilder.ts+++ b/packages/conversation-flow/src/transformation/FlatListBuilder.ts@@ -627,6 +627,8 @@ export class FlatListBuilder {this.suppressInactiveExplicitContinuations(lastAssistant, allToolMessages, processedIds);}+ yield this.continueGroupMemberReplies(assistantChain, flatList, processedIds, allMessages);+const parentIds = [...(lastAssistant ? [lastAssistant.id] : []),...allToolMessages.map((toolMessage) => toolMessage.id),@@ -663,6 +665,46 @@ export class FlatListBuilder {);}+ /**+ * Server-side `speak` parents the in-group member's reply to the supervisor+ * assistant that issued the call β a sibling of the tool result β while the+ * supervisor resumes through the tool result. Once it resumes, the collector+ * folds the speak turn into the group as an intermediate step, so the member+ * reply hangs off a consumed assistant that no continuation walk visits.+ * Render it (and the member's own follow-up steps) right after the group.+ * Only `orchestrationRole: 'member'` children qualify: regenerated supervisor+ * branches under the same shell stay hidden. Isolated members live in their+ * own thread and stay out of the main transcript.+ */+ private *continueGroupMemberReplies(+ assistantChain: Message[],+ flatList: Message[],+ processedIds: Set<string>,+ allMessages: Message[],+ ): FlatListWork {+ for (const assistant of assistantChain.slice(0, -1)) {+ const memberIds = this.childIdsInScope(assistant.id).filter((id) => {+ const message = this.messageMap.get(id);+ return (+ message?.role === 'assistant' &&+ message.metadata?.orchestrationRole === 'member' &&+ (!message.threadId || message.threadId === this.threadScope) &&+ !processedIds.has(id)+ );+ });++ for (const memberId of memberIds) {+ yield this.buildFlatListRecursiveForChild(+ assistant.id,+ memberId,+ flatList,+ processedIds,+ allMessages,+ );+ }+ }+ }+/*** A user can interrupt a tool run before its final assistant is persisted.* The collector folds that run into one group, so the interruption is attached