@drakonkat/ai-helper 1.2.20 → 1.2.21

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.
@@ -3,7 +3,9 @@ import { STARTUP_SERVICE_IDS, normalizeStartServices } from "./startup-services.
3
3
  import {
4
4
  HINDSIGHT_DEFAULTS,
5
5
  HINDSIGHT_KEY_ENV,
6
+ LOCAL_LLM_OPERATIONS,
6
7
  normalizeHindsightConfig,
8
+ normalizeLocalLlmConfig,
7
9
  readHindsightApiKey,
8
10
  validateHindsightApiKey,
9
11
  } from "./hindsight.js";
@@ -14,7 +16,7 @@ const STARTUP_FIELD = { key: "startServices", group: "Startup", label: "Services
14
16
 
15
17
  /** Shown only when Hindsight is selected for start/up. "config" maps to the saved settings. */
16
18
  export const HINDSIGHT_FIELDS = Object.freeze([
17
- { key: "hindsightContainer", config: "containerName", group: "Hindsight", label: "Hindsight container name", help: "Docker container started/stopped by aih. An existing container with this name is reused as is; the settings below apply only to containers aih creates or adopts (u on hindsight in the dashboard imports an existing container's settings)." },
19
+ { key: "hindsightContainer", config: "containerName", group: "Hindsight", label: "Hindsight container name", help: "Docker container started/stopped by aih. An existing container with this name is reused as is; the settings below apply only to containers aih creates or adopts (u twice on hindsight in the dashboard imports an existing container or docker compose stack, including its local LLM)." },
18
20
  { key: "hindsightImage", config: "image", group: "Hindsight", label: "Docker image", help: "Default ghcr.io/vectorize-io/hindsight:latest. Pin a version (e.g. :1.2.0) for repeatable installs: update (u) moves it to the latest release." },
19
21
  { key: "hindsightApiPort", config: "apiPort", group: "Hindsight", label: "API port", help: "Loopback port for the Hindsight API and its /health check. Default 8888." },
20
22
  { key: "hindsightUiPort", config: "uiPort", group: "Hindsight", label: "Web UI port", help: "Loopback port of the Hindsight control plane, opened with o in the dashboard. Default 9999." },
@@ -26,6 +28,31 @@ export const HINDSIGHT_FIELDS = Object.freeze([
26
28
  { key: "hindsightEnvFile", config: "envFile", group: "Hindsight advanced", label: "Extra environment file", help: "Optional docker --env-file with advanced HINDSIGHT_* settings (timeouts, failover models...). Empty = none." },
27
29
  ]);
28
30
 
31
+ /** The local LLM step; "llm" maps to config.localLlm. The other fields appear only when it is on. */
32
+ const LOCAL = "Hindsight local LLM";
33
+ export const LOCAL_LLM_TOGGLE = Object.freeze({ key: "hindsightLocalEnabled", llm: "enabled", group: LOCAL, label: "Local LLM for Hindsight", choices: ["off", "on"],
34
+ help: "on = aih also runs a llama.cpp server container (hindsight-llm), starts it before Hindsight and stops it after, and sends the operations you choose to it. Other operations keep the LLM above." });
35
+ export const LOCAL_LLM_FIELDS = Object.freeze([
36
+ { key: "hindsightLocalImage", llm: "image", group: LOCAL, label: "llama.cpp server image", help: "Pin a build, e.g. ghcr.io/ggml-org/llama.cpp:server-cuda-b12282. u re-pulls this tag but never moves it: change the build here, by hand." },
37
+ { key: "hindsightLocalModelsDir", llm: "modelsDir", group: LOCAL, label: "Models folder", help: "Absolute host folder with the GGUF files, mounted read-only at /models." },
38
+ { key: "hindsightLocalModel", llm: "model", group: LOCAL, label: "Model file", help: "GGUF file inside the models folder, e.g. LFM3-1.7B-Q8_0.gguf." },
39
+ { key: "hindsightLocalLora", llm: "lora", group: LOCAL, label: "LoRA adapter", help: "Optional GGUF adapter inside the models folder. Empty = none." },
40
+ { key: "hindsightLocalAlias", llm: "alias", group: LOCAL, label: "Model alias", help: "Name the server answers to and Hindsight asks for (--alias)." },
41
+ { key: "hindsightLocalPort", llm: "port", group: LOCAL, label: "Host port", help: "Loopback port of the server, for its health check and your own tests. Hindsight uses the Docker network. Default 8085." },
42
+ { key: "hindsightLocalGpus", llm: "gpus", group: LOCAL, label: "GPUs", help: "docker --gpus value: all, a count, device=0,1, or none for CPU only (then use a CPU image tag)." },
43
+ { key: "hindsightLocalCtxSize", llm: "ctxSize", group: LOCAL, label: "Context size", help: "Total --ctx-size, shared by the parallel slots: each slot gets context / slots tokens." },
44
+ { key: "hindsightLocalParallel", llm: "parallel", group: LOCAL, label: "Parallel slots", help: "--parallel: requests served at the same time." },
45
+ { key: "hindsightLocalExtraArgs", llm: "extraArgs", group: LOCAL, label: "Extra llama-server arguments", help: "Space-separated, e.g. --jinja --n-gpu-layers 999 --flash-attn on --no-kv-unified --metrics. Model, adapter, alias, host, port, context and slots come from the fields above." },
46
+ { key: "hindsightLocalOperations", llm: "operations", group: LOCAL, label: "Operations sent to the local LLM", choices: LOCAL_LLM_OPERATIONS, multiple: true,
47
+ help: "Arrows move; Space toggles. Mental model refresh follows reflect unless chosen separately. Unchosen operations use the Hindsight LLM." },
48
+ { key: "hindsightLocalMaxConcurrent", llm: "maxConcurrent", group: LOCAL, label: "Max concurrent requests", help: "Per operation; at most the parallel slots." },
49
+ { key: "hindsightLocalTimeout", llm: "timeout", group: LOCAL, label: "Timeout (seconds)", help: "Per request to the local LLM." },
50
+ { key: "hindsightLocalMaxContext", llm: "maxContextTokens", group: LOCAL, label: "Reflect max context tokens", help: "Reflect prompt budget. Context + completion must fit one slot." },
51
+ { key: "hindsightLocalMaxCompletion", llm: "maxCompletionTokens", group: LOCAL, label: "Reflect max completion tokens", help: "Reflect answer budget." },
52
+ ]);
53
+ const ALL_LOCAL_FIELDS = [LOCAL_LLM_TOGGLE, ...LOCAL_LLM_FIELDS];
54
+ const localValue = (field, llm) => field.llm === "enabled" ? (llm.enabled ? "on" : "off") : Array.isArray(llm[field.llm]) ? llm[field.llm].join(",") : String(llm[field.llm] ?? "");
55
+
29
56
  const clean = value => String(value ?? "").replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
30
57
  const isHindsightField = field => field.key.startsWith("hindsight");
31
58
 
@@ -43,8 +70,9 @@ export class ProxySetupWizard {
43
70
  this.hindsightSaved = state.config?.hindsight ? savedHindsight(state) : null;
44
71
  const hindsight = savedHindsight(state);
45
72
  for (const f of HINDSIGHT_FIELDS) this.draft[f.key] = f.secret ? "" : String(hindsight[f.config] ?? "");
73
+ for (const f of ALL_LOCAL_FIELDS) this.draft[f.key] = localValue(f, hindsight.localLlm);
46
74
  this.keyStatus = keyStatus;
47
- this.serviceIndex = 0;
75
+ this.choiceIndex = {};
48
76
  this.initial = { ...this.draft };
49
77
  this.active = state.services?.proxy?.proxyOptions;
50
78
  this.running = state.services?.proxy?.status === "running";
@@ -60,16 +88,21 @@ export class ProxySetupWizard {
60
88
  }
61
89
 
62
90
  get hindsightSelected() { return this.draft.startServices.split(",").includes("hindsight"); }
91
+ get localLlmSelected() { return this.hindsightSelected && this.draft.hindsightLocalEnabled === "on"; }
63
92
  get fields() {
64
93
  return [...PROXY_FIELDS.filter(f => !f.stage || this.draft.preset.split("-").includes(f.stage)), STARTUP_FIELD,
65
- ...(this.hindsightSelected ? HINDSIGHT_FIELDS : [])];
94
+ ...(this.hindsightSelected ? [...HINDSIGHT_FIELDS, LOCAL_LLM_TOGGLE] : []), ...(this.localLlmSelected ? LOCAL_LLM_FIELDS : [])];
66
95
  }
67
96
  get field() { return this.fields[this.step]; }
68
97
  get summary() { return this.step >= this.fields.length; }
69
98
  get dirty() { return JSON.stringify(this.initial) !== JSON.stringify(this.draft); }
70
99
 
71
100
  hindsightDraft() {
72
- return Object.fromEntries(HINDSIGHT_FIELDS.filter(f => !f.secret).map(f => [f.config, this.draft[f.key]]));
101
+ return { ...Object.fromEntries(HINDSIGHT_FIELDS.filter(f => !f.secret).map(f => [f.config, this.draft[f.key]])), localLlm: this.localLlmDraft() };
102
+ }
103
+
104
+ localLlmDraft(enabled = this.draft.hindsightLocalEnabled === "on") {
105
+ return { ...Object.fromEntries(LOCAL_LLM_FIELDS.map(f => [f.llm, this.draft[f.key]])), enabled };
73
106
  }
74
107
 
75
108
  /** null when Hindsight is not started by start/up: its settings are then left untouched. */
@@ -93,8 +126,10 @@ export class ProxySetupWizard {
93
126
  validateField() {
94
127
  const field = this.field;
95
128
  const value = this.draft[field.key];
96
- if (field.multiple) { normalizeStartServices(value.split(",")); return; }
129
+ if (field === STARTUP_FIELD) { normalizeStartServices(value.split(",")); return; }
97
130
  if (field.secret) { validateHindsightApiKey(value.trim()); return; }
131
+ // Field by field only the format is checked; the summary checks the whole local LLM setup.
132
+ if (field.llm) { normalizeLocalLlmConfig(this.localLlmDraft(false)); return; }
98
133
  if (isHindsightField(field)) { normalizeHindsightConfig(this.hindsightDraft()); return; }
99
134
  // Validate only this field, against known valid defaults, without discarding other edits.
100
135
  const base = proxySetupDraft({ services: {} });
@@ -150,11 +185,12 @@ export class ProxySetupWizard {
150
185
  }
151
186
  if (f.choices) {
152
187
  if (f.multiple) {
153
- if (key === "\x1b[A") this.serviceIndex = (this.serviceIndex + f.choices.length - 1) % f.choices.length;
154
- if (key === "\x1b[B") this.serviceIndex = (this.serviceIndex + 1) % f.choices.length;
188
+ const index = this.choiceIndex[f.key] || 0;
189
+ if (key === "\x1b[A") this.choiceIndex[f.key] = (index + f.choices.length - 1) % f.choices.length;
190
+ if (key === "\x1b[B") this.choiceIndex[f.key] = (index + 1) % f.choices.length;
155
191
  if (key === " ") {
156
192
  const selected = new Set(value.split(",").filter(Boolean));
157
- const service = f.choices[this.serviceIndex];
193
+ const service = f.choices[index];
158
194
  if (selected.has(service)) selected.delete(service); else selected.add(service);
159
195
  this.draft[f.key] = f.choices.filter(id => selected.has(id)).join(",");
160
196
  }
@@ -210,13 +246,15 @@ export class ProxySetupWizard {
210
246
  return wrap(`${before !== after ? "* " : " "}${f.label}: ${after || (f.key === "models" ? "pxpipe defaults; RTK/Headroom on all" : "(none)")}${before !== after ? ` (previous: ${before || "empty"})` : ""}`);
211
247
  });
212
248
  if (this.hindsightSelected) {
213
- body.push(...HINDSIGHT_FIELDS.flatMap(f => {
249
+ body.push(...[...HINDSIGHT_FIELDS, LOCAL_LLM_TOGGLE, ...(this.localLlmSelected ? LOCAL_LLM_FIELDS : [])].flatMap(f => {
214
250
  if (f.secret) return wrap(this.draft[f.key].trim() ? `* ${f.label}: new key (hidden)` : ` ${f.label}: unchanged (${this.keyDescription()})`);
215
251
  const before = this.initial[f.key];
216
252
  const after = this.draft[f.key];
217
253
  return wrap(`${before !== after ? "* " : " "}${f.label}: ${after || "(none)"}${before !== after ? ` (previous: ${before || "empty"})` : ""}`);
218
254
  }));
219
255
  body.push(...wrap("Hindsight: settings are saved; apply recreates a running container created by aih (data volume kept). Other containers are left unchanged until adopted (u on the dashboard)."));
256
+ if (this.localLlmSelected) body.push(...wrap("Local LLM: started before Hindsight and stopped after it; a new or changed server must answer a test completion before Hindsight starts."));
257
+ else if (this.hindsightSaved?.localLlm?.enabled) body.push(...wrap("Local LLM: turned off; apply stops its container and Hindsight uses its own LLM for every operation."));
220
258
  }
221
259
  body.push(...wrap(this.running ? "WARNING: apply now restarts the proxy and interrupts active requests/streams." : "Apply now starts the proxy. Save only leaves it stopped."));
222
260
  body.push(...this.checks.flatMap(wrap));
@@ -225,7 +263,7 @@ export class ProxySetupWizard {
225
263
  const f = this.field;
226
264
  body.push(...wrap(f.label), ...wrap(f.help), "");
227
265
  if (f.choices) body.push(...f.choices.map((v, i) => f.multiple
228
- ? `${i === this.serviceIndex ? ">" : " "} [${this.draft[f.key].split(",").includes(v) ? "x" : " "}] ${v}`
266
+ ? `${i === (this.choiceIndex[f.key] || 0) ? ">" : " "} [${this.draft[f.key].split(",").includes(v) ? "x" : " "}] ${v}`
229
267
  : `${v === this.draft[f.key] ? ">" : " "} ${v.replaceAll("-", " -> ")}`));
230
268
  else {
231
269
  const value = this.draft[f.key];
@@ -236,6 +274,9 @@ export class ProxySetupWizard {
236
274
  if (f.secret) {
237
275
  body.push(...wrap("Initial value: (empty: keep the current key)"));
238
276
  body.push(...wrap(`Current setting: key ${this.keyDescription()}`));
277
+ } else if (f.llm) {
278
+ body.push(...wrap(`Initial value: ${this.initial[f.key] || "(empty)"}`));
279
+ body.push(...wrap(`Current setting: ${this.hindsightSaved ? localValue(f, this.hindsightSaved.localLlm) || "(empty)" : "(not configured: defaults)"}`));
239
280
  } else if (isHindsightField(f)) {
240
281
  body.push(...wrap(`Initial value: ${this.initial[f.key] || "(empty)"}`));
241
282
  body.push(...wrap(`Current setting: ${this.hindsightSaved ? String(this.hindsightSaved[f.config] ?? "") || "(empty)" : "(not configured: defaults)"}`));
package/src/tui.js CHANGED
@@ -565,7 +565,9 @@ export class Dashboard {
565
565
  const rows = this.statuses.map((s, idx) => {
566
566
  const active = idx === this.selected;
567
567
  const version = this.versions[s.id];
568
- const update = version?.updateAvailable === true
568
+ const update = version?.pinned
569
+ ? dim("pinned")
570
+ : version?.updateAvailable === true
569
571
  ? yellow(bold(`↑ ${version.latestVersion}`))
570
572
  : version?.updateAvailable === false
571
573
  ? green("up to date")
@@ -3,7 +3,7 @@ import { readFile, realpath } from "node:fs/promises";
3
3
  import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
4
4
  import { baseUrl, fetchJson } from "./metrics.js";
5
5
  import { SERVICE_PACKAGES as PACKAGES, SERVICES } from "../config.js";
6
- import { HINDSIGHT_RELEASES_URL } from "../hindsight.js";
6
+ import { HINDSIGHT_RELEASES_URL, LOCAL_LLM_ID } from "../hindsight.js";
7
7
  import { refreshToolPath } from "./tool-path.js";
8
8
 
9
9
  function parseVersion(value) {
@@ -143,6 +143,13 @@ export async function getServiceVersions(statuses, {
143
143
  const readCommands = () => commands ||= Promise.resolve().then(() => processCommands(services)).catch(() => ({}));
144
144
  // Docker images: the running API reports its version; releases come from GitHub.
145
145
  const dockerVersions = docker.map(async status => {
146
+ // The llama.cpp build is pinned and changed by hand: report it, never offer an update.
147
+ if (status.id === LOCAL_LLM_ID) {
148
+ const version = status.status === "running" && baseUrl(status.apiUrl)
149
+ ? await request(`${baseUrl(status.apiUrl)}/props`, 2000).then(data => typeof data?.build_info === "string" ? data.build_info.split("-")[0] : null).catch(() => null)
150
+ : null;
151
+ return [status.id, { version, latestVersion: null, updateAvailable: null, pinned: true }];
152
+ }
146
153
  const [version, latestVersion] = await Promise.all([
147
154
  status.status === "running" && baseUrl(status.apiUrl)
148
155
  ? request(`${baseUrl(status.apiUrl)}/health/live`, 2000).then(data => cleanVersion(data?.version)).catch(() => null)