@goodandready/dsh-ollama-cloud-provider 0.1.39 → 0.1.40

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.
@@ -35,10 +35,16 @@ function recordRequest(provider, model, at = Date.now(), purpose, account, reaso
35
35
  const path = requestLogPath();
36
36
  const directory = dirname(path);
37
37
  if (readyDirectory !== directory) {
38
- await mkdir(directory, { recursive: true });
38
+ await mkdir(directory, {
39
+ recursive: true,
40
+ mode: 448
41
+ });
39
42
  readyDirectory = directory;
40
43
  }
41
- await appendFile(path, line, "utf8");
44
+ await appendFile(path, line, {
45
+ encoding: "utf8",
46
+ mode: 384
47
+ });
42
48
  appends += 1;
43
49
  if (appends % COMPACT_EVERY === 0) await compactRequestLog();
44
50
  } catch (error) {
@@ -59,7 +65,10 @@ async function compactRequestLog(maxBytes = MAX_LOG_BYTES, keepLines = KEEP_LINE
59
65
  if (lines.length <= keepLines) return;
60
66
  const temporary = `${path}.tmp`;
61
67
  try {
62
- await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`, "utf8");
68
+ await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`, {
69
+ encoding: "utf8",
70
+ mode: 384
71
+ });
63
72
  await rename(temporary, path);
64
73
  } catch (error) {
65
74
  await rm(temporary, { force: true }).catch((caught) => {
@@ -1,5 +1,8 @@
1
1
  // src/routes-values.ts
2
2
  const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
3
+ function unsafeKey(key) {
4
+ return key === "__proto__" || key === "constructor" || key === "prototype";
5
+ }
3
6
  const CREDENTIAL_REF_PATTERN = /^OLLAMA_[A-Z0-9_]+$/;
4
7
  /** The plugin may read and write only its own credential namespace. */
5
8
  function normalizeCredentialRef(value) {
@@ -75,7 +78,7 @@ function cleanModelOverrides(value) {
75
78
  if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
76
79
  const out = {};
77
80
  for (const [id, entry] of Object.entries(value)) {
78
- if (id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
81
+ if (unsafeKey(id) || id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
79
82
  const record = entry;
80
83
  const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
81
84
  const thinking = typeof record.thinking === "boolean" ? record.thinking : void 0;
@@ -95,25 +98,35 @@ function cleanModelOverrides(value) {
95
98
  function cleanBooleanRecord(value) {
96
99
  if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
97
100
  const out = {};
98
- for (const [key, entry] of Object.entries(value)) if (typeof entry === "boolean") out[key] = entry;
101
+ for (const [key, entry] of Object.entries(value)) {
102
+ if (unsafeKey(key)) continue;
103
+ if (typeof entry === "boolean") out[key] = entry;
104
+ }
99
105
  return out;
100
106
  }
101
107
  function cleanNumberRecord(value) {
102
108
  if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
103
109
  const out = {};
104
- for (const [key, entry] of Object.entries(value)) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
110
+ for (const [key, entry] of Object.entries(value)) {
111
+ if (unsafeKey(key)) continue;
112
+ if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
113
+ }
105
114
  return out;
106
115
  }
107
116
  function cleanTransports(value) {
108
117
  if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
109
118
  const out = {};
110
- for (const [key, entry] of Object.entries(value)) if (typeof entry === "string" && TRANSPORTS.has(entry)) out[key] = entry;
119
+ for (const [key, entry] of Object.entries(value)) {
120
+ if (unsafeKey(key)) continue;
121
+ if (typeof entry === "string" && TRANSPORTS.has(entry)) out[key] = entry;
122
+ }
111
123
  return out;
112
124
  }
113
125
  function cleanPrices(value) {
114
126
  if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
115
127
  const out = {};
116
128
  for (const [key, entry] of Object.entries(value)) {
129
+ if (unsafeKey(key)) continue;
117
130
  if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
118
131
  const raw = entry;
119
132
  const price = {};
package/lib/routes.js CHANGED
@@ -11,7 +11,7 @@ import { createRouteGuard } from "./route-guard.js";
11
11
  import { publicConfig } from "./routes-public.js";
12
12
  import { clearStatusCache, probeServiceCached } from "./status.js";
13
13
  import { cleanAccounts, cleanBooleanRecord, cleanManualModels, cleanModelAccountRules, cleanModelOverrides, cleanNumberRecord, cleanPrices, cleanStringArray, cleanTransports, isCredentialRef, normalizeCredentialRef } from "./routes-values.js";
14
- import { safeErrorMessage } from "./sanitize.js";
14
+ import { redactText, safeErrorMessage } from "./sanitize.js";
15
15
  import { ReasoningEffortId } from "@deepseek-ai/dsh-llm";
16
16
  import { credentialRef } from "@deepseek-ai/dsh-credentials";
17
17
  // src/routes.ts
@@ -288,7 +288,7 @@ async function buildState(ctx, deps) {
288
288
  keyInfo = {
289
289
  configured: info.configured,
290
290
  writable: info.writable,
291
- ...info.source === void 0 ? {} : { source: info.source }
291
+ ...info.source === void 0 ? {} : { source: redactText(info.source) }
292
292
  };
293
293
  } catch (error) {
294
294
  bestEffortFailed("primary credential describe failed", error);
@@ -342,7 +342,7 @@ async function buildState(ctx, deps) {
342
342
  accounts.push({
343
343
  ...slot,
344
344
  configured,
345
- source,
345
+ source: redactText(source),
346
346
  active: activeLabel === slot.label,
347
347
  coolingUntil: health?.coolingUntil ?? 0,
348
348
  mark: health?.reason ?? "",
@@ -350,6 +350,7 @@ async function buildState(ctx, deps) {
350
350
  });
351
351
  }
352
352
  let models = [];
353
+ let providerError;
353
354
  try {
354
355
  models = (await deps.adapter.listModels(PROVIDER, { unfiltered: true })).map((model) => {
355
356
  const meta = effectiveModel(model.id, config);
@@ -370,7 +371,9 @@ async function buildState(ctx, deps) {
370
371
  visible: modelVisible(model.id, config)
371
372
  };
372
373
  });
373
- } catch {
374
+ } catch (error) {
375
+ bestEffortFailed("state:listModels", error);
376
+ providerError = redactText(error instanceof Error ? error.message : String(error));
374
377
  models = [];
375
378
  }
376
379
  const displayEntry = resolved.find((entry) => (entry.label ?? "default") === activeLabel) ?? resolved[0];
@@ -382,7 +385,9 @@ async function buildState(ctx, deps) {
382
385
  models,
383
386
  usage: usage ?? null,
384
387
  routing: deps.adapter.routingTrace(),
385
- revision: settingsRevision(ctx)
388
+ revision: settingsRevision(ctx),
389
+ configWritable: typeof getSettings(ctx)?.mutate === "function" && getSettings(ctx)?.writable !== false,
390
+ ...providerError === void 0 ? {} : { providerError }
386
391
  };
387
392
  }
388
393
  async function refreshAndState(ctx, deps, res) {
@@ -440,7 +445,7 @@ function applyRoutes(ctx, web, deps) {
440
445
  const config = deps.config();
441
446
  let providerError;
442
447
  const models = await deps.adapter.listModels(PROVIDER, { unfiltered: true }).catch((err) => {
443
- providerError = err instanceof Error ? err.message : String(err);
448
+ providerError = redactText(err instanceof Error ? err.message : String(err));
444
449
  bestEffortFailed("health:listModels", err);
445
450
  return [];
446
451
  });
package/lib/status.js CHANGED
@@ -14,6 +14,7 @@ async function probeService(config, fetchImpl = fetch) {
14
14
  try {
15
15
  const response = await fetchImpl(tagsUrlFor(config), {
16
16
  method: "GET",
17
+ redirect: "error",
17
18
  signal: controller.signal,
18
19
  headers: { accept: "application/json" }
19
20
  });
@@ -1,6 +1,7 @@
1
1
  import { bestEffortFailed } from "../best-effort.js";
2
2
  import { LlmError } from "@deepseek-ai/dsh-llm";
3
3
  // src/transports/ndjson.ts
4
+ const MAX_FRAME_BYTES = 1e6;
4
5
  async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
5
6
  if (signal?.aborted === true) throw signal.reason;
6
7
  return await new Promise((resolve, reject) => {
@@ -13,13 +14,13 @@ async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
13
14
  action();
14
15
  };
15
16
  const timer = setTimeout(() => {
16
- reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
17
17
  finish(() => reject(new LlmError(`Ollama Cloud stream went idle for ${idleTimeoutMs}ms`, "TIMEOUT")));
18
+ reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
18
19
  }, idleTimeoutMs);
19
20
  timer.unref?.();
20
21
  const onAbort = () => {
21
- reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
22
22
  finish(() => reject(signal?.reason ?? new DOMException("aborted", "AbortError")));
23
+ reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
23
24
  };
24
25
  signal?.addEventListener("abort", onAbort, { once: true });
25
26
  reader.read().then((value) => finish(() => resolve(value)), (error) => finish(() => reject(error)));
@@ -38,6 +39,7 @@ async function* ndjsonObjects(body, idleTimeoutMs, signal) {
38
39
  const result = await readWithIdleTimeout(reader, idleTimeoutMs, signal);
39
40
  if (result.done) break;
40
41
  buffer += decoder.decode(result.value, { stream: true }).replace(/\r\n/g, "\n");
42
+ if (Buffer.byteLength(buffer, "utf8") > MAX_FRAME_BYTES) throw new LlmError("Ollama Cloud sent an oversized stream frame", "PROVIDER_HTTP_ERROR");
41
43
  for (;;) {
42
44
  const boundary = buffer.indexOf("\n");
43
45
  if (boundary < 0) break;
@@ -65,7 +67,12 @@ async function* ndjsonObjects(body, idleTimeoutMs, signal) {
65
67
  if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) yield parsed;
66
68
  }
67
69
  } finally {
68
- reader.releaseLock();
70
+ await reader.cancel().catch((err) => bestEffortFailed("ndjson:cancel", err));
71
+ try {
72
+ reader.releaseLock();
73
+ } catch (error) {
74
+ bestEffortFailed("ndjson:release", error);
75
+ }
69
76
  }
70
77
  }
71
78
  export { ndjsonObjects };
@@ -108,24 +108,36 @@ async function* parseChatStream(response, idleTimeoutMs, signal) {
108
108
  entry = {
109
109
  dshIndex: nextIndex++,
110
110
  id: uniqueToolCallId(toolCall.id),
111
+ upstreamId: typeof toolCall.id === "string" ? toolCall.id.trim() : "",
111
112
  name: "",
112
113
  args: "",
113
114
  started: false
114
115
  };
115
116
  toolCalls.set(key, entry);
116
117
  }
117
- if (typeof toolCall.id === "string" && toolCall.id.trim() !== "") entry.id = ToolCallId(toolCall.id.trim());
118
+ if (entry.upstreamId === "" && typeof toolCall.id === "string" && toolCall.id.trim() !== "") entry.upstreamId = toolCall.id.trim();
118
119
  if (toolCall.function?.name !== void 0 && toolCall.function.name !== "") entry.name = toolCall.function.name;
120
+ const argumentsDelta = toolCall.function?.arguments ?? "";
121
+ if (argumentsDelta !== "") entry.args += argumentsDelta;
122
+ if (entry.upstreamId === "" && !entry.started) continue;
119
123
  if (!entry.started) {
124
+ entry.id = ToolCallId(entry.upstreamId);
120
125
  entry.started = true;
126
+ sawContent = true;
121
127
  yield {
122
128
  type: "block-start",
123
129
  index: entry.dshIndex,
124
130
  blockType: "tool-call"
125
131
  };
132
+ yield {
133
+ type: "tool-call-delta",
134
+ index: entry.dshIndex,
135
+ id: entry.id,
136
+ ...entry.name === "" ? {} : { name: entry.name },
137
+ argumentsDelta: entry.args
138
+ };
139
+ continue;
126
140
  }
127
- const argumentsDelta = toolCall.function?.arguments ?? "";
128
- if (argumentsDelta !== "") entry.args += argumentsDelta;
129
141
  sawContent = true;
130
142
  yield {
131
143
  type: "tool-call-delta",
@@ -141,6 +153,23 @@ async function* parseChatStream(response, idleTimeoutMs, signal) {
141
153
  }
142
154
  }
143
155
  }
156
+ for (const entry of toolCalls.values()) {
157
+ if (entry.started) continue;
158
+ entry.started = true;
159
+ sawContent = true;
160
+ yield {
161
+ type: "block-start",
162
+ index: entry.dshIndex,
163
+ blockType: "tool-call"
164
+ };
165
+ yield {
166
+ type: "tool-call-delta",
167
+ index: entry.dshIndex,
168
+ id: ToolCallId(entry.id),
169
+ ...entry.name === "" ? {} : { name: entry.name },
170
+ argumentsDelta: entry.args
171
+ };
172
+ }
144
173
  if (!sawContent) throw new LlmError("Ollama Cloud completed the request without content", "EMPTY_RESPONSE");
145
174
  if (!terminated) throw new LlmError("Ollama Cloud stream ended before [DONE] or finish_reason", "TRANSPORT");
146
175
  if (textIndex !== void 0) yield {
@@ -1,6 +1,7 @@
1
1
  import { bestEffortFailed } from "../best-effort.js";
2
2
  import { LlmError } from "@deepseek-ai/dsh-llm";
3
3
  // src/transports/sse.ts
4
+ const MAX_FRAME_BYTES = 1e6;
4
5
  async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
5
6
  if (signal?.aborted === true) throw signal.reason;
6
7
  return await new Promise((resolve, reject) => {
@@ -13,12 +14,13 @@ async function readWithIdleTimeout(reader, idleTimeoutMs, signal) {
13
14
  action();
14
15
  };
15
16
  const timer = setTimeout(() => {
16
- reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
17
17
  finish(() => reject(new LlmError(`Ollama Cloud stream went idle for ${idleTimeoutMs}ms`, "TIMEOUT")));
18
+ reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
18
19
  }, idleTimeoutMs);
20
+ timer.unref?.();
19
21
  const onAbort = () => {
20
- reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
21
22
  finish(() => reject(signal?.reason ?? new DOMException("aborted", "AbortError")));
23
+ reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
22
24
  };
23
25
  signal?.addEventListener("abort", onAbort, { once: true });
24
26
  reader.read().then((value) => finish(() => resolve(value)), (error) => finish(() => reject(error)));
@@ -34,6 +36,7 @@ async function* sseData(body, idleTimeoutMs, signal) {
34
36
  const result = await readWithIdleTimeout(reader, idleTimeoutMs, signal);
35
37
  if (result.done) break;
36
38
  buffer = (buffer + decoder.decode(result.value, { stream: true })).replace(/\r\n/g, "\n");
39
+ if (Buffer.byteLength(buffer, "utf8") > MAX_FRAME_BYTES) throw new LlmError("Ollama Cloud sent an oversized stream event", "PROVIDER_HTTP_ERROR");
37
40
  for (;;) {
38
41
  const boundary = buffer.indexOf("\n\n");
39
42
  if (boundary < 0) break;
@@ -49,7 +52,12 @@ async function* sseData(body, idleTimeoutMs, signal) {
49
52
  if (data !== "") yield data;
50
53
  }
51
54
  } finally {
52
- reader.releaseLock();
55
+ await reader.cancel().catch((err) => bestEffortFailed("sse:cancel", err));
56
+ try {
57
+ reader.releaseLock();
58
+ } catch (error) {
59
+ bestEffortFailed("sse:release", error);
60
+ }
53
61
  }
54
62
  }
55
63
  export { sseData };
package/lib/usage.js CHANGED
@@ -111,13 +111,21 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
111
111
  const ttl = hit.report === void 0 ? FAILURE_TTL_MS : USAGE_TTL_MS;
112
112
  return Date.now() - hit.checkedAt < ttl ? hit : void 0;
113
113
  };
114
- const request = async (url, key) => {
114
+ const noteUsageCall = () => {
115
+ const now = Date.now();
116
+ const recent = usageCalls.filter((at) => now - at < USAGE_WINDOW_MS);
117
+ recent.push(now);
118
+ usageCalls.length = 0;
119
+ usageCalls.push(...recent);
120
+ };
121
+ const request = async (url, key, countUsage = false) => {
115
122
  const current = config();
116
123
  const policy = retryPolicy({
117
124
  ...current,
118
125
  retryMaxRetries: Math.max(2, current.retryMaxRetries)
119
126
  });
120
127
  for (let attempt = 0;; attempt += 1) {
128
+ if (countUsage) noteUsageCall();
121
129
  let response;
122
130
  try {
123
131
  response = await fetchImpl(url, {
@@ -126,6 +134,7 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
126
134
  accept: "application/json",
127
135
  "user-agent": current.userAgent
128
136
  },
137
+ redirect: "error",
129
138
  signal: AbortSignal.timeout(USAGE_TIMEOUT_MS)
130
139
  });
131
140
  } catch (error) {
@@ -168,16 +177,12 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
168
177
  };
169
178
  }
170
179
  const now = Date.now();
171
- const recent = usageCalls.filter((at) => now - at < USAGE_WINDOW_MS);
172
- if (recent.length >= USAGE_WINDOW_LIMIT) return void 0;
173
- recent.push(now);
174
- usageCalls.length = 0;
175
- usageCalls.push(...recent);
180
+ if (usageCalls.filter((at) => now - at < USAGE_WINDOW_MS).length >= USAGE_WINDOW_LIMIT) return { rateLimited: true };
176
181
  try {
177
182
  const url = new URL(current.usageUrl);
178
183
  url.searchParams.set("range", range);
179
184
  url.searchParams.set("group_by", "model");
180
- const response = await request(url.toString(), key);
185
+ const response = await request(url.toString(), key, true);
181
186
  if (!response.ok) {
182
187
  if (epoch === generation) cache.set(cacheKey, {
183
188
  error: `usage endpoint returned HTTP ${response.status}`,
@@ -195,15 +200,17 @@ function createUsageReader(config, resolveKey, fetchImpl = fetch) {
195
200
  }
196
201
  };
197
202
  const compose = async (key, range, epoch) => {
198
- const [balance, usage] = await Promise.all([readBalance(key), readUsage(key, range, epoch)]);
203
+ const [balance, usageRead] = await Promise.all([readBalance(key), readUsage(key, range, epoch)]);
204
+ const rateLimited = usageRead !== void 0 && "rateLimited" in usageRead;
205
+ const usage = usageRead !== void 0 && !("rateLimited" in usageRead) ? usageRead : void 0;
199
206
  const reportKey = `${fingerprint(key)}:report:${range}`;
200
207
  if (balance === void 0 && usage === void 0) {
201
208
  const result = {
202
- error: "usage and balance are unavailable",
209
+ error: rateLimited ? "usage rate limit reached; retry shortly" : "usage and balance are unavailable",
203
210
  checkedAt: Date.now()
204
211
  };
205
212
  const previous = lastGood.get(reportKey);
206
- if (epoch === generation) cache.set(reportKey, result);
213
+ if (!rateLimited && epoch === generation) cache.set(reportKey, result);
207
214
  return previous === void 0 ? result : {
208
215
  ...previous,
209
216
  stale: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-ollama-cloud-provider",
3
- "version": "0.1.39",
3
+ "version": "0.1.40",
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 --test-concurrency=1 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/reasoning.test.ts tests/catalog-cache.test.ts tests/attempt.test.ts tests/model-groups.test.tsx tests/discovery.test.ts tests/sanitize.test.ts tests/retry.test.ts tests/provider-card.test.tsx tests/host-verification.test.ts tests/ui-actions.test.tsx tests/tool-call-id.test.ts tests/deploy-script.test.ts tests/endpoints.test.ts tests/route-guard.test.ts",
76
+ "test": "node --import tsx --test --test-concurrency=1 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/reasoning.test.ts tests/catalog-cache.test.ts tests/attempt.test.ts tests/model-groups.test.tsx tests/discovery.test.ts tests/sanitize.test.ts tests/retry.test.ts tests/provider-card.test.tsx tests/host-verification.test.ts tests/ui-actions.test.tsx tests/tool-call-id.test.ts tests/deploy-script.test.ts tests/endpoints.test.ts tests/route-guard.test.ts tests/catalog-scheduler.test.ts",
77
77
  "check": "npm run typecheck && npm test && npm run build && npm run pack:check",
78
78
  "release": "npm run check && mkdir -p dist && npm pack --pack-destination dist",
79
79
  "sync:models": "node scripts/sync-models.mjs",