Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(gemini): align thinkingLevel config resolution across the stack (#13457)

Sun1313839bca4bc1a71
diff --git a/packages/model-runtime/src/providers/google/index.test.ts b/packages/model-runtime/src/providers/google/index.test.ts
index 83fdabf81ae..18cfc5ce806 100644
--- a/packages/model-runtime/src/providers/google/index.test.ts
+++ b/packages/model-runtime/src/providers/google/index.test.ts
@@ -12,6 +12,10 @@ const provider = 'google';
const bizErrorType = 'ProviderBizError';
const invalidErrorType = 'InvalidProviderAPIKey';
+async function* createEmptyAsyncGenerator<T>(): AsyncGenerator<T> {
+ yield* [] as unknown as T[];
+}
+
// Mock the console.error to avoid polluting test output
vi.spyOn(console, 'error').mockImplementation(() => {});
@@ -21,7 +25,7 @@ beforeEach(() => {
instance = new LobeGoogleAI({ apiKey: 'test' });
// Use vi.spyOn to mock the chat.completions.create method
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
});
@@ -354,16 +358,16 @@ describe('LobeGoogleAI', () => {
const enhancedStream = instance['createEnhancedStream'](mockStream, abortController.signal);
const reader = enhancedStream.getReader();
- const chunks: any[] = [];
+ let chunks: any[] = [];
// Read first value then cancel to trigger error chunk
- chunks.push((await reader.read()).value); // eslint-disable-line unicorn/no-immediate-mutation
+ chunks = chunks.concat((await reader.read()).value);
abortController.abort();
// Read all remaining chunks
let result;
while (!(result = await reader.read()).done) {
- chunks.push(result.value);
+ chunks = chunks.concat(result.value);
}
// Batch-assert the entire chunks array
@@ -381,9 +385,7 @@ describe('LobeGoogleAI', () => {
});
it('should handle stream cancellation without data', async () => {
- const mockStream = (async function* () {
- // Empty stream
- })();
+ const mockStream = createEmptyAsyncGenerator<{ text: string }>();
const abortController = new AbortController();
const enhancedStream = instance['createEnhancedStream'](mockStream, abortController.signal);
@@ -408,13 +410,13 @@ describe('LobeGoogleAI', () => {
const enhancedStream = instance['createEnhancedStream'](mockStream, abortController.signal);
const reader = enhancedStream.getReader();
- const chunks: any[] = [];
+ let chunks: any[] = [];
// Read first value then collect remaining chunks (error included)
- chunks.push((await reader.read()).value); // eslint-disable-line unicorn/no-immediate-mutation
+ chunks = chunks.concat((await reader.read()).value);
let result;
while (!(result = await reader.read()).done) {
- chunks.push(result.value);
+ chunks = chunks.concat(result.value);
}
// Assert both data and error chunk together
@@ -434,6 +436,7 @@ describe('LobeGoogleAI', () => {
it('should handle AbortError without data', async () => {
// eslint-disable-next-line require-yield
const mockStream = (async function* () {
+ yield* [] as any;
throw new Error('aborted');
})();
@@ -441,17 +444,15 @@ describe('LobeGoogleAI', () => {
const enhancedStream = instance['createEnhancedStream'](mockStream, abortController.signal);
const reader = enhancedStream.getReader();
- const chunks: any[] = [];
// Read error chunk
const chunk1 = await reader.read();
- chunks.push(chunk1.value);
// Stream should be closed
const chunk2 = await reader.read();
expect(chunk2.done).toBe(true);
- expect(chunks[0][LOBE_ERROR_KEY]).toEqual({
+ expect(chunk1.value[LOBE_ERROR_KEY]).toEqual({
body: {
message: 'aborted',
name: 'AbortError',
@@ -474,13 +475,13 @@ describe('LobeGoogleAI', () => {
const enhancedStream = instance['createEnhancedStream'](mockStream, abortController.signal);
const reader = enhancedStream.getReader();
- const chunks: any[] = [];
+ let chunks: any[] = [];
// Read first value then collect remaining chunks (parsing error)
- chunks.push((await reader.read()).value); // eslint-disable-line unicorn/no-immediate-mutation
+ chunks = chunks.concat((await reader.read()).value);
let result;
while (!(result = await reader.read()).done) {
- chunks.push(result.value);
+ chunks = chunks.concat(result.value);
}
expect(chunks).toEqual([
@@ -501,7 +502,7 @@ describe('LobeGoogleAI', () => {
describe('thinkingConfig includeThoughts logic', () => {
it('should enable thinking when thinkingBudget is set', async () => {
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
await instance.chat({
@@ -517,7 +518,7 @@ describe('thinkingConfig includeThoughts logic', () => {
});
it('should enable thinking when thinkingLevel is set', async () => {
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
await instance.chat({
@@ -533,7 +534,7 @@ describe('thinkingConfig includeThoughts logic', () => {
});
it('should let API decide thinking for gemini-3-pro-image models without explicit params', async () => {
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
await instance.chat({
@@ -548,8 +549,23 @@ describe('thinkingConfig includeThoughts logic', () => {
expect(config.thinkingConfig?.includeThoughts).toBeUndefined();
});
+ it('should omit thinkingConfig when all fields are undefined', async () => {
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
+ vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
+
+ await instance.chat({
+ messages: [{ content: 'Hello', role: 'user' }],
+ model: 'gemini-3.1-pro-preview',
+ temperature: 0,
+ });
+
+ const callArgs = (instance['client'].models.generateContentStream as any).mock.calls[0];
+ const config = callArgs[0].config;
+ expect(config.thinkingConfig).toBeUndefined();
+ });
+
it('should enable thinking for thinking-enabled models', async () => {
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
await instance.chat({
@@ -564,7 +580,7 @@ describe('thinkingConfig includeThoughts logic', () => {
});
it('should disable thinking when resolvedThinkingBudget is 0', async () => {
- const mockStreamData = (async function* (): AsyncGenerator<GenerateContentResponse> {})();
+ const mockStreamData = createEmptyAsyncGenerator<GenerateContentResponse>();
vi.spyOn(instance['client'].models, 'generateContentStream').mockResolvedValue(mockStreamData);
await instance.chat({
diff --git a/packages/model-runtime/src/providers/google/index.ts b/packages/model-runtime/src/providers/google/index.ts
index 78a88bcdd79..fabfe3f2212 100644
--- a/packages/model-runtime/src/providers/google/index.ts
+++ b/packages/model-runtime/src/providers/google/index.ts
@@ -1,10 +1,10 @@
-import {
- type GenerateContentConfig,
- type HttpOptions,
- type ThinkingConfig,
- type Tool as GoogleFunctionCallTool,
-} from '@google/genai';
import { GoogleGenAI } from '@google/genai';
+import type {
+ GenerateContentConfig,
+ HttpOptions,
+ ThinkingConfig,
+ Tool as GoogleFunctionCallTool,
+} from '@google/genai';
import debug from 'debug';
import { type LobeRuntimeAI } from '../../core/BaseAI';
@@ -57,6 +57,19 @@ const isGemini3OrAbove = (model?: string): boolean => {
return Number.parseInt(match[1], 10) >= 3;
};
+const normalizeThinkingConfig = (config?: ThinkingConfig): ThinkingConfig | undefined => {
+ if (!config) return undefined;
+
+ const { includeThoughts, thinkingBudget, thinkingLevel } = config;
+
+ // Avoid sending `thinkingConfig: {}` (all fields undefined) which can lead upstream
+ // to treat thinking as disabled and produce no thought parts.
+ if (includeThoughts === undefined && thinkingBudget === undefined && thinkingLevel === undefined)
+ return undefined;
+
+ return config;
+};
+
const modelsDisableInstuction = new Set([
'gemini-2.0-flash-exp',
'gemini-2.0-flash-exp-image-generation',
@@ -217,7 +230,7 @@ export class LobeGoogleAI implements LobeRuntimeAI {
thinkingConfig:
modelsDisableInstuction.has(model) || model.toLowerCase().includes('learnlm')
? undefined
- : thinkingConfig,
+ : normalizeThinkingConfig(thinkingConfig),
// https://ai.google.dev/gemini-api/docs/tool-combination
// Vertex AI does not support includeServerSideToolInvocations
toolConfig:
diff --git a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel2Slider.tsx b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel2Slider.tsx
index 89e75b33e06..d295477d233 100644
--- a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel2Slider.tsx
+++ b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel2Slider.tsx
@@ -7,7 +7,7 @@ type ThinkingLevel2 = (typeof THINKING_LEVELS_2)[number];
export type ThinkingLevel2SliderProps = CreatedLevelSliderProps<ThinkingLevel2>;
const ThinkingLevel2Slider = createLevelSliderComponent<ThinkingLevel2>({
- configKey: 'thinkingLevel',
+ configKey: 'thinkingLevel2',
defaultValue: 'high',
levels: THINKING_LEVELS_2,
style: { minWidth: 110 },
diff --git a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel3Slider.tsx b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel3Slider.tsx
index d3bad22164e..4c3d60559ba 100644
--- a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel3Slider.tsx
+++ b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel3Slider.tsx
@@ -7,7 +7,7 @@ type ThinkingLevel3 = (typeof THINKING_LEVELS_3)[number];
export type ThinkingLevel3SliderProps = CreatedLevelSliderProps<ThinkingLevel3>;
const ThinkingLevel3Slider = createLevelSliderComponent<ThinkingLevel3>({
- configKey: 'thinkingLevel',
+ configKey: 'thinkingLevel3',
defaultValue: 'high',
levels: THINKING_LEVELS_3,
style: { minWidth: 160 },
diff --git a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel4Slider.tsx b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel4Slider.tsx
index f80b41ecae0..ce2c24e0924 100644
--- a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel4Slider.tsx
+++ b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel4Slider.tsx
@@ -7,7 +7,7 @@ type ThinkingLevel4 = (typeof THINKING_LEVELS_4)[number];
export type ThinkingLevel4SliderProps = CreatedLevelSliderProps<ThinkingLevel4>;
const ThinkingLevel4Slider = createLevelSliderComponent<ThinkingLevel4>({
- configKey: 'thinkingLevel',
+ configKey: 'thinkingLevel4',
defaultValue: 'minimal',
levels: THINKING_LEVELS_4,
style: { minWidth: 110 },
diff --git a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel5Slider.tsx b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel5Slider.tsx
index 05818fd9e4d..66579392718 100644
--- a/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel5Slider.tsx
+++ b/src/features/ModelSwitchPanel/components/ControlsForm/ThinkingLevel5Slider.tsx
@@ -7,7 +7,7 @@ type ThinkingLevel5 = (typeof THINKING_LEVELS_5)[number];
export type ThinkingLevel5SliderProps = CreatedLevelSliderProps<ThinkingLevel5>;
const ThinkingLevel5Slider = createLevelSliderComponent<ThinkingLevel5>({
- configKey: 'thinkingLevel',
+ configKey: 'thinkingLevel5',
defaultValue: 'minimal',
levels: THINKING_LEVELS_5,
style: { minWidth: 200 },
diff --git a/src/features/ModelSwitchPanel/components/ControlsForm/createLevelSlider.tsx b/src/features/ModelSwitchPanel/components/ControlsForm/createLevelSlider.tsx
index fe967303ca3..0a690792638 100644
--- a/src/features/ModelSwitchPanel/components/ControlsForm/createLevelSlider.tsx
+++ b/src/features/ModelSwitchPanel/components/ControlsForm/createLevelSlider.tsx
@@ -1,8 +1,8 @@
'use client';
-import { type LobeAgentChatConfig } from '@lobechat/types';
-import { type SliderSingleProps } from 'antd/es/slider';
-import { type CSSProperties } from 'react';
+import type { LobeAgentChatConfig } from '@lobechat/types';
+import type { SliderSingleProps } from 'antd/es/slider';
+import type { CSSProperties } from 'react';
import { memo } from 'react';
import { useAgentId } from '@/features/ChatInput/hooks/useAgentId';
@@ -71,13 +71,18 @@ export function createLevelSliderComponent<T extends string>(config: LevelSlider
const { updateAgentChatConfig } = useUpdateAgentConfig();
const agentConfig = useAgentStore((s) => chatConfigByIdSelectors.getChatConfigById(agentId)(s));
- const storeValue = (agentConfig[configKey] as T) || dv;
+ const resolveValue = (): T => {
+ const rawValue = agentConfig[configKey];
+ if (typeof rawValue === 'string' && levels.includes(rawValue as T)) return rawValue as T;
+
+ return dv;
+ };
const handleChange = (newValue: T) => {
updateAgentChatConfig({ [configKey]: newValue });
};
- return <LevelSliderInner defaultValue={dv} value={storeValue} onChange={handleChange} />;
+ return <LevelSliderInner defaultValue={dv} value={resolveValue()} onChange={handleChange} />;
});
// Main exported component - chooses between controlled and store mode
diff --git a/src/services/chat/mecha/modelParamsResolver.test.ts b/src/services/chat/mecha/modelParamsResolver.test.ts
index 2a61418e8ac..c82beab6fb1 100644
--- a/src/services/chat/mecha/modelParamsResolver.test.ts
+++ b/src/services/chat/mecha/modelParamsResolver.test.ts
@@ -479,7 +479,7 @@ describe('resolveModelExtendParams', () => {
provider: 'provider',
});
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('high');
});
});
@@ -498,7 +498,7 @@ describe('resolveModelExtendParams', () => {
chatConfig: {
thinkingLevel2: 'low',
} as any,
- model: 'gemini-3-pro-preview',
+ model: 'gemini-3.1-pro-preview',
provider: 'google',
});
@@ -508,68 +508,171 @@ describe('resolveModelExtendParams', () => {
it('should not set thinkingLevel when thinkingLevel2 is not configured', () => {
const result = resolveModelExtendParams({
chatConfig: {} as any,
- model: 'gemini-3-pro-preview',
+ model: 'gemini-3.1-pro-preview',
provider: 'google',
});
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('high');
+ });
+ });
+
+ describe('thinkingLevel3 param', () => {
+ beforeEach(() => {
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
+ () => true,
+ );
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
+ 'thinkingLevel3',
+ ]);
});
- it('should not read from thinkingLevel config key', () => {
+ it('should set thinkingLevel from thinkingLevel3 config key', () => {
const result = resolveModelExtendParams({
chatConfig: {
- thinkingLevel: 'high',
+ thinkingLevel3: 'medium',
} as any,
- model: 'gemini-3-pro-preview',
+ model: 'gemini-3.1-pro-preview',
provider: 'google',
});
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('medium');
+ });
+
+ it('should not set thinkingLevel when thinkingLevel3 is not configured', () => {
+ const result = resolveModelExtendParams({
+ chatConfig: {} as any,
+ model: 'gemini-3.1-pro-preview',
+ provider: 'google',
+ });
+
+ expect(result.thinkingLevel).toBe('high');
});
});
- describe('thinkingLevel3 param', () => {
+ describe('thinkingLevel4 param', () => {
beforeEach(() => {
vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
() => true,
);
vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
- 'thinkingLevel3',
+ 'thinkingLevel4',
]);
});
- it('should set thinkingLevel from thinkingLevel3 config key', () => {
+ it('should set thinkingLevel from thinkingLevel4 config key', () => {
const result = resolveModelExtendParams({
chatConfig: {
- thinkingLevel3: 'medium',
+ thinkingLevel4: 'minimal',
} as any,
- model: 'gemini-3.1-pro-preview',
+ model: 'gemini-3.1-flash-image-preview',
+ provider: 'google',
+ });
+
+ expect(result.thinkingLevel).toBe('minimal');
+ });
+
+ it('should use the default thinkingLevel when thinkingLevel4 is not configured', () => {
+ const result = resolveModelExtendParams({
+ chatConfig: {} as any,
+ model: 'gemini-3.1-flash-image-preview',
+ provider: 'google',
+ });
+
+ expect(result.thinkingLevel).toBe('minimal');
+ });
+ });
+
+ describe('thinkingLevel5 param', () => {
+ beforeEach(() => {
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
+ () => true,
+ );
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
+ 'thinkingLevel5',
+ ]);
+ });
+
+ it('should set thinkingLevel from thinkingLevel5 config key', () => {
+ const result = resolveModelExtendParams({
+ chatConfig: {
+ thinkingLevel5: 'medium',
+ } as any,
+ model: 'gemini-3.1-flash-lite-preview',
provider: 'google',
});
expect(result.thinkingLevel).toBe('medium');
});
- it('should not set thinkingLevel when thinkingLevel3 is not configured', () => {
+ it('should use the default thinkingLevel when thinkingLevel5 is not configured', () => {
const result = resolveModelExtendParams({
chatConfig: {} as any,
- model: 'gemini-3.1-pro-preview',
+ model: 'gemini-3.1-flash-lite-preview',
provider: 'google',
});
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('minimal');
});
+ });
+
+ describe('thinkingLevel selection order', () => {
+ it('should use the first configured thinkingLevel* extend param in modelExtendParams order', () => {
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
+ () => true,
+ );
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
+ 'thinkingLevel',
+ 'thinkingLevel3',
+ ]);
- it('should not read from thinkingLevel config key', () => {
const result = resolveModelExtendParams({
chatConfig: {
thinkingLevel: 'high',
+ thinkingLevel3: 'medium',
} as any,
model: 'gemini-3.1-pro-preview',
provider: 'google',
});
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('high');
+ });
+
+ it('should prefer the first configured thinkingLevel param before defaulting', () => {
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
+ () => true,
+ );
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
+ 'thinkingLevel',
+ 'thinkingLevel3',
+ ]);
+
+ const result = resolveModelExtendParams({
+ chatConfig: {
+ thinkingLevel3: 'medium',
+ } as any,
+ model: 'gemini-3.1-pro-preview',
+ provider: 'google',
+ });
+
+ expect(result.thinkingLevel).toBe('medium');
+ });
+
+ it('should fall back to the first supported thinkingLevel default when none are configured', () => {
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'isModelHasExtendParams').mockReturnValue(
+ () => true,
+ );
+ vi.spyOn(aiModelSelectors.aiModelSelectors, 'modelExtendParams').mockReturnValue(() => [
+ 'thinkingLevel4',
+ 'thinkingLevel3',
+ ]);
+
+ const result = resolveModelExtendParams({
+ chatConfig: {} as any,
+ model: 'gemini-3.1-pro-preview',
+ provider: 'google',
+ });
+
+ expect(result.thinkingLevel).toBe('minimal');
});
});
});
@@ -1141,7 +1244,7 @@ describe('resolveModelExtendParams', () => {
expect(result.reasoning_effort).toBeUndefined();
expect(result.verbosity).toBeUndefined();
expect(result.thinkingBudget).toBeUndefined();
- expect(result.thinkingLevel).toBeUndefined();
+ expect(result.thinkingLevel).toBe('high');
expect(result.urlContext).toBeUndefined();
expect(result.imageAspectRatio).toBeUndefined();
expect(result.imageResolution).toBeUndefined();
diff --git a/src/services/chat/mecha/modelParamsResolver.ts b/src/services/chat/mecha/modelParamsResolver.ts
index 24a30e91766..8de006f7eb7 100644
--- a/src/services/chat/mecha/modelParamsResolver.ts
+++ b/src/services/chat/mecha/modelParamsResolver.ts
@@ -1,4 +1,5 @@
-import { type LobeAgentChatConfig } from '@lobechat/types';
+import type { LobeAgentChatConfig } from '@lobechat/types';
+import type { ExtendParamsType } from 'model-bank';
import { aiModelSelectors, getAiInfraStoreState } from '@/store/aiInfra';
@@ -30,6 +31,22 @@ export interface ModelExtendParams {
verbosity?: string;
}
+const DEFAULT_THINKING_LEVEL_BY_EXTEND_PARAM = {
+ thinkingLevel: 'high',
+ thinkingLevel2: 'high',
+ thinkingLevel3: 'high',
+ thinkingLevel4: 'minimal',
+ thinkingLevel5: 'minimal',
+} as const satisfies Partial<Record<ExtendParamsType, string>>;
+
+const THINKING_LEVEL_PARAM_TO_CONFIG_KEY = {
+ thinkingLevel: 'thinkingLevel',
+ thinkingLevel2: 'thinkingLevel2',
+ thinkingLevel3: 'thinkingLevel3',
+ thinkingLevel4: 'thinkingLevel4',
+ thinkingLevel5: 'thinkingLevel5',
+} as const satisfies Partial<Record<ExtendParamsType, keyof LobeAgentChatConfig>>;
+
/**
* Resolves extended parameters for model runtime based on model capabilities and chat config
*
@@ -168,24 +185,24 @@ export const resolveModelExtendParams = (ctx: ModelParamsContext): ModelExtendPa
extendParams.thinkingBudget = chatConfig.thinkingBudget;
}
- if (modelExtendParams.includes('thinkingLevel') && chatConfig.thinkingLevel) {
- extendParams.thinkingLevel = chatConfig.thinkingLevel;
- }
-
- if (modelExtendParams.includes('thinkingLevel2') && chatConfig.thinkingLevel2) {
- extendParams.thinkingLevel = chatConfig.thinkingLevel2;
- }
+ const supportedThinkingLevelParams = modelExtendParams.filter(
+ (extendParam): extendParam is keyof typeof THINKING_LEVEL_PARAM_TO_CONFIG_KEY =>
+ extendParam in THINKING_LEVEL_PARAM_TO_CONFIG_KEY,
+ );
- if (modelExtendParams.includes('thinkingLevel3') && chatConfig.thinkingLevel3) {
- extendParams.thinkingLevel = chatConfig.thinkingLevel3;
- }
+ for (const supportedThinkingLevelParam of supportedThinkingLevelParams) {
+ const configKey = THINKING_LEVEL_PARAM_TO_CONFIG_KEY[supportedThinkingLevelParam];
+ const value = chatConfig[configKey];
- if (modelExtendParams.includes('thinkingLevel4') && chatConfig.thinkingLevel4) {
- extendParams.thinkingLevel = chatConfig.thinkingLevel4;
+ if (typeof value === 'string') {
+ extendParams.thinkingLevel = value;
+ break;
+ }
}
- if (modelExtendParams.includes('thinkingLevel5') && chatConfig.thinkingLevel5) {
- extendParams.thinkingLevel = chatConfig.thinkingLevel5;
+ if (!extendParams.thinkingLevel && supportedThinkingLevelParams.length > 0) {
+ extendParams.thinkingLevel =
+ DEFAULT_THINKING_LEVEL_BY_EXTEND_PARAM[supportedThinkingLevelParams[0]];
}
// URL context