dsh-magpie-connect 0.3.4 → 0.3.6

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/lib/index.js CHANGED
@@ -109,7 +109,13 @@ var staticMagpieModels = [
109
109
  var FETCH_TIMEOUT_MS = 3e4;
110
110
  var ModelCatalog = class {
111
111
  #entries = /* @__PURE__ */ new Map();
112
- #hidden = /* @__PURE__ */ new Set();
112
+ /**
113
+ * Allowlist of model ids the picker offers. `null` = inherit the whole
114
+ * directory (the default, and what a fresh install gets); a Set (possibly
115
+ * empty) = exactly these ids. Mirrors DSH's own Models settings, where a
116
+ * provider's `models` array replaces the catalog instead of filtering it.
117
+ */
118
+ #enabled = null;
113
119
  #updatedAt = 0;
114
120
  #lastError = "";
115
121
  #refreshSeconds;
@@ -226,6 +232,32 @@ var ModelCatalog = class {
226
232
  #authHeaders() {
227
233
  return this.#apiKey === "" ? {} : { authorization: `Bearer ${this.#apiKey}` };
228
234
  }
235
+ /**
236
+ * Adopt a directory the settings page just read from the gateway.
237
+ *
238
+ * The fetch dialog asks the endpoint the form shows and gets the freshest
239
+ * answer the session will see; the background refresh loop may be minutes
240
+ * behind, or silently failing on a 401. Without this, a model the gateway has
241
+ * dropped stays in the local directory — and therefore in the picker and the
242
+ * settings list — until the next *successful* refresh, which may never come.
243
+ *
244
+ * Whole-set replacement, never a merge: the reply is the gateway's entire
245
+ * catalog, so anything it no longer lists is gone. Empty input is refused
246
+ * rather than treated as "nothing served", because a bad endpoint or a
247
+ * transient failure must not blank the picker.
248
+ * @param entries - the decoded gateway directory just fetched.
249
+ * @returns whether the directory was adopted.
250
+ */
251
+ adoptDirectory(entries) {
252
+ if (entries.size === 0) return false;
253
+ this.#entries = new Map(entries);
254
+ this.#updatedAt = this.#now();
255
+ this.#lastError = "";
256
+ if (this.#cachePath) void saveModelsCache(this.#cachePath, entries, this.#now()).catch(() => {
257
+ });
258
+ this.#announce();
259
+ return true;
260
+ }
229
261
  async refreshModels() {
230
262
  if (!this.configured) {
231
263
  this.#lastError = "magpie gateway API URL or key is not configured \u2014 set both on the Magpie settings page";
@@ -254,37 +286,44 @@ var ModelCatalog = class {
254
286
  return this.#entries.get(model);
255
287
  }
256
288
  /**
257
- * Replace the settings-page hidden set (models excluded from the picker).
258
- * Announces: hiding/showing models changes the exposed list, and DSH's
259
- * picker only re-reads it on `llm/adapters-updated`.
289
+ * Replace the settings-page allowlist (`null` = inherit the whole directory).
290
+ * Announces: the exposed set changes, and DSH's picker only re-reads it on
291
+ * `llm/adapters-updated`.
292
+ * @param ids - the allowed model ids, or `null` to inherit.
260
293
  */
261
- setHidden(ids) {
262
- const next = new Set(ids);
263
- if (next.size === this.#hidden.size && [...next].every((id) => this.#hidden.has(id))) return;
264
- this.#hidden = next;
294
+ setEnabled(ids) {
295
+ const next = ids === null ? null : new Set(ids);
296
+ if (this.#sameSet(next)) return;
297
+ this.#enabled = next;
265
298
  this.#announce();
266
299
  }
267
- /** Currently hidden model ids. */
268
- hidden() {
269
- return [...this.#hidden];
300
+ /** The allowlist as stored: `null` when inheriting. */
301
+ enabled() {
302
+ return this.#enabled === null ? null : [...this.#enabled];
303
+ }
304
+ #sameSet(next) {
305
+ const current = this.#enabled;
306
+ if (next === null || current === null) return next === current;
307
+ return next.size === current.size && [...next].every((id) => current.has(id));
308
+ }
309
+ /** Whether the picker offers one id under the current allowlist. */
310
+ #offers(model) {
311
+ return this.#enabled === null || this.#enabled.has(model);
270
312
  }
271
313
  /**
272
- * Drop hidden ids the gateway no longer serves.
273
- *
274
- * The hidden set is only meaningful against a live directory: keeping an id
275
- * that has since left `GET /models` would make the settings page carry rows
276
- * nobody can act on, and a model that later returns would come back
277
- * invisibly hidden for no reason the user can see. Called on save, when the
278
- * directory is the freshest the session has.
279
- * @param ids - the hidden set as submitted.
280
- * @returns the subset still worth storing.
314
+ * Where one model id comes from right now: the live directory, the built-in
315
+ * snapshot, or nothing. The settings page labels its rows with this so a
316
+ * model the gateway stopped listing is not mistaken for one it still serves.
317
+ * @param model - the id to classify.
318
+ * @returns `'gateway'`, `'static'`, or `'none'`.
281
319
  */
282
- pruneHidden(ids) {
283
- if (!this.configured || this.#entries.size === 0) return [...ids];
284
- return ids.filter((id) => this.#entries.has(id));
320
+ sourceFor(model) {
321
+ if (this.#entries.has(model)) return "gateway";
322
+ if (this.configured && staticMagpieModels.some((entry) => entry.id === model)) return "static";
323
+ return "none";
285
324
  }
286
- isHidden(model) {
287
- return this.#hidden.has(model);
325
+ isEnabled(model) {
326
+ return this.#offers(model);
288
327
  }
289
328
  /**
290
329
  * Point the refresh loop at another API root or credential (settings page
@@ -324,7 +363,7 @@ var ModelCatalog = class {
324
363
  }
325
364
  decision(model) {
326
365
  if (!this.configured) return { allowed: false, source: "unconfigured", known: false };
327
- if (this.#hidden.has(model)) return { allowed: false, source: "hidden_by_settings", known: true };
366
+ if (!this.#offers(model)) return { allowed: false, source: "not_enabled_by_settings", known: true };
328
367
  if (this.#entries.has(model)) return { allowed: true, source: "gateway", known: true };
329
368
  const fallback = staticMagpieModels.find((entry) => entry.id === model);
330
369
  if (fallback) return { allowed: true, source: "static_verified", known: false };
@@ -379,11 +418,23 @@ var ModelCatalog = class {
379
418
  maxTokensFor(model) {
380
419
  return this.#entryFor(model)?.maxTokens ?? 32768;
381
420
  }
382
- /** ids exposed to DSH: live gateway list minus hidden (empty while unconfigured). */
421
+ /**
422
+ * Every id the directory knows right now, ignoring the allowlist: the live
423
+ * gateway list, else the cached copy, else the built-in snapshot.
424
+ *
425
+ * This is what the settings page renders (each row then says whether the
426
+ * allowlist offers it) and what the one-time migration intersects against.
427
+ */
428
+ directory() {
429
+ if (!this.configured) return [];
430
+ if (this.#entries.size === 0) return staticMagpieModels.map((entry) => entry.id);
431
+ return [...this.#entries.keys()].sort();
432
+ }
433
+ /** ids exposed to DSH: the directory filtered by the allowlist (empty while unconfigured). */
383
434
  list() {
384
435
  if (!this.configured) return [];
385
- if (this.#entries.size === 0) return staticMagpieModels.map((entry) => entry.id).filter((id) => !this.#hidden.has(id));
386
- return [...this.#entries.keys()].filter((id) => !this.#hidden.has(id)).sort();
436
+ if (this.#entries.size === 0) return staticMagpieModels.map((entry) => entry.id).filter((id) => this.#offers(id));
437
+ return [...this.#entries.keys()].filter((id) => this.#offers(id)).sort();
387
438
  }
388
439
  /** Catalog health snapshot (pending/ready/stale + counts). */
389
440
  snapshot() {
@@ -1041,7 +1092,7 @@ var MagpieAdapter = class {
1041
1092
  baseUrl: this.#fallbackBaseUrl,
1042
1093
  auth: {
1043
1094
  apiKey: {
1044
- name: "Magpie LAN gateway",
1095
+ name: "Magpie gateway",
1045
1096
  resolve: async () => ({ auth: { apiKey: this.#runtime?.apiKey() ?? this.#fallbackApiKey } })
1046
1097
  }
1047
1098
  },
@@ -1236,12 +1287,12 @@ function normalizeBaseUrl(raw) {
1236
1287
  }
1237
1288
  return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
1238
1289
  }
1239
- function normalizeHiddenModels(raw) {
1290
+ function normalizeEnabledModels(raw) {
1240
1291
  if (raw === void 0) return [];
1241
- if (!Array.isArray(raw)) throw new Error("dsh-magpie-connect: hiddenModels must be an array of model ids");
1292
+ if (!Array.isArray(raw)) throw new Error("dsh-magpie-connect: enabledModels must be an array of model ids");
1242
1293
  const out = [];
1243
1294
  for (const entry of raw) {
1244
- if (typeof entry !== "string") throw new Error("dsh-magpie-connect: hiddenModels must be an array of model ids");
1295
+ if (typeof entry !== "string") throw new Error("dsh-magpie-connect: enabledModels must be an array of model ids");
1245
1296
  const id = entry.trim();
1246
1297
  if (id !== "" && !out.includes(id)) out.push(id);
1247
1298
  }
@@ -1258,9 +1309,30 @@ function normalizePageSettings(raw) {
1258
1309
  }
1259
1310
  out.apiKey = record.apiKey.trim();
1260
1311
  }
1261
- if (record.hiddenModels !== void 0) out.hiddenModels = normalizeHiddenModels(record.hiddenModels);
1312
+ if (record.enabledModels !== void 0) out.enabledModels = normalizeEnabledModels(record.enabledModels);
1313
+ if (record.migratedFromHiddenModels === true) out.migratedFromHiddenModels = true;
1262
1314
  return out;
1263
1315
  }
1316
+ function cloneSettings(source) {
1317
+ return {
1318
+ ...source,
1319
+ ...source.enabledModels === void 0 ? {} : { enabledModels: [...source.enabledModels] }
1320
+ };
1321
+ }
1322
+ function migrateLegacySettings(raw, known) {
1323
+ if (!raw || typeof raw !== "object") return { settings: {}, migrated: false };
1324
+ const record = raw;
1325
+ const legacy = record.hiddenModels;
1326
+ if (record.migratedFromHiddenModels === true || !Array.isArray(legacy)) {
1327
+ return { settings: normalizePageSettings(raw), migrated: false };
1328
+ }
1329
+ const hidden = new Set(legacy.filter((entry) => typeof entry === "string").map((entry) => entry.trim()).filter((entry) => entry !== ""));
1330
+ const base = { ...record, migratedFromHiddenModels: true };
1331
+ delete base.hiddenModels;
1332
+ if (known !== void 0) base.enabledModels = known.filter((id) => !hidden.has(id));
1333
+ const settings = normalizePageSettings(base);
1334
+ return { settings, migrated: true };
1335
+ }
1264
1336
  function resolveEffectiveEndpoint(patch, page) {
1265
1337
  return {
1266
1338
  baseUrl: page.baseUrl ?? patch.baseUrl,
@@ -1277,19 +1349,34 @@ var SettingsStore = class {
1277
1349
  constructor(options = {}) {
1278
1350
  this.#path = options.path;
1279
1351
  }
1280
- /** Load persisted settings (missing/corrupt file reads as empty, never throws). */
1281
- async load() {
1352
+ /**
1353
+ * Load persisted settings (missing/corrupt file reads as empty, never throws).
1354
+ *
1355
+ * Also runs the one-time `hiddenModels` -> `enabledModels` conversion. The
1356
+ * rewrite is attempted at startup rather than lazily so the file on disk
1357
+ * matches what the running process believes; a failed write is not fatal
1358
+ * (the in-memory result is still correct), and the migration marker makes a
1359
+ * retry idempotent.
1360
+ * @param known - ids the old blacklist rules would have offered, used to
1361
+ * compute the allowlist; omitted when the directory is not known yet.
1362
+ */
1363
+ async load(known) {
1282
1364
  if (!this.#path) return this.get();
1365
+ let raw;
1283
1366
  try {
1284
- const raw = JSON.parse(await readFile2(this.#path, "utf8"));
1285
- this.#settings = normalizePageSettings(raw);
1367
+ raw = JSON.parse(await readFile2(this.#path, "utf8"));
1286
1368
  } catch {
1287
1369
  this.#settings = {};
1370
+ return this.get();
1288
1371
  }
1372
+ const { settings, migrated } = migrateLegacySettings(raw, known);
1373
+ this.#settings = settings;
1374
+ if (migrated) await this.persist().catch(() => {
1375
+ });
1289
1376
  return this.get();
1290
1377
  }
1291
1378
  get() {
1292
- return { ...this.#settings, hiddenModels: [...this.#settings.hiddenModels ?? []] };
1379
+ return cloneSettings(this.#settings);
1293
1380
  }
1294
1381
  /** Merge a partial payload, persist, and notify listeners. */
1295
1382
  async save(partial) {
@@ -1301,7 +1388,7 @@ var SettingsStore = class {
1301
1388
  }
1302
1389
  /** Replace the whole document (used at startup after load). */
1303
1390
  replace(next) {
1304
- this.#settings = { ...next, hiddenModels: [...next.hiddenModels ?? []] };
1391
+ this.#settings = cloneSettings(next);
1305
1392
  this.#emit();
1306
1393
  }
1307
1394
  onChange(listener) {
@@ -1378,9 +1465,13 @@ async function readRouteBody(request) {
1378
1465
  }
1379
1466
  function buildModelRows(backend) {
1380
1467
  const { store, catalog } = backend;
1381
- const hidden = new Set(store.get().hiddenModels ?? []);
1382
- const ids = /* @__PURE__ */ new Set([...catalog.list(), ...hidden]);
1383
- return [...ids].sort().map((id) => ({ ...toRow(id, catalog.getEntry(id)), hidden: hidden.has(id) }));
1468
+ const allowlist = store.get().enabledModels;
1469
+ const allowed = allowlist === void 0 ? void 0 : new Set(allowlist);
1470
+ return catalog.directory().sort().map((id) => ({
1471
+ ...toRow(id, catalog.getEntry(id)),
1472
+ enabled: allowed === void 0 || allowed.has(id),
1473
+ source: catalog.sourceFor(id)
1474
+ }));
1384
1475
  }
1385
1476
  function backendOrThrow() {
1386
1477
  if (!currentBackend) throw new Error("dsh-magpie-connect: settings backend not ready");
@@ -1394,7 +1485,8 @@ async function handleSettingsGet() {
1394
1485
  return routeOk({
1395
1486
  baseUrl: endpoint.baseUrl,
1396
1487
  apiKey: endpoint.apiKey,
1397
- hiddenModels: page.hiddenModels ?? [],
1488
+ enabledModels: page.enabledModels,
1489
+ inheritedModels: page.enabledModels === void 0,
1398
1490
  fromPage: page,
1399
1491
  fromPatch: backend.patchEndpoint
1400
1492
  });
@@ -1407,9 +1499,6 @@ async function handleSettingsPost(request) {
1407
1499
  const backend = backendOrThrow();
1408
1500
  const body = await readRouteBody(request);
1409
1501
  const normalized = normalizePageSettings(body);
1410
- if (normalized.hiddenModels !== void 0) {
1411
- normalized.hiddenModels = backend.catalog.pruneHidden(normalized.hiddenModels);
1412
- }
1413
1502
  const saved = await backend.store.save(normalized);
1414
1503
  backend.applyPageSettings();
1415
1504
  if (normalized.baseUrl !== void 0 || normalized.apiKey !== void 0) {
@@ -1419,7 +1508,12 @@ async function handleSettingsPost(request) {
1419
1508
  ]);
1420
1509
  }
1421
1510
  const endpoint = resolveEffectiveEndpoint(backend.patchEndpoint, saved);
1422
- return routeOk({ baseUrl: endpoint.baseUrl, apiKey: endpoint.apiKey, hiddenModels: saved.hiddenModels ?? [] });
1511
+ return routeOk({
1512
+ baseUrl: endpoint.baseUrl,
1513
+ apiKey: endpoint.apiKey,
1514
+ enabledModels: saved.enabledModels,
1515
+ inheritedModels: saved.enabledModels === void 0
1516
+ });
1423
1517
  } catch (error) {
1424
1518
  return routeFail(error);
1425
1519
  }
@@ -1462,6 +1556,7 @@ async function handleDiscoverPost(request) {
1462
1556
  const body = await readRouteBody(request);
1463
1557
  const { origin, key } = probeTarget(backend, body);
1464
1558
  const models = await fetchMagpieModels(origin, fetch, { headers: { authorization: `Bearer ${key}` } });
1559
+ backend.catalog.adoptDirectory(models);
1465
1560
  const candidates = [...models.entries()].sort(([left], [right]) => left.localeCompare(right)).map(([id, entry]) => toRow(id, entry));
1466
1561
  return routeOk({ count: candidates.length, models: candidates });
1467
1562
  } catch (error) {
@@ -1601,7 +1696,7 @@ function apply(ctx, config = {}) {
1601
1696
  const endpoint = resolveEffectiveEndpoint(patchEndpoint, page);
1602
1697
  const configured = isConfigured(endpoint);
1603
1698
  catalog.setEndpoint(configured ? endpoint.baseUrl.replace(/\/+$/, "") : "", configured ? endpoint.apiKey : "");
1604
- catalog.setHidden(page.hiddenModels ?? []);
1699
+ catalog.setEnabled(page.enabledModels ?? null);
1605
1700
  };
1606
1701
  const adapter = new MagpieAdapter(catalog, {
1607
1702
  providerId: cfg.providerId,
@@ -1632,11 +1727,11 @@ function apply(ctx, config = {}) {
1632
1727
  ctx.llm.registerAdapter([cfg.providerId], adapter);
1633
1728
  logger.info(`dsh-magpie-connect: adapter registered for "${cfg.providerId}" (catalog warms up in background)`);
1634
1729
  void (async () => {
1635
- await store.load();
1636
- applyPageSettings();
1637
1730
  await catalog.start().catch((err) => {
1638
1731
  logger.error(`dsh-magpie-connect: catalog start failed: ${err instanceof Error ? err.message : String(err)}`);
1639
1732
  });
1733
+ await store.load(catalog.directory());
1734
+ applyPageSettings();
1640
1735
  })().catch((err) => {
1641
1736
  logger.error(`dsh-magpie-connect: startup failed: ${err instanceof Error ? err.message : String(err)}`);
1642
1737
  });