Files
OmniRoute/open-sse/services/usage/agentrouter.ts
Diego Rodrigues de Sa e Souza e667ab12d1 fix(usage): wire agentrouter balance quota into dashboard Quota UI (#10078) (#10472)
* fix(usage): wire agentrouter balance quota into dashboard Quota UI (#10078)

* fix(usage): render AgentRouter wallet balance as USD in the Quota UI (#10078)

The prior fix wired AgentRouter's balance into getUsageForProvider() and
USAGE_SUPPORTED_PROVIDERS, but the actual dollar figure never reached the
Dashboard Quota UI: quotas.balance.remaining carried a synthetic two-state
percent (100/0) instead of the real dollarBalance, and the Provider Limits
renderer only formats a row as "$X.XX" when isCredits/currency/creditCount
are set, which the generic quota-parsing path never sets. A configured
balance rendered as a bare "100% left" percentage, not USD.

Shape quotas.balance.remaining as the real USD amount (clamped to 0) and add
an agentrouter branch to quotaParsing.ts that builds a credits-style row
(same buildCreditsQuota() pattern as DeepSeek/Claude extra-usage), so a
configured balance shows a currency-formatted dollar amount and an
exhausted balance always renders as exactly $0.00.

---------

Co-authored-by: adevwithpurpose <adevwithpurpose@users.noreply.github.com>
2026-08-18 10:50:35 -03:00

73 lines
2.9 KiB
TypeScript

/**
* usage/agentrouter.ts — AgentRouter (New-API) balance quota shapes the Provider
* Limits dashboard expects.
*
* Reuses the already-registered preflight/monitor fetcher (OpenAI-style routing
* apiKey vs console System Access Token + New-Api-User id) instead of re-implementing
* the HTTP call, so the 60s in-memory cache in agentrouterQuotaFetcher.ts is shared.
*
* AgentRouter exposes a raw New-API credit balance, not a real grant to divide by —
* so, following the DeepSeek boolean-availability precedent, `remainingPercentage` is
* only a two-state signal (100 = has balance, 0 = exhausted) used for the quota-card
* bar color. The human-meaningful number — the actual USD balance (rawQuota /
* QUOTA_PER_UNIT) — MUST travel inside `quotas.balance.remaining` so the Dashboard
* Quota UI's credits-row renderer (quotaParsing.ts::parseAgentrouterQuota, which reads
* `quota.remaining`/`quota.currency`) can format it with a currency symbol instead of
* dropping it: `getUsageForProvider()`'s top-level `remainingUsd`/`availableUsd`/
* `balance` sibling fields exist for API/CLI consumers only — parseQuotaData() (the
* Dashboard renderer) never reads them, only `data.quotas` (#10078 follow-up).
*/
import { fetchAgentrouterQuota, type AgentrouterQuota } from "../agentrouterQuotaFetcher.ts";
import { type UsageQuota } from "./quota.ts";
type JsonRecord = Record<string, unknown>;
/**
* AgentRouter balance → dashboard usage shape.
*
* Returns `{ message }` when the fetch returns null (no console credentials, an
* upstream error, or a rejected token), which the Provider Limits UI renders as a
* graceful per-row status instead of crashing the whole page. Otherwise shapes the
* balance into a single USD `quotas.balance` entry whose `remaining` field carries
* the exact dollar amount (never negative, exactly 0 when the wallet is exhausted).
*/
export async function getAgentrouterUsage(
connectionId: string | undefined,
connection: JsonRecord
) {
const quota = (await fetchAgentrouterQuota(
connectionId || "",
connection
)) as AgentrouterQuota | null;
if (!quota) {
return {
message:
"AgentRouter balance not available. Add the Console API Key + New-API User ID to the connection to view usage.",
};
}
// `dollarBalance` is already `rawQuota / QUOTA_PER_UNIT` (agentrouterQuotaFetcher.ts);
// clamp defensively so an exhausted/mis-parsed wallet never surfaces as negative.
const remainingUsd = Math.max(0, quota.dollarBalance);
const remainingPercentage = quota.limitReached ? 0 : 100;
const balance: UsageQuota = {
used: 0,
total: 0,
remaining: remainingUsd,
remainingPercentage,
resetAt: quota.resetAt ?? null,
unlimited: true,
currency: "USD",
displayName: "Wallet Balance (USD)",
};
return {
plan: "AgentRouter",
quotas: { balance },
remainingUsd,
availableUsd: remainingUsd,
balance: remainingUsd,
};
}