@goodandready/dsh-ollama-cloud-provider 0.1.39 → 0.1.41
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 +26 -3
- package/README.md +64 -23
- package/README.ru.md +78 -18
- package/README.zh.md +86 -17
- package/lib/adapter.js +9 -2
- package/lib/catalog-cache.js +8 -2
- package/lib/catalog-scheduler.js +34 -0
- package/lib/catalog.js +23 -14
- package/lib/client.js +331 -191
- package/lib/config.js +19 -6
- package/lib/credentials.d.ts +1 -3
- package/lib/credentials.js +6 -6
- package/lib/discovery.js +74 -6
- package/lib/index.d.ts +2 -2
- package/lib/index.js +18 -14
- package/lib/quota-model.js +5 -6
- package/lib/request-log.js +12 -3
- package/lib/routes-values.js +10 -83
- package/lib/routes.js +13 -7
- package/lib/status.js +1 -0
- package/lib/transports/ndjson.js +10 -26
- package/lib/transports/openai-chat.js +32 -3
- package/lib/transports/sse.js +10 -25
- package/lib/transports/stream-reader.js +31 -0
- package/lib/usage.js +17 -10
- package/package.json +3 -3
package/lib/config.js
CHANGED
|
@@ -7,7 +7,7 @@ const DEFAULT_API_BASE = "https://ollama.com";
|
|
|
7
7
|
const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
|
|
8
8
|
const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
|
|
9
9
|
const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
|
|
10
|
-
const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.
|
|
10
|
+
const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.41";
|
|
11
11
|
const DEFAULT_CONTEXT_WINDOW = 131072;
|
|
12
12
|
const DEFAULT_MAX_TOKENS = 32768;
|
|
13
13
|
const DEFAULT_GENERATE_MAX_TOKENS = 131072;
|
|
@@ -120,7 +120,7 @@ function modelOverrideRecord(value) {
|
|
|
120
120
|
if (typeof value !== "object" || value === null || Array.isArray(value)) return {};
|
|
121
121
|
const out = {};
|
|
122
122
|
for (const [id, entry] of Object.entries(value)) {
|
|
123
|
-
if (id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
123
|
+
if (unsafeKey(id) || id === "" || id.includes("..") || typeof entry !== "object" || entry === null || Array.isArray(entry)) continue;
|
|
124
124
|
const record = entry;
|
|
125
125
|
const contextWindow = integerInRange(record.contextWindow, 1, 1e7);
|
|
126
126
|
const efforts = Array.isArray(record.reasoningEfforts) ? record.reasoningEfforts.filter((item) => typeof item === "string" && item.trim() !== "") : void 0;
|
|
@@ -172,19 +172,29 @@ function manualModelList(value) {
|
|
|
172
172
|
function recordOf(value) {
|
|
173
173
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
|
|
174
174
|
}
|
|
175
|
+
function unsafeKey(key) {
|
|
176
|
+
return key === "__proto__" || key === "constructor" || key === "prototype";
|
|
177
|
+
}
|
|
175
178
|
function numberRecord(value) {
|
|
176
179
|
const out = {};
|
|
177
|
-
for (const [key, entry] of Object.entries(recordOf(value)))
|
|
180
|
+
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
181
|
+
if (unsafeKey(key)) continue;
|
|
182
|
+
if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
|
|
183
|
+
}
|
|
178
184
|
return out;
|
|
179
185
|
}
|
|
180
186
|
function booleanRecord(value) {
|
|
181
187
|
const out = {};
|
|
182
|
-
for (const [key, entry] of Object.entries(recordOf(value)))
|
|
188
|
+
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
189
|
+
if (unsafeKey(key)) continue;
|
|
190
|
+
if (typeof entry === "boolean") out[key] = entry;
|
|
191
|
+
}
|
|
183
192
|
return out;
|
|
184
193
|
}
|
|
185
194
|
function priceRecord(value) {
|
|
186
195
|
const out = {};
|
|
187
196
|
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
197
|
+
if (unsafeKey(key)) continue;
|
|
188
198
|
const raw = recordOf(entry);
|
|
189
199
|
const price = {};
|
|
190
200
|
if (typeof raw.input === "number" && raw.input >= 0) price.input = raw.input;
|
|
@@ -197,7 +207,10 @@ function priceRecord(value) {
|
|
|
197
207
|
const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
|
|
198
208
|
function transportRecord(value) {
|
|
199
209
|
const out = {};
|
|
200
|
-
for (const [key, entry] of Object.entries(recordOf(value)))
|
|
210
|
+
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
211
|
+
if (unsafeKey(key)) continue;
|
|
212
|
+
if (typeof entry === "string" && TRANSPORTS.has(entry)) out[key] = entry;
|
|
213
|
+
}
|
|
201
214
|
return out;
|
|
202
215
|
}
|
|
203
216
|
function ruleList(value) {
|
|
@@ -275,6 +288,6 @@ function resolveConfig(config) {
|
|
|
275
288
|
userAgent: typeof config.userAgent === "string" && config.userAgent !== "" ? config.userAgent : DEFAULT_USER_AGENT
|
|
276
289
|
};
|
|
277
290
|
}
|
|
278
|
-
export { Config, DEFAULT_API_BASE, DEFAULT_API_KEY_ENV, DEFAULT_BALANCE_URL, DEFAULT_CONTEXT_WINDOW, DEFAULT_GENERATE_MAX_TOKENS, DEFAULT_MAX_TOKENS, DEFAULT_OPENAI_BASE, DEFAULT_USAGE_URL, DEFAULT_USER_AGENT, NS, resolveConfig, unwrapVolatileConfig };
|
|
291
|
+
export { Config, DEFAULT_API_BASE, DEFAULT_API_KEY_ENV, DEFAULT_BALANCE_URL, DEFAULT_CONTEXT_WINDOW, DEFAULT_GENERATE_MAX_TOKENS, DEFAULT_MAX_TOKENS, DEFAULT_OPENAI_BASE, DEFAULT_USAGE_URL, DEFAULT_USER_AGENT, NS, booleanRecord, manualModelList, modelOverrideRecord, numberRecord, priceRecord, resolveConfig, ruleList, transportRecord, unwrapVolatileConfig };
|
|
279
292
|
|
|
280
293
|
//# sourceMappingURL=config.js.map
|
package/lib/credentials.d.ts
CHANGED
|
@@ -17,7 +17,5 @@ interface KeyPool {
|
|
|
17
17
|
* Keys are deduplicated; a slot without a resolvable value is skipped.
|
|
18
18
|
*/
|
|
19
19
|
declare function createKeyPool(ctx: Context, config: () => ResolvedConfig): KeyPool;
|
|
20
|
-
|
|
21
|
-
declare function createKeyResolver(ctx: Context, config: () => ResolvedConfig): KeyResolver;
|
|
22
|
-
export { KeyPool, KeyResolver, ResolvedKey, createKeyPool, createKeyResolver };
|
|
20
|
+
export { KeyPool, KeyResolver, ResolvedKey, createKeyPool };
|
|
23
21
|
//# sourceMappingURL=credentials.d.ts.map
|
package/lib/credentials.js
CHANGED
|
@@ -20,6 +20,10 @@ async function resolveRef(ctx, refName) {
|
|
|
20
20
|
source: ambient.source
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
+
function accountLabel(label, index) {
|
|
24
|
+
const trimmed = typeof label === "string" ? label.trim() : "";
|
|
25
|
+
return trimmed === "" ? `account-${index + 2}` : trimmed;
|
|
26
|
+
}
|
|
23
27
|
function slotsOf(config) {
|
|
24
28
|
const slots = [{
|
|
25
29
|
label: "default",
|
|
@@ -27,7 +31,7 @@ function slotsOf(config) {
|
|
|
27
31
|
refName: config.apiKeyEnv
|
|
28
32
|
}];
|
|
29
33
|
for (const [index, account] of config.accounts.entries()) {
|
|
30
|
-
const label =
|
|
34
|
+
const label = accountLabel(account.label, index);
|
|
31
35
|
const literal = typeof account.apiKey === "string" && account.apiKey.trim() !== "" ? account.apiKey.trim() : void 0;
|
|
32
36
|
const refName = typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() !== "" ? account.apiKeyEnv.trim() : void 0;
|
|
33
37
|
if (literal === void 0 && refName === void 0) continue;
|
|
@@ -96,10 +100,6 @@ function createKeyPool(ctx, config) {
|
|
|
96
100
|
function missingKey(config) {
|
|
97
101
|
return new LlmError(`llm-ollama-cloud: no API key. Store ${config.apiKeyEnv} through the credentials service, export it in the launching environment, or set config.apiKey in the composition. Create a key at https://ollama.com/settings/keys.`, "MISSING_CREDENTIAL");
|
|
98
102
|
}
|
|
99
|
-
|
|
100
|
-
function createKeyResolver(ctx, config) {
|
|
101
|
-
return createKeyPool(ctx, config).resolve;
|
|
102
|
-
}
|
|
103
|
-
export { createKeyPool, createKeyResolver };
|
|
103
|
+
export { accountLabel, createKeyPool };
|
|
104
104
|
|
|
105
105
|
//# sourceMappingURL=credentials.js.map
|
package/lib/discovery.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { sanitizeUrl } from "./sanitize.js";
|
|
2
2
|
import { isIP } from "node:net";
|
|
3
3
|
import { lookup } from "node:dns/promises";
|
|
4
|
+
import { request } from "node:https";
|
|
4
5
|
// src/discovery.ts
|
|
5
6
|
const DISCOVERY_TIMEOUT_MS = 15e3;
|
|
7
|
+
const MAX_DISCOVERY_BYTES = 1e6;
|
|
6
8
|
var DiscoveryError = class extends Error {
|
|
7
9
|
status;
|
|
8
10
|
constructor(message, status) {
|
|
@@ -33,10 +35,14 @@ function isPrivateAddress(address) {
|
|
|
33
35
|
const clean = address.toLowerCase().replace(/^\[|\]$/g, "");
|
|
34
36
|
if (isIP(clean) === 4) return isPrivateV4(clean);
|
|
35
37
|
if (isIP(clean) !== 6) return true;
|
|
36
|
-
|
|
38
|
+
if (clean.startsWith("::ffff:")) {
|
|
39
|
+
const embedded = clean.slice(7);
|
|
40
|
+
return isIP(embedded) === 4 ? isPrivateV4(embedded) : true;
|
|
41
|
+
}
|
|
42
|
+
return clean === "::1" || clean === "::" || clean.startsWith("::") || clean.startsWith("fc") || clean.startsWith("fd") || /^fe[89ab]/.test(clean) || clean.startsWith("ff") || clean.startsWith("2001:db8") || clean.startsWith("64:ff9b:") || clean.startsWith("2002:");
|
|
37
43
|
}
|
|
38
44
|
async function assertPublicEndpoint(base, options) {
|
|
39
|
-
if (options.allowPrivate === true) return;
|
|
45
|
+
if (options.allowPrivate === true) return void 0;
|
|
40
46
|
const url = new URL(base);
|
|
41
47
|
if (url.protocol !== "https:") throw new DiscoveryError("endpoint must use https");
|
|
42
48
|
const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");
|
|
@@ -52,6 +58,7 @@ async function assertPublicEndpoint(base, options) {
|
|
|
52
58
|
throw new DiscoveryError("could not resolve endpoint");
|
|
53
59
|
}
|
|
54
60
|
if (addresses.length === 0 || addresses.some(isPrivateAddress)) throw new DiscoveryError("private endpoint refused");
|
|
61
|
+
return addresses;
|
|
55
62
|
}
|
|
56
63
|
function idsOf(payload) {
|
|
57
64
|
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return [];
|
|
@@ -69,7 +76,8 @@ function idsOf(payload) {
|
|
|
69
76
|
return typeof id === "string" && id !== "" ? id : void 0;
|
|
70
77
|
}).filter((id) => id !== void 0) : [];
|
|
71
78
|
}
|
|
72
|
-
async function get(url, key, fetchImpl, signal) {
|
|
79
|
+
async function get(url, key, fetchImpl, signal, pinned) {
|
|
80
|
+
if (pinned !== void 0 && pinned.length > 0 && fetchImpl === fetch) return pinnedJson(url, key, signal, pinned[0]);
|
|
73
81
|
const response = await fetchImpl(url, {
|
|
74
82
|
method: "GET",
|
|
75
83
|
headers: {
|
|
@@ -83,19 +91,79 @@ async function get(url, key, fetchImpl, signal) {
|
|
|
83
91
|
return response.json();
|
|
84
92
|
}
|
|
85
93
|
/**
|
|
94
|
+
* Connect to the already validated address instead of resolving the name again,
|
|
95
|
+
* so a second DNS answer cannot redirect the request to a private host.
|
|
96
|
+
*/
|
|
97
|
+
function pinnedJson(url, key, signal, address) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
const target = new URL(url);
|
|
100
|
+
const headers = {
|
|
101
|
+
accept: "application/json",
|
|
102
|
+
host: target.host
|
|
103
|
+
};
|
|
104
|
+
if (key !== "") headers.authorization = `Bearer ${key}`;
|
|
105
|
+
let settled = false;
|
|
106
|
+
const fail = (error) => {
|
|
107
|
+
if (settled) return;
|
|
108
|
+
settled = true;
|
|
109
|
+
reject(error);
|
|
110
|
+
};
|
|
111
|
+
const req = request({
|
|
112
|
+
protocol: "https:",
|
|
113
|
+
host: address,
|
|
114
|
+
port: target.port === "" ? 443 : Number(target.port),
|
|
115
|
+
path: `${target.pathname}${target.search}`,
|
|
116
|
+
method: "GET",
|
|
117
|
+
headers,
|
|
118
|
+
servername: target.hostname.replace(/^\[|\]$/g, ""),
|
|
119
|
+
signal
|
|
120
|
+
}, (res) => {
|
|
121
|
+
const chunks = [];
|
|
122
|
+
let size = 0;
|
|
123
|
+
res.on("data", (chunk) => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
size += chunk.length;
|
|
126
|
+
if (size > MAX_DISCOVERY_BYTES) {
|
|
127
|
+
res.destroy(new DiscoveryError("endpoint response too large"));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
chunks.push(chunk);
|
|
131
|
+
});
|
|
132
|
+
res.on("error", fail);
|
|
133
|
+
res.on("end", () => {
|
|
134
|
+
if (settled) return;
|
|
135
|
+
const status = res.statusCode ?? 0;
|
|
136
|
+
if (status < 200 || status >= 300) {
|
|
137
|
+
fail(new DiscoveryError(`endpoint answered ${status}`, status));
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const payload = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
142
|
+
settled = true;
|
|
143
|
+
resolve(payload);
|
|
144
|
+
} catch {
|
|
145
|
+
fail(new DiscoveryError("endpoint returned invalid JSON"));
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
req.on("error", fail);
|
|
150
|
+
req.end();
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
86
154
|
* Discover a catalog for an unsaved endpoint with a one-shot key.
|
|
87
155
|
* This function performs no persistence and never includes the key in errors.
|
|
88
156
|
*/
|
|
89
157
|
async function discoverModels(rawBase, key, fetchImpl = fetch, options = {}) {
|
|
90
158
|
const base = normalizeBase(rawBase);
|
|
91
|
-
await assertPublicEndpoint(base, options);
|
|
159
|
+
const pinned = await assertPublicEndpoint(base, options);
|
|
92
160
|
const budget = AbortSignal.timeout(DISCOVERY_TIMEOUT_MS);
|
|
93
161
|
let payload;
|
|
94
162
|
try {
|
|
95
|
-
payload = await get(`${base}/v1/models`, key, fetchImpl, budget);
|
|
163
|
+
payload = await get(`${base}/v1/models`, key, fetchImpl, budget, pinned);
|
|
96
164
|
} catch (error) {
|
|
97
165
|
if (error instanceof DiscoveryError && error.status === 401) throw error;
|
|
98
|
-
if (error instanceof DiscoveryError && error.status === 404) payload = await get(`${base}/api/tags`, key, fetchImpl, budget);
|
|
166
|
+
if (error instanceof DiscoveryError && error.status === 404) payload = await get(`${base}/api/tags`, key, fetchImpl, budget, pinned);
|
|
99
167
|
else throw error;
|
|
100
168
|
}
|
|
101
169
|
return {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OllamaCloudAdapter, OllamaCloudAdapterDeps } from "./adapter.js";
|
|
2
2
|
import { Config, ResolvedConfig, TransportId, resolveConfig } from "./config.js";
|
|
3
3
|
import { PROVIDER } from "./provider-ids.js";
|
|
4
|
-
import { ResolvedKey, createKeyPool
|
|
4
|
+
import { ResolvedKey, createKeyPool } from "./credentials.js";
|
|
5
5
|
import { CommandDeps, applyCommands } from "./commands.js";
|
|
6
6
|
import { RouteDeps, applyRoutes } from "./routes.js";
|
|
7
7
|
import { UsageReport, UsageResult, UsageWindow, createUsageReader, normalizeBalance, normalizeUsage } from "./usage.js";
|
|
@@ -16,5 +16,5 @@ declare const inject: string[];
|
|
|
16
16
|
* without a restart.
|
|
17
17
|
*/
|
|
18
18
|
declare function apply(ctx: Context, config: Config): void;
|
|
19
|
-
export { type CommandDeps, Config, OllamaCloudAdapter, type OllamaCloudAdapterDeps, type Config as OllamaCloudConfig, PROVIDER, type ResolvedConfig, type ResolvedKey, type RouteDeps, type TransportId, type UsageReport, type UsageResult, type UsageWindow, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool,
|
|
19
|
+
export { type CommandDeps, Config, OllamaCloudAdapter, type OllamaCloudAdapterDeps, type Config as OllamaCloudConfig, PROVIDER, type ResolvedConfig, type ResolvedKey, type RouteDeps, type TransportId, type UsageReport, type UsageResult, type UsageWindow, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool, createUsageReader, describeModel, effectiveModel, inject, name, normalizeBalance, normalizeUsage, resolveConfig };
|
|
20
20
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
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
|
-
import {
|
|
7
|
+
import { accountLabel, createKeyPool } from "./credentials.js";
|
|
7
8
|
import { registerPluginUpdater } from "./plugin-updater.js";
|
|
8
9
|
import { PROVIDER } from "./provider-ids.js";
|
|
9
10
|
import { flushRequestLog, readRequestHeatmap } from "./request-log.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) => {
|
|
@@ -88,8 +92,8 @@ function apply(ctx, config) {
|
|
|
88
92
|
id: "default",
|
|
89
93
|
label: "default"
|
|
90
94
|
}, ...current().accounts.map((account, index) => ({
|
|
91
|
-
id:
|
|
92
|
-
label:
|
|
95
|
+
id: accountLabel(account.label, index),
|
|
96
|
+
label: accountLabel(account.label, index)
|
|
93
97
|
}))],
|
|
94
98
|
visibleModels: () => current().visibleModels,
|
|
95
99
|
activeAccount: () => current().activeAccount,
|
|
@@ -128,6 +132,6 @@ function apply(ctx, config) {
|
|
|
128
132
|
}), "dsh-ollama-cloud-provider: plugin updater");
|
|
129
133
|
});
|
|
130
134
|
}
|
|
131
|
-
export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool,
|
|
135
|
+
export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool, createUsageReader, describeModel, effectiveModel, inject, name, normalizeBalance, normalizeUsage, resolveConfig };
|
|
132
136
|
|
|
133
137
|
//# sourceMappingURL=index.js.map
|
package/lib/quota-model.js
CHANGED
|
@@ -26,8 +26,7 @@ function quotaBars(report) {
|
|
|
26
26
|
if (report === void 0) return [];
|
|
27
27
|
if (report.kind === "credits" && report.credits !== void 0) {
|
|
28
28
|
const credits = report.credits;
|
|
29
|
-
return [bar("included", "
|
|
30
|
-
detail: `$${credits.includedRemaining.toFixed(2)} of $${credits.includedAllowance.toFixed(2)} left` + (credits.purchased > 0 ? ` · $${credits.purchased.toFixed(2)} purchased` : ""),
|
|
29
|
+
return [bar("included", "", credits.includedAllowance > 0 ? (credits.includedAllowance - credits.includedRemaining) / credits.includedAllowance * 100 : 0, {
|
|
31
30
|
detailValues: {
|
|
32
31
|
remaining: credits.includedRemaining,
|
|
33
32
|
allowance: credits.includedAllowance,
|
|
@@ -36,26 +35,26 @@ function quotaBars(report) {
|
|
|
36
35
|
...credits.periodUntil === void 0 ? {} : { resetsAt: credits.periodUntil }
|
|
37
36
|
})];
|
|
38
37
|
}
|
|
39
|
-
return [legacyBar("fiveHour", "
|
|
38
|
+
return [legacyBar("fiveHour", "", report.legacy?.fiveHour), legacyBar("weekly", "", report.legacy?.weekly)].filter((entry) => entry !== void 0);
|
|
40
39
|
}
|
|
41
40
|
function windowRows(report) {
|
|
42
41
|
if (report === void 0) return [];
|
|
43
42
|
return [
|
|
44
43
|
report.windows.d24 === void 0 ? void 0 : {
|
|
45
44
|
key: "24h",
|
|
46
|
-
label: "
|
|
45
|
+
label: "",
|
|
47
46
|
labelKey: "last24h",
|
|
48
47
|
totals: report.windows.d24
|
|
49
48
|
},
|
|
50
49
|
report.windows.d7 === void 0 ? void 0 : {
|
|
51
50
|
key: "7d",
|
|
52
|
-
label: "
|
|
51
|
+
label: "",
|
|
53
52
|
labelKey: "last7d",
|
|
54
53
|
totals: report.windows.d7
|
|
55
54
|
},
|
|
56
55
|
report.windows.d30 === void 0 ? void 0 : {
|
|
57
56
|
key: "30d",
|
|
58
|
-
label: "
|
|
57
|
+
label: "",
|
|
59
58
|
labelKey: "last30d",
|
|
60
59
|
totals: report.windows.d30
|
|
61
60
|
}
|
package/lib/request-log.js
CHANGED
|
@@ -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) {
|
|
@@ -59,7 +65,10 @@ async function compactRequestLog(maxBytes = MAX_LOG_BYTES, keepLines = KEEP_LINE
|
|
|
59
65
|
if (lines.length <= keepLines) return;
|
|
60
66
|
const temporary = `${path}.tmp`;
|
|
61
67
|
try {
|
|
62
|
-
await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`,
|
|
68
|
+
await writeFile(temporary, `${lines.slice(-keepLines).join("\n")}\n`, {
|
|
69
|
+
encoding: "utf8",
|
|
70
|
+
mode: 384
|
|
71
|
+
});
|
|
63
72
|
await rename(temporary, path);
|
|
64
73
|
} catch (error) {
|
|
65
74
|
await rm(temporary, { force: true }).catch((caught) => {
|
package/lib/routes-values.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { booleanRecord, manualModelList, modelOverrideRecord, numberRecord, priceRecord, ruleList, transportRecord } from "./config.js";
|
|
2
|
+
import { accountLabel } from "./credentials.js";
|
|
1
3
|
// src/routes-values.ts
|
|
2
|
-
const TRANSPORTS = /* @__PURE__ */ new Set(["native", "openai-chat"]);
|
|
3
4
|
const CREDENTIAL_REF_PATTERN = /^OLLAMA_[A-Z0-9_]+$/;
|
|
4
5
|
/** The plugin may read and write only its own credential namespace. */
|
|
5
6
|
function normalizeCredentialRef(value) {
|
|
@@ -20,7 +21,7 @@ function cleanAccounts(value) {
|
|
|
20
21
|
const apiKeyEnv = typeof record.apiKeyEnv === "string" ? record.apiKeyEnv.trim() : "";
|
|
21
22
|
if (label === "" && apiKeyEnv === "") continue;
|
|
22
23
|
out.push({
|
|
23
|
-
label: label
|
|
24
|
+
label: accountLabel(label, out.length),
|
|
24
25
|
apiKeyEnv
|
|
25
26
|
});
|
|
26
27
|
}
|
|
@@ -30,99 +31,25 @@ function cleanStringArray(value) {
|
|
|
30
31
|
return Array.isArray(value) ? value.filter((entry) => typeof entry === "string" && entry !== "") : [];
|
|
31
32
|
}
|
|
32
33
|
function cleanModelAccountRules(value) {
|
|
33
|
-
|
|
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;
|
|
34
|
+
return ruleList(value);
|
|
47
35
|
}
|
|
48
36
|
function cleanManualModels(value) {
|
|
49
|
-
|
|
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;
|
|
37
|
+
return manualModelList(value);
|
|
73
38
|
}
|
|
74
39
|
function cleanModelOverrides(value) {
|
|
75
|
-
|
|
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;
|
|
40
|
+
return modelOverrideRecord(value);
|
|
94
41
|
}
|
|
95
42
|
function cleanBooleanRecord(value) {
|
|
96
|
-
|
|
97
|
-
const out = {};
|
|
98
|
-
for (const [key, entry] of Object.entries(value)) if (typeof entry === "boolean") out[key] = entry;
|
|
99
|
-
return out;
|
|
43
|
+
return booleanRecord(value);
|
|
100
44
|
}
|
|
101
45
|
function cleanNumberRecord(value) {
|
|
102
|
-
|
|
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;
|
|
46
|
+
return numberRecord(value);
|
|
106
47
|
}
|
|
107
48
|
function cleanTransports(value) {
|
|
108
|
-
|
|
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;
|
|
49
|
+
return transportRecord(value);
|
|
112
50
|
}
|
|
113
51
|
function cleanPrices(value) {
|
|
114
|
-
|
|
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;
|
|
52
|
+
return priceRecord(value);
|
|
126
53
|
}
|
|
127
54
|
export { cleanAccounts, cleanBooleanRecord, cleanManualModels, cleanModelAccountRules, cleanModelOverrides, cleanNumberRecord, cleanPrices, cleanStringArray, cleanTransports, isCredentialRef, normalizeCredentialRef };
|
|
128
55
|
|
package/lib/routes.js
CHANGED
|
@@ -2,6 +2,7 @@ import { bestEffortFailed } from "./best-effort.js";
|
|
|
2
2
|
import { readModelAnalytics } from "./analytics.js";
|
|
3
3
|
import { clearCatalogCache, effectiveModel, modelAvailable, modelVisible, refreshCatalog } from "./catalog.js";
|
|
4
4
|
import { NS } from "./config.js";
|
|
5
|
+
import { accountLabel } from "./credentials.js";
|
|
5
6
|
import { discoverModels } from "./discovery.js";
|
|
6
7
|
import { checkEndpoint } from "./endpoints.js";
|
|
7
8
|
import { priceFor } from "./pricing.js";
|
|
@@ -11,7 +12,7 @@ import { createRouteGuard } from "./route-guard.js";
|
|
|
11
12
|
import { publicConfig } from "./routes-public.js";
|
|
12
13
|
import { clearStatusCache, probeServiceCached } from "./status.js";
|
|
13
14
|
import { cleanAccounts, cleanBooleanRecord, cleanManualModels, cleanModelAccountRules, cleanModelOverrides, cleanNumberRecord, cleanPrices, cleanStringArray, cleanTransports, isCredentialRef, normalizeCredentialRef } from "./routes-values.js";
|
|
14
|
-
import { safeErrorMessage } from "./sanitize.js";
|
|
15
|
+
import { redactText, safeErrorMessage } from "./sanitize.js";
|
|
15
16
|
import { ReasoningEffortId } from "@deepseek-ai/dsh-llm";
|
|
16
17
|
import { credentialRef } from "@deepseek-ai/dsh-credentials";
|
|
17
18
|
// src/routes.ts
|
|
@@ -288,7 +289,7 @@ async function buildState(ctx, deps) {
|
|
|
288
289
|
keyInfo = {
|
|
289
290
|
configured: info.configured,
|
|
290
291
|
writable: info.writable,
|
|
291
|
-
...info.source === void 0 ? {} : { source: info.source }
|
|
292
|
+
...info.source === void 0 ? {} : { source: redactText(info.source) }
|
|
292
293
|
};
|
|
293
294
|
} catch (error) {
|
|
294
295
|
bestEffortFailed("primary credential describe failed", error);
|
|
@@ -312,7 +313,7 @@ async function buildState(ctx, deps) {
|
|
|
312
313
|
ref: config.apiKeyEnv,
|
|
313
314
|
default: true
|
|
314
315
|
}, ...config.accounts.map((account, index) => ({
|
|
315
|
-
label:
|
|
316
|
+
label: accountLabel(account.label, index),
|
|
316
317
|
ref: typeof account.apiKeyEnv === "string" ? account.apiKeyEnv.trim() : "",
|
|
317
318
|
default: false
|
|
318
319
|
}))];
|
|
@@ -342,7 +343,7 @@ async function buildState(ctx, deps) {
|
|
|
342
343
|
accounts.push({
|
|
343
344
|
...slot,
|
|
344
345
|
configured,
|
|
345
|
-
source,
|
|
346
|
+
source: redactText(source),
|
|
346
347
|
active: activeLabel === slot.label,
|
|
347
348
|
coolingUntil: health?.coolingUntil ?? 0,
|
|
348
349
|
mark: health?.reason ?? "",
|
|
@@ -350,6 +351,7 @@ async function buildState(ctx, deps) {
|
|
|
350
351
|
});
|
|
351
352
|
}
|
|
352
353
|
let models = [];
|
|
354
|
+
let providerError;
|
|
353
355
|
try {
|
|
354
356
|
models = (await deps.adapter.listModels(PROVIDER, { unfiltered: true })).map((model) => {
|
|
355
357
|
const meta = effectiveModel(model.id, config);
|
|
@@ -370,7 +372,9 @@ async function buildState(ctx, deps) {
|
|
|
370
372
|
visible: modelVisible(model.id, config)
|
|
371
373
|
};
|
|
372
374
|
});
|
|
373
|
-
} catch {
|
|
375
|
+
} catch (error) {
|
|
376
|
+
bestEffortFailed("state:listModels", error);
|
|
377
|
+
providerError = redactText(error instanceof Error ? error.message : String(error));
|
|
374
378
|
models = [];
|
|
375
379
|
}
|
|
376
380
|
const displayEntry = resolved.find((entry) => (entry.label ?? "default") === activeLabel) ?? resolved[0];
|
|
@@ -382,7 +386,9 @@ async function buildState(ctx, deps) {
|
|
|
382
386
|
models,
|
|
383
387
|
usage: usage ?? null,
|
|
384
388
|
routing: deps.adapter.routingTrace(),
|
|
385
|
-
revision: settingsRevision(ctx)
|
|
389
|
+
revision: settingsRevision(ctx),
|
|
390
|
+
configWritable: typeof getSettings(ctx)?.mutate === "function" && getSettings(ctx)?.writable !== false,
|
|
391
|
+
...providerError === void 0 ? {} : { providerError }
|
|
386
392
|
};
|
|
387
393
|
}
|
|
388
394
|
async function refreshAndState(ctx, deps, res) {
|
|
@@ -440,7 +446,7 @@ function applyRoutes(ctx, web, deps) {
|
|
|
440
446
|
const config = deps.config();
|
|
441
447
|
let providerError;
|
|
442
448
|
const models = await deps.adapter.listModels(PROVIDER, { unfiltered: true }).catch((err) => {
|
|
443
|
-
providerError = err instanceof Error ? err.message : String(err);
|
|
449
|
+
providerError = redactText(err instanceof Error ? err.message : String(err));
|
|
444
450
|
bestEffortFailed("health:listModels", err);
|
|
445
451
|
return [];
|
|
446
452
|
});
|
package/lib/status.js
CHANGED