@drakonkat/ai-helper 1.2.17 → 1.2.19
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.
- package/ARCHITECTURE.md +21 -4
- package/CHANGELOG.md +28 -0
- package/README.md +131 -40
- package/package.json +5 -4
- package/src/config.js +12 -12
- package/src/health.js +81 -0
- package/src/hindsight.js +285 -0
- package/src/index.js +34 -14
- package/src/manager.js +350 -9
- package/src/proxy.js +10 -0
- package/src/startup-services.js +5 -2
- package/src/tui-proxy-setup.js +81 -9
- package/src/tui.js +155 -93
- package/src/utils/metrics.js +3 -3
- package/src/utils/process.js +7 -65
- package/src/utils/versions.js +16 -3
package/src/tui-proxy-setup.js
CHANGED
|
@@ -1,18 +1,49 @@
|
|
|
1
1
|
import { PROXY_FIELDS, proxySetupDraft, normalizeSetupDraft } from "./proxy-setup.js";
|
|
2
2
|
import { STARTUP_SERVICE_IDS, normalizeStartServices } from "./startup-services.js";
|
|
3
|
+
import {
|
|
4
|
+
HINDSIGHT_DEFAULTS,
|
|
5
|
+
HINDSIGHT_KEY_ENV,
|
|
6
|
+
normalizeHindsightConfig,
|
|
7
|
+
readHindsightApiKey,
|
|
8
|
+
validateHindsightApiKey,
|
|
9
|
+
} from "./hindsight.js";
|
|
3
10
|
|
|
4
11
|
const STARTUP_FIELD = { key: "startServices", group: "Startup", label: "Services started by aih start / up",
|
|
5
12
|
choices: STARTUP_SERVICE_IDS, multiple: true,
|
|
6
|
-
help: "Arrows move; Space toggles. Choose at least one service. Explicit CLI targets override this list. RTK is a filter; bundled pxpipe does not need its standalone service. Changes take effect on the next aih start/up." };
|
|
13
|
+
help: "Arrows move; Space toggles. Choose at least one service. Explicit CLI targets override this list. RTK is a filter; bundled pxpipe does not need its standalone service. Hindsight runs in Docker; selecting it adds its settings to this setup. Changes take effect on the next aih start/up." };
|
|
14
|
+
|
|
15
|
+
/** Shown only when Hindsight is selected for start/up. "config" maps to the saved settings. */
|
|
16
|
+
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)." },
|
|
18
|
+
{ 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
|
+
{ key: "hindsightApiPort", config: "apiPort", group: "Hindsight", label: "API port", help: "Loopback port for the Hindsight API and its /health check. Default 8888." },
|
|
20
|
+
{ 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." },
|
|
21
|
+
{ key: "hindsightVolume", config: "volume", group: "Hindsight", label: "Data volume", help: "Docker volume holding the memory database (/home/hindsight/.pg0). Kept when the container is recreated or updated." },
|
|
22
|
+
{ key: "hindsightLlmProvider", config: "llmProvider", group: "Hindsight LLM", label: "LLM provider", help: "HINDSIGHT_API_LLM_PROVIDER, e.g. openai, anthropic, gemini, groq, ollama, lmstudio. OpenAI-compatible gateways use openai plus a base URL." },
|
|
23
|
+
{ key: "hindsightLlmModel", config: "llmModel", group: "Hindsight LLM", label: "LLM model", help: "HINDSIGHT_API_LLM_MODEL. Empty = Hindsight's default for the provider." },
|
|
24
|
+
{ key: "hindsightLlmBaseUrl", config: "llmBaseUrl", group: "Hindsight LLM", label: "LLM base URL", help: "Optional OpenAI-compatible endpoint. Inside the container use host.docker.internal instead of localhost, e.g. http://host.docker.internal:10100/v1. Empty = provider default." },
|
|
25
|
+
{ key: "hindsightApiKey", secret: true, group: "Hindsight LLM", label: "LLM API key", help: `Saved to ~/.aih/hindsight.env and passed with docker --env-file; never stored in state.json or on command lines. Empty = keep the saved key (or ${HINDSIGHT_KEY_ENV} from the environment).` },
|
|
26
|
+
{ 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
|
+
]);
|
|
7
28
|
|
|
8
29
|
const clean = value => String(value ?? "").replace(/[\x00-\x1f\x7f-\x9f]/g, " ");
|
|
30
|
+
const isHindsightField = field => field.key.startsWith("hindsight");
|
|
31
|
+
|
|
32
|
+
function savedHindsight(state) {
|
|
33
|
+
try { return normalizeHindsightConfig(state.config?.hindsight); }
|
|
34
|
+
catch { return normalizeHindsightConfig(HINDSIGHT_DEFAULTS); }
|
|
35
|
+
}
|
|
9
36
|
|
|
10
37
|
/** Pure wizard state; all process and disk actions belong to Dashboard/manager. */
|
|
11
38
|
export class ProxySetupWizard {
|
|
12
|
-
constructor(state) {
|
|
39
|
+
constructor(state, { keyStatus = readHindsightApiKey().source } = {}) {
|
|
13
40
|
this.draft = proxySetupDraft(state);
|
|
14
41
|
this.currentStartServices = normalizeStartServices(state.config?.startServices);
|
|
15
42
|
this.draft.startServices = this.currentStartServices.join(",");
|
|
43
|
+
this.hindsightSaved = state.config?.hindsight ? savedHindsight(state) : null;
|
|
44
|
+
const hindsight = savedHindsight(state);
|
|
45
|
+
for (const f of HINDSIGHT_FIELDS) this.draft[f.key] = f.secret ? "" : String(hindsight[f.config] ?? "");
|
|
46
|
+
this.keyStatus = keyStatus;
|
|
16
47
|
this.serviceIndex = 0;
|
|
17
48
|
this.initial = { ...this.draft };
|
|
18
49
|
this.active = state.services?.proxy?.proxyOptions;
|
|
@@ -28,13 +59,34 @@ export class ProxySetupWizard {
|
|
|
28
59
|
this.pasting = false;
|
|
29
60
|
}
|
|
30
61
|
|
|
31
|
-
get
|
|
62
|
+
get hindsightSelected() { return this.draft.startServices.split(",").includes("hindsight"); }
|
|
63
|
+
get fields() {
|
|
64
|
+
return [...PROXY_FIELDS.filter(f => !f.stage || this.draft.preset.split("-").includes(f.stage)), STARTUP_FIELD,
|
|
65
|
+
...(this.hindsightSelected ? HINDSIGHT_FIELDS : [])];
|
|
66
|
+
}
|
|
32
67
|
get field() { return this.fields[this.step]; }
|
|
33
68
|
get summary() { return this.step >= this.fields.length; }
|
|
34
69
|
get dirty() { return JSON.stringify(this.initial) !== JSON.stringify(this.draft); }
|
|
35
70
|
|
|
71
|
+
hindsightDraft() {
|
|
72
|
+
return Object.fromEntries(HINDSIGHT_FIELDS.filter(f => !f.secret).map(f => [f.config, this.draft[f.key]]));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** null when Hindsight is not started by start/up: its settings are then left untouched. */
|
|
76
|
+
hindsightSettings() {
|
|
77
|
+
if (!this.hindsightSelected) return null;
|
|
78
|
+
const apiKey = this.draft.hindsightApiKey.trim();
|
|
79
|
+
if (apiKey) validateHindsightApiKey(apiKey);
|
|
80
|
+
return { config: normalizeHindsightConfig(this.hindsightDraft()), apiKey };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
keyDescription() {
|
|
84
|
+
return this.keyStatus === "file" ? "saved" : this.keyStatus === "env" ? `from ${HINDSIGHT_KEY_ENV}` : "not set";
|
|
85
|
+
}
|
|
86
|
+
|
|
36
87
|
validate() {
|
|
37
88
|
normalizeStartServices(this.draft.startServices.split(","));
|
|
89
|
+
this.hindsightSettings();
|
|
38
90
|
return normalizeSetupDraft(this.draft);
|
|
39
91
|
}
|
|
40
92
|
|
|
@@ -42,6 +94,8 @@ export class ProxySetupWizard {
|
|
|
42
94
|
const field = this.field;
|
|
43
95
|
const value = this.draft[field.key];
|
|
44
96
|
if (field.multiple) { normalizeStartServices(value.split(",")); return; }
|
|
97
|
+
if (field.secret) { validateHindsightApiKey(value.trim()); return; }
|
|
98
|
+
if (isHindsightField(field)) { normalizeHindsightConfig(this.hindsightDraft()); return; }
|
|
45
99
|
// Validate only this field, against known valid defaults, without discarding other edits.
|
|
46
100
|
const base = proxySetupDraft({ services: {} });
|
|
47
101
|
base.upstream = "https://example.com";
|
|
@@ -147,7 +201,7 @@ export class ProxySetupWizard {
|
|
|
147
201
|
if (line || !lines.length) lines.push(line);
|
|
148
202
|
return lines;
|
|
149
203
|
};
|
|
150
|
-
const title = `
|
|
204
|
+
const title = `AIH SETUP | ${this.summary ? "Summary" : `${this.field.group} (${this.step + 1}/${this.fields.length})`}`;
|
|
151
205
|
let body = [];
|
|
152
206
|
if (this.summary) {
|
|
153
207
|
body = [...PROXY_FIELDS, STARTUP_FIELD].flatMap(f => {
|
|
@@ -155,6 +209,15 @@ export class ProxySetupWizard {
|
|
|
155
209
|
const after = this.draft[f.key];
|
|
156
210
|
return wrap(`${before !== after ? "* " : " "}${f.label}: ${after || (f.key === "models" ? "pxpipe defaults; RTK/Headroom on all" : "(none)")}${before !== after ? ` (previous: ${before || "empty"})` : ""}`);
|
|
157
211
|
});
|
|
212
|
+
if (this.hindsightSelected) {
|
|
213
|
+
body.push(...HINDSIGHT_FIELDS.flatMap(f => {
|
|
214
|
+
if (f.secret) return wrap(this.draft[f.key].trim() ? `* ${f.label}: new key (hidden)` : ` ${f.label}: unchanged (${this.keyDescription()})`);
|
|
215
|
+
const before = this.initial[f.key];
|
|
216
|
+
const after = this.draft[f.key];
|
|
217
|
+
return wrap(`${before !== after ? "* " : " "}${f.label}: ${after || "(none)"}${before !== after ? ` (previous: ${before || "empty"})` : ""}`);
|
|
218
|
+
}));
|
|
219
|
+
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)."));
|
|
220
|
+
}
|
|
158
221
|
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."));
|
|
159
222
|
body.push(...this.checks.flatMap(wrap));
|
|
160
223
|
if (this.error) body.push(...wrap(`Error: ${this.error}`));
|
|
@@ -166,12 +229,21 @@ export class ProxySetupWizard {
|
|
|
166
229
|
: `${v === this.draft[f.key] ? ">" : " "} ${v.replaceAll("-", " -> ")}`));
|
|
167
230
|
else {
|
|
168
231
|
const value = this.draft[f.key];
|
|
232
|
+
const shown = f.secret ? "*".repeat(value.length) : value;
|
|
169
233
|
const start = Math.max(0, this.cursor - width + 6);
|
|
170
|
-
body.push(...wrap("> " +
|
|
234
|
+
body.push(...wrap("> " + shown.slice(start, this.cursor) + "|" + shown.slice(this.cursor, start + width - 4)));
|
|
235
|
+
}
|
|
236
|
+
if (f.secret) {
|
|
237
|
+
body.push(...wrap("Initial value: (empty: keep the current key)"));
|
|
238
|
+
body.push(...wrap(`Current setting: key ${this.keyDescription()}`));
|
|
239
|
+
} else if (isHindsightField(f)) {
|
|
240
|
+
body.push(...wrap(`Initial value: ${this.initial[f.key] || "(empty)"}`));
|
|
241
|
+
body.push(...wrap(`Current setting: ${this.hindsightSaved ? String(this.hindsightSaved[f.config] ?? "") || "(empty)" : "(not configured: defaults)"}`));
|
|
242
|
+
} else {
|
|
243
|
+
body.push(...wrap(`Initial value: ${this.initial[f.key] || "(empty)"}`));
|
|
244
|
+
const current = f.multiple ? this.currentStartServices.join(",") : this.active ? String(this.active[f.key] ?? "") || "(empty)" : "(not configured)";
|
|
245
|
+
body.push(...wrap(`Current setting: ${current}${!f.multiple && !this.running && this.active ? " (proxy stopped)" : ""}`));
|
|
171
246
|
}
|
|
172
|
-
body.push(...wrap(`Initial value: ${this.initial[f.key] || "(empty)"}`));
|
|
173
|
-
const current = f.multiple ? this.currentStartServices.join(",") : this.active ? String(this.active[f.key] ?? "") || "(empty)" : "(not configured)";
|
|
174
|
-
body.push(...wrap(`Current setting: ${current}${!f.multiple && !this.running && this.active ? " (proxy stopped)" : ""}`));
|
|
175
247
|
}
|
|
176
248
|
const actions = ["Check", "Save only", "Save and apply now", "Edit", "Cancel"];
|
|
177
249
|
const controls = this.summary
|
|
@@ -179,7 +251,7 @@ export class ProxySetupWizard {
|
|
|
179
251
|
: wrap("Enter/Tab next | Shift+Tab back | Esc cancel").concat(wrap(this.field.multiple ? "Arrows move | Space toggle" : this.field.choices ? "Arrows/Space change" : "Ctrl+U clear | Home/End move cursor"));
|
|
180
252
|
const hint = this.summary && this.action === 2
|
|
181
253
|
? this.running ? "RESTART: interrupts requests/streams." : "Apply starts the proxy."
|
|
182
|
-
: this.summary ? "Arrows select | Enter confirm | Esc cancel" : "Local draft;
|
|
254
|
+
: this.summary ? "Arrows select | Enter confirm | Esc cancel" : "Local draft; services unchanged.";
|
|
183
255
|
const status = this.cancelPending ? "Discard draft? Enter/Esc confirms; any other key continues" : busy ? "Operation in progress..." : this.error || hint;
|
|
184
256
|
const statusLines = wrap(status);
|
|
185
257
|
const tail = [...controls.flatMap(wrap), ...statusLines.slice(0, 2)];
|