( request: ProviderRequest, config: AnthropicProviderConfig )
| 185 | * and the Azure Anthropic provider. |
| 186 | */ |
| 187 | export async function executeAnthropicProviderRequest( |
| 188 | request: ProviderRequest, |
| 189 | config: AnthropicProviderConfig |
| 190 | ): Promise<ProviderResponse | StreamingExecution> { |
| 191 | const { logger, providerId, providerLabel } = config |
| 192 | |
| 193 | if (!request.apiKey) { |
| 194 | throw new Error(`API key is required for ${providerLabel}`) |
| 195 | } |
| 196 | |
| 197 | const modelId = request.model |
| 198 | const useNativeStructuredOutputs = !!( |
| 199 | request.responseFormat && supportsNativeStructuredOutputs(modelId) |
| 200 | ) |
| 201 | |
| 202 | const anthropic = config.createClient(request.apiKey, useNativeStructuredOutputs) |
| 203 | |
| 204 | const messages: Anthropic.Messages.MessageParam[] = [] |
| 205 | let systemPrompt = request.systemPrompt || '' |
| 206 | |
| 207 | if (request.context) { |
| 208 | messages.push({ |
| 209 | role: 'user', |
| 210 | content: request.context, |
| 211 | }) |
| 212 | } |
| 213 | |
| 214 | if (request.messages) { |
| 215 | request.messages.forEach((msg) => { |
| 216 | if (msg.role === 'function') { |
| 217 | messages.push({ |
| 218 | role: 'user', |
| 219 | content: [ |
| 220 | { |
| 221 | type: 'tool_result', |
| 222 | tool_use_id: msg.name || '', |
| 223 | content: msg.content || undefined, |
| 224 | }, |
| 225 | ], |
| 226 | }) |
| 227 | } else if (msg.function_call) { |
| 228 | const toolUseId = `${msg.function_call.name}-${Date.now()}` |
| 229 | messages.push({ |
| 230 | role: 'assistant', |
| 231 | content: [ |
| 232 | { |
| 233 | type: 'tool_use', |
| 234 | id: toolUseId, |
| 235 | name: msg.function_call.name, |
| 236 | input: JSON.parse(msg.function_call.arguments), |
| 237 | }, |
| 238 | ], |
| 239 | }) |
| 240 | } else { |
| 241 | const content = buildAnthropicMessageContent(msg.content, msg.files, config.providerId) |
| 242 | messages.push({ |
| 243 | role: msg.role === 'assistant' ? 'assistant' : 'user', |
| 244 | // double-cast-allowed: shared attachment builder returns Anthropic-compatible content blocks but avoids importing SDK-only union types |
no test coverage detected