@mupt-ai/dari-gateway 1.0.368 → 1.0.369
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/link/self_hosted.js +0 -1
- package/dist/platform/platform.d.ts +2 -0
- package/dist/platform/routing_contract.d.ts +1 -0
- package/dist/platform/routing_contract.js +4 -0
- package/dist/protocol/output/anthropic_messages.d.ts +1 -0
- package/dist/protocol/output/anthropic_messages.js +1 -1
- package/dist/protocol/output/anthropic_messages_output.d.ts +1 -1
- package/dist/protocol/output/anthropic_messages_output.js +3 -1
- package/dist/protocol/output/anthropic_messages_stream.d.ts +1 -0
- package/dist/protocol/output/anthropic_messages_stream.js +1 -1
- package/dist/protocol/output/chat_completions.d.ts +2 -0
- package/dist/protocol/output/chat_completions.js +5 -3
- package/dist/protocol/output/openai_responses.d.ts +1 -0
- package/dist/protocol/output/openai_responses.js +1 -1
- package/dist/protocol/output/openai_responses_output.d.ts +1 -1
- package/dist/protocol/output/openai_responses_output.js +3 -1
- package/dist/protocol/output/openai_responses_stream.d.ts +1 -0
- package/dist/protocol/output/openai_responses_stream.js +1 -1
- package/dist/providers/execution.js +2 -0
- package/dist/providers/provider_call.d.ts +1 -0
- package/dist/providers/provider_call.js +2 -0
- package/dist/runtime/prepare_routed_completion.d.ts +1 -0
- package/dist/runtime/prepare_routed_completion.js +4 -0
- package/dist/runtime/routed_completion.js +2 -0
- package/dist/runtime/routed_protocol.d.ts +2 -0
- package/dist/self_host/connected.d.ts +7 -3
- package/dist/self_host/connected.js +9 -10
- package/dist/self_host/hosted_sync.js +1 -1
- package/dist/self_host/main.d.ts +3 -2
- package/dist/self_host/main.js +16 -6
- package/dist/self_host/private_mode.d.ts +16 -0
- package/dist/self_host/private_mode.js +35 -0
- package/dist/self_host/resolve.js +5 -2
- package/dist/self_host/subscriptions/http.js +2 -17
- package/dist/self_host/subscriptions/store.d.ts +7 -6
- package/dist/self_host/subscriptions/store.js +15 -27
- package/dist/telemetry/report.js +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +2 -2
- package/src/link/self_hosted.ts +0 -1
- package/src/platform/platform.ts +6 -0
- package/src/platform/routing_contract.ts +5 -0
- package/src/protocol/output/anthropic_messages.ts +2 -1
- package/src/protocol/output/anthropic_messages_output.ts +4 -1
- package/src/protocol/output/anthropic_messages_stream.ts +2 -1
- package/src/protocol/output/chat_completions.ts +7 -3
- package/src/protocol/output/openai_responses.ts +2 -1
- package/src/protocol/output/openai_responses_output.ts +4 -1
- package/src/protocol/output/openai_responses_stream.ts +2 -1
- package/src/providers/execution.ts +2 -0
- package/src/providers/provider_call.ts +5 -0
- package/src/runtime/prepare_routed_completion.ts +4 -0
- package/src/runtime/routed_completion.ts +2 -0
- package/src/runtime/routed_protocol.ts +2 -0
- package/src/self_host/connected.ts +16 -12
- package/src/self_host/hosted_sync.ts +1 -1
- package/src/self_host/main.ts +14 -6
- package/src/self_host/private_mode.ts +41 -0
- package/src/self_host/resolve.ts +4 -2
- package/src/self_host/subscriptions/http.ts +2 -15
- package/src/self_host/subscriptions/store.ts +15 -34
- package/src/telemetry/report.ts +1 -0
- package/src/types.ts +3 -0
|
@@ -37,7 +37,6 @@ export type SubscriptionCredential = {
|
|
|
37
37
|
accountEmail: string | null;
|
|
38
38
|
accountId: string | null;
|
|
39
39
|
owner: string | null;
|
|
40
|
-
private: boolean;
|
|
41
40
|
accessToken: string;
|
|
42
41
|
refreshToken: string;
|
|
43
42
|
expiresAt: Date;
|
|
@@ -63,7 +62,6 @@ export type PendingOAuthSession = {
|
|
|
63
62
|
flow: Record<string, string>;
|
|
64
63
|
credentialName: string | null;
|
|
65
64
|
owner: string | null;
|
|
66
|
-
private: boolean;
|
|
67
65
|
movingFrom: string | null;
|
|
68
66
|
expiresAt: Date;
|
|
69
67
|
};
|
|
@@ -86,7 +84,7 @@ export type AccountRef = {
|
|
|
86
84
|
* other computers. `name` is the plan's handle.
|
|
87
85
|
*/
|
|
88
86
|
export type ConnectedAccount = AccountRef & {
|
|
89
|
-
place: "dari" | "this_computer"
|
|
87
|
+
place: "dari" | "this_computer";
|
|
90
88
|
name: string | null;
|
|
91
89
|
connectedAt?: Date;
|
|
92
90
|
};
|
|
@@ -149,15 +147,18 @@ export declare class SubscriptionStore {
|
|
|
149
147
|
* null is the gateway owner), or `elsewhere` (the owner's plans on hosted
|
|
150
148
|
* Dari and other computers) holds it.
|
|
151
149
|
*/
|
|
152
|
-
connect(tokens: OAuthTokens, reconnectName: string | null, now?: Date, owner?: string | null,
|
|
150
|
+
connect(tokens: OAuthTokens, reconnectName: string | null, now?: Date, owner?: string | null, elsewhere?: ConnectedAccount[]): SubscriptionCredential | null;
|
|
153
151
|
private refuseSecondCopy;
|
|
154
152
|
/** Persist refreshed tokens. Keeps the stored account id when the refresh carries none. */
|
|
155
153
|
updateTokens(id: string, tokens: OAuthTokens, now?: Date): void;
|
|
156
154
|
touch(id: string, now?: Date): void;
|
|
157
155
|
/** An empty label clears it. Null when the subscription is not connected. */
|
|
158
156
|
rename(name: string, label: string | null, now?: Date): SubscriptionCredential | null;
|
|
159
|
-
/**
|
|
160
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Clear the marks of plans connected as private, before private mode
|
|
159
|
+
* replaced them; true when any was marked.
|
|
160
|
+
*/
|
|
161
|
+
clearLegacyPrivate(): boolean;
|
|
161
162
|
/** Remove a subscription. Returns what was removed, or null when it was not connected. */
|
|
162
163
|
disconnect(name: string, owner?: string | null): SubscriptionCredential | null;
|
|
163
164
|
/**
|
|
@@ -40,7 +40,6 @@ const PROVIDER_LABELS = {
|
|
|
40
40
|
const PLACE_NAMES = {
|
|
41
41
|
dari: "on Dari",
|
|
42
42
|
this_computer: "on This Computer",
|
|
43
|
-
private: "Private on This Computer",
|
|
44
43
|
};
|
|
45
44
|
function connectedAccountMessage(account) {
|
|
46
45
|
const asName = account.name === null ? "" : ` as ${account.name}`;
|
|
@@ -63,7 +62,7 @@ const ADDED_COLUMNS = {
|
|
|
63
62
|
usage_windows: "TEXT",
|
|
64
63
|
model_holds: "TEXT",
|
|
65
64
|
owner: "TEXT",
|
|
66
|
-
private: "INTEGER",
|
|
65
|
+
private: "INTEGER", // read once, by clearLegacyPrivate
|
|
67
66
|
auto_redeem_resets: "INTEGER", // no longer read; resets follow each model's sources
|
|
68
67
|
};
|
|
69
68
|
export class SubscriptionStore {
|
|
@@ -115,12 +114,11 @@ export class SubscriptionStore {
|
|
|
115
114
|
flow_json TEXT NOT NULL,
|
|
116
115
|
credential_name TEXT,
|
|
117
116
|
owner TEXT,
|
|
118
|
-
private INTEGER,
|
|
119
117
|
moving_from TEXT,
|
|
120
118
|
expires_at INTEGER NOT NULL
|
|
121
119
|
)`);
|
|
122
120
|
const sessionColumns = new Set(db.query("PRAGMA table_info(subscription_oauth_sessions)").all().map((column) => column.name));
|
|
123
|
-
for (const [column, type] of [["owner", "TEXT"], ["
|
|
121
|
+
for (const [column, type] of [["owner", "TEXT"], ["moving_from", "TEXT"]]) {
|
|
124
122
|
if (!sessionColumns.has(column))
|
|
125
123
|
db.run(`ALTER TABLE subscription_oauth_sessions ADD COLUMN ${column} ${type}`);
|
|
126
124
|
}
|
|
@@ -281,7 +279,7 @@ export class SubscriptionStore {
|
|
|
281
279
|
* null is the gateway owner), or `elsewhere` (the owner's plans on hosted
|
|
282
280
|
* Dari and other computers) holds it.
|
|
283
281
|
*/
|
|
284
|
-
connect(tokens, reconnectName, now = new Date(), owner = null,
|
|
282
|
+
connect(tokens, reconnectName, now = new Date(), owner = null, elsewhere = []) {
|
|
285
283
|
const connected = this.db.transaction(() => {
|
|
286
284
|
const existing = reconnectName !== null ? this.getByName(reconnectName, owner ?? null) : null;
|
|
287
285
|
if (reconnectName !== null && existing?.provider !== tokens.provider)
|
|
@@ -297,7 +295,7 @@ export class SubscriptionStore {
|
|
|
297
295
|
this.db.run(`UPDATE subscription_credentials SET access_token = ?, refresh_token = ?, expires_at = ?,
|
|
298
296
|
account_id = ?, account_email = ?, unavailable_until = ?, unavailable_reason = ?,
|
|
299
297
|
usage_utilization = ?, usage_resets_at = ?, usage_observed_at = ?, usage_windows = ?, model_holds = ?,
|
|
300
|
-
|
|
298
|
+
updated_at = ?
|
|
301
299
|
WHERE id = ?`, [
|
|
302
300
|
this.seal(tokens.accessToken, tokenPlace(existing.id, "access_token")),
|
|
303
301
|
this.seal(tokens.refreshToken, tokenPlace(existing.id, "refresh_token")),
|
|
@@ -311,7 +309,6 @@ export class SubscriptionStore {
|
|
|
311
309
|
usage?.observedAt.getTime() ?? null,
|
|
312
310
|
usage === null ? null : windowsJson(usage),
|
|
313
311
|
sameAccount ? modelHoldsJson(existing.modelHolds) : null,
|
|
314
|
-
isPrivate ? 1 : 0,
|
|
315
312
|
now.getTime(),
|
|
316
313
|
existing.id,
|
|
317
314
|
]);
|
|
@@ -325,16 +322,15 @@ export class SubscriptionStore {
|
|
|
325
322
|
const position = this.db
|
|
326
323
|
.query("SELECT COALESCE(MAX(position), 0) + 1 AS next FROM subscription_credentials")
|
|
327
324
|
.get().next;
|
|
328
|
-
this.db.run(`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner,
|
|
325
|
+
this.db.run(`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner,
|
|
329
326
|
access_token, refresh_token, expires_at, position, created_at, updated_at)
|
|
330
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
327
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
331
328
|
id,
|
|
332
329
|
tokens.provider,
|
|
333
330
|
this.freeName(BASE_NAMES[tokens.provider]),
|
|
334
331
|
tokens.accountEmail,
|
|
335
332
|
tokens.accountId,
|
|
336
333
|
owner,
|
|
337
|
-
isPrivate ? 1 : 0,
|
|
338
334
|
this.seal(tokens.accessToken, tokenPlace(id, "access_token")),
|
|
339
335
|
this.seal(tokens.refreshToken, tokenPlace(id, "refresh_token")),
|
|
340
336
|
tokens.expiresAt.getTime(),
|
|
@@ -355,7 +351,7 @@ export class SubscriptionStore {
|
|
|
355
351
|
provider: local.provider,
|
|
356
352
|
accountId: local.accountId,
|
|
357
353
|
accountEmail: local.accountEmail,
|
|
358
|
-
place:
|
|
354
|
+
place: "this_computer",
|
|
359
355
|
name: local.name,
|
|
360
356
|
}));
|
|
361
357
|
}
|
|
@@ -394,17 +390,12 @@ export class SubscriptionStore {
|
|
|
394
390
|
]);
|
|
395
391
|
return this.get(credential.id);
|
|
396
392
|
}
|
|
397
|
-
/**
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
this.db.run("UPDATE subscription_credentials SET private =
|
|
403
|
-
isPrivate ? 1 : 0,
|
|
404
|
-
now.getTime(),
|
|
405
|
-
credential.id,
|
|
406
|
-
]);
|
|
407
|
-
return this.get(credential.id);
|
|
393
|
+
/**
|
|
394
|
+
* Clear the marks of plans connected as private, before private mode
|
|
395
|
+
* replaced them; true when any was marked.
|
|
396
|
+
*/
|
|
397
|
+
clearLegacyPrivate() {
|
|
398
|
+
return this.db.run("UPDATE subscription_credentials SET private = NULL WHERE private = 1").changes > 0;
|
|
408
399
|
}
|
|
409
400
|
/** Remove a subscription. Returns what was removed, or null when it was not connected. */
|
|
410
401
|
disconnect(name, owner = null) {
|
|
@@ -462,14 +453,13 @@ export class SubscriptionStore {
|
|
|
462
453
|
createSession(session, now = new Date()) {
|
|
463
454
|
const token = randomBytes(32).toString("base64url");
|
|
464
455
|
this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
|
|
465
|
-
this.db.run(`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner,
|
|
466
|
-
VALUES (?, ?, ?, ?, ?, ?,
|
|
456
|
+
this.db.run(`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, moving_from, expires_at)
|
|
457
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`, [
|
|
467
458
|
token,
|
|
468
459
|
session.provider,
|
|
469
460
|
this.seal(JSON.stringify(session.flow), flowPlace(token)),
|
|
470
461
|
session.credentialName,
|
|
471
462
|
session.owner,
|
|
472
|
-
session.private ? 1 : 0,
|
|
473
463
|
session.movingFrom,
|
|
474
464
|
session.expiresAt.getTime(),
|
|
475
465
|
]);
|
|
@@ -488,7 +478,6 @@ export class SubscriptionStore {
|
|
|
488
478
|
flow: JSON.parse(this.open(row.flow_json, flowPlace(row.token))),
|
|
489
479
|
credentialName: row.credential_name,
|
|
490
480
|
owner: row.owner,
|
|
491
|
-
private: row.private === 1,
|
|
492
481
|
movingFrom: row.moving_from,
|
|
493
482
|
expiresAt: new Date(row.expires_at),
|
|
494
483
|
};
|
|
@@ -589,7 +578,6 @@ function credentialFromRow(row) {
|
|
|
589
578
|
accountEmail: row.account_email,
|
|
590
579
|
accountId: row.account_id,
|
|
591
580
|
owner: row.owner,
|
|
592
|
-
private: row.private === 1,
|
|
593
581
|
accessToken: row.access_token,
|
|
594
582
|
refreshToken: row.refresh_token,
|
|
595
583
|
expiresAt: new Date(row.expires_at),
|
package/dist/telemetry/report.js
CHANGED
|
@@ -457,6 +457,7 @@ export class RequestReporter {
|
|
|
457
457
|
gatewayPlatform().recordRequest({
|
|
458
458
|
...(link === undefined ? {} : { link }),
|
|
459
459
|
...(subscriptionCredentialId === undefined ? {} : { subscriptionCredentialId }),
|
|
460
|
+
private: a.credentials.private === true,
|
|
460
461
|
router: requestRecordRouter(a.credentials),
|
|
461
462
|
candidates: a.candidates.map(({ model, reasoningEffort }) => ({ model, reasoningEffort })),
|
|
462
463
|
...(a.sessionPinKey === undefined ? {} : { sessionPinKey: a.sessionPinKey }),
|
package/dist/types.d.ts
CHANGED
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.369",
|
|
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.369",
|
|
56
56
|
"@mupt-ai/pi-ai": "1.5.2-mupt.3"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
package/src/link/self_hosted.ts
CHANGED
package/src/platform/platform.ts
CHANGED
|
@@ -507,6 +507,9 @@ export type ResolveRouterRequest = {
|
|
|
507
507
|
// The client's own identifying headers (ingress/client_harness.ts), for a
|
|
508
508
|
// platform that hands the request on.
|
|
509
509
|
clientHeaders?: Record<string, string>;
|
|
510
|
+
// The request asked to stay out of Activity, as ResolvedRouter.private; a
|
|
511
|
+
// platform that hands it on asks the same.
|
|
512
|
+
private?: boolean;
|
|
510
513
|
// A later resolve with this request's `conversationId` replaces this one's
|
|
511
514
|
// credentials, so none from it serve the request: a platform should not
|
|
512
515
|
// mark a credential used or refresh it for this resolve.
|
|
@@ -614,6 +617,9 @@ export type RequestRecord = {
|
|
|
614
617
|
link?: { grantId: string; thinking: string | null; costUsd: number | null; credentialId: string | null };
|
|
615
618
|
// The user's plan that served the request, when one did.
|
|
616
619
|
subscriptionCredentialId?: string;
|
|
620
|
+
// The request asked to stay out of Activity (ResolvedRouter.private): only
|
|
621
|
+
// what billing needs may be kept.
|
|
622
|
+
private: boolean;
|
|
617
623
|
// The router's non-secret routing settings, for embedders that compare or
|
|
618
624
|
// replay routing decisions.
|
|
619
625
|
router: RequestRecordRouter;
|
|
@@ -8,3 +8,8 @@ export const ROUTING_PREFIX_HASHES_MAX_COUNT = 200;
|
|
|
8
8
|
// The provider id of hosted Dari when a gateway connected to it calls it for
|
|
9
9
|
// a model, as `dari/model/<id>` with the user's Dari key.
|
|
10
10
|
export const HOSTED_DARI_PROVIDER = "dari";
|
|
11
|
+
|
|
12
|
+
// A request with this header set to `true` stays out of Activity: the
|
|
13
|
+
// platform keeps only what billing needs. A gateway in private mode sets it on
|
|
14
|
+
// every request, and on each it hands hosted Dari.
|
|
15
|
+
export const PRIVATE_HEADER = "x-dari-private";
|
|
@@ -17,6 +17,7 @@ export function anthropicMessagesResponse(args: {
|
|
|
17
17
|
request: InteractionRequest;
|
|
18
18
|
routing: RoutingDecision;
|
|
19
19
|
conversationId?: string;
|
|
20
|
+
private?: boolean;
|
|
20
21
|
}): Record<string, unknown> {
|
|
21
22
|
return {
|
|
22
23
|
id: anthropicMessageId(args.id),
|
|
@@ -29,6 +30,6 @@ export function anthropicMessagesResponse(args: {
|
|
|
29
30
|
// As on Chat Completions. Null means unpriced, never a fabricated free request.
|
|
30
31
|
usage: { ...anthropicUsage(args.output), cost: args.modelCost ?? null },
|
|
31
32
|
...(args.output.safeguardResults ? { safeguard_results: args.output.safeguardResults } : {}),
|
|
32
|
-
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
33
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId, args.private),
|
|
33
34
|
};
|
|
34
35
|
}
|
|
@@ -120,7 +120,8 @@ export function anthropicUsage(output: Pick<InteractionOutput, "usage">): Record
|
|
|
120
120
|
export function anthropicRouting(
|
|
121
121
|
decision: ServedDecision,
|
|
122
122
|
requestId: string,
|
|
123
|
-
conversationId?: string
|
|
123
|
+
conversationId?: string,
|
|
124
|
+
isPrivate?: boolean
|
|
124
125
|
): Record<string, unknown> {
|
|
125
126
|
return {
|
|
126
127
|
requested_model: "dari/routing",
|
|
@@ -129,6 +130,8 @@ export function anthropicRouting(
|
|
|
129
130
|
reason: decision.reason,
|
|
130
131
|
request_id: requestId,
|
|
131
132
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
133
|
+
// Kept out of Activity, so no link to it there.
|
|
134
|
+
...(isPrivate ? { private: true } : {}),
|
|
132
135
|
...(decision.leaseTurnsRemaining === undefined
|
|
133
136
|
? {}
|
|
134
137
|
: { lease_turns_remaining: decision.leaseTurnsRemaining }),
|
|
@@ -24,6 +24,7 @@ export function anthropicMessagesSseStream(args: {
|
|
|
24
24
|
source: ProviderIdentity;
|
|
25
25
|
request: InteractionRequest;
|
|
26
26
|
conversationId?: string;
|
|
27
|
+
private?: boolean;
|
|
27
28
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
28
29
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
29
30
|
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
@@ -59,7 +60,7 @@ export function anthropicMessagesSseStream(args: {
|
|
|
59
60
|
stop_reason: null,
|
|
60
61
|
stop_sequence: null,
|
|
61
62
|
usage: { input_tokens: 0, output_tokens: 0 },
|
|
62
|
-
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId),
|
|
63
|
+
dari_routing: anthropicRouting(args.routing, args.id, args.conversationId, args.private),
|
|
63
64
|
},
|
|
64
65
|
});
|
|
65
66
|
|
|
@@ -20,6 +20,7 @@ export function chatCompletionResponse(args: {
|
|
|
20
20
|
modelCost?: number | null;
|
|
21
21
|
routing: RoutingDecision;
|
|
22
22
|
conversationId?: string;
|
|
23
|
+
private?: boolean;
|
|
23
24
|
}): Record<string, unknown> {
|
|
24
25
|
return {
|
|
25
26
|
id: completionId(args.id),
|
|
@@ -34,7 +35,7 @@ export function chatCompletionResponse(args: {
|
|
|
34
35
|
},
|
|
35
36
|
],
|
|
36
37
|
usage: usagePayload(args.output, args.modelCost),
|
|
37
|
-
dari_routing: routingPayload(args.routing, args.id, args.conversationId),
|
|
38
|
+
dari_routing: routingPayload(args.routing, args.id, args.conversationId, args.private),
|
|
38
39
|
};
|
|
39
40
|
}
|
|
40
41
|
|
|
@@ -46,6 +47,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
46
47
|
source: ProviderIdentity;
|
|
47
48
|
request: InteractionRequest;
|
|
48
49
|
conversationId?: string;
|
|
50
|
+
private?: boolean;
|
|
49
51
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
50
52
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
51
53
|
// Optional terminal barrier. Streaming deltas are unaffected, but [DONE]
|
|
@@ -75,7 +77,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
75
77
|
usage: usagePayload(message, args.modelCost?.(message, true)) });
|
|
76
78
|
};
|
|
77
79
|
|
|
78
|
-
send(chunk({ completion, created, model: args.model, delta: {}, finishReason: null, routing: routingPayload(args.routing, args.id, args.conversationId) }));
|
|
80
|
+
send(chunk({ completion, created, model: args.model, delta: {}, finishReason: null, routing: routingPayload(args.routing, args.id, args.conversationId, args.private) }));
|
|
79
81
|
send(chunk({ completion, created, model: args.model, delta: { role: "assistant" }, finishReason: null }));
|
|
80
82
|
|
|
81
83
|
const argumentDeltaSeen = new Set<string>();
|
|
@@ -208,7 +210,7 @@ export function chatCompletionsSseStream(args: {
|
|
|
208
210
|
});
|
|
209
211
|
}
|
|
210
212
|
|
|
211
|
-
function routingPayload(decision: RoutingDecision, requestId: string, conversationId?: string): Record<string, unknown> {
|
|
213
|
+
function routingPayload(decision: RoutingDecision, requestId: string, conversationId?: string, isPrivate?: boolean): Record<string, unknown> {
|
|
212
214
|
return {
|
|
213
215
|
requested_model: "dari/routing",
|
|
214
216
|
selected_model: decision.selectedModel,
|
|
@@ -216,6 +218,8 @@ function routingPayload(decision: RoutingDecision, requestId: string, conversati
|
|
|
216
218
|
reason: decision.reason,
|
|
217
219
|
request_id: requestId,
|
|
218
220
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
221
|
+
// Kept out of Activity, so no link to it there.
|
|
222
|
+
...(isPrivate ? { private: true } : {}),
|
|
219
223
|
};
|
|
220
224
|
}
|
|
221
225
|
|
|
@@ -15,6 +15,7 @@ export function openAIResponsesResponse(args: {
|
|
|
15
15
|
request: InteractionRequest;
|
|
16
16
|
routing: RoutingDecision;
|
|
17
17
|
conversationId?: string;
|
|
18
|
+
private?: boolean;
|
|
18
19
|
}): Record<string, unknown> {
|
|
19
20
|
return {
|
|
20
21
|
id: openAIResponsesResponseId(args.id),
|
|
@@ -26,7 +27,7 @@ export function openAIResponsesResponse(args: {
|
|
|
26
27
|
// As on Chat Completions. Null means unpriced, never a fabricated free request.
|
|
27
28
|
usage: { ...openAIResponsesUsage(args.output), cost: args.modelCost ?? null },
|
|
28
29
|
parallel_tool_calls: args.request.parallelToolCalls ?? true,
|
|
29
|
-
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId),
|
|
30
|
+
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId, args.private),
|
|
30
31
|
};
|
|
31
32
|
}
|
|
32
33
|
import type { RoutingDecision } from "@mupt-ai/dari-router/policy-engine";
|
|
@@ -46,7 +46,8 @@ export function openAIResponsesUsage(output: Pick<InteractionOutput, "usage">):
|
|
|
46
46
|
export function openAIResponsesRouting(
|
|
47
47
|
decision: RoutingDecision,
|
|
48
48
|
requestId: string,
|
|
49
|
-
conversationId?: string
|
|
49
|
+
conversationId?: string,
|
|
50
|
+
isPrivate?: boolean
|
|
50
51
|
): Record<string, unknown> {
|
|
51
52
|
return {
|
|
52
53
|
requested_model: "dari/routing",
|
|
@@ -55,6 +56,8 @@ export function openAIResponsesRouting(
|
|
|
55
56
|
reason: decision.reason,
|
|
56
57
|
request_id: requestId,
|
|
57
58
|
...(conversationId ? { conversation_id: conversationId } : {}),
|
|
59
|
+
// Kept out of Activity, so no link to it there.
|
|
60
|
+
...(isPrivate ? { private: true } : {}),
|
|
58
61
|
};
|
|
59
62
|
}
|
|
60
63
|
|
|
@@ -29,6 +29,7 @@ type StreamArgs = {
|
|
|
29
29
|
source: ProviderIdentity;
|
|
30
30
|
request: InteractionRequest;
|
|
31
31
|
conversationId?: string;
|
|
32
|
+
private?: boolean;
|
|
32
33
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
33
34
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
34
35
|
// Optional terminal barrier. Streaming deltas are unaffected, but the
|
|
@@ -623,7 +624,7 @@ function responseEnvelope(
|
|
|
623
624
|
parallel_tool_calls: args.request.parallelToolCalls ?? true,
|
|
624
625
|
// `cost` as on Chat Completions: null means unpriced.
|
|
625
626
|
...(interaction ? { usage: { ...openAIResponsesUsage(interaction), cost: cost ?? null } } : {}),
|
|
626
|
-
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId),
|
|
627
|
+
dari_routing: openAIResponsesRouting(args.routing, args.id, args.conversationId, args.private),
|
|
627
628
|
};
|
|
628
629
|
}
|
|
629
630
|
|
|
@@ -193,6 +193,7 @@ export async function completeNonStreaming(args: {
|
|
|
193
193
|
request: args.interaction,
|
|
194
194
|
routing: choice.decision,
|
|
195
195
|
conversationId: choice.execution.conversationId,
|
|
196
|
+
private: choice.execution.private,
|
|
196
197
|
});
|
|
197
198
|
} catch (error) {
|
|
198
199
|
await reportTerminalProviderFailure({
|
|
@@ -678,6 +679,7 @@ export async function executeStreaming(args: {
|
|
|
678
679
|
source: started.providerStream.source,
|
|
679
680
|
request: args.interaction,
|
|
680
681
|
conversationId: execution.conversationId,
|
|
682
|
+
private: execution.private,
|
|
681
683
|
clientSignal: args.clientSignal,
|
|
682
684
|
modelCost: (message, failed) => args.reporter.modelCost({
|
|
683
685
|
model: args.choice.decision.selectedModel, reasoning_effort: args.choice.decision.reasoningEffort,
|
|
@@ -89,6 +89,9 @@ export type ProviderExecution = ProviderCallConfig & {
|
|
|
89
89
|
subscriptionResetMinHours?: number;
|
|
90
90
|
// The thinking the provider was sent, as X-Dari-Thinking-Level reports it.
|
|
91
91
|
thinkingLevel: string;
|
|
92
|
+
// The request stays out of Activity (ResolvedRouter.private); the response
|
|
93
|
+
// says so, for status lines that would otherwise link to it.
|
|
94
|
+
private?: boolean;
|
|
92
95
|
usageContext: RoutingUsageContext | undefined;
|
|
93
96
|
headers: Record<string, string>;
|
|
94
97
|
};
|
|
@@ -181,6 +184,7 @@ export function prepareProviderExecution(args: {
|
|
|
181
184
|
: {}),
|
|
182
185
|
...(providerKeySource === "subscription" && supplied?.account ? { subscriptionAccount: supplied.account } : {}),
|
|
183
186
|
thinkingLevel,
|
|
187
|
+
...(args.credentials.private === true ? { private: true } : {}),
|
|
184
188
|
usageContext: usageContextFor({
|
|
185
189
|
billable: args.billable,
|
|
186
190
|
credentials: args.credentials,
|
|
@@ -198,6 +202,7 @@ export function prepareProviderExecution(args: {
|
|
|
198
202
|
? {}
|
|
199
203
|
: { "X-Dari-Lease-Turns-Remaining": String(args.decision.leaseTurnsRemaining) }),
|
|
200
204
|
...(args.decision.sessionPinned === true ? { "X-Dari-Session-Pinned": "true" } : {}),
|
|
205
|
+
...(args.credentials.private === true ? { "X-Dari-Private": "true" } : {}),
|
|
201
206
|
},
|
|
202
207
|
};
|
|
203
208
|
}
|
|
@@ -76,6 +76,7 @@ export async function prepareRoutedCompletion(args: {
|
|
|
76
76
|
requestId: string;
|
|
77
77
|
clientRequestId: string | null;
|
|
78
78
|
tagsHeader: string | null;
|
|
79
|
+
privateRequest?: boolean;
|
|
79
80
|
clientHarness?: ClientHarness;
|
|
80
81
|
clientHeaders?: Record<string, string>;
|
|
81
82
|
harnessConversationIdentity?: HarnessConversationIdentity;
|
|
@@ -88,6 +89,7 @@ export async function prepareRoutedCompletion(args: {
|
|
|
88
89
|
apiKey: args.apiKey,
|
|
89
90
|
includePublicEvalCatalog: true,
|
|
90
91
|
clientHeaders: args.clientHeaders,
|
|
92
|
+
...(args.privateRequest ? { private: true } : {}),
|
|
91
93
|
};
|
|
92
94
|
// Direct /v1 callers are authenticated by API key, so their canonical
|
|
93
95
|
// organization router is not known until this resolve completes. Fingerprint
|
|
@@ -177,6 +179,8 @@ export async function prepareRoutedCompletion(args: {
|
|
|
177
179
|
const strictAccess = strictProviderAccess(credentials, roleModel.interaction);
|
|
178
180
|
const access = linkGrantAccess(strictAccess.credentials, strictAccess.interaction);
|
|
179
181
|
credentials = access.credentials;
|
|
182
|
+
// A Dari Link app's usage is read back from its requests, so they are kept.
|
|
183
|
+
if (args.privateRequest && !credentials.link_grant_id) credentials = { ...credentials, private: true };
|
|
180
184
|
// A child token's tags label every request it makes, whatever the request says.
|
|
181
185
|
Object.assign(tags, credentials.link_tags);
|
|
182
186
|
args = {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
|
|
3
3
|
import { isBackgroundRequest, type InteractionRequest } from "../interaction/types.js";
|
|
4
|
+
import { PRIVATE_HEADER } from "../platform/routing_contract.js";
|
|
4
5
|
import { explicitModelId } from "../protocol/input/router_model.js";
|
|
5
6
|
import { captureRequestError } from "../observability/errors.js";
|
|
6
7
|
import type { SelectorBackend } from "../strategies/routing.js";
|
|
@@ -42,6 +43,7 @@ export async function handleRoutedCompletion(args: {
|
|
|
42
43
|
requestId,
|
|
43
44
|
clientRequestId,
|
|
44
45
|
tagsHeader: args.request.headers.get("x-dari-tags"),
|
|
46
|
+
privateRequest: args.request.headers.get(PRIVATE_HEADER) === "true",
|
|
45
47
|
clientHarness: args.clientHarness,
|
|
46
48
|
clientHeaders: clientHeaders(args.request.headers),
|
|
47
49
|
harnessConversationIdentity: args.harnessConversationIdentity,
|
|
@@ -10,6 +10,7 @@ export type RoutedCompletionResponseArgs = {
|
|
|
10
10
|
request: InteractionRequest;
|
|
11
11
|
routing: RoutingDecision;
|
|
12
12
|
conversationId?: string;
|
|
13
|
+
private?: boolean;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
16
|
export type RoutedCompletionStreamArgs = {
|
|
@@ -20,6 +21,7 @@ export type RoutedCompletionStreamArgs = {
|
|
|
20
21
|
source: ProviderIdentity;
|
|
21
22
|
request: InteractionRequest;
|
|
22
23
|
conversationId?: string;
|
|
24
|
+
private?: boolean;
|
|
23
25
|
onFinal?: (outcome: ProviderStreamOutcome) => void;
|
|
24
26
|
modelCost?: (message: Record<string, unknown>, failed: boolean) => number | null;
|
|
25
27
|
beforeClose?: (outcome: ProviderStreamOutcome) => Promise<void> | void;
|
|
@@ -5,10 +5,11 @@ 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, HOSTED_DARI_PROVIDER } from "../platform/routing_contract.js";
|
|
8
|
+
import { DEFAULT_ROUTER_ALIAS, HOSTED_DARI_PROVIDER, PRIVATE_HEADER } from "../platform/routing_contract.js";
|
|
9
9
|
import type { Source } from "../models/sources.js";
|
|
10
10
|
import { parseGatewayConfig, type RouterConfig } from "./config.js";
|
|
11
11
|
import type { HostedSync } from "./hosted_sync.js";
|
|
12
|
+
import type { PrivateMode } from "./private_mode.js";
|
|
12
13
|
import { createSelfHostedPlatform } from "./platform.js";
|
|
13
14
|
import { gatewayKeyIndex, resolveSelfHostedRouter } from "./resolve.js";
|
|
14
15
|
|
|
@@ -28,9 +29,11 @@ import { gatewayKeyIndex, resolveSelfHostedRouter } from "./resolve.js";
|
|
|
28
29
|
* reports, goes to hosted with the user's Dari key.
|
|
29
30
|
* - The requests served here sync to hosted Activity (hosted_sync.ts), each
|
|
30
31
|
* naming the plan that served it. The plans themselves never leave this
|
|
31
|
-
* computer.
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* computer. Requests hosted Dari served are hosted's to log. Dari Link and
|
|
33
|
+
* Activity are hosted's, not served here.
|
|
34
|
+
* - In private mode (private_mode.ts) every request is private
|
|
35
|
+
* (PRIVATE_HEADER): none syncs, and hosted Dari keeps only what billing
|
|
36
|
+
* needs of those it serves.
|
|
34
37
|
*/
|
|
35
38
|
export type HostedConnection = {
|
|
36
39
|
// The hosted routing service, e.g. https://routing.dari.dev.
|
|
@@ -81,14 +84,9 @@ export function createConnectedPlatform(deps: SelfHostedPlatformDeps & {
|
|
|
81
84
|
},
|
|
82
85
|
recordRequest(record) {
|
|
83
86
|
console.info("routing request", JSON.stringify(record.summary));
|
|
84
|
-
// Hosted Dari logs the requests it served itself
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// does one whose plan is gone, since it may have been private.
|
|
88
|
-
if (record.subscriptionCredentialId !== undefined) {
|
|
89
|
-
const plan = deps.subscriptions.get(record.subscriptionCredentialId);
|
|
90
|
-
if (plan === null || plan.private) return;
|
|
91
|
-
}
|
|
87
|
+
// Hosted Dari logs the requests it served itself, and private ones
|
|
88
|
+
// stay on this computer.
|
|
89
|
+
if (record.summary.selected_provider === HOSTED_DARI_PROVIDER || record.private) return;
|
|
92
90
|
try {
|
|
93
91
|
deps.sync.enqueue(record.row);
|
|
94
92
|
} catch (error) {
|
|
@@ -185,9 +183,15 @@ export function connectedHandler(deps: {
|
|
|
185
183
|
gatewayKeys: readonly string[];
|
|
186
184
|
hosted: HostedConnection;
|
|
187
185
|
hostedRouter: HostedRouters;
|
|
186
|
+
privateMode: PrivateMode;
|
|
188
187
|
fetch: typeof fetch;
|
|
189
188
|
}): (request: Request) => Promise<Response> {
|
|
190
189
|
return async (request) => {
|
|
190
|
+
if (deps.privateMode.on) {
|
|
191
|
+
const headers = new Headers(request.headers);
|
|
192
|
+
headers.set(PRIVATE_HEADER, "true");
|
|
193
|
+
request = new Request(request, { headers });
|
|
194
|
+
}
|
|
191
195
|
const { pathname } = new URL(request.url);
|
|
192
196
|
if (pathname === "/healthz" || pathname === "/health" || pathname === "/api/hello") {
|
|
193
197
|
return await deps.gateway(request);
|
|
@@ -94,7 +94,7 @@ export class HostedSync {
|
|
|
94
94
|
const onDari = await readHostedAccounts(this.hosted, this.fetch);
|
|
95
95
|
this.accountsFailures = 0;
|
|
96
96
|
for (const plan of this.subscriptions.list()) {
|
|
97
|
-
if (plan.owner !== null || plan.
|
|
97
|
+
if (plan.owner !== null || plan.accountId === null) continue;
|
|
98
98
|
const moved = onDari.some((account) => account.provider === plan.provider
|
|
99
99
|
&& account.accountId === plan.accountId
|
|
100
100
|
&& account.connectedAt !== undefined && account.connectedAt > plan.createdAt);
|