@goodandready/dsh-ollama-cloud-provider 0.1.27 → 0.1.29
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 +11 -0
- package/README.md +4 -20
- package/README.ru.md +4 -4
- package/README.zh.md +4 -4
- package/lib/client.js +112 -362
- package/lib/index.d.ts +1 -19
- package/lib/index.js +34 -569
- package/package.json +2 -2
package/lib/index.js
CHANGED
|
@@ -122,7 +122,7 @@ const DEFAULT_API_BASE = "https://ollama.com";
|
|
|
122
122
|
const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
|
|
123
123
|
const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
|
|
124
124
|
const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
|
|
125
|
-
const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.
|
|
125
|
+
const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.29";
|
|
126
126
|
const DEFAULT_CONTEXT_WINDOW = 131072;
|
|
127
127
|
const DEFAULT_MAX_TOKENS = 32768;
|
|
128
128
|
const DEFAULT_GENERATE_MAX_TOKENS = 131072;
|
|
@@ -284,23 +284,23 @@ function manualModelList(value) {
|
|
|
284
284
|
}
|
|
285
285
|
return out;
|
|
286
286
|
}
|
|
287
|
-
function recordOf
|
|
287
|
+
function recordOf(value) {
|
|
288
288
|
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
|
|
289
289
|
}
|
|
290
290
|
function numberRecord(value) {
|
|
291
291
|
const out = {};
|
|
292
|
-
for (const [key, entry] of Object.entries(recordOf
|
|
292
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
|
|
293
293
|
return out;
|
|
294
294
|
}
|
|
295
295
|
function booleanRecord(value) {
|
|
296
296
|
const out = {};
|
|
297
|
-
for (const [key, entry] of Object.entries(recordOf
|
|
297
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "boolean") out[key] = entry;
|
|
298
298
|
return out;
|
|
299
299
|
}
|
|
300
300
|
function priceRecord(value) {
|
|
301
301
|
const out = {};
|
|
302
|
-
for (const [key, entry] of Object.entries(recordOf
|
|
303
|
-
const raw = recordOf
|
|
302
|
+
for (const [key, entry] of Object.entries(recordOf(value))) {
|
|
303
|
+
const raw = recordOf(entry);
|
|
304
304
|
const price = {};
|
|
305
305
|
if (typeof raw.input === "number" && raw.input >= 0) price.input = raw.input;
|
|
306
306
|
if (typeof raw.output === "number" && raw.output >= 0) price.output = raw.output;
|
|
@@ -312,14 +312,14 @@ function priceRecord(value) {
|
|
|
312
312
|
const TRANSPORTS$1 = /* @__PURE__ */ new Set(["native", "openai-chat"]);
|
|
313
313
|
function transportRecord(value) {
|
|
314
314
|
const out = {};
|
|
315
|
-
for (const [key, entry] of Object.entries(recordOf
|
|
315
|
+
for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "string" && TRANSPORTS$1.has(entry)) out[key] = entry;
|
|
316
316
|
return out;
|
|
317
317
|
}
|
|
318
318
|
function ruleList(value) {
|
|
319
319
|
if (!Array.isArray(value)) return [];
|
|
320
320
|
const out = [];
|
|
321
321
|
for (const entry of value) {
|
|
322
|
-
const raw = recordOf
|
|
322
|
+
const raw = recordOf(entry);
|
|
323
323
|
const pattern = typeof raw.pattern === "string" ? raw.pattern.trim() : "";
|
|
324
324
|
const account = typeof raw.account === "string" ? raw.account.trim() : "";
|
|
325
325
|
if (pattern !== "" && account !== "") out.push({
|
|
@@ -343,7 +343,7 @@ function resolveConfig(config) {
|
|
|
343
343
|
const numCtx = integerInRange(config.numCtx, 1, 1e7);
|
|
344
344
|
const stop = nonEmptyStrings(config.stop);
|
|
345
345
|
return {
|
|
346
|
-
apiKeyEnv: typeof config.apiKeyEnv === "string" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
|
|
346
|
+
apiKeyEnv: typeof config.apiKeyEnv === "string" && config.apiKeyEnv.trim() !== "" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
|
|
347
347
|
...apiKey === void 0 ? {} : { apiKey },
|
|
348
348
|
apiBase: typeof config.apiBase === "string" && config.apiBase !== "" ? config.apiBase : DEFAULT_API_BASE,
|
|
349
349
|
openAiBase: typeof config.openAiBase === "string" && config.openAiBase !== "" ? config.openAiBase : DEFAULT_OPENAI_BASE,
|
|
@@ -954,123 +954,6 @@ const GENERATED_MODELS = {
|
|
|
954
954
|
}
|
|
955
955
|
}
|
|
956
956
|
};
|
|
957
|
-
// src/routing.ts
|
|
958
|
-
function trim(url) {
|
|
959
|
-
return url.replace(/\/+$/, "");
|
|
960
|
-
}
|
|
961
|
-
function rootFor(config) {
|
|
962
|
-
return trim(config.apiBase);
|
|
963
|
-
}
|
|
964
|
-
function modelsUrlFor(config) {
|
|
965
|
-
return `${rootFor(config)}/v1/models`;
|
|
966
|
-
}
|
|
967
|
-
function tagsUrlFor(config) {
|
|
968
|
-
return `${rootFor(config)}/api/tags`;
|
|
969
|
-
}
|
|
970
|
-
/** Endpoint for one model and one wire protocol. */
|
|
971
|
-
function endpointFor(transport, config) {
|
|
972
|
-
return transport === "openai-chat" ? `${trim(config.openAiBase)}/chat/completions` : `${rootFor(config)}/api/chat`;
|
|
973
|
-
}
|
|
974
|
-
// src/model-probe.ts
|
|
975
|
-
const PROBE_TTL_MS = 3e5;
|
|
976
|
-
const cache$2 = /* @__PURE__ */ new Map();
|
|
977
|
-
function keyOf(config, model) {
|
|
978
|
-
return `${rootFor(config)}|${model}`;
|
|
979
|
-
}
|
|
980
|
-
function isLocalEndpoint(config) {
|
|
981
|
-
if (config.apiKeyEnv === "") return true;
|
|
982
|
-
try {
|
|
983
|
-
const host = new URL(config.apiBase).hostname;
|
|
984
|
-
return host === "127.0.0.1" || host === "localhost" || host === "::1" || host === "[::1]";
|
|
985
|
-
} catch {
|
|
986
|
-
return false;
|
|
987
|
-
}
|
|
988
|
-
}
|
|
989
|
-
function contextOf(info) {
|
|
990
|
-
let best;
|
|
991
|
-
for (const [key, value] of Object.entries(info)) {
|
|
992
|
-
if (!key.endsWith(".context_length")) continue;
|
|
993
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) continue;
|
|
994
|
-
best = best === void 0 ? value : Math.max(best, value);
|
|
995
|
-
}
|
|
996
|
-
return best === void 0 ? void 0 : Math.trunc(best);
|
|
997
|
-
}
|
|
998
|
-
function probeOf(body) {
|
|
999
|
-
if (typeof body !== "object" || body === null || Array.isArray(body)) return void 0;
|
|
1000
|
-
const record = body;
|
|
1001
|
-
const info = typeof record.model_info === "object" && record.model_info !== null ? record.model_info : {};
|
|
1002
|
-
const capabilities = Array.isArray(record.capabilities) ? record.capabilities.filter((entry) => typeof entry === "string") : [];
|
|
1003
|
-
const thinking = capabilities.includes("thinking");
|
|
1004
|
-
const vision = capabilities.includes("vision");
|
|
1005
|
-
const contextWindow = contextOf(info);
|
|
1006
|
-
const input = vision ? ["text", "image"] : ["text"];
|
|
1007
|
-
return {
|
|
1008
|
-
...contextWindow === void 0 ? {} : { contextWindow },
|
|
1009
|
-
input,
|
|
1010
|
-
reasoning: thinking ? ["off", "high"] : [],
|
|
1011
|
-
reasoningToggle: thinking,
|
|
1012
|
-
tools: capabilities.includes("tools"),
|
|
1013
|
-
available: true
|
|
1014
|
-
};
|
|
1015
|
-
}
|
|
1016
|
-
/** Probe one local model via `/api/show`, caching successful answers for five minutes. */
|
|
1017
|
-
async function probeModelOutcome(config, model, fetchImpl = fetch) {
|
|
1018
|
-
if (!isLocalEndpoint(config)) return { missing: false };
|
|
1019
|
-
const key = keyOf(config, model);
|
|
1020
|
-
const hit = cache$2.get(key);
|
|
1021
|
-
if (hit !== void 0 && Date.now() - hit.at < PROBE_TTL_MS) return {
|
|
1022
|
-
probe: hit.value,
|
|
1023
|
-
missing: false
|
|
1024
|
-
};
|
|
1025
|
-
try {
|
|
1026
|
-
const response = await fetchImpl(`${rootFor(config)}/api/show`, {
|
|
1027
|
-
method: "POST",
|
|
1028
|
-
headers: {
|
|
1029
|
-
accept: "application/json",
|
|
1030
|
-
"content-type": "application/json",
|
|
1031
|
-
"user-agent": config.userAgent
|
|
1032
|
-
},
|
|
1033
|
-
body: JSON.stringify({ model }),
|
|
1034
|
-
signal: AbortSignal.timeout(1e4)
|
|
1035
|
-
});
|
|
1036
|
-
if (response.status === 404 || response.status === 410) return { missing: true };
|
|
1037
|
-
if (!response.ok) return { missing: false };
|
|
1038
|
-
const value = probeOf(await response.json());
|
|
1039
|
-
if (value === void 0) return { missing: false };
|
|
1040
|
-
cache$2.set(key, {
|
|
1041
|
-
at: Date.now(),
|
|
1042
|
-
value
|
|
1043
|
-
});
|
|
1044
|
-
return {
|
|
1045
|
-
probe: value,
|
|
1046
|
-
missing: false
|
|
1047
|
-
};
|
|
1048
|
-
} catch {
|
|
1049
|
-
return { missing: false };
|
|
1050
|
-
}
|
|
1051
|
-
}
|
|
1052
|
-
async function probeModel(config, model, fetchImpl = fetch) {
|
|
1053
|
-
return (await probeModelOutcome(config, model, fetchImpl)).probe;
|
|
1054
|
-
}
|
|
1055
|
-
/** Clear all probe entries, or only entries for one endpoint. */
|
|
1056
|
-
function clearModelProbeCache(baseURL) {
|
|
1057
|
-
if (baseURL === void 0) {
|
|
1058
|
-
cache$2.clear();
|
|
1059
|
-
return;
|
|
1060
|
-
}
|
|
1061
|
-
const prefix = `${baseURL.replace(/\/+$/, "")}|`;
|
|
1062
|
-
for (const key of cache$2.keys()) if (key.startsWith(prefix)) cache$2.delete(key);
|
|
1063
|
-
}
|
|
1064
|
-
function modelProbeCacheInfo() {
|
|
1065
|
-
return [...cache$2.entries()].map(([key, entry]) => {
|
|
1066
|
-
const separator = key.indexOf("|");
|
|
1067
|
-
return {
|
|
1068
|
-
baseURL: key.slice(0, separator),
|
|
1069
|
-
model: key.slice(separator + 1),
|
|
1070
|
-
at: entry.at
|
|
1071
|
-
};
|
|
1072
|
-
});
|
|
1073
|
-
}
|
|
1074
957
|
// src/pricing.ts
|
|
1075
958
|
/** `gpt-oss:120b` → `gpt-oss`; used as the second lookup key. */
|
|
1076
959
|
function baseIdOf(id) {
|
|
@@ -1213,6 +1096,23 @@ function normalizeThinking(id, levels, toggle, fallback) {
|
|
|
1213
1096
|
...candidate === void 0 ? {} : { defaultReasoning: candidate }
|
|
1214
1097
|
};
|
|
1215
1098
|
}
|
|
1099
|
+
// src/routing.ts
|
|
1100
|
+
function trim(url) {
|
|
1101
|
+
return url.replace(/\/+$/, "");
|
|
1102
|
+
}
|
|
1103
|
+
function rootFor(config) {
|
|
1104
|
+
return trim(config.apiBase);
|
|
1105
|
+
}
|
|
1106
|
+
function modelsUrlFor(config) {
|
|
1107
|
+
return `${rootFor(config)}/v1/models`;
|
|
1108
|
+
}
|
|
1109
|
+
function tagsUrlFor(config) {
|
|
1110
|
+
return `${rootFor(config)}/api/tags`;
|
|
1111
|
+
}
|
|
1112
|
+
/** Endpoint for one model and one wire protocol. */
|
|
1113
|
+
function endpointFor(transport, config) {
|
|
1114
|
+
return transport === "openai-chat" ? `${trim(config.openAiBase)}/chat/completions` : `${rootFor(config)}/api/chat`;
|
|
1115
|
+
}
|
|
1216
1116
|
// src/catalog.ts
|
|
1217
1117
|
const CATALOG_TTL_MS = 6e5;
|
|
1218
1118
|
const CATALOG_TIMEOUT_MS = 15e3;
|
|
@@ -1298,44 +1198,6 @@ function metaFromLive(entry, caps) {
|
|
|
1298
1198
|
}
|
|
1299
1199
|
};
|
|
1300
1200
|
}
|
|
1301
|
-
function metaFromProbe(id, probe) {
|
|
1302
|
-
if (probe === void 0) return void 0;
|
|
1303
|
-
const base = GENERATED_MODELS[id];
|
|
1304
|
-
const normalized = normalizeThinking(id, probe.reasoning.length > 0 ? probe.reasoning : base?.reasoning ?? [], probe.reasoningToggle || base?.reasoningToggle === true, base?.defaultReasoning);
|
|
1305
|
-
return {
|
|
1306
|
-
id,
|
|
1307
|
-
name: base?.name ?? id,
|
|
1308
|
-
transport: base?.transport ?? "native",
|
|
1309
|
-
contextWindow: probe.contextWindow ?? base?.contextWindow ?? 131072,
|
|
1310
|
-
maxTokens: base?.maxTokens ?? 32768,
|
|
1311
|
-
input: [...probe.input],
|
|
1312
|
-
reasoning: normalized.reasoning,
|
|
1313
|
-
reasoningToggle: probe.reasoningToggle || base?.reasoningToggle === true,
|
|
1314
|
-
tools: probe.tools || base?.tools === true,
|
|
1315
|
-
free: base?.free === true,
|
|
1316
|
-
available: probe.available,
|
|
1317
|
-
...normalized.defaultReasoning === void 0 ? {} : { defaultReasoning: normalized.defaultReasoning },
|
|
1318
|
-
cost: base?.cost ?? {
|
|
1319
|
-
input: 0,
|
|
1320
|
-
output: 0,
|
|
1321
|
-
cacheRead: 0
|
|
1322
|
-
}
|
|
1323
|
-
};
|
|
1324
|
-
}
|
|
1325
|
-
async function adoptProbes(config, ids, base, fetchImpl) {
|
|
1326
|
-
const outcomes = await Promise.all(ids.slice(0, 64).map(async (id) => ({
|
|
1327
|
-
id,
|
|
1328
|
-
outcome: await probeModelOutcome(config, id, fetchImpl)
|
|
1329
|
-
})));
|
|
1330
|
-
const kept = [];
|
|
1331
|
-
for (const { id, outcome } of outcomes) {
|
|
1332
|
-
if (outcome.missing) continue;
|
|
1333
|
-
kept.push(id);
|
|
1334
|
-
const meta = metaFromProbe(id, outcome.probe);
|
|
1335
|
-
if (meta !== void 0) liveMeta.set(liveKey(base, id), meta);
|
|
1336
|
-
}
|
|
1337
|
-
return kept;
|
|
1338
|
-
}
|
|
1339
1201
|
function metaForBase(base) {
|
|
1340
1202
|
const out = {};
|
|
1341
1203
|
const prefix = `${base}|`;
|
|
@@ -1434,7 +1296,7 @@ async function refreshNow(config, fetchImpl, key, keepOnFailure, epoch) {
|
|
|
1434
1296
|
const discovered = fromData.length > 0 ? fromData : fromTags;
|
|
1435
1297
|
if (discovered.length > 0) {
|
|
1436
1298
|
for (const liveKey of liveMeta.keys()) if (liveKey.startsWith(`${key}|`)) liveMeta.delete(liveKey);
|
|
1437
|
-
const ids =
|
|
1299
|
+
const ids = discovered;
|
|
1438
1300
|
cache$1.set(key, {
|
|
1439
1301
|
at: Date.now(),
|
|
1440
1302
|
ids
|
|
@@ -1467,7 +1329,6 @@ function clearCatalogCache() {
|
|
|
1467
1329
|
inflight.clear();
|
|
1468
1330
|
liveMeta.clear();
|
|
1469
1331
|
diskLoaded.clear();
|
|
1470
|
-
clearModelProbeCache();
|
|
1471
1332
|
clearCatalogSnapshot();
|
|
1472
1333
|
}
|
|
1473
1334
|
function baseMeta(id, base) {
|
|
@@ -3447,205 +3308,6 @@ function windowRows(report) {
|
|
|
3447
3308
|
}
|
|
3448
3309
|
].filter((entry) => entry !== void 0);
|
|
3449
3310
|
}
|
|
3450
|
-
// src/local-models.ts
|
|
3451
|
-
const LOCAL_TIMEOUT_MS = 15e3;
|
|
3452
|
-
const MODEL_NAME = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
|
|
3453
|
-
function formatBytes(bytes) {
|
|
3454
|
-
if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
|
|
3455
|
-
const units = [
|
|
3456
|
-
"B",
|
|
3457
|
-
"KB",
|
|
3458
|
-
"MB",
|
|
3459
|
-
"GB",
|
|
3460
|
-
"TB"
|
|
3461
|
-
];
|
|
3462
|
-
let value = bytes;
|
|
3463
|
-
let index = 0;
|
|
3464
|
-
while (value >= 1024 && index < units.length - 1) {
|
|
3465
|
-
value /= 1024;
|
|
3466
|
-
index += 1;
|
|
3467
|
-
}
|
|
3468
|
-
return `${value >= 10 || index === 0 ? value.toFixed(0) : value.toFixed(1)} ${units[index]}`;
|
|
3469
|
-
}
|
|
3470
|
-
function assertLocal(config) {
|
|
3471
|
-
if (!isLocalEndpoint(config)) throw new Error("local endpoint required");
|
|
3472
|
-
}
|
|
3473
|
-
function assertModelName(model) {
|
|
3474
|
-
const name = model.trim();
|
|
3475
|
-
if (name === "" || name.includes("..") || !MODEL_NAME.test(name)) throw new Error("invalid model name");
|
|
3476
|
-
return name;
|
|
3477
|
-
}
|
|
3478
|
-
function recordOf(value) {
|
|
3479
|
-
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
3480
|
-
}
|
|
3481
|
-
function stringOf(value) {
|
|
3482
|
-
return typeof value === "string" && value !== "" ? value : void 0;
|
|
3483
|
-
}
|
|
3484
|
-
function numberOf(value) {
|
|
3485
|
-
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
3486
|
-
}
|
|
3487
|
-
function detailsOf(value) {
|
|
3488
|
-
const record = recordOf(value);
|
|
3489
|
-
if (record === void 0) return void 0;
|
|
3490
|
-
const family = stringOf(record.family);
|
|
3491
|
-
const parameterSize = stringOf(record.parameter_size);
|
|
3492
|
-
const quantization = stringOf(record.quantization_level);
|
|
3493
|
-
const out = {
|
|
3494
|
-
...family === void 0 ? {} : { family },
|
|
3495
|
-
...parameterSize === void 0 ? {} : { parameterSize },
|
|
3496
|
-
...quantization === void 0 ? {} : { quantization }
|
|
3497
|
-
};
|
|
3498
|
-
return Object.keys(out).length === 0 ? void 0 : out;
|
|
3499
|
-
}
|
|
3500
|
-
async function localJson(config, path, init, fetchImpl) {
|
|
3501
|
-
const response = await fetchImpl(`${rootFor(config)}${path}`, {
|
|
3502
|
-
...init,
|
|
3503
|
-
headers: {
|
|
3504
|
-
accept: "application/json",
|
|
3505
|
-
"content-type": "application/json",
|
|
3506
|
-
"user-agent": config.userAgent,
|
|
3507
|
-
...init.headers
|
|
3508
|
-
},
|
|
3509
|
-
signal: AbortSignal.timeout(LOCAL_TIMEOUT_MS)
|
|
3510
|
-
});
|
|
3511
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
3512
|
-
return response.json();
|
|
3513
|
-
}
|
|
3514
|
-
async function readLocalInventory(config, fetchImpl = fetch) {
|
|
3515
|
-
assertLocal(config);
|
|
3516
|
-
const [tagsPayload, psPayload] = await Promise.all([localJson(config, "/api/tags", { method: "GET" }, fetchImpl), localJson(config, "/api/ps", { method: "GET" }, fetchImpl).catch(() => void 0)]);
|
|
3517
|
-
const tags = recordOf(tagsPayload);
|
|
3518
|
-
const models = [];
|
|
3519
|
-
for (const entry of Array.isArray(tags?.models) ? tags.models : []) {
|
|
3520
|
-
const record = recordOf(entry);
|
|
3521
|
-
if (record === void 0) continue;
|
|
3522
|
-
const name = stringOf(record.name) ?? stringOf(record.model);
|
|
3523
|
-
if (name === void 0) continue;
|
|
3524
|
-
const size = numberOf(record.size) ?? 0;
|
|
3525
|
-
const digest = stringOf(record.digest);
|
|
3526
|
-
const modifiedAt = stringOf(record.modified_at);
|
|
3527
|
-
const details = detailsOf(record.details);
|
|
3528
|
-
models.push({
|
|
3529
|
-
name,
|
|
3530
|
-
size,
|
|
3531
|
-
...digest === void 0 ? {} : { digest },
|
|
3532
|
-
...modifiedAt === void 0 ? {} : { modifiedAt },
|
|
3533
|
-
...details === void 0 ? {} : { details }
|
|
3534
|
-
});
|
|
3535
|
-
}
|
|
3536
|
-
models.sort((a, b) => a.name.localeCompare(b.name));
|
|
3537
|
-
const running = [];
|
|
3538
|
-
const ps = recordOf(psPayload);
|
|
3539
|
-
for (const entry of Array.isArray(ps?.models) ? ps.models : []) {
|
|
3540
|
-
const record = recordOf(entry);
|
|
3541
|
-
if (record === void 0) continue;
|
|
3542
|
-
const name = stringOf(record.name) ?? stringOf(record.model);
|
|
3543
|
-
if (name === void 0) continue;
|
|
3544
|
-
const digest = stringOf(record.digest);
|
|
3545
|
-
const expiresAt = stringOf(record.expires_at);
|
|
3546
|
-
running.push({
|
|
3547
|
-
name,
|
|
3548
|
-
size: numberOf(record.size) ?? 0,
|
|
3549
|
-
sizeVram: numberOf(record.size_vram) ?? 0,
|
|
3550
|
-
...digest === void 0 ? {} : { digest },
|
|
3551
|
-
...expiresAt === void 0 ? {} : { expiresAt }
|
|
3552
|
-
});
|
|
3553
|
-
}
|
|
3554
|
-
running.sort((a, b) => a.name.localeCompare(b.name));
|
|
3555
|
-
return {
|
|
3556
|
-
models,
|
|
3557
|
-
running,
|
|
3558
|
-
diskBytes: models.reduce((sum, model) => sum + model.size, 0),
|
|
3559
|
-
runningBytes: running.reduce((sum, model) => sum + model.sizeVram, 0)
|
|
3560
|
-
};
|
|
3561
|
-
}
|
|
3562
|
-
async function showLocalModel(config, model, fetchImpl = fetch) {
|
|
3563
|
-
assertLocal(config);
|
|
3564
|
-
const name = assertModelName(model);
|
|
3565
|
-
const payload = recordOf(await localJson(config, "/api/show", {
|
|
3566
|
-
method: "POST",
|
|
3567
|
-
body: JSON.stringify({ model: name })
|
|
3568
|
-
}, fetchImpl));
|
|
3569
|
-
if (payload === void 0) throw new Error("invalid show response");
|
|
3570
|
-
const capabilities = Array.isArray(payload.capabilities) ? payload.capabilities.filter((entry) => typeof entry === "string") : [];
|
|
3571
|
-
const modifiedAt = stringOf(payload.modified_at);
|
|
3572
|
-
const details = detailsOf(payload.details);
|
|
3573
|
-
const parameters = stringOf(payload.parameters);
|
|
3574
|
-
return {
|
|
3575
|
-
name,
|
|
3576
|
-
...modifiedAt === void 0 ? {} : { modifiedAt },
|
|
3577
|
-
...details === void 0 ? {} : { details },
|
|
3578
|
-
capabilities,
|
|
3579
|
-
...parameters === void 0 ? {} : { parameters }
|
|
3580
|
-
};
|
|
3581
|
-
}
|
|
3582
|
-
async function pullLocalModel(config, model, onProgress = () => {}, fetchImpl = fetch, signal) {
|
|
3583
|
-
assertLocal(config);
|
|
3584
|
-
const name = assertModelName(model);
|
|
3585
|
-
const response = await fetchImpl(`${rootFor(config)}/api/pull`, {
|
|
3586
|
-
method: "POST",
|
|
3587
|
-
headers: {
|
|
3588
|
-
accept: "application/x-ndjson",
|
|
3589
|
-
"content-type": "application/json",
|
|
3590
|
-
"user-agent": config.userAgent
|
|
3591
|
-
},
|
|
3592
|
-
body: JSON.stringify({
|
|
3593
|
-
model: name,
|
|
3594
|
-
stream: true
|
|
3595
|
-
}),
|
|
3596
|
-
...signal === void 0 ? {} : { signal }
|
|
3597
|
-
});
|
|
3598
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
3599
|
-
if (response.body === null) throw new Error("empty pull stream");
|
|
3600
|
-
const reader = response.body.getReader();
|
|
3601
|
-
const decoder = new TextDecoder();
|
|
3602
|
-
let buffer = "";
|
|
3603
|
-
let last = { status: "starting" };
|
|
3604
|
-
const consume = (line) => {
|
|
3605
|
-
if (line.trim() === "") return;
|
|
3606
|
-
const entry = recordOf(JSON.parse(line));
|
|
3607
|
-
if (entry === void 0) return;
|
|
3608
|
-
if (typeof entry.error === "string" && entry.error !== "") throw new Error(entry.error);
|
|
3609
|
-
if (typeof entry.status === "string") {
|
|
3610
|
-
const digest = stringOf(entry.digest);
|
|
3611
|
-
const completed = numberOf(entry.completed);
|
|
3612
|
-
const total = numberOf(entry.total);
|
|
3613
|
-
last = {
|
|
3614
|
-
status: entry.status,
|
|
3615
|
-
...digest === void 0 ? {} : { digest },
|
|
3616
|
-
...completed === void 0 ? {} : { completed },
|
|
3617
|
-
...total === void 0 ? {} : { total }
|
|
3618
|
-
};
|
|
3619
|
-
onProgress(last);
|
|
3620
|
-
}
|
|
3621
|
-
};
|
|
3622
|
-
for (;;) {
|
|
3623
|
-
const { done, value } = await reader.read();
|
|
3624
|
-
buffer += decoder.decode(value, { stream: !done });
|
|
3625
|
-
const lines = buffer.split("\n");
|
|
3626
|
-
buffer = lines.pop() ?? "";
|
|
3627
|
-
for (const line of lines) consume(line);
|
|
3628
|
-
if (done) break;
|
|
3629
|
-
}
|
|
3630
|
-
if (buffer.trim() !== "") consume(buffer);
|
|
3631
|
-
return last;
|
|
3632
|
-
}
|
|
3633
|
-
/** Delete one local model. Callers must obtain explicit confirmation first. */
|
|
3634
|
-
async function removeLocalModel(config, model, fetchImpl = fetch) {
|
|
3635
|
-
assertLocal(config);
|
|
3636
|
-
const name = assertModelName(model);
|
|
3637
|
-
const response = await fetchImpl(`${rootFor(config)}/api/delete`, {
|
|
3638
|
-
method: "DELETE",
|
|
3639
|
-
headers: {
|
|
3640
|
-
accept: "application/json",
|
|
3641
|
-
"content-type": "application/json",
|
|
3642
|
-
"user-agent": config.userAgent
|
|
3643
|
-
},
|
|
3644
|
-
body: JSON.stringify({ model: name }),
|
|
3645
|
-
signal: AbortSignal.timeout(LOCAL_TIMEOUT_MS)
|
|
3646
|
-
});
|
|
3647
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
3648
|
-
}
|
|
3649
3311
|
// src/commands.ts
|
|
3650
3312
|
function bar(percent, width = 10) {
|
|
3651
3313
|
const clamped = Math.max(0, Math.min(100, percent));
|
|
@@ -3760,57 +3422,6 @@ async function testText(deps, model) {
|
|
|
3760
3422
|
};
|
|
3761
3423
|
}
|
|
3762
3424
|
}
|
|
3763
|
-
async function localText(deps) {
|
|
3764
|
-
const config = deps.config();
|
|
3765
|
-
if (!isLocalEndpoint(config)) return "Local models are available only with a local Ollama endpoint.";
|
|
3766
|
-
const inventory = await readLocalInventory(config);
|
|
3767
|
-
const running = new Map(inventory.running.map((entry) => [entry.name, entry]));
|
|
3768
|
-
const lines = [`Local Ollama · disk ${formatBytes(inventory.diskBytes)} · running ${formatBytes(inventory.runningBytes)}`];
|
|
3769
|
-
if (inventory.models.length === 0) return [...lines, "No local models."].join("\n");
|
|
3770
|
-
for (const model of inventory.models) {
|
|
3771
|
-
const active = running.get(model.name);
|
|
3772
|
-
const details = model.details === void 0 ? "" : [
|
|
3773
|
-
model.details.family,
|
|
3774
|
-
model.details.parameterSize,
|
|
3775
|
-
model.details.quantization
|
|
3776
|
-
].filter((entry) => entry !== void 0).join(" ");
|
|
3777
|
-
lines.push(`- ${model.name} · ${formatBytes(model.size)}${active === void 0 ? "" : " · RUNNING"}${details === "" ? "" : ` · ${details}`}`);
|
|
3778
|
-
}
|
|
3779
|
-
return lines.join("\n");
|
|
3780
|
-
}
|
|
3781
|
-
async function localPsText(deps) {
|
|
3782
|
-
const config = deps.config();
|
|
3783
|
-
if (!isLocalEndpoint(config)) return "Local models are available only with a local Ollama endpoint.";
|
|
3784
|
-
const inventory = await readLocalInventory(config);
|
|
3785
|
-
if (inventory.running.length === 0) return "No running models.";
|
|
3786
|
-
return inventory.running.map((entry) => `- ${entry.name} · VRAM ${formatBytes(entry.sizeVram)}${entry.expiresAt === void 0 ? "" : ` · expires ${entry.expiresAt}`}`).join("\n");
|
|
3787
|
-
}
|
|
3788
|
-
async function localShowText(deps, model) {
|
|
3789
|
-
const config = deps.config();
|
|
3790
|
-
if (!isLocalEndpoint(config)) return "Local models are available only with a local Ollama endpoint.";
|
|
3791
|
-
const show = await showLocalModel(config, model);
|
|
3792
|
-
const lines = [`Local model: ${show.name}`];
|
|
3793
|
-
if (show.modifiedAt !== void 0) lines.push(`Modified: ${show.modifiedAt}`);
|
|
3794
|
-
if (show.details !== void 0) lines.push(`Details: ${show.details.family ?? "?"} ${show.details.parameterSize ?? ""} ${show.details.quantization ?? ""}`.trim());
|
|
3795
|
-
if (show.capabilities.length > 0) lines.push(`Capabilities: ${show.capabilities.join(", ")}`);
|
|
3796
|
-
if (show.parameters !== void 0) lines.push(`Parameters:\n${show.parameters.slice(0, 2e3)}`);
|
|
3797
|
-
return lines.join("\n");
|
|
3798
|
-
}
|
|
3799
|
-
async function localPullText(deps, model) {
|
|
3800
|
-
const config = deps.config();
|
|
3801
|
-
if (!isLocalEndpoint(config)) return "Local models are available only with a local Ollama endpoint.";
|
|
3802
|
-
let fraction = 0;
|
|
3803
|
-
return `${model}: ${(await pullLocalModel(config, model, (progress) => {
|
|
3804
|
-
if (progress.total !== void 0 && progress.total > 0) fraction = (progress.completed ?? 0) / progress.total;
|
|
3805
|
-
})).status}${fraction > 0 ? ` · ${Math.round(fraction * 100)}%` : ""}`;
|
|
3806
|
-
}
|
|
3807
|
-
async function localRemoveText(deps, model, confirmed) {
|
|
3808
|
-
if (!confirmed) return `Refusing to remove ${model}: add --yes to confirm.`;
|
|
3809
|
-
const config = deps.config();
|
|
3810
|
-
if (!isLocalEndpoint(config)) return "Local models are available only with a local Ollama endpoint.";
|
|
3811
|
-
await removeLocalModel(config, model);
|
|
3812
|
-
return `${model}: removed.`;
|
|
3813
|
-
}
|
|
3814
3425
|
async function statusText(deps) {
|
|
3815
3426
|
const config = deps.config();
|
|
3816
3427
|
const rules = config.modelAccountRules.length === 0 ? "none" : config.modelAccountRules.map((rule) => `${rule.pattern} → ${rule.account}`).join(", ");
|
|
@@ -3829,7 +3440,7 @@ function applyCommands(ctx, deps) {
|
|
|
3829
3440
|
ctx.commands.register({
|
|
3830
3441
|
name: "ollama",
|
|
3831
3442
|
description: "Ollama Cloud status, balance, accounts, models and rules",
|
|
3832
|
-
input: { hint: "status | usage | balance | accounts | models | rules | test [model]
|
|
3443
|
+
input: { hint: "status | usage | balance | accounts | models | rules | test [model]" },
|
|
3833
3444
|
handler: async (invocation) => {
|
|
3834
3445
|
const raw = invocation.rawInput.trim();
|
|
3835
3446
|
const parts = raw === "" ? ["status"] : raw.split(/\s+/);
|
|
@@ -3870,50 +3481,9 @@ function applyCommands(ctx, deps) {
|
|
|
3870
3481
|
text: result.text
|
|
3871
3482
|
};
|
|
3872
3483
|
}
|
|
3873
|
-
case "local": {
|
|
3874
|
-
const words = arg === "" ? [] : arg.split(/\s+/);
|
|
3875
|
-
const action = (words[0] ?? "models").toLowerCase();
|
|
3876
|
-
const confirmed = words.includes("--yes");
|
|
3877
|
-
const model = words.slice(1).filter((word) => word !== "--yes").join(" ");
|
|
3878
|
-
try {
|
|
3879
|
-
if (action === "models") return {
|
|
3880
|
-
kind: "success",
|
|
3881
|
-
text: await localText(deps)
|
|
3882
|
-
};
|
|
3883
|
-
if (action === "ps") return {
|
|
3884
|
-
kind: "success",
|
|
3885
|
-
text: await localPsText(deps)
|
|
3886
|
-
};
|
|
3887
|
-
if (model === "") return {
|
|
3888
|
-
kind: "error",
|
|
3889
|
-
text: `Usage: /ollama local ${action} <model>`
|
|
3890
|
-
};
|
|
3891
|
-
if (action === "show") return {
|
|
3892
|
-
kind: "success",
|
|
3893
|
-
text: await localShowText(deps, model)
|
|
3894
|
-
};
|
|
3895
|
-
if (action === "pull") return {
|
|
3896
|
-
kind: "success",
|
|
3897
|
-
text: await localPullText(deps, model)
|
|
3898
|
-
};
|
|
3899
|
-
if (action === "remove") return {
|
|
3900
|
-
kind: confirmed ? "success" : "error",
|
|
3901
|
-
text: await localRemoveText(deps, model, confirmed)
|
|
3902
|
-
};
|
|
3903
|
-
return {
|
|
3904
|
-
kind: "error",
|
|
3905
|
-
text: `Unknown local action "${action}". Use models, ps, show, pull, or remove.`
|
|
3906
|
-
};
|
|
3907
|
-
} catch (error) {
|
|
3908
|
-
return {
|
|
3909
|
-
kind: "error",
|
|
3910
|
-
text: safeErrorMessage(error, "local command failed")
|
|
3911
|
-
};
|
|
3912
|
-
}
|
|
3913
|
-
}
|
|
3914
3484
|
default: return {
|
|
3915
3485
|
kind: "error",
|
|
3916
|
-
text: `Unknown /ollama argument "${verb}". Use status, usage, balance, accounts, models, rules, test [model]
|
|
3486
|
+
text: `Unknown /ollama argument "${verb}". Use status, usage, balance, accounts, models, rules, or test [model].`
|
|
3917
3487
|
};
|
|
3918
3488
|
}
|
|
3919
3489
|
}
|
|
@@ -4011,12 +3581,6 @@ async function resolveSlot(ctx, slot) {
|
|
|
4011
3581
|
function createKeyPool(ctx, config) {
|
|
4012
3582
|
const all = async () => {
|
|
4013
3583
|
const current = config();
|
|
4014
|
-
const hasConfiguredKey = current.apiKey !== void 0 || current.accounts.some((account) => typeof account.apiKey === "string" && account.apiKey.trim() !== "" || typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() !== "");
|
|
4015
|
-
if (current.apiKeyEnv === "" && !hasConfiguredKey) return [{
|
|
4016
|
-
key: "",
|
|
4017
|
-
source: "none",
|
|
4018
|
-
label: "default"
|
|
4019
|
-
}];
|
|
4020
3584
|
const pinned = current.activeAccount.trim();
|
|
4021
3585
|
const slots = slotsOf(current);
|
|
4022
3586
|
if (pinned !== "") {
|
|
@@ -4065,13 +3629,13 @@ function header(request, name) {
|
|
|
4065
3629
|
const value = request.headers?.[name];
|
|
4066
3630
|
return Array.isArray(value) ? value[0] : value;
|
|
4067
3631
|
}
|
|
4068
|
-
function isLoopback
|
|
3632
|
+
function isLoopback(value) {
|
|
4069
3633
|
const address = value?.toLowerCase().replace(/^\[|\]$/g, "");
|
|
4070
3634
|
return address === "localhost" || address === "localhost." || address === "::1" || address?.startsWith("127.") === true || address?.startsWith("::ffff:127.") === true;
|
|
4071
3635
|
}
|
|
4072
3636
|
function isTrustedUpdateRequest(request) {
|
|
4073
3637
|
if (header(request, UPDATE_HEADER) !== "1") return false;
|
|
4074
|
-
if (!isLoopback
|
|
3638
|
+
if (!isLoopback(request.socket?.remoteAddress)) return false;
|
|
4075
3639
|
const site = header(request, "sec-fetch-site");
|
|
4076
3640
|
if (site !== void 0 && site !== "same-origin") return false;
|
|
4077
3641
|
const origin = header(request, "origin");
|
|
@@ -4079,7 +3643,7 @@ function isTrustedUpdateRequest(request) {
|
|
|
4079
3643
|
if (origin === void 0 || host === void 0) return false;
|
|
4080
3644
|
try {
|
|
4081
3645
|
const url = new URL(origin);
|
|
4082
|
-
return (url.protocol === "http:" || url.protocol === "https:") && isLoopback
|
|
3646
|
+
return (url.protocol === "http:" || url.protocol === "https:") && isLoopback(url.hostname) && url.host === host;
|
|
4083
3647
|
} catch {
|
|
4084
3648
|
return false;
|
|
4085
3649
|
}
|
|
@@ -4666,14 +4230,6 @@ function normalizeBase(raw) {
|
|
|
4666
4230
|
if (url.username !== "" || url.password !== "") throw new DiscoveryError("endpoint URL must not contain credentials");
|
|
4667
4231
|
return sanitizeUrl(value);
|
|
4668
4232
|
}
|
|
4669
|
-
function isLoopback(url) {
|
|
4670
|
-
try {
|
|
4671
|
-
const host = new URL(url).hostname;
|
|
4672
|
-
return host === "127.0.0.1" || host === "localhost" || host === "::1" || host === "[::1]";
|
|
4673
|
-
} catch {
|
|
4674
|
-
return false;
|
|
4675
|
-
}
|
|
4676
|
-
}
|
|
4677
4233
|
function idsOf(payload) {
|
|
4678
4234
|
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return [];
|
|
4679
4235
|
const record = payload;
|
|
@@ -4720,8 +4276,7 @@ async function discoverModels(rawBase, key, fetchImpl = fetch) {
|
|
|
4720
4276
|
}
|
|
4721
4277
|
return {
|
|
4722
4278
|
base,
|
|
4723
|
-
models: [...new Set(idsOf(payload))]
|
|
4724
|
-
local: isLoopback(base)
|
|
4279
|
+
models: [...new Set(idsOf(payload))]
|
|
4725
4280
|
};
|
|
4726
4281
|
}
|
|
4727
4282
|
// src/routes-public.ts
|
|
@@ -5492,96 +5047,6 @@ function applyRoutes(ctx, web, deps) {
|
|
|
5492
5047
|
}
|
|
5493
5048
|
await refreshAndState(ctx, deps, res);
|
|
5494
5049
|
});
|
|
5495
|
-
route("/dsh-ollama/local/models", async (req, res) => {
|
|
5496
|
-
if (!await privateRead(req, res)) return;
|
|
5497
|
-
const config = deps.config();
|
|
5498
|
-
if (!isLocalEndpoint(config)) {
|
|
5499
|
-
json(res, 400, { error: "local endpoint required" });
|
|
5500
|
-
return;
|
|
5501
|
-
}
|
|
5502
|
-
try {
|
|
5503
|
-
json(res, 200, await readLocalInventory(config, deps.localFetch));
|
|
5504
|
-
} catch (error) {
|
|
5505
|
-
json(res, 502, { error: error instanceof Error ? error.message : "local models failed" });
|
|
5506
|
-
}
|
|
5507
|
-
});
|
|
5508
|
-
route("/dsh-ollama/local/show", async (req, res) => {
|
|
5509
|
-
if (!await privateRead(req, res)) return;
|
|
5510
|
-
const config = deps.config();
|
|
5511
|
-
if (!isLocalEndpoint(config)) {
|
|
5512
|
-
json(res, 400, { error: "local endpoint required" });
|
|
5513
|
-
return;
|
|
5514
|
-
}
|
|
5515
|
-
const model = new URL(req.url ?? "/", "http://localhost").searchParams.get("model") ?? "";
|
|
5516
|
-
try {
|
|
5517
|
-
json(res, 200, await showLocalModel(config, model, deps.localFetch));
|
|
5518
|
-
} catch (error) {
|
|
5519
|
-
json(res, 400, { error: error instanceof Error ? error.message : "show failed" });
|
|
5520
|
-
}
|
|
5521
|
-
});
|
|
5522
|
-
route("/dsh-ollama/local/pull", async (req, res) => {
|
|
5523
|
-
if (!await requirePost(req, res, verifyHost)) return;
|
|
5524
|
-
const config = deps.config();
|
|
5525
|
-
if (!isLocalEndpoint(config)) {
|
|
5526
|
-
json(res, 400, { error: "local endpoint required" });
|
|
5527
|
-
return;
|
|
5528
|
-
}
|
|
5529
|
-
let body;
|
|
5530
|
-
try {
|
|
5531
|
-
body = await readJson(req);
|
|
5532
|
-
} catch (error) {
|
|
5533
|
-
json(res, 400, { error: error instanceof Error ? error.message : "invalid body" });
|
|
5534
|
-
return;
|
|
5535
|
-
}
|
|
5536
|
-
const model = typeof body.model === "string" ? body.model : "";
|
|
5537
|
-
res.statusCode = 200;
|
|
5538
|
-
res.setHeader("content-type", "application/x-ndjson; charset=utf-8");
|
|
5539
|
-
res.setHeader("cache-control", "no-store");
|
|
5540
|
-
const write = (value) => {
|
|
5541
|
-
res.write(`${JSON.stringify(value)}\n`);
|
|
5542
|
-
};
|
|
5543
|
-
try {
|
|
5544
|
-
const last = await pullLocalModel(config, model, write, deps.localFetch);
|
|
5545
|
-
clearModelProbeCache(config.apiBase);
|
|
5546
|
-
write({
|
|
5547
|
-
status: last.status,
|
|
5548
|
-
done: true
|
|
5549
|
-
});
|
|
5550
|
-
} catch (error) {
|
|
5551
|
-
write({ error: error instanceof Error ? error.message : "pull failed" });
|
|
5552
|
-
}
|
|
5553
|
-
res.end();
|
|
5554
|
-
});
|
|
5555
|
-
route("/dsh-ollama/local/remove", async (req, res) => {
|
|
5556
|
-
if (!await requirePost(req, res, verifyHost)) return;
|
|
5557
|
-
const config = deps.config();
|
|
5558
|
-
if (!isLocalEndpoint(config)) {
|
|
5559
|
-
json(res, 400, { error: "local endpoint required" });
|
|
5560
|
-
return;
|
|
5561
|
-
}
|
|
5562
|
-
let body;
|
|
5563
|
-
try {
|
|
5564
|
-
body = await readJson(req);
|
|
5565
|
-
} catch (error) {
|
|
5566
|
-
json(res, 400, { error: error instanceof Error ? error.message : "invalid body" });
|
|
5567
|
-
return;
|
|
5568
|
-
}
|
|
5569
|
-
if (body.confirm !== true) {
|
|
5570
|
-
json(res, 400, { error: "confirmation required" });
|
|
5571
|
-
return;
|
|
5572
|
-
}
|
|
5573
|
-
const model = typeof body.model === "string" ? body.model : "";
|
|
5574
|
-
try {
|
|
5575
|
-
await removeLocalModel(config, model, deps.localFetch);
|
|
5576
|
-
clearModelProbeCache(config.apiBase);
|
|
5577
|
-
json(res, 200, {
|
|
5578
|
-
ok: true,
|
|
5579
|
-
model
|
|
5580
|
-
});
|
|
5581
|
-
} catch (error) {
|
|
5582
|
-
json(res, 502, { error: error instanceof Error ? error.message : "remove failed" });
|
|
5583
|
-
}
|
|
5584
|
-
});
|
|
5585
5050
|
route("/dsh-ollama/discover", async (req, res) => {
|
|
5586
5051
|
if (!await requirePost(req, res, verifyHost)) return;
|
|
5587
5052
|
let body;
|
|
@@ -6416,6 +5881,6 @@ function apply(ctx, config) {
|
|
|
6416
5881
|
}), "dsh-ollama-cloud-provider: plugin updater");
|
|
6417
5882
|
});
|
|
6418
5883
|
}
|
|
6419
|
-
export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache,
|
|
5884
|
+
export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool, createKeyResolver, createUsageReader, describeModel, effectiveModel, inject, name, normalizeBalance, normalizeUsage, resolveConfig };
|
|
6420
5885
|
|
|
6421
5886
|
//# sourceMappingURL=index.js.map
|