| 379 | * @returns Formatted agent context string |
| 380 | */ |
| 381 | export function formatAgentReferenceContext( |
| 382 | referenceData: AgentReferenceData[] |
| 383 | ): string { |
| 384 | if (referenceData.length === 0) return ''; |
| 385 | |
| 386 | let context = "\n\n=== REFERENCED AGENTS ===\n"; |
| 387 | context += "The user has referenced the following agents. Here are their current configurations:\n\n"; |
| 388 | |
| 389 | referenceData.forEach(data => { |
| 390 | const { reference, agent, code, memory, recentRuns } = data; |
| 391 | |
| 392 | if (!agent) { |
| 393 | context += `@${reference.agentId} - AGENT NOT FOUND\n`; |
| 394 | context += `The user referenced @${reference.agentId} but this agent doesn't exist.\n\n`; |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | // Format in official Observer agent file format |
| 399 | context += `$$$\n`; |
| 400 | context += `id: ${agent.id}\n`; |
| 401 | context += `name: ${agent.name}\n`; |
| 402 | context += `description: ${agent.description}\n`; |
| 403 | context += `model_name: ${agent.model_name}\n`; |
| 404 | context += `loop_interval_seconds: ${agent.loop_interval_seconds}\n`; |
| 405 | context += `system_prompt: |\n`; |
| 406 | |
| 407 | // Indent system prompt lines |
| 408 | const systemPromptLines = agent.system_prompt.split('\n'); |
| 409 | systemPromptLines.forEach(line => { |
| 410 | context += ` ${line}\n`; |
| 411 | }); |
| 412 | |
| 413 | context += `code: |\n`; |
| 414 | |
| 415 | // Indent code lines if available |
| 416 | if (code) { |
| 417 | const codeLines = code.split('\n'); |
| 418 | codeLines.forEach(line => { |
| 419 | context += ` ${line}\n`; |
| 420 | }); |
| 421 | } |
| 422 | |
| 423 | // Add memory (truncate if too long) |
| 424 | if (memory.trim()) { |
| 425 | const memoryContent = memory.length > 500 ? memory.substring(0, 500) + '...' : memory; |
| 426 | context += `memory: |\n`; |
| 427 | const memoryLines = memoryContent.split('\n'); |
| 428 | memoryLines.forEach(line => { |
| 429 | context += ` ${line}\n`; |
| 430 | }); |
| 431 | } else { |
| 432 | context += `memory: ""\n`; |
| 433 | } |
| 434 | |
| 435 | context += `$$$\n\n`; |
| 436 | |
| 437 | // Add recent performance data after the agent config |
| 438 | if (recentRuns.length > 0) { |