@mupt-ai/dari-gateway 0.0.0-stage → 0.2.0

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 (133) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +135 -2
  3. package/dari-gateway.example.yaml +28 -0
  4. package/package.json +42 -4
  5. package/src/bun.ts +4 -0
  6. package/src/context/eval_catalog.ts +11 -0
  7. package/src/context/interaction_fingerprint.ts +691 -0
  8. package/src/context/interaction_fingerprint_format.ts +189 -0
  9. package/src/context/interaction_lease_depth.ts +150 -0
  10. package/src/context/lease_history.ts +200 -0
  11. package/src/context/pricing.ts +30 -0
  12. package/src/context/provider_prompt_accounting.ts +241 -0
  13. package/src/context/routing_context.ts +337 -0
  14. package/src/http/auth.ts +43 -0
  15. package/src/http/errors.ts +114 -0
  16. package/src/http/handler.ts +110 -0
  17. package/src/http/http2_server.ts +122 -0
  18. package/src/http/serve.ts +119 -0
  19. package/src/index.ts +22 -0
  20. package/src/ingress/anthropic_messages.ts +162 -0
  21. package/src/ingress/anthropic_models.ts +28 -0
  22. package/src/ingress/chat_completions.ts +49 -0
  23. package/src/ingress/claude_code/request_context.ts +36 -0
  24. package/src/ingress/codex.ts +81 -0
  25. package/src/ingress/codex_models.ts +113 -0
  26. package/src/ingress/json.ts +113 -0
  27. package/src/ingress/models.ts +55 -0
  28. package/src/interaction/capabilities.ts +30 -0
  29. package/src/interaction/image.ts +12 -0
  30. package/src/interaction/portable_reasoning_state.ts +25 -0
  31. package/src/interaction/provider_continuation_state.ts +36 -0
  32. package/src/interaction/tool_identity.ts +65 -0
  33. package/src/interaction/types.ts +282 -0
  34. package/src/models/context_limits.ts +8 -0
  35. package/src/models/execution_provider.ts +22 -0
  36. package/src/models/gateways.ts +73 -0
  37. package/src/models/ids.ts +26 -0
  38. package/src/models/provider_defaults.ts +47 -0
  39. package/src/models/runtime_overrides.ts +221 -0
  40. package/src/observability/errors.ts +25 -0
  41. package/src/platform/platform.ts +204 -0
  42. package/src/platform/routing_contract.ts +6 -0
  43. package/src/protocol/input/anthropic_messages.ts +531 -0
  44. package/src/protocol/input/chat_completions.ts +255 -0
  45. package/src/protocol/input/chat_completions_validation.ts +293 -0
  46. package/src/protocol/input/openai_responses.ts +749 -0
  47. package/src/protocol/input/router_model.ts +53 -0
  48. package/src/protocol/output/anthropic_messages.ts +32 -0
  49. package/src/protocol/output/anthropic_messages_output.ts +129 -0
  50. package/src/protocol/output/anthropic_messages_stream.ts +281 -0
  51. package/src/protocol/output/chat_completions.ts +451 -0
  52. package/src/protocol/output/chat_reasoning_detail.ts +19 -0
  53. package/src/protocol/output/delivered_stream_snapshot.ts +31 -0
  54. package/src/protocol/output/openai_responses.ts +30 -0
  55. package/src/protocol/output/openai_responses_output.ts +159 -0
  56. package/src/protocol/output/openai_responses_stream.ts +631 -0
  57. package/src/protocol/output/sse_stream.ts +102 -0
  58. package/src/providers/anthropic_safeguards.ts +115 -0
  59. package/src/providers/aws_assume_role.ts +147 -0
  60. package/src/providers/cancel_estimate.ts +140 -0
  61. package/src/providers/execution.ts +800 -0
  62. package/src/providers/model_capabilities.ts +50 -0
  63. package/src/providers/pi.ts +499 -0
  64. package/src/providers/pi_context.ts +466 -0
  65. package/src/providers/pi_options.ts +416 -0
  66. package/src/providers/pi_output.ts +500 -0
  67. package/src/providers/provider_attempt_runner.ts +153 -0
  68. package/src/providers/provider_call.ts +433 -0
  69. package/src/providers/provider_reported_cost.ts +138 -0
  70. package/src/providers/reasoning.ts +59 -0
  71. package/src/providers/response_capture.ts +65 -0
  72. package/src/providers/stream_finalization.ts +264 -0
  73. package/src/providers/subscription_limits.ts +208 -0
  74. package/src/providers/subscription_usage.ts +99 -0
  75. package/src/runtime/claude_code/auxiliary_routing.ts +80 -0
  76. package/src/runtime/customer_disconnect.ts +66 -0
  77. package/src/runtime/explicit_model.ts +86 -0
  78. package/src/runtime/harness_identity/conversation.ts +87 -0
  79. package/src/runtime/harness_identity/extract.ts +323 -0
  80. package/src/runtime/harness_identity/index.ts +14 -0
  81. package/src/runtime/prepare_routed_completion.ts +224 -0
  82. package/src/runtime/provider_stream_lifecycle.ts +105 -0
  83. package/src/runtime/provider_stream_outcome.ts +83 -0
  84. package/src/runtime/request_tags.ts +34 -0
  85. package/src/runtime/routed_completion.ts +102 -0
  86. package/src/runtime/routed_protocol.ts +33 -0
  87. package/src/runtime/routed_runtime.ts +722 -0
  88. package/src/runtime/routing_lease.ts +273 -0
  89. package/src/runtime/runtime_reporter.ts +115 -0
  90. package/src/self_host/config.ts +173 -0
  91. package/src/self_host/main.ts +104 -0
  92. package/src/self_host/platform.ts +63 -0
  93. package/src/self_host/prefix_store.ts +315 -0
  94. package/src/self_host/resolve.ts +240 -0
  95. package/src/self_host/subscriptions/access.ts +43 -0
  96. package/src/self_host/subscriptions/http.ts +204 -0
  97. package/src/self_host/subscriptions/index.ts +43 -0
  98. package/src/self_host/subscriptions/oauth.ts +395 -0
  99. package/src/self_host/subscriptions/store.ts +339 -0
  100. package/src/strategies/auto_routing_selector.ts +72 -0
  101. package/src/strategies/eval_relevance.ts +57 -0
  102. package/src/strategies/fallback_decision.ts +30 -0
  103. package/src/strategies/priority_selector.ts +40 -0
  104. package/src/strategies/route_decision.ts +106 -0
  105. package/src/strategies/routing.ts +455 -0
  106. package/src/strategies/selector_projection.ts +207 -0
  107. package/src/telemetry/background.ts +40 -0
  108. package/src/telemetry/benchmark_replay.ts +160 -0
  109. package/src/telemetry/billing_finalization.ts +167 -0
  110. package/src/telemetry/ingest_values.ts +15 -0
  111. package/src/telemetry/model_cost.ts +14 -0
  112. package/src/telemetry/prefix_state.ts +183 -0
  113. package/src/telemetry/report.ts +832 -0
  114. package/src/telemetry/requests/interaction_observability.ts +258 -0
  115. package/src/telemetry/requests/retained_interaction_request.ts +20 -0
  116. package/src/telemetry/scrub.ts +38 -0
  117. package/src/telemetry/skills/claude/catalog.ts +119 -0
  118. package/src/telemetry/skills/claude/file_reads.ts +153 -0
  119. package/src/telemetry/skills/claude/observations.ts +450 -0
  120. package/src/telemetry/skills/codex/catalog.ts +171 -0
  121. package/src/telemetry/skills/codex/exec_file_reads.ts +55 -0
  122. package/src/telemetry/skills/codex/explicit.ts +116 -0
  123. package/src/telemetry/skills/codex/file_reads.ts +51 -0
  124. package/src/telemetry/skills/codex/identity.ts +21 -0
  125. package/src/telemetry/skills/observations.ts +94 -0
  126. package/src/telemetry/skills/paths.ts +82 -0
  127. package/src/telemetry/skills/pi.ts +276 -0
  128. package/src/telemetry/skills/shell_file_reads.ts +267 -0
  129. package/src/telemetry/skills/shell_words.ts +202 -0
  130. package/src/telemetry/tools/input.ts +16 -0
  131. package/src/telemetry/tools/observations.ts +349 -0
  132. package/src/telemetry/usage.ts +173 -0
  133. package/src/types.ts +150 -0
@@ -0,0 +1,339 @@
1
+ /**
2
+ * SQLite storage for personal subscription credentials and pending OAuth flows.
3
+ *
4
+ * Tokens are stored as PLAINTEXT in the gateway's own database file. Anyone
5
+ * who can read that file can use the connected subscriptions until the
6
+ * provider revokes them, so keep the file on storage only the gateway's
7
+ * operator can read, and treat backups of it as secrets.
8
+ *
9
+ * Connection order (`position`) is the order routing switches through: the
10
+ * active credential for a provider is the first one, in that order, that is
11
+ * not held. A hold ends on its own once `unavailable_until` passes.
12
+ */
13
+ import type { Database } from "bun:sqlite";
14
+ import { randomBytes, randomUUID } from "node:crypto";
15
+
16
+ import type { OAuthTokens, SubscriptionProvider } from "./oauth";
17
+
18
+ export type SubscriptionCredential = {
19
+ id: string;
20
+ provider: SubscriptionProvider;
21
+ // Stable env-var-style handle the CLI addresses a subscription by.
22
+ name: string;
23
+ // The user's own name for the subscription.
24
+ label: string | null;
25
+ accountEmail: string | null;
26
+ accountId: string | null;
27
+ accessToken: string;
28
+ refreshToken: string;
29
+ expiresAt: Date;
30
+ unavailableUntil: Date | null;
31
+ unavailableReason: string | null;
32
+ position: number;
33
+ createdAt: Date;
34
+ updatedAt: Date;
35
+ lastUsedAt: Date | null;
36
+ };
37
+
38
+ export type PendingOAuthSession = {
39
+ token: string;
40
+ provider: SubscriptionProvider;
41
+ flow: Record<string, string>;
42
+ // The subscription this flow reconnects; null connects another one.
43
+ credentialName: string | null;
44
+ expiresAt: Date;
45
+ };
46
+
47
+ const BASE_NAMES: Record<SubscriptionProvider, string> = {
48
+ anthropic_claude_code: "ANTHROPIC_CLAUDE_CODE_OAUTH_TOKEN",
49
+ openai_codex: "OPENAI_CODEX_OAUTH_TOKEN",
50
+ };
51
+
52
+ type CredentialRow = {
53
+ id: string;
54
+ provider: SubscriptionProvider;
55
+ name: string;
56
+ label: string | null;
57
+ account_email: string | null;
58
+ account_id: string | null;
59
+ access_token: string;
60
+ refresh_token: string;
61
+ expires_at: number;
62
+ unavailable_until: number | null;
63
+ unavailable_reason: string | null;
64
+ position: number;
65
+ created_at: number;
66
+ updated_at: number;
67
+ last_used_at: number | null;
68
+ };
69
+
70
+ type SessionRow = {
71
+ token: string;
72
+ provider: SubscriptionProvider;
73
+ flow_json: string;
74
+ credential_name: string | null;
75
+ expires_at: number;
76
+ };
77
+
78
+ export class SubscriptionStore {
79
+ constructor(private readonly db: Database) {
80
+ db.run(`CREATE TABLE IF NOT EXISTS subscription_credentials (
81
+ id TEXT PRIMARY KEY,
82
+ provider TEXT NOT NULL,
83
+ name TEXT NOT NULL UNIQUE,
84
+ label TEXT,
85
+ account_email TEXT,
86
+ account_id TEXT,
87
+ access_token TEXT NOT NULL,
88
+ refresh_token TEXT NOT NULL,
89
+ expires_at INTEGER NOT NULL,
90
+ unavailable_until INTEGER,
91
+ unavailable_reason TEXT,
92
+ position INTEGER NOT NULL,
93
+ created_at INTEGER NOT NULL,
94
+ updated_at INTEGER NOT NULL,
95
+ last_used_at INTEGER
96
+ )`);
97
+ db.run(`CREATE TABLE IF NOT EXISTS subscription_oauth_sessions (
98
+ token TEXT PRIMARY KEY,
99
+ provider TEXT NOT NULL,
100
+ flow_json TEXT NOT NULL,
101
+ credential_name TEXT,
102
+ expires_at INTEGER NOT NULL
103
+ )`);
104
+ }
105
+
106
+ /** Every subscription, grouped by provider, in connection order. */
107
+ list(provider?: SubscriptionProvider): SubscriptionCredential[] {
108
+ const rows = provider === undefined
109
+ ? this.db.query<CredentialRow, []>("SELECT * FROM subscription_credentials ORDER BY provider, position").all()
110
+ : this.db
111
+ .query<CredentialRow, [string]>("SELECT * FROM subscription_credentials WHERE provider = ? ORDER BY position")
112
+ .all(provider);
113
+ return rows.map(credentialFromRow);
114
+ }
115
+
116
+ get(id: string): SubscriptionCredential | null {
117
+ const row = this.db.query<CredentialRow, [string]>("SELECT * FROM subscription_credentials WHERE id = ?").get(id);
118
+ return row === null ? null : credentialFromRow(row);
119
+ }
120
+
121
+ getByName(name: string): SubscriptionCredential | null {
122
+ const row = this.db
123
+ .query<CredentialRow, [string]>("SELECT * FROM subscription_credentials WHERE name = ?")
124
+ .get(normalizeName(name));
125
+ return row === null ? null : credentialFromRow(row);
126
+ }
127
+
128
+ /** The credential routing uses now, or null when none is connected or every one is held. */
129
+ activeCredential(provider: SubscriptionProvider, now = new Date()): SubscriptionCredential | null {
130
+ // Stay on the most recently used subscription while it serves, so its
131
+ // prompt cache stays warm; once it is held, move on in connection order.
132
+ const credentials = this.list(provider);
133
+ let start = 0;
134
+ credentials.forEach((credential, index) => {
135
+ const lastUsed = credential.lastUsedAt?.getTime() ?? -1;
136
+ if (lastUsed > (credentials[start]!.lastUsedAt?.getTime() ?? -1)) start = index;
137
+ });
138
+ for (let offset = 0; offset < credentials.length; offset += 1) {
139
+ const credential = credentials[(start + offset) % credentials.length]!;
140
+ if (!isHeld(credential, now)) return credential;
141
+ }
142
+ return null;
143
+ }
144
+
145
+ /**
146
+ * Store tokens from a completed OAuth flow. `reconnectName` replaces that
147
+ * subscription (null when it is not connected). Otherwise a subscription
148
+ * for the same account email is replaced, and anything else is connected
149
+ * last in order under the first free name.
150
+ */
151
+ connect(tokens: OAuthTokens, reconnectName: string | null, now = new Date()): SubscriptionCredential | null {
152
+ return this.db.transaction(() => {
153
+ const existing = reconnectName !== null
154
+ ? this.getByName(reconnectName)
155
+ : this.list(tokens.provider).find(
156
+ (credential) => tokens.accountEmail !== null && credential.accountEmail === tokens.accountEmail,
157
+ ) ?? null;
158
+ if (reconnectName !== null && existing?.provider !== tokens.provider) return null;
159
+
160
+ if (existing !== null) {
161
+ // Another (or an unidentified) account must not inherit the previous account's hold.
162
+ const sameAccount = tokens.accountEmail !== null && tokens.accountEmail === existing.accountEmail;
163
+ this.db.run(
164
+ `UPDATE subscription_credentials SET access_token = ?, refresh_token = ?, expires_at = ?,
165
+ account_id = ?, account_email = ?, unavailable_until = ?, unavailable_reason = ?, updated_at = ?
166
+ WHERE id = ?`,
167
+ [
168
+ tokens.accessToken,
169
+ tokens.refreshToken,
170
+ tokens.expiresAt.getTime(),
171
+ tokens.accountId,
172
+ tokens.accountEmail,
173
+ sameAccount ? existing.unavailableUntil?.getTime() ?? null : null,
174
+ sameAccount ? existing.unavailableReason : null,
175
+ now.getTime(),
176
+ existing.id,
177
+ ],
178
+ );
179
+ return this.get(existing.id);
180
+ }
181
+
182
+ const id = `cred_${randomUUID().replaceAll("-", "")}`;
183
+ const position = this.db
184
+ .query<{ next: number }, []>("SELECT COALESCE(MAX(position), 0) + 1 AS next FROM subscription_credentials")
185
+ .get()!.next;
186
+ this.db.run(
187
+ `INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, access_token,
188
+ refresh_token, expires_at, position, created_at, updated_at)
189
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
190
+ [
191
+ id,
192
+ tokens.provider,
193
+ this.freeName(BASE_NAMES[tokens.provider]),
194
+ tokens.accountEmail,
195
+ tokens.accountId,
196
+ tokens.accessToken,
197
+ tokens.refreshToken,
198
+ tokens.expiresAt.getTime(),
199
+ position,
200
+ now.getTime(),
201
+ now.getTime(),
202
+ ],
203
+ );
204
+ return this.get(id);
205
+ })();
206
+ }
207
+
208
+ /** Persist refreshed tokens. Keeps the stored account id when the refresh carries none. */
209
+ updateTokens(id: string, tokens: OAuthTokens, now = new Date()): void {
210
+ this.db.run(
211
+ `UPDATE subscription_credentials SET access_token = ?, refresh_token = ?, expires_at = ?,
212
+ account_id = COALESCE(?, account_id), updated_at = ? WHERE id = ?`,
213
+ [tokens.accessToken, tokens.refreshToken, tokens.expiresAt.getTime(), tokens.accountId, now.getTime(), id],
214
+ );
215
+ }
216
+
217
+ touch(id: string, now = new Date()): void {
218
+ this.db.run("UPDATE subscription_credentials SET last_used_at = ? WHERE id = ?", [now.getTime(), id]);
219
+ }
220
+
221
+ /** An empty label clears it. Null when the subscription is not connected. */
222
+ rename(name: string, label: string | null, now = new Date()): SubscriptionCredential | null {
223
+ const normalized = label?.trim() || null;
224
+ if (normalized !== null && normalized.length > 120) {
225
+ throw new RangeError("Subscription name must be at most 120 characters.");
226
+ }
227
+ const credential = this.getByName(name);
228
+ if (credential === null) return null;
229
+ this.db.run("UPDATE subscription_credentials SET label = ?, updated_at = ? WHERE id = ?", [
230
+ normalized,
231
+ now.getTime(),
232
+ credential.id,
233
+ ]);
234
+ return this.get(credential.id);
235
+ }
236
+
237
+ /** Remove a subscription. Returns what was removed, or null when it was not connected. */
238
+ disconnect(name: string): SubscriptionCredential | null {
239
+ const credential = this.getByName(name);
240
+ if (credential === null) return null;
241
+ this.db.run("DELETE FROM subscription_credentials WHERE id = ?", [credential.id]);
242
+ return credential;
243
+ }
244
+
245
+ /**
246
+ * Hold an exhausted subscription until its reset. A hold only grows: a later
247
+ * report with an earlier reset never shortens a longer one. Returns whether
248
+ * a subscription for the same provider is available now.
249
+ */
250
+ markUnavailable(id: string, until: Date, reason: string, now = new Date()): boolean {
251
+ const credential = this.get(id);
252
+ if (credential === null) return false;
253
+ if (!isHeld(credential, now) || until > credential.unavailableUntil!) {
254
+ this.db.run(
255
+ "UPDATE subscription_credentials SET unavailable_until = ?, unavailable_reason = ?, updated_at = ? WHERE id = ?",
256
+ [until.getTime(), reason, now.getTime(), id],
257
+ );
258
+ }
259
+ return this.activeCredential(credential.provider, now) !== null;
260
+ }
261
+
262
+ /** Clear a usage-limit hold. Null when the subscription is not connected. */
263
+ markAvailable(name: string, now = new Date()): SubscriptionCredential | null {
264
+ const credential = this.getByName(name);
265
+ if (credential === null) return null;
266
+ this.db.run(
267
+ "UPDATE subscription_credentials SET unavailable_until = NULL, unavailable_reason = NULL, updated_at = ? WHERE id = ?",
268
+ [now.getTime(), credential.id],
269
+ );
270
+ return this.get(credential.id);
271
+ }
272
+
273
+ createSession(
274
+ session: Omit<PendingOAuthSession, "token">,
275
+ now = new Date(),
276
+ ): PendingOAuthSession {
277
+ const token = randomBytes(32).toString("base64url");
278
+ this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
279
+ this.db.run(
280
+ "INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, expires_at) VALUES (?, ?, ?, ?, ?)",
281
+ [token, session.provider, JSON.stringify(session.flow), session.credentialName, session.expiresAt.getTime()],
282
+ );
283
+ return { token, ...session };
284
+ }
285
+
286
+ /** A pending session, including an expired one so callers can say it expired. */
287
+ getSession(token: string): PendingOAuthSession | null {
288
+ const row = this.db
289
+ .query<SessionRow, [string]>("SELECT * FROM subscription_oauth_sessions WHERE token = ?")
290
+ .get(token);
291
+ if (row === null) return null;
292
+ return {
293
+ token: row.token,
294
+ provider: row.provider,
295
+ flow: JSON.parse(row.flow_json) as Record<string, string>,
296
+ credentialName: row.credential_name,
297
+ expiresAt: new Date(row.expires_at),
298
+ };
299
+ }
300
+
301
+ deleteSession(token: string): void {
302
+ this.db.run("DELETE FROM subscription_oauth_sessions WHERE token = ?", [token]);
303
+ }
304
+
305
+ private freeName(base: string): string {
306
+ let name = base;
307
+ for (let index = 2; this.getByName(name) !== null; index += 1) name = `${base}_${index}`;
308
+ return name;
309
+ }
310
+ }
311
+
312
+ export function isHeld(credential: SubscriptionCredential, now: Date): boolean {
313
+ return credential.unavailableUntil !== null && credential.unavailableUntil > now;
314
+ }
315
+
316
+ /** Subscription names are env-var-style identifiers; matching is case-insensitive. */
317
+ export function normalizeName(name: string): string {
318
+ return name.trim().toUpperCase();
319
+ }
320
+
321
+ function credentialFromRow(row: CredentialRow): SubscriptionCredential {
322
+ return {
323
+ id: row.id,
324
+ provider: row.provider,
325
+ name: row.name,
326
+ label: row.label,
327
+ accountEmail: row.account_email,
328
+ accountId: row.account_id,
329
+ accessToken: row.access_token,
330
+ refreshToken: row.refresh_token,
331
+ expiresAt: new Date(row.expires_at),
332
+ unavailableUntil: row.unavailable_until === null ? null : new Date(row.unavailable_until),
333
+ unavailableReason: row.unavailable_reason,
334
+ position: row.position,
335
+ createdAt: new Date(row.created_at),
336
+ updatedAt: new Date(row.updated_at),
337
+ lastUsedAt: row.last_used_at === null ? null : new Date(row.last_used_at),
338
+ };
339
+ }
@@ -0,0 +1,72 @@
1
+ import { parseSelectorDecision } from "@mupt-ai/dari-router/policy-engine";
2
+ import { HttpError } from "../http/errors";
3
+ import type { SelectorBackend } from "./routing";
4
+
5
+ export const DARI_AUTO_ROUTING_ENDPOINT = "https://routing.dari.dev/v1/auto-router";
6
+ // Same selector identity and input budget as `createAutoRouter` in
7
+ // @mupt-ai/dari-router, which calls the same API.
8
+ const AUTO_ROUTER_MODEL = "dari/auto-router";
9
+ const AUTO_ROUTER_CONTEXT_WINDOW_CHARS = 32_000;
10
+ const AUTO_ROUTER_TIMEOUT_MS = 30_000;
11
+
12
+ /**
13
+ * Choose among a router's models with Dari Auto Routing, the hosted routing
14
+ * policy. Each selection is one call to the Auto Router API with a Dari API
15
+ * key; the gateway still executes the chosen model with its own credentials.
16
+ */
17
+ export function createAutoRoutingSelectorBackend(config: {
18
+ apiKey: string;
19
+ endpoint?: string;
20
+ fetch?: typeof fetch;
21
+ }): SelectorBackend {
22
+ const apiKey = config.apiKey.trim();
23
+ if (!apiKey) {
24
+ throw new Error("Dari Auto Routing needs an API key.");
25
+ }
26
+ const endpoint = (config.endpoint ?? DARI_AUTO_ROUTING_ENDPOINT)
27
+ .replace(/\/+$/, "")
28
+ .replace(/\/select$/, "");
29
+ const fetchImpl = config.fetch ?? fetch;
30
+ return {
31
+ selectorModel: AUTO_ROUTER_MODEL,
32
+ contextWindowChars: async () => AUTO_ROUTER_CONTEXT_WINDOW_CHARS,
33
+ select: async (args) => {
34
+ let response: Response;
35
+ try {
36
+ response = await fetchImpl(`${endpoint}/select`, {
37
+ method: "POST",
38
+ headers: {
39
+ Authorization: `Bearer ${apiKey}`,
40
+ "Content-Type": "application/json",
41
+ },
42
+ body: JSON.stringify(args.selectorRequest),
43
+ signal: AbortSignal.timeout(AUTO_ROUTER_TIMEOUT_MS),
44
+ });
45
+ } catch {
46
+ throw new HttpError(502, "Dari Auto Routing request failed.", "selector_execution_failed");
47
+ }
48
+ if (!response.ok) {
49
+ throw new HttpError(
50
+ 502,
51
+ `Dari Auto Routing returned HTTP ${response.status}.`,
52
+ "selector_execution_failed",
53
+ );
54
+ }
55
+ const outputText = await response.text();
56
+ // The gateway applies its own stateless hold; drop any lease the
57
+ // policy stamps so it is not mistaken for a negotiated one.
58
+ const { leaseTurnsRemaining: _leaseTurnsRemaining, ...decision } =
59
+ parseSelectorDecision(outputText).decision;
60
+ return {
61
+ decision,
62
+ selectorModel: args.selectorModel,
63
+ candidates: args.candidates,
64
+ selectorInput: args.selectorInput,
65
+ selectorRequest: args.selectorRequest,
66
+ outputText,
67
+ usage: null,
68
+ latencyMs: Date.now() - args.startedAt,
69
+ };
70
+ },
71
+ };
72
+ }
@@ -0,0 +1,57 @@
1
+ import { RouterFrameworkError } from "@mupt-ai/dari-router";
2
+ import {
3
+ JevOutputError,
4
+ selectRelevantEvals,
5
+ type ChatMessage,
6
+ type JevEvalRelevanceSelection,
7
+ type ResolvedJevConfig,
8
+ type RouterEval,
9
+ } from "@mupt-ai/dari-router/policy-engine";
10
+
11
+ import { publicCatalogSelectorEvals } from "../context/eval_catalog";
12
+ import { scrubSecrets } from "../telemetry/scrub";
13
+ import type { ResolvedRouter } from "../types";
14
+
15
+ export type EvalRelevanceDetails =
16
+ | {
17
+ model: string;
18
+ cards: JevEvalRelevanceSelection["cards"];
19
+ usage: JevEvalRelevanceSelection["usage"];
20
+ }
21
+ | { error: "eval_relevance_failed" };
22
+
23
+ // The candidate pool is every eval the organization can use: its own cards
24
+ // plus the public catalog. Jev keeps the cards relevant to this request and
25
+ // those become the selector's evidence. A Jev outage degrades to the
26
+ // organization's own cards rather than failing the request.
27
+ export async function relevantEvals(args: {
28
+ credentials: ResolvedRouter;
29
+ selectorMessages: ChatMessage[];
30
+ config: ResolvedJevConfig;
31
+ }): Promise<{ evals: RouterEval[]; details: EvalRelevanceDetails }> {
32
+ const organizationEvals = args.credentials.evals ?? [];
33
+ const pool = [
34
+ ...organizationEvals,
35
+ ...publicCatalogSelectorEvals(args.credentials.public_eval_catalog),
36
+ ];
37
+ try {
38
+ const selection = await selectRelevantEvals(
39
+ pool,
40
+ {
41
+ task: args.selectorMessages.find((message) => message.role === "user") ?? null,
42
+ conversation: args.selectorMessages,
43
+ },
44
+ args.config,
45
+ );
46
+ return {
47
+ evals: selection.evals,
48
+ details: { model: selection.model, cards: selection.cards, usage: selection.usage },
49
+ };
50
+ } catch (error) {
51
+ if (!(error instanceof RouterFrameworkError || error instanceof JevOutputError)) {
52
+ throw error;
53
+ }
54
+ console.error("eval relevance failed", scrubSecrets(error.message));
55
+ return { evals: organizationEvals, details: { error: "eval_relevance_failed" } };
56
+ }
57
+ }
@@ -0,0 +1,30 @@
1
+ import {
2
+ providerForModel,
3
+ type RoutingCandidate,
4
+ type RoutingDecision,
5
+ } from "@mupt-ai/dari-router/policy-engine";
6
+
7
+ export function deriveFallbackDecision(args: {
8
+ candidates: RoutingCandidate[];
9
+ selected: RoutingCandidate;
10
+ enabled?: boolean;
11
+ requiresDifferentProvider?: boolean;
12
+ providerForModel?: (model: string) => string;
13
+ }): RoutingDecision | undefined {
14
+ if (!args.enabled) return undefined;
15
+ const resolveProvider = args.providerForModel ?? providerForModel;
16
+ const selectedProvider = resolveProvider(args.selected.model);
17
+ const fallback = args.candidates.find(
18
+ (candidate) =>
19
+ candidate.model !== args.selected.model &&
20
+ (!args.requiresDifferentProvider ||
21
+ resolveProvider(candidate.model) !== selectedProvider),
22
+ );
23
+ return fallback
24
+ ? {
25
+ selectedModel: fallback.model,
26
+ reasoningEffort: fallback.reasoningEffort,
27
+ reason: "Runtime-selected fallback for the routed primary decision.",
28
+ }
29
+ : undefined;
30
+ }
@@ -0,0 +1,40 @@
1
+ import type { SelectorBackend } from "./routing";
2
+
3
+ const PRIORITY_SELECTOR_MODEL = "dari-gateway/priority";
4
+ // Priority selection reads no messages, so the sized selector input only
5
+ // needs room for the router's candidates.
6
+ const PRIORITY_CONTEXT_WINDOW_CHARS = 1_000_000;
7
+
8
+ /**
9
+ * Serve the router's first eligible model/thinking-level pair, in the order
10
+ * the router lists them. Provider failures still fall back to the next pair
11
+ * when the router enables model fallback.
12
+ */
13
+ export function createPrioritySelectorBackend(): SelectorBackend {
14
+ return {
15
+ selectorModel: PRIORITY_SELECTOR_MODEL,
16
+ contextWindowChars: async () => PRIORITY_CONTEXT_WINDOW_CHARS,
17
+ prepareMessages: () => [],
18
+ supportsSpeculativeRouting: false,
19
+ select: async (args) => {
20
+ const first = args.candidates[0];
21
+ if (first === undefined) {
22
+ throw new Error("Priority selection needs at least one candidate.");
23
+ }
24
+ return {
25
+ decision: {
26
+ selectedModel: first.model,
27
+ reasoningEffort: first.reasoningEffort,
28
+ reason: "First eligible model on the router.",
29
+ },
30
+ selectorModel: args.selectorModel,
31
+ candidates: args.candidates,
32
+ selectorInput: args.selectorInput,
33
+ selectorRequest: args.selectorRequest,
34
+ outputText: "",
35
+ usage: null,
36
+ latencyMs: Date.now() - args.startedAt,
37
+ };
38
+ },
39
+ };
40
+ }
@@ -0,0 +1,106 @@
1
+ import type {
2
+ ChatMessage,
3
+ PreviousDecision,
4
+ RoutingDecision,
5
+ StrategyCandidateResolution,
6
+ SelectorLeaseHistoryEntry,
7
+ } from "@mupt-ai/dari-router/policy-engine";
8
+ import { publicCatalogSelectorEvals } from "../context/eval_catalog";
9
+ import { HttpError } from "../http/errors";
10
+ import { relevantEvals } from "./eval_relevance";
11
+ import { executionProviderForModel } from "../models/execution_provider";
12
+ import { selectModel, type SelectorBackend, type SelectorRun } from "./routing";
13
+ import type { SelectorImage } from "./selector_projection";
14
+ import type { ResolvedRouter } from "../types";
15
+
16
+ export type RoutedDecision = {
17
+ decision: RoutingDecision;
18
+ fallbackDecision?: RoutingDecision;
19
+ run?: SelectorRun;
20
+ details?: Record<string, unknown>;
21
+ };
22
+
23
+ type RouteDecisionArgs = {
24
+ selectorMessages: ChatMessage[];
25
+ selectorImages?: SelectorImage[];
26
+ resolution: StrategyCandidateResolution;
27
+ credentials: ResolvedRouter;
28
+ previousDecision?: PreviousDecision;
29
+ conversationId: string;
30
+ selectorBackend: SelectorBackend;
31
+ leaseHistory?: SelectorLeaseHistoryEntry[];
32
+ };
33
+
34
+ export async function routeDecision(
35
+ args: RouteDecisionArgs,
36
+ ): Promise<RoutedDecision> {
37
+ const { credentials, resolution } = args;
38
+ const custom = resolution.strategy === "custom"
39
+ ? resolution.custom
40
+ : undefined;
41
+ if (custom === null) {
42
+ throw new HttpError(
43
+ 502,
44
+ "Router custom rule configuration is missing.",
45
+ "custom_config_missing",
46
+ );
47
+ }
48
+ if (custom && resolution.candidates.length === 0) {
49
+ const error = new HttpError(
50
+ 502,
51
+ "No custom routing rule or default model/thinking-level pair is routable for this request.",
52
+ "custom_no_routable_candidates",
53
+ ) as HttpError & { decisionDetails?: Record<string, unknown> };
54
+ error.decisionDetails = { custom_rules: custom.audit };
55
+ throw error;
56
+ }
57
+
58
+ const relevance = args.selectorBackend.evalRelevance !== undefined
59
+ && resolution.candidates.length > 1
60
+ ? await relevantEvals({
61
+ credentials,
62
+ selectorMessages: args.selectorMessages,
63
+ config: args.selectorBackend.evalRelevance,
64
+ })
65
+ : undefined;
66
+ const run = await selectModel({
67
+ messages: args.selectorMessages,
68
+ ...(args.selectorImages !== undefined && args.selectorImages.length > 0
69
+ ? { images: args.selectorImages }
70
+ : {}),
71
+ candidates: resolution.candidates,
72
+ allCandidates: resolution.allCandidates,
73
+ evals: relevance?.evals ?? credentials.evals ?? [],
74
+ imputationReferenceEvals: credentials.eval_score_imputation === true
75
+ ? publicCatalogSelectorEvals(credentials.public_eval_catalog)
76
+ : undefined,
77
+ previousDecision: args.previousDecision,
78
+ costEstimates: resolution.costEstimates,
79
+ sessionId: args.conversationId,
80
+ ...(custom
81
+ ? { customRules: custom.rules, defaultTarget: custom.defaultTarget }
82
+ : {}),
83
+ selectorBackend: args.selectorBackend,
84
+ modelFallbackEnabled: credentials.model_fallback_enabled !== false,
85
+ fallbackRequiresDifferentProvider:
86
+ credentials.fallback_requires_different_provider !== false,
87
+ executionProviderForModel: (model) =>
88
+ executionProviderForModel(credentials, model),
89
+ imputeEvalScores: credentials.eval_score_imputation === true,
90
+ leaseHistory: args.leaseHistory,
91
+ leaseContextEnabled: credentials.lease_context_enabled !== false,
92
+ });
93
+ const details = {
94
+ ...(custom ? { custom_rules: custom.audit } : {}),
95
+ ...(run.usage?.policy_bypassed === "single_candidate"
96
+ ? { selector_policy_bypass: "single_candidate" }
97
+ : {}),
98
+ ...(relevance ? { eval_relevance: relevance.details } : {}),
99
+ };
100
+ return {
101
+ decision: run.decision,
102
+ fallbackDecision: run.fallbackDecision,
103
+ run,
104
+ ...(Object.keys(details).length > 0 ? { details } : {}),
105
+ };
106
+ }