Skip to content

Commit 733b11e

Browse files
author
Wian van der Merwe
committed
feat(usage): report Pi and OpenCode upstream accounts
1 parent 5ef5685 commit 733b11e

17 files changed

Lines changed: 1305 additions & 21 deletions

File tree

‎apps/mobile/src/features/usage/UsageLimitsSection.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type ProviderConsumeResetCreditOutcome,
77
type ProviderConsumeResetCreditInput,
88
type ServerProvider,
9+
type ServerProviderMonetaryUsage,
910
type ServerProviderResetCredits,
1011
type ServerProviderUsageWindow,
1112
} from "@t3tools/contracts";
@@ -17,6 +18,7 @@ import {
1718
paceRemainingPercent,
1819
remainingPercent,
1920
} from "@t3tools/shared/usageLimits";
21+
import { formatUsd } from "@t3tools/shared/usageFormat";
2022
import { type ReactNode, useContext, useRef, useState } from "react";
2123
import { Atom } from "effect/reactivity";
2224
import { Linking, Pressable, View } from "react-native";
@@ -38,6 +40,18 @@ import { usageProviderKind, useProviderColors } from "./usageProviders";
3840

3941
const PACE_LABEL = { ahead: "ahead of pace", on: "on pace", under: "under pace" } as const;
4042

43+
function monetaryUsageText(monetary: ServerProviderMonetaryUsage | undefined): string | null {
44+
if (!monetary) return null;
45+
const text = [
46+
monetary.usedUsd !== undefined ? `${formatUsd(monetary.usedUsd)} used` : null,
47+
monetary.remainingUsd !== undefined ? `${formatUsd(monetary.remainingUsd)} left` : null,
48+
monetary.limitUsd !== undefined ? `${formatUsd(monetary.limitUsd)} limit` : null,
49+
]
50+
.filter((part) => part !== null)
51+
.join(" · ");
52+
return text === "" ? null : text;
53+
}
54+
4155
type Driver = ServerProvider["driver"];
4256

4357
function useBarColor(driver: Driver): string | null {
@@ -172,6 +186,7 @@ export function AccountLimits(props: {
172186
if (!limits) return null;
173187
const notice = limitsNotice(limits);
174188
const externalUsage = limits.externalUsage;
189+
const monetaryText = monetaryUsageText(limits.monetaryUsage);
175190
const padding = dense ? "px-4 py-3" : "p-4";
176191
return (
177192
<View
@@ -203,6 +218,9 @@ export function AccountLimits(props: {
203218
))}
204219
</View>
205220
)}
221+
{monetaryText ? (
222+
<Text className="text-xs tabular-nums text-foreground-tertiary">{monetaryText}</Text>
223+
) : null}
206224
{externalUsage ? (
207225
<Pressable
208226
accessibilityRole="link"

‎apps/web/src/components/usage/UsageLimitsPooled.tsx‎

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
paceRemainingPercent,
1515
remainingPercent,
1616
} from "@t3tools/shared/usageLimits";
17+
import { formatUsd } from "@t3tools/shared/usageFormat";
1718
import type { Popover as PopoverPrimitive } from "@base-ui/react/popover";
1819
import { AlertTriangleIcon, ExternalLinkIcon, TicketIcon } from "lucide-react";
1920
import {
@@ -132,6 +133,27 @@ function Row({ label, children }: { readonly label: string; readonly children: R
132133
);
133134
}
134135

136+
type MonetaryUsage = NonNullable<LimitAccount["limits"]["monetaryUsage"]>;
137+
138+
function monetaryUsageOf(limits: LimitAccount["limits"]): MonetaryUsage | null {
139+
const monetary = limits.monetaryUsage;
140+
if (!monetary) return null;
141+
const { usedUsd, remainingUsd, limitUsd } = monetary;
142+
return usedUsd === undefined && remainingUsd === undefined && limitUsd === undefined
143+
? null
144+
: monetary;
145+
}
146+
147+
function monetaryUsageText(monetary: MonetaryUsage): string {
148+
return [
149+
monetary.usedUsd !== undefined ? `${formatUsd(monetary.usedUsd)} used` : null,
150+
monetary.remainingUsd !== undefined ? `${formatUsd(monetary.remainingUsd)} left` : null,
151+
monetary.limitUsd !== undefined ? `${formatUsd(monetary.limitUsd)} limit` : null,
152+
]
153+
.filter((part) => part !== null)
154+
.join(" · ");
155+
}
156+
135157
function SegmentPopover({
136158
account,
137159
window,
@@ -157,6 +179,7 @@ function SegmentPopover({
157179
: account.sourceLabel;
158180
const credits =
159181
redeem && account.limits.resetCredits?.availableCount ? account.limits.resetCredits : null;
182+
const monetary = monetaryUsageOf(account.limits);
160183
return (
161184
<div className="flex w-72 max-w-[calc(100vw-3rem)] flex-col gap-2.5 text-xs">
162185
<div className="flex min-w-0 flex-col gap-0.5">
@@ -182,6 +205,19 @@ function SegmentPopover({
182205
<Row label={account.environments.length > 0 ? "Signed in" : "Via"}>{where}</Row>
183206
) : null}
184207
</div>
208+
{monetary ? (
209+
<div className="flex flex-col gap-1 border-t border-border/60 pt-2.5">
210+
{monetary.usedUsd !== undefined ? (
211+
<Row label="Used">{formatUsd(monetary.usedUsd)}</Row>
212+
) : null}
213+
{monetary.remainingUsd !== undefined ? (
214+
<Row label="Remaining">{formatUsd(monetary.remainingUsd)}</Row>
215+
) : null}
216+
{monetary.limitUsd !== undefined ? (
217+
<Row label="Limit">{formatUsd(monetary.limitUsd)}</Row>
218+
) : null}
219+
</div>
220+
) : null}
185221
<div className="flex flex-col gap-1 border-t border-border/60 pt-2.5">
186222
<Row label="Left">{remaining}%</Row>
187223
{window.resetsAt ? (
@@ -568,6 +604,26 @@ function PoolWindowCard({
568604
);
569605
}
570606

607+
function PoolMonetaryAccounts({ pool }: { readonly pool: LimitPool }) {
608+
const rows = pool.accounts.flatMap((account) => {
609+
const monetary = monetaryUsageOf(account.limits);
610+
return monetary ? [{ account, monetary }] : [];
611+
});
612+
return (
613+
<div className="flex flex-col gap-2 rounded-lg border border-border/60 p-4">
614+
<span className="text-xs text-muted-foreground">No rate-limit windows reported</span>
615+
{rows.map(({ account, monetary }) => (
616+
<span key={account.key} className="flex items-baseline justify-between gap-3 text-xs">
617+
<AccountName account={account} className="min-w-0 truncate font-medium text-foreground" />
618+
<span className="shrink-0 text-foreground tabular-nums">
619+
{monetaryUsageText(monetary)}
620+
</span>
621+
</span>
622+
))}
623+
</div>
624+
);
625+
}
626+
571627
function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) {
572628
const color = barColor(pool.driver);
573629
const label = providerClients.get(pool.driver)?.label ?? String(pool.driver);
@@ -584,19 +640,24 @@ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: nu
584640
/>
585641
{label}
586642
</h2>
587-
{windows.map((window) => {
588-
const details = pool.driver === "cursor" ? cursorUsageWindowDetails(window.id) : undefined;
589-
return (
590-
<PoolWindowCard
591-
key={`${window.kind}:${window.id}`}
592-
pool={window}
593-
color={color}
594-
now={now}
595-
label={details?.label}
596-
description={details?.description}
597-
/>
598-
);
599-
})}
643+
{windows.length === 0 ? (
644+
<PoolMonetaryAccounts pool={pool} />
645+
) : (
646+
windows.map((window) => {
647+
const details =
648+
pool.driver === "cursor" ? cursorUsageWindowDetails(window.id) : undefined;
649+
return (
650+
<PoolWindowCard
651+
key={`${window.kind}:${window.id}`}
652+
pool={window}
653+
color={color}
654+
now={now}
655+
label={details?.label}
656+
description={details?.description}
657+
/>
658+
);
659+
})
660+
)}
600661
</section>
601662
);
602663
}

‎docs/user/usage.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ or endpoint configurations do not report subscription limits.
144144
API-key accounts may not report subscription limits. This also applies to Claude connections
145145
using a proxy through `ANTHROPIC_AUTH_TOKEN`.
146146

147+
Pi and OpenCode can reuse the credentials their CLIs already save to report account limits. Kimi Code
148+
shows its subscription windows, and OpenRouter shows credit and pay-as-you-go amounts, including
149+
a limit only when the provider reports one. Providers without a supported reporting API do not
150+
show online limits; their transcript token and cost history still appears under **Tokens** and
151+
**Cost**.
152+
147153
## Connect a CLIProxyAPI hub
148154

149155
To see pooled accounts, open **Settings → Providers → Usage providers → Add hub**. Choose the

‎packages/contracts/src/providerUsageLimits.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,21 @@ export const ServerProviderResetCredits = Schema.Struct({
2828
});
2929
export type ServerProviderResetCredits = typeof ServerProviderResetCredits.Type;
3030

31+
/** Normalized pay-as-you-go / credit spend in USD; distinct from reset credits. */
32+
export const ServerProviderMonetaryUsage = Schema.Struct({
33+
usedUsd: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),
34+
remainingUsd: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),
35+
limitUsd: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),
36+
});
37+
export type ServerProviderMonetaryUsage = typeof ServerProviderMonetaryUsage.Type;
38+
3139
/** Clear bars for unavailable accounts; retain last good bars after a failed probe. */
3240
export const ServerProviderUsageLimits = Schema.Struct({
3341
checkedAt: IsoDateTime,
3442
windows: ForwardCompatibleArray(ServerProviderUsageWindow),
3543
credentialFingerprint: Schema.optional(TrimmedNonEmptyString),
3644
resetCredits: Schema.optional(ServerProviderResetCredits),
45+
monetaryUsage: Schema.optional(ServerProviderMonetaryUsage),
3746
externalUsage: Schema.optional(
3847
Schema.Struct({
3948
label: TrimmedNonEmptyString,

‎packages/contracts/src/server.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import { EditorId, FileManagerRevealKind, RemoteOpenTarget } from "./editor.ts";
2626
import { ModelCapabilities, ProviderOptionDescriptor } from "./model.ts";
2727
import { RuntimeMode } from "./providerPolicy.ts";
2828
import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
29-
import { ServerProviderUsageLimits, UsageLimitSourceSnapshots } from "./providerUsageLimits.ts";
29+
import {
30+
ServerProviderUsageLimits,
31+
UsageLimitSourceAccount,
32+
UsageLimitSourceSnapshots,
33+
} from "./providerUsageLimits.ts";
3034
import { ServerSettings } from "./settings.ts";
3135
import { DesktopUpdateChannelSchema } from "./ipc.ts";
3236

@@ -261,6 +265,9 @@ export const ServerProvider = Schema.Struct({
261265
skills: Schema.Array(ServerProviderSkill).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
262266
workspaceSnapshots: Schema.optionalKey(Schema.Array(ServerProviderWorkspaceSnapshot)),
263267
usageLimits: Schema.optional(ServerProviderUsageLimits),
268+
// One CLI provider may expose several authenticated upstream accounts; kept
269+
// separate so clients that predate it never replace `usageLimits` handling.
270+
usageAccounts: Schema.optional(ForwardCompatibleArray(UsageLimitSourceAccount)),
264271
versionAdvisory: Schema.optionalKey(ServerProviderVersionAdvisory),
265272
compatibilityAdvisory: Schema.optionalKey(ServerProviderCompatibilityAdvisory),
266273
updateState: Schema.optionalKey(ServerProviderUpdateState),

‎packages/provider-core/package.json‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
"types": "./src/server/instanceIdentity.ts",
5656
"import": "./src/server/instanceIdentity.ts"
5757
},
58+
"./server/kimiUsage": {
59+
"types": "./src/server/kimiUsage.ts",
60+
"import": "./src/server/kimiUsage.ts"
61+
},
5862
"./server/maintenance": {
5963
"types": "./src/server/maintenance.ts",
6064
"import": "./src/server/maintenance.ts"
@@ -91,6 +95,10 @@
9195
"types": "./src/server/notification.ts",
9296
"import": "./src/server/notification.ts"
9397
},
98+
"./server/openRouterUsage": {
99+
"types": "./src/server/openRouterUsage.ts",
100+
"import": "./src/server/openRouterUsage.ts"
101+
},
94102
"./server/orchestrationInstructions": {
95103
"types": "./src/server/orchestrationInstructions.ts",
96104
"import": "./src/server/orchestrationInstructions.ts"
@@ -179,6 +187,10 @@
179187
"types": "./src/server/threadTitleContext.ts",
180188
"import": "./src/server/threadTitleContext.ts"
181189
},
190+
"./server/upstreamUsage": {
191+
"types": "./src/server/upstreamUsage.ts",
192+
"import": "./src/server/upstreamUsage.ts"
193+
},
182194
"./server/usage": {
183195
"types": "./src/server/usage.ts",
184196
"import": "./src/server/usage.ts"

0 commit comments

Comments
 (0)