@goodandready/dsh-ollama-cloud-provider 0.1.37 → 0.1.39
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 +21 -0
- package/README.md +13 -6
- package/README.ru.md +13 -6
- package/README.zh.md +13 -6
- package/lib/account-rules.js +26 -0
- package/lib/adapter.d.ts +92 -0
- package/lib/adapter.js +566 -0
- package/lib/analytics.d.ts +81 -0
- package/lib/analytics.js +314 -0
- package/lib/attempt.js +29 -0
- package/lib/best-effort.js +20 -0
- package/lib/catalog-cache.js +81 -0
- package/lib/catalog.d.ts +10 -0
- package/lib/catalog.js +309 -0
- package/lib/commands.d.ts +14 -0
- package/lib/commands.js +228 -0
- package/lib/config.d.ts +173 -0
- package/lib/config.js +280 -0
- package/lib/context-override.js +38 -0
- package/lib/credentials.d.ts +23 -0
- package/lib/credentials.js +105 -0
- package/lib/discovery.js +108 -0
- package/lib/endpoints.js +58 -0
- package/lib/errors.d.ts +9 -0
- package/lib/errors.js +72 -0
- package/lib/files.js +13 -0
- package/lib/images.js +78 -0
- package/lib/index.d.ts +10 -521
- package/lib/index.js +20 -5921
- package/lib/local-host.js +47 -0
- package/lib/messages.js +263 -0
- package/lib/models.generated.d.ts +26 -0
- package/lib/models.generated.js +499 -0
- package/lib/plugin-updater.js +320 -0
- package/lib/pricing.js +78 -0
- package/lib/provider-ids.d.ts +4 -0
- package/lib/provider-ids.js +9 -0
- package/lib/quota-model.js +66 -0
- package/lib/reasoning.js +71 -0
- package/lib/request-log.d.ts +38 -0
- package/lib/request-log.js +203 -0
- package/lib/retry.js +24 -0
- package/lib/route-guard.js +25 -0
- package/lib/routes-public.js +59 -0
- package/lib/routes-values.js +129 -0
- package/lib/routes.d.ts +28 -0
- package/lib/routes.js +733 -0
- package/lib/routing.js +20 -0
- package/lib/sanitize.js +43 -0
- package/lib/status.js +60 -0
- package/lib/tool-schema.js +109 -0
- package/lib/tools.js +38 -0
- package/lib/transports/native.js +156 -0
- package/lib/transports/ndjson.js +73 -0
- package/lib/transports/openai-chat.js +278 -0
- package/lib/transports/sse.js +57 -0
- package/lib/transports/tool-call-id.js +17 -0
- package/lib/tui-settings.js +246 -0
- package/lib/usage.d.ts +69 -0
- package/lib/usage.js +277 -0
- package/lib/web.js +143 -0
- package/package.json +6 -5
package/lib/routes.js
ADDED
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { readModelAnalytics } from "./analytics.js";
|
|
3
|
+
import { clearCatalogCache, effectiveModel, modelAvailable, modelVisible, refreshCatalog } from "./catalog.js";
|
|
4
|
+
import { NS } from "./config.js";
|
|
5
|
+
import { discoverModels } from "./discovery.js";
|
|
6
|
+
import { checkEndpoint } from "./endpoints.js";
|
|
7
|
+
import { priceFor } from "./pricing.js";
|
|
8
|
+
import { authorityHostname, isLocalHostname } from "./local-host.js";
|
|
9
|
+
import { PROVIDER } from "./provider-ids.js";
|
|
10
|
+
import { createRouteGuard } from "./route-guard.js";
|
|
11
|
+
import { publicConfig } from "./routes-public.js";
|
|
12
|
+
import { clearStatusCache, probeServiceCached } from "./status.js";
|
|
13
|
+
import { cleanAccounts, cleanBooleanRecord, cleanManualModels, cleanModelAccountRules, cleanModelOverrides, cleanNumberRecord, cleanPrices, cleanStringArray, cleanTransports, isCredentialRef, normalizeCredentialRef } from "./routes-values.js";
|
|
14
|
+
import { safeErrorMessage } from "./sanitize.js";
|
|
15
|
+
import { ReasoningEffortId } from "@deepseek-ai/dsh-llm";
|
|
16
|
+
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
17
|
+
// src/routes.ts
|
|
18
|
+
const MAX_BODY_BYTES = 262144;
|
|
19
|
+
function json(res, status, value) {
|
|
20
|
+
const safe = typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.error === "string" ? {
|
|
21
|
+
...value,
|
|
22
|
+
error: safeErrorMessage(value.error)
|
|
23
|
+
} : value;
|
|
24
|
+
res.statusCode = status;
|
|
25
|
+
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
26
|
+
res.setHeader("cache-control", "no-store");
|
|
27
|
+
res.end(JSON.stringify(safe));
|
|
28
|
+
}
|
|
29
|
+
function sourceHost(value) {
|
|
30
|
+
if (value === void 0 || value === "") return void 0;
|
|
31
|
+
try {
|
|
32
|
+
return new URL(value).host;
|
|
33
|
+
} catch {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The Desktop renderer runs on `dsh-app://app`. Its host fetch has no Origin
|
|
39
|
+
* or Referer, but it carries the host auth cookie and the desktop user agent.
|
|
40
|
+
* The names alone are not proof: verify the cookie against the host's own
|
|
41
|
+
* authenticated API, which rejects forged values with 401/403.
|
|
42
|
+
*/
|
|
43
|
+
function desktopShellRequest(req) {
|
|
44
|
+
const agent = req.headers["user-agent"] ?? "";
|
|
45
|
+
const cookie = req.headers.cookie ?? "";
|
|
46
|
+
return agent.includes("@deepseek-ai/dsh-desktop/") && cookie.includes("dsh-auth-");
|
|
47
|
+
}
|
|
48
|
+
function localVerificationOrigin(req) {
|
|
49
|
+
const port = req.socket.localPort;
|
|
50
|
+
if (port === void 0 || port <= 0) return void 0;
|
|
51
|
+
return `${req.socket.encrypted === true ? "https" : "http"}://127.0.0.1:${port}`;
|
|
52
|
+
}
|
|
53
|
+
async function defaultVerifyHost(req) {
|
|
54
|
+
const localOrigin = localVerificationOrigin(req);
|
|
55
|
+
const cookie = req.headers.cookie;
|
|
56
|
+
if (localOrigin === void 0 || cookie === void 0 || cookie === "") return false;
|
|
57
|
+
try {
|
|
58
|
+
const response = await fetch(`${localOrigin}/api/version`, {
|
|
59
|
+
headers: {
|
|
60
|
+
cookie,
|
|
61
|
+
accept: "application/json"
|
|
62
|
+
},
|
|
63
|
+
redirect: "manual",
|
|
64
|
+
signal: AbortSignal.timeout(2500)
|
|
65
|
+
});
|
|
66
|
+
await response.body?.cancel();
|
|
67
|
+
if (response.status >= 300 && response.status < 400) return false;
|
|
68
|
+
if (response.status === 401 || response.status === 403 || response.status >= 500) return false;
|
|
69
|
+
const anonymous = await fetch(`${localOrigin}/api/version`, {
|
|
70
|
+
headers: { accept: "application/json" },
|
|
71
|
+
redirect: "manual",
|
|
72
|
+
signal: AbortSignal.timeout(2500)
|
|
73
|
+
});
|
|
74
|
+
await anonymous.body?.cancel();
|
|
75
|
+
if (anonymous.status !== 401 && anonymous.status !== 403) return false;
|
|
76
|
+
return response.status >= 200 && response.status < 300 || response.status === 404;
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Write routes must prove a same-origin browser context. A LAN bridge that
|
|
83
|
+
* rewrites Host and socket to loopback would otherwise let any non-browser
|
|
84
|
+
* client write keys and settings without an Origin. Fail closed when neither
|
|
85
|
+
* Origin nor Referer is present.
|
|
86
|
+
*/
|
|
87
|
+
function writeSourceError(req) {
|
|
88
|
+
const host = req.headers.host;
|
|
89
|
+
if (host === void 0 || host === "") return "request host required";
|
|
90
|
+
const origin = req.headers.origin;
|
|
91
|
+
if (origin !== void 0 && origin !== "") return sourceHost(origin) === host ? void 0 : "cross-origin request refused";
|
|
92
|
+
const referer = req.headers.referer;
|
|
93
|
+
if (referer === void 0 || referer === "") return "request origin required";
|
|
94
|
+
return sourceHost(referer) === host ? void 0 : "cross-origin request refused";
|
|
95
|
+
}
|
|
96
|
+
function allowedOrigin(req) {
|
|
97
|
+
const source = req.headers.origin ?? req.headers.referer;
|
|
98
|
+
if (source === void 0 || source === "") return true;
|
|
99
|
+
try {
|
|
100
|
+
const parsed = new URL(source);
|
|
101
|
+
const host = req.headers.host;
|
|
102
|
+
if (host !== void 0 && parsed.host === host) return true;
|
|
103
|
+
return parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost" || parsed.hostname === "::1" || parsed.hostname === "[::1]";
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* DNS rebinding keeps the browser's `Host` pointing at the attacker's name
|
|
110
|
+
* while the socket reaches loopback. Write endpoints accept only a literal
|
|
111
|
+
* loopback Host.
|
|
112
|
+
*/
|
|
113
|
+
function trustedHost(req) {
|
|
114
|
+
const hostname = authorityHostname(req.headers.host);
|
|
115
|
+
return hostname !== void 0 && isLocalHostname(hostname);
|
|
116
|
+
}
|
|
117
|
+
function loopbackHost(req) {
|
|
118
|
+
const host = req.headers.host;
|
|
119
|
+
if (host === void 0 || host === "") return false;
|
|
120
|
+
try {
|
|
121
|
+
const hostname = new URL(`http://${host}`).hostname;
|
|
122
|
+
return hostname === "127.0.0.1" || hostname === "localhost" || hostname === "::1" || hostname === "[::1]";
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
async function readJson(req) {
|
|
128
|
+
const chunks = [];
|
|
129
|
+
let size = 0;
|
|
130
|
+
for await (const chunk of req) {
|
|
131
|
+
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
132
|
+
size += buffer.length;
|
|
133
|
+
if (size > MAX_BODY_BYTES) throw new Error("payload too large");
|
|
134
|
+
chunks.push(buffer);
|
|
135
|
+
}
|
|
136
|
+
if (size === 0) return {};
|
|
137
|
+
const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
138
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) throw new Error("body must be a JSON object");
|
|
139
|
+
return parsed;
|
|
140
|
+
}
|
|
141
|
+
async function requirePost(req, res, verifyHost) {
|
|
142
|
+
if (req.method !== "POST") {
|
|
143
|
+
json(res, 405, { error: "POST required" });
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
if (!loopbackHost(req)) {
|
|
147
|
+
json(res, 403, { error: "non-loopback Host refused" });
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
const remote = req.socket.remoteAddress ?? "";
|
|
151
|
+
if (!(remote === "127.0.0.1" || remote === "::1" || remote === "::ffff:127.0.0.1")) {
|
|
152
|
+
json(res, 403, { error: "write endpoints are restricted to the local machine" });
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (!desktopShellRequest(req)) {
|
|
156
|
+
const sourceError = writeSourceError(req);
|
|
157
|
+
if (sourceError !== void 0) {
|
|
158
|
+
json(res, 403, { error: sourceError });
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (!await verifyHost(req)) {
|
|
163
|
+
json(res, 403, { error: "authentication required" });
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
if (!(req.headers["content-type"] ?? "").includes("application/json")) {
|
|
167
|
+
json(res, 415, { error: "application/json required" });
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
function getSettings(ctx) {
|
|
173
|
+
return ctx.get("settings");
|
|
174
|
+
}
|
|
175
|
+
function getCredentials(ctx) {
|
|
176
|
+
return ctx.get("credentials");
|
|
177
|
+
}
|
|
178
|
+
function settingsRevision(ctx) {
|
|
179
|
+
try {
|
|
180
|
+
return getSettings(ctx)?.describe?.({ redactSecrets: true }).find((row) => row.ns === NS)?.revision;
|
|
181
|
+
} catch {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Writes are limited to this plugin's credential namespace. Without this
|
|
187
|
+
* allowlist any same-origin caller could overwrite or unset another plugin's
|
|
188
|
+
* key in the shared credential store.
|
|
189
|
+
*/
|
|
190
|
+
function canWriteCredentialRef(config, ref) {
|
|
191
|
+
if (!isCredentialRef(ref)) return false;
|
|
192
|
+
if (ref === config.apiKeyEnv) return true;
|
|
193
|
+
for (const account of config.accounts) if (typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() === ref) return true;
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
function configOps(patch) {
|
|
197
|
+
const ops = [];
|
|
198
|
+
const set = (field, value) => {
|
|
199
|
+
if (value === null || value === void 0) ops.push({
|
|
200
|
+
op: "unset",
|
|
201
|
+
path: [field]
|
|
202
|
+
});
|
|
203
|
+
else ops.push({
|
|
204
|
+
op: "set",
|
|
205
|
+
path: [field],
|
|
206
|
+
value
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
if ("apiKeyEnv" in patch) {
|
|
210
|
+
if (patch.apiKeyEnv === null || patch.apiKeyEnv === void 0) set("apiKeyEnv", void 0);
|
|
211
|
+
else if (typeof patch.apiKeyEnv === "string" && patch.apiKeyEnv.trim() === "") set("apiKeyEnv", "");
|
|
212
|
+
else {
|
|
213
|
+
const ref = normalizeCredentialRef(patch.apiKeyEnv);
|
|
214
|
+
if (ref === void 0) throw new Error("invalid credential reference");
|
|
215
|
+
set("apiKeyEnv", ref);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const field of [
|
|
219
|
+
"apiBase",
|
|
220
|
+
"openAiBase",
|
|
221
|
+
"usageUrl",
|
|
222
|
+
"balanceUrl"
|
|
223
|
+
]) {
|
|
224
|
+
if (!(field in patch)) continue;
|
|
225
|
+
const raw = patch[field];
|
|
226
|
+
if (raw === null || raw === void 0 || typeof raw === "string" && raw.trim() === "") {
|
|
227
|
+
set(field, void 0);
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
const checked = checkEndpoint(field, raw);
|
|
231
|
+
if (!checked.ok) throw new Error(checked.error);
|
|
232
|
+
set(field, checked.value);
|
|
233
|
+
}
|
|
234
|
+
if ("transport" in patch) set("transport", patch.transport);
|
|
235
|
+
if ("modelTransports" in patch) set("modelTransports", cleanTransports(patch.modelTransports));
|
|
236
|
+
if ("requestTimeoutMs" in patch) set("requestTimeoutMs", patch.requestTimeoutMs);
|
|
237
|
+
if ("streamIdleTimeoutMs" in patch) set("streamIdleTimeoutMs", patch.streamIdleTimeoutMs);
|
|
238
|
+
if ("attemptTimeoutMs" in patch) set("attemptTimeoutMs", patch.attemptTimeoutMs);
|
|
239
|
+
if ("retryMaxRetries" in patch) set("retryMaxRetries", patch.retryMaxRetries);
|
|
240
|
+
if ("retryBaseDelayMs" in patch) set("retryBaseDelayMs", patch.retryBaseDelayMs);
|
|
241
|
+
if ("retryMaxDelayMs" in patch) set("retryMaxDelayMs", patch.retryMaxDelayMs);
|
|
242
|
+
if ("retryJitter" in patch) set("retryJitter", patch.retryJitter);
|
|
243
|
+
if ("visibleModels" in patch) set("visibleModels", cleanStringArray(patch.visibleModels));
|
|
244
|
+
if ("showAllModels" in patch) set("showAllModels", patch.showAllModels === true);
|
|
245
|
+
if ("modelVisibility" in patch) set("modelVisibility", cleanBooleanRecord(patch.modelVisibility));
|
|
246
|
+
if ("manualModels" in patch) set("manualModels", cleanManualModels(patch.manualModels));
|
|
247
|
+
if ("modelOrder" in patch) set("modelOrder", cleanStringArray(patch.modelOrder));
|
|
248
|
+
if ("modelOverrides" in patch) set("modelOverrides", cleanModelOverrides(patch.modelOverrides));
|
|
249
|
+
if ("filterModelsByPlan" in patch) set("filterModelsByPlan", patch.filterModelsByPlan === true);
|
|
250
|
+
if ("contextWindow" in patch) set("contextWindow", patch.contextWindow);
|
|
251
|
+
if ("contextWindowOverrides" in patch) set("contextWindowOverrides", cleanNumberRecord(patch.contextWindowOverrides));
|
|
252
|
+
if ("maxOutputTokens" in patch) set("maxOutputTokens", patch.maxOutputTokens);
|
|
253
|
+
if ("temperature" in patch) set("temperature", patch.temperature);
|
|
254
|
+
if ("topK" in patch) set("topK", patch.topK);
|
|
255
|
+
if ("topP" in patch) set("topP", patch.topP);
|
|
256
|
+
if ("minP" in patch) set("minP", patch.minP);
|
|
257
|
+
if ("seed" in patch) set("seed", patch.seed);
|
|
258
|
+
if ("repeatPenalty" in patch) set("repeatPenalty", patch.repeatPenalty);
|
|
259
|
+
if ("repeatLastN" in patch) set("repeatLastN", patch.repeatLastN);
|
|
260
|
+
if ("numCtx" in patch) set("numCtx", patch.numCtx);
|
|
261
|
+
if ("keepAlive" in patch) set("keepAlive", patch.keepAlive);
|
|
262
|
+
if ("stop" in patch) set("stop", cleanStringArray(patch.stop));
|
|
263
|
+
if ("autoRefresh" in patch) set("autoRefresh", patch.autoRefresh === true);
|
|
264
|
+
if ("refreshMinutes" in patch) set("refreshMinutes", patch.refreshMinutes);
|
|
265
|
+
if ("accounts" in patch) {
|
|
266
|
+
const accounts = cleanAccounts(patch.accounts);
|
|
267
|
+
for (const account of accounts) if (account.apiKeyEnv !== "" && !isCredentialRef(account.apiKeyEnv)) throw new Error("invalid credential reference");
|
|
268
|
+
set("accounts", accounts);
|
|
269
|
+
}
|
|
270
|
+
if ("modelAccountRules" in patch) set("modelAccountRules", cleanModelAccountRules(patch.modelAccountRules));
|
|
271
|
+
if ("stickySession" in patch) set("stickySession", patch.stickySession === true);
|
|
272
|
+
if ("activeAccount" in patch) set("activeAccount", patch.activeAccount);
|
|
273
|
+
if ("showSidebarQuota" in patch) set("showSidebarQuota", patch.showSidebarQuota === true);
|
|
274
|
+
if ("planLabel" in patch) set("planLabel", patch.planLabel);
|
|
275
|
+
if ("priceOverrides" in patch) set("priceOverrides", cleanPrices(patch.priceOverrides));
|
|
276
|
+
if ("userAgent" in patch) set("userAgent", patch.userAgent);
|
|
277
|
+
return ops;
|
|
278
|
+
}
|
|
279
|
+
async function buildState(ctx, deps) {
|
|
280
|
+
const config = deps.config();
|
|
281
|
+
const credentials = getCredentials(ctx);
|
|
282
|
+
let keyInfo = {
|
|
283
|
+
configured: false,
|
|
284
|
+
writable: false
|
|
285
|
+
};
|
|
286
|
+
if (credentials !== void 0) try {
|
|
287
|
+
const info = await credentials.describe(credentialRef(config.apiKeyEnv));
|
|
288
|
+
keyInfo = {
|
|
289
|
+
configured: info.configured,
|
|
290
|
+
writable: info.writable,
|
|
291
|
+
...info.source === void 0 ? {} : { source: info.source }
|
|
292
|
+
};
|
|
293
|
+
} catch (error) {
|
|
294
|
+
bestEffortFailed("primary credential describe failed", error);
|
|
295
|
+
}
|
|
296
|
+
const resolved = await deps.pool.resolveAll(PROVIDER).catch((err) => {
|
|
297
|
+
bestEffortFailed("pool.resolveAll", err);
|
|
298
|
+
return [];
|
|
299
|
+
});
|
|
300
|
+
if (!keyInfo.configured) keyInfo = {
|
|
301
|
+
...keyInfo,
|
|
302
|
+
configured: resolved.length > 0
|
|
303
|
+
};
|
|
304
|
+
const usageByKey = /* @__PURE__ */ new Map();
|
|
305
|
+
await Promise.all([...new Set(resolved.map((entry) => entry.key))].map(async (key) => {
|
|
306
|
+
usageByKey.set(key, await deps.usage.read(key) ?? null);
|
|
307
|
+
}));
|
|
308
|
+
const usageByLabel = /* @__PURE__ */ new Map();
|
|
309
|
+
for (const entry of resolved) usageByLabel.set(entry.label ?? "default", usageByKey.get(entry.key) ?? null);
|
|
310
|
+
const slots = [{
|
|
311
|
+
label: "default",
|
|
312
|
+
ref: config.apiKeyEnv,
|
|
313
|
+
default: true
|
|
314
|
+
}, ...config.accounts.map((account, index) => ({
|
|
315
|
+
label: typeof account.label === "string" && account.label.trim() !== "" ? account.label.trim() : `account-${index + 2}`,
|
|
316
|
+
ref: typeof account.apiKeyEnv === "string" ? account.apiKeyEnv.trim() : "",
|
|
317
|
+
default: false
|
|
318
|
+
}))];
|
|
319
|
+
let activeLabel = "";
|
|
320
|
+
for (const entry of resolved) if (deps.adapter.accountHealth(entry.key) === void 0) {
|
|
321
|
+
activeLabel = entry.label ?? "default";
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
const keyByLabel = new Map(resolved.map((entry) => [entry.label ?? "default", entry]));
|
|
325
|
+
const accounts = [];
|
|
326
|
+
for (const slot of slots) {
|
|
327
|
+
let configured = false;
|
|
328
|
+
let source = "";
|
|
329
|
+
if (slot.ref !== "" && credentials !== void 0) try {
|
|
330
|
+
const info = await credentials.describe(credentialRef(slot.ref));
|
|
331
|
+
configured = info.configured;
|
|
332
|
+
source = info.source ?? "";
|
|
333
|
+
} catch (error) {
|
|
334
|
+
bestEffortFailed("account credential describe failed", error);
|
|
335
|
+
}
|
|
336
|
+
const hit = keyByLabel.get(slot.label);
|
|
337
|
+
if (!configured && hit !== void 0) {
|
|
338
|
+
configured = true;
|
|
339
|
+
source = hit.source;
|
|
340
|
+
}
|
|
341
|
+
const health = hit === void 0 ? void 0 : deps.adapter.accountHealth(hit.key);
|
|
342
|
+
accounts.push({
|
|
343
|
+
...slot,
|
|
344
|
+
configured,
|
|
345
|
+
source,
|
|
346
|
+
active: activeLabel === slot.label,
|
|
347
|
+
coolingUntil: health?.coolingUntil ?? 0,
|
|
348
|
+
mark: health?.reason ?? "",
|
|
349
|
+
usage: hit === void 0 ? null : usageByLabel.get(slot.label) ?? null
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
let models = [];
|
|
353
|
+
try {
|
|
354
|
+
models = (await deps.adapter.listModels(PROVIDER, { unfiltered: true })).map((model) => {
|
|
355
|
+
const meta = effectiveModel(model.id, config);
|
|
356
|
+
return {
|
|
357
|
+
id: model.id,
|
|
358
|
+
name: model.name,
|
|
359
|
+
description: model.description ?? "",
|
|
360
|
+
transport: meta.transport,
|
|
361
|
+
contextWindow: meta.contextWindow,
|
|
362
|
+
maxTokens: meta.maxTokens,
|
|
363
|
+
free: meta.free,
|
|
364
|
+
cost: priceFor(model.id, config.priceOverrides),
|
|
365
|
+
available: modelAvailable(model.id, config),
|
|
366
|
+
input: meta.input,
|
|
367
|
+
reasoning: meta.reasoning,
|
|
368
|
+
defaultReasoning: meta.defaultReasoning ?? null,
|
|
369
|
+
tools: meta.tools,
|
|
370
|
+
visible: modelVisible(model.id, config)
|
|
371
|
+
};
|
|
372
|
+
});
|
|
373
|
+
} catch {
|
|
374
|
+
models = [];
|
|
375
|
+
}
|
|
376
|
+
const displayEntry = resolved.find((entry) => (entry.label ?? "default") === activeLabel) ?? resolved[0];
|
|
377
|
+
const usage = displayEntry === void 0 ? null : usageByLabel.get(displayEntry.label ?? "default") ?? null;
|
|
378
|
+
return {
|
|
379
|
+
config: publicConfig(config),
|
|
380
|
+
key: keyInfo,
|
|
381
|
+
accounts,
|
|
382
|
+
models,
|
|
383
|
+
usage: usage ?? null,
|
|
384
|
+
routing: deps.adapter.routingTrace(),
|
|
385
|
+
revision: settingsRevision(ctx)
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
async function refreshAndState(ctx, deps, res) {
|
|
389
|
+
try {
|
|
390
|
+
json(res, 200, await buildState(ctx, deps));
|
|
391
|
+
} catch (error) {
|
|
392
|
+
json(res, 500, { error: error instanceof Error ? error.message : "state failed" });
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/** Register the Host HTTP surface consumed by the browser settings page. */
|
|
396
|
+
function applyRoutes(ctx, web, deps) {
|
|
397
|
+
const disposers = [];
|
|
398
|
+
const verifyHost = deps.verifyHost ?? deps.verifyDesktop ?? defaultVerifyHost;
|
|
399
|
+
const route = (path, handler) => {
|
|
400
|
+
disposers.push(web.register({
|
|
401
|
+
kind: "exact",
|
|
402
|
+
path,
|
|
403
|
+
handler
|
|
404
|
+
}));
|
|
405
|
+
};
|
|
406
|
+
const refreshGuard = createRouteGuard(2e3);
|
|
407
|
+
const modelsGuard = createRouteGuard(2e3);
|
|
408
|
+
const discoverGuard = createRouteGuard(1e3);
|
|
409
|
+
const testGuard = createRouteGuard(2e3);
|
|
410
|
+
const readOnly = (req, res) => {
|
|
411
|
+
if (req.method !== "GET") {
|
|
412
|
+
json(res, 405, { error: "GET required" });
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
if (!trustedHost(req)) {
|
|
416
|
+
json(res, 403, { error: "non-local Host refused" });
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
if (!allowedOrigin(req)) {
|
|
420
|
+
json(res, 403, { error: "cross-origin request refused" });
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
return true;
|
|
424
|
+
};
|
|
425
|
+
const privateRead = async (req, res) => {
|
|
426
|
+
if (!readOnly(req, res)) return false;
|
|
427
|
+
if (!await verifyHost(req)) {
|
|
428
|
+
json(res, 403, { error: "authentication required" });
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
return true;
|
|
432
|
+
};
|
|
433
|
+
route("/dsh-ollama/state", async (req, res) => {
|
|
434
|
+
if (!await privateRead(req, res)) return;
|
|
435
|
+
await refreshAndState(ctx, deps, res);
|
|
436
|
+
});
|
|
437
|
+
route("/dsh-ollama/health", async (req, res) => {
|
|
438
|
+
if (!readOnly(req, res)) return;
|
|
439
|
+
try {
|
|
440
|
+
const config = deps.config();
|
|
441
|
+
let providerError;
|
|
442
|
+
const models = await deps.adapter.listModels(PROVIDER, { unfiltered: true }).catch((err) => {
|
|
443
|
+
providerError = err instanceof Error ? err.message : String(err);
|
|
444
|
+
bestEffortFailed("health:listModels", err);
|
|
445
|
+
return [];
|
|
446
|
+
});
|
|
447
|
+
json(res, 200, {
|
|
448
|
+
ok: !providerError,
|
|
449
|
+
providers: [PROVIDER],
|
|
450
|
+
accounts: config.accounts.length + 1,
|
|
451
|
+
models: models.length,
|
|
452
|
+
...providerError ? { providerError } : {}
|
|
453
|
+
});
|
|
454
|
+
} catch (error) {
|
|
455
|
+
json(res, 500, { error: error instanceof Error ? error.message : "health failed" });
|
|
456
|
+
}
|
|
457
|
+
});
|
|
458
|
+
route("/dsh-ollama/status", async (req, res) => {
|
|
459
|
+
if (!readOnly(req, res)) return;
|
|
460
|
+
try {
|
|
461
|
+
json(res, 200, await probeServiceCached(deps.config(), fetch));
|
|
462
|
+
} catch (error) {
|
|
463
|
+
json(res, 200, {
|
|
464
|
+
ok: false,
|
|
465
|
+
status: null,
|
|
466
|
+
latencyMs: 0,
|
|
467
|
+
checkedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
468
|
+
error: error instanceof Error ? error.message : "status failed"
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
route("/dsh-ollama/analytics", async (req, res) => {
|
|
473
|
+
if (!await privateRead(req, res)) return;
|
|
474
|
+
const url = new URL(req.url ?? "/", "http://localhost");
|
|
475
|
+
const requested = Number(url.searchParams.get("days") ?? "30");
|
|
476
|
+
const days = Number.isFinite(requested) ? Math.max(1, Math.min(365, Math.trunc(requested))) : 30;
|
|
477
|
+
try {
|
|
478
|
+
const report = deps.analytics === void 0 ? await readModelAnalytics(days, deps.config().priceOverrides) : await deps.analytics(days);
|
|
479
|
+
const heatmap = deps.heatmap === void 0 ? void 0 : await deps.heatmap(days);
|
|
480
|
+
json(res, 200, heatmap === void 0 ? report : {
|
|
481
|
+
...report,
|
|
482
|
+
heatmap
|
|
483
|
+
});
|
|
484
|
+
} catch (error) {
|
|
485
|
+
json(res, 500, { error: error instanceof Error ? error.message : "analytics failed" });
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
route("/dsh-ollama/usage", async (req, res) => {
|
|
489
|
+
if (!await privateRead(req, res)) return;
|
|
490
|
+
const raw = new URL(req.url ?? "/", "http://localhost").searchParams.get("range") ?? "7d";
|
|
491
|
+
const range = raw === "24h" || raw === "30d" ? raw : "7d";
|
|
492
|
+
try {
|
|
493
|
+
json(res, 200, await deps.usage.usage(range) ?? {
|
|
494
|
+
error: "usage unavailable",
|
|
495
|
+
checkedAt: Date.now()
|
|
496
|
+
});
|
|
497
|
+
} catch (error) {
|
|
498
|
+
json(res, 500, { error: error instanceof Error ? error.message : "usage failed" });
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
route("/dsh-ollama/balance", async (req, res) => {
|
|
502
|
+
if (!await privateRead(req, res)) return;
|
|
503
|
+
try {
|
|
504
|
+
json(res, 200, await deps.usage.read() ?? {
|
|
505
|
+
error: "balance unavailable",
|
|
506
|
+
checkedAt: Date.now()
|
|
507
|
+
});
|
|
508
|
+
} catch (error) {
|
|
509
|
+
json(res, 500, { error: error instanceof Error ? error.message : "balance failed" });
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
route("/dsh-ollama/cooldowns/reset", async (req, res) => {
|
|
513
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
514
|
+
deps.adapter.clearCooldowns?.();
|
|
515
|
+
json(res, 200, { ok: true });
|
|
516
|
+
});
|
|
517
|
+
route("/dsh-ollama/config", async (req, res) => {
|
|
518
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
519
|
+
const settings = getSettings(ctx);
|
|
520
|
+
if (settings?.mutate === void 0) {
|
|
521
|
+
json(res, 503, { error: "settings service unavailable" });
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (settings.writable === false) {
|
|
525
|
+
json(res, 403, { error: "settings are read-only" });
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
try {
|
|
529
|
+
const body = await readJson(req);
|
|
530
|
+
const patch = typeof body.patch === "object" && body.patch !== null && !Array.isArray(body.patch) ? body.patch : body;
|
|
531
|
+
let ops;
|
|
532
|
+
try {
|
|
533
|
+
ops = configOps(patch);
|
|
534
|
+
} catch (error) {
|
|
535
|
+
json(res, 400, { error: error instanceof Error ? error.message : "invalid patch" });
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (ops.length === 0) {
|
|
539
|
+
json(res, 400, { error: "no known fields in patch" });
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
await settings.mutate(NS, ops, settingsRevision(ctx));
|
|
543
|
+
deps.usage.clear();
|
|
544
|
+
deps.refreshTui?.();
|
|
545
|
+
json(res, 200, { ok: true });
|
|
546
|
+
} catch (error) {
|
|
547
|
+
json(res, 500, { error: error instanceof Error ? error.message : "config write failed" });
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
route("/dsh-ollama/key", async (req, res) => {
|
|
551
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
552
|
+
const credentials = getCredentials(ctx);
|
|
553
|
+
if (credentials === void 0) {
|
|
554
|
+
json(res, 503, { error: "credentials service unavailable" });
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
try {
|
|
558
|
+
const body = await readJson(req);
|
|
559
|
+
const refName = typeof body.ref === "string" && body.ref.trim() !== "" ? body.ref.trim() : deps.config().apiKeyEnv;
|
|
560
|
+
if (!canWriteCredentialRef(deps.config(), refName)) {
|
|
561
|
+
json(res, 400, { error: "unknown credential reference" });
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const key = typeof body.key === "string" ? body.key.trim() : "";
|
|
565
|
+
if (key === "") {
|
|
566
|
+
json(res, 400, { error: "key is required" });
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
await credentials.set(credentialRef(refName), key);
|
|
570
|
+
json(res, 200, {
|
|
571
|
+
ok: true,
|
|
572
|
+
ref: refName
|
|
573
|
+
});
|
|
574
|
+
} catch (error) {
|
|
575
|
+
json(res, 500, { error: error instanceof Error ? error.message : "key write failed" });
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
route("/dsh-ollama/key/clear", async (req, res) => {
|
|
579
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
580
|
+
const credentials = getCredentials(ctx);
|
|
581
|
+
if (credentials === void 0) {
|
|
582
|
+
json(res, 503, { error: "credentials service unavailable" });
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
try {
|
|
586
|
+
const body = await readJson(req);
|
|
587
|
+
const refName = typeof body.ref === "string" && body.ref.trim() !== "" ? body.ref.trim() : deps.config().apiKeyEnv;
|
|
588
|
+
if (!canWriteCredentialRef(deps.config(), refName)) {
|
|
589
|
+
json(res, 400, { error: "unknown credential reference" });
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
await credentials.unset(credentialRef(refName));
|
|
593
|
+
json(res, 200, {
|
|
594
|
+
ok: true,
|
|
595
|
+
ref: refName
|
|
596
|
+
});
|
|
597
|
+
} catch (error) {
|
|
598
|
+
json(res, 500, { error: error instanceof Error ? error.message : "key clear failed" });
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
route("/dsh-ollama/refresh", async (req, res) => {
|
|
602
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
603
|
+
const gate = refreshGuard.begin();
|
|
604
|
+
if (gate !== "ok") {
|
|
605
|
+
json(res, 429, { error: gate === "busy" ? "request already in progress" : "rate limited; retry shortly" });
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
try {
|
|
609
|
+
clearStatusCache();
|
|
610
|
+
deps.usage.clear();
|
|
611
|
+
await refreshAndState(ctx, deps, res);
|
|
612
|
+
} finally {
|
|
613
|
+
refreshGuard.end();
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
route("/dsh-ollama/models/refresh", async (req, res) => {
|
|
617
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
618
|
+
const gate = modelsGuard.begin();
|
|
619
|
+
if (gate !== "ok") {
|
|
620
|
+
json(res, 429, { error: gate === "busy" ? "request already in progress" : "rate limited; retry shortly" });
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
try {
|
|
624
|
+
clearCatalogCache();
|
|
625
|
+
clearStatusCache();
|
|
626
|
+
deps.usage.clear();
|
|
627
|
+
try {
|
|
628
|
+
await refreshCatalog(deps.config(), fetch, true);
|
|
629
|
+
} catch (error) {
|
|
630
|
+
bestEffortFailed("catalog refresh failed", error);
|
|
631
|
+
}
|
|
632
|
+
await refreshAndState(ctx, deps, res);
|
|
633
|
+
} finally {
|
|
634
|
+
modelsGuard.end();
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
route("/dsh-ollama/discover", async (req, res) => {
|
|
638
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
639
|
+
const gate = discoverGuard.begin();
|
|
640
|
+
if (gate !== "ok") {
|
|
641
|
+
json(res, 429, { error: gate === "busy" ? "request already in progress" : "rate limited; retry shortly" });
|
|
642
|
+
return;
|
|
643
|
+
}
|
|
644
|
+
try {
|
|
645
|
+
let body;
|
|
646
|
+
try {
|
|
647
|
+
body = await readJson(req);
|
|
648
|
+
} catch (error) {
|
|
649
|
+
json(res, 400, { error: error instanceof Error ? error.message : "invalid body" });
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const apiBase = typeof body.apiBase === "string" ? body.apiBase : "";
|
|
653
|
+
const apiKey = typeof body.apiKey === "string" ? body.apiKey : "";
|
|
654
|
+
try {
|
|
655
|
+
json(res, 200, await discoverModels(apiBase, apiKey, deps.discoveryFetch));
|
|
656
|
+
} catch (error) {
|
|
657
|
+
json(res, 400, { error: error instanceof Error ? error.message : "discovery failed" });
|
|
658
|
+
}
|
|
659
|
+
} finally {
|
|
660
|
+
discoverGuard.end();
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
route("/dsh-ollama/test", async (req, res) => {
|
|
664
|
+
if (!await requirePost(req, res, verifyHost)) return;
|
|
665
|
+
const gate = testGuard.begin();
|
|
666
|
+
if (gate !== "ok") {
|
|
667
|
+
json(res, 429, { error: gate === "busy" ? "request already in progress" : "rate limited; retry shortly" });
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
try {
|
|
671
|
+
let body;
|
|
672
|
+
try {
|
|
673
|
+
body = await readJson(req);
|
|
674
|
+
} catch (error) {
|
|
675
|
+
json(res, 400, { error: error instanceof Error ? error.message : "invalid body" });
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
const model = typeof body.model === "string" && body.model !== "" ? body.model : "gpt-oss:120b";
|
|
679
|
+
const started = Date.now();
|
|
680
|
+
const controller = new AbortController();
|
|
681
|
+
const timer = setTimeout(() => controller.abort(), 6e4);
|
|
682
|
+
let text = "";
|
|
683
|
+
let finish;
|
|
684
|
+
try {
|
|
685
|
+
const options = {
|
|
686
|
+
provider: PROVIDER,
|
|
687
|
+
model,
|
|
688
|
+
messages: [{
|
|
689
|
+
role: "user",
|
|
690
|
+
content: [{
|
|
691
|
+
type: "text",
|
|
692
|
+
text: "Reply with exactly: OK"
|
|
693
|
+
}]
|
|
694
|
+
}],
|
|
695
|
+
reasoningEffort: ReasoningEffortId("off"),
|
|
696
|
+
maxTokens: 64,
|
|
697
|
+
signal: controller.signal
|
|
698
|
+
};
|
|
699
|
+
let reasoning = "";
|
|
700
|
+
for await (const chunk of deps.adapter.stream(options)) if (chunk.type === "text-delta") text += chunk.text;
|
|
701
|
+
else if (chunk.type === "reasoning-delta") reasoning += chunk.text;
|
|
702
|
+
else if (chunk.type === "finish") finish = chunk.reason;
|
|
703
|
+
const reply = text === "" ? reasoning : text;
|
|
704
|
+
json(res, 200, {
|
|
705
|
+
ok: true,
|
|
706
|
+
model,
|
|
707
|
+
provider: PROVIDER,
|
|
708
|
+
latencyMs: Date.now() - started,
|
|
709
|
+
text: reply.slice(0, 200),
|
|
710
|
+
finish
|
|
711
|
+
});
|
|
712
|
+
} catch (error) {
|
|
713
|
+
json(res, 200, {
|
|
714
|
+
ok: false,
|
|
715
|
+
model,
|
|
716
|
+
provider: PROVIDER,
|
|
717
|
+
latencyMs: Date.now() - started,
|
|
718
|
+
error: controller.signal.aborted ? "timeout" : error instanceof Error ? error.message : "test failed"
|
|
719
|
+
});
|
|
720
|
+
} finally {
|
|
721
|
+
clearTimeout(timer);
|
|
722
|
+
}
|
|
723
|
+
} finally {
|
|
724
|
+
testGuard.end();
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
ctx.effect(() => () => {
|
|
728
|
+
for (const dispose of disposers.splice(0)) dispose();
|
|
729
|
+
}, "dsh-ollama-cloud-provider: HTTP routes");
|
|
730
|
+
}
|
|
731
|
+
export { applyRoutes, defaultVerifyHost, localVerificationOrigin };
|
|
732
|
+
|
|
733
|
+
//# sourceMappingURL=routes.js.map
|