| 359 | * @returns Formatted agent context string |
| 360 | */ |
| 361 | export function formatAgentReferenceContext( |
| 362 | referenceData: AgentReferenceData[] |
| 363 | ): string { |
| 364 | if (referenceData.length === 0) return ''; |
| 365 | |
| 366 | let context = "\n\n=== REFERENCED AGENTS ===\n"; |
| 367 | context += "The user has referenced the following agents. Here are their current configurations:\n\n"; |
| 368 | |
| 369 | referenceData.forEach(data => { |
| 370 | const { reference, agent, code, memory, recentRuns } = data; |
| 371 | |
| 372 | if (!agent) { |
| 373 | context += `@${reference.agentId} - AGENT NOT FOUND\n`; |
| 374 | context += `The user referenced @${reference.agentId} but this agent doesn't exist.\n\n`; |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // Format in official Observer agent file format |
| 379 | context += `$$$\n`; |
| 380 | context += `id: ${agent.id}\n`; |
| 381 | context += `name: ${agent.name}\n`; |
| 382 | context += `description: ${agent.description}\n`; |
| 383 | context += `model_name: ${agent.model_name}\n`; |
| 384 | context += `loop_interval_seconds: ${agent.loop_interval_seconds}\n`; |
| 385 | context += `system_prompt: |\n`; |
| 386 | |
| 387 | // Indent system prompt lines |
| 388 | const systemPromptLines = agent.system_prompt.split('\n'); |
| 389 | systemPromptLines.forEach(line => { |
| 390 | context += ` ${line}\n`; |
| 391 | }); |
| 392 | |
| 393 | context += `code: |\n`; |
| 394 | |
| 395 | // Indent code lines if available |
| 396 | if (code) { |
| 397 | const codeLines = code.split('\n'); |
| 398 | codeLines.forEach(line => { |
| 399 | context += ` ${line}\n`; |
| 400 | }); |
| 401 | } |
| 402 | |
| 403 | // Add memory (truncate if too long) |
| 404 | if (memory.trim()) { |
| 405 | const memoryContent = memory.length > 500 ? memory.substring(0, 500) + '...' : memory; |
| 406 | context += `memory: |\n`; |
| 407 | const memoryLines = memoryContent.split('\n'); |
| 408 | memoryLines.forEach(line => { |
| 409 | context += ` ${line}\n`; |
| 410 | }); |
| 411 | } else { |
| 412 | context += `memory: ""\n`; |
| 413 | } |
| 414 | |
| 415 | context += `$$$\n\n`; |
| 416 | |
| 417 | // Add recent performance data after the agent config |
| 418 | if (recentRuns.length > 0) { |