Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ function parseBlocks(blocks: ContentBlock[]): MessageSegment[] {
segments.push({ ...nextGroup, isOpen })
}

const lastSubagentBlockIndex = new Map<string, number>()
for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]
if (block.type === 'subagent' && block.content) {
lastSubagentBlockIndex.set(block.content, i)
}
}
Comment thread
icecrasher321 marked this conversation as resolved.
const hasSubagentBlockAfter = (name: string, index: number): boolean => {
const last = lastSubagentBlockIndex.get(name)
return last !== undefined && last > index
}

for (let i = 0; i < blocks.length; i++) {
const block = blocks[i]

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

if (isDispatch) {
if (hasSubagentBlockAfter(tc.name, i)) {
continue
}
if (!group || group.agentName !== tc.name) {
if (group) {
pushGroup(group)
Expand Down
27 changes: 12 additions & 15 deletions apps/sim/lib/copilot/request/go/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,29 +361,26 @@ export async function runStreamLoop(
flushSubagentThinkingBlock(context)
flushThinkingBlock(context)
if (spanEvt === MothershipStreamV1SpanLifecycleEvent.start) {
const lastParent = context.subAgentParentStack[context.subAgentParentStack.length - 1]
const lastBlock = context.contentBlocks[context.contentBlocks.length - 1]
if (toolCallId) {
if (lastParent !== toolCallId) {
if (!context.subAgentParentStack.includes(toolCallId)) {
context.subAgentParentStack.push(toolCallId)
}
context.subAgentParentToolCallId = toolCallId
context.subAgentContent[toolCallId] ??= ''
context.subAgentToolCalls[toolCallId] ??= []
}
if (
subagentName &&
!(
lastParent === toolCallId &&
lastBlock?.type === 'subagent' &&
lastBlock.content === subagentName
if (subagentName) {
const alreadyOpen = context.contentBlocks.some(
(b) =>
b.type === 'subagent' && b.content === subagentName && b.endedAt === undefined
)
Comment thread
icecrasher321 marked this conversation as resolved.
Outdated
) {
context.contentBlocks.push({
type: 'subagent',
content: subagentName,
Comment thread
icecrasher321 marked this conversation as resolved.
timestamp: Date.now(),
})
if (!alreadyOpen) {
context.contentBlocks.push({
type: 'subagent',
content: subagentName,
timestamp: Date.now(),
})
}
}
Comment thread
icecrasher321 marked this conversation as resolved.
return
}
Expand Down
Loading