Skip to content

Commit 998b585

Browse files
committed
Fix tests, extract utils for parsing agent ids
1 parent 9f0b66d commit 998b585

File tree

3 files changed

+93
-38
lines changed

3 files changed

+93
-38
lines changed

backend/src/templates/agent-registry.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,13 @@ import {
1212
} from '@codebuff/common/templates/agent-validation'
1313
import { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
1414
import { DEFAULT_ORG_PREFIX } from '@codebuff/common/util/agent-name-normalization'
15+
import { parsePublishedAgentId } from '@codebuff/common/util/agent-id-parsing'
1516

1617
export type AgentRegistry = Record<string, AgentTemplate>
1718

1819
// Global database cache - only state in the system
1920
const databaseAgentCache = new Map<string, AgentTemplate | null>()
2021

21-
/**
22-
* Parse agent ID to extract publisher, agent name, and version
23-
*/
24-
export function parseAgentId(fullAgentId: string): {
25-
publisherId: string
26-
agentId: string
27-
version?: string
28-
} | null {
29-
// Check if it's in the publisher/agent-id[@version] format
30-
const parts = fullAgentId.split('/')
31-
if (parts.length !== 2) {
32-
return null
33-
}
34-
35-
const [publisherId, agentNameWithVersion] = parts
36-
37-
// Check for version suffix
38-
const versionMatch = agentNameWithVersion.match(/^(.+)@(.+)$/)
39-
if (versionMatch) {
40-
const [, agentId, version] = versionMatch
41-
return { publisherId, agentId, version }
42-
}
43-
44-
return { publisherId, agentId: agentNameWithVersion }
45-
}
46-
4722
/**
4823
* Fetch an agent from the database by publisher/agent-id[@version] format
4924
*/
@@ -168,10 +143,12 @@ export async function getAgentTemplate(
168143
return databaseAgentCache.get(cacheKey) || null
169144
}
170145

171-
const parsed = parseAgentId(agentId)
146+
const parsed = parsePublishedAgentId(agentId)
172147
if (!parsed) {
173148
// If agentId doesn't parse as publisher/agent format, try as codebuff/agentId
174-
const codebuffParsed = parseAgentId(`${DEFAULT_ORG_PREFIX}${agentId}`)
149+
const codebuffParsed = parsePublishedAgentId(
150+
`${DEFAULT_ORG_PREFIX}${agentId}`,
151+
)
175152
if (codebuffParsed) {
176153
const dbAgent = await fetchAgentFromDatabase(codebuffParsed)
177154
if (dbAgent) {

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { MAX_AGENT_STEPS_DEFAULT } from '@codebuff/common/constants/agents'
22
import { generateCompactId } from '@codebuff/common/util/string'
3+
import { parseAgentId } from '@codebuff/common/util/agent-id-parsing'
34

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

108
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
@@ -288,22 +286,27 @@ const getMatchingSpawn = (
288286
spawnableAgents: AgentTemplateType[],
289287
childFullAgentId: string,
290288
) => {
291-
const parsedChildAgentId = parseAgentId(childFullAgentId)
292-
if (!parsedChildAgentId) return null
293289
const {
294290
publisherId: childPublisherId,
295291
agentId: childAgentId,
296292
version: childVersion,
297-
} = parsedChildAgentId
293+
} = parseAgentId(childFullAgentId)
294+
295+
if (!childAgentId) {
296+
return null
297+
}
298298

299299
for (const spawnableAgent of spawnableAgents) {
300-
const parsedSpawnableAgent = parseAgentId(spawnableAgent)
301-
if (!parsedSpawnableAgent) continue
302300
const {
303301
publisherId: spawnablePublisherId,
304302
agentId: spawnableAgentId,
305303
version: spawnableVersion,
306-
} = parsedSpawnableAgent
304+
} = parseAgentId(spawnableAgent)
305+
306+
if (!spawnableAgentId) {
307+
continue
308+
}
309+
307310
if (
308311
spawnableAgentId === childAgentId &&
309312
spawnablePublisherId === childPublisherId &&
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Parse agent ID to extract publisher, agent name, and version
3+
* Supports formats:
4+
* - publisher/agentId[@version]
5+
* - agentId[@version] (no publisher)
6+
*/
7+
export function parseAgentId(fullAgentId: string): {
8+
publisherId?: string
9+
agentId?: string
10+
version?: string
11+
} {
12+
// Check if it's in the publisher/agent-id[@version] format
13+
const parts = fullAgentId.split('/')
14+
15+
if (parts.length === 2) {
16+
// Full format: publisher/agentId[@version]
17+
const [publisherId, agentNameWithVersion] = parts
18+
19+
if (!publisherId || !agentNameWithVersion) {
20+
return { publisherId: undefined, agentId: undefined, version: undefined }
21+
}
22+
23+
// Check for version suffix
24+
const versionMatch = agentNameWithVersion.match(/^(.+)@(.+)$/)
25+
if (versionMatch) {
26+
const [, agentId, version] = versionMatch
27+
return { publisherId, agentId, version }
28+
}
29+
30+
return { publisherId, agentId: agentNameWithVersion }
31+
} else if (parts.length === 1) {
32+
// Just agent name (for backward compatibility)
33+
const agentNameWithVersion = parts[0]
34+
35+
if (!agentNameWithVersion) {
36+
return { publisherId: undefined, agentId: undefined, version: undefined }
37+
}
38+
39+
// Check for version suffix
40+
const versionMatch = agentNameWithVersion.match(/^(.+)@(.+)$/)
41+
if (versionMatch) {
42+
const [, agentId, version] = versionMatch
43+
return { publisherId: undefined, agentId, version }
44+
}
45+
46+
return {
47+
publisherId: undefined,
48+
agentId: agentNameWithVersion,
49+
version: undefined,
50+
}
51+
}
52+
53+
return { publisherId: undefined, agentId: undefined, version: undefined }
54+
}
55+
56+
/**
57+
* Parse publishded agent ID to extract publisher, agent name, and optionally version
58+
*
59+
* If the agent ID is not in the publisher/agent format, return null
60+
*/
61+
export function parsePublishedAgentId(fullAgentId: string): {
62+
publisherId: string
63+
agentId: string
64+
version?: string
65+
} | null {
66+
const { publisherId, agentId, version } = parseAgentId(fullAgentId)
67+
if (!publisherId || !agentId) {
68+
return null
69+
}
70+
return {
71+
publisherId,
72+
agentId,
73+
version,
74+
}
75+
}

0 commit comments

Comments
 (0)