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

View File

@@ -253,10 +253,7 @@ export const AppContainer = (props: AppContainerProps) => {
[],
);
// Helper to determine the effective model, considering the fallback state.
const getEffectiveModel = useCallback(() => config.getModel(), [config]);
const [currentModel, setCurrentModel] = useState(getEffectiveModel());
const [currentModel, setCurrentModel] = useState(config.getModel());
const [userTier, setUserTier] = useState<UserTierId | undefined>(undefined);
@@ -341,7 +338,7 @@ export const AppContainer = (props: AppContainerProps) => {
return () => {
coreEvents.off(CoreEvent.ModelChanged, handleModelChanged);
};
}, [getEffectiveModel, config]);
}, [config]);
const { consoleMessages, clearConsoleMessages: clearConsoleMessagesState } =
useConsoleMessages();

View File

@@ -27,7 +27,7 @@ import {
ToolCallEvent,
debugLogger,
ReadManyFilesTool,
getEffectiveModel,
resolveModel,
createWorkingStdio,
startupProfiler,
} from '@google/gemini-cli-core';
@@ -282,7 +282,7 @@ export class Session {
const functionCalls: FunctionCall[] = [];
try {
const model = getEffectiveModel(
const model = resolveModel(
this.config.getModel(),
this.config.getPreviewFeatures(),
);

View File

@@ -36,8 +36,6 @@ export function resolvePolicyChain(
preferredModel?: string,
wrapsAround: boolean = false,
): ModelPolicyChain {
// Availability uses the active/requested model directly. Legacy fallback logic
// (getEffectiveModel) only applies when availability is disabled.
const modelFromConfig =
preferredModel ?? config.getActiveModel?.() ?? config.getModel();

View File

@@ -6,7 +6,7 @@
import { describe, it, expect } from 'vitest';
import {
getEffectiveModel,
resolveModel,
resolveClassifierModel,
isGemini2Model,
DEFAULT_GEMINI_MODEL,
@@ -38,69 +38,69 @@ describe('supportsMultimodalFunctionResponse', () => {
});
});
describe('getEffectiveModel', () => {
describe('delegation to resolveModel', () => {
describe('resolveModel', () => {
describe('delegation logic', () => {
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);
});
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);
});
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,
);
expect(getEffectiveModel(DEFAULT_GEMINI_FLASH_MODEL, false)).toBe(
expect(resolveModel(DEFAULT_GEMINI_FLASH_MODEL, false)).toBe(
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,
);
});
it('should return a custom model name when requested', () => {
const customModel = 'custom-model-v1';
const model = getEffectiveModel(customModel, false);
const model = resolveModel(customModel, false);
expect(model).toBe(customModel);
});
describe('with preview features', () => {
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
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);
});
});

View File

@@ -33,7 +33,7 @@ export const DEFAULT_GEMINI_EMBEDDING_MODEL = 'gemini-embedding-001';
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.
*
* @param requestedModel The model alias or concrete model name requested by the user.
@@ -100,21 +100,6 @@ export function resolveClassifierModel(
}
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(
model: string,
previewFeaturesEnabled: boolean = false,

View File

@@ -390,7 +390,7 @@ export class GeminiClient {
}
}
private _getEffectiveModelForCurrentTurn(): string {
private _getActiveModelForCurrentTurn(): string {
if (this.currentSequenceModel) {
return this.currentSequenceModel;
}
@@ -460,7 +460,7 @@ export class GeminiClient {
}
// 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.
// 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.
// Compression occurs before we choose a model, so calling `count_tokens`
// 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(
this.getChat(),

View File

@@ -23,7 +23,7 @@ import { InstallationManager } from '../utils/installationManager.js';
import { FakeContentGenerator } from './fakeContentGenerator.js';
import { parseCustomHeaders } from '../utils/customHeaderUtils.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.
@@ -117,7 +117,7 @@ export async function createContentGenerator(
return FakeContentGenerator.fromFile(gcConfig.fakeResponses);
}
const version = await getVersion();
const model = getEffectiveModel(
const model = resolveModel(
gcConfig.getModel(),
gcConfig.getPreviewFeatures(),
);

View File

@@ -25,18 +25,6 @@ export async function handleFallback(
failedModel: string,
authType?: string,
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> {
if (authType !== AuthType.LOGIN_WITH_GOOGLE) {
return null;

View File

@@ -7,8 +7,8 @@
import type { Config } from '../../config/config.js';
import {
DEFAULT_GEMINI_MODEL_AUTO,
getEffectiveModel,
PREVIEW_GEMINI_MODEL_AUTO,
resolveModel,
} from '../../config/models.js';
import type { BaseLlmClient } from '../../core/baseLlmClient.js';
import type {
@@ -39,7 +39,7 @@ export class OverrideStrategy implements RoutingStrategy {
// Return the overridden model name.
return {
model: getEffectiveModel(overrideModel, config.getPreviewFeatures()),
model: resolveModel(overrideModel, config.getPreviewFeatures()),
metadata: {
source: this.name,
latencyMs: 0,