Check if a user has access to preview model (#87)

This commit is contained in:
Sehoon Shon
2025-12-16 13:50:44 -05:00
committed by Tommaso Sciortino
parent d591140f62
commit ae5068b8cb
8 changed files with 307 additions and 31 deletions
+109 -1
View File
@@ -34,7 +34,11 @@ import { logRipgrepFallback } from '../telemetry/loggers.js';
import { RipgrepFallbackEvent } from '../telemetry/types.js';
import { ToolRegistry } from '../tools/tool-registry.js';
import { DEFAULT_MODEL_CONFIGS } from './defaultModelConfigs.js';
import { DEFAULT_GEMINI_MODEL } from './models.js';
import {
DEFAULT_GEMINI_MODEL,
DEFAULT_GEMINI_MODEL_AUTO,
PREVIEW_GEMINI_MODEL,
} from './models.js';
vi.mock('fs', async (importOriginal) => {
const actual = await importOriginal<typeof import('fs')>();
@@ -1736,3 +1740,107 @@ describe('Availability Service Integration', () => {
expect(spy).toHaveBeenCalled();
});
});
describe('Config Quota & Preview Model Access', () => {
let config: Config;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let mockCodeAssistServer: any;
const baseParams: ConfigParameters = {
cwd: '/tmp',
targetDir: '/tmp',
debugMode: false,
sessionId: 'test-session',
model: 'gemini-pro',
usageStatisticsEnabled: false,
embeddingModel: 'gemini-embedding', // required in type but not in the original file I copied, adding here
sandbox: {
command: 'docker',
image: 'gemini-cli-sandbox',
},
};
beforeEach(() => {
vi.clearAllMocks();
mockCodeAssistServer = {
projectId: 'test-project',
retrieveUserQuota: vi.fn(),
};
vi.mocked(getCodeAssistServer).mockReturnValue(mockCodeAssistServer);
config = new Config(baseParams);
});
describe('refreshUserQuota', () => {
it('should update hasAccessToPreviewModel to true if quota includes preview model', async () => {
mockCodeAssistServer.retrieveUserQuota.mockResolvedValue({
buckets: [{ modelId: PREVIEW_GEMINI_MODEL }],
});
await config.refreshUserQuota();
expect(config.getHasAccessToPreviewModel()).toBe(true);
});
it('should update hasAccessToPreviewModel to false if quota does not include preview model', async () => {
mockCodeAssistServer.retrieveUserQuota.mockResolvedValue({
buckets: [{ modelId: 'some-other-model' }],
});
await config.refreshUserQuota();
expect(config.getHasAccessToPreviewModel()).toBe(false);
});
it('should update hasAccessToPreviewModel to false if buckets are undefined', async () => {
mockCodeAssistServer.retrieveUserQuota.mockResolvedValue({});
await config.refreshUserQuota();
expect(config.getHasAccessToPreviewModel()).toBe(false);
});
it('should return undefined and not update if codeAssistServer is missing', async () => {
vi.mocked(getCodeAssistServer).mockReturnValue(undefined);
const result = await config.refreshUserQuota();
expect(result).toBeUndefined();
expect(config.getHasAccessToPreviewModel()).toBe(false);
});
it('should return undefined if retrieveUserQuota fails', async () => {
mockCodeAssistServer.retrieveUserQuota.mockRejectedValue(
new Error('Network error'),
);
const result = await config.refreshUserQuota();
expect(result).toBeUndefined();
// Should remain default (false)
expect(config.getHasAccessToPreviewModel()).toBe(false);
});
});
describe('setPreviewFeatures', () => {
it('should reset model to default auto if disabling preview features while using a preview model', () => {
config.setPreviewFeatures(true);
config.setModel(PREVIEW_GEMINI_MODEL);
config.setPreviewFeatures(false);
expect(config.getModel()).toBe(DEFAULT_GEMINI_MODEL_AUTO);
});
it('should NOT reset model if disabling preview features while NOT using a preview model', () => {
config.setPreviewFeatures(true);
const nonPreviewModel = 'gemini-1.5-pro';
config.setModel(nonPreviewModel);
config.setPreviewFeatures(false);
expect(config.getModel()).toBe(nonPreviewModel);
});
it('should NOT reset model if enabling preview features', () => {
config.setPreviewFeatures(false);
config.setModel(PREVIEW_GEMINI_MODEL); // Just pretending it was set somehow
config.setPreviewFeatures(true);
expect(config.getModel()).toBe(PREVIEW_GEMINI_MODEL);
});
});
});
+55
View File
@@ -48,8 +48,10 @@ import { tokenLimit } from '../core/tokenLimits.js';
import {
DEFAULT_GEMINI_EMBEDDING_MODEL,
DEFAULT_GEMINI_FLASH_MODEL,
DEFAULT_GEMINI_MODEL_AUTO,
DEFAULT_THINKING_MODE,
isPreviewModel,
PREVIEW_GEMINI_MODEL,
} from './models.js';
import { shouldAttemptBrowserLaunch } from '../utils/browser.js';
import type { MCPOAuthConfig } from '../mcp/oauth-provider.js';
@@ -81,6 +83,7 @@ import { PolicyEngine } from '../policy/policy-engine.js';
import type { PolicyEngineConfig } from '../policy/types.js';
import { HookSystem } from '../hooks/index.js';
import type { UserTierId } from '../code_assist/types.js';
import type { RetrieveUserQuotaResponse } from '../code_assist/types.js';
import { getCodeAssistServer } from '../code_assist/codeAssist.js';
import type { Experiments } from '../code_assist/experiments/experiments.js';
import { AgentRegistry } from '../agents/registry.js';
@@ -379,6 +382,7 @@ export class Config {
private readonly bugCommand: BugCommandSettings | undefined;
private model: string;
private previewFeatures: boolean | undefined;
private hasAccessToPreviewModel: boolean = false;
private readonly noBrowser: boolean;
private readonly folderTrust: boolean;
private ideMode: boolean;
@@ -732,6 +736,10 @@ export class Config {
const codeAssistServer = getCodeAssistServer(this);
if (codeAssistServer) {
if (codeAssistServer.projectId) {
await this.refreshUserQuota();
}
this.experimentsPromise = getExperiments(codeAssistServer)
.then((experiments) => {
this.setExperiments(experiments);
@@ -753,8 +761,21 @@ export class Config {
this.experimentsPromise = undefined;
}
const authType = this.contentGeneratorConfig.authType;
if (
authType === AuthType.USE_GEMINI ||
authType === AuthType.USE_VERTEX_AI
) {
this.setHasAccessToPreviewModel(true);
}
// Reset the session flag since we're explicitly changing auth and using default model
this.inFallbackMode = false;
// Update model if user no longer has access to the preview model
if (!this.hasAccessToPreviewModel && isPreviewModel(this.model)) {
this.setModel(DEFAULT_GEMINI_MODEL_AUTO);
}
}
async getExperimentsAsync(): Promise<Experiments | undefined> {
@@ -948,9 +969,43 @@ export class Config {
}
setPreviewFeatures(previewFeatures: boolean) {
// If it's using a preview model and it's turning off previewFeatures,
// switch the model to the default auto mode.
if (this.previewFeatures && !previewFeatures) {
if (isPreviewModel(this.getModel())) {
this.setModel(DEFAULT_GEMINI_MODEL_AUTO);
}
}
this.previewFeatures = previewFeatures;
}
getHasAccessToPreviewModel(): boolean {
return this.hasAccessToPreviewModel;
}
setHasAccessToPreviewModel(hasAccess: boolean): void {
this.hasAccessToPreviewModel = hasAccess;
}
async refreshUserQuota(): Promise<RetrieveUserQuotaResponse | undefined> {
const codeAssistServer = getCodeAssistServer(this);
if (!codeAssistServer || !codeAssistServer.projectId) {
return undefined;
}
try {
const quota = await codeAssistServer.retrieveUserQuota({
project: codeAssistServer.projectId,
});
const hasAccess =
quota.buckets?.some((b) => b.modelId === PREVIEW_GEMINI_MODEL) ?? false;
this.setHasAccessToPreviewModel(hasAccess);
return quota;
} catch (e) {
debugLogger.debug('Failed to retrieve user quota', e);
return undefined;
}
}
getCoreTools(): string[] | undefined {
return this.coreTools;
}