@engenty/wizards-runtime 0.11.13 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/apps/runtime/dist/app.js +2 -0
  2. package/apps/runtime/dist/cli/account.js +1 -1
  3. package/apps/runtime/dist/cli/features.js +45 -2
  4. package/apps/runtime/dist/docs/dev/plugins/reference.md +3 -3
  5. package/apps/runtime/dist/docs/dev/plugins/studio.md +1 -1
  6. package/apps/runtime/dist/docs/user-guide/models.md +22 -2
  7. package/apps/runtime/dist/docs/user-guide/server.md +15 -2
  8. package/apps/runtime/dist/features.js +9 -0
  9. package/apps/runtime/dist/index.js +7 -0
  10. package/apps/runtime/dist/local-models.js +192 -0
  11. package/apps/runtime/dist/models.js +250 -75
  12. package/apps/runtime/dist/plugins/install.js +120 -0
  13. package/apps/runtime/dist/plugins/loader.js +33 -3
  14. package/apps/runtime/dist/plugins/pro.js +9 -9
  15. package/apps/runtime/dist/prompts/tools.md +1 -1
  16. package/apps/runtime/dist/routes/plugins.js +110 -2
  17. package/apps/runtime/dist/routes/runtime-settings.js +108 -0
  18. package/apps/runtime/dist/routes/studio.js +64 -2
  19. package/apps/runtime/dist/services/project-files.js +11 -1
  20. package/apps/runtime/dist/services/project-index.js +47 -4
  21. package/apps/runtime/dist/texts.js +4 -0
  22. package/apps/runtime/dist/update.js +10 -2
  23. package/apps/web/dist/assets/AboutDialog-BOJjBIYk.js +1 -0
  24. package/apps/web/dist/assets/{ChangelogDialog-Cxwkcjdy.js → ChangelogDialog-I9DEn_ra.js} +1 -1
  25. package/apps/web/dist/assets/{CodeEditor-BijLsoJX.js → CodeEditor-BgUofYLQ.js} +1 -1
  26. package/apps/web/dist/assets/EditorPage-DEOxHFXn.js +3 -0
  27. package/apps/web/dist/assets/{FlowPreview-Wjt28MdG.js → FlowPreview-C6DeATyk.js} +1 -1
  28. package/apps/web/dist/assets/{Marketplace-CpjRqTvn.js → Marketplace-Dt7ktvc3.js} +2 -2
  29. package/apps/web/dist/assets/{NewWizardChannels-CelW1MGq.js → NewWizardChannels-BrVFc6PV.js} +1 -1
  30. package/apps/web/dist/assets/{NewWizardPage-Coswm2WX.js → NewWizardPage-Dl8sMR2e.js} +1 -1
  31. package/apps/web/dist/assets/OnboardingPage-BN5IfLGL.js +1 -0
  32. package/apps/web/dist/assets/{PreviewPane-BUwlltqA.js → PreviewPane-BDzDuTz1.js} +5 -5
  33. package/apps/web/dist/assets/ProjectPage-BqkkyEpP.js +1 -0
  34. package/apps/web/dist/assets/PublicRunner-bAakEGwv.js +1 -0
  35. package/apps/web/dist/assets/{ReadOnly-DZMZPM-W.js → ReadOnly-MBVPGEhw.js} +1 -1
  36. package/apps/web/dist/assets/SettingsPage-DDe1BC-3.js +2 -0
  37. package/apps/web/dist/assets/SetupPage-CmTC9K8_.js +1 -0
  38. package/apps/web/dist/assets/{SharePage-DF8LO8-K.js → SharePage-D9_IxO3M.js} +1 -1
  39. package/apps/web/dist/assets/changelog-iTu1De5h.js +1 -0
  40. package/apps/web/dist/assets/i18n-7CvqRUdn.js +55 -0
  41. package/apps/web/dist/assets/index-CNwXyMzH.js +49 -0
  42. package/apps/web/dist/assets/index-Dxwbli0m.css +1 -0
  43. package/apps/web/dist/assets/{wrench-Dr19y-10.js → lock-D1093O6-.js} +1 -1
  44. package/apps/web/dist/assets/{phone-JGS60FQU.js → phone-YeDiW-kN.js} +1 -1
  45. package/apps/web/dist/assets/{runners-CwTP03YR.js → runners-DP0vYvTF.js} +1 -1
  46. package/apps/web/dist/assets/session-Bio3Vaq1.js +2 -0
  47. package/apps/web/dist/assets/{theme-D6QLPvJL.js → theme-AlaEfdLQ.js} +2 -2
  48. package/apps/web/dist/assets/{ui-Bhp_a_44.js → ui-CU-cIuyK.js} +2 -2
  49. package/apps/web/dist/assets/{upload-DPLDQsRT.js → upload-DbWtEr1f.js} +1 -1
  50. package/apps/web/dist/assets/{useMutation-Cmn8b7tT.js → useMutation-erJIov3t.js} +1 -1
  51. package/apps/web/dist/assets/{volume-2-DGqbhyPO.js → volume-2-V1P9DYWO.js} +1 -1
  52. package/apps/web/dist/index.html +8 -8
  53. package/apps/web/dist/manifest.webmanifest +1 -1
  54. package/apps/web/dist/mcp/flow.html +25 -25
  55. package/node_modules/@engenty/wizards/dist/plugin/studio.d.ts +1 -1
  56. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +6 -0
  57. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +6 -0
  58. package/package.json +1 -1
  59. package/plugin/.claude-plugin/plugin.json +1 -1
  60. package/plugin/plugin.json +1 -1
  61. package/apps/web/dist/assets/AboutDialog-pMHleH_y.js +0 -1
  62. package/apps/web/dist/assets/EditorPage-v7cpENx3.js +0 -3
  63. package/apps/web/dist/assets/Harness-BrP-ENHg.css +0 -1
  64. package/apps/web/dist/assets/Harness-TUpGhGX5.js +0 -16
  65. package/apps/web/dist/assets/OnboardingPage-C9XM_VXs.js +0 -1
  66. package/apps/web/dist/assets/ProjectPage-Cw7hdXO6.js +0 -1
  67. package/apps/web/dist/assets/PublicRunner-B5RGNs07.js +0 -1
  68. package/apps/web/dist/assets/SettingsPage-CfL1e3Vs.js +0 -2
  69. package/apps/web/dist/assets/SetupPage-BRPenYa-.js +0 -1
  70. package/apps/web/dist/assets/changelog-CO6ysfQg.js +0 -1
  71. package/apps/web/dist/assets/i18n-BYeH2Dmi.js +0 -55
  72. package/apps/web/dist/assets/index-BYZBN0xx.css +0 -1
  73. package/apps/web/dist/assets/index-Bl7qOZv0.js +0 -34
  74. package/apps/web/dist/assets/session-CngLvd02.js +0 -2
  75. package/apps/web/dist/assets/settings-menu-C0yo5q3t.js +0 -1
@@ -36,6 +36,7 @@ import { publicPluginRoutes } from "./routes/plugin-public.js";
36
36
  import { pluginRoutes } from "./routes/plugins.js";
37
37
  import { reportRoutes } from "./routes/reports.js";
38
38
  import { clientIp, connectCallback, publicRoutes, runRoutes, shareRoutes } from "./routes/runs.js";
39
+ import { runtimeSettingsRoutes } from "./routes/runtime-settings.js";
39
40
  import { studio } from "./routes/studio.js";
40
41
  import { wizardStream } from "./routes/wizard-stream.js";
41
42
  import { runTicketValid } from "./secrets/signing.js";
@@ -306,6 +307,7 @@ if (serves.studio) {
306
307
  app.route("/api/studio/plugins", pluginRoutes);
307
308
  app.route("/api/studio/organization", organizationRoutes);
308
309
  app.route("/api/studio/person", personRoutes);
310
+ app.route("/api/studio/runtime", runtimeSettingsRoutes);
309
311
  app.route("/api/studio", studio);
310
312
  app.route("/api/v1", apiRoutes);
311
313
  app.route("/api/local/debug", localDebugRoutes);
@@ -235,7 +235,7 @@ async function pickUp(paths, interactive, why) {
235
235
  const { restart } = await import("./status.js");
236
236
  await restart(paths);
237
237
  }
238
- const SIGNED_IN = "your plan’s Pro modules load and your published wizards arrive in the cloud";
238
+ const SIGNED_IN = "your plan’s Pro plugins load and your published wizards arrive in the cloud";
239
239
  async function signIn(paths, interactive) {
240
240
  await ready();
241
241
  const linked = await linkedAccount();
@@ -1,3 +1,4 @@
1
+ import * as p from "@clack/prompts";
1
2
  import { defaultFeatures, FEATURE_NAME, parseFeatures, resolveFeatures, } from "@engenty/wizards-shared/features";
2
3
  import { declaredFeatures, featuresFile, fileFeatures, writeFeature } from "../features.js";
3
4
  import { discoverPlugins } from "../plugins/discovery.js";
@@ -8,11 +9,11 @@ import { cmd, spelled, tilde } from "./ui.js";
8
9
  * `wizards features` — the feature switches of this install, from its `.features` file. Not in
9
10
  * the help: for whoever works on a part before it is on for everyone.
10
11
  *
11
- * features what is on, and where that comes from
12
+ * features what is on, and where that comes from (in a terminal: a checkbox list)
12
13
  * features <name> on|off put a switch into the file
13
14
  * features <name> default take it out again
14
15
  */
15
- export function featuresCommand(args) {
16
+ export async function featuresCommand(args) {
16
17
  const [name, value] = args;
17
18
  const file = featuresFile();
18
19
  if (!name) {
@@ -25,6 +26,9 @@ export function featuresCommand(args) {
25
26
  const names = [
26
27
  ...new Set([...Object.keys(declared), ...Object.keys(fromEnv), ...Object.keys(fromFile)]),
27
28
  ];
29
+ if (process.stdin.isTTY && process.stdout.isTTY) {
30
+ return pickFeatures(names, declared, fromEnv, fromFile, all, file);
31
+ }
28
32
  for (const n of names) {
29
33
  const source = n in fromFile ? "file" : n in fromEnv ? "FEATURES" : "default";
30
34
  const d = declared[n];
@@ -48,3 +52,42 @@ export function featuresCommand(args) {
48
52
  console.error(`Usage: ${cmd(`features ${name} on|off|default`)}`);
49
53
  return 2;
50
54
  }
55
+ /**
56
+ * The switches as a checkbox list. What is ticked is on; a switch that ends up as it would be
57
+ * without the file leaves the file, so it follows the default again.
58
+ */
59
+ async function pickFeatures(names, declared, fromEnv, fromFile, all, file) {
60
+ const without = resolveFeatures(defaultFeatures(declared), fromEnv);
61
+ const answer = await p.multiselect({
62
+ message: spelled("Which switches should be on? (space to switch, enter to save)"),
63
+ options: names.map((n) => ({
64
+ value: n,
65
+ label: n,
66
+ hint: [
67
+ spelled(declared[n]?.label ?? ""),
68
+ n in fromFile ? "set in the file" : n in fromEnv ? "set by FEATURES" : "",
69
+ ]
70
+ .filter(Boolean)
71
+ .join(", "),
72
+ })),
73
+ initialValues: names.filter((n) => all[n]),
74
+ required: false,
75
+ });
76
+ if (p.isCancel(answer)) {
77
+ p.cancel("Nothing changed.");
78
+ return 130;
79
+ }
80
+ const changed = [];
81
+ for (const n of names) {
82
+ const on = answer.includes(n);
83
+ const next = on === Boolean(without[n]) ? undefined : on;
84
+ if (next !== fromFile[n]) {
85
+ writeFeature(n, next ?? null, file);
86
+ changed.push(`${n} ${on ? "on" : "off"}${next === undefined ? " (default)" : ""}`);
87
+ }
88
+ }
89
+ p.outro(changed.length
90
+ ? spelled(`${changed.join(", ")}. Saved in ${tilde(file)}; engenty wizards picks this up straight away.`)
91
+ : "Nothing changed.");
92
+ return 0;
93
+ }
@@ -250,7 +250,7 @@ The default export is a `StudioPlugin`: `(studio: StudioPluginContext) => void`.
250
250
  | `studio.plugin` | `{ id, name, version }` |
251
251
  | `studio.registerPage({ path, component, wide? })` | A page at `/studio<path>` |
252
252
  | `studio.registerNav({ to, label, icon })` | An icon in the app bar. `label: () => string` |
253
- | `studio.registerSettingsSection({ id, label, icon, component, menu? })` | A section at `/studio/settings/<id>` |
253
+ | `studio.registerSettingsSection({ id, label, icon, component, menu? })` | A section one level down in Settings › Plugins, at `/studio/settings/plugins?show=<id>` |
254
254
  | `studio.registerShareSection({ runner, component })` | [Runners](./runners.md#the-runners-part-of-the-share-dialog). The runner’s own settings for a wizard (a keyword), under the share dialog’s gear once it is switched on and set up. `component` gets `{ wizard: { id, title } }` |
255
255
  | `studio.registerShareSetup({ runner, title, component, notice? })` | [Runners](./runners.md#setting-up-inside-the-dialog). What “Set up” in the runner’s row opens inside the share dialog. `component` gets `{ wizard, Actions, done(banner?) }`, `notice` `{ wizard, open }` |
256
256
  | `studio.registerAssistantCard({ tool, component })` | Draws a tool’s result in the space assistant’s chat. `component` gets `{ data, send }` |
@@ -311,8 +311,8 @@ And public, nobody signed in:
311
311
  | `ALL /api/public/plugins/<id>/shared/…` | A plugin’s shared routes, one address for every tenant. `server.sharedUrl` gives the full address |
312
312
  | `/w/<token>/<runner>` | A plugin’s page runner for a wizard that switched it on (built; from source `/api/public/wizards/<token>/runners/<runner>`) |
313
313
 
314
- The studio’s addresses of a plugin: `/studio<path>` for its pages, `/studio/settings/<id>` for a
315
- settings section, `/studio/space/<group>#<id>` for a section of the space page and
314
+ The studio’s addresses of a plugin: `/studio<path>` for its pages, `/studio/settings/plugins?show=<id>`
315
+ for a settings section, `/studio/space/<group>#<id>` for a section of the space page and
316
316
  `/studio/space/<id>` for a part of it.
317
317
 
318
318
  ## Settings
@@ -86,7 +86,7 @@ export default defineStudioPlugin((studio) => {
86
86
  |---|---|
87
87
  | `studio.registerPage({ path, component, wide? })` | A page of the studio at `/studio<path>`, inside the studio’s frame. `path` takes `:name` parts. `wide`: the page lays out its own columns and takes the screen’s width |
88
88
  | `studio.registerNav({ to, label, icon })` | An icon in the app bar that opens a page |
89
- | `studio.registerSettingsSection({ id, label, icon, component, menu? })` | A section of the settings at `/studio/settings/<id>`. `menu`: the user menu links to it directly |
89
+ | `studio.registerSettingsSection({ id, label, icon, component, menu? })` | A section of the settings, one level down in Settings › Plugins, at `/studio/settings/plugins?show=<id>` (`/studio/settings/<id>` leads there). `menu`: the user menu links to it directly |
90
90
  | `studio.registerSpaceSection({ id, group?, label, hint?, component })` | A section of a part of the space page at `/studio/space/<group>#<id>`, below the part’s own. See [The space](#the-space) |
91
91
  | `studio.registerSpacePart({ id, label, icon, after?, component })` | A part of the space page of the plugin’s own, at `/studio/space/<id>`. See [A part of the plugin’s own](#a-part-of-the-plugins-own) |
92
92
  | `studio.registerShareSection({ runner, component })` | A runner’s own settings for a wizard (a keyword), under the share dialog’s gear once the runner is on and set up. See [Runners](./runners.md#the-runners-part-of-the-share-dialog) |
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  title: Models and account
3
3
  description: What runs the AI in your wizards – an AI client on your computer, your own API keys, a local model or credits.
4
- version: 0.11.1
4
+ version: 0.11.13
5
5
  ---
6
6
 
7
7
  Settings → Models decides what runs the AI in your wizards. On your computer you set it up
@@ -27,7 +27,7 @@ A capability’s page asks “Who does it?”:
27
27
  | Your subscription | Claude Code, Codex, Antigravity or Cursor Agent on this computer, using your sign-in there (text; Codex also makes images; Antigravity runs steps without their tools) | Your subscription |
28
28
  | Your own API key | OpenAI, Anthropic, Google Gemini, fal.ai, ElevenLabs, Replicate or Vercel AI Gateway | The provider bills your key |
29
29
  | Apple Intelligence | The model on this Mac: short texts and decisions (Classifier, Standard) and voice notes. See [Apple Intelligence](#apple-intelligence) | Nothing |
30
- | Local model | [Ollama](https://ollama.com) on this computer (text) | Nothing |
30
+ | Local model | [Ollama](https://ollama.com) on this computer: text, search and decisions. See [Local models](#local-models) | Nothing |
31
31
  | engenty credits | Your account’s credits. engenty chooses the model, or you choose one it offers (FLUX on fal, ElevenLabs voices, Veo …) | Credits |
32
32
 
33
33
  Below that you choose the provider’s model, and “Try it” makes one real call: a short answer, a
@@ -37,6 +37,26 @@ With an account, credits step in where you have nothing of your own set up – f
37
37
  videos without your own API key. The switch “Credits step in” on the page “engenty credits” turns
38
38
  that off. A capability with nothing of its own set up then doesn’t run.
39
39
 
40
+ ## Local models
41
+
42
+ Text & reasoning, Search and Decisions each have a part “Local model”. It lists:
43
+
44
+ - **On this computer:** the models Ollama holds that can do this job, with their size and what
45
+ else they read (images, sound) or can do (tools). “Use” picks one; the bin removes it from
46
+ Ollama.
47
+ - **To download:** the models engenty suggests for the job, with their size. The first one that
48
+ fits your computer’s memory is marked “suggested”. “Download and use” loads it into Ollama
49
+ and picks it.
50
+ - **Another model from Ollama:** any name from [Ollama’s library](https://ollama.com/library),
51
+ such as `llama3.2:3b`. It is downloaded if Ollama doesn’t have it yet.
52
+
53
+ On a Mac with Apple silicon, Ollama 0.40 and later runs the models that come as MLX on Apple’s
54
+ MLX. Where Ollama isn’t running, the part offers to start it, and on a Mac to install it.
55
+
56
+ “Ollama” under “Access” shows Ollama on its own: whether it runs, every model it holds with the
57
+ jobs it serves, the models to download for all jobs, and Ollama’s address. A model a job uses
58
+ can’t be removed there; pick another one on that job’s page first.
59
+
40
60
  ## Apple Intelligence
41
61
 
42
62
  On a Mac with Apple silicon and macOS 26 or later, the [setup](./first-start.md) and “Text &
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  title: Run it on a server
3
3
  description: One Docker container behind an https proxy, so others can open your wizards' links.
4
- version: 0.9.3
4
+ version: 0.11.13
5
5
  ---
6
6
 
7
7
  engenty wizards on your computer answers on `localhost` only. On a server with a public address,
@@ -33,11 +33,24 @@ The logs show a link (“Open the studio: …”). Open it once: it signs your b
33
33
  |---|---|
34
34
  | Port | Point the proxy at port 8891 |
35
35
  | Data | Databases and files are on the `/data` volume. Back that volume up |
36
- | Models | Your own API keys or Ollama. The subscriptions of AI clients live on your own computer, not on a server |
36
+ | Models | Your own API keys or Ollama (see below). The subscriptions of AI clients live on your own computer, not on a server |
37
37
  | PDF, PNG, browser steps, MP4 | Chromium and ffmpeg are in the image |
38
38
  | Code & shell | The sandbox is off in this image, so steps can’t use that tool. `compose.yaml` shows how to switch it on: with Docker, or with agentOS built into the image |
39
39
  | Plugins | Put them in a folder that you mount into the container and name in `PLUGINS_DIR` |
40
40
 
41
+ ## Models on the server
42
+
43
+ `compose.yaml` holds an Ollama service that is off by default. It comes without models. To use it,
44
+ add a line to `.env`:
45
+
46
+ ```bash
47
+ COMPOSE_PROFILES=ollama
48
+ ```
49
+
50
+ Then run `docker compose up -d --build` again. In the studio, Settings › Models › Search offers to
51
+ download EmbeddingGemma 2 (about 1.3 GB). The model goes to the `ollama-models` volume, so it stays
52
+ when you update. Only engenty wizards reaches this Ollama; it has no port of its own.
53
+
41
54
  ## Update
42
55
 
43
56
  ```bash
@@ -121,3 +121,12 @@ export function writeFeature(name, on, file = featuresFile()) {
121
121
  fileCache = null;
122
122
  return fileFeatures(file);
123
123
  }
124
+ /**
125
+ * Turns a switch on in the `.features` file where it is off now: installing a plugin does that
126
+ * for `plugins`. A runtime of many tenants leaves its switches to the Manage-App.
127
+ */
128
+ export function switchOn(name) {
129
+ if (!managed && !feature(name)) {
130
+ writeFeature(name, true);
131
+ }
132
+ }
@@ -102,6 +102,13 @@ async function purge() {
102
102
  }
103
103
  }
104
104
  void purge().catch((err) => console.error("[retention]", err));
105
+ // Assets came into the index later than documents: what is not there yet goes in once.
106
+ if (env.serves.studio) {
107
+ const { indexMissingAssets } = await import("./services/project-index.js");
108
+ for (const id of await knownTenants()) {
109
+ void withTenant(id, () => indexMissingAssets()).catch((err) => console.error(`[project-index ${id}]`, err));
110
+ }
111
+ }
105
112
  setInterval(() => void purge().catch((err) => console.error("[retention]", err)), 3600_000).unref();
106
113
  // A runtime that runs alone sends again what did not arrive in its account's cloud.
107
114
  if (!managed) {
@@ -0,0 +1,192 @@
1
+ import { totalmem } from "node:os";
2
+ import { ollamaRoot } from "./decision-model.js";
3
+ import { env } from "./env.js";
4
+ import { forgetLocalModels, localOllamaUrl } from "./models.js";
5
+ /**
6
+ * The models in Ollama on this computer, for Settings › Models: what is there and what each can
7
+ * do (Ollama's own `capabilities`), the models the Manage-App offers per job (`GET
8
+ * /v1/local-models` of the account's Manage-App), and the downloads and removals the studio
9
+ * starts. Text, search and decisions each pick their model from it.
10
+ */
11
+ /** What a model does in the studio: a text class, the search of a space's knowledge, decisions. */
12
+ export const LOCAL_JOBS = ["text", "search", "decision"];
13
+ /** The share of the computer's memory a model may take and still leave room for the rest. */
14
+ const MEMORY_SHARE = 0.7;
15
+ /** The job a capability of Ollama's serves. */
16
+ const JOB_OF = {
17
+ completion: "text",
18
+ embedding: "search",
19
+ decision: "decision",
20
+ };
21
+ const gb = (bytes) => Math.round((bytes / 1e9) * 10) / 10;
22
+ async function getJson(url, init, timeoutMs = 2000) {
23
+ try {
24
+ const res = await fetch(url, { ...init, signal: AbortSignal.timeout(timeoutMs) });
25
+ return res.ok ? (await res.json()) : null;
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
31
+ /** What `/api/show` said about a model, by its digest: it does not change while the digest stays. */
32
+ const shown = new Map();
33
+ async function installedModels(root) {
34
+ const [tags, ps] = await Promise.all([
35
+ getJson(`${root}/api/tags`),
36
+ getJson(`${root}/api/ps`),
37
+ ]);
38
+ const loaded = new Set((ps?.models ?? []).map((m) => m.name));
39
+ return Promise.all((tags?.models ?? []).map(async (m) => {
40
+ let show = shown.get(m.digest);
41
+ if (!show) {
42
+ show =
43
+ (await getJson(`${root}/api/show`, {
44
+ method: "POST",
45
+ headers: { "content-type": "application/json" },
46
+ body: JSON.stringify({ model: m.name }),
47
+ }, 5000)) ?? {};
48
+ shown.set(m.digest, show);
49
+ }
50
+ const capabilities = show.capabilities ?? [];
51
+ return {
52
+ name: m.name,
53
+ size: gb(m.size),
54
+ jobs: LOCAL_JOBS.filter((job) => capabilities.some((c) => JOB_OF[c] === job)),
55
+ capabilities,
56
+ runner: show.details?.runner || null,
57
+ quantization: show.details?.quantization_level || null,
58
+ loaded: loaded.has(m.name),
59
+ };
60
+ }));
61
+ }
62
+ /** The Manage-App's list at the last look: kept for an hour, asked again after five minutes when it did not answer. */
63
+ let offered = null;
64
+ async function offeredModels() {
65
+ if (offered && Date.now() < offered.at) {
66
+ return offered.models;
67
+ }
68
+ const body = await getJson(`${env.local.accountUrl}/v1/local-models`, undefined, 3000);
69
+ const models = body?.models?.filter((m) => LOCAL_JOBS.includes(m.job)) ?? null;
70
+ offered = { models, at: Date.now() + (models ? 60 : 5) * 60_000 };
71
+ return models;
72
+ }
73
+ const downloads = new Map();
74
+ /** While Ollama is being installed or started from the studio, and how that ended. */
75
+ let ollamaStart = null;
76
+ /** Ollama on this computer, its models and the ones to download, for the settings page. */
77
+ export async function localModels() {
78
+ const url = await localOllamaUrl();
79
+ const root = ollamaRoot(url);
80
+ const { findOllama, freeGb } = await import("./cli/ollama.js");
81
+ const version = (await getJson(`${root}/api/version`))?.version ?? null;
82
+ const memory = Math.round(totalmem() / 2 ** 30);
83
+ const installed = version ? await installedModels(root) : [];
84
+ const held = new Set(installed.map((m) => m.name));
85
+ const list = await offeredModels();
86
+ return {
87
+ ollama: {
88
+ url,
89
+ /** Null: Ollama does not answer at the address. */
90
+ version,
91
+ /** The `ollama` command is on this computer, though it may not run. */
92
+ installed: Boolean(version || findOllama()),
93
+ /** The studio can install Ollama itself: on a Mac, as the setup does. */
94
+ canInstall: process.platform === "darwin",
95
+ start: ollamaStart,
96
+ },
97
+ /** The computer's memory and the free space for models, in GB. */
98
+ memory,
99
+ free: await freeGb(),
100
+ installed,
101
+ /** Null: the Manage-App did not answer, the list shows the installed models alone. */
102
+ offered: list?.map((m) => ({
103
+ ...m,
104
+ installed: held.has(m.name) || held.has(`${m.name}:latest`),
105
+ fits: m.memory <= memory * MEMORY_SHARE,
106
+ })) ?? null,
107
+ downloads: [...downloads.values()],
108
+ };
109
+ }
110
+ /**
111
+ * Pulls a model into Ollama in the background; `then` runs once it ended well. A download of
112
+ * the same model that runs already is left to finish.
113
+ */
114
+ export async function downloadLocalModel(model, then) {
115
+ const running = downloads.get(model);
116
+ if (running && !running.finished) {
117
+ return;
118
+ }
119
+ const state = { model, done: 0, total: 0, error: null, finished: false };
120
+ downloads.set(model, state);
121
+ const { pullModel } = await import("./cli/ollama.js");
122
+ void pullModel(model, (done, total) => {
123
+ state.done = done;
124
+ state.total = total;
125
+ }, { OLLAMA_URL: await localOllamaUrl() })
126
+ .then(async () => {
127
+ forgetLocalModels();
128
+ state.finished = true;
129
+ // A finished download is done; the list shows the model itself now.
130
+ downloads.delete(model);
131
+ await then?.();
132
+ })
133
+ .catch((err) => {
134
+ const unreachable = /fetch failed|ECONNREFUSED/i.test(`${err.message} ${err.cause ?? ""}`);
135
+ state.error = unreachable ? "ollama" : err.message;
136
+ state.finished = true;
137
+ });
138
+ }
139
+ /** Removes a model from Ollama, and a download of it that failed; Ollama's reason where it refuses. */
140
+ export async function removeLocalModel(model) {
141
+ downloads.delete(model);
142
+ forgetLocalModels();
143
+ const res = await fetch(`${ollamaRoot(await localOllamaUrl())}/api/delete`, {
144
+ method: "DELETE",
145
+ headers: { "content-type": "application/json" },
146
+ body: JSON.stringify({ model }),
147
+ signal: AbortSignal.timeout(10_000),
148
+ });
149
+ if (!res.ok) {
150
+ const text = (await res.text()).trim();
151
+ let reason = text;
152
+ try {
153
+ reason = JSON.parse(text).error ?? text;
154
+ }
155
+ catch { }
156
+ throw new Error(reason || `Ollama answered ${res.status}.`);
157
+ }
158
+ }
159
+ /**
160
+ * Installs Ollama where it is missing (on a Mac) and starts it, as `wizards setup` does, in the
161
+ * background; the settings page follows `ollama.start`.
162
+ */
163
+ export async function startLocalOllama() {
164
+ if (ollamaStart?.running) {
165
+ return;
166
+ }
167
+ const state = { running: true, error: null };
168
+ ollamaStart = state;
169
+ const { findOllama, installOllama, startOllama } = await import("./cli/ollama.js");
170
+ const { layout } = await import("./cli/home.js");
171
+ const paths = layout();
172
+ void (async () => {
173
+ let path = findOllama();
174
+ if (!path) {
175
+ const outcome = await installOllama(paths, () => undefined);
176
+ if (!outcome.ok) {
177
+ throw new Error(outcome.message);
178
+ }
179
+ path = findOllama();
180
+ }
181
+ if (!(await startOllama(path, paths))) {
182
+ throw new Error("Ollama isn’t answering.");
183
+ }
184
+ forgetLocalModels();
185
+ })()
186
+ .catch((err) => {
187
+ state.error = err.message;
188
+ })
189
+ .finally(() => {
190
+ state.running = false;
191
+ });
192
+ }