(
accessToken: string,
auth: Extract<Auth.Info, { type: "oauth" }>,
providerID: string,
)
| 1193 | } |
| 1194 | |
| 1195 | async function ensureProjectContext( |
| 1196 | accessToken: string, |
| 1197 | auth: Extract<Auth.Info, { type: "oauth" }>, |
| 1198 | providerID: string, |
| 1199 | ): Promise<string> { |
| 1200 | const configuredProjectId = |
| 1201 | process.env.GOOGLE_CLOUD_PROJECT ?? process.env.GCP_PROJECT ?? process.env.GCLOUD_PROJECT |
| 1202 | if (configuredProjectId && configuredProjectId.trim().length > 0) { |
| 1203 | await logDebug("project.configured", { projectId: configuredProjectId.trim() }) |
| 1204 | return configuredProjectId.trim() |
| 1205 | } |
| 1206 | |
| 1207 | const refreshParts = parseRefreshParts(auth.refresh) |
| 1208 | const projectId = refreshParts.projectId |
| 1209 | if (projectId || refreshParts.managedProjectId) { |
| 1210 | await logDebug("project.from_refresh", { |
| 1211 | projectId: projectId ?? null, |
| 1212 | managedProjectId: refreshParts.managedProjectId ?? null, |
| 1213 | }) |
| 1214 | return projectId || refreshParts.managedProjectId || "" |
| 1215 | } |
| 1216 | |
| 1217 | const loadPayload = await loadManagedProject(accessToken, projectId) |
| 1218 | if (loadPayload?.cloudaicompanionProject) { |
| 1219 | const updated: Extract<Auth.Info, { type: "oauth" }> = { |
| 1220 | type: "oauth", |
| 1221 | access: auth.access, |
| 1222 | refresh: formatRefreshParts({ |
| 1223 | refreshToken: refreshParts.refreshToken || auth.refresh, |
| 1224 | projectId: refreshParts.projectId, |
| 1225 | managedProjectId: loadPayload.cloudaicompanionProject, |
| 1226 | }), |
| 1227 | expires: auth.expires, |
| 1228 | } |
| 1229 | await Auth.set(providerID, updated) |
| 1230 | await logDebug("project.from_load", { managedProjectId: loadPayload.cloudaicompanionProject }) |
| 1231 | return loadPayload.cloudaicompanionProject |
| 1232 | } |
| 1233 | |
| 1234 | if (!loadPayload) { |
| 1235 | await logDebug("project.load_failed") |
| 1236 | throw new Error( |
| 1237 | "Google Gemini requires a Google Cloud project. Enable Gemini for Google Cloud API and set GOOGLE_CLOUD_PROJECT.", |
| 1238 | ) |
| 1239 | } |
| 1240 | |
| 1241 | const currentTierId = loadPayload?.currentTier?.id ?? undefined |
| 1242 | if (currentTierId && currentTierId !== "FREE") { |
| 1243 | throw new Error("Google Gemini requires a Google Cloud project for non-free tiers.") |
| 1244 | } |
| 1245 | |
| 1246 | const allowedTiers = Array.isArray(loadPayload?.allowedTiers) ? loadPayload.allowedTiers : [] |
| 1247 | let defaultTierId: string | undefined |
| 1248 | for (const tier of allowedTiers) { |
| 1249 | if (tier?.isDefault) { |
| 1250 | defaultTierId = tier.id |
| 1251 | break |
| 1252 | } |
no test coverage detected