| 272 | * OPTIMIZATION: Caches loaded agents to avoid re-reading from disk |
| 273 | */ |
| 274 | export async function buildAgentsOption( |
| 275 | agentNames: string[], |
| 276 | cwd?: string |
| 277 | ): Promise< |
| 278 | Record< |
| 279 | string, |
| 280 | { description: string; prompt: string; tools?: string[]; model?: AgentModel } |
| 281 | > |
| 282 | > { |
| 283 | if (agentNames.length === 0) return {} |
| 284 | |
| 285 | const agents: Record< |
| 286 | string, |
| 287 | { description: string; prompt: string; tools?: string[]; model?: AgentModel } |
| 288 | > = {} |
| 289 | |
| 290 | for (const name of agentNames) { |
| 291 | // Create cache key including cwd to handle project-specific agents |
| 292 | const cacheKey = cwd ? `${name}:${cwd}` : name |
| 293 | |
| 294 | // Check cache first |
| 295 | let agent = agentCache.get(cacheKey) |
| 296 | if (agent === undefined) { |
| 297 | // Not in cache, load from disk |
| 298 | console.log(`[agents] Cache MISS for ${name} - loading from disk`) |
| 299 | agent = await loadAgent(name, cwd) |
| 300 | agentCache.set(cacheKey, agent) |
| 301 | } else { |
| 302 | console.log(`[agents] Cache HIT for ${name}`) |
| 303 | } |
| 304 | |
| 305 | if (agent) { |
| 306 | agents[name] = { |
| 307 | description: agent.description, |
| 308 | prompt: agent.prompt, |
| 309 | ...(agent.tools && { tools: agent.tools }), |
| 310 | ...(agent.model && { model: agent.model }), |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return agents |
| 316 | } |