dsh-cliproxyapi-pro 1.9.1 → 1.10.1

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.
@@ -34,7 +34,8 @@ export function plainObject(value) {
34
34
 
35
35
  /**
36
36
  * Normalize the state file into `{ version, refreshIntervalSeconds,
37
- * probeTimeoutMs, accounts[] }`. A legacy single-account file (flat fields,
37
+ * probeTimeoutMs, onlineModelInfo, accounts[] }` (a file without
38
+ * `onlineModelInfo` reads as on). A legacy single-account file (flat fields,
38
39
  * no `accounts` array) becomes one account with id `default`, so its
39
40
  * provider route, key reference and model selection carry over unchanged.
40
41
  * Invalid or duplicate account ids are replaced with generated ones.
@@ -62,6 +63,7 @@ export function normalizeState(raw = {}) {
62
63
  version: STATE_VERSION,
63
64
  refreshIntervalSeconds: globals.refreshIntervalSeconds,
64
65
  probeTimeoutMs: globals.probeTimeoutMs,
66
+ onlineModelInfo: globals.onlineModelInfo,
65
67
  accounts,
66
68
  };
67
69
  }
@@ -1,4 +1,4 @@
1
- // Credentials, media preferences, account controller and refresh scheduling.
1
+ // Credentials, media preferences, online model info, account controller and refresh scheduling.
2
2
 
3
3
  import { existsSync } from "node:fs";
4
4
  import { join } from "node:path";
@@ -6,6 +6,7 @@ import { accountKindOverrides, loadMediaSettings } from "../../media/index.js";
6
6
  import { outsideHmrTransaction } from "../apply-utils.js";
7
7
  import { toCredentialRef, getEnvValue } from "../config.js";
8
8
  import { createCpaController } from "../controller.js";
9
+ import { createModelInfo } from "../model-info.js";
9
10
 
10
11
  export function createController(shared) {
11
12
  const { ctx, readExistingPiAiRoute, readDefaultModel, readState, writeState, updateDirectory } = shared;
@@ -76,10 +77,25 @@ export function createController(shared) {
76
77
  return loaded;
77
78
  };
78
79
 
80
+ // Public model catalogs for recommendations (download only; global switch, default on).
81
+ // Timers live in lifecycle.js; new data re-syncs the accounts once.
82
+ const modelInfo = createModelInfo({
83
+ cachePath: join(updateDirectory, "model-info.json"),
84
+ enabled: () => readState().onlineModelInfo !== false,
85
+ logger: ctx.logger,
86
+ onChange: () => {
87
+ void controller.refreshAll().finally(() => shared.syncAgentTool?.());
88
+ },
89
+ });
90
+
79
91
  const controller = createCpaController({
80
92
  settings: ctx.settings,
81
93
  readState,
82
94
  writeState,
95
+ modelInfo,
96
+ onModelInfoToggled: () => {
97
+ void modelInfo.refresh().finally(() => controller.refreshAll().finally(() => shared.syncAgentTool?.()));
98
+ },
83
99
  mediaKindOverrides: (id) => accountKindOverrides(readMedia().settings, id),
84
100
  resolveApiKey,
85
101
  describeApiKey,
@@ -101,5 +117,5 @@ export function createController(shared) {
101
117
  onGlobalsChanged: () => reschedule(),
102
118
  });
103
119
 
104
- return { resolveApiKey, describeApiKey, reschedule, mediaSettingsPath, readMedia, controller };
120
+ return { resolveApiKey, describeApiKey, reschedule, mediaSettingsPath, readMedia, controller, modelInfo };
105
121
  }
@@ -1,4 +1,4 @@
1
- // Refresh startup, video resume, credential listener and refresh-loop disposal.
1
+ // Refresh startup, online model info schedule, video resume, credential listener and refresh-loop disposal.
2
2
 
3
3
  import { outsideHmrTransaction } from "../apply-utils.js";
4
4
 
@@ -13,6 +13,22 @@ export function createLifecycle(shared) {
13
13
  }, 600)
14
14
  );
15
15
 
16
+ // Online model info: first look ~5 s after start, then hourly checks that
17
+ // download only when a source is a day old (or an hour after a failure).
18
+ const { modelInfo } = shared;
19
+ ctx.effect(() => {
20
+ const run = () => void modelInfo.refresh().catch(() => {});
21
+ const timers = outsideHmrTransaction(ctx, () => ({
22
+ first: setTimeout(run, 5000),
23
+ every: setInterval(run, 60 * 60 * 1000),
24
+ }));
25
+ return () => {
26
+ clearTimeout(timers.first);
27
+ clearInterval(timers.every);
28
+ modelInfo.dispose();
29
+ };
30
+ }, "cpa: online model info");
31
+
16
32
  ctx.on("credentials/reference-updated", (ref) => {
17
33
  // A management key changed (also from the DSH credentials page): start a new failure count.
18
34
  quota.keyChanged(ref);
@@ -26,7 +26,11 @@ export const ACCOUNT_FIELDS = [
26
26
  "selectedModels",
27
27
  "modelOverrides",
28
28
  ];
29
- export const GLOBAL_FIELDS = ["refreshIntervalSeconds", "probeTimeoutMs"];
29
+ /**
30
+ * All-account settings. `onlineModelInfo` (default on) lets recommendations
31
+ * use public model catalogs (lib/host/model-info.js); download only.
32
+ */
33
+ export const GLOBAL_FIELDS = ["refreshIntervalSeconds", "probeTimeoutMs", "onlineModelInfo"];
30
34
  export const DEFAULT_REFRESH_INTERVAL_SECONDS = 300;
31
35
  export const DEFAULT_PROBE_TIMEOUT_MS = 5000;
32
36
 
@@ -192,6 +196,7 @@ export function resolveConfig(rawConfig = {}) {
192
196
  protocolMode,
193
197
  refreshIntervalSeconds,
194
198
  probeTimeoutMs,
199
+ onlineModelInfo: readVolatile(rawConfig?.onlineModelInfo, true) !== false,
195
200
  includePrefix,
196
201
  excludePrefix,
197
202
  ...(selectedModels === undefined ? {} : { selectedModels }),
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { diagnoseMedia, mediaAliasTarget, MediaError } from "../media/index.js";
4
4
  import { DEFAULT_API_KEY_ENV, GLOBAL_FIELDS, toCredentialRef, resolveConfig } from "./config.js";
5
- import { recommendModels } from "./models.js";
5
+ import { planRecommendations, selectionSuggestions } from "./recommend.js";
6
6
  import { isChatModel, managedRouteKeys } from "./routes.js";
7
7
  import { pickFields, normalizeState, accountConflicts, assertNoConflicts, newAccountDefaults, patchAccount } from "./accounts.js";
8
8
  import { normalizeCpaRoot, discoverCpa } from "./discovery.js";
@@ -46,6 +46,7 @@ export function createCpaController(deps) {
46
46
  ...account,
47
47
  refreshIntervalSeconds: state.refreshIntervalSeconds,
48
48
  probeTimeoutMs: state.probeTimeoutMs,
49
+ onlineModelInfo: state.onlineModelInfo,
49
50
  }),
50
51
  // Route keys of every other (non-conflicting) account: never written or
51
52
  // removed here. A conflicting account owns no keys.
@@ -72,6 +73,9 @@ export function createCpaController(deps) {
72
73
  readDefaultModel: deps.readDefaultModel,
73
74
  // User reclassifications (chat / image / video) from media-settings.json.
74
75
  kindOverrides: () => deps.mediaKindOverrides?.(id) ?? {},
76
+ // Online model facts (lib/host/model-info.js); undefined while switched off.
77
+ lookupInfo: (model) => deps.modelInfo?.lookup(model),
78
+ ...(deps.modelInfo === undefined ? {} : { modelInfoReady: () => deps.modelInfo.ready() }),
75
79
  // Persist a first-run selection unless one was saved in the meantime.
76
80
  seedSelection: (ids) => {
77
81
  const state = deps.readState();
@@ -162,12 +166,34 @@ export function createCpaController(deps) {
162
166
  writable: keyInfo.writable !== false,
163
167
  source: keyInfo.source ?? null,
164
168
  },
165
- // One model per family from this account's catalog (the panel's reset button).
166
- recommendedModels: recommendModels(runtime.chatModels()),
169
+ ...advice(runtime, config),
167
170
  status: st,
168
171
  };
169
172
  };
170
173
 
174
+ /**
175
+ * Recommendations (the panel's reset button, with a reason per model) and
176
+ * suggestions about the saved selection. Display only; nothing is applied.
177
+ */
178
+ const advice = (runtime, config) => {
179
+ const lookup = (model) => deps.modelInfo?.lookup(model);
180
+ const chat = runtime.chatModels();
181
+ const plan = planRecommendations(chat, { lookup });
182
+ const all = runtime.allDiscoveredModels ?? [];
183
+ return {
184
+ recommendedModels: plan.ids,
185
+ recommendations: plan.details,
186
+ suggestions:
187
+ config.selectedModels === undefined || config.selectedModels.length === 0
188
+ ? { upgrades: [], missing: [], retired: [] }
189
+ : selectionSuggestions(chat, config.selectedModels, {
190
+ lookup,
191
+ allIds: all.map((m) => m.id),
192
+ catalogComplete: all.length > 0 && runtime.listingOk !== false,
193
+ }),
194
+ };
195
+ };
196
+
171
197
  const statusPayload = async (extra = {}) => {
172
198
  const state = deps.readState();
173
199
  const accounts = [];
@@ -177,6 +203,8 @@ export function createCpaController(deps) {
177
203
  global: {
178
204
  refreshIntervalSeconds: state.refreshIntervalSeconds,
179
205
  probeTimeoutMs: state.probeTimeoutMs,
206
+ onlineModelInfo: state.onlineModelInfo !== false,
207
+ ...(deps.modelInfo === undefined ? {} : { modelInfo: deps.modelInfo.status() }),
180
208
  },
181
209
  accounts,
182
210
  ...extra,
@@ -201,6 +229,7 @@ export function createCpaController(deps) {
201
229
  const next = normalizeState({ ...state, ...pickFields(patch, GLOBAL_FIELDS), accounts });
202
230
  deps.writeState(next);
203
231
  if (next.refreshIntervalSeconds !== state.refreshIntervalSeconds) deps.onGlobalsChanged?.(next);
232
+ if (next.onlineModelInfo !== state.onlineModelInfo) deps.onModelInfoToggled?.(next.onlineModelInfo);
204
233
  return next;
205
234
  };
206
235
 
@@ -145,6 +145,9 @@ export function parseCatalog(payload) {
145
145
  "maxTokens",
146
146
  ]);
147
147
  const rawName = item.display_name ?? item.name;
148
+ // Unix seconds from the plain listing; a catalog `priority` below 100 is upstream-curated.
149
+ const created = positiveInteger(item.created);
150
+ const priority = typeof item.priority === "number" && Number.isInteger(item.priority) && item.priority >= 0 ? item.priority : undefined;
148
151
  models.set(id, {
149
152
  id,
150
153
  ...(typeof rawName === "string" && rawName.trim() !== "" ? { name: rawName.trim() } : {}),
@@ -159,6 +162,8 @@ export function parseCatalog(payload) {
159
162
  inputModalities: inputModalities.length > 0 ? inputModalities : ["text"],
160
163
  ...(outputModalities.length === 0 ? {} : { outputModalities }),
161
164
  ...(catalogType === undefined ? {} : { catalogType }),
165
+ ...(created === undefined ? {} : { created }),
166
+ ...(priority === undefined ? {} : { priority }),
162
167
  reasoning: item.reasoning === true || levels.some((level) => level !== "off"),
163
168
  reasoningLevels: levels,
164
169
  ...(Object.keys(reasoningWire).length === 0 ? {} : { reasoningWire }),
@@ -230,7 +235,8 @@ export async function fetchListing(fetchImpl, rootURL, options) {
230
235
 
231
236
  /**
232
237
  * Fill catalog gaps from the plain listing: `owned_by` (native protocol),
233
- * the registry `type` and output modalities (media kind). A media model that
238
+ * the registry `type` and output modalities (media kind) and `created`
239
+ * (release ordering for recommendations). A media model that
234
240
  * only the plain listing carries is added; plain-only chat models are not.
235
241
  */
236
242
  export function mergeListing(models, listing) {
@@ -238,7 +244,7 @@ export function mergeListing(models, listing) {
238
244
  const plainEntry = listing.get(m.id);
239
245
  if (plainEntry === undefined) return m;
240
246
  const next = { ...m };
241
- for (const field of ["ownedBy", "catalogType", "outputModalities"]) {
247
+ for (const field of ["ownedBy", "catalogType", "outputModalities", "created"]) {
242
248
  if (next[field] === undefined && plainEntry[field] !== undefined) next[field] = plainEntry[field];
243
249
  }
244
250
  next.detectedKind = detectMediaKind(next);
@@ -0,0 +1,363 @@
1
+ // Optional online model facts from public catalogs: models.dev (lines, release
2
+ // dates, status, limits, prices) and OpenRouter (capability index, end dates).
3
+ //
4
+ // Download only: plain GETs of public JSON, no key, account, address or model
5
+ // list is ever sent. A compact index is cached in `model-info.json` next to the
6
+ // account state and refreshed at most daily; without it recommendations fall
7
+ // back to the catalog's own facts. Switched by the global `onlineModelInfo`.
8
+
9
+ import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
10
+ import { dirname } from "node:path";
11
+ import { matchKeys } from "./model-names.js";
12
+
13
+ export const MODEL_INFO_SOURCES = {
14
+ modelsDev: { url: "https://models.dev/api.json", maxBytes: 32 * 1024 * 1024 },
15
+ openRouter: { url: "https://openrouter.ai/api/v1/models", maxBytes: 16 * 1024 * 1024 },
16
+ };
17
+ export const MODEL_INFO_TTL_MS = 24 * 60 * 60 * 1000;
18
+ export const MODEL_INFO_RETRY_MS = 60 * 60 * 1000;
19
+ export const MODEL_INFO_TIMEOUT_MS = 30_000;
20
+ export const MODEL_INFO_CACHE_VERSION = 1;
21
+ const MAX_ENTRIES = 20_000;
22
+
23
+ const text = (value, max = 80) => (typeof value === "string" && value.trim() !== "" ? value.trim().slice(0, max) : undefined);
24
+ const count = (value) => (typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined);
25
+ const amount = (value) => (typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : undefined);
26
+ const day = (value) => (typeof value === "string" && /^\d{4}-\d{2}(-\d{2})?/.test(value) ? value.slice(0, 10) : undefined);
27
+
28
+ function compact(entry) {
29
+ return Object.fromEntries(Object.entries(entry).filter(([, v]) => v !== undefined));
30
+ }
31
+
32
+ /**
33
+ * models.dev `api.json` → `[{ id, rank, family, released, status, context, output, costOutput }]`.
34
+ * `rank` 0 = the model's canonical provider, 1 = another first-party
35
+ * provider, 2 = a reseller; the best rank wins a duplicate id.
36
+ */
37
+ export function parseModelsDev(payload) {
38
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload)) throw new Error("models.dev: not an object");
39
+ const providers = Object.entries(payload).filter(([, p]) => typeof p?.models === "object" && p.models !== null);
40
+ const firstParty = new Set();
41
+ for (const [, provider] of providers) {
42
+ for (const model of Object.values(provider.models)) {
43
+ const canonical = text(model?.canonical_model_id, 200);
44
+ if (canonical?.includes("/")) firstParty.add(canonical.split("/")[0]);
45
+ }
46
+ }
47
+ const out = [];
48
+ for (const [providerId, provider] of providers) {
49
+ for (const [id, model] of Object.entries(provider.models)) {
50
+ if (typeof model !== "object" || model === null || out.length >= MAX_ENTRIES) continue;
51
+ const canonical = text(model.canonical_model_id, 200);
52
+ out.push(compact({
53
+ id: text(model.id, 200) ?? id,
54
+ rank: canonical?.startsWith(`${providerId}/`) ? 0 : firstParty.has(providerId) ? 1 : 2,
55
+ family: text(model.family),
56
+ released: day(model.release_date),
57
+ status: text(model.status, 20),
58
+ context: count(model.limit?.context),
59
+ output: count(model.limit?.output),
60
+ costOutput: amount(model.cost?.output),
61
+ }));
62
+ }
63
+ }
64
+ if (out.length === 0) throw new Error("models.dev: no models");
65
+ // Resellers repeat first-party models; keep one entry per spelling (best rank first).
66
+ const best = new Map();
67
+ for (const entry of out) {
68
+ const key = matchKeys(entry.id)?.exact ?? `id:${entry.id}`;
69
+ const held = best.get(key);
70
+ if (held === undefined || entry.rank < held.rank) best.set(key, entry);
71
+ }
72
+ return [...best.values()];
73
+ }
74
+
75
+ /** OpenRouter `/api/v1/models` → `[{ id, rank, released, expires, context, intelligence }]`. */
76
+ export function parseOpenRouter(payload) {
77
+ if (!Array.isArray(payload?.data)) throw new Error("openrouter: no data array");
78
+ const out = [];
79
+ for (const model of payload.data) {
80
+ const id = text(model?.id, 200);
81
+ // `:batch`, `:free`... are billing variants of the same model.
82
+ if (id === undefined || id.includes(":") || out.length >= MAX_ENTRIES) continue;
83
+ const created = count(model.created);
84
+ out.push(compact({
85
+ id,
86
+ rank: 0,
87
+ released: created === undefined ? undefined : new Date(created * 1000).toISOString().slice(0, 10),
88
+ expires: day(model.expiration_date),
89
+ context: count(model.context_length),
90
+ intelligence: amount(model.benchmarks?.artificial_analysis?.intelligence_index),
91
+ }));
92
+ }
93
+ if (out.length === 0) throw new Error("openrouter: no models");
94
+ return out;
95
+ }
96
+
97
+ /** Exact and loose lookup maps over one source's entries. */
98
+ export function buildIndex(entries = []) {
99
+ const exact = new Map();
100
+ const loose = new Map();
101
+ for (const entry of entries) {
102
+ const keys = matchKeys(entry.id);
103
+ if (keys === undefined) continue;
104
+ const rank = Number.isInteger(entry.rank) ? entry.rank : 2;
105
+ const held = exact.get(keys.exact);
106
+ if (held === undefined || rank < held.rank) exact.set(keys.exact, { entry, rank });
107
+ // A loose key prefers the plain model over its variants, then the better source rank.
108
+ const score = (keys.hasVariant ? 10 : 0) + rank;
109
+ const looseHeld = loose.get(keys.loose);
110
+ if (looseHeld === undefined || score < looseHeld.score) loose.set(keys.loose, { entry, score });
111
+ }
112
+ return { exact, loose };
113
+ }
114
+
115
+ function find(index, texts) {
116
+ const parsed = texts.map((value) => matchKeys(value)).filter(Boolean);
117
+ for (const keys of parsed) {
118
+ const hit = index.exact.get(keys.exact);
119
+ if (hit !== undefined) return { entry: hit.entry, exact: true };
120
+ }
121
+ for (const keys of parsed) {
122
+ const hit = index.loose.get(keys.loose);
123
+ if (hit !== undefined) return { entry: hit.entry, exact: false };
124
+ }
125
+ return undefined;
126
+ }
127
+
128
+ /**
129
+ * Facts about one catalog model from both sources, or undefined.
130
+ * `exact` is true when the id matched without dropping variant words, the
131
+ * only case in which limits may be used for the model itself.
132
+ */
133
+ export function lookupModel(indexes, model) {
134
+ const texts = [model.id, ...(typeof model.name === "string" ? [model.name] : [])];
135
+ const md = indexes.modelsDev === undefined ? undefined : find(indexes.modelsDev, texts);
136
+ const or = indexes.openRouter === undefined ? undefined : find(indexes.openRouter, texts);
137
+ if (md === undefined && or === undefined) return undefined;
138
+ const a = md?.entry ?? {};
139
+ const b = or?.entry ?? {};
140
+ return compact({
141
+ family: a.family,
142
+ released: a.released ?? b.released,
143
+ status: a.status,
144
+ expires: b.expires,
145
+ context: a.context ?? b.context,
146
+ output: a.output,
147
+ costOutput: a.costOutput,
148
+ intelligence: b.intelligence,
149
+ exact: md !== undefined ? md.exact : or.exact,
150
+ });
151
+ }
152
+
153
+ /** Upper bound for an output limit taken from online info (channels often cap lower). */
154
+ export const ONLINE_MAX_TOKENS_CAP = 128_000;
155
+
156
+ /**
157
+ * Fill limits the CPA catalog left out from an exact online match. Catalog
158
+ * values always win; the filled fields are listed in `onlineLimits`.
159
+ */
160
+ export function withOnlineLimits(model, info) {
161
+ if (info?.exact !== true) return model;
162
+ const filled = [];
163
+ const next = { ...model };
164
+ if (next.contextWindow === undefined && info.context !== undefined) {
165
+ next.contextWindow = Math.min(info.context, 10_000_000);
166
+ filled.push("contextWindow");
167
+ }
168
+ if (next.maxTokens === undefined && info.output !== undefined) {
169
+ next.maxTokens = Math.min(info.output, ONLINE_MAX_TOKENS_CAP, next.contextWindow ?? Infinity);
170
+ filled.push("maxTokens");
171
+ }
172
+ return filled.length === 0 ? model : { ...next, onlineLimits: filled };
173
+ }
174
+
175
+ export async function fetchSource(fetchImpl, source, options = {}) {
176
+ const signal = AbortSignal.timeout(options.timeoutMs ?? MODEL_INFO_TIMEOUT_MS);
177
+ const response = await fetchImpl(source.url, {
178
+ method: "GET",
179
+ headers: { Accept: "application/json", ...(options.etag ? { "If-None-Match": options.etag } : {}) },
180
+ signal: options.signal === undefined ? signal : AbortSignal.any([options.signal, signal]),
181
+ });
182
+ if (response.status === 304) return { notModified: true };
183
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
184
+ const declared = Number(response.headers?.get?.("content-length"));
185
+ if (Number.isFinite(declared) && declared > source.maxBytes) throw new Error("response too large");
186
+ const body = await response.text();
187
+ if (Buffer.byteLength(body) > source.maxBytes) throw new Error("response too large");
188
+ let json;
189
+ try {
190
+ json = JSON.parse(body);
191
+ } catch {
192
+ throw new Error("not valid JSON");
193
+ }
194
+ return { json, etag: text(response.headers?.get?.("etag"), 200) };
195
+ }
196
+
197
+ export function loadModelInfoCache(path) {
198
+ try {
199
+ const raw = JSON.parse(readFileSync(path, "utf8"));
200
+ if (raw?.version !== MODEL_INFO_CACHE_VERSION || typeof raw.sources !== "object" || raw.sources === null) return { sources: {} };
201
+ const sources = {};
202
+ for (const name of Object.keys(MODEL_INFO_SOURCES)) {
203
+ const s = raw.sources[name];
204
+ if (typeof s !== "object" || s === null) continue;
205
+ sources[name] = {
206
+ ...(Number.isFinite(s.fetchedAt) ? { fetchedAt: s.fetchedAt } : {}),
207
+ ...(Number.isFinite(s.attemptedAt) ? { attemptedAt: s.attemptedAt } : {}),
208
+ ...(typeof s.etag === "string" ? { etag: s.etag } : {}),
209
+ ...(typeof s.error === "string" ? { error: s.error.slice(0, 200) } : {}),
210
+ models: Array.isArray(s.models) ? s.models.filter((m) => typeof m?.id === "string").slice(0, MAX_ENTRIES) : [],
211
+ };
212
+ }
213
+ return { sources };
214
+ } catch {
215
+ return { sources: {} };
216
+ }
217
+ }
218
+
219
+ export function saveModelInfoCache(path, cache) {
220
+ try {
221
+ mkdirSync(dirname(path), { recursive: true });
222
+ const tmp = `${path}.${Date.now().toString(36)}.tmp`;
223
+ writeFileSync(tmp, JSON.stringify({ version: MODEL_INFO_CACHE_VERSION, sources: cache.sources }), "utf8");
224
+ renameSync(tmp, path);
225
+ } catch {}
226
+ }
227
+
228
+ const PARSERS = { modelsDev: parseModelsDev, openRouter: parseOpenRouter };
229
+
230
+ /**
231
+ * The online model-info service of one activation.
232
+ * @param options.cachePath - `model-info.json`.
233
+ * @param options.enabled - `() => boolean`, the global `onlineModelInfo`.
234
+ * @param options.fetch / options.now - injectable for tests.
235
+ * @param options.onChange - called after new data arrived.
236
+ */
237
+ export function createModelInfo(options) {
238
+ // Resolved per request so a proxy plugin's patched global fetch applies.
239
+ const fetchImpl = options.fetch ?? ((...args) => globalThis.fetch(...args));
240
+ const now = options.now ?? Date.now;
241
+ const controller = new AbortController();
242
+ let cache = loadModelInfoCache(options.cachePath);
243
+ let indexes = {};
244
+ const rebuild = () => {
245
+ indexes = Object.fromEntries(
246
+ Object.entries(cache.sources).filter(([, s]) => s.models.length > 0).map(([name, s]) => [name, buildIndex(s.models)])
247
+ );
248
+ };
249
+ rebuild();
250
+ let running;
251
+ let lookups = new WeakMap();
252
+
253
+ const enabled = () => {
254
+ try {
255
+ return options.enabled?.() !== false;
256
+ } catch {
257
+ return true;
258
+ }
259
+ };
260
+
261
+ const due = (source, force) => {
262
+ if (force) return true;
263
+ const t = now();
264
+ if (source?.fetchedAt !== undefined && t - source.fetchedAt < MODEL_INFO_TTL_MS) return false;
265
+ return source?.attemptedAt === undefined || t - source.attemptedAt >= MODEL_INFO_RETRY_MS;
266
+ };
267
+
268
+ const runRefresh = async (force) => {
269
+ let changed = false;
270
+ for (const [name, source] of Object.entries(MODEL_INFO_SOURCES)) {
271
+ const previous = cache.sources[name];
272
+ if (!due(previous, force) || controller.signal.aborted) continue;
273
+ const attemptedAt = now();
274
+ try {
275
+ const result = await fetchSource(fetchImpl, source, { etag: previous?.models.length ? previous.etag : undefined, signal: controller.signal });
276
+ if (result.notModified) {
277
+ const { error: _stale, ...kept } = previous;
278
+ cache.sources[name] = { ...kept, fetchedAt: attemptedAt, attemptedAt };
279
+ } else {
280
+ cache.sources[name] = {
281
+ fetchedAt: attemptedAt,
282
+ attemptedAt,
283
+ ...(result.etag === undefined ? {} : { etag: result.etag }),
284
+ models: PARSERS[name](result.json),
285
+ };
286
+ changed = true;
287
+ }
288
+ } catch (error) {
289
+ if (controller.signal.aborted) return changed;
290
+ const message = error instanceof Error ? error.message : String(error);
291
+ cache.sources[name] = { ...(previous ?? { models: [] }), attemptedAt, error: message.slice(0, 200) };
292
+ options.logger?.warn?.(`dsh-cliproxyapi: online model info from ${source.url} unavailable (${message}); recommendations use the catalog`);
293
+ }
294
+ saveModelInfoCache(options.cachePath, cache);
295
+ }
296
+ if (changed) {
297
+ rebuild();
298
+ lookups = new WeakMap();
299
+ }
300
+ return changed;
301
+ };
302
+
303
+ const service = {
304
+ /** Fetch the sources that are due (all with `force`); no-op while switched off. */
305
+ refresh({ force = false } = {}) {
306
+ if (!enabled() || controller.signal.aborted) return Promise.resolve(false);
307
+ if (running !== undefined) return running;
308
+ running = runRefresh(force)
309
+ .then((changed) => {
310
+ if (changed) options.onChange?.();
311
+ return changed;
312
+ })
313
+ .finally(() => {
314
+ running = undefined;
315
+ });
316
+ return running;
317
+ },
318
+
319
+ /** Resolve once some data is available, a first download finished, or after `timeoutMs`. */
320
+ async ready(timeoutMs = 4000) {
321
+ if (!enabled() || Object.keys(indexes).length > 0) return;
322
+ const pending = running ?? service.refresh();
323
+ let timer;
324
+ await Promise.race([pending.catch(() => {}), new Promise((resolve) => { timer = setTimeout(resolve, timeoutMs); })]);
325
+ clearTimeout(timer);
326
+ },
327
+
328
+ /** Online facts for one model (memoized per model object), or undefined while switched off. */
329
+ lookup(model) {
330
+ if (!enabled() || model === null || typeof model !== "object") return undefined;
331
+ if (lookups.has(model)) return lookups.get(model);
332
+ const info = lookupModel(indexes, model);
333
+ lookups.set(model, info);
334
+ return info;
335
+ },
336
+
337
+ status() {
338
+ const on = enabled();
339
+ const sources = {};
340
+ for (const name of Object.keys(MODEL_INFO_SOURCES)) {
341
+ const s = cache.sources[name];
342
+ sources[name] = {
343
+ models: s?.models.length ?? 0,
344
+ ...(s?.fetchedAt === undefined ? {} : { fetchedAt: new Date(s.fetchedAt).toISOString() }),
345
+ // A successful download replaces the record, so a kept error is the latest attempt's.
346
+ ...(s?.error === undefined ? {} : { error: s.error }),
347
+ };
348
+ }
349
+ const anyData = Object.values(sources).some((s) => s.models > 0);
350
+ const anyError = Object.values(sources).some((s) => s.error !== undefined);
351
+ return {
352
+ enabled: on,
353
+ state: !on ? "off" : running !== undefined ? "loading" : anyError && !anyData ? "error" : anyData ? (anyError ? "partial" : "ready") : "idle",
354
+ sources,
355
+ };
356
+ },
357
+
358
+ dispose() {
359
+ controller.abort(new Error("dsh-cliproxyapi disposed"));
360
+ },
361
+ };
362
+ return service;
363
+ }