@goodandready/dsh-ollama-cloud-provider 0.1.38 → 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.
- package/CHANGELOG.md +23 -0
- package/README.md +13 -6
- package/README.ru.md +13 -6
- package/README.zh.md +13 -6
- package/lib/adapter.js +10 -3
- package/lib/catalog-cache.js +8 -2
- package/lib/catalog-scheduler.js +34 -0
- package/lib/catalog.js +23 -14
- package/lib/client.js +171 -94
- package/lib/config.js +23 -9
- package/lib/discovery.js +75 -7
- package/lib/endpoints.js +58 -0
- package/lib/index.js +14 -10
- package/lib/local-host.js +17 -8
- package/lib/plugin-updater.js +35 -8
- package/lib/request-log.js +22 -4
- package/lib/route-guard.js +25 -0
- package/lib/routes-values.js +17 -4
- package/lib/routes.js +138 -77
- package/lib/status.js +20 -1
- package/lib/transports/ndjson.js +10 -3
- package/lib/transports/openai-chat.js +32 -3
- package/lib/transports/sse.js +11 -3
- package/lib/usage.js +17 -10
- package/package.json +2 -2
package/lib/endpoints.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
// src/endpoints.ts
|
|
3
|
+
function isLinkLocalHost(hostname) {
|
|
4
|
+
const host = hostname.replace(/^\[|\]$/g, "").toLowerCase();
|
|
5
|
+
if (host === "0.0.0.0" || host === "::") return true;
|
|
6
|
+
const family = isIP(host);
|
|
7
|
+
if (family === 4) return host.startsWith("169.254.");
|
|
8
|
+
if (family === 6) return /^fe[89ab]/.test(host);
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Endpoint URLs carry the API key, so only an absolute http(s) URL without
|
|
13
|
+
* embedded credentials or a fragment is accepted, and link-local/metadata
|
|
14
|
+
* addresses are refused.
|
|
15
|
+
*/
|
|
16
|
+
function checkEndpoint(field, value) {
|
|
17
|
+
if (typeof value !== "string" || value.trim() === "") return {
|
|
18
|
+
ok: false,
|
|
19
|
+
error: `${field} must be a non-empty URL`
|
|
20
|
+
};
|
|
21
|
+
const candidate = value.trim();
|
|
22
|
+
let url;
|
|
23
|
+
try {
|
|
24
|
+
url = new URL(candidate);
|
|
25
|
+
} catch {
|
|
26
|
+
return {
|
|
27
|
+
ok: false,
|
|
28
|
+
error: `${field} must be an absolute URL`
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return {
|
|
32
|
+
ok: false,
|
|
33
|
+
error: `${field} must use http or https`
|
|
34
|
+
};
|
|
35
|
+
if (url.username !== "" || url.password !== "") return {
|
|
36
|
+
ok: false,
|
|
37
|
+
error: `${field} must not embed credentials`
|
|
38
|
+
};
|
|
39
|
+
if (url.hash !== "") return {
|
|
40
|
+
ok: false,
|
|
41
|
+
error: `${field} must not contain a fragment`
|
|
42
|
+
};
|
|
43
|
+
if (isLinkLocalHost(url.hostname)) return {
|
|
44
|
+
ok: false,
|
|
45
|
+
error: `${field} must not target a link-local address`
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
ok: true,
|
|
49
|
+
value: candidate
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function endpointOr(value, field, fallback) {
|
|
53
|
+
const checked = checkEndpoint(field, value);
|
|
54
|
+
return checked.ok ? checked.value ?? fallback : fallback;
|
|
55
|
+
}
|
|
56
|
+
export { checkEndpoint, endpointOr };
|
|
57
|
+
|
|
58
|
+
//# sourceMappingURL=endpoints.js.map
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { OllamaCloudAdapter } from "./adapter.js";
|
|
2
2
|
import { bestEffortFailed, setBestEffortLogger } from "./best-effort.js";
|
|
3
3
|
import { clearCatalogCache, describeModel, effectiveModel, refreshCatalog } from "./catalog.js";
|
|
4
|
+
import { createCatalogScheduler } from "./catalog-scheduler.js";
|
|
4
5
|
import { applyCommands } from "./commands.js";
|
|
5
6
|
import { Config, NS, resolveConfig, unwrapVolatileConfig } from "./config.js";
|
|
6
7
|
import { createKeyPool, createKeyResolver } from "./credentials.js";
|
|
@@ -49,22 +50,25 @@ function apply(ctx, config) {
|
|
|
49
50
|
flushRequestLog();
|
|
50
51
|
}, "dsh-ollama-cloud-provider: flush request log");
|
|
51
52
|
ctx.effect(() => {
|
|
52
|
-
const
|
|
53
|
-
const warm = initial.autoRefresh ? setTimeout(() => {
|
|
53
|
+
const warm = current().autoRefresh ? setTimeout(() => {
|
|
54
54
|
const config = current();
|
|
55
55
|
if (config.autoRefresh) refreshCatalog(config, fetch).catch((err) => bestEffortFailed("warmCatalog", err));
|
|
56
56
|
}, 1e3) : void 0;
|
|
57
57
|
warm?.unref?.();
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
const scheduler = createCatalogScheduler({
|
|
59
|
+
current: () => {
|
|
60
|
+
const config = current();
|
|
61
|
+
return {
|
|
62
|
+
autoRefresh: config.autoRefresh,
|
|
63
|
+
refreshMinutes: config.refreshMinutes
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
refresh: () => refreshCatalog(current(), fetch, true),
|
|
67
|
+
onError: (error) => bestEffortFailed("refreshCatalog", error)
|
|
68
|
+
});
|
|
65
69
|
return () => {
|
|
66
70
|
if (warm !== void 0) clearTimeout(warm);
|
|
67
|
-
|
|
71
|
+
scheduler.dispose();
|
|
68
72
|
};
|
|
69
73
|
}, "dsh-ollama-cloud-provider: catalog refresh scheduler");
|
|
70
74
|
ctx.inject(["settings"], (settingsCtx) => {
|
package/lib/local-host.js
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
import { networkInterfaces } from "node:os";
|
|
2
2
|
import { isIP } from "node:net";
|
|
3
3
|
// src/local-host.ts
|
|
4
|
-
const
|
|
5
|
-
"localhost",
|
|
6
|
-
"localhost.",
|
|
7
|
-
"::1",
|
|
8
|
-
"[::1]"
|
|
9
|
-
]);
|
|
4
|
+
const LOOPBACK_NAMES = /* @__PURE__ */ new Set(["localhost", "localhost."]);
|
|
10
5
|
function normalize(value) {
|
|
11
6
|
return value.toLowerCase().replace(/^\[|\]$/g, "").replace(/%.+$/, "");
|
|
12
7
|
}
|
|
8
|
+
/**
|
|
9
|
+
* A DNS name is never loopback: only a literal IPv4/IPv6 loopback address is.
|
|
10
|
+
* Prefix matching on the raw string would accept `127.0.0.1.attacker.example`,
|
|
11
|
+
* which is exactly the host a DNS-rebinding page presents.
|
|
12
|
+
*/
|
|
13
|
+
function isLoopbackAddress(value) {
|
|
14
|
+
if (value === void 0) return false;
|
|
15
|
+
const address = normalize(value);
|
|
16
|
+
const family = isIP(address);
|
|
17
|
+
if (family === 4) return address.startsWith("127.");
|
|
18
|
+
if (family === 6) return address === "::1" || address.startsWith("::ffff:127.");
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
13
21
|
function isLoopbackHostname(value) {
|
|
14
22
|
const hostname = normalize(value);
|
|
15
|
-
|
|
23
|
+
if (LOOPBACK_NAMES.has(hostname)) return true;
|
|
24
|
+
return isLoopbackAddress(hostname);
|
|
16
25
|
}
|
|
17
26
|
function localAddresses() {
|
|
18
27
|
const addresses = /* @__PURE__ */ new Set();
|
|
@@ -33,6 +42,6 @@ function authorityHostname(value) {
|
|
|
33
42
|
return;
|
|
34
43
|
}
|
|
35
44
|
}
|
|
36
|
-
export { authorityHostname, isLocalHostname, isLoopbackHostname };
|
|
45
|
+
export { authorityHostname, isLocalHostname, isLoopbackAddress, isLoopbackHostname };
|
|
37
46
|
|
|
38
47
|
//# sourceMappingURL=local-host.js.map
|
package/lib/plugin-updater.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { authorityHostname, isLocalHostname, isLoopbackAddress, isLoopbackHostname } from "./local-host.js";
|
|
2
3
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
3
4
|
import { readFile } from "node:fs/promises";
|
|
4
5
|
import { homedir } from "node:os";
|
|
@@ -14,13 +15,9 @@ function header(request, name) {
|
|
|
14
15
|
const value = request.headers?.[name];
|
|
15
16
|
return Array.isArray(value) ? value[0] : value;
|
|
16
17
|
}
|
|
17
|
-
function isLoopback(value) {
|
|
18
|
-
const address = value?.toLowerCase().replace(/^\[|\]$/g, "");
|
|
19
|
-
return address === "localhost" || address === "localhost." || address === "::1" || address?.startsWith("127.") === true || address?.startsWith("::ffff:127.") === true;
|
|
20
|
-
}
|
|
21
18
|
function isTrustedUpdateRequest(request) {
|
|
22
19
|
if (header(request, UPDATE_HEADER) !== "1") return false;
|
|
23
|
-
if (!
|
|
20
|
+
if (!isLoopbackAddress(request.socket?.remoteAddress)) return false;
|
|
24
21
|
const site = header(request, "sec-fetch-site");
|
|
25
22
|
if (site !== void 0 && site !== "same-origin") return false;
|
|
26
23
|
const origin = header(request, "origin");
|
|
@@ -28,7 +25,25 @@ function isTrustedUpdateRequest(request) {
|
|
|
28
25
|
if (origin === void 0 || host === void 0) return false;
|
|
29
26
|
try {
|
|
30
27
|
const url = new URL(origin);
|
|
31
|
-
return (url.protocol === "http:" || url.protocol === "https:") &&
|
|
28
|
+
return (url.protocol === "http:" || url.protocol === "https:") && isLoopbackHostname(url.hostname) && url.host === host;
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Read-only status must also come from the local UI: a remote page that reaches
|
|
35
|
+
* the port through DNS rebinding presents its own host name and is refused.
|
|
36
|
+
*/
|
|
37
|
+
function isTrustedReadRequest(request) {
|
|
38
|
+
const host = header(request, "host");
|
|
39
|
+
if (host === void 0 || host === "") return false;
|
|
40
|
+
const hostname = authorityHostname(host);
|
|
41
|
+
if (hostname === void 0 || !isLocalHostname(hostname)) return false;
|
|
42
|
+
const origin = header(request, "origin");
|
|
43
|
+
if (origin === void 0 || origin === "") return true;
|
|
44
|
+
try {
|
|
45
|
+
const url = new URL(origin);
|
|
46
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.host === host && isLocalHostname(url.hostname);
|
|
32
47
|
} catch {
|
|
33
48
|
return false;
|
|
34
49
|
}
|
|
@@ -84,13 +99,16 @@ function profileFromArgv(argv) {
|
|
|
84
99
|
};
|
|
85
100
|
}
|
|
86
101
|
}
|
|
102
|
+
const RUNTIME_CACHE_MS = 3e4;
|
|
103
|
+
let runtimeCache;
|
|
87
104
|
function runtime() {
|
|
105
|
+
if (runtimeCache !== void 0 && Date.now() - runtimeCache.at < RUNTIME_CACHE_MS) return runtimeCache.value;
|
|
88
106
|
const fromArgv = profileFromArgv(process.argv);
|
|
89
107
|
const profileDir = resolve(process.env.DSH_PROFILE_DIR ?? fromArgv?.dir ?? resolve(homedir(), ".dsh", "profiles", "web"));
|
|
90
108
|
const selected = process.env.DSH_PROFILE_NAME ?? profileNameFromArgv(process.argv) ?? fromArgv?.name;
|
|
91
109
|
const profileName = validProfileName(selected) ? selected : validProfileName(basename(profileDir)) ? basename(profileDir) : "web";
|
|
92
110
|
const cliEntry = findDshCliEntry();
|
|
93
|
-
|
|
111
|
+
const value = cliEntry === void 0 ? {
|
|
94
112
|
profileName,
|
|
95
113
|
profileDir
|
|
96
114
|
} : {
|
|
@@ -98,6 +116,11 @@ function runtime() {
|
|
|
98
116
|
profileDir,
|
|
99
117
|
cliEntry
|
|
100
118
|
};
|
|
119
|
+
runtimeCache = {
|
|
120
|
+
at: Date.now(),
|
|
121
|
+
value
|
|
122
|
+
};
|
|
123
|
+
return value;
|
|
101
124
|
}
|
|
102
125
|
function parseSemver(value) {
|
|
103
126
|
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(value);
|
|
@@ -240,6 +263,10 @@ function registerPluginUpdater(ctx, options) {
|
|
|
240
263
|
try {
|
|
241
264
|
const target = runtime();
|
|
242
265
|
if (request.method === "GET" || request.method === "HEAD") {
|
|
266
|
+
if (!isTrustedReadRequest(request)) {
|
|
267
|
+
json(response, 403, { error: "Rejected non-local update status request." });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
243
270
|
const payload = await status(options, target);
|
|
244
271
|
response.writeHead(200, {
|
|
245
272
|
"content-type": "application/json; charset=utf-8",
|
|
@@ -288,6 +315,6 @@ function registerPluginUpdater(ctx, options) {
|
|
|
288
315
|
}
|
|
289
316
|
});
|
|
290
317
|
}
|
|
291
|
-
export { isNewerVersion, isTrustedUpdateRequest, registerPluginUpdater };
|
|
318
|
+
export { isNewerVersion, isTrustedReadRequest, isTrustedUpdateRequest, registerPluginUpdater };
|
|
292
319
|
|
|
293
320
|
//# sourceMappingURL=plugin-updater.js.map
|
package/lib/request-log.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
-
import { appendFile, mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { appendFile, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
4
|
import { dirname, join } from "node:path";
|
|
5
5
|
// src/request-log.ts
|
|
@@ -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, {
|
|
38
|
+
await mkdir(directory, {
|
|
39
|
+
recursive: true,
|
|
40
|
+
mode: 448
|
|
41
|
+
});
|
|
39
42
|
readyDirectory = directory;
|
|
40
43
|
}
|
|
41
|
-
await appendFile(path, line,
|
|
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) {
|
|
@@ -57,7 +63,19 @@ async function compactRequestLog(maxBytes = MAX_LOG_BYTES, keepLines = KEEP_LINE
|
|
|
57
63
|
if ((await stat(path)).size <= maxBytes) return;
|
|
58
64
|
const lines = (await readFile(path, "utf8")).split("\n").filter((line) => line !== "");
|
|
59
65
|
if (lines.length <= keepLines) return;
|
|
60
|
-
|
|
66
|
+
const temporary = `${path}.tmp`;
|
|
67
|
+
try {
|
|
68
|
+
await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`, {
|
|
69
|
+
encoding: "utf8",
|
|
70
|
+
mode: 384
|
|
71
|
+
});
|
|
72
|
+
await rename(temporary, path);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
await rm(temporary, { force: true }).catch((caught) => {
|
|
75
|
+
bestEffortFailed("request log compaction cleanup failed", caught);
|
|
76
|
+
});
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
61
79
|
} catch (error) {
|
|
62
80
|
bestEffortFailed("request log compaction failed", error);
|
|
63
81
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/route-guard.ts
|
|
2
|
+
/**
|
|
3
|
+
* Serialises one expensive route: at most one request in flight, and no more
|
|
4
|
+
* than one start per `minIntervalMs`.
|
|
5
|
+
*/
|
|
6
|
+
function createRouteGuard(minIntervalMs, now = Date.now) {
|
|
7
|
+
let last = Number.NEGATIVE_INFINITY;
|
|
8
|
+
let inflight = false;
|
|
9
|
+
return {
|
|
10
|
+
begin() {
|
|
11
|
+
if (inflight) return "busy";
|
|
12
|
+
const at = now();
|
|
13
|
+
if (at - last < minIntervalMs) return "rate";
|
|
14
|
+
last = at;
|
|
15
|
+
inflight = true;
|
|
16
|
+
return "ok";
|
|
17
|
+
},
|
|
18
|
+
end() {
|
|
19
|
+
inflight = false;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export { createRouteGuard };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=route-guard.js.map
|
package/lib/routes-values.js
CHANGED
|
@@ -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))
|
|
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))
|
|
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))
|
|
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 = {};
|