@mupt-ai/dari-gateway 1.0.353 → 1.0.355
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 +5 -5
- package/dist/link/assets.js +0 -3
- package/dist/link/client.d.ts +2 -2
- package/dist/link/client.js +2 -2
- package/dist/link/endpoints.js +31 -8
- package/dist/link/errors.js +0 -1
- package/dist/link/self_hosted.js +11 -3
- package/dist/link/service.d.ts +3 -1
- package/dist/link/service.js +1 -0
- package/dist/models/catalog.js +0 -1
- package/dist/models/provider_defaults.js +0 -5
- package/dist/models/provider_models.json +0 -182
- package/dist/models/subscriptions.js +0 -1
- package/dist/platform/platform.d.ts +11 -0
- package/dist/providers/codex_usage.d.ts +6 -0
- package/dist/providers/codex_usage.js +2 -1
- package/dist/providers/execution.js +0 -3
- package/dist/providers/provider_call.js +1 -5
- package/dist/providers/subscription_limits.d.ts +1 -1
- package/dist/providers/subscription_limits.js +1 -4
- package/dist/providers/subscription_resets.d.ts +53 -0
- package/dist/providers/subscription_resets.js +184 -0
- package/dist/self_host/connected.d.ts +51 -0
- package/dist/self_host/connected.js +197 -0
- package/dist/self_host/hosted_sync.d.ts +53 -0
- package/dist/self_host/hosted_sync.js +261 -0
- package/dist/self_host/main.d.ts +5 -0
- package/dist/self_host/main.js +58 -10
- package/dist/self_host/platform.js +15 -2
- package/dist/self_host/resolve.js +0 -1
- package/dist/self_host/subscriptions/http.d.ts +4 -1
- package/dist/self_host/subscriptions/http.js +83 -11
- package/dist/self_host/subscriptions/index.d.ts +3 -3
- package/dist/self_host/subscriptions/index.js +3 -3
- package/dist/self_host/subscriptions/oauth.d.ts +3 -4
- package/dist/self_host/subscriptions/oauth.js +9 -76
- package/dist/self_host/subscriptions/resets.d.ts +41 -0
- package/dist/self_host/subscriptions/resets.js +235 -0
- package/dist/self_host/subscriptions/store.d.ts +61 -8
- package/dist/self_host/subscriptions/store.js +151 -26
- package/dist/self_host/subscriptions/usage.d.ts +3 -1
- package/dist/self_host/subscriptions/usage.js +12 -7
- package/dist/types.d.ts +1 -1
- package/package.json +2 -2
- package/src/link/assets.ts +0 -3
- package/src/link/client.ts +5 -3
- package/src/link/endpoints.ts +35 -7
- package/src/link/errors.ts +0 -1
- package/src/link/self_hosted.ts +9 -3
- package/src/link/service.ts +4 -0
- package/src/models/catalog.ts +0 -1
- package/src/models/provider_defaults.ts +0 -5
- package/src/models/provider_models.json +0 -182
- package/src/models/subscriptions.ts +0 -1
- package/src/platform/platform.ts +9 -0
- package/src/providers/codex_usage.ts +2 -1
- package/src/providers/execution.ts +0 -3
- package/src/providers/provider_call.ts +1 -5
- package/src/providers/subscription_limits.ts +1 -3
- package/src/providers/subscription_resets.ts +256 -0
- package/src/self_host/connected.ts +239 -0
- package/src/self_host/hosted_sync.ts +285 -0
- package/src/self_host/main.ts +55 -9
- package/src/self_host/platform.ts +13 -1
- package/src/self_host/resolve.ts +0 -1
- package/src/self_host/subscriptions/http.ts +85 -10
- package/src/self_host/subscriptions/index.ts +5 -2
- package/src/self_host/subscriptions/oauth.ts +8 -91
- package/src/self_host/subscriptions/resets.ts +254 -0
- package/src/self_host/subscriptions/store.ts +185 -27
- package/src/self_host/subscriptions/usage.ts +13 -7
- package/src/types.ts +0 -3
- package/dist/providers/github_copilot.d.ts +0 -8
- package/dist/providers/github_copilot.js +0 -21
- package/src/providers/github_copilot.ts +0 -24
|
@@ -6,12 +6,49 @@ import { openWithoutKey } from "../secret_box.js";
|
|
|
6
6
|
export const SWITCH_AT_UTILIZATION = 0.95;
|
|
7
7
|
// A conversation idle this long is forgotten; its next turn starts over.
|
|
8
8
|
const CONVERSATION_PIN_TTL_MS = 7 * 24 * 60 * 60_000;
|
|
9
|
+
/** Connecting an account the same user already has connected; the message says where it lives and how to move it. */
|
|
10
|
+
export class SubscriptionAccountConnectedError extends Error {
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Whether two plans are the same provider account: by account id when both
|
|
14
|
+
* carry one, otherwise by email (Claude Code sign-ins carry only an email).
|
|
15
|
+
*/
|
|
16
|
+
export function sameConnectedAccount(a, b) {
|
|
17
|
+
if (a.provider !== b.provider)
|
|
18
|
+
return false;
|
|
19
|
+
if (a.accountId !== null && b.accountId !== null)
|
|
20
|
+
return a.accountId === b.accountId;
|
|
21
|
+
return a.accountEmail !== null && b.accountEmail !== null
|
|
22
|
+
&& a.accountEmail.toLowerCase() === b.accountEmail.toLowerCase();
|
|
23
|
+
}
|
|
24
|
+
const PROVIDER_LABELS = {
|
|
25
|
+
anthropic_claude_code: "Claude",
|
|
26
|
+
openai_codex: "ChatGPT",
|
|
27
|
+
meta_muse: "Muse Code",
|
|
28
|
+
opencode_go: "OpenCode Go",
|
|
29
|
+
xai_grok: "Grok",
|
|
30
|
+
vercel_token_pass: "Vercel Token Pass",
|
|
31
|
+
};
|
|
32
|
+
function connectedAccountMessage(account) {
|
|
33
|
+
const label = PROVIDER_LABELS[account.provider];
|
|
34
|
+
const handle = account.name ?? "NAME";
|
|
35
|
+
const asName = account.name === null ? "" : ` as ${handle}`;
|
|
36
|
+
switch (account.place) {
|
|
37
|
+
case "dari":
|
|
38
|
+
return `This ${label} account is already connected on Dari${asName}. Move it with \`dari accounts subscription move ${handle} --on computer\`, or disconnect it first.`;
|
|
39
|
+
case "this_computer":
|
|
40
|
+
return `This ${label} account is already connected on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\`, or disconnect it first.`;
|
|
41
|
+
case "private":
|
|
42
|
+
return `This ${label} account is already connected Private on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\` or \`--on computer\`, or disconnect it first.`;
|
|
43
|
+
case "other_computer":
|
|
44
|
+
return `This ${label} account is already connected on another computer${account.gatewayName ? ` (${account.gatewayName})` : ""}. Disconnect it there, or move it to Dari there with \`dari accounts subscription move\`.`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
9
47
|
const BASE_NAMES = {
|
|
10
48
|
anthropic_claude_code: "ANTHROPIC_CLAUDE_CODE_OAUTH_TOKEN",
|
|
11
49
|
openai_codex: "OPENAI_CODEX_OAUTH_TOKEN",
|
|
12
50
|
meta_muse: "META_MUSE_OAUTH_TOKEN",
|
|
13
51
|
opencode_go: "OPENCODE_GO_OAUTH_TOKEN",
|
|
14
|
-
github_copilot: "GITHUB_COPILOT_OAUTH_TOKEN",
|
|
15
52
|
xai_grok: "XAI_GROK_OAUTH_TOKEN",
|
|
16
53
|
vercel_token_pass: "VERCEL_TOKEN_PASS_API_KEY",
|
|
17
54
|
};
|
|
@@ -24,13 +61,28 @@ const ADDED_COLUMNS = {
|
|
|
24
61
|
usage_windows: "TEXT",
|
|
25
62
|
model_holds: "TEXT",
|
|
26
63
|
owner: "TEXT",
|
|
64
|
+
private: "INTEGER",
|
|
65
|
+
auto_redeem_resets: "INTEGER",
|
|
27
66
|
};
|
|
28
67
|
export class SubscriptionStore {
|
|
29
68
|
db;
|
|
30
69
|
box;
|
|
31
|
-
|
|
70
|
+
options;
|
|
71
|
+
/**
|
|
72
|
+
* Told of each change hosted Dari's copy of the plans would show: `plans`
|
|
73
|
+
* for a plan connected, removed, renamed, moved, held, or released, and
|
|
74
|
+
* `usage` for reported usage or use.
|
|
75
|
+
*/
|
|
76
|
+
onChange;
|
|
77
|
+
/**
|
|
78
|
+
* `privatePlansServe: false` (a gateway connected to hosted Dari) leaves
|
|
79
|
+
* private plans out of routing and `list`; `list(provider, true)` still
|
|
80
|
+
* shows them.
|
|
81
|
+
*/
|
|
82
|
+
constructor(db, box = null, options = {}) {
|
|
32
83
|
this.db = db;
|
|
33
84
|
this.box = box;
|
|
85
|
+
this.options = options;
|
|
34
86
|
db.run(`CREATE TABLE IF NOT EXISTS subscription_credentials (
|
|
35
87
|
id TEXT PRIMARY KEY,
|
|
36
88
|
provider TEXT NOT NULL,
|
|
@@ -49,6 +101,8 @@ export class SubscriptionStore {
|
|
|
49
101
|
usage_windows TEXT,
|
|
50
102
|
model_holds TEXT,
|
|
51
103
|
owner TEXT,
|
|
104
|
+
private INTEGER,
|
|
105
|
+
auto_redeem_resets INTEGER,
|
|
52
106
|
position INTEGER NOT NULL,
|
|
53
107
|
created_at INTEGER NOT NULL,
|
|
54
108
|
updated_at INTEGER NOT NULL,
|
|
@@ -72,11 +126,13 @@ export class SubscriptionStore {
|
|
|
72
126
|
flow_json TEXT NOT NULL,
|
|
73
127
|
credential_name TEXT,
|
|
74
128
|
owner TEXT,
|
|
129
|
+
private INTEGER,
|
|
75
130
|
expires_at INTEGER NOT NULL
|
|
76
131
|
)`);
|
|
77
|
-
const sessionColumns = db.query("PRAGMA table_info(subscription_oauth_sessions)").all();
|
|
78
|
-
|
|
79
|
-
|
|
132
|
+
const sessionColumns = new Set(db.query("PRAGMA table_info(subscription_oauth_sessions)").all().map((column) => column.name));
|
|
133
|
+
for (const [column, type] of [["owner", "TEXT"], ["private", "INTEGER"]]) {
|
|
134
|
+
if (!sessionColumns.has(column))
|
|
135
|
+
db.run(`ALTER TABLE subscription_oauth_sessions ADD COLUMN ${column} ${type}`);
|
|
80
136
|
}
|
|
81
137
|
this.encryptStored();
|
|
82
138
|
}
|
|
@@ -129,9 +185,14 @@ export class SubscriptionStore {
|
|
|
129
185
|
refresh_token: this.open(row.refresh_token, tokenPlace(row.id, "refresh_token")),
|
|
130
186
|
});
|
|
131
187
|
}
|
|
132
|
-
/**
|
|
133
|
-
|
|
134
|
-
|
|
188
|
+
/**
|
|
189
|
+
* The gateway owner's subscriptions that serve, grouped by provider, in
|
|
190
|
+
* connection order; with `includePrivate`, private ones too even where they
|
|
191
|
+
* do not serve.
|
|
192
|
+
*/
|
|
193
|
+
list(provider, includePrivate = false) {
|
|
194
|
+
const privateShown = includePrivate || this.options.privatePlansServe !== false;
|
|
195
|
+
return this.every(provider).filter((credential) => credential.owner === null && (privateShown || !credential.private));
|
|
135
196
|
}
|
|
136
197
|
/** The subscriptions a Dari Link user connected. */
|
|
137
198
|
owned(owner) {
|
|
@@ -210,6 +271,7 @@ export class SubscriptionStore {
|
|
|
210
271
|
}
|
|
211
272
|
this.db.run(`UPDATE subscription_credentials SET usage_utilization = ?, usage_resets_at = ?, usage_observed_at = ?,
|
|
212
273
|
usage_windows = ? WHERE id = ?`, [usage.utilization, usage.resetsAt.getTime(), now.getTime(), windowsJson(usage), id]);
|
|
274
|
+
this.onChange?.("usage");
|
|
213
275
|
}
|
|
214
276
|
// A pin idle past CONVERSATION_PIN_TTL_MS is forgotten: the conversation is
|
|
215
277
|
// assigned like a new one.
|
|
@@ -227,28 +289,31 @@ export class SubscriptionStore {
|
|
|
227
289
|
}
|
|
228
290
|
/**
|
|
229
291
|
* Store tokens from a completed OAuth flow. `reconnectName` replaces that
|
|
230
|
-
* subscription (null when it is not connected)
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
292
|
+
* subscription (null when it is not connected) and keeps its place, as long
|
|
293
|
+
* as it is still the same account. Otherwise the sign-in is connected last
|
|
294
|
+
* in order under the first free name. A sign-in refused as a second copy
|
|
295
|
+
* throws `SubscriptionAccountConnectedError`: the same account is already
|
|
296
|
+
* connected for this owner (`owner` is the Dari Link user connecting it;
|
|
297
|
+
* null is the gateway owner), or `elsewhere` (the owner's plans on hosted
|
|
298
|
+
* Dari and other computers) holds it.
|
|
234
299
|
*/
|
|
235
|
-
connect(tokens, reconnectName, now = new Date(), owner = null) {
|
|
236
|
-
|
|
237
|
-
const existing = reconnectName !== null
|
|
238
|
-
? this.getByName(reconnectName, owner ?? null)
|
|
239
|
-
: this.every(tokens.provider).filter((credential) => credential.owner === owner).find((credential) => tokens.accountEmail !== null && credential.accountEmail === tokens.accountEmail) ?? null;
|
|
300
|
+
connect(tokens, reconnectName, now = new Date(), owner = null, isPrivate = false, elsewhere = []) {
|
|
301
|
+
const connected = this.db.transaction(() => {
|
|
302
|
+
const existing = reconnectName !== null ? this.getByName(reconnectName, owner ?? null) : null;
|
|
240
303
|
if (reconnectName !== null && existing?.provider !== tokens.provider)
|
|
241
304
|
return null;
|
|
242
305
|
if (existing !== null) {
|
|
243
|
-
// Another
|
|
244
|
-
//
|
|
245
|
-
//
|
|
246
|
-
const sameAccount =
|
|
306
|
+
// Another account must not inherit the previous account's holds,
|
|
307
|
+
// reported usage, or conversations (whose cache is the previous
|
|
308
|
+
// account's). Reconnecting the same account keeps them.
|
|
309
|
+
const sameAccount = sameConnectedAccount(existing, tokens);
|
|
310
|
+
if (!sameAccount)
|
|
311
|
+
this.refuseSecondCopy(tokens, owner ?? null, existing.id, elsewhere);
|
|
247
312
|
const usage = sameAccount ? existing.usage : null;
|
|
248
313
|
this.db.run(`UPDATE subscription_credentials SET access_token = ?, refresh_token = ?, expires_at = ?,
|
|
249
314
|
account_id = ?, account_email = ?, unavailable_until = ?, unavailable_reason = ?,
|
|
250
315
|
usage_utilization = ?, usage_resets_at = ?, usage_observed_at = ?, usage_windows = ?, model_holds = ?,
|
|
251
|
-
updated_at = ?
|
|
316
|
+
private = ?, updated_at = ?
|
|
252
317
|
WHERE id = ?`, [
|
|
253
318
|
this.seal(tokens.accessToken, tokenPlace(existing.id, "access_token")),
|
|
254
319
|
this.seal(tokens.refreshToken, tokenPlace(existing.id, "refresh_token")),
|
|
@@ -262,6 +327,7 @@ export class SubscriptionStore {
|
|
|
262
327
|
usage?.observedAt.getTime() ?? null,
|
|
263
328
|
usage === null ? null : windowsJson(usage),
|
|
264
329
|
sameAccount ? modelHoldsJson(existing.modelHolds) : null,
|
|
330
|
+
isPrivate ? 1 : 0,
|
|
265
331
|
now.getTime(),
|
|
266
332
|
existing.id,
|
|
267
333
|
]);
|
|
@@ -270,19 +336,21 @@ export class SubscriptionStore {
|
|
|
270
336
|
}
|
|
271
337
|
return this.get(existing.id);
|
|
272
338
|
}
|
|
339
|
+
this.refuseSecondCopy(tokens, owner ?? null, null, elsewhere);
|
|
273
340
|
const id = `cred_${randomUUID().replaceAll("-", "")}`;
|
|
274
341
|
const position = this.db
|
|
275
342
|
.query("SELECT COALESCE(MAX(position), 0) + 1 AS next FROM subscription_credentials")
|
|
276
343
|
.get().next;
|
|
277
|
-
this.db.run(`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner,
|
|
278
|
-
refresh_token, expires_at, position, created_at, updated_at)
|
|
279
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
344
|
+
this.db.run(`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner, private,
|
|
345
|
+
access_token, refresh_token, expires_at, position, created_at, updated_at)
|
|
346
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
280
347
|
id,
|
|
281
348
|
tokens.provider,
|
|
282
349
|
this.freeName(BASE_NAMES[tokens.provider]),
|
|
283
350
|
tokens.accountEmail,
|
|
284
351
|
tokens.accountId,
|
|
285
352
|
owner,
|
|
353
|
+
isPrivate ? 1 : 0,
|
|
286
354
|
this.seal(tokens.accessToken, tokenPlace(id, "access_token")),
|
|
287
355
|
this.seal(tokens.refreshToken, tokenPlace(id, "refresh_token")),
|
|
288
356
|
tokens.expiresAt.getTime(),
|
|
@@ -292,6 +360,27 @@ export class SubscriptionStore {
|
|
|
292
360
|
]);
|
|
293
361
|
return this.get(id);
|
|
294
362
|
})();
|
|
363
|
+
if (connected !== null)
|
|
364
|
+
this.onChange?.("plans");
|
|
365
|
+
return connected;
|
|
366
|
+
}
|
|
367
|
+
// Throws when the owner's other plans, or `elsewhere`, already hold this
|
|
368
|
+
// account. `exceptId` is the plan being reconnected, which may not be a copy.
|
|
369
|
+
refuseSecondCopy(tokens, owner, exceptId, elsewhere) {
|
|
370
|
+
const local = this.every(tokens.provider).find((credential) => credential.owner === owner && credential.id !== exceptId && sameConnectedAccount(credential, tokens));
|
|
371
|
+
if (local !== undefined) {
|
|
372
|
+
throw new SubscriptionAccountConnectedError(connectedAccountMessage({
|
|
373
|
+
provider: local.provider,
|
|
374
|
+
accountId: local.accountId,
|
|
375
|
+
accountEmail: local.accountEmail,
|
|
376
|
+
place: local.private ? "private" : "this_computer",
|
|
377
|
+
name: local.name,
|
|
378
|
+
gatewayName: null,
|
|
379
|
+
}));
|
|
380
|
+
}
|
|
381
|
+
const other = elsewhere.find((account) => sameConnectedAccount(account, tokens));
|
|
382
|
+
if (other !== undefined)
|
|
383
|
+
throw new SubscriptionAccountConnectedError(connectedAccountMessage(other));
|
|
295
384
|
}
|
|
296
385
|
/** Persist refreshed tokens. Keeps the stored account id when the refresh carries none. */
|
|
297
386
|
updateTokens(id, tokens, now = new Date()) {
|
|
@@ -307,6 +396,7 @@ export class SubscriptionStore {
|
|
|
307
396
|
}
|
|
308
397
|
touch(id, now = new Date()) {
|
|
309
398
|
this.db.run("UPDATE subscription_credentials SET last_used_at = ? WHERE id = ?", [now.getTime(), id]);
|
|
399
|
+
this.onChange?.("usage");
|
|
310
400
|
}
|
|
311
401
|
/** An empty label clears it. Null when the subscription is not connected. */
|
|
312
402
|
rename(name, label, now = new Date()) {
|
|
@@ -322,6 +412,32 @@ export class SubscriptionStore {
|
|
|
322
412
|
now.getTime(),
|
|
323
413
|
credential.id,
|
|
324
414
|
]);
|
|
415
|
+
this.onChange?.("plans");
|
|
416
|
+
return this.get(credential.id);
|
|
417
|
+
}
|
|
418
|
+
/** Make the gateway owner's subscription private, or not. Null when it is not connected. */
|
|
419
|
+
setPrivate(name, isPrivate, now = new Date()) {
|
|
420
|
+
const credential = this.getByName(name);
|
|
421
|
+
if (credential === null)
|
|
422
|
+
return null;
|
|
423
|
+
this.db.run("UPDATE subscription_credentials SET private = ?, updated_at = ? WHERE id = ?", [
|
|
424
|
+
isPrivate ? 1 : 0,
|
|
425
|
+
now.getTime(),
|
|
426
|
+
credential.id,
|
|
427
|
+
]);
|
|
428
|
+
this.onChange?.("plans");
|
|
429
|
+
return this.get(credential.id);
|
|
430
|
+
}
|
|
431
|
+
/** Let routing spend the plan's banked resets, or not. Null when it is not connected. */
|
|
432
|
+
setAutoRedeemResets(name, enabled, now = new Date()) {
|
|
433
|
+
const credential = this.getByName(name);
|
|
434
|
+
if (credential === null)
|
|
435
|
+
return null;
|
|
436
|
+
this.db.run("UPDATE subscription_credentials SET auto_redeem_resets = ?, updated_at = ? WHERE id = ?", [
|
|
437
|
+
enabled ? 1 : 0,
|
|
438
|
+
now.getTime(),
|
|
439
|
+
credential.id,
|
|
440
|
+
]);
|
|
325
441
|
return this.get(credential.id);
|
|
326
442
|
}
|
|
327
443
|
/** Remove a subscription. Returns what was removed, or null when it was not connected. */
|
|
@@ -331,6 +447,7 @@ export class SubscriptionStore {
|
|
|
331
447
|
return null;
|
|
332
448
|
this.db.run("DELETE FROM subscription_credentials WHERE id = ?", [credential.id]);
|
|
333
449
|
this.db.run("DELETE FROM subscription_conversation_pins WHERE credential_id = ?", [credential.id]);
|
|
450
|
+
this.onChange?.("plans");
|
|
334
451
|
return credential;
|
|
335
452
|
}
|
|
336
453
|
/**
|
|
@@ -344,6 +461,7 @@ export class SubscriptionStore {
|
|
|
344
461
|
return false;
|
|
345
462
|
if (!isHeld(credential, now) || until > credential.unavailableUntil) {
|
|
346
463
|
this.db.run("UPDATE subscription_credentials SET unavailable_until = ?, unavailable_reason = ?, updated_at = ? WHERE id = ?", [until.getTime(), reason, now.getTime(), id]);
|
|
464
|
+
this.onChange?.("plans");
|
|
347
465
|
}
|
|
348
466
|
return this.activeCredential(credential.provider, now) !== null;
|
|
349
467
|
}
|
|
@@ -365,6 +483,7 @@ export class SubscriptionStore {
|
|
|
365
483
|
now.getTime(),
|
|
366
484
|
id,
|
|
367
485
|
]);
|
|
486
|
+
this.onChange?.("plans");
|
|
368
487
|
}
|
|
369
488
|
return true;
|
|
370
489
|
}
|
|
@@ -375,17 +494,20 @@ export class SubscriptionStore {
|
|
|
375
494
|
return null;
|
|
376
495
|
this.db.run(`UPDATE subscription_credentials SET unavailable_until = NULL, unavailable_reason = NULL, model_holds = NULL,
|
|
377
496
|
updated_at = ? WHERE id = ?`, [now.getTime(), credential.id]);
|
|
497
|
+
this.onChange?.("plans");
|
|
378
498
|
return this.get(credential.id);
|
|
379
499
|
}
|
|
380
500
|
createSession(session, now = new Date()) {
|
|
381
501
|
const token = randomBytes(32).toString("base64url");
|
|
382
502
|
this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
|
|
383
|
-
this.db.run(
|
|
503
|
+
this.db.run(`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, private, expires_at)
|
|
504
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
|
384
505
|
token,
|
|
385
506
|
session.provider,
|
|
386
507
|
this.seal(JSON.stringify(session.flow), flowPlace(token)),
|
|
387
508
|
session.credentialName,
|
|
388
509
|
session.owner,
|
|
510
|
+
session.private ? 1 : 0,
|
|
389
511
|
session.expiresAt.getTime(),
|
|
390
512
|
]);
|
|
391
513
|
return { token, ...session };
|
|
@@ -403,6 +525,7 @@ export class SubscriptionStore {
|
|
|
403
525
|
flow: JSON.parse(this.open(row.flow_json, flowPlace(row.token))),
|
|
404
526
|
credentialName: row.credential_name,
|
|
405
527
|
owner: row.owner,
|
|
528
|
+
private: row.private === 1,
|
|
406
529
|
expiresAt: new Date(row.expires_at),
|
|
407
530
|
};
|
|
408
531
|
}
|
|
@@ -502,6 +625,8 @@ function credentialFromRow(row) {
|
|
|
502
625
|
accountEmail: row.account_email,
|
|
503
626
|
accountId: row.account_id,
|
|
504
627
|
owner: row.owner,
|
|
628
|
+
private: row.private === 1,
|
|
629
|
+
autoRedeemResets: row.auto_redeem_resets === 1,
|
|
505
630
|
accessToken: row.access_token,
|
|
506
631
|
refreshToken: row.refresh_token,
|
|
507
632
|
expiresAt: new Date(row.expires_at),
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { OAuthFetch } from "./oauth.js";
|
|
2
|
+
import type { ProviderBaseUrls } from "./resets.js";
|
|
2
3
|
import type { SubscriptionStore } from "./store.js";
|
|
3
|
-
/** Read every stale OpenCode Go and Token Pass plan again; a failed read keeps the last one. */
|
|
4
|
+
/** Read every stale Claude Code, OpenCode Go, and Token Pass plan again; a failed read keeps the last one. */
|
|
4
5
|
export declare function refreshStaleUsage(store: SubscriptionStore, options: {
|
|
5
6
|
fetch: OAuthFetch;
|
|
6
7
|
now?: Date;
|
|
8
|
+
baseUrls?: ProviderBaseUrls;
|
|
7
9
|
}): Promise<void>;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { openCodeGoUsageFromReport, tokenPassUsageFromCredits } from "../../providers/subscription_limits.js";
|
|
2
|
+
import { claudeUsageFromReport, claudeUsageReport } from "../../providers/subscription_resets.js";
|
|
2
3
|
import { refreshedAccessToken } from "./access.js";
|
|
3
4
|
/**
|
|
4
5
|
* Claude Code, ChatGPT, and Muse Code report usage on the responses the
|
|
5
6
|
* gateway serves. OpenCode Go and Vercel Token Pass do not: Go reports it from
|
|
6
7
|
* a usage endpoint that takes the plan's workspace, and Token Pass its
|
|
7
|
-
* remaining credit from AI Gateway's credits endpoint.
|
|
8
|
-
*
|
|
8
|
+
* remaining credit from AI Gateway's credits endpoint. A Claude Code plan
|
|
9
|
+
* that served nothing lately reports it from Claude Code's usage endpoint, as
|
|
10
|
+
* hosted Dari reads it. The accounts view reads these again when the last
|
|
11
|
+
* reading is stale.
|
|
9
12
|
*/
|
|
10
13
|
// A read, failed or not, is not repeated within this interval.
|
|
11
14
|
const READ_INTERVAL_MS = 60_000;
|
|
@@ -16,11 +19,11 @@ const TOKEN_PASS_CREDITS_URL = "https://ai-gateway.vercel.sh/v1/credits";
|
|
|
16
19
|
// not re-read on every listing. A recorded reading stamps the credential
|
|
17
20
|
// itself, which a reconnect clears.
|
|
18
21
|
const unrecordedAt = new Map();
|
|
19
|
-
/** Read every stale OpenCode Go and Token Pass plan again; a failed read keeps the last one. */
|
|
22
|
+
/** Read every stale Claude Code, OpenCode Go, and Token Pass plan again; a failed read keeps the last one. */
|
|
20
23
|
export async function refreshStaleUsage(store, options) {
|
|
21
24
|
const now = options.now ?? new Date();
|
|
22
25
|
const due = (at) => now.getTime() - (at ?? 0) >= READ_INTERVAL_MS;
|
|
23
|
-
const stale = [...store.list("opencode_go"), ...store.list("vercel_token_pass")].filter((credential) => due(credential.usage?.observedAt.getTime()) && due(unrecordedAt.get(credential.id)));
|
|
26
|
+
const stale = [...store.list("anthropic_claude_code"), ...store.list("opencode_go"), ...store.list("vercel_token_pass")].filter((credential) => due(credential.usage?.observedAt.getTime()) && due(unrecordedAt.get(credential.id)));
|
|
24
27
|
await Promise.all(stale.map(async (credential) => {
|
|
25
28
|
unrecordedAt.set(credential.id, now.getTime());
|
|
26
29
|
try {
|
|
@@ -33,9 +36,11 @@ export async function refreshStaleUsage(store, options) {
|
|
|
33
36
|
}
|
|
34
37
|
async function readUsage(store, credential, options) {
|
|
35
38
|
const token = await refreshedAccessToken(store, credential, options);
|
|
36
|
-
const usage = credential.provider === "
|
|
37
|
-
? await
|
|
38
|
-
:
|
|
39
|
+
const usage = credential.provider === "anthropic_claude_code"
|
|
40
|
+
? claudeUsageFromReport(await claudeUsageReport(options.fetch, token, options.baseUrls?.claude), options.now)
|
|
41
|
+
: credential.provider === "vercel_token_pass"
|
|
42
|
+
? await readTokenPassUsage(token, options)
|
|
43
|
+
: await readOpenCodeGoUsage(credential, token, options);
|
|
39
44
|
if (usage === null)
|
|
40
45
|
return;
|
|
41
46
|
unrecordedAt.delete(credential.id);
|
package/dist/types.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export type AwsAssumeRoleProviderAuth = {
|
|
|
18
18
|
region: string;
|
|
19
19
|
};
|
|
20
20
|
export type ResolvedProviderAuth = BearerProviderAuth | AwsSigV4ProviderAuth;
|
|
21
|
-
export type SubscriptionKind = "openai_codex" | "anthropic_claude_code" | "meta_muse" | "opencode_go" | "
|
|
21
|
+
export type SubscriptionKind = "openai_codex" | "anthropic_claude_code" | "meta_muse" | "opencode_go" | "xai_grok" | "vercel_token_pass";
|
|
22
22
|
export type ProviderCredential = {
|
|
23
23
|
apiKey: string;
|
|
24
24
|
baseUrl?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mupt-ai/dari-gateway",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.355",
|
|
4
4
|
"description": "Self-hostable LLM gateway for coding agents and APIs: one endpoint for Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses, with subscription pooling, failover, and prompt-cache-aware routing.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@aws-sdk/client-sts": "3.1063.0",
|
|
55
|
-
"@mupt-ai/dari-router": "1.0.
|
|
55
|
+
"@mupt-ai/dari-router": "1.0.355",
|
|
56
56
|
"@mupt-ai/pi-ai": "1.5.2-mupt.3"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
package/src/link/assets.ts
CHANGED
|
@@ -11,7 +11,6 @@ const MARKS: Record<string, string> = {
|
|
|
11
11
|
anthropic: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M13.827 3.52h3.603L24 20h-3.603l-6.57-16.48zm-7.258 0h3.767L16.906 20h-3.674l-1.343-3.461H5.017l-1.344 3.46H0L6.57 3.522zm4.132 9.959L8.453 7.687 6.205 13.48H10.7z"></path></svg>`,
|
|
12
12
|
claude: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M4.709 15.955l4.72-2.647.08-.23-.08-.128H9.2l-.79-.048-2.698-.073-2.339-.097-2.266-.122-.571-.121L0 11.784l.055-.352.48-.321.686.06 1.52.103 2.278.158 1.652.097 2.449.255h.389l.055-.157-.134-.098-.103-.097-2.358-1.596-2.552-1.688-1.336-.972-.724-.491-.364-.462-.158-1.008.656-.722.881.06.225.061.893.686 1.908 1.476 2.491 1.833.365.304.145-.103.019-.073-.164-.274-1.355-2.446-1.446-2.49-.644-1.032-.17-.619a2.97 2.97 0 01-.104-.729L6.283.134 6.696 0l.996.134.42.364.62 1.414 1.002 2.229 1.555 3.03.456.898.243.832.091.255h.158V9.01l.128-1.706.237-2.095.23-2.695.08-.76.376-.91.747-.492.584.28.48.685-.067.444-.286 1.851-.559 2.903-.364 1.942h.212l.243-.242.985-1.306 1.652-2.064.73-.82.85-.904.547-.431h1.033l.76 1.129-.34 1.166-1.064 1.347-.881 1.142-1.264 1.7-.79 1.36.073.11.188-.02 2.856-.606 1.543-.28 1.841-.315.833.388.091.395-.328.807-1.969.486-2.309.462-3.439.813-.042.03.049.061 1.549.146.662.036h1.622l3.02.225.79.522.474.638-.079.485-1.215.62-1.64-.389-3.829-.91-1.312-.329h-.182v.11l1.093 1.068 2.006 1.81 2.509 2.33.127.578-.322.455-.34-.049-2.205-1.657-.851-.747-1.926-1.62h-.128v.17l.444.649 2.345 3.521.122 1.08-.17.353-.608.213-.668-.122-1.374-1.925-1.415-2.167-1.143-1.943-.14.08-.674 7.254-.316.37-.729.28-.607-.461-.322-.747.322-1.476.389-1.924.315-1.53.286-1.9.17-.632-.012-.042-.14.018-1.434 1.967-2.18 2.945-1.726 1.845-.414.164-.717-.37.067-.662.401-.589 2.388-3.036 1.44-1.882.93-1.086-.006-.158h-.055L4.132 18.56l-1.13.146-.487-.456.061-.746.231-.243 1.908-1.312-.006.006z"></path></svg>`,
|
|
13
13
|
gemini: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M12 24A14.304 14.304 0 000 12 14.304 14.304 0 0012 0a14.305 14.305 0 0012 12 14.305 14.305 0 00-12 12"></path></svg>`,
|
|
14
|
-
githubcopilot: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M19.245 5.364c1.322 1.36 1.877 3.216 2.11 5.817.622 0 1.2.135 1.592.654l.73.964c.21.278.323.61.323.955v2.62c0 .339-.173.669-.453.868C20.239 19.602 16.157 21.5 12 21.5c-4.6 0-9.205-2.583-11.547-4.258-.28-.2-.452-.53-.453-.868v-2.62c0-.345.113-.679.321-.956l.73-.963c.392-.517.974-.654 1.593-.654l.029-.297c.25-2.446.81-4.213 2.082-5.52 2.461-2.54 5.71-2.851 7.146-2.864h.198c1.436.013 4.685.323 7.146 2.864zm-7.244 4.328c-.284 0-.613.016-.962.05-.123.447-.305.85-.57 1.108-1.05 1.023-2.316 1.18-2.994 1.18-.638 0-1.306-.13-1.851-.464-.516.165-1.012.403-1.044.996a65.882 65.882 0 00-.063 2.884l-.002.48c-.002.563-.005 1.126-.013 1.69.002.326.204.63.51.765 2.482 1.102 4.83 1.657 6.99 1.657 2.156 0 4.504-.555 6.985-1.657a.854.854 0 00.51-.766c.03-1.682.006-3.372-.076-5.053-.031-.596-.528-.83-1.046-.996-.546.333-1.212.464-1.85.464-.677 0-1.942-.157-2.993-1.18-.266-.258-.447-.661-.57-1.108-.32-.032-.64-.049-.96-.05zm-2.525 4.013c.539 0 .976.426.976.95v1.753c0 .525-.437.95-.976.95a.964.964 0 01-.976-.95v-1.752c0-.525.437-.951.976-.951zm5 0c.539 0 .976.426.976.95v1.753c0 .525-.437.95-.976.95a.964.964 0 01-.976-.95v-1.752c0-.525.437-.951.976-.951zM7.635 5.087c-1.05.102-1.935.438-2.385.906-.975 1.037-.765 3.668-.21 4.224.405.394 1.17.657 1.995.657h.09c.649-.013 1.785-.176 2.73-1.11.435-.41.705-1.433.675-2.47-.03-.834-.27-1.52-.63-1.813-.39-.336-1.275-.482-2.265-.394zm6.465.394c-.36.292-.6.98-.63 1.813-.03 1.037.24 2.06.675 2.47.968.957 2.136 1.104 2.776 1.11h.044c.825 0 1.59-.263 1.995-.657.555-.556.765-3.187-.21-4.224-.45-.468-1.335-.804-2.385-.906-.99-.088-1.875.058-2.265.394zM12 7.615c-.24 0-.525.015-.84.044.03.16.045.336.06.526l-.001.159a2.94 2.94 0 01-.014.25c.225-.022.425-.027.612-.028h.366c.187 0 .387.006.612.028-.015-.146-.015-.277-.015-.409.015-.19.03-.365.06-.526a9.29 9.29 0 00-.84-.044z"></path></svg>`,
|
|
15
14
|
grok: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M9.27 15.29l7.978-5.897c.391-.29.95-.177 1.137.272.98 2.369.542 5.215-1.41 7.169-1.951 1.954-4.667 2.382-7.149 1.406l-2.711 1.257c3.889 2.661 8.611 2.003 11.562-.953 2.341-2.344 3.066-5.539 2.388-8.42l.006.007c-.983-4.232.242-5.924 2.75-9.383.06-.082.12-.164.179-.248l-3.301 3.305v-.01L9.267 15.292M7.623 16.723c-2.792-2.67-2.31-6.801.071-9.184 1.761-1.763 4.647-2.483 7.166-1.425l2.705-1.25a7.808 7.808 0 00-1.829-1A8.975 8.975 0 005.984 5.83c-2.533 2.536-3.33 6.436-1.962 9.764 1.022 2.487-.653 4.246-2.34 6.022-.599.63-1.199 1.259-1.682 1.925l7.62-6.815"></path></svg>`,
|
|
16
15
|
meta: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M6.897 4c1.915 0 3.516.932 5.43 3.376l.282-.373c.19-.246.383-.484.58-.71l.313-.35C14.588 4.788 15.792 4 17.225 4c1.273 0 2.469.557 3.491 1.516l.218.213c1.73 1.765 2.917 4.71 3.053 8.026l.011.392.002.25c0 1.501-.28 2.759-.818 3.7l-.14.23-.108.153c-.301.42-.664.758-1.086 1.009l-.265.142-.087.04a3.493 3.493 0 01-.302.118 4.117 4.117 0 01-1.33.208c-.524 0-.996-.067-1.438-.215-.614-.204-1.163-.56-1.726-1.116l-.227-.235c-.753-.812-1.534-1.976-2.493-3.586l-1.43-2.41-.544-.895-1.766 3.13-.343.592C7.597 19.156 6.227 20 4.356 20c-1.21 0-2.205-.42-2.936-1.182l-.168-.184c-.484-.573-.837-1.311-1.043-2.189l-.067-.32a8.69 8.69 0 01-.136-1.288L0 14.468c.002-.745.06-1.49.174-2.23l.1-.573c.298-1.53.828-2.958 1.536-4.157l.209-.34c1.177-1.83 2.789-3.053 4.615-3.16L6.897 4zm-.033 2.615l-.201.01c-.83.083-1.606.673-2.252 1.577l-.138.199-.01.018c-.67 1.017-1.185 2.378-1.456 3.845l-.004.022a12.591 12.591 0 00-.207 2.254l.002.188c.004.18.017.36.04.54l.043.291c.092.503.257.908.486 1.208l.117.137c.303.323.698.492 1.17.492 1.1 0 1.796-.676 3.696-3.641l2.175-3.4.454-.701-.139-.198C9.11 7.3 8.084 6.616 6.864 6.616zm10.196-.552l-.176.007c-.635.048-1.223.359-1.82.933l-.196.198c-.439.462-.887 1.064-1.367 1.807l.266.398c.18.274.362.56.55.858l.293.475 1.396 2.335.695 1.114c.583.926 1.03 1.6 1.408 2.082l.213.262c.282.326.529.54.777.673l.102.05c.227.1.457.138.718.138.176.002.35-.023.518-.073.338-.104.61-.32.813-.637l.095-.163.077-.162c.194-.459.29-1.06.29-1.785l-.006-.449c-.08-2.871-.938-5.372-2.2-6.798l-.176-.189c-.67-.683-1.444-1.074-2.27-1.074z"></path></svg>`,
|
|
17
16
|
moonshot: `<svg class="logo" viewBox="0 0 24 24" fill="currentColor" fill-rule="evenodd" aria-hidden="true"><path d="M1.052 16.916l9.539 2.552a21.007 21.007 0 00.06 2.033l5.956 1.593a11.997 11.997 0 01-5.586.865l-.18-.016-.044-.004-.084-.009-.094-.01a11.605 11.605 0 01-.157-.02l-.107-.014-.11-.016a11.962 11.962 0 01-.32-.051l-.042-.008-.075-.013-.107-.02-.07-.015-.093-.019-.075-.016-.095-.02-.097-.023-.094-.022-.068-.017-.088-.022-.09-.024-.095-.025-.082-.023-.109-.03-.062-.02-.084-.025-.093-.028-.105-.034-.058-.019-.08-.026-.09-.031-.066-.024a6.293 6.293 0 01-.044-.015l-.068-.025-.101-.037-.057-.022-.08-.03-.087-.035-.088-.035-.079-.032-.095-.04-.063-.028-.063-.027a5.655 5.655 0 01-.041-.018l-.066-.03-.103-.047-.052-.024-.096-.046-.062-.03-.084-.04-.086-.044-.093-.047-.052-.027-.103-.055-.057-.03-.058-.032a6.49 6.49 0 01-.046-.026l-.094-.053-.06-.034-.051-.03-.072-.041-.082-.05-.093-.056-.052-.032-.084-.053-.061-.039-.079-.05-.07-.047-.053-.035a7.785 7.785 0 01-.054-.036l-.044-.03-.044-.03a6.066 6.066 0 01-.04-.028l-.057-.04-.076-.054-.069-.05-.074-.054-.056-.042-.076-.057-.076-.059-.086-.067-.045-.035-.064-.052-.074-.06-.089-.073-.046-.039-.046-.039a7.516 7.516 0 01-.043-.037l-.045-.04-.061-.053-.07-.062-.068-.06-.062-.058-.067-.062-.053-.05-.088-.084a13.28 13.28 0 01-.099-.097l-.029-.028-.041-.042-.069-.07-.05-.051-.05-.053a6.457 6.457 0 01-.168-.179l-.08-.088-.062-.07-.071-.08-.042-.049-.053-.062-.058-.068-.046-.056a7.175 7.175 0 01-.027-.033l-.045-.055-.066-.082-.041-.052-.05-.064-.02-.025a11.99 11.99 0 01-1.44-2.402zm-1.02-5.794l11.353 3.037a20.468 20.468 0 00-.469 2.011l10.817 2.894a12.076 12.076 0 01-1.845 2.005L.657 15.923l-.016-.046-.035-.104a11.965 11.965 0 01-.05-.153l-.007-.023a11.896 11.896 0 01-.207-.741l-.03-.126-.018-.08-.021-.097-.018-.081-.018-.09-.017-.084-.018-.094c-.026-.141-.05-.283-.071-.426l-.017-.118-.011-.083-.013-.102a12.01 12.01 0 01-.019-.161l-.005-.047a12.12 12.12 0 01-.034-2.145zm1.593-5.15l11.948 3.196c-.368.605-.705 1.231-1.01 1.875l11.295 3.022c-.142.82-.368 1.612-.668 2.365l-11.55-3.09L.124 10.26l.015-.1.008-.049.01-.067.015-.087.018-.098c.026-.148.056-.295.088-.442l.028-.124.02-.085.024-.097c.022-.09.045-.18.07-.268l.028-.102.023-.083.03-.1.025-.082.03-.096.026-.082.031-.095a11.896 11.896 0 011.01-2.232zm4.442-4.4L17.352 4.59a20.77 20.77 0 00-1.688 1.721l7.823 2.093c.267.852.442 1.744.513 2.665L2.106 5.213l.045-.065.027-.04.04-.055.046-.065.055-.076.054-.072.064-.086.05-.065.057-.073.055-.07.06-.074.055-.069.065-.077.054-.066.066-.077.053-.06.072-.082.053-.06.067-.074.054-.058.073-.078.058-.06.063-.067.168-.17.1-.098.059-.056.076-.071a12.084 12.084 0 012.272-1.677zM12.017 0h.097l.082.001.069.001.054.002.068.002.046.001.076.003.047.002.06.003.054.002.087.005.105.007.144.011.088.007.044.004.077.008.082.008.047.005.102.012.05.006.108.014.081.01.042.006.065.01.207.032.07.012.065.011.14.026.092.018.11.022.046.01.075.016.041.01L14.7.3l.042.01.065.015.049.012.071.017.096.024.112.03.113.03.113.032.05.015.07.02.078.024.073.023.05.016.05.016.076.025.099.033.102.036.048.017.064.023.093.034.11.041.116.045.1.04.047.02.06.024.041.018.063.026.04.018.057.025.11.048.1.046.074.035.075.036.06.028.092.046.091.045.102.052.053.028.049.026.046.024.06.033.041.022.052.029.088.05.106.06.087.051.057.034.053.032.096.059.088.055.098.062.036.024.064.041.084.056.04.027.062.042.062.043.023.017c.054.037.108.075.161.114l.083.06.065.048.056.043.086.065.082.064.04.03.05.041.086.069.079.065.085.071c.712.6 1.353 1.283 1.909 2.031L7.222.994l.062-.027.065-.028.081-.034.086-.035c.113-.045.227-.09.341-.131l.096-.035.093-.033.084-.03.096-.031c.087-.03.176-.058.264-.085l.091-.027.086-.025.102-.03.085-.023.1-.026L9.04.37l.09-.023.091-.022.095-.022.09-.02.098-.021.091-.02.095-.018.092-.018.1-.018.091-.016.098-.017.092-.014.097-.015.092-.013.102-.013.091-.012.105-.012.09-.01.105-.01c.093-.01.186-.018.28-.024l.106-.008.09-.005.11-.006.093-.004.1-.004.097-.002.099-.002.197-.002z"></path></svg>`,
|
|
@@ -28,7 +27,6 @@ const MARK_FOR: Record<string, string> = {
|
|
|
28
27
|
openai_codex: "openai",
|
|
29
28
|
meta_muse: "meta",
|
|
30
29
|
opencode_go: "opencode",
|
|
31
|
-
github_copilot: "githubcopilot",
|
|
32
30
|
xai_grok: "grok",
|
|
33
31
|
vercel_token_pass: "vercel",
|
|
34
32
|
anthropic: "anthropic",
|
|
@@ -39,7 +37,6 @@ const MARK_FOR: Record<string, string> = {
|
|
|
39
37
|
google: "gemini",
|
|
40
38
|
openrouter: "openrouter",
|
|
41
39
|
"vercel-ai-gateway": "vercel",
|
|
42
|
-
"github-copilot": "githubcopilot",
|
|
43
40
|
"vercel-token-pass": "vercel",
|
|
44
41
|
};
|
|
45
42
|
|
package/src/link/client.ts
CHANGED
|
@@ -181,7 +181,7 @@ export async function registerClient(
|
|
|
181
181
|
* The approval URL to send the user to, with a fresh PKCE verifier and state.
|
|
182
182
|
* `clients` names the clients the app runs, such as `claude_code`, so the
|
|
183
183
|
* user can grant plans that serve only those. `plans` limits the page to
|
|
184
|
-
* those plan kinds, such as `openai_codex`. `purposes` says what the app does
|
|
184
|
+
* those plan kinds, such as `openai_codex`, or `"supported"`. `purposes` says what the app does
|
|
185
185
|
* with each audience's plans, shown beside them. `grantId` (update mode)
|
|
186
186
|
* opens the page on that existing grant: the user changes its plans in place,
|
|
187
187
|
* its id and tokens stay, and the new code redeems for more tokens of it.
|
|
@@ -226,7 +226,9 @@ export type AuthorizeOptions = {
|
|
|
226
226
|
redirectUri: string;
|
|
227
227
|
state?: string;
|
|
228
228
|
clients?: string[];
|
|
229
|
-
|
|
229
|
+
// Plan kinds to offer, such as ["openai_codex", "api_key"], or "supported"
|
|
230
|
+
// for ChatGPT, Grok, OpenCode Go, Vercel Token Pass and every API key; omit to offer every kind.
|
|
231
|
+
plans?: string[] | "supported";
|
|
230
232
|
// The providers and gateways whose API keys the app can use, such as
|
|
231
233
|
// ["anthropic", "openrouter"]; omit to offer every one.
|
|
232
234
|
keyProviders?: string[];
|
|
@@ -260,7 +262,7 @@ function authorizationUrl(args: AuthorizeOptions & { codeChallenge: string; stat
|
|
|
260
262
|
scope: "route",
|
|
261
263
|
...(args.responseMode === "web_message" ? { response_mode: "web_message" } : {}),
|
|
262
264
|
...(args.clients?.length ? { client: args.clients.join(" ") } : {}),
|
|
263
|
-
...(args.plans?.length ? { plans: args.plans.join(" ") } : {}),
|
|
265
|
+
...(args.plans?.length ? { plans: [args.plans].flat().join(" ") } : {}),
|
|
264
266
|
...(args.keyProviders?.length ? { key_providers: args.keyProviders.join(" ") } : {}),
|
|
265
267
|
...(args.grantId ? { grant_id: args.grantId } : {}),
|
|
266
268
|
...Object.fromEntries(
|
package/src/link/endpoints.ts
CHANGED
|
@@ -163,6 +163,10 @@ const PLAN_KINDS = Object.keys(SUBSCRIPTION_PLAN_LABELS) as SubscriptionKind[];
|
|
|
163
163
|
const API_KEY = "api_key";
|
|
164
164
|
type OfferKind = SubscriptionKind | typeof API_KEY;
|
|
165
165
|
const OFFER_KINDS: readonly OfferKind[] = [...PLAN_KINDS, API_KEY];
|
|
166
|
+
// `plans=supported` offers the plans whose providers clearly allow use in
|
|
167
|
+
// other apps, and every API key.
|
|
168
|
+
const SUPPORTED = "supported";
|
|
169
|
+
const SUPPORTED_KINDS: readonly OfferKind[] = ["openai_codex", "xai_grok", "opencode_go", "vercel_token_pass", API_KEY];
|
|
166
170
|
// Who a plan serves, as `serves` on a grant and as `purpose_<audience>` on
|
|
167
171
|
// the authorize request.
|
|
168
172
|
type PlanAudience = "any_app" | "claude_code" | "muse_code";
|
|
@@ -1265,9 +1269,9 @@ async function authorizeRequest(link: LinkPlatform, params: URLSearchParams): Pr
|
|
|
1265
1269
|
if (!clients.every((client) => (CLIENTS as readonly string[]).includes(client))) {
|
|
1266
1270
|
return badRequest(`client lists the clients the app runs: ${CLIENTS.join(", ")}.`);
|
|
1267
1271
|
}
|
|
1268
|
-
const plans = listParam(params.get("plans"));
|
|
1272
|
+
const plans = [...new Set(listParam(params.get("plans")).flatMap((plan): string[] => plan === SUPPORTED ? [...SUPPORTED_KINDS] : [plan]))];
|
|
1269
1273
|
if (!plans.every((plan) => (OFFER_KINDS as readonly string[]).includes(plan))) {
|
|
1270
|
-
return badRequest(`plans lists the kinds to offer: ${OFFER_KINDS.join(", ")}.`);
|
|
1274
|
+
return badRequest(`plans lists the kinds to offer: ${SUPPORTED}, or any of ${OFFER_KINDS.join(", ")}.`);
|
|
1271
1275
|
}
|
|
1272
1276
|
const keyProviders = listParam(params.get("key_providers"));
|
|
1273
1277
|
if (!keyProviders.every((provider) => /^[a-z0-9-]{1,64}$/.test(provider))) {
|
|
@@ -1320,9 +1324,32 @@ async function homePage(approval: LinkApprovalPage, authorize: AuthorizeRequest,
|
|
|
1320
1324
|
if (current instanceof Response) return current;
|
|
1321
1325
|
if (session === null) return introPage(approval, authorize);
|
|
1322
1326
|
const subscriptions = await approval.subscriptions(session.subject);
|
|
1327
|
+
const onComputers = await plansOnComputersNote(approval, authorize, session.subject, subscriptions);
|
|
1323
1328
|
return shareable(authorize, subscriptions).length > 0
|
|
1324
|
-
? choosePage(authorize, session, subscriptions, current)
|
|
1325
|
-
: introPage(approval, authorize);
|
|
1329
|
+
? choosePage(authorize, session, subscriptions, current, undefined, onComputers)
|
|
1330
|
+
: introPage(approval, authorize, onComputers);
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
// Where the user keeps a plan the app could use on one of their computers,
|
|
1334
|
+
// for each kind they have no plan of here: apps reach only the platform's.
|
|
1335
|
+
async function plansOnComputersNote(
|
|
1336
|
+
approval: LinkApprovalPage,
|
|
1337
|
+
authorize: AuthorizeRequest,
|
|
1338
|
+
subject: string,
|
|
1339
|
+
subscriptions: LinkSubscription[],
|
|
1340
|
+
): Promise<string> {
|
|
1341
|
+
if (approval.plansOnComputers === undefined) return "";
|
|
1342
|
+
const here = new Set(subscriptions.map((subscription) => subscription.provider));
|
|
1343
|
+
const usable = new Set(connectKinds(approval, authorize));
|
|
1344
|
+
const computers = new Map<SubscriptionKind, string[]>();
|
|
1345
|
+
for (const plan of await approval.plansOnComputers(subject)) {
|
|
1346
|
+
if (here.has(plan.provider) || !usable.has(plan.provider)) continue;
|
|
1347
|
+
const names = computers.get(plan.provider) ?? [];
|
|
1348
|
+
if (!names.includes(plan.computer)) names.push(plan.computer);
|
|
1349
|
+
computers.set(plan.provider, names);
|
|
1350
|
+
}
|
|
1351
|
+
return [...computers].map(([kind, names]) =>
|
|
1352
|
+
`<p class="hint">Your ${KINDS[kind].name} plan is on ${escapeHtml(names.join(" and "))}. Apps can only use plans on Dari.</p>`).join("\n");
|
|
1326
1353
|
}
|
|
1327
1354
|
|
|
1328
1355
|
// In update mode, the grant being changed. A grant this browser's user does
|
|
@@ -1855,13 +1882,12 @@ const KINDS: Record<SubscriptionKind, { name: string; plan: string; at: string;
|
|
|
1855
1882
|
openai_codex: { name: "ChatGPT", plan: "Plus or Pro plan", at: "OpenAI" },
|
|
1856
1883
|
meta_muse: { name: "Meta Muse", plan: "Muse Code plan", at: "Meta" },
|
|
1857
1884
|
opencode_go: { name: "OpenCode Go", plan: "Go plan", at: "OpenCode" },
|
|
1858
|
-
github_copilot: { name: "GitHub Copilot", plan: "Copilot plan", at: "GitHub" },
|
|
1859
1885
|
xai_grok: { name: "Grok", plan: "SuperGrok plan", at: "xAI" },
|
|
1860
1886
|
vercel_token_pass: { name: "Vercel Token Pass", plan: "Token Pass", at: "Vercel", pastedKey: true },
|
|
1861
1887
|
};
|
|
1862
1888
|
|
|
1863
1889
|
// The first page a new user sees: what connecting means, and one button.
|
|
1864
|
-
function introPage(approval: LinkApprovalPage, authorize: AuthorizeRequest): Response {
|
|
1890
|
+
function introPage(approval: LinkApprovalPage, authorize: AuthorizeRequest, onComputers = ""): Response {
|
|
1865
1891
|
const app = escapeHtml(authorize.app.name);
|
|
1866
1892
|
const kinds = connectKinds(approval, authorize);
|
|
1867
1893
|
const keys = keyProviders(approval, authorize);
|
|
@@ -1883,7 +1909,7 @@ function introPage(approval: LinkApprovalPage, authorize: AuthorizeRequest): Res
|
|
|
1883
1909
|
<ul class="points">
|
|
1884
1910
|
<li>${ICONS.zap}<div><strong>Connect In Seconds</strong><p>${kinds.length > 0 ? `Use what you already pay for: ${escapeHtml(ways)}.` : "Add the API key you already have."}</p></div></li>
|
|
1885
1911
|
<li>${ICONS.shield}<div><strong>You Stay In Control</strong><p>${app} can only send model requests on what you share. It sees each shared plan's account email, never a key or your other plans, and you can disconnect it any time.</p></div></li>
|
|
1886
|
-
</ul
|
|
1912
|
+
</ul>${onComputers}`,
|
|
1887
1913
|
footer: `<button class="btn primary" type="submit" name="decision" value="select">Continue</button>`,
|
|
1888
1914
|
});
|
|
1889
1915
|
}
|
|
@@ -1980,6 +2006,7 @@ function choosePage(
|
|
|
1980
2006
|
subscriptions: LinkSubscription[],
|
|
1981
2007
|
current: GrantToUpdate | null,
|
|
1982
2008
|
error?: string,
|
|
2009
|
+
onComputers = "",
|
|
1983
2010
|
): Response {
|
|
1984
2011
|
const app = escapeHtml(authorize.app.name);
|
|
1985
2012
|
const plans = shareable(authorize, subscriptions);
|
|
@@ -1994,6 +2021,7 @@ function choosePage(
|
|
|
1994
2021
|
<!-- First in the form, so Enter in a limit field allows rather than pressing Connect Another. -->
|
|
1995
2022
|
<button type="submit" name="decision" value="approve" tabindex="-1" aria-hidden="true" style="position:absolute;left:-9999px"></button>
|
|
1996
2023
|
${plans.map((plan) => planOption(authorize, plan, ticked.has(plan.id))).join("\n")}
|
|
2024
|
+
${onComputers}
|
|
1997
2025
|
<details${current?.limits.length ? " open" : ""}><summary>Set Limits</summary>
|
|
1998
2026
|
${plans.map((plan) => {
|
|
1999
2027
|
const set = current?.limits.find((limit) => limit.credentialId === plan.id);
|
package/src/link/errors.ts
CHANGED
|
@@ -52,7 +52,6 @@ export const SUBSCRIPTION_PLAN_LABELS: Record<SubscriptionKind, string> = {
|
|
|
52
52
|
openai_codex: "ChatGPT Plus/Pro",
|
|
53
53
|
meta_muse: "Meta Muse",
|
|
54
54
|
opencode_go: "OpenCode Go",
|
|
55
|
-
github_copilot: "GitHub Copilot",
|
|
56
55
|
xai_grok: "Grok",
|
|
57
56
|
vercel_token_pass: "Vercel Token Pass",
|
|
58
57
|
};
|
package/src/link/self_hosted.ts
CHANGED
|
@@ -10,11 +10,11 @@ import { checkProviderKey } from "../providers/key_check.js";
|
|
|
10
10
|
import { gatewayKeyIndex, type SelfHostedResolverDeps } from "../self_host/resolve.js";
|
|
11
11
|
import {
|
|
12
12
|
CODEX_APPROVAL_PENDING,
|
|
13
|
-
GITHUB_APPROVAL_PENDING,
|
|
14
13
|
META_APPROVAL_PENDING,
|
|
15
14
|
OPENCODE_APPROVAL_PENDING,
|
|
16
15
|
XAI_APPROVAL_PENDING,
|
|
17
16
|
SUBSCRIPTION_PROVIDERS,
|
|
17
|
+
SubscriptionAccountConnectedError,
|
|
18
18
|
SubscriptionOAuthInputError,
|
|
19
19
|
SubscriptionOAuthProviderError,
|
|
20
20
|
completeOAuth,
|
|
@@ -120,6 +120,7 @@ export function selfHostedLink(
|
|
|
120
120
|
flow: authorization.flow,
|
|
121
121
|
credentialName: null,
|
|
122
122
|
owner: newSubject(),
|
|
123
|
+
private: false,
|
|
123
124
|
expiresAt: authorization.expiresAt,
|
|
124
125
|
});
|
|
125
126
|
return { session: session.token, authorizationUrl: authorization.authorizationUrl, userCode: authorization.userCode };
|
|
@@ -138,7 +139,6 @@ export function selfHostedLink(
|
|
|
138
139
|
CODEX_APPROVAL_PENDING,
|
|
139
140
|
META_APPROVAL_PENDING,
|
|
140
141
|
OPENCODE_APPROVAL_PENDING,
|
|
141
|
-
GITHUB_APPROVAL_PENDING,
|
|
142
142
|
XAI_APPROVAL_PENDING,
|
|
143
143
|
].includes(error.message)) return null;
|
|
144
144
|
throw new HttpError(400, error.message, "invalid_request");
|
|
@@ -149,7 +149,13 @@ export function selfHostedLink(
|
|
|
149
149
|
throw error;
|
|
150
150
|
}
|
|
151
151
|
const owner = subject ?? session.owner;
|
|
152
|
-
|
|
152
|
+
let credentialId: string;
|
|
153
|
+
try {
|
|
154
|
+
credentialId = deps.subscriptions.connect(tokens, null, new Date(), owner)!.id;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (error instanceof SubscriptionAccountConnectedError) throw new HttpError(409, error.message, "conflict_error");
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
153
159
|
deps.subscriptions.deleteSession(token);
|
|
154
160
|
return { subject: owner, credentialId };
|
|
155
161
|
},
|