@goodandready/dsh-ollama-cloud-provider 0.1.25 → 0.1.27

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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.27 — 2026-11-05
4
+
5
+ - Usage reader keeps the last good snapshot per key/range and returns it as
6
+ `stale` when balance and usage later fail; the card marks stale data instead
7
+ of losing the meters.
8
+ - Usage requests retry transient/429/5xx failures with bounded backoff.
9
+ - Added session/weekly/monthly aliases and per-window model activity
10
+ (`byWindow`) alongside the existing 24h/7d/30d windows and sidebar meters.
11
+ - Added last-good, retry and window tests; 138/138.
12
+
13
+ ## 0.1.26 — 2026-11-05
14
+
15
+ - Added configurable pre-response retry/backoff (`retryMaxRetries`, base/max
16
+ delay, jitter). Only headers/pre-first-token failures retry; post-token
17
+ streams never retry, and account rotation is not duplicated.
18
+ - Sanitized endpoint URLs, provider errors, status errors and web errors:
19
+ userinfo and secret query parameters are stripped, control characters and
20
+ tokens are redacted, and long messages are bounded before logs/UI.
21
+ - Added retry/backoff and sanitizer tests, plus a 503 retry adapter test;
22
+ 137/137.
23
+
3
24
  ## 0.1.25 — 2026-11-05
4
25
 
5
26
  - Added safe model discovery against an unsaved endpoint with an optional
package/README.md CHANGED
@@ -137,17 +137,17 @@ npm pack
137
137
  Install the published package:
138
138
 
139
139
  ```bash
140
- dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.25
140
+ dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.27
141
141
  # DSH Desktop:
142
- dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.25
142
+ dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.27
143
143
  ```
144
144
 
145
145
  Or install the packed tarball into a profile:
146
146
 
147
147
  ```bash
148
- dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
148
+ dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
149
149
  # DSH Desktop:
150
- dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
150
+ dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
151
151
  ```
152
152
 
153
153
  Restart dsh (or DSH Desktop), then pick **Ollama Cloud** in the model picker.
package/README.ru.md CHANGED
@@ -103,17 +103,17 @@ npm pack
103
103
  Установка опубликованного пакета из npm:
104
104
 
105
105
  ```bash
106
- dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.25
106
+ dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.27
107
107
  # DSH Desktop:
108
- dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.25
108
+ dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.27
109
109
  ```
110
110
 
111
111
  Либо установка из упакованного tarball:
112
112
 
113
113
  ```bash
114
- dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
114
+ dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
115
115
  # DSH Desktop:
116
- dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
116
+ dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
117
117
  ```
118
118
 
119
119
  После установки перезапустите dsh (или DSH Desktop) и выберите **Ollama Cloud** в списке моделей.
package/README.zh.md CHANGED
@@ -102,17 +102,17 @@ npm pack
102
102
  从 npm 安装已发布版本:
103
103
 
104
104
  ```bash
105
- dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.25
105
+ dsh plugin --profile web add @goodandready/dsh-ollama-cloud-provider@0.1.27
106
106
  # DSH 桌面端:
107
- dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.25
107
+ dsh plugin --profile desktop add @goodandready/dsh-ollama-cloud-provider@0.1.27
108
108
  ```
109
109
 
110
110
  或通过打包文件安装:
111
111
 
112
112
  ```bash
113
- dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
113
+ dsh plugin --profile web add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
114
114
  # DSH 桌面端:
115
- dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.25.tgz
115
+ dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud-provider-0.1.27.tgz
116
116
  ```
117
117
 
118
118
  安装完成后重启 dsh 或 DSH 桌面端,在模型列表中选择 **Ollama Cloud** 即可。
package/lib/client.js CHANGED
@@ -40,6 +40,7 @@ window.__ModuleLoader__.load({
40
40
  quotaCardName: "Ollama Cloud",
41
41
  noUsage: "No quota data",
42
42
  usageError: "Quota unavailable",
43
+ lastGoodUsage: "Showing the last good usage snapshot.",
43
44
  quotaHint: "Balance and usage come from the official /api/balance and /api/usage endpoints. Spend is metered in dollars, not requests.",
44
45
  quotaExhausted: "Included credits are nearly exhausted.",
45
46
  quotaWarning: "More than 80% of the included credits are used.",
@@ -122,6 +123,10 @@ window.__ModuleLoader__.load({
122
123
  streamIdleTimeoutMs: "Stream idle timeout (ms)",
123
124
  attemptTimeoutMs: "Attempt budget (ms, 0 = off)",
124
125
  attemptTimeoutHint: "Whole-attempt timeout; a fresh budget is created for every retry.",
126
+ retryMaxRetries: "Retry attempts",
127
+ retryBaseDelayMs: "Retry base delay (ms)",
128
+ retryMaxDelayMs: "Retry max delay (ms)",
129
+ retryJitter: "Retry jitter (0–1)",
125
130
  maxOutputTokens: "Output limit (0 = provider default)",
126
131
  contextWindow: "Context window override",
127
132
  discover: "Discover draft endpoint",
@@ -287,6 +292,7 @@ window.__ModuleLoader__.load({
287
292
  quotaCardName: "Ollama Cloud",
288
293
  noUsage: "没有配额数据",
289
294
  usageError: "配额不可用",
295
+ lastGoodUsage: "正在显示上次可用的用量快照。",
290
296
  quotaHint: "余额和用量来自官方 /api/balance 与 /api/usage。费用按美元计量,而不是按请求数。",
291
297
  quotaExhausted: "包含额度即将用完。",
292
298
  quotaWarning: "已使用超过 80% 的包含额度。",
@@ -369,6 +375,10 @@ window.__ModuleLoader__.load({
369
375
  streamIdleTimeoutMs: "流空闲超时(毫秒)",
370
376
  attemptTimeoutMs: "单次尝试预算(毫秒,0 = 关闭)",
371
377
  attemptTimeoutHint: "整次尝试超时;每次重试都会创建新的预算。",
378
+ retryMaxRetries: "重试次数",
379
+ retryBaseDelayMs: "重试基础延迟(毫秒)",
380
+ retryMaxDelayMs: "重试最大延迟(毫秒)",
381
+ retryJitter: "重试抖动(0–1)",
372
382
  maxOutputTokens: "输出上限(0 = 提供方默认)",
373
383
  contextWindow: "上下文窗口覆盖",
374
384
  discover: "发现草稿端点",
@@ -1369,7 +1379,7 @@ window.__ModuleLoader__.load({
1369
1379
  const worst = worstUsedPercent(report);
1370
1380
  const plan = state?.config.planLabel?.trim() ?? "";
1371
1381
  const name = plan === "" ? t("quotaCardName") : `${t("quotaCardName")} · ${plan}`;
1372
- const detail = bars.map((entry) => `${entry.labelKey === void 0 ? entry.label : t(entry.labelKey)}: ${entry.remainingPercent.toFixed(0)}% ${t("remaining")}`).join(" · ");
1382
+ const detail = bars.map((entry) => `${entry.labelKey === void 0 ? entry.label : t(entry.labelKey)}: ${entry.remainingPercent.toFixed(0)}% ${t("remaining")}`).join(" · ") + (state?.usage?.stale === true ? ` · ${t("lastGoodUsage")}` : "");
1373
1383
  return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
1374
1384
  type: "button",
1375
1385
  className: "ocl-foot",
@@ -1452,6 +1462,7 @@ window.__ModuleLoader__.load({
1452
1462
  const bars = quotaBars(activeReport);
1453
1463
  const rows = windowRows(activeReport);
1454
1464
  const activeAccount = state?.accounts.find((account) => account.active);
1465
+ const stale = usage?.stale === true || state?.usage?.stale === true;
1455
1466
  const refresh = () => {
1456
1467
  setBusy(true);
1457
1468
  refreshState().then(() => load()).finally(() => setBusy(false));
@@ -1543,6 +1554,10 @@ window.__ModuleLoader__.load({
1543
1554
  usage.error
1544
1555
  ]
1545
1556
  }),
1557
+ stale ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", {
1558
+ className: "ocl-notice",
1559
+ children: t("lastGoodUsage")
1560
+ }) : null,
1546
1561
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(UsageBars, {
1547
1562
  bars,
1548
1563
  t
@@ -1732,6 +1747,10 @@ window.__ModuleLoader__.load({
1732
1747
  requestTimeoutMs: String(state.config.requestTimeoutMs),
1733
1748
  streamIdleTimeoutMs: String(state.config.streamIdleTimeoutMs),
1734
1749
  attemptTimeoutMs: String(state.config.attemptTimeoutMs ?? 0),
1750
+ retryMaxRetries: String(state.config.retryMaxRetries ?? 0),
1751
+ retryBaseDelayMs: String(state.config.retryBaseDelayMs ?? 250),
1752
+ retryMaxDelayMs: String(state.config.retryMaxDelayMs ?? 2e3),
1753
+ retryJitter: String(state.config.retryJitter ?? .2),
1735
1754
  maxOutputTokens: textOf(state.config.maxOutputTokens),
1736
1755
  contextWindow: textOf(state.config.contextWindow),
1737
1756
  temperature: textOf(state.config.temperature),
@@ -1814,6 +1833,10 @@ window.__ModuleLoader__.load({
1814
1833
  requestTimeoutMs: numberOrNull(draft.requestTimeoutMs) ?? 3e5,
1815
1834
  streamIdleTimeoutMs: numberOrNull(draft.streamIdleTimeoutMs) ?? 3e5,
1816
1835
  attemptTimeoutMs: boundedNumber(draft.attemptTimeoutMs, 0, 36e5, true) ?? 0,
1836
+ retryMaxRetries: boundedNumber(draft.retryMaxRetries, 0, 10, true) ?? 0,
1837
+ retryBaseDelayMs: boundedNumber(draft.retryBaseDelayMs, 0, 6e4, true) ?? 250,
1838
+ retryMaxDelayMs: boundedNumber(draft.retryMaxDelayMs, 0, 12e4, true) ?? 2e3,
1839
+ retryJitter: boundedNumber(draft.retryJitter, 0, 1, false) ?? .2,
1817
1840
  maxOutputTokens: numberOrNull(draft.maxOutputTokens),
1818
1841
  contextWindow: numberOrNull(draft.contextWindow),
1819
1842
  temperature: boundedNumber(draft.temperature, 0, 2, false),
@@ -3717,6 +3740,30 @@ window.__ModuleLoader__.load({
3717
3740
  value: draft.attemptTimeoutMs,
3718
3741
  onChange: (value) => update({ attemptTimeoutMs: value })
3719
3742
  }),
3743
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
3744
+ label: t("retryMaxRetries"),
3745
+ type: "number",
3746
+ value: draft.retryMaxRetries,
3747
+ onChange: (value) => update({ retryMaxRetries: value })
3748
+ }),
3749
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
3750
+ label: t("retryBaseDelayMs"),
3751
+ type: "number",
3752
+ value: draft.retryBaseDelayMs,
3753
+ onChange: (value) => update({ retryBaseDelayMs: value })
3754
+ }),
3755
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
3756
+ label: t("retryMaxDelayMs"),
3757
+ type: "number",
3758
+ value: draft.retryMaxDelayMs,
3759
+ onChange: (value) => update({ retryMaxDelayMs: value })
3760
+ }),
3761
+ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
3762
+ label: t("retryJitter"),
3763
+ type: "number",
3764
+ value: draft.retryJitter,
3765
+ onChange: (value) => update({ retryJitter: value })
3766
+ }),
3720
3767
  /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Field, {
3721
3768
  label: t("maxOutputTokens"),
3722
3769
  type: "number",
package/lib/index.d.ts CHANGED
@@ -66,6 +66,12 @@ interface Config {
66
66
  streamIdleTimeoutMs?: number;
67
67
  /** Whole-attempt budget; 0 disables the independent budget. */
68
68
  attemptTimeoutMs?: number;
69
+ /** Pre-response retry count; 0 disables internal retries. */
70
+ retryMaxRetries?: number;
71
+ retryBaseDelayMs?: number;
72
+ retryMaxDelayMs?: number;
73
+ /** Jitter fraction, 0..1. */
74
+ retryJitter?: number;
69
75
  /** Visible-model allowlist; empty with `showAllModels:false` means none. */
70
76
  visibleModels?: string[];
71
77
  /** Explicit all-models switch; absent means derive from `visibleModels`. */
@@ -131,6 +137,10 @@ interface ResolvedConfig {
131
137
  requestTimeoutMs: number;
132
138
  streamIdleTimeoutMs: number;
133
139
  attemptTimeoutMs: number;
140
+ retryMaxRetries: number;
141
+ retryBaseDelayMs: number;
142
+ retryMaxDelayMs: number;
143
+ retryJitter: number;
134
144
  visibleModels: readonly string[];
135
145
  showAllModels: boolean;
136
146
  modelVisibility: Readonly<Record<string, boolean>>;
@@ -335,16 +345,23 @@ interface UsageReport {
335
345
  d24?: UsageTotals;
336
346
  d7?: UsageTotals;
337
347
  d30?: UsageTotals;
348
+ session?: UsageTotals;
349
+ weekly?: UsageTotals;
350
+ monthly?: UsageTotals;
338
351
  };
339
352
  byModel: Array<{
340
353
  model: string;
341
354
  } & UsageTotals>;
355
+ byWindow?: Partial<Record<UsageRange, Array<{
356
+ model: string;
357
+ } & UsageTotals>>>;
342
358
  range: UsageRange;
343
359
  }
344
360
  interface UsageResult {
345
361
  report?: UsageReport;
346
362
  error?: string;
347
363
  checkedAt: number;
364
+ stale?: boolean;
348
365
  }
349
366
  interface UsageReader {
350
367
  /** Balance plus the 7-day spend window for one key. */
package/lib/index.js CHANGED
@@ -33,14 +33,59 @@ function orderKeysForModel(keys, model, rules) {
33
33
  if (preferred === void 0) return keys;
34
34
  return [preferred, ...keys.filter((entry) => entry !== preferred)];
35
35
  }
36
+ // src/sanitize.ts
37
+ const SECRET_QUERY = /* @__PURE__ */ new Set([
38
+ "api_key",
39
+ "apikey",
40
+ "key",
41
+ "token",
42
+ "access_token",
43
+ "refresh_token",
44
+ "password",
45
+ "passwd",
46
+ "secret",
47
+ "client_secret",
48
+ "sig",
49
+ "signature",
50
+ "credential",
51
+ "code",
52
+ "auth"
53
+ ]);
54
+ const MAX_TEXT = 300;
55
+ function redactText(value, max = MAX_TEXT) {
56
+ let text = value.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, "").replace(/\r?\n/g, " ").replace(/\s+/g, " ").trim();
57
+ text = text.replace(/\bBearer\s+[A-Za-z0-9._~+/=-]+/gi, "Bearer [redacted]").replace(/\bsk-[A-Za-z0-9_-]{8,}\b/g, "sk-[redacted]").replace(/\b(api[_-]?key|token|secret|password|authorization)\s*[:=]\s*[^\s&,"']+/gi, "$1=[redacted]");
58
+ return text.length > max ? `${text.slice(0, Math.max(0, max - 1))}…` : text;
59
+ }
60
+ function sanitizeUrl(raw) {
61
+ try {
62
+ const url = new URL(raw);
63
+ url.username = "";
64
+ url.password = "";
65
+ url.hash = "";
66
+ for (const key of [...url.searchParams.keys()]) if (SECRET_QUERY.has(key.toLowerCase())) url.searchParams.delete(key);
67
+ return url.toString().replace(/\/$/, "");
68
+ } catch {
69
+ return redactText(raw);
70
+ }
71
+ }
72
+ function safeErrorMessage(error, fallback = "request failed") {
73
+ const text = error instanceof Error ? error.message : typeof error === "string" ? error : fallback;
74
+ return redactText(text === "" ? fallback : text);
75
+ }
36
76
  // src/best-effort.ts
77
+ /**
78
+ * One place for deliberately ignored failures. Every call names what was
79
+ * skipped; when a host logger is registered the label and message go to debug
80
+ * instead of disappearing.
81
+ */
37
82
  let sink;
38
83
  function setBestEffortLogger(logger) {
39
84
  sink = logger;
40
85
  }
41
86
  function bestEffortFailed(label, error) {
42
87
  try {
43
- sink?.debug?.(`${label}: ${error instanceof Error ? error.message : String(error)}`);
88
+ sink?.debug?.(`${label}: ${redactText(error instanceof Error ? error.message : String(error))}`);
44
89
  } catch {}
45
90
  }
46
91
  // src/attempt.ts
@@ -77,7 +122,7 @@ const DEFAULT_API_BASE = "https://ollama.com";
77
122
  const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
78
123
  const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
79
124
  const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
80
- const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.25";
125
+ const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.27";
81
126
  const DEFAULT_CONTEXT_WINDOW = 131072;
82
127
  const DEFAULT_MAX_TOKENS = 32768;
83
128
  const DEFAULT_GENERATE_MAX_TOKENS = 131072;
@@ -97,6 +142,10 @@ const Config = z.object({
97
142
  requestTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
98
143
  streamIdleTimeoutMs: markVolatile(z.number().min(1).max(2147483647).default(3e5)),
99
144
  attemptTimeoutMs: markVolatile(z.number().min(0).max(36e5).step(1).default(0)),
145
+ retryMaxRetries: markVolatile(z.number().min(0).max(10).step(1).default(0)),
146
+ retryBaseDelayMs: markVolatile(z.number().min(0).max(6e4).step(1).default(250)),
147
+ retryMaxDelayMs: markVolatile(z.number().min(0).max(12e4).step(1).default(2e3)),
148
+ retryJitter: markVolatile(z.number().min(0).max(1).default(.2)),
100
149
  visibleModels: markVolatile(z.array(z.string()).default([])),
101
150
  showAllModels: markVolatile(z.boolean()),
102
151
  modelVisibility: markVolatile(z.dict(z.boolean()).default({})),
@@ -305,6 +354,10 @@ function resolveConfig(config) {
305
354
  requestTimeoutMs: positiveInt(config.requestTimeoutMs, 3e5),
306
355
  streamIdleTimeoutMs: positiveInt(config.streamIdleTimeoutMs, 3e5),
307
356
  attemptTimeoutMs: typeof config.attemptTimeoutMs === "number" && Number.isInteger(config.attemptTimeoutMs) && config.attemptTimeoutMs >= 0 && config.attemptTimeoutMs <= 36e5 ? config.attemptTimeoutMs : 0,
357
+ retryMaxRetries: typeof config.retryMaxRetries === "number" && Number.isInteger(config.retryMaxRetries) && config.retryMaxRetries >= 0 && config.retryMaxRetries <= 10 ? config.retryMaxRetries : 0,
358
+ retryBaseDelayMs: positiveInt(config.retryBaseDelayMs, 250),
359
+ retryMaxDelayMs: positiveInt(config.retryMaxDelayMs, 2e3),
360
+ retryJitter: typeof config.retryJitter === "number" && config.retryJitter >= 0 && config.retryJitter <= 1 ? config.retryJitter : .2,
308
361
  visibleModels,
309
362
  showAllModels: config.showAllModels === true ? true : config.showAllModels === false ? false : visibleModels.length === 0,
310
363
  modelVisibility: booleanRecord(config.modelVisibility),
@@ -1535,7 +1588,7 @@ function isQuotaText(detail) {
1535
1588
  */
1536
1589
  function classifyResponseError(status, text, headers) {
1537
1590
  const body = parseErrorBody(text);
1538
- const message = body.message ?? (text.trim() === "" ? `HTTP ${status}` : text.trim().slice(0, 500));
1591
+ const message = redactText(body.message ?? (text.trim() === "" ? `HTTP ${status}` : text.trim().slice(0, 500)));
1539
1592
  const detail = `${body.type ?? ""} ${message}`.toLowerCase();
1540
1593
  const retry = retryAfterMs(headers);
1541
1594
  const options = {
@@ -2042,6 +2095,27 @@ function buildChatBody(options, model, config, images) {
2042
2095
  ...stop === void 0 || stop.length === 0 ? {} : { stop }
2043
2096
  };
2044
2097
  }
2098
+ // src/retry.ts
2099
+ function retryPolicy(value) {
2100
+ const integer = (number, fallback) => typeof number === "number" && Number.isInteger(number) && number >= 0 ? number : fallback;
2101
+ const jitter = typeof value.retryJitter === "number" && value.retryJitter >= 0 && value.retryJitter <= 1 ? value.retryJitter : .2;
2102
+ const baseDelayMs = integer(value.retryBaseDelayMs, 250);
2103
+ return {
2104
+ maxRetries: integer(value.retryMaxRetries, 0),
2105
+ baseDelayMs,
2106
+ maxDelayMs: Math.max(baseDelayMs, integer(value.retryMaxDelayMs, 2e3)),
2107
+ jitter
2108
+ };
2109
+ }
2110
+ function retryDelay(attempt, policy, random = Math.random) {
2111
+ const exponential = policy.baseDelayMs * 2 ** Math.max(0, attempt);
2112
+ const capped = Math.min(policy.maxDelayMs, exponential);
2113
+ const spread = capped * policy.jitter;
2114
+ return Math.max(0, Math.round(capped - spread + random() * spread * 2));
2115
+ }
2116
+ function isRetryableStatus(status) {
2117
+ return status === 408 || status === 429 || status >= 500;
2118
+ }
2045
2119
  // src/provider-ids.ts
2046
2120
  /** Provider route owned by this plugin. */
2047
2121
  const PROVIDER = "ollama-cloud";
@@ -3012,7 +3086,7 @@ var OllamaCloudAdapter = class extends LlmAdapter {
3012
3086
  logAttempt(true);
3013
3087
  return;
3014
3088
  } catch (error) {
3015
- const classified = budget.timedOut ? new LlmError(`Ollama Cloud attempt exceeded ${config.attemptTimeoutMs}ms`, "TIMEOUT", { cause: error }) : error instanceof LlmError ? error : new LlmError(error instanceof Error ? error.message : "Ollama Cloud request failed", "TRANSPORT", { cause: error });
3089
+ const classified = budget.timedOut ? new LlmError(`Ollama Cloud attempt exceeded ${config.attemptTimeoutMs}ms`, "TIMEOUT", { cause: error }) : error instanceof LlmError ? error : new LlmError(error instanceof Error ? redactText(error.message) : "Ollama Cloud request failed", "TRANSPORT", { cause: error });
3016
3090
  logAttempt(false, classified.code);
3017
3091
  if (started || options.signal?.aborted === true) throw budget.timedOut ? classified : error;
3018
3092
  lastError = classified;
@@ -3128,29 +3202,53 @@ var OllamaCloudAdapter = class extends LlmAdapter {
3128
3202
  accept: model.transport === "openai-chat" ? "text/event-stream" : "application/x-ndjson",
3129
3203
  "user-agent": config.userAgent
3130
3204
  };
3131
- const controller = new AbortController();
3132
- let timedOut = false;
3133
- const onAbort = () => controller.abort(signal?.reason);
3134
- signal?.addEventListener("abort", onAbort, { once: true });
3135
- const timer = setTimeout(() => {
3136
- timedOut = true;
3137
- controller.abort();
3138
- }, config.requestTimeoutMs);
3139
- timer.unref?.();
3140
- try {
3141
- return await this.fetchImpl(url, {
3142
- method: "POST",
3143
- headers,
3144
- body: JSON.stringify(body),
3145
- signal: controller.signal
3146
- });
3147
- } catch (error) {
3148
- if (signal?.aborted === true) throw signal.reason ?? error;
3149
- if (timedOut) throw new LlmError(`Ollama Cloud did not return response headers within ${config.requestTimeoutMs}ms`, "TIMEOUT", { cause: error });
3150
- throw error;
3151
- } finally {
3152
- clearTimeout(timer);
3153
- signal?.removeEventListener("abort", onAbort);
3205
+ const policy = retryPolicy(config);
3206
+ const wait = async (attempt) => {
3207
+ await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt, policy)));
3208
+ signal?.throwIfAborted();
3209
+ };
3210
+ for (let attempt = 0;; attempt += 1) {
3211
+ const controller = new AbortController();
3212
+ let timedOut = false;
3213
+ const onAbort = () => controller.abort(signal?.reason);
3214
+ signal?.addEventListener("abort", onAbort, { once: true });
3215
+ const timer = setTimeout(() => {
3216
+ timedOut = true;
3217
+ controller.abort();
3218
+ }, config.requestTimeoutMs);
3219
+ timer.unref?.();
3220
+ try {
3221
+ const response = await this.fetchImpl(url, {
3222
+ method: "POST",
3223
+ headers,
3224
+ body: JSON.stringify(body),
3225
+ signal: controller.signal
3226
+ });
3227
+ if (attempt < policy.maxRetries && isRetryableStatus(response.status)) {
3228
+ await response.body?.cancel();
3229
+ await wait(attempt);
3230
+ continue;
3231
+ }
3232
+ return response;
3233
+ } catch (error) {
3234
+ if (signal?.aborted === true) throw signal.reason ?? error;
3235
+ if (timedOut) {
3236
+ const timeout = new LlmError(`Ollama Cloud did not return response headers within ${config.requestTimeoutMs}ms`, "TIMEOUT", { cause: error });
3237
+ if (attempt < policy.maxRetries) {
3238
+ await wait(attempt);
3239
+ continue;
3240
+ }
3241
+ throw timeout;
3242
+ }
3243
+ if (attempt < policy.maxRetries) {
3244
+ await wait(attempt);
3245
+ continue;
3246
+ }
3247
+ throw error;
3248
+ } finally {
3249
+ clearTimeout(timer);
3250
+ signal?.removeEventListener("abort", onAbort);
3251
+ }
3154
3252
  }
3155
3253
  }
3156
3254
  keyId(key) {
@@ -3625,7 +3723,7 @@ async function modelsText(deps) {
3625
3723
  return `- ${model.id} · ${tags.join(" · ")}`;
3626
3724
  }).join("\n");
3627
3725
  } catch (error) {
3628
- return `Models unavailable: ${error instanceof Error ? error.message : String(error)}`;
3726
+ return `Models unavailable: ${safeErrorMessage(error)}`;
3629
3727
  }
3630
3728
  }
3631
3729
  async function testText(deps, model) {
@@ -3658,7 +3756,7 @@ async function testText(deps, model) {
3658
3756
  } catch (error) {
3659
3757
  return {
3660
3758
  ok: false,
3661
- text: `${model}: failed — ${error instanceof Error ? error.message : String(error)}`
3759
+ text: `${model}: failed — ${safeErrorMessage(error)}`
3662
3760
  };
3663
3761
  }
3664
3762
  }
@@ -3809,7 +3907,7 @@ function applyCommands(ctx, deps) {
3809
3907
  } catch (error) {
3810
3908
  return {
3811
3909
  kind: "error",
3812
- text: error instanceof Error ? error.message : "local command failed"
3910
+ text: safeErrorMessage(error, "local command failed")
3813
3911
  };
3814
3912
  }
3815
3913
  }
@@ -4566,7 +4664,7 @@ function normalizeBase(raw) {
4566
4664
  }
4567
4665
  if (url.protocol !== "http:" && url.protocol !== "https:") throw new DiscoveryError("endpoint must use http or https");
4568
4666
  if (url.username !== "" || url.password !== "") throw new DiscoveryError("endpoint URL must not contain credentials");
4569
- return value;
4667
+ return sanitizeUrl(value);
4570
4668
  }
4571
4669
  function isLoopback(url) {
4572
4670
  try {
@@ -4630,15 +4728,19 @@ async function discoverModels(rawBase, key, fetchImpl = fetch) {
4630
4728
  function publicConfig(config) {
4631
4729
  return {
4632
4730
  apiKeyEnv: config.apiKeyEnv,
4633
- apiBase: config.apiBase,
4634
- openAiBase: config.openAiBase,
4635
- usageUrl: config.usageUrl,
4636
- balanceUrl: config.balanceUrl,
4731
+ apiBase: sanitizeUrl(config.apiBase),
4732
+ openAiBase: sanitizeUrl(config.openAiBase),
4733
+ usageUrl: sanitizeUrl(config.usageUrl),
4734
+ balanceUrl: sanitizeUrl(config.balanceUrl),
4637
4735
  transport: config.transport,
4638
4736
  modelTransports: config.modelTransports,
4639
4737
  requestTimeoutMs: config.requestTimeoutMs,
4640
4738
  streamIdleTimeoutMs: config.streamIdleTimeoutMs,
4641
4739
  attemptTimeoutMs: config.attemptTimeoutMs,
4740
+ retryMaxRetries: config.retryMaxRetries,
4741
+ retryBaseDelayMs: config.retryBaseDelayMs,
4742
+ retryMaxDelayMs: config.retryMaxDelayMs,
4743
+ retryJitter: config.retryJitter,
4642
4744
  visibleModels: config.visibleModels,
4643
4745
  showAllModels: config.showAllModels,
4644
4746
  modelVisibility: config.modelVisibility,
@@ -4708,7 +4810,7 @@ async function probeService(config, fetchImpl = fetch) {
4708
4810
  status: null,
4709
4811
  latencyMs: Date.now() - started,
4710
4812
  checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
4711
- error: controller.signal.aborted ? "timeout" : error instanceof Error ? error.message : String(error)
4813
+ error: controller.signal.aborted ? "timeout" : safeErrorMessage(error)
4712
4814
  };
4713
4815
  } finally {
4714
4816
  clearTimeout(timer);
@@ -4843,10 +4945,14 @@ function cleanPrices(value) {
4843
4945
  // src/routes.ts
4844
4946
  const MAX_BODY_BYTES = 262144;
4845
4947
  function json(res, status, value) {
4948
+ const safe = typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.error === "string" ? {
4949
+ ...value,
4950
+ error: safeErrorMessage(value.error)
4951
+ } : value;
4846
4952
  res.statusCode = status;
4847
4953
  res.setHeader("content-type", "application/json; charset=utf-8");
4848
4954
  res.setHeader("cache-control", "no-store");
4849
- res.end(JSON.stringify(value));
4955
+ res.end(JSON.stringify(safe));
4850
4956
  }
4851
4957
  function sourceHost(value) {
4852
4958
  if (value === void 0 || value === "") return void 0;
@@ -5030,6 +5136,10 @@ function configOps(patch) {
5030
5136
  if ("requestTimeoutMs" in patch) set("requestTimeoutMs", patch.requestTimeoutMs);
5031
5137
  if ("streamIdleTimeoutMs" in patch) set("streamIdleTimeoutMs", patch.streamIdleTimeoutMs);
5032
5138
  if ("attemptTimeoutMs" in patch) set("attemptTimeoutMs", patch.attemptTimeoutMs);
5139
+ if ("retryMaxRetries" in patch) set("retryMaxRetries", patch.retryMaxRetries);
5140
+ if ("retryBaseDelayMs" in patch) set("retryBaseDelayMs", patch.retryBaseDelayMs);
5141
+ if ("retryMaxDelayMs" in patch) set("retryMaxDelayMs", patch.retryMaxDelayMs);
5142
+ if ("retryJitter" in patch) set("retryJitter", patch.retryJitter);
5033
5143
  if ("visibleModels" in patch) set("visibleModels", cleanStringArray(patch.visibleModels));
5034
5144
  if ("showAllModels" in patch) set("showAllModels", patch.showAllModels === true);
5035
5145
  if ("modelVisibility" in patch) set("modelVisibility", cleanBooleanRecord(patch.modelVisibility));
@@ -5881,6 +5991,7 @@ function normalizeUsage(payload, range) {
5881
5991
  /** Cached reader for Ollama Cloud balance and usage. Fail-open by design. */
5882
5992
  function createUsageReader(config, resolveKey, fetchImpl = fetch) {
5883
5993
  const cache = /* @__PURE__ */ new Map();
5994
+ const lastGood = /* @__PURE__ */ new Map();
5884
5995
  const inflight = /* @__PURE__ */ new Map();
5885
5996
  const usageCalls = [];
5886
5997
  let generation = 0;
@@ -5900,14 +6011,35 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
5900
6011
  };
5901
6012
  const request = async (url, key) => {
5902
6013
  const current = config();
5903
- return fetchImpl(url, {
5904
- headers: {
5905
- authorization: `Bearer ${key}`,
5906
- accept: "application/json",
5907
- "user-agent": current.userAgent
5908
- },
5909
- signal: AbortSignal.timeout(USAGE_TIMEOUT_MS)
6014
+ const policy = retryPolicy({
6015
+ ...current,
6016
+ retryMaxRetries: Math.max(2, current.retryMaxRetries)
5910
6017
  });
6018
+ for (let attempt = 0;; attempt += 1) {
6019
+ let response;
6020
+ try {
6021
+ response = await fetchImpl(url, {
6022
+ headers: {
6023
+ authorization: `Bearer ${key}`,
6024
+ accept: "application/json",
6025
+ "user-agent": current.userAgent
6026
+ },
6027
+ signal: AbortSignal.timeout(USAGE_TIMEOUT_MS)
6028
+ });
6029
+ } catch (error) {
6030
+ if (attempt < policy.maxRetries) {
6031
+ await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt, policy)));
6032
+ continue;
6033
+ }
6034
+ throw error;
6035
+ }
6036
+ if (attempt < policy.maxRetries && isRetryableStatus(response.status)) {
6037
+ await response.body?.cancel();
6038
+ await new Promise((resolve) => setTimeout(resolve, retryDelay(attempt, policy)));
6039
+ continue;
6040
+ }
6041
+ return response;
6042
+ }
5911
6043
  };
5912
6044
  const readBalance = async (key) => {
5913
6045
  if (key === "") return void 0;
@@ -5962,32 +6094,44 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
5962
6094
  };
5963
6095
  const compose = async (key, range, epoch) => {
5964
6096
  const [balance, usage] = await Promise.all([readBalance(key), readUsage(key, range, epoch)]);
6097
+ const reportKey = `${fingerprint(key)}:report:${range}`;
5965
6098
  if (balance === void 0 && usage === void 0) {
5966
6099
  const result = {
5967
6100
  error: "usage and balance are unavailable",
5968
6101
  checkedAt: Date.now()
5969
6102
  };
5970
- if (epoch === generation) cache.set(`${fingerprint(key)}:report:${range}`, result);
5971
- return result;
6103
+ const previous = lastGood.get(reportKey);
6104
+ if (epoch === generation) cache.set(reportKey, result);
6105
+ return previous === void 0 ? result : {
6106
+ ...previous,
6107
+ stale: true,
6108
+ ...result.error === void 0 ? {} : { error: result.error }
6109
+ };
5972
6110
  }
6111
+ const windowKey = range === "24h" ? "d24" : range === "7d" ? "d7" : "d30";
5973
6112
  const result = {
5974
6113
  report: {
5975
6114
  kind: balance?.kind ?? "credits",
5976
6115
  ...balance?.credits === void 0 ? {} : { credits: balance.credits },
5977
6116
  ...balance?.legacy === void 0 ? {} : { legacy: balance.legacy },
5978
6117
  windows: usage === void 0 ? {} : {
5979
- ...range === "24h" ? { d24: usage.totals } : {},
5980
- ...range === "7d" ? { d7: usage.totals } : {},
5981
- ...range === "30d" ? { d30: usage.totals } : {}
6118
+ [windowKey]: usage.totals,
6119
+ ...range === "24h" ? { session: usage.totals } : {},
6120
+ ...range === "7d" ? { weekly: usage.totals } : {},
6121
+ ...range === "30d" ? { monthly: usage.totals } : {}
5982
6122
  },
5983
6123
  byModel: usage?.byModel ?? [],
6124
+ ...usage === void 0 ? {} : { byWindow: { [range]: usage.byModel } },
5984
6125
  range
5985
6126
  },
5986
6127
  checkedAt: Date.now()
5987
6128
  };
5988
6129
  if (epoch === generation) {
5989
- cache.set(`${fingerprint(key)}:report:${range}`, result);
5990
- if (usage !== void 0) cache.set(`${fingerprint(key)}:${range}`, result);
6130
+ cache.set(reportKey, result);
6131
+ if (usage !== void 0) {
6132
+ cache.set(`${fingerprint(key)}:${range}`, result);
6133
+ lastGood.set(reportKey, result);
6134
+ }
5991
6135
  }
5992
6136
  return result;
5993
6137
  };
@@ -6000,7 +6144,17 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
6000
6144
  const cacheKey = `${fingerprint(key)}:report:${range}`;
6001
6145
  const hit = cache.get(cacheKey);
6002
6146
  const ttl = hit?.report === void 0 ? FAILURE_TTL_MS : BALANCE_TTL_MS;
6003
- if (hit !== void 0 && Date.now() - hit.checkedAt < ttl) return hit;
6147
+ if (hit !== void 0 && Date.now() - hit.checkedAt < ttl) {
6148
+ if (hit.report === void 0) {
6149
+ const previous = lastGood.get(cacheKey);
6150
+ if (previous !== void 0) return {
6151
+ ...previous,
6152
+ stale: true,
6153
+ ...hit.error === void 0 ? {} : { error: hit.error }
6154
+ };
6155
+ }
6156
+ return hit;
6157
+ }
6004
6158
  const existing = inflight.get(cacheKey);
6005
6159
  if (existing !== void 0) return existing;
6006
6160
  const task = compose(key, range, generation).finally(() => inflight.delete(cacheKey));
@@ -6062,6 +6216,7 @@ async function postOnce(options, url, payload, caller) {
6062
6216
  if (apiKey === void 0) throw new WebError("ollama-cloud web capabilities need an API key; configure the credential reference through plugin settings", "OLLAMA_WEB_MISSING_CREDENTIAL");
6063
6217
  const fetchImpl = options.fetch ?? fetch;
6064
6218
  const timeoutMs = options.timeoutMs ?? 15e3;
6219
+ const safeUrl = sanitizeUrl(url);
6065
6220
  const budget = createAttemptBudget(timeoutMs, caller);
6066
6221
  let response;
6067
6222
  try {
@@ -6080,15 +6235,15 @@ async function postOnce(options, url, payload, caller) {
6080
6235
  budget.dispose();
6081
6236
  if (budget.callerAborted) throw new WebError("ollama-cloud web request aborted by caller", "ABORTED", { cause: error });
6082
6237
  if (budget.timedOut) throw new WebError(`ollama-cloud web request timed out after ${timeoutMs}ms`, "OLLAMA_WEB_TIMEOUT", { cause: error });
6083
- if (isRedirectFailure(error)) throw new WebError(`${url} attempted a redirect`, "WEB_PROVIDER_ERROR", { cause: error });
6084
- const detail = error instanceof Error && error.message !== "" ? `: ${error.message}` : "";
6085
- throw new WebError(`could not reach ${url}${detail}`, "OLLAMA_WEB_TRANSPORT", { cause: error });
6238
+ if (isRedirectFailure(error)) throw new WebError(`${safeUrl} attempted a redirect`, "WEB_PROVIDER_ERROR", { cause: error });
6239
+ const detail = error instanceof Error && error.message !== "" ? `: ${redactText(error.message)}` : "";
6240
+ throw new WebError(`could not reach ${safeUrl}${detail}`, "OLLAMA_WEB_TRANSPORT", { cause: error });
6086
6241
  }
6087
6242
  if (!response.ok) {
6088
6243
  budget.dispose();
6089
6244
  await response.body?.cancel();
6090
6245
  const hint = response.status === 401 || response.status === 403 ? "; check the API key" : "";
6091
- throw new WebError(`${url} answered ${response.status}${hint}`, "WEB_PROVIDER_ERROR");
6246
+ throw new WebError(`${safeUrl} answered ${response.status}${hint}`, "WEB_PROVIDER_ERROR");
6092
6247
  }
6093
6248
  try {
6094
6249
  return {
@@ -6098,7 +6253,7 @@ async function postOnce(options, url, payload, caller) {
6098
6253
  } catch (error) {
6099
6254
  if (budget.callerAborted) throw new WebError("ollama-cloud web request aborted by caller", "ABORTED", { cause: error });
6100
6255
  if (budget.timedOut) throw new WebError(`ollama-cloud web request timed out after ${timeoutMs}ms`, "OLLAMA_WEB_TIMEOUT", { cause: error });
6101
- throw new WebError(`${url} did not answer with JSON`, "OLLAMA_WEB_BAD_REPLY", { cause: error });
6256
+ throw new WebError(`${safeUrl} did not answer with JSON`, "OLLAMA_WEB_BAD_REPLY", { cause: error });
6102
6257
  } finally {
6103
6258
  budget.dispose();
6104
6259
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-ollama-cloud-provider",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "description": "Ollama Cloud LLM provider plugin for DeepSeek Harness (dsh).",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -73,7 +73,7 @@
73
73
  "build": "tsdown",
74
74
  "prepare": "tsdown 1>&2",
75
75
  "typecheck": "tsc --noEmit",
76
- "test": "node --import tsx --test tests/core.test.ts tests/usage.test.ts tests/adapter.test.ts tests/routes.test.ts tests/native.test.ts tests/openai-chat.test.ts tests/commands.test.ts tests/tui-settings.test.ts tests/analytics.test.ts tests/apply.test.ts tests/account-rules.test.ts tests/request-log.test.ts tests/image-budget.test.ts tests/tool-schema.test.ts tests/tools.test.ts tests/plugin-updater.test.ts tests/compat.test.ts tests/status.test.ts tests/settings-draft.test.ts tests/update-state.test.ts tests/ui-render.test.tsx tests/update-card-view.test.tsx tests/web.test.ts tests/mock-ollama.test.ts tests/local.test.ts tests/reasoning.test.ts tests/catalog-cache.test.ts tests/local-models.test.ts tests/attempt.test.ts tests/model-groups.test.tsx tests/discovery.test.ts",
76
+ "test": "node --import tsx --test tests/core.test.ts tests/usage.test.ts tests/adapter.test.ts tests/routes.test.ts tests/native.test.ts tests/openai-chat.test.ts tests/commands.test.ts tests/tui-settings.test.ts tests/analytics.test.ts tests/apply.test.ts tests/account-rules.test.ts tests/request-log.test.ts tests/image-budget.test.ts tests/tool-schema.test.ts tests/tools.test.ts tests/plugin-updater.test.ts tests/compat.test.ts tests/status.test.ts tests/settings-draft.test.ts tests/update-state.test.ts tests/ui-render.test.tsx tests/update-card-view.test.tsx tests/web.test.ts tests/mock-ollama.test.ts tests/local.test.ts tests/reasoning.test.ts tests/catalog-cache.test.ts tests/local-models.test.ts tests/attempt.test.ts tests/model-groups.test.tsx tests/discovery.test.ts tests/sanitize.test.ts tests/retry.test.ts",
77
77
  "check": "npm run typecheck && npm test && npm run build",
78
78
  "release": "npm run check && npm pack",
79
79
  "sync:models": "node scripts/sync-models.mjs"