dsh-model-think-level 2.3.2 → 2.3.4

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