@mupt-ai/dari-gateway 1.0.355 → 1.0.356
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/dist/ingress/models.js +8 -1
- package/dist/link/self_hosted.js +1 -0
- package/dist/platform/platform.d.ts +1 -0
- package/dist/platform/routing_contract.d.ts +1 -0
- package/dist/platform/routing_contract.js +3 -0
- package/dist/self_host/connected.js +5 -2
- package/dist/self_host/subscriptions/http.js +7 -1
- package/dist/self_host/subscriptions/store.d.ts +1 -0
- package/dist/self_host/subscriptions/store.js +6 -3
- package/package.json +2 -2
- package/src/ingress/models.ts +8 -1
- package/src/link/self_hosted.ts +1 -0
- package/src/platform/platform.ts +4 -0
- package/src/platform/routing_contract.ts +4 -0
- package/src/self_host/connected.ts +5 -2
- package/src/self_host/subscriptions/http.ts +8 -1
- package/src/self_host/subscriptions/store.ts +10 -3
package/dist/ingress/models.js
CHANGED
|
@@ -18,6 +18,7 @@ import { anthropicModelsResponse } from "./anthropic_models.js";
|
|
|
18
18
|
import { namedClientHarness } from "./client_harness.js";
|
|
19
19
|
import { codexModelsEtag, codexModelsResponse } from "./codex_models.js";
|
|
20
20
|
import { CANONICAL_ROUTER_MODEL, explicitModelSlug, LINK_CHILD_TOKEN_PREFIX } from "../protocol/input/router_model.js";
|
|
21
|
+
import { GATEWAY_ID_HEADER } from "../platform/routing_contract.js";
|
|
21
22
|
export async function handleModels(request, routerId) {
|
|
22
23
|
// Anthropic SDK clients always send anthropic-version, and API-key probes
|
|
23
24
|
// authenticate via x-api-key instead of a bearer Authorization header.
|
|
@@ -36,9 +37,15 @@ export async function handleModels(request, routerId) {
|
|
|
36
37
|
// names its own version in the query, as only Codex does.
|
|
37
38
|
const codexDirect = !anthropicClient && query.has("client_version");
|
|
38
39
|
const clientHarness = namedClientHarness(request.headers) ?? (codexDirect ? "codex" : undefined);
|
|
40
|
+
const gatewayId = request.headers.get(GATEWAY_ID_HEADER)?.trim();
|
|
39
41
|
let router;
|
|
40
42
|
try {
|
|
41
|
-
router = await resolveRouter({
|
|
43
|
+
router = await resolveRouter({
|
|
44
|
+
routerId,
|
|
45
|
+
apiKey,
|
|
46
|
+
...(clientHarness ? { clientHarness } : {}),
|
|
47
|
+
...(gatewayId ? { gatewayId } : {}),
|
|
48
|
+
});
|
|
42
49
|
}
|
|
43
50
|
catch (error) {
|
|
44
51
|
// A Dari Link grant that can serve this client nothing now lists no
|
package/dist/link/self_hosted.js
CHANGED
|
@@ -99,6 +99,7 @@ export function selfHostedLink(deps) {
|
|
|
99
99
|
credentialName: null,
|
|
100
100
|
owner: newSubject(),
|
|
101
101
|
private: false,
|
|
102
|
+
movingFrom: null,
|
|
102
103
|
expiresAt: authorization.expiresAt,
|
|
103
104
|
});
|
|
104
105
|
return { session: session.token, authorizationUrl: authorization.authorizationUrl, userCode: authorization.userCode };
|
|
@@ -3,3 +3,6 @@
|
|
|
3
3
|
export const DEFAULT_ROUTER_ALIAS = "default";
|
|
4
4
|
// The most prefix hashes one router resolution looks up.
|
|
5
5
|
export const ROUTING_PREFIX_HASHES_MAX_COUNT = 200;
|
|
6
|
+
// Names the gateway connected to hosted Dari that sent a request, so a model
|
|
7
|
+
// list counts the plans that gateway keeps on its computer.
|
|
8
|
+
export const GATEWAY_ID_HEADER = "x-dari-gateway-id";
|
|
@@ -3,7 +3,7 @@ import { isReasoningEffort } from "@mupt-ai/dari-router/policy-engine";
|
|
|
3
3
|
import { anthropicToken, bearerToken } from "../http/auth.js";
|
|
4
4
|
import { errorResponse, HttpError } from "../http/errors.js";
|
|
5
5
|
import { anthropicErrorResponse } from "../ingress/anthropic_messages.js";
|
|
6
|
-
import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
|
|
6
|
+
import { DEFAULT_ROUTER_ALIAS, GATEWAY_ID_HEADER } from "../platform/routing_contract.js";
|
|
7
7
|
import { explicitModelId } from "../protocol/input/router_model.js";
|
|
8
8
|
import { parseGatewayConfig } from "./config.js";
|
|
9
9
|
import { createSelfHostedPlatform } from "./platform.js";
|
|
@@ -84,7 +84,8 @@ function hostedRouters(hosted, fetchImpl) {
|
|
|
84
84
|
async function readHostedRouter(hosted, fetchImpl, routerId) {
|
|
85
85
|
const prefix = routerId === DEFAULT_ROUTER_ALIAS ? "" : `/${encodeURIComponent(routerId)}`;
|
|
86
86
|
const response = await fetchImpl(`${hosted.url}${prefix}/v1/models`, {
|
|
87
|
-
|
|
87
|
+
// Lists the models this gateway's plans serve, though hosted holds none.
|
|
88
|
+
headers: { authorization: `Bearer ${hosted.apiKey}`, [GATEWAY_ID_HEADER]: hosted.gatewayId },
|
|
88
89
|
signal: AbortSignal.timeout(30_000),
|
|
89
90
|
});
|
|
90
91
|
if (!response.ok)
|
|
@@ -176,6 +177,8 @@ async function forward(request, body, deps, fail) {
|
|
|
176
177
|
headers.delete(name);
|
|
177
178
|
}
|
|
178
179
|
headers.set("authorization", `Bearer ${deps.hosted.apiKey}`);
|
|
180
|
+
// An agent's model list includes the models this gateway's plans serve.
|
|
181
|
+
headers.set(GATEWAY_ID_HEADER, deps.hosted.gatewayId);
|
|
179
182
|
let response;
|
|
180
183
|
try {
|
|
181
184
|
response = await deps.fetch(`${deps.hosted.url}${url.pathname}${url.search}`, {
|
|
@@ -146,6 +146,11 @@ async function startSession(deps, body, now) {
|
|
|
146
146
|
const isPrivate = body.private ?? reconnecting?.private ?? false;
|
|
147
147
|
if (typeof isPrivate !== "boolean")
|
|
148
148
|
throw new HttpError(400, "private must be true or false.");
|
|
149
|
+
// `dari accounts subscription move` names the hosted plan it moves here.
|
|
150
|
+
const movingFrom = body.moving_from ?? null;
|
|
151
|
+
if (movingFrom !== null && (typeof movingFrom !== "string" || !movingFrom.trim())) {
|
|
152
|
+
throw new HttpError(400, "moving_from must be a non-empty string.");
|
|
153
|
+
}
|
|
149
154
|
const authorization = await startOAuth(provider, { fetch: deps.fetch, now });
|
|
150
155
|
const session = deps.store.createSession({
|
|
151
156
|
provider: provider,
|
|
@@ -153,6 +158,7 @@ async function startSession(deps, body, now) {
|
|
|
153
158
|
credentialName,
|
|
154
159
|
owner: null,
|
|
155
160
|
private: isPrivate,
|
|
161
|
+
movingFrom,
|
|
156
162
|
expiresAt: authorization.expiresAt,
|
|
157
163
|
}, now);
|
|
158
164
|
return {
|
|
@@ -189,7 +195,7 @@ async function completeSession(deps, body, now) {
|
|
|
189
195
|
let elsewhere = [];
|
|
190
196
|
if (deps.hostedAccounts !== undefined && !reconnectsSameAccount) {
|
|
191
197
|
try {
|
|
192
|
-
elsewhere = await deps.hostedAccounts();
|
|
198
|
+
elsewhere = (await deps.hostedAccounts()).filter((account) => !(account.place === "dari" && account.name === session.movingFrom));
|
|
193
199
|
}
|
|
194
200
|
catch (error) {
|
|
195
201
|
// The provider code is spent: keep the sign-in, checked against this
|
|
@@ -127,10 +127,11 @@ export class SubscriptionStore {
|
|
|
127
127
|
credential_name TEXT,
|
|
128
128
|
owner TEXT,
|
|
129
129
|
private INTEGER,
|
|
130
|
+
moving_from TEXT,
|
|
130
131
|
expires_at INTEGER NOT NULL
|
|
131
132
|
)`);
|
|
132
133
|
const sessionColumns = new Set(db.query("PRAGMA table_info(subscription_oauth_sessions)").all().map((column) => column.name));
|
|
133
|
-
for (const [column, type] of [["owner", "TEXT"], ["private", "INTEGER"]]) {
|
|
134
|
+
for (const [column, type] of [["owner", "TEXT"], ["private", "INTEGER"], ["moving_from", "TEXT"]]) {
|
|
134
135
|
if (!sessionColumns.has(column))
|
|
135
136
|
db.run(`ALTER TABLE subscription_oauth_sessions ADD COLUMN ${column} ${type}`);
|
|
136
137
|
}
|
|
@@ -500,14 +501,15 @@ export class SubscriptionStore {
|
|
|
500
501
|
createSession(session, now = new Date()) {
|
|
501
502
|
const token = randomBytes(32).toString("base64url");
|
|
502
503
|
this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
|
|
503
|
-
this.db.run(`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, private, expires_at)
|
|
504
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
|
504
|
+
this.db.run(`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, private, moving_from, expires_at)
|
|
505
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
505
506
|
token,
|
|
506
507
|
session.provider,
|
|
507
508
|
this.seal(JSON.stringify(session.flow), flowPlace(token)),
|
|
508
509
|
session.credentialName,
|
|
509
510
|
session.owner,
|
|
510
511
|
session.private ? 1 : 0,
|
|
512
|
+
session.movingFrom,
|
|
511
513
|
session.expiresAt.getTime(),
|
|
512
514
|
]);
|
|
513
515
|
return { token, ...session };
|
|
@@ -526,6 +528,7 @@ export class SubscriptionStore {
|
|
|
526
528
|
credentialName: row.credential_name,
|
|
527
529
|
owner: row.owner,
|
|
528
530
|
private: row.private === 1,
|
|
531
|
+
movingFrom: row.moving_from,
|
|
529
532
|
expiresAt: new Date(row.expires_at),
|
|
530
533
|
};
|
|
531
534
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mupt-ai/dari-gateway",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.356",
|
|
4
4
|
"description": "Self-hostable LLM gateway for coding agents and APIs: one endpoint for Anthropic Messages, OpenAI Chat Completions, and OpenAI Responses, with subscription pooling, failover, and prompt-cache-aware routing.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@aws-sdk/client-sts": "3.1063.0",
|
|
55
|
-
"@mupt-ai/dari-router": "1.0.
|
|
55
|
+
"@mupt-ai/dari-router": "1.0.356",
|
|
56
56
|
"@mupt-ai/pi-ai": "1.5.2-mupt.3"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
package/src/ingress/models.ts
CHANGED
|
@@ -19,6 +19,7 @@ import { anthropicModelsResponse } from "./anthropic_models.js";
|
|
|
19
19
|
import { namedClientHarness } from "./client_harness.js";
|
|
20
20
|
import { codexModelsEtag, codexModelsResponse } from "./codex_models.js";
|
|
21
21
|
import { CANONICAL_ROUTER_MODEL, explicitModelSlug, LINK_CHILD_TOKEN_PREFIX } from "../protocol/input/router_model.js";
|
|
22
|
+
import { GATEWAY_ID_HEADER } from "../platform/routing_contract.js";
|
|
22
23
|
|
|
23
24
|
export async function handleModels(request: Request, routerId: string): Promise<Response> {
|
|
24
25
|
// Anthropic SDK clients always send anthropic-version, and API-key probes
|
|
@@ -39,9 +40,15 @@ export async function handleModels(request: Request, routerId: string): Promise<
|
|
|
39
40
|
// names its own version in the query, as only Codex does.
|
|
40
41
|
const codexDirect = !anthropicClient && query.has("client_version");
|
|
41
42
|
const clientHarness = namedClientHarness(request.headers) ?? (codexDirect ? "codex" : undefined);
|
|
43
|
+
const gatewayId = request.headers.get(GATEWAY_ID_HEADER)?.trim();
|
|
42
44
|
let router;
|
|
43
45
|
try {
|
|
44
|
-
router = await resolveRouter({
|
|
46
|
+
router = await resolveRouter({
|
|
47
|
+
routerId,
|
|
48
|
+
apiKey,
|
|
49
|
+
...(clientHarness ? { clientHarness } : {}),
|
|
50
|
+
...(gatewayId ? { gatewayId } : {}),
|
|
51
|
+
});
|
|
45
52
|
} catch (error) {
|
|
46
53
|
// A Dari Link grant that can serve this client nothing now lists no
|
|
47
54
|
// models; a child token still lists its one model.
|
package/src/link/self_hosted.ts
CHANGED
|
@@ -121,6 +121,7 @@ export function selfHostedLink(
|
|
|
121
121
|
credentialName: null,
|
|
122
122
|
owner: newSubject(),
|
|
123
123
|
private: false,
|
|
124
|
+
movingFrom: null,
|
|
124
125
|
expiresAt: authorization.expiresAt,
|
|
125
126
|
});
|
|
126
127
|
return { session: session.token, authorizationUrl: authorization.authorizationUrl, userCode: authorization.userCode };
|
package/src/platform/platform.ts
CHANGED
|
@@ -512,6 +512,10 @@ export type ResolveRouterRequest = {
|
|
|
512
512
|
conversationId?: string;
|
|
513
513
|
includePublicEvalCatalog?: boolean;
|
|
514
514
|
clientHarness?: ClientHarness;
|
|
515
|
+
// The connected gateway listing this router's models: a model a plan on
|
|
516
|
+
// that gateway serves is listed though hosted Dari holds no plan for it.
|
|
517
|
+
// Model lists only; that gateway serves those models itself.
|
|
518
|
+
gatewayId?: string;
|
|
515
519
|
// A later resolve with this request's `conversationId` replaces this one's
|
|
516
520
|
// credentials, so none from it serve the request: a platform should not
|
|
517
521
|
// mark a credential used or refresh it for this resolve.
|
|
@@ -4,3 +4,7 @@ export const DEFAULT_ROUTER_ALIAS = "default";
|
|
|
4
4
|
|
|
5
5
|
// The most prefix hashes one router resolution looks up.
|
|
6
6
|
export const ROUTING_PREFIX_HASHES_MAX_COUNT = 200;
|
|
7
|
+
|
|
8
|
+
// Names the gateway connected to hosted Dari that sent a request, so a model
|
|
9
|
+
// list counts the plans that gateway keeps on its computer.
|
|
10
|
+
export const GATEWAY_ID_HEADER = "x-dari-gateway-id";
|
|
@@ -5,7 +5,7 @@ import { anthropicToken, bearerToken } from "../http/auth.js";
|
|
|
5
5
|
import { errorResponse, HttpError } from "../http/errors.js";
|
|
6
6
|
import { anthropicErrorResponse } from "../ingress/anthropic_messages.js";
|
|
7
7
|
import type { GatewayPlatform } from "../platform/platform.js";
|
|
8
|
-
import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
|
|
8
|
+
import { DEFAULT_ROUTER_ALIAS, GATEWAY_ID_HEADER } from "../platform/routing_contract.js";
|
|
9
9
|
import { explicitModelId } from "../protocol/input/router_model.js";
|
|
10
10
|
import { parseGatewayConfig, type RouterConfig } from "./config.js";
|
|
11
11
|
import type { HostedSync } from "./hosted_sync.js";
|
|
@@ -116,7 +116,8 @@ function hostedRouters(hosted: HostedConnection, fetchImpl: typeof fetch) {
|
|
|
116
116
|
async function readHostedRouter(hosted: HostedConnection, fetchImpl: typeof fetch, routerId: string): Promise<RouterConfig> {
|
|
117
117
|
const prefix = routerId === DEFAULT_ROUTER_ALIAS ? "" : `/${encodeURIComponent(routerId)}`;
|
|
118
118
|
const response = await fetchImpl(`${hosted.url}${prefix}/v1/models`, {
|
|
119
|
-
|
|
119
|
+
// Lists the models this gateway's plans serve, though hosted holds none.
|
|
120
|
+
headers: { authorization: `Bearer ${hosted.apiKey}`, [GATEWAY_ID_HEADER]: hosted.gatewayId },
|
|
120
121
|
signal: AbortSignal.timeout(30_000),
|
|
121
122
|
});
|
|
122
123
|
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
@@ -219,6 +220,8 @@ async function forward(
|
|
|
219
220
|
headers.delete(name);
|
|
220
221
|
}
|
|
221
222
|
headers.set("authorization", `Bearer ${deps.hosted.apiKey}`);
|
|
223
|
+
// An agent's model list includes the models this gateway's plans serve.
|
|
224
|
+
headers.set(GATEWAY_ID_HEADER, deps.hosted.gatewayId);
|
|
222
225
|
let response: Response;
|
|
223
226
|
try {
|
|
224
227
|
response = await deps.fetch(`${deps.hosted.url}${url.pathname}${url.search}`, {
|
|
@@ -170,6 +170,11 @@ async function startSession(deps: SubscriptionHttpDeps, body: Record<string, unk
|
|
|
170
170
|
// A reconnected plan stays where it was unless the caller says otherwise.
|
|
171
171
|
const isPrivate = body.private ?? reconnecting?.private ?? false;
|
|
172
172
|
if (typeof isPrivate !== "boolean") throw new HttpError(400, "private must be true or false.");
|
|
173
|
+
// `dari accounts subscription move` names the hosted plan it moves here.
|
|
174
|
+
const movingFrom = body.moving_from ?? null;
|
|
175
|
+
if (movingFrom !== null && (typeof movingFrom !== "string" || !movingFrom.trim())) {
|
|
176
|
+
throw new HttpError(400, "moving_from must be a non-empty string.");
|
|
177
|
+
}
|
|
173
178
|
const authorization = await startOAuth(provider as SubscriptionProvider, { fetch: deps.fetch, now });
|
|
174
179
|
const session = deps.store.createSession(
|
|
175
180
|
{
|
|
@@ -178,6 +183,7 @@ async function startSession(deps: SubscriptionHttpDeps, body: Record<string, unk
|
|
|
178
183
|
credentialName,
|
|
179
184
|
owner: null,
|
|
180
185
|
private: isPrivate,
|
|
186
|
+
movingFrom,
|
|
181
187
|
expiresAt: authorization.expiresAt,
|
|
182
188
|
},
|
|
183
189
|
now,
|
|
@@ -216,7 +222,8 @@ async function completeSession(deps: SubscriptionHttpDeps, body: Record<string,
|
|
|
216
222
|
let elsewhere: ConnectedAccount[] = [];
|
|
217
223
|
if (deps.hostedAccounts !== undefined && !reconnectsSameAccount) {
|
|
218
224
|
try {
|
|
219
|
-
elsewhere = await deps.hostedAccounts()
|
|
225
|
+
elsewhere = (await deps.hostedAccounts()).filter((account) =>
|
|
226
|
+
!(account.place === "dari" && account.name === session.movingFrom));
|
|
220
227
|
} catch (error) {
|
|
221
228
|
// The provider code is spent: keep the sign-in, checked against this
|
|
222
229
|
// gateway's own plans only, rather than make the user start over.
|
|
@@ -87,6 +87,9 @@ export type PendingOAuthSession = {
|
|
|
87
87
|
owner: string | null;
|
|
88
88
|
// Whether the plan connects as private (see SubscriptionCredential).
|
|
89
89
|
private: boolean;
|
|
90
|
+
// The plan on hosted Dari this sign-in moves here, by name: its account is
|
|
91
|
+
// not a second copy, since the caller disconnects it there next.
|
|
92
|
+
movingFrom: string | null;
|
|
90
93
|
expiresAt: Date;
|
|
91
94
|
};
|
|
92
95
|
|
|
@@ -202,6 +205,7 @@ type SessionRow = {
|
|
|
202
205
|
credential_name: string | null;
|
|
203
206
|
owner: string | null;
|
|
204
207
|
private: number | null;
|
|
208
|
+
moving_from: string | null;
|
|
205
209
|
expires_at: number;
|
|
206
210
|
};
|
|
207
211
|
|
|
@@ -268,12 +272,13 @@ export class SubscriptionStore {
|
|
|
268
272
|
credential_name TEXT,
|
|
269
273
|
owner TEXT,
|
|
270
274
|
private INTEGER,
|
|
275
|
+
moving_from TEXT,
|
|
271
276
|
expires_at INTEGER NOT NULL
|
|
272
277
|
)`);
|
|
273
278
|
const sessionColumns = new Set(
|
|
274
279
|
db.query<{ name: string }, []>("PRAGMA table_info(subscription_oauth_sessions)").all().map((column) => column.name),
|
|
275
280
|
);
|
|
276
|
-
for (const [column, type] of [["owner", "TEXT"], ["private", "INTEGER"]] as const) {
|
|
281
|
+
for (const [column, type] of [["owner", "TEXT"], ["private", "INTEGER"], ["moving_from", "TEXT"]] as const) {
|
|
277
282
|
if (!sessionColumns.has(column)) db.run(`ALTER TABLE subscription_oauth_sessions ADD COLUMN ${column} ${type}`);
|
|
278
283
|
}
|
|
279
284
|
this.encryptStored();
|
|
@@ -711,8 +716,8 @@ export class SubscriptionStore {
|
|
|
711
716
|
const token = randomBytes(32).toString("base64url");
|
|
712
717
|
this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
|
|
713
718
|
this.db.run(
|
|
714
|
-
`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, private, expires_at)
|
|
715
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
719
|
+
`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, private, moving_from, expires_at)
|
|
720
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
716
721
|
[
|
|
717
722
|
token,
|
|
718
723
|
session.provider,
|
|
@@ -720,6 +725,7 @@ export class SubscriptionStore {
|
|
|
720
725
|
session.credentialName,
|
|
721
726
|
session.owner,
|
|
722
727
|
session.private ? 1 : 0,
|
|
728
|
+
session.movingFrom,
|
|
723
729
|
session.expiresAt.getTime(),
|
|
724
730
|
],
|
|
725
731
|
);
|
|
@@ -739,6 +745,7 @@ export class SubscriptionStore {
|
|
|
739
745
|
credentialName: row.credential_name,
|
|
740
746
|
owner: row.owner,
|
|
741
747
|
private: row.private === 1,
|
|
748
|
+
movingFrom: row.moving_from,
|
|
742
749
|
expiresAt: new Date(row.expires_at),
|
|
743
750
|
};
|
|
744
751
|
}
|