@goodandready/dsh-ollama-cloud-provider 0.1.13 → 0.1.15

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
@@ -49,7 +49,7 @@ const DEFAULT_API_BASE = "https://ollama.com";
49
49
  const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
50
50
  const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
51
51
  const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
52
- const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.13";
52
+ const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.15";
53
53
  const DEFAULT_CONTEXT_WINDOW = 131072;
54
54
  const DEFAULT_MAX_TOKENS = 32768;
55
55
  const DEFAULT_GENERATE_MAX_TOKENS = 131072;
@@ -69,6 +69,7 @@ const Config = z.object({
69
69
  requestTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
70
70
  streamIdleTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
71
71
  visibleModels: markVolatile(z.array(z.string()).default([])),
72
+ showAllModels: markVolatile(z.boolean()),
72
73
  modelVisibility: markVolatile(z.dict(z.boolean()).default({})),
73
74
  filterModelsByPlan: markVolatile(z.boolean().default(true)),
74
75
  contextWindow: markVolatile(z.number().min(1).max(1e7).step(1)),
@@ -155,6 +156,7 @@ function ruleList(value) {
155
156
  /** Normalize a raw config (possibly bypassing schemastery) into request facts. */
156
157
  function resolveConfig(config) {
157
158
  const apiKey = typeof config.apiKey === "string" && config.apiKey.trim() !== "" ? config.apiKey.trim() : void 0;
159
+ const visibleModels = Array.isArray(config.visibleModels) ? config.visibleModels.filter((id) => typeof id === "string" && id !== "") : [];
158
160
  return {
159
161
  apiKeyEnv: typeof config.apiKeyEnv === "string" && config.apiKeyEnv.trim() !== "" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
160
162
  ...apiKey === void 0 ? {} : { apiKey },
@@ -166,7 +168,8 @@ function resolveConfig(config) {
166
168
  modelTransports: transportRecord(config.modelTransports),
167
169
  requestTimeoutMs: positiveInt(config.requestTimeoutMs, 3e5),
168
170
  streamIdleTimeoutMs: positiveInt(config.streamIdleTimeoutMs, 3e5),
169
- visibleModels: Array.isArray(config.visibleModels) ? config.visibleModels.filter((id) => typeof id === "string" && id !== "") : [],
171
+ visibleModels,
172
+ showAllModels: config.showAllModels === true ? true : config.showAllModels === false ? false : visibleModels.length === 0,
170
173
  modelVisibility: booleanRecord(config.modelVisibility),
171
174
  filterModelsByPlan: config.filterModelsByPlan !== false,
172
175
  ...typeof config.contextWindow === "number" && Number.isInteger(config.contextWindow) && config.contextWindow > 0 ? { contextWindow: config.contextWindow } : {},
@@ -726,10 +729,12 @@ function isOffPeak(at = Date.now()) {
726
729
  function estimateCost(input) {
727
730
  const price = priceFor(input.model, input.priceOverrides);
728
731
  const meta = metaForPrice(input.model);
729
- const offPeak = isOffPeak(input.at) && meta?.cost.offPeakInput !== void 0;
730
- const inputRate = offPeak ? meta?.cost.offPeakInput ?? price.input : price.input;
731
- const outputRate = offPeak ? meta?.cost.offPeakOutput ?? price.output : price.output;
732
- const cacheRate = offPeak ? meta?.cost.offPeakCacheRead ?? price.cacheRead : price.cacheRead;
732
+ const override = input.priceOverrides?.[input.model] ?? input.priceOverrides?.[baseIdOf(input.model)];
733
+ const offPeakAvailable = meta?.cost.offPeakInput !== void 0 || meta?.cost.offPeakOutput !== void 0 || meta?.cost.offPeakCacheRead !== void 0;
734
+ const offPeak = isOffPeak(input.at) && offPeakAvailable;
735
+ const inputRate = override?.input ?? (offPeak ? meta?.cost.offPeakInput ?? price.input : price.input);
736
+ const outputRate = override?.output ?? (offPeak ? meta?.cost.offPeakOutput ?? price.output : price.output);
737
+ const cacheRate = override?.cacheRead ?? (offPeak ? meta?.cost.offPeakCacheRead ?? price.cacheRead : price.cacheRead);
733
738
  const usd = (input.inputTokens * inputRate + input.outputTokens * outputRate + (input.cacheReadTokens ?? 0) * cacheRate) / 1e6;
734
739
  return {
735
740
  usd: price.priced ? usd : 0,
@@ -773,6 +778,10 @@ const CATALOG_TIMEOUT_MS = 15e3;
773
778
  const liveMeta = /* @__PURE__ */ new Map();
774
779
  const cache$1 = /* @__PURE__ */ new Map();
775
780
  const inflight = /* @__PURE__ */ new Map();
781
+ let generation = 0;
782
+ function liveKey(base, id) {
783
+ return `${base}|${id}`;
784
+ }
776
785
  const FALLBACK_META = {
777
786
  id: "",
778
787
  name: "",
@@ -846,16 +855,23 @@ function metaFromLive(entry, caps) {
846
855
  }
847
856
  };
848
857
  }
849
- /** Replace the in-memory live metadata with one successful catalog response. */
850
- function adoptLive(models, tags) {
858
+ /** Replace the in-memory live metadata for one API base with one successful response. */
859
+ function adoptLive(models, tags, base) {
851
860
  const caps = capabilitiesByModel(tags);
852
861
  const ids = [];
853
862
  for (const entry of models) {
854
863
  const meta = metaFromLive(entry, caps.get(str$1(entry.slug) ?? ""));
855
864
  if (meta.id === "") continue;
856
- liveMeta.set(meta.id, meta);
865
+ liveMeta.set(liveKey(base, meta.id), meta);
857
866
  ids.push(meta.id);
858
867
  }
868
+ const keep = new Set(ids);
869
+ const prefix = `${base}|`;
870
+ for (const key of liveMeta.keys()) {
871
+ if (!key.startsWith(prefix)) continue;
872
+ const id = key.slice(prefix.length);
873
+ if (!keep.has(id)) liveMeta.delete(key);
874
+ }
859
875
  return ids;
860
876
  }
861
877
  async function fetchJson(url, config, fetchImpl, signal) {
@@ -885,17 +901,18 @@ async function refreshCatalog(config, fetchImpl = fetch, force = false) {
885
901
  if (Date.now() - cached.at >= CATALOG_TTL_MS) refreshInBackground(config, fetchImpl, key);
886
902
  return cached.ids;
887
903
  }
888
- return refreshNow(config, fetchImpl, key, cached !== void 0);
904
+ return refreshNow(config, fetchImpl, key, cached !== void 0, generation);
889
905
  }
890
- async function refreshNow(config, fetchImpl, key, keepOnFailure) {
906
+ async function refreshNow(config, fetchImpl, key, keepOnFailure, epoch) {
891
907
  const signal = AbortSignal.timeout(CATALOG_TIMEOUT_MS);
892
908
  const [modelsResult, tagsResult] = await Promise.allSettled([fetchJson(modelsUrlFor(config), config, fetchImpl, signal), fetchJson(tagsUrlFor(config), config, fetchImpl, signal)]);
909
+ if (epoch !== generation) return Object.keys(GENERATED_MODELS);
893
910
  if (modelsResult.status === "fulfilled") {
894
911
  const payload = modelsResult.value;
895
912
  const tags = tagsResult.status === "fulfilled" ? tagsResult.value : void 0;
896
913
  const live = Array.isArray(payload.models) ? payload.models : [];
897
914
  if (live.length > 0) {
898
- const ids = adoptLive(live, tags);
915
+ const ids = adoptLive(live, tags, key);
899
916
  if (ids.length > 0) {
900
917
  cache$1.set(key, {
901
918
  at: Date.now(),
@@ -906,6 +923,7 @@ async function refreshNow(config, fetchImpl, key, keepOnFailure) {
906
923
  }
907
924
  const fromData = Array.isArray(payload.data) ? payload.data.map((entry) => str$1(entry.id)).filter((id) => id !== void 0) : [];
908
925
  if (fromData.length > 0) {
926
+ for (const liveKey of liveMeta.keys()) if (liveKey.startsWith(`${key}|`)) liveMeta.delete(liveKey);
909
927
  cache$1.set(key, {
910
928
  at: Date.now(),
911
929
  ids: fromData
@@ -918,7 +936,7 @@ async function refreshNow(config, fetchImpl, key, keepOnFailure) {
918
936
  if (stale !== void 0) return stale.ids;
919
937
  }
920
938
  const fallback = Object.keys(GENERATED_MODELS);
921
- cache$1.set(key, {
939
+ if (epoch === generation) cache$1.set(key, {
922
940
  at: Date.now(),
923
941
  ids: fallback
924
942
  });
@@ -926,17 +944,19 @@ async function refreshNow(config, fetchImpl, key, keepOnFailure) {
926
944
  }
927
945
  function refreshInBackground(config, fetchImpl, key) {
928
946
  if (inflight.has(key)) return;
929
- const task = refreshNow(config, fetchImpl, key, true).catch(() => cache$1.get(key)?.ids ?? Object.keys(GENERATED_MODELS)).finally(() => {
947
+ const task = refreshNow(config, fetchImpl, key, true, generation).catch(() => cache$1.get(key)?.ids ?? Object.keys(GENERATED_MODELS)).finally(() => {
930
948
  inflight.delete(key);
931
949
  });
932
950
  inflight.set(key, task);
933
951
  }
934
952
  function clearCatalogCache() {
953
+ generation += 1;
935
954
  cache$1.clear();
936
955
  inflight.clear();
956
+ liveMeta.clear();
937
957
  }
938
- function baseMeta(id) {
939
- const meta = liveMeta.get(id) ?? GENERATED_MODELS[id];
958
+ function baseMeta(id, base) {
959
+ const meta = liveMeta.get(liveKey(base, id)) ?? GENERATED_MODELS[id];
940
960
  return meta === void 0 ? {
941
961
  ...FALLBACK_META,
942
962
  id,
@@ -944,7 +964,7 @@ function baseMeta(id) {
944
964
  } : meta;
945
965
  }
946
966
  function effectiveModel(id, config) {
947
- const meta = baseMeta(id);
967
+ const meta = baseMeta(id, config.apiBase);
948
968
  return {
949
969
  ...meta,
950
970
  transport: config.modelTransports[id] ?? config.transport,
@@ -961,12 +981,12 @@ function effectiveMaxTokens(meta, config) {
961
981
  function modelVisible(id, config) {
962
982
  const override = config.modelVisibility[id];
963
983
  if (override !== void 0) return override;
964
- if (config.visibleModels.length === 0) return true;
984
+ if (config.showAllModels) return true;
965
985
  return config.visibleModels.includes(id);
966
986
  }
967
987
  function modelAvailable(id, config) {
968
988
  if (!config.filterModelsByPlan) return true;
969
- return baseMeta(id).available !== false;
989
+ return baseMeta(id, config.apiBase).available !== false;
970
990
  }
971
991
  function sortForPicker(ids, config, unfiltered = false) {
972
992
  return ids.filter((id) => unfiltered || modelVisible(id, config) && modelAvailable(id, config)).sort((left, right) => {
@@ -1121,7 +1141,6 @@ async function resolveImages(options, store, signal) {
1121
1141
  const base64 = Buffer.from(request.data).toString("base64");
1122
1142
  images.set(ref.attachmentId, {
1123
1143
  mediaType: request.mediaType,
1124
- base64,
1125
1144
  dataUrl: `data:${request.mediaType};base64,${base64}`,
1126
1145
  width: request.width,
1127
1146
  height: request.height
@@ -1559,7 +1578,6 @@ async function readRequestHeatmap(days, now = Date.now()) {
1559
1578
  const models = /* @__PURE__ */ new Map();
1560
1579
  const accounts = /* @__PURE__ */ new Map();
1561
1580
  const accountMetrics = /* @__PURE__ */ new Map();
1562
- const seen = /* @__PURE__ */ new Set();
1563
1581
  let total = 0;
1564
1582
  let firstAt = null;
1565
1583
  let lastAt = null;
@@ -1594,9 +1612,6 @@ async function readRequestHeatmap(days, now = Date.now()) {
1594
1612
  const provider = typeof parsed.provider === "string" ? parsed.provider : "";
1595
1613
  const model = typeof parsed.model === "string" ? parsed.model : "";
1596
1614
  const account = typeof parsed.account === "string" && parsed.account !== "" ? parsed.account : "unknown";
1597
- const key = `${at}|${provider}|${model}|${account}`;
1598
- if (seen.has(key)) continue;
1599
- seen.add(key);
1600
1615
  const date = new Date(at);
1601
1616
  const hour = date.getHours();
1602
1617
  const day = (date.getDay() + 6) % 7;
@@ -1922,6 +1937,7 @@ async function* parseNativeStream(response, idleTimeoutMs, signal) {
1922
1937
  }
1923
1938
  }
1924
1939
  if (!sawContent) throw new LlmError("Ollama Cloud returned an empty stream", "EMPTY_RESPONSE");
1940
+ throw new LlmError("Ollama Cloud stream ended before done:true", "TRANSPORT");
1925
1941
  }
1926
1942
  // src/transports/sse.ts
1927
1943
  async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
@@ -1956,7 +1972,7 @@ async function* sseData(body, idleTimeoutMs, signal) {
1956
1972
  for (;;) {
1957
1973
  const result = await readWithIdleTimeout(reader, idleTimeoutMs, signal);
1958
1974
  if (result.done) break;
1959
- buffer += decoder.decode(result.value, { stream: true }).replace(/\r\n/g, "\n");
1975
+ buffer = (buffer + decoder.decode(result.value, { stream: true })).replace(/\r\n/g, "\n");
1960
1976
  for (;;) {
1961
1977
  const boundary = buffer.indexOf("\n\n");
1962
1978
  if (boundary < 0) break;
@@ -2020,13 +2036,17 @@ async function* parseChatStream(response, idleTimeoutMs, signal) {
2020
2036
  let finishReason;
2021
2037
  let usage;
2022
2038
  let sawContent = false;
2039
+ let terminated = false;
2023
2040
  for await (const data of sseData(response.body, idleTimeoutMs, signal)) {
2024
- if (data === "[DONE]") break;
2041
+ if (data === "[DONE]") {
2042
+ terminated = true;
2043
+ break;
2044
+ }
2025
2045
  let chunk;
2026
2046
  try {
2027
2047
  chunk = JSON.parse(data);
2028
- } catch {
2029
- continue;
2048
+ } catch (error) {
2049
+ throw new LlmError("Ollama Cloud sent an invalid SSE event", "PROVIDER_HTTP_ERROR", { cause: error });
2030
2050
  }
2031
2051
  const streamError = classifyStreamError(chunk);
2032
2052
  if (streamError !== void 0) throw streamError;
@@ -2104,9 +2124,14 @@ async function* parseChatStream(response, idleTimeoutMs, signal) {
2104
2124
  argumentsDelta
2105
2125
  };
2106
2126
  }
2107
- if (choice.finish_reason !== void 0 && choice.finish_reason !== null) finishReason = choice.finish_reason;
2127
+ if (choice.finish_reason !== void 0 && choice.finish_reason !== null) {
2128
+ finishReason = choice.finish_reason;
2129
+ terminated = true;
2130
+ }
2108
2131
  }
2109
2132
  }
2133
+ if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
2134
+ if (!terminated) throw new LlmError("Ollama Cloud stream ended before [DONE] or finish_reason", "TRANSPORT");
2110
2135
  if (textIndex !== void 0) yield {
2111
2136
  type: "block-end",
2112
2137
  index: textIndex,
@@ -2140,7 +2165,6 @@ async function* parseChatStream(response, idleTimeoutMs, signal) {
2140
2165
  type: "usage",
2141
2166
  usage
2142
2167
  };
2143
- if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
2144
2168
  yield {
2145
2169
  type: "finish",
2146
2170
  reason: finishReasonOf(finishReason, toolCalls.size > 0)
@@ -2256,6 +2280,8 @@ var OllamaCloudAdapter = class extends LlmAdapter {
2256
2280
  stickyWrites = Promise.resolve();
2257
2281
  /** key fingerprint → cooldown deadline and the rejection that caused it. */
2258
2282
  cooldowns = /* @__PURE__ */ new Map();
2283
+ /** key fingerprint + model → short preference mark for model-scoped rejections. */
2284
+ modelCooldowns = /* @__PURE__ */ new Map();
2259
2285
  sticky = /* @__PURE__ */ new Map();
2260
2286
  routing = [];
2261
2287
  warnedTools = false;
@@ -2356,6 +2382,9 @@ var OllamaCloudAdapter = class extends LlmAdapter {
2356
2382
  }
2357
2383
  }
2358
2384
  }
2385
+ const availableKeys = keys.filter((entry) => !this.coolingDown(entry.key));
2386
+ const preferredKeys = availableKeys.filter((entry) => !this.modelCoolingDown(entry.key, options.model));
2387
+ keys = preferredKeys.length > 0 ? preferredKeys : availableKeys;
2359
2388
  const url = endpointFor(model.transport, config);
2360
2389
  let images;
2361
2390
  if (hasImages(options)) {
@@ -2408,6 +2437,12 @@ var OllamaCloudAdapter = class extends LlmAdapter {
2408
2437
  lastError = error;
2409
2438
  logAttempt(false, error.code);
2410
2439
  const canRotate = index < keys.length - 1;
2440
+ if (this.modelScoped(error)) {
2441
+ this.markModelCooldown(entry.key, options.model, error.code === "UNSUPPORTED_MODEL" ? "model-unsupported" : "model-quota");
2442
+ responseFailure = true;
2443
+ if (canRotate) continue;
2444
+ throw error;
2445
+ }
2411
2446
  const retryable = error.code === "INVALID_CREDENTIAL" || error.code === "RATE_LIMIT" || error.code === "QUOTA" || error.code === "SERVER";
2412
2447
  const credentialOrQuota = error.code === "INVALID_CREDENTIAL" || error.code === "RATE_LIMIT" || error.code === "QUOTA";
2413
2448
  if (retryable) {
@@ -2441,8 +2476,13 @@ var OllamaCloudAdapter = class extends LlmAdapter {
2441
2476
  if (started || options.signal?.aborted === true) throw error;
2442
2477
  lastError = classified;
2443
2478
  const transient = this.isTransient(classified);
2444
- const canRotate = index < keys.length - 1 && transient;
2479
+ const canRotate = index < keys.length - 1 && (transient || this.modelScoped(classified));
2445
2480
  const credentialOrQuota = classified.code === "INVALID_CREDENTIAL" || classified.code === "RATE_LIMIT" || classified.code === "QUOTA";
2481
+ if (this.modelScoped(classified)) {
2482
+ this.markModelCooldown(entry.key, options.model, classified.code === "UNSUPPORTED_MODEL" ? "model-unsupported" : "model-quota");
2483
+ if (!canRotate) throw classified;
2484
+ continue;
2485
+ }
2446
2486
  if (!responseFailure && (canRotate || credentialOrQuota)) this.markCooldown(entry.key, 0);
2447
2487
  if (!canRotate) throw classified;
2448
2488
  }
@@ -2583,6 +2623,42 @@ var OllamaCloudAdapter = class extends LlmAdapter {
2583
2623
  }
2584
2624
  return true;
2585
2625
  }
2626
+ modelKey(key, model) {
2627
+ return `${this.keyId(key)}:${model}`;
2628
+ }
2629
+ modelCoolingDown(key, model) {
2630
+ const id = this.modelKey(key, model);
2631
+ const entry = this.modelCooldowns.get(id);
2632
+ if (entry === void 0) return false;
2633
+ if (entry.until <= Date.now()) {
2634
+ this.modelCooldowns.delete(id);
2635
+ return false;
2636
+ }
2637
+ return true;
2638
+ }
2639
+ markModelCooldown(key, model, reason) {
2640
+ const now = Date.now();
2641
+ for (const [id, entry] of this.modelCooldowns) if (entry.until <= now) this.modelCooldowns.delete(id);
2642
+ this.modelCooldowns.set(this.modelKey(key, model), {
2643
+ until: now + 3e5,
2644
+ reason
2645
+ });
2646
+ const overflow = this.modelCooldowns.size - 200;
2647
+ if (overflow <= 0) return;
2648
+ const oldest = [...this.modelCooldowns.entries()].sort((a, b) => a[1].until - b[1].until).slice(0, overflow);
2649
+ for (const [id] of oldest) this.modelCooldowns.delete(id);
2650
+ }
2651
+ /**
2652
+ * A model-scoped rejection says nothing about the key's other models:
2653
+ * `:free` models and other paid models may still work on the same account.
2654
+ * Only account-wide quota, credential and rate-limit responses cool the key.
2655
+ */
2656
+ modelScoped(error) {
2657
+ if (error.code === "UNSUPPORTED_MODEL") return true;
2658
+ if (error.code !== "QUOTA") return false;
2659
+ const detail = error.message.toLowerCase();
2660
+ return detail.includes("model") && (detail.includes("credit") || detail.includes("quota") || detail.includes("allowance") || detail.includes("limit"));
2661
+ }
2586
2662
  /** Current cooldown for one resolved key, if any. */
2587
2663
  accountHealth(key) {
2588
2664
  const id = this.keyId(key);
@@ -2669,15 +2745,18 @@ function offLabel(id, meta) {
2669
2745
  }
2670
2746
  // src/quota-model.ts
2671
2747
  function bar$1(key, label, usedPercent, extra = {}) {
2748
+ const labelKey = key === "included" ? "quotaIncludedCredits" : key === "fiveHour" ? "quotaFiveHour" : key === "weekly" ? "quotaWeekly" : void 0;
2672
2749
  const used = Math.max(0, Math.min(100, usedPercent));
2673
2750
  return {
2674
2751
  key,
2675
2752
  label,
2753
+ ...labelKey === void 0 ? {} : { labelKey },
2676
2754
  usedPercent: used,
2677
2755
  remainingPercent: 100 - used,
2678
2756
  ...extra.resetsAt === void 0 ? {} : { resetsAt: extra.resetsAt },
2679
2757
  ...extra.status === void 0 ? {} : { status: extra.status },
2680
- ...extra.detail === void 0 ? {} : { detail: extra.detail }
2758
+ ...extra.detail === void 0 ? {} : { detail: extra.detail },
2759
+ ...extra.detailValues === void 0 ? {} : { detailValues: extra.detailValues }
2681
2760
  };
2682
2761
  }
2683
2762
  function legacyBar(key, label, window) {
@@ -2694,6 +2773,11 @@ function quotaBars(report) {
2694
2773
  const credits = report.credits;
2695
2774
  return [bar$1("included", "Included credits", credits.includedAllowance > 0 ? (credits.includedAllowance - credits.includedRemaining) / credits.includedAllowance * 100 : 0, {
2696
2775
  detail: `$${credits.includedRemaining.toFixed(2)} of $${credits.includedAllowance.toFixed(2)} left` + (credits.purchased > 0 ? ` · $${credits.purchased.toFixed(2)} purchased` : ""),
2776
+ detailValues: {
2777
+ remaining: credits.includedRemaining,
2778
+ allowance: credits.includedAllowance,
2779
+ purchased: credits.purchased
2780
+ },
2697
2781
  ...credits.periodUntil === void 0 ? {} : { resetsAt: credits.periodUntil }
2698
2782
  })];
2699
2783
  }
@@ -2705,16 +2789,19 @@ function windowRows(report) {
2705
2789
  report.windows.d24 === void 0 ? void 0 : {
2706
2790
  key: "24h",
2707
2791
  label: "24 hours",
2792
+ labelKey: "last24h",
2708
2793
  totals: report.windows.d24
2709
2794
  },
2710
2795
  report.windows.d7 === void 0 ? void 0 : {
2711
2796
  key: "7d",
2712
2797
  label: "7 days",
2798
+ labelKey: "last7d",
2713
2799
  totals: report.windows.d7
2714
2800
  },
2715
2801
  report.windows.d30 === void 0 ? void 0 : {
2716
2802
  key: "30d",
2717
2803
  label: "30 days",
2804
+ labelKey: "last30d",
2718
2805
  totals: report.windows.d30
2719
2806
  }
2720
2807
  ].filter((entry) => entry !== void 0);
@@ -2802,6 +2889,7 @@ async function modelsText(deps) {
2802
2889
  async function testText(deps, model) {
2803
2890
  const started = Date.now();
2804
2891
  let text = "";
2892
+ let reasoning = "";
2805
2893
  try {
2806
2894
  for await (const chunk of deps.adapter.stream({
2807
2895
  provider: PROVIDER,
@@ -2813,11 +2901,17 @@ async function testText(deps, model) {
2813
2901
  text: "Reply with exactly OK."
2814
2902
  }]
2815
2903
  }],
2816
- maxTokens: 16
2904
+ reasoningEffort: ReasoningEffortId("off"),
2905
+ maxTokens: 64
2817
2906
  })) if (chunk.type === "text-delta") text += chunk.text;
2907
+ else if (chunk.type === "reasoning-delta") reasoning += chunk.text;
2908
+ if (text === "") return {
2909
+ ok: false,
2910
+ text: `${model}: empty reply${reasoning === "" ? "" : ` (reasoning only: ${reasoning.slice(0, 80)})`}`
2911
+ };
2818
2912
  return {
2819
2913
  ok: true,
2820
- text: `${model}: OK in ${Date.now() - started} ms${text === "" ? "" : ` · ${text.slice(0, 80)}`}`
2914
+ text: `${model}: OK in ${Date.now() - started} ms · ${text.slice(0, 80)}`
2821
2915
  };
2822
2916
  } catch (error) {
2823
2917
  return {
@@ -3526,8 +3620,26 @@ async function readModelAnalytics(days = 30, priceOverrides = {}) {
3526
3620
  if (cached !== void 0 && Date.now() - cached.at < CACHE_TTL_MS) return cached.report;
3527
3621
  const dir = projectionDirectory();
3528
3622
  const sessions = [];
3623
+ let truncated = false;
3624
+ let totalFiles = 0;
3529
3625
  try {
3530
- const files = (await readdir(dir, { withFileTypes: true })).filter((entry) => entry.isFile() && entry.name.endsWith(".json")).slice(0, MAX_FILES);
3626
+ const candidates = (await readdir(dir, { withFileTypes: true })).filter((entry) => entry.isFile() && entry.name.endsWith(".json"));
3627
+ totalFiles = candidates.length;
3628
+ truncated = candidates.length > MAX_FILES;
3629
+ let files = candidates;
3630
+ if (truncated) files = (await Promise.all(candidates.map(async (entry) => {
3631
+ try {
3632
+ return {
3633
+ entry,
3634
+ at: (await stat(join(dir, entry.name))).mtimeMs
3635
+ };
3636
+ } catch {
3637
+ return {
3638
+ entry,
3639
+ at: 0
3640
+ };
3641
+ }
3642
+ }))).sort((left, right) => right.at - left.at).slice(0, MAX_FILES).map((item) => item.entry);
3531
3643
  let cursor = 0;
3532
3644
  const workers = Array.from({ length: Math.min(16, files.length) }, async () => {
3533
3645
  while (true) {
@@ -3550,7 +3662,11 @@ async function readModelAnalytics(days = 30, priceOverrides = {}) {
3550
3662
  } catch (error) {
3551
3663
  bestEffortFailed("projection cache is unavailable", error);
3552
3664
  }
3553
- const report = aggregateSessions(sessions, clamped, Date.now(), priceOverrides);
3665
+ const report = {
3666
+ ...aggregateSessions(sessions, clamped, Date.now(), priceOverrides),
3667
+ truncated,
3668
+ totalFiles
3669
+ };
3554
3670
  cache.set(cacheKey, {
3555
3671
  at: Date.now(),
3556
3672
  report
@@ -3570,6 +3686,7 @@ function publicConfig(config) {
3570
3686
  requestTimeoutMs: config.requestTimeoutMs,
3571
3687
  streamIdleTimeoutMs: config.streamIdleTimeoutMs,
3572
3688
  visibleModels: config.visibleModels,
3689
+ showAllModels: config.showAllModels,
3573
3690
  modelVisibility: config.modelVisibility,
3574
3691
  filterModelsByPlan: config.filterModelsByPlan,
3575
3692
  contextWindow: config.contextWindow ?? null,
@@ -3630,6 +3747,16 @@ async function probeService(config, fetchImpl = fetch) {
3630
3747
  }
3631
3748
  // src/routes-values.ts
3632
3749
  const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
3750
+ const CREDENTIAL_REF_PATTERN = /^OLLAMA_[A-Z0-9_]+$/;
3751
+ /** The plugin may read and write only its own credential namespace. */
3752
+ function normalizeCredentialRef(value) {
3753
+ if (typeof value !== "string") return void 0;
3754
+ const ref = value.trim();
3755
+ return CREDENTIAL_REF_PATTERN.test(ref) ? ref : void 0;
3756
+ }
3757
+ function isCredentialRef(value) {
3758
+ return normalizeCredentialRef(value) !== void 0;
3759
+ }
3633
3760
  function cleanAccounts(value) {
3634
3761
  if (!Array.isArray(value)) return [];
3635
3762
  const out = [];
@@ -3715,14 +3842,35 @@ function sourceHost(value) {
3715
3842
  }
3716
3843
  /**
3717
3844
  * The Desktop renderer runs on `dsh-app://app`. Its host fetch has no Origin
3718
- * or Referer, but it carries the signed host auth cookie and the desktop user
3719
- * agent. Require both so a plain LAN client without either stays fail-closed.
3845
+ * or Referer, but it carries the host auth cookie and the desktop user agent.
3846
+ * The names alone are not proof: verify the cookie against the host's own
3847
+ * authenticated API, which rejects forged values with 401/403.
3720
3848
  */
3721
3849
  function desktopShellRequest(req) {
3722
3850
  const agent = req.headers["user-agent"] ?? "";
3723
3851
  const cookie = req.headers.cookie ?? "";
3724
3852
  return agent.includes("@deepseek-ai/dsh-desktop/") && cookie.includes("dsh-auth-");
3725
3853
  }
3854
+ async function defaultVerifyHost(req) {
3855
+ const host = req.headers.host;
3856
+ const cookie = req.headers.cookie;
3857
+ if (host === void 0 || host === "" || cookie === void 0 || cookie === "") return false;
3858
+ try {
3859
+ const response = await fetch(`http://${host}/api/version`, {
3860
+ headers: {
3861
+ cookie,
3862
+ accept: "application/json"
3863
+ },
3864
+ redirect: "manual",
3865
+ signal: AbortSignal.timeout(2500)
3866
+ });
3867
+ await response.body?.cancel();
3868
+ if (response.status >= 300 && response.status < 400) return false;
3869
+ return response.status < 500 && response.status !== 401 && response.status !== 403;
3870
+ } catch {
3871
+ return false;
3872
+ }
3873
+ }
3726
3874
  /**
3727
3875
  * Write routes must prove a same-origin browser context. A LAN bridge that
3728
3876
  * rewrites Host and socket to loopback would otherwise let any non-browser
@@ -3734,7 +3882,6 @@ function writeSourceError(req) {
3734
3882
  if (host === void 0 || host === "") return "request host required";
3735
3883
  const origin = req.headers.origin;
3736
3884
  if (origin !== void 0 && origin !== "") return sourceHost(origin) === host ? void 0 : "cross-origin request refused";
3737
- if (desktopShellRequest(req)) return void 0;
3738
3885
  const referer = req.headers.referer;
3739
3886
  if (referer === void 0 || referer === "") return "request origin required";
3740
3887
  return sourceHost(referer) === host ? void 0 : "cross-origin request refused";
@@ -3780,7 +3927,7 @@ async function readJson(req) {
3780
3927
  if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("body must be a JSON object");
3781
3928
  return parsed;
3782
3929
  }
3783
- function requirePost(req, res) {
3930
+ async function requirePost(req, res, verifyDesktop) {
3784
3931
  if (req.method !== "POST") {
3785
3932
  json(res, 405, { error: "POST required" });
3786
3933
  return false;
@@ -3789,16 +3936,23 @@ function requirePost(req, res) {
3789
3936
  json(res, 403, { error: "non-loopback Host refused" });
3790
3937
  return false;
3791
3938
  }
3792
- const sourceError = writeSourceError(req);
3793
- if (sourceError !== void 0) {
3794
- json(res, 403, { error: sourceError });
3795
- return false;
3796
- }
3797
3939
  const remote = req.socket.remoteAddress ?? "";
3798
3940
  if (!(remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1")) {
3799
3941
  json(res, 403, { error: "write endpoints are restricted to the local machine" });
3800
3942
  return false;
3801
3943
  }
3944
+ if (desktopShellRequest(req)) {
3945
+ if (!await verifyDesktop(req)) {
3946
+ json(res, 403, { error: "desktop authentication failed" });
3947
+ return false;
3948
+ }
3949
+ } else {
3950
+ const sourceError = writeSourceError(req);
3951
+ if (sourceError !== void 0) {
3952
+ json(res, 403, { error: sourceError });
3953
+ return false;
3954
+ }
3955
+ }
3802
3956
  if (!(req.headers["content-type"] ?? "").includes("application/json")) {
3803
3957
  json(res, 415, { error: "application/json required" });
3804
3958
  return false;
@@ -3824,9 +3978,10 @@ function settingsRevision(ctx) {
3824
3978
  * key in the shared credential store.
3825
3979
  */
3826
3980
  function canWriteCredentialRef(config, ref) {
3981
+ if (!isCredentialRef(ref)) return false;
3827
3982
  if (ref === config.apiKeyEnv) return true;
3828
3983
  for (const account of config.accounts) if (typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() === ref) return true;
3829
- return /^OLLAMA_[A-Z0-9_]+$/.test(ref);
3984
+ return true;
3830
3985
  }
3831
3986
  function configOps(patch) {
3832
3987
  const ops = [];
@@ -3841,7 +3996,14 @@ function configOps(patch) {
3841
3996
  value
3842
3997
  });
3843
3998
  };
3844
- if ("apiKeyEnv" in patch) set("apiKeyEnv", patch.apiKeyEnv);
3999
+ if ("apiKeyEnv" in patch) {
4000
+ if (patch.apiKeyEnv === null || patch.apiKeyEnv === void 0) set("apiKeyEnv", void 0);
4001
+ else {
4002
+ const ref = normalizeCredentialRef(patch.apiKeyEnv);
4003
+ if (ref === void 0) throw new Error("invalid credential reference");
4004
+ set("apiKeyEnv", ref);
4005
+ }
4006
+ }
3845
4007
  if ("apiBase" in patch) set("apiBase", patch.apiBase);
3846
4008
  if ("openAiBase" in patch) set("openAiBase", patch.openAiBase);
3847
4009
  if ("usageUrl" in patch) set("usageUrl", patch.usageUrl);
@@ -3851,12 +4013,17 @@ function configOps(patch) {
3851
4013
  if ("requestTimeoutMs" in patch) set("requestTimeoutMs", patch.requestTimeoutMs);
3852
4014
  if ("streamIdleTimeoutMs" in patch) set("streamIdleTimeoutMs", patch.streamIdleTimeoutMs);
3853
4015
  if ("visibleModels" in patch) set("visibleModels", cleanStringArray(patch.visibleModels));
4016
+ if ("showAllModels" in patch) set("showAllModels", patch.showAllModels === true);
3854
4017
  if ("modelVisibility" in patch) set("modelVisibility", cleanBooleanRecord(patch.modelVisibility));
3855
4018
  if ("filterModelsByPlan" in patch) set("filterModelsByPlan", patch.filterModelsByPlan === true);
3856
4019
  if ("contextWindow" in patch) set("contextWindow", patch.contextWindow);
3857
4020
  if ("contextWindowOverrides" in patch) set("contextWindowOverrides", cleanNumberRecord(patch.contextWindowOverrides));
3858
4021
  if ("maxOutputTokens" in patch) set("maxOutputTokens", patch.maxOutputTokens);
3859
- if ("accounts" in patch) set("accounts", cleanAccounts(patch.accounts));
4022
+ if ("accounts" in patch) {
4023
+ const accounts = cleanAccounts(patch.accounts);
4024
+ for (const account of accounts) if (account.apiKeyEnv !== "" && !isCredentialRef(account.apiKeyEnv)) throw new Error("invalid credential reference");
4025
+ set("accounts", accounts);
4026
+ }
3860
4027
  if ("modelAccountRules" in patch) set("modelAccountRules", cleanModelAccountRules(patch.modelAccountRules));
3861
4028
  if ("stickySession" in patch) set("stickySession", patch.stickySession === true);
3862
4029
  if ("activeAccount" in patch) set("activeAccount", patch.activeAccount);
@@ -3982,6 +4149,7 @@ async function refreshAndState(ctx, deps, res) {
3982
4149
  /** Register the Host HTTP surface consumed by the browser settings page. */
3983
4150
  function applyRoutes(ctx, web, deps) {
3984
4151
  const disposers = [];
4152
+ const verifyHost = deps.verifyHost ?? deps.verifyDesktop ?? defaultVerifyHost;
3985
4153
  const route = (path, handler) => {
3986
4154
  disposers.push(web.register({
3987
4155
  kind: "exact",
@@ -4000,8 +4168,16 @@ function applyRoutes(ctx, web, deps) {
4000
4168
  }
4001
4169
  return true;
4002
4170
  };
4171
+ const privateRead = async (req, res) => {
4172
+ if (!readOnly(req, res)) return false;
4173
+ if (!await verifyHost(req)) {
4174
+ json(res, 403, { error: "authentication required" });
4175
+ return false;
4176
+ }
4177
+ return true;
4178
+ };
4003
4179
  route("/dsh-ollama/state", async (req, res) => {
4004
- if (!readOnly(req, res)) return;
4180
+ if (!await privateRead(req, res)) return;
4005
4181
  await refreshAndState(ctx, deps, res);
4006
4182
  });
4007
4183
  route("/dsh-ollama/health", async (req, res) => {
@@ -4034,7 +4210,7 @@ function applyRoutes(ctx, web, deps) {
4034
4210
  }
4035
4211
  });
4036
4212
  route("/dsh-ollama/analytics", async (req, res) => {
4037
- if (!readOnly(req, res)) return;
4213
+ if (!await privateRead(req, res)) return;
4038
4214
  const url = new URL(req.url ?? "/", "http://localhost");
4039
4215
  const requested = Number(url.searchParams.get("days") ?? "30");
4040
4216
  const days = Number.isFinite(requested) ? Math.max(1, Math.min(365, Math.trunc(requested))) : 30;
@@ -4050,7 +4226,7 @@ function applyRoutes(ctx, web, deps) {
4050
4226
  }
4051
4227
  });
4052
4228
  route("/dsh-ollama/usage", async (req, res) => {
4053
- if (!readOnly(req, res)) return;
4229
+ if (!await privateRead(req, res)) return;
4054
4230
  const raw = new URL(req.url ?? "/", "http://localhost").searchParams.get("range") ?? "7d";
4055
4231
  const range = raw === "24h" || raw === "30d" ? raw : "7d";
4056
4232
  try {
@@ -4063,7 +4239,7 @@ function applyRoutes(ctx, web, deps) {
4063
4239
  }
4064
4240
  });
4065
4241
  route("/dsh-ollama/balance", async (req, res) => {
4066
- if (!readOnly(req, res)) return;
4242
+ if (!await privateRead(req, res)) return;
4067
4243
  try {
4068
4244
  json(res, 200, await deps.usage.read() ?? {
4069
4245
  error: "balance unavailable",
@@ -4074,7 +4250,7 @@ function applyRoutes(ctx, web, deps) {
4074
4250
  }
4075
4251
  });
4076
4252
  route("/dsh-ollama/config", async (req, res) => {
4077
- if (!requirePost(req, res)) return;
4253
+ if (!await requirePost(req, res, verifyHost)) return;
4078
4254
  const settings = getSettings(ctx);
4079
4255
  if (settings?.mutate === void 0) {
4080
4256
  json(res, 503, { error: "settings service unavailable" });
@@ -4086,7 +4262,14 @@ function applyRoutes(ctx, web, deps) {
4086
4262
  }
4087
4263
  try {
4088
4264
  const body = await readJson(req);
4089
- const ops = configOps(typeof body.patch === "object" && body.patch !== null && !Array.isArray(body.patch) ? body.patch : body);
4265
+ const patch = typeof body.patch === "object" && body.patch !== null && !Array.isArray(body.patch) ? body.patch : body;
4266
+ let ops;
4267
+ try {
4268
+ ops = configOps(patch);
4269
+ } catch (error) {
4270
+ json(res, 400, { error: error instanceof Error ? error.message : "invalid patch" });
4271
+ return;
4272
+ }
4090
4273
  if (ops.length === 0) {
4091
4274
  json(res, 400, { error: "no known fields in patch" });
4092
4275
  return;
@@ -4100,7 +4283,7 @@ function applyRoutes(ctx, web, deps) {
4100
4283
  }
4101
4284
  });
4102
4285
  route("/dsh-ollama/key", async (req, res) => {
4103
- if (!requirePost(req, res)) return;
4286
+ if (!await requirePost(req, res, verifyHost)) return;
4104
4287
  const credentials = getCredentials(ctx);
4105
4288
  if (credentials === void 0) {
4106
4289
  json(res, 503, { error: "credentials service unavailable" });
@@ -4128,7 +4311,7 @@ function applyRoutes(ctx, web, deps) {
4128
4311
  }
4129
4312
  });
4130
4313
  route("/dsh-ollama/key/clear", async (req, res) => {
4131
- if (!requirePost(req, res)) return;
4314
+ if (!await requirePost(req, res, verifyHost)) return;
4132
4315
  const credentials = getCredentials(ctx);
4133
4316
  if (credentials === void 0) {
4134
4317
  json(res, 503, { error: "credentials service unavailable" });
@@ -4151,12 +4334,12 @@ function applyRoutes(ctx, web, deps) {
4151
4334
  }
4152
4335
  });
4153
4336
  route("/dsh-ollama/refresh", async (req, res) => {
4154
- if (!requirePost(req, res)) return;
4337
+ if (!await requirePost(req, res, verifyHost)) return;
4155
4338
  deps.usage.clear();
4156
4339
  await refreshAndState(ctx, deps, res);
4157
4340
  });
4158
4341
  route("/dsh-ollama/models/refresh", async (req, res) => {
4159
- if (!requirePost(req, res)) return;
4342
+ if (!await requirePost(req, res, verifyHost)) return;
4160
4343
  clearCatalogCache();
4161
4344
  deps.usage.clear();
4162
4345
  try {
@@ -4167,7 +4350,7 @@ function applyRoutes(ctx, web, deps) {
4167
4350
  await refreshAndState(ctx, deps, res);
4168
4351
  });
4169
4352
  route("/dsh-ollama/test", async (req, res) => {
4170
- if (!requirePost(req, res)) return;
4353
+ if (!await requirePost(req, res, verifyHost)) return;
4171
4354
  let body;
4172
4355
  try {
4173
4356
  body = await readJson(req);
@@ -4558,7 +4741,9 @@ function normalizeUsage(payload, range) {
4558
4741
  /** Cached reader for Ollama Cloud balance and usage. Fail-open by design. */
4559
4742
  function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4560
4743
  const cache = /* @__PURE__ */ new Map();
4744
+ const inflight = /* @__PURE__ */ new Map();
4561
4745
  const usageCalls = [];
4746
+ let generation = 0;
4562
4747
  const keyFor = async (explicit) => {
4563
4748
  if (explicit !== void 0 && explicit !== "") return explicit;
4564
4749
  try {
@@ -4594,11 +4779,12 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4594
4779
  return;
4595
4780
  }
4596
4781
  };
4597
- const readUsage = async (key, range) => {
4782
+ const readUsage = async (key, range, epoch) => {
4598
4783
  const current = config();
4599
4784
  const cacheKey = `${fingerprint(key)}:${range}`;
4600
4785
  const hit = cached(cacheKey);
4601
- if (hit?.report !== void 0) {
4786
+ if (hit !== void 0) {
4787
+ if (hit.report === void 0) return void 0;
4602
4788
  const totals = hit.report.windows[range === "24h" ? "d24" : range === "7d" ? "d7" : "d30"];
4603
4789
  return totals === void 0 ? void 0 : {
4604
4790
  totals,
@@ -4617,7 +4803,7 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4617
4803
  url.searchParams.set("group_by", "model");
4618
4804
  const response = await request(url.toString(), key);
4619
4805
  if (!response.ok) {
4620
- cache.set(cacheKey, {
4806
+ if (epoch === generation) cache.set(cacheKey, {
4621
4807
  error: `usage endpoint returned HTTP ${response.status}`,
4622
4808
  checkedAt: Date.now()
4623
4809
  });
@@ -4625,19 +4811,23 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4625
4811
  }
4626
4812
  return normalizeUsage(await response.json(), range);
4627
4813
  } catch (error) {
4628
- cache.set(cacheKey, {
4814
+ if (epoch === generation) cache.set(cacheKey, {
4629
4815
  error: error instanceof Error ? error.message : "usage request failed",
4630
4816
  checkedAt: Date.now()
4631
4817
  });
4632
4818
  return;
4633
4819
  }
4634
4820
  };
4635
- const compose = async (key, range) => {
4636
- const [balance, usage] = await Promise.all([readBalance(key), readUsage(key, range)]);
4637
- if (balance === void 0 && usage === void 0) return {
4638
- error: "usage and balance are unavailable",
4639
- checkedAt: Date.now()
4640
- };
4821
+ const compose = async (key, range, epoch) => {
4822
+ const [balance, usage] = await Promise.all([readBalance(key), readUsage(key, range, epoch)]);
4823
+ if (balance === void 0 && usage === void 0) {
4824
+ const result = {
4825
+ error: "usage and balance are unavailable",
4826
+ checkedAt: Date.now()
4827
+ };
4828
+ if (epoch === generation) cache.set(`${fingerprint(key)}:report:${range}`, result);
4829
+ return result;
4830
+ }
4641
4831
  const result = {
4642
4832
  report: {
4643
4833
  kind: balance?.kind ?? "credits",
@@ -4653,8 +4843,10 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4653
4843
  },
4654
4844
  checkedAt: Date.now()
4655
4845
  };
4656
- cache.set(`${fingerprint(key)}:report:${range}`, result);
4657
- if (usage !== void 0) cache.set(`${fingerprint(key)}:${range}`, result);
4846
+ if (epoch === generation) {
4847
+ cache.set(`${fingerprint(key)}:report:${range}`, result);
4848
+ if (usage !== void 0) cache.set(`${fingerprint(key)}:${range}`, result);
4849
+ }
4658
4850
  return result;
4659
4851
  };
4660
4852
  const reader = async (range, explicitKey) => {
@@ -4665,13 +4857,20 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
4665
4857
  };
4666
4858
  const cacheKey = `${fingerprint(key)}:report:${range}`;
4667
4859
  const hit = cache.get(cacheKey);
4668
- if (hit?.report !== void 0 && Date.now() - hit.checkedAt < BALANCE_TTL_MS) return hit;
4669
- return compose(key, range);
4860
+ const ttl = hit?.report === void 0 ? FAILURE_TTL_MS : BALANCE_TTL_MS;
4861
+ if (hit !== void 0 && Date.now() - hit.checkedAt < ttl) return hit;
4862
+ const existing = inflight.get(cacheKey);
4863
+ if (existing !== void 0) return existing;
4864
+ const task = compose(key, range, generation).finally(() => inflight.delete(cacheKey));
4865
+ inflight.set(cacheKey, task);
4866
+ return task;
4670
4867
  };
4671
4868
  reader.read = (key) => reader("7d", key);
4672
4869
  reader.usage = (range, key) => reader(range, key);
4673
4870
  reader.clear = () => {
4871
+ generation += 1;
4674
4872
  cache.clear();
4873
+ inflight.clear();
4675
4874
  };
4676
4875
  return reader;
4677
4876
  }