Skip to content

Commit 340c154

Browse files
committed
fix(mothership): parallel subagent rendering
1 parent d93a6f5 commit 340c154

2 files changed

Lines changed: 27 additions & 15 deletions

File tree

apps/sim/app/workspace/[workspaceId]/home/components/message-content/message-content.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ function parseBlocks(blocks: ContentBlock[]): MessageSegment[] {
161161
segments.push({ ...nextGroup, isOpen })
162162
}
163163

164+
const lastSubagentBlockIndex = new Map<string, number>()
165+
for (let i = 0; i < blocks.length; i++) {
166+
const block = blocks[i]
167+
if (block.type === 'subagent' && block.content) {
168+
lastSubagentBlockIndex.set(block.content, i)
169+
}
170+
}
171+
const hasSubagentBlockAfter = (name: string, index: number): boolean => {
172+
const last = lastSubagentBlockIndex.get(name)
173+
return last !== undefined && last > index
174+
}
175+
164176
for (let i = 0; i < blocks.length; i++) {
165177
const block = blocks[i]
166178

@@ -267,6 +279,9 @@ function parseBlocks(blocks: ContentBlock[]): MessageSegment[] {
267279
const isDispatch = SUBAGENT_KEYS.has(tc.name) && !tc.calledBy
268280

269281
if (isDispatch) {
282+
if (hasSubagentBlockAfter(tc.name, i)) {
283+
continue
284+
}
270285
if (!group || group.agentName !== tc.name) {
271286
if (group) {
272287
pushGroup(group)

apps/sim/lib/copilot/request/go/stream.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -361,29 +361,26 @@ export async function runStreamLoop(
361361
flushSubagentThinkingBlock(context)
362362
flushThinkingBlock(context)
363363
if (spanEvt === MothershipStreamV1SpanLifecycleEvent.start) {
364-
const lastParent = context.subAgentParentStack[context.subAgentParentStack.length - 1]
365-
const lastBlock = context.contentBlocks[context.contentBlocks.length - 1]
366364
if (toolCallId) {
367-
if (lastParent !== toolCallId) {
365+
if (!context.subAgentParentStack.includes(toolCallId)) {
368366
context.subAgentParentStack.push(toolCallId)
369367
}
370368
context.subAgentParentToolCallId = toolCallId
371369
context.subAgentContent[toolCallId] ??= ''
372370
context.subAgentToolCalls[toolCallId] ??= []
373371
}
374-
if (
375-
subagentName &&
376-
!(
377-
lastParent === toolCallId &&
378-
lastBlock?.type === 'subagent' &&
379-
lastBlock.content === subagentName
372+
if (subagentName) {
373+
const alreadyOpen = context.contentBlocks.some(
374+
(b) =>
375+
b.type === 'subagent' && b.content === subagentName && b.endedAt === undefined
380376
)
381-
) {
382-
context.contentBlocks.push({
383-
type: 'subagent',
384-
content: subagentName,
385-
timestamp: Date.now(),
386-
})
377+
if (!alreadyOpen) {
378+
context.contentBlocks.push({
379+
type: 'subagent',
380+
content: subagentName,
381+
timestamp: Date.now(),
382+
})
383+
}
387384
}
388385
return
389386
}

0 commit comments

Comments
 (0)