@mupt-ai/dari-gateway 1.0.321 → 1.0.323

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.
@@ -176,10 +176,11 @@ export type LinkPlatform = {
176
176
  };
177
177
 
178
178
  /**
179
- * What the gateway's approval and connections pages need. The pages need no
180
- * account: a user connects a plan by signing in to its provider, and the
181
- * gateway remembers them in a signed cookie as a subject, so their plans are
182
- * there the next time an app asks and they manage their connections there.
179
+ * What the gateway's approval and connections pages need. The gateway
180
+ * remembers the user in a signed cookie as a subject, so their plans are there
181
+ * the next time an app asks and they manage their connections there. With
182
+ * `signIn` the subject is the account they signed in with; without it the
183
+ * pages need no account, and a user's first plan makes their subject.
183
184
  */
184
185
  export type LinkApprovalPage = {
185
186
  /**
@@ -187,6 +188,11 @@ export type LinkApprovalPage = {
187
188
  * `link_not_configured`) when the platform has none.
188
189
  */
189
190
  sessionSecret(): string;
191
+ /**
192
+ * Set on a platform whose users have accounts: the page then acts only for
193
+ * a user who signed in with one, so their plans follow them to any browser.
194
+ */
195
+ signIn?: LinkSignIn;
190
196
  /** The kinds of plan a user can connect on the page, in the order offered. */
191
197
  connectable: readonly SubscriptionKind[];
192
198
  /** The providers and gateways a user can add an API key for, in the order offered. */
@@ -230,6 +236,18 @@ export type LinkApprovalPage = {
230
236
  setBudget(subject: string, grantId: string, budget: LinkBudget[]): Promise<boolean>;
231
237
  };
232
238
 
239
+ /**
240
+ * The platform's own sign-in. The page sends the user to `url()` with `state`
241
+ * and `next`; the platform signs them in and returns them to
242
+ * `/v1/link/session` with the same `next` and a ticket for that state.
243
+ */
244
+ export type LinkSignIn = {
245
+ /** Throws HttpError(503, `link_not_configured`) when the platform has none. */
246
+ url(): string;
247
+ /** Who a ticket signed in, for which state, and when (epoch ms); null for no ticket. */
248
+ redeem(ticket: string): Promise<{ subject: string; state: string; issuedAt: number } | null>;
249
+ };
250
+
233
251
  /** A plan or API key the user added, and who they are now. */
234
252
  export type LinkPlanAdded = { subject: string; credentialId: string };
235
253
 
@@ -39,7 +39,12 @@ export function piRequestContext(headers: Headers): AgentRequestContext | undefi
39
39
  }
40
40
  const kind = headers.get("x-pi-request-kind")?.trim();
41
41
  return {
42
- requestClass: kind === "turn" ? "main" : kind === "compaction" ? "compaction" : "unknown",
42
+ requestClass: kind === "turn" ? "main"
43
+ : kind === "compaction" ? "compaction"
44
+ // The Dari extension's web search tool: one request beside the turn,
45
+ // served as background work like Claude Code's own search.
46
+ : kind === "search" ? "auxiliary"
47
+ : "unknown",
43
48
  };
44
49
  }
45
50
 
@@ -10,15 +10,10 @@ import {
10
10
  import { HttpError } from "../http/errors.js";
11
11
  import type { ResolveRouterRequest } from "../platform/platform.js";
12
12
  import { DEFAULT_ROUTER_ALIAS } from "../platform/routing_contract.js";
13
- import { codexModelConfig, subscriptionClient, subscriptionModelProvider } from "../models/subscriptions.js";
13
+ import { codexModelConfig, subscriptionModelProvider } from "../models/subscriptions.js";
14
14
  import { CANONICAL_MODELS } from "../models/canonical.js";
15
15
  import { gatewayFor } from "../models/gateways.js";
16
- import {
17
- linkGrantUnavailableError,
18
- modelNotServed,
19
- plansExhausted,
20
- plansForAnotherClient,
21
- } from "../link/errors.js";
16
+ import { linkNothingServed, plansExhausted, providersUnavailable } from "../link/errors.js";
22
17
  import { registeredModelPrice } from "../providers/pi.js";
23
18
  import type { LinkUnavailable, ProviderCredential as SuppliedProviderCredential, ProviderKeySource, ResolvedRouter } from "../types.js";
24
19
  import {
@@ -152,9 +147,15 @@ export async function resolveSelfHostedRouter(
152
147
  // A model-specific hold tries the pool's other plans before removing the
153
148
  // model. Its provider's active plan and conversation pin stay unchanged.
154
149
  const modelCredentials: Record<string, SuppliedProviderCredential> = {};
150
+ // A provider the request wanted that no granted plan served.
155
151
  const linkUnavailable = grant === null
156
152
  ? {}
157
- : grantUnavailable(granted, credentials, request.clientHarness, now);
153
+ : providersUnavailable(
154
+ granted.map((credential) => ({ kind: credential.provider, unavailableUntil: credential.unavailableUntil })),
155
+ [...credentials].filter(([, credential]) => credential === null).map(([provider]) => provider),
156
+ request.clientHarness,
157
+ now,
158
+ );
158
159
  const models = [];
159
160
  for (const model of listed) {
160
161
  const credential = credentials.get(providerForModel(model.id));
@@ -185,11 +186,7 @@ export async function resolveSelfHostedRouter(
185
186
  // A grant with a gateway key serves the gateway's models by name, listed or not.
186
187
  const gatewayKeyed = [...grantedKeys.keys()].some((provider) => gatewayFor(provider) !== undefined);
187
188
  if (models.length === 0 && !gatewayKeyed) {
188
- if (grant !== null) {
189
- throw grant.pins?.length === 1
190
- ? modelNotServed({ link_unavailable: linkUnavailable }, grant.pins[0]!.model)
191
- : linkGrantUnavailableError(linkUnavailable);
192
- }
189
+ if (grant !== null) throw linkNothingServed(linkUnavailable, grant.pins);
193
190
  const missing = [...credentials].filter(([, credential]) => credential == null).map(([provider]) => provider);
194
191
  throw unavailableRouterError(request.routerId, exhausted, missing);
195
192
  }
@@ -312,40 +309,6 @@ function grantModels(
312
309
  return models.filter((model) => pinned.has(model.id)).map((model) => ({ id: model.id }));
313
310
  }
314
311
 
315
- /**
316
- * Why each provider with a granted plan serves nothing now: its plans serve
317
- * only another client, or are all at their usage limits.
318
- */
319
- function grantUnavailable(
320
- granted: readonly SubscriptionCredential[],
321
- credentials: ReadonlyMap<string, ProviderCredential | null>,
322
- clientHarness: ResolveRouterRequest["clientHarness"],
323
- now: Date,
324
- ): Record<string, LinkUnavailable> {
325
- const unavailable: Record<string, LinkUnavailable> = {};
326
- for (const provider of new Set(granted.map((credential) => subscriptionModelProvider(credential.provider)))) {
327
- // Only a provider the request wanted and no plan served.
328
- if (credentials.get(provider) !== null) continue;
329
- const plans = granted.filter((credential) => subscriptionModelProvider(credential.provider) === provider);
330
- const usable = plans.filter((credential) => {
331
- const client = subscriptionClient(credential.provider);
332
- return client === undefined || client === clientHarness;
333
- });
334
- if (usable.length === 0) {
335
- unavailable[provider] = plansForAnotherClient(plans.map((credential) => credential.provider));
336
- continue;
337
- }
338
- const held = usable.flatMap((credential) =>
339
- credential.unavailableUntil !== null && credential.unavailableUntil > now ? [credential.unavailableUntil] : []
340
- );
341
- unavailable[provider] = plansExhausted(
342
- usable.map((credential) => credential.provider),
343
- held.length === usable.length ? new Date(Math.min(...held.map((date) => date.getTime()))) : null,
344
- );
345
- }
346
- return unavailable;
347
- }
348
-
349
312
  // Configured endpoints read their own key variable, or send a placeholder
350
313
  // when they take none; known providers use their standard variable.
351
314
  export async function configuredProviderApiKey(