| 199 | |
| 200 | // Implementation |
| 201 | export async function loadLocalAgents({ |
| 202 | agentsPath, |
| 203 | verbose = false, |
| 204 | validate = false, |
| 205 | }: { |
| 206 | agentsPath?: string |
| 207 | verbose?: boolean |
| 208 | validate?: boolean |
| 209 | }): Promise<LoadedAgents | LoadLocalAgentsResult> { |
| 210 | const agents: LoadedAgents = {} |
| 211 | |
| 212 | const agentDirs = agentsPath ? [agentsPath] : getDefaultAgentDirs() |
| 213 | const allAgentFiles = agentDirs.flatMap((dir) => getAllAgentFiles(dir)) |
| 214 | |
| 215 | if (allAgentFiles.length === 0) { |
| 216 | return validate ? { agents, validationErrors: [] } : agents |
| 217 | } |
| 218 | |
| 219 | for (const fullPath of allAgentFiles) { |
| 220 | try { |
| 221 | const agentModule = await importAgentModule(fullPath) |
| 222 | if (!agentModule) { |
| 223 | continue |
| 224 | } |
| 225 | const agentDefinition = agentModule.default ?? agentModule |
| 226 | |
| 227 | if (!agentDefinition?.id || !agentDefinition?.model) { |
| 228 | if (verbose) { |
| 229 | console.error( |
| 230 | `Agent definition missing required attributes (id, model): ${fullPath}`, |
| 231 | ) |
| 232 | } |
| 233 | continue |
| 234 | } |
| 235 | |
| 236 | const processedAgentDefinition: LoadedAgentDefinition = { |
| 237 | ...agentDefinition, |
| 238 | _sourceFilePath: fullPath, |
| 239 | } |
| 240 | if (agentDefinition.handleSteps) { |
| 241 | processedAgentDefinition.handleSteps = |
| 242 | agentDefinition.handleSteps.toString() |
| 243 | } |
| 244 | |
| 245 | // Resolve $env references in MCP server configs |
| 246 | try { |
| 247 | resolveAgentMcpEnv(processedAgentDefinition) |
| 248 | } catch (error) { |
| 249 | if (verbose) { |
| 250 | console.error(error instanceof Error ? error.message : String(error)) |
| 251 | } |
| 252 | continue |
| 253 | } |
| 254 | |
| 255 | agents[processedAgentDefinition.id] = processedAgentDefinition |
| 256 | } catch (error) { |
| 257 | if (verbose) { |
| 258 | console.error( |