@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
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { appendFile, mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
|
+
// src/request-log.ts
|
|
6
|
+
const DAY_MS = 864e5;
|
|
7
|
+
const MAX_LOG_BYTES = 2097152;
|
|
8
|
+
const KEEP_LINES = 5e3;
|
|
9
|
+
const COMPACT_EVERY = 200;
|
|
10
|
+
/** Append-only request metadata. The path is local; no prompt or response is stored. */
|
|
11
|
+
function requestLogPath() {
|
|
12
|
+
const home = process.env.DSH_HOME?.trim() || join(homedir(), ".dsh");
|
|
13
|
+
return join(home, "storages", "dsh-ollama", "requests.jsonl");
|
|
14
|
+
}
|
|
15
|
+
let queue = Promise.resolve();
|
|
16
|
+
let readyDirectory = "";
|
|
17
|
+
let appends = 0;
|
|
18
|
+
function recordRequest(provider, model, at = Date.now(), purpose, account, reason, outcome) {
|
|
19
|
+
const line = `${JSON.stringify({
|
|
20
|
+
at,
|
|
21
|
+
provider,
|
|
22
|
+
model,
|
|
23
|
+
...purpose === void 0 ? {} : { purpose },
|
|
24
|
+
...account === void 0 ? {} : { account },
|
|
25
|
+
...reason === void 0 ? {} : { reason },
|
|
26
|
+
...outcome === void 0 ? {} : {
|
|
27
|
+
ok: outcome.ok,
|
|
28
|
+
latencyMs: outcome.latencyMs,
|
|
29
|
+
...outcome.ttftMs === void 0 ? {} : { ttftMs: outcome.ttftMs },
|
|
30
|
+
...outcome.error === void 0 ? {} : { error: outcome.error }
|
|
31
|
+
}
|
|
32
|
+
})}\n`;
|
|
33
|
+
queue = queue.then(async () => {
|
|
34
|
+
try {
|
|
35
|
+
const path = requestLogPath();
|
|
36
|
+
const directory = dirname(path);
|
|
37
|
+
if (readyDirectory !== directory) {
|
|
38
|
+
await mkdir(directory, { recursive: true });
|
|
39
|
+
readyDirectory = directory;
|
|
40
|
+
}
|
|
41
|
+
await appendFile(path, line, "utf8");
|
|
42
|
+
appends += 1;
|
|
43
|
+
if (appends % COMPACT_EVERY === 0) await compactRequestLog();
|
|
44
|
+
} catch (error) {
|
|
45
|
+
bestEffortFailed("request log append failed", error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/** Await pending appends (tests and shutdown). */
|
|
50
|
+
async function flushRequestLog() {
|
|
51
|
+
await queue;
|
|
52
|
+
}
|
|
53
|
+
/** Best-effort compaction: once the file is large, keep the newest lines. */
|
|
54
|
+
async function compactRequestLog(maxBytes = MAX_LOG_BYTES, keepLines = KEEP_LINES) {
|
|
55
|
+
const path = requestLogPath();
|
|
56
|
+
try {
|
|
57
|
+
if ((await stat(path)).size <= maxBytes) return;
|
|
58
|
+
const lines = (await readFile(path, "utf8")).split("\n").filter((line) => line !== "");
|
|
59
|
+
if (lines.length <= keepLines) return;
|
|
60
|
+
const temporary = `${path}.tmp`;
|
|
61
|
+
try {
|
|
62
|
+
await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`, "utf8");
|
|
63
|
+
await rename(temporary, path);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
await rm(temporary, { force: true }).catch((caught) => {
|
|
66
|
+
bestEffortFailed("request log compaction cleanup failed", caught);
|
|
67
|
+
});
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
bestEffortFailed("request log compaction failed", error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function readRequestHeatmap(days, now = Date.now()) {
|
|
75
|
+
const since = now - Math.max(1, Math.min(365, Math.trunc(days))) * DAY_MS;
|
|
76
|
+
const hours = Array.from({ length: 24 }, () => 0);
|
|
77
|
+
const grid = Array.from({ length: 7 }, () => Array.from({ length: 24 }, () => 0));
|
|
78
|
+
const models = /* @__PURE__ */ new Map();
|
|
79
|
+
const accounts = /* @__PURE__ */ new Map();
|
|
80
|
+
const accountMetrics = /* @__PURE__ */ new Map();
|
|
81
|
+
let total = 0;
|
|
82
|
+
let firstAt = null;
|
|
83
|
+
let lastAt = null;
|
|
84
|
+
let text;
|
|
85
|
+
try {
|
|
86
|
+
text = await readFile(requestLogPath(), "utf8");
|
|
87
|
+
} catch {
|
|
88
|
+
return {
|
|
89
|
+
since,
|
|
90
|
+
total,
|
|
91
|
+
hours,
|
|
92
|
+
grid,
|
|
93
|
+
firstAt,
|
|
94
|
+
lastAt,
|
|
95
|
+
models: [],
|
|
96
|
+
accounts: []
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
for (const line of text.split("\n")) {
|
|
100
|
+
if (line === "") continue;
|
|
101
|
+
let parsed;
|
|
102
|
+
try {
|
|
103
|
+
parsed = JSON.parse(line);
|
|
104
|
+
} catch {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) continue;
|
|
108
|
+
const at = parsed.at;
|
|
109
|
+
if (typeof at !== "number" || !Number.isFinite(at) || at < since || at > now + 6e4) continue;
|
|
110
|
+
const purpose = typeof parsed.purpose === "string" ? parsed.purpose : "";
|
|
111
|
+
if (purpose === "session-title" || purpose === "compaction") continue;
|
|
112
|
+
const provider = typeof parsed.provider === "string" ? parsed.provider : "";
|
|
113
|
+
const model = typeof parsed.model === "string" ? parsed.model : "";
|
|
114
|
+
const account = typeof parsed.account === "string" && parsed.account !== "" ? parsed.account : "unknown";
|
|
115
|
+
const date = new Date(at);
|
|
116
|
+
const hour = date.getHours();
|
|
117
|
+
const day = (date.getDay() + 6) % 7;
|
|
118
|
+
const row = grid[day];
|
|
119
|
+
hours[hour] = (hours[hour] ?? 0) + 1;
|
|
120
|
+
if (row !== void 0) row[hour] = (row[hour] ?? 0) + 1;
|
|
121
|
+
if (provider !== "" && model !== "") {
|
|
122
|
+
const modelKey = `${provider}/${model}`;
|
|
123
|
+
let entry = models.get(modelKey);
|
|
124
|
+
if (entry === void 0) {
|
|
125
|
+
entry = {
|
|
126
|
+
provider,
|
|
127
|
+
model,
|
|
128
|
+
total: 0,
|
|
129
|
+
hours: Array.from({ length: 24 }, () => 0),
|
|
130
|
+
grid: Array.from({ length: 7 }, () => Array.from({ length: 24 }, () => 0))
|
|
131
|
+
};
|
|
132
|
+
models.set(modelKey, entry);
|
|
133
|
+
}
|
|
134
|
+
entry.total += 1;
|
|
135
|
+
entry.hours[hour] = (entry.hours[hour] ?? 0) + 1;
|
|
136
|
+
const modelRow = entry.grid[day];
|
|
137
|
+
if (modelRow !== void 0) modelRow[hour] = (modelRow[hour] ?? 0) + 1;
|
|
138
|
+
}
|
|
139
|
+
let accountEntry = accounts.get(account);
|
|
140
|
+
if (accountEntry === void 0) {
|
|
141
|
+
accountEntry = {
|
|
142
|
+
account,
|
|
143
|
+
total: 0,
|
|
144
|
+
errors: 0,
|
|
145
|
+
avgLatencyMs: null,
|
|
146
|
+
avgTtftMs: null,
|
|
147
|
+
hours: Array.from({ length: 24 }, () => 0),
|
|
148
|
+
grid: Array.from({ length: 7 }, () => Array.from({ length: 24 }, () => 0))
|
|
149
|
+
};
|
|
150
|
+
accounts.set(account, accountEntry);
|
|
151
|
+
}
|
|
152
|
+
accountEntry.total += 1;
|
|
153
|
+
let metrics = accountMetrics.get(account);
|
|
154
|
+
if (metrics === void 0) {
|
|
155
|
+
metrics = {
|
|
156
|
+
errors: 0,
|
|
157
|
+
latencySum: 0,
|
|
158
|
+
latencyCount: 0,
|
|
159
|
+
ttftSum: 0,
|
|
160
|
+
ttftCount: 0
|
|
161
|
+
};
|
|
162
|
+
accountMetrics.set(account, metrics);
|
|
163
|
+
}
|
|
164
|
+
if (parsed.ok === false) metrics.errors += 1;
|
|
165
|
+
const latency = parsed.latencyMs;
|
|
166
|
+
if (typeof latency === "number" && Number.isFinite(latency) && latency >= 0) {
|
|
167
|
+
metrics.latencySum += latency;
|
|
168
|
+
metrics.latencyCount += 1;
|
|
169
|
+
}
|
|
170
|
+
const ttft = parsed.ttftMs;
|
|
171
|
+
if (typeof ttft === "number" && Number.isFinite(ttft) && ttft >= 0) {
|
|
172
|
+
metrics.ttftSum += ttft;
|
|
173
|
+
metrics.ttftCount += 1;
|
|
174
|
+
}
|
|
175
|
+
accountEntry.hours[hour] = (accountEntry.hours[hour] ?? 0) + 1;
|
|
176
|
+
const accountRow = accountEntry.grid[day];
|
|
177
|
+
if (accountRow !== void 0) accountRow[hour] = (accountRow[hour] ?? 0) + 1;
|
|
178
|
+
total += 1;
|
|
179
|
+
if (firstAt === null || at < firstAt) firstAt = at;
|
|
180
|
+
if (lastAt === null || at > lastAt) lastAt = at;
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
since,
|
|
184
|
+
total,
|
|
185
|
+
hours,
|
|
186
|
+
grid,
|
|
187
|
+
firstAt,
|
|
188
|
+
lastAt,
|
|
189
|
+
models: [...models.values()].sort((a, b) => b.total - a.total || a.model.localeCompare(b.model)),
|
|
190
|
+
accounts: [...accounts.values()].map((entry) => {
|
|
191
|
+
const metrics = accountMetrics.get(entry.account);
|
|
192
|
+
return {
|
|
193
|
+
...entry,
|
|
194
|
+
errors: metrics?.errors ?? 0,
|
|
195
|
+
avgLatencyMs: metrics !== void 0 && metrics.latencyCount > 0 ? metrics.latencySum / metrics.latencyCount : null,
|
|
196
|
+
avgTtftMs: metrics !== void 0 && metrics.ttftCount > 0 ? metrics.ttftSum / metrics.ttftCount : null
|
|
197
|
+
};
|
|
198
|
+
}).sort((a, b) => b.total - a.total || a.account.localeCompare(b.account))
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
export { compactRequestLog, flushRequestLog, readRequestHeatmap, recordRequest, requestLogPath };
|
|
202
|
+
|
|
203
|
+
//# sourceMappingURL=request-log.js.map
|
package/lib/retry.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/retry.ts
|
|
2
|
+
function retryPolicy(value) {
|
|
3
|
+
const integer = (number, fallback) => typeof number === "number" && Number.isInteger(number) && number >= 0 ? number : fallback;
|
|
4
|
+
const jitter = typeof value.retryJitter === "number" && value.retryJitter >= 0 && value.retryJitter <= 1 ? value.retryJitter : .2;
|
|
5
|
+
const baseDelayMs = integer(value.retryBaseDelayMs, 250);
|
|
6
|
+
return {
|
|
7
|
+
maxRetries: integer(value.retryMaxRetries, 0),
|
|
8
|
+
baseDelayMs,
|
|
9
|
+
maxDelayMs: Math.max(baseDelayMs, integer(value.retryMaxDelayMs, 2e3)),
|
|
10
|
+
jitter
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function retryDelay(attempt, policy, random = Math.random) {
|
|
14
|
+
const exponential = policy.baseDelayMs * 2 ** Math.max(0, attempt);
|
|
15
|
+
const capped = Math.min(policy.maxDelayMs, exponential);
|
|
16
|
+
const spread = capped * policy.jitter;
|
|
17
|
+
return Math.max(0, Math.round(capped - spread + random() * spread * 2));
|
|
18
|
+
}
|
|
19
|
+
function isRetryableStatus(status) {
|
|
20
|
+
return status === 408 || status === 429 || status >= 500;
|
|
21
|
+
}
|
|
22
|
+
export { isRetryableStatus, retryDelay, retryPolicy };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -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
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { sanitizeUrl } from "./sanitize.js";
|
|
2
|
+
// src/routes-public.ts
|
|
3
|
+
function publicConfig(config) {
|
|
4
|
+
return {
|
|
5
|
+
apiKeyEnv: config.apiKeyEnv,
|
|
6
|
+
apiBase: sanitizeUrl(config.apiBase),
|
|
7
|
+
openAiBase: sanitizeUrl(config.openAiBase),
|
|
8
|
+
usageUrl: sanitizeUrl(config.usageUrl),
|
|
9
|
+
balanceUrl: sanitizeUrl(config.balanceUrl),
|
|
10
|
+
transport: config.transport,
|
|
11
|
+
modelTransports: config.modelTransports,
|
|
12
|
+
requestTimeoutMs: config.requestTimeoutMs,
|
|
13
|
+
streamIdleTimeoutMs: config.streamIdleTimeoutMs,
|
|
14
|
+
attemptTimeoutMs: config.attemptTimeoutMs,
|
|
15
|
+
retryMaxRetries: config.retryMaxRetries,
|
|
16
|
+
retryBaseDelayMs: config.retryBaseDelayMs,
|
|
17
|
+
retryMaxDelayMs: config.retryMaxDelayMs,
|
|
18
|
+
retryJitter: config.retryJitter,
|
|
19
|
+
visibleModels: config.visibleModels,
|
|
20
|
+
showAllModels: config.showAllModels,
|
|
21
|
+
modelVisibility: config.modelVisibility,
|
|
22
|
+
manualModels: config.manualModels,
|
|
23
|
+
modelOverrides: config.modelOverrides,
|
|
24
|
+
modelOrder: config.modelOrder,
|
|
25
|
+
filterModelsByPlan: config.filterModelsByPlan,
|
|
26
|
+
contextWindow: config.contextWindow ?? null,
|
|
27
|
+
contextWindowOverrides: config.contextWindowOverrides,
|
|
28
|
+
maxOutputTokens: config.maxOutputTokens ?? null,
|
|
29
|
+
temperature: config.temperature ?? null,
|
|
30
|
+
topK: config.topK ?? null,
|
|
31
|
+
topP: config.topP ?? null,
|
|
32
|
+
minP: config.minP ?? null,
|
|
33
|
+
seed: config.seed ?? null,
|
|
34
|
+
repeatPenalty: config.repeatPenalty ?? null,
|
|
35
|
+
repeatLastN: config.repeatLastN ?? null,
|
|
36
|
+
numCtx: config.numCtx ?? null,
|
|
37
|
+
keepAlive: config.keepAlive ?? "",
|
|
38
|
+
stop: config.stop ?? [],
|
|
39
|
+
autoRefresh: config.autoRefresh,
|
|
40
|
+
refreshMinutes: config.refreshMinutes,
|
|
41
|
+
accounts: config.accounts.map((account) => ({
|
|
42
|
+
label: typeof account.label === "string" ? account.label : "",
|
|
43
|
+
apiKeyEnv: typeof account.apiKeyEnv === "string" ? account.apiKeyEnv : ""
|
|
44
|
+
})),
|
|
45
|
+
modelAccountRules: config.modelAccountRules.map((rule) => ({
|
|
46
|
+
pattern: rule.pattern,
|
|
47
|
+
account: rule.account
|
|
48
|
+
})),
|
|
49
|
+
stickySession: config.stickySession,
|
|
50
|
+
activeAccount: config.activeAccount,
|
|
51
|
+
showSidebarQuota: config.showSidebarQuota,
|
|
52
|
+
planLabel: config.planLabel,
|
|
53
|
+
priceOverrides: config.priceOverrides,
|
|
54
|
+
userAgent: config.userAgent
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export { publicConfig };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=routes-public.js.map
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// src/routes-values.ts
|
|
2
|
+
const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
|
|
3
|
+
const CREDENTIAL_REF_PATTERN = /^OLLAMA_[A-Z0-9_]+$/;
|
|
4
|
+
/** The plugin may read and write only its own credential namespace. */
|
|
5
|
+
function normalizeCredentialRef(value) {
|
|
6
|
+
if (typeof value !== "string") return void 0;
|
|
7
|
+
const ref = value.trim();
|
|
8
|
+
return CREDENTIAL_REF_PATTERN.test(ref) ? ref : void 0;
|
|
9
|
+
}
|
|
10
|
+
function isCredentialRef(value) {
|
|
11
|
+
return normalizeCredentialRef(value) !== void 0;
|
|
12
|
+
}
|
|
13
|
+
function cleanAccounts(value) {
|
|
14
|
+
if (!Array.isArray(value)) return [];
|
|
15
|
+
const out = [];
|
|
16
|
+
for (const entry of value) {
|
|
17
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
18
|
+
const record = entry;
|
|
19
|
+
const label = typeof record.label === "string" ? record.label.trim() : "";
|
|
20
|
+
const apiKeyEnv = typeof record.apiKeyEnv === "string" ? record.apiKeyEnv.trim() : "";
|
|
21
|
+
if (label === "" && apiKeyEnv === "") continue;
|
|
22
|
+
out.push({
|
|
23
|
+
label: label === "" ? `account-${out.length + 2}` : label,
|
|
24
|
+
apiKeyEnv
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
function cleanStringArray(value) {
|
|
30
|
+
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry !== "") : [];
|
|
31
|
+
}
|
|
32
|
+
function cleanModelAccountRules(value) {
|
|
33
|
+
if (!Array.isArray(value)) return [];
|
|
34
|
+
const out = [];
|
|
35
|
+
for (const entry of value) {
|
|
36
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
37
|
+
const record = entry;
|
|
38
|
+
const pattern = typeof record.pattern === "string" ? record.pattern.trim() : "";
|
|
39
|
+
const account = typeof record.account === "string" ? record.account.trim() : "";
|
|
40
|
+
if (pattern === "" || account === "") continue;
|
|
41
|
+
out.push({
|
|
42
|
+
pattern,
|
|
43
|
+
account
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
function cleanManualModels(value) {
|
|
49
|
+
if (!Array.isArray(value)) return [];
|
|
50
|
+
const out = [];
|
|
51
|
+
for (const entry of value) {
|
|
52
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
53
|
+
const record = entry;
|
|
54
|
+
const id = typeof record.id === "string" ? record.id.trim() : "";
|
|
55
|
+
if (id === "" || id.includes("..") || !/^[A-Za-z0-9][A-Za-z0-9._:/-]*$/.test(id)) continue;
|
|
56
|
+
const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
|
|
57
|
+
const thinking = typeof record.thinking === "boolean" ? record.thinking : void 0;
|
|
58
|
+
const normalizedEfforts = thinking === true && (efforts === void 0 || efforts.length === 0) ? ["off", "high"] : thinking === false ? [] : efforts;
|
|
59
|
+
out.push({
|
|
60
|
+
id,
|
|
61
|
+
enabled: record.enabled !== false,
|
|
62
|
+
...typeof record.name === "string" && record.name.trim() !== "" ? { name: record.name.trim() } : {},
|
|
63
|
+
...typeof record.contextWindow === "number" && Number.isInteger(record.contextWindow) && record.contextWindow > 0 ? { contextWindow: record.contextWindow } : {},
|
|
64
|
+
...typeof record.maxTokens === "number" && Number.isInteger(record.maxTokens) && record.maxTokens > 0 ? { maxTokens: record.maxTokens } : {},
|
|
65
|
+
...typeof record.image === "boolean" ? { image: record.image } : {},
|
|
66
|
+
...thinking === void 0 ? {} : { thinking },
|
|
67
|
+
...normalizedEfforts === void 0 ? {} : { reasoningEfforts: normalizedEfforts },
|
|
68
|
+
...typeof record.defaultReasoning === "string" && record.defaultReasoning.trim() !== "" ? { defaultReasoning: record.defaultReasoning.trim() } : {},
|
|
69
|
+
...typeof record.tools === "boolean" ? { tools: record.tools } : {}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
function cleanModelOverrides(value) {
|
|
75
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
76
|
+
const out = {};
|
|
77
|
+
for (const [id, entry] of Object.entries(value)) {
|
|
78
|
+
if (id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
79
|
+
const record = entry;
|
|
80
|
+
const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
|
|
81
|
+
const thinking = typeof record.thinking === "boolean" ? record.thinking : void 0;
|
|
82
|
+
const normalizedEfforts = thinking === true && (efforts === void 0 || efforts.length === 0) ? ["off", "high"] : thinking === false ? [] : efforts;
|
|
83
|
+
const outEntry = {
|
|
84
|
+
...typeof record.contextWindow === "number" && Number.isInteger(record.contextWindow) && record.contextWindow > 0 ? { contextWindow: record.contextWindow } : {},
|
|
85
|
+
...typeof record.image === "boolean" ? { image: record.image } : {},
|
|
86
|
+
...thinking === void 0 ? {} : { thinking },
|
|
87
|
+
...normalizedEfforts === void 0 ? {} : { reasoningEfforts: normalizedEfforts },
|
|
88
|
+
...typeof record.defaultEffort === "string" && record.defaultEffort.trim() !== "" ? { defaultEffort: record.defaultEffort.trim() } : {},
|
|
89
|
+
...typeof record.enabled === "boolean" ? { enabled: record.enabled } : {}
|
|
90
|
+
};
|
|
91
|
+
if (Object.keys(outEntry).length > 0) out[id] = outEntry;
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
function cleanBooleanRecord(value) {
|
|
96
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
97
|
+
const out = {};
|
|
98
|
+
for (const [key, entry] of Object.entries(value)) if (typeof entry === "boolean") out[key] = entry;
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
function cleanNumberRecord(value) {
|
|
102
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
103
|
+
const out = {};
|
|
104
|
+
for (const [key, entry] of Object.entries(value)) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
function cleanTransports(value) {
|
|
108
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
109
|
+
const out = {};
|
|
110
|
+
for (const [key, entry] of Object.entries(value)) if (typeof entry === "string" && TRANSPORTS.has(entry)) out[key] = entry;
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
function cleanPrices(value) {
|
|
114
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
115
|
+
const out = {};
|
|
116
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
117
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
118
|
+
const raw = entry;
|
|
119
|
+
const price = {};
|
|
120
|
+
if (typeof raw.input === "number" && raw.input >= 0) price.input = raw.input;
|
|
121
|
+
if (typeof raw.output === "number" && raw.output >= 0) price.output = raw.output;
|
|
122
|
+
if (typeof raw.cacheRead === "number" && raw.cacheRead >= 0) price.cacheRead = raw.cacheRead;
|
|
123
|
+
if (Object.keys(price).length > 0) out[key] = price;
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
export { cleanAccounts, cleanBooleanRecord, cleanManualModels, cleanModelAccountRules, cleanModelOverrides, cleanNumberRecord, cleanPrices, cleanStringArray, cleanTransports, isCredentialRef, normalizeCredentialRef };
|
|
128
|
+
|
|
129
|
+
//# sourceMappingURL=routes-values.js.map
|
package/lib/routes.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { OllamaCloudAdapter } from "./adapter.js";
|
|
2
|
+
import { AnalyticsReport } from "./analytics.js";
|
|
3
|
+
import { ResolvedConfig } from "./config.js";
|
|
4
|
+
import { KeyPool } from "./credentials.js";
|
|
5
|
+
import { RequestHeatmap } from "./request-log.js";
|
|
6
|
+
import { UsageReader } from "./usage.js";
|
|
7
|
+
import { Context } from "@deepseek-ai/cordis";
|
|
8
|
+
import { IncomingMessage } from "node:http";
|
|
9
|
+
import { WebServer } from "@deepseek-ai/dsh-host-webserver";
|
|
10
|
+
interface RouteDeps {
|
|
11
|
+
readonly config: () => ResolvedConfig;
|
|
12
|
+
readonly adapter: OllamaCloudAdapter;
|
|
13
|
+
readonly pool: KeyPool;
|
|
14
|
+
readonly usage: UsageReader;
|
|
15
|
+
readonly analytics?: (days: number) => Promise<AnalyticsReport>;
|
|
16
|
+
readonly heatmap?: (days: number) => Promise<RequestHeatmap>;
|
|
17
|
+
readonly refreshTui?: () => void;
|
|
18
|
+
/** Test seam; defaults to verifying the cookie against the host API. */
|
|
19
|
+
readonly verifyDesktop?: (req: IncomingMessage) => Promise<boolean>;
|
|
20
|
+
/** Same verifier for private reads; falls back to `verifyDesktop`. */
|
|
21
|
+
readonly verifyHost?: (req: IncomingMessage) => Promise<boolean>;
|
|
22
|
+
/** Test seam for unsaved-endpoint discovery. */
|
|
23
|
+
readonly discoveryFetch?: typeof fetch;
|
|
24
|
+
}
|
|
25
|
+
/** Register the Host HTTP surface consumed by the browser settings page. */
|
|
26
|
+
declare function applyRoutes(ctx: Context, web: WebServer, deps: RouteDeps): void;
|
|
27
|
+
export { RouteDeps, applyRoutes };
|
|
28
|
+
//# sourceMappingURL=routes.d.ts.map
|