11import { isEqual } from 'lodash'
22
33import { formatToolOutput } from './codebuff-client'
4- import { shouldCollapseByDefault } from './constants'
4+ import { shouldCollapseByDefault , shouldCollapseForParent } from './constants'
55
66import type {
77 ContentBlock ,
@@ -250,6 +250,30 @@ export const appendInterruptionNotice = (
250250 return [ ...blocks , interruptionNotice ]
251251}
252252
253+ /**
254+ * Recursively finds an agent block by ID and returns its agent type.
255+ * Returns undefined if not found.
256+ */
257+ export const findAgentTypeById = (
258+ blocks : ContentBlock [ ] ,
259+ agentId : string ,
260+ ) : string | undefined => {
261+ for ( const block of blocks ) {
262+ if ( block . type === 'agent' ) {
263+ if ( block . agentId === agentId ) {
264+ return block . agentType
265+ }
266+ if ( block . blocks ) {
267+ const found = findAgentTypeById ( block . blocks , agentId )
268+ if ( found ) {
269+ return found
270+ }
271+ }
272+ }
273+ }
274+ return undefined
275+ }
276+
253277/**
254278 * Options for creating an agent content block.
255279 */
@@ -262,6 +286,8 @@ export interface CreateAgentBlockOptions {
262286 spawnToolCallId ?: string
263287 /** The index within the spawn_agents call */
264288 spawnIndex ?: number
289+ /** The agent type of the parent agent that spawned this one */
290+ parentAgentType ?: string
265291}
266292
267293/**
@@ -270,7 +296,10 @@ export interface CreateAgentBlockOptions {
270296export const createAgentBlock = (
271297 options : CreateAgentBlockOptions ,
272298) : AgentContentBlock => {
273- const { agentId, agentType, prompt, params, spawnToolCallId, spawnIndex } = options
299+ const { agentId, agentType, prompt, params, spawnToolCallId, spawnIndex, parentAgentType } = options
300+ const shouldCollapse =
301+ shouldCollapseByDefault ( agentType || '' ) ||
302+ shouldCollapseForParent ( agentType || '' , parentAgentType )
274303 return {
275304 type : 'agent' ,
276305 agentId,
@@ -283,7 +312,7 @@ export const createAgentBlock = (
283312 ...( params && { params } ) ,
284313 ...( spawnToolCallId && { spawnToolCallId } ) ,
285314 ...( spawnIndex !== undefined && { spawnIndex } ) ,
286- ...( shouldCollapseByDefault ( agentType || '' ) && { isCollapsed : true } ) ,
315+ ...( shouldCollapse && { isCollapsed : true } ) ,
287316 }
288317}
289318
0 commit comments