diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 42c5008fb2..93b4b0214f 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -76,9 +76,8 @@ import { MessageBus } from '../confirmation-bus/message-bus.js'; import { PolicyEngine } from '../policy/policy-engine.js'; import type { PolicyEngineConfig } from '../policy/types.js'; import type { UserTierId } from '../code_assist/types.js'; -import { ProxyAgent, setGlobalDispatcher } from 'undici'; - import { AgentRegistry } from '../agents/registry.js'; +import { setGlobalProxy } from '../utils/fetch.js'; import { SubagentToolWrapper } from '../agents/subagent-tool-wrapper.js'; export enum ApprovalMode { @@ -506,7 +505,7 @@ export class Config { } if (this.getProxy()) { - setGlobalDispatcher(new ProxyAgent(this.getProxy() as string)); + setGlobalProxy(this.getProxy() as string); } this.geminiClient = new GeminiClient(this); this.modelRouterService = new ModelRouterService(this); diff --git a/packages/core/src/tools/web-fetch.ts b/packages/core/src/tools/web-fetch.ts index 5e1835a13c..2cc2575472 100644 --- a/packages/core/src/tools/web-fetch.ts +++ b/packages/core/src/tools/web-fetch.ts @@ -21,9 +21,12 @@ import { getErrorMessage } from '../utils/errors.js'; import type { Config } from '../config/config.js'; import { ApprovalMode, DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js'; import { getResponseText } from '../utils/partUtils.js'; -import { fetchWithTimeout, isPrivateIp } from '../utils/fetch.js'; +import { + fetchWithTimeout, + isPrivateIp, + setGlobalProxy, +} from '../utils/fetch.js'; import { convert } from 'html-to-text'; -import { ProxyAgent, setGlobalDispatcher } from 'undici'; import { logWebFetchFallbackAttempt, WebFetchFallbackAttemptEvent, @@ -425,7 +428,7 @@ export class WebFetchTool extends BaseDeclarativeTool< ); const proxy = config.getProxy(); if (proxy) { - setGlobalDispatcher(new ProxyAgent(proxy as string)); + setGlobalProxy(proxy); } } diff --git a/packages/core/src/utils/fetch.ts b/packages/core/src/utils/fetch.ts index dfbcca18f0..ffe6165507 100644 --- a/packages/core/src/utils/fetch.ts +++ b/packages/core/src/utils/fetch.ts @@ -6,6 +6,7 @@ import { getErrorMessage, isNodeError } from './errors.js'; import { URL } from 'node:url'; +import { ProxyAgent, setGlobalDispatcher } from 'undici'; const PRIVATE_IP_RANGES = [ /^10\./, @@ -55,3 +56,11 @@ export async function fetchWithTimeout( clearTimeout(timeoutId); } } + +export function setGlobalProxy(proxy: string) { + try { + setGlobalDispatcher(new ProxyAgent(proxy)); + } catch (e) { + console.error(`Failed to set proxy: ${getErrorMessage(e)}`); + } +}