Skip to content

Commit 59eaafe

Browse files
Refactor agent loading and validation logic
- Modified npm-app to load local agents only when no specific agent is requested using Promise pattern - Improved agent validation in agent-registry by validating with original agentId first - Enhanced code formatting in websocket-action for better readability 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent a0ae426 commit 59eaafe

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

backend/src/templates/agent-registry.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,44 +100,45 @@ async function fetchAgentFromDatabase(parsedAgentId: {
100100

101101
const rawAgentData = agentConfig.data as DynamicAgentTemplate
102102

103-
// Ensure the agent has the full publisher/agent-id@version as its ID
104-
const agentDataWithId = {
105-
...rawAgentData,
106-
id: `${publisherId}/${agentId}@${agentConfig.version}`,
107-
}
108-
109-
// Use validateSingleAgent to convert to AgentTemplate type
110-
const validationResult = validateSingleAgent(agentDataWithId, {
111-
filePath: `${publisherId}/${agentId}@${agentConfig.version}`,
112-
skipSubagentValidation: true,
113-
})
103+
// Validate the raw agent data with the original agentId (not full identifier)
104+
const validationResult = validateSingleAgent(
105+
{ ...rawAgentData, id: agentId },
106+
{
107+
filePath: `${publisherId}/${agentId}@${agentConfig.version}`,
108+
skipSubagentValidation: true,
109+
},
110+
)
114111

115112
if (!validationResult.success) {
116113
logger.error(
117114
{
118115
publisherId,
119116
agentId,
120117
version: agentConfig.version,
121-
fullAgentId: agentDataWithId.id,
122118
error: validationResult.error,
123119
},
124120
'fetchAgentFromDatabase: Agent validation failed',
125121
)
126122
return null
127123
}
128124

125+
// Set the correct full agent ID for the final template
126+
const agentTemplate = {
127+
...validationResult.agentTemplate!,
128+
id: `${publisherId}/${agentId}@${agentConfig.version}`,
129+
}
130+
129131
logger.debug(
130132
{
131133
publisherId,
132134
agentId,
133135
version: agentConfig.version,
134-
fullAgentId: agentDataWithId.id,
135-
agentConfig,
136+
fullAgentId: agentTemplate.id,
136137
},
137138
'fetchAgentFromDatabase: Successfully loaded and validated agent from database',
138139
)
139140

140-
return validationResult.agentTemplate!
141+
return agentTemplate
141142
} catch (error) {
142143
logger.error(
143144
{ publisherId, agentId, version, error },

backend/src/websockets/websocket-action.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ export const callMainPrompt = async (
234234
const { fileContext } = action.sessionState
235235

236236
// Assemble local agent templates from fileContext
237-
const { agentTemplates: localAgentTemplates } = assembleLocalAgentTemplates(fileContext)
237+
const { agentTemplates: localAgentTemplates } =
238+
assembleLocalAgentTemplates(fileContext)
238239

239240
const result = await mainPrompt(ws, action, {
240241
userId,

npm-app/src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,23 @@ async function codebuff({
5353

5454
const initFileContextPromise = initProjectFileContextWithWorker(projectRoot)
5555

56+
// Only load local agents if no specific agent is requested
57+
const loadLocalAgentsPromise = new Promise<void>((resolve) => {
58+
if (!agent) {
59+
loadLocalAgents({ verbose: true }).then(() => {
60+
const codebuffConfig = loadCodebuffConfig()
61+
displayLoadedAgents(codebuffConfig)
62+
})
63+
}
64+
resolve()
65+
})
66+
5667
const readyPromise = Promise.all([
5768
initFileContextPromise,
5869
processCleanupPromise,
59-
60-
loadLocalAgents({ verbose: true }).then(() =>
61-
displayLoadedAgents(codebuffConfig),
62-
),
70+
loadLocalAgentsPromise,
6371
])
6472

65-
const codebuffConfig = loadCodebuffConfig()
66-
6773
// Initialize the CLI singleton
6874
CLI.initialize(readyPromise, {
6975
git,
@@ -74,6 +80,7 @@ async function codebuff({
7480
print,
7581
trace,
7682
})
83+
7784
const cli = CLI.getInstance()
7885

7986
await cli.printInitialPrompt({ initialInput, runInitFlow })

0 commit comments

Comments
 (0)