Skip to content

Commit 9f0b66d

Browse files
committed
Find a matching spawnable agent type when spawning
1 parent fa5b77d commit 9f0b66d

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

backend/src/templates/agent-registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const databaseAgentCache = new Map<string, AgentTemplate | null>()
2121
/**
2222
* Parse agent ID to extract publisher, agent name, and version
2323
*/
24-
function parseAgentId(fullAgentId: string): {
24+
export function parseAgentId(fullAgentId: string): {
2525
publisherId: string
2626
agentId: string
2727
version?: string

backend/src/tools/handlers/tool/spawn-agents.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { MAX_AGENT_STEPS_DEFAULT } from '@codebuff/common/constants/agents'
22
import { generateCompactId } from '@codebuff/common/util/string'
33

4-
import { getAgentTemplate } from '../../../templates/agent-registry'
4+
import {
5+
getAgentTemplate,
6+
parseAgentId,
7+
} from '../../../templates/agent-registry'
58
import { logger } from '../../../util/logger'
69

710
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
@@ -113,19 +116,22 @@ export const handleSpawnAgents = ((params: {
113116
}
114117
const results = await Promise.allSettled(
115118
agents.map(async ({ agent_type: agentTypeStr, prompt, params }) => {
116-
const agentType = agentTypeStr as AgentTemplateType
117119
const agentTemplate = await getAgentTemplate(
118-
agentType,
120+
agentTypeStr,
119121
localAgentTemplates,
120122
)
121123

122124
if (!agentTemplate) {
123125
throw new Error(`Agent type ${agentTypeStr} not found.`)
124126
}
125127

126-
if (!parentAgentTemplate.spawnableAgents.includes(agentType)) {
128+
const agentType = getMatchingSpawn(
129+
parentAgentTemplate.spawnableAgents,
130+
agentTypeStr,
131+
)
132+
if (!agentType) {
127133
throw new Error(
128-
`Agent type ${parentAgentTemplate.id} is not allowed to spawn child agent type ${agentType}.`,
134+
`Agent type ${parentAgentTemplate.id} is not allowed to spawn child agent type ${agentTypeStr}.`,
129135
)
130136
}
131137

@@ -277,3 +283,56 @@ export const handleSpawnAgents = ((params: {
277283
state: {},
278284
}
279285
}) satisfies CodebuffToolHandlerFunction<'spawn_agents'>
286+
287+
const getMatchingSpawn = (
288+
spawnableAgents: AgentTemplateType[],
289+
childFullAgentId: string,
290+
) => {
291+
const parsedChildAgentId = parseAgentId(childFullAgentId)
292+
if (!parsedChildAgentId) return null
293+
const {
294+
publisherId: childPublisherId,
295+
agentId: childAgentId,
296+
version: childVersion,
297+
} = parsedChildAgentId
298+
299+
for (const spawnableAgent of spawnableAgents) {
300+
const parsedSpawnableAgent = parseAgentId(spawnableAgent)
301+
if (!parsedSpawnableAgent) continue
302+
const {
303+
publisherId: spawnablePublisherId,
304+
agentId: spawnableAgentId,
305+
version: spawnableVersion,
306+
} = parsedSpawnableAgent
307+
if (
308+
spawnableAgentId === childAgentId &&
309+
spawnablePublisherId === childPublisherId &&
310+
spawnableVersion === childVersion
311+
) {
312+
return spawnableAgent
313+
}
314+
if (!childVersion && childPublisherId) {
315+
if (
316+
spawnablePublisherId === childPublisherId &&
317+
spawnableAgentId === childAgentId
318+
) {
319+
return spawnableAgent
320+
}
321+
}
322+
if (!childPublisherId && childVersion) {
323+
if (
324+
spawnableAgentId === childAgentId &&
325+
spawnableVersion === childVersion
326+
) {
327+
return spawnableAgent
328+
}
329+
}
330+
331+
if (!childVersion && !childPublisherId) {
332+
if (spawnableAgentId === childAgentId) {
333+
return spawnableAgent
334+
}
335+
}
336+
}
337+
return null
338+
}

0 commit comments

Comments
 (0)