dsh-model-think-level 2.3.2 → 2.3.3

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
@@ -6836,6 +6836,8 @@ function apply(ctx, config = {}) {
6836
6836
  const api = normalizeKeyProtocol(profile["api"]);
6837
6837
  const baseURL = typeof profile["baseURL"] === "string" ? profile["baseURL"].trim() : "";
6838
6838
  if (api === void 0 || !LISTABLE_PROTOCOLS.has(api) || baseURL.length === 0) return;
6839
+ const declaredIds = Array.isArray(profile["models"]) ? profile["models"].filter(isRecord).map((model) => typeof model["id"] === "string" ? model["id"].trim() : "").filter(Boolean) : [];
6840
+ if (declaredIds.length === 0) return;
6839
6841
  let document;
6840
6842
  try {
6841
6843
  document = readKeyIndex(indexFile);
@@ -6844,8 +6846,9 @@ function apply(ctx, config = {}) {
6844
6846
  }
6845
6847
  const endpoints = routeEndpointsOf(document, route);
6846
6848
  const listed = keyEntriesOf(document, route);
6849
+ const active = listed.filter((entry) => entry.disabled !== true);
6847
6850
  const seat = enabledRefOf(profile);
6848
- const keys = listed.map((entry) => ({ ref: entry.ref, entry }));
6851
+ const keys = active.map((entry) => ({ ref: entry.ref, entry }));
6849
6852
  if (seat !== void 0 && !listed.some((entry) => entry.ref === seat)) keys.push({ ref: seat, entry: void 0 });
6850
6853
  if (keys.length < 2) return;
6851
6854
  const credentials = ctx.get("credentials");
@@ -6902,19 +6905,32 @@ function apply(ctx, config = {}) {
6902
6905
  if (declared.length === 0) return;
6903
6906
  let next = readKeyIndex(indexFile);
6904
6907
  let mutated = false;
6908
+ const emptied = [];
6905
6909
  for (const [ref, ids] of served) {
6906
6910
  lastKnownScopes.set(`${route}\0${ref}`, ids);
6907
6911
  recordServes(route, ref, ids);
6908
6912
  const entry = keyEntriesOf(next, route).find((candidate) => candidate.ref === ref);
6909
6913
  if (entry === void 0) continue;
6910
- if (entry.models !== void 0 && entry.auto !== true) continue;
6911
6914
  const target = declared.filter((id) => ids.includes(id));
6915
+ if (target.length === 0) {
6916
+ if (entry.disabled !== true) emptied.push(ref);
6917
+ continue;
6918
+ }
6912
6919
  const scope = target.length === declared.length ? void 0 : target;
6913
6920
  const unchanged = scope === void 0 ? entry.models === void 0 : entry.models !== void 0 && entry.models.length === scope.length && scope.every((id) => entry.models?.includes(id));
6914
6921
  if (unchanged) continue;
6915
6922
  next = withKeyAutoScope(next, route, ref, scope);
6916
6923
  mutated = true;
6917
6924
  }
6925
+ if (emptied.length > 0) {
6926
+ const after = keyEntriesOf(next, route);
6927
+ const activeLeft = after.filter((entry) => entry.disabled !== true && !emptied.includes(entry.ref)).length;
6928
+ const seatLeft = seat !== void 0 && !after.some((entry) => entry.ref === seat) ? 1 : 0;
6929
+ if (activeLeft + seatLeft > 0) {
6930
+ for (const ref of emptied) next = withKeyDisabled(next, route, ref, true);
6931
+ mutated = true;
6932
+ }
6933
+ }
6918
6934
  if (!mutated) return;
6919
6935
  try {
6920
6936
  writeKeyIndex(next, indexFile);
@@ -6936,6 +6952,50 @@ function apply(ctx, config = {}) {
6936
6952
  keyProbeRuns.set(route, run);
6937
6953
  return run;
6938
6954
  };
6955
+ const PROBE_SAVE_DELAY_MS = 1e3;
6956
+ const probeSignatures = /* @__PURE__ */ new Map();
6957
+ const probeSavePending = /* @__PURE__ */ new Set();
6958
+ let probeSaveTimer;
6959
+ const probeSignatureOf = (profile) => JSON.stringify([
6960
+ profile["api"],
6961
+ profile["baseURL"],
6962
+ profile["apiKeyEnv"],
6963
+ profile["headers"],
6964
+ Array.isArray(profile["models"]) ? profile["models"].filter(isRecord).map((model) => typeof model["id"] === "string" ? model["id"].trim() : "").filter(Boolean).sort() : []
6965
+ ]);
6966
+ const armProbeTimer = () => {
6967
+ if (probeStopped || probeSavePending.size === 0 || probeSaveTimer !== void 0) return;
6968
+ probeSaveTimer = setTimeout(() => {
6969
+ probeSaveTimer = void 0;
6970
+ const routes = Array.from(probeSavePending);
6971
+ probeSavePending.clear();
6972
+ for (const route of routes) void runKeyProbe(route);
6973
+ }, PROBE_SAVE_DELAY_MS);
6974
+ };
6975
+ const scheduleKeyProbes = (changed) => {
6976
+ if (probeStopped) return;
6977
+ const providers = providersOf();
6978
+ for (const route of Array.from(probeSignatures.keys())) {
6979
+ if (!Object.hasOwn(providers, route)) probeSignatures.delete(route);
6980
+ }
6981
+ for (const [route, profile] of Object.entries(providers)) {
6982
+ if (!isRecord(profile)) continue;
6983
+ const signature = probeSignatureOf(profile);
6984
+ if (probeSignatures.get(route) === signature) continue;
6985
+ probeSignatures.set(route, signature);
6986
+ probeSavePending.add(route);
6987
+ }
6988
+ if (changed !== void 0 && Object.hasOwn(providers, changed)) probeSavePending.add(changed);
6989
+ armProbeTimer();
6990
+ };
6991
+ for (const [route, profile] of Object.entries(providersOf())) {
6992
+ if (isRecord(profile)) probeSignatures.set(route, probeSignatureOf(profile));
6993
+ }
6994
+ ctx.effect(() => () => {
6995
+ if (probeSaveTimer !== void 0) clearTimeout(probeSaveTimer);
6996
+ probeSaveTimer = void 0;
6997
+ probeSavePending.clear();
6998
+ });
6939
6999
  const headerRetryTimers = /* @__PURE__ */ new Set();
6940
7000
  let headerRetryAttempt = 0;
6941
7001
  const stopHeaderRetries = () => {
@@ -6982,6 +7042,7 @@ function apply(ctx, config = {}) {
6982
7042
  catalogueSourceStamp = source;
6983
7043
  invalidateListings();
6984
7044
  }
7045
+ scheduleKeyProbes();
6985
7046
  }
6986
7047
  });
6987
7048
  overlaySource.keys = async ({ route, model }) => {
@@ -7576,11 +7637,7 @@ function apply(ctx, config = {}) {
7576
7637
  if (fact?.masked !== void 0) view.masked = fact.masked;
7577
7638
  if (stored?.alias !== void 0) view.alias = stored.alias;
7578
7639
  if (stored?.models !== void 0) view.models = stored.models.filter((id) => declaredModels.has(id));
7579
- const observed = servesOfRoute(servesDocument(), route).get(ref2)?.filter((id) => declaredModels.has(id));
7580
- if (observed !== void 0 && observed.length > 0) {
7581
- ;
7582
- view.observed = observed;
7583
- }
7640
+ if (stored?.auto === true) view.auto = true;
7584
7641
  if (stored?.protocol !== void 0) view.protocol = stored.protocol;
7585
7642
  const rest = keyRest.stateOf(restKeyOf(route, ref2), now);
7586
7643
  if (rest !== void 0) {
@@ -7873,8 +7930,8 @@ function apply(ctx, config = {}) {
7873
7930
  } else if (op === "probe") {
7874
7931
  const listed = keyEntriesOf(readKeyIndex(indexFile), route);
7875
7932
  const seat = enabledNow();
7876
- const distinct = new Set(listed.map((entry) => entry.ref));
7877
- if (seat !== void 0) distinct.add(seat);
7933
+ const distinct = new Set(listed.filter((entry) => entry.disabled !== true).map((entry) => entry.ref));
7934
+ if (seat !== void 0 && !listed.some((entry) => entry.ref === seat)) distinct.add(seat);
7878
7935
  if (distinct.size < 2) {
7879
7936
  refuse("a scope probe needs a second key; a single key serves whatever it serves");
7880
7937
  return;
@@ -7887,6 +7944,7 @@ function apply(ctx, config = {}) {
7887
7944
  refreshKeyScopes();
7888
7945
  if (["add", "remove", "value", "protocol", "endpoints", "enable"].includes(op)) {
7889
7946
  invalidateListings(route);
7947
+ scheduleKeyProbes(route);
7890
7948
  }
7891
7949
  releaseMutation?.();
7892
7950
  releaseMutation = void 0;