From f6b1ce2e3a2261e17a0a6653325725aa08f09741 Mon Sep 17 00:00:00 2001 From: anup-openai <148260946+anup-openai@users.noreply.github.com> Date: Fri, 2 May 2025 12:08:13 -0700 Subject: [PATCH] Configure HTTPS agent for proxies (#775) - Some workflows require you to route openAI API traffic through a proxy - See https://github.com/openai/openai-node/tree/v4?tab=readme-ov-file#configuring-an-https-agent-eg-for-proxies for more details --------- Co-authored-by: Thibault Sottiaux Co-authored-by: Fouad Matin --- codex-cli/package.json | 1 + codex-cli/src/utils/agent/agent-loop.ts | 5 +++++ pnpm-lock.yaml | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/codex-cli/package.json b/codex-cli/package.json index 31678626..e2454582 100644 --- a/codex-cli/package.json +++ b/codex-cli/package.json @@ -34,6 +34,7 @@ "fast-npm-meta": "^0.4.2", "figures": "^6.1.0", "file-type": "^20.1.0", + "https-proxy-agent": "^7.0.6", "ink": "^5.2.0", "js-yaml": "^4.1.0", "marked": "^15.0.7", diff --git a/codex-cli/src/utils/agent/agent-loop.ts b/codex-cli/src/utils/agent/agent-loop.ts index 20a12e7c..85d1d3e7 100644 --- a/codex-cli/src/utils/agent/agent-loop.ts +++ b/codex-cli/src/utils/agent/agent-loop.ts @@ -29,6 +29,7 @@ import { setSessionId, } from "../session.js"; import { handleExecCommand } from "./handle-exec-command.js"; +import { HttpsProxyAgent } from "https-proxy-agent"; import { randomUUID } from "node:crypto"; import OpenAI, { APIConnectionTimeoutError } from "openai"; @@ -38,6 +39,9 @@ const RATE_LIMIT_RETRY_WAIT_MS = parseInt( 10, ); +// See https://github.com/openai/openai-node/tree/v4?tab=readme-ov-file#configuring-an-https-agent-eg-for-proxies +const PROXY_URL = process.env["HTTPS_PROXY"]; + export type CommandConfirmation = { review: ReviewDecision; applyPatch?: ApplyPatchCommand | undefined; @@ -314,6 +318,7 @@ export class AgentLoop { : {}), ...(OPENAI_PROJECT ? { "OpenAI-Project": OPENAI_PROJECT } : {}), }, + httpAgent: PROXY_URL ? new HttpsProxyAgent(PROXY_URL) : undefined, ...(timeoutMs !== undefined ? { timeout: timeoutMs } : {}), }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 06ce3f6c..a0efcba7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -52,6 +52,9 @@ importers: file-type: specifier: ^20.1.0 version: 20.4.1 + https-proxy-agent: + specifier: ^7.0.6 + version: 7.0.6 ink: specifier: ^5.2.0 version: 5.2.0(@types/react@18.3.20)(react@18.3.1) @@ -680,6 +683,10 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + agent-base@7.1.3: + resolution: {integrity: sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==} + engines: {node: '>= 14'} + agentkeepalive@4.6.0: resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==} engines: {node: '>= 8.0.0'} @@ -1383,6 +1390,10 @@ packages: highlight.js@10.7.3: resolution: {integrity: sha512-tzcUFauisWKNHaRkN4Wjl/ZA07gENAjFl3J/c480dprkGTg5EQstgaNFqBfUqCq54kZRIEcreTsAgF/m2quD7A==} + https-proxy-agent@7.0.6: + resolution: {integrity: sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==} + engines: {node: '>= 14'} + human-signals@5.0.0: resolution: {integrity: sha512-AXcZb6vzzrFAUE61HnN4mpLqd/cSIwNQjtNWR0euPm6y0iqx3G4gOXaIDdtdDwZmhwe82LA6+zinmW4UBWVePQ==} engines: {node: '>=16.17.0'} @@ -2920,6 +2931,8 @@ snapshots: acorn@8.14.1: {} + agent-base@7.1.3: {} + agentkeepalive@4.6.0: dependencies: humanize-ms: 1.2.1 @@ -3784,6 +3797,13 @@ snapshots: highlight.js@10.7.3: {} + https-proxy-agent@7.0.6: + dependencies: + agent-base: 7.1.3 + debug: 4.4.0 + transitivePeerDependencies: + - supports-color + human-signals@5.0.0: {} humanize-ms@1.2.1: