Files
paseo/plugins/copilot-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

147 lines
4.7 KiB
TypeScript

import type { UsageInput } from "../shared/input.js";
import { promises as fs } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import { z } from "zod";
import {
unavailable,
windowFromUsedPct,
type UsageWindow,
type UsageAccount,
type UsageScope,
type UsageReport,
type UsageDetail,
} from "@getpaseo/plugin/server/usage";
const ApiOptionalStringSchema = z.preprocess(
(value) => (value == null ? undefined : value),
z.coerce.string().optional(),
);
const CopilotUsageResponseSchema = z.object({
copilot_plan: ApiOptionalStringSchema,
quota_reset_date: ApiOptionalStringSchema,
quota_reset_date_utc: ApiOptionalStringSchema,
quota_snapshots: z
.object({
premium_interactions: z
.object({
entitlement: z.number().nonnegative().nullish(),
remaining: z.number().nonnegative().nullish(),
percent_remaining: z.number().min(0).max(100).nullish(),
unlimited: z.boolean().optional(),
})
.nullish(),
})
.nullish(),
});
async function readToken(input: UsageInput): Promise<string | undefined> {
if (input.store === "env") return process.env[input.locator];
try {
const raw = await fs.readFile(input.locator, "utf8");
return raw.match(/oauth_token:\s*["']?([a-zA-Z0-9_-]+)["']?/)?.[1];
} catch {
return undefined;
}
}
const OPENCODE_COPILOT_PROVIDERS = new Set(["github-copilot", "github-copilot-enterprise"]);
async function discoverFromEnv(
env: NodeJS.ProcessEnv,
{ matchHostEnv = false }: { matchHostEnv?: boolean } = {},
): Promise<UsageAccount[]> {
const candidates: UsageInput[] = [];
for (const locator of ["COPILOT_TOKEN", "GITHUB_TOKEN", "GITHUB_PAT"]) {
const value = env[locator];
if (value && (!matchHostEnv || value === process.env[locator])) {
candidates.push({ store: "env", locator });
}
}
// File store reflects the host GitHub CLI login (matching Host -> Usage).
const home = matchHostEnv ? env.HOME || env.USERPROFILE || homedir() : homedir();
if (env.APPDATA)
candidates.push({
store: "file",
locator: join(env.APPDATA, "GitHub CLI", "hosts.yml"),
});
candidates.push({ store: "file", locator: join(home, ".config", "gh", "hosts.yml") });
for (const input of candidates) if (await readToken(input)) return [{ key: "default", input }];
return [];
}
export async function discover(): Promise<UsageAccount[]> {
return discoverFromEnv(process.env);
}
export async function discoverSession(
scope: Extract<UsageScope, { kind: "session" }>,
): Promise<UsageAccount[]> {
const modelProvider = scope.model?.includes("/") ? scope.model.split("/", 1)[0] : undefined;
const matches =
scope.provider === "copilot" ||
(scope.provider === "opencode" && OPENCODE_COPILOT_PROVIDERS.has(modelProvider ?? ""));
if (!matches) return [];
return discoverFromEnv(scope.env, { matchHostEnv: true });
}
export async function fetchUsage(
input: UsageInput,
fetchApi: typeof fetch = fetch,
): Promise<UsageReport> {
const token = await readToken(input);
if (!token) throw new Error("Copilot login store no longer exists");
const res = await fetchApi("https://api.github.com/copilot_internal/user", {
signal: AbortSignal.timeout(15_000),
headers: {
Authorization: `token ${token}`,
Accept: "application/json",
"Editor-Version": "vscode/1.96.2",
"Editor-Plugin-Version": "copilot-chat/0.26.7",
"User-Agent": "GitHubCopilotChat/0.26.7",
"X-Github-Api-Version": "2025-04-01",
},
});
if (res.status === 401 || res.status === 403)
return unavailable({ kind: "rejected", status: res.status });
if (!res.ok) throw new Error(`Copilot usage API returned ${res.status}`);
const resp = CopilotUsageResponseSchema.parse(await res.json());
const details: UsageDetail[] = resp.quota_reset_date
? [{ id: "reset", label: "Quota reset", value: resp.quota_reset_date }]
: [];
const windows: UsageWindow[] = [];
const premium = resp.quota_snapshots?.premium_interactions;
if (premium && !premium.unlimited) {
if (premium.percent_remaining != null) {
windows.push(
windowFromUsedPct({
id: "premium_interactions",
label: "Premium requests",
utilizationPct: 100 - premium.percent_remaining,
resetsAt: resp.quota_reset_date_utc || resp.quota_reset_date,
}),
);
}
if (premium.remaining != null && premium.entitlement != null) {
details.push({
id: "premium_remaining",
label: "Premium requests remaining",
value: `${premium.remaining} / ${premium.entitlement}`,
});
}
}
return {
status: "available",
planLabel: resp.copilot_plan || undefined,
windows,
balances: [],
details,
};
}