(
env: Env = process.env,
factories: Partial<ProxyAgentFactories> = {},
)
| 261 | * dispatcher untouched. |
| 262 | */ |
| 263 | export function createProxyDispatcher( |
| 264 | env: Env = process.env, |
| 265 | factories: Partial<ProxyAgentFactories> = {}, |
| 266 | ): Dispatcher | undefined { |
| 267 | const { makeHttpAgent = defaultMakeHttpAgent, makeSocksAgent = defaultMakeSocksAgent } = factories; |
| 268 | try { |
| 269 | if (hasHttpProxy(env)) { |
| 270 | // Coerce a missing value to '' (falsy to undici) so EnvHttpProxyAgent |
| 271 | // neither builds a broken agent from a socks: URI nor re-reads a |
| 272 | // blank-masked value from env. |
| 273 | const { httpProxy, httpsProxy } = resolveHttpProxyUrls(env); |
| 274 | return makeHttpAgent({ |
| 275 | httpProxy: httpProxy ?? '', |
| 276 | httpsProxy: httpsProxy ?? '', |
| 277 | noProxy: resolveNoProxy(env), |
| 278 | }); |
| 279 | } |
| 280 | const socks = resolveSocksProxy(env); |
| 281 | if (socks !== undefined) { |
| 282 | return makeSocksAgent({ proxy: socks, noProxy: resolveNoProxy(env) }); |
| 283 | } |
| 284 | return undefined; |
| 285 | } catch (error) { |
| 286 | // A malformed proxy URL makes agent construction throw synchronously. Don't |
| 287 | // abort startup with a raw stack trace — report it and fall back to direct. |
| 288 | const reason = error instanceof Error ? error.message : String(error); |
| 289 | process.stderr.write(`kimi: ignoring invalid proxy configuration (${reason}); connecting directly\n`); |
| 290 | return undefined; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | export interface InstallProxyDeps { |
| 295 | readonly setGlobalDispatcher: (dispatcher: Dispatcher) => void; |
no test coverage detected