1+ import { validateSingleAgent } from '@codebuff/common/templates/agent-validation'
12import { userColumns } from '@codebuff/common/types/contracts/database'
3+ import { DynamicAgentTemplateSchema } from '@codebuff/common/types/dynamic-agent-template'
24import { getErrorObject } from '@codebuff/common/util/error'
5+ import z from 'zod/v4'
36
47import { WEBSITE_URL } from '../constants'
58
@@ -12,13 +15,19 @@ import type {
1215 StartAgentRunFn ,
1316 UserColumn ,
1417} from '@codebuff/common/types/contracts/database'
18+ import type { DynamicAgentTemplate } from '@codebuff/common/types/dynamic-agent-template'
1519import type { ParamsOf } from '@codebuff/common/types/function-params'
1620
1721const userInfoCache : Record <
1822 string ,
1923 Awaited < GetUserInfoFromApiKeyOutput < UserColumn > >
2024> = { }
2125
26+ const agentsResponseSchema = z . object ( {
27+ version : z . string ( ) ,
28+ data : DynamicAgentTemplateSchema ,
29+ } )
30+
2231export async function getUserInfoFromApiKey < T extends UserColumn > (
2332 params : GetUserInfoFromApiKeyInput < T > ,
2433) : GetUserInfoFromApiKeyOutput < T > {
@@ -100,7 +109,57 @@ export async function fetchAgentFromDatabase(
100109 logger . error ( { response } , 'fetchAgentFromDatabase request failed' )
101110 return null
102111 }
103- return response . json ( )
112+
113+ const responseJson = await response . json ( )
114+ const parseResult = agentsResponseSchema . safeParse ( responseJson )
115+ if ( ! parseResult . success ) {
116+ logger . error (
117+ { responseJson, parseResult } ,
118+ `fetchAgentFromDatabase parse error` ,
119+ )
120+ return null
121+ }
122+
123+ const agentConfig = parseResult . data
124+ const rawAgentData = agentConfig . data as DynamicAgentTemplate
125+
126+ // Validate the raw agent data with the original agentId (not full identifier)
127+ const validationResult = validateSingleAgent ( {
128+ template : { ...rawAgentData , id : agentId , version : agentConfig . version } ,
129+ filePath : `${ publisherId } /${ agentId } @${ agentConfig . version } ` ,
130+ } )
131+
132+ if ( ! validationResult . success ) {
133+ logger . error (
134+ {
135+ publisherId,
136+ agentId,
137+ version : agentConfig . version ,
138+ error : validationResult . error ,
139+ } ,
140+ 'fetchAgentFromDatabase: Agent validation failed' ,
141+ )
142+ return null
143+ }
144+
145+ // Set the correct full agent ID for the final template
146+ const agentTemplate = {
147+ ...validationResult . agentTemplate ! ,
148+ id : `${ publisherId } /${ agentId } @${ agentConfig . version } ` ,
149+ }
150+
151+ logger . debug (
152+ {
153+ publisherId,
154+ agentId,
155+ version : agentConfig . version ,
156+ fullAgentId : agentTemplate . id ,
157+ parsedAgentId,
158+ } ,
159+ 'fetchAgentFromDatabase: Successfully loaded and validated agent from database' ,
160+ )
161+
162+ return agentTemplate
104163 } catch ( error ) {
105164 logger . error (
106165 { error : getErrorObject ( error ) , parsedAgentId } ,
0 commit comments