|
1 | 1 | import { MAX_AGENT_STEPS_DEFAULT } from '@codebuff/common/constants/agents' |
2 | 2 | import { generateCompactId } from '@codebuff/common/util/string' |
3 | 3 |
|
4 | | -import { getAgentTemplate } from '../../../templates/agent-registry' |
| 4 | +import { |
| 5 | + getAgentTemplate, |
| 6 | + parseAgentId, |
| 7 | +} from '../../../templates/agent-registry' |
5 | 8 | import { logger } from '../../../util/logger' |
6 | 9 |
|
7 | 10 | import type { CodebuffToolHandlerFunction } from '../handler-function-type' |
@@ -113,19 +116,22 @@ export const handleSpawnAgents = ((params: { |
113 | 116 | } |
114 | 117 | const results = await Promise.allSettled( |
115 | 118 | agents.map(async ({ agent_type: agentTypeStr, prompt, params }) => { |
116 | | - const agentType = agentTypeStr as AgentTemplateType |
117 | 119 | const agentTemplate = await getAgentTemplate( |
118 | | - agentType, |
| 120 | + agentTypeStr, |
119 | 121 | localAgentTemplates, |
120 | 122 | ) |
121 | 123 |
|
122 | 124 | if (!agentTemplate) { |
123 | 125 | throw new Error(`Agent type ${agentTypeStr} not found.`) |
124 | 126 | } |
125 | 127 |
|
126 | | - if (!parentAgentTemplate.spawnableAgents.includes(agentType)) { |
| 128 | + const agentType = getMatchingSpawn( |
| 129 | + parentAgentTemplate.spawnableAgents, |
| 130 | + agentTypeStr, |
| 131 | + ) |
| 132 | + if (!agentType) { |
127 | 133 | 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}.`, |
129 | 135 | ) |
130 | 136 | } |
131 | 137 |
|
@@ -277,3 +283,56 @@ export const handleSpawnAgents = ((params: { |
277 | 283 | state: {}, |
278 | 284 | } |
279 | 285 | }) 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