Skip to content

Commit be92f5d

Browse files
IGN-Stylyclaude
andcommitted
feat(web): show OpenRouter credit balance in Usage → Limits
There was no way to see an OpenRouter balance inside T3 Code. OpenRouter is not a driver and reports no quota, so Limits had nothing to say about it and users running agents on credits had to leave the app to check what was left. Adds `openrouter` as a second `usageLimitSources` kind, reusing that pipeline's secret storage, poll interval, and publish channel rather than building a parallel one. The server tries `/api/v1/credits` for the account balance and falls back to `/api/v1/key` only on 403, so an ordinary inference key still reports its own spend and limit. Balances render as their own section in Limits, headed by the OpenRouter mark. `UsageLimitSourceConfig` becomes a tagged union so an OpenRouter source cannot carry a hub URL. Credits are a sibling of the quota-window type, not a reinterpretation of it: a prepaid balance has no window to pool or reset to count down to. Three shared selectors treated "source with no accounts" as a fault, which would have made a healthy balance emit a spurious notice and a failing one offer /usage-limits for every driver; they now discriminate on kind. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 6c58362 commit be92f5d

15 files changed

Lines changed: 922 additions & 71 deletions

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveProviderInstanceEnabled,
77
ServerSettings,
88
ServerSettingsPatch,
9+
UsageLimitSourceId,
910
} from "@t3tools/contracts";
1011
import { createModelSelection } from "@t3tools/shared/model";
1112
import { assert, it } from "@effect/vitest";
@@ -1181,6 +1182,43 @@ it.layer(NodeServices.layer)("server settings", (it) => {
11811182
);
11821183
}
11831184

1185+
// The secret path keys off `managementKey`, which every source kind carries,
1186+
// so a credit source must travel the same route as a hub without its own code.
1187+
it.effect("stores an OpenRouter key outside settings.json and restores it on edit", () =>
1188+
Effect.gen(function* () {
1189+
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;
1190+
const serverConfig = yield* ServerConfig.ServerConfig;
1191+
const fileSystem = yield* FileSystem.FileSystem;
1192+
const sourceId = UsageLimitSourceId.make("openrouter");
1193+
1194+
const next = yield* serverSettings.updateSettings({
1195+
usageLimitSources: {
1196+
[sourceId]: { kind: "openrouter", managementKey: "sk-or-provisioning", enabled: true },
1197+
},
1198+
});
1199+
assert.equal(next.usageLimitSources[sourceId]?.kind, "openrouter");
1200+
assert.equal(next.usageLimitSources[sourceId]?.managementKey, "sk-or-provisioning");
1201+
1202+
const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
1203+
assert.notInclude(raw, "sk-or-provisioning");
1204+
1205+
// A client edits the label while echoing the redaction marker back; the
1206+
// stored key must survive rather than being overwritten with the marker.
1207+
const relabelled = yield* serverSettings.updateSettings({
1208+
usageLimitSources: {
1209+
[sourceId]: {
1210+
kind: "openrouter",
1211+
label: "Work account",
1212+
managementKey: "••••••",
1213+
enabled: true,
1214+
},
1215+
},
1216+
});
1217+
assert.equal(relabelled.usageLimitSources[sourceId]?.managementKey, "sk-or-provisioning");
1218+
assert.equal(relabelled.usageLimitSources[sourceId]?.label, "Work account");
1219+
}).pipe(Effect.provide(makeServerSettingsLayer())),
1220+
);
1221+
11841222
it.effect("stores sensitive provider instance environment values outside settings.json", () =>
11851223
Effect.gen(function* () {
11861224
const serverSettings = yield* ServerSettingsModule.ServerSettingsService;

‎apps/server/src/usage/UsageLimitSources.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import * as Stream from "effect/Stream";
3636
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
3737
import { ServerSettingsService } from "../serverSettings.ts";
3838
import { makeCliproxyApi } from "./cliproxyApi.ts";
39+
import { makeOpenRouterApi } from "./openrouterApi.ts";
3940

4041
export class UsageLimitSources extends Context.Service<
4142
UsageLimitSources,
@@ -53,6 +54,7 @@ export class UsageLimitSources extends Context.Service<
5354

5455
function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
5556
if (config.label) return config.label;
57+
if (config.kind === "openrouter") return "OpenRouter";
5658
try {
5759
return new URL(config.url).host;
5860
} catch {
@@ -63,6 +65,7 @@ function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
6365
/** @public Service construction is part of the canonical Effect module API. */
6466
export const make = Effect.gen(function* () {
6567
const api = yield* makeCliproxyApi;
68+
const openrouter = yield* makeOpenRouterApi;
6669
const settingsService = yield* ServerSettingsService;
6770
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
6871
const stateRef = yield* Ref.make<ReadonlyArray<UsageLimitSourceSnapshot>>([]);
@@ -78,7 +81,22 @@ export const make = Effect.gen(function* () {
7881
const checkedAt = DateTime.formatIso(yield* DateTime.now);
7982
const base = { id, kind: config.kind, label: sourceLabel(id, config), checkedAt } as const;
8083
if (config.managementKey.length === 0) {
81-
return { ...base, accounts: [], error: "No management key configured." };
84+
return {
85+
...base,
86+
accounts: [],
87+
error:
88+
config.kind === "openrouter" ? "No API key configured." : "No management key configured.",
89+
};
90+
}
91+
// A credit source reports money left rather than pooled accounts, so it
92+
// fills `credits` and leaves `accounts` empty.
93+
if (config.kind === "openrouter") {
94+
const credits = yield* openrouter.readCredits(config).pipe(Effect.result);
95+
if (credits._tag === "Failure") {
96+
yield* Effect.logDebug("usage limit source read failed", { id, cause: credits.failure });
97+
return { ...base, accounts: [], error: credits.failure.detail };
98+
}
99+
return { ...base, accounts: [], credits: credits.success };
82100
}
83101
const accounts = yield* api.readAccounts(config).pipe(Effect.result);
84102
if (accounts._tag === "Failure") {
@@ -129,6 +147,12 @@ export const make = Effect.gen(function* () {
129147
detail: "The usage limit source is missing or disabled.",
130148
});
131149
}
150+
// Only a hub banks redeemable reset credits; a credit balance has none.
151+
if (config.kind !== "cliproxy") {
152+
return yield* new UsageLimitSourceError({
153+
detail: "This usage limit source has no reset credits.",
154+
});
155+
}
132156
const result = yield* api.consume(config, input.accountId, input.creditId);
133157
const snapshot = yield* readSource(input.sourceId, config);
134158
const previous = yield* Ref.get(stateRef);

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
UsageLimitSourceError,
66
type ProviderConsumeResetCreditResult,
77
type UsageLimitSourceAccount,
8-
type UsageLimitSourceConfig,
8+
type CliproxyUsageLimitSourceConfig,
99
} from "@t3tools/contracts";
1010
import * as DateTime from "effect/DateTime";
1111
import * as Effect from "effect/Effect";
@@ -116,7 +116,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
116116
const client = yield* HttpClient.HttpClient;
117117

118118
const management = Effect.fn("CliproxyApi.management")(function* (
119-
config: UsageLimitSourceConfig,
119+
config: CliproxyUsageLimitSourceConfig,
120120
path: string,
121121
body?: unknown,
122122
) {
@@ -140,13 +140,15 @@ export const makeCliproxyApi = Effect.gen(function* () {
140140
return response;
141141
});
142142

143-
const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (config: UsageLimitSourceConfig) {
143+
const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (
144+
config: CliproxyUsageLimitSourceConfig,
145+
) {
144146
const response = yield* management(config, "auth-files");
145147
return (yield* decodeAuthFiles(response)).files;
146148
});
147149

148150
const apiCall = Effect.fn("CliproxyApi.apiCall")(function* (
149-
config: UsageLimitSourceConfig,
151+
config: CliproxyUsageLimitSourceConfig,
150152
account: typeof AuthFile.Type,
151153
url: string,
152154
data?: unknown,
@@ -180,7 +182,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
180182
});
181183

182184
const credits = Effect.fn("CliproxyApi.credits")(function* (
183-
config: UsageLimitSourceConfig,
185+
config: CliproxyUsageLimitSourceConfig,
184186
account: typeof AuthFile.Type,
185187
) {
186188
const body = yield* apiCall(config, account, CREDIT_URL);
@@ -197,7 +199,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
197199
});
198200

199201
const readAccount = Effect.fn("CliproxyApi.readAccount")(function* (
200-
config: UsageLimitSourceConfig,
202+
config: CliproxyUsageLimitSourceConfig,
201203
account: typeof AuthFile.Type,
202204
) {
203205
const checkedAt = DateTime.formatIso(yield* DateTime.now);
@@ -293,7 +295,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
293295
});
294296

295297
const readAccounts = Effect.fn("CliproxyApi.readAccounts")(function* (
296-
config: UsageLimitSourceConfig,
298+
config: CliproxyUsageLimitSourceConfig,
297299
): Effect.fn.Return<ReadonlyArray<UsageLimitSourceAccount>, UsageLimitSourceError> {
298300
const accounts = yield* authFiles(config).pipe(
299301
Effect.mapError(
@@ -311,7 +313,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
311313
});
312314

313315
const consume = Effect.fn("CliproxyApi.consume")(function* (
314-
config: UsageLimitSourceConfig,
316+
config: CliproxyUsageLimitSourceConfig,
315317
accountId: string,
316318
creditId: string,
317319
): Effect.fn.Return<ProviderConsumeResetCreditResult, UsageLimitSourceError> {
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
import * as Effect from "effect/Effect";
3+
import { HttpClient, HttpClientResponse } from "effect/unstable/http";
4+
5+
import { makeOpenRouterApi } from "./openrouterApi.ts";
6+
7+
const config = {
8+
kind: "openrouter",
9+
managementKey: "sk-or-secret",
10+
enabled: true,
11+
} as const;
12+
13+
/** One canned reply per endpoint path, plus the paths actually requested. */
14+
function fixture(replies: Record<string, { status: number; body: unknown }>) {
15+
const paths: string[] = [];
16+
const http = HttpClient.make((request) =>
17+
Effect.sync(() => {
18+
expect(request.headers.authorization).toBe("Bearer sk-or-secret");
19+
const path = new URL(request.url).pathname;
20+
paths.push(path);
21+
const reply = replies[path] ?? { status: 404, body: {} };
22+
return HttpClientResponse.fromWeb(
23+
request,
24+
Response.json(reply.body, { status: reply.status }),
25+
);
26+
}),
27+
);
28+
return { paths, api: makeOpenRouterApi.pipe(Effect.provideService(HttpClient.HttpClient, http)) };
29+
}
30+
31+
describe("OpenRouter credit reads", () => {
32+
it.effect("reports the account balance a provisioning key can see", () =>
33+
Effect.gen(function* () {
34+
const test = fixture({
35+
"/api/v1/credits": {
36+
status: 200,
37+
body: { data: { total_credits: 100.5, total_usage: 25.75 } },
38+
},
39+
});
40+
const api = yield* test.api;
41+
42+
expect(yield* api.readCredits(config)).toEqual({
43+
scope: "account",
44+
usedUsd: 25.75,
45+
purchasedUsd: 100.5,
46+
remainingUsd: 74.75,
47+
});
48+
// The narrower read is never made when the account answers.
49+
expect(test.paths).toEqual(["/api/v1/credits"]);
50+
}),
51+
);
52+
53+
it.effect("falls back to the key's own allowance when OpenRouter refuses with 403", () =>
54+
Effect.gen(function* () {
55+
const test = fixture({
56+
"/api/v1/credits": {
57+
status: 403,
58+
body: {
59+
error: { code: 403, message: "Only management keys can perform this operation" },
60+
},
61+
},
62+
"/api/v1/key": {
63+
status: 200,
64+
body: { data: { usage: 1.42, limit: 10, limit_remaining: 8.58, is_free_tier: false } },
65+
},
66+
});
67+
const api = yield* test.api;
68+
69+
expect(yield* api.readCredits(config)).toEqual({
70+
scope: "key",
71+
usedUsd: 1.42,
72+
limitUsd: 10,
73+
remainingUsd: 8.58,
74+
isFreeTier: false,
75+
});
76+
expect(test.paths).toEqual(["/api/v1/credits", "/api/v1/key"]);
77+
}),
78+
);
79+
80+
it.effect("leaves an uncapped key with spend only, so no balance is invented", () =>
81+
Effect.gen(function* () {
82+
const test = fixture({
83+
"/api/v1/credits": { status: 403, body: {} },
84+
"/api/v1/key": {
85+
status: 200,
86+
body: { data: { usage: 3.5, limit: null, limit_remaining: null, is_free_tier: true } },
87+
},
88+
});
89+
const api = yield* test.api;
90+
91+
expect(yield* api.readCredits(config)).toEqual({
92+
scope: "key",
93+
usedUsd: 3.5,
94+
isFreeTier: true,
95+
});
96+
}),
97+
);
98+
99+
it.effect("reports a rejected key rather than retrying it against the other endpoint", () =>
100+
Effect.gen(function* () {
101+
const test = fixture({ "/api/v1/credits": { status: 401, body: {} } });
102+
const api = yield* test.api;
103+
const result = yield* api.readCredits(config).pipe(Effect.result);
104+
105+
expect(result._tag).toBe("Failure");
106+
if (result._tag === "Failure") {
107+
expect(result.failure.detail).toBe("OpenRouter rejected the API key.");
108+
}
109+
expect(test.paths).toEqual(["/api/v1/credits"]);
110+
}),
111+
);
112+
113+
it.effect("reports a body it cannot read instead of publishing a partial balance", () =>
114+
Effect.gen(function* () {
115+
const test = fixture({
116+
"/api/v1/credits": { status: 200, body: { data: { total_credits: "lots" } } },
117+
});
118+
const api = yield* test.api;
119+
const result = yield* api.readCredits(config).pipe(Effect.result);
120+
121+
expect(result._tag).toBe("Failure");
122+
if (result._tag === "Failure") {
123+
expect(result.failure.detail).toBe("OpenRouter returned an unexpected balance.");
124+
}
125+
}),
126+
);
127+
128+
it.effect("never calls OpenRouter without a key", () =>
129+
Effect.gen(function* () {
130+
const test = fixture({});
131+
const api = yield* test.api;
132+
const result = yield* api.readCredits({ ...config, managementKey: "" }).pipe(Effect.result);
133+
134+
expect(result._tag).toBe("Failure");
135+
expect(test.paths).toEqual([]);
136+
}),
137+
);
138+
});

0 commit comments

Comments
 (0)