Files
paseo/plugins/opencode-go-usage-source/server/console.ts
T
Mohamed Boudra 065e4a95b1 Show OpenCode Go usage for the credential OpenCode serves Go with (#10612)
* fix(usage): follow the OpenCode credential that serves Go

OpenCode 2 serves OpenCode Go with the active credential on the
opencode integration when that Console workspace has Go, and uses the
opencode-go key only otherwise. The usage card now reads the same
credential: a Console sign-in (with its organization) first, then a key
on opencode, then the opencode-go key. Keys read the Console Go status
before the usage API, which floors each percent.

Refs #6428

* fix(usage): keep OpenCode Go usage on the sign-in through outages

A Console outage no longer shows the fallback key's usage, and a slow
Console probe no longer uses up the refresh deadline for a working key.

* fix(usage): skip OpenCode keys saved for another Console

A key on the opencode integration can name its own Console server.
Usage now leaves it alone instead of sending it to opencode.ai.
2026-11-23 23:07:16 +07:00

88 lines
2.9 KiB
TypeScript

import { z } from "zod";
import {
toneFromUsedPct,
unavailable,
windowFromUsedPct,
type UsageReport,
type UsageWindow,
} from "@getpaseo/plugin/server/usage";
// The endpoint the Console's own Go page reads. It is not publicly documented.
const STATUS_URL = "https://opencode.ai/console/api/go/status";
const GO_PRODUCTS = new Set(["go", "go-plus", "go-teams"]);
export const NO_SUBSCRIPTION = unavailable({
kind: "no_quota",
detail: "No active OpenCode Go subscription",
});
const amount = z.union([z.string(), z.number()]).transform(Number);
const meterSchema = z.object({
limitMicroCents: amount,
usedMicroCents: amount,
resetsAt: z.iso.datetime(),
});
const statusSchema = z.object({
product: z.string().nullish(),
// An ended or refunded subscription keeps its product and sends no access.
access: z
.object({
meters: z.object({
fiveHour: z.unknown().optional(),
week: z.unknown().optional(),
month: z.unknown().optional(),
}),
})
.nullish(),
});
const METERS = [
["fiveHour", "rolling", "Rolling", ""],
["week", "weekly", "Weekly", "wk"],
["month", "monthly", "Monthly", "mo"],
] as const;
/**
* Reads the Go meters the Console shows. A sign-in names its organization; an API key belongs to
* one workspace and sends none. Throws on failures that say nothing about the login.
*/
export async function readConsoleGo(
login: { token: string; orgID?: string },
fetchApi: typeof fetch,
timeoutMs = 15_000,
): Promise<UsageReport> {
const response = await fetchApi(STATUS_URL, {
headers: {
Authorization: `Bearer ${login.token}`,
Accept: "application/json",
...(login.orgID ? { "x-org-id": login.orgID } : {}),
},
// The token goes to the Console origin only.
redirect: "error",
signal: AbortSignal.timeout(timeoutMs),
});
if (response.status === 401 || response.status === 403)
return unavailable({ kind: "rejected", status: response.status, refreshedBy: "opencode" });
if (!response.ok) throw new Error(`OpenCode Console Go status returned ${response.status}`);
const status = statusSchema.parse(await response.json());
if (!status.product || !GO_PRODUCTS.has(status.product) || !status.access) return NO_SUBSCRIPTION;
const windows = METERS.flatMap(([meter, id, label, shortLabel]): UsageWindow[] => {
const parsed = meterSchema.safeParse(status.access?.meters[meter]);
if (!parsed.success) return [];
const { limitMicroCents: limit, usedMicroCents: used, resetsAt } = parsed.data;
if (!(limit > 0) || !(used >= 0)) return [];
const usedPct = Math.min(100, (used / limit) * 100);
return [
windowFromUsedPct({
id,
label,
shortLabel,
utilizationPct: usedPct,
resetsAt,
tone: toneFromUsedPct(usedPct),
}),
];
});
if (!windows.length) throw new Error("OpenCode Console Go status has no readable meters");
return { status: "available", planLabel: "Go", windows };
}