shadok-ai 0.19.1 → 0.19.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/dist/claude-auth.d.ts +36 -6
- package/dist/claude-auth.js +140 -126
- package/dist/claude-auth.js.map +1 -1
- package/dist/gauge-login.d.ts +145 -0
- package/dist/gauge-login.js +284 -0
- package/dist/gauge-login.js.map +1 -0
- package/dist/server.js +54 -6
- package/dist/server.js.map +1 -1
- package/dist/usage.d.ts +30 -19
- package/dist/usage.js +96 -58
- package/dist/usage.js.map +1 -1
- package/package.json +1 -1
- package/public/auth-expiry.js +47 -0
- package/public/index.html +80 -11
package/README.md
CHANGED
|
@@ -215,6 +215,19 @@ Enter to skip; you can add it later from the web UI).
|
|
|
215
215
|
- **Quota gauges + pace guard** — 5h and 7d subscription usage, with an
|
|
216
216
|
optional block when you're burning faster than the window elapses (any
|
|
217
217
|
message can force through).
|
|
218
|
+
On an instance whose agents run on a long-lived `setup-token` (multi-tenant),
|
|
219
|
+
that token cannot read usage (it is scoped `user:inference` only), so the gauges
|
|
220
|
+
stay blank unless you add the **usage gauge login**: version menu → *Set up
|
|
221
|
+
usage gauge login…*, then sign in with the account whose usage you want shown.
|
|
222
|
+
It is a separate, optional claude.ai sign-in kept in its own config dir
|
|
223
|
+
(`~/.shadok-ai/gauge/<instance>/.claude`) and used only for the gauges —
|
|
224
|
+
agents never see it. Shadok keeps its access token fresh on its own, but
|
|
225
|
+
Anthropic fixes the sign-in itself at about a month and refreshing never
|
|
226
|
+
extends it, so you re-login roughly **monthly**; a banner reminds you 5 days
|
|
227
|
+
before. Not configured = nothing changes. File credential store only (Linux,
|
|
228
|
+
Docker); on macOS the row is hidden. Endpoints (admin): `GET /usage-gauge`,
|
|
229
|
+
`POST /usage-gauge/login`, `POST /usage-gauge/code`,
|
|
230
|
+
`DELETE /usage-gauge/login` (cancel), `DELETE /usage-gauge` (remove).
|
|
218
231
|
- **Notifications** — favicon, title badge and an optional sound when an agent
|
|
219
232
|
needs you. It only blinks when the tab is hidden *and* an unmuted channel is
|
|
220
233
|
actually waiting. Mute a single agent from its menu, or **mute the whole
|
package/dist/claude-auth.d.ts
CHANGED
|
@@ -64,18 +64,48 @@ export declare function probeWith(run: () => Promise<string>, opts?: {
|
|
|
64
64
|
sleep?: (ms: number) => Promise<void>;
|
|
65
65
|
}): Promise<AuthStatus>;
|
|
66
66
|
export declare function authStatus(force?: boolean): Promise<AuthStatus>;
|
|
67
|
-
export
|
|
68
|
-
export declare function cancelLogin(): void;
|
|
69
|
-
export declare function startLogin(force?: boolean): Promise<{
|
|
67
|
+
export type StartLoginResult = {
|
|
70
68
|
url: string;
|
|
71
69
|
} | {
|
|
72
70
|
alreadySignedIn: true;
|
|
73
71
|
} | {
|
|
74
72
|
error: string;
|
|
75
|
-
}
|
|
76
|
-
export
|
|
73
|
+
};
|
|
74
|
+
export type SubmitLoginResult = {
|
|
77
75
|
ok: true;
|
|
78
76
|
} | {
|
|
79
77
|
ok: false;
|
|
80
78
|
error: string;
|
|
81
|
-
}
|
|
79
|
+
};
|
|
80
|
+
export interface LoginFlow {
|
|
81
|
+
pending(): boolean;
|
|
82
|
+
cancel(): void;
|
|
83
|
+
start(force?: boolean): Promise<StartLoginResult>;
|
|
84
|
+
submit(code: string): Promise<SubmitLoginResult>;
|
|
85
|
+
}
|
|
86
|
+
export interface LoginFlowOptions {
|
|
87
|
+
/** Environment of the `claude auth login` child. Default: this process's. A
|
|
88
|
+
* second flow (the usage-gauge login, src/gauge-login.ts) points the CLI at its
|
|
89
|
+
* OWN `CLAUDE_CONFIG_DIR` here, so it can never touch the instance's credentials. */
|
|
90
|
+
env?: () => NodeJS.ProcessEnv;
|
|
91
|
+
/** Refuse to start (answering `alreadySignedIn`) when this says so and the
|
|
92
|
+
* caller did not force. Absent = always start. */
|
|
93
|
+
signedIn?: () => Promise<boolean>;
|
|
94
|
+
/** Called after a code was accepted (clean exit). */
|
|
95
|
+
onSuccess?: () => void;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* ONE login flow per credential store. The instance login is machine-global, so
|
|
99
|
+
* a second concurrent login would race the first for the same keychain entry;
|
|
100
|
+
* a flow therefore keeps at most one child. The web card and Telegram share the
|
|
101
|
+
* same URL, and a code pasted from either finishes the other's flow.
|
|
102
|
+
*
|
|
103
|
+
* It is a factory because the usage-gauge login is a SECOND, independent store
|
|
104
|
+
* (its own config dir) — module-level state would have forced the two to cancel
|
|
105
|
+
* each other.
|
|
106
|
+
*/
|
|
107
|
+
export declare function createLoginFlow(opts?: LoginFlowOptions): LoginFlow;
|
|
108
|
+
export declare const loginPending: () => boolean;
|
|
109
|
+
export declare const cancelLogin: () => void;
|
|
110
|
+
export declare const startLogin: (force?: boolean) => Promise<StartLoginResult>;
|
|
111
|
+
export declare const submitLoginCode: (code: string) => Promise<SubmitLoginResult>;
|
package/dist/claude-auth.js
CHANGED
|
@@ -113,135 +113,149 @@ export async function authStatus(force = false) {
|
|
|
113
113
|
return status;
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
|
-
* ONE flow
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* a code pasted from either finishes the other's flow.
|
|
116
|
+
* ONE login flow per credential store. The instance login is machine-global, so
|
|
117
|
+
* a second concurrent login would race the first for the same keychain entry;
|
|
118
|
+
* a flow therefore keeps at most one child. The web card and Telegram share the
|
|
119
|
+
* same URL, and a code pasted from either finishes the other's flow.
|
|
120
|
+
*
|
|
121
|
+
* It is a factory because the usage-gauge login is a SECOND, independent store
|
|
122
|
+
* (its own config dir) — module-level state would have forced the two to cancel
|
|
123
|
+
* each other.
|
|
120
124
|
*/
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
flow = null;
|
|
131
|
-
}
|
|
132
|
-
export async function startLogin(force = false) {
|
|
133
|
-
// Reuse a flow that already printed its URL instead of replacing it. Each
|
|
134
|
-
// flow carries its own PKCE challenge, so restarting would silently kill the
|
|
135
|
-
// link the user is already looking at — which a plain page reload, or a
|
|
136
|
-
// second tab, would otherwise do.
|
|
137
|
-
if (flow && !flow.ended && flow.url)
|
|
138
|
-
return { url: flow.url };
|
|
139
|
-
// Never spawn a sign-in on an instance that is already signed in — UNLESS the
|
|
140
|
-
// caller explicitly forces it. This is the backstop for the whole class of
|
|
141
|
-
// "it launched for nothing" (an auto card on a signed-in instance), but a
|
|
142
|
-
// deliberate "re-login now" IS the signed-in case: the user wants to renew
|
|
143
|
-
// the session BEFORE its refresh token lapses. Verified against the real CLI:
|
|
144
|
-
// `claude auth login --claudeai` while signed in prints a fresh URL and a
|
|
145
|
-
// code prompt — a working re-login, writing new credentials only on a code
|
|
146
|
-
// exchange — so forcing is both necessary and sufficient here.
|
|
147
|
-
if (!force && (await authStatus(true)).state === "signed-in")
|
|
148
|
-
return { alreadySignedIn: true };
|
|
149
|
-
cancelLogin();
|
|
150
|
-
const child = spawn(claudeCommand(), ["auth", "login", "--claudeai"], {
|
|
151
|
-
// BROWSER is neutralised: on a desktop host the CLI would otherwise open a
|
|
152
|
-
// tab on the SERVER's machine, which is not where the user is.
|
|
153
|
-
env: { ...process.env, BROWSER: "/usr/bin/true" },
|
|
154
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
155
|
-
});
|
|
156
|
-
const f = {
|
|
157
|
-
child,
|
|
158
|
-
url: null,
|
|
159
|
-
out: "",
|
|
160
|
-
pending: null,
|
|
161
|
-
ended: false,
|
|
162
|
-
timer: setTimeout(() => cancelLogin(), FLOW_IDLE_MS),
|
|
125
|
+
export function createLoginFlow(opts = {}) {
|
|
126
|
+
let flow = null;
|
|
127
|
+
const pending = () => flow !== null && !flow.ended;
|
|
128
|
+
const cancel = () => {
|
|
129
|
+
if (!flow)
|
|
130
|
+
return;
|
|
131
|
+
clearTimeout(flow.timer);
|
|
132
|
+
flow.child.kill();
|
|
133
|
+
flow = null;
|
|
163
134
|
};
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
f
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
f.url = url;
|
|
200
|
-
clearInterval(poll);
|
|
201
|
-
clearTimeout(deadline);
|
|
202
|
-
resolve({ url });
|
|
203
|
-
}
|
|
204
|
-
else if (flow !== f || f.ended) {
|
|
205
|
-
clearInterval(poll);
|
|
206
|
-
clearTimeout(deadline);
|
|
207
|
-
resolve({ error: "the login process ended before printing a URL" });
|
|
208
|
-
}
|
|
209
|
-
}, 100);
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
export async function submitLoginCode(code) {
|
|
213
|
-
const f = flow;
|
|
214
|
-
if (!f || f.ended)
|
|
215
|
-
return { ok: false, error: "no sign-in is in progress — start one first" };
|
|
216
|
-
const trimmed = code.trim();
|
|
217
|
-
if (!trimmed)
|
|
218
|
-
return { ok: false, error: "empty code" };
|
|
219
|
-
// The user is clearly still here: push the idle deadline back.
|
|
220
|
-
clearTimeout(f.timer);
|
|
221
|
-
f.timer = setTimeout(() => cancelLogin(), FLOW_IDLE_MS);
|
|
222
|
-
// Forget what came before, so a stale "Invalid code" from a previous attempt
|
|
223
|
-
// is never mistaken for the verdict on THIS one.
|
|
224
|
-
f.out = "";
|
|
225
|
-
const verdict = await new Promise((resolve) => {
|
|
226
|
-
const t = setTimeout(() => {
|
|
135
|
+
const start = async (force = false) => {
|
|
136
|
+
// Reuse a flow that already printed its URL instead of replacing it. Each
|
|
137
|
+
// flow carries its own PKCE challenge, so restarting would silently kill the
|
|
138
|
+
// link the user is already looking at — which a plain page reload, or a
|
|
139
|
+
// second tab, would otherwise do.
|
|
140
|
+
if (flow && !flow.ended && flow.url)
|
|
141
|
+
return { url: flow.url };
|
|
142
|
+
// Never spawn a sign-in on an instance that is already signed in — UNLESS the
|
|
143
|
+
// caller explicitly forces it. This is the backstop for the whole class of
|
|
144
|
+
// "it launched for nothing" (an auto card on a signed-in instance), but a
|
|
145
|
+
// deliberate "re-login now" IS the signed-in case: the user wants to renew
|
|
146
|
+
// the session BEFORE its refresh token lapses. Verified against the real CLI:
|
|
147
|
+
// `claude auth login --claudeai` while signed in prints a fresh URL and a
|
|
148
|
+
// code prompt — a working re-login, writing new credentials only on a code
|
|
149
|
+
// exchange — so forcing is both necessary and sufficient here.
|
|
150
|
+
if (!force && opts.signedIn && (await opts.signedIn()))
|
|
151
|
+
return { alreadySignedIn: true };
|
|
152
|
+
cancel();
|
|
153
|
+
const child = spawn(claudeCommand(), ["auth", "login", "--claudeai"], {
|
|
154
|
+
// BROWSER is neutralised: on a desktop host the CLI would otherwise open a
|
|
155
|
+
// tab on the SERVER's machine, which is not where the user is.
|
|
156
|
+
env: { ...(opts.env ? opts.env() : process.env), BROWSER: "/usr/bin/true" },
|
|
157
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
158
|
+
});
|
|
159
|
+
const f = {
|
|
160
|
+
child,
|
|
161
|
+
url: null,
|
|
162
|
+
out: "",
|
|
163
|
+
pending: null,
|
|
164
|
+
ended: false,
|
|
165
|
+
timer: setTimeout(() => cancel(), FLOW_IDLE_MS),
|
|
166
|
+
};
|
|
167
|
+
flow = f;
|
|
168
|
+
const settle = (v) => {
|
|
169
|
+
const done = f.pending;
|
|
227
170
|
f.pending = null;
|
|
228
|
-
|
|
229
|
-
}, 30_000);
|
|
230
|
-
f.pending = (r) => {
|
|
231
|
-
clearTimeout(t);
|
|
232
|
-
resolve(r);
|
|
171
|
+
done?.(v);
|
|
233
172
|
};
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
173
|
+
const onData = (d) => {
|
|
174
|
+
f.out += d.toString();
|
|
175
|
+
if (parseLoginOutcome(f.out) === "invalid-code")
|
|
176
|
+
settle("invalid-code");
|
|
177
|
+
};
|
|
178
|
+
child.stdout.on("data", onData);
|
|
179
|
+
child.stderr.on("data", onData);
|
|
180
|
+
// SUCCESS IS AN EXIT, NOT A STRING. The "Invalid code…" wording was captured
|
|
181
|
+
// from the real CLI; the success wording never was, and inventing one would
|
|
182
|
+
// produce a sign-in that silently never completes. A clean exit after a code
|
|
183
|
+
// was submitted is the signal we can actually prove.
|
|
184
|
+
child.on("exit", (code) => {
|
|
185
|
+
f.ended = true;
|
|
186
|
+
settle(code === 0 ? "success" : "ended");
|
|
187
|
+
});
|
|
188
|
+
// A `claude` that is not on PATH must not leave the caller hanging until the
|
|
189
|
+
// 30s deadline with no explanation.
|
|
190
|
+
child.on("error", () => {
|
|
191
|
+
f.ended = true;
|
|
192
|
+
settle("ended");
|
|
193
|
+
});
|
|
194
|
+
return new Promise((resolve) => {
|
|
195
|
+
const deadline = setTimeout(() => {
|
|
196
|
+
cancel();
|
|
197
|
+
resolve({ error: "claude auth login printed no URL within 30s" });
|
|
198
|
+
}, 30_000);
|
|
199
|
+
const poll = setInterval(() => {
|
|
200
|
+
const url = parseLoginUrl(f.out);
|
|
201
|
+
if (url) {
|
|
202
|
+
f.url = url;
|
|
203
|
+
clearInterval(poll);
|
|
204
|
+
clearTimeout(deadline);
|
|
205
|
+
resolve({ url });
|
|
206
|
+
}
|
|
207
|
+
else if (flow !== f || f.ended) {
|
|
208
|
+
clearInterval(poll);
|
|
209
|
+
clearTimeout(deadline);
|
|
210
|
+
resolve({ error: "the login process ended before printing a URL" });
|
|
211
|
+
}
|
|
212
|
+
}, 100);
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
const submit = async (code) => {
|
|
216
|
+
const f = flow;
|
|
217
|
+
if (!f || f.ended)
|
|
218
|
+
return { ok: false, error: "no sign-in is in progress — start one first" };
|
|
219
|
+
const trimmed = code.trim();
|
|
220
|
+
if (!trimmed)
|
|
221
|
+
return { ok: false, error: "empty code" };
|
|
222
|
+
// The user is clearly still here: push the idle deadline back.
|
|
223
|
+
clearTimeout(f.timer);
|
|
224
|
+
f.timer = setTimeout(() => cancel(), FLOW_IDLE_MS);
|
|
225
|
+
// Forget what came before, so a stale "Invalid code" from a previous attempt
|
|
226
|
+
// is never mistaken for the verdict on THIS one.
|
|
227
|
+
f.out = "";
|
|
228
|
+
const verdict = await new Promise((resolve) => {
|
|
229
|
+
const t = setTimeout(() => {
|
|
230
|
+
f.pending = null;
|
|
231
|
+
resolve("timeout");
|
|
232
|
+
}, 30_000);
|
|
233
|
+
f.pending = (r) => {
|
|
234
|
+
clearTimeout(t);
|
|
235
|
+
resolve(r);
|
|
236
|
+
};
|
|
237
|
+
f.child.stdin.write(trimmed + "\n");
|
|
238
|
+
});
|
|
239
|
+
if (verdict === "success") {
|
|
240
|
+
cancel();
|
|
241
|
+
opts.onSuccess?.();
|
|
242
|
+
return { ok: true };
|
|
243
|
+
}
|
|
244
|
+
if (verdict === "invalid-code")
|
|
245
|
+
return { ok: false, error: "Invalid code. Please make sure the full code was copied." };
|
|
246
|
+
if (verdict === "ended")
|
|
247
|
+
return { ok: false, error: "the sign-in was refused — start a new one and try again" };
|
|
248
|
+
return { ok: false, error: "the CLI did not answer within 30s" };
|
|
249
|
+
};
|
|
250
|
+
return { pending, cancel, start, submit };
|
|
246
251
|
}
|
|
252
|
+
/** The instance's own sign-in: the default store, the default environment. */
|
|
253
|
+
const instanceFlow = createLoginFlow({
|
|
254
|
+
signedIn: async () => (await authStatus(true)).state === "signed-in",
|
|
255
|
+
onSuccess: invalidateAuthStatus,
|
|
256
|
+
});
|
|
257
|
+
export const loginPending = () => instanceFlow.pending();
|
|
258
|
+
export const cancelLogin = () => instanceFlow.cancel();
|
|
259
|
+
export const startLogin = (force = false) => instanceFlow.start(force);
|
|
260
|
+
export const submitLoginCode = (code) => instanceFlow.submit(code);
|
|
247
261
|
//# sourceMappingURL=claude-auth.js.map
|
package/dist/claude-auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"claude-auth.js","sourceRoot":"","sources":["../src/claude-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAuC,MAAM,oBAAoB,CAAC;AAC1F,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAqCpE,yFAAyF;AACzF,MAAM,UAAU,eAAe,CAAC,MAAc;IAC5C,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC7B,IAAI,CAAC,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;YACjD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;QAC/C,IAAI,CAAC,CAAC,QAAQ,KAAK,IAAI;YAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC;QACzE,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,KAAK,EAAE,WAAW;YAClB,GAAG,CAAC,OAAO,CAAC,CAAC,UAAU,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACzE,GAAG,CAAC,OAAO,CAAC,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CA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mCAAmC,EAAE,CAAC;IACnE,CAAC,CAAC;IAEF,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;AAC5C,CAAC;AAED,8EAA8E;AAC9E,MAAM,YAAY,GAAG,eAAe,CAAC;IACnC,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,KAAK,WAAW;IACpE,SAAS,EAAE,oBAAoB;CAChC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG,GAAY,EAAE,CAAC,YAAY,CAAC,OAAO,EAAE,CAAC;AAClE,MAAM,CAAC,MAAM,WAAW,GAAG,GAAS,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC;AAC7D,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAK,GAAG,KAAK,EAA6B,EAAE,CAAC,YAAY,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAClG,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAY,EAA8B,EAAE,CAAC,YAAY,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { type LoginFlow } from "./claude-auth.js";
|
|
2
|
+
/**
|
|
3
|
+
* The usage-gauge login: an OPTIONAL, dedicated claude.ai sign-in used ONLY to
|
|
4
|
+
* feed the 5h/7d gauge.
|
|
5
|
+
*
|
|
6
|
+
* Why it exists. `GET /api/oauth/usage` needs the `user:profile` scope. The
|
|
7
|
+
* long-lived `setup-token` a multi-tenant instance runs its agents on is scoped
|
|
8
|
+
* `user:inference` only, so it can authenticate agents and still never read the
|
|
9
|
+
* gauge — a scope limit, not an endpoint one. An interactive `claude auth login
|
|
10
|
+
* --claudeai` mints a credential that does carry `user:profile`, so we keep one
|
|
11
|
+
* on the side, in a config dir of its own, and use ONLY its access token for the
|
|
12
|
+
* usage request. Agents never see it.
|
|
13
|
+
*
|
|
14
|
+
* Three facts shape the module (see docs/superpowers/specs/2026-11-10-usage-gauge-login.md):
|
|
15
|
+
* - the login's REFRESH token has a fixed window (~1 month from sign-in) that a
|
|
16
|
+
* refresh does not extend, so a manual re-login is inherent — we remind, we
|
|
17
|
+
* cannot avoid it;
|
|
18
|
+
* - the refresh token ROTATES, so the store has exactly ONE owner (this module)
|
|
19
|
+
* and lives in its own `CLAUDE_CONFIG_DIR`, never the agents' or the
|
|
20
|
+
* instance's;
|
|
21
|
+
* - we reuse the CLI for both the login and the refresh and implement no OAuth
|
|
22
|
+
* client of our own (the client id and token endpoint are internal).
|
|
23
|
+
*
|
|
24
|
+
* Opt-in and additive: with no credential in the gauge dir, none of this runs
|
|
25
|
+
* and `usageCandidates` is exactly the list it was before.
|
|
26
|
+
*/
|
|
27
|
+
/** Warn this long before the refresh token's hard wall. A month-long credential
|
|
28
|
+
* whose renewal needs the human's browser deserves more notice than the 48 h
|
|
29
|
+
* the instance login gets. */
|
|
30
|
+
export declare const GAUGE_WARN_MS: number;
|
|
31
|
+
/** Refresh once the access token has less than this left. Hours-long tokens, a
|
|
32
|
+
* 15-min tick: this leaves several attempts before it actually lapses. */
|
|
33
|
+
export declare const REFRESH_LEAD_MS: number;
|
|
34
|
+
/** Do not retry a refresh sooner than this after an attempt (success or not). */
|
|
35
|
+
export declare const REFRESH_BACKOFF_MS: number;
|
|
36
|
+
/** The dedicated config dir: one per instance, away from every other store. */
|
|
37
|
+
export declare function gaugeConfigDir(home: string, key: string): string;
|
|
38
|
+
/** Pure: the environment for any CLI child that must act on the gauge store. */
|
|
39
|
+
export declare function gaugeEnv(base: NodeJS.ProcessEnv, dir: string): NodeJS.ProcessEnv;
|
|
40
|
+
export interface GaugeCreds {
|
|
41
|
+
accessToken: string | null;
|
|
42
|
+
hasRefreshToken: boolean;
|
|
43
|
+
/** ms epoch, or null when the blob carries none. */
|
|
44
|
+
accessExpiresAt: number | null;
|
|
45
|
+
refreshExpiresAt: number | null;
|
|
46
|
+
/** Null when the blob lists none (then the scope is not asserted either way). */
|
|
47
|
+
scopes: string[] | null;
|
|
48
|
+
}
|
|
49
|
+
/** Pure: a `.credentials.json` blob → what we need. Unreadable → null. */
|
|
50
|
+
export declare function parseGaugeCreds(raw: string): GaugeCreds | null;
|
|
51
|
+
export declare function readGaugeCreds(dir: string): GaugeCreds | null;
|
|
52
|
+
/** The signed-in account's address, for the card (`.claude.json` of the gauge
|
|
53
|
+
* dir). Never throws. */
|
|
54
|
+
export declare function readGaugeEmail(dir: string): string | null;
|
|
55
|
+
export type GaugeState = "none" | "ok" | "expiring" | "expired" | "wrong-scope";
|
|
56
|
+
/** Pure: where the gauge login stands. `expiring` = inside GAUGE_WARN_MS of the
|
|
57
|
+
* refresh token's wall; `expired` = past it (or no way left to renew). */
|
|
58
|
+
export declare function gaugeState(c: GaugeCreds | null, now: number): GaugeState;
|
|
59
|
+
/** Pure: the Bearer to offer the usage endpoint, or null. A lapsed or
|
|
60
|
+
* wrong-scope token is not offered — `getUsage` would only burn a request on it
|
|
61
|
+
* before falling through. */
|
|
62
|
+
export declare function usableAccessToken(c: GaugeCreds | null, now: number): string | null;
|
|
63
|
+
/** Pure: should the keeper refresh now? */
|
|
64
|
+
export declare function shouldRefresh(c: GaugeCreds | null, now: number, lastAttemptAt: number | null): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Pure: the usage Bearers in preference order. The gauge login first (the only
|
|
67
|
+
* one guaranteed to carry `user:profile`), then the vault long-lived token (the
|
|
68
|
+
* account the agents run on), then the CLI's live store / env. `getUsage` keeps
|
|
69
|
+
* the first that REPORTS A WINDOW, so an absent or stale gauge login simply falls
|
|
70
|
+
* through to today's behaviour.
|
|
71
|
+
*/
|
|
72
|
+
export declare function usageCandidates(c: {
|
|
73
|
+
gauge: string | null;
|
|
74
|
+
longLived: string | null;
|
|
75
|
+
live: string | null;
|
|
76
|
+
}): Array<string | null>;
|
|
77
|
+
/** Pure: the CLI invocation that makes it refresh its own access token.
|
|
78
|
+
*
|
|
79
|
+
* There is no `claude auth refresh` (checked on 2.1.322: "unknown command");
|
|
80
|
+
* the CLI refreshes lazily, before an authenticated request. So the cheapest
|
|
81
|
+
* honest trigger is the smallest request there is: one `-p` turn on the
|
|
82
|
+
* smallest model, no session kept. The OUTCOME is judged on the credentials
|
|
83
|
+
* file afterwards — never on what the command printed. */
|
|
84
|
+
export declare function refreshInvocation(): {
|
|
85
|
+
command: string;
|
|
86
|
+
args: string[];
|
|
87
|
+
};
|
|
88
|
+
/** The shape of the last refresh attempt, shown on the card. Never a token. */
|
|
89
|
+
export interface RefreshOutcome {
|
|
90
|
+
at: number;
|
|
91
|
+
ok: boolean;
|
|
92
|
+
error?: string;
|
|
93
|
+
}
|
|
94
|
+
/** What the cockpit shows (`GET /usage-gauge`, and `gauge` inside `GET /auth`). */
|
|
95
|
+
export interface GaugeView {
|
|
96
|
+
/** False where the credential store is not a file (macOS keychain): the row is hidden. */
|
|
97
|
+
supported: boolean;
|
|
98
|
+
state: GaugeState;
|
|
99
|
+
email: string | null;
|
|
100
|
+
refreshExpiresAt: number | null;
|
|
101
|
+
accessExpiresAt: number | null;
|
|
102
|
+
lastRefresh: RefreshOutcome | null;
|
|
103
|
+
}
|
|
104
|
+
/** Pure: the view over already-read facts. */
|
|
105
|
+
export declare function gaugeView(c: GaugeCreds | null, email: string | null, lastRefresh: RefreshOutcome | null, now: number, supported?: boolean): GaugeView;
|
|
106
|
+
export type ExecFn = (command: string, args: string[], opts: {
|
|
107
|
+
env: NodeJS.ProcessEnv;
|
|
108
|
+
cwd: string;
|
|
109
|
+
timeout: number;
|
|
110
|
+
}) => Promise<{
|
|
111
|
+
error: string | null;
|
|
112
|
+
}>;
|
|
113
|
+
/**
|
|
114
|
+
* One keep-fresh attempt. Success means the stored access expiry MOVED FORWARD:
|
|
115
|
+
* an exit code of 0 proves a model answered, not that the store was rewritten.
|
|
116
|
+
* Pure over `exec` and the file reads, so the shape is tested without a login.
|
|
117
|
+
*/
|
|
118
|
+
export declare function refreshOnce(dir: string, now: () => number, exec?: ExecFn, baseEnv?: NodeJS.ProcessEnv): Promise<RefreshOutcome>;
|
|
119
|
+
/** Forget the gauge login (opt out). Refuses a path that is not ours. */
|
|
120
|
+
export declare function forgetGauge(dir: string): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* The runtime half: owns the dir, the login flow and the keeper. Built once by
|
|
123
|
+
* the server. `supported` is false on macOS, where the CLI keeps credentials in
|
|
124
|
+
* the keychain rather than the file we read.
|
|
125
|
+
*/
|
|
126
|
+
export interface Gauge {
|
|
127
|
+
readonly dir: string;
|
|
128
|
+
readonly login: LoginFlow;
|
|
129
|
+
/** The Bearer for the usage endpoint, or null. */
|
|
130
|
+
token(): string | null;
|
|
131
|
+
view(): GaugeView;
|
|
132
|
+
/** Start the keep-fresh timer. Returns a stop function. */
|
|
133
|
+
startKeeper(log?: (line: string) => void): () => void;
|
|
134
|
+
forget(): boolean;
|
|
135
|
+
/** One keep-fresh pass now; resolves when it settles. */
|
|
136
|
+
tick(log?: (line: string) => void): Promise<void>;
|
|
137
|
+
}
|
|
138
|
+
export declare function createGauge(opts?: {
|
|
139
|
+
home?: string;
|
|
140
|
+
key?: string;
|
|
141
|
+
now?: () => number;
|
|
142
|
+
exec?: ExecFn;
|
|
143
|
+
platform?: NodeJS.Platform;
|
|
144
|
+
onChange?: () => void;
|
|
145
|
+
}): Gauge;
|