@mupt-ai/dari-gateway 1.0.367 → 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/README.md +4 -3
- package/dist/link/client.d.ts +3 -1
- package/dist/link/client.js +4 -1
- package/dist/link/endpoints.js +33 -5
- 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/input/chat_completions.js +9 -13
- package/dist/protocol/input/chat_completions_validation.js +2 -7
- package/dist/protocol/input/openai_responses.js +3 -3
- 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/stub_gateway.d.ts +1 -0
- package/dist/self_host/stub_gateway.js +7 -6
- package/dist/self_host/stub_main.js +1 -1
- 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/client.ts +7 -1
- package/src/link/endpoints.ts +38 -5
- 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/input/chat_completions.ts +8 -18
- package/src/protocol/input/chat_completions_validation.ts +2 -9
- package/src/protocol/input/openai_responses.ts +3 -8
- 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/stub_gateway.ts +12 -7
- package/src/self_host/stub_main.ts +1 -1
- 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
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Database } from "bun:sqlite";
|
|
2
|
+
|
|
3
|
+
import { HttpError } from "../http/errors.js";
|
|
4
|
+
import { parseJsonRequest } from "../ingress/json.js";
|
|
5
|
+
|
|
6
|
+
/** `GET` reads private mode and `PUT {"private": true|false}` sets it. */
|
|
7
|
+
export const PRIVATE_MODE_ROUTE = "/v1/gateway/private";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Private mode of a gateway connected to hosted Dari, kept in its database:
|
|
11
|
+
* on, none of its requests reach Activity, whichever plan or Dari serves them.
|
|
12
|
+
* It sends none to hosted Activity, and asks hosted Dari to keep only what
|
|
13
|
+
* billing needs of each it serves (PRIVATE_HEADER).
|
|
14
|
+
*/
|
|
15
|
+
export class PrivateMode {
|
|
16
|
+
constructor(private readonly db: Database) {
|
|
17
|
+
db.run("CREATE TABLE IF NOT EXISTS gateway_settings (key TEXT PRIMARY KEY, value TEXT NOT NULL)");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get on(): boolean {
|
|
21
|
+
return this.db.query<{ value: string }, []>("SELECT value FROM gateway_settings WHERE key = 'private'").get()?.value === "1";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
set(on: boolean): void {
|
|
25
|
+
this.db.run(
|
|
26
|
+
"INSERT INTO gateway_settings (key, value) VALUES ('private', ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value",
|
|
27
|
+
[on ? "1" : "0"],
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function handlePrivateModeRequest(request: Request, mode: PrivateMode): Promise<Response> {
|
|
33
|
+
if (request.method === "PUT") {
|
|
34
|
+
const body = await parseJsonRequest(request);
|
|
35
|
+
if (typeof body.private !== "boolean") throw new HttpError(400, "private must be true or false.", "invalid_request_error");
|
|
36
|
+
mode.set(body.private);
|
|
37
|
+
} else if (request.method !== "GET") {
|
|
38
|
+
throw new HttpError(405, "Use GET or PUT.", "method_not_allowed");
|
|
39
|
+
}
|
|
40
|
+
return Response.json({ private: mode.on }, { headers: { "Cache-Control": "no-store" } });
|
|
41
|
+
}
|
package/src/self_host/resolve.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "@mupt-ai/dari-router/policy-engine";
|
|
11
11
|
import { HttpError } from "../http/errors.js";
|
|
12
12
|
import type { ResolveRouterRequest } from "../platform/platform.js";
|
|
13
|
-
import { DEFAULT_ROUTER_ALIAS, HOSTED_DARI_PROVIDER } from "../platform/routing_contract.js";
|
|
13
|
+
import { DEFAULT_ROUTER_ALIAS, HOSTED_DARI_PROVIDER, PRIVATE_HEADER } from "../platform/routing_contract.js";
|
|
14
14
|
import { explicitModelSlug } from "../protocol/input/router_model.js";
|
|
15
15
|
import { CLIENT_HARNESS_HEADER } from "../ingress/client_harness.js";
|
|
16
16
|
import {
|
|
@@ -461,7 +461,8 @@ function candidateConfig(
|
|
|
461
461
|
* called for `dari/model/<id>` with the user's Dari key, in Anthropic's API
|
|
462
462
|
* for Anthropic's models and OpenAI's Responses API for the rest, with the
|
|
463
463
|
* client's own identifying headers and the coding agent it named, so hosted
|
|
464
|
-
* sees the same client. Hosted Dari bills and logs what it serves
|
|
464
|
+
* sees the same client. Hosted Dari bills and logs what it serves; a private
|
|
465
|
+
* request asks it to keep only what billing needs.
|
|
465
466
|
*/
|
|
466
467
|
function hostedCandidate(hosted: HostedConnection, request: ResolveRouterRequest, modelId: string): Candidate {
|
|
467
468
|
let routerUrl = hosted.url;
|
|
@@ -475,6 +476,7 @@ function hostedCandidate(hosted: HostedConnection, request: ResolveRouterRequest
|
|
|
475
476
|
}
|
|
476
477
|
const headers: Record<string, string> = { ...request.clientHeaders };
|
|
477
478
|
if (request.clientHarness !== undefined) headers[CLIENT_HARNESS_HEADER] = request.clientHarness;
|
|
479
|
+
if (request.private === true) headers[PRIVATE_HEADER] = "true";
|
|
478
480
|
return {
|
|
479
481
|
provider: HOSTED_DARI_PROVIDER,
|
|
480
482
|
key: hosted.apiKey,
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
* `gateway.connect()` (or `POST <controlUrl>/connect`) is its user approving
|
|
23
23
|
* it: it resolves with what `DariLink.open` hands your page, `{code,
|
|
24
24
|
* codeVerifier, state}`, or rejects with the `LinkError` it would (`deny`).
|
|
25
|
-
* `clients`, `plans`, `keyProviders`, and `grantId` are `DariLink.open`'s.
|
|
25
|
+
* `clients`, `plans`, `keyProviders`, `allowPooling`, and `grantId` are `DariLink.open`'s.
|
|
26
26
|
*
|
|
27
27
|
* To drive an approval for an app you registered yourself:
|
|
28
28
|
* `gateway.approve(authorizeUrl)` (or `POST <controlUrl>/approve` with
|
|
29
29
|
* `{"url"}`) opens the approval page as the user who connected the stand-in
|
|
30
|
-
* plans,
|
|
30
|
+
* plans, keeps the plans the page ticks (or ticks `credentialIds`), approves (or
|
|
31
31
|
* denies with `deny`), and returns what the page hands the app:
|
|
32
32
|
* `{message, targetOrigin}`, the `web_message` post or the redirect's query.
|
|
33
33
|
* Each lands in `gateway.messages` and `GET <controlUrl>/messages`.
|
|
@@ -121,13 +121,16 @@ export type StubGatewayOptions = {
|
|
|
121
121
|
export type StubLinkApp = { gatewayUrl: string; clientId: string; clientSecret: string; redirectUri: string };
|
|
122
122
|
|
|
123
123
|
export type StubConnectOptions = {
|
|
124
|
-
// The plans and keys the user shares. Default:
|
|
124
|
+
// The plans and keys the user shares. Default: those the page ticks, every
|
|
125
|
+
// one the app can use (one per provider without `allowPooling`), or in
|
|
126
|
+
// update mode the ones the grant shares now.
|
|
125
127
|
credentialIds?: string[];
|
|
126
128
|
deny?: boolean;
|
|
127
129
|
// As `DariLink.open` takes them.
|
|
128
130
|
clients?: string[];
|
|
129
131
|
plans?: string[];
|
|
130
132
|
keyProviders?: string[];
|
|
133
|
+
allowPooling?: boolean;
|
|
131
134
|
grantId?: string;
|
|
132
135
|
};
|
|
133
136
|
|
|
@@ -227,6 +230,7 @@ export async function startStubGateway(options: StubGatewayOptions = {}): Promis
|
|
|
227
230
|
clients?: string[];
|
|
228
231
|
plans?: string[];
|
|
229
232
|
key_providers?: string[];
|
|
233
|
+
allow_pooling?: boolean;
|
|
230
234
|
grant_id?: string;
|
|
231
235
|
};
|
|
232
236
|
try {
|
|
@@ -236,6 +240,7 @@ export async function startStubGateway(options: StubGatewayOptions = {}): Promis
|
|
|
236
240
|
...(body.clients ? { clients: body.clients } : {}),
|
|
237
241
|
...(body.plans ? { plans: body.plans } : {}),
|
|
238
242
|
...(body.key_providers ? { keyProviders: body.key_providers } : {}),
|
|
243
|
+
...(body.allow_pooling ? { allowPooling: true } : {}),
|
|
239
244
|
...(body.grant_id ? { grantId: body.grant_id } : {}),
|
|
240
245
|
});
|
|
241
246
|
return Response.json({ code: result.code, code_verifier: result.codeVerifier, state: result.state });
|
|
@@ -445,8 +450,8 @@ ${keys.filter((key) => key === "openrouter").map((key) => ` ${key}:\n baseUr
|
|
|
445
450
|
};
|
|
446
451
|
}
|
|
447
452
|
|
|
448
|
-
// The approval page as the plans' user goes through it:
|
|
449
|
-
// approve or deny; then what the page hands the app.
|
|
453
|
+
// The approval page as the plans' user goes through it: keep the plans it
|
|
454
|
+
// ticks (or tick the given ones), approve or deny; then what the page hands the app.
|
|
450
455
|
async function driveApproval(
|
|
451
456
|
baseUrl: string,
|
|
452
457
|
cookie: string,
|
|
@@ -464,7 +469,7 @@ async function driveApproval(
|
|
|
464
469
|
+ 'A Claude Code plan serves only an app that asks for clients: ["claude_code"].',
|
|
465
470
|
);
|
|
466
471
|
}
|
|
467
|
-
const
|
|
472
|
+
const ticked = [...choose.matchAll(/name="credential_id" value="([^"]+)"[^>]* checked>/g)].map((match) => match[1]!);
|
|
468
473
|
const decided = await fetch(`${baseUrl}${url.pathname}`, {
|
|
469
474
|
method: "POST",
|
|
470
475
|
redirect: "manual",
|
|
@@ -473,7 +478,7 @@ async function driveApproval(
|
|
|
473
478
|
...params,
|
|
474
479
|
["csrf", csrf],
|
|
475
480
|
["decision", options.deny ? "deny" : "approve"],
|
|
476
|
-
...(options.credentialIds ??
|
|
481
|
+
...(options.credentialIds ?? ticked).map((id) => ["credential_id", id]),
|
|
477
482
|
]),
|
|
478
483
|
});
|
|
479
484
|
const location = decided.headers.get("location");
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* page's user's: `userCookie` is the `Cookie` header that opens the page as them. `app` is a server
|
|
12
12
|
* app registered on the gateway (`gatewayUrl`, `clientId`, `clientSecret`, `redirectUri`), and
|
|
13
13
|
* `POST <controlUrl>/connect` (with `{"credential_ids"?, "deny"?, "clients"?, "plans"?,
|
|
14
|
-
* "key_providers"?, "grant_id"?}`, all optional) is its user approving it: it answers
|
|
14
|
+
* "key_providers"?, "allow_pooling"?, "grant_id"?}`, all optional) is its user approving it: it answers
|
|
15
15
|
* `{"code", "code_verifier", "state"}`, what Connect with Dari hands your page. `GET <controlUrl>/served`
|
|
16
16
|
* answers `{"calls": [...]}`: every provider call so far, with its path,
|
|
17
17
|
* model, thinking, and client headers. For an app you registered yourself, `POST <controlUrl>/approve` with
|
|
@@ -43,7 +43,7 @@ export type SubscriptionHttpDeps = {
|
|
|
43
43
|
const BASE = "/v1/organizations/current/credentials";
|
|
44
44
|
// Subscription names are env-var-style, which keeps other credential routes
|
|
45
45
|
// such as `provider-credentials/...` out of these matches.
|
|
46
|
-
const NAMED = /^\/([A-Za-z_][A-Za-z0-9_]*)(\/label|\/mark-available|\/
|
|
46
|
+
const NAMED = /^\/([A-Za-z_][A-Za-z0-9_]*)(\/label|\/mark-available|\/resets|\/resets\/[^/]+\/use)?$/;
|
|
47
47
|
|
|
48
48
|
class HttpError extends Error {
|
|
49
49
|
constructor(readonly status: number, message: string) {
|
|
@@ -98,13 +98,6 @@ export async function handleSubscriptionRequest(
|
|
|
98
98
|
}
|
|
99
99
|
return json(200, summary(deps.store, found(renamed, name), now));
|
|
100
100
|
}
|
|
101
|
-
if (action === "/private" && request.method === "PUT") {
|
|
102
|
-
// Private plans stay off hosted Dari: a connected gateway neither
|
|
103
|
-
// serves nor reports them.
|
|
104
|
-
const body = await readBody(request);
|
|
105
|
-
if (typeof body.private !== "boolean") throw new HttpError(400, "private must be true or false.");
|
|
106
|
-
return json(200, summary(deps.store, found(deps.store.setPrivate(name, body.private, now), name), now));
|
|
107
|
-
}
|
|
108
101
|
if (action === "/mark-available" && request.method === "POST") {
|
|
109
102
|
return json(200, summary(deps.store, found(deps.store.markAvailable(name, now), name), now));
|
|
110
103
|
}
|
|
@@ -161,9 +154,6 @@ async function startSession(deps: SubscriptionHttpDeps, body: Record<string, unk
|
|
|
161
154
|
if (credentialName !== null && reconnecting?.provider !== provider) {
|
|
162
155
|
throw new HttpError(404, `Unknown subscription credential ${JSON.stringify(credentialName)}.`);
|
|
163
156
|
}
|
|
164
|
-
// A reconnected plan stays where it was unless the caller says otherwise.
|
|
165
|
-
const isPrivate = body.private ?? reconnecting?.private ?? false;
|
|
166
|
-
if (typeof isPrivate !== "boolean") throw new HttpError(400, "private must be true or false.");
|
|
167
157
|
// `dari accounts subscription connect` names the hosted plan it moves here.
|
|
168
158
|
const movingFrom = body.moving_from ?? null;
|
|
169
159
|
if (movingFrom !== null && (typeof movingFrom !== "string" || !movingFrom.trim())) {
|
|
@@ -176,7 +166,6 @@ async function startSession(deps: SubscriptionHttpDeps, body: Record<string, unk
|
|
|
176
166
|
flow: authorization.flow,
|
|
177
167
|
credentialName,
|
|
178
168
|
owner: null,
|
|
179
|
-
private: isPrivate,
|
|
180
169
|
movingFrom,
|
|
181
170
|
expiresAt: authorization.expiresAt,
|
|
182
171
|
},
|
|
@@ -224,7 +213,7 @@ async function completeSession(deps: SubscriptionHttpDeps, body: Record<string,
|
|
|
224
213
|
console.error("hosted account check failed", error instanceof Error ? error.message : String(error));
|
|
225
214
|
}
|
|
226
215
|
}
|
|
227
|
-
const stored = deps.store.connect(tokens, session.credentialName, now, null,
|
|
216
|
+
const stored = deps.store.connect(tokens, session.credentialName, now, null, elsewhere);
|
|
228
217
|
if (stored === null) {
|
|
229
218
|
throw new HttpError(404, `Unknown subscription credential ${JSON.stringify(session.credentialName)}.`);
|
|
230
219
|
}
|
|
@@ -317,8 +306,6 @@ function credentialBody(credential: SubscriptionCredential, active: boolean, now
|
|
|
317
306
|
// subscription here belongs to the gateway's one user.
|
|
318
307
|
credential_scope: "user",
|
|
319
308
|
oauth_provider: credential.provider,
|
|
320
|
-
// Kept off hosted Dari (`dari accounts subscription connect --on private`).
|
|
321
|
-
private: credential.private,
|
|
322
309
|
created_at: credential.createdAt.toISOString(),
|
|
323
310
|
last_used_at: credential.lastUsedAt?.toISOString() ?? null,
|
|
324
311
|
unavailable_until: held ? credential.unavailableUntil!.toISOString() : null,
|
|
@@ -50,9 +50,6 @@ export type SubscriptionCredential = {
|
|
|
50
50
|
// The Dari Link user who connected the plan on the approval page; null for
|
|
51
51
|
// the gateway owner's own plans, which alone serve the gateway's keys.
|
|
52
52
|
owner: string | null;
|
|
53
|
-
// Its requests stay on this computer: a gateway connected to hosted Dari
|
|
54
|
-
// never sends them to Activity.
|
|
55
|
-
private: boolean;
|
|
56
53
|
accessToken: string;
|
|
57
54
|
refreshToken: string;
|
|
58
55
|
expiresAt: Date;
|
|
@@ -82,8 +79,6 @@ export type PendingOAuthSession = {
|
|
|
82
79
|
credentialName: string | null;
|
|
83
80
|
// The Dari Link user connecting the plan; null for the gateway owner.
|
|
84
81
|
owner: string | null;
|
|
85
|
-
// Whether the plan connects as private (see SubscriptionCredential).
|
|
86
|
-
private: boolean;
|
|
87
82
|
// The plan on hosted Dari this sign-in moves here, by name: its account is
|
|
88
83
|
// not a second copy, since the caller disconnects it there next.
|
|
89
84
|
movingFrom: string | null;
|
|
@@ -112,7 +107,7 @@ export type AccountRef = {
|
|
|
112
107
|
* other computers. `name` is the plan's handle.
|
|
113
108
|
*/
|
|
114
109
|
export type ConnectedAccount = AccountRef & {
|
|
115
|
-
place: "dari" | "this_computer"
|
|
110
|
+
place: "dari" | "this_computer";
|
|
116
111
|
name: string | null;
|
|
117
112
|
// When a plan on Dari was connected; hosted Dari before this release does
|
|
118
113
|
// not say.
|
|
@@ -145,7 +140,6 @@ const PROVIDER_LABELS: Record<SubscriptionProvider, string> = {
|
|
|
145
140
|
const PLACE_NAMES: Record<ConnectedAccount["place"], string> = {
|
|
146
141
|
dari: "on Dari",
|
|
147
142
|
this_computer: "on This Computer",
|
|
148
|
-
private: "Private on This Computer",
|
|
149
143
|
};
|
|
150
144
|
|
|
151
145
|
function connectedAccountMessage(account: ConnectedAccount): string {
|
|
@@ -170,7 +164,6 @@ type CredentialRow = {
|
|
|
170
164
|
account_email: string | null;
|
|
171
165
|
account_id: string | null;
|
|
172
166
|
owner: string | null;
|
|
173
|
-
private: number | null;
|
|
174
167
|
access_token: string;
|
|
175
168
|
refresh_token: string;
|
|
176
169
|
expires_at: number;
|
|
@@ -196,7 +189,7 @@ const ADDED_COLUMNS = {
|
|
|
196
189
|
usage_windows: "TEXT",
|
|
197
190
|
model_holds: "TEXT",
|
|
198
191
|
owner: "TEXT",
|
|
199
|
-
private: "INTEGER",
|
|
192
|
+
private: "INTEGER", // read once, by clearLegacyPrivate
|
|
200
193
|
auto_redeem_resets: "INTEGER", // no longer read; resets follow each model's sources
|
|
201
194
|
} as const;
|
|
202
195
|
|
|
@@ -206,7 +199,6 @@ type SessionRow = {
|
|
|
206
199
|
flow_json: string;
|
|
207
200
|
credential_name: string | null;
|
|
208
201
|
owner: string | null;
|
|
209
|
-
private: number | null;
|
|
210
202
|
moving_from: string | null;
|
|
211
203
|
expires_at: number;
|
|
212
204
|
};
|
|
@@ -260,14 +252,13 @@ export class SubscriptionStore {
|
|
|
260
252
|
flow_json TEXT NOT NULL,
|
|
261
253
|
credential_name TEXT,
|
|
262
254
|
owner TEXT,
|
|
263
|
-
private INTEGER,
|
|
264
255
|
moving_from TEXT,
|
|
265
256
|
expires_at INTEGER NOT NULL
|
|
266
257
|
)`);
|
|
267
258
|
const sessionColumns = new Set(
|
|
268
259
|
db.query<{ name: string }, []>("PRAGMA table_info(subscription_oauth_sessions)").all().map((column) => column.name),
|
|
269
260
|
);
|
|
270
|
-
for (const [column, type] of [["owner", "TEXT"], ["
|
|
261
|
+
for (const [column, type] of [["owner", "TEXT"], ["moving_from", "TEXT"]] as const) {
|
|
271
262
|
if (!sessionColumns.has(column)) db.run(`ALTER TABLE subscription_oauth_sessions ADD COLUMN ${column} ${type}`);
|
|
272
263
|
}
|
|
273
264
|
this.encryptStored();
|
|
@@ -472,7 +463,6 @@ export class SubscriptionStore {
|
|
|
472
463
|
reconnectName: string | null,
|
|
473
464
|
now = new Date(),
|
|
474
465
|
owner: string | null = null,
|
|
475
|
-
isPrivate = false,
|
|
476
466
|
elsewhere: ConnectedAccount[] = [],
|
|
477
467
|
): SubscriptionCredential | null {
|
|
478
468
|
const connected = this.db.transaction(() => {
|
|
@@ -490,7 +480,7 @@ export class SubscriptionStore {
|
|
|
490
480
|
`UPDATE subscription_credentials SET access_token = ?, refresh_token = ?, expires_at = ?,
|
|
491
481
|
account_id = ?, account_email = ?, unavailable_until = ?, unavailable_reason = ?,
|
|
492
482
|
usage_utilization = ?, usage_resets_at = ?, usage_observed_at = ?, usage_windows = ?, model_holds = ?,
|
|
493
|
-
|
|
483
|
+
updated_at = ?
|
|
494
484
|
WHERE id = ?`,
|
|
495
485
|
[
|
|
496
486
|
this.seal(tokens.accessToken, tokenPlace(existing.id, "access_token")),
|
|
@@ -505,7 +495,6 @@ export class SubscriptionStore {
|
|
|
505
495
|
usage?.observedAt.getTime() ?? null,
|
|
506
496
|
usage === null ? null : windowsJson(usage),
|
|
507
497
|
sameAccount ? modelHoldsJson(existing.modelHolds) : null,
|
|
508
|
-
isPrivate ? 1 : 0,
|
|
509
498
|
now.getTime(),
|
|
510
499
|
existing.id,
|
|
511
500
|
],
|
|
@@ -522,9 +511,9 @@ export class SubscriptionStore {
|
|
|
522
511
|
.query<{ next: number }, []>("SELECT COALESCE(MAX(position), 0) + 1 AS next FROM subscription_credentials")
|
|
523
512
|
.get()!.next;
|
|
524
513
|
this.db.run(
|
|
525
|
-
`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner,
|
|
514
|
+
`INSERT INTO subscription_credentials (id, provider, name, account_email, account_id, owner,
|
|
526
515
|
access_token, refresh_token, expires_at, position, created_at, updated_at)
|
|
527
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
|
|
516
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
528
517
|
[
|
|
529
518
|
id,
|
|
530
519
|
tokens.provider,
|
|
@@ -532,7 +521,6 @@ export class SubscriptionStore {
|
|
|
532
521
|
tokens.accountEmail,
|
|
533
522
|
tokens.accountId,
|
|
534
523
|
owner,
|
|
535
|
-
isPrivate ? 1 : 0,
|
|
536
524
|
this.seal(tokens.accessToken, tokenPlace(id, "access_token")),
|
|
537
525
|
this.seal(tokens.refreshToken, tokenPlace(id, "refresh_token")),
|
|
538
526
|
tokens.expiresAt.getTime(),
|
|
@@ -556,7 +544,7 @@ export class SubscriptionStore {
|
|
|
556
544
|
provider: local.provider,
|
|
557
545
|
accountId: local.accountId,
|
|
558
546
|
accountEmail: local.accountEmail,
|
|
559
|
-
place:
|
|
547
|
+
place: "this_computer",
|
|
560
548
|
name: local.name,
|
|
561
549
|
}));
|
|
562
550
|
}
|
|
@@ -600,16 +588,12 @@ export class SubscriptionStore {
|
|
|
600
588
|
return this.get(credential.id);
|
|
601
589
|
}
|
|
602
590
|
|
|
603
|
-
/**
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
now.getTime(),
|
|
610
|
-
credential.id,
|
|
611
|
-
]);
|
|
612
|
-
return this.get(credential.id);
|
|
591
|
+
/**
|
|
592
|
+
* Clear the marks of plans connected as private, before private mode
|
|
593
|
+
* replaced them; true when any was marked.
|
|
594
|
+
*/
|
|
595
|
+
clearLegacyPrivate(): boolean {
|
|
596
|
+
return this.db.run("UPDATE subscription_credentials SET private = NULL WHERE private = 1").changes > 0;
|
|
613
597
|
}
|
|
614
598
|
|
|
615
599
|
/** Remove a subscription. Returns what was removed, or null when it was not connected. */
|
|
@@ -678,15 +662,14 @@ export class SubscriptionStore {
|
|
|
678
662
|
const token = randomBytes(32).toString("base64url");
|
|
679
663
|
this.db.run("DELETE FROM subscription_oauth_sessions WHERE expires_at <= ?", [now.getTime()]);
|
|
680
664
|
this.db.run(
|
|
681
|
-
`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner,
|
|
682
|
-
VALUES (?, ?, ?, ?, ?, ?,
|
|
665
|
+
`INSERT INTO subscription_oauth_sessions (token, provider, flow_json, credential_name, owner, moving_from, expires_at)
|
|
666
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
|
683
667
|
[
|
|
684
668
|
token,
|
|
685
669
|
session.provider,
|
|
686
670
|
this.seal(JSON.stringify(session.flow), flowPlace(token)),
|
|
687
671
|
session.credentialName,
|
|
688
672
|
session.owner,
|
|
689
|
-
session.private ? 1 : 0,
|
|
690
673
|
session.movingFrom,
|
|
691
674
|
session.expiresAt.getTime(),
|
|
692
675
|
],
|
|
@@ -706,7 +689,6 @@ export class SubscriptionStore {
|
|
|
706
689
|
flow: JSON.parse(this.open(row.flow_json, flowPlace(row.token))) as Record<string, string>,
|
|
707
690
|
credentialName: row.credential_name,
|
|
708
691
|
owner: row.owner,
|
|
709
|
-
private: row.private === 1,
|
|
710
692
|
movingFrom: row.moving_from,
|
|
711
693
|
expiresAt: new Date(row.expires_at),
|
|
712
694
|
};
|
|
@@ -820,7 +802,6 @@ function credentialFromRow(row: CredentialRow): SubscriptionCredential {
|
|
|
820
802
|
accountEmail: row.account_email,
|
|
821
803
|
accountId: row.account_id,
|
|
822
804
|
owner: row.owner,
|
|
823
|
-
private: row.private === 1,
|
|
824
805
|
accessToken: row.access_token,
|
|
825
806
|
refreshToken: row.refresh_token,
|
|
826
807
|
expiresAt: new Date(row.expires_at),
|
package/src/telemetry/report.ts
CHANGED
|
@@ -650,6 +650,7 @@ export class RequestReporter {
|
|
|
650
650
|
gatewayPlatform().recordRequest({
|
|
651
651
|
...(link === undefined ? {} : { link }),
|
|
652
652
|
...(subscriptionCredentialId === undefined ? {} : { subscriptionCredentialId }),
|
|
653
|
+
private: a.credentials.private === true,
|
|
653
654
|
router: requestRecordRouter(a.credentials),
|
|
654
655
|
candidates: a.candidates.map(({ model, reasoningEffort }) => ({ model, reasoningEffort })),
|
|
655
656
|
...(a.sessionPinKey === undefined ? {} : { sessionPinKey: a.sessionPinKey }),
|
package/src/types.ts
CHANGED
|
@@ -206,6 +206,9 @@ export type ResolvedRouter = {
|
|
|
206
206
|
// Tags a Dari Link child token carries onto every request it makes.
|
|
207
207
|
link_tags?: Record<string, string> | null;
|
|
208
208
|
zdr?: boolean;
|
|
209
|
+
// The request asked to stay out of Activity (`x-dari-private: true`): the
|
|
210
|
+
// platform records only what billing needs, and the response says so.
|
|
211
|
+
private?: boolean;
|
|
209
212
|
allow_long_context?: boolean;
|
|
210
213
|
speculative_routing?: boolean;
|
|
211
214
|
// Route each session's (and each subagent's) first turn, then serve that
|