(responseText: string, agentType: 'browser' | 'python')
| 82 | }; |
| 83 | |
| 84 | function parseAgentResponse(responseText: string, agentType: 'browser' | 'python'): { agent: CompleteAgent, code: string } | null { |
| 85 | try { |
| 86 | const agentBlockRegex = /\$\$\$\s*\n?([\s\S]*?)\n?\$\$\$/; |
| 87 | const agentBlockMatch = responseText.match(agentBlockRegex); |
| 88 | const relevantText = agentBlockMatch && agentBlockMatch[1] ? agentBlockMatch[1].trim() : responseText.trim(); |
| 89 | const codeMatch = relevantText.match(/code:\s*\|\s*\n([\s\S]*?)(?=\nmemory:)/); |
| 90 | const systemPromptMatch = relevantText.match(/system_prompt:\s*\|\s*\n([\s\S]*?)(?=\ncode:)/); |
| 91 | if (!codeMatch || !codeMatch[1] || !systemPromptMatch || !systemPromptMatch[1]) { |
| 92 | return null; |
| 93 | } |
| 94 | let codeSection = codeMatch[1]; |
| 95 | if (agentType === 'python') { |
| 96 | const lines = codeSection.split('\n'); |
| 97 | let minIndent = Infinity; |
| 98 | for (const line of lines) { |
| 99 | if (line.trim() !== '') { |
| 100 | const spaceMatch = line.match(/^\s*/); |
| 101 | const leadingSpaces = spaceMatch ? spaceMatch[0].length : 0; |
| 102 | minIndent = Math.min(minIndent, leadingSpaces); |
| 103 | } |
| 104 | } |
| 105 | if (minIndent < Infinity && minIndent > 0) { |
| 106 | codeSection = lines.map(line => line.trim() === '' ? '' : line.slice(minIndent)).join('\n'); |
| 107 | } |
| 108 | } |
| 109 | const getId = (field: string) => { |
| 110 | const match = relevantText.match(new RegExp(`${field}:\\s*([^\\n]+)`)); |
| 111 | return match && match[1] ? match[1].trim() : ''; |
| 112 | }; |
| 113 | const agent: CompleteAgent = { |
| 114 | id: getId('id'), |
| 115 | name: getId('name'), |
| 116 | description: getId('description'), |
| 117 | model_name: getId('model_name'), // This will be overridden by the selected model later |
| 118 | system_prompt: systemPromptMatch[1].trimEnd(), |
| 119 | loop_interval_seconds: parseFloat(getId('loop_interval_seconds')), |
| 120 | }; |
| 121 | return { agent, code: codeSection }; |
| 122 | } catch (error) { |
| 123 | console.error("Error parsing agent response:", error); |
| 124 | return null; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | interface GenerateAgentProps { |
no test coverage detected