@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,320 @@
|
|
|
1
|
+
import { bestEffortFailed } from "./best-effort.js";
|
|
2
|
+
import { authorityHostname, isLocalHostname, isLoopbackAddress, isLoopbackHostname } from "./local-host.js";
|
|
3
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { basename, dirname, isAbsolute, resolve } from "node:path";
|
|
7
|
+
import { spawn } from "node:child_process";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
// src/plugin-updater.ts
|
|
10
|
+
const UPDATE_HEADER = "x-dsh-plugin-update";
|
|
11
|
+
const UPDATE_TIMEOUT_MS = 6e5;
|
|
12
|
+
const VERSION_CACHE_MS = 3e5;
|
|
13
|
+
let latestCache;
|
|
14
|
+
function header(request, name) {
|
|
15
|
+
const value = request.headers?.[name];
|
|
16
|
+
return Array.isArray(value) ? value[0] : value;
|
|
17
|
+
}
|
|
18
|
+
function isTrustedUpdateRequest(request) {
|
|
19
|
+
if (header(request, UPDATE_HEADER) !== "1") return false;
|
|
20
|
+
if (!isLoopbackAddress(request.socket?.remoteAddress)) return false;
|
|
21
|
+
const site = header(request, "sec-fetch-site");
|
|
22
|
+
if (site !== void 0 && site !== "same-origin") return false;
|
|
23
|
+
const origin = header(request, "origin");
|
|
24
|
+
const host = header(request, "host");
|
|
25
|
+
if (origin === void 0 || host === void 0) return false;
|
|
26
|
+
try {
|
|
27
|
+
const url = new URL(origin);
|
|
28
|
+
return (url.protocol === "http:" || url.protocol === "https:") && isLoopbackHostname(url.hostname) && url.host === host;
|
|
29
|
+
} catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Read-only status must also come from the local UI: a remote page that reaches
|
|
35
|
+
* the port through DNS rebinding presents its own host name and is refused.
|
|
36
|
+
*/
|
|
37
|
+
function isTrustedReadRequest(request) {
|
|
38
|
+
const host = header(request, "host");
|
|
39
|
+
if (host === void 0 || host === "") return false;
|
|
40
|
+
const hostname = authorityHostname(host);
|
|
41
|
+
if (hostname === void 0 || !isLocalHostname(hostname)) return false;
|
|
42
|
+
const origin = header(request, "origin");
|
|
43
|
+
if (origin === void 0 || origin === "") return true;
|
|
44
|
+
try {
|
|
45
|
+
const url = new URL(origin);
|
|
46
|
+
return (url.protocol === "http:" || url.protocol === "https:") && url.host === host && isLocalHostname(url.hostname);
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function validProfileName(value) {
|
|
52
|
+
return typeof value === "string" && value !== "" && value !== "." && value !== ".." && !value.includes("/") && !value.includes("\\") && !/[\0-\x1f\x7f]/.test(value);
|
|
53
|
+
}
|
|
54
|
+
function profileNameFromArgv(argv) {
|
|
55
|
+
for (let index = 2; index < argv.length; index += 1) {
|
|
56
|
+
if (argv[index] === "--profile") return argv[index + 1];
|
|
57
|
+
if (argv[index]?.startsWith("--profile=")) return argv[index].slice(10);
|
|
58
|
+
}
|
|
59
|
+
return argv[2] === "web" ? "web" : void 0;
|
|
60
|
+
}
|
|
61
|
+
function findDshCliEntry() {
|
|
62
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
63
|
+
const fromArgv = process.argv[1];
|
|
64
|
+
if (fromArgv !== void 0 && fromArgv !== "") candidates.add(fromArgv.startsWith("file:") ? fileURLToPath(fromArgv) : resolve(process.cwd(), fromArgv));
|
|
65
|
+
for (const value of process.argv.slice(2)) if (/[\\/](?:app\.asar[\\/])?dsh[\\/]?$/i.test(value)) candidates.add(resolve(value));
|
|
66
|
+
for (const candidate of candidates) {
|
|
67
|
+
if (!existsSync(candidate)) continue;
|
|
68
|
+
let entry = candidate;
|
|
69
|
+
try {
|
|
70
|
+
if (statSync(candidate).isDirectory()) entry = resolve(candidate, "package.json");
|
|
71
|
+
} catch {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
for (let directory = dirname(entry);; directory = dirname(directory)) {
|
|
75
|
+
const manifestPaths = [resolve(directory, "package.json"), resolve(directory, "node_modules", "@deepseek-ai", "dsh", "package.json")];
|
|
76
|
+
for (const manifestPath of manifestPaths) {
|
|
77
|
+
if (!existsSync(manifestPath)) continue;
|
|
78
|
+
try {
|
|
79
|
+
const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
|
|
80
|
+
const bin = typeof manifest.bin === "string" ? manifest.bin : typeof manifest.bin === "object" && manifest.bin !== null ? manifest.bin.dsh : void 0;
|
|
81
|
+
if (manifest.name === "@deepseek-ai/dsh" && typeof bin === "string" && !isAbsolute(bin)) {
|
|
82
|
+
const cli = resolve(dirname(manifestPath), bin);
|
|
83
|
+
if (existsSync(cli)) return cli;
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
bestEffortFailed("package manifest scan failed", error);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (dirname(directory) === directory) break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function profileFromArgv(argv) {
|
|
94
|
+
for (const value of argv) {
|
|
95
|
+
const match = /[\\/]\.dsh[\\/]profiles[\\/]([^\\/]+)[\\/]?$/i.exec(value);
|
|
96
|
+
if (match !== null && validProfileName(match[1])) return {
|
|
97
|
+
dir: resolve(value),
|
|
98
|
+
name: match[1]
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const RUNTIME_CACHE_MS = 3e4;
|
|
103
|
+
let runtimeCache;
|
|
104
|
+
function runtime() {
|
|
105
|
+
if (runtimeCache !== void 0 && Date.now() - runtimeCache.at < RUNTIME_CACHE_MS) return runtimeCache.value;
|
|
106
|
+
const fromArgv = profileFromArgv(process.argv);
|
|
107
|
+
const profileDir = resolve(process.env.DSH_PROFILE_DIR ?? fromArgv?.dir ?? resolve(homedir(), ".dsh", "profiles", "web"));
|
|
108
|
+
const selected = process.env.DSH_PROFILE_NAME ?? profileNameFromArgv(process.argv) ?? fromArgv?.name;
|
|
109
|
+
const profileName = validProfileName(selected) ? selected : validProfileName(basename(profileDir)) ? basename(profileDir) : "web";
|
|
110
|
+
const cliEntry = findDshCliEntry();
|
|
111
|
+
const value = cliEntry === void 0 ? {
|
|
112
|
+
profileName,
|
|
113
|
+
profileDir
|
|
114
|
+
} : {
|
|
115
|
+
profileName,
|
|
116
|
+
profileDir,
|
|
117
|
+
cliEntry
|
|
118
|
+
};
|
|
119
|
+
runtimeCache = {
|
|
120
|
+
at: Date.now(),
|
|
121
|
+
value
|
|
122
|
+
};
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
function parseSemver(value) {
|
|
126
|
+
const match = /^v?(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/.exec(value);
|
|
127
|
+
if (match === null) return void 0;
|
|
128
|
+
return {
|
|
129
|
+
core: [
|
|
130
|
+
Number(match[1]),
|
|
131
|
+
Number(match[2]),
|
|
132
|
+
Number(match[3])
|
|
133
|
+
],
|
|
134
|
+
prerelease: match[4]?.split(".") ?? []
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function comparePrerelease(left, right) {
|
|
138
|
+
if (left.length === 0 || right.length === 0) return left.length === right.length ? 0 : left.length === 0 ? 1 : -1;
|
|
139
|
+
const length = Math.max(left.length, right.length);
|
|
140
|
+
for (let index = 0; index < length; index += 1) {
|
|
141
|
+
const a = left[index];
|
|
142
|
+
const b = right[index];
|
|
143
|
+
if (a === void 0 || b === void 0) return a === b ? 0 : a === void 0 ? -1 : 1;
|
|
144
|
+
if (a === b) continue;
|
|
145
|
+
const aNumeric = /^\d+$/.test(a);
|
|
146
|
+
const bNumeric = /^\d+$/.test(b);
|
|
147
|
+
if (aNumeric && bNumeric) {
|
|
148
|
+
const aNumber = BigInt(a);
|
|
149
|
+
const bNumber = BigInt(b);
|
|
150
|
+
if (aNumber !== bNumber) return aNumber > bNumber ? 1 : -1;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (aNumeric !== bNumeric) return aNumeric ? -1 : 1;
|
|
154
|
+
return a > b ? 1 : -1;
|
|
155
|
+
}
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
function isNewerVersion(currentValue, candidateValue) {
|
|
159
|
+
const current = parseSemver(currentValue);
|
|
160
|
+
const candidate = parseSemver(candidateValue);
|
|
161
|
+
if (current === void 0 || candidate === void 0) return false;
|
|
162
|
+
for (let index = 0; index < 3; index += 1) if (candidate.core[index] !== current.core[index]) return candidate.core[index] > current.core[index];
|
|
163
|
+
return comparePrerelease(candidate.prerelease, current.prerelease) > 0;
|
|
164
|
+
}
|
|
165
|
+
async function latestVersion(packageName, registry) {
|
|
166
|
+
if (latestCache?.packageName === packageName && latestCache.registry === registry && Date.now() < latestCache.expiresAt) return latestCache.version;
|
|
167
|
+
try {
|
|
168
|
+
const response = await fetch(`${registry.replace(/\/$/, "")}/${encodeURIComponent(packageName)}/latest`, { signal: AbortSignal.timeout(8e3) });
|
|
169
|
+
if (!response.ok) return void 0;
|
|
170
|
+
const value = await response.json();
|
|
171
|
+
if (typeof value.version !== "string" || value.version === "") return void 0;
|
|
172
|
+
latestCache = {
|
|
173
|
+
packageName,
|
|
174
|
+
registry,
|
|
175
|
+
version: value.version,
|
|
176
|
+
expiresAt: Date.now() + VERSION_CACHE_MS
|
|
177
|
+
};
|
|
178
|
+
return value.version;
|
|
179
|
+
} catch {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async function currentVersion(manifestUrl) {
|
|
184
|
+
const value = JSON.parse(await readFile(manifestUrl, "utf8"));
|
|
185
|
+
if (typeof value.version !== "string" || value.version === "") throw new Error("Cannot read current plugin version.");
|
|
186
|
+
return value.version;
|
|
187
|
+
}
|
|
188
|
+
async function status(options, target) {
|
|
189
|
+
const current = await currentVersion(options.manifestUrl);
|
|
190
|
+
const latest = await latestVersion(options.packageName, options.registry ?? "https://registry.npmjs.org");
|
|
191
|
+
return {
|
|
192
|
+
packageName: options.packageName,
|
|
193
|
+
currentVersion: current,
|
|
194
|
+
...latest === void 0 ? {} : { latestVersion: latest },
|
|
195
|
+
latestCheckFailed: latest === void 0,
|
|
196
|
+
updateAvailable: latest !== void 0 && isNewerVersion(current, latest),
|
|
197
|
+
profileName: target.profileName,
|
|
198
|
+
canAutoUpdate: target.cliEntry !== void 0
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
async function installExact(target, packageSpec, options) {
|
|
202
|
+
if (target.cliEntry === void 0) throw new Error("Automatic update is unavailable in this runtime.");
|
|
203
|
+
await new Promise((resolvePromise, reject) => {
|
|
204
|
+
const child = spawn(process.execPath, [
|
|
205
|
+
target.cliEntry,
|
|
206
|
+
"plugin",
|
|
207
|
+
"--profile",
|
|
208
|
+
target.profileName,
|
|
209
|
+
"add",
|
|
210
|
+
"--config.minimumReleaseAge=0",
|
|
211
|
+
packageSpec,
|
|
212
|
+
`--registry=${options.registry ?? "https://registry.npmjs.org/"}`
|
|
213
|
+
], {
|
|
214
|
+
cwd: target.profileDir,
|
|
215
|
+
windowsHide: true,
|
|
216
|
+
stdio: [
|
|
217
|
+
"ignore",
|
|
218
|
+
"pipe",
|
|
219
|
+
"pipe"
|
|
220
|
+
],
|
|
221
|
+
env: {
|
|
222
|
+
...process.env,
|
|
223
|
+
NO_COLOR: "1",
|
|
224
|
+
ELECTRON_RUN_AS_NODE: "1"
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
let detail = "";
|
|
228
|
+
child.stdout?.on("data", (chunk) => {
|
|
229
|
+
detail = (detail + String(chunk)).slice(-4e3);
|
|
230
|
+
});
|
|
231
|
+
child.stderr?.on("data", (chunk) => {
|
|
232
|
+
detail = (detail + String(chunk)).slice(-4e3);
|
|
233
|
+
});
|
|
234
|
+
const timer = setTimeout(() => {
|
|
235
|
+
child.kill();
|
|
236
|
+
reject(/* @__PURE__ */ new Error("Update timed out; use the normal DSH update flow."));
|
|
237
|
+
}, UPDATE_TIMEOUT_MS);
|
|
238
|
+
child.once("error", (error) => {
|
|
239
|
+
clearTimeout(timer);
|
|
240
|
+
reject(error);
|
|
241
|
+
});
|
|
242
|
+
child.once("exit", (code) => {
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
if (code === 0) resolvePromise();
|
|
245
|
+
else reject(new Error(detail.trim() || `Update exited with code ${String(code)}.`));
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function json(response, statusCode, value) {
|
|
250
|
+
response.writeHead(statusCode, {
|
|
251
|
+
"content-type": "application/json; charset=utf-8",
|
|
252
|
+
"cache-control": "no-store"
|
|
253
|
+
});
|
|
254
|
+
response.end(JSON.stringify(value));
|
|
255
|
+
}
|
|
256
|
+
function registerPluginUpdater(ctx, options) {
|
|
257
|
+
const host = ctx;
|
|
258
|
+
let installing = false;
|
|
259
|
+
return host.webServer.register({
|
|
260
|
+
kind: "exact",
|
|
261
|
+
path: options.endpoint,
|
|
262
|
+
handler: async (request, response) => {
|
|
263
|
+
try {
|
|
264
|
+
const target = runtime();
|
|
265
|
+
if (request.method === "GET" || request.method === "HEAD") {
|
|
266
|
+
if (!isTrustedReadRequest(request)) {
|
|
267
|
+
json(response, 403, { error: "Rejected non-local update status request." });
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const payload = await status(options, target);
|
|
271
|
+
response.writeHead(200, {
|
|
272
|
+
"content-type": "application/json; charset=utf-8",
|
|
273
|
+
"cache-control": "no-store"
|
|
274
|
+
});
|
|
275
|
+
response.end(request.method === "HEAD" ? void 0 : JSON.stringify(payload));
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (request.method !== "POST") {
|
|
279
|
+
response.writeHead(405, { allow: "GET, HEAD, POST" });
|
|
280
|
+
response.end();
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (!isTrustedUpdateRequest(request)) {
|
|
284
|
+
json(response, 403, { error: "Rejected non-local or cross-origin update request." });
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
if (installing) {
|
|
288
|
+
json(response, 409, { error: "This plugin is already updating." });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
installing = true;
|
|
292
|
+
try {
|
|
293
|
+
const before = await status(options, target);
|
|
294
|
+
if (before.latestVersion === void 0) {
|
|
295
|
+
json(response, 503, { error: "The latest version is temporarily unavailable." });
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (!before.updateAvailable) {
|
|
299
|
+
json(response, 200, before);
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
await installExact(target, `${options.packageName}@${before.latestVersion}`, options);
|
|
303
|
+
json(response, 200, {
|
|
304
|
+
...before,
|
|
305
|
+
updatedVersion: before.latestVersion,
|
|
306
|
+
restartRequired: true
|
|
307
|
+
});
|
|
308
|
+
} finally {
|
|
309
|
+
installing = false;
|
|
310
|
+
}
|
|
311
|
+
} catch (error) {
|
|
312
|
+
ctx.logger("llm-ollama-cloud").warn(`plugin updater failed: ${String(error)}`);
|
|
313
|
+
json(response, 503, { error: "Plugin update failed; see server logs." });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
export { isNewerVersion, isTrustedReadRequest, isTrustedUpdateRequest, registerPluginUpdater };
|
|
319
|
+
|
|
320
|
+
//# sourceMappingURL=plugin-updater.js.map
|
package/lib/pricing.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { GENERATED_MODELS } from "./models.generated.js";
|
|
2
|
+
// src/pricing.ts
|
|
3
|
+
/** `gpt-oss:120b` → `gpt-oss`; used as the second lookup key. */
|
|
4
|
+
function baseIdOf(id) {
|
|
5
|
+
const at = id.indexOf(":");
|
|
6
|
+
return at < 0 ? id : id.slice(0, at);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Resolve the generated metadata for a priced id: exact tag, base tag, or the
|
|
10
|
+
* first tagged variant when only a base id is known.
|
|
11
|
+
*/
|
|
12
|
+
function metaForPrice(id) {
|
|
13
|
+
const exact = GENERATED_MODELS[id];
|
|
14
|
+
if (exact !== void 0) return exact;
|
|
15
|
+
const base = baseIdOf(id);
|
|
16
|
+
if (base !== id) return GENERATED_MODELS[base];
|
|
17
|
+
const prefix = `${id}:`;
|
|
18
|
+
for (const [key, meta] of Object.entries(GENERATED_MODELS)) if (key.startsWith(prefix)) return meta;
|
|
19
|
+
}
|
|
20
|
+
function priceFor(id, overrides = {}) {
|
|
21
|
+
const meta = metaForPrice(id);
|
|
22
|
+
const override = overrides[id] ?? overrides[baseIdOf(id)];
|
|
23
|
+
const cost = meta?.cost;
|
|
24
|
+
const input = override?.input ?? cost?.input ?? 0;
|
|
25
|
+
const output = override?.output ?? cost?.output ?? 0;
|
|
26
|
+
const cacheRead = override?.cacheRead ?? cost?.cacheRead ?? 0;
|
|
27
|
+
const priced = override !== void 0 || cost !== void 0 && (input > 0 || output > 0 || cacheRead > 0);
|
|
28
|
+
return {
|
|
29
|
+
input,
|
|
30
|
+
output,
|
|
31
|
+
cacheRead,
|
|
32
|
+
free: meta?.free === true,
|
|
33
|
+
priced
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Ollama's off-peak window: outside 12:00–18:00 UTC on weekdays, plus the whole
|
|
38
|
+
* weekend. Published estimates, not a contract.
|
|
39
|
+
*/
|
|
40
|
+
function isOffPeak(at = Date.now()) {
|
|
41
|
+
const date = new Date(at);
|
|
42
|
+
const day = date.getUTCDay();
|
|
43
|
+
if (day === 0 || day === 6) return true;
|
|
44
|
+
const hour = date.getUTCHours();
|
|
45
|
+
return hour < 12 || hour >= 18;
|
|
46
|
+
}
|
|
47
|
+
function estimateCost(input) {
|
|
48
|
+
const price = priceFor(input.model, input.priceOverrides);
|
|
49
|
+
const meta = metaForPrice(input.model);
|
|
50
|
+
const override = input.priceOverrides?.[input.model] ?? input.priceOverrides?.[baseIdOf(input.model)];
|
|
51
|
+
const offPeakAvailable = meta?.cost.offPeakInput !== void 0 || meta?.cost.offPeakOutput !== void 0 || meta?.cost.offPeakCacheRead !== void 0;
|
|
52
|
+
const offPeak = isOffPeak(input.at) && offPeakAvailable;
|
|
53
|
+
const inputRate = override?.input ?? (offPeak ? meta?.cost.offPeakInput ?? price.input : price.input);
|
|
54
|
+
const outputRate = override?.output ?? (offPeak ? meta?.cost.offPeakOutput ?? price.output : price.output);
|
|
55
|
+
const cacheRate = override?.cacheRead ?? (offPeak ? meta?.cost.offPeakCacheRead ?? price.cacheRead : price.cacheRead);
|
|
56
|
+
const usd = (input.inputTokens * inputRate + input.outputTokens * outputRate + (input.cacheReadTokens ?? 0) * cacheRate) / 1e6;
|
|
57
|
+
return {
|
|
58
|
+
usd: price.priced ? usd : 0,
|
|
59
|
+
priced: price.priced,
|
|
60
|
+
free: price.free,
|
|
61
|
+
offPeak
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function formatPrice(price) {
|
|
65
|
+
if (price.free) return "FREE";
|
|
66
|
+
if (!price.priced) return "price unknown";
|
|
67
|
+
const money = (value) => value.toFixed(2).replace(/\.00$/, "");
|
|
68
|
+
return `$${money(price.input)}/$${money(price.output)} per 1M`;
|
|
69
|
+
}
|
|
70
|
+
function formatUsd(value) {
|
|
71
|
+
if (!Number.isFinite(value)) return "$0.00";
|
|
72
|
+
if (Math.abs(value) >= 1) return `$${value.toFixed(2)}`;
|
|
73
|
+
if (Math.abs(value) >= .01) return `$${value.toFixed(3)}`;
|
|
74
|
+
return `$${value.toFixed(4)}`;
|
|
75
|
+
}
|
|
76
|
+
export { baseIdOf, estimateCost, formatPrice, formatUsd, isOffPeak, metaForPrice, priceFor };
|
|
77
|
+
|
|
78
|
+
//# sourceMappingURL=pricing.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// src/provider-ids.ts
|
|
2
|
+
/** Provider route owned by this plugin. */
|
|
3
|
+
const PROVIDER = "ollama-cloud";
|
|
4
|
+
/** Historical ids kept only for analytics continuity. */
|
|
5
|
+
const LEGACY_PROVIDERS = ["ollama"];
|
|
6
|
+
const PLUGIN_AND_LEGACY_PROVIDERS = /* @__PURE__ */ new Set([PROVIDER, ...LEGACY_PROVIDERS]);
|
|
7
|
+
export { LEGACY_PROVIDERS, PLUGIN_AND_LEGACY_PROVIDERS, PROVIDER };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=provider-ids.js.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// src/quota-model.ts
|
|
2
|
+
function bar(key, label, usedPercent, extra = {}) {
|
|
3
|
+
const labelKey = key === "included" ? "quotaIncludedCredits" : key === "fiveHour" ? "quotaFiveHour" : key === "weekly" ? "quotaWeekly" : void 0;
|
|
4
|
+
const used = Math.max(0, Math.min(100, usedPercent));
|
|
5
|
+
return {
|
|
6
|
+
key,
|
|
7
|
+
label,
|
|
8
|
+
...labelKey === void 0 ? {} : { labelKey },
|
|
9
|
+
usedPercent: used,
|
|
10
|
+
remainingPercent: 100 - used,
|
|
11
|
+
...extra.resetsAt === void 0 ? {} : { resetsAt: extra.resetsAt },
|
|
12
|
+
...extra.status === void 0 ? {} : { status: extra.status },
|
|
13
|
+
...extra.detail === void 0 ? {} : { detail: extra.detail },
|
|
14
|
+
...extra.detailValues === void 0 ? {} : { detailValues: extra.detailValues }
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function legacyBar(key, label, window) {
|
|
18
|
+
if (window === void 0) return void 0;
|
|
19
|
+
return bar(key, label, window.percentUsed, {
|
|
20
|
+
...window.resetsAt === void 0 ? {} : { resetsAt: window.resetsAt },
|
|
21
|
+
...window.status === void 0 ? {} : { status: window.status }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Bars for the sidebar card and the settings page, newest plan format first. */
|
|
25
|
+
function quotaBars(report) {
|
|
26
|
+
if (report === void 0) return [];
|
|
27
|
+
if (report.kind === "credits" && report.credits !== void 0) {
|
|
28
|
+
const credits = report.credits;
|
|
29
|
+
return [bar("included", "Included credits", credits.includedAllowance > 0 ? (credits.includedAllowance - credits.includedRemaining) / credits.includedAllowance * 100 : 0, {
|
|
30
|
+
detail: `$${credits.includedRemaining.toFixed(2)} of $${credits.includedAllowance.toFixed(2)} left` + (credits.purchased > 0 ? ` · $${credits.purchased.toFixed(2)} purchased` : ""),
|
|
31
|
+
detailValues: {
|
|
32
|
+
remaining: credits.includedRemaining,
|
|
33
|
+
allowance: credits.includedAllowance,
|
|
34
|
+
purchased: credits.purchased
|
|
35
|
+
},
|
|
36
|
+
...credits.periodUntil === void 0 ? {} : { resetsAt: credits.periodUntil }
|
|
37
|
+
})];
|
|
38
|
+
}
|
|
39
|
+
return [legacyBar("fiveHour", "5-hour window", report.legacy?.fiveHour), legacyBar("weekly", "Weekly window", report.legacy?.weekly)].filter((entry) => entry !== void 0);
|
|
40
|
+
}
|
|
41
|
+
function windowRows(report) {
|
|
42
|
+
if (report === void 0) return [];
|
|
43
|
+
return [
|
|
44
|
+
report.windows.d24 === void 0 ? void 0 : {
|
|
45
|
+
key: "24h",
|
|
46
|
+
label: "24 hours",
|
|
47
|
+
labelKey: "last24h",
|
|
48
|
+
totals: report.windows.d24
|
|
49
|
+
},
|
|
50
|
+
report.windows.d7 === void 0 ? void 0 : {
|
|
51
|
+
key: "7d",
|
|
52
|
+
label: "7 days",
|
|
53
|
+
labelKey: "last7d",
|
|
54
|
+
totals: report.windows.d7
|
|
55
|
+
},
|
|
56
|
+
report.windows.d30 === void 0 ? void 0 : {
|
|
57
|
+
key: "30d",
|
|
58
|
+
label: "30 days",
|
|
59
|
+
labelKey: "last30d",
|
|
60
|
+
totals: report.windows.d30
|
|
61
|
+
}
|
|
62
|
+
].filter((entry) => entry !== void 0);
|
|
63
|
+
}
|
|
64
|
+
export { quotaBars, windowRows };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=quota-model.js.map
|
package/lib/reasoning.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// src/reasoning.ts
|
|
2
|
+
const EFFORT_ORDER = [
|
|
3
|
+
"off",
|
|
4
|
+
"minimal",
|
|
5
|
+
"low",
|
|
6
|
+
"medium",
|
|
7
|
+
"high",
|
|
8
|
+
"xhigh",
|
|
9
|
+
"max"
|
|
10
|
+
];
|
|
11
|
+
const FAMILY_DEFAULTS = [
|
|
12
|
+
{
|
|
13
|
+
prefix: "glm-5.3",
|
|
14
|
+
effort: "max"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
prefix: "glm-5.2",
|
|
18
|
+
effort: "max"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
prefix: "kimi-k3",
|
|
22
|
+
effort: "max"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
prefix: "kimi-k2",
|
|
26
|
+
effort: "high"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
prefix: "deepseek-v4",
|
|
30
|
+
effort: "high"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
prefix: "minimax-m3",
|
|
34
|
+
effort: "high"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
prefix: "gpt-oss",
|
|
38
|
+
effort: "medium"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
prefix: "nemotron",
|
|
42
|
+
effort: "low"
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
function normalizeEfforts(levels) {
|
|
46
|
+
const seen = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const level of levels) {
|
|
48
|
+
const clean = level.trim().toLowerCase();
|
|
49
|
+
if (clean !== "") seen.add(clean);
|
|
50
|
+
}
|
|
51
|
+
const ordered = EFFORT_ORDER.filter((level) => seen.has(level));
|
|
52
|
+
const extra = [...seen].filter((level) => !EFFORT_ORDER.includes(level)).sort();
|
|
53
|
+
return [...ordered, ...extra];
|
|
54
|
+
}
|
|
55
|
+
function defaultEffortFor(id, levels) {
|
|
56
|
+
const family = id.split(":")[0]?.toLowerCase() ?? "";
|
|
57
|
+
for (const entry of FAMILY_DEFAULTS) if (family.startsWith(entry.prefix) && levels.includes(entry.effort)) return entry.effort;
|
|
58
|
+
}
|
|
59
|
+
/** Normalize boolean switches and level ladders, then apply the known family default. */
|
|
60
|
+
function normalizeThinking(id, levels, toggle, fallback) {
|
|
61
|
+
const reasoning = normalizeEfforts(levels);
|
|
62
|
+
if (reasoning.length === 0 && toggle) reasoning.push("off", "high");
|
|
63
|
+
const candidate = fallback !== void 0 && reasoning.includes(fallback) ? fallback : defaultEffortFor(id, reasoning) ?? (toggle && reasoning.includes("high") ? "high" : void 0);
|
|
64
|
+
return {
|
|
65
|
+
reasoning,
|
|
66
|
+
...candidate === void 0 ? {} : { defaultReasoning: candidate }
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export { defaultEffortFor, normalizeEfforts, normalizeThinking };
|
|
70
|
+
|
|
71
|
+
//# sourceMappingURL=reasoning.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
interface RequestOutcome {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
latencyMs: number;
|
|
4
|
+
/** Request start; the log uses it as the event timestamp. */
|
|
5
|
+
at?: number;
|
|
6
|
+
ttftMs?: number;
|
|
7
|
+
error?: string;
|
|
8
|
+
}
|
|
9
|
+
interface ModelRequestHeatmap {
|
|
10
|
+
provider: string;
|
|
11
|
+
model: string;
|
|
12
|
+
total: number;
|
|
13
|
+
hours: number[];
|
|
14
|
+
grid: number[][];
|
|
15
|
+
}
|
|
16
|
+
interface AccountRequestHeatmap {
|
|
17
|
+
account: string;
|
|
18
|
+
total: number;
|
|
19
|
+
errors: number;
|
|
20
|
+
avgLatencyMs: number | null;
|
|
21
|
+
avgTtftMs: number | null;
|
|
22
|
+
hours: number[];
|
|
23
|
+
grid: number[][];
|
|
24
|
+
}
|
|
25
|
+
interface RequestHeatmap {
|
|
26
|
+
since: number;
|
|
27
|
+
total: number;
|
|
28
|
+
/** Requests per hour of day, local time. */
|
|
29
|
+
hours: number[];
|
|
30
|
+
/** Monday-first weekday × hour grid, local time. */
|
|
31
|
+
grid: number[][];
|
|
32
|
+
firstAt: number | null;
|
|
33
|
+
lastAt: number | null;
|
|
34
|
+
models: ModelRequestHeatmap[];
|
|
35
|
+
accounts: AccountRequestHeatmap[];
|
|
36
|
+
}
|
|
37
|
+
export { AccountRequestHeatmap, ModelRequestHeatmap, RequestHeatmap, RequestOutcome };
|
|
38
|
+
//# sourceMappingURL=request-log.d.ts.map
|