Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const DEFAULT_DASHSCOPE_BASE_URL =
'https://dashscope.aliyuncs.com/compatible-mode/v1';
export const DEFAULT_DEEPSEEK_BASE_URL = 'https://api.deepseek.com/v1';
export const DEFAULT_OPEN_ROUTER_BASE_URL = 'https://openrouter.ai/api/v1';
export const DASHSCOPE_PROXY_BASE_URL = process.env['DASHSCOPE_PROXY_BASE_URL'];
Comment thread
HeZiGang marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import { DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES } from '../constants.js';
import { buildRuntimeFetchOptions } from '../../../utils/runtimeFetchOptions.js';
import type { OpenAIRuntimeFetchOptions } from '../../../utils/runtimeFetchOptions.js';

const mockDebugLogger = vi.hoisted(() => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}));
vi.mock('../../../utils/debugLogger.js', () => ({
createDebugLogger: vi.fn(() => mockDebugLogger),
}));

// Mock OpenAI
vi.mock('openai', () => ({
default: vi.fn().mockImplementation((config) => ({
Expand All @@ -38,6 +48,30 @@ vi.mock('../../../utils/runtimeFetchOptions.js', () => ({
buildRuntimeFetchOptions: vi.fn(),
}));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Test mock diverges from production and is fragile

The vi.mock('../constants.js') uses a getter for DASHSCOPE_PROXY_BASE_URL (dynamic read from process.env), but production uses export const (frozen at import time). Additionally, the mock duplicates all 6 other constant exports verbatim — any future constant added to constants.ts would be silently undefined in these tests. Consider using importOriginal to preserve real exports:

Suggested change
}));
vi.mock('../constants.js', async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
get DASHSCOPE_PROXY_BASE_URL() {
return process.env['DASHSCOPE_PROXY_BASE_URL'];
},
};
});

— glm-5.1 via Qwen Code /review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is already implemented at lines 60–62. Why is this recommendation showing up?


vi.mock('../constants.js', async (importOriginal) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] 重复的 vi.mock 调用——第一个是死代码,导致 tsc 编译错误

对同一模块 ../constants.js 有两个 vi.mock 调用。第一个(L51-59, async (importOriginal))被第二个(L62-73, 硬编码所有常量)完全覆盖。第一个 mock 中的 ...actual 展开触发 TS2698: Spread types may only be created from object types。第二个 mock 硬编码了 DEFAULT_TIMEOUT: 120000 等值——如果 constants.ts 中真实常量发生变化,mock 将漂移。

Suggested change
vi.mock('../constants.js', async (importOriginal) => {
// 删除第一个 vi.mock 块(L51-59),删除第二个 mock 中的硬编码常量,改为:
vi.mock('../constants.js', async (importOriginal) => {
const actual = await importOriginal<typeof import('../constants.js')>();
return {
...actual,
get DASHSCOPE_PROXY_BASE_URL() {
return process.env['DASHSCOPE_PROXY_BASE_URL'];
},
};
});

— DeepSeek/deepseek-v4-pro via Qwen Code /review

const actual = await importOriginal();
return {
...actual,
get DASHSCOPE_PROXY_BASE_URL() {
return process.env['DASHSCOPE_PROXY_BASE_URL'];
},
};
});

// Mock DASHSCOPE_PROXY_BASE_URL so tests can control its value
vi.mock('../constants.js', () => ({
DEFAULT_TIMEOUT: 120000,
DEFAULT_MAX_RETRIES: 3,
DEFAULT_OPENAI_BASE_URL: 'https://api.openai.com/v1',
DEFAULT_DASHSCOPE_BASE_URL:
'https://dashscope.aliyuncs.com/compatible-mode/v1',
DEFAULT_DEEPSEEK_BASE_URL: 'https://api.deepseek.com/v1',
DEFAULT_OPEN_ROUTER_BASE_URL: 'https://openrouter.ai/api/v1',
get DASHSCOPE_PROXY_BASE_URL() {
return process.env['DASHSCOPE_PROXY_BASE_URL'];
},
}));

describe('DashScopeOpenAICompatibleProvider', () => {
let provider: DashScopeOpenAICompatibleProvider;
let mockContentGeneratorConfig: ContentGeneratorConfig;
Expand Down Expand Up @@ -162,6 +196,76 @@ describe('DashScopeOpenAICompatibleProvider', () => {
expect(result).toBe(false);
});
});

it('should return true when baseUrl matches DASHSCOPE_PROXY_BASE_URL', () => {
vi.stubEnv(
'DASHSCOPE_PROXY_BASE_URL',
'https://your-proxy.com/dashscope',
);

const config = {
authType: AuthType.USE_OPENAI,
baseUrl: 'https://your-proxy.com/dashscope',
} as ContentGeneratorConfig;

const result =
DashScopeOpenAICompatibleProvider.isDashScopeProvider(config);
expect(result).toBe(true);
});

it('should return false when baseUrl does not match DASHSCOPE_PROXY_BASE_URL', () => {
vi.stubEnv(
'DASHSCOPE_PROXY_BASE_URL',
'https://your-proxy.com/dashscope',
);

const config = {
authType: AuthType.USE_OPENAI,
baseUrl: 'https://other-proxy.com/dashscope',
} as ContentGeneratorConfig;

const result =
DashScopeOpenAICompatibleProvider.isDashScopeProvider(config);
expect(result).toBe(false);
});

it('should debug log when baseUrl does not match DASHSCOPE_PROXY_BASE_URL', () => {
vi.stubEnv(
'DASHSCOPE_PROXY_BASE_URL',
'https://your-proxy.com/dashscope',
);

const config = {
authType: AuthType.USE_OPENAI,
baseUrl: 'https://other-proxy.com/dashscope',
} as ContentGeneratorConfig;

const result =
DashScopeOpenAICompatibleProvider.isDashScopeProvider(config);

expect(result).toBe(false);
expect(mockDebugLogger.debug).toHaveBeenCalledWith(
expect.stringContaining(
"DASHSCOPE_PROXY_BASE_URL is configured as 'https://your-proxy.com/dashscope'",
),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Critical] Test assertion does not match production code — the test will fail.

The test expects the debug log to contain "DASHSCOPE_PROXY_BASE_URL is configured as 'https://your-proxy.com/dashscope'", but the actual code in dashscope.ts outputs "DASHSCOPE_PROXY_BASE_URL is configured but the request baseUrl does not match. DashScope headers/cache control will be skipped.".

The expect.stringContaining check will fail because the actual message uses "configured but" instead of "configured as", and does not include the URL value.

Suggested change
),
"DASHSCOPE_PROXY_BASE_URL is configured but the request baseUrl does not match. DashScope headers/cache control will be skipped.",

— DeepSeek/deepseek-v4-pro via Qwen Code /review

);
});

it('should return true when baseUrl matches DASHSCOPE_PROXY_BASE_URL with trailing slash', () => {
vi.stubEnv(
'DASHSCOPE_PROXY_BASE_URL',
'https://your-proxy.com/dashscope',
);

const config = {
authType: AuthType.USE_OPENAI,
baseUrl: 'https://your-proxy.com/dashscope/',
} as ContentGeneratorConfig;

const result =
DashScopeOpenAICompatibleProvider.isDashScopeProvider(config);
expect(result).toBe(true);
});
});

describe('buildHeaders', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
DEFAULT_TIMEOUT,
DEFAULT_MAX_RETRIES,
DEFAULT_DASHSCOPE_BASE_URL,
DASHSCOPE_PROXY_BASE_URL,
} from '../constants.js';
import type {
DashScopeRequestMetadata,
Expand All @@ -15,6 +16,7 @@
ChatCompletionToolWithCache,
} from './types.js';
import { buildRuntimeFetchOptions } from '../../../utils/runtimeFetchOptions.js';
import { createDebugLogger } from '../../../utils/debugLogger.js';
import { DefaultOpenAICompatibleProvider } from './default.js';

export class DashScopeOpenAICompatibleProvider extends DefaultOpenAICompatibleProvider {
Expand All @@ -33,8 +35,31 @@
if (authType === AuthType.QWEN_OAUTH) return true;
if (!baseUrl) return true;

const normalizedBaseUrl = baseUrl.endsWith('/')
Comment thread
HeZiGang marked this conversation as resolved.
Resolved
? baseUrl.slice(0, -1)
: baseUrl;

// Matches: dashscope.aliyuncs.com, *.dashscope.aliyuncs.com, or *.dashscope-intl.aliyuncs.com
return /([\w-]+\.)?dashscope(-intl)?\.aliyuncs\.com/i.test(baseUrl);
const isDashscopeOrigin =
/([\w-]+\.)?dashscope(-intl)?\.aliyuncs\.com/i.test(normalizedBaseUrl);

// Check if proxy is configured and matches
const normalizedProxyUrl = DASHSCOPE_PROXY_BASE_URL?.endsWith('/')
? DASHSCOPE_PROXY_BASE_URL.slice(0, -1)
: DASHSCOPE_PROXY_BASE_URL;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Case-sensitivity inconsistency between origin check and proxy check

The origin-domain regex on line 43 uses the /i (case-insensitive) flag, but the proxy URL comparison here uses strict ===. If DASHSCOPE_PROXY_BASE_URL and baseUrl differ only in casing (common in CI/CD environments where env vars and config files come from different sources), the proxy match silently fails and DashScope-specific headers, cache control, and session tracking are all skipped.

Suggested change
const isProxyConfigured = Boolean(
normalizedProxyUrl &&
normalizedBaseUrl.toLowerCase() === normalizedProxyUrl.toLowerCase(),
);

— glm-5.1 via Qwen Code /review

const isProxyConfigured = Boolean(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] No diagnostic logging when proxy URL doesn't match

When DASHSCOPE_PROXY_BASE_URL is set but does not match baseUrl, the code silently falls through to the default provider. DashScope-specific headers (X-DashScope-CacheControl, X-DashScope-UserAgent, X-DashScope-AuthType), cache control, and session tracking are all skipped with zero indication of why. This is the hardest failure mode to debug — the user sees "prompt caching not working" with no error or warning. Consider adding a debug/trace-level log when the proxy URL is set but doesn't match.

— glm-5.1 via Qwen Code /review

Comment thread
HeZiGang marked this conversation as resolved.
Resolved
normalizedProxyUrl &&
normalizedBaseUrl.toLowerCase() === normalizedProxyUrl.toLowerCase(),
);

if (normalizedProxyUrl && !isDashscopeOrigin && !isProxyConfigured) {
createDebugLogger('DashScopeOpenAICompatibleProvider').debug(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] 调试日志可能泄露嵌入 URL 的凭据 & 内联 createDebugLogger 违反代码库约定

两个问题在此行交汇:(1) 完整代理 URL 被写入调试日志文件,如果用户将凭据嵌入 URL(如 https://user:pass@proxy.com/dashscope),凭据将以明文持久化到磁盘(QWEN_DEBUG_LOG_FILE 默认启用)。(2) createDebugLogger 在方法内部内联调用,而代码库中 80+ 处调用均遵循模块级单例模式(const debugLogger = createDebugLogger('NAME'))。

Suggested change
createDebugLogger('DashScopeOpenAICompatibleProvider').debug(
// 模块顶层:
const debugLogger = createDebugLogger('DashScopeOpenAICompatibleProvider');
// 在 isDashScopeProvider 内:
if (normalizedProxyUrl && !isDashscopeOrigin && !isProxyMatch) {
debugLogger.debug(
`DASHSCOPE_PROXY_BASE_URL is configured but the request baseUrl does not match. DashScope headers/cache control will be skipped.`,
);
}

— DeepSeek/deepseek-v4-pro via Qwen Code /review

`DASHSCOPE_PROXY_BASE_URL is configured as '${normalizedProxyUrl}', but the request baseUrl '${normalizedBaseUrl}' does not match. DashScope headers/cache control will be skipped.`,
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] Debug log on proxy mismatch does not include the actual URL values being compared.

When DASHSCOPE_PROXY_BASE_URL is configured but doesn't match, the current log message does not show either the proxy URL or the request baseUrl. Users cannot diagnose why the match failed without adding their own logging.

Suggested change
);
debugLogger.debug(
`DASHSCOPE_PROXY_BASE_URL is configured as '${normalizedProxyUrl}' ` +
`but the request baseUrl ('${normalizedBaseUrl}') does not match. ` +
`DashScope headers/cache control will be skipped.`,
);

— DeepSeek/deepseek-v4-pro via Qwen Code /review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the url in logs to prevent URL exposure.

}

return isDashscopeOrigin || isProxyConfigured;
}

override buildHeaders(): Record<string, string | undefined> {
Expand Down
Loading