fix(proxy): Add error handling to proxy agent creation (#11538)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
This commit is contained in:
Shreya Keshive
2025-10-21 12:43:37 -07:00
committed by GitHub
parent 62dc9683bd
commit e72c00cf9a
3 changed files with 17 additions and 6 deletions
+2 -3
View File
@@ -76,9 +76,8 @@ import { MessageBus } from '../confirmation-bus/message-bus.js';
import { PolicyEngine } from '../policy/policy-engine.js'; import { PolicyEngine } from '../policy/policy-engine.js';
import type { PolicyEngineConfig } from '../policy/types.js'; import type { PolicyEngineConfig } from '../policy/types.js';
import type { UserTierId } from '../code_assist/types.js'; import type { UserTierId } from '../code_assist/types.js';
import { ProxyAgent, setGlobalDispatcher } from 'undici';
import { AgentRegistry } from '../agents/registry.js'; import { AgentRegistry } from '../agents/registry.js';
import { setGlobalProxy } from '../utils/fetch.js';
import { SubagentToolWrapper } from '../agents/subagent-tool-wrapper.js'; import { SubagentToolWrapper } from '../agents/subagent-tool-wrapper.js';
export enum ApprovalMode { export enum ApprovalMode {
@@ -506,7 +505,7 @@ export class Config {
} }
if (this.getProxy()) { if (this.getProxy()) {
setGlobalDispatcher(new ProxyAgent(this.getProxy() as string)); setGlobalProxy(this.getProxy() as string);
} }
this.geminiClient = new GeminiClient(this); this.geminiClient = new GeminiClient(this);
this.modelRouterService = new ModelRouterService(this); this.modelRouterService = new ModelRouterService(this);
+6 -3
View File
@@ -21,9 +21,12 @@ import { getErrorMessage } from '../utils/errors.js';
import type { Config } from '../config/config.js'; import type { Config } from '../config/config.js';
import { ApprovalMode, DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js'; import { ApprovalMode, DEFAULT_GEMINI_FLASH_MODEL } from '../config/config.js';
import { getResponseText } from '../utils/partUtils.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 { convert } from 'html-to-text';
import { ProxyAgent, setGlobalDispatcher } from 'undici';
import { import {
logWebFetchFallbackAttempt, logWebFetchFallbackAttempt,
WebFetchFallbackAttemptEvent, WebFetchFallbackAttemptEvent,
@@ -425,7 +428,7 @@ export class WebFetchTool extends BaseDeclarativeTool<
); );
const proxy = config.getProxy(); const proxy = config.getProxy();
if (proxy) { if (proxy) {
setGlobalDispatcher(new ProxyAgent(proxy as string)); setGlobalProxy(proxy);
} }
} }
+9
View File
@@ -6,6 +6,7 @@
import { getErrorMessage, isNodeError } from './errors.js'; import { getErrorMessage, isNodeError } from './errors.js';
import { URL } from 'node:url'; import { URL } from 'node:url';
import { ProxyAgent, setGlobalDispatcher } from 'undici';
const PRIVATE_IP_RANGES = [ const PRIVATE_IP_RANGES = [
/^10\./, /^10\./,
@@ -55,3 +56,11 @@ export async function fetchWithTimeout(
clearTimeout(timeoutId); clearTimeout(timeoutId);
} }
} }
export function setGlobalProxy(proxy: string) {
try {
setGlobalDispatcher(new ProxyAgent(proxy));
} catch (e) {
console.error(`Failed to set proxy: ${getErrorMessage(e)}`);
}
}