chore(core): refactor model resolution and cleanup fallback logic (#15228)

This commit is contained in:
Adam Weidman
2025-12-22 10:18:51 -05:00
committed by GitHub
parent 58fd00a3df
commit d6a2f1d670
9 changed files with 28 additions and 60 deletions
+2 -5
View File
@@ -253,10 +253,7 @@ export const AppContainer = (props: AppContainerProps) => {
[], [],
); );
// Helper to determine the effective model, considering the fallback state. const [currentModel, setCurrentModel] = useState(config.getModel());
const getEffectiveModel = useCallback(() => config.getModel(), [config]);
const [currentModel, setCurrentModel] = useState(getEffectiveModel());
const [userTier, setUserTier] = useState<UserTierId | undefined>(undefined); const [userTier, setUserTier] = useState<UserTierId | undefined>(undefined);
@@ -341,7 +338,7 @@ export const AppContainer = (props: AppContainerProps) => {
return () => { return () => {
coreEvents.off(CoreEvent.ModelChanged, handleModelChanged); coreEvents.off(CoreEvent.ModelChanged, handleModelChanged);
}; };
}, [getEffectiveModel, config]); }, [config]);
const { consoleMessages, clearConsoleMessages: clearConsoleMessagesState } = const { consoleMessages, clearConsoleMessages: clearConsoleMessagesState } =
useConsoleMessages(); useConsoleMessages();
@@ -27,7 +27,7 @@ import {
ToolCallEvent, ToolCallEvent,
debugLogger, debugLogger,
ReadManyFilesTool, ReadManyFilesTool,
getEffectiveModel, resolveModel,
createWorkingStdio, createWorkingStdio,
startupProfiler, startupProfiler,
} from '@google/gemini-cli-core'; } from '@google/gemini-cli-core';
@@ -282,7 +282,7 @@ export class Session {
const functionCalls: FunctionCall[] = []; const functionCalls: FunctionCall[] = [];
try { try {
const model = getEffectiveModel( const model = resolveModel(
this.config.getModel(), this.config.getModel(),
this.config.getPreviewFeatures(), this.config.getPreviewFeatures(),
); );
@@ -36,8 +36,6 @@ export function resolvePolicyChain(
preferredModel?: string, preferredModel?: string,
wrapsAround: boolean = false, wrapsAround: boolean = false,
): ModelPolicyChain { ): ModelPolicyChain {
// Availability uses the active/requested model directly. Legacy fallback logic
// (getEffectiveModel) only applies when availability is disabled.
const modelFromConfig = const modelFromConfig =
preferredModel ?? config.getActiveModel?.() ?? config.getModel(); preferredModel ?? config.getActiveModel?.() ?? config.getModel();
+16 -16
View File
@@ -6,7 +6,7 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from 'vitest';
import { import {
getEffectiveModel, resolveModel,
resolveClassifierModel, resolveClassifierModel,
isGemini2Model, isGemini2Model,
DEFAULT_GEMINI_MODEL, DEFAULT_GEMINI_MODEL,
@@ -38,69 +38,69 @@ describe('supportsMultimodalFunctionResponse', () => {
}); });
}); });
describe('getEffectiveModel', () => { describe('resolveModel', () => {
describe('delegation to resolveModel', () => { describe('delegation logic', () => {
it('should return the Preview Pro model when auto-gemini-3 is requested', () => { it('should return the Preview Pro model when auto-gemini-3 is requested', () => {
const model = getEffectiveModel(PREVIEW_GEMINI_MODEL_AUTO, false); const model = resolveModel(PREVIEW_GEMINI_MODEL_AUTO, false);
expect(model).toBe(PREVIEW_GEMINI_MODEL); expect(model).toBe(PREVIEW_GEMINI_MODEL);
}); });
it('should return the Default Pro model when auto-gemini-2.5 is requested', () => { it('should return the Default Pro model when auto-gemini-2.5 is requested', () => {
const model = getEffectiveModel(DEFAULT_GEMINI_MODEL_AUTO, false); const model = resolveModel(DEFAULT_GEMINI_MODEL_AUTO, false);
expect(model).toBe(DEFAULT_GEMINI_MODEL); expect(model).toBe(DEFAULT_GEMINI_MODEL);
}); });
it('should return the requested model as-is for explicit specific models', () => { it('should return the requested model as-is for explicit specific models', () => {
expect(getEffectiveModel(DEFAULT_GEMINI_MODEL, false)).toBe( expect(resolveModel(DEFAULT_GEMINI_MODEL, false)).toBe(
DEFAULT_GEMINI_MODEL, DEFAULT_GEMINI_MODEL,
); );
expect(getEffectiveModel(DEFAULT_GEMINI_FLASH_MODEL, false)).toBe( expect(resolveModel(DEFAULT_GEMINI_FLASH_MODEL, false)).toBe(
DEFAULT_GEMINI_FLASH_MODEL, DEFAULT_GEMINI_FLASH_MODEL,
); );
expect(getEffectiveModel(DEFAULT_GEMINI_FLASH_LITE_MODEL, false)).toBe( expect(resolveModel(DEFAULT_GEMINI_FLASH_LITE_MODEL, false)).toBe(
DEFAULT_GEMINI_FLASH_LITE_MODEL, DEFAULT_GEMINI_FLASH_LITE_MODEL,
); );
}); });
it('should return a custom model name when requested', () => { it('should return a custom model name when requested', () => {
const customModel = 'custom-model-v1'; const customModel = 'custom-model-v1';
const model = getEffectiveModel(customModel, false); const model = resolveModel(customModel, false);
expect(model).toBe(customModel); expect(model).toBe(customModel);
}); });
describe('with preview features', () => { describe('with preview features', () => {
it('should return the preview model when pro alias is requested', () => { it('should return the preview model when pro alias is requested', () => {
const model = getEffectiveModel(GEMINI_MODEL_ALIAS_PRO, true); const model = resolveModel(GEMINI_MODEL_ALIAS_PRO, true);
expect(model).toBe(PREVIEW_GEMINI_MODEL); expect(model).toBe(PREVIEW_GEMINI_MODEL);
}); });
it('should return the default pro model when pro alias is requested and preview is off', () => { it('should return the default pro model when pro alias is requested and preview is off', () => {
const model = getEffectiveModel(GEMINI_MODEL_ALIAS_PRO, false); const model = resolveModel(GEMINI_MODEL_ALIAS_PRO, false);
expect(model).toBe(DEFAULT_GEMINI_MODEL); expect(model).toBe(DEFAULT_GEMINI_MODEL);
}); });
it('should return the flash model when flash is requested and preview is on', () => { it('should return the flash model when flash is requested and preview is on', () => {
const model = getEffectiveModel(GEMINI_MODEL_ALIAS_FLASH, true); const model = resolveModel(GEMINI_MODEL_ALIAS_FLASH, true);
expect(model).toBe(PREVIEW_GEMINI_FLASH_MODEL); expect(model).toBe(PREVIEW_GEMINI_FLASH_MODEL);
}); });
it('should return the flash model when lite is requested and preview is on', () => { it('should return the flash model when lite is requested and preview is on', () => {
const model = getEffectiveModel(GEMINI_MODEL_ALIAS_FLASH_LITE, true); const model = resolveModel(GEMINI_MODEL_ALIAS_FLASH_LITE, true);
expect(model).toBe(DEFAULT_GEMINI_FLASH_LITE_MODEL); expect(model).toBe(DEFAULT_GEMINI_FLASH_LITE_MODEL);
}); });
it('should return the flash model when the flash model name is explicitly requested and preview is on', () => { it('should return the flash model when the flash model name is explicitly requested and preview is on', () => {
const model = getEffectiveModel(DEFAULT_GEMINI_FLASH_MODEL, true); const model = resolveModel(DEFAULT_GEMINI_FLASH_MODEL, true);
expect(model).toBe(DEFAULT_GEMINI_FLASH_MODEL); expect(model).toBe(DEFAULT_GEMINI_FLASH_MODEL);
}); });
it('should return the lite model when the lite model name is requested and preview is on', () => { it('should return the lite model when the lite model name is requested and preview is on', () => {
const model = getEffectiveModel(DEFAULT_GEMINI_FLASH_LITE_MODEL, true); const model = resolveModel(DEFAULT_GEMINI_FLASH_LITE_MODEL, true);
expect(model).toBe(DEFAULT_GEMINI_FLASH_LITE_MODEL); expect(model).toBe(DEFAULT_GEMINI_FLASH_LITE_MODEL);
}); });
it('should return the default gemini model when the model is explicitly set and preview is on', () => { it('should return the default gemini model when the model is explicitly set and preview is on', () => {
const model = getEffectiveModel(DEFAULT_GEMINI_MODEL, true); const model = resolveModel(DEFAULT_GEMINI_MODEL, true);
expect(model).toBe(DEFAULT_GEMINI_MODEL); expect(model).toBe(DEFAULT_GEMINI_MODEL);
}); });
}); });
+1 -16
View File
@@ -33,7 +33,7 @@ export const DEFAULT_GEMINI_EMBEDDING_MODEL = 'gemini-embedding-001';
export const DEFAULT_THINKING_MODE = 8192; export const DEFAULT_THINKING_MODE = 8192;
/** /**
* Resolves the requested model alias (e.g., 'auto', 'pro', 'flash', 'flash-lite') * Resolves the requested model alias (e.g., 'auto-gemini-3', 'pro', 'flash', 'flash-lite')
* to a concrete model name, considering preview features. * to a concrete model name, considering preview features.
* *
* @param requestedModel The model alias or concrete model name requested by the user. * @param requestedModel The model alias or concrete model name requested by the user.
@@ -100,21 +100,6 @@ export function resolveClassifierModel(
} }
return resolveModel(requestedModel, previewFeaturesEnabled); return resolveModel(requestedModel, previewFeaturesEnabled);
} }
/**
* Determines the effective model to use.
*
* @param requestedModel The model that was originally requested.
* @param previewFeaturesEnabled A boolean indicating if preview features are enabled.
* @returns The effective model name.
*/
export function getEffectiveModel(
requestedModel: string,
previewFeaturesEnabled: boolean | undefined,
): string {
return resolveModel(requestedModel, previewFeaturesEnabled);
}
export function getDisplayString( export function getDisplayString(
model: string, model: string,
previewFeaturesEnabled: boolean = false, previewFeaturesEnabled: boolean = false,
+3 -3
View File
@@ -390,7 +390,7 @@ export class GeminiClient {
} }
} }
private _getEffectiveModelForCurrentTurn(): string { private _getActiveModelForCurrentTurn(): string {
if (this.currentSequenceModel) { if (this.currentSequenceModel) {
return this.currentSequenceModel; return this.currentSequenceModel;
} }
@@ -460,7 +460,7 @@ export class GeminiClient {
} }
// Check for context window overflow // Check for context window overflow
const modelForLimitCheck = this._getEffectiveModelForCurrentTurn(); const modelForLimitCheck = this._getActiveModelForCurrentTurn();
// Estimate tokens. For text-only requests, we estimate based on character length. // Estimate tokens. For text-only requests, we estimate based on character length.
// For requests with non-text parts (like images, tools), we use the countTokens API. // For requests with non-text parts (like images, tools), we use the countTokens API.
@@ -762,7 +762,7 @@ export class GeminiClient {
// If the model is 'auto', we will use a placeholder model to check. // If the model is 'auto', we will use a placeholder model to check.
// Compression occurs before we choose a model, so calling `count_tokens` // Compression occurs before we choose a model, so calling `count_tokens`
// before the model is chosen would result in an error. // before the model is chosen would result in an error.
const model = this._getEffectiveModelForCurrentTurn(); const model = this._getActiveModelForCurrentTurn();
const { newHistory, info } = await this.compressionService.compress( const { newHistory, info } = await this.compressionService.compress(
this.getChat(), this.getChat(),
+2 -2
View File
@@ -23,7 +23,7 @@ import { InstallationManager } from '../utils/installationManager.js';
import { FakeContentGenerator } from './fakeContentGenerator.js'; import { FakeContentGenerator } from './fakeContentGenerator.js';
import { parseCustomHeaders } from '../utils/customHeaderUtils.js'; import { parseCustomHeaders } from '../utils/customHeaderUtils.js';
import { RecordingContentGenerator } from './recordingContentGenerator.js'; import { RecordingContentGenerator } from './recordingContentGenerator.js';
import { getVersion, getEffectiveModel } from '../../index.js'; import { getVersion, resolveModel } from '../../index.js';
/** /**
* Interface abstracting the core functionalities for generating content and counting tokens. * Interface abstracting the core functionalities for generating content and counting tokens.
@@ -117,7 +117,7 @@ export async function createContentGenerator(
return FakeContentGenerator.fromFile(gcConfig.fakeResponses); return FakeContentGenerator.fromFile(gcConfig.fakeResponses);
} }
const version = await getVersion(); const version = await getVersion();
const model = getEffectiveModel( const model = resolveModel(
gcConfig.getModel(), gcConfig.getModel(),
gcConfig.getPreviewFeatures(), gcConfig.getPreviewFeatures(),
); );
-12
View File
@@ -25,18 +25,6 @@ export async function handleFallback(
failedModel: string, failedModel: string,
authType?: string, authType?: string,
error?: unknown, error?: unknown,
): Promise<string | boolean | null> {
return handlePolicyDrivenFallback(config, failedModel, authType, error);
}
/**
* New fallback logic using the ModelAvailabilityService
*/
async function handlePolicyDrivenFallback(
config: Config,
failedModel: string,
authType?: string,
error?: unknown,
): Promise<string | boolean | null> { ): Promise<string | boolean | null> {
if (authType !== AuthType.LOGIN_WITH_GOOGLE) { if (authType !== AuthType.LOGIN_WITH_GOOGLE) {
return null; return null;
@@ -7,8 +7,8 @@
import type { Config } from '../../config/config.js'; import type { Config } from '../../config/config.js';
import { import {
DEFAULT_GEMINI_MODEL_AUTO, DEFAULT_GEMINI_MODEL_AUTO,
getEffectiveModel,
PREVIEW_GEMINI_MODEL_AUTO, PREVIEW_GEMINI_MODEL_AUTO,
resolveModel,
} from '../../config/models.js'; } from '../../config/models.js';
import type { BaseLlmClient } from '../../core/baseLlmClient.js'; import type { BaseLlmClient } from '../../core/baseLlmClient.js';
import type { import type {
@@ -39,7 +39,7 @@ export class OverrideStrategy implements RoutingStrategy {
// Return the overridden model name. // Return the overridden model name.
return { return {
model: getEffectiveModel(overrideModel, config.getPreviewFeatures()), model: resolveModel(overrideModel, config.getPreviewFeatures()),
metadata: { metadata: {
source: this.name, source: this.name,
latencyMs: 0, latencyMs: 0,