Files
gemini-cli/packages/core/src/code_assist/setup.ts

94 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-06-09 15:14:06 -07:00
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
ClientMetadata,
GeminiUserTier,
LoadCodeAssistResponse,
OnboardUserRequest,
UserTierId,
} from './types.js';
2025-06-12 18:00:17 -07:00
import { CodeAssistServer } from './server.js';
import { OAuth2Client } from 'google-auth-library';
2025-06-09 15:14:06 -07:00
export class ProjectIdRequiredError extends Error {
constructor() {
super(
'This account requires setting the GOOGLE_CLOUD_PROJECT env var. See https://goo.gle/gemini-cli-auth-docs#workspace-gca',
);
}
}
export interface UserData {
projectId: string;
userTier: UserTierId;
}
2025-06-10 16:00:13 -07:00
/**
*
* @param projectId the user's project id, if any
* @returns the user's actual project id
*/
export async function setupUser(client: OAuth2Client): Promise<UserData> {
let projectId = process.env.GOOGLE_CLOUD_PROJECT || undefined;
2025-08-01 14:37:56 -05:00
const caServer = new CodeAssistServer(client, projectId, {}, '', undefined);
2025-06-09 15:14:06 -07:00
const clientMetadata: ClientMetadata = {
ideType: 'IDE_UNSPECIFIED',
platform: 'PLATFORM_UNSPECIFIED',
pluginType: 'GEMINI',
duetProject: projectId,
2025-06-09 15:14:06 -07:00
};
2025-06-12 18:00:17 -07:00
const loadRes = await caServer.loadCodeAssist({
cloudaicompanionProject: projectId,
2025-06-10 16:00:13 -07:00
metadata: clientMetadata,
});
2025-06-09 15:14:06 -07:00
if (!projectId && loadRes.cloudaicompanionProject) {
projectId = loadRes.cloudaicompanionProject;
}
const tier = getOnboardTier(loadRes);
if (tier.userDefinedCloudaicompanionProject && !projectId) {
throw new ProjectIdRequiredError();
}
2025-06-10 16:00:13 -07:00
const onboardReq: OnboardUserRequest = {
tierId: tier.id,
cloudaicompanionProject: projectId,
2025-06-09 15:14:06 -07:00
metadata: clientMetadata,
};
// Poll onboardUser until long running operation is complete.
let lroRes = await caServer.onboardUser(onboardReq);
while (!lroRes.done) {
await new Promise((f) => setTimeout(f, 5000));
lroRes = await caServer.onboardUser(onboardReq);
}
return {
projectId: lroRes.response?.cloudaicompanionProject?.id || '',
userTier: tier.id,
};
}
function getOnboardTier(res: LoadCodeAssistResponse): GeminiUserTier {
if (res.currentTier) {
return res.currentTier;
}
for (const tier of res.allowedTiers || []) {
if (tier.isDefault) {
return tier;
2025-06-11 13:26:41 -07:00
}
2025-06-09 15:14:06 -07:00
}
return {
name: '',
description: '',
id: UserTierId.LEGACY,
userDefinedCloudaicompanionProject: true,
};
2025-06-09 15:14:06 -07:00
}