@mupt-ai/dari-gateway 1.0.290 → 1.0.292
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 +1 -1
- package/dist/ingress/codex_models.d.ts +1 -1
- package/dist/ingress/codex_models.js +1 -1
- package/dist/ingress/models.js +11 -1
- package/dist/ingress/muse_models.d.ts +1 -1
- package/dist/ingress/muse_models.js +1 -1
- package/dist/models/canonical.d.ts +7 -2
- package/dist/models/canonical.js +3 -3
- package/dist/models/canonical_models.json +337 -0
- package/dist/platform/platform.d.ts +30 -2
- package/dist/providers/execution.js +66 -1
- package/dist/providers/provider_attempt_runner.d.ts +3 -0
- package/dist/providers/provider_attempt_runner.js +11 -2
- package/dist/providers/response_capture.d.ts +3 -1
- package/dist/providers/response_capture.js +6 -2
- package/dist/providers/subscription_limits.d.ts +16 -0
- package/dist/providers/subscription_limits.js +25 -0
- package/dist/runtime/prepare_routed_completion.js +2 -0
- package/dist/self_host/main.js +4 -3
- package/dist/self_host/platform.d.ts +1 -1
- package/dist/self_host/platform.js +15 -0
- package/dist/self_host/resolve.d.ts +1 -0
- package/dist/self_host/resolve.js +7 -2
- package/dist/self_host/subscription_test_gateway.d.ts +16 -0
- package/dist/self_host/subscription_test_gateway.js +120 -0
- package/dist/self_host/subscriptions/http.js +8 -1
- package/dist/self_host/subscriptions/index.d.ts +3 -2
- package/dist/self_host/subscriptions/index.js +3 -2
- package/dist/self_host/subscriptions/store.d.ts +36 -5
- package/dist/self_host/subscriptions/store.js +129 -17
- package/dist/self_host/subscriptions/test_support.d.ts +15 -0
- package/dist/self_host/subscriptions/test_support.js +34 -0
- package/dist/types.d.ts +1 -0
- package/package.json +2 -2
- package/src/ingress/codex_models.ts +2 -2
- package/src/ingress/models.ts +14 -1
- package/src/ingress/muse_models.ts +2 -2
- package/src/models/canonical.ts +10 -3
- package/src/models/canonical_models.json +337 -0
- package/src/platform/platform.ts +39 -2
- package/src/providers/execution.ts +72 -0
- package/src/providers/provider_attempt_runner.ts +18 -2
- package/src/providers/response_capture.ts +10 -2
- package/src/providers/subscription_limits.ts +39 -0
- package/src/runtime/prepare_routed_completion.ts +2 -0
- package/src/self_host/main.ts +4 -3
- package/src/self_host/platform.ts +14 -1
- package/src/self_host/resolve.ts +10 -2
- package/src/self_host/subscription_test_gateway.ts +129 -0
- package/src/self_host/subscriptions/http.ts +9 -1
- package/src/self_host/subscriptions/index.ts +3 -2
- package/src/self_host/subscriptions/store.ts +181 -19
- package/src/types.ts +4 -0
- package/dist/models/canonical_model_ids.json +0 -33
- package/src/models/canonical_model_ids.json +0 -33
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ The gateway is the runtime behind Dari's hosted routing service, packaged to run
|
|
|
27
27
|
## Features
|
|
28
28
|
|
|
29
29
|
- **One endpoint, three protocols.** `/v1/messages`, `/v1/chat/completions`, and `/v1/responses` (plus `/v1/models`) reach API-key providers in the [Pi AI](https://www.npmjs.com/package/@mupt-ai/pi-ai) catalog, such as Anthropic, OpenAI, and OpenRouter, or any OpenAI-compatible server.
|
|
30
|
-
- **Subscription pooling.** Connect several Claude Code (Max/Pro) or ChatGPT plans.
|
|
30
|
+
- **Subscription pooling.** Connect several Claude Code (Max/Pro) or ChatGPT plans. Each conversation stays on one plan so its prompt cache stays warm. Claude Code plans report their usage on every response: once a plan has used 95% of a window, its conversations move to another plan at their next turn, and new conversations start on the plan whose quota resets soonest. A plan that reports a usage limit is held until its reset, and the request it refused is served by the next plan at once. When every plan is held, the provider's API key serves if you set one.
|
|
31
31
|
- **Failover.** A provider failure retries the router's next model.
|
|
32
32
|
- **Prompt-cache aware.** Conversations keep their identity and serving model across turns, so provider caches stay warm. Responses carry `X-Dari-Selected-Model`, `X-Dari-Thinking-Level`, and `X-Dari-Conversation-Id` headers for status lines.
|
|
33
33
|
- **Coding agents.** `dari claude`, `dari codex`, `dari opencode`, and `dari pi` from the [Dari CLI](https://github.com/mupt-ai/dari) launch straight against your gateway.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { ResolvedRouter } from "../types.js";
|
|
2
|
-
export declare function codexModelsResponse(router: Pick<ResolvedRouter, "enabled_models" | "model_configs" | "allow_long_context" | "autorouting_enabled">, imageInputFor: (model: string) => boolean): Record<string, unknown>;
|
|
2
|
+
export declare function codexModelsResponse(router: Pick<ResolvedRouter, "enabled_models" | "model_configs" | "allow_long_context" | "autorouting_enabled" | "routing_listed">, imageInputFor: (model: string) => boolean): Record<string, unknown>;
|
|
@@ -71,7 +71,7 @@ function codexCatalogEntry(args) {
|
|
|
71
71
|
export function codexModelsResponse(router, imageInputFor) {
|
|
72
72
|
return {
|
|
73
73
|
models: [
|
|
74
|
-
...(router.autorouting_enabled === false ? [] : [codexCatalogEntry({
|
|
74
|
+
...(router.autorouting_enabled === false || router.routing_listed === false ? [] : [codexCatalogEntry({
|
|
75
75
|
slug: CANONICAL_ROUTER_MODEL,
|
|
76
76
|
displayName: "Dari Routing",
|
|
77
77
|
description: "Routes Codex HTTP Responses requests through the configured Dari model router.",
|
package/dist/ingress/models.js
CHANGED
|
@@ -25,7 +25,8 @@ export async function handleModels(request, routerId) {
|
|
|
25
25
|
const apiKey = anthropicClient ? anthropicToken(request.headers) : bearerToken(request.headers);
|
|
26
26
|
// Resolution authenticates the key and confirms the router exists even
|
|
27
27
|
// when the response body (Anthropic shape) does not depend on it.
|
|
28
|
-
const
|
|
28
|
+
const clientHarness = listedClientHarness(request.headers);
|
|
29
|
+
const router = await resolveRouter({ routerId, apiKey, ...(clientHarness ? { clientHarness } : {}) });
|
|
29
30
|
const query = new URL(request.url).searchParams;
|
|
30
31
|
if (query.get("detail") === "1") {
|
|
31
32
|
const providers = query.get("provider")?.split(",").map((id) => id.trim()).filter(Boolean);
|
|
@@ -63,3 +64,12 @@ export async function handleModels(request, routerId) {
|
|
|
63
64
|
return response;
|
|
64
65
|
}
|
|
65
66
|
}
|
|
67
|
+
// The Dari CLI names the agent a model list is for, so subscriptions that
|
|
68
|
+
// serve only that agent (a Claude Code or Muse Code plan) count toward the
|
|
69
|
+
// list as they will for its requests. It changes only what is listed: each
|
|
70
|
+
// request is still recognized by its own headers.
|
|
71
|
+
const CLIENT_HARNESS_HEADER = "x-dari-client-harness";
|
|
72
|
+
function listedClientHarness(headers) {
|
|
73
|
+
const value = headers.get(CLIENT_HARNESS_HEADER)?.trim();
|
|
74
|
+
return value === "claude_code" || value === "muse_code" ? value : undefined;
|
|
75
|
+
}
|
|
@@ -5,5 +5,5 @@ import type { ResolvedRouter } from "../types.js";
|
|
|
5
5
|
* models lets Muse Code pick from them and start on the first; `dari/routing`
|
|
6
6
|
* leads it only on the Autorouting plan.
|
|
7
7
|
*/
|
|
8
|
-
export declare function museCodeModelsResponse(router: Pick<ResolvedRouter, "enabled_models" | "autorouting_enabled">): Record<string, unknown>;
|
|
8
|
+
export declare function museCodeModelsResponse(router: Pick<ResolvedRouter, "enabled_models" | "autorouting_enabled" | "routing_listed">): Record<string, unknown>;
|
|
9
9
|
export declare function handleMuseCodeModels(request: Request, routerId: string): Promise<Response>;
|
|
@@ -9,7 +9,7 @@ import { resolveRouter } from "../strategies/routing.js";
|
|
|
9
9
|
*/
|
|
10
10
|
export function museCodeModelsResponse(router) {
|
|
11
11
|
const ids = [
|
|
12
|
-
...(router.autorouting_enabled === false ? [] : ["dari/routing"]),
|
|
12
|
+
...(router.autorouting_enabled === false || router.routing_listed === false ? [] : ["dari/routing"]),
|
|
13
13
|
...router.enabled_models.map((model) => `dari/model/${model}`),
|
|
14
14
|
];
|
|
15
15
|
return {
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export type CanonicalModel = {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
thinking_levels: string[];
|
|
5
|
+
};
|
|
6
|
+
/** The canonical models Dari serves, from the monorepo's router catalog. */
|
|
7
|
+
export declare const CANONICAL_MODELS: readonly CanonicalModel[];
|
package/dist/models/canonical.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
/** The canonical
|
|
3
|
-
export const
|
|
1
|
+
import canonicalModels from "./canonical_models.json" with { type: "json" };
|
|
2
|
+
/** The canonical models Dari serves, from the monorepo's router catalog. */
|
|
3
|
+
export const CANONICAL_MODELS = canonicalModels;
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "openai/gpt-6-astra",
|
|
4
|
+
"name": "GPT-6 Astra",
|
|
5
|
+
"thinking_levels": [
|
|
6
|
+
"low",
|
|
7
|
+
"medium",
|
|
8
|
+
"high",
|
|
9
|
+
"xhigh",
|
|
10
|
+
"max"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"id": "openai/gpt-6.1-terra",
|
|
15
|
+
"name": "GPT-6.1 Terra",
|
|
16
|
+
"thinking_levels": [
|
|
17
|
+
"off",
|
|
18
|
+
"low",
|
|
19
|
+
"medium",
|
|
20
|
+
"high",
|
|
21
|
+
"xhigh",
|
|
22
|
+
"max"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"id": "openai/gpt-6.1-sol",
|
|
27
|
+
"name": "GPT-6.1 Sol",
|
|
28
|
+
"thinking_levels": [
|
|
29
|
+
"off",
|
|
30
|
+
"low",
|
|
31
|
+
"medium",
|
|
32
|
+
"high",
|
|
33
|
+
"xhigh",
|
|
34
|
+
"max"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"id": "openai/gpt-6-sol",
|
|
39
|
+
"name": "GPT-6 Sol",
|
|
40
|
+
"thinking_levels": [
|
|
41
|
+
"off",
|
|
42
|
+
"low",
|
|
43
|
+
"medium",
|
|
44
|
+
"high",
|
|
45
|
+
"xhigh",
|
|
46
|
+
"max"
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"id": "openai/gpt-6-luna",
|
|
51
|
+
"name": "GPT-6 Luna",
|
|
52
|
+
"thinking_levels": [
|
|
53
|
+
"off",
|
|
54
|
+
"low",
|
|
55
|
+
"medium",
|
|
56
|
+
"high",
|
|
57
|
+
"xhigh",
|
|
58
|
+
"max"
|
|
59
|
+
]
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"id": "openai/gpt-5.6-sol",
|
|
63
|
+
"name": "GPT-5.6 Sol",
|
|
64
|
+
"thinking_levels": [
|
|
65
|
+
"off",
|
|
66
|
+
"low",
|
|
67
|
+
"medium",
|
|
68
|
+
"high",
|
|
69
|
+
"xhigh",
|
|
70
|
+
"max"
|
|
71
|
+
]
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
"id": "openai/gpt-5.6-terra",
|
|
75
|
+
"name": "GPT-5.6 Terra",
|
|
76
|
+
"thinking_levels": [
|
|
77
|
+
"off",
|
|
78
|
+
"low",
|
|
79
|
+
"medium",
|
|
80
|
+
"high",
|
|
81
|
+
"xhigh",
|
|
82
|
+
"max"
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"id": "openai/gpt-5.6-luna",
|
|
87
|
+
"name": "GPT-5.6 Luna",
|
|
88
|
+
"thinking_levels": [
|
|
89
|
+
"off",
|
|
90
|
+
"low",
|
|
91
|
+
"medium",
|
|
92
|
+
"high",
|
|
93
|
+
"xhigh",
|
|
94
|
+
"max"
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"id": "anthropic/claude-opus-5-5",
|
|
99
|
+
"name": "Claude Opus 5.5",
|
|
100
|
+
"thinking_levels": [
|
|
101
|
+
"off",
|
|
102
|
+
"low",
|
|
103
|
+
"medium",
|
|
104
|
+
"high",
|
|
105
|
+
"xhigh",
|
|
106
|
+
"max"
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
"id": "anthropic/claude-opus-5",
|
|
111
|
+
"name": "Claude Opus 5",
|
|
112
|
+
"thinking_levels": [
|
|
113
|
+
"off",
|
|
114
|
+
"low",
|
|
115
|
+
"medium",
|
|
116
|
+
"high",
|
|
117
|
+
"xhigh",
|
|
118
|
+
"max"
|
|
119
|
+
]
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"id": "anthropic/claude-sonnet-5-5",
|
|
123
|
+
"name": "Claude Sonnet 5.5",
|
|
124
|
+
"thinking_levels": [
|
|
125
|
+
"low",
|
|
126
|
+
"medium",
|
|
127
|
+
"high",
|
|
128
|
+
"xhigh",
|
|
129
|
+
"max"
|
|
130
|
+
]
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"id": "anthropic/claude-sonnet-5",
|
|
134
|
+
"name": "Claude Sonnet 5",
|
|
135
|
+
"thinking_levels": [
|
|
136
|
+
"off",
|
|
137
|
+
"low",
|
|
138
|
+
"medium",
|
|
139
|
+
"high",
|
|
140
|
+
"xhigh",
|
|
141
|
+
"max"
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"id": "anthropic/claude-fable-5-2",
|
|
146
|
+
"name": "Claude Fable 5.2",
|
|
147
|
+
"thinking_levels": [
|
|
148
|
+
"low",
|
|
149
|
+
"medium",
|
|
150
|
+
"high",
|
|
151
|
+
"xhigh",
|
|
152
|
+
"max"
|
|
153
|
+
]
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
"id": "anthropic/claude-fable-5-1",
|
|
157
|
+
"name": "Claude Fable 5.1",
|
|
158
|
+
"thinking_levels": [
|
|
159
|
+
"low",
|
|
160
|
+
"medium",
|
|
161
|
+
"high",
|
|
162
|
+
"xhigh",
|
|
163
|
+
"max"
|
|
164
|
+
]
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
"id": "anthropic/claude-fable-5",
|
|
168
|
+
"name": "Claude Fable 5",
|
|
169
|
+
"thinking_levels": [
|
|
170
|
+
"low",
|
|
171
|
+
"medium",
|
|
172
|
+
"high",
|
|
173
|
+
"xhigh",
|
|
174
|
+
"max"
|
|
175
|
+
]
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"id": "anthropic/claude-haiku-5-5",
|
|
179
|
+
"name": "Claude Haiku 5.5",
|
|
180
|
+
"thinking_levels": [
|
|
181
|
+
"low",
|
|
182
|
+
"medium",
|
|
183
|
+
"high",
|
|
184
|
+
"xhigh",
|
|
185
|
+
"max"
|
|
186
|
+
]
|
|
187
|
+
},
|
|
188
|
+
{
|
|
189
|
+
"id": "anthropic/claude-haiku-4-5",
|
|
190
|
+
"name": "Claude Haiku 4.5",
|
|
191
|
+
"thinking_levels": [
|
|
192
|
+
"off",
|
|
193
|
+
"low",
|
|
194
|
+
"medium",
|
|
195
|
+
"high"
|
|
196
|
+
]
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
"id": "meta/muse-wonder-1.0",
|
|
200
|
+
"name": "Muse Wonder 1.0",
|
|
201
|
+
"thinking_levels": [
|
|
202
|
+
"minimal",
|
|
203
|
+
"low",
|
|
204
|
+
"medium",
|
|
205
|
+
"high",
|
|
206
|
+
"xhigh",
|
|
207
|
+
"max"
|
|
208
|
+
]
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
"id": "meta/muse-spark-1.3",
|
|
212
|
+
"name": "Muse Spark 1.3",
|
|
213
|
+
"thinking_levels": [
|
|
214
|
+
"minimal",
|
|
215
|
+
"low",
|
|
216
|
+
"medium",
|
|
217
|
+
"high",
|
|
218
|
+
"xhigh",
|
|
219
|
+
"max"
|
|
220
|
+
]
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
"id": "meta/muse-ember",
|
|
224
|
+
"name": "Muse Ember",
|
|
225
|
+
"thinking_levels": [
|
|
226
|
+
"minimal",
|
|
227
|
+
"low",
|
|
228
|
+
"medium",
|
|
229
|
+
"high",
|
|
230
|
+
"xhigh",
|
|
231
|
+
"max"
|
|
232
|
+
]
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"id": "meta/muse-spark-1.2",
|
|
236
|
+
"name": "Muse Spark 1.2",
|
|
237
|
+
"thinking_levels": [
|
|
238
|
+
"minimal",
|
|
239
|
+
"low",
|
|
240
|
+
"medium",
|
|
241
|
+
"high",
|
|
242
|
+
"xhigh"
|
|
243
|
+
]
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"id": "moonshotai/Kimi-K3",
|
|
247
|
+
"name": "Kimi K3",
|
|
248
|
+
"thinking_levels": [
|
|
249
|
+
"low",
|
|
250
|
+
"high",
|
|
251
|
+
"max"
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
"id": "zai-org/GLM-5.3",
|
|
256
|
+
"name": "GLM 5.3",
|
|
257
|
+
"thinking_levels": [
|
|
258
|
+
"low",
|
|
259
|
+
"high",
|
|
260
|
+
"max"
|
|
261
|
+
]
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
"id": "zai-org/GLM-5.3-Flash",
|
|
265
|
+
"name": "GLM 5.3 Flash",
|
|
266
|
+
"thinking_levels": [
|
|
267
|
+
"low",
|
|
268
|
+
"high",
|
|
269
|
+
"max"
|
|
270
|
+
]
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"id": "deepseek-ai/DeepSeek-V4-Pro-0813",
|
|
274
|
+
"name": "DeepSeek V4 Pro 0813",
|
|
275
|
+
"thinking_levels": [
|
|
276
|
+
"off",
|
|
277
|
+
"high",
|
|
278
|
+
"max"
|
|
279
|
+
]
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
"id": "zai-org/GLM-5.2",
|
|
283
|
+
"name": "GLM 5.2",
|
|
284
|
+
"thinking_levels": [
|
|
285
|
+
"off",
|
|
286
|
+
"high",
|
|
287
|
+
"max"
|
|
288
|
+
]
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
"id": "deepseek-ai/DeepSeek-V4-Flash-0731",
|
|
292
|
+
"name": "DeepSeek V4 Flash 0731",
|
|
293
|
+
"thinking_levels": [
|
|
294
|
+
"off",
|
|
295
|
+
"high",
|
|
296
|
+
"max"
|
|
297
|
+
]
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
"id": "deepseek-ai/DeepSeek-V4.1-Flash-1017",
|
|
301
|
+
"name": "DeepSeek V4.1 Flash 1017",
|
|
302
|
+
"thinking_levels": [
|
|
303
|
+
"off",
|
|
304
|
+
"high",
|
|
305
|
+
"max"
|
|
306
|
+
]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"id": "deepseek-ai/DeepSeek-V4.1-Flash",
|
|
310
|
+
"name": "DeepSeek V4.1 Flash",
|
|
311
|
+
"thinking_levels": [
|
|
312
|
+
"off",
|
|
313
|
+
"high",
|
|
314
|
+
"max"
|
|
315
|
+
]
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
"id": "xai/grok-4.6",
|
|
319
|
+
"name": "Grok 4.6",
|
|
320
|
+
"thinking_levels": [
|
|
321
|
+
"low",
|
|
322
|
+
"medium",
|
|
323
|
+
"high",
|
|
324
|
+
"xhigh"
|
|
325
|
+
]
|
|
326
|
+
},
|
|
327
|
+
{
|
|
328
|
+
"id": "xai/grok-4.7",
|
|
329
|
+
"name": "Grok 4.7",
|
|
330
|
+
"thinking_levels": [
|
|
331
|
+
"low",
|
|
332
|
+
"medium",
|
|
333
|
+
"high",
|
|
334
|
+
"xhigh"
|
|
335
|
+
]
|
|
336
|
+
}
|
|
337
|
+
]
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Provider, ReasoningEffort, RoutingStrategy } from "@mupt-ai/dari-router/policy-engine";
|
|
2
2
|
import type { HarnessConversationIdentity } from "../runtime/harness_identity/index.js";
|
|
3
3
|
import type { InteractionRequest, SourceProtocol } from "../interaction/types.js";
|
|
4
|
-
import type { SubscriptionUsageLimit } from "../providers/subscription_limits.js";
|
|
4
|
+
import type { SubscriptionUsage, SubscriptionUsageLimit } from "../providers/subscription_limits.js";
|
|
5
5
|
import type { ClientHarness, ProviderCredential, ProviderKeySource, ResolvedRouter } from "../types.js";
|
|
6
6
|
/**
|
|
7
7
|
* Everything the gateway runtime needs from the deployment around it: router
|
|
@@ -42,6 +42,19 @@ export type GatewayPlatform = {
|
|
|
42
42
|
* another connected subscription for the same provider can serve a retry.
|
|
43
43
|
*/
|
|
44
44
|
markSubscriptionUnavailable(hold: SubscriptionHold): Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Optional. Record how much of a personal subscription's quota a successful
|
|
47
|
+
* response reports used, so the platform can move new conversations to
|
|
48
|
+
* plans with headroom before one runs out.
|
|
49
|
+
*/
|
|
50
|
+
recordSubscriptionUsage?(report: SubscriptionUsageReport): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Optional. After `markSubscriptionUnavailable` reports another plan can
|
|
53
|
+
* serve, the plan that serves this conversation next, so the gateway
|
|
54
|
+
* retries the request on it at once instead of failing it. Undefined when
|
|
55
|
+
* none can.
|
|
56
|
+
*/
|
|
57
|
+
nextSubscription?(request: NextSubscriptionRequest): Promise<NextSubscription | undefined>;
|
|
45
58
|
/**
|
|
46
59
|
* Account one call served on a platform-managed provider key. Only reached
|
|
47
60
|
* when `resolveRouter` reports a "managed" key source.
|
|
@@ -59,6 +72,7 @@ export type ResolveRouterRequest = {
|
|
|
59
72
|
conversationId?: string;
|
|
60
73
|
includePublicEvalCatalog?: boolean;
|
|
61
74
|
clientHarness?: ClientHarness;
|
|
75
|
+
provisional?: boolean;
|
|
62
76
|
};
|
|
63
77
|
export type ProviderCredentialRequest = {
|
|
64
78
|
provider: Provider;
|
|
@@ -101,6 +115,20 @@ export type SubscriptionHold = {
|
|
|
101
115
|
credentialId: string | undefined;
|
|
102
116
|
limit: SubscriptionUsageLimit;
|
|
103
117
|
};
|
|
118
|
+
export type NextSubscriptionRequest = {
|
|
119
|
+
userId: string;
|
|
120
|
+
provider: Provider;
|
|
121
|
+
conversationId: string;
|
|
122
|
+
};
|
|
123
|
+
export type NextSubscription = {
|
|
124
|
+
credentialId: string;
|
|
125
|
+
apiKey: string;
|
|
126
|
+
};
|
|
127
|
+
export type SubscriptionUsageReport = {
|
|
128
|
+
userId: string;
|
|
129
|
+
credentialId: string;
|
|
130
|
+
usage: SubscriptionUsage;
|
|
131
|
+
};
|
|
104
132
|
export type RequestRecord = {
|
|
105
133
|
row: Record<string, unknown>;
|
|
106
134
|
summary: RequestSummary;
|
|
@@ -169,7 +197,7 @@ export type RequestErrorContext = {
|
|
|
169
197
|
sourceProtocol: SourceProtocol | "openai_models" | "anthropic_models";
|
|
170
198
|
status: number;
|
|
171
199
|
};
|
|
172
|
-
export type OperationalErrorCode = "background_drain_timeout" | "billing_usage_report_failed" | "routing_report_assembly_failed" | "routing_report_barrier_failed" | "server_force_stop_failed" | "session_pin_write_failed" | "stream_report_finalization_failed" | "subscription_unavailable_ingest_failed";
|
|
200
|
+
export type OperationalErrorCode = "background_drain_timeout" | "billing_usage_report_failed" | "routing_report_assembly_failed" | "routing_report_barrier_failed" | "server_force_stop_failed" | "session_pin_write_failed" | "stream_report_finalization_failed" | "subscription_unavailable_ingest_failed" | "subscription_usage_record_failed" | "next_subscription_lookup_failed";
|
|
173
201
|
export type OperationalErrorContext = {
|
|
174
202
|
code: OperationalErrorCode;
|
|
175
203
|
requestId?: string;
|
|
@@ -12,7 +12,7 @@ import { runProviderAttemptPlan, } from "./provider_attempt_runner.js";
|
|
|
12
12
|
import { estimatedCancelUsage, NO_DELIVERED_OUTPUT, observeDeliveredOutput, } from "./cancel_estimate.js";
|
|
13
13
|
import { providerResponseCaptureFromError } from "./response_capture.js";
|
|
14
14
|
import { createStreamFinalizer } from "./stream_finalization.js";
|
|
15
|
-
import { detectSubscriptionUsageLimit, failureWithSubscriptionUsageLimit, retryAfterSeconds, subscriptionUsageLimitFromFailure, subscriptionSwitchedMessage, subscriptionUsageLimitMessage, } from "./subscription_limits.js";
|
|
15
|
+
import { detectSubscriptionUsageLimit, subscriptionUsageFromHeaders, failureWithSubscriptionUsageLimit, retryAfterSeconds, subscriptionUsageLimitFromFailure, subscriptionSwitchedMessage, subscriptionUsageLimitMessage, } from "./subscription_limits.js";
|
|
16
16
|
export async function executeNonStreaming(args) {
|
|
17
17
|
const result = await completeNonStreaming(args);
|
|
18
18
|
if (result.reportBarrierError !== undefined) {
|
|
@@ -132,6 +132,7 @@ async function completeWithRetries(args) {
|
|
|
132
132
|
clientSignal: args.clientSignal,
|
|
133
133
|
}),
|
|
134
134
|
recordFailedAttempt: ({ choice, attempt, result }) => recordSettledFailure(args.reporter, choice, attempt, result),
|
|
135
|
+
onSubscription: onNextSubscription,
|
|
135
136
|
});
|
|
136
137
|
}
|
|
137
138
|
async function runCompletionAttempt(args) {
|
|
@@ -149,6 +150,7 @@ async function runCompletionAttempt(args) {
|
|
|
149
150
|
if (isProviderFailureMessage(completion.message)) {
|
|
150
151
|
return providerFailedAttempt(completion.message, completion.providerLatencyMs, args.choice.execution, started.responseCapture);
|
|
151
152
|
}
|
|
153
|
+
noteSubscriptionUsage(args.choice.execution, started.responseCapture);
|
|
152
154
|
if (args.choice.execution.usageContext && hasUsage(completion.message)) {
|
|
153
155
|
await reportRoutingUsage({
|
|
154
156
|
...args.choice.execution.usageContext,
|
|
@@ -264,6 +266,7 @@ export async function prepareStreamingExecution(args) {
|
|
|
264
266
|
clientSignal: args.clientSignal,
|
|
265
267
|
}),
|
|
266
268
|
recordFailedAttempt: ({ choice, attempt, result }) => recordSettledFailure(args.reporter, choice, attempt, result),
|
|
269
|
+
onSubscription: onNextSubscription,
|
|
267
270
|
});
|
|
268
271
|
args.reporter.attachServingDecision(result.choice.decision);
|
|
269
272
|
if (result.kind === "customer_canceled") {
|
|
@@ -311,6 +314,7 @@ async function runStreamSetupAttempt(args) {
|
|
|
311
314
|
started.customerAbort.dispose();
|
|
312
315
|
return providerFailedAttempt(scrubProviderMessage(primed.failure, args.choice.execution.auth), Date.now() - startedAt, args.choice.execution, started.responseCapture);
|
|
313
316
|
}
|
|
317
|
+
noteSubscriptionUsage(args.choice.execution, started.responseCapture);
|
|
314
318
|
return {
|
|
315
319
|
kind: "succeeded",
|
|
316
320
|
value: {
|
|
@@ -428,11 +432,17 @@ async function providerFailedAttempt(message, latencyMs, execution, responseCapt
|
|
|
428
432
|
credentialId: execution.subscriptionCredentialId,
|
|
429
433
|
limit,
|
|
430
434
|
});
|
|
435
|
+
// Nothing reached the client yet, so the same request is retried on the
|
|
436
|
+
// next plan at once when the platform names it.
|
|
437
|
+
const nextSubscription = switched
|
|
438
|
+
? await nextSubscriptionFor(execution)
|
|
439
|
+
: undefined;
|
|
431
440
|
return {
|
|
432
441
|
kind: "provider_failed",
|
|
433
442
|
message: failureWithSubscriptionUsageLimit(message, { ...limit, switched }),
|
|
434
443
|
latencyMs,
|
|
435
444
|
retryable: false,
|
|
445
|
+
...(nextSubscription === undefined ? {} : { nextSubscription }),
|
|
436
446
|
};
|
|
437
447
|
}
|
|
438
448
|
// The provider call was in flight, so the prompt was paid for. A completion
|
|
@@ -474,6 +484,61 @@ function isPromptPolicyRejection(message) {
|
|
|
474
484
|
const text = providerFailureMessage(message).toLowerCase();
|
|
475
485
|
return text.includes("invalid prompt") && text.includes("usage policy");
|
|
476
486
|
}
|
|
487
|
+
// The plan that serves this conversation after its plan was held, or
|
|
488
|
+
// undefined when the platform names none. A failure leaves the request to
|
|
489
|
+
// fail as before.
|
|
490
|
+
async function nextSubscriptionFor(execution) {
|
|
491
|
+
const platform = gatewayPlatform();
|
|
492
|
+
if (platform.nextSubscription === undefined || execution.subscriptionUserId === undefined)
|
|
493
|
+
return undefined;
|
|
494
|
+
try {
|
|
495
|
+
return await platform.nextSubscription({
|
|
496
|
+
userId: execution.subscriptionUserId,
|
|
497
|
+
provider: execution.provider,
|
|
498
|
+
conversationId: execution.conversationId,
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
console.error("next subscription lookup failed", error);
|
|
503
|
+
captureOperationalError(error, { code: "next_subscription_lookup_failed" });
|
|
504
|
+
return undefined;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
// The same choice served by another of the user's plans.
|
|
508
|
+
function onNextSubscription(choice, next) {
|
|
509
|
+
return {
|
|
510
|
+
...choice,
|
|
511
|
+
execution: {
|
|
512
|
+
...choice.execution,
|
|
513
|
+
apiKey: next.apiKey,
|
|
514
|
+
auth: { type: "bearer", apiKey: next.apiKey },
|
|
515
|
+
subscriptionCredentialId: next.credentialId,
|
|
516
|
+
},
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
// A served subscription request reports the plan's remaining quota; record
|
|
520
|
+
// it without waiting, and never fail the request over it.
|
|
521
|
+
function noteSubscriptionUsage(execution, responseCapture) {
|
|
522
|
+
const platform = gatewayPlatform();
|
|
523
|
+
const headers = responseCapture?.succeeded;
|
|
524
|
+
if (platform.recordSubscriptionUsage === undefined ||
|
|
525
|
+
headers === undefined ||
|
|
526
|
+
execution.subscriptionUserId === undefined ||
|
|
527
|
+
execution.subscriptionCredentialId === undefined) {
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const usage = subscriptionUsageFromHeaders(execution.provider, execution.providerKeySource, headers);
|
|
531
|
+
if (usage === null)
|
|
532
|
+
return;
|
|
533
|
+
platform.recordSubscriptionUsage({
|
|
534
|
+
userId: execution.subscriptionUserId,
|
|
535
|
+
credentialId: execution.subscriptionCredentialId,
|
|
536
|
+
usage,
|
|
537
|
+
}).catch((error) => {
|
|
538
|
+
console.error("subscription usage record failed", error);
|
|
539
|
+
captureOperationalError(error, { code: "subscription_usage_record_failed" });
|
|
540
|
+
});
|
|
541
|
+
}
|
|
477
542
|
// Never fails the request: the provider already refused. Resolves whether
|
|
478
543
|
// another subscription now serves.
|
|
479
544
|
async function holdSubscription(hold) {
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import type { NextSubscription } from "../platform/platform.js";
|
|
1
2
|
export type ProviderAttemptFailure = {
|
|
2
3
|
kind: "provider_failed";
|
|
3
4
|
message: Record<string, unknown>;
|
|
4
5
|
latencyMs: number;
|
|
5
6
|
retryable: boolean;
|
|
7
|
+
nextSubscription?: NextSubscription;
|
|
6
8
|
} | {
|
|
7
9
|
kind: "customer_canceled";
|
|
8
10
|
message: Record<string, unknown>;
|
|
@@ -44,6 +46,7 @@ type RunProviderAttemptPlanArgs<TChoice, TValue, TInitial> = {
|
|
|
44
46
|
attempt: number;
|
|
45
47
|
result: ProviderAttemptFailure;
|
|
46
48
|
}) => void;
|
|
49
|
+
onSubscription?: (choice: TChoice, next: NextSubscription) => TChoice;
|
|
47
50
|
};
|
|
48
51
|
/**
|
|
49
52
|
* Owns the order-sensitive provider plan shared by completion and stream setup:
|
|
@@ -26,11 +26,15 @@ export async function runProviderAttemptPlan(args) {
|
|
|
26
26
|
}
|
|
27
27
|
return primary;
|
|
28
28
|
}
|
|
29
|
-
async function runChoice(args,
|
|
29
|
+
async function runChoice(args, firstChoice, initial) {
|
|
30
|
+
let choice = firstChoice;
|
|
30
31
|
let latencyMs = 0;
|
|
31
32
|
let lastFailure = null;
|
|
32
33
|
let lastAttempt = 0;
|
|
33
|
-
|
|
34
|
+
// A switch to another plan retries without spending the retry budget; each
|
|
35
|
+
// switch holds a plan, so switches are bounded by the plans connected.
|
|
36
|
+
let maxAttempts = args.retries + 1;
|
|
37
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
34
38
|
lastAttempt = attempt;
|
|
35
39
|
if (attempt !== 1 || initial === undefined) {
|
|
36
40
|
if (args.isCustomerCanceled(choice)) {
|
|
@@ -57,6 +61,11 @@ async function runChoice(args, choice, initial) {
|
|
|
57
61
|
failure: result.message,
|
|
58
62
|
};
|
|
59
63
|
}
|
|
64
|
+
if (result.kind === "provider_failed" && result.nextSubscription !== undefined && args.onSubscription) {
|
|
65
|
+
choice = args.onSubscription(choice, result.nextSubscription);
|
|
66
|
+
maxAttempts += 1;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
60
69
|
if (!result.retryable)
|
|
61
70
|
break;
|
|
62
71
|
}
|
|
@@ -7,9 +7,11 @@ export type ProviderFetch = (input: string | URL | Request, init?: RequestInit)
|
|
|
7
7
|
export type ProviderResponseCapture = {
|
|
8
8
|
fetch: ProviderFetch;
|
|
9
9
|
last: CapturedProviderResponse | undefined;
|
|
10
|
+
succeeded: Headers | undefined;
|
|
10
11
|
};
|
|
11
12
|
/**
|
|
12
|
-
* A fetch wrapper that remembers the last failed provider response
|
|
13
|
+
* A fetch wrapper that remembers the last failed provider response, and the
|
|
14
|
+
* headers of the last successful one. pi-ai
|
|
13
15
|
* flattens provider errors into one message string before they reach the
|
|
14
16
|
* runtime, so this is the only place the status, rate-limit headers, and
|
|
15
17
|
* structured error body of a rejected call can still be read.
|