(configText: string)
| 123 | * @returns An object containing the agent and code, or null on failure. |
| 124 | */ |
| 125 | export function parseAgentResponse(configText: string): { agent: CompleteAgent, code: string } | null { |
| 126 | try { |
| 127 | const codeMatch = configText.match(/code:\s*\|\s*\n([\s\S]*?)(?=\nmemory:|$)/); |
| 128 | const systemPromptMatch = configText.match(/system_prompt:\s*\|\s*\n([\s\S]*?)(?=\ncode:)/); |
| 129 | |
| 130 | if (!codeMatch || !codeMatch[1] || !systemPromptMatch || !systemPromptMatch[1]) { |
| 131 | console.error("Parsing Error: Could not find system_prompt or code sections."); |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | const getField = (field: string): string => { |
| 136 | const match = configText.match(new RegExp(`^${field}:\\s*([^\\n]+)`, 'm')); |
| 137 | return match && match[1] ? match[1].trim() : ''; |
| 138 | }; |
| 139 | |
| 140 | const agent: CompleteAgent = { |
| 141 | id: getField('id') || `agent_${Date.now()}`, // Fallback to ensure an ID exists |
| 142 | name: getField('name') || 'Untitled Agent', |
| 143 | description: getField('description') || 'No description.', |
| 144 | model_name: getField('model_name'), |
| 145 | system_prompt: systemPromptMatch[1].trimEnd(), |
| 146 | loop_interval_seconds: parseFloat(getField('loop_interval_seconds')) || 60, |
| 147 | }; |
| 148 | |
| 149 | // Basic validation |
| 150 | if (!agent.model_name) { |
| 151 | console.error("Parsing Error: model_name is missing."); |
| 152 | return null; |
| 153 | } |
| 154 | |
| 155 | return { agent, code: codeMatch[1] }; |
| 156 | } catch (error) { |
| 157 | console.error("Fatal error parsing agent response:", error); |
| 158 | return null; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // =================================================================================== |
| 163 | // AGENT REFERENCE PARSER FUNCTIONS |
no test coverage detected