( params: ParamsOf<FetchAgentFromDatabaseFn>, )
| 213 | } |
| 214 | |
| 215 | export async function fetchAgentFromDatabase( |
| 216 | params: ParamsOf<FetchAgentFromDatabaseFn>, |
| 217 | ): ReturnType<FetchAgentFromDatabaseFn> { |
| 218 | const { apiKey, parsedAgentId, logger } = params |
| 219 | const { publisherId, agentId, version } = parsedAgentId |
| 220 | |
| 221 | const url = new URL( |
| 222 | `/api/v1/agents/${publisherId}/${agentId}/${version ? version : 'latest'}`, |
| 223 | WEBSITE_URL, |
| 224 | ) |
| 225 | |
| 226 | try { |
| 227 | const response = await fetchWithRetry( |
| 228 | url, |
| 229 | { |
| 230 | method: 'GET', |
| 231 | headers: { |
| 232 | Authorization: `Bearer ${apiKey}`, |
| 233 | }, |
| 234 | }, |
| 235 | logger, |
| 236 | ) |
| 237 | |
| 238 | if (!response.ok) { |
| 239 | logger.error({ response }, 'fetchAgentFromDatabase request failed') |
| 240 | return null |
| 241 | } |
| 242 | |
| 243 | const responseJson = await response.json() |
| 244 | const parseResult = agentsResponseSchema.safeParse(responseJson) |
| 245 | if (!parseResult.success) { |
| 246 | logger.error( |
| 247 | { responseJson, parseResult }, |
| 248 | `fetchAgentFromDatabase parse error`, |
| 249 | ) |
| 250 | return null |
| 251 | } |
| 252 | |
| 253 | const agentConfig = parseResult.data |
| 254 | const rawAgentData = agentConfig.data as DynamicAgentTemplate |
| 255 | |
| 256 | // Validate the raw agent data with the original agentId (not full identifier) |
| 257 | const validationResult = validateSingleAgent({ |
| 258 | template: { ...rawAgentData, id: agentId, version: agentConfig.version }, |
| 259 | filePath: `${publisherId}/${agentId}@${agentConfig.version}`, |
| 260 | }) |
| 261 | |
| 262 | if (!validationResult.success) { |
| 263 | logger.error( |
| 264 | { |
| 265 | publisherId, |
| 266 | agentId, |
| 267 | version: agentConfig.version, |
| 268 | error: validationResult.error, |
| 269 | }, |
| 270 | 'fetchAgentFromDatabase: Agent validation failed', |
| 271 | ) |
| 272 | return null |
no test coverage detected