fix(chatopenai): honor proxy URL#6504
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for a proxy URL in the ChatOpenAI node by integrating ProxyAgent from undici. It adds a new proxyUrl input field, configures the OpenAI client fetch dispatcher with the proxy agent, and includes corresponding unit tests. The feedback suggests caching and reusing ProxyAgent instances using a module-level cache map to prevent socket leaks and enable connection pooling across multiple executions.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| import { ProxyAgent } from 'undici' | ||
|
|
There was a problem hiding this comment.
To prevent socket leaks and enable connection reuse (keep-alive) across multiple executions, we should cache and reuse ProxyAgent instances instead of creating a new one on every initialization. Let's declare a module-level cache map for the proxy agents.
import { ProxyAgent } from 'undici'
const proxyAgentCache = new Map<string, ProxyAgent>()| if (basePath || parsedBaseOptions || proxyUrl) { | ||
| obj.configuration = { | ||
| baseURL: basePath, | ||
| defaultHeaders: parsedBaseOptions | ||
| defaultHeaders: parsedBaseOptions, | ||
| ...(proxyUrl | ||
| ? { | ||
| fetchOptions: { | ||
| dispatcher: new ProxyAgent(proxyUrl) | ||
| } | ||
| } | ||
| : {}) | ||
| } | ||
| } |
There was a problem hiding this comment.
Retrieve the cached ProxyAgent instance for the given proxyUrl to benefit from connection pooling and avoid resource exhaustion.
if (basePath || parsedBaseOptions || proxyUrl) {
let dispatcher: ProxyAgent | undefined = undefined
if (proxyUrl) {
let agent = proxyAgentCache.get(proxyUrl)
if (!agent) {
agent = new ProxyAgent(proxyUrl)
proxyAgentCache.set(proxyUrl, agent)
}
dispatcher = agent
}
obj.configuration = {
baseURL: basePath,
defaultHeaders: parsedBaseOptions,
...(dispatcher
? {
fetchOptions: {
dispatcher
}
}
: {})
}
}18bc438 to
32fdc8b
Compare
|
Addressed the review suggestion by caching ProxyAgent instances per proxy URL and extending the regression test to verify repeated init calls reuse the dispatcher. Re-verified with the targeted ChatOpenAI test, eslint, prettier, flowise-components build, and git diff check. |
Summary
Fixes #5276
Tests
Note: full flowise-components lint currently fails on unrelated unused eslint-disable directives in existing files.