2025-06-09 15:14:06 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2025-08-26 00:04:53 +02:00
|
|
|
import type {
|
2025-06-26 08:27:20 -07:00
|
|
|
ClientMetadata,
|
|
|
|
|
GeminiUserTier,
|
|
|
|
|
LoadCodeAssistResponse,
|
|
|
|
|
OnboardUserRequest,
|
|
|
|
|
} from './types.js';
|
2025-08-26 00:04:53 +02:00
|
|
|
import { UserTierId } from './types.js';
|
2025-06-12 18:00:17 -07:00
|
|
|
import { CodeAssistServer } from './server.js';
|
2025-10-27 16:05:11 -04:00
|
|
|
import type { AuthClient } from 'google-auth-library';
|
2025-06-09 15:14:06 -07:00
|
|
|
|
2025-06-26 08:27:20 -07:00
|
|
|
export class ProjectIdRequiredError extends Error {
|
|
|
|
|
constructor() {
|
|
|
|
|
super(
|
2025-10-09 16:12:54 +05:30
|
|
|
'This account requires setting the GOOGLE_CLOUD_PROJECT or GOOGLE_CLOUD_PROJECT_ID env var. See https://goo.gle/gemini-cli-auth-docs#workspace-gca',
|
2025-06-26 08:27:20 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-21 13:44:43 -07:00
|
|
|
export interface UserData {
|
|
|
|
|
projectId: string;
|
|
|
|
|
userTier: UserTierId;
|
2026-01-23 16:03:53 -05:00
|
|
|
userTierName?: string;
|
2025-07-21 13:44:43 -07:00
|
|
|
}
|
|
|
|
|
|
2025-06-10 16:00:13 -07:00
|
|
|
/**
|
|
|
|
|
*
|
2025-08-25 16:16:30 -07:00
|
|
|
* @param projectId the user's project id, if any
|
|
|
|
|
* @returns the user's actual project id
|
2025-06-10 16:00:13 -07:00
|
|
|
*/
|
2025-10-27 16:05:11 -04:00
|
|
|
export async function setupUser(client: AuthClient): Promise<UserData> {
|
2025-10-09 16:12:54 +05:30
|
|
|
const projectId =
|
|
|
|
|
process.env['GOOGLE_CLOUD_PROJECT'] ||
|
|
|
|
|
process.env['GOOGLE_CLOUD_PROJECT_ID'] ||
|
|
|
|
|
undefined;
|
2026-01-23 16:03:53 -05:00
|
|
|
const caServer = new CodeAssistServer(
|
|
|
|
|
client,
|
|
|
|
|
projectId,
|
|
|
|
|
{},
|
|
|
|
|
'',
|
|
|
|
|
undefined,
|
|
|
|
|
undefined,
|
|
|
|
|
);
|
2025-08-13 14:04:58 -07:00
|
|
|
const coreClientMetadata: ClientMetadata = {
|
2025-06-09 15:14:06 -07:00
|
|
|
ideType: 'IDE_UNSPECIFIED',
|
|
|
|
|
platform: 'PLATFORM_UNSPECIFIED',
|
|
|
|
|
pluginType: 'GEMINI',
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-25 16:16:30 -07:00
|
|
|
const loadRes = await caServer.loadCodeAssist({
|
|
|
|
|
cloudaicompanionProject: projectId,
|
|
|
|
|
metadata: {
|
|
|
|
|
...coreClientMetadata,
|
|
|
|
|
duetProject: projectId,
|
|
|
|
|
},
|
|
|
|
|
});
|
2025-06-09 15:14:06 -07:00
|
|
|
|
2025-08-13 14:04:58 -07:00
|
|
|
if (loadRes.currentTier) {
|
|
|
|
|
if (!loadRes.cloudaicompanionProject) {
|
|
|
|
|
if (projectId) {
|
|
|
|
|
return {
|
|
|
|
|
projectId,
|
|
|
|
|
userTier: loadRes.currentTier.id,
|
2026-01-23 16:03:53 -05:00
|
|
|
userTierName: loadRes.currentTier.name,
|
2025-08-13 14:04:58 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
throw new ProjectIdRequiredError();
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
projectId: loadRes.cloudaicompanionProject,
|
|
|
|
|
userTier: loadRes.currentTier.id,
|
2026-01-23 16:03:53 -05:00
|
|
|
userTierName: loadRes.currentTier.name,
|
2025-08-13 14:04:58 -07:00
|
|
|
};
|
2025-06-26 08:27:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const tier = getOnboardTier(loadRes);
|
2025-06-12 19:32:13 -07:00
|
|
|
|
2025-08-13 14:04:58 -07:00
|
|
|
let onboardReq: OnboardUserRequest;
|
2025-08-25 16:16:30 -07:00
|
|
|
if (tier.id === UserTierId.FREE) {
|
2025-08-13 14:04:58 -07:00
|
|
|
// The free tier uses a managed google cloud project. Setting a project in the `onboardUser` request causes a `Precondition Failed` error.
|
|
|
|
|
onboardReq = {
|
|
|
|
|
tierId: tier.id,
|
|
|
|
|
cloudaicompanionProject: undefined,
|
|
|
|
|
metadata: coreClientMetadata,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
onboardReq = {
|
|
|
|
|
tierId: tier.id,
|
|
|
|
|
cloudaicompanionProject: projectId,
|
|
|
|
|
metadata: {
|
|
|
|
|
...coreClientMetadata,
|
|
|
|
|
duetProject: projectId,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-06-26 08:27:20 -07:00
|
|
|
|
|
|
|
|
let lroRes = await caServer.onboardUser(onboardReq);
|
2026-01-07 00:38:59 +05:30
|
|
|
if (!lroRes.done && lroRes.name) {
|
|
|
|
|
const operationName = lroRes.name;
|
|
|
|
|
while (!lroRes.done) {
|
|
|
|
|
await new Promise((f) => setTimeout(f, 5000));
|
|
|
|
|
lroRes = await caServer.getOperation(operationName);
|
|
|
|
|
}
|
2025-06-26 08:27:20 -07:00
|
|
|
}
|
2025-08-11 11:04:44 -07:00
|
|
|
|
2025-08-13 14:04:58 -07:00
|
|
|
if (!lroRes.response?.cloudaicompanionProject?.id) {
|
|
|
|
|
if (projectId) {
|
|
|
|
|
return {
|
|
|
|
|
projectId,
|
|
|
|
|
userTier: tier.id,
|
2026-01-23 16:03:53 -05:00
|
|
|
userTierName: tier.name,
|
2025-08-13 14:04:58 -07:00
|
|
|
};
|
|
|
|
|
}
|
2025-08-11 11:04:44 -07:00
|
|
|
throw new ProjectIdRequiredError();
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-25 16:16:30 -07:00
|
|
|
return {
|
2025-08-13 14:04:58 -07:00
|
|
|
projectId: lroRes.response.cloudaicompanionProject.id,
|
2025-07-21 13:44:43 -07:00
|
|
|
userTier: tier.id,
|
2026-01-23 16:03:53 -05:00
|
|
|
userTierName: tier.name,
|
2025-07-21 13:44:43 -07:00
|
|
|
};
|
2025-06-26 08:27:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getOnboardTier(res: LoadCodeAssistResponse): GeminiUserTier {
|
|
|
|
|
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
|
|
|
}
|
2025-06-26 08:27:20 -07:00
|
|
|
return {
|
|
|
|
|
name: '',
|
|
|
|
|
description: '',
|
|
|
|
|
id: UserTierId.LEGACY,
|
|
|
|
|
userDefinedCloudaicompanionProject: true,
|
|
|
|
|
};
|
2025-06-09 15:14:06 -07:00
|
|
|
}
|