@goodandready/dsh-ollama-cloud-provider 0.1.34 → 0.1.36
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 +18 -0
- package/README.md +2 -2
- package/README.ru.md +1 -1
- package/README.zh.md +4 -0
- package/lib/client.js +46 -11
- package/lib/index.d.ts +3 -1
- package/lib/index.js +96 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.36 — 2026-11-09
|
|
4
|
+
|
|
5
|
+
- Resilience: shortened 401/403 credential rejection cooldown from 24 hours to 5 minutes, added explicit reset endpoint (`POST /dsh-ollama/cooldowns/reset`) and "Reset cooldowns" UI action in settings page, and surfaced exact remaining wait time and root cause (`INVALID_CREDENTIAL`, `QUOTA`, `RATE_LIMIT`) when all keys are cooling (#117).
|
|
6
|
+
- Security & Hygiene: untracked internal agent instructions (`AGENTS.md`), project metadata (`index.md`), Gitea templates (`.gitea/`), and internal design/audit documentation (`docs/`) from git mirror trees and added them to `.gitignore`, preventing public leakage to external mirrors (#118).
|
|
7
|
+
- Security: added `trustedHost` check to `readOnly` route validation, rejecting DNS rebinding attacks on `/dsh-ollama/health` and `/dsh-ollama/status` when non-local `Host` headers are supplied (#119).
|
|
8
|
+
- Protocol Conformance: prioritized token exhaustion `done_reason === 'length'` (`max-tokens`) over partial in-band tool call markers in native transport streaming (`src/transports/native.ts`) (#120).
|
|
9
|
+
- State Persistence: replaced direct file writes with atomic file write (`atomicWriteFile` using temp file + `rename`) for `cooldowns.json` and `sticky.json` to prevent file corruption on unexpected crashes (#121).
|
|
10
|
+
- Plugin Identifier: updated Cordis plugin export `name` in `src/index.ts` to match the canonical package name `@goodandready/dsh-ollama-cloud-provider` (#122).
|
|
11
|
+
- Worktree & Branch Hygiene: pruned 20 merged stale worktrees and 26 merged local branches to eliminate ~1.4 GB repository bloat while preserving active and uncommitted branches (#123).
|
|
12
|
+
|
|
13
|
+
## 0.1.35 — 2026-11-09
|
|
14
|
+
|
|
15
|
+
- Security: enforce host authentication check (`verifyHost`) across all loopback write routes (`/config`, `/key`), closing unauthenticated mutation bypass on non-desktop browser requests (#111).
|
|
16
|
+
- Security: validate request `Referer` when `Origin` is omitted on GET endpoints to reject cross-origin ambient read requests (#112).
|
|
17
|
+
- Security: validate URL scheme in `OllamaWebFetchProvider.fetch` to reject non-HTTP schemes (`file:`, `ftp:`) and malformed URLs with structured `WebError` (#115).
|
|
18
|
+
- Localization: localized all 11 sampling and generation parameter labels and tokens header in the Chinese dictionary (`const zh`) (#113).
|
|
19
|
+
- Accessibility: added explicit `aria-label` attributes to standalone form controls (model selection checkbox, API key inputs, account credentials, model routing rules, model search) in the settings UI for WCAG 2.1 SC 4.1.2 compliance (#114).
|
|
20
|
+
|
|
3
21
|
## 0.1.34 — 2026-11-07
|
|
4
22
|
|
|
5
23
|
- Security: patched transient zod memory exhaustion vulnerability (GHSA-mrhx-c7xc-m3w5) in dependencies via package overrides, achieving 0 audit vulnerabilities (#108).
|
package/README.md
CHANGED
|
@@ -183,7 +183,7 @@ Create a key at <https://ollama.com/settings/keys>. Keys do not expire; `x-api-k
|
|
|
183
183
|
|
|
184
184
|
Add a second account in **Settings → Ollama Cloud → Accounts**: give it a name and a credential reference (for example `OLLAMA_API_KEY_2`), paste its key, then Save. The default account always serves first unless `activeAccount` pins another one.
|
|
185
185
|
|
|
186
|
-
Rotation happens **before any content is streamed**. When the serving key is rejected with an invalid credential, a rate limit or an exhausted quota, the adapter marks that key and retries the same request with the next usable account. A request that already started streaming is not replayed. Cooldowns are stored in `~/.dsh/storages/dsh-ollama/cooldowns.json` keyed by a SHA-256 fingerprint — raw keys are never written.
|
|
186
|
+
Rotation happens **before any content is streamed**. When the serving key is rejected with an invalid credential, a rate limit or an exhausted quota, the adapter marks that key and retries the same request with the next usable account. A request that already started streaming is not replayed. Cooldowns are stored in `~/.dsh/storages/dsh-ollama/cooldowns.json` keyed by a SHA-256 fingerprint — raw keys are never written. Authentication rejections (401/403) cool keys for 5 minutes instead of locking them for a day, and quota rejections follow provider Retry-After headers. Cooldowns can be cleared at any time via the **Reset cooldowns** button in the Accounts card or by sending `POST /dsh-ollama/cooldowns/reset`.
|
|
187
187
|
|
|
188
188
|
### Model → account rules
|
|
189
189
|
|
|
@@ -210,7 +210,7 @@ Open **Settings → Ollama Cloud**:
|
|
|
210
210
|
|
|
211
211
|
- **Key** — status, paste a key, clear it, run **Test connection** against a chosen model. The probe disables thinking so the reply is visible.
|
|
212
212
|
- **Balance and usage** — credit bar or legacy windows, spend windows, warning at 80 % and exhausted at 95 %.
|
|
213
|
-
- **Accounts** — extra keys, per-account spend, pin, remove, set a key.
|
|
213
|
+
- **Accounts** — extra keys, per-account spend, pin, remove, set a key, reset cooldowns.
|
|
214
214
|
- **Model → account rules** — glob rules.
|
|
215
215
|
- **Routing** — sticky session per conversation.
|
|
216
216
|
- **Models** — search, refresh the live catalog, hide models, override the wire transport per model, see price, context and capabilities.
|
package/README.ru.md
CHANGED
|
@@ -160,7 +160,7 @@ dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud
|
|
|
160
160
|
|
|
161
161
|
Дополнительный аккаунт добавляется в **Настройки → Ollama Cloud → Аккаунты**: укажите имя метки и credential reference (например, `OLLAMA_API_KEY_2`), вставьте токен и сохраните. Ротация происходит **строго до начала генерации потока**: отклонённый ключ получает cooldown, а запрос повторяется со следующим готовым аккаунтом.
|
|
162
162
|
|
|
163
|
-
Состояния cooldown сохраняются в `~/.dsh/storages/dsh-ollama/cooldowns.json` по SHA-256 хешу ключа.
|
|
163
|
+
Состояния cooldown сохраняются в `~/.dsh/storages/dsh-ollama/cooldowns.json` по SHA-256 хешу ключа. Ошибки авторизации (401/403) охлаждают ключ на 5 минут вместо суток, ошибки квоты учитывают Retry-After. Сбросить охлаждение всех ключей можно кнопкой «Сбросить охлаждение» в карточке аккаунтов или вызовом `POST /dsh-ollama/cooldowns/reset`.
|
|
164
164
|
|
|
165
165
|
### Правила привязки «модель ➔ аккаунт»
|
|
166
166
|
|
package/README.zh.md
CHANGED
|
@@ -155,6 +155,10 @@ dsh plugin --profile desktop add /absolute/path/to/goodandready-dsh-ollama-cloud
|
|
|
155
155
|
|
|
156
156
|
密钥可在官方后台生成:<https://ollama.com/settings/keys>。云端网关仅接受 `Authorization: Bearer` 请求头。
|
|
157
157
|
|
|
158
|
+
### 多账号轮换与冷却重置
|
|
159
|
+
|
|
160
|
+
多账号在首字输出前自动轮换。鉴权失败(401/403)进入 5 分钟冷却期,配额错误遵循 Retry-After 响应头。可在设置页中通过「重置冷却」按钮或调用 `POST /dsh-ollama/cooldowns/reset` 随时清除冷却状态。
|
|
161
|
+
|
|
158
162
|
### 模型到账号路由规则
|
|
159
163
|
|
|
160
164
|
```yaml
|
package/lib/client.js
CHANGED
|
@@ -71,6 +71,8 @@ window.__ModuleLoader__.load({
|
|
|
71
71
|
removeAccount: "Remove",
|
|
72
72
|
addAccount: "Add account",
|
|
73
73
|
checkAllAccounts: "Check all accounts",
|
|
74
|
+
resetCooldowns: "Reset cooldowns",
|
|
75
|
+
cooldownsReset: "Cooldowns reset",
|
|
74
76
|
activeAccount: "Active account",
|
|
75
77
|
active: "active",
|
|
76
78
|
auto: "Automatic",
|
|
@@ -292,7 +294,7 @@ window.__ModuleLoader__.load({
|
|
|
292
294
|
window: "窗口",
|
|
293
295
|
requests: "请求",
|
|
294
296
|
spend: "费用",
|
|
295
|
-
tokens: "Token",
|
|
297
|
+
tokens: "Token 数",
|
|
296
298
|
spend7d: "7 天费用",
|
|
297
299
|
remaining: "剩余",
|
|
298
300
|
quotaIncludedCredits: "包含额度",
|
|
@@ -316,6 +318,8 @@ window.__ModuleLoader__.load({
|
|
|
316
318
|
removeAccount: "移除",
|
|
317
319
|
addAccount: "添加账号",
|
|
318
320
|
checkAllAccounts: "检查所有账号",
|
|
321
|
+
resetCooldowns: "重置冷却",
|
|
322
|
+
cooldownsReset: "冷却已重置",
|
|
319
323
|
activeAccount: "当前账号",
|
|
320
324
|
active: "当前",
|
|
321
325
|
auto: "自动",
|
|
@@ -424,17 +428,17 @@ window.__ModuleLoader__.load({
|
|
|
424
428
|
no: "否",
|
|
425
429
|
generation: "生成默认值",
|
|
426
430
|
generationHint: "Ollama 原生默认值;空字段不发送,请求参数优先。",
|
|
427
|
-
temperature: "Temperature",
|
|
428
|
-
topK: "Top K",
|
|
429
|
-
topP: "Top P",
|
|
430
|
-
minP: "Min P",
|
|
431
|
-
seed: "Seed",
|
|
432
|
-
repeatPenalty: "Repeat penalty",
|
|
433
|
-
repeatLastN: "Repeat last N",
|
|
434
|
-
numCtx: "num_ctx",
|
|
435
|
-
keepAlive: "Keep
|
|
431
|
+
temperature: "采样温度 (Temperature)",
|
|
432
|
+
topK: "Top K 采样",
|
|
433
|
+
topP: "Top P 采样",
|
|
434
|
+
minP: "Min P 采样",
|
|
435
|
+
seed: "随机种子 (Seed)",
|
|
436
|
+
repeatPenalty: "重复惩罚 (Repeat penalty)",
|
|
437
|
+
repeatLastN: "重复惩罚窗口 (Repeat last N)",
|
|
438
|
+
numCtx: "上下文大小 (num_ctx)",
|
|
439
|
+
keepAlive: "保活时间 (Keep-alive)",
|
|
436
440
|
keepAliveHint: "Ollama keep_alive,例如 5m 或 -1。",
|
|
437
|
-
stop: "Stop sequences",
|
|
441
|
+
stop: "停止序列 (Stop sequences)",
|
|
438
442
|
stopHint: "逗号分隔,作为原生 stop 列表发送。",
|
|
439
443
|
autoRefresh: "自动刷新模型目录",
|
|
440
444
|
refreshMinutes: "刷新间隔(分钟)",
|
|
@@ -608,6 +612,12 @@ window.__ModuleLoader__.load({
|
|
|
608
612
|
})
|
|
609
613
|
});
|
|
610
614
|
}
|
|
615
|
+
function resetCooldowns() {
|
|
616
|
+
return call("/dsh-ollama/cooldowns/reset", {
|
|
617
|
+
method: "POST",
|
|
618
|
+
body: "{}"
|
|
619
|
+
});
|
|
620
|
+
}
|
|
611
621
|
function saveConfig(patch) {
|
|
612
622
|
return call("/dsh-ollama/config", {
|
|
613
623
|
method: "POST",
|
|
@@ -2220,6 +2230,7 @@ window.__ModuleLoader__.load({
|
|
|
2220
2230
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
2221
2231
|
type: "checkbox",
|
|
2222
2232
|
checked: visible,
|
|
2233
|
+
"aria-label": `${model.id} ${t("visibility")}`,
|
|
2223
2234
|
onChange: (event) => onVisible(event.target.checked)
|
|
2224
2235
|
}),
|
|
2225
2236
|
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
@@ -2376,6 +2387,15 @@ window.__ModuleLoader__.load({
|
|
|
2376
2387
|
setError(caught instanceof Error ? caught.message : t("failed"));
|
|
2377
2388
|
}
|
|
2378
2389
|
}, [t]);
|
|
2390
|
+
const handleResetCooldowns = (0, react.useCallback)(async () => {
|
|
2391
|
+
try {
|
|
2392
|
+
await resetCooldowns();
|
|
2393
|
+
setNotice(t("cooldownsReset"));
|
|
2394
|
+
await refresh();
|
|
2395
|
+
} catch (caught) {
|
|
2396
|
+
setError(caught instanceof Error ? caught.message : t("failed"));
|
|
2397
|
+
}
|
|
2398
|
+
}, [refresh, t]);
|
|
2379
2399
|
const reloadModels = (0, react.useCallback)(async () => {
|
|
2380
2400
|
try {
|
|
2381
2401
|
const next = await refreshModels();
|
|
@@ -2550,6 +2570,7 @@ window.__ModuleLoader__.load({
|
|
|
2550
2570
|
className: "ocl-input",
|
|
2551
2571
|
type: "password",
|
|
2552
2572
|
placeholder: t("pasteKey"),
|
|
2573
|
+
"aria-label": t("pasteKey"),
|
|
2553
2574
|
value: keyDraft,
|
|
2554
2575
|
onChange: (event) => setKeyDraft(event.target.value)
|
|
2555
2576
|
}),
|
|
@@ -2753,6 +2774,7 @@ window.__ModuleLoader__.load({
|
|
|
2753
2774
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
2754
2775
|
className: "ocl-input",
|
|
2755
2776
|
placeholder: t("accountLabel"),
|
|
2777
|
+
"aria-label": t("accountLabel"),
|
|
2756
2778
|
value: account.label,
|
|
2757
2779
|
onChange: (event) => {
|
|
2758
2780
|
const accounts = draft.accounts.map((entry, position) => position === index ? {
|
|
@@ -2765,6 +2787,7 @@ window.__ModuleLoader__.load({
|
|
|
2765
2787
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
2766
2788
|
className: "ocl-input",
|
|
2767
2789
|
placeholder: t("accountRef"),
|
|
2790
|
+
"aria-label": t("accountRef"),
|
|
2768
2791
|
value: account.apiKeyEnv,
|
|
2769
2792
|
onChange: (event) => {
|
|
2770
2793
|
const accounts = draft.accounts.map((entry, position) => position === index ? {
|
|
@@ -2816,6 +2839,7 @@ window.__ModuleLoader__.load({
|
|
|
2816
2839
|
className: "ocl-input",
|
|
2817
2840
|
type: "password",
|
|
2818
2841
|
placeholder: t("pasteKey"),
|
|
2842
|
+
"aria-label": `${t("pasteKey")} (${account.label})`,
|
|
2819
2843
|
value: accountKeys[account.label] ?? "",
|
|
2820
2844
|
onChange: (event) => {
|
|
2821
2845
|
setAccountKeys((current) => ({
|
|
@@ -2883,6 +2907,14 @@ window.__ModuleLoader__.load({
|
|
|
2883
2907
|
},
|
|
2884
2908
|
children: t("checkAllAccounts")
|
|
2885
2909
|
}),
|
|
2910
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
|
|
2911
|
+
type: "button",
|
|
2912
|
+
className: "ocl-button",
|
|
2913
|
+
onClick: () => {
|
|
2914
|
+
handleResetCooldowns();
|
|
2915
|
+
},
|
|
2916
|
+
children: t("resetCooldowns")
|
|
2917
|
+
}),
|
|
2886
2918
|
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", {
|
|
2887
2919
|
className: "ocl-badge",
|
|
2888
2920
|
children: [
|
|
@@ -2918,6 +2950,7 @@ window.__ModuleLoader__.load({
|
|
|
2918
2950
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
2919
2951
|
className: "ocl-input",
|
|
2920
2952
|
placeholder: t("rulePattern"),
|
|
2953
|
+
"aria-label": t("rulePattern"),
|
|
2921
2954
|
value: rule.pattern,
|
|
2922
2955
|
onChange: (event) => {
|
|
2923
2956
|
const rules = draft.modelAccountRules.map((entry, position) => position === index ? {
|
|
@@ -2930,6 +2963,7 @@ window.__ModuleLoader__.load({
|
|
|
2930
2963
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
2931
2964
|
className: "ocl-input",
|
|
2932
2965
|
placeholder: t("ruleAccount"),
|
|
2966
|
+
"aria-label": t("ruleAccount"),
|
|
2933
2967
|
value: rule.account,
|
|
2934
2968
|
onChange: (event) => {
|
|
2935
2969
|
const rules = draft.modelAccountRules.map((entry, position) => position === index ? {
|
|
@@ -3093,6 +3127,7 @@ window.__ModuleLoader__.load({
|
|
|
3093
3127
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("input", {
|
|
3094
3128
|
className: "ocl-input",
|
|
3095
3129
|
placeholder: t("modelSearch"),
|
|
3130
|
+
"aria-label": t("modelSearch"),
|
|
3096
3131
|
value: search,
|
|
3097
3132
|
onChange: (event) => setSearch(event.target.value)
|
|
3098
3133
|
}),
|
package/lib/index.d.ts
CHANGED
|
@@ -306,6 +306,8 @@ declare class OllamaCloudAdapter extends LlmAdapter {
|
|
|
306
306
|
} | undefined;
|
|
307
307
|
/** Await pending cooldown persistence writes (tests and shutdown). */
|
|
308
308
|
flushCooldowns(): Promise<void>;
|
|
309
|
+
/** Manually clear all active account and model cooldowns. */
|
|
310
|
+
clearCooldowns(): void;
|
|
309
311
|
private loadCooldowns;
|
|
310
312
|
private pruneCooldowns;
|
|
311
313
|
private persistCooldowns;
|
|
@@ -517,7 +519,7 @@ interface EffectiveModel extends OllamaCloudModelMeta {
|
|
|
517
519
|
}
|
|
518
520
|
declare function effectiveModel(id: string, config: ResolvedConfig): EffectiveModel;
|
|
519
521
|
declare function describeModel(meta: EffectiveModel, config: ResolvedConfig): string;
|
|
520
|
-
declare const name = "
|
|
522
|
+
declare const name = "@goodandready/dsh-ollama-cloud-provider";
|
|
521
523
|
declare const inject: string[];
|
|
522
524
|
/**
|
|
523
525
|
* Register the `ollama-cloud` route against the dsh `llm` service. Requests
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
2
|
import { existsSync, readFileSync, statSync, unlinkSync } from "node:fs";
|
|
3
|
-
import { appendFile, mkdir, readFile, readdir, rename, stat, unlink, writeFile } from "node:fs/promises";
|
|
4
|
-
import { homedir } from "node:os";
|
|
3
|
+
import { appendFile, mkdir, readFile, readdir, rename, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir, networkInterfaces } from "node:os";
|
|
5
5
|
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
6
6
|
import { CONTEXT_WINDOW_EXCEEDED_CODE, LlmAdapter, LlmError, ReasoningEffortId, ToolCallId, attributionHeaders, isContextWindowExceededError, resolveRetryPolicy } from "@deepseek-ai/dsh-llm";
|
|
7
7
|
import z from "@deepseek-ai/schemastery";
|
|
@@ -2284,8 +2284,8 @@ function text(value) {
|
|
|
2284
2284
|
return typeof value === "string" ? value : "";
|
|
2285
2285
|
}
|
|
2286
2286
|
function finishReasonOf$1(raw, hasToolCalls) {
|
|
2287
|
-
if (hasToolCalls) return { kind: "tool-calls" };
|
|
2288
2287
|
if (raw === "length") return { kind: "max-tokens" };
|
|
2288
|
+
if (hasToolCalls) return { kind: "tool-calls" };
|
|
2289
2289
|
return { kind: "stop" };
|
|
2290
2290
|
}
|
|
2291
2291
|
function usageOf(chunk) {
|
|
@@ -2757,6 +2757,18 @@ function effortName(id) {
|
|
|
2757
2757
|
if (id === "off" || id === "none") return "Off";
|
|
2758
2758
|
return id.charAt(0).toUpperCase() + id.slice(1);
|
|
2759
2759
|
}
|
|
2760
|
+
async function atomicWriteFile(filePath, data) {
|
|
2761
|
+
const dir = dirname(filePath);
|
|
2762
|
+
await mkdir(dir, { recursive: true });
|
|
2763
|
+
const tmp = join(dir, `.${basename(filePath)}.${randomUUID()}.tmp`);
|
|
2764
|
+
try {
|
|
2765
|
+
await writeFile(tmp, data, "utf8");
|
|
2766
|
+
await rename(tmp, filePath);
|
|
2767
|
+
} catch (error) {
|
|
2768
|
+
await rm(tmp, { force: true }).catch(() => {});
|
|
2769
|
+
throw error;
|
|
2770
|
+
}
|
|
2771
|
+
}
|
|
2760
2772
|
var OllamaCloudAdapter = class extends LlmAdapter {
|
|
2761
2773
|
deps;
|
|
2762
2774
|
fetchImpl;
|
|
@@ -2981,7 +2993,18 @@ var OllamaCloudAdapter = class extends LlmAdapter {
|
|
|
2981
2993
|
budget.dispose();
|
|
2982
2994
|
}
|
|
2983
2995
|
}
|
|
2984
|
-
const
|
|
2996
|
+
const cooldownEntries = rawKeys.map((entry) => this.cooldowns.get(this.keyId(entry.key))).filter((entry) => entry !== void 0 && entry.until > Date.now());
|
|
2997
|
+
let finalError = lastError;
|
|
2998
|
+
if (finalError === void 0) {
|
|
2999
|
+
if (cooldownEntries.length > 0) {
|
|
3000
|
+
const minUntil = Math.min(...cooldownEntries.map((e) => e.until));
|
|
3001
|
+
const remainingMinutes = Math.max(1, Math.ceil((minUntil - Date.now()) / 6e4));
|
|
3002
|
+
const reasons = new Set(cooldownEntries.map((e) => e.reason));
|
|
3003
|
+
if (reasons.has("invalid-credential") && reasons.size === 1) finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down due to invalid credentials (retry in ${remainingMinutes}m)`, "INVALID_CREDENTIAL");
|
|
3004
|
+
else if (reasons.has("quota") && !reasons.has("rate-limit")) finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down due to quota limits (retry in ${remainingMinutes}m)`, "QUOTA");
|
|
3005
|
+
else finalError = new LlmError(`llm-ollama-cloud: every configured account is cooling down after provider rejections (retry in ${remainingMinutes}m)`, "RATE_LIMIT");
|
|
3006
|
+
} else finalError = new LlmError("llm-ollama-cloud: every configured account is cooling down after provider rejections", "RATE_LIMIT");
|
|
3007
|
+
}
|
|
2985
3008
|
if (!loggedAny) record(options.provider, options.model, options.purpose, lastAccount, reason, {
|
|
2986
3009
|
at: runStartedAt,
|
|
2987
3010
|
ok: false,
|
|
@@ -3063,8 +3086,7 @@ var OllamaCloudAdapter = class extends LlmAdapter {
|
|
|
3063
3086
|
this.pruneSticky();
|
|
3064
3087
|
const payload = JSON.stringify({ sticky: Object.fromEntries(this.sticky) });
|
|
3065
3088
|
this.stickyWrites = this.stickyWrites.then(async () => {
|
|
3066
|
-
await
|
|
3067
|
-
await writeFile(this.stickyPath, payload, "utf8");
|
|
3089
|
+
await atomicWriteFile(this.stickyPath, payload);
|
|
3068
3090
|
}).catch(() => {});
|
|
3069
3091
|
}
|
|
3070
3092
|
pushTrace(entry) {
|
|
@@ -3195,6 +3217,12 @@ var OllamaCloudAdapter = class extends LlmAdapter {
|
|
|
3195
3217
|
async flushCooldowns() {
|
|
3196
3218
|
await this.cooldownWrites;
|
|
3197
3219
|
}
|
|
3220
|
+
/** Manually clear all active account and model cooldowns. */
|
|
3221
|
+
clearCooldowns() {
|
|
3222
|
+
this.cooldowns.clear();
|
|
3223
|
+
this.modelCooldowns.clear();
|
|
3224
|
+
this.persistCooldowns();
|
|
3225
|
+
}
|
|
3198
3226
|
loadCooldowns() {
|
|
3199
3227
|
try {
|
|
3200
3228
|
const entries = JSON.parse(readFileSync(this.cooldownPath, "utf8")).cooldowns;
|
|
@@ -3227,8 +3255,7 @@ var OllamaCloudAdapter = class extends LlmAdapter {
|
|
|
3227
3255
|
this.pruneCooldowns();
|
|
3228
3256
|
const payload = JSON.stringify({ cooldowns: Object.fromEntries(this.cooldowns) });
|
|
3229
3257
|
this.cooldownWrites = this.cooldownWrites.then(async () => {
|
|
3230
|
-
await
|
|
3231
|
-
await writeFile(this.cooldownPath, payload, "utf8");
|
|
3258
|
+
await atomicWriteFile(this.cooldownPath, payload);
|
|
3232
3259
|
}).catch(() => {});
|
|
3233
3260
|
}
|
|
3234
3261
|
markCooldown(key, status, headers, code) {
|
|
@@ -3239,7 +3266,7 @@ var OllamaCloudAdapter = class extends LlmAdapter {
|
|
|
3239
3266
|
delayMs = Number.isFinite(seconds) && seconds > 0 ? seconds * 1e3 : 6e5;
|
|
3240
3267
|
reason = "quota";
|
|
3241
3268
|
} else if (status === 401 || status === 403) {
|
|
3242
|
-
delayMs =
|
|
3269
|
+
delayMs = 3e5;
|
|
3243
3270
|
reason = "invalid-credential";
|
|
3244
3271
|
} else if (status === 429) {
|
|
3245
3272
|
const seconds = Number(headers?.get("retry-after") ?? NaN);
|
|
@@ -4326,6 +4353,39 @@ async function discoverModels(rawBase, key, fetchImpl = fetch, options = {}) {
|
|
|
4326
4353
|
models: [...new Set(idsOf(payload))]
|
|
4327
4354
|
};
|
|
4328
4355
|
}
|
|
4356
|
+
// src/local-host.ts
|
|
4357
|
+
const LOOPBACK = /* @__PURE__ */ new Set([
|
|
4358
|
+
"localhost",
|
|
4359
|
+
"localhost.",
|
|
4360
|
+
"::1",
|
|
4361
|
+
"[::1]"
|
|
4362
|
+
]);
|
|
4363
|
+
function normalize(value) {
|
|
4364
|
+
return value.toLowerCase().replace(/^\[|\]$/g, "").replace(/%.+$/, "");
|
|
4365
|
+
}
|
|
4366
|
+
function isLoopbackHostname(value) {
|
|
4367
|
+
const hostname = normalize(value);
|
|
4368
|
+
return LOOPBACK.has(hostname) || hostname.startsWith("127.") || hostname.startsWith("::ffff:127.");
|
|
4369
|
+
}
|
|
4370
|
+
function localAddresses() {
|
|
4371
|
+
const addresses = /* @__PURE__ */ new Set();
|
|
4372
|
+
for (const entries of Object.values(networkInterfaces())) for (const entry of entries ?? []) addresses.add(normalize(entry.address));
|
|
4373
|
+
return addresses;
|
|
4374
|
+
}
|
|
4375
|
+
function isLocalHostname(value) {
|
|
4376
|
+
const hostname = normalize(value);
|
|
4377
|
+
if (isLoopbackHostname(hostname)) return true;
|
|
4378
|
+
if (isIP(hostname) === 0) return false;
|
|
4379
|
+
return localAddresses().has(hostname);
|
|
4380
|
+
}
|
|
4381
|
+
function authorityHostname(value) {
|
|
4382
|
+
if (value === void 0 || value === "") return void 0;
|
|
4383
|
+
try {
|
|
4384
|
+
return normalize(new URL(`http://${value}`).hostname);
|
|
4385
|
+
} catch {
|
|
4386
|
+
return;
|
|
4387
|
+
}
|
|
4388
|
+
}
|
|
4329
4389
|
// src/routes-public.ts
|
|
4330
4390
|
function publicConfig(config) {
|
|
4331
4391
|
return {
|
|
@@ -4624,10 +4684,10 @@ function writeSourceError(req) {
|
|
|
4624
4684
|
return sourceHost(referer) === host ? void 0 : "cross-origin request refused";
|
|
4625
4685
|
}
|
|
4626
4686
|
function allowedOrigin(req) {
|
|
4627
|
-
const
|
|
4628
|
-
if (
|
|
4687
|
+
const source = req.headers.origin ?? req.headers.referer;
|
|
4688
|
+
if (source === void 0 || source === "") return true;
|
|
4629
4689
|
try {
|
|
4630
|
-
const parsed = new URL(
|
|
4690
|
+
const parsed = new URL(source);
|
|
4631
4691
|
const host = req.headers.host;
|
|
4632
4692
|
if (host !== void 0 && parsed.host === host) return true;
|
|
4633
4693
|
return parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost" || parsed.hostname === "::1" || parsed.hostname === "[::1]";
|
|
@@ -4640,6 +4700,10 @@ function allowedOrigin(req) {
|
|
|
4640
4700
|
* while the socket reaches loopback. Write endpoints accept only a literal
|
|
4641
4701
|
* loopback Host.
|
|
4642
4702
|
*/
|
|
4703
|
+
function trustedHost(req) {
|
|
4704
|
+
const hostname = authorityHostname(req.headers.host);
|
|
4705
|
+
return hostname !== void 0 && isLocalHostname(hostname);
|
|
4706
|
+
}
|
|
4643
4707
|
function loopbackHost(req) {
|
|
4644
4708
|
const host = req.headers.host;
|
|
4645
4709
|
if (host === void 0 || host === "") return false;
|
|
@@ -4664,7 +4728,7 @@ async function readJson(req) {
|
|
|
4664
4728
|
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("body must be a JSON object");
|
|
4665
4729
|
return parsed;
|
|
4666
4730
|
}
|
|
4667
|
-
async function requirePost(req, res,
|
|
4731
|
+
async function requirePost(req, res, verifyHost) {
|
|
4668
4732
|
if (req.method !== "POST") {
|
|
4669
4733
|
json(res, 405, { error: "POST required" });
|
|
4670
4734
|
return false;
|
|
@@ -4678,18 +4742,17 @@ async function requirePost(req, res, verifyDesktop) {
|
|
|
4678
4742
|
json(res, 403, { error: "write endpoints are restricted to the local machine" });
|
|
4679
4743
|
return false;
|
|
4680
4744
|
}
|
|
4681
|
-
if (desktopShellRequest(req)) {
|
|
4682
|
-
if (!await verifyDesktop(req)) {
|
|
4683
|
-
json(res, 403, { error: "desktop authentication failed" });
|
|
4684
|
-
return false;
|
|
4685
|
-
}
|
|
4686
|
-
} else {
|
|
4745
|
+
if (!desktopShellRequest(req)) {
|
|
4687
4746
|
const sourceError = writeSourceError(req);
|
|
4688
4747
|
if (sourceError !== void 0) {
|
|
4689
4748
|
json(res, 403, { error: sourceError });
|
|
4690
4749
|
return false;
|
|
4691
4750
|
}
|
|
4692
4751
|
}
|
|
4752
|
+
if (!await verifyHost(req)) {
|
|
4753
|
+
json(res, 403, { error: "authentication required" });
|
|
4754
|
+
return false;
|
|
4755
|
+
}
|
|
4693
4756
|
if (!(req.headers["content-type"] ?? "").includes("application/json")) {
|
|
4694
4757
|
json(res, 415, { error: "application/json required" });
|
|
4695
4758
|
return false;
|
|
@@ -4920,6 +4983,10 @@ function applyRoutes(ctx, web, deps) {
|
|
|
4920
4983
|
json(res, 405, { error: "GET required" });
|
|
4921
4984
|
return false;
|
|
4922
4985
|
}
|
|
4986
|
+
if (!trustedHost(req)) {
|
|
4987
|
+
json(res, 403, { error: "non-local Host refused" });
|
|
4988
|
+
return false;
|
|
4989
|
+
}
|
|
4923
4990
|
if (!allowedOrigin(req)) {
|
|
4924
4991
|
json(res, 403, { error: "cross-origin request refused" });
|
|
4925
4992
|
return false;
|
|
@@ -5007,6 +5074,11 @@ function applyRoutes(ctx, web, deps) {
|
|
|
5007
5074
|
json(res, 500, { error: error instanceof Error ? error.message : "balance failed" });
|
|
5008
5075
|
}
|
|
5009
5076
|
});
|
|
5077
|
+
route("/dsh-ollama/cooldowns/reset", async (req, res) => {
|
|
5078
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
5079
|
+
deps.adapter.clearCooldowns?.();
|
|
5080
|
+
json(res, 200, { ok: true });
|
|
5081
|
+
});
|
|
5010
5082
|
route("/dsh-ollama/config", async (req, res) => {
|
|
5011
5083
|
if (!await requirePost(req, res, verifyHost)) return;
|
|
5012
5084
|
const settings = getSettings(ctx);
|
|
@@ -5816,6 +5888,9 @@ var OllamaWebSearchProvider = class extends OllamaWebEndpoint {
|
|
|
5816
5888
|
};
|
|
5817
5889
|
var OllamaWebFetchProvider = class extends OllamaWebEndpoint {
|
|
5818
5890
|
async fetch(request, signal) {
|
|
5891
|
+
if (!URL.canParse(request.url)) throw new WebError("invalid web fetch URL", "INVALID_URL");
|
|
5892
|
+
const parsed = new URL(request.url);
|
|
5893
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") throw new WebError("only http: and https: protocols are supported for web fetch", "UNSUPPORTED_PROTOCOL");
|
|
5819
5894
|
const { status, body } = await postJson(this.options, "/api/web_fetch", { url: request.url }, signal);
|
|
5820
5895
|
const decoded = decodeFetchResponse(body);
|
|
5821
5896
|
return {
|
|
@@ -5830,7 +5905,7 @@ var OllamaWebFetchProvider = class extends OllamaWebEndpoint {
|
|
|
5830
5905
|
}
|
|
5831
5906
|
};
|
|
5832
5907
|
// src/index.ts
|
|
5833
|
-
const name = "
|
|
5908
|
+
const name = "@goodandready/dsh-ollama-cloud-provider";
|
|
5834
5909
|
const inject = ["llm"];
|
|
5835
5910
|
/**
|
|
5836
5911
|
* Register the `ollama-cloud` route against the dsh `llm` service. Requests
|