Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
269 changes: 266 additions & 3 deletions packages/core/src/core/openaiContentGenerator/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { OpenAIContentConverter } from './converter.js';
import { openaiRequestCaptureContext } from './requestCaptureContext.js';
import { StreamingToolCallParser } from './streamingToolCallParser.js';
import type { Config } from '../../config/config.js';
import type { ContentGeneratorConfig, AuthType } from '../contentGenerator.js';
import { AuthType, type ContentGeneratorConfig } from '../contentGenerator.js';
import type { OpenAICompatibleProvider } from './provider/index.js';

// Mock dependencies
Expand Down Expand Up @@ -463,8 +463,24 @@ describe('ContentGenerationPipeline', () => {
});

it('should override enable_thinking when thinkingConfig disables it', async () => {
// Arrange — provider injects enable_thinking: true via extra_body,
// but request explicitly disables thinking
// Arrange — provider injects enable_thinking: true via extra_body
// (e.g. user configured `enableThinking: true` via setup wizard,
// see provider-config.ts), but request explicitly disables thinking.
// DashScope hostname + qwen model name are both required: the gate
// is hostname + model-name to avoid leaking the qwen-specific
// `enable_thinking` field to non-qwen routings (off-DashScope, or
// GLM/DeepSeek on the same DashScope hostname).
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
model: 'qwen3.5-flash',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

(mockProvider.buildRequest as Mock).mockImplementation((req) => ({
...req,
enable_thinking: true, // Simulates extra_body injection
Expand Down Expand Up @@ -747,6 +763,253 @@ describe('ContentGenerationPipeline', () => {
expect(apiCall.thinking).toBeUndefined();
});

it('emits enable_thinking:false on DashScope hostname when includeThoughts is false', async () => {
// Regression for #4501: qwen3 hybrid models (e.g. qwen3.5-flash)
// default to thinking-on. Provider buildRequest never auto-injects
// `enable_thinking`, so a previous guarded `'enable_thinking' in typed`
// check never fired and side-queries burned reasoning tokens (24-95x
// output bloat in production). The disable must be emitted explicitly.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
model: 'qwen3.5-flash',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

// Provider passes the request through unchanged — simulates the
// common case where the user has not configured
// `extra_body.enable_thinking` (so the field never appears on the
// wire body unless we add it here).
const request: GenerateContentParameters = {
model: 'test-model',
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
contents: [{ parts: [{ text: 'Summarize' }], role: 'user' }],
config: { thinkingConfig: { includeThoughts: false } },
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Summarize' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'forked_query');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBe(false);
});

it('emits enable_thinking:false on DashScope hostname when reasoning is configured to false', async () => {
// Config-level opt-out (`reasoning: false`) should also disable
// qwen3 thinking, mirroring the DeepSeek pair above.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
model: 'qwen3.5-flash',
reasoning: false,
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

const request: GenerateContentParameters = {
model: 'test-model',
contents: [{ parts: [{ text: 'Hello' }], role: 'user' }],
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Hello' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'main');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBe(false);
});

it('emits enable_thinking:false on QWEN_OAUTH regardless of baseUrl', async () => {
// QWEN_OAUTH activates the DashScope provider regardless of baseUrl
// (see DashScopeOpenAICompatibleProvider.isDashScopeProvider line 47).
// Verify the gate fires through that path too — important because
// QWEN_OAUTH is the default flow for first-time users and does not
// go through the wizard's `extra_body` setup.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
authType: AuthType.QWEN_OAUTH,
baseUrl: 'https://some-oauth-issued-endpoint.example/v1',
model: 'qwen3-coder-flash',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

const request: GenerateContentParameters = {
model: 'test-model',
contents: [{ parts: [{ text: 'Hi' }], role: 'user' }],
config: { thinkingConfig: { includeThoughts: false } },
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Hi' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'forked_query');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBe(false);
});

it('emits enable_thinking:false on internal alibaba-inc.com hostname', async () => {
// Internal Alibaba domains proxy to DashScope-compatible APIs and
// are treated as DashScope by design (provider/dashscope.ts:75-78).
// Cover the internal-origin path explicitly so a future tightening
// of the hostname rules does not silently drop coverage for
// internal users.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://gateway.alibaba-inc.com/v1',
model: 'qwen3.5-flash',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

const request: GenerateContentParameters = {
model: 'test-model',
contents: [{ parts: [{ text: 'Hi' }], role: 'user' }],
config: { thinkingConfig: { includeThoughts: false } },
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Hi' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'forked_query');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBe(false);
});

it('does NOT emit enable_thinking on a non-DashScope hostname', async () => {
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
// `enable_thinking` is a qwen-specific extension. Pushing it at a
// strict OpenAI-compatible backend could trip an unknown-key 400
// and would also pollute logs with a meaningless field. Mirror of
// the DeepSeek negative test above.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://api.openai.com/v1',
model: 'gpt-5',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

const request: GenerateContentParameters = {
model: 'test-model',
contents: [{ parts: [{ text: 'Suggest' }], role: 'user' }],
config: { thinkingConfig: { includeThoughts: false } },
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Suggest' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'forked_query');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBeUndefined();
});

it('does NOT emit enable_thinking on a non-qwen model routed through DashScope', async () => {
// DashScope's compatible-mode endpoint routes multiple model families
// (qwen3, GLM, DeepSeek). Hostname alone is not enough — GLM uses
// `extra_body.thinking.enabled` and DeepSeek-on-DashScope uses
// `thinking: { type: 'disabled' }`, so sending `enable_thinking` is
// at best a no-op and at worst forwarded upstream and rejected.
mockContentGeneratorConfig = {
...mockContentGeneratorConfig,
baseUrl: 'https://dashscope.aliyuncs.com/compatible-mode/v1',
model: 'glm-5',
} as ContentGeneratorConfig;
mockConfig = {
...mockConfig,
contentGeneratorConfig: mockContentGeneratorConfig,
};
pipeline = new ContentGenerationPipeline(mockConfig);

const request: GenerateContentParameters = {
model: 'test-model',
contents: [{ parts: [{ text: 'Summarize' }], role: 'user' }],
config: { thinkingConfig: { includeThoughts: false } },
};

(mockConverter.convertGeminiRequestToOpenAI as Mock).mockReturnValue([
{ role: 'user', content: 'Summarize' },
]);
(mockConverter.convertOpenAIResponseToGemini as Mock).mockReturnValue(
new GenerateContentResponse(),
);
(mockClient.chat.completions.create as Mock).mockResolvedValue({
id: 'r',
choices: [{ message: { content: 'ok' }, finish_reason: 'stop' }],
} as OpenAI.Chat.ChatCompletion);

await pipeline.execute(request, 'forked_query');

const apiCall = (mockClient.chat.completions.create as Mock).mock
.calls[0][0];
expect(apiCall.enable_thinking).toBeUndefined();
});

it('should handle errors and log them', async () => {
// Arrange
const request: GenerateContentParameters = {
Expand Down
20 changes: 18 additions & 2 deletions packages/core/src/core/openaiContentGenerator/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@google/genai';
import type { ContentGeneratorConfig } from '../contentGenerator.js';
import { OpenAIContentConverter } from './converter.js';
import { DashScopeOpenAICompatibleProvider } from './provider/dashscope.js';
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
import { isDeepSeekHostname } from './provider/deepseek.js';
import { openaiRequestCaptureContext } from './requestCaptureContext.js';
import { StreamingToolCallParser } from './streamingToolCallParser.js';
Expand Down Expand Up @@ -361,7 +362,21 @@ export class ContentGenerationPipeline {
this.contentGeneratorConfig.reasoning === false;
if (reasoningDisabled) {
const typed = providerRequest as unknown as Record<string, unknown>;
if ('enable_thinking' in typed) {
// Provider buildRequest doesn't auto-inject `enable_thinking`, so a
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
// guarded `in typed` check would never fire for default qwen3 configs.
// Hostname + model-name gate avoids leaking this qwen-specific field
// to non-qwen routings on the same DashScope hostname (GLM uses
// `extra_body.thinking.enabled`, DeepSeek-on-DashScope uses
// `thinking: { type: 'disabled' }`; sending `enable_thinking` to them
// is at best a no-op, at worst forwarded upstream and rejected).
if (
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
DashScopeOpenAICompatibleProvider.isDashScopeProvider(
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
this.contentGeneratorConfig,
) &&
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
(this.contentGeneratorConfig.model ?? '')
Comment thread
doudouOUC marked this conversation as resolved.
Resolved
.toLowerCase()
.startsWith('qwen')
) {
typed['enable_thinking'] = false;
}
// Strip reasoning config — extra_body could inject it, overriding
Expand Down Expand Up @@ -471,7 +486,8 @@ export class ContentGenerationPipeline {
// - glm-4.7 — thinking is enabled by default; can be disabled via `extra_body.thinking.enabled`
// - kimi-k2-thinking — thinking is enabled by default and cannot be disabled
// - gpt-5.x series — thinking is enabled by default; can be disabled via `reasoning.effort`
// - qwen3 series — model-dependent; can be manually disabled via `extra_body.enable_thinking`
// - qwen3 series — model-dependent; emitted as `enable_thinking: false`
// on DashScope endpoints when reasoning is disabled
//
// Given this inconsistency, we avoid mapping values and only pass through the
// configured reasoning object when explicitly enabled. This keeps provider- and
Expand Down
Loading