@mupt-ai/dari-gateway 1.0.339 → 1.0.341

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.
Files changed (69) hide show
  1. package/README.md +9 -7
  2. package/dist/index.d.ts +1 -1
  3. package/dist/link/assets.js +7 -0
  4. package/dist/link/budget.d.ts +18 -7
  5. package/dist/link/budget.js +58 -16
  6. package/dist/link/endpoints.js +64 -45
  7. package/dist/link/errors.js +4 -1
  8. package/dist/link/self_hosted.d.ts +3 -1
  9. package/dist/link/self_hosted.js +25 -9
  10. package/dist/link/service.d.ts +4 -4
  11. package/dist/link/service.js +41 -22
  12. package/dist/models/catalog.js +1 -0
  13. package/dist/models/execution_provider.d.ts +6 -1
  14. package/dist/models/execution_provider.js +12 -0
  15. package/dist/models/provider_defaults.js +9 -0
  16. package/dist/models/provider_models.json +217 -0
  17. package/dist/models/subscriptions.d.ts +5 -0
  18. package/dist/models/subscriptions.js +21 -2
  19. package/dist/platform/platform.d.ts +11 -7
  20. package/dist/providers/execution.d.ts +5 -0
  21. package/dist/providers/execution.js +12 -2
  22. package/dist/providers/github_copilot.d.ts +8 -0
  23. package/dist/providers/github_copilot.js +21 -0
  24. package/dist/providers/pi.js +3 -0
  25. package/dist/providers/provider_attempt_runner.d.ts +2 -0
  26. package/dist/providers/provider_attempt_runner.js +9 -0
  27. package/dist/providers/provider_call.js +6 -2
  28. package/dist/providers/subscription_limits.d.ts +6 -5
  29. package/dist/providers/subscription_limits.js +20 -9
  30. package/dist/runtime/routed_runtime.js +68 -3
  31. package/dist/self_host/resolve.d.ts +11 -9
  32. package/dist/self_host/resolve.js +249 -182
  33. package/dist/self_host/subscriptions/index.d.ts +6 -5
  34. package/dist/self_host/subscriptions/index.js +6 -5
  35. package/dist/self_host/subscriptions/oauth.d.ts +7 -3
  36. package/dist/self_host/subscriptions/oauth.js +195 -5
  37. package/dist/self_host/subscriptions/store.js +3 -0
  38. package/dist/telemetry/prefix_state.d.ts +1 -0
  39. package/dist/telemetry/prefix_state.js +1 -1
  40. package/dist/telemetry/report.js +2 -3
  41. package/dist/types.d.ts +6 -2
  42. package/package.json +2 -2
  43. package/src/index.ts +1 -1
  44. package/src/link/assets.ts +7 -0
  45. package/src/link/budget.ts +53 -18
  46. package/src/link/endpoints.ts +66 -45
  47. package/src/link/errors.ts +4 -1
  48. package/src/link/self_hosted.ts +26 -8
  49. package/src/link/service.ts +45 -23
  50. package/src/models/catalog.ts +1 -0
  51. package/src/models/execution_provider.ts +24 -1
  52. package/src/models/provider_defaults.ts +9 -0
  53. package/src/models/provider_models.json +217 -0
  54. package/src/models/subscriptions.ts +22 -2
  55. package/src/platform/platform.ts +13 -8
  56. package/src/providers/execution.ts +22 -2
  57. package/src/providers/github_copilot.ts +24 -0
  58. package/src/providers/pi.ts +2 -0
  59. package/src/providers/provider_attempt_runner.ts +15 -0
  60. package/src/providers/provider_call.ts +6 -2
  61. package/src/providers/subscription_limits.ts +20 -10
  62. package/src/runtime/routed_runtime.ts +79 -3
  63. package/src/self_host/resolve.ts +255 -187
  64. package/src/self_host/subscriptions/index.ts +7 -4
  65. package/src/self_host/subscriptions/oauth.ts +264 -8
  66. package/src/self_host/subscriptions/store.ts +3 -0
  67. package/src/telemetry/prefix_state.ts +4 -1
  68. package/src/telemetry/report.ts +2 -3
  69. package/src/types.ts +27 -3
@@ -1,4 +1,137 @@
1
1
  [
2
+ {
3
+ "model_id": "openai/gpt-6.2-astra",
4
+ "provider": "github-copilot",
5
+ "native_model_id": "gpt-6.2-astra",
6
+ "api": "openai-responses",
7
+ "base_url": "https://api.individual.githubcopilot.com"
8
+ },
9
+ {
10
+ "model_id": "openai/gpt-6-astra",
11
+ "provider": "github-copilot",
12
+ "native_model_id": "gpt-6-astra",
13
+ "api": "openai-responses",
14
+ "base_url": "https://api.individual.githubcopilot.com"
15
+ },
16
+ {
17
+ "model_id": "openai/gpt-6.1-terra",
18
+ "provider": "github-copilot",
19
+ "native_model_id": "gpt-6.1-terra",
20
+ "api": "openai-responses",
21
+ "base_url": "https://api.individual.githubcopilot.com"
22
+ },
23
+ {
24
+ "model_id": "openai/gpt-6.2-sol",
25
+ "provider": "github-copilot",
26
+ "native_model_id": "gpt-6.2-sol",
27
+ "api": "openai-responses",
28
+ "base_url": "https://api.individual.githubcopilot.com"
29
+ },
30
+ {
31
+ "model_id": "openai/gpt-6.1-sol",
32
+ "provider": "github-copilot",
33
+ "native_model_id": "gpt-6.1-sol",
34
+ "api": "openai-responses",
35
+ "base_url": "https://api.individual.githubcopilot.com"
36
+ },
37
+ {
38
+ "model_id": "openai/gpt-6-sol",
39
+ "provider": "github-copilot",
40
+ "native_model_id": "gpt-6-sol",
41
+ "api": "openai-responses",
42
+ "base_url": "https://api.individual.githubcopilot.com"
43
+ },
44
+ {
45
+ "model_id": "openai/gpt-6-luna",
46
+ "provider": "github-copilot",
47
+ "native_model_id": "gpt-6-luna",
48
+ "api": "openai-responses",
49
+ "base_url": "https://api.individual.githubcopilot.com"
50
+ },
51
+ {
52
+ "model_id": "openai/gpt-5.6-sol",
53
+ "provider": "github-copilot",
54
+ "native_model_id": "gpt-5.6-sol",
55
+ "api": "openai-responses",
56
+ "base_url": "https://api.individual.githubcopilot.com"
57
+ },
58
+ {
59
+ "model_id": "openai/gpt-5.6-terra",
60
+ "provider": "github-copilot",
61
+ "native_model_id": "gpt-5.6-terra",
62
+ "api": "openai-responses",
63
+ "base_url": "https://api.individual.githubcopilot.com"
64
+ },
65
+ {
66
+ "model_id": "openai/gpt-5.6-luna",
67
+ "provider": "github-copilot",
68
+ "native_model_id": "gpt-5.6-luna",
69
+ "api": "openai-responses",
70
+ "base_url": "https://api.individual.githubcopilot.com"
71
+ },
72
+ {
73
+ "model_id": "anthropic/claude-opus-5-5",
74
+ "provider": "github-copilot",
75
+ "native_model_id": "claude-opus-5.5",
76
+ "api": "anthropic-messages",
77
+ "base_url": "https://api.individual.githubcopilot.com"
78
+ },
79
+ {
80
+ "model_id": "anthropic/claude-opus-5",
81
+ "provider": "github-copilot",
82
+ "native_model_id": "claude-opus-5",
83
+ "api": "anthropic-messages",
84
+ "base_url": "https://api.individual.githubcopilot.com"
85
+ },
86
+ {
87
+ "model_id": "anthropic/claude-sonnet-5-5",
88
+ "provider": "github-copilot",
89
+ "native_model_id": "claude-sonnet-5.5",
90
+ "api": "anthropic-messages",
91
+ "base_url": "https://api.individual.githubcopilot.com"
92
+ },
93
+ {
94
+ "model_id": "anthropic/claude-sonnet-5",
95
+ "provider": "github-copilot",
96
+ "native_model_id": "claude-sonnet-5",
97
+ "api": "anthropic-messages",
98
+ "base_url": "https://api.individual.githubcopilot.com"
99
+ },
100
+ {
101
+ "model_id": "anthropic/claude-fable-5-2",
102
+ "provider": "github-copilot",
103
+ "native_model_id": "claude-fable-5.2",
104
+ "api": "anthropic-messages",
105
+ "base_url": "https://api.individual.githubcopilot.com"
106
+ },
107
+ {
108
+ "model_id": "anthropic/claude-fable-5-1",
109
+ "provider": "github-copilot",
110
+ "native_model_id": "claude-fable-5.1",
111
+ "api": "anthropic-messages",
112
+ "base_url": "https://api.individual.githubcopilot.com"
113
+ },
114
+ {
115
+ "model_id": "anthropic/claude-fable-5",
116
+ "provider": "github-copilot",
117
+ "native_model_id": "claude-fable-5",
118
+ "api": "anthropic-messages",
119
+ "base_url": "https://api.individual.githubcopilot.com"
120
+ },
121
+ {
122
+ "model_id": "anthropic/claude-haiku-5-5",
123
+ "provider": "github-copilot",
124
+ "native_model_id": "claude-haiku-5.5",
125
+ "api": "anthropic-messages",
126
+ "base_url": "https://api.individual.githubcopilot.com"
127
+ },
128
+ {
129
+ "model_id": "anthropic/claude-haiku-4-5",
130
+ "provider": "github-copilot",
131
+ "native_model_id": "claude-haiku-4.5",
132
+ "api": "anthropic-messages",
133
+ "base_url": "https://api.individual.githubcopilot.com"
134
+ },
2
135
  {
3
136
  "model_id": "meta/muse-spark-open",
4
137
  "provider": "fireworks",
@@ -55,6 +188,13 @@
55
188
  "api": "openai-completions",
56
189
  "base_url": "https://api.fireworks.ai/inference/v1"
57
190
  },
191
+ {
192
+ "model_id": "deepseek-ai/DeepSeek-V4.1-Flash",
193
+ "provider": "vercel-token-pass",
194
+ "native_model_id": "deepseek/deepseek-v4.1-flash",
195
+ "api": "anthropic-messages",
196
+ "base_url": "https://ai-gateway.vercel.sh"
197
+ },
58
198
  {
59
199
  "model_id": "deepseek-ai/DeepSeek-V4.1-Flash-1017",
60
200
  "provider": "fireworks",
@@ -62,6 +202,20 @@
62
202
  "api": "openai-completions",
63
203
  "base_url": "https://api.fireworks.ai/inference/v1"
64
204
  },
205
+ {
206
+ "model_id": "xai/grok-4.6",
207
+ "provider": "github-copilot",
208
+ "native_model_id": "grok-4.6",
209
+ "api": "openai-responses",
210
+ "base_url": "https://api.individual.githubcopilot.com"
211
+ },
212
+ {
213
+ "model_id": "xai/grok-4.7",
214
+ "provider": "github-copilot",
215
+ "native_model_id": "grok-4.7",
216
+ "api": "openai-responses",
217
+ "base_url": "https://api.individual.githubcopilot.com"
218
+ },
65
219
  {
66
220
  "model_id": "moonshotai/Kimi-K3",
67
221
  "provider": "opencode-go",
@@ -69,6 +223,20 @@
69
223
  "api": "openai-completions",
70
224
  "base_url": "https://opencode.ai/inference/go/openai/v1"
71
225
  },
226
+ {
227
+ "model_id": "moonshotai/Kimi-K3",
228
+ "provider": "github-copilot",
229
+ "native_model_id": "kimi-k3",
230
+ "api": "openai-completions",
231
+ "base_url": "https://api.individual.githubcopilot.com"
232
+ },
233
+ {
234
+ "model_id": "moonshotai/Kimi-K3",
235
+ "provider": "vercel-token-pass",
236
+ "native_model_id": "moonshotai/kimi-k3",
237
+ "api": "anthropic-messages",
238
+ "base_url": "https://ai-gateway.vercel.sh"
239
+ },
72
240
  {
73
241
  "model_id": "moonshotai/Kimi-K3.1-Pro",
74
242
  "provider": "fireworks",
@@ -83,6 +251,13 @@
83
251
  "api": "openai-completions",
84
252
  "base_url": "https://opencode.ai/inference/go/openai/v1"
85
253
  },
254
+ {
255
+ "model_id": "moonshotai/Kimi-K3.1-Pro",
256
+ "provider": "vercel-token-pass",
257
+ "native_model_id": "moonshotai/kimi-k3.1-pro",
258
+ "api": "anthropic-messages",
259
+ "base_url": "https://ai-gateway.vercel.sh"
260
+ },
86
261
  {
87
262
  "model_id": "zai-org/GLM-5.3",
88
263
  "provider": "opencode-go",
@@ -90,6 +265,13 @@
90
265
  "api": "openai-completions",
91
266
  "base_url": "https://opencode.ai/inference/go/openai/v1"
92
267
  },
268
+ {
269
+ "model_id": "zai-org/GLM-5.3",
270
+ "provider": "vercel-token-pass",
271
+ "native_model_id": "zai/glm-5.3",
272
+ "api": "anthropic-messages",
273
+ "base_url": "https://ai-gateway.vercel.sh"
274
+ },
93
275
  {
94
276
  "model_id": "zai-org/GLM-5.3-Flash",
95
277
  "provider": "opencode-go",
@@ -97,6 +279,13 @@
97
279
  "api": "openai-completions",
98
280
  "base_url": "https://opencode.ai/inference/go/openai/v1"
99
281
  },
282
+ {
283
+ "model_id": "zai-org/GLM-5.3-Flash",
284
+ "provider": "vercel-token-pass",
285
+ "native_model_id": "zai/glm-5.3-flash",
286
+ "api": "anthropic-messages",
287
+ "base_url": "https://ai-gateway.vercel.sh"
288
+ },
100
289
  {
101
290
  "model_id": "zai-org/GLM-5.2",
102
291
  "provider": "opencode-go",
@@ -110,5 +299,33 @@
110
299
  "native_model_id": "deepseek-v4.1-flash",
111
300
  "api": "openai-completions",
112
301
  "base_url": "https://opencode.ai/inference/go/openai/v1"
302
+ },
303
+ {
304
+ "model_id": "google/gemini-4-argon",
305
+ "provider": "github-copilot",
306
+ "native_model_id": "gemini-4-argon",
307
+ "api": "openai-completions",
308
+ "base_url": "https://api.individual.githubcopilot.com"
309
+ },
310
+ {
311
+ "model_id": "google/gemini-4-carbon",
312
+ "provider": "github-copilot",
313
+ "native_model_id": "gemini-4-carbon",
314
+ "api": "openai-completions",
315
+ "base_url": "https://api.individual.githubcopilot.com"
316
+ },
317
+ {
318
+ "model_id": "google/gemini-4-hydrogen",
319
+ "provider": "github-copilot",
320
+ "native_model_id": "gemini-4-hydrogen",
321
+ "api": "openai-completions",
322
+ "base_url": "https://api.individual.githubcopilot.com"
323
+ },
324
+ {
325
+ "model_id": "google/gemini-3.8-flash",
326
+ "provider": "github-copilot",
327
+ "native_model_id": "gemini-3.8-flash",
328
+ "api": "openai-completions",
329
+ "base_url": "https://api.individual.githubcopilot.com"
113
330
  }
114
331
  ]
@@ -1,5 +1,6 @@
1
- import { nativeModelId, type RouterModelPrice } from "@mupt-ai/dari-router/policy-engine";
1
+ import { nativeModelId, providerForModel, type RouterModelPrice } from "@mupt-ai/dari-router/policy-engine";
2
2
 
3
+ import { providerModel } from "./canonical.js";
3
4
  import type { ClientHarness, ResolvedRouterModelConfig, SubscriptionKind } from "../types.js";
4
5
 
5
6
  const CODEX_BACKEND_URL = "https://chatgpt.com/backend-api";
@@ -7,11 +8,18 @@ const CODEX_BACKEND_URL = "https://chatgpt.com/backend-api";
7
8
  // The execution providers whose models each kind of plan serves; a ChatGPT
8
9
  // plan's OpenAI models execute as `openai-codex` once resolve points them at
9
10
  // the Codex backend.
11
+ //
12
+ // The order is the order a model's plans serve it in, maker plans first, then
13
+ // the router's API credential. It matches the control plane's
14
+ // SUBSCRIPTION_OAUTH_PROVIDERS; keep the two in sync.
10
15
  const SUBSCRIPTION_MODEL_PROVIDERS: Record<SubscriptionKind, readonly string[]> = {
11
- openai_codex: ["openai", "openai-codex"],
12
16
  anthropic_claude_code: ["anthropic"],
17
+ openai_codex: ["openai", "openai-codex"],
18
+ xai_grok: ["xai"],
13
19
  meta_muse: ["meta"],
14
20
  opencode_go: ["opencode-go"],
21
+ github_copilot: ["github-copilot"],
22
+ vercel_token_pass: ["vercel-token-pass"],
15
23
  };
16
24
 
17
25
  // The client each plan is limited to, as its provider limits it: a Claude Code
@@ -26,6 +34,18 @@ export function subscriptionModelProvider(kind: SubscriptionKind): string {
26
34
  return SUBSCRIPTION_MODEL_PROVIDERS[kind][0]!;
27
35
  }
28
36
 
37
+ /**
38
+ * The plans that can serve a model, in the order they serve it: each whose
39
+ * provider is the model's maker or serves it as its own model.
40
+ */
41
+ export function modelSubscriptions(modelId: string): SubscriptionKind[] {
42
+ const maker = providerForModel(modelId);
43
+ return (Object.keys(SUBSCRIPTION_MODEL_PROVIDERS) as SubscriptionKind[]).filter((kind) => {
44
+ const provider = subscriptionModelProvider(kind);
45
+ return provider === maker || providerModel(modelId, provider) !== undefined;
46
+ });
47
+ }
48
+
29
49
  /**
30
50
  * The only client a plan may serve, or undefined when it serves any client:
31
51
  * ChatGPT plans serve any client through the Codex backend.
@@ -266,8 +266,6 @@ export type LinkApprovalPage = {
266
266
  connections(subject: string): Promise<LinkConnection[]>;
267
267
  /** The user ends one of their grants; false when they have none by that id. */
268
268
  disconnect(subject: string, grantId: string): Promise<boolean>;
269
- /** Replace one of the user's grants' budget (empty removes it); false when they have none by that id. */
270
- setBudget(subject: string, grantId: string, budget: LinkBudget[]): Promise<boolean>;
271
269
  /** Replace one of the user's grants' monthly spend limits; false when they have none by that id. */
272
270
  setLimits(subject: string, grantId: string, limits: LinkLimit[]): Promise<boolean>;
273
271
  };
@@ -317,19 +315,26 @@ export type LinkConnect = {
317
315
  export type LinkBudget = { windowMinutes: number; maxTokens: number };
318
316
 
319
317
  /**
320
- * The most a grant may spend on one of its plans or API keys in a calendar
321
- * month (UTC), in US dollars. A plan's spend is what its requests would have
322
- * cost at API prices. See link/budget.ts.
318
+ * A grant's limit on one of its plans or API keys; exactly one field is set.
319
+ * A plan leaves the grant while any of its plan-wide quota windows is at
320
+ * `maxPercent` used, whoever used it, until that window resets: the rest is
321
+ * the user's. An API key leaves it once the grant's spend on it this calendar
322
+ * month (UTC) reaches `monthlyUsd`. See link/budget.ts.
323
323
  */
324
- export type LinkLimit = { credentialId: string; monthlyUsd: number };
324
+ export type LinkLimit = { credentialId: string; maxPercent?: number; monthlyUsd?: number };
325
325
 
326
- /** A granted plan or key's spend this month, and its limit when it has one. */
326
+ /** A granted plan or key's spend and standing, and its limit when it has one. */
327
327
  export type LinkSpend = {
328
328
  credentialId: string;
329
+ // What the grant spent on it this month: for a plan, at API prices.
329
330
  spentUsd: number;
330
331
  monthlyUsd: number | null;
331
- // At its limit: the grant leaves it out until the month ends.
332
+ maxPercent: number | null;
333
+ // A plan's most-used plan-wide window, in percent; null when unknown.
334
+ usedPercent: number | null;
335
+ // At its limit: the grant leaves it out until `resetsAt`.
332
336
  reached: boolean;
337
+ resetsAt: string | null;
333
338
  };
334
339
 
335
340
  /** A budget window as it stands for a grant. */
@@ -49,6 +49,7 @@ import {
49
49
  import type { NextSubscription } from "../platform/platform.js";
50
50
  import { providerResponseCaptureFromError, type ProviderResponseCapture } from "./response_capture.js";
51
51
  import { createStreamFinalizer } from "./stream_finalization.js";
52
+ import { copilotEndpoint } from "./github_copilot.js";
52
53
  import { readCodexModelLimitReset, readCodexUsage } from "./codex_usage.js";
53
54
  import {
54
55
  detectSubscriptionUsageLimit,
@@ -67,8 +68,14 @@ import {
67
68
  export type ProviderChoice = {
68
69
  decision: RoutingDecision;
69
70
  execution: ProviderExecution;
71
+ // Which of the model's fallbacks serves, or undefined for its first
72
+ // candidate.
73
+ fallback?: number;
70
74
  };
71
75
 
76
+ // The same model served by its next candidate; see ResolvedRouter.model_fallbacks.
77
+ export type NextProviderChoice = (choice: ProviderChoice) => Promise<ProviderChoice | undefined>;
78
+
72
79
  export type PreparedStreamExecution = {
73
80
  choice: ProviderChoice;
74
81
  started: StartedStream;
@@ -92,6 +99,7 @@ export async function executeNonStreaming(args: {
92
99
  interaction: InteractionRequest;
93
100
  primary: ProviderChoice;
94
101
  primaryRetries: number;
102
+ nextChoice?: NextProviderChoice;
95
103
  reporter: RoutedRuntimeReporter;
96
104
  protocol: RoutedProtocol;
97
105
  startedCompletion?: StartedCompletion;
@@ -132,6 +140,7 @@ export async function completeNonStreaming(args: {
132
140
  interaction: InteractionRequest;
133
141
  primary: ProviderChoice;
134
142
  primaryRetries: number;
143
+ nextChoice?: NextProviderChoice;
135
144
  reporter: RoutedRuntimeReporter;
136
145
  protocol: RoutedProtocol;
137
146
  startedCompletion?: StartedCompletion;
@@ -251,6 +260,7 @@ async function completeWithRetries(args: {
251
260
  interaction: InteractionRequest;
252
261
  primary: ProviderChoice;
253
262
  primaryRetries: number;
263
+ nextChoice?: NextProviderChoice;
254
264
  reporter: RoutedRuntimeReporter;
255
265
  startedCompletion?: StartedCompletion;
256
266
  clientSignal?: AbortSignal;
@@ -271,6 +281,7 @@ async function completeWithRetries(args: {
271
281
  recordFailedAttempt: ({ choice, attempt, result }) =>
272
282
  recordSettledFailure(args.reporter, choice, attempt, result),
273
283
  onSubscription: onNextSubscription,
284
+ nextChoice: args.nextChoice,
274
285
  });
275
286
  }
276
287
 
@@ -493,6 +504,7 @@ export async function prepareStreamingExecution(args: {
493
504
  interaction: InteractionRequest;
494
505
  primary: ProviderChoice;
495
506
  primaryRetries: number;
507
+ nextChoice?: NextProviderChoice;
496
508
  reporter: RoutedRuntimeReporter;
497
509
  startedStream?: StartedStream;
498
510
  initialError?: unknown;
@@ -520,6 +532,7 @@ export async function prepareStreamingExecution(args: {
520
532
  recordFailedAttempt: ({ choice, attempt, result }) =>
521
533
  recordSettledFailure(args.reporter, choice, attempt, result),
522
534
  onSubscription: onNextSubscription,
535
+ nextChoice: args.nextChoice,
523
536
  });
524
537
  args.reporter.attachServingDecision(result.choice.decision);
525
538
  if (result.kind === "customer_canceled") {
@@ -748,6 +761,9 @@ async function providerFailedAttempt(
748
761
  if (execution.providerKeySource === "subscription") logUnclassifiedRefusal(execution, responseCapture);
749
762
  // The customer's own key was refused: theirs to fix, not a gateway fault.
750
763
  const rejected = execution.providerKeySource === "user" && (status === 401 || status === 403);
764
+ // A plan whose token is refused cannot serve; the model's next candidate
765
+ // may. The plan is not held: a refused token is not a usage limit.
766
+ const planRefused = execution.providerKeySource === "subscription" && (status === 401 || status === 403);
751
767
  return {
752
768
  kind: "provider_failed",
753
769
  message: rejected
@@ -755,6 +771,7 @@ async function providerFailedAttempt(
755
771
  : message,
756
772
  latencyMs,
757
773
  retryable: !isPromptPolicyRejection(message),
774
+ ...(planRefused ? { failover: true as const } : {}),
758
775
  };
759
776
  }
760
777
  const limit = detected.oauthProvider === "openai_codex" && detected.model === undefined
@@ -774,7 +791,8 @@ async function providerFailedAttempt(
774
791
  ...(execution.subscriptionLinkGrantId ? { linkGrantId: execution.subscriptionLinkGrantId } : {}),
775
792
  });
776
793
  // Nothing reached the client yet, so the same request is retried on the
777
- // next plan with quota for this model, including model-specific limits.
794
+ // next plan of this kind with quota for this model, including
795
+ // model-specific limits; with none, on the model's next candidate.
778
796
  const nextSubscription = switched
779
797
  ? await nextSubscriptionFor(execution)
780
798
  : undefined;
@@ -783,7 +801,7 @@ async function providerFailedAttempt(
783
801
  message: failureWithSubscriptionUsageLimit(message, { ...limit, switched }),
784
802
  latencyMs,
785
803
  retryable: false,
786
- ...(nextSubscription === undefined ? {} : { nextSubscription }),
804
+ ...(nextSubscription === undefined ? { failover: true as const } : { nextSubscription }),
787
805
  };
788
806
  }
789
807
 
@@ -914,6 +932,8 @@ function onNextSubscription(choice: ProviderChoice, next: NextSubscription): Pro
914
932
  apiKey: next.apiKey,
915
933
  auth: { type: "bearer", apiKey: next.apiKey },
916
934
  subscriptionCredentialId: next.credentialId,
935
+ // A Copilot token names its own account's API origin.
936
+ ...(choice.execution.provider === "github-copilot" ? { endpoint: copilotEndpoint(next.apiKey) } : {}),
917
937
  },
918
938
  };
919
939
  }
@@ -0,0 +1,24 @@
1
+ import type { ProviderEndpoint } from "./pi.js";
2
+
3
+ // Copilot serves only editors that name themselves; these are VS Code's
4
+ // Copilot Chat, whose sign-in Dari runs.
5
+ export const COPILOT_HEADERS: Readonly<Record<string, string>> = {
6
+ "User-Agent": "GitHubCopilotChat/0.35.0",
7
+ "Editor-Version": "vscode/1.107.0",
8
+ "Editor-Plugin-Version": "copilot-chat/0.35.0",
9
+ "Copilot-Integration-Id": "vscode-chat",
10
+ };
11
+
12
+ /**
13
+ * Where a Copilot token is served: its account's API origin, which the token
14
+ * names as `proxy-ep=proxy.<host>` (`api.<host>` serves the models), with the
15
+ * Copilot headers. A token that names none keeps the configured origin.
16
+ */
17
+ export function copilotEndpoint(token: string): ProviderEndpoint {
18
+ const proxy = /(?:^|;)proxy-ep=([^;]+)/.exec(token)?.[1];
19
+ const host = proxy?.replace(/^proxy\./, "api.");
20
+ return {
21
+ ...(host && /^[a-z0-9.-]+$/i.test(host) ? { baseUrl: `https://${host}` } : {}),
22
+ headers: { ...COPILOT_HEADERS },
23
+ };
24
+ }
@@ -473,7 +473,9 @@ function resolveModelMetadata(
473
473
  // Claude deployments through the Anthropic registry, whose model metadata
474
474
  // (input modalities, adaptive-thinking compat, thinking level maps) is the
475
475
  // same on Foundry; the resolved config still owns api, base URL, and cost.
476
+ // A Vercel Token Pass key serves Vercel AI Gateway's own models.
476
477
  function registryProvider(provider: string, modelId: string): string {
478
+ if (provider === "vercel-token-pass") return "vercel-ai-gateway";
477
479
  if (provider !== "azure") return provider;
478
480
  return providerForModel(modelId) === "anthropic" ? "anthropic" : "azure-openai-responses";
479
481
  }
@@ -10,6 +10,9 @@ export type ProviderAttemptFailure =
10
10
  // The plan that replaced an exhausted one; the same request is retried
11
11
  // on it at once.
12
12
  nextSubscription?: NextSubscription;
13
+ // Neither this credential nor a retry on it can serve: the same request
14
+ // moves to the model's next candidate (another plan, or the API key).
15
+ failover?: true;
13
16
  }
14
17
  | {
15
18
  kind: "customer_canceled";
@@ -51,6 +54,9 @@ type RunProviderAttemptPlanArgs<TChoice, TValue, TInitial> = {
51
54
  // The choice served by another of the user's plans; without it a failure
52
55
  // naming one is not retried on it.
53
56
  onSubscription?: (choice: TChoice, next: NextSubscription) => TChoice;
57
+ // The same model served by its next candidate, or undefined when none is
58
+ // left; without it a failure asking for failover stands.
59
+ nextChoice?: (choice: TChoice) => Promise<TChoice | undefined>;
54
60
  };
55
61
 
56
62
  type ChoiceResult<TChoice, TValue> = ProviderAttemptPlanResult<TChoice, TValue>;
@@ -130,6 +136,15 @@ async function runChoice<TChoice, TValue, TInitial>(
130
136
  maxAttempts += 1;
131
137
  continue;
132
138
  }
139
+ if (result.kind === "provider_failed" && result.failover === true && args.nextChoice) {
140
+ const next = await args.nextChoice(choice);
141
+ if (next !== undefined) {
142
+ // The next candidate gets the router's full retry budget.
143
+ choice = next;
144
+ maxAttempts = attempt + args.retries + 1;
145
+ continue;
146
+ }
147
+ }
133
148
  if (!result.retryable) break;
134
149
  }
135
150
 
@@ -36,6 +36,7 @@ import type {
36
36
  ServedDecision,
37
37
  } from "../types.js";
38
38
  import { awsRoleCredentials } from "./aws_assume_role.js";
39
+ import { copilotEndpoint } from "./github_copilot.js";
39
40
  import {
40
41
  attachProviderResponseCapture,
41
42
  createProviderResponseCapture,
@@ -166,7 +167,10 @@ export function prepareProviderExecution(args: {
166
167
  ...(args.systemPromptSuffix ? { systemPromptSuffix: args.systemPromptSuffix } : {}),
167
168
  ...(supplied?.baseUrl || supplied?.headers
168
169
  ? { endpoint: { baseUrl: supplied.baseUrl, headers: supplied.headers } }
169
- : {}),
170
+ // A Copilot token names its account's own API origin.
171
+ : provider === "github-copilot" && auth.type === "bearer"
172
+ ? { endpoint: copilotEndpoint(auth.apiKey) }
173
+ : {}),
170
174
  provider,
171
175
  providerKeySource,
172
176
  // Only resolved personal subscriptions are held at their usage limit;
@@ -174,7 +178,7 @@ export function prepareProviderExecution(args: {
174
178
  ...(providerKeySource === "subscription" && (!supplied || supplied.credentialId !== undefined) && args.credentials.attributed_user_id
175
179
  ? {
176
180
  subscriptionUserId: args.credentials.attributed_user_id,
177
- subscriptionCredentialId: supplied?.credentialId ?? args.credentials.subscription_credential_ids?.[provider],
181
+ subscriptionCredentialId: supplied?.credentialId,
178
182
  ...(args.credentials.link_grant_id ? { subscriptionLinkGrantId: args.credentials.link_grant_id } : {}),
179
183
  }
180
184
  : {}),
@@ -1,10 +1,10 @@
1
1
  import { isRecord } from "@mupt-ai/dari-router/policy-engine";
2
2
 
3
3
  import { providerFailureMessage } from "../runtime/provider_stream_outcome.js";
4
- import type { ProviderKeySource } from "../types.js";
4
+ import type { ProviderKeySource, SubscriptionKind } from "../types.js";
5
5
  import type { CapturedProviderResponse } from "./response_capture.js";
6
6
 
7
- export type SubscriptionOAuthProvider = "anthropic_claude_code" | "openai_codex" | "meta_muse" | "opencode_go";
7
+ export type SubscriptionOAuthProvider = SubscriptionKind;
8
8
 
9
9
  export type SubscriptionUsageLimit = {
10
10
  oauthProvider: SubscriptionOAuthProvider;
@@ -168,6 +168,9 @@ const SUBSCRIPTION_LABEL: Record<SubscriptionOAuthProvider, string> = {
168
168
  openai_codex: "ChatGPT",
169
169
  meta_muse: "Muse Code",
170
170
  opencode_go: "OpenCode Go",
171
+ github_copilot: "GitHub Copilot",
172
+ xai_grok: "Grok",
173
+ vercel_token_pass: "Vercel Token Pass",
171
174
  };
172
175
 
173
176
  // Providers whose plan page the accounts page links to for buying another.
@@ -182,15 +185,19 @@ export function subscriptionOAuthProviderFor(
182
185
  if (provider === "openai" || provider === "openai-codex") return "openai_codex";
183
186
  if (provider === "meta") return "meta_muse";
184
187
  if (provider === "opencode-go") return "opencode_go";
188
+ if (provider === "github-copilot") return "github_copilot";
189
+ if (provider === "xai") return "xai_grok";
190
+ if (provider === "vercel-token-pass") return "vercel_token_pass";
185
191
  return null;
186
192
  }
187
193
 
188
194
  /**
189
195
  * Recognize a personal subscription that has run out of quota. Claude Code
190
196
  * answers 429 with the unified rate-limit headers; the ChatGPT backend answers
191
- * 429 with a structured `error.code` and `resets_at`. Meta documents no
192
- * usage-limit signature for Muse Code plans, so any 429 on one holds it until
193
- * the reset Meta gives, if any. OpenCode Go names its refusal
197
+ * 429 with a structured `error.code` and `resets_at`. Meta, GitHub Copilot,
198
+ * xAI, Kimi, and Vercel Token Pass document no usage-limit signature for their
199
+ * plans, so any 429 on one holds it until the reset it gives, if any. OpenCode
200
+ * Go names its refusal
194
201
  * `GoUsageLimitError`. The Codex WebSocket
195
202
  * transport bypasses fetch, so its usage-limit error only survives as message
196
203
  * text and is matched there without a reset time.
@@ -210,9 +217,11 @@ export function detectSubscriptionUsageLimit(args: {
210
217
  if (oauthProvider === "anthropic_claude_code") {
211
218
  return anthropicUsageLimit(args.captured, args.model, now);
212
219
  }
213
- if (oauthProvider === "meta_muse") return museUsageLimit(args.captured, now);
214
220
  if (oauthProvider === "opencode_go") return openCodeGoUsageLimit(args.captured, now);
215
- return codexUsageLimit(args.captured, providerFailureMessage(args.failure), args.model, now);
221
+ if (oauthProvider === "openai_codex") {
222
+ return codexUsageLimit(args.captured, providerFailureMessage(args.failure), args.model, now);
223
+ }
224
+ return planRateLimit(oauthProvider, args.captured, now);
216
225
  }
217
226
 
218
227
  // Claude Code plans also cap some model families on their own
@@ -294,15 +303,16 @@ function codexModelLimitExhausted(headers: Headers): boolean {
294
303
  return other && !plan;
295
304
  }
296
305
 
297
- function museUsageLimit(
306
+ function planRateLimit(
307
+ oauthProvider: SubscriptionOAuthProvider,
298
308
  captured: CapturedProviderResponse | undefined,
299
309
  now: Date,
300
310
  ): SubscriptionUsageLimit | null {
301
311
  if (captured === undefined || captured.status !== 429) return null;
302
312
  return {
303
- oauthProvider: "meta_muse",
313
+ oauthProvider,
304
314
  resetsAt: laterReset(codexErrorBody(captured.bodyText)?.resets_at, captured.headers, now),
305
- reason: "Muse Code usage limit reached.",
315
+ reason: `${SUBSCRIPTION_LABEL[oauthProvider]} usage limit reached.`,
306
316
  };
307
317
  }
308
318