File size: 27,891 Bytes
d2880ff
 
cef9371
e61da98
5d3283b
d2880ff
 
0e56f1a
d2880ff
0e56f1a
 
 
 
 
d2880ff
0e56f1a
 
 
 
 
 
 
 
d2880ff
0e56f1a
 
d2880ff
 
 
 
 
 
 
 
0e56f1a
 
d2880ff
 
0e56f1a
d2880ff
 
 
 
e61da98
d2880ff
0e56f1a
 
 
 
 
d2880ff
 
0e56f1a
 
d2880ff
 
 
 
 
 
e61da98
 
 
 
 
d2880ff
 
cef9371
d2880ff
 
 
 
 
 
0e56f1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2880ff
0e56f1a
 
d2880ff
0e56f1a
cef9371
0e56f1a
 
 
 
d2880ff
 
0e56f1a
d2880ff
 
 
 
0e56f1a
cef9371
 
0e56f1a
d2880ff
cef9371
 
 
0e56f1a
d2880ff
 
0e56f1a
 
 
d2880ff
 
e61da98
d2880ff
0e56f1a
 
 
 
 
 
 
 
 
 
 
cef9371
 
 
0e56f1a
 
 
 
 
e61da98
0e56f1a
 
 
 
 
 
46d9e41
0e56f1a
 
 
 
 
46d9e41
0e56f1a
46d9e41
0e56f1a
 
 
 
d2880ff
 
46d9e41
 
 
 
 
 
 
 
5d3283b
 
46d9e41
 
 
 
 
 
 
 
5d3283b
 
 
 
 
46d9e41
 
5d3283b
 
 
46d9e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e56f1a
d2880ff
 
 
46d9e41
0e56f1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2880ff
 
46d9e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e56f1a
d2880ff
 
 
 
 
 
 
 
 
 
cef9371
d2880ff
 
0e56f1a
e61da98
 
 
 
 
 
 
 
 
d2880ff
 
 
 
 
 
 
e61da98
 
 
 
0e56f1a
 
 
 
 
 
 
e61da98
d2880ff
 
 
e61da98
d2880ff
 
 
 
e61da98
 
d2880ff
 
 
 
 
 
 
cef9371
d2880ff
 
 
 
 
 
 
 
 
 
 
 
46d9e41
d2880ff
 
 
 
 
46d9e41
 
 
 
d2880ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d9e41
 
 
d2880ff
 
 
 
 
 
 
 
 
 
0e56f1a
d2880ff
 
 
 
0e56f1a
 
 
 
d2880ff
46d9e41
 
 
 
0e56f1a
 
d2880ff
 
 
 
46d9e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d3283b
46d9e41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2880ff
 
 
46d9e41
d2880ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d9e41
d2880ff
 
 
 
 
 
46d9e41
d2880ff
 
 
 
46d9e41
 
d2880ff
0e56f1a
d2880ff
 
 
 
 
 
 
 
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
// The Model Manager: one place to download, remove and switch local models, and to add,
// edit, test and switch between any number of API models.
import { LOCAL_MODELS, effectiveModel, modelRunsHere } from "./config.js";
import { PARTIAL_CACHE } from "./fast-download.js";
import { hfSignInAvailable, signInWithHuggingFace, cancelSignIn, loadSession, saveSession, deleteSession, sessionExpired, embedded, standaloneUrl } from "./hf-auth.js";
import { API_PRESETS, findPreset, RemoteLLM, loadApiKey, saveApiKey, deleteApiKey, apiKeyRemembered } from "./remote-llm.js";

const AVATAR_BASE = new URL("../vendor/avatars/", import.meta.url).href;

/**
 * A rounded tile with an organization's Hugging Face avatar (vendor/avatars/<name>.png) on
 * a white backing, like an app icon; without an avatar, a neutral tile with a glyph.
 */
export function avatarTile(name, size = "", glyph = null) {
  const tile = document.createElement("span");
  tile.className = `avatar ${size}`.trim();
  if (name) {
    const img = document.createElement("img");
    img.src = AVATAR_BASE + name + ".png";
    img.alt = "";
    img.loading = "lazy";
    img.decoding = "async";
    tile.append(img);
  } else {
    tile.classList.add("glyph");
    if (glyph) tile.append(glyph);
  }
  return tile;
}

const newId = () => "api-" + Math.random().toString(36).slice(2, 10);
const hostOf = (url) => { try { return new URL(url).host; } catch { return url; } };

/**
 * deps: { settings, saveSettings, h, icon, fmtBytes, setup, activate(sel) -> Promise,
 *         isActive(sel), gpuNote() -> {ok, text} | null, isMobile() }
 */
export function createModelManager(deps) {
  const { settings, saveSettings, h, icon, fmtBytes, setup } = deps;
  const dlg = document.getElementById("mm");
  const editDlg = document.getElementById("api-edit");
  const downloads = new Map(); // model id -> { loaded, total, ctrl, error }
  const cached = new Map(); // model id -> bytes in Cache Storage
  const partialIds = new Set(); // models with pieces of an unfinished download
  let busy = null; // id being activated
  let tab = "local";

  const chip = (iconName, text, cls = "") => h("span", { class: `chip ${cls}`.trim() }, iconName ? icon(iconName) : null, text);
  const pill = (text, cls = "") => h("span", { class: `pill ${cls}`.trim() }, text);
  const activePill = () => h("span", { class: "pill live" }, h("i", { class: "dot" }), "Active");

  // ---------- manager ----------
  async function open(which) {
    tab = which ?? (settings.activeModel?.kind === "api" ? "api" : "local");
    render();
    if (!dlg.open) dlg.showModal();
    await refreshCached();
  }

  async function refreshCached() {
    const partialUrls = await caches.open(PARTIAL_CACHE).then((c) => c.keys()).then((ks) => ks.map((r) => r.url)).catch(() => []);
    for (const m of LOCAL_MODELS) {
      if (partialUrls.some((u) => u.startsWith(`https://huggingface.co/${m.repo}/`))) partialIds.add(m.id);
      else if (!downloads.has(m.id)) partialIds.delete(m.id);
    }
    await Promise.all(LOCAL_MODELS.map(async (m) => {
      const [st, bytes] = await Promise.all([setup.modelStatus(m).catch(() => ({ ready: false })), setup.modelCachedBytes(m).catch(() => 0)]);
      cached.set(m.id, st.ready ? bytes || effectiveModel(m).bytes : 0);
    }));
    if (dlg.open) render();
  }
  const isDownloaded = (m) => (cached.get(m.id) ?? 0) > 0;

  function render() {
    renderSummary();
    const downloadedCount = LOCAL_MODELS.filter(isDownloaded).length;
    document.getElementById("mm-tabs").replaceChildren(
      tabButton("local", "On this device", downloadedCount ? `${downloadedCount}/${LOCAL_MODELS.length}` : String(LOCAL_MODELS.length), "cpu"),
      tabButton("api", "API models", String(settings.apiModels.length), "globe"));
    const panel = document.getElementById("mm-panel");
    panel.replaceChildren(...(tab === "local" ? localPanel() : apiPanel()).filter(Boolean));
  }

  function tabButton(id, label, count, iconName) {
    return h("button", { type: "button", role: "tab", class: "mm-tab", "aria-selected": String(tab === id),
      onclick: () => { tab = id; render(); } }, icon(iconName), label, h("span", { class: "count" }, count));
  }

  function renderSummary() {
    const a = settings.activeModel;
    const local = a?.kind === "local" ? LOCAL_MODELS.find((m) => m.id === a.id) : null;
    const api = a?.kind === "api" ? settings.apiModels.find((x) => x.id === a.id) : null;
    const el = document.getElementById("mm-active");
    if (!local && !api) {
      el.className = "mm-summary empty";
      el.replaceChildren(
        h("span", { class: "summary-icon" }, icon("cpu")),
        h("div", {}, h("div", { class: "summary-title" }, "No model yet"),
          h("div", { class: "summary-sub" }, "Download a model to run on your GPU, or add an API model. Then press Use.")));
      return;
    }
    const p = api ? findPreset(api.preset) : null;
    el.className = "mm-summary";
    el.replaceChildren(
      avatarTile(local ? local.avatar : p.avatar, "md", icon("plus")),
      h("div", { class: "summary-text" },
        h("div", { class: "summary-label" }, "Active model"),
        h("div", { class: "summary-title" }, local ? local.label : api.name),
        h("div", { class: "summary-sub" }, local ? `${local.by} · runs on this device with WebGPU` : `${p.label} API · ${api.model}`)),
      activePill());
  }

  // ---------- local models ----------
  function localPanel() {
    const gpu = deps.gpuNote();
    return [
      gpu ? h("div", { class: "mm-banner" }, icon("alert"), h("span", {}, gpu.text)) : null,
      deps.isMobile() ? h("div", { class: "mm-banner" }, icon("alert"), h("span", {}, "On phones, local models usually don't fit in graphics memory. Qwen3.5 0.8B is your best bet, or use an API model.")) : null,
      h("div", { class: "mc-list" }, LOCAL_MODELS.map(localCard)),
      h("p", { class: "mm-foot" }, "Local models run on your GPU with WebGPU and keep working offline once downloaded. Bigger models are smarter but slower and need more graphics memory."),
    ];
  }

  function localCard(m) {
    const sel = { kind: "local", id: m.id };
    const d = downloads.get(m.id);
    const inUse = deps.isActive(sel);
    const have = isDownloaded(m);
    const downloading = d && !d.error;
    const runs = modelRunsHere(m);
    const em = effectiveModel(m);

    let actions;
    if (!runs && !have) {
      actions = [h("button", { class: "btn small", type: "button", disabled: true, title: "This model has no build without f16 shaders" }, "Needs f16")];
    } else if (downloading) {
      actions = [h("button", { class: "btn small", type: "button", onclick: () => d.ctrl.abort() }, "Cancel")];
    } else if (have) {
      actions = [
        inUse ? activePill()
          : h("button", { class: "btn small primary", type: "button", disabled: busy != null, onclick: () => use(sel) }, busy === m.id ? "Loading…" : "Use"),
        h("button", { class: "icon-btn sm", type: "button", title: `Remove ${m.label}`, "aria-label": `Remove ${m.label}`, onclick: () => remove(m) }, icon("trash")),
      ];
    } else {
      actions = [h("button", { class: "btn small primary", type: "button", onclick: () => download(m) }, icon("download"), partialIds.has(m.id) ? "Resume" : "Download")];
    }

    const pct = d?.total ? Math.round((d.loaded / d.total) * 100) : 0;
    return h("div", { class: "mc" + (inUse ? " active" : "") + (downloading ? " busy" : ""), "data-local": m.id },
      avatarTile(m.avatar, "lg"),
      h("div", { class: "mc-body" },
        h("div", { class: "mc-title" },
          h("span", { class: "mc-name" }, m.label),
          m.id === LOCAL_MODELS[0].id ? pill("Recommended", "accent") : null),
        h("div", { class: "mc-by" }, `by ${m.by}`),
        h("p", { class: "mc-note" }, m.note),
        h("div", { class: "chips" },
          !runs ? chip("alert", "Needs f16 shaders", "warn")
            : have ? chip("check", "Downloaded", "ok") : chip("download", fmtBytes(em.bytes)),
          em.variant === "q4" ? chip(null, "q4 build", "muted") : null,
          chip("layers", `${Math.round(m.contextMax / 1024)}k context`),
          chip("brain", m.thinking ? "Reasoning" : "No reasoning", m.thinking ? "" : "muted"),
          chip(null, m.license, "muted")),
        downloading ? h("div", { class: "mc-progress" },
          h("div", { class: "bar" }, h("i", { style: `width:${pct}%` })),
          h("span", { class: "mc-progress-text" }, `${pct}% · ${fmtBytes(d.loaded)} of ${fmtBytes(d.total)}${d.speed ?? ""}`)) : null,
        d?.error ? h("div", { class: "mc-error" }, d.error) : null),
      h("div", { class: "mc-actions" }, actions));
  }

  // ---------- API models ----------
  function apiPanel() {
    const stack = ["huggingface", "openrouter", "openai", "anthropic", "google"];
    const add = h("button", { type: "button", class: "add-card", onclick: () => openEditor(null) },
      h("span", { class: "avatar-stack" }, stack.map((a) => avatarTile(a, "sm"))),
      h("span", { class: "add-text" },
        h("span", { class: "add-title" }, icon("plus"), "Add an API model"),
        h("span", { class: "add-sub" }, `${API_PRESETS.length - 1} providers, from OpenAI and Anthropic to Ollama on your own computer`)));
    const hasHf = settings.apiModels.some((a) => a.auth === "hf-oauth");
    return [
      hfSignInAvailable() && !hasHf ? hfQuickStart() : null,
      settings.apiModels.length ? h("div", { class: "mc-list" }, settings.apiModels.map(apiCard)) : null,
      add,
      h("p", { class: "mm-foot" }, "Only the model runs on the provider's servers: the agent needs internet, and the provider receives your messages and the command output. Linux stays on this device. Keys are sent only to their own provider."),
    ];
  }

  // One click: sign in, pick the default tool-capable model, start using it.
  let quickBusy = false;
  function hfQuickStart() {
    const p = findPreset("huggingface");
    return h("div", { class: "hf-quick" },
      avatarTile("huggingface", "lg"),
      h("div", { class: "hf-quick-text" },
        h("div", { class: "hf-quick-title" }, "Start right away with Hugging Face"),
        h("div", { class: "hf-quick-sub" }, `Sign in and LocalWebAgent uses ${p.defaultModel} through Hugging Face Inference Providers. No API key to copy; usage counts against your Hugging Face account.`),
        embedNote()),
      h("div", { class: "hf-quick-actions" },
        quickBusy
          ? [h("span", { class: "hf-wait" }, h("span", { class: "spinner" }), "Waiting for Hugging Face…"),
            h("button", { class: "linkish plain", type: "button", onclick: () => cancelSignIn() }, "Cancel")]
          : hfButton(() => quickSignIn())));
  }

  function hfButton(onclick, label = "Sign in with Hugging Face") {
    // Embedded in the Space page, the popup can't hand its result back: sign in in the app's own tab.
    if (embedded()) {
      return h("a", { class: "hf-btn", href: standaloneUrl(), target: "_blank", rel: "noopener" },
        avatarTile("huggingface", "xs"), h("span", {}, "Open LocalWebAgent to sign in"));
    }
    return h("button", { class: "hf-btn", type: "button", onclick }, avatarTile("huggingface", "xs"), h("span", {}, label));
  }
  const embedNote = () => embedded()
    ? h("p", { class: "field-hint embed-note" }, "You're using LocalWebAgent inside the Space page. There, the browser keeps its storage separate, so Hugging Face sign-in has to happen in LocalWebAgent's own tab. Models, keys and Linux files in this embedded view are also separate from that tab. An access token works here too.")
    : null;

  async function quickSignIn() {
    quickBusy = true;
    render();
    try {
      const session = await signInWithHuggingFace();
      const p = findPreset("huggingface");
      let model = p.defaultModel;
      try {
        const ids = await RemoteLLM.listModels({ baseUrl: p.baseUrl, key: session.accessToken });
        if (ids.length && !ids.includes(model)) model = ids[0];
      } catch {}
      const entry = { id: newId(), name: model.split("/").pop(), preset: p.id, baseUrl: p.baseUrl, model, auth: "hf-oauth" };
      saveSession(entry.id, session);
      settings.apiModels.push(entry);
      saveSettings();
      quickBusy = false;
      tab = "api";
      await use({ kind: "api", id: entry.id });
    } catch (err) {
      quickBusy = false;
      render();
      if (!err.cancelled) alert(err.message);
    }
  }

  function apiCard(a) {
    const p = findPreset(a.preset);
    const sel = { kind: "api", id: a.id };
    const inUse = deps.isActive(sel);
    if (a.auth === "hf-oauth") return hfCard(a, p, sel, inUse);
    const keyless = !p.noKey && !loadApiKey(a.id);
    return h("div", { class: "mc" + (inUse ? " active" : ""), "data-api": a.id },
      avatarTile(p.avatar, "lg", icon("plus")),
      h("div", { class: "mc-body" },
        h("div", { class: "mc-title" }, h("span", { class: "mc-name" }, a.name)),
        h("div", { class: "mc-by" }, `${p.label} API`),
        h("div", { class: "chips" },
          chip("cpu", a.model, "mono"),
          chip("globe", hostOf(a.baseUrl), "muted"),
          p.noKey ? null : keyless ? chip("key", "Key needed", "warn") : chip("key", apiKeyRemembered(a.id) ? "Key saved" : "Key for this tab", "muted")),
        keyless ? h("div", { class: "mc-error" }, "The key was only kept for an earlier tab. Edit to enter it again.") : null),
      h("div", { class: "mc-actions" },
        inUse ? activePill()
          : h("button", { class: "btn small primary", type: "button", disabled: busy != null, onclick: () => use(sel) }, busy === a.id ? "Connecting…" : "Use"),
        h("span", { class: "mc-icons" },
          h("button", { class: "icon-btn sm", type: "button", title: "Edit", "aria-label": `Edit ${a.name}`, onclick: () => openEditor(a) }, icon("pencil")),
          h("button", { class: "icon-btn sm", type: "button", title: "Delete", "aria-label": `Delete ${a.name}`, onclick: () => removeApi(a) }, icon("trash")))));
  }

  function cardActions(a, sel, inUse) {
    return h("div", { class: "mc-actions" },
      inUse ? activePill()
        : h("button", { class: "btn small primary", type: "button", disabled: busy != null, onclick: () => use(sel) }, busy === a.id ? "Connecting…" : "Use"),
      h("span", { class: "mc-icons" },
        h("button", { class: "icon-btn sm", type: "button", title: "Edit", "aria-label": `Edit ${a.name}`, onclick: () => openEditor(a) }, icon("pencil")),
        h("button", { class: "icon-btn sm", type: "button", title: "Delete", "aria-label": `Delete ${a.name}`, onclick: () => removeApi(a) }, icon("trash"))));
  }

  function hfCard(a, p, sel, inUse) {
    const s = loadSession(a.id);
    const ended = !s || (sessionExpired(s) && !s.refreshToken);
    const who = s?.user?.username ? `@${s.user.username}` : s?.user?.name || "your account";
    return h("div", { class: "mc" + (inUse ? " active" : ""), "data-api": a.id },
      avatarTile(p.avatar, "lg"),
      h("div", { class: "mc-body" },
        h("div", { class: "mc-title" }, h("span", { class: "mc-name" }, a.name)),
        h("div", { class: "mc-by" }, "Hugging Face Inference Providers"),
        h("div", { class: "chips" },
          chip("cpu", a.model, "mono"),
          ended ? chip("key", "Signed out", "warn")
            : h("span", { class: "chip user-chip" }, s.user?.avatarUrl ? h("img", { src: s.user.avatarUrl, alt: "", crossorigin: "anonymous" }) : null, `Signed in as ${who}`)),
        ended ? h("div", { class: "mc-error" }, "Your Hugging Face sign-in has ended. Edit to sign in again.") : null),
      cardActions(a, sel, inUse));
  }

  // ---------- actions ----------
  async function use(sel) {
    busy = sel.id;
    render();
    try { await deps.activate(sel); }
    catch (err) { alert(err.message); }
    finally { busy = null; render(); }
  }

  async function download(m) {
    const ctrl = new AbortController();
    const d = { loaded: 0, total: effectiveModel(m).bytes, ctrl, error: null };
    downloads.set(m.id, d);
    render();
    let lastPaint = 0;
    const samples = []; // [time, bytes] for a moving speed average
    const started = performance.now();
    const speedText = () => {
      if (samples.length < 2) return "";
      const [t0, b0] = samples[0];
      const [t1, b1] = samples[samples.length - 1];
      const bps = ((b1 - b0) / (t1 - t0)) * 1000;
      return bps > 0 ? ` · ${fmtBytes(bps)}/s` : "";
    };
    try {
      await setup.requestPersistence();
      await setup.downloadModel(m, {
        signal: ctrl.signal,
        onProgress: (loaded, total) => {
          d.loaded = loaded; d.total = total;
          const now = performance.now();
          // Skip the first moment: pieces resumed from an earlier attempt arrive instantly.
          if (now - started > 700) samples.push([now, loaded]);
          while (samples.length > 2 && now - samples[0][0] > 4000) samples.shift();
          d.speed = speedText();
          const card = dlg.querySelector(`[data-local="${m.id}"]`);
          if (!card || now - lastPaint < 100) return;
          lastPaint = now;
          const pct = total ? Math.round((loaded / total) * 100) : 0;
          const bar = card.querySelector(".mc-progress .bar i");
          if (bar) bar.style.width = pct + "%";
          const t = card.querySelector(".mc-progress-text");
          if (t) t.textContent = `${pct}% · ${fmtBytes(loaded)} of ${fmtBytes(total)}${d.speed}`;
        },
      });
      downloads.delete(m.id);
      partialIds.delete(m.id);
      await refreshCached();
      // First model ever: start using it right away.
      if (!settings.activeModel) await use({ kind: "local", id: m.id });
    } catch (err) {
      if (err.name === "AbortError") { downloads.delete(m.id); partialIds.add(m.id); } // pieces kept: next time resumes
      else { d.error = `Download failed: ${err.message}. Finished parts are kept, so Resume continues from there.`; partialIds.add(m.id); }
    }
    render();
  }

  async function remove(m) {
    const sel = { kind: "local", id: m.id };
    const inUse = deps.isActive(sel);
    if (!confirm(`Remove ${m.label} (${fmtBytes(cached.get(m.id) || effectiveModel(m).bytes)}) from this browser?${inUse ? " It is in use and will be unloaded." : ""} You can download it again later.`)) return;
    if (inUse) await deps.activate(null);
    await setup.removeModel(m);
    await refreshCached();
    render();
  }

  async function removeApi(a) {
    if (!confirm(`Delete the API model "${a.name}" and its stored key?`)) return;
    if (deps.isActive({ kind: "api", id: a.id })) await deps.activate(null);
    settings.apiModels = settings.apiModels.filter((x) => x.id !== a.id);
    saveSettings();
    deleteApiKey(a.id);
    deleteSession(a.id);
    render();
  }

  // ---------- API model editor ----------
  let editing = null; // existing entry, or null when adding
  let hfSession = null; // session from "Sign in with Hugging Face" in this editor
  let useToken = false; // Hugging Face: the user chose an access token instead of signing in
  let signingIn = false;
  const signInMode = () => !!preset.signIn && hfSignInAvailable() && !useToken;
  let preset = API_PRESETS[0];
  const $e = (id) => document.getElementById(id);
  const note = (text, kind = "") => { const n = $e("ae-note"); n.className = "note " + kind; n.textContent = text; };

  function openEditor(entry = null) {
    editing = entry;
    $e("ae-title").textContent = entry ? "Edit API model" : "Add an API model";
    preset = entry ? findPreset(entry.preset) : API_PRESETS[0];
    $e("ae-base").value = entry?.baseUrl ?? preset.baseUrl;
    $e("ae-model").value = entry?.model ?? "";
    $e("ae-name").value = entry?.name ?? "";
    $e("ae-key").value = entry ? loadApiKey(entry.id) : "";
    $e("ae-remember").checked = entry ? apiKeyRemembered(entry.id) : true;
    $e("ae-models").replaceChildren();
    $e("ae-save").textContent = entry ? "Save" : "Add and use";
    hfSession = entry?.auth === "hf-oauth" ? loadSession(entry.id) : null;
    useToken = !!entry && entry.preset === "huggingface" && entry.auth !== "hf-oauth";
    signingIn = false;
    note("");
    renderProviders();
    syncPreset(false);
    editDlg.showModal();
  }

  function renderProviders() {
    $e("ae-providers").replaceChildren(...API_PRESETS.map((p) => h("button", {
      type: "button", class: "provider" + (p.id === preset.id ? " on" : ""), "aria-pressed": String(p.id === preset.id), title: p.label,
      onclick: () => { preset = p; renderProviders(); syncPreset(true); },
    }, avatarTile(p.avatar, "sm", icon("plus")), h("span", {}, p.label))));
  }

  function syncPreset(fill) {
    if (fill) { $e("ae-base").value = preset.baseUrl; $e("ae-models").replaceChildren(); }
    $e("ae-chosen").replaceChildren(
      avatarTile(preset.avatar, "md", icon("plus")),
      h("div", {}, h("div", { class: "summary-title" }, preset.label),
        h("div", { class: "summary-sub" }, preset.hint ?? (preset.noKey ? "Runs on your computer." : "OpenAI-compatible API."))));
    $e("ae-base-row").hidden = preset.id !== "custom" && !!preset.baseUrl && $e("ae-base").value === preset.baseUrl;
    const signIn = signInMode();
    $e("ae-key-row").hidden = !!preset.noKey || signIn;
    $e("ae-remember-row").hidden = !!preset.noKey || signIn;
    renderHfBlock(signIn);
    $e("ae-keylink").replaceChildren(preset.keysUrl
      ? h("a", { href: preset.keysUrl, target: "_blank", rel: "noopener" }, `Get your ${preset.label} API key`, icon("open")) : "");
    $e("ae-base-shown").textContent = $e("ae-base").value;
    $e("ae-base-summary").hidden = !$e("ae-base-row").hidden;
  }

  // The Hugging Face sign-in block in the editor (replaces the key field).
  function renderHfBlock(signIn) {
    const box = $e("ae-hf");
    box.hidden = !preset.signIn || !hfSignInAvailable();
    if (box.hidden) return;
    if (!signIn) {
      box.replaceChildren(h("button", { class: "linkish plain", type: "button", onclick: () => { useToken = false; syncPreset(false); } }, "Sign in with Hugging Face instead of a token"));
      return;
    }
    const ended = hfSession && sessionExpired(hfSession) && !hfSession.refreshToken;
    if (hfSession && !ended) {
      const u = hfSession.user ?? {};
      box.replaceChildren(h("div", { class: "hf-signed" },
        u.avatarUrl ? h("img", { class: "hf-user-avatar", src: u.avatarUrl, alt: "", crossorigin: "anonymous" }) : avatarTile("huggingface", "md"),
        h("div", { class: "hf-signed-text" },
          h("div", { class: "summary-title" }, u.name ? `Signed in as ${u.name}` : "Signed in"),
          h("div", { class: "summary-sub" }, u.username ? `@${u.username} · Inference Providers access` : "Inference Providers access")),
        h("button", { class: "btn small", type: "button", onclick: () => { hfSession = null; syncPreset(false); } }, "Sign out")));
      return;
    }
    box.replaceChildren(
      signingIn
        ? h("div", { class: "hf-wait big" }, h("span", { class: "spinner" }), "Waiting for Hugging Face…",
          h("button", { class: "linkish plain", type: "button", onclick: () => cancelSignIn() }, "Cancel"))
        : hfButton(() => editorSignIn()),
      embedded() ? embedNote() : h("p", { class: "field-hint" }, ended ? "Your sign-in has ended. Sign in again to keep using this model." : "No key to copy: LocalWebAgent only gets permission to run inference on your account, billed to your Hugging Face account."),
      h("button", { class: "linkish plain", type: "button", onclick: () => { useToken = true; syncPreset(false); $e("ae-key").focus(); } }, "Use an access token instead"));
  }

  async function editorSignIn() {
    signingIn = true;
    renderHfBlock(true);
    try {
      hfSession = await signInWithHuggingFace();
      signingIn = false;
      syncPreset(false);
      // Fill in the model list and a good default right away.
      note("Signed in. Loading models…");
      try {
        const ids = await RemoteLLM.listModels(form());
        $e("ae-models").replaceChildren(...ids.map((id) => h("option", { value: id })));
        if (!$e("ae-model").value) $e("ae-model").value = ids.includes(preset.defaultModel) ? preset.defaultModel : ids[0] ?? "";
        note(`Signed in. ${ids.length} models with tool calling available; ${$e("ae-model").value} is selected.`, "ok");
      } catch (err) { note(err.message, "error"); }
    } catch (err) {
      signingIn = false;
      renderHfBlock(true);
      note(err.cancelled ? "" : err.message, err.cancelled ? "" : "error");
    }
  }

  const form = () => ({
    baseUrl: $e("ae-base").value.trim(),
    model: $e("ae-model").value.trim(),
    key: signInMode() ? hfSession?.accessToken ?? "" : $e("ae-key").value.trim(),
    headers: preset.headers,
  });

  $e("ae-base-edit").addEventListener("click", () => { $e("ae-base-row").hidden = false; $e("ae-base-summary").hidden = true; $e("ae-base").focus(); });
  $e("ae-base").addEventListener("input", () => { $e("ae-base-shown").textContent = $e("ae-base").value; });
  $e("ae-close").addEventListener("click", () => editDlg.close());
  editDlg.addEventListener("click", (e) => { if (e.target === editDlg) editDlg.close(); });
  dlg.addEventListener("click", (e) => { if (e.target === dlg) dlg.close(); });
  $e("mm-close").addEventListener("click", () => dlg.close());

  $e("ae-load-models").addEventListener("click", async () => {
    const f = form();
    if (!f.baseUrl) return note("Enter the base URL first.", "error");
    const btn = $e("ae-load-models");
    btn.disabled = true;
    note("Loading the provider's models…");
    try {
      const ids = await RemoteLLM.listModels(f);
      $e("ae-models").replaceChildren(...ids.map((id) => h("option", { value: id })));
      note(ids.length ? `${ids.length} models found. Start typing in Model ID to pick one.` : "The provider returned no models.", ids.length ? "ok" : "");
      $e("ae-model").focus();
    } catch (err) { note(err.message, "error"); }
    finally { btn.disabled = false; }
  });

  $e("ae-test").addEventListener("click", async () => {
    const f = form();
    if (!f.baseUrl || !f.model) return note("Enter a model ID first.", "error");
    const btn = $e("ae-test");
    btn.disabled = true;
    note("Testing…");
    try { note(`Works. The model replied: "${(await RemoteLLM.test(f)).slice(0, 80)}"`, "ok"); }
    catch (err) { note(err.message, "error"); }
    finally { btn.disabled = false; }
  });

  $e("ae-form").addEventListener("submit", async (e) => {
    e.preventDefault();
    const f = form();
    if (!f.baseUrl || !f.model) return note("Enter a base URL and a model ID.", "error");
    try { new URL(f.baseUrl); } catch { return note("The base URL isn't a valid URL.", "error"); }
    if (!preset.noKey && !f.key) return note(signInMode() ? "Sign in with Hugging Face first." : `Enter your ${preset.label} API key.`, "error");
    const entry = {
      id: editing?.id ?? newId(),
      name: $e("ae-name").value.trim() || f.model,
      preset: preset.id,
      baseUrl: f.baseUrl,
      model: f.model,
      ...(signInMode() ? { auth: "hf-oauth" } : {}),
    };
    if (editing) settings.apiModels = settings.apiModels.map((a) => (a.id === entry.id ? entry : a));
    else settings.apiModels.push(entry);
    saveSettings();
    if (entry.auth === "hf-oauth") { saveSession(entry.id, hfSession); deleteApiKey(entry.id); }
    else { saveApiKey(entry.id, f.key, $e("ae-remember").checked); deleteSession(entry.id); }
    editDlg.close();
    tab = "api";
    render();
    const sel = { kind: "api", id: entry.id };
    // New entries are put to use; edits of the active one reconnect with the new settings.
    if (!editing || deps.isActive(sel)) await use(sel);
  });

  return { open, openEditor, render, refreshCached };
}