( userPrompt: string, model: ModelName, existingIdentifiers: string[], abortSignal: AbortSignal, )
| 120 | ` |
| 121 | |
| 122 | export async function generateAgent( |
| 123 | userPrompt: string, |
| 124 | model: ModelName, |
| 125 | existingIdentifiers: string[], |
| 126 | abortSignal: AbortSignal, |
| 127 | ): Promise<GeneratedAgent> { |
| 128 | const existingList = |
| 129 | existingIdentifiers.length > 0 |
| 130 | ? `\n\nIMPORTANT: The following identifiers already exist and must NOT be used: ${existingIdentifiers.join(', ')}` |
| 131 | : '' |
| 132 | |
| 133 | const prompt = `Create an agent configuration based on this request: "${userPrompt}".${existingList} |
| 134 | Return ONLY the JSON object, no other text.` |
| 135 | |
| 136 | const userMessage = createUserMessage({ content: prompt }) |
| 137 | |
| 138 | // Fetch user and system contexts |
| 139 | const userContext = await getUserContext() |
| 140 | |
| 141 | // Prepend user context to messages and append system context to system prompt |
| 142 | const messagesWithContext = prependUserContext([userMessage], userContext) |
| 143 | |
| 144 | // Include memory instructions when the feature is enabled |
| 145 | const systemPrompt = isAutoMemoryEnabled() |
| 146 | ? AGENT_CREATION_SYSTEM_PROMPT + AGENT_MEMORY_INSTRUCTIONS |
| 147 | : AGENT_CREATION_SYSTEM_PROMPT |
| 148 | |
| 149 | const response = await queryModelWithoutStreaming({ |
| 150 | messages: normalizeMessagesForAPI(messagesWithContext), |
| 151 | systemPrompt: asSystemPrompt([systemPrompt]), |
| 152 | thinkingConfig: { type: 'disabled' as const }, |
| 153 | tools: [], |
| 154 | signal: abortSignal, |
| 155 | options: { |
| 156 | getToolPermissionContext: async () => getEmptyToolPermissionContext(), |
| 157 | model, |
| 158 | toolChoice: undefined, |
| 159 | agents: [], |
| 160 | isNonInteractiveSession: false, |
| 161 | hasAppendSystemPrompt: false, |
| 162 | querySource: 'agent_creation', |
| 163 | mcpTools: [], |
| 164 | }, |
| 165 | }) |
| 166 | |
| 167 | const textBlocks = response.message.content.filter( |
| 168 | (block): block is ContentBlock & { type: 'text' } => block.type === 'text', |
| 169 | ) |
| 170 | const responseText = textBlocks.map(block => block.text).join('\n') |
| 171 | |
| 172 | let parsed: GeneratedAgent |
| 173 | try { |
| 174 | parsed = jsonParse(responseText.trim()) |
| 175 | } catch { |
| 176 | const jsonMatch = responseText.match(/\{[\s\S]*\}/) |
| 177 | if (!jsonMatch) { |
| 178 | throw new Error('No JSON object found in response') |
| 179 | } |
no test coverage detected