summaryrefslogtreecommitdiff
path: root/packages/core/src
diff options
context:
space:
mode:
authorSandy Tao <[email protected]>2025-07-15 21:13:30 -0700
committerGitHub <[email protected]>2025-07-16 04:13:30 +0000
commitcba272082d15a6b9fb4e21bc27ed1d56fa5b9a56 (patch)
treebd0e54879334819c823e5a892d1709066ed6618a /packages/core/src
parentd622e596a14338771bdd43a8812ff7fc02f7ebe8 (diff)
Run model availability check in the background to speed up startup (#4256)
Diffstat (limited to 'packages/core/src')
-rw-r--r--packages/core/src/config/config.test.ts6
-rw-r--r--packages/core/src/config/config.ts4
-rw-r--r--packages/core/src/core/contentGenerator.test.ts16
-rw-r--r--packages/core/src/core/contentGenerator.ts16
4 files changed, 25 insertions, 17 deletions
diff --git a/packages/core/src/config/config.test.ts b/packages/core/src/config/config.test.ts
index bb074a71..e34880a6 100644
--- a/packages/core/src/config/config.test.ts
+++ b/packages/core/src/config/config.test.ts
@@ -151,14 +151,12 @@ describe('Server Config (config.ts)', () => {
apiKey: 'test-key',
};
- (createContentGeneratorConfig as Mock).mockResolvedValue(
- mockContentConfig,
- );
+ (createContentGeneratorConfig as Mock).mockReturnValue(mockContentConfig);
await config.refreshAuth(authType);
expect(createContentGeneratorConfig).toHaveBeenCalledWith(
- MODEL, // Should be called with the original model 'gemini-pro'
+ config,
authType,
);
// Verify that contentGeneratorConfig is updated with the new model
diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts
index 268871ca..59f1e1ba 100644
--- a/packages/core/src/config/config.ts
+++ b/packages/core/src/config/config.ts
@@ -274,8 +274,8 @@ export class Config {
}
async refreshAuth(authMethod: AuthType) {
- this.contentGeneratorConfig = await createContentGeneratorConfig(
- this.model,
+ this.contentGeneratorConfig = createContentGeneratorConfig(
+ this,
authMethod,
);
diff --git a/packages/core/src/core/contentGenerator.test.ts b/packages/core/src/core/contentGenerator.test.ts
index 92144aa4..c50678af 100644
--- a/packages/core/src/core/contentGenerator.test.ts
+++ b/packages/core/src/core/contentGenerator.test.ts
@@ -64,12 +64,18 @@ describe('createContentGenerator', () => {
describe('createContentGeneratorConfig', () => {
const originalEnv = process.env;
+ const mockConfig = {
+ getModel: vi.fn().mockReturnValue('gemini-pro'),
+ setModel: vi.fn(),
+ flashFallbackHandler: vi.fn(),
+ } as unknown as Config;
beforeEach(() => {
// Reset modules to re-evaluate imports and environment variables
vi.resetModules();
// Restore process.env before each test
process.env = { ...originalEnv };
+ vi.clearAllMocks();
});
afterAll(() => {
@@ -80,7 +86,7 @@ describe('createContentGeneratorConfig', () => {
it('should configure for Gemini using GEMINI_API_KEY when set', async () => {
process.env.GEMINI_API_KEY = 'env-gemini-key';
const config = await createContentGeneratorConfig(
- undefined,
+ mockConfig,
AuthType.USE_GEMINI,
);
expect(config.apiKey).toBe('env-gemini-key');
@@ -90,7 +96,7 @@ describe('createContentGeneratorConfig', () => {
it('should not configure for Gemini if GEMINI_API_KEY is empty', async () => {
process.env.GEMINI_API_KEY = '';
const config = await createContentGeneratorConfig(
- undefined,
+ mockConfig,
AuthType.USE_GEMINI,
);
expect(config.apiKey).toBeUndefined();
@@ -100,7 +106,7 @@ describe('createContentGeneratorConfig', () => {
it('should configure for Vertex AI using GOOGLE_API_KEY when set', async () => {
process.env.GOOGLE_API_KEY = 'env-google-key';
const config = await createContentGeneratorConfig(
- undefined,
+ mockConfig,
AuthType.USE_VERTEX_AI,
);
expect(config.apiKey).toBe('env-google-key');
@@ -111,7 +117,7 @@ describe('createContentGeneratorConfig', () => {
process.env.GOOGLE_CLOUD_PROJECT = 'env-gcp-project';
process.env.GOOGLE_CLOUD_LOCATION = 'env-gcp-location';
const config = await createContentGeneratorConfig(
- undefined,
+ mockConfig,
AuthType.USE_VERTEX_AI,
);
expect(config.vertexai).toBe(true);
@@ -123,7 +129,7 @@ describe('createContentGeneratorConfig', () => {
process.env.GOOGLE_CLOUD_PROJECT = '';
process.env.GOOGLE_CLOUD_LOCATION = '';
const config = await createContentGeneratorConfig(
- undefined,
+ mockConfig,
AuthType.USE_VERTEX_AI,
);
expect(config.apiKey).toBeUndefined();
diff --git a/packages/core/src/core/contentGenerator.ts b/packages/core/src/core/contentGenerator.ts
index d3434c23..109c0ffc 100644
--- a/packages/core/src/core/contentGenerator.ts
+++ b/packages/core/src/core/contentGenerator.ts
@@ -52,17 +52,17 @@ export type ContentGeneratorConfig = {
authType?: AuthType | undefined;
};
-export async function createContentGeneratorConfig(
- model: string | undefined,
+export function createContentGeneratorConfig(
+ config: Config,
authType: AuthType | undefined,
-): Promise<ContentGeneratorConfig> {
+): ContentGeneratorConfig {
const geminiApiKey = process.env.GEMINI_API_KEY || undefined;
const googleApiKey = process.env.GOOGLE_API_KEY || undefined;
const googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT || undefined;
const googleCloudLocation = process.env.GOOGLE_CLOUD_LOCATION || undefined;
// Use runtime model from config if available, otherwise fallback to parameter or default
- const effectiveModel = model || DEFAULT_GEMINI_MODEL;
+ const effectiveModel = config.getModel() || DEFAULT_GEMINI_MODEL;
const contentGeneratorConfig: ContentGeneratorConfig = {
model: effectiveModel,
@@ -80,10 +80,14 @@ export async function createContentGeneratorConfig(
if (authType === AuthType.USE_GEMINI && geminiApiKey) {
contentGeneratorConfig.apiKey = geminiApiKey;
contentGeneratorConfig.vertexai = false;
- contentGeneratorConfig.model = await getEffectiveModel(
+ getEffectiveModel(
contentGeneratorConfig.apiKey,
contentGeneratorConfig.model,
- );
+ ).then((newModel) => {
+ if (newModel !== contentGeneratorConfig.model) {
+ config.flashFallbackHandler?.(contentGeneratorConfig.model, newModel);
+ }
+ });
return contentGeneratorConfig;
}