Skip to content

Commit 157f52e

Browse files
committed
refactor(backend): simplify parentAgentId handling with switch statement
Replace nested if-else chains with cleaner switch statement when adding parentAgentId to nested agent events for proper UI nesting. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent db07ecc commit 157f52e

File tree

1 file changed

+56
-41
lines changed

1 file changed

+56
-41
lines changed

backend/src/run-programmatic-step.ts

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -309,47 +309,62 @@ export async function runProgrammaticStep(
309309
// Only add parentAgentId if this programmatic agent has a parent (i.e., it's nested)
310310
// This ensures we don't add parentAgentId to top-level spawns
311311
if (state.agentState.parentId) {
312-
// Add parentAgentId to nested agent events for proper nesting in UI
313-
if (
314-
chunk.type === 'subagent_start' ||
315-
chunk.type === 'subagent_finish'
316-
) {
317-
// Only add parentAgentId if it's not already set (e.g., by spawn_agents)
318-
if (!(chunk as any).parentAgentId) {
319-
logger.debug(
320-
{
321-
eventType: chunk.type,
322-
agentId: chunk.agentId,
323-
parentId: state.agentState.agentId,
324-
},
325-
`run-programmatic-step: Adding parentAgentId to ${chunk.type} event`,
326-
)
327-
onResponseChunk({
328-
...chunk,
329-
parentAgentId: state.agentState.agentId,
330-
})
331-
return
332-
}
333-
}
334-
335-
// Add parentAgentId to tool calls and results from nested agents
336-
if (chunk.type === 'tool_call' || chunk.type === 'tool_result') {
337-
// Only add parentAgentId if it's not already set
338-
if (!(chunk as any).parentAgentId) {
339-
logger.debug(
340-
{
341-
eventType: chunk.type,
342-
agentId: (chunk as any).agentId,
343-
parentId: state.agentState.agentId,
344-
},
345-
`run-programmatic-step: Adding parentAgentId to ${chunk.type} event`,
346-
)
347-
onResponseChunk({
348-
...chunk,
349-
parentAgentId: state.agentState.agentId,
350-
})
351-
return
352-
}
312+
const parentAgentId = state.agentState.agentId
313+
314+
switch (chunk.type) {
315+
case 'subagent_start':
316+
case 'subagent_finish':
317+
if (!chunk.parentAgentId) {
318+
logger.debug(
319+
{
320+
eventType: chunk.type,
321+
agentId: chunk.agentId,
322+
parentId: parentAgentId,
323+
},
324+
`run-programmatic-step: Adding parentAgentId to ${chunk.type} event`,
325+
)
326+
onResponseChunk({
327+
...chunk,
328+
parentAgentId,
329+
})
330+
return
331+
}
332+
break
333+
case 'tool_call':
334+
if (!chunk.parentAgentId) {
335+
logger.debug(
336+
{
337+
eventType: chunk.type,
338+
agentId: chunk.agentId,
339+
parentId: parentAgentId,
340+
},
341+
`run-programmatic-step: Adding parentAgentId to ${chunk.type} event`,
342+
)
343+
onResponseChunk({
344+
...chunk,
345+
parentAgentId,
346+
})
347+
return
348+
}
349+
break
350+
case 'tool_result':
351+
if (!chunk.parentAgentId) {
352+
logger.debug(
353+
{
354+
eventType: chunk.type,
355+
parentId: parentAgentId,
356+
},
357+
`run-programmatic-step: Adding parentAgentId to ${chunk.type} event`,
358+
)
359+
onResponseChunk({
360+
...chunk,
361+
parentAgentId,
362+
})
363+
return
364+
}
365+
break
366+
default:
367+
break
353368
}
354369
}
355370

0 commit comments

Comments
 (0)