@mupt-ai/dari-gateway 1.0.298 → 1.0.300

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.
@@ -13,6 +13,12 @@ import type { ClientHarness, ProviderCredential, ProviderKeySource, ResolvedRout
13
13
  * log, and continue.
14
14
  */
15
15
  export type GatewayPlatform = {
16
+ /**
17
+ * Optional. The web page where users manage their subscriptions; usage-limit
18
+ * errors link to it, with `?buy=<provider>` to add another plan. Without
19
+ * one they point at `dari accounts`.
20
+ */
21
+ accountsUrl?: string;
16
22
  /**
17
23
  * Authenticate the request's key and return the router it may use, with
18
24
  * decrypted provider credentials and the prefix-cache evidence matching
@@ -244,8 +244,9 @@ async function throwProviderFailure(args) {
244
244
  throw new HttpError(503, subscriptionSwitchedMessage(limit), "subscription_switched", undefined, { "retry-after": "1" });
245
245
  }
246
246
  if (limit !== null) {
247
- const codex = codexUsageLimitError(limit);
248
- throw new HttpError(429, subscriptionUsageLimitMessage(limit), "subscription_usage_limit", undefined, { "retry-after": String(retryAfterSeconds(limit)), ...codex?.headers }, codex?.body);
247
+ const accountsUrl = gatewayPlatform().accountsUrl;
248
+ const codex = codexUsageLimitError(limit, accountsUrl);
249
+ throw new HttpError(429, subscriptionUsageLimitMessage(limit, accountsUrl), "subscription_usage_limit", undefined, { "retry-after": String(retryAfterSeconds(limit)), ...codex?.headers }, codex?.body);
249
250
  }
250
251
  throw new HttpError(502, providerFailureMessage(result.failure), "provider_execution_failed");
251
252
  }
@@ -42,14 +42,20 @@ export declare function detectSubscriptionUsageLimit(args: {
42
42
  /** Tag a provider failure so telemetry and the terminal error know why. */
43
43
  export declare function failureWithSubscriptionUsageLimit(failure: Record<string, unknown>, limit: SubscriptionUsageLimit): Record<string, unknown>;
44
44
  export declare function subscriptionUsageLimitFromFailure(failure: Record<string, unknown>): SubscriptionUsageLimit | null;
45
- export declare function subscriptionUsageLimitMessage(limit: SubscriptionUsageLimit): string;
45
+ /**
46
+ * The error for a plan out of quota with nothing to switch to. With the
47
+ * platform's accounts page it links there; without one (a self-hosted
48
+ * gateway) it points at `dari accounts`, which manages either.
49
+ */
50
+ export declare function subscriptionUsageLimitMessage(limit: SubscriptionUsageLimit, accountsUrl?: string): string;
46
51
  /**
47
52
  * Codex shows its own usage-limit notice and drops the message of any other
48
53
  * 429, so an exhausted ChatGPT plan answers in the ChatGPT backend's shape:
49
- * the body type Codex recognizes, and the buy link as its promo message.
50
- * Codex renders "You've hit your usage limit. <promo>, or try again at <reset>."
54
+ * the body type Codex recognizes, and how to add another plan as its promo
55
+ * message. Codex renders "You've hit your usage limit. <promo>, or try again
56
+ * at <reset>."
51
57
  */
52
- export declare function codexUsageLimitError(limit: SubscriptionUsageLimit): {
58
+ export declare function codexUsageLimitError(limit: SubscriptionUsageLimit, accountsUrl?: string): {
53
59
  body: Record<string, unknown>;
54
60
  headers: Record<string, string>;
55
61
  } | null;
@@ -46,7 +46,7 @@ const SUBSCRIPTION_LABEL = {
46
46
  openai_codex: "ChatGPT",
47
47
  meta_muse: "Muse Code",
48
48
  };
49
- // Providers whose plan page Dari's credentials page links to for buying another.
49
+ // Providers whose plan page the accounts page links to for buying another.
50
50
  const BUYABLE_SUBSCRIPTIONS = new Set(["anthropic_claude_code", "openai_codex"]);
51
51
  export function subscriptionOAuthProviderFor(provider, keySource) {
52
52
  if (keySource !== "subscription")
@@ -202,37 +202,39 @@ export function subscriptionUsageLimitFromFailure(failure) {
202
202
  ...(marker.switched === true ? { switched: true } : {}),
203
203
  };
204
204
  }
205
- export function subscriptionUsageLimitMessage(limit) {
206
- return `Your ${SUBSCRIPTION_LABEL[limit.oauthProvider]} subscription usage limit is reached. `
207
- + `It resets at ${limit.resetsAt.toISOString()}. `
208
- + "Mark it available early at https://app.dari.dev/credentials."
209
- + subscriptionBuyHint(limit.oauthProvider);
210
- }
211
- // To keep going now: Dari's page links to subscribing another account and
212
- // connects it.
213
- function subscriptionBuyHint(provider) {
214
- const action = subscriptionBuyAction(provider);
215
- return action === null ? "" : ` To keep going now, ${action.charAt(0).toLowerCase()}${action.slice(1)}.`;
216
- }
217
- function subscriptionBuyAction(provider) {
218
- if (!BUYABLE_SUBSCRIPTIONS.has(provider))
219
- return null;
220
- return `Add another ${SUBSCRIPTION_LABEL[provider]} subscription at `
221
- + `https://app.dari.dev/credentials?buy=${provider}`;
205
+ /**
206
+ * The error for a plan out of quota with nothing to switch to. With the
207
+ * platform's accounts page it links there; without one (a self-hosted
208
+ * gateway) it points at `dari accounts`, which manages either.
209
+ */
210
+ export function subscriptionUsageLimitMessage(limit, accountsUrl) {
211
+ const label = SUBSCRIPTION_LABEL[limit.oauthProvider];
212
+ const reached = `Your ${label} subscription usage limit is reached. It resets at ${limit.resetsAt.toISOString()}. `;
213
+ if (accountsUrl === undefined) {
214
+ return reached
215
+ + `To keep going now, add another ${label} subscription or mark this one available early with \`dari accounts\`.`;
216
+ }
217
+ const buy = BUYABLE_SUBSCRIPTIONS.has(limit.oauthProvider)
218
+ ? ` To keep going now, add another ${label} subscription at ${accountsUrl}?buy=${limit.oauthProvider}.`
219
+ : "";
220
+ return `${reached}Mark it available early at ${accountsUrl}.${buy}`;
222
221
  }
223
222
  /**
224
223
  * Codex shows its own usage-limit notice and drops the message of any other
225
224
  * 429, so an exhausted ChatGPT plan answers in the ChatGPT backend's shape:
226
- * the body type Codex recognizes, and the buy link as its promo message.
227
- * Codex renders "You've hit your usage limit. <promo>, or try again at <reset>."
225
+ * the body type Codex recognizes, and how to add another plan as its promo
226
+ * message. Codex renders "You've hit your usage limit. <promo>, or try again
227
+ * at <reset>."
228
228
  */
229
- export function codexUsageLimitError(limit) {
229
+ export function codexUsageLimitError(limit, accountsUrl) {
230
230
  if (limit.oauthProvider !== "openai_codex")
231
231
  return null;
232
- const promo = subscriptionBuyAction(limit.oauthProvider);
232
+ const promo = accountsUrl === undefined
233
+ ? "Add another ChatGPT subscription with `dari accounts`"
234
+ : `Add another ChatGPT subscription at ${accountsUrl}?buy=openai_codex`;
233
235
  return {
234
236
  body: { type: "usage_limit_reached", resets_at: Math.ceil(limit.resetsAt.getTime() / 1000) },
235
- headers: promo === null ? {} : { "x-codex-promo-message": promo },
237
+ headers: { "x-codex-promo-message": promo },
236
238
  };
237
239
  }
238
240
  export function subscriptionSwitchedMessage(limit) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mupt-ai/dari-gateway",
3
- "version": "1.0.298",
3
+ "version": "1.0.300",
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": {
@@ -51,7 +51,7 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@aws-sdk/client-sts": "3.1063.0",
54
- "@mupt-ai/dari-router": "1.0.298",
54
+ "@mupt-ai/dari-router": "1.0.300",
55
55
  "@mupt-ai/pi-ai": "1.4.0-mupt.3"
56
56
  },
57
57
  "devDependencies": {
@@ -23,6 +23,12 @@ import type {
23
23
  * log, and continue.
24
24
  */
25
25
  export type GatewayPlatform = {
26
+ /**
27
+ * Optional. The web page where users manage their subscriptions; usage-limit
28
+ * errors link to it, with `?buy=<provider>` to add another plan. Without
29
+ * one they point at `dari accounts`.
30
+ */
31
+ accountsUrl?: string;
26
32
  /**
27
33
  * Authenticate the request's key and return the router it may use, with
28
34
  * decrypted provider credentials and the prefix-cache evidence matching
@@ -439,10 +439,11 @@ async function throwProviderFailure(args: {
439
439
  );
440
440
  }
441
441
  if (limit !== null) {
442
- const codex = codexUsageLimitError(limit);
442
+ const accountsUrl = gatewayPlatform().accountsUrl;
443
+ const codex = codexUsageLimitError(limit, accountsUrl);
443
444
  throw new HttpError(
444
445
  429,
445
- subscriptionUsageLimitMessage(limit),
446
+ subscriptionUsageLimitMessage(limit, accountsUrl),
446
447
  "subscription_usage_limit",
447
448
  undefined,
448
449
  { "retry-after": String(retryAfterSeconds(limit)), ...codex?.headers },
@@ -80,7 +80,7 @@ const SUBSCRIPTION_LABEL: Record<SubscriptionOAuthProvider, string> = {
80
80
  meta_muse: "Muse Code",
81
81
  };
82
82
 
83
- // Providers whose plan page Dari's credentials page links to for buying another.
83
+ // Providers whose plan page the accounts page links to for buying another.
84
84
  const BUYABLE_SUBSCRIPTIONS = new Set<SubscriptionOAuthProvider>(["anthropic_claude_code", "openai_codex"]);
85
85
 
86
86
  export function subscriptionOAuthProviderFor(
@@ -263,41 +263,42 @@ export function subscriptionUsageLimitFromFailure(
263
263
  };
264
264
  }
265
265
 
266
- export function subscriptionUsageLimitMessage(limit: SubscriptionUsageLimit): string {
267
- return `Your ${SUBSCRIPTION_LABEL[limit.oauthProvider]} subscription usage limit is reached. `
268
- + `It resets at ${limit.resetsAt.toISOString()}. `
269
- + "Mark it available early at https://app.dari.dev/credentials."
270
- + subscriptionBuyHint(limit.oauthProvider);
271
- }
272
-
273
- // To keep going now: Dari's page links to subscribing another account and
274
- // connects it.
275
- function subscriptionBuyHint(provider: SubscriptionOAuthProvider): string {
276
- const action = subscriptionBuyAction(provider);
277
- return action === null ? "" : ` To keep going now, ${action.charAt(0).toLowerCase()}${action.slice(1)}.`;
278
- }
279
-
280
- function subscriptionBuyAction(provider: SubscriptionOAuthProvider): string | null {
281
- if (!BUYABLE_SUBSCRIPTIONS.has(provider)) return null;
282
- return `Add another ${SUBSCRIPTION_LABEL[provider]} subscription at `
283
- + `https://app.dari.dev/credentials?buy=${provider}`;
266
+ /**
267
+ * The error for a plan out of quota with nothing to switch to. With the
268
+ * platform's accounts page it links there; without one (a self-hosted
269
+ * gateway) it points at `dari accounts`, which manages either.
270
+ */
271
+ export function subscriptionUsageLimitMessage(limit: SubscriptionUsageLimit, accountsUrl?: string): string {
272
+ const label = SUBSCRIPTION_LABEL[limit.oauthProvider];
273
+ const reached = `Your ${label} subscription usage limit is reached. It resets at ${limit.resetsAt.toISOString()}. `;
274
+ if (accountsUrl === undefined) {
275
+ return reached
276
+ + `To keep going now, add another ${label} subscription or mark this one available early with \`dari accounts\`.`;
277
+ }
278
+ const buy = BUYABLE_SUBSCRIPTIONS.has(limit.oauthProvider)
279
+ ? ` To keep going now, add another ${label} subscription at ${accountsUrl}?buy=${limit.oauthProvider}.`
280
+ : "";
281
+ return `${reached}Mark it available early at ${accountsUrl}.${buy}`;
284
282
  }
285
283
 
286
284
  /**
287
285
  * Codex shows its own usage-limit notice and drops the message of any other
288
286
  * 429, so an exhausted ChatGPT plan answers in the ChatGPT backend's shape:
289
- * the body type Codex recognizes, and the buy link as its promo message.
290
- * Codex renders "You've hit your usage limit. <promo>, or try again at <reset>."
287
+ * the body type Codex recognizes, and how to add another plan as its promo
288
+ * message. Codex renders "You've hit your usage limit. <promo>, or try again
289
+ * at <reset>."
291
290
  */
292
- export function codexUsageLimitError(limit: SubscriptionUsageLimit): {
291
+ export function codexUsageLimitError(limit: SubscriptionUsageLimit, accountsUrl?: string): {
293
292
  body: Record<string, unknown>;
294
293
  headers: Record<string, string>;
295
294
  } | null {
296
295
  if (limit.oauthProvider !== "openai_codex") return null;
297
- const promo = subscriptionBuyAction(limit.oauthProvider);
296
+ const promo = accountsUrl === undefined
297
+ ? "Add another ChatGPT subscription with `dari accounts`"
298
+ : `Add another ChatGPT subscription at ${accountsUrl}?buy=openai_codex`;
298
299
  return {
299
300
  body: { type: "usage_limit_reached", resets_at: Math.ceil(limit.resetsAt.getTime() / 1000) },
300
- headers: promo === null ? {} : { "x-codex-promo-message": promo },
301
+ headers: { "x-codex-promo-message": promo },
301
302
  };
302
303
  }
303
304