@mupt-ai/dari-gateway 1.0.358 → 1.0.360

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.
@@ -54,6 +54,8 @@ export type ProviderEndpointConfig = {
54
54
  baseUrl: string;
55
55
  api: string;
56
56
  apiKeyEnv?: string;
57
+ contextWindow?: number;
58
+ maxOutputTokens?: number;
57
59
  };
58
60
  export type RouterConfig = {
59
61
  models: RouterModelConfig[];
@@ -95,7 +95,21 @@ function parseProvider(id, raw) {
95
95
  if (raw.apiKeyEnv !== undefined && (typeof raw.apiKeyEnv !== "string" || !raw.apiKeyEnv.trim())) {
96
96
  throw new Error(`providers.${id}.apiKeyEnv must name an environment variable.`);
97
97
  }
98
+ const limits = {};
99
+ for (const field of ["contextWindow", "maxOutputTokens"]) {
100
+ const n = raw[field];
101
+ if (n === undefined)
102
+ continue;
103
+ if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 1) {
104
+ throw new Error(`providers.${id}.${field} must be a positive token count.`);
105
+ }
106
+ limits[field] = n;
107
+ }
108
+ if (limits.contextWindow && limits.maxOutputTokens && limits.maxOutputTokens > limits.contextWindow) {
109
+ throw new Error(`providers.${id}.maxOutputTokens exceeds the context window.`);
110
+ }
98
111
  return {
112
+ ...limits,
99
113
  baseUrl: raw.baseUrl.replace(/\/+$/, ""),
100
114
  api: typeof raw.api === "string" ? raw.api.trim() : "openai-completions",
101
115
  ...(typeof raw.apiKeyEnv === "string" ? { apiKeyEnv: raw.apiKeyEnv.trim() } : {}),
@@ -46,8 +46,9 @@ export declare class HostedSync {
46
46
  private call;
47
47
  }
48
48
  /**
49
- * The user's plans hosted Dari holds on other computers and on Dari, for the
50
- * check before a sign-in connects an account: `GET /v1/gateway/accounts`
51
- * returns each one's provider, account identity, and where it lives, never a token.
49
+ * The user's plans on hosted Dari, for the check before a sign-in connects an
50
+ * account: `GET /v1/gateway/accounts` returns each one's provider, account
51
+ * identity, and name, never a token. Other computers may hold the same
52
+ * account, so only Dari's plans count.
52
53
  */
53
54
  export declare function readHostedAccounts(hosted: HostedConnection, fetchImpl: typeof globalThis.fetch): Promise<ConnectedAccount[]>;
@@ -194,12 +194,16 @@ export class HostedSync {
194
194
  }
195
195
  }
196
196
  /**
197
- * The user's plans hosted Dari holds on other computers and on Dari, for the
198
- * check before a sign-in connects an account: `GET /v1/gateway/accounts`
199
- * returns each one's provider, account identity, and where it lives, never a token.
197
+ * The user's plans on hosted Dari, for the check before a sign-in connects an
198
+ * account: `GET /v1/gateway/accounts` returns each one's provider, account
199
+ * identity, and name, never a token. Other computers may hold the same
200
+ * account, so only Dari's plans count.
200
201
  */
201
202
  export async function readHostedAccounts(hosted, fetchImpl) {
202
- const response = await fetchImpl(`${hosted.url}/v1/gateway/accounts?gateway_id=${encodeURIComponent(hosted.gatewayId)}`, {
203
+ const response = await fetchImpl(
204
+ // Hosted Dari before this release requires gateway_id; it leaves this
205
+ // gateway's own plans out.
206
+ `${hosted.url}/v1/gateway/accounts?gateway_id=${encodeURIComponent(hosted.gatewayId)}`, {
203
207
  headers: { authorization: `Bearer ${hosted.apiKey}` },
204
208
  signal: AbortSignal.timeout(30_000),
205
209
  });
@@ -208,20 +212,26 @@ export async function readHostedAccounts(hosted, fetchImpl) {
208
212
  const body = await response.json();
209
213
  if (!Array.isArray(body.accounts))
210
214
  throw new Error("hosted accounts response has no accounts list");
211
- return body.accounts.map((entry) => {
215
+ const accounts = [];
216
+ for (const entry of body.accounts) {
217
+ // Hosted Dari before this release also listed other computers' plans.
218
+ if (entry.where !== "dari")
219
+ continue;
212
220
  if (!SUBSCRIPTION_PROVIDERS.includes(entry.provider)) {
213
221
  throw new Error(`hosted accounts response names an unknown provider ${JSON.stringify(entry.provider)}`);
214
222
  }
215
- const dari = entry.where === "dari";
216
- return {
217
- provider: entry.provider,
218
- accountId: typeof entry.account_id === "string" ? entry.account_id : null,
219
- accountEmail: typeof entry.account_email === "string" ? entry.account_email : null,
220
- place: dari ? "dari" : "other_computer",
221
- name: typeof entry.name === "string" ? entry.name : null,
222
- gatewayName: typeof entry.gateway_name === "string" ? entry.gateway_name : null,
223
- };
224
- });
223
+ let accountId = null;
224
+ if (typeof entry.account_id === "string")
225
+ accountId = entry.account_id;
226
+ let accountEmail = null;
227
+ if (typeof entry.account_email === "string")
228
+ accountEmail = entry.account_email;
229
+ let name = null;
230
+ if (typeof entry.name === "string")
231
+ name = entry.name;
232
+ accounts.push({ provider: entry.provider, accountId, accountEmail, place: "dari", name });
233
+ }
234
+ return accounts;
225
235
  }
226
236
  function backoff(failures) {
227
237
  return Math.min(RETRY_MAX_MS, RETRY_BASE_MS * 2 ** Math.max(0, failures - 1));
@@ -207,6 +207,8 @@ export async function startSelfHostedGateway(env = process.env) {
207
207
  auto_routing: autoRoutingKey ? "dari" : "priority",
208
208
  ...(hosted === null ? {} : { connected: { hosted_url: hosted.url, gateway_id: hosted.gatewayId } }),
209
209
  }));
210
+ if (env.DARI_SESSION_PORT_FILE)
211
+ await Bun.write(env.DARI_SESSION_PORT_FILE, String(server.port));
210
212
  sync?.start();
211
213
  const idleUsage = sync === null ? null : setInterval(() => {
212
214
  void refreshStaleUsage(subscriptions, { fetch, baseUrls });
@@ -325,9 +325,14 @@ function candidateConfig(deps, modelId, candidate) {
325
325
  }
326
326
  const served = providerModel(modelId, provider);
327
327
  const endpoint = deps.config.providers[provider];
328
+ const limits = {
329
+ ...(endpoint?.contextWindow !== undefined ? { context_window: endpoint.contextWindow } : {}),
330
+ ...(endpoint?.maxOutputTokens !== undefined ? { max_output_tokens: endpoint.maxOutputTokens } : {}),
331
+ };
328
332
  if (served !== undefined) {
329
333
  return {
330
334
  provider,
335
+ ...limits,
331
336
  native_model_id: served.native_model_id,
332
337
  api: endpoint?.api ?? served.api,
333
338
  base_url: endpoint?.baseUrl ?? served.base_url,
@@ -338,6 +343,7 @@ function candidateConfig(deps, modelId, candidate) {
338
343
  return undefined;
339
344
  return {
340
345
  provider,
346
+ ...limits,
341
347
  native_model_id: nativeModelId(modelId),
342
348
  api: endpoint.api,
343
349
  base_url: endpoint.baseUrl,
@@ -74,14 +74,13 @@ export type AccountRef = {
74
74
  accountEmail: string | null;
75
75
  };
76
76
  /**
77
- * A plan of the same user that already holds an account, wherever it lives.
78
- * `name` is the plan's handle where it is known; `gatewayName` names the other
79
- * computer for an `other_computer` plan.
77
+ * A plan of the same user that already holds an account: on hosted Dari, or
78
+ * on this computer. The same account may also be on any number of the user's
79
+ * other computers. `name` is the plan's handle.
80
80
  */
81
81
  export type ConnectedAccount = AccountRef & {
82
- place: "dari" | "this_computer" | "private" | "other_computer";
82
+ place: "dari" | "this_computer" | "private";
83
83
  name: string | null;
84
- gatewayName: string | null;
85
84
  };
86
85
  /** Connecting an account the same user already has connected; the message says where it lives and how to move it. */
87
86
  export declare class SubscriptionAccountConnectedError extends Error {
@@ -40,8 +40,6 @@ function connectedAccountMessage(account) {
40
40
  return `This ${label} account is already connected on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\`, or disconnect it first.`;
41
41
  case "private":
42
42
  return `This ${label} account is already connected Private on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\` or \`--on computer\`, or disconnect it first.`;
43
- case "other_computer":
44
- return `This ${label} account is already connected on another computer${account.gatewayName ? ` (${account.gatewayName})` : ""}. Disconnect it there, or move it to Dari there with \`dari accounts subscription move\`.`;
45
43
  }
46
44
  }
47
45
  const BASE_NAMES = {
@@ -376,7 +374,6 @@ export class SubscriptionStore {
376
374
  accountEmail: local.accountEmail,
377
375
  place: local.private ? "private" : "this_computer",
378
376
  name: local.name,
379
- gatewayName: null,
380
377
  }));
381
378
  }
382
379
  const other = elsewhere.find((account) => sameConnectedAccount(account, tokens));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mupt-ai/dari-gateway",
3
- "version": "1.0.358",
3
+ "version": "1.0.360",
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.358",
55
+ "@mupt-ai/dari-router": "1.0.360",
56
56
  "@mupt-ai/pi-ai": "1.5.2-mupt.3"
57
57
  },
58
58
  "devDependencies": {
@@ -74,6 +74,8 @@ export type ProviderEndpointConfig = {
74
74
  api: string;
75
75
  // Environment variable holding the endpoint's API key; absent sends none.
76
76
  apiKeyEnv?: string;
77
+ contextWindow?: number;
78
+ maxOutputTokens?: number;
77
79
  };
78
80
 
79
81
  export type RouterConfig = {
@@ -205,7 +207,20 @@ function parseProvider(id: string, raw: unknown): ProviderEndpointConfig {
205
207
  if (raw.apiKeyEnv !== undefined && (typeof raw.apiKeyEnv !== "string" || !raw.apiKeyEnv.trim())) {
206
208
  throw new Error(`providers.${id}.apiKeyEnv must name an environment variable.`);
207
209
  }
210
+ const limits: Pick<ProviderEndpointConfig, "contextWindow" | "maxOutputTokens"> = {};
211
+ for (const field of ["contextWindow", "maxOutputTokens"] as const) {
212
+ const n = raw[field];
213
+ if (n === undefined) continue;
214
+ if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 1) {
215
+ throw new Error(`providers.${id}.${field} must be a positive token count.`);
216
+ }
217
+ limits[field] = n;
218
+ }
219
+ if (limits.contextWindow && limits.maxOutputTokens && limits.maxOutputTokens > limits.contextWindow) {
220
+ throw new Error(`providers.${id}.maxOutputTokens exceeds the context window.`);
221
+ }
208
222
  return {
223
+ ...limits,
209
224
  baseUrl: raw.baseUrl.replace(/\/+$/, ""),
210
225
  api: typeof raw.api === "string" ? raw.api.trim() : "openai-completions",
211
226
  ...(typeof raw.apiKeyEnv === "string" ? { apiKeyEnv: raw.apiKeyEnv.trim() } : {}),
@@ -212,15 +212,18 @@ export class HostedSync {
212
212
  }
213
213
 
214
214
  /**
215
- * The user's plans hosted Dari holds on other computers and on Dari, for the
216
- * check before a sign-in connects an account: `GET /v1/gateway/accounts`
217
- * returns each one's provider, account identity, and where it lives, never a token.
215
+ * The user's plans on hosted Dari, for the check before a sign-in connects an
216
+ * account: `GET /v1/gateway/accounts` returns each one's provider, account
217
+ * identity, and name, never a token. Other computers may hold the same
218
+ * account, so only Dari's plans count.
218
219
  */
219
220
  export async function readHostedAccounts(
220
221
  hosted: HostedConnection,
221
222
  fetchImpl: typeof globalThis.fetch,
222
223
  ): Promise<ConnectedAccount[]> {
223
224
  const response = await fetchImpl(
225
+ // Hosted Dari before this release requires gateway_id; it leaves this
226
+ // gateway's own plans out.
224
227
  `${hosted.url}/v1/gateway/accounts?gateway_id=${encodeURIComponent(hosted.gatewayId)}`,
225
228
  {
226
229
  headers: { authorization: `Bearer ${hosted.apiKey}` },
@@ -230,20 +233,22 @@ export async function readHostedAccounts(
230
233
  if (!response.ok) throw new Error(`HTTP ${response.status}: ${(await response.text()).slice(0, 300)}`);
231
234
  const body = await response.json() as { accounts?: unknown };
232
235
  if (!Array.isArray(body.accounts)) throw new Error("hosted accounts response has no accounts list");
233
- return body.accounts.map((entry: Record<string, unknown>) => {
236
+ const accounts: ConnectedAccount[] = [];
237
+ for (const entry of body.accounts as Array<Record<string, unknown>>) {
238
+ // Hosted Dari before this release also listed other computers' plans.
239
+ if (entry.where !== "dari") continue;
234
240
  if (!SUBSCRIPTION_PROVIDERS.includes(entry.provider as SubscriptionProvider)) {
235
241
  throw new Error(`hosted accounts response names an unknown provider ${JSON.stringify(entry.provider)}`);
236
242
  }
237
- const dari = entry.where === "dari";
238
- return {
239
- provider: entry.provider as SubscriptionProvider,
240
- accountId: typeof entry.account_id === "string" ? entry.account_id : null,
241
- accountEmail: typeof entry.account_email === "string" ? entry.account_email : null,
242
- place: dari ? "dari" : "other_computer",
243
- name: typeof entry.name === "string" ? entry.name : null,
244
- gatewayName: typeof entry.gateway_name === "string" ? entry.gateway_name : null,
245
- };
246
- });
243
+ let accountId: string | null = null;
244
+ if (typeof entry.account_id === "string") accountId = entry.account_id;
245
+ let accountEmail: string | null = null;
246
+ if (typeof entry.account_email === "string") accountEmail = entry.account_email;
247
+ let name: string | null = null;
248
+ if (typeof entry.name === "string") name = entry.name;
249
+ accounts.push({ provider: entry.provider as SubscriptionProvider, accountId, accountEmail, place: "dari", name });
250
+ }
251
+ return accounts;
247
252
  }
248
253
 
249
254
  function backoff(failures: number): number {
@@ -234,6 +234,7 @@ export async function startSelfHostedGateway(env: Record<string, string> = proce
234
234
  ...(hosted === null ? {} : { connected: { hosted_url: hosted.url, gateway_id: hosted.gatewayId } }),
235
235
  }),
236
236
  );
237
+ if (env.DARI_SESSION_PORT_FILE) await Bun.write(env.DARI_SESSION_PORT_FILE, String(server.port));
237
238
  sync?.start();
238
239
  const idleUsage = sync === null ? null : setInterval(() => {
239
240
  void refreshStaleUsage(subscriptions, { fetch, baseUrls });
@@ -400,9 +400,14 @@ function candidateConfig(
400
400
  }
401
401
  const served = providerModel(modelId, provider);
402
402
  const endpoint = deps.config.providers[provider];
403
+ const limits = {
404
+ ...(endpoint?.contextWindow !== undefined ? { context_window: endpoint.contextWindow } : {}),
405
+ ...(endpoint?.maxOutputTokens !== undefined ? { max_output_tokens: endpoint.maxOutputTokens } : {}),
406
+ };
403
407
  if (served !== undefined) {
404
408
  return {
405
409
  provider,
410
+ ...limits,
406
411
  native_model_id: served.native_model_id,
407
412
  api: endpoint?.api ?? served.api,
408
413
  base_url: endpoint?.baseUrl ?? served.base_url,
@@ -412,6 +417,7 @@ function candidateConfig(
412
417
  if (endpoint === undefined) return undefined;
413
418
  return {
414
419
  provider,
420
+ ...limits,
415
421
  native_model_id: nativeModelId(modelId),
416
422
  api: endpoint.api,
417
423
  base_url: endpoint.baseUrl,
@@ -98,14 +98,13 @@ export type AccountRef = {
98
98
  };
99
99
 
100
100
  /**
101
- * A plan of the same user that already holds an account, wherever it lives.
102
- * `name` is the plan's handle where it is known; `gatewayName` names the other
103
- * computer for an `other_computer` plan.
101
+ * A plan of the same user that already holds an account: on hosted Dari, or
102
+ * on this computer. The same account may also be on any number of the user's
103
+ * other computers. `name` is the plan's handle.
104
104
  */
105
105
  export type ConnectedAccount = AccountRef & {
106
- place: "dari" | "this_computer" | "private" | "other_computer";
106
+ place: "dari" | "this_computer" | "private";
107
107
  name: string | null;
108
- gatewayName: string | null;
109
108
  };
110
109
 
111
110
  /** Connecting an account the same user already has connected; the message says where it lives and how to move it. */
@@ -142,8 +141,6 @@ function connectedAccountMessage(account: ConnectedAccount): string {
142
141
  return `This ${label} account is already connected on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\`, or disconnect it first.`;
143
142
  case "private":
144
143
  return `This ${label} account is already connected Private on This Computer${asName}. Move it with \`dari accounts subscription move ${handle} --on dari\` or \`--on computer\`, or disconnect it first.`;
145
- case "other_computer":
146
- return `This ${label} account is already connected on another computer${account.gatewayName ? ` (${account.gatewayName})` : ""}. Disconnect it there, or move it to Dari there with \`dari accounts subscription move\`.`;
147
144
  }
148
145
  }
149
146
 
@@ -572,7 +569,6 @@ export class SubscriptionStore {
572
569
  accountEmail: local.accountEmail,
573
570
  place: local.private ? "private" : "this_computer",
574
571
  name: local.name,
575
- gatewayName: null,
576
572
  }));
577
573
  }
578
574
  const other = elsewhere.find((account) => sameConnectedAccount(account, tokens));