Skip to content

Commit 9014964

Browse files
refactor: only include agentId in tool calls for subagents
Conditionally add agentId to tool call metadata only when the agent has a parent. This prevents the base agent from unnecessarily including its ID in tool calls while maintaining proper tracking for spawned subagents. Also simplified CLI agent block logic by removing unused helper and using existing recursive update function. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent a2a0e48 commit 9014964

File tree

3 files changed

+14
-46
lines changed

3 files changed

+14
-46
lines changed

backend/src/tools/stream-parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ export async function processStreamWithTools(
160160
toolCallId,
161161
toolName,
162162
input,
163-
agentId: agentState.agentId,
163+
// Only include agentId for subagents (agents with a parent)
164+
...(agentState.parentId && { agentId: agentState.agentId }),
164165
})
165166
} else {
166167
// First non-str_replace tool marks end of str_replace phase

backend/src/tools/tool-executor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ export function executeToolCall<T extends ToolName>(
193193
toolCallId: toolCall.toolCallId,
194194
toolName,
195195
input: toolCall.input,
196-
agentId: state.agentState?.agentId,
196+
// Only include agentId for subagents (agents with a parent)
197+
...(state.agentState?.parentId && { agentId: state.agentState.agentId }),
197198
})
198199

199200
toolCalls.push(toolCall)
@@ -430,7 +431,8 @@ export async function executeCustomToolCall(
430431
toolCallId: toolCall.toolCallId,
431432
toolName,
432433
input: toolCall.input,
433-
agentId: state.agentState?.agentId,
434+
// Only include agentId for subagents (agents with a parent)
435+
...(state.agentState?.parentId && { agentId: state.agentState.agentId }),
434436
})
435437

436438
toolCalls.push(toolCall)

cli/src/hooks/use-send-message.ts

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,6 @@ const hiddenToolNames = new Set<ToolName | 'spawn_agent_inline'>([
2424
'spawn_agents',
2525
])
2626

27-
// Helper function to check if an agent block exists
28-
const findAgentBlock = (
29-
blocks: ContentBlock[],
30-
targetAgentId: string,
31-
): boolean => {
32-
for (const block of blocks) {
33-
if (block.type === 'agent' && block.agentId === targetAgentId) {
34-
return true
35-
}
36-
if (block.type === 'agent' && block.blocks) {
37-
if (findAgentBlock(block.blocks, targetAgentId)) {
38-
return true
39-
}
40-
}
41-
}
42-
return false
43-
}
44-
4527
// Helper function to recursively update blocks
4628
const updateBlocksRecursively = (
4729
blocks: ContentBlock[],
@@ -532,15 +514,12 @@ export const useSendMessage = ({
532514
setMessages((prev) =>
533515
prev.map((msg) => {
534516
if (msg.id === aiMessageId && msg.blocks) {
535-
const blocks = msg.blocks.map((block) => {
536-
if (
537-
block.type === 'agent' &&
538-
block.agentId === tempId
539-
) {
540-
return { ...block, agentId: event.agentId }
541-
}
542-
return block
543-
})
517+
// Use recursive update to rename nested agents too
518+
const blocks = updateBlocksRecursively(
519+
msg.blocks,
520+
tempId,
521+
(block) => ({ ...block, agentId: event.agentId }),
522+
)
544523
return { ...msg, blocks }
545524
}
546525
return msg
@@ -732,22 +711,8 @@ export const useSendMessage = ({
732711
agentId: agentId || 'none',
733712
})
734713

735-
// Check if this tool call should be nested in a subagent
736-
// Only nest if the agent block actually exists
737-
const shouldNestInAgent = agentId && setMessages.length > 0
738-
let agentBlockExists = false
739-
740-
if (shouldNestInAgent) {
741-
setMessages((prev) => {
742-
const msg = prev.find((m) => m.id === aiMessageId)
743-
if (msg?.blocks) {
744-
agentBlockExists = findAgentBlock(msg.blocks, agentId)
745-
}
746-
return prev
747-
})
748-
}
749-
750-
if (agentBlockExists) {
714+
// If this tool call belongs to a subagent, add it to that agent's blocks
715+
if (agentId) {
751716
logger.info('setMessages: tool_call for subagent', {
752717
agentId,
753718
toolName,

0 commit comments

Comments
 (0)