llmman 0.1.366

Run any agent on any model, models stored as OCI images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// The model picker (local store + hosted providers with a usable key),
// the Pull dialog and the Models dialog. Nothing here knows a model name;
// everything comes from the daemon.

import * as api from "./api.js";
import * as settings from "./settings.js";
import { toast, formatBytes, $, icon, iconButton } from "./util.js";

const state = {
  local: [], // [{id, loaded, capabilities: [..] | null}]
  providers: [], // [{id, name, models: [{id, cost}] | null}]
  selected: settings.get("model") || "",
  loadedAt: 0,
  loading: null,
  listeners: new Set(),
};

export function selected() {
  return state.selected;
}

export function onChange(fn) {
  state.listeners.add(fn);
  return () => state.listeners.delete(fn);
}

function emit() {
  for (const fn of state.listeners) fn(state.selected);
}

export function select(ref) {
  state.selected = ref || "";
  settings.set({ model: state.selected });
  $("#model-btn-name").textContent = displayName(state.selected);
  emit();
}

/** A short label for a ref: `model · Provider` for hosted ones. */
export function displayName(ref) {
  if (!ref) return "Choose a model";
  const remote = api.splitRemoteRef(ref);
  if (remote) {
    const p = state.providers.find((p) => p.id === remote.provider);
    return `${remote.model} · ${p?.name || remote.provider}`;
  }
  return ref;
}

/** Whether `ref` is offered by the picker right now. */
export function isAvailable(ref) {
  const remote = api.splitRemoteRef(ref);
  if (remote) return state.providers.some((p) => p.id === remote.provider);
  return state.local.some((m) => m.id === ref && chattable(m));
}

export function isLoaded(ref) {
  return state.local.some((m) => m.id === ref && m.loaded);
}

export function markLoaded(ref, loaded = true) {
  const m = state.local.find((m) => m.id === ref);
  if (m) m.loaded = loaded;
}

/** Reload local models and the provider list (not each provider's models). */
export async function refresh({ quiet = false } = {}) {
  if (state.loading) return state.loading;
  state.loading = (async () => {
    try {
      const [local, providers] = await Promise.all([
        api.listLocal(),
        api.listProviders().catch(() => []),
      ]);
      state.local = local.sort((a, b) => a.id.localeCompare(b.id));
      await annotateCapabilities(state.local);
      const usable = providers.filter((p) => p.key_usable);
      // Keep already-fetched model lists for providers still present.
      state.providers = usable
        .map((p) => ({
          id: p.id,
          name: p.name,
          count: p.models,
          models: state.providers.find((q) => q.id === p.id)?.models ?? null,
        }))
        .sort((a, b) => a.name.localeCompare(b.name));
      state.loadedAt = Date.now();
      // First run with nothing chosen: pick a loaded chat model, else the
      // first chat model, so the composer is usable immediately.
      if (!state.selected || !knownRef(state.selected)) {
        const chat = state.local.filter(chattable);
        const pick = chat.find((m) => m.loaded) || chat[0];
        if (pick) select(pick.id);
        else if (state.selected && !knownRef(state.selected)) select("");
      } else {
        $("#model-btn-name").textContent = displayName(state.selected);
      }
    } catch (e) {
      if (!quiet) toast(`Could not list models: ${e.message}`, "error");
    } finally {
      state.loading = null;
    }
  })();
  return state.loading;
}

/**
 * `/api/show` per local model, for its capabilities: a store holds image
 * and video generation models too, and the chat picker should not offer
 * those. Cached by reference across refreshes; a failure leaves `null`,
 * which counts as chattable rather than hiding a model on a hiccup.
 */
const capabilityCache = new Map();
async function annotateCapabilities(local) {
  await Promise.all(
    local.map(async (m) => {
      if (!capabilityCache.has(m.id)) {
        try {
          const info = await api.showModel(m.id);
          capabilityCache.set(m.id, Array.isArray(info.capabilities) ? info.capabilities : null);
        } catch {
          capabilityCache.set(m.id, null);
        }
      }
      m.capabilities = capabilityCache.get(m.id);
    }),
  );
}

/** Whether a local model can take a chat turn. */
export function chattable(m) {
  return !m.capabilities || m.capabilities.includes("completion");
}

/** Capabilities that are not chat, for a label: "image", "video", ... */
function otherCapabilities(m) {
  return (m.capabilities || []).filter((c) => c !== "completion" && c !== "vision" && c !== "tools").join(" · ");
}

function knownRef(ref) {
  if (api.splitRemoteRef(ref)) return true; // provider models are checked lazily
  return state.local.some((m) => m.id === ref && chattable(m));
}

async function ensureProviderModels() {
  await Promise.all(
    state.providers
      .filter((p) => p.models === null)
      .map(async (p) => {
        try {
          p.models = await api.providerModels(p.id);
        } catch {
          p.models = [];
        }
      }),
  );
}

// ---- Picker menu --------------------------------------------------------

let menuOpen = false;

export function initPicker() {
  const btn = $("#model-btn");
  const menu = $("#model-menu");
  const search = $("#model-search");

  btn.addEventListener("click", (e) => {
    e.stopPropagation();
    if (menuOpen) closeMenu();
    else openMenu();
  });
  search.addEventListener("input", renderMenu);
  search.addEventListener("keydown", (e) => {
    if (e.key === "Escape") closeMenu();
    if (e.key === "Enter") {
      const first = menu.querySelector(".menu-item");
      if (first) first.click();
    }
  });
  document.addEventListener("click", (e) => {
    if (menuOpen && !menu.contains(e.target) && e.target !== btn) closeMenu();
  });
  document.addEventListener("keydown", (e) => {
    if (e.key === "Escape" && menuOpen) closeMenu();
  });
  $("#models-refresh").addEventListener("click", async () => {
    state.providers.forEach((p) => (p.models = null));
    await refresh();
    await ensureProviderModels();
    renderMenu();
  });
  $("#pull-open").addEventListener("click", () => {
    closeMenu();
    openPullDialog();
  });
  window.addEventListener("resize", () => menuOpen && positionMenu());
}

async function openMenu() {
  const menu = $("#model-menu");
  const btn = $("#model-btn");
  menuOpen = true;
  btn.setAttribute("aria-expanded", "true");
  menu.classList.remove("hidden");
  $("#model-search").value = "";
  renderMenu();
  positionMenu();
  $("#model-search").focus();
  // Refresh in the background if the list is stale; providers' models load lazily.
  if (Date.now() - state.loadedAt > 5000) await refresh({ quiet: true });
  await ensureProviderModels();
  if (menuOpen) renderMenu();
}

function closeMenu() {
  menuOpen = false;
  $("#model-menu").classList.add("hidden");
  $("#model-btn").setAttribute("aria-expanded", "false");
}

function positionMenu() {
  const menu = $("#model-menu");
  const view = $("#view-chat").getBoundingClientRect();
  const rect = $("#model-btn").getBoundingClientRect();
  menu.style.bottom = `${view.bottom - rect.top + 8}px`;
  menu.style.right = `${Math.max(8, view.right - rect.right)}px`;
  menu.style.left = "auto";
  menu.style.top = "auto";
  // Never taller than the room above the button.
  menu.style.maxHeight = `${Math.max(160, rect.top - view.top - 16)}px`;
}

function renderMenu() {
  const body = $("#model-menu-body");
  const q = $("#model-search").value.trim().toLowerCase();
  const match = (s) => !q || s.toLowerCase().includes(q);
  body.replaceChildren();

  const chatModels = state.local.filter(chattable);
  const local = chatModels.filter((m) => match(m.id));
  body.appendChild(section("i-chip", "Local"));
  if (!local.length) {
    body.appendChild(
      emptyRow(
        chatModels.length
          ? "No local model matches."
          : state.local.length
            ? "No local chat models — the store has only image/video models."
            : "No local models yet — pull one below, or `llmman pull <ref>`.",
      ),
    );
  }
  for (const m of local) {
    body.appendChild(
      menuItem({
        ref: m.id,
        name: m.id,
        sub: m.loaded ? "loaded" : "",
        dot: m.loaded ? "ok" : "",
        eject: m.loaded,
      }),
    );
  }
  const others = state.local.filter((m) => !chattable(m)).length;
  if (others && !q) {
    body.appendChild(emptyRow(`${others} image/video model${others === 1 ? "" : "s"} not shown — see Models.`));
  }

  for (const p of state.providers) {
    const models = p.models;
    const shown = models ? models.filter((m) => match(m.id) || match(p.name)) : [];
    if (models && !shown.length && q) continue;
    body.appendChild(section("i-cloud", p.name));
    if (models === null) {
      body.appendChild(emptyRow("Loading…"));
      continue;
    }
    if (!shown.length) {
      body.appendChild(emptyRow("No models listed."));
      continue;
    }
    for (const m of shown) {
      body.appendChild(
        menuItem({
          ref: api.remoteRef(p.id, m.id),
          name: m.id,
          sub: m.cost ? `$${trimNumber(m.cost.input)} / $${trimNumber(m.cost.output)} per M` : "",
        }),
      );
    }
  }

  if (!state.providers.length && !q) {
    const hint = emptyRow(
      "Hosted providers appear here when llmman serve has a key for them (e.g. OPENAI_API_KEY or [providers] in llmman.conf) and is bound to loopback.",
    );
    body.appendChild(hint);
  }
}

function trimNumber(n) {
  if (n == null) return "?";
  return Number(n).toString();
}

function section(iconId, label) {
  const d = document.createElement("div");
  d.className = "menu-section";
  d.appendChild(icon(iconId));
  d.appendChild(document.createTextNode(label));
  return d;
}

function emptyRow(text) {
  const d = document.createElement("div");
  d.className = "menu-empty";
  d.textContent = text;
  return d;
}

/** A row: the select button, plus an unload button beside it when loaded. */
function menuItem({ ref, name, sub, dot, eject }) {
  const row = document.createElement("div");
  row.className = "menu-row";
  const b = document.createElement("button");
  b.type = "button";
  b.className = "menu-item" + (ref === state.selected ? " selected" : "");
  b.setAttribute("role", "menuitem");
  b.title = ref;
  if (dot !== undefined) {
    const d = document.createElement("span");
    d.className = "dot status-dot " + (dot || "");
    b.appendChild(d);
  }
  const n = document.createElement("span");
  n.className = "menu-item-name";
  n.textContent = name;
  b.appendChild(n);
  if (sub) {
    const s = document.createElement("span");
    s.className = "menu-item-sub";
    s.textContent = sub;
    b.appendChild(s);
  }
  b.addEventListener("click", () => {
    select(ref);
    closeMenu();
  });
  row.appendChild(b);
  if (eject) {
    row.appendChild(
      iconButton("i-eject", `Unload ${name}`, () => unload(ref).then(renderMenu), "icon-btn eject"),
    );
  }
  return row;
}

async function unload(ref) {
  try {
    await api.unloadModel(ref);
    markLoaded(ref, false);
    toast(`Unloaded ${ref}`);
  } catch (err) {
    toast(err.message, "error");
  }
}

// ---- Pull dialog ------------------------------------------------------

let pullAbort = null;

export function initPullDialog() {
  const dialog = $("#pull-dialog");
  const form = $("#pull-form");
  form.addEventListener("submit", (e) => {
    if (e.submitter?.value === "pull") {
      e.preventDefault();
      startPull();
    }
  });
  $("#pull-cancel").addEventListener("click", () => dialog.close());
  dialog.addEventListener("close", () => {
    pullAbort?.abort();
    pullAbort = null;
  });
  $("#models-dialog-pull").addEventListener("click", () => {
    $("#models-dialog").close();
    openPullDialog();
  });
}

export function openPullDialog(prefill = "") {
  const dialog = $("#pull-dialog");
  $("#pull-ref").value = prefill;
  $("#pull-ref").disabled = false;
  $("#pull-go").disabled = false;
  $("#pull-progress").classList.add("hidden");
  $("#pull-bar").style.width = "0";
  $("#pull-bar").classList.remove("indeterminate");
  $("#pull-status").textContent = "";
  $("#pull-detail").textContent = "";
  dialog.showModal();
  $("#pull-ref").focus();
}

async function startPull() {
  const ref = $("#pull-ref").value.trim();
  if (!ref) return;
  $("#pull-ref").disabled = true;
  $("#pull-go").disabled = true;
  $("#pull-progress").classList.remove("hidden");
  $("#pull-status").textContent = `Pulling ${ref}`;
  $("#pull-bar").classList.add("indeterminate");
  const abort = new AbortController();
  pullAbort = abort;
  const before = new Set(state.local.map((m) => m.id));
  const layers = new Map(); // digest → {total, completed}
  try {
    await api.pull(
      ref,
      (ev) => {
        if (ev.status) $("#pull-status").textContent = ev.status;
        if (ev.total) {
          layers.set(ev.digest || ev.status || "_", { total: ev.total, completed: ev.completed || 0 });
          let total = 0, done = 0;
          for (const l of layers.values()) (total += l.total), (done += l.completed);
          if (total > 0) {
            $("#pull-bar").classList.remove("indeterminate");
            $("#pull-bar").style.width = `${Math.min(100, (100 * done) / total).toFixed(1)}%`;
            $("#pull-detail").textContent = `${formatBytes(done)} of ${formatBytes(total)}`;
          }
        }
      },
      abort.signal,
    );
    $("#pull-bar").classList.remove("indeterminate");
    $("#pull-bar").style.width = "100%";
    $("#pull-status").textContent = "Done";
    toast(`Pulled ${ref}`);
    await refresh({ quiet: true });
    // What appeared is what was pulled; if it was already there, match
    // the reference as the daemon canonicalizes it.
    const local =
      state.local.find((m) => !before.has(m.id) && chattable(m)) ||
      state.local.find((m) => sameModel(m.id, ref));
    if (local) select(local.id);
    $("#pull-dialog").close();
  } catch (e) {
    if (e.name === "AbortError") return;
    $("#pull-bar").classList.remove("indeterminate");
    $("#pull-status").textContent = "Pull failed";
    $("#pull-detail").textContent = e.message;
    $("#pull-ref").disabled = false;
    $("#pull-go").disabled = false;
  } finally {
    if (pullAbort === abort) pullAbort = null;
  }
}

/**
 * Whether stored `id` is `ref` as the daemon canonicalized it: a registry
 * host and/or a default tag added, never a longer name (`myfoo` for `foo`).
 */
function sameModel(id, ref) {
  const forms = [ref, `${ref}:latest`];
  return forms.some((f) => id === f || id.endsWith(`/${f}`));
}

// ---- Models dialog ----------------------------------------------------

export function initModelsDialog() {
  $("#nav-models").addEventListener("click", openModelsDialog);
  $("#models-close").addEventListener("click", () => $("#models-dialog").close());
}

async function openModelsDialog() {
  const dialog = $("#models-dialog");
  const body = $("#models-dialog-body");
  body.replaceChildren(emptyRow("Loading…"));
  dialog.showModal();
  await renderModelsDialog();
}

async function renderModelsDialog() {
  const body = $("#models-dialog-body");
  let tags, running;
  try {
    [tags, running] = await Promise.all([api.listLocalDetailed(), api.listRunning().catch(() => [])]);
  } catch (e) {
    body.replaceChildren(emptyRow(e.message));
    return;
  }
  const loaded = new Set(running.map((m) => m.name || m.model));
  body.replaceChildren();
  if (!tags.length) {
    body.appendChild(emptyRow("No local models. Pull one to get started."));
  }
  for (const m of tags.sort((a, b) => a.name.localeCompare(b.name))) {
    const row = document.createElement("div");
    row.className = "models-row";
    const dot = document.createElement("span");
    dot.className = "dot " + (loaded.has(m.name) ? "ok" : "");
    dot.title = loaded.has(m.name) ? "loaded" : "not loaded";
    row.appendChild(dot);
    const name = document.createElement("span");
    name.className = "name";
    name.textContent = m.name;
    name.title = m.digest || "";
    row.appendChild(name);
    const fmt = document.createElement("span");
    fmt.className = "meta";
    const d = m.details || {};
    const known = state.local.find((l) => l.id === m.name);
    const caps = known ? otherCapabilities(known) : "";
    fmt.textContent = [caps, d.format, d.parameter_size, d.quantization_level].filter(Boolean).join(" · ");
    row.appendChild(fmt);
    const size = document.createElement("span");
    size.className = "meta";
    size.textContent = formatBytes(m.size || 0);
    row.appendChild(size);
    const actions = document.createElement("span");
    actions.className = "actions";
    if (!known || chattable(known)) {
      actions.appendChild(
        iconButton("i-check", "Use in chat", () => {
          select(m.name);
          $("#models-dialog").close();
        }),
      );
    }
    if (loaded.has(m.name)) {
      actions.appendChild(iconButton("i-eject", "Unload", () => unload(m.name).then(renderModelsDialog)));
    }
    actions.appendChild(
      iconButton("i-trash", "Delete from this machine", async () => {
        if (!confirm(`Delete ${m.name} from the local store?`)) return;
        try {
          await api.deleteModel(m.name);
          toast(`Deleted ${m.name}`);
          await refresh({ quiet: true });
        } catch (e) {
          toast(e.message, "error");
        }
        renderModelsDialog();
      }),
    );
    row.appendChild(actions);
    body.appendChild(row);
  }
}