Skip to content

Commit ee2d786

Browse files
feat(usage): add command-based usage limit sources
Adds a `kind: "command"` usage-limit source that runs a configured command on the server host and reads a `{ "accounts": [...] }` JSON document from stdout. The command owns credential lookup itself, so agent accounts that are not behind a CLIProxyAPI hub can report quota in the Limits view without storing secrets in settings or command arguments. Nonzero exits, malformed output, and a 30s timeout surface as source errors alongside the existing hub errors. Reset-credit redemption stays CLIProxyAPI-only, and the settings dialog gains a hub/command toggle. Refs #23883 Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent d7a8d6f commit ee2d786

9 files changed

Lines changed: 441 additions & 58 deletions

File tree

‎apps/server/src/settingsSecrets.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
CodexSettings,
55
EasDeviceHostConfig,
66
type ServerSettings,
7-
UsageLimitSourceConfig,
7+
CliproxyUsageLimitSourceConfig,
8+
CommandUsageLimitSourceConfig,
89
secretSettingsFields,
910
} from "@t3tools/contracts";
1011
import { acpRegistryClient } from "@t3tools/provider-acp-registry/client";
@@ -31,7 +32,10 @@ const providerSecretFields = new Map([
3132
[definition.driverKind, secretSettingsFields(definition.settingsSchema)] as const,
3233
),
3334
]);
34-
const usageLimitSecretFields = secretSettingsFields(UsageLimitSourceConfig);
35+
const usageLimitSecretFields = [
36+
...secretSettingsFields(CliproxyUsageLimitSourceConfig),
37+
...secretSettingsFields(CommandUsageLimitSourceConfig),
38+
];
3539
const easDeviceHostSecretFields = secretSettingsFields(EasDeviceHostConfig);
3640
const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");
3741

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

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"
44
import * as Effect from "effect/Effect";
55
import * as Layer from "effect/Layer";
66
import * as Schema from "effect/Schema";
7+
import * as Sink from "effect/Sink";
8+
import * as Stream from "effect/Stream";
79
import { HttpClient } from "effect/http";
10+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
811

912
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
1013
import * as ServerSettings from "../serverSettings.ts";
@@ -78,6 +81,182 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
7881
HttpClient.HttpClient,
7982
HttpClient.make(() => Effect.die("Unexpected network request")),
8083
),
84+
commandSpawnerLayer({}),
85+
NodeCrypto.layer,
86+
]),
87+
Effect.scoped,
88+
),
89+
);
90+
91+
const VALID_COMMAND_OUTPUT = JSON.stringify({
92+
accounts: [
93+
{
94+
id: "droid-b",
95+
driver: "droid",
96+
97+
plan: "pro",
98+
usageLimits: {
99+
checkedAt: "2026-10-23T00:00:00.000Z",
100+
windows: [
101+
{
102+
id: "monthly",
103+
kind: "monthly",
104+
label: "Monthly quota",
105+
usedPercent: 41,
106+
},
107+
],
108+
},
109+
},
110+
],
111+
});
112+
113+
const commandSpawnerLayer = (
114+
outputs: Readonly<Record<string, { stdout: string; stderr?: string; exitCode: number }>>,
115+
) =>
116+
Layer.succeed(
117+
ChildProcessSpawner.ChildProcessSpawner,
118+
ChildProcessSpawner.make((command) => {
119+
const result = "command" in command ? outputs[command.command] : undefined;
120+
if (result === undefined) {
121+
return Effect.die(
122+
`Unexpected command: ${"command" in command ? command.command : "piped"}`,
123+
);
124+
}
125+
const encode = new TextEncoder();
126+
return Effect.succeed(
127+
ChildProcessSpawner.makeHandle({
128+
pid: ChildProcessSpawner.ProcessId(1),
129+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(result.exitCode)),
130+
isRunning: Effect.succeed(false),
131+
kill: () => Effect.void,
132+
unref: Effect.succeed(Effect.void),
133+
stdin: Sink.drain,
134+
stdout:
135+
result.stdout.length > 0 ? Stream.make(encode.encode(result.stdout)) : Stream.empty,
136+
stderr:
137+
(result.stderr ?? "").length > 0
138+
? Stream.make(encode.encode(result.stderr ?? ""))
139+
: Stream.empty,
140+
all: Stream.empty,
141+
getInputFd: () => Sink.drain,
142+
getOutputFd: () => Stream.empty,
143+
}),
144+
);
145+
}),
146+
);
147+
148+
it.effect("reads accounts from command sources and reports command failures", () =>
149+
Effect.gen(function* () {
150+
const sources = yield* UsageLimitSources.make;
151+
yield* sources.refresh;
152+
const snapshots = yield* sources.current;
153+
154+
const byId = new Map(snapshots.map((snapshot) => [snapshot.id, snapshot]));
155+
expect(snapshots.length).toBe(4);
156+
157+
const ok = byId.get(UsageLimitSourceId.make("command-ok"));
158+
expect(ok?.kind).toBe("command");
159+
expect(ok?.label).toBe("/usr/local/bin/droidpool");
160+
expect(ok?.error).toBeUndefined();
161+
expect(ok?.accounts).toEqual([
162+
{
163+
id: "droid-b",
164+
driver: "droid",
165+
166+
plan: "pro",
167+
usageLimits: {
168+
checkedAt: "2026-10-23T00:00:00.000Z",
169+
windows: [
170+
{
171+
id: "monthly",
172+
kind: "monthly",
173+
label: "Monthly quota",
174+
usedPercent: 41,
175+
},
176+
],
177+
},
178+
},
179+
]);
180+
181+
const named = byId.get(UsageLimitSourceId.make("command-named"));
182+
expect(named?.label).toBe("Pool status");
183+
expect(named?.accounts.length).toBe(1);
184+
185+
const malformed = byId.get(UsageLimitSourceId.make("command-bad-json"));
186+
expect(malformed?.accounts).toEqual([]);
187+
expect(malformed?.error).toBe(
188+
'The command\'s output is not valid usage-limit JSON (expected { "accounts": [...] }).',
189+
);
190+
191+
const failing = byId.get(UsageLimitSourceId.make("command-fails"));
192+
expect(failing?.accounts).toEqual([]);
193+
expect(failing?.error).toBe(
194+
"The command failed with exit code 3. pool is locked by another process",
195+
);
196+
197+
const reset = yield* sources
198+
.consumeResetCredit({
199+
sourceId: UsageLimitSourceId.make("command-ok"),
200+
accountId: "droid-b",
201+
creditId: "credit-1",
202+
})
203+
.pipe(Effect.result);
204+
expect(reset._tag).toBe("Failure");
205+
if (reset._tag === "Failure") {
206+
expect(reset.failure.detail).toBe(
207+
"Reset credits are only available for CLIProxyAPI hub sources.",
208+
);
209+
}
210+
}).pipe(
211+
Effect.provide([
212+
ServerSettings.layerTest({
213+
usageLimitSources: {
214+
[UsageLimitSourceId.make("command-ok")]: {
215+
kind: "command",
216+
command: "/usr/local/bin/droidpool",
217+
args: ["list", "--json"],
218+
enabled: true,
219+
},
220+
[UsageLimitSourceId.make("command-named")]: {
221+
kind: "command",
222+
label: "Pool status",
223+
command: "/usr/local/bin/droidpool2",
224+
enabled: true,
225+
},
226+
[UsageLimitSourceId.make("command-bad-json")]: {
227+
kind: "command",
228+
command: "/usr/local/bin/bad-json",
229+
enabled: true,
230+
},
231+
[UsageLimitSourceId.make("command-fails")]: {
232+
kind: "command",
233+
command: "/usr/local/bin/fails",
234+
enabled: true,
235+
},
236+
[UsageLimitSourceId.make("command-off")]: {
237+
kind: "command",
238+
command: "/usr/local/bin/droidpool",
239+
enabled: false,
240+
},
241+
},
242+
}),
243+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
244+
shouldRunScopeWork: () => Effect.succeed(false),
245+
}),
246+
Layer.succeed(
247+
HttpClient.HttpClient,
248+
HttpClient.make(() => Effect.die("Unexpected network request")),
249+
),
250+
commandSpawnerLayer({
251+
"/usr/local/bin/droidpool": { stdout: VALID_COMMAND_OUTPUT, exitCode: 0 },
252+
"/usr/local/bin/droidpool2": { stdout: VALID_COMMAND_OUTPUT, exitCode: 0 },
253+
"/usr/local/bin/bad-json": { stdout: "not json at all", exitCode: 0 },
254+
"/usr/local/bin/fails": {
255+
stdout: "",
256+
stderr: "warning: something minor\npool is locked by another process\n",
257+
exitCode: 3,
258+
},
259+
}),
81260
NodeCrypto.layer,
82261
]),
83262
Effect.scoped,

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as Stream from "effect/Stream";
2323
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
2424
import * as Settings from "../serverSettings.ts";
2525
import { makeCliproxyApi } from "./cliproxyApi.ts";
26+
import { makeCommandApi } from "./commandApi.ts";
2627

2728
export class UsageLimitSources extends Context.Service<
2829
UsageLimitSources,
@@ -39,6 +40,7 @@ export class UsageLimitSources extends Context.Service<
3940
function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
4041
if (config.label) return config.label;
4142
const fallback = id.trim() || "Usage limit source";
43+
if (config.kind === "command") return config.command || fallback;
4244
try {
4345
return new URL(config.url).host || fallback;
4446
} catch {
@@ -49,6 +51,7 @@ function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
4951
/** @public Service construction is part of the canonical Effect module API. */
5052
export const make = Effect.gen(function* () {
5153
const api = yield* makeCliproxyApi;
54+
const commandApi = yield* makeCommandApi;
5255
const settingsService = yield* Settings.ServerSettingsService;
5356
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
5457
const stateRef = yield* Ref.make<ReadonlyArray<UsageLimitSourceSnapshot>>([]);
@@ -63,10 +66,16 @@ export const make = Effect.gen(function* () {
6366
) {
6467
const checkedAt = DateTime.formatIso(yield* DateTime.now);
6568
const base = { id, kind: config.kind, label: sourceLabel(id, config), checkedAt } as const;
66-
if (config.managementKey.length === 0) {
69+
const read =
70+
config.kind === "command"
71+
? commandApi.readAccounts(config)
72+
: config.managementKey.length === 0
73+
? null
74+
: api.readAccounts(config);
75+
if (read === null) {
6776
return { ...base, accounts: [], error: "No management key configured." };
6877
}
69-
const accounts = yield* api.readAccounts(config).pipe(Effect.result);
78+
const accounts = yield* read.pipe(Effect.result);
7079
if (accounts._tag === "Failure") {
7180
yield* Effect.logDebug("usage limit source read failed", { id, cause: accounts.failure });
7281
return { ...base, accounts: [], error: accounts.failure.detail };
@@ -110,7 +119,17 @@ export const make = Effect.gen(function* () {
110119
Effect.mapError(() => new UsageLimitSourceError({ detail: "Could not read hub settings." })),
111120
);
112121
const config = settings.usageLimitSources[input.sourceId];
113-
if (!config?.enabled || !config.managementKey) {
122+
if (!config?.enabled) {
123+
return yield* new UsageLimitSourceError({
124+
detail: "The usage limit source is missing or disabled.",
125+
});
126+
}
127+
if (config.kind !== "cliproxy") {
128+
return yield* new UsageLimitSourceError({
129+
detail: "Reset credits are only available for CLIProxyAPI hub sources.",
130+
});
131+
}
132+
if (!config.managementKey) {
114133
return yield* new UsageLimitSourceError({
115134
detail: "The usage limit source is missing or disabled.",
116135
});

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
UsageLimitSourceError,
44
type ProviderConsumeResetCreditResult,
55
type UsageLimitSourceAccount,
6-
type UsageLimitSourceConfig,
6+
type CliproxyUsageLimitSourceConfig,
77
} from "@t3tools/contracts";
88
import * as Cause from "effect/Cause";
99
import * as Crypto from "effect/Crypto";
@@ -164,7 +164,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
164164
const crypto = yield* Crypto.Crypto;
165165

166166
const management = Effect.fn("CliproxyApi.management")(function* (
167-
config: UsageLimitSourceConfig,
167+
config: CliproxyUsageLimitSourceConfig,
168168
path: string,
169169
body?: unknown,
170170
) {
@@ -192,13 +192,15 @@ export const makeCliproxyApi = Effect.gen(function* () {
192192
return response;
193193
});
194194

195-
const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (config: UsageLimitSourceConfig) {
195+
const authFiles = Effect.fn("CliproxyApi.authFiles")(function* (
196+
config: CliproxyUsageLimitSourceConfig,
197+
) {
196198
const response = yield* management(config, "auth-files");
197199
return (yield* decodeAuthFiles(response)).files;
198200
});
199201

200202
const apiCall = Effect.fn("CliproxyApi.apiCall")(function* (
201-
config: UsageLimitSourceConfig,
203+
config: CliproxyUsageLimitSourceConfig,
202204
account: typeof AuthFile.Type,
203205
url: string,
204206
data?: unknown,
@@ -237,7 +239,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
237239
});
238240

239241
const credits = Effect.fn("CliproxyApi.credits")(function* (
240-
config: UsageLimitSourceConfig,
242+
config: CliproxyUsageLimitSourceConfig,
241243
account: typeof AuthFile.Type,
242244
) {
243245
const body = yield* apiCall(config, account, CREDIT_URL);
@@ -254,7 +256,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
254256
});
255257

256258
const readAccount = Effect.fn("CliproxyApi.readAccount")(function* (
257-
config: UsageLimitSourceConfig,
259+
config: CliproxyUsageLimitSourceConfig,
258260
account: typeof AuthFile.Type,
259261
) {
260262
const checkedAt = DateTime.formatIso(yield* DateTime.now);
@@ -354,7 +356,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
354356
});
355357

356358
const readAccounts = Effect.fn("CliproxyApi.readAccounts")(function* (
357-
config: UsageLimitSourceConfig,
359+
config: CliproxyUsageLimitSourceConfig,
358360
): Effect.fn.Return<ReadonlyArray<UsageLimitSourceAccount>, UsageLimitSourceError> {
359361
const accounts = yield* authFiles(config).pipe(
360362
Effect.mapError((error) =>
@@ -374,7 +376,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
374376
});
375377

376378
const consume = Effect.fn("CliproxyApi.consume")(function* (
377-
config: UsageLimitSourceConfig,
379+
config: CliproxyUsageLimitSourceConfig,
378380
accountId: string,
379381
creditId: string,
380382
): Effect.fn.Return<ProviderConsumeResetCreditResult, UsageLimitSourceError> {

0 commit comments

Comments
 (0)