Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): source Gemini config from code, not env

David Le69a2d5aadeaf
diff --git a/plans/manual-test-plan-2026-06-18.md b/plans/manual-test-plan-2026-06-18.md
index f4e92120..573eb7fa 100644
--- a/plans/manual-test-plan-2026-06-18.md
+++ b/plans/manual-test-plan-2026-06-18.md
@@ -47,7 +47,6 @@ Do this once before the runs below.
- `BROWSER_USE_API_KEY` (starts with `bu_`) — backend only, for `web.automate`.
- `GEMINI_API_KEY` **or** `GOOGLE_APPLICATION_CREDENTIALS_JSON` — image
generate/edit, voice Gemini fallback, site asset route.
- - Optional: `GEMINI_IMAGE_MODEL`, `GEMINI_TRANSCRIPTION_MODEL`.
- **Account/credits.** Browser automation and image generation charge real
credits — have a funded account (top up `balanceMicros` on a 402 per dev convention).
diff --git a/site/src/lib/inference/adapters/gemini-client.ts b/site/src/lib/inference/adapters/gemini-client.ts
index 75211675..dac53d42 100644
--- a/site/src/lib/inference/adapters/gemini-client.ts
+++ b/site/src/lib/inference/adapters/gemini-client.ts
@@ -21,6 +21,10 @@ export type GeminiClientFactory = (options: GoogleGenAIOptions) => GeminiClient;
const vertexLocation = "global";
const vertexAIScope = "https://www.googleapis.com/auth/cloud-platform";
+// Client-side request timeout so a stalled upstream call aborts instead of hanging
+// the caller (e.g. a harness step) forever. Generous enough to clear a slow 4K
+// image generation while still bounding a true hang.
+const requestTimeoutMs = 120_000;
export function defaultGeminiClientFactory(options: GoogleGenAIOptions): GeminiClient {
return new GoogleGenAI(options);
@@ -31,26 +35,17 @@ export function geminiClientConfig(environment: AdapterEnvironment): {
options: GoogleGenAIOptions;
service: "vertex-ai";
} {
- const apiVersion = environment.GEMINI_API_VERSION?.trim() || undefined;
- const timeout = numberFromString(environment.GEMINI_TIMEOUT_MS);
- const httpOptions: GoogleGenAIOptions["httpOptions"] | undefined =
- timeout === undefined ? undefined : { timeout };
const googleCredentials = googleCredentialsFromEnvironment(environment);
const project = googleCredentials?.project_id;
const options: GoogleGenAIOptions = {
vertexai: true,
location: vertexLocation,
+ httpOptions: { timeout: requestTimeoutMs },
};
if (project) {
options.project = project;
}
- if (apiVersion) {
- options.apiVersion = apiVersion;
- }
- if (httpOptions) {
- options.httpOptions = httpOptions;
- }
if (googleCredentials) {
options.googleAuthOptions = {
authClient: googleAuthClient(googleCredentials),
@@ -187,11 +182,3 @@ export function geminiCandidateParts(candidate: JsonObject | undefined): JsonObj
}
return candidate.content.parts.filter(isJsonObject);
}
-
-function numberFromString(value: string | undefined): number | undefined {
- if (!value?.trim()) {
- return undefined;
- }
- const number = Number(value);
- return Number.isFinite(number) ? number : undefined;
-}
diff --git a/site/src/lib/inference/adapters/gemini-image.ts b/site/src/lib/inference/adapters/gemini-image.ts
index 3ea8729d..19941d59 100644
--- a/site/src/lib/inference/adapters/gemini-image.ts
+++ b/site/src/lib/inference/adapters/gemini-image.ts
@@ -40,8 +40,7 @@ export function createGeminiImageAssetProvider(
): InferenceProvider {
const clientConfig = geminiClientConfig(environment);
const configured = clientConfig.configured;
- const defaultModel =
- environment.GEMINI_IMAGE_MODEL?.trim() || geminiModelRoles.imageGeneration;
+ const defaultModel = geminiModelRoles.imageGeneration;
async function listModels(
modalities: InferenceModality[],
@@ -77,9 +76,8 @@ export function createGeminiImageAssetProvider(
}
const model = request.model?.trim() || defaultModel;
- // Fail before spending: the GEMINI_IMAGE_MODEL override (or a caller model) may resolve to an
- // id with no configured price. The preflight catches a caller-supplied model, but not a model
- // resolved from the env default, so guard here too — never run a generation we can't price.
+ // Fail before spending: a caller-supplied model may resolve to an id with no configured price.
+ // The preflight catches that, but guard here too — never run a generation we can't price.
if (!providerCreditPricing(providerID, model)) {
throw new InferenceProviderError(
"No credit price is configured for the selected image model.",
diff --git a/site/src/lib/inference/gemini-models.ts b/site/src/lib/inference/gemini-models.ts
index ecc2437a..cb33abc4 100644
--- a/site/src/lib/inference/gemini-models.ts
+++ b/site/src/lib/inference/gemini-models.ts
@@ -11,7 +11,7 @@ export const geminiModels = {
flash: "gemini-3.5-flash",
flashLite: "gemini-3.1-flash-lite",
// Generative image editing/generation ("nano banana"). Bump here when adopting a
- // newer image model; the backend also honors a GEMINI_IMAGE_MODEL override.
+ // newer image model.
flashImage: "gemini-2.5-flash-image",
// "Nano banana pro": higher-fidelity image editing/generation that takes a real
// aspectRatio + imageSize (1K/2K/4K) via imageConfig. Gemini 3 preview models are
diff --git a/site/src/lib/inference/screenshot-parsing/gemini-flash.ts b/site/src/lib/inference/screenshot-parsing/gemini-flash.ts
index da1113e6..8b143dc1 100644
--- a/site/src/lib/inference/screenshot-parsing/gemini-flash.ts
+++ b/site/src/lib/inference/screenshot-parsing/gemini-flash.ts
@@ -40,6 +40,10 @@ const defaultScreenshotParseModel = geminiModelRoles.screenshotParse;
const defaultDebugOverlayScreenshotParseModel = geminiModelRoles.screenshotParse;
const defaultVertexLocation = "global";
const vertexAIScope = "https://www.googleapis.com/auth/cloud-platform";
+// Client-side request timeout so a stalled parse aborts instead of hanging the
+// per-step agent loop; screenshot parsing is normally a few seconds, so this only
+// trips on a true hang.
+const requestTimeoutMs = 120_000;
export function createGeminiFlashScreenshotParser(
environment: AdapterEnvironment = process.env,
@@ -51,11 +55,11 @@ export function createGeminiFlashScreenshotParser(
id: screenshotProviderID,
inferenceProvider: geminiProviderID,
configured: config.configured,
- modelForRequest: (request) => screenshotParseModelForRequest(request, environment),
+ modelForRequest: (request) => screenshotParseModelForRequest(request),
async parse(request) {
ensureConfigured(config.configured);
const client = clientFactory(config.options);
- const model = screenshotParseModelForRequest(request, environment);
+ const model = screenshotParseModelForRequest(request);
const startedAt = performance.now();
let rawResponse: unknown;
@@ -89,7 +93,7 @@ export function createGeminiFlashScreenshotParser(
async *stream(request) {
ensureConfigured(config.configured);
const client = clientFactory(config.options);
- const model = screenshotParseModelForRequest(request, environment);
+ const model = screenshotParseModelForRequest(request);
const startedAt = performance.now();
const generator = await streamGeminiContent(client, request, model);
let accumulatedText = "";
@@ -146,18 +150,12 @@ export function createGeminiFlashScreenshotParser(
};
}
-export function screenshotParseModelForRequest(
- request: ScreenshotParseRequest,
- environment: AdapterEnvironment = process.env,
-) {
+export function screenshotParseModelForRequest(request: ScreenshotParseRequest) {
if (isDebugOverlayRequest(request)) {
- return environment.GEMINI_SCREENSHOT_PARSE_DEBUG_MODEL?.trim()
- || environment.GEMINI_SCREENSHOT_PARSE_MODEL?.trim()
- || defaultDebugOverlayScreenshotParseModel;
+ return defaultDebugOverlayScreenshotParseModel;
}
- return environment.GEMINI_SCREENSHOT_PARSE_MODEL?.trim()
- || defaultScreenshotParseModel;
+ return defaultScreenshotParseModel;
}
function geminiRequestParameters(
@@ -554,10 +552,6 @@ export function geminiClientConfig(environment: AdapterEnvironment = process.env
service: "vertex-ai" | "gemini-api";
location: string;
} {
- const apiVersion = environment.GEMINI_API_VERSION?.trim() || undefined;
- const timeout = numberFromString(environment.GEMINI_TIMEOUT_MS);
- const httpOptions: GoogleGenAIOptions["httpOptions"] | undefined =
- timeout === undefined ? undefined : { timeout };
const googleCredentials = googleCredentialsFromEnvironment(environment);
const project = googleCredentials?.project_id;
const apiKey =
@@ -566,20 +560,12 @@ export function geminiClientConfig(environment: AdapterEnvironment = process.env
|| "";
if (project) {
- const location = environment.GEMINI_VERTEX_LOCATION?.trim()
- || environment.GOOGLE_VERTEX_LOCATION?.trim()
- || defaultVertexLocation;
const options: GoogleGenAIOptions = {
vertexai: true,
- location,
+ location: defaultVertexLocation,
project,
+ httpOptions: { timeout: requestTimeoutMs },
};
- if (apiVersion) {
- options.apiVersion = apiVersion;
- }
- if (httpOptions) {
- options.httpOptions = httpOptions;
- }
if (googleCredentials) {
options.googleAuthOptions = {
authClient: googleAuthClient(googleCredentials),
@@ -589,20 +575,16 @@ export function geminiClientConfig(environment: AdapterEnvironment = process.env
configured: true,
options,
service: "vertex-ai",
- location,
+ location: defaultVertexLocation,
};
}
- const options: GoogleGenAIOptions = {};
+ const options: GoogleGenAIOptions = {
+ httpOptions: { timeout: requestTimeoutMs },
+ };
if (apiKey) {
options.apiKey = apiKey;
}
- if (apiVersion) {
- options.apiVersion = apiVersion;
- }
- if (httpOptions) {
- options.httpOptions = httpOptions;
- }
return {
configured: Boolean(apiKey),
@@ -697,10 +679,3 @@ function stringValue(value: JsonValue | undefined) {
return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
-function numberFromString(value: string | undefined) {
- if (!value) {
- return undefined;
- }
- const parsed = Number(value);
- return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
-}
diff --git a/site/src/lib/inference/vertex-live.ts b/site/src/lib/inference/vertex-live.ts
index dd923683..9186f273 100644
--- a/site/src/lib/inference/vertex-live.ts
+++ b/site/src/lib/inference/vertex-live.ts
@@ -4,10 +4,10 @@ import { InferenceProviderError } from "@/lib/inference/providers";
const vertexAIScope = "https://www.googleapis.com/auth/cloud-platform";
const defaultVertexLocation = "global";
-// The backend owns the Live model id (the client never selects it). Override
-// with GEMINI_LIVE_MODEL. `gemini-live-2.5-flash` is the model that resolves on
-// Vertex AI in the `global` location and accepts the BidiGenerateContent setup
-// (verified end-to-end via GeminiLiveCommandSessionLiveSmokeTests).
+// The backend owns the Live model id (the client never selects it).
+// `gemini-live-2.5-flash` is the model that resolves on Vertex AI in the `global`
+// location and accepts the BidiGenerateContent setup (verified end-to-end via
+// GeminiLiveCommandSessionLiveSmokeTests).
const defaultLiveModel = "gemini-live-2.5-flash";
type AdapterEnvironment = Record<string, string | undefined>;
@@ -95,11 +95,8 @@ export async function mintVertexLiveConnection(
});
}
- const location =
- environment.GEMINI_VERTEX_LOCATION?.trim()
- || environment.GOOGLE_VERTEX_LOCATION?.trim()
- || defaultVertexLocation;
- const model = environment.GEMINI_LIVE_MODEL?.trim() || defaultLiveModel;
+ const location = defaultVertexLocation;
+ const model = defaultLiveModel;
const host =
location === "global"
? "aiplatform.googleapis.com"