Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions packages/cli/src/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1773,7 +1773,7 @@ describe('loadCliConfig model selection', () => {
});

it('always prefers model from argv', async () => {
process.argv = ['node', 'script.js', '--model', 'gemini-2.5-flash-preview'];
process.argv = ['node', 'script.js', '--model', 'gemini-2.5-flash'];
const argv = await parseArguments(createTestMergedSettings());
const config = await loadCliConfig(
createTestMergedSettings({
Expand All @@ -1785,11 +1785,11 @@ describe('loadCliConfig model selection', () => {
argv,
);

expect(config.getModel()).toBe('gemini-2.5-flash-preview');
expect(config.getModel()).toBe('gemini-2.5-flash');
});

it('selects the model from argv if provided', async () => {
process.argv = ['node', 'script.js', '--model', 'gemini-2.5-flash-preview'];
process.argv = ['node', 'script.js', '--model', 'gemini-2.5-flash'];
const argv = await parseArguments(createTestMergedSettings());
const config = await loadCliConfig(
createTestMergedSettings({
Expand All @@ -1799,7 +1799,7 @@ describe('loadCliConfig model selection', () => {
argv,
);

expect(config.getModel()).toBe('gemini-2.5-flash-preview');
expect(config.getModel()).toBe('gemini-2.5-flash');
});

it('selects the default auto model if provided via auto alias', async () => {
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
type HierarchicalMemory,
coreEvents,
GEMINI_MODEL_ALIAS_AUTO,
isValidModelOrAlias,
getValidModelsAndAliases,
getAdminErrorMessage,
isHeadlessMode,
Config,
Expand Down Expand Up @@ -664,6 +666,18 @@ export async function loadCliConfig(
const specifiedModel =
argv.model || process.env['GEMINI_MODEL'] || settings.model?.name;

// Validate the model if one was explicitly specified
if (specifiedModel && specifiedModel !== GEMINI_MODEL_ALIAS_AUTO) {
if (!isValidModelOrAlias(specifiedModel)) {
const validModels = getValidModelsAndAliases();

throw new FatalConfigError(
`Invalid model: "${specifiedModel}"\n\n` +
`Valid models and aliases:\n${validModels.map((m) => ` - ${m}`).join('\n')}\n\n` +
`Use /model to switch models interactively.`,
);
}
}
const resolvedModel =
specifiedModel === GEMINI_MODEL_ALIAS_AUTO
? defaultModel
Expand Down
64 changes: 64 additions & 0 deletions packages/core/src/config/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
GEMINI_MODEL_ALIAS_PRO,
GEMINI_MODEL_ALIAS_FLASH,
GEMINI_MODEL_ALIAS_AUTO,
GEMINI_MODEL_ALIAS_FLASH_LITE,
PREVIEW_GEMINI_FLASH_MODEL,
PREVIEW_GEMINI_MODEL_AUTO,
DEFAULT_GEMINI_MODEL_AUTO,
Expand All @@ -30,6 +31,10 @@
PREVIEW_GEMINI_3_1_CUSTOM_TOOLS_MODEL,
isPreviewModel,
isProModel,
isValidModelOrAlias,
getValidModelsAndAliases,
VALID_GEMINI_MODELS,
VALID_ALIASES,
} from './models.js';

describe('isPreviewModel', () => {
Expand Down Expand Up @@ -168,7 +173,7 @@
expect(supportsMultimodalFunctionResponse('gemini-3-pro')).toBe(true);
});

it('should return false for gemini-2 models', () => {

Check warning on line 176 in packages/core/src/config/models.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-2". Please make sure this change is appropriate to submit.
expect(supportsMultimodalFunctionResponse('gemini-2.5-pro')).toBe(false);
expect(supportsMultimodalFunctionResponse('gemini-2.5-flash')).toBe(false);
});
Expand Down Expand Up @@ -389,3 +394,62 @@
).toBe(false);
});
});

describe('isValidModelOrAlias', () => {
it('should return true for valid model names', () => {
expect(isValidModelOrAlias(DEFAULT_GEMINI_MODEL)).toBe(true);
expect(isValidModelOrAlias(PREVIEW_GEMINI_MODEL)).toBe(true);
expect(isValidModelOrAlias(DEFAULT_GEMINI_FLASH_MODEL)).toBe(true);
expect(isValidModelOrAlias(DEFAULT_GEMINI_FLASH_LITE_MODEL)).toBe(true);
expect(isValidModelOrAlias(PREVIEW_GEMINI_FLASH_MODEL)).toBe(true);
expect(isValidModelOrAlias(PREVIEW_GEMINI_3_1_MODEL)).toBe(true);
expect(isValidModelOrAlias(PREVIEW_GEMINI_3_1_CUSTOM_TOOLS_MODEL)).toBe(
true,
);
});

it('should return true for valid aliases', () => {
expect(isValidModelOrAlias(GEMINI_MODEL_ALIAS_AUTO)).toBe(true);
expect(isValidModelOrAlias(GEMINI_MODEL_ALIAS_PRO)).toBe(true);
expect(isValidModelOrAlias(GEMINI_MODEL_ALIAS_FLASH)).toBe(true);
expect(isValidModelOrAlias(GEMINI_MODEL_ALIAS_FLASH_LITE)).toBe(true);
expect(isValidModelOrAlias(PREVIEW_GEMINI_MODEL_AUTO)).toBe(true);
expect(isValidModelOrAlias(DEFAULT_GEMINI_MODEL_AUTO)).toBe(true);
});

it('should return true for custom (non-gemini) models', () => {
expect(isValidModelOrAlias('gpt-4')).toBe(true);
expect(isValidModelOrAlias('claude-3')).toBe(true);
expect(isValidModelOrAlias('my-custom-model')).toBe(true);
});

it('should return false for invalid gemini model names', () => {
expect(isValidModelOrAlias('gemini-4-pro')).toBe(false);

Check warning on line 427 in packages/core/src/config/models.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-4". Please make sure this change is appropriate to submit.
expect(isValidModelOrAlias('gemini-99-flash')).toBe(false);

Check warning on line 428 in packages/core/src/config/models.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-99". Please make sure this change is appropriate to submit.
expect(isValidModelOrAlias('gemini-invalid')).toBe(false);
});
});

describe('getValidModelsAndAliases', () => {
it('should return a sorted array', () => {
const result = getValidModelsAndAliases();
const sorted = [...result].sort();
expect(result).toEqual(sorted);
});

it('should include all valid models and aliases', () => {
const result = getValidModelsAndAliases();
for (const model of VALID_GEMINI_MODELS) {
expect(result).toContain(model);
}
for (const alias of VALID_ALIASES) {
expect(result).toContain(alias);
}
});

it('should not contain duplicates', () => {
const result = getValidModelsAndAliases();
const unique = [...new Set(result)];
expect(result).toEqual(unique);
});
});
43 changes: 43 additions & 0 deletions packages/core/src/config/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

export const PREVIEW_GEMINI_MODEL = 'gemini-3-pro-preview';
export const PREVIEW_GEMINI_3_1_MODEL = 'gemini-3.1-pro-preview';

Check warning on line 8 in packages/core/src/config/models.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-3.1". Please make sure this change is appropriate to submit.
export const PREVIEW_GEMINI_3_1_CUSTOM_TOOLS_MODEL =
'gemini-3.1-pro-preview-customtools';

Check warning on line 10 in packages/core/src/config/models.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-3.1". Please make sure this change is appropriate to submit.
export const PREVIEW_GEMINI_FLASH_MODEL = 'gemini-3-flash-preview';
export const DEFAULT_GEMINI_MODEL = 'gemini-2.5-pro';
export const DEFAULT_GEMINI_FLASH_MODEL = 'gemini-2.5-flash';
Expand All @@ -32,6 +32,15 @@
export const GEMINI_MODEL_ALIAS_FLASH = 'flash';
export const GEMINI_MODEL_ALIAS_FLASH_LITE = 'flash-lite';

export const VALID_ALIASES = new Set([
GEMINI_MODEL_ALIAS_AUTO,
GEMINI_MODEL_ALIAS_PRO,
GEMINI_MODEL_ALIAS_FLASH,
GEMINI_MODEL_ALIAS_FLASH_LITE,
PREVIEW_GEMINI_MODEL_AUTO,
DEFAULT_GEMINI_MODEL_AUTO,
]);

export const DEFAULT_GEMINI_EMBEDDING_MODEL = 'gemini-embedding-001';

// Cap the thinking at 8192 to prevent run-away thinking loops.
Expand Down Expand Up @@ -201,7 +210,7 @@
* @returns True if the model is a Gemini-2.x model.
*/
export function isGemini2Model(model: string): boolean {
return /^gemini-2(\.|$)/.test(model);

Check warning on line 213 in packages/core/src/config/models.ts

View workflow job for this annotation

GitHub Actions / Lint

Found sensitive keyword "gemini-2". Please make sure this change is appropriate to submit.
}

/**
Expand Down Expand Up @@ -283,3 +292,37 @@
);
}
}

/**
* Checks if the model name is valid (either a valid model or a valid alias).
*
* @param model The model name to check.
* @returns True if the model is valid.
*/
export function isValidModelOrAlias(model: string): boolean {
// Check if it's a valid alias
if (VALID_ALIASES.has(model)) {
return true;
}

// Check if it's a valid model name
if (VALID_GEMINI_MODELS.has(model)) {
return true;
}

// Allow custom models (non-gemini models)
if (!model.startsWith('gemini-')) {
return true;
}

return false;
}

/**
* Gets a list of all valid model names and aliases for error messages.
*
* @returns Array of valid model names and aliases.
*/
export function getValidModelsAndAliases(): string[] {
return [...new Set([...VALID_ALIASES, ...VALID_GEMINI_MODELS])].sort();
}
Loading