mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-28 04:03:05 +00:00
Keep usage logins visible and explain unavailable quota (#5876)
* fix(usage): keep existing logins visible and explain unavailable quota Credential discovery must preserve accounts whose login exists even when it has expired. Keep vendor failures on the discovered account and isolate discovery failures without inventing provider cards. * test(usage): align credential route tests with account discovery
This commit is contained in:
@@ -4,6 +4,8 @@ import { join } from "node:path";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
hashAccountKey,
|
||||
unavailable,
|
||||
type UsageAccount,
|
||||
toneFromUsedPct,
|
||||
windowFromUsedPct,
|
||||
type UsageReport,
|
||||
@@ -44,23 +46,22 @@ export async function readDefaultKey(path = authPath()): Promise<string | null>
|
||||
}
|
||||
}
|
||||
|
||||
export async function discover(path = authPath()): Promise<Array<{}>> {
|
||||
return (await readDefaultKey(path)) ? [{}] : [];
|
||||
export async function discover(path = authPath()): Promise<UsageAccount[]> {
|
||||
return (await readDefaultKey(path)) ? [{ key: hashAccountKey(path), input: { path } }] : [];
|
||||
}
|
||||
|
||||
export async function fetchUsage(
|
||||
_input: Input,
|
||||
input: Input,
|
||||
fetchApi: typeof fetch = fetch,
|
||||
path = authPath(),
|
||||
): Promise<UsageReport> {
|
||||
const apiKey = await readDefaultKey(path);
|
||||
if (!apiKey) return { status: "unavailable", windows: [] };
|
||||
const apiKey = await readDefaultKey(input.path);
|
||||
if (!apiKey) throw new Error("OpenCode Go login store no longer exists");
|
||||
const response = await fetchApi("https://opencode.ai/zen/go/v1/usage", {
|
||||
headers: { Authorization: `Bearer ${apiKey}`, Accept: "application/json" },
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (response.status === 401 || response.status === 403)
|
||||
return { status: "unavailable", windows: [] };
|
||||
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 = (
|
||||
@@ -82,8 +83,3 @@ export async function fetchUsage(
|
||||
);
|
||||
return { status: "available", planLabel: "Go", windows };
|
||||
}
|
||||
|
||||
export async function identify(_input: Input, path = authPath()) {
|
||||
const key = await readDefaultKey(path);
|
||||
return key ? { key: hashAccountKey(key) } : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user