@goodandready/dsh-ollama-cloud-provider 0.1.27 → 0.1.30

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/lib/index.js CHANGED
@@ -9,6 +9,8 @@ import { credentialRef } from "@deepseek-ai/dsh-credentials";
9
9
  import { launchEnvironmentOf } from "@deepseek-ai/dsh-launch-environment";
10
10
  import { spawn } from "node:child_process";
11
11
  import { fileURLToPath } from "node:url";
12
+ import { lookup } from "node:dns/promises";
13
+ import { isIP } from "node:net";
12
14
  import { WebError } from "@deepseek-ai/dsh-web";
13
15
  // src/account-rules.ts
14
16
  function glob(pattern) {
@@ -122,7 +124,7 @@ const DEFAULT_API_BASE = "https://ollama.com";
122
124
  const DEFAULT_OPENAI_BASE = "https://ollama.com/v1";
123
125
  const DEFAULT_USAGE_URL = "https://ollama.com/api/usage";
124
126
  const DEFAULT_BALANCE_URL = "https://ollama.com/api/balance";
125
- const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.27";
127
+ const DEFAULT_USER_AGENT = "dsh-ollama-cloud-provider/0.1.30";
126
128
  const DEFAULT_CONTEXT_WINDOW = 131072;
127
129
  const DEFAULT_MAX_TOKENS = 32768;
128
130
  const DEFAULT_GENERATE_MAX_TOKENS = 131072;
@@ -284,23 +286,23 @@ function manualModelList(value) {
284
286
  }
285
287
  return out;
286
288
  }
287
- function recordOf$1(value) {
289
+ function recordOf(value) {
288
290
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : {};
289
291
  }
290
292
  function numberRecord(value) {
291
293
  const out = {};
292
- for (const [key, entry] of Object.entries(recordOf$1(value))) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
294
+ for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "number" && Number.isInteger(entry) && entry > 0) out[key] = entry;
293
295
  return out;
294
296
  }
295
297
  function booleanRecord(value) {
296
298
  const out = {};
297
- for (const [key, entry] of Object.entries(recordOf$1(value))) if (typeof entry === "boolean") out[key] = entry;
299
+ for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "boolean") out[key] = entry;
298
300
  return out;
299
301
  }
300
302
  function priceRecord(value) {
301
303
  const out = {};
302
- for (const [key, entry] of Object.entries(recordOf$1(value))) {
303
- const raw = recordOf$1(entry);
304
+ for (const [key, entry] of Object.entries(recordOf(value))) {
305
+ const raw = recordOf(entry);
304
306
  const price = {};
305
307
  if (typeof raw.input === "number" && raw.input >= 0) price.input = raw.input;
306
308
  if (typeof raw.output === "number" && raw.output >= 0) price.output = raw.output;
@@ -312,14 +314,14 @@ function priceRecord(value) {
312
314
  const TRANSPORTS$1 = /* @__PURE__ */ new Set(["native", "openai-chat"]);
313
315
  function transportRecord(value) {
314
316
  const out = {};
315
- for (const [key, entry] of Object.entries(recordOf$1(value))) if (typeof entry === "string" && TRANSPORTS$1.has(entry)) out[key] = entry;
317
+ for (const [key, entry] of Object.entries(recordOf(value))) if (typeof entry === "string" && TRANSPORTS$1.has(entry)) out[key] = entry;
316
318
  return out;
317
319
  }
318
320
  function ruleList(value) {
319
321
  if (!Array.isArray(value)) return [];
320
322
  const out = [];
321
323
  for (const entry of value) {
322
- const raw = recordOf$1(entry);
324
+ const raw = recordOf(entry);
323
325
  const pattern = typeof raw.pattern === "string" ? raw.pattern.trim() : "";
324
326
  const account = typeof raw.account === "string" ? raw.account.trim() : "";
325
327
  if (pattern !== "" && account !== "") out.push({
@@ -343,7 +345,7 @@ function resolveConfig(config) {
343
345
  const numCtx = integerInRange(config.numCtx, 1, 1e7);
344
346
  const stop = nonEmptyStrings(config.stop);
345
347
  return {
346
- apiKeyEnv: typeof config.apiKeyEnv === "string" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
348
+ apiKeyEnv: typeof config.apiKeyEnv === "string" && config.apiKeyEnv.trim() !== "" ? config.apiKeyEnv.trim() : DEFAULT_API_KEY_ENV,
347
349
  ...apiKey === void 0 ? {} : { apiKey },
348
350
  apiBase: typeof config.apiBase === "string" && config.apiBase !== "" ? config.apiBase : DEFAULT_API_BASE,
349
351
  openAiBase: typeof config.openAiBase === "string" && config.openAiBase !== "" ? config.openAiBase : DEFAULT_OPENAI_BASE,
@@ -954,123 +956,6 @@ const GENERATED_MODELS = {
954
956
  }
955
957
  }
956
958
  };
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
959
  // src/pricing.ts
1075
960
  /** `gpt-oss:120b` → `gpt-oss`; used as the second lookup key. */
1076
961
  function baseIdOf(id) {
@@ -1213,6 +1098,23 @@ function normalizeThinking(id, levels, toggle, fallback) {
1213
1098
  ...candidate === void 0 ? {} : { defaultReasoning: candidate }
1214
1099
  };
1215
1100
  }
1101
+ // src/routing.ts
1102
+ function trim(url) {
1103
+ return url.replace(/\/+$/, "");
1104
+ }
1105
+ function rootFor(config) {
1106
+ return trim(config.apiBase);
1107
+ }
1108
+ function modelsUrlFor(config) {
1109
+ return `${rootFor(config)}/v1/models`;
1110
+ }
1111
+ function tagsUrlFor(config) {
1112
+ return `${rootFor(config)}/api/tags`;
1113
+ }
1114
+ /** Endpoint for one model and one wire protocol. */
1115
+ function endpointFor(transport, config) {
1116
+ return transport === "openai-chat" ? `${trim(config.openAiBase)}/chat/completions` : `${rootFor(config)}/api/chat`;
1117
+ }
1216
1118
  // src/catalog.ts
1217
1119
  const CATALOG_TTL_MS = 6e5;
1218
1120
  const CATALOG_TIMEOUT_MS = 15e3;
@@ -1298,44 +1200,6 @@ function metaFromLive(entry, caps) {
1298
1200
  }
1299
1201
  };
1300
1202
  }
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
1203
  function metaForBase(base) {
1340
1204
  const out = {};
1341
1205
  const prefix = `${base}|`;
@@ -1434,7 +1298,7 @@ async function refreshNow(config, fetchImpl, key, keepOnFailure, epoch) {
1434
1298
  const discovered = fromData.length > 0 ? fromData : fromTags;
1435
1299
  if (discovered.length > 0) {
1436
1300
  for (const liveKey of liveMeta.keys()) if (liveKey.startsWith(`${key}|`)) liveMeta.delete(liveKey);
1437
- const ids = isLocalEndpoint(config) ? await adoptProbes(config, discovered, key, fetchImpl) : discovered;
1301
+ const ids = discovered;
1438
1302
  cache$1.set(key, {
1439
1303
  at: Date.now(),
1440
1304
  ids
@@ -1467,7 +1331,6 @@ function clearCatalogCache() {
1467
1331
  inflight.clear();
1468
1332
  liveMeta.clear();
1469
1333
  diskLoaded.clear();
1470
- clearModelProbeCache();
1471
1334
  clearCatalogSnapshot();
1472
1335
  }
1473
1336
  function baseMeta(id, base) {
@@ -3447,205 +3310,6 @@ function windowRows(report) {
3447
3310
  }
3448
3311
  ].filter((entry) => entry !== void 0);
3449
3312
  }
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
3313
  // src/commands.ts
3650
3314
  function bar(percent, width = 10) {
3651
3315
  const clamped = Math.max(0, Math.min(100, percent));
@@ -3760,57 +3424,6 @@ async function testText(deps, model) {
3760
3424
  };
3761
3425
  }
3762
3426
  }
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
3427
  async function statusText(deps) {
3815
3428
  const config = deps.config();
3816
3429
  const rules = config.modelAccountRules.length === 0 ? "none" : config.modelAccountRules.map((rule) => `${rule.pattern} → ${rule.account}`).join(", ");
@@ -3829,7 +3442,7 @@ function applyCommands(ctx, deps) {
3829
3442
  ctx.commands.register({
3830
3443
  name: "ollama",
3831
3444
  description: "Ollama Cloud status, balance, accounts, models and rules",
3832
- input: { hint: "status | usage | balance | accounts | models | rules | test [model] | local [models|ps|show|pull|remove] [model]" },
3445
+ input: { hint: "status | usage | balance | accounts | models | rules | test [model]" },
3833
3446
  handler: async (invocation) => {
3834
3447
  const raw = invocation.rawInput.trim();
3835
3448
  const parts = raw === "" ? ["status"] : raw.split(/\s+/);
@@ -3870,50 +3483,9 @@ function applyCommands(ctx, deps) {
3870
3483
  text: result.text
3871
3484
  };
3872
3485
  }
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
3486
  default: return {
3915
3487
  kind: "error",
3916
- text: `Unknown /ollama argument "${verb}". Use status, usage, balance, accounts, models, rules, test [model], or local.`
3488
+ text: `Unknown /ollama argument "${verb}". Use status, usage, balance, accounts, models, rules, or test [model].`
3917
3489
  };
3918
3490
  }
3919
3491
  }
@@ -4011,12 +3583,6 @@ async function resolveSlot(ctx, slot) {
4011
3583
  function createKeyPool(ctx, config) {
4012
3584
  const all = async () => {
4013
3585
  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
3586
  const pinned = current.activeAccount.trim();
4021
3587
  const slots = slotsOf(current);
4022
3588
  if (pinned !== "") {
@@ -4065,13 +3631,13 @@ function header(request, name) {
4065
3631
  const value = request.headers?.[name];
4066
3632
  return Array.isArray(value) ? value[0] : value;
4067
3633
  }
4068
- function isLoopback$1(value) {
3634
+ function isLoopback(value) {
4069
3635
  const address = value?.toLowerCase().replace(/^\[|\]$/g, "");
4070
3636
  return address === "localhost" || address === "localhost." || address === "::1" || address?.startsWith("127.") === true || address?.startsWith("::ffff:127.") === true;
4071
3637
  }
4072
3638
  function isTrustedUpdateRequest(request) {
4073
3639
  if (header(request, UPDATE_HEADER) !== "1") return false;
4074
- if (!isLoopback$1(request.socket?.remoteAddress)) return false;
3640
+ if (!isLoopback(request.socket?.remoteAddress)) return false;
4075
3641
  const site = header(request, "sec-fetch-site");
4076
3642
  if (site !== void 0 && site !== "same-origin") return false;
4077
3643
  const origin = header(request, "origin");
@@ -4079,7 +3645,7 @@ function isTrustedUpdateRequest(request) {
4079
3645
  if (origin === void 0 || host === void 0) return false;
4080
3646
  try {
4081
3647
  const url = new URL(origin);
4082
- return (url.protocol === "http:" || url.protocol === "https:") && isLoopback$1(url.hostname) && url.host === host;
3648
+ return (url.protocol === "http:" || url.protocol === "https:") && isLoopback(url.hostname) && url.host === host;
4083
3649
  } catch {
4084
3650
  return false;
4085
3651
  }
@@ -4666,13 +4232,35 @@ function normalizeBase(raw) {
4666
4232
  if (url.username !== "" || url.password !== "") throw new DiscoveryError("endpoint URL must not contain credentials");
4667
4233
  return sanitizeUrl(value);
4668
4234
  }
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]";
4235
+ function isPrivateV4(address) {
4236
+ const parts = address.split(".").map(Number);
4237
+ if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255)) return true;
4238
+ const [a, b] = parts;
4239
+ return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || a === 192 && b === 0 || a === 198 && (b === 18 || b === 19) || a >= 224;
4240
+ }
4241
+ function isPrivateAddress(address) {
4242
+ const clean = address.toLowerCase().replace(/^\[|\]$/g, "");
4243
+ if (isIP(clean) === 4) return isPrivateV4(clean);
4244
+ if (isIP(clean) !== 6) return true;
4245
+ return clean === "::1" || clean === "::" || clean.startsWith("fc") || clean.startsWith("fd") || /^fe[89ab]/.test(clean) || clean.startsWith("ff") || clean.startsWith("2001:db8") || clean.startsWith("::ffff:");
4246
+ }
4247
+ async function assertPublicEndpoint(base, options) {
4248
+ if (options.allowPrivate === true) return;
4249
+ const url = new URL(base);
4250
+ if (url.protocol !== "https:") throw new DiscoveryError("endpoint must use https");
4251
+ const host = url.hostname.toLowerCase().replace(/^\[|\]$/g, "");
4252
+ if (host === "localhost" || host.endsWith(".localhost")) throw new DiscoveryError("private endpoint refused");
4253
+ let addresses;
4254
+ if (isIP(host) !== 0) addresses = [host];
4255
+ else try {
4256
+ addresses = (await lookup(host, {
4257
+ all: true,
4258
+ verbatim: true
4259
+ })).map((entry) => entry.address);
4673
4260
  } catch {
4674
- return false;
4261
+ throw new DiscoveryError("could not resolve endpoint");
4675
4262
  }
4263
+ if (addresses.length === 0 || addresses.some(isPrivateAddress)) throw new DiscoveryError("private endpoint refused");
4676
4264
  }
4677
4265
  function idsOf(payload) {
4678
4266
  if (typeof payload !== "object" || payload === null || Array.isArray(payload)) return [];
@@ -4707,8 +4295,9 @@ async function get(url, key, fetchImpl, signal) {
4707
4295
  * Discover a catalog for an unsaved endpoint with a one-shot key.
4708
4296
  * This function performs no persistence and never includes the key in errors.
4709
4297
  */
4710
- async function discoverModels(rawBase, key, fetchImpl = fetch) {
4298
+ async function discoverModels(rawBase, key, fetchImpl = fetch, options = {}) {
4711
4299
  const base = normalizeBase(rawBase);
4300
+ await assertPublicEndpoint(base, options);
4712
4301
  const budget = AbortSignal.timeout(DISCOVERY_TIMEOUT_MS);
4713
4302
  let payload;
4714
4303
  try {
@@ -4720,8 +4309,7 @@ async function discoverModels(rawBase, key, fetchImpl = fetch) {
4720
4309
  }
4721
4310
  return {
4722
4311
  base,
4723
- models: [...new Set(idsOf(payload))],
4724
- local: isLoopback(base)
4312
+ models: [...new Set(idsOf(payload))]
4725
4313
  };
4726
4314
  }
4727
4315
  // src/routes-public.ts
@@ -4973,12 +4561,17 @@ function desktopShellRequest(req) {
4973
4561
  const cookie = req.headers.cookie ?? "";
4974
4562
  return agent.includes("@deepseek-ai/dsh-desktop/") && cookie.includes("dsh-auth-");
4975
4563
  }
4564
+ function localVerificationOrigin(req) {
4565
+ const port = req.socket.localPort;
4566
+ if (port === void 0 || port <= 0) return void 0;
4567
+ return `${req.socket.encrypted === true ? "https" : "http"}://127.0.0.1:${port}`;
4568
+ }
4976
4569
  async function defaultVerifyHost(req) {
4977
- const host = req.headers.host;
4570
+ const localOrigin = localVerificationOrigin(req);
4978
4571
  const cookie = req.headers.cookie;
4979
- if (host === void 0 || host === "" || cookie === void 0 || cookie === "") return false;
4572
+ if (localOrigin === void 0 || cookie === void 0 || cookie === "") return false;
4980
4573
  try {
4981
- const response = await fetch(`http://${host}/api/version`, {
4574
+ const response = await fetch(`${localOrigin}/api/version`, {
4982
4575
  headers: {
4983
4576
  cookie,
4984
4577
  accept: "application/json"
@@ -4988,6 +4581,15 @@ async function defaultVerifyHost(req) {
4988
4581
  });
4989
4582
  await response.body?.cancel();
4990
4583
  if (response.status >= 300 && response.status < 400) return false;
4584
+ if (response.status === 404) {
4585
+ const anonymous = await fetch(`${localOrigin}/api/version`, {
4586
+ headers: { accept: "application/json" },
4587
+ redirect: "manual",
4588
+ signal: AbortSignal.timeout(2500)
4589
+ });
4590
+ await anonymous.body?.cancel();
4591
+ return anonymous.status === 401 || anonymous.status === 403;
4592
+ }
4991
4593
  return response.status < 500 && response.status !== 401 && response.status !== 403;
4992
4594
  } catch {
4993
4595
  return false;
@@ -5103,7 +4705,7 @@ function canWriteCredentialRef(config, ref) {
5103
4705
  if (!isCredentialRef(ref)) return false;
5104
4706
  if (ref === config.apiKeyEnv) return true;
5105
4707
  for (const account of config.accounts) if (typeof account.apiKeyEnv === "string" && account.apiKeyEnv.trim() === ref) return true;
5106
- return true;
4708
+ return false;
5107
4709
  }
5108
4710
  function configOps(patch) {
5109
4711
  const ops = [];
@@ -5492,96 +5094,6 @@ function applyRoutes(ctx, web, deps) {
5492
5094
  }
5493
5095
  await refreshAndState(ctx, deps, res);
5494
5096
  });
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
5097
  route("/dsh-ollama/discover", async (req, res) => {
5586
5098
  if (!await requirePost(req, res, verifyHost)) return;
5587
5099
  let body;
@@ -6416,6 +5928,6 @@ function apply(ctx, config) {
6416
5928
  }), "dsh-ollama-cloud-provider: plugin updater");
6417
5929
  });
6418
5930
  }
6419
- export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, clearModelProbeCache, createKeyPool, createKeyResolver, createUsageReader, describeModel, effectiveModel, inject, modelProbeCacheInfo, name, normalizeBalance, normalizeUsage, probeModel, resolveConfig };
5931
+ export { Config, OllamaCloudAdapter, PROVIDER, apply, applyCommands, applyRoutes, classifyResponseError, clearCatalogCache, createKeyPool, createKeyResolver, createUsageReader, describeModel, effectiveModel, inject, name, normalizeBalance, normalizeUsage, resolveConfig };
6420
5932
 
6421
5933
  //# sourceMappingURL=index.js.map