ereniko commited on
Commit
d2880ff
·
unverified ·
1 Parent(s): ea31752

v0.6.0: Model Manager, Linux-only setup, multiple API models with logos

Browse files
Files changed (10) hide show
  1. README.md +31 -16
  2. css/app.css +73 -16
  3. index.html +44 -40
  4. js/config.js +1 -1
  5. js/logos.js +14 -0
  6. js/main.js +144 -204
  7. js/model-manager.js +312 -0
  8. js/remote-llm.js +66 -22
  9. js/setup.js +43 -0
  10. sw.js +2 -2
README.md CHANGED
@@ -25,17 +25,20 @@ custom_headers:
25
 
26
  An AI agent with its own Linux computer, entirely inside one web page.
27
 
28
- - **Model:** [LFM3-1.7B](https://huggingface.co/LiquidAI/LFM3-1.7B) by default, or one of [several other local models](#local-models), running on your GPU through WebGPU with [transformers.js](https://github.com/huggingface/transformers.js). If your device can't run them, you can [use an API model instead](#use-an-api-model).
29
  - **Computer:** a real Debian (i386) machine running in the browser on [CheerpX](https://cheerpx.io), the engine behind [WebVM](https://webvm.io). It always runs on your device, whichever model you use.
30
  - **Agent harness:** the model calls `run_command`, `write_file` and `read_file` tools. Commands run in the same terminal you see, so you can watch and take over at any time.
31
  - **File viewer:** browse the machine's files and open code (syntax highlighted), text, Markdown, images, audio and video. HTML pages open as sandboxed live previews together with the stylesheets, scripts, images and data files they reference, and links between pages work.
32
- - **Offline:** the first visit downloads everything (about 1.6 GB) into your browser's storage. After that it works with no internet connection.
 
33
 
34
- **Requirements for the local model:** desktop Chrome or Edge with WebGPU and `shader-f16` support, about 2.5 GB of free disk space, and 8 GB or more RAM. Open the app at its own URL (`https://<owner>-localwebagent.static.hf.space`), because the embedded view on huggingface.co can't enable the cross-origin isolation CheerpX needs.
 
 
35
 
36
  ## Local models
37
 
38
- Pick one in **Settings → Model → Change**, or with "Choose a different local model" on the setup screen. All are 4-bit (q4f16) ONNX builds loaded straight from their Hugging Face repositories; switching downloads the new model once, and the model picker can delete models you no longer use.
39
 
40
  | Model | Download | Context | Reasoning | Notes |
41
  |---|---:|---:|:---:|---|
@@ -50,28 +53,39 @@ The context size setting is capped at each model's own window.
50
 
51
  ## Phones and low-memory devices
52
 
53
- The default model needs about 1.4 GB of graphics memory. Most phones can't give that much to a browser tab, so the app warns you on phones and suggests an API model or the smaller Qwen3.5 0.8B instead. You can still try any local model. If it fails to load, the app explains why (out of graphics memory, no WebGPU support, or a failed download), keeps Linux running, and offers to switch to an API model, pick a smaller model, or try again. The raw error is under "Technical details".
54
 
55
  To make the local model fit on more devices, the weights are kept on disk and read tensor by tensor while they are copied to the GPU, instead of being held in memory all at once. On phones and devices reporting less than 8 GB of RAM, the model also loads before Linux starts, so the two don't compete for memory.
56
 
57
- ## Use an API model
58
 
59
- Any OpenAI-compatible Chat Completions API works, for example OpenRouter, OpenAI, the Hugging Face router, Groq, or a local Ollama or LM Studio server. Open it from the setup screen ("Use an API model instead"), from the error message when the local model fails, or from **Settings → Model → Change**:
60
 
61
- | Field | What to enter |
62
- |---|---|
63
- | Provider | A preset fills in the base URL. Choose "Other" for anything else. |
64
- | Base URL | The API root, for example `https://openrouter.ai/api/v1`. |
65
- | API key | Your provider's key. Leave it empty for local servers that don't need one. |
66
- | Model ID | As your provider lists it. Pick a model that supports tool calling. |
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- Use **Test** to check the settings before saving. Only the model moves to the provider: the Linux machine and its files stay in your browser. That also means the agent needs an internet connection, and the provider receives your messages and the output of the commands the agent runs.
69
 
70
- The key stays only until you close the tab, unless you turn on "Remember key on this device", which saves it in this browser's local storage. The provider must allow requests from web pages (CORS); OpenRouter, OpenAI and the Hugging Face router do. To go back to the local model, use "Use the local model" in the same dialog.
71
 
72
  ## Settings
73
 
74
- - **Context size:** how much of the conversation the model sees, in thousands of tokens. The defaults are 32k for the local model (longer prompts are slower and use more GPU memory) and 128k for API models (set it to your model's context window). Each backend keeps its own value. A single command's output can use up to about 15% of it; older output is trimmed only when the conversation no longer fits.
75
  - **Reasoning:** let the model think before each step. Slower, more reliable on multi-step tasks.
76
  - **Approve commands:** ask before running each command or writing a file.
77
  - **Max steps per task** and **command timeout**.
@@ -82,3 +96,4 @@ The key stays only until you close the tab, unless you turn on "Remember key on
82
  - LFM3-1.7B and LFM3-Omni-2B: [LFM Open License v1.0](https://huggingface.co/LiquidAI/LFM3-1.7B-ONNX/blob/main/LICENSE). Qwen3, Qwen3.5 and Granite 4.0: Apache-2.0. No weights are re-hosted here: your browser downloads them directly from the repositories linked above.
83
  - CheerpX: proprietary, free for personal and open-source projects. It is **not** included in this Space. Each visitor's browser loads it from Leaning Technologies' CDN and caches it locally.
84
  - The Debian disk image (`disk/`) contains Debian packages under their respective free licenses.
 
 
25
 
26
  An AI agent with its own Linux computer, entirely inside one web page.
27
 
28
+ - **Model:** [LFM3-1.7B](https://huggingface.co/LiquidAI/LFM3-1.7B) by default, or one of [several other local models](#local-models), running on your GPU through WebGPU with [transformers.js](https://github.com/huggingface/transformers.js). If your device can't run them, you can [use an API model instead](#api-models).
29
  - **Computer:** a real Debian (i386) machine running in the browser on [CheerpX](https://cheerpx.io), the engine behind [WebVM](https://webvm.io). It always runs on your device, whichever model you use.
30
  - **Agent harness:** the model calls `run_command`, `write_file` and `read_file` tools. Commands run in the same terminal you see, so you can watch and take over at any time.
31
  - **File viewer:** browse the machine's files and open code (syntax highlighted), text, Markdown, images, audio and video. HTML pages open as sandboxed live previews together with the stylesheets, scripts, images and data files they reference, and links between pages work.
32
+ - **Model Manager:** download, remove and switch local models, and save any number of API models from 15 providers, all in one place.
33
+ - **Offline:** setup installs only the Linux machine (about 175 MB). Local models are downloaded separately in the Model Manager. Once downloaded, everything works with no internet connection.
34
 
35
+ **Getting started:** open the app, install Linux, and the Model Manager opens. Download a local model or add an API model, press **Use**, and start a task. Open the Model Manager again any time from the chip icon in the title bar, the model name next to the logo, or **Settings → Model**.
36
+
37
+ **Requirements for local models:** desktop Chrome or Edge with WebGPU and `shader-f16` support, about 2.5 GB of free disk space, and 8 GB or more RAM. Open the app at its own URL (`https://<owner>-localwebagent.static.hf.space`), because the embedded view on huggingface.co can't enable the cross-origin isolation CheerpX needs.
38
 
39
  ## Local models
40
 
41
+ Download, remove and switch them in the Model Manager. Downloads show their progress, can be cancelled, and are stored in the browser so the model also works offline. Switching models doesn't reload the page. The first model you download is put to use automatically. All are 4-bit (q4f16) ONNX builds loaded straight from their Hugging Face repositories.
42
 
43
  | Model | Download | Context | Reasoning | Notes |
44
  |---|---:|---:|:---:|---|
 
53
 
54
  ## Phones and low-memory devices
55
 
56
+ The default model needs about 1.4 GB of graphics memory. Most phones can't give that much to a browser tab, so the app warns you on phones and suggests an API model or the smaller Qwen3.5 0.8B instead. Linux installs either way, and you can still try any local model. If one fails to load, the app explains why (out of graphics memory, no WebGPU support, or a failed download), keeps Linux running, and offers the Model Manager or another try. The raw error is under "Technical details".
57
 
58
  To make the local model fit on more devices, the weights are kept on disk and read tensor by tensor while they are copied to the GPU, instead of being held in memory all at once. On phones and devices reporting less than 8 GB of RAM, the model also loads before Linux starts, so the two don't compete for memory.
59
 
60
+ ## API models
61
 
62
+ Any number of API models can be saved; use **Add API model** in the Model Manager. Pick a provider, paste your key, and choose a model ID. **Load list** fetches the provider's models to choose from, and **Test** sends a tiny request to check everything works.
63
 
64
+ | Provider | Base URL | Notes |
65
+ |---|---|---|
66
+ | OpenRouter | `https://openrouter.ai/api/v1` | Models from every major lab behind one key. |
67
+ | OpenAI | `https://api.openai.com/v1` | |
68
+ | Anthropic | `https://api.anthropic.com/v1` | Through Anthropic's OpenAI-compatible endpoint. |
69
+ | Google Gemini | `https://generativelanguage.googleapis.com/v1beta/openai` | |
70
+ | Hugging Face | `https://router.huggingface.co/v1` | Inference Providers; the token needs the Inference permission. |
71
+ | Groq | `https://api.groq.com/openai/v1` | |
72
+ | Mistral AI | `https://api.mistral.ai/v1` | |
73
+ | DeepSeek | `https://api.deepseek.com/v1` | |
74
+ | xAI | `https://api.x.ai/v1` | |
75
+ | Together AI | `https://api.together.xyz/v1` | |
76
+ | Fireworks AI | `https://api.fireworks.ai/inference/v1` | |
77
+ | Cerebras | `https://api.cerebras.ai/v1` | |
78
+ | Ollama | `http://localhost:11434/v1` | On your computer. Set `OLLAMA_ORIGINS` to the app's address. |
79
+ | LM Studio | `http://localhost:1234/v1` | On your computer. Turn on CORS in the server settings. |
80
+ | Other | any | Any OpenAI-compatible Chat Completions API that allows requests from web pages (CORS). |
81
 
82
+ Every hosted provider above was checked to accept requests from web pages. Pick a model that supports tool calling. Only the model moves to the provider: the Linux machine and its files stay in your browser. That also means the agent needs an internet connection, and the provider receives your messages and the output of the commands the agent runs.
83
 
84
+ Each API model keeps its own key, only until you close the tab unless you turn on "Remember key on this device", which saves it in this browser's local storage. Keys are sent only to that model's provider.
85
 
86
  ## Settings
87
 
88
+ - **Context size:** how much of the conversation the model sees, in thousands of tokens. The defaults are 32k for local models (longer prompts are slower and use more GPU memory; capped at each model's window) and 128k for API models (set it to your model's context window). Each backend keeps its own value. A single command's output can use up to about 15% of it; older output is trimmed only when the conversation no longer fits.
89
  - **Reasoning:** let the model think before each step. Slower, more reliable on multi-step tasks.
90
  - **Approve commands:** ask before running each command or writing a file.
91
  - **Max steps per task** and **command timeout**.
 
96
  - LFM3-1.7B and LFM3-Omni-2B: [LFM Open License v1.0](https://huggingface.co/LiquidAI/LFM3-1.7B-ONNX/blob/main/LICENSE). Qwen3, Qwen3.5 and Granite 4.0: Apache-2.0. No weights are re-hosted here: your browser downloads them directly from the repositories linked above.
97
  - CheerpX: proprietary, free for personal and open-source projects. It is **not** included in this Space. Each visitor's browser loads it from Leaning Technologies' CDN and caches it locally.
98
  - The Debian disk image (`disk/`) contains Debian packages under their respective free licenses.
99
+ - Provider logos come from [Simple Icons](https://simpleicons.org) (CC0-1.0). The marks are trademarks of their owners and are shown only to identify each provider; providers without a Simple Icons mark get a plain letter badge.
css/app.css CHANGED
@@ -497,24 +497,81 @@ dialog[open] { animation: pop .14s ease-out; }
497
  .field-hint { margin: -4px 0 8px; font-size: 12px; color: var(--text-3); }
498
  .note.ok { color: var(--ok); }
499
  .dlg-foot { display: flex; align-items: center; gap: 8px; padding: 12px 22px 18px; border-top: 1px solid var(--border); }
500
- #api-dlg, #model-dlg { max-height: calc(100dvh - 32px); overflow-y: auto; }
501
- #model-dlg { width: min(560px, calc(100% - 32px)); }
502
- .model-list { display: flex; flex-direction: column; gap: 8px; }
503
- .model-opt {
504
- display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; cursor: pointer;
505
- border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
506
- }
507
- .model-opt:hover { background: var(--surface-2); }
508
- .model-opt:has(input:checked) { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
509
- .model-opt input { margin: 3px 0 0; accent-color: var(--primary); flex: none; }
510
- .mo-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
511
- .mo-name { font-weight: 600; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
512
  .mo-tag { font-size: 10.5px; font-weight: 500; color: var(--text-2); border: 1px solid var(--border-strong); border-radius: 99px; padding: 0 7px; }
513
- .mo-note { font-size: 12.5px; color: var(--text-2); }
514
- .mo-meta { font-size: 11.5px; color: var(--text-3); }
515
- .mo-state { font-size: 11.5px; color: var(--text-3); display: flex; gap: 10px; align-items: center; margin-top: 2px; }
516
- .mo-dl { color: var(--ok); }
517
  .linkish { border: 0; background: none; padding: 0; color: var(--bad); font-size: 11.5px; text-decoration: underline; cursor: pointer; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  #tog-thinking:disabled { opacity: .45; cursor: not-allowed; }
519
 
520
  /* ---------- narrow screens ---------- */
 
497
  .field-hint { margin: -4px 0 8px; font-size: 12px; color: var(--text-3); }
498
  .note.ok { color: var(--ok); }
499
  .dlg-foot { display: flex; align-items: center; gap: 8px; padding: 12px 22px 18px; border-top: 1px solid var(--border); }
 
 
 
 
 
 
 
 
 
 
 
 
500
  .mo-tag { font-size: 10.5px; font-weight: 500; color: var(--text-2); border: 1px solid var(--border-strong); border-radius: 99px; padding: 0 7px; }
501
+ .mo-tag.on { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 50%, var(--border)); }
 
 
 
502
  .linkish { border: 0; background: none; padding: 0; color: var(--bad); font-size: 11.5px; text-decoration: underline; cursor: pointer; }
503
+ .linkish.plain { color: var(--text-2); font-size: 12px; }
504
+
505
+ /* titlebar model button */
506
+ button.brand-model { border: 0; background: none; padding: 2px 6px; margin: -2px -6px; border-radius: 6px; cursor: pointer; font: inherit; color: inherit; }
507
+ button.brand-model:hover { background: var(--surface-2); }
508
+
509
+ /* logo tiles (provider logos from Simple Icons, or letter badges) */
510
+ .logo-tile {
511
+ flex: none; width: 36px; height: 36px; border-radius: 10px; display: inline-grid; place-items: center;
512
+ background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
513
+ }
514
+ .logo-tile svg { width: 20px; height: 20px; }
515
+ .logo-tile.mono { color: #fff; border-color: transparent; font: 600 13px/1 var(--sans); letter-spacing: -.02em; }
516
+ .logo-tile.mono.small-text { font-size: 10.5px; }
517
+ .logo-tile.sm { width: 28px; height: 28px; border-radius: 8px; }
518
+ .logo-tile.sm svg { width: 16px; height: 16px; }
519
+ .logo-tile.sm.mono { font-size: 11px; }
520
+ .logo-tile.sm.mono.small-text { font-size: 8.5px; }
521
+
522
+ /* Model Manager */
523
+ #mm, #api-edit { max-height: calc(100dvh - 32px); overflow-y: auto; }
524
+ #mm { width: min(680px, calc(100% - 32px)); }
525
+ #api-edit { width: min(560px, calc(100% - 32px)); }
526
+ .mm-active { margin: 4px 22px 4px; padding: 10px 12px; border-radius: var(--radius-sm); background: var(--surface-2); font-size: 12.5px; color: var(--text-2); }
527
+ .mm-active strong { color: var(--text); font-weight: 600; }
528
+ .mm-list { display: flex; flex-direction: column; gap: 8px; }
529
+ .mm-row {
530
+ display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: start; padding: 12px 14px;
531
+ border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
532
+ }
533
+ .mm-row.active { border-color: color-mix(in srgb, var(--ok) 55%, var(--border)); }
534
+ .mm-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
535
+ .mm-name { font-weight: 600; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
536
+ .mm-note { font-size: 12.5px; color: var(--text-2); }
537
+ .mm-meta { font-size: 11.5px; color: var(--text-3); overflow-wrap: anywhere; }
538
+ .mm-error { font-size: 12px; color: var(--bad); }
539
+ .mm-row progress { width: 100%; height: 4px; margin-top: 6px; appearance: none; border: 0; border-radius: 99px; background: var(--surface-3); overflow: hidden; }
540
+ .mm-row progress::-webkit-progress-bar { background: var(--surface-3); }
541
+ .mm-row progress::-webkit-progress-value { background: var(--text); transition: width .2s; }
542
+ .mm-row progress::-moz-progress-bar { background: var(--text); }
543
+ .mm-actions { display: flex; flex-direction: column; align-items: stretch; gap: 6px; min-width: 96px; }
544
+ .mm-progress-text { font-size: 11.5px; color: var(--text-2); text-align: right; font-variant-numeric: tabular-nums; }
545
+ .mm-empty { margin: 0; padding: 14px; border: 1px dashed var(--border-strong); border-radius: var(--radius); color: var(--text-3); font-size: 12.5px; }
546
+ .mm-warn { margin: 0 0 10px; padding: 8px 10px; border-radius: var(--radius-sm); font-size: 12.5px; color: var(--text-2);
547
+ border: 1px solid color-mix(in srgb, var(--warn) 50%, var(--border)); }
548
+ .mm-section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
549
+ .mm-section-head .section-label { margin: 0; }
550
+ .btn svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; }
551
+
552
+ /* API model editor */
553
+ .providers { display: grid; grid-template-columns: repeat(auto-fill, minmax(146px, 1fr)); gap: 6px; margin-bottom: 10px; }
554
+ .provider {
555
+ display: flex; align-items: center; gap: 8px; padding: 6px 8px; min-width: 0; border-radius: 10px; text-align: left;
556
+ border: 1px solid var(--border); background: var(--surface); font-size: 12px; font-weight: 500; color: var(--text);
557
+ }
558
+ .provider span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
559
+ .provider:hover { background: var(--surface-2); }
560
+ .provider.on { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
561
+ .ae-hint { margin: 0 0 10px; }
562
+ .ae-hint a { color: var(--text); }
563
+ .ae-base-summary { margin: 0 0 12px; font-size: 12px; color: var(--text-3); overflow-wrap: anywhere; }
564
+ .field-inline { display: flex; gap: 8px; }
565
+ .field-inline input { flex: 1; }
566
+ .field-inline .btn { height: 36px; }
567
+ .callout.info { border-color: color-mix(in srgb, var(--focus) 45%, var(--border)); }
568
+ .callout.info .callout-title svg { color: var(--focus); }
569
+
570
+ @media (max-width: 560px) {
571
+ .mm-row { grid-template-columns: auto 1fr; }
572
+ .mm-actions { grid-column: 1 / -1; flex-direction: row; justify-content: flex-end; min-width: 0; }
573
+ .mm-progress-text { margin-right: auto; text-align: left; align-self: center; }
574
+ }
575
  #tog-thinking:disabled { opacity: .45; cursor: not-allowed; }
576
 
577
  /* ---------- narrow screens ---------- */
index.html CHANGED
@@ -42,6 +42,8 @@
42
  <symbol id="i-alert" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/><path d="M12 8v4M12 16h.01"/></symbol>
43
  <symbol id="i-maximize" viewBox="0 0 24 24"><path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/></symbol>
44
  <symbol id="i-minimize" viewBox="0 0 24 24"><path d="M4 14h6v6M20 10h-6V4M14 10l7-7M3 21l7-7"/></symbol>
 
 
45
  <symbol id="i-open" viewBox="0 0 24 24"><path d="M15 3h6v6M10 14 21 3M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></symbol>
46
  </defs>
47
  </svg>
@@ -51,7 +53,7 @@
51
  <img src="icon.svg" alt="" width="18" height="18">
52
  <span class="brand-name">LocalWebAgent</span>
53
  <span class="brand-sep">/</span>
54
- <span class="brand-model mono" id="model-name">LFM3-1.7B</span>
55
  <span class="brand-device mono" id="gpu-name">WebGPU</span>
56
  </div>
57
  <div class="status" role="status" aria-live="polite">
@@ -60,6 +62,7 @@
60
  <span class="status-item" data-status="offline" data-state="idle"><i></i><span class="status-text">Offline</span></span>
61
  </div>
62
  <div class="titlebar-actions">
 
63
  <button class="icon-btn" id="new-chat" title="New conversation" aria-label="New conversation"><svg><use href="#i-new"/></svg></button>
64
  <button class="icon-btn" id="open-settings" title="Settings" aria-label="Settings"><svg><use href="#i-sliders"/></svg></button>
65
  </div>
@@ -134,23 +137,18 @@
134
  <div class="banner warn" id="mobile-banner" hidden>
135
  <svg><use href="#i-alert"/></svg>
136
  <div>
137
- <strong>Phones usually can't run the local model.</strong>
138
- It needs about 1.4 GB of graphics memory, more than most phones give a browser tab. Use an API model instead: the Linux machine still runs on your phone, only the model runs on the provider's servers. You can also pick the smaller Qwen3.5 0.8B (0.47 GB) and try it anyway.
139
  </div>
140
  </div>
141
- <h2>Install for offline use</h2>
142
- <p class="lede">A language model and a Linux machine, both running inside this browser tab. Download once, about 1.6 GB, and it keeps working without an internet connection.</p>
143
 
144
  <div class="section-label">Requirements</div>
145
  <ul class="checks" id="checks"></ul>
146
 
147
  <div class="section-label">Components</div>
148
  <div class="components">
149
- <div class="component" data-row="model" data-state="todo">
150
- <div class="c-main"><span class="c-name">LFM3 1.7B</span><span class="c-desc">Language model by Liquid AI, q4f16 ONNX</span></div>
151
- <div class="c-side"><span class="c-size mono" id="model-size"></span><span class="row-state">Not installed</span></div>
152
- <progress max="1" value="0"></progress>
153
- </div>
154
  <div class="component" data-row="disk" data-state="todo">
155
  <div class="c-main"><span class="c-name">Debian 10 disk</span><span class="c-desc">Python, GCC, Node, Git, SQLite and coreutils</span></div>
156
  <div class="c-side"><span class="c-size mono" id="disk-size">170 MB</span><span class="row-state">Not installed</span></div>
@@ -164,11 +162,9 @@
164
  </div>
165
 
166
  <button class="btn primary block" id="install" disabled>Install</button>
167
- <button class="btn block secondary" id="setup-api" type="button">Use an API model instead</button>
168
- <button class="btn block secondary" id="setup-model" type="button">Choose a different local model</button>
169
  <p class="note" id="setup-note"></p>
170
  <p class="fine">
171
- The model is <a href="https://huggingface.co/LiquidAI/LFM3-1.7B" target="_blank" rel="noopener">LFM3-1.7B</a> (LFM Open License), loaded from Hugging Face.
172
  <a href="https://cheerpx.io" target="_blank" rel="noopener">CheerpX</a> is free for personal and open-source use; it loads from Leaning Technologies' CDN and is cached by your browser.
173
  </p>
174
  </div>
@@ -184,7 +180,7 @@
184
  <div class="section-label">Model</div>
185
  <div class="row-setting">
186
  <span><span class="rs-title">Language model</span><span class="rs-desc" id="set-model-desc"></span></span>
187
- <button type="button" class="btn" id="set-model">Change</button>
188
  </div>
189
  </div>
190
  <div class="dlg-section">
@@ -224,53 +220,61 @@
224
  </form>
225
  </dialog>
226
 
227
- <dialog id="model-dlg" aria-labelledby="model-title">
228
  <div class="dlg-head">
229
- <h2 id="model-title">Choose a model</h2>
230
- <button class="icon-btn" type="button" id="model-close" aria-label="Close"><svg><use href="#i-x"/></svg></button>
231
  </div>
 
232
  <div class="dlg-section">
233
  <div class="section-label">On this device</div>
234
  <p class="dlg-lede">Runs on your GPU with WebGPU and works offline once downloaded. Bigger models are more capable but slower and need more graphics memory.</p>
235
- <div class="model-list" id="model-list"></div>
 
236
  </div>
237
  <div class="dlg-section">
238
- <div class="section-label">Through an API</div>
239
- <div class="row-setting">
240
- <span><span class="rs-title">API model</span><span class="rs-desc" id="model-api-desc"></span></span>
241
- <button type="button" class="btn" id="model-api">Set up</button>
242
  </div>
243
- </div>
244
- <div class="dlg-foot">
245
- <span class="spacer"></span>
246
- <button type="button" class="btn primary" id="model-save">Use this model</button>
247
  </div>
248
  </dialog>
249
 
250
- <dialog id="api-dlg" aria-labelledby="api-title">
251
- <form id="api-form" novalidate>
252
  <div class="dlg-head">
253
- <h2 id="api-title">Use an API model</h2>
254
- <button class="icon-btn" type="button" id="api-close" aria-label="Close"><svg><use href="#i-x"/></svg></button>
255
  </div>
256
  <div class="dlg-section">
257
- <p class="dlg-lede">The Linux machine keeps running in this tab. Only the language model runs on the provider's servers, so the agent needs an internet connection and the provider receives your messages and the command output. Any OpenAI-compatible Chat Completions API works.</p>
258
- <label class="field"><span class="field-label">Provider</span><select id="api-preset"></select></label>
259
- <label class="field"><span class="field-label">Base URL</span><input id="api-base" type="url" placeholder="https://api.example.com/v1" spellcheck="false" autocomplete="off"></label>
260
- <label class="field"><span class="field-label">API key</span><input id="api-key" type="password" placeholder="Leave empty for local servers without a key" spellcheck="false" autocomplete="off"></label>
261
- <label class="field"><span class="field-label">Model ID</span><input id="api-model" placeholder="As listed by your provider" spellcheck="false" autocomplete="off"></label>
 
 
 
 
 
 
 
 
 
262
  <p class="field-hint">Pick a model that supports tool calling.</p>
 
263
  <label class="row-setting">
264
  <span><span class="rs-title">Remember key on this device</span><span class="rs-desc">Saved in this browser. When off, the key is forgotten when you close the tab.</span></span>
265
- <input type="checkbox" class="switch" id="api-remember">
266
  </label>
267
- <p class="note" id="api-note" role="status"></p>
268
  </div>
269
  <div class="dlg-foot">
270
- <button type="button" class="btn" id="api-local" hidden>Use the local model</button>
271
  <span class="spacer"></span>
272
- <button type="button" class="btn" id="api-test">Test</button>
273
- <button type="submit" class="btn primary" id="api-save">Save and use</button>
274
  </div>
275
  </form>
276
  </dialog>
 
42
  <symbol id="i-alert" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9"/><path d="M12 8v4M12 16h.01"/></symbol>
43
  <symbol id="i-maximize" viewBox="0 0 24 24"><path d="M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7"/></symbol>
44
  <symbol id="i-minimize" viewBox="0 0 24 24"><path d="M4 14h6v6M20 10h-6V4M14 10l7-7M3 21l7-7"/></symbol>
45
+ <symbol id="i-cpu" viewBox="0 0 24 24"><rect x="6" y="6" width="12" height="12" rx="2"/><path d="M9 2v4M15 2v4M9 18v4M15 18v4M2 9h4M2 15h4M18 9h4M18 15h4"/></symbol>
46
+ <symbol id="i-plus" viewBox="0 0 24 24"><path d="M12 5v14M5 12h14"/></symbol>
47
  <symbol id="i-open" viewBox="0 0 24 24"><path d="M15 3h6v6M10 14 21 3M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/></symbol>
48
  </defs>
49
  </svg>
 
53
  <img src="icon.svg" alt="" width="18" height="18">
54
  <span class="brand-name">LocalWebAgent</span>
55
  <span class="brand-sep">/</span>
56
+ <button class="brand-model mono" id="model-name" type="button" title="Open the Model Manager">No model</button>
57
  <span class="brand-device mono" id="gpu-name">WebGPU</span>
58
  </div>
59
  <div class="status" role="status" aria-live="polite">
 
62
  <span class="status-item" data-status="offline" data-state="idle"><i></i><span class="status-text">Offline</span></span>
63
  </div>
64
  <div class="titlebar-actions">
65
+ <button class="icon-btn" id="open-models" title="Model Manager" aria-label="Model Manager"><svg><use href="#i-cpu"/></svg></button>
66
  <button class="icon-btn" id="new-chat" title="New conversation" aria-label="New conversation"><svg><use href="#i-new"/></svg></button>
67
  <button class="icon-btn" id="open-settings" title="Settings" aria-label="Settings"><svg><use href="#i-sliders"/></svg></button>
68
  </div>
 
137
  <div class="banner warn" id="mobile-banner" hidden>
138
  <svg><use href="#i-alert"/></svg>
139
  <div>
140
+ <strong>Phones usually can't run local models.</strong>
141
+ They need 0.5 to 3 GB of graphics memory, more than most phones give a browser tab. Linux installs fine either way. Afterwards, in the Model Manager, add an API model (Linux keeps running on your phone, only the model runs on the provider's servers) or try the small Qwen3.5 0.8B.
142
  </div>
143
  </div>
144
+ <h2>Install the Linux machine</h2>
145
+ <p class="lede">A real Debian computer running inside this browser tab. Install it once (about 175 MB) and it keeps working without an internet connection. Next you'll choose a language model in the Model Manager: download one that runs on your GPU, or connect an API.</p>
146
 
147
  <div class="section-label">Requirements</div>
148
  <ul class="checks" id="checks"></ul>
149
 
150
  <div class="section-label">Components</div>
151
  <div class="components">
 
 
 
 
 
152
  <div class="component" data-row="disk" data-state="todo">
153
  <div class="c-main"><span class="c-name">Debian 10 disk</span><span class="c-desc">Python, GCC, Node, Git, SQLite and coreutils</span></div>
154
  <div class="c-side"><span class="c-size mono" id="disk-size">170 MB</span><span class="row-state">Not installed</span></div>
 
162
  </div>
163
 
164
  <button class="btn primary block" id="install" disabled>Install</button>
 
 
165
  <p class="note" id="setup-note"></p>
166
  <p class="fine">
167
+ Language models are added afterwards and load straight from their Hugging Face repositories under their own licenses.
168
  <a href="https://cheerpx.io" target="_blank" rel="noopener">CheerpX</a> is free for personal and open-source use; it loads from Leaning Technologies' CDN and is cached by your browser.
169
  </p>
170
  </div>
 
180
  <div class="section-label">Model</div>
181
  <div class="row-setting">
182
  <span><span class="rs-title">Language model</span><span class="rs-desc" id="set-model-desc"></span></span>
183
+ <button type="button" class="btn" id="set-model">Model Manager</button>
184
  </div>
185
  </div>
186
  <div class="dlg-section">
 
220
  </form>
221
  </dialog>
222
 
223
+ <dialog id="mm" class="mm" aria-labelledby="mm-title">
224
  <div class="dlg-head">
225
+ <h2 id="mm-title">Model Manager</h2>
226
+ <button class="icon-btn" type="button" id="mm-close" aria-label="Close"><svg><use href="#i-x"/></svg></button>
227
  </div>
228
+ <div class="mm-active" id="mm-active"></div>
229
  <div class="dlg-section">
230
  <div class="section-label">On this device</div>
231
  <p class="dlg-lede">Runs on your GPU with WebGPU and works offline once downloaded. Bigger models are more capable but slower and need more graphics memory.</p>
232
+ <div id="mm-local-note"></div>
233
+ <div class="mm-list" id="mm-local"></div>
234
  </div>
235
  <div class="dlg-section">
236
+ <div class="mm-section-head">
237
+ <div class="section-label">API models</div>
238
+ <button type="button" class="btn small" id="mm-add-api"><svg><use href="#i-plus"/></svg>Add API model</button>
 
239
  </div>
240
+ <p class="dlg-lede">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.</p>
241
+ <div class="mm-list" id="mm-api"></div>
 
 
242
  </div>
243
  </dialog>
244
 
245
+ <dialog id="api-edit" class="api-edit" aria-labelledby="ae-title">
246
+ <form id="ae-form" novalidate>
247
  <div class="dlg-head">
248
+ <h2 id="ae-title">Add an API model</h2>
249
+ <button class="icon-btn" type="button" id="ae-close" aria-label="Close"><svg><use href="#i-x"/></svg></button>
250
  </div>
251
  <div class="dlg-section">
252
+ <div class="section-label">Provider</div>
253
+ <div class="providers" id="ae-providers"></div>
254
+ <p class="field-hint ae-hint" id="ae-hint"></p>
255
+ <p class="ae-base-summary" id="ae-base-summary"><span class="mono" id="ae-base-shown"></span> <button type="button" class="linkish plain" id="ae-base-edit">Change URL</button></p>
256
+ <label class="field" id="ae-base-row"><span class="field-label">Base URL</span><input id="ae-base" type="url" placeholder="https://api.example.com/v1" spellcheck="false" autocomplete="off"></label>
257
+ <label class="field" id="ae-key-row"><span class="field-label">API key</span><input id="ae-key" type="password" spellcheck="false" autocomplete="off"></label>
258
+ <div class="field">
259
+ <span class="field-label">Model ID</span>
260
+ <div class="field-inline">
261
+ <input id="ae-model" list="ae-models" placeholder="As listed by the provider" spellcheck="false" autocomplete="off">
262
+ <button type="button" class="btn" id="ae-load-models">Load list</button>
263
+ </div>
264
+ <datalist id="ae-models"></datalist>
265
+ </div>
266
  <p class="field-hint">Pick a model that supports tool calling.</p>
267
+ <label class="field"><span class="field-label">Name (optional)</span><input id="ae-name" placeholder="Shown in the Model Manager; defaults to the model ID" autocomplete="off"></label>
268
  <label class="row-setting">
269
  <span><span class="rs-title">Remember key on this device</span><span class="rs-desc">Saved in this browser. When off, the key is forgotten when you close the tab.</span></span>
270
+ <input type="checkbox" class="switch" id="ae-remember">
271
  </label>
272
+ <p class="note" id="ae-note" role="status"></p>
273
  </div>
274
  <div class="dlg-foot">
 
275
  <span class="spacer"></span>
276
+ <button type="button" class="btn" id="ae-test">Test</button>
277
+ <button type="submit" class="btn primary" id="ae-save">Add and use</button>
278
  </div>
279
  </form>
280
  </dialog>
js/config.js CHANGED
@@ -1,5 +1,5 @@
1
  // Central configuration. Everything that pins a version or a location lives here.
2
- export const APP_VERSION = "0.5.1";
3
 
4
  // --- Local language models (loaded from the Hugging Face Hub, cached in Cache Storage) ---
5
  // Each runs on WebGPU through transformers.js. `chunks` is how many external weight files
 
1
  // Central configuration. Everything that pins a version or a location lives here.
2
+ export const APP_VERSION = "0.6.0";
3
 
4
  // --- Local language models (loaded from the Hugging Face Hub, cached in Cache Storage) ---
5
  // Each runs on WebGPU through transformers.js. `chunks` is how many external weight files
js/logos.js ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Brand logos from Simple Icons (https://simpleicons.org, v16.37.0), CC0-1.0.
2
+ // The marks themselves are trademarks of their owners and are shown only to identify
3
+ // each provider. Generated; each entry is a single 24x24 SVG path plus the brand color.
4
+ export const LOGOS = {
5
+ "anthropic": { title: "Anthropic", hex: "#191919", path: "M17.3041 3.541h-3.6718l6.696 16.918H24Zm-10.6082 0L0 20.459h3.7442l1.3693-3.5527h7.0052l1.3693 3.5528h3.7442L10.5363 3.5409Zm-.3712 10.2232 2.2914-5.9456 2.2914 5.9456Z" },
6
+ "huggingface": { title: "Hugging Face", hex: "#FFD21E", path: "M12.025 1.13c-5.77 0-10.449 4.647-10.449 10.378 0 1.112.178 2.181.503 3.185.064-.222.203-.444.416-.577a.96.96 0 0 1 .524-.15c.293 0 .584.124.84.284.278.173.48.408.71.694.226.282.458.611.684.951v-.014c.017-.324.106-.622.264-.874s.403-.487.762-.543c.3-.047.596.06.787.203s.31.313.4.467c.15.257.212.468.233.542.01.026.653 1.552 1.657 2.54.616.605 1.01 1.223 1.082 1.912.055.537-.096 1.059-.38 1.572.637.121 1.294.187 1.967.187.657 0 1.298-.063 1.921-.178-.287-.517-.44-1.041-.384-1.581.07-.69.465-1.307 1.081-1.913 1.004-.987 1.647-2.513 1.657-2.539.021-.074.083-.285.233-.542.09-.154.208-.323.4-.467a1.08 1.08 0 0 1 .787-.203c.359.056.604.29.762.543s.247.55.265.874v.015c.225-.34.457-.67.683-.952.23-.286.432-.52.71-.694.257-.16.547-.284.84-.285a.97.97 0 0 1 .524.151c.228.143.373.388.43.625l.006.04a10.3 10.3 0 0 0 .534-3.273c0-5.731-4.678-10.378-10.449-10.378M8.327 6.583a1.5 1.5 0 0 1 .713.174 1.487 1.487 0 0 1 .617 2.013c-.183.343-.762-.214-1.102-.094-.38.134-.532.914-.917.71a1.487 1.487 0 0 1 .69-2.803m7.486 0a1.487 1.487 0 0 1 .689 2.803c-.385.204-.536-.576-.916-.71-.34-.12-.92.437-1.103.094a1.487 1.487 0 0 1 .617-2.013 1.5 1.5 0 0 1 .713-.174m-10.68 1.55a.96.96 0 1 1 0 1.921.96.96 0 0 1 0-1.92m13.838 0a.96.96 0 1 1 0 1.92.96.96 0 0 1 0-1.92M8.489 11.458c.588.01 1.965 1.157 3.572 1.164 1.607-.007 2.984-1.155 3.572-1.164.196-.003.305.12.305.454 0 .886-.424 2.328-1.563 3.202-.22-.756-1.396-1.366-1.63-1.32q-.011.001-.02.006l-.044.026-.01.008-.03.024q-.018.017-.035.036l-.032.04a1 1 0 0 0-.058.09l-.014.025q-.049.088-.11.19a1 1 0 0 1-.083.116 1.2 1.2 0 0 1-.173.18q-.035.029-.075.058a1.3 1.3 0 0 1-.251-.243 1 1 0 0 1-.076-.107c-.124-.193-.177-.363-.337-.444-.034-.016-.104-.008-.2.022q-.094.03-.216.087-.06.028-.125.063l-.13.074q-.067.04-.136.086a3 3 0 0 0-.135.096 3 3 0 0 0-.26.219 2 2 0 0 0-.12.121 2 2 0 0 0-.106.128l-.002.002a2 2 0 0 0-.09.132l-.001.001a1.2 1.2 0 0 0-.105.212q-.013.036-.024.073c-1.139-.875-1.563-2.317-1.563-3.203 0-.334.109-.457.305-.454m.836 10.354c.824-1.19.766-2.082-.365-3.194-1.13-1.112-1.789-2.738-1.789-2.738s-.246-.945-.806-.858-.97 1.499.202 2.362c1.173.864-.233 1.45-.685.64-.45-.812-1.683-2.896-2.322-3.295s-1.089-.175-.938.647 2.822 2.813 2.562 3.244-1.176-.506-1.176-.506-2.866-2.567-3.49-1.898.473 1.23 2.037 2.16c1.564.932 1.686 1.178 1.464 1.53s-3.675-2.511-4-1.297c-.323 1.214 3.524 1.567 3.287 2.405-.238.839-2.71-1.587-3.216-.642-.506.946 3.49 2.056 3.522 2.064 1.29.33 4.568 1.028 5.713-.624m5.349 0c-.824-1.19-.766-2.082.365-3.194 1.13-1.112 1.789-2.738 1.789-2.738s.246-.945.806-.858.97 1.499-.202 2.362c-1.173.864.233 1.45.685.64.451-.812 1.683-2.896 2.322-3.295s1.089-.175.938.647-2.822 2.813-2.562 3.244 1.176-.506 1.176-.506 2.866-2.567 3.49-1.898-.473 1.23-2.037 2.16c-1.564.932-1.686 1.178-1.464 1.53s3.675-2.511 4-1.297c.323 1.214-3.524 1.567-3.287 2.405.238.839 2.71-1.587 3.216-.642.506.946-3.49 2.056-3.522 2.064-1.29.33-4.568 1.028-5.713-.624" },
7
+ "openrouter": { title: "OpenRouter", hex: "#94A3B8", path: "M16.778 1.844v1.919q-.569-.026-1.138-.032-.708-.008-1.415.037c-1.93.126-4.023.728-6.149 2.237-2.911 2.066-2.731 1.95-4.14 2.75-.396.223-1.342.574-2.185.798-.841.225-1.753.333-1.751.333v4.229s.768.108 1.61.333c.842.224 1.789.575 2.185.799 1.41.798 1.228.683 4.14 2.75 2.126 1.509 4.22 2.11 6.148 2.236.88.058 1.716.041 2.555.005v1.918l7.222-4.168-7.222-4.17v2.176c-.86.038-1.611.065-2.278.021-1.364-.09-2.417-.357-3.979-1.465-2.244-1.593-2.866-2.027-3.68-2.508.889-.518 1.449-.906 3.822-2.59 1.56-1.109 2.614-1.377 3.978-1.466.667-.044 1.418-.017 2.278.02v2.176L24 6.014Z" },
8
+ "mistralai": { title: "Mistral AI", hex: "#FA520F", path: "M17.143 3.429v3.428h-3.429v3.429h-3.428V6.857H6.857V3.43H3.43v13.714H0v3.428h10.286v-3.428H6.857v-3.429h3.429v3.429h3.429v-3.429h3.428v3.429h-3.428v3.428H24v-3.428h-3.43V3.429z" },
9
+ "googlegemini": { title: "Google Gemini", hex: "#8E75B2", path: "M11.04 19.32Q12 21.51 12 24q0-2.49.93-4.68.96-2.19 2.58-3.81t3.81-2.55Q21.51 12 24 12q-2.49 0-4.68-.93a12.3 12.3 0 0 1-3.81-2.58 12.3 12.3 0 0 1-2.58-3.81Q12 2.49 12 0q0 2.49-.96 4.68-.93 2.19-2.55 3.81a12.3 12.3 0 0 1-3.81 2.58Q2.49 12 0 12q2.49 0 4.68.96 2.19.93 3.81 2.55t2.55 3.81" },
10
+ "deepseek": { title: "DeepSeek", hex: "#5786FE", path: "M23.748 4.651c-.254-.124-.364.113-.512.233-.051.04-.094.09-.137.137-.372.397-.806.657-1.373.626-.829-.046-1.537.214-2.163.848-.133-.782-.575-1.248-1.247-1.548-.352-.155-.708-.311-.955-.65-.172-.24-.219-.509-.305-.774-.055-.16-.11-.323-.293-.35-.2-.031-.278.136-.356.276-.313.572-.434 1.202-.422 1.84.027 1.436.633 2.58 1.838 3.393.137.094.172.187.129.323-.082.28-.18.553-.266.833-.055.179-.137.218-.328.14a5.5 5.5 0 0 1-1.737-1.179c-.857-.828-1.631-1.743-2.597-2.46a12 12 0 0 0-.689-.47c-.985-.957.13-1.743.387-1.836.27-.098.094-.433-.778-.428-.872.003-1.67.295-2.687.685a3 3 0 0 1-.465.136 9.6 9.6 0 0 0-2.883-.101c-1.885.21-3.39 1.1-4.497 2.622C.082 8.776-.231 10.854.152 13.02c.403 2.284 1.568 4.175 3.36 5.653 1.857 1.533 3.997 2.284 6.438 2.14 1.482-.085 3.132-.284 4.994-1.86.47.234.962.328 1.78.398.629.058 1.235-.031 1.705-.129.735-.155.684-.836.418-.961-2.155-1.004-1.682-.595-2.112-.926 1.095-1.295 2.768-3.598 3.284-6.733.05-.346.115-.834.108-1.114-.004-.171.035-.238.23-.257a4.2 4.2 0 0 0 1.545-.475c1.397-.763 1.96-2.016 2.093-3.517.02-.23-.004-.467-.247-.588M11.58 18.168c-2.088-1.642-3.101-2.183-3.52-2.16-.39.024-.32.472-.234.763.09.288.207.487.371.74.114.167.192.416-.113.603-.673.416-1.842-.14-1.897-.168-1.361-.801-2.5-1.86-3.301-3.306-.775-1.393-1.225-2.888-1.299-4.482-.02-.385.094-.522.477-.592a4.7 4.7 0 0 1 1.53-.038c2.131.311 3.946 1.264 5.467 2.774.868.86 1.525 1.887 2.202 2.89.72 1.066 1.494 2.082 2.48 2.915.348.291.626.513.892.677-.802.09-2.14.109-3.055-.615zm1.001-6.44a.306.306 0 0 1 .415-.287.3.3 0 0 1 .113.074.3.3 0 0 1 .086.214c0 .17-.136.307-.308.307a.303.303 0 0 1-.306-.307m3.11 1.596c-.2.081-.4.151-.591.16a1.25 1.25 0 0 1-.798-.254c-.274-.23-.47-.358-.551-.758a1.7 1.7 0 0 1 .015-.588c.07-.327-.007-.537-.238-.727-.188-.156-.426-.199-.689-.199a.6.6 0 0 1-.254-.078.253.253 0 0 1-.114-.358 1 1 0 0 1 .192-.21c.356-.202.767-.136 1.146.016.352.144.618.408 1.001.782.392.451.462.576.685.915.176.264.336.536.446.848.066.194-.02.353-.25.45" },
11
+ "ollama": { title: "Ollama", hex: "#000000", path: "M16.361 10.26a.894.894 0 0 0-.558.47l-.072.148.001.207c0 .193.004.217.059.353.076.193.152.312.291.448.24.238.51.3.872.205a.86.86 0 0 0 .517-.436.752.752 0 0 0 .08-.498c-.064-.453-.33-.782-.724-.897a1.06 1.06 0 0 0-.466 0zm-9.203.005c-.305.096-.533.32-.65.639a1.187 1.187 0 0 0-.06.52c.057.309.31.59.598.667.362.095.632.033.872-.205.14-.136.215-.255.291-.448.055-.136.059-.16.059-.353l.001-.207-.072-.148a.894.894 0 0 0-.565-.472 1.02 1.02 0 0 0-.474.007Zm4.184 2c-.131.071-.223.25-.195.383.031.143.157.288.353.407.105.063.112.072.117.136.004.038-.01.146-.029.243-.02.094-.036.194-.036.222.002.074.07.195.143.253.064.052.076.054.255.059.164.005.198.001.264-.03.169-.082.212-.234.15-.525-.052-.243-.042-.28.087-.355.137-.08.281-.219.324-.314a.365.365 0 0 0-.175-.48.394.394 0 0 0-.181-.033c-.126 0-.207.03-.355.124l-.085.053-.053-.032c-.219-.13-.259-.145-.391-.143a.396.396 0 0 0-.193.032zm.39-2.195c-.373.036-.475.05-.654.086-.291.06-.68.195-.951.328-.94.46-1.589 1.226-1.787 2.114-.04.176-.045.234-.045.53 0 .294.005.357.043.524.264 1.16 1.332 2.017 2.714 2.173.3.033 1.596.033 1.896 0 1.11-.125 2.064-.727 2.493-1.571.114-.226.169-.372.22-.602.039-.167.044-.23.044-.523 0-.297-.005-.355-.045-.531-.288-1.29-1.539-2.304-3.072-2.497a6.873 6.873 0 0 0-.855-.031zm.645.937a3.283 3.283 0 0 1 1.44.514c.223.148.537.458.671.662.166.251.26.508.303.82.02.143.01.251-.043.482-.08.345-.332.705-.672.957a3.115 3.115 0 0 1-.689.348c-.382.122-.632.144-1.525.138-.582-.006-.686-.01-.853-.042-.57-.107-1.022-.334-1.35-.68-.264-.28-.385-.535-.45-.946-.03-.192.025-.509.137-.776.136-.326.488-.73.836-.963.403-.269.934-.46 1.422-.512.187-.02.586-.02.773-.002zm-5.503-11a1.653 1.653 0 0 0-.683.298C5.617.74 5.173 1.666 4.985 2.819c-.07.436-.119 1.04-.119 1.503 0 .544.064 1.24.155 1.721.02.107.031.202.023.208a8.12 8.12 0 0 1-.187.152 5.324 5.324 0 0 0-.949 1.02 5.49 5.49 0 0 0-.94 2.339 6.625 6.625 0 0 0-.023 1.357c.091.78.325 1.438.727 2.04l.13.195-.037.064c-.269.452-.498 1.105-.605 1.732-.084.496-.095.629-.095 1.294 0 .67.009.803.088 1.266.095.555.288 1.143.503 1.534.071.128.243.393.264.407.007.003-.014.067-.046.141a7.405 7.405 0 0 0-.548 1.873c-.062.417-.071.552-.071.991 0 .56.031.832.148 1.279L3.42 24h1.478l-.05-.091c-.297-.552-.325-1.575-.068-2.597.117-.472.25-.819.498-1.296l.148-.29v-.177c0-.165-.003-.184-.057-.293a.915.915 0 0 0-.194-.25 1.74 1.74 0 0 1-.385-.543c-.424-.92-.506-2.286-.208-3.451.124-.486.329-.918.544-1.154a.787.787 0 0 0 .223-.531c0-.195-.07-.355-.224-.522a3.136 3.136 0 0 1-.817-1.729c-.14-.96.114-2.005.69-2.834.563-.814 1.353-1.336 2.237-1.475.199-.033.57-.028.776.01.226.04.367.028.512-.041.179-.085.268-.19.374-.431.093-.215.165-.333.36-.576.234-.29.46-.489.822-.729.413-.27.884-.467 1.352-.561.17-.035.25-.04.569-.04.319 0 .398.005.569.04a4.07 4.07 0 0 1 1.914.997c.117.109.398.457.488.602.034.057.095.177.132.267.105.241.195.346.374.43.14.068.286.082.503.045.343-.058.607-.053.943.016 1.144.23 2.14 1.173 2.581 2.437.385 1.108.276 2.267-.296 3.153-.097.15-.193.27-.333.419-.301.322-.301.722-.001 1.053.493.539.801 1.866.708 3.036-.062.772-.26 1.463-.533 1.854a2.096 2.096 0 0 1-.224.258.916.916 0 0 0-.194.25c-.054.109-.057.128-.057.293v.178l.148.29c.248.476.38.823.498 1.295.253 1.008.231 2.01-.059 2.581a.845.845 0 0 0-.044.098c0 .006.329.009.732.009h.73l.02-.074.036-.134c.019-.076.057-.3.088-.516.029-.217.029-1.016 0-1.258-.11-.875-.295-1.57-.597-2.226-.032-.074-.053-.138-.046-.141.008-.005.057-.074.108-.152.376-.569.607-1.284.724-2.228.031-.26.031-1.378 0-1.628-.083-.645-.182-1.082-.348-1.525a6.083 6.083 0 0 0-.329-.7l-.038-.064.131-.194c.402-.604.636-1.262.727-2.04a6.625 6.625 0 0 0-.024-1.358 5.512 5.512 0 0 0-.939-2.339 5.325 5.325 0 0 0-.95-1.02 8.097 8.097 0 0 1-.186-.152.692.692 0 0 1 .023-.208c.208-1.087.201-2.443-.017-3.503-.19-.924-.535-1.658-.98-2.082-.354-.338-.716-.482-1.15-.455-.996.059-1.8 1.205-2.116 3.01a6.805 6.805 0 0 0-.097.726c0 .036-.007.066-.015.066a.96.96 0 0 1-.149-.078A4.857 4.857 0 0 0 12 3.03c-.832 0-1.687.243-2.456.698a.958.958 0 0 1-.148.078c-.008 0-.015-.03-.015-.066a6.71 6.71 0 0 0-.097-.725C8.997 1.392 8.337.319 7.46.048a2.096 2.096 0 0 0-.585-.041Zm.293 1.402c.248.197.523.759.682 1.388.03.113.06.244.069.292.007.047.026.152.041.233.067.365.098.76.102 1.24l.002.475-.12.175-.118.178h-.278c-.324 0-.646.041-.954.124l-.238.06c-.033.007-.038-.003-.057-.144a8.438 8.438 0 0 1 .016-2.323c.124-.788.413-1.501.696-1.711.067-.05.079-.049.157.013zm9.825-.012c.17.126.358.46.498.888.28.854.36 2.028.212 3.145-.019.14-.024.151-.057.144l-.238-.06a3.693 3.693 0 0 0-.954-.124h-.278l-.119-.178-.119-.175.002-.474c.004-.669.066-1.19.214-1.772.157-.623.434-1.185.68-1.382.078-.062.09-.063.159-.012z" },
12
+ "lmstudio": { title: "LM Studio", hex: "#000000", path: "M14.025 0c3.492 0 5.237 0 6.571.68a6.24 6.24 0 0 1 2.725 2.724C24 4.738 24 6.484 24 9.975v4.05c0 3.492 0 5.237-.68 6.571a6.24 6.24 0 0 1-2.724 2.725c-1.334.679-3.08.679-6.571.679h-4.05c-3.492 0-5.237 0-6.571-.68A6.24 6.24 0 0 1 .68 20.597C0 19.262 0 17.516 0 14.025v-4.05c0-3.492 0-5.237.68-6.571A6.23 6.23 0 0 1 3.404.68C4.738 0 6.484 0 9.975 0zM7.688 16.313a1.313 1.313 0 0 0 0 2.625h11.625a1.313 1.313 0 0 0 0-2.625zm-3-3.75a1.313 1.313 0 0 0 0 2.624h11.625a1.313 1.313 0 0 0 0-2.624zm3-3.75a1.313 1.313 0 0 0 0 2.624h11.625a1.313 1.313 0 0 0 0-2.624zm-3-3.75a1.313 1.313 0 0 0 0 2.625h11.625a1.313 1.313 0 0 0 0-2.625z" },
13
+ "perplexity": { title: "Perplexity", hex: "#1FB8CD", path: "M22.3977 7.0896h-2.3106V.0676l-7.5094 6.3542V.1577h-1.1554v6.1966L4.4904 0v7.0896H1.6023v10.3976h2.8882V24l6.932-6.3591v6.2005h1.1554v-6.0469l6.9318 6.1807v-6.4879h2.8882V7.0896zm-3.4657-4.531v4.531h-5.355l5.355-4.531zm-13.2862.0676 4.8691 4.4634H5.6458V2.6262zM2.7576 16.332V8.245h7.8476l-6.1149 6.1147v1.9723H2.7576zm2.8882 5.0404v-3.8852h.0001v-2.6488l5.7763-5.7764v7.0111l-5.7764 5.2993zm12.7086.0248-5.7766-5.1509V9.0618l5.7766 5.7766v6.5588zm2.8882-5.0652h-1.733v-1.9723L13.3948 8.245h7.8478v8.087z" },
14
+ };
js/main.js CHANGED
@@ -1,7 +1,8 @@
1
  import { DEFAULTS, MODEL_DEVICE, APP_VERSION, LOCAL_MODELS, findModel } from "./config.js";
2
  import * as setup from "./setup.js";
3
  import { LLM } from "./llm.js";
4
- import { RemoteLLM, API_PRESETS, loadApiKey, saveApiKey, apiKeyRemembered } from "./remote-llm.js";
 
5
  import { VM } from "./vm.js";
6
  import { Agent } from "./agent.js";
7
  import { renderMarkdown } from "./markdown.js";
@@ -51,22 +52,62 @@ function status(name, state, text) {
51
  el.title = text;
52
  }
53
 
54
- // ---------- model backend: local (WebGPU) or an OpenAI-compatible API ----------
55
  const isMobile = () => !!navigator.userAgentData?.mobile || /Android|iPhone|iPad|Mobile/i.test(navigator.userAgent);
56
- const usingApi = () => settings.backend === "api" && !!settings.api?.baseUrl && !!settings.api?.model;
57
- const apiConfig = () => ({ baseUrl: settings.api.baseUrl, model: settings.api.model, key: loadApiKey() });
58
- const apiHost = () => { try { return new URL(settings.api.baseUrl).host; } catch { return settings.api?.baseUrl ?? ""; } };
59
 
60
- // The active context budget follows the backend.
61
- const currentModel = () => findModel(settings.localModel);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  const smallestModel = () => LOCAL_MODELS.reduce((a, b) => (b.bytes < a.bytes ? b : a));
 
 
 
63
  const syncContextBudget = () => {
64
- settings.contextBudgetTokens = usingApi() ? settings.contextApiTokens : Math.min(settings.contextLocalTokens, currentModel().contextMax);
 
65
  };
66
  syncContextBudget();
67
 
68
- let llm = usingApi() ? new RemoteLLM(apiConfig()) : new LLM(currentModel());
 
 
 
 
 
 
 
 
 
 
 
 
69
  let modelError = null; // why the local model failed to load, if it did
 
 
70
  const vm = new VM();
71
  let agent = null;
72
  let diskManifest = null;
@@ -77,10 +118,6 @@ let files = null;
77
  async function init() {
78
  $("#version").textContent = "v" + APP_VERSION;
79
  setBackendLabels();
80
- const m = currentModel();
81
- $("#model-size").textContent = fmtBytes(m.bytes);
82
- $(".c-name", $('[data-row="model"]')).textContent = m.label;
83
- $(".c-desc", $('[data-row="model"]')).textContent = `Language model by ${m.by}, ${m.dtype} ONNX`;
84
  syncToggles();
85
 
86
  const checks = await environmentChecks();
@@ -90,19 +127,13 @@ async function init() {
90
  try { await setup.ensureServiceWorker(); }
91
  catch (err) { checks.sw = { ok: false, text: err.message }; renderChecks(checks); return showSetup(false); }
92
 
93
- const [cx, disk, model] = await Promise.all([setup.cheerpxStatus(), setup.installedDisk(), setup.modelStatus(currentModel())]);
 
94
  diskManifest = disk;
95
- const installed = { cx: cx.ready, disk: !!disk, model: model.ready };
96
  for (const k of Object.keys(installed)) if (installed[k]) setRow(k, 1, "Installed");
97
- const needModel = !usingApi();
98
- if (!needModel) setRow("model", 1, "Using API");
99
- const all = installed.cx && installed.disk && (installed.model || !needModel);
100
- if (!all) status("offline", "warn", "Not installed");
101
- else if (needModel) status("offline", "ok", "Available offline");
102
- else status("offline", "warn", "Model via API");
103
-
104
- if (all && (checks.gpu.ok || !needModel)) start();
105
- else showSetup(checks.gpu.ok || !needModel);
106
  }
107
 
108
  async function environmentChecks() {
@@ -111,8 +142,7 @@ async function environmentChecks() {
111
  ? { ok: true, text: "Cross-origin isolation" }
112
  : { ok: false, text: window.top !== window ? "The embedded view can't run the VM. Open the app in its own tab." : "Cross-origin isolation is off (COOP/COEP headers missing)" };
113
  c.sw = "serviceWorker" in navigator ? { ok: true, text: "Offline storage" } : { ok: false, text: "Offline storage unavailable (private window?)" };
114
- if (usingApi()) c.gpu = { ok: true, text: `Model runs through an API (${apiHost()})` };
115
- else if (MODEL_DEVICE !== "webgpu") c.gpu = { ok: true, warn: true, text: `Model runs on ${MODEL_DEVICE} (slow; for testing)` };
116
  else if (!navigator.gpu) c.gpu = { ok: false, text: "WebGPU unavailable. Use desktop Chrome or Edge." };
117
  else {
118
  try {
@@ -122,11 +152,16 @@ async function environmentChecks() {
122
  else c.gpu = { ok: true, text: "WebGPU with f16 shaders" + (a.info?.vendor ? ` · ${a.info.vendor}` : "") };
123
  } catch (e) { c.gpu = { ok: false, text: "WebGPU error: " + e.message }; }
124
  }
 
 
 
 
125
  const est = await setup.storageEstimate();
126
  const free = est ? est.quota - est.usage : null;
127
  c.storage = free == null ? { ok: true, text: "Storage space unknown" }
128
- : free > 2.2e9 ? { ok: true, text: `${fmtBytes(free)} of browser storage available` }
129
- : { ok: true, warn: true, text: `Only ${fmtBytes(free)} of browser storage available; 2.2 GB needed` };
 
130
  return c;
131
  }
132
 
@@ -149,13 +184,7 @@ function setRow(k, frac, text) {
149
 
150
  function showSetup(canInstall) {
151
  $("#setup").hidden = false;
152
- $("#mobile-banner").hidden = !(isMobile() && !usingApi());
153
- $("#setup-api").textContent = usingApi() ? `Using ${settings.api.model} via ${apiHost()} · Change` : "Use an API model instead";
154
- if (usingApi()) {
155
- $(".c-name", $('[data-row="model"]')).textContent = settings.api.model;
156
- $(".c-desc", $('[data-row="model"]')).textContent = `Runs through ${apiHost()}, nothing to download`;
157
- $("#model-size").textContent = "";
158
- }
159
  const btn = $("#install");
160
  btn.disabled = !canInstall || !navigator.onLine;
161
  if (!navigator.onLine) $("#setup-note").textContent = "You're offline. Connect once to install; after that it works without internet.";
@@ -181,14 +210,10 @@ async function install() {
181
  diskManifest = await setup.downloadDisk((d, t) => setRow("disk", d / t, `${Math.round((d / t) * 100)}%`));
182
  setRow("disk", 1, "Installed");
183
  })();
184
- const modelTask = usingApi() ? Promise.resolve() : loadModel((f, text) => setRow("model", f, text)).then(
185
- () => setRow("model", 1, "Installed"),
186
- (err) => { console.error(err); modelError = err; setRow("model", 0, "Failed"); }, // shown once the app opens
187
- );
188
- await Promise.all([cxTask, diskTask, modelTask]);
189
- if (usingApi()) status("offline", "warn", "Model via API");
190
- else status("offline", modelError ? "warn" : "ok", modelError ? "Model not installed" : "Available offline");
191
- start();
192
  } catch (err) {
193
  console.error(err);
194
  note.className = "note error";
@@ -210,7 +235,7 @@ function loadModel(onProgress) {
210
  files.set(p.file, { loaded: p.status === "done" ? (prev?.total ?? p.loaded) : p.loaded, total: p.total || prev?.total || 0 });
211
  let loaded = 0, total = 0;
212
  for (const f of files.values()) { loaded += f.loaded; total += f.total; }
213
- total = Math.max(total, currentModel().bytes);
214
  const frac = Math.min(0.99, loaded / total);
215
  onProgress?.(frac, `${fmtBytes(loaded)} of ${fmtBytes(total)}`);
216
  status("model", "busy", `Loading ${Math.round(frac * 100)}%`);
@@ -240,6 +265,7 @@ function acquireVmLock() {
240
  }
241
 
242
  async function start() {
 
243
  $("#setup").hidden = true;
244
  if (!(await acquireVmLock())) {
245
  status("vm", "bad", "Open in another tab");
@@ -267,8 +293,8 @@ async function start() {
267
  }
268
  setComposerEnabled();
269
  };
270
- if (isMobile() && !usingApi() && !llm.ready && !modelError) showMobileWarning();
271
- if (memoryConstrained() && !usingApi()) {
272
  // Upload the weights to the GPU before Linux takes its share of memory: on phones
273
  // the two together at peak are what makes the browser kill the GPU process.
274
  status("vm", "busy", "Waiting for the model");
@@ -287,10 +313,20 @@ function memoryConstrained() {
287
 
288
  // Loads the current backend unless it is ready or already failed. Never rejects: a
289
  // local-model failure is shown as a callout with the API option.
290
- function loadModelOnce() {
291
- if (llm.ready) return Promise.resolve();
292
- if (modelError) { showModelFailure(modelError); return Promise.resolve(); }
 
293
  const mine = llm;
 
 
 
 
 
 
 
 
 
294
  return loadModel().catch((err) => {
295
  if (mine !== llm) return; // replaced (e.g. switched to an API model) while loading
296
  console.error(err);
@@ -326,11 +362,9 @@ function showModelFailure(err) {
326
  status("model", "bad", "Model failed");
327
  clearModelCallouts();
328
  const el = callout("error", "The model couldn't load",
329
- `${friendlyModelError(err)} Linux is still running on this device. You can connect an API model instead and the agent will keep using this machine.`,
330
  [
331
- h("button", { class: "btn primary small", type: "button", onclick: () => openApiDialog() }, "Use an API model"),
332
- currentModel() !== smallestModel()
333
- ? h("button", { class: "btn small", type: "button", onclick: () => openModelDialog() }, "Choose a smaller model") : null,
334
  h("button", { class: "btn small", type: "button", onclick: () => { el.remove(); retryLocalModel(); } }, "Try again"),
335
  ],
336
  String(err?.message ?? err));
@@ -338,27 +372,58 @@ function showModelFailure(err) {
338
  }
339
 
340
  function showMobileWarning() {
 
341
  callout("warn", "You're on a phone or tablet",
342
- `${currentModel().label} needs about ${fmtBytes(currentModel().bytes)} of graphics memory, which most phones can't give a browser tab, so it will probably fail to load. Trying anyway. To skip the wait, use an API model (only the model moves to the provider, Linux keeps running here)${currentModel() !== smallestModel() ? `, or try the smaller ${smallestModel().label}` : ""}.`,
343
- [
344
- h("button", { class: "btn primary small", type: "button", onclick: () => openApiDialog() }, "Use an API model"),
345
- currentModel() !== smallestModel()
346
- ? h("button", { class: "btn small", type: "button", onclick: () => openModelDialog() }, "Choose a smaller model") : null,
347
- ]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  }
349
 
350
  function retryLocalModel() {
351
  modelError = null;
352
  llm.dispose?.();
353
- llm = new LLM(currentModel());
354
  if (agent) agent.llm = llm;
355
  setComposerEnabled();
356
  loadModelOnce();
357
  }
358
 
359
  function setBackendLabels() {
360
- $("#model-name").textContent = usingApi() ? settings.api.model : currentModel().label;
361
- $("#gpu-name").textContent = usingApi() ? "API" : MODEL_DEVICE === "webgpu" ? "WebGPU" : MODEL_DEVICE;
 
362
  }
363
 
364
  // ---------- conversation rendering ----------
@@ -506,7 +571,8 @@ function setComposerEnabled() {
506
  $("#stop").hidden = !running;
507
  $("#send").disabled = !ready;
508
  $("#prompt").placeholder = ready ? "Describe a task for the agent"
509
- : !llm.ready ? (modelError ? "No model loaded. Use an API model or try again." : "Loading the model…")
 
510
  : "Starting Linux…";
511
  }
512
 
@@ -555,9 +621,9 @@ function newConversation() {
555
 
556
  // ---------- composer toggles ----------
557
  function syncToggles() {
558
- const canThink = usingApi() || currentModel().thinking;
559
  $("#tog-thinking").disabled = !canThink;
560
- $("#tog-thinking").title = canThink ? "Let the model reason before each step" : `${currentModel().label} has no reasoning mode`;
561
  $("#tog-thinking").setAttribute("aria-pressed", String(!!settings.thinking && canThink));
562
  $("#tog-confirm").setAttribute("aria-pressed", String(!!settings.confirmCommands));
563
  }
@@ -648,7 +714,7 @@ for (const tab of document.querySelectorAll("[data-tab]")) tab.addEventListener(
648
 
649
  const dlg = $("#settings");
650
  $("#open-settings").addEventListener("click", () => {
651
- $("#set-model-desc").textContent = usingApi() ? `${settings.api.model} via ${apiHost()}` : `${currentModel().label} on this device (WebGPU)`;
652
  $("#set-thinking").checked = settings.thinking;
653
  $("#set-confirm").checked = settings.confirmCommands;
654
  $("#set-steps").value = settings.maxSteps;
@@ -676,151 +742,25 @@ dlg.addEventListener("close", () => {
676
  syncToggles();
677
  });
678
  dlg.addEventListener("click", (e) => { if (e.target === dlg) dlg.close(); }); // click on backdrop
679
- $("#set-model").addEventListener("click", () => { dlg.close(); openModelDialog(); });
680
-
681
- // ---------- model picker ----------
682
- const modelDlg = $("#model-dlg");
683
- let pickedModel = null;
684
-
685
- async function openModelDialog() {
686
- pickedModel = usingApi() ? null : currentModel().id;
687
- $("#model-api-desc").textContent = usingApi() ? `In use: ${settings.api.model} via ${apiHost()}` : "OpenRouter, OpenAI, Hugging Face or any OpenAI-compatible API. Linux stays on this device.";
688
- renderModelList();
689
- modelDlg.showModal();
690
- // Fill in what's downloaded (slow-ish: reads Cache Storage).
691
- for (const m of LOCAL_MODELS) {
692
- const bytes = await setup.modelCachedBytes(m).catch(() => 0);
693
- const el = $(`[data-model="${m.id}"] .mo-state`, modelDlg);
694
- if (!el) continue;
695
- const loaded = !usingApi() && m.id === currentModel().id && llm.ready;
696
- el.replaceChildren(bytes > 1e6
697
- ? h("span", { class: "mo-dl" }, `Downloaded · ${fmtBytes(bytes)}`)
698
- : h("span", {}, `${fmtBytes(m.bytes)} download`));
699
- if (bytes > 1e6 && !loaded) {
700
- el.append(h("button", { class: "linkish", type: "button", onclick: async (e) => {
701
- e.preventDefault();
702
- if (!confirm(`Delete the downloaded files of ${m.label}? It can be downloaded again later.`)) return;
703
- await setup.removeModel(m);
704
- el.replaceChildren(h("span", {}, `${fmtBytes(m.bytes)} download`));
705
- } }, "Delete"));
706
- }
707
- }
708
- }
709
-
710
- function renderModelList() {
711
- $("#model-list").replaceChildren(...LOCAL_MODELS.map((m) => h("label", { class: "model-opt", "data-model": m.id },
712
- h("input", { type: "radio", name: "local-model", value: m.id, checked: m.id === pickedModel, onchange: () => { pickedModel = m.id; syncModelSave(); } }),
713
- h("span", { class: "mo-main" },
714
- h("span", { class: "mo-name" }, m.label, m.id === LOCAL_MODELS[0].id ? h("span", { class: "mo-tag" }, "Default") : null,
715
- !m.thinking ? h("span", { class: "mo-tag" }, "No reasoning") : null),
716
- h("span", { class: "mo-note" }, m.note),
717
- h("span", { class: "mo-meta" }, `${m.by} · ${m.license} · up to ${Math.round(m.contextMax / 1024)}k context`),
718
- h("span", { class: "mo-state" }, `${fmtBytes(m.bytes)} download`)))));
719
- syncModelSave();
720
- }
721
-
722
- function syncModelSave() {
723
- const same = !usingApi() && pickedModel === currentModel().id;
724
- $("#model-save").disabled = !pickedModel || same;
725
- $("#model-save").textContent = same ? "In use" : "Use this model";
726
- }
727
-
728
- $("#model-close").addEventListener("click", () => modelDlg.close());
729
- modelDlg.addEventListener("click", (e) => { if (e.target === modelDlg) modelDlg.close(); });
730
- $("#model-api").addEventListener("click", () => { modelDlg.close(); openApiDialog(); });
731
- $("#model-save").addEventListener("click", () => {
732
- if (!pickedModel) return;
733
- if (agent?.running) return alert("Stop the agent before switching models.");
734
- settings.localModel = pickedModel;
735
- settings.backend = "local";
736
- saveSettings();
737
- location.reload(); // a fresh worker loads (and if needed downloads) the new model
738
- });
739
- $("#setup-model").addEventListener("click", () => openModelDialog());
740
-
741
- // ---------- API model dialog ----------
742
- const apiDlg = $("#api-dlg");
743
- $("#api-preset").replaceChildren(...API_PRESETS.map((p) => h("option", { value: p.id }, p.label)));
744
- $("#api-preset").addEventListener("change", () => {
745
- const p = API_PRESETS.find((x) => x.id === $("#api-preset").value);
746
- if (p?.baseUrl) $("#api-base").value = p.baseUrl;
747
- else { $("#api-base").value = ""; $("#api-base").focus(); }
748
- });
749
- const apiForm = () => ({
750
- preset: $("#api-preset").value,
751
- baseUrl: $("#api-base").value.trim(),
752
- model: $("#api-model").value.trim(),
753
- key: $("#api-key").value.trim(),
754
- });
755
- const apiNote = (text, kind = "") => { const n = $("#api-note"); n.className = "note " + kind; n.textContent = text; };
756
-
757
- function openApiDialog() {
758
- const a = settings.api ?? {};
759
- $("#api-preset").value = a.preset ?? API_PRESETS[0].id;
760
- $("#api-base").value = a.baseUrl ?? API_PRESETS[0].baseUrl;
761
- $("#api-model").value = a.model ?? "";
762
- $("#api-key").value = loadApiKey();
763
- $("#api-remember").checked = apiKeyRemembered();
764
- $("#api-local").hidden = !usingApi();
765
- apiNote("");
766
- apiDlg.showModal();
767
- }
768
-
769
- $("#api-close").addEventListener("click", () => apiDlg.close());
770
- apiDlg.addEventListener("click", (e) => { if (e.target === apiDlg) apiDlg.close(); });
771
-
772
- $("#api-test").addEventListener("click", async () => {
773
- const f = apiForm();
774
- if (!f.baseUrl || !f.model) return apiNote("Enter a base URL and a model ID first.", "error");
775
- const btn = $("#api-test");
776
- btn.disabled = true;
777
- apiNote("Testing…");
778
- try {
779
- const reply = await RemoteLLM.test(f);
780
- apiNote(`Works. The model replied: "${reply.slice(0, 80)}"`, "ok");
781
- } catch (err) { apiNote(err.message, "error"); }
782
- finally { btn.disabled = false; }
783
- });
784
-
785
- $("#api-form").addEventListener("submit", async (e) => {
786
- e.preventDefault();
787
- const f = apiForm();
788
- if (!f.baseUrl || !f.model) return apiNote("Enter a base URL and a model ID.", "error");
789
- if (agent?.running) return apiNote("Stop the agent before switching models.", "error");
790
- try { new URL(f.baseUrl); } catch { return apiNote("The base URL isn't a valid URL.", "error"); }
791
- settings.backend = "api";
792
- settings.api = { preset: f.preset, baseUrl: f.baseUrl, model: f.model };
793
- saveSettings();
794
- syncContextBudget();
795
- saveApiKey(f.key, $("#api-remember").checked);
796
- apiDlg.close();
797
- if (!$("#setup").hidden) return location.reload(); // re-run setup without the local model
798
- const old = llm;
799
- llm = new RemoteLLM(apiConfig());
800
- old.dispose?.(); // frees the local model's GPU memory
801
- modelError = null;
802
- if (agent) agent.llm = llm;
803
- clearModelCallouts();
804
- setBackendLabels();
805
- status("offline", "warn", "Model via API");
806
- await loadModelOnce();
807
- addNotice(`Now using ${f.model} through ${apiHost()}. Linux still runs on this device.`);
808
- });
809
-
810
- $("#api-local").addEventListener("click", () => {
811
- if (agent?.running) return apiNote("Stop the agent before switching models.", "error");
812
- settings.backend = "local";
813
- saveSettings();
814
- location.reload();
815
  });
816
- $("#setup-api").addEventListener("click", () => openApiDialog());
 
817
  $("#reset-vm").addEventListener("click", async () => {
818
  if (!confirm("Erase all files and changes made inside the Linux machine? The page will reload.")) return;
819
  if (vm.overlayName) await setup.resetVmStorage(vm.overlayName);
820
  location.reload();
821
  });
822
  $("#uninstall").addEventListener("click", async () => {
823
- if (!confirm("Remove the model, the Linux disk and all saved files from this browser? Reinstalling needs an internet connection.")) return;
824
  await setup.uninstallAll();
825
  location.reload();
826
  });
 
1
  import { DEFAULTS, MODEL_DEVICE, APP_VERSION, LOCAL_MODELS, findModel } from "./config.js";
2
  import * as setup from "./setup.js";
3
  import { LLM } from "./llm.js";
4
+ import { RemoteLLM, findPreset, loadApiKey, migrateLegacyKey } from "./remote-llm.js";
5
+ import { createModelManager } from "./model-manager.js";
6
  import { VM } from "./vm.js";
7
  import { Agent } from "./agent.js";
8
  import { renderMarkdown } from "./markdown.js";
 
52
  el.title = text;
53
  }
54
 
55
+ // ---------- language model: none, local (WebGPU) or one of the saved API models ----------
56
  const isMobile = () => !!navigator.userAgentData?.mobile || /Android|iPhone|iPad|Mobile/i.test(navigator.userAgent);
 
 
 
57
 
58
+ // Before the Model Manager (v0.5 and older) there was one API model and one local choice.
59
+ (function migrateModelSettings() {
60
+ if (Array.isArray(settings.apiModels) && "activeModel" in settings) return;
61
+ let hadSettings = false;
62
+ try { hadSettings = localStorage.getItem("lwa-settings") != null; } catch {}
63
+ settings.apiModels = Array.isArray(settings.apiModels) ? settings.apiModels : [];
64
+ if (settings.api?.baseUrl && settings.api?.model && !settings.apiModels.length) {
65
+ settings.apiModels.push({ id: "api-1", name: settings.api.model, preset: settings.api.preset ?? "custom", baseUrl: settings.api.baseUrl, model: settings.api.model });
66
+ migrateLegacyKey("api-1");
67
+ }
68
+ if (!("activeModel" in settings)) {
69
+ settings.activeModel = settings.backend === "api" && settings.apiModels.length ? { kind: "api", id: settings.apiModels[0].id }
70
+ : hadSettings ? { kind: "local", id: findModel(settings.localModel).id } // they had a local model installed
71
+ : null;
72
+ }
73
+ delete settings.backend; delete settings.api; delete settings.localModel;
74
+ saveSettings();
75
+ })();
76
+
77
+ const activeKind = () => settings.activeModel?.kind ?? null;
78
+ const activeLocal = () => (activeKind() === "local" ? LOCAL_MODELS.find((m) => m.id === settings.activeModel.id) ?? null : null);
79
+ const activeApi = () => (activeKind() === "api" ? settings.apiModels.find((a) => a.id === settings.activeModel.id) ?? null : null);
80
+ const usingApi = () => !!activeApi();
81
+ const isActive = (sel) => !!sel && settings.activeModel?.kind === sel.kind && settings.activeModel?.id === sel.id;
82
+ const apiConfigFor = (a) => ({ baseUrl: a.baseUrl, model: a.model, key: loadApiKey(a.id), headers: findPreset(a.preset).headers });
83
+ const apiHost = () => { try { return new URL(activeApi().baseUrl).host; } catch { return activeApi()?.baseUrl ?? ""; } };
84
+ const activeLabel = () => activeApi()?.name ?? activeLocal()?.label ?? "No model";
85
  const smallestModel = () => LOCAL_MODELS.reduce((a, b) => (b.bytes < a.bytes ? b : a));
86
+ if (settings.activeModel && !activeLocal() && !activeApi()) settings.activeModel = null; // points at something deleted
87
+
88
+ // The active context budget follows the model.
89
  const syncContextBudget = () => {
90
+ settings.contextBudgetTokens = usingApi() ? settings.contextApiTokens
91
+ : Math.min(settings.contextLocalTokens, (activeLocal() ?? LOCAL_MODELS[0]).contextMax);
92
  };
93
  syncContextBudget();
94
 
95
+ // Stand-in while no model is chosen.
96
+ const noModel = () => ({
97
+ none: true, ready: false, async load() {}, stop() {}, resetCache() {}, dispose() {},
98
+ generate() { return Promise.reject(new Error("No model selected. Open the Model Manager to choose one.")); },
99
+ });
100
+ function createLlm() {
101
+ const a = activeApi();
102
+ if (a) return new RemoteLLM(apiConfigFor(a));
103
+ const m = activeLocal();
104
+ return m ? new LLM(m) : noModel();
105
+ }
106
+
107
+ let llm = createLlm();
108
  let modelError = null; // why the local model failed to load, if it did
109
+ let gpuCheck = null; // result of the WebGPU check, for the Model Manager
110
+ let started = false;
111
  const vm = new VM();
112
  let agent = null;
113
  let diskManifest = null;
 
118
  async function init() {
119
  $("#version").textContent = "v" + APP_VERSION;
120
  setBackendLabels();
 
 
 
 
121
  syncToggles();
122
 
123
  const checks = await environmentChecks();
 
127
  try { await setup.ensureServiceWorker(); }
128
  catch (err) { checks.sw = { ok: false, text: err.message }; renderChecks(checks); return showSetup(false); }
129
 
130
+ // Setup installs only Linux; models are added later in the Model Manager.
131
+ const [cx, disk] = await Promise.all([setup.cheerpxStatus(), setup.installedDisk()]);
132
  diskManifest = disk;
133
+ const installed = { cx: cx.ready, disk: !!disk };
134
  for (const k of Object.keys(installed)) if (installed[k]) setRow(k, 1, "Installed");
135
+ if (installed.cx && installed.disk) { updateOfflineStatus(); start(); }
136
+ else { status("offline", "warn", "Not installed"); showSetup(true); }
 
 
 
 
 
 
 
137
  }
138
 
139
  async function environmentChecks() {
 
142
  ? { ok: true, text: "Cross-origin isolation" }
143
  : { ok: false, text: window.top !== window ? "The embedded view can't run the VM. Open the app in its own tab." : "Cross-origin isolation is off (COOP/COEP headers missing)" };
144
  c.sw = "serviceWorker" in navigator ? { ok: true, text: "Offline storage" } : { ok: false, text: "Offline storage unavailable (private window?)" };
145
+ if (MODEL_DEVICE !== "webgpu") c.gpu = { ok: true, warn: true, text: `Model runs on ${MODEL_DEVICE} (slow; for testing)` };
 
146
  else if (!navigator.gpu) c.gpu = { ok: false, text: "WebGPU unavailable. Use desktop Chrome or Edge." };
147
  else {
148
  try {
 
152
  else c.gpu = { ok: true, text: "WebGPU with f16 shaders" + (a.info?.vendor ? ` · ${a.info.vendor}` : "") };
153
  } catch (e) { c.gpu = { ok: false, text: "WebGPU error: " + e.message }; }
154
  }
155
+ // WebGPU only matters for local models, which are optional: report it, don't block setup.
156
+ gpuCheck = c.gpu;
157
+ c.gpu = c.gpu.ok ? { ok: true, text: c.gpu.text + ": local models can run" }
158
+ : { ok: true, warn: true, text: `${c.gpu.text}. Local models won't run here; API models will.` };
159
  const est = await setup.storageEstimate();
160
  const free = est ? est.quota - est.usage : null;
161
  c.storage = free == null ? { ok: true, text: "Storage space unknown" }
162
+ : free > 3e9 ? { ok: true, text: `${fmtBytes(free)} of browser storage available` }
163
+ : free > 0.4e9 ? { ok: true, warn: true, text: `${fmtBytes(free)} of browser storage available: enough for Linux, maybe not for local models (0.5 to 3 GB each)` }
164
+ : { ok: false, text: `Only ${fmtBytes(free)} of browser storage available; Linux needs about 400 MB` };
165
  return c;
166
  }
167
 
 
184
 
185
  function showSetup(canInstall) {
186
  $("#setup").hidden = false;
187
+ $("#mobile-banner").hidden = !isMobile();
 
 
 
 
 
 
188
  const btn = $("#install");
189
  btn.disabled = !canInstall || !navigator.onLine;
190
  if (!navigator.onLine) $("#setup-note").textContent = "You're offline. Connect once to install; after that it works without internet.";
 
210
  diskManifest = await setup.downloadDisk((d, t) => setRow("disk", d / t, `${Math.round((d / t) * 100)}%`));
211
  setRow("disk", 1, "Installed");
212
  })();
213
+ await Promise.all([cxTask, diskTask]);
214
+ updateOfflineStatus();
215
+ await start();
216
+ if (!settings.activeModel) models.open(); // next step: pick a model
 
 
 
 
217
  } catch (err) {
218
  console.error(err);
219
  note.className = "note error";
 
235
  files.set(p.file, { loaded: p.status === "done" ? (prev?.total ?? p.loaded) : p.loaded, total: p.total || prev?.total || 0 });
236
  let loaded = 0, total = 0;
237
  for (const f of files.values()) { loaded += f.loaded; total += f.total; }
238
+ total = Math.max(total, activeLocal()?.bytes ?? 0);
239
  const frac = Math.min(0.99, loaded / total);
240
  onProgress?.(frac, `${fmtBytes(loaded)} of ${fmtBytes(total)}`);
241
  status("model", "busy", `Loading ${Math.round(frac * 100)}%`);
 
265
  }
266
 
267
  async function start() {
268
+ started = true;
269
  $("#setup").hidden = true;
270
  if (!(await acquireVmLock())) {
271
  status("vm", "bad", "Open in another tab");
 
293
  }
294
  setComposerEnabled();
295
  };
296
+ if (isMobile() && activeLocal() && !llm.ready && !modelError) showMobileWarning();
297
+ if (memoryConstrained() && activeLocal()) {
298
  // Upload the weights to the GPU before Linux takes its share of memory: on phones
299
  // the two together at peak are what makes the browser kill the GPU process.
300
  status("vm", "busy", "Waiting for the model");
 
313
 
314
  // Loads the current backend unless it is ready or already failed. Never rejects: a
315
  // local-model failure is shown as a callout with the API option.
316
+ async function loadModelOnce() {
317
+ if (llm.ready) return;
318
+ if (llm.none) { status("model", "warn", "No model"); showNoModel(); setComposerEnabled(); return; }
319
+ if (modelError) { showModelFailure(modelError); return; }
320
  const mine = llm;
321
+ const m = activeLocal();
322
+ // Downloads happen only in the Model Manager, never implicitly here.
323
+ if (m && !(await setup.modelStatus(m)).ready) {
324
+ if (mine !== llm) return;
325
+ status("model", "warn", "Not downloaded");
326
+ showNoModel(`${m.label} isn't downloaded in this browser. `);
327
+ setComposerEnabled();
328
+ return;
329
+ }
330
  return loadModel().catch((err) => {
331
  if (mine !== llm) return; // replaced (e.g. switched to an API model) while loading
332
  console.error(err);
 
362
  status("model", "bad", "Model failed");
363
  clearModelCallouts();
364
  const el = callout("error", "The model couldn't load",
365
+ `${friendlyModelError(err)} Linux is still running on this device. In the Model Manager you can pick a smaller model${activeLocal() !== smallestModel() ? ` (${smallestModel().label} is the smallest)` : ""} or add an API model, and the agent will keep using this machine.`,
366
  [
367
+ h("button", { class: "btn primary small", type: "button", onclick: () => models.open() }, "Open Model Manager"),
 
 
368
  h("button", { class: "btn small", type: "button", onclick: () => { el.remove(); retryLocalModel(); } }, "Try again"),
369
  ],
370
  String(err?.message ?? err));
 
372
  }
373
 
374
  function showMobileWarning() {
375
+ const m = activeLocal();
376
  callout("warn", "You're on a phone or tablet",
377
+ `${m.label} needs about ${fmtBytes(m.bytes)} of graphics memory, which most phones can't give a browser tab, so it may fail to load. Trying anyway. In the Model Manager you can switch to an API model (only the model moves to the provider, Linux keeps running here)${m !== smallestModel() ? ` or the smaller ${smallestModel().label}` : ""}.`,
378
+ [h("button", { class: "btn primary small", type: "button", onclick: () => models.open() }, "Open Model Manager")]);
379
+ }
380
+
381
+ function showNoModel(prefix = "") {
382
+ clearModelCallouts();
383
+ callout("info", "Choose a language model",
384
+ `${prefix}Linux is ready. Open the Model Manager to download a model that runs on your GPU, or to add an API model (OpenAI, Anthropic, Gemini, OpenRouter, Hugging Face and more).`,
385
+ [h("button", { class: "btn primary small", type: "button", onclick: () => models.open() }, "Open Model Manager")]);
386
+ }
387
+
388
+ function updateOfflineStatus() {
389
+ if (usingApi()) status("offline", "warn", "Model via API");
390
+ else status("offline", "ok", "Available offline");
391
+ }
392
+
393
+ /** Switches the language model (null = none), unloading the previous one. */
394
+ async function activateModel(sel) {
395
+ if (agent?.running) throw new Error("Stop the agent before switching models.");
396
+ const old = llm;
397
+ settings.activeModel = sel;
398
+ saveSettings();
399
+ llm = createLlm();
400
+ old.dispose?.(); // frees a local model's GPU memory
401
+ modelError = null;
402
+ if (agent) agent.llm = llm;
403
+ clearModelCallouts();
404
+ syncContextBudget();
405
+ setBackendLabels();
406
+ syncToggles();
407
+ updateOfflineStatus();
408
+ setComposerEnabled();
409
+ if (!started) return;
410
+ await loadModelOnce();
411
+ if (llm.ready) addNotice(usingApi() ? `Now using ${activeLabel()} through ${apiHost()}. Linux still runs on this device.` : `Now using ${activeLabel()} on this device.`);
412
  }
413
 
414
  function retryLocalModel() {
415
  modelError = null;
416
  llm.dispose?.();
417
+ llm = createLlm();
418
  if (agent) agent.llm = llm;
419
  setComposerEnabled();
420
  loadModelOnce();
421
  }
422
 
423
  function setBackendLabels() {
424
+ $("#model-name").textContent = activeLabel();
425
+ $("#gpu-name").textContent = usingApi() ? "API" : activeLocal() ? (MODEL_DEVICE === "webgpu" ? "WebGPU" : MODEL_DEVICE) : "";
426
+ $("#gpu-name").hidden = !settings.activeModel;
427
  }
428
 
429
  // ---------- conversation rendering ----------
 
571
  $("#stop").hidden = !running;
572
  $("#send").disabled = !ready;
573
  $("#prompt").placeholder = ready ? "Describe a task for the agent"
574
+ : llm.none ? "Choose a model in the Model Manager to start"
575
+ : !llm.ready ? (modelError ? "No model loaded. Open the Model Manager or try again." : "Loading the model…")
576
  : "Starting Linux…";
577
  }
578
 
 
621
 
622
  // ---------- composer toggles ----------
623
  function syncToggles() {
624
+ const canThink = activeLocal()?.thinking ?? true;
625
  $("#tog-thinking").disabled = !canThink;
626
+ $("#tog-thinking").title = canThink ? "Let the model reason before each step" : `${activeLocal().label} has no reasoning mode`;
627
  $("#tog-thinking").setAttribute("aria-pressed", String(!!settings.thinking && canThink));
628
  $("#tog-confirm").setAttribute("aria-pressed", String(!!settings.confirmCommands));
629
  }
 
714
 
715
  const dlg = $("#settings");
716
  $("#open-settings").addEventListener("click", () => {
717
+ $("#set-model-desc").textContent = usingApi() ? `${activeLabel()} via ${apiHost()}` : activeLocal() ? `${activeLabel()} on this device (WebGPU)` : "None chosen yet";
718
  $("#set-thinking").checked = settings.thinking;
719
  $("#set-confirm").checked = settings.confirmCommands;
720
  $("#set-steps").value = settings.maxSteps;
 
742
  syncToggles();
743
  });
744
  dlg.addEventListener("click", (e) => { if (e.target === dlg) dlg.close(); }); // click on backdrop
745
+ $("#set-model").addEventListener("click", () => { dlg.close(); models.open(); });
746
+
747
+ // ---------- Model Manager ----------
748
+ const models = createModelManager({
749
+ settings, saveSettings, h, fmtBytes, setup,
750
+ activate: activateModel,
751
+ isActive,
752
+ isMobile,
753
+ gpuNote: () => gpuCheck && !gpuCheck.ok ? { ok: false, text: `${gpuCheck.text}, so local models can't run in this browser. API models work.` } : null,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
754
  });
755
+ $("#open-models").addEventListener("click", () => models.open());
756
+ $("#model-name").addEventListener("click", () => models.open());
757
  $("#reset-vm").addEventListener("click", async () => {
758
  if (!confirm("Erase all files and changes made inside the Linux machine? The page will reload.")) return;
759
  if (vm.overlayName) await setup.resetVmStorage(vm.overlayName);
760
  location.reload();
761
  });
762
  $("#uninstall").addEventListener("click", async () => {
763
+ if (!confirm("Remove the downloaded models, the Linux disk and all saved files from this browser? Reinstalling needs an internet connection.")) return;
764
  await setup.uninstallAll();
765
  location.reload();
766
  });
js/model-manager.js ADDED
@@ -0,0 +1,312 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // The Model Manager: one place to download, remove and switch local models, and to add,
2
+ // edit, test and switch between any number of API models.
3
+ import { LOCAL_MODELS } from "./config.js";
4
+ import { LOGOS } from "./logos.js";
5
+ import { API_PRESETS, findPreset, RemoteLLM, loadApiKey, saveApiKey, deleteApiKey, apiKeyRemembered } from "./remote-llm.js";
6
+
7
+ const SVG_NS = "http://www.w3.org/2000/svg";
8
+
9
+ // Letter badges for local model makers (Simple Icons has no marks for them).
10
+ const MAKER_BADGE = { "Liquid AI": { text: "LQ", bg: "#4338ca" }, Qwen: { text: "Q", bg: "#615ced" }, IBM: { text: "IBM", bg: "#0f62fe" } };
11
+
12
+ /** A 36px tile with a provider logo, or a letter badge when there is no logo. */
13
+ export function logoTile({ logo, mono } = {}, size = "") {
14
+ const tile = document.createElement("span");
15
+ tile.className = "logo-tile " + size;
16
+ const l = logo && LOGOS[logo];
17
+ if (l) {
18
+ const svg = document.createElementNS(SVG_NS, "svg");
19
+ svg.setAttribute("viewBox", "0 0 24 24");
20
+ svg.setAttribute("role", "img");
21
+ svg.setAttribute("aria-label", l.title);
22
+ const path = document.createElementNS(SVG_NS, "path");
23
+ path.setAttribute("d", l.path);
24
+ // Near-black marks follow the text color so they stay visible in dark mode.
25
+ const dark = parseInt(l.hex.slice(1), 16) < 0x333333;
26
+ path.setAttribute("fill", dark ? "currentColor" : l.hex);
27
+ svg.append(path);
28
+ tile.append(svg);
29
+ } else {
30
+ const m = mono ?? { text: "?", bg: "#71717a" };
31
+ tile.classList.add("mono");
32
+ tile.style.background = m.bg;
33
+ tile.textContent = m.text;
34
+ if (m.text.length > 2) tile.classList.add("small-text");
35
+ }
36
+ return tile;
37
+ }
38
+
39
+ const newId = () => "api-" + Math.random().toString(36).slice(2, 10);
40
+ const hostOf = (url) => { try { return new URL(url).host; } catch { return url; } };
41
+
42
+ /**
43
+ * deps: { settings, saveSettings, h, fmtBytes, setup, activate(sel) -> Promise, isActive(sel),
44
+ * activeLoaded(), gpuNote() -> {ok, text} | null, isMobile() }
45
+ */
46
+ export function createModelManager(deps) {
47
+ const { settings, saveSettings, h, fmtBytes, setup } = deps;
48
+ const dlg = document.getElementById("mm");
49
+ const editDlg = document.getElementById("api-edit");
50
+ const downloads = new Map(); // model id -> { loaded, total, ctrl, error }
51
+ const cached = new Map(); // model id -> bytes in Cache Storage
52
+ let busy = null; // id being activated
53
+
54
+ // ---------- manager ----------
55
+ async function open() {
56
+ render();
57
+ if (!dlg.open) dlg.showModal();
58
+ await refreshCached();
59
+ }
60
+
61
+ async function refreshCached() {
62
+ await Promise.all(LOCAL_MODELS.map(async (m) => {
63
+ const [st, bytes] = await Promise.all([setup.modelStatus(m).catch(() => ({ ready: false })), setup.modelCachedBytes(m).catch(() => 0)]);
64
+ cached.set(m.id, st.ready ? bytes || m.bytes : 0);
65
+ }));
66
+ if (dlg.open) render();
67
+ }
68
+ const isDownloaded = (m) => (cached.get(m.id) ?? 0) > 0;
69
+
70
+ function render() {
71
+ const active = settings.activeModel;
72
+ const activeLabel = !active ? null : active.kind === "local"
73
+ ? LOCAL_MODELS.find((m) => m.id === active.id)?.label
74
+ : settings.apiModels.find((a) => a.id === active.id)?.name;
75
+ document.getElementById("mm-active").replaceChildren(activeLabel
76
+ ? h("span", {}, "In use: ", h("strong", {}, activeLabel), active.kind === "api" ? " (API)" : " (on this device)")
77
+ : h("span", {}, "No model in use yet. Download a local model or add an API model, then press Use."));
78
+
79
+ const gpu = deps.gpuNote();
80
+ document.getElementById("mm-local-note").replaceChildren(
81
+ gpu && !gpu.ok ? h("p", { class: "mm-warn" }, gpu.text) : null,
82
+ deps.isMobile() ? h("p", { class: "mm-warn" }, "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,
83
+ );
84
+ document.getElementById("mm-local").replaceChildren(...LOCAL_MODELS.map(localRow));
85
+ document.getElementById("mm-api").replaceChildren(
86
+ ...(settings.apiModels.length ? settings.apiModels.map(apiRow)
87
+ : [h("p", { class: "mm-empty" }, "No API models yet. Add one to use OpenAI, Anthropic, Gemini, OpenRouter, Hugging Face and more. Linux still runs on this device.")]),
88
+ );
89
+ }
90
+
91
+ function localRow(m) {
92
+ const sel = { kind: "local", id: m.id };
93
+ const d = downloads.get(m.id);
94
+ const inUse = deps.isActive(sel);
95
+ const have = isDownloaded(m);
96
+ const tags = [
97
+ m.id === LOCAL_MODELS[0].id ? h("span", { class: "mo-tag" }, "Recommended") : null,
98
+ !m.thinking ? h("span", { class: "mo-tag" }, "No reasoning") : null,
99
+ inUse ? h("span", { class: "mo-tag on" }, "In use") : null,
100
+ ];
101
+ let actions;
102
+ if (d && !d.error) {
103
+ const pct = d.total ? Math.round((d.loaded / d.total) * 100) : 0;
104
+ actions = [
105
+ h("span", { class: "mm-progress-text" }, `${pct}% · ${fmtBytes(d.loaded)} of ${fmtBytes(d.total)}`),
106
+ h("button", { class: "btn small", type: "button", onclick: () => d.ctrl.abort() }, "Cancel"),
107
+ ];
108
+ } else if (have) {
109
+ actions = [
110
+ inUse ? null : h("button", { class: "btn small primary", type: "button", disabled: busy != null, onclick: () => use(sel) }, busy === m.id ? "Loading…" : "Use"),
111
+ h("button", { class: "btn small", type: "button", onclick: () => remove(m) }, "Remove"),
112
+ ];
113
+ } else {
114
+ actions = [h("button", { class: "btn small primary", type: "button", onclick: () => download(m) }, `Download · ${fmtBytes(m.bytes)}`)];
115
+ }
116
+ return h("div", { class: "mm-row" + (inUse ? " active" : ""), "data-local": m.id },
117
+ logoTile({ mono: MAKER_BADGE[m.by] }),
118
+ h("div", { class: "mm-main" },
119
+ h("div", { class: "mm-name" }, m.label, tags),
120
+ h("div", { class: "mm-note" }, m.note),
121
+ h("div", { class: "mm-meta" }, [m.by, m.license, `${Math.round(m.contextMax / 1024)}k context`,
122
+ have ? `downloaded, ${fmtBytes(cached.get(m.id))}` : `${fmtBytes(m.bytes)} download`].join(" · ")),
123
+ d?.error ? h("div", { class: "mm-error" }, d.error) : null,
124
+ d && !d.error ? h("progress", { max: 1, value: d.total ? d.loaded / d.total : 0 }) : null),
125
+ h("div", { class: "mm-actions" }, actions));
126
+ }
127
+
128
+ function apiRow(a) {
129
+ const p = findPreset(a.preset);
130
+ const sel = { kind: "api", id: a.id };
131
+ const inUse = deps.isActive(sel);
132
+ return h("div", { class: "mm-row" + (inUse ? " active" : ""), "data-api": a.id },
133
+ logoTile(p),
134
+ h("div", { class: "mm-main" },
135
+ h("div", { class: "mm-name" }, a.name, inUse ? h("span", { class: "mo-tag on" }, "In use") : null),
136
+ h("div", { class: "mm-meta" }, [p.label, a.model, hostOf(a.baseUrl)].join(" · ")),
137
+ !p.noKey && !loadApiKey(a.id) ? h("div", { class: "mm-error" }, "No key stored (it was only kept for that tab). Edit to enter it again.") : null),
138
+ h("div", { class: "mm-actions" },
139
+ inUse ? null : h("button", { class: "btn small primary", type: "button", disabled: busy != null, onclick: () => use(sel) }, busy === a.id ? "Connecting…" : "Use"),
140
+ h("button", { class: "btn small", type: "button", onclick: () => openEditor(a) }, "Edit"),
141
+ h("button", { class: "btn small", type: "button", onclick: () => removeApi(a) }, "Delete")));
142
+ }
143
+
144
+ async function use(sel) {
145
+ busy = sel.id;
146
+ render();
147
+ try { await deps.activate(sel); }
148
+ catch (err) { alert(err.message); }
149
+ finally { busy = null; render(); }
150
+ }
151
+
152
+ async function download(m) {
153
+ const ctrl = new AbortController();
154
+ const d = { loaded: 0, total: m.bytes, ctrl, error: null };
155
+ downloads.set(m.id, d);
156
+ render();
157
+ let lastRender = 0;
158
+ try {
159
+ await setup.requestPersistence();
160
+ await setup.downloadModel(m, {
161
+ signal: ctrl.signal,
162
+ onProgress: (loaded, total) => {
163
+ d.loaded = loaded; d.total = total;
164
+ const row = dlg.querySelector(`[data-local="${m.id}"]`);
165
+ const now = performance.now();
166
+ if (row && now - lastRender > 100) {
167
+ lastRender = now;
168
+ const pr = row.querySelector("progress");
169
+ if (pr) pr.value = total ? loaded / total : 0;
170
+ const t = row.querySelector(".mm-progress-text");
171
+ if (t) t.textContent = `${total ? Math.round((loaded / total) * 100) : 0}% · ${fmtBytes(loaded)} of ${fmtBytes(total)}`;
172
+ }
173
+ },
174
+ });
175
+ downloads.delete(m.id);
176
+ await refreshCached();
177
+ // First model ever: start using it right away.
178
+ if (!settings.activeModel) await use({ kind: "local", id: m.id });
179
+ } catch (err) {
180
+ if (err.name === "AbortError") downloads.delete(m.id);
181
+ else d.error = "Download failed: " + err.message;
182
+ }
183
+ render();
184
+ }
185
+
186
+ async function remove(m) {
187
+ const sel = { kind: "local", id: m.id };
188
+ const inUse = deps.isActive(sel);
189
+ if (!confirm(`Remove ${m.label} (${fmtBytes(cached.get(m.id) || m.bytes)}) from this browser?${inUse ? " It is in use and will be unloaded." : ""} You can download it again later.`)) return;
190
+ if (inUse) await deps.activate(null);
191
+ await setup.removeModel(m);
192
+ await refreshCached();
193
+ render();
194
+ }
195
+
196
+ async function removeApi(a) {
197
+ if (!confirm(`Delete the API model "${a.name}" and its stored key?`)) return;
198
+ if (deps.isActive({ kind: "api", id: a.id })) await deps.activate(null);
199
+ settings.apiModels = settings.apiModels.filter((x) => x.id !== a.id);
200
+ saveSettings();
201
+ deleteApiKey(a.id);
202
+ render();
203
+ }
204
+
205
+ // ---------- API model editor ----------
206
+ let editing = null; // existing entry, or null when adding
207
+ let preset = API_PRESETS[0];
208
+ const $e = (id) => document.getElementById(id);
209
+ const note = (text, kind = "") => { const n = $e("ae-note"); n.className = "note " + kind; n.textContent = text; };
210
+
211
+ function openEditor(entry = null) {
212
+ editing = entry;
213
+ $e("ae-title").textContent = entry ? "Edit API model" : "Add an API model";
214
+ preset = entry ? findPreset(entry.preset) : API_PRESETS[0];
215
+ $e("ae-base").value = entry?.baseUrl ?? preset.baseUrl;
216
+ $e("ae-model").value = entry?.model ?? "";
217
+ $e("ae-name").value = entry?.name ?? "";
218
+ $e("ae-key").value = entry ? loadApiKey(entry.id) : "";
219
+ $e("ae-remember").checked = entry ? apiKeyRemembered(entry.id) : true;
220
+ $e("ae-models").replaceChildren();
221
+ $e("ae-save").textContent = entry ? "Save" : "Add and use";
222
+ note("");
223
+ renderProviders();
224
+ syncPreset(false);
225
+ editDlg.showModal();
226
+ }
227
+
228
+ function renderProviders() {
229
+ $e("ae-providers").replaceChildren(...API_PRESETS.map((p) => h("button", {
230
+ type: "button", class: "provider" + (p.id === preset.id ? " on" : ""), "aria-pressed": String(p.id === preset.id), title: p.label,
231
+ onclick: () => { preset = p; renderProviders(); syncPreset(true); },
232
+ }, logoTile(p, "sm"), h("span", {}, p.label))));
233
+ }
234
+
235
+ function syncPreset(fill) {
236
+ if (fill) { $e("ae-base").value = preset.baseUrl; $e("ae-models").replaceChildren(); }
237
+ $e("ae-base-row").hidden = preset.id !== "custom" && !!preset.baseUrl && $e("ae-base").value === preset.baseUrl;
238
+ $e("ae-key-row").hidden = !!preset.noKey;
239
+ $e("ae-hint").replaceChildren(
240
+ preset.hint ? h("span", {}, preset.hint + " ") : null,
241
+ preset.keysUrl ? h("a", { href: preset.keysUrl, target: "_blank", rel: "noopener" }, `Get your ${preset.label} API key`) : null);
242
+ $e("ae-base-shown").textContent = $e("ae-base").value;
243
+ $e("ae-base-summary").hidden = !$e("ae-base-row").hidden;
244
+ }
245
+
246
+ const form = () => ({
247
+ baseUrl: $e("ae-base").value.trim(),
248
+ model: $e("ae-model").value.trim(),
249
+ key: preset.noKey ? $e("ae-key").value.trim() : $e("ae-key").value.trim(),
250
+ headers: preset.headers,
251
+ });
252
+
253
+ $e("ae-base-edit").addEventListener("click", () => { $e("ae-base-row").hidden = false; $e("ae-base-summary").hidden = true; $e("ae-base").focus(); });
254
+ $e("ae-base").addEventListener("input", () => { $e("ae-base-shown").textContent = $e("ae-base").value; });
255
+ $e("ae-close").addEventListener("click", () => editDlg.close());
256
+ editDlg.addEventListener("click", (e) => { if (e.target === editDlg) editDlg.close(); });
257
+ dlg.addEventListener("click", (e) => { if (e.target === dlg) dlg.close(); });
258
+ $e("mm-close").addEventListener("click", () => dlg.close());
259
+ $e("mm-add-api").addEventListener("click", () => openEditor(null));
260
+
261
+ $e("ae-load-models").addEventListener("click", async () => {
262
+ const f = form();
263
+ if (!f.baseUrl) return note("Enter the base URL first.", "error");
264
+ const btn = $e("ae-load-models");
265
+ btn.disabled = true;
266
+ note("Loading the provider's models…");
267
+ try {
268
+ const ids = await RemoteLLM.listModels(f);
269
+ $e("ae-models").replaceChildren(...ids.map((id) => h("option", { value: id })));
270
+ note(ids.length ? `${ids.length} models found. Start typing in Model ID to pick one.` : "The provider returned no models.", ids.length ? "ok" : "");
271
+ $e("ae-model").focus();
272
+ } catch (err) { note(err.message, "error"); }
273
+ finally { btn.disabled = false; }
274
+ });
275
+
276
+ $e("ae-test").addEventListener("click", async () => {
277
+ const f = form();
278
+ if (!f.baseUrl || !f.model) return note("Enter a model ID first.", "error");
279
+ const btn = $e("ae-test");
280
+ btn.disabled = true;
281
+ note("Testing…");
282
+ try { note(`Works. The model replied: "${(await RemoteLLM.test(f)).slice(0, 80)}"`, "ok"); }
283
+ catch (err) { note(err.message, "error"); }
284
+ finally { btn.disabled = false; }
285
+ });
286
+
287
+ $e("ae-form").addEventListener("submit", async (e) => {
288
+ e.preventDefault();
289
+ const f = form();
290
+ if (!f.baseUrl || !f.model) return note("Enter a base URL and a model ID.", "error");
291
+ try { new URL(f.baseUrl); } catch { return note("The base URL isn't a valid URL.", "error"); }
292
+ if (!preset.noKey && !f.key) return note(`Enter your ${preset.label} API key.`, "error");
293
+ const entry = {
294
+ id: editing?.id ?? newId(),
295
+ name: $e("ae-name").value.trim() || f.model,
296
+ preset: preset.id,
297
+ baseUrl: f.baseUrl,
298
+ model: f.model,
299
+ };
300
+ if (editing) settings.apiModels = settings.apiModels.map((a) => (a.id === entry.id ? entry : a));
301
+ else settings.apiModels.push(entry);
302
+ saveSettings();
303
+ saveApiKey(entry.id, f.key, $e("ae-remember").checked);
304
+ editDlg.close();
305
+ render();
306
+ const sel = { kind: "api", id: entry.id };
307
+ // New entries are put to use; edits of the active one reconnect with the new settings.
308
+ if (!editing || deps.isActive(sel)) await use(sel);
309
+ });
310
+
311
+ return { open, openEditor, render, refreshCached };
312
+ }
js/remote-llm.js CHANGED
@@ -7,34 +7,63 @@
7
  // the streamed reply is rendered back into that text format for the agent to parse.
8
  import { parseAssistant } from "./toolcalls.js";
9
 
 
 
 
10
  export const API_PRESETS = [
11
- { id: "openrouter", label: "OpenRouter", baseUrl: "https://openrouter.ai/api/v1" },
12
- { id: "openai", label: "OpenAI", baseUrl: "https://api.openai.com/v1" },
13
- { id: "huggingface", label: "Hugging Face", baseUrl: "https://router.huggingface.co/v1" },
14
- { id: "custom", label: "Other (OpenAI-compatible)", baseUrl: "" },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ];
 
16
 
17
- const KEY_STORE = "lwa-api-key";
 
 
 
18
 
19
- export function loadApiKey() {
20
- try { return localStorage.getItem(KEY_STORE) ?? sessionStorage.getItem(KEY_STORE) ?? ""; } catch { return ""; }
 
21
  }
22
-
23
- /** remember=true keeps the key on this device; otherwise only until the tab is closed. */
24
- export function saveApiKey(key, remember) {
25
- try {
26
- localStorage.removeItem(KEY_STORE);
27
- sessionStorage.removeItem(KEY_STORE);
28
- if (key) (remember ? localStorage : sessionStorage).setItem(KEY_STORE, key);
29
- } catch {}
30
  }
 
31
 
32
- export const apiKeyRemembered = () => { try { return localStorage.getItem(KEY_STORE) != null; } catch { return false; } };
 
 
 
 
 
 
33
 
34
  const endpoint = (baseUrl) => baseUrl.replace(/\/+$/, "").replace(/\/chat\/completions$/, "") + "/chat/completions";
35
 
36
- function headersFor(key) {
37
- const h = { "Content-Type": "application/json" };
38
  if (key) h.Authorization = `Bearer ${key}`;
39
  return h;
40
  }
@@ -115,7 +144,7 @@ export function toApiMessages(messages) {
115
  // ---------- backend ----------
116
 
117
  export class RemoteLLM {
118
- /** config: { baseUrl, model, key } */
119
  constructor(config) {
120
  this.config = config;
121
  this.ready = false;
@@ -143,7 +172,7 @@ export class RemoteLLM {
143
  try {
144
  res = await fetch(endpoint(config.baseUrl), {
145
  method: "POST",
146
- headers: headersFor(config.key),
147
  body: JSON.stringify({ model: config.model, messages: [{ role: "user", content: "Reply with the word OK." }] }),
148
  });
149
  } catch (err) { throw networkError(config.baseUrl, err); }
@@ -154,8 +183,23 @@ export class RemoteLLM {
154
  return String(msg.content ?? "").trim() || "(empty reply)";
155
  }
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  async generate(messages, tools, options = {}, onEvent = null) {
158
- const { baseUrl, model, key } = this.config;
159
  const ctrl = new AbortController();
160
  this.ctrl = ctrl;
161
  const t0 = performance.now();
@@ -195,7 +239,7 @@ export class RemoteLLM {
195
  try {
196
  res = await fetch(endpoint(baseUrl), {
197
  method: "POST",
198
- headers: headersFor(key),
199
  body: JSON.stringify({ model, messages: toApiMessages(messages), tools, tool_choice: "auto", stream: true }),
200
  signal: ctrl.signal,
201
  });
 
7
  // the streamed reply is rendered back into that text format for the agent to parse.
8
  import { parseAssistant } from "./toolcalls.js";
9
 
10
+ // Providers with an OpenAI-compatible Chat Completions API that accept requests from web
11
+ // pages (CORS preflight checked for each). `logo` is a key of LOGOS (logos.js); providers
12
+ // Simple Icons has no mark for get a letter badge (`mono`) instead.
13
  export const API_PRESETS = [
14
+ { id: "openrouter", label: "OpenRouter", baseUrl: "https://openrouter.ai/api/v1", logo: "openrouter", keysUrl: "https://openrouter.ai/keys",
15
+ hint: "Hundreds of models from every major lab behind one key." },
16
+ { id: "openai", label: "OpenAI", baseUrl: "https://api.openai.com/v1", mono: { text: "AI", bg: "#0d0d0d" }, keysUrl: "https://platform.openai.com/api-keys" },
17
+ { id: "anthropic", label: "Anthropic", baseUrl: "https://api.anthropic.com/v1", logo: "anthropic", keysUrl: "https://console.anthropic.com/settings/keys",
18
+ headers: { "anthropic-dangerous-direct-browser-access": "true" }, hint: "Uses Anthropic's OpenAI-compatible endpoint." },
19
+ { id: "gemini", label: "Google Gemini", baseUrl: "https://generativelanguage.googleapis.com/v1beta/openai", logo: "googlegemini", keysUrl: "https://aistudio.google.com/apikey" },
20
+ { id: "huggingface", label: "Hugging Face", baseUrl: "https://router.huggingface.co/v1", logo: "huggingface", keysUrl: "https://huggingface.co/settings/tokens",
21
+ hint: "Inference Providers: open models from many hosts. Use a token with the Inference permission." },
22
+ { id: "groq", label: "Groq", baseUrl: "https://api.groq.com/openai/v1", mono: { text: "G", bg: "#f55036" }, keysUrl: "https://console.groq.com/keys" },
23
+ { id: "mistral", label: "Mistral AI", baseUrl: "https://api.mistral.ai/v1", logo: "mistralai", keysUrl: "https://console.mistral.ai/api-keys" },
24
+ { id: "deepseek", label: "DeepSeek", baseUrl: "https://api.deepseek.com/v1", logo: "deepseek", keysUrl: "https://platform.deepseek.com/api_keys" },
25
+ { id: "xai", label: "xAI", baseUrl: "https://api.x.ai/v1", mono: { text: "xAI", bg: "#0d0d0d" }, keysUrl: "https://console.x.ai" },
26
+ { id: "together", label: "Together AI", baseUrl: "https://api.together.xyz/v1", mono: { text: "T", bg: "#0f6fff" }, keysUrl: "https://api.together.ai/settings/api-keys" },
27
+ { id: "fireworks", label: "Fireworks AI", baseUrl: "https://api.fireworks.ai/inference/v1", mono: { text: "F", bg: "#6720ff" }, keysUrl: "https://fireworks.ai/account/api-keys" },
28
+ { id: "cerebras", label: "Cerebras", baseUrl: "https://api.cerebras.ai/v1", mono: { text: "C", bg: "#f05a28" }, keysUrl: "https://cloud.cerebras.ai" },
29
+ { id: "ollama", label: "Ollama", baseUrl: "http://localhost:11434/v1", logo: "ollama", noKey: true,
30
+ hint: "Runs on your computer. Start it with OLLAMA_ORIGINS set to this page's address; Chrome may ask to allow access to local devices." },
31
+ { id: "lmstudio", label: "LM Studio", baseUrl: "http://localhost:1234/v1", logo: "lmstudio", noKey: true,
32
+ hint: "Runs on your computer. Turn on CORS in LM Studio's server settings; Chrome may ask to allow access to local devices." },
33
+ { id: "custom", label: "Other", baseUrl: "", mono: { text: "+", bg: "#71717a" }, hint: "Any OpenAI-compatible Chat Completions API." },
34
  ];
35
+ export const findPreset = (id) => API_PRESETS.find((p) => p.id === id) ?? API_PRESETS[API_PRESETS.length - 1];
36
 
37
+ // Keys live apart from the settings, one per saved API model: in localStorage when the
38
+ // user asked to remember it, otherwise in sessionStorage (gone when the tab closes).
39
+ const keyName = (id) => `lwa-api-key:${id}`;
40
+ const storages = () => { try { return [localStorage, sessionStorage]; } catch { return []; } };
41
 
42
+ export function loadApiKey(id) {
43
+ for (const st of storages()) { const v = st.getItem(keyName(id)); if (v != null) return v; }
44
+ return "";
45
  }
46
+ export function saveApiKey(id, key, remember) {
47
+ deleteApiKey(id);
48
+ try { if (key) (remember ? localStorage : sessionStorage).setItem(keyName(id), key); } catch {}
49
+ }
50
+ export function deleteApiKey(id) {
51
+ for (const st of storages()) st.removeItem(keyName(id));
 
 
52
  }
53
+ export const apiKeyRemembered = (id) => { try { return localStorage.getItem(keyName(id)) != null; } catch { return false; } };
54
 
55
+ /** Moves the single key of v0.4 (before multiple API models) to the given model id. */
56
+ export function migrateLegacyKey(id) {
57
+ for (const st of storages()) {
58
+ const v = st.getItem("lwa-api-key");
59
+ if (v != null) { st.setItem(keyName(id), v); st.removeItem("lwa-api-key"); }
60
+ }
61
+ }
62
 
63
  const endpoint = (baseUrl) => baseUrl.replace(/\/+$/, "").replace(/\/chat\/completions$/, "") + "/chat/completions";
64
 
65
+ function headersFor(key, extra) {
66
+ const h = { "Content-Type": "application/json", ...extra };
67
  if (key) h.Authorization = `Bearer ${key}`;
68
  return h;
69
  }
 
144
  // ---------- backend ----------
145
 
146
  export class RemoteLLM {
147
+ /** config: { baseUrl, model, key, headers } */
148
  constructor(config) {
149
  this.config = config;
150
  this.ready = false;
 
172
  try {
173
  res = await fetch(endpoint(config.baseUrl), {
174
  method: "POST",
175
+ headers: headersFor(config.key, config.headers),
176
  body: JSON.stringify({ model: config.model, messages: [{ role: "user", content: "Reply with the word OK." }] }),
177
  });
178
  } catch (err) { throw networkError(config.baseUrl, err); }
 
183
  return String(msg.content ?? "").trim() || "(empty reply)";
184
  }
185
 
186
+ /** The provider's model IDs (GET /models), sorted; for the model ID field's suggestions. */
187
+ static async listModels(config) {
188
+ const url = config.baseUrl.replace(/\/+$/, "").replace(/\/chat\/completions$/, "") + "/models";
189
+ let res;
190
+ try {
191
+ const h = headersFor(config.key, config.headers);
192
+ delete h["Content-Type"];
193
+ res = await fetch(url, { headers: h });
194
+ } catch (err) { throw networkError(config.baseUrl, err); }
195
+ if (!res.ok) throw await httpError(res);
196
+ const j = await res.json().catch(() => null);
197
+ const list = Array.isArray(j) ? j : j?.data ?? j?.models ?? [];
198
+ return [...new Set(list.map((m) => (typeof m === "string" ? m : m.id ?? m.name)).filter(Boolean))].sort();
199
+ }
200
+
201
  async generate(messages, tools, options = {}, onEvent = null) {
202
+ const { baseUrl, model, key, headers } = this.config;
203
  const ctrl = new AbortController();
204
  this.ctrl = ctrl;
205
  const t0 = performance.now();
 
239
  try {
240
  res = await fetch(endpoint(baseUrl), {
241
  method: "POST",
242
+ headers: headersFor(key, headers),
243
  body: JSON.stringify({ model, messages: toApiMessages(messages), tools, tool_choice: "auto", stream: true }),
244
  signal: ctrl.signal,
245
  });
js/setup.js CHANGED
@@ -67,6 +67,49 @@ export async function modelCachedBytes(model) {
67
  return total;
68
  }
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  /** Deletes one model's downloaded files. */
71
  export async function removeModel(model) {
72
  const cache = await caches.open(CACHE_MODEL);
 
67
  return total;
68
  }
69
 
70
+ /**
71
+ * Downloads every file a local model needs into Cache Storage (the cache transformers.js
72
+ * reads), streaming so big weight files never sit in memory. Files already cached are
73
+ * skipped. onProgress(loaded, total) in bytes; signal aborts. A file is only stored once
74
+ * it arrived completely.
75
+ */
76
+ export async function downloadModel(model, { onProgress, signal } = {}) {
77
+ const cache = await caches.open(CACHE_MODEL);
78
+ const files = modelFiles(model);
79
+ const sizes = new Map(); // file -> known size
80
+ let done = 0; // bytes of finished files
81
+ let total = model.bytes;
82
+ const report = (current) => onProgress?.(Math.min(done + current, total), total);
83
+ for (const f of files) {
84
+ const url = hubUrl(model, f);
85
+ const hit = await cache.match(url);
86
+ if (hit) { done += Number(hit.headers.get("Content-Length")) || 0; continue; }
87
+ const res = await fetch(url, { signal });
88
+ if (!res.ok || !res.body) throw new Error(`Download failed (HTTP ${res.status}) for ${f}`);
89
+ const size = Number(res.headers.get("Content-Length")) || 0;
90
+ sizes.set(f, size);
91
+ total = Math.max(total, done + size);
92
+ let got = 0;
93
+ let last = 0;
94
+ const counted = res.body.pipeThrough(new TransformStream({
95
+ transform(chunk, ctl) {
96
+ got += chunk.byteLength;
97
+ const now = performance.now();
98
+ if (now - last > 150) { last = now; report(got); }
99
+ ctl.enqueue(chunk);
100
+ },
101
+ }));
102
+ await cache.put(url, new Response(counted, { status: 200, headers: new Headers(res.headers) }));
103
+ if (size && got !== size) {
104
+ await cache.delete(url);
105
+ throw new Error(`Incomplete download of ${f} (${got} of ${size} bytes)`);
106
+ }
107
+ done += got;
108
+ report(0);
109
+ }
110
+ onProgress?.(done, done);
111
+ }
112
+
113
  /** Deletes one model's downloaded files. */
114
  export async function removeModel(model) {
115
  const cache = await caches.open(CACHE_MODEL);
sw.js CHANGED
@@ -6,7 +6,7 @@
6
  // (__vm__/disk.ext2), backed by the chunks the setup screen stored in Cache Storage.
7
  // 4. Adds the cross-origin isolation headers (COOP/COEP) that SharedArrayBuffer,
8
  // CheerpX and multi-threaded WASM need, even when a host forgets to send them.
9
- const SHELL_VERSION = "lwa-shell-0.5.1";
10
  const CX_ORIGIN = "https://cxrtnc.leaningtech.com";
11
  const CX_CACHE_PREFIX = "lwa-cheerpx-";
12
  const DISK_CACHE = "lwa-disk";
@@ -18,7 +18,7 @@ const chunkKey = (i) => SCOPE + "__vm__/chunk/" + i;
18
  const SHELL_FILES = [
19
  "./", "index.html", "css/app.css", "icon.svg",
20
  "js/config.js", "js/config-defaults.js", "js/main.js", "js/setup.js", "js/vm.js", "js/agent.js", "js/llm.js",
21
- "js/llm-worker.js", "js/llm-engine.js", "js/lazy-weights.js", "js/remote-llm.js", "js/prompts.js", "js/toolcalls.js", "js/markdown.js", "js/files.js",
22
  "vendor/transformers.min.js", "vendor/highlight.min.js",
23
  "vendor/ort/ort-wasm-simd-threaded.asyncify.mjs", "vendor/ort/ort-wasm-simd-threaded.asyncify.wasm",
24
  "vendor/xterm/xterm.mjs", "vendor/xterm/xterm.css", "vendor/xterm/addon-fit.mjs",
 
6
  // (__vm__/disk.ext2), backed by the chunks the setup screen stored in Cache Storage.
7
  // 4. Adds the cross-origin isolation headers (COOP/COEP) that SharedArrayBuffer,
8
  // CheerpX and multi-threaded WASM need, even when a host forgets to send them.
9
+ const SHELL_VERSION = "lwa-shell-0.6.0";
10
  const CX_ORIGIN = "https://cxrtnc.leaningtech.com";
11
  const CX_CACHE_PREFIX = "lwa-cheerpx-";
12
  const DISK_CACHE = "lwa-disk";
 
18
  const SHELL_FILES = [
19
  "./", "index.html", "css/app.css", "icon.svg",
20
  "js/config.js", "js/config-defaults.js", "js/main.js", "js/setup.js", "js/vm.js", "js/agent.js", "js/llm.js",
21
+ "js/llm-worker.js", "js/llm-engine.js", "js/lazy-weights.js", "js/remote-llm.js", "js/model-manager.js", "js/logos.js", "js/prompts.js", "js/toolcalls.js", "js/markdown.js", "js/files.js",
22
  "vendor/transformers.min.js", "vendor/highlight.min.js",
23
  "vendor/ort/ort-wasm-simd-threaded.asyncify.mjs", "vendor/ort/ort-wasm-simd-threaded.asyncify.wasm",
24
  "vendor/xterm/xterm.mjs", "vendor/xterm/xterm.css", "vendor/xterm/addon-fit.mjs",