Skip to content
Merged
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
6 changes: 4 additions & 2 deletions packages/cli/src/commands/channel/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProxyAgent, setGlobalDispatcher } from 'undici';
import { EnvHttpProxyAgent, setGlobalDispatcher } from 'undici';
import { normalizeProxyUrl } from '@qwen-code/qwen-code-core';

/**
Expand All @@ -15,7 +15,9 @@ export function resolveProxy(
): string | undefined {
const proxyUrl = resolveProxyUrl(cliProxy, settingsProxy);
if (proxyUrl) {
setGlobalDispatcher(new ProxyAgent(proxyUrl));
setGlobalDispatcher(
new EnvHttpProxyAgent({ httpProxy: proxyUrl, httpsProxy: proxyUrl }),
);
}
return proxyUrl;
}
Expand Down
35 changes: 25 additions & 10 deletions packages/cli/src/commands/channel/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { pathToFileURL } from 'node:url';
import type { ChannelBaseOptions } from '@qwen-code/channel-base';

const mockSetGlobalDispatcher = vi.hoisted(() => vi.fn());
const mockProxyAgent = vi.hoisted(() =>
vi.fn((url: string) => ({ proxyUrl: url })),
const mockEnvHttpProxyAgent = vi.hoisted(() =>
vi.fn((opts: { httpProxy: string; httpsProxy: string }) => ({
proxyUrl: opts.httpProxy,
})),
);
const mockNormalizeProxyUrl = vi.hoisted(() => vi.fn((url?: string) => url));
const mockStorageGetGlobalQwenDir = vi.hoisted(() =>
Expand Down Expand Up @@ -94,7 +96,7 @@ const mockSessionRouter = vi.hoisted(() =>
);

vi.mock('undici', () => ({
ProxyAgent: mockProxyAgent,
EnvHttpProxyAgent: mockEnvHttpProxyAgent,
setGlobalDispatcher: mockSetGlobalDispatcher,
}));

Expand Down Expand Up @@ -223,7 +225,10 @@ describe('resolveProxy', () => {
);

expect(proxy).toBe('http://cli.example.com:8080');
expect(mockProxyAgent).toHaveBeenCalledWith('http://cli.example.com:8080');
expect(mockEnvHttpProxyAgent).toHaveBeenCalledWith({
httpProxy: 'http://cli.example.com:8080',
httpsProxy: 'http://cli.example.com:8080',
});
expect(mockSetGlobalDispatcher).toHaveBeenCalledWith({
proxyUrl: 'http://cli.example.com:8080',
});
Expand All @@ -235,9 +240,10 @@ describe('resolveProxy', () => {
const proxy = resolveProxy(undefined, 'http://settings.example.com:8080');

expect(proxy).toBe('http://settings.example.com:8080');
expect(mockProxyAgent).toHaveBeenCalledWith(
'http://settings.example.com:8080',
);
expect(mockEnvHttpProxyAgent).toHaveBeenCalledWith({
httpProxy: 'http://settings.example.com:8080',
httpsProxy: 'http://settings.example.com:8080',
});
});

it('falls back to proxy environment variables', () => {
Expand All @@ -246,7 +252,10 @@ describe('resolveProxy', () => {
const proxy = resolveProxy();

expect(proxy).toBe('http://env.example.com:8080');
expect(mockProxyAgent).toHaveBeenCalledWith('http://env.example.com:8080');
expect(mockEnvHttpProxyAgent).toHaveBeenCalledWith({
httpProxy: 'http://env.example.com:8080',
httpsProxy: 'http://env.example.com:8080',
});
});
});

Expand Down Expand Up @@ -310,8 +319,14 @@ describe('startCommand.handler', () => {
}

expect(mockLoadSettings).toHaveBeenCalledWith(process.cwd());
expect(mockProxyAgent).toHaveBeenCalledWith(settingsProxy);
expect(mockProxyAgent).not.toHaveBeenCalledWith(envProxy);
expect(mockEnvHttpProxyAgent).toHaveBeenCalledWith({
httpProxy: settingsProxy,
httpsProxy: settingsProxy,
});
expect(mockEnvHttpProxyAgent).not.toHaveBeenCalledWith({
httpProxy: envProxy,
httpsProxy: envProxy,
});
expect(mockCreateChannel).toHaveBeenCalledWith(
'telegram',
mockParsedChannelConfig,
Expand Down
Loading