(opts?: { forAnthropicAPI?: boolean })
| 286 | * should pass `true` here. |
| 287 | */ |
| 288 | export function getProxyFetchOptions(opts?: { forAnthropicAPI?: boolean }): { |
| 289 | tls?: TLSConfig |
| 290 | dispatcher?: undici.Dispatcher |
| 291 | proxy?: string |
| 292 | unix?: string |
| 293 | keepalive?: false |
| 294 | } { |
| 295 | const base = keepAliveDisabled ? ({ keepalive: false } as const) : {} |
| 296 | |
| 297 | // ANTHROPIC_UNIX_SOCKET tunnels through the `claude ssh` auth proxy, which |
| 298 | // hardcodes the upstream to the Anthropic API. Scope to the Anthropic API |
| 299 | // client so MCP/SSE/other callers don't get their requests misrouted. |
| 300 | if (opts?.forAnthropicAPI) { |
| 301 | const unixSocket = process.env.ANTHROPIC_UNIX_SOCKET |
| 302 | if (unixSocket && typeof Bun !== 'undefined') { |
| 303 | return { ...base, unix: unixSocket } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | const proxyUrl = getProxyUrl() |
| 308 | |
| 309 | // If we have a proxy, use the proxy agent (which includes mTLS config) |
| 310 | if (proxyUrl) { |
| 311 | if (typeof Bun !== 'undefined') { |
| 312 | return { ...base, proxy: proxyUrl, ...getTLSFetchOptions() } |
| 313 | } |
| 314 | return { ...base, dispatcher: getProxyAgent(proxyUrl) } |
| 315 | } |
| 316 | |
| 317 | // Otherwise, use TLS options directly if available |
| 318 | return { ...base, ...getTLSFetchOptions() } |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Configure global HTTP agents for both axios and undici |
no test coverage detected