Files
paseo/plugins/opencode-go-usage-source/server/usage.ts
T
Mohamed BoudraandJiayang Wu 4842f7e24d fix(usage): support OpenCode v2 usageSession, OpenCode session scopes, and Codex opencode.db logins (#11153)
* fix(usage): support OpenCode v2 usageSession, session scopes, and Codex opencode.db logins

- Implement usageSession() on OpenCodeV2Session with provider "opencode",
  authoritative model tracking (info.model falling back to config.model),
  effective provider + launch harness environment, and a stable sessionKey.
- Read active and migrated OpenAI OAuth logins from OpenCode v2's opencode.db
  in codex-usage-source (honoring absolute/relative OPENCODE_DB, active=1 or
  migrated active IS NULL, falling back to v1 auth.json, and normalizing
  provider to "opencode").
- Add OpenCode session-scope discovery to built-in usage sources OpenCode
  serves (opencode-go, minimax, zai, kimi, grok, copilot, openrouter-host).

Co-authored-by: Jiayang Wu <651475459@qq.com>

* fix(usage): keep Copilot/Kimi default visibility hints and deduplicate session discovery helpers

---------

Co-authored-by: Jiayang Wu <651475459@qq.com>
2026-11-28 14:22:19 +07:00

183 lines
6.8 KiB
TypeScript

import { dirname } from "node:path";
import { z } from "zod";
import {
hashAccountKey,
unavailable,
type UsageAccount,
type UsageScope,
toneFromUsedPct,
windowFromUsedPct,
type UsageReport,
} from "@getpaseo/plugin/server/usage";
import type { Input } from "../shared/input.js";
import { NO_SUBSCRIPTION, readConsoleGo } from "./console.js";
import {
installedOpenCode,
loginStore,
readLogins,
type GoCredential,
type OpenCodeInstall,
} from "./opencode.js";
const windowSchema = z.object({
status: z.enum(["ok", "rate-limited"]),
percent: z.number().finite(),
resetsAt: z.iso.datetime(),
});
const responseSchema = z.object({
usage: z.object({ rolling: windowSchema, weekly: windowSchema, monthly: windowSchema }),
});
export async function discover(install = installedOpenCode()): Promise<UsageAccount[]> {
const store = await loginStore(install);
const logins = await readLogins(install, store);
if (!logins.opencode && !logins.go) return [];
// One OpenCode data directory holds one Go login, whichever store it lives in. Usage settings
// are saved per account, so a login OpenCode 2 imports keeps the identity it had in auth.json.
return [{ key: hashAccountKey(install.authFile), input: { store } }];
}
export async function discoverSession(
scope: Extract<UsageScope, { kind: "session" }>,
install?: OpenCodeInstall,
): Promise<UsageAccount[]> {
if (scope.provider !== "opencode" || !scope.model?.startsWith("opencode-go/")) return [];
const target = install ?? installedOpenCode(scope.env);
const accounts = await discover(target);
if (!accounts.length || install) return accounts;
const defaultInstall = installedOpenCode();
const dataHome =
target.authFile !== defaultInstall.authFile ? dirname(dirname(target.authFile)) : undefined;
const database =
scope.env.OPENCODE_DB && scope.env.OPENCODE_DB !== process.env.OPENCODE_DB
? scope.env.OPENCODE_DB
: undefined;
if (!dataHome && !database) return accounts;
const scoped: UsageAccount[] = [];
for (const account of accounts) {
const base = account.input as Input;
const input: Input = { store: base.store };
if (dataHome) input.dataHome = dataHome;
if (database) input.database = database;
scoped.push({
key: account.key,
...(account.label ? { label: account.label } : {}),
input,
});
}
return scoped;
}
export async function fetchUsage(
input: Input,
fetchApi: typeof fetch = fetch,
install?: OpenCodeInstall,
): Promise<UsageReport> {
const target = install ?? installedOpenCode(envWithInput(process.env, input));
const logins = await readLogins(target, input.store);
const only = logins.opencode ?? logins.go;
if (!only) throw new Error("OpenCode Go login no longer exists");
if (logins.opencode && logins.go) return readEitherLogin(logins.opencode, logins.go, fetchApi);
return readCredential(only, fetchApi, { workspaceKey: only === logins.opencode });
}
function envWithInput(env: NodeJS.ProcessEnv, input: Input): NodeJS.ProcessEnv {
if (!input.dataHome && !input.database) return env;
return {
...env,
...(input.dataHome ? { XDG_DATA_HOME: input.dataHome } : {}),
...(input.database ? { OPENCODE_DB: input.database } : {}),
};
}
/**
* OpenCode serves Go with the `opencode` credential whenever that Console workspace has Go, so
* the card follows it. The `opencode-go` key serves Go when the Console refuses that credential.
* A temporary failure keeps OpenCode on the `opencode` credential, so it throws instead.
*/
async function readEitherLogin(
opencode: GoCredential,
go: GoCredential,
fetchApi: typeof fetch,
): Promise<UsageReport> {
if (opencode.type === "console") {
const signIn = await readSignIn(opencode, fetchApi);
if (signIn.status !== "unavailable") return signIn;
const fallback = await readCredential(go, fetchApi);
// A connected Console account explains a rejected fallback key better than the key does.
return isRejected(fallback) ? signIn : fallback;
}
const primary = await readCredential(opencode, fetchApi, { workspaceKey: true });
return primary.status === "unavailable" ? readCredential(go, fetchApi) : primary;
}
function readCredential(
credential: GoCredential,
fetchApi: typeof fetch,
options: { workspaceKey?: boolean } = {},
): Promise<UsageReport> {
return credential.type === "console"
? readSignIn(credential, fetchApi)
: readKey(credential.key, fetchApi, options);
}
async function readSignIn(
signIn: Extract<GoCredential, { type: "console" }>,
fetchApi: typeof fetch,
): Promise<UsageReport> {
// OpenCode refreshes the sign-in itself; reading never competes with it.
if (signIn.expires > 0 && signIn.expires <= Date.now())
return unavailable({
kind: "expired",
expiresAt: new Date(signIn.expires).toISOString(),
refreshedBy: "opencode",
});
return readConsoleGo({ token: signIn.access, orgID: signIn.orgID }, fetchApi);
}
/**
* A key reads the Console meters first, since the usage API floors each percent. Any Console
* failure falls through to the usage API, which decides what the key's errors mean. The Console
* gets a short budget so the usage API still answers within the daemon's 20 second refresh.
*/
async function readKey(
key: string,
fetchApi: typeof fetch,
{ workspaceKey = false }: { workspaceKey?: boolean } = {},
): Promise<UsageReport> {
const fromConsole = await readConsoleGo({ token: key }, fetchApi, 3_000).catch(() => null);
if (fromConsole?.status === "available") return fromConsole;
const response = await fetchApi("https://opencode.ai/zen/go/v1/usage", {
headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
signal: AbortSignal.timeout(15_000),
});
// The usage API accepts any key of a Console workspace and refuses one without Go.
if (response.status === 403 && workspaceKey) return NO_SUBSCRIPTION;
if (response.status === 401 || response.status === 403)
return unavailable({ kind: "rejected", status: response.status, refreshedBy: "opencode" });
if (!response.ok) throw new Error(`OpenCode Go usage API returned ${response.status}`);
const data = responseSchema.parse(await response.json());
const windows = (
[
// The rolling window's length is not reported, so its percent stands without a name.
["rolling", "Rolling", "", data.usage.rolling],
["weekly", "Weekly", "wk", data.usage.weekly],
["monthly", "Monthly", "mo", data.usage.monthly],
] as const
).map(([id, label, shortLabel, value]) =>
windowFromUsedPct({
id,
label,
shortLabel,
utilizationPct: value.percent,
resetsAt: value.resetsAt,
tone: toneFromUsedPct(value.percent),
}),
);
return { status: "available", planLabel: "Go", windows };
}
function isRejected(report: UsageReport): boolean {
return report.status === "unavailable" && report.problem.kind === "rejected";
}