-
Notifications
You must be signed in to change notification settings - Fork 196
Feat/proxy support #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xsgeng
wants to merge
10
commits into
MoonshotAI:main
Choose a base branch
from
xsgeng:feat/proxy-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/proxy support #337
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0beece0
feat: add HTTP proxy support for LLM providers and OAuth
xsgeng 01d1546
Merge branch 'MoonshotAI:main' into feat/proxy-support
xsgeng b689c8c
docs: document HTTP_PROXY, HTTPS_PROXY, and NO_PROXY env vars
xsgeng a080bee
chore(kosong,oauth): add cross-reference comments to proxy modules
xsgeng a3a53de
test(kosong): add proxy fetch tests
xsgeng 4c083ff
test(oauth): add proxy fetch tests
xsgeng 31d87b8
fix(proxy): prefer lowercase no_proxy over NO_PROXY
xsgeng 9a3459f
fix(auth): use proxy-aware fetch for open-platform API key verification
xsgeng baefd1f
chore(deps): bump undici from 6.21.0 to ^6.21.2
xsgeng bb1c554
docs: clarify that Google providers are not yet proxied
xsgeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@moonshot-ai/kosong": minor | ||
| "@moonshot-ai/kimi-code-oauth": minor | ||
| "@moonshot-ai/kimi-code": minor | ||
| --- | ||
|
|
||
| Add support for HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables in LLM provider and OAuth HTTP requests. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { fetch as undiciFetch, EnvHttpProxyAgent } from 'undici'; | ||
|
|
||
| const nativeFetch = globalThis.fetch; | ||
| let proxyAgent: EnvHttpProxyAgent | undefined; | ||
| let proxyFetch: typeof fetch | undefined; | ||
|
|
||
| /** | ||
| * Return a `fetch` implementation that honours `HTTP_PROXY` / `HTTPS_PROXY` | ||
| * (and their lower-case variants) as well as `NO_PROXY`. | ||
| * | ||
| * When no proxy environment variables are set this returns the global | ||
| * `fetch` so there is no runtime overhead. | ||
| * | ||
| * Note: this module is intentionally duplicated in `packages/oauth/src/proxy-fetch.ts` | ||
| * because `oauth` does not depend on `kosong`. Keep the two files in sync. | ||
| */ | ||
| export function getProxyFetch(): typeof fetch { | ||
| if (proxyFetch !== undefined) { | ||
| return proxyFetch; | ||
| } | ||
|
|
||
| const hasProxy = | ||
| process.env['HTTP_PROXY'] || | ||
| process.env['http_proxy'] || | ||
| process.env['HTTPS_PROXY'] || | ||
| process.env['https_proxy']; | ||
|
|
||
| if (!hasProxy) { | ||
| proxyFetch = fetch; | ||
| return proxyFetch; | ||
| } | ||
|
|
||
| const noProxyEnv = process.env['NO_PROXY'] ?? process.env['no_proxy']; | ||
| const noProxy = noProxyEnv === undefined ? 'localhost,127.0.0.1' : noProxyEnv; | ||
|
xsgeng marked this conversation as resolved.
Outdated
|
||
|
|
||
| proxyAgent = new EnvHttpProxyAgent({ noProxy }); | ||
| proxyFetch = (url, init) => { | ||
| // If global fetch has been replaced (e.g. mocked in tests), delegate to it | ||
| if (globalThis.fetch !== nativeFetch) { | ||
| return globalThis.fetch(url, init); | ||
| } | ||
| return undiciFetch( | ||
| url, | ||
| { ...init, dispatcher: proxyAgent } as Parameters<typeof undiciFetch>[1], | ||
| ); | ||
| }; | ||
| return proxyFetch; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const mockUndiciFetch = vi.hoisted(() => vi.fn()); | ||
| const MockEnvHttpProxyAgent = vi.hoisted(() => vi.fn()); | ||
|
|
||
| vi.mock('undici', () => ({ | ||
| fetch: mockUndiciFetch, | ||
| EnvHttpProxyAgent: MockEnvHttpProxyAgent, | ||
| })); | ||
|
|
||
| describe('getProxyFetch', () => { | ||
| const originalEnv = process.env; | ||
| const originalFetch = globalThis.fetch; | ||
|
|
||
| beforeEach(() => { | ||
| process.env = { ...originalEnv }; | ||
| delete process.env['HTTP_PROXY']; | ||
| delete process.env['http_proxy']; | ||
| delete process.env['HTTPS_PROXY']; | ||
| delete process.env['https_proxy']; | ||
| delete process.env['NO_PROXY']; | ||
| delete process.env['no_proxy']; | ||
| mockUndiciFetch.mockClear(); | ||
| MockEnvHttpProxyAgent.mockClear(); | ||
| globalThis.fetch = originalFetch; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env = originalEnv; | ||
| globalThis.fetch = originalFetch; | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| it('returns global fetch when no proxy variables are set', async () => { | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const result = getProxyFetch(); | ||
| expect(result).toBe(originalFetch); | ||
| }); | ||
|
|
||
| it('creates an agent and returns a wrapped fetch when HTTP_PROXY is set', async () => { | ||
| process.env['HTTP_PROXY'] = 'http://proxy.example.com:8080'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const result = getProxyFetch(); | ||
|
|
||
| expect(result).not.toBe(originalFetch); | ||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledTimes(1); | ||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledWith({ | ||
| noProxy: 'localhost,127.0.0.1', | ||
| }); | ||
|
|
||
| const requestInit = { method: 'POST' }; | ||
| await result('https://api.example.com', requestInit); | ||
| expect(mockUndiciFetch).toHaveBeenCalledTimes(1); | ||
| expect(mockUndiciFetch).toHaveBeenCalledWith( | ||
| 'https://api.example.com', | ||
| expect.objectContaining({ method: 'POST', dispatcher: expect.anything() }), | ||
| ); | ||
| }); | ||
|
|
||
| it('honours lowercase http_proxy', async () => { | ||
| process.env['http_proxy'] = 'http://proxy.example.com:8080'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const result = getProxyFetch(); | ||
|
|
||
| expect(result).not.toBe(originalFetch); | ||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('honours HTTPS_PROXY', async () => { | ||
| process.env['HTTPS_PROXY'] = 'https://proxy.example.com:8443'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const result = getProxyFetch(); | ||
|
|
||
| expect(result).not.toBe(originalFetch); | ||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('passes NO_PROXY to the agent', async () => { | ||
| process.env['HTTP_PROXY'] = 'http://proxy.example.com:8080'; | ||
| process.env['NO_PROXY'] = 'example.com,internal.local'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| getProxyFetch(); | ||
|
|
||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledWith({ | ||
| noProxy: 'example.com,internal.local', | ||
| }); | ||
| }); | ||
|
|
||
| it('passes lowercase no_proxy to the agent', async () => { | ||
| process.env['HTTP_PROXY'] = 'http://proxy.example.com:8080'; | ||
| process.env['no_proxy'] = 'example.com'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| getProxyFetch(); | ||
|
|
||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledWith({ | ||
| noProxy: 'example.com', | ||
| }); | ||
| }); | ||
|
|
||
| it('caches the fetch implementation across calls', async () => { | ||
| process.env['HTTP_PROXY'] = 'http://proxy.example.com:8080'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const first = getProxyFetch(); | ||
| const second = getProxyFetch(); | ||
|
|
||
| expect(second).toBe(first); | ||
| expect(MockEnvHttpProxyAgent).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('delegates to global fetch when it has been replaced (e.g. mocked)', async () => { | ||
| process.env['HTTP_PROXY'] = 'http://proxy.example.com:8080'; | ||
| vi.resetModules(); | ||
| const { getProxyFetch } = await import('#/proxy'); | ||
| const proxyFetch = getProxyFetch(); | ||
|
|
||
| const mockGlobalFetch = vi.fn(); | ||
| globalThis.fetch = mockGlobalFetch; | ||
|
|
||
| await proxyFetch('https://api.example.com', { method: 'GET' }); | ||
| expect(mockGlobalFetch).toHaveBeenCalledTimes(1); | ||
| expect(mockGlobalFetch).toHaveBeenCalledWith('https://api.example.com', { method: 'GET' }); | ||
| expect(mockUndiciFetch).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.