Skip to content
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
jest.mock('@langchain/openai', () => ({
ChatOpenAI: jest.fn(),
ChatOpenAIFields: {}
}))

jest.mock('undici', () => ({
ProxyAgent: jest.fn().mockImplementation((url) => ({ proxyUrl: url }))
}))

jest.mock('../../../src/utils', () => ({
getBaseClasses: jest.fn().mockReturnValue(['BaseChatModel']),
getCredentialData: jest.fn(),
getCredentialParam: jest.fn(),
isReasoningModelOpenAI: jest.fn().mockReturnValue(false)
}))

jest.mock('../../../src/modelLoader', () => ({
MODEL_TYPE: { CHAT: 'chat' },
getModels: jest.fn()
}))

jest.mock('./FlowiseChatOpenAI', () => ({
ChatOpenAI: jest.fn().mockImplementation((id, fields) => ({
id,
fields,
setMultiModalOption: jest.fn()
}))
}))

import { ProxyAgent } from 'undici'
import { getCredentialData, getCredentialParam } from '../../../src/utils'

const { ChatOpenAI } = require('./FlowiseChatOpenAI')
const { nodeClass: ChatOpenAINode } = require('./ChatOpenAI')

describe('ChatOpenAI node', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('reuses the proxy dispatcher across init calls', async () => {
;(getCredentialData as jest.Mock).mockResolvedValue({ openAIApiKey: 'sk-test' })
;(getCredentialParam as jest.Mock).mockImplementation((key, credentialData) => credentialData[key])

const node = new ChatOpenAINode()
const nodeData = {
credential: 'cred-1',
inputs: {
modelName: 'gpt-4o-mini',
temperature: '0.2',
proxyUrl: 'http://corporate-proxy.example.com:3128',
baseOptions: {
'OpenAI-Beta': 'assistants=v2'
}
}
}

await node.init(
{
...nodeData,
id: 'chatOpenAI_0'
},
'',
{}
)
await node.init(
{
...nodeData,
id: 'chatOpenAI_1'
},
'',
{}
)

expect(ProxyAgent).toHaveBeenCalledWith('http://corporate-proxy.example.com:3128')
expect(ProxyAgent).toHaveBeenCalledTimes(1)
expect(ChatOpenAI).toHaveBeenCalledTimes(2)
expect(ChatOpenAI.mock.calls[1][1].configuration.fetchOptions.dispatcher).toBe(
ChatOpenAI.mock.calls[0][1].configuration.fetchOptions.dispatcher
)
expect(ChatOpenAI).toHaveBeenCalledWith(
'chatOpenAI_0',
expect.objectContaining({
configuration: {
defaultHeaders: {
'OpenAI-Beta': 'assistants=v2'
},
fetchOptions: {
dispatcher: { proxyUrl: 'http://corporate-proxy.example.com:3128' }
}
}
})
)
})
})
32 changes: 30 additions & 2 deletions packages/components/nodes/chatmodels/ChatOpenAI/ChatOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { getBaseClasses, getCredentialData, getCredentialParam, isReasoningModel
import { ChatOpenAI } from './FlowiseChatOpenAI'
import { getModels, MODEL_TYPE } from '../../../src/modelLoader'
import { OpenAI as OpenAIClient } from 'openai'
import { ProxyAgent } from 'undici'

Comment thread
luochen211 marked this conversation as resolved.
const proxyAgents = new Map<string, ProxyAgent>()

const getProxyAgent = (proxyUrl: string): ProxyAgent => {
let proxyAgent = proxyAgents.get(proxyUrl)
if (!proxyAgent) {
proxyAgent = new ProxyAgent(proxyUrl)
proxyAgents.set(proxyUrl, proxyAgent)
}
return proxyAgent
}

class ChatOpenAI_ChatModels implements INode {
label: string
Expand Down Expand Up @@ -200,6 +212,14 @@ class ChatOpenAI_ChatModels implements INode {
description: 'Override the default base URL for the API, e.g., "https://api.example.com/v2/',
additionalParams: true
},
{
label: 'Proxy Url',
name: 'proxyUrl',
type: 'string',
optional: true,
description: 'Proxy URL to use for OpenAI API requests, e.g., "http://proxy.example.com:3128"',
additionalParams: true
},
{
label: 'Base Options',
name: 'baseOptions',
Expand Down Expand Up @@ -230,6 +250,7 @@ class ChatOpenAI_ChatModels implements INode {
const streaming = nodeData.inputs?.streaming as boolean
const strictToolCalling = nodeData.inputs?.strictToolCalling as boolean
const basePath = nodeData.inputs?.basepath as string
const proxyUrl = nodeData.inputs?.proxyUrl as string
const baseOptions = nodeData.inputs?.baseOptions
const reasoningEffort = nodeData.inputs?.reasoningEffort as OpenAIClient.ReasoningEffort | null
const reasoningSummary = nodeData.inputs?.reasoningSummary as 'auto' | 'concise' | 'detailed' | null
Expand Down Expand Up @@ -286,10 +307,17 @@ class ChatOpenAI_ChatModels implements INode {
}
}

if (basePath || parsedBaseOptions) {
if (basePath || parsedBaseOptions || proxyUrl) {
obj.configuration = {
baseURL: basePath,
defaultHeaders: parsedBaseOptions
defaultHeaders: parsedBaseOptions,
...(proxyUrl
? {
fetchOptions: {
dispatcher: getProxyAgent(proxyUrl)
}
}
: {})
}
}
Comment thread
luochen211 marked this conversation as resolved.

Expand Down
1 change: 1 addition & 0 deletions packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@
"srt-parser-2": "^1.2.3",
"supergateway": "3.0.1",
"typeorm": "^0.3.28",
"undici": "6.23.0",
"uuid": "^10.0.0",
"vm2": "3.11.2",
"weaviate-client": "3.12.0",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.