Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

plan-usage.ts stays within its size: compact the OpenCode helpers

milind-soni66582fc2f2ed
diff --git a/scripts/testing/file-size-ratchet.test.ts b/scripts/testing/file-size-ratchet.test.ts
index 40a364e9d6..70b9209d59 100644
--- a/scripts/testing/file-size-ratchet.test.ts
+++ b/scripts/testing/file-size-ratchet.test.ts
@@ -39,7 +39,7 @@ const LINES: Record<string, number> = {
"scripts/mcp-server.ts": 1414,
"server/boat.ts": 1258,
"server/drivers/pi.ts": 1036,
- "server/plan-usage.ts": 1050,
+ "server/plan-usage.ts": 1046,
"server/container-computer.ts": 1225,
"src/components/ModelPicker.tsx": 1046,
"src/components/PhoneSetupFlow.tsx": 1146,
diff --git a/server/plan-usage.ts b/server/plan-usage.ts
index 9b0df5e38e..648bad6b0f 100644
--- a/server/plan-usage.ts
+++ b/server/plan-usage.ts
@@ -863,11 +863,7 @@ function readGrokCredential(
}
/** The key saved in Settings, or the login `opencode auth login` keeps. `auth.json` is where OpenCode v1 wrote one; v2 keeps credentials in its own database, so this path covers installs migrated from v1 (the saved key covers v2). */
-function readOpenCodeCredential(
- env: NodeJS.ProcessEnv,
- readText: (path: string) => string | null,
- now: number,
-): PlanCredential {
+function readOpenCodeCredential(env: NodeJS.ProcessEnv, readText: (path: string) => string | null, now: number): PlanCredential {
const key = secretString(env.OPENCODE_API_KEY);
if (key) return { token: key, accountId: null, expired: false };
const raw = env.OPENCODE_AUTH_CONTENT?.trim() || openCodeAuthText(env, readText);
@@ -890,10 +886,7 @@ function openCodeAuthText(env: NodeJS.ProcessEnv, readText: (path: string) => st
process.platform === "darwin" ? join(home, "Library", "Application Support") : "",
process.platform === "win32" ? env.LOCALAPPDATA || join(home, "AppData", "Local") : "",
].filter(Boolean);
- for (const root of roots) {
- const text = readText(join(root, "opencode", "auth.json"));
- if (text) return text;
- }
+ for (const root of roots) { const text = readText(join(root, "opencode", "auth.json")); if (text) return text; }
return null;
}
function errorRow(account: PlanAccount, error: string): PlanProviderRow {
@@ -1042,16 +1035,11 @@ async function fetchGrok(account: PlanAccount, token: string, deps: PlanUsageDep
/** Go's allowance, read with the account's own key or login. A key without a Go subscription answers 403. */
async function fetchOpenCode(account: PlanAccount, token: string, deps: PlanUsageDeps): Promise<PlanProviderRow> {
- const result = await fetchJson(deps.fetch, OPENCODE_GO_USAGE_URL, {
- Authorization: `Bearer ${token}`,
- Accept: "application/json",
- }, deps.timeoutMs ?? PROVIDER_TIMEOUT_MS);
+ const result = await fetchJson(deps.fetch, OPENCODE_GO_USAGE_URL, { Authorization: `Bearer ${token}`, Accept: "application/json" }, deps.timeoutMs ?? PROVIDER_TIMEOUT_MS);
// 403 is Go's own answer for a key without a Go subscription: the account works and has no allowance to report.
if (result.status === 403) return okRow(account, { plan: null, fiveHour: closedWindow(), weekly: closedWindow(), extra: [], models: [] });
if (authRejected(result.status)) return errorRow(account, "OpenCode refused the saved credentials");
- if (!result.ok || result.body === null || typeof result.body !== "object") {
- return errorRow(account, result.status === 0 || result.status >= 500 ? couldNotReach("opencode") : unexpectedUsage("opencode"));
- }
+ if (!result.ok || result.body === null || typeof result.body !== "object") return errorRow(account, result.status === 0 || result.status >= 500 ? couldNotReach("opencode") : unexpectedUsage("opencode"));
const windows = parseOpenCodeUsage(result.body);
// A 200 with no meter at all is not an allowance: it is an answer this row cannot read.
return windows.plan ? okRow(account, windows) : errorRow(account, unexpectedUsage("opencode"));