@drakonkat/ai-helper 1.2.18 → 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 +7 -1
- package/CHANGELOG.md +10 -0
- package/README.md +25 -5
- package/package.json +1 -1
- package/src/hindsight.js +31 -2
- package/src/index.js +9 -4
- package/src/manager.js +107 -21
- package/src/tui-proxy-setup.js +3 -3
- package/src/tui.js +6 -1
package/ARCHITECTURE.md
CHANGED
|
@@ -91,7 +91,13 @@ belong to Docker Desktop (`com.docker.backend`, `wslrelay`). Readiness is the HT
|
|
|
91
91
|
status of Hindsight's `GET /health`; the live dashboard polls it every 5 seconds.
|
|
92
92
|
When Docker is unavailable, inspection is retried at most every 10 seconds so the
|
|
93
93
|
one-second dashboard refresh never blocks on a stopped daemon. Implementation:
|
|
94
|
-
`src/hindsight.js` (validation, docker argv, health probe, preflight).
|
|
94
|
+
`src/hindsight.js` (validation, docker argv, health probe, preflight, adoption settings).
|
|
95
|
+
|
|
96
|
+
Only containers labelled `io.aih.managed=true` are recreated. Any other container is
|
|
97
|
+
reused as is until the user adopts it (`update --adopt`): its image, ports, volume and
|
|
98
|
+
the environment it adds to its image become aih's settings, and aih recreates it.
|
|
99
|
+
Update stops the container, archives its volume to `~/.aih/backups` and only then
|
|
100
|
+
removes it; it never uses `docker rm -f`, which would SIGKILL the embedded database.
|
|
95
101
|
|
|
96
102
|
---
|
|
97
103
|
|
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,16 @@ All notable changes to **`ai-helper` (`aih`)** are documented in this file.
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
+
## [1.2.19] - 2026-11-10
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- **Adopt an existing Hindsight container**: `aih update hindsight --adopt`, or `u` twice in the live dashboard, imports the image, ports, data volume and environment of a container aih did not create (for example docker compose) into aih's settings and recreates it on the same volume. Without that explicit request such containers are still left untouched.
|
|
11
|
+
- `aih update hindsight` archives the data volume to `~/.aih/backups` (two most recent kept) before replacing the container; a failed archive leaves the old container running.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- `aih update hindsight` moves a version tag of the official image (e.g. `:1.1.0`) to the latest GitHub release and saves it in the settings, instead of pulling the same tag again. `:latest` and custom images behave as before.
|
|
15
|
+
- Hindsight updates stop the container before removing it instead of `docker rm -f`.
|
|
16
|
+
|
|
7
17
|
## [1.2.18] - 2026-11-06
|
|
8
18
|
|
|
9
19
|
### Changed
|
package/README.md
CHANGED
|
@@ -112,13 +112,32 @@ not by PID, and never kills processes on its ports (on Windows they belong to Do
|
|
|
112
112
|
- **stop** runs `docker stop`; data stays in the volume. **logs** shows `docker logs`;
|
|
113
113
|
the docker commands run by aih are recorded in `~/.aih/logs/hindsight.log`.
|
|
114
114
|
- **An existing container with the same name is reused as is**, for example one created
|
|
115
|
-
by docker compose. aih starts/stops it but never recreates or updates it
|
|
115
|
+
by docker compose. aih starts/stops it but never recreates or updates it until you
|
|
116
|
+
adopt it.
|
|
117
|
+
- **adopt** (`aih update hindsight --adopt`, or `u` twice in the dashboard: the first
|
|
118
|
+
press explains, the second adopts) makes aih the owner of such a container. aih reads
|
|
119
|
+
its image, published ports, data volume and environment, saves provider, model and
|
|
120
|
+
base URL in the settings, the LLM key in `~/.aih/hindsight.env` and every other
|
|
121
|
+
variable set on top of the image (failover models, timeouts...) in
|
|
122
|
+
`~/.aih/hindsight-extra.env`, then stops it and recreates it on the same volume with
|
|
123
|
+
the same image. Ports are published on loopback only. Adoption is refused, changing
|
|
124
|
+
nothing, unless the data is in a single Docker volume mounted at
|
|
125
|
+
`/home/hindsight/.pg0`. Afterwards stop using the tool that created the container
|
|
126
|
+
(`docker compose up` would conflict on the name).
|
|
116
127
|
- Containers created by aih carry the labels `io.aih.managed=true` and a settings
|
|
117
128
|
fingerprint: after a settings change they are recreated on the next start (or by
|
|
118
129
|
*Save and apply now* if running), always keeping the data volume.
|
|
119
|
-
- **update** (`aih update hindsight` or `u`)
|
|
120
|
-
|
|
121
|
-
|
|
130
|
+
- **update** (`aih update hindsight` or `u`) works on containers created or adopted by
|
|
131
|
+
aih. When the image is the official one with a version tag (`:1.1.0`), it pulls the
|
|
132
|
+
latest GitHub release and saves the new tag in the settings; any other tag
|
|
133
|
+
(`:latest`, custom images) is pulled again and nothing happens if the image did not
|
|
134
|
+
change. Before replacing the container aih stops it and archives the data volume to
|
|
135
|
+
`~/.aih/backups/hindsight-<timestamp>.tgz` (the two most recent are kept), because a
|
|
136
|
+
newer version may migrate the database. If the archive fails, the old container is
|
|
137
|
+
started again and nothing changes. A running container is recreated and checked again
|
|
138
|
+
through `/health`. To go back, restore the archive into the volume with the container
|
|
139
|
+
stopped and set the previous tag in the settings:
|
|
140
|
+
`docker run --rm -v hindsight-data:/d -v ~/.aih/backups:/b alpine sh -c "rm -rf /d/* /d/.[!.]* && tar xzf /b/<file> -C /d"`.
|
|
122
141
|
|
|
123
142
|
Settings are edited in `aih status --ui` → **`c`** after selecting **hindsight** in the
|
|
124
143
|
Startup step: container name, image (default `ghcr.io/vectorize-io/hindsight:latest`),
|
|
@@ -167,7 +186,7 @@ spaces, such as `--interceptor "./my hooks/interceptor.mjs"`.
|
|
|
167
186
|
| `restart [services...] [--json]` | `aih restart ocx hindsight --json` |
|
|
168
187
|
| `restart proxy [upstream] [proxy options] [--json]` | `aih restart proxy --models="gpt-6-astra,anthropic/claude-fable*"` |
|
|
169
188
|
| `update [--json]` | `aih update` (update aih itself from npm) |
|
|
170
|
-
| `update <services...> [--json]` | `aih update ocx hindsight --json`; `aih ocx update` |
|
|
189
|
+
| `update <services...> [--json] [--adopt]` | `aih update ocx hindsight --json`; `aih ocx update`; `aih update hindsight --adopt` |
|
|
171
190
|
| `open [services...] [--repo]` | `aih open hindsight`; `aih open proxy --repo` |
|
|
172
191
|
| `logs <service> [-n <lines>] [-f]` | `aih logs proxy --lines=100 --follow` |
|
|
173
192
|
| `stats proxy [--json]` | `aih stats proxy --json` |
|
|
@@ -372,6 +391,7 @@ built-in proxy with `aih restart proxy` to load the updated code.
|
|
|
372
391
|
aih ocx update # update OpenCodex to the latest available release
|
|
373
392
|
aih update ocx # equivalent command-first form
|
|
374
393
|
aih update ocx hindsight # update selected services
|
|
394
|
+
aih update hindsight --adopt # take over a Hindsight container not created by aih
|
|
375
395
|
aih update pxpipe ocx hindsight headroom # all managed services, not aih itself
|
|
376
396
|
aih update ocx --json # JSON result array, exit code 1 on any failure
|
|
377
397
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drakonkat/ai-helper",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.19",
|
|
4
4
|
"description": "Zero-config background service manager, auto-discovery daemon & dashboard launcher for AI agent ecosystems (pxpipe, ocx, hindsight, headroom)",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"module": "src/index.js",
|
package/src/hindsight.js
CHANGED
|
@@ -6,9 +6,10 @@ import { ensureDir, getAihDir, getLogsDir, getServiceLogPath } from "./utils/pat
|
|
|
6
6
|
import { execCommand } from "./utils/process.js";
|
|
7
7
|
|
|
8
8
|
/** Hindsight runs in Docker; these defaults match the upstream quick start. */
|
|
9
|
+
export const HINDSIGHT_REPOSITORY = "ghcr.io/vectorize-io/hindsight";
|
|
9
10
|
export const HINDSIGHT_DEFAULTS = Object.freeze({
|
|
10
11
|
containerName: "hindsight",
|
|
11
|
-
image: "
|
|
12
|
+
image: HINDSIGHT_REPOSITORY + ":latest",
|
|
12
13
|
apiPort: 8888,
|
|
13
14
|
uiPort: 9999,
|
|
14
15
|
volume: "hindsight-data",
|
|
@@ -154,9 +155,37 @@ export async function inspectContainer(name, run = execCommand, timeout = 5000)
|
|
|
154
155
|
managed: labels["io.aih.managed"] === "true",
|
|
155
156
|
configHash: labels["io.aih.config"] || null,
|
|
156
157
|
composeProject: labels["com.docker.compose.project"] || null,
|
|
158
|
+
// Read only to adopt a container aih did not create.
|
|
159
|
+
env: container?.Config?.Env || [],
|
|
160
|
+
mounts: (container?.Mounts || []).map(m => ({ type: m.Type, name: m.Name, destination: m.Destination })),
|
|
161
|
+
ports: container?.HostConfig?.PortBindings || {},
|
|
157
162
|
};
|
|
158
163
|
}
|
|
159
164
|
|
|
165
|
+
/**
|
|
166
|
+
* Settings that let aih recreate a container it did not create: image, ports, data volume and
|
|
167
|
+
* the variables set on top of its image. Throws when something would be lost.
|
|
168
|
+
*/
|
|
169
|
+
export function adoptedHindsightSettings(containerName, container, imageEnv) {
|
|
170
|
+
const data = container.mounts.find(m => m.destination === DATA_DIR);
|
|
171
|
+
if (data?.type !== "volume" || container.mounts.length !== 1) {
|
|
172
|
+
throw new Error("its data must be in one Docker volume mounted at " + DATA_DIR + ", with no other mounts");
|
|
173
|
+
}
|
|
174
|
+
const inherited = new Set(imageEnv);
|
|
175
|
+
const env = new Map(container.env.filter(line => !inherited.has(line)).map(line => line.split(/=(.*)/s).slice(0, 2)));
|
|
176
|
+
const take = name => { const value = env.get(name); env.delete(name); return value; };
|
|
177
|
+
const hostPort = port => container.ports[port + "/tcp"]?.[0]?.HostPort;
|
|
178
|
+
take("HINDSIGHT_CP_DATAPLANE_API_URL"); // always set by aih
|
|
179
|
+
const key = validateHindsightApiKey(take(HINDSIGHT_KEY_ENV) || "");
|
|
180
|
+
const config = normalizeHindsightConfig({
|
|
181
|
+
containerName, image: container.image, volume: data.name, apiPort: hostPort(API_PORT), uiPort: hostPort(UI_PORT),
|
|
182
|
+
llmProvider: take("HINDSIGHT_API_LLM_PROVIDER"), llmModel: take("HINDSIGHT_API_LLM_MODEL"), llmBaseUrl: take("HINDSIGHT_API_LLM_BASE_URL"),
|
|
183
|
+
});
|
|
184
|
+
const extra = [...env].map(([name, value]) => name + "=" + value);
|
|
185
|
+
if (extra.some(line => /[\r\n]/.test(line))) throw new Error("a multi-line environment value cannot be written to an env file");
|
|
186
|
+
return { config, key, extra };
|
|
187
|
+
}
|
|
188
|
+
|
|
160
189
|
export async function imageId(image, run = execCommand) {
|
|
161
190
|
const result = await run("docker", ["image", "inspect", "--format", "{{.Id}}", image], 10000);
|
|
162
191
|
return result.exitCode === 0 ? result.stdout.trim() || null : null;
|
|
@@ -239,7 +268,7 @@ export async function preflightHindsight(config, { newKey = "", run = execComman
|
|
|
239
268
|
let container = null;
|
|
240
269
|
try { container = await inspectContainer(config.containerName, run); } catch (error) { checks.push("Warning: " + error.message); }
|
|
241
270
|
if (container) {
|
|
242
|
-
const owner = container.managed ? "created by aih" : "not created by aih" + (container.composeProject ? " (compose project '" + container.composeProject + "')" : "") + "; it is reused as is and the settings above do not change it";
|
|
271
|
+
const owner = container.managed ? "created by aih" : "not created by aih" + (container.composeProject ? " (compose project '" + container.composeProject + "')" : "") + "; it is reused as is and the settings above do not change it. To manage it from aih, adopt it: press u on hindsight in the dashboard";
|
|
243
272
|
checks.push("Container '" + config.containerName + "' " + container.status + ", " + owner);
|
|
244
273
|
if (container.managed && container.configHash !== hindsightConfigHash(config, newKey || saved.key)) {
|
|
245
274
|
checks.push("Settings changed: aih recreates the container on its next start (data volume kept)");
|
package/src/index.js
CHANGED
|
@@ -66,12 +66,16 @@ ${bold("HINDSIGHT (DOCKER):")}
|
|
|
66
66
|
Configure container, ports, LLM provider/model/base URL and API key in status --ui (c),
|
|
67
67
|
after selecting hindsight in the Startup step. The key is kept in ~/.aih/hindsight.env;
|
|
68
68
|
HINDSIGHT_API_LLM_API_KEY from the environment is used when no key is saved.
|
|
69
|
-
update
|
|
69
|
+
update moves a version tag of the official image to the latest release (other tags are pulled
|
|
70
|
+
again), archives the data volume in ~/.aih/backups and recreates the container. A container
|
|
71
|
+
not created by aih (e.g. docker compose) is left alone until adopted: update hindsight --adopt
|
|
72
|
+
imports its image, ports, volume and environment and recreates it on the same volume.
|
|
70
73
|
|
|
71
74
|
${bold("OPTIONS:")}
|
|
72
75
|
${gray("-n, --lines <num>")} Number of log lines to show (logs only; default: 50)
|
|
73
76
|
${gray("-f, --follow")} Follow log output in real-time (logs only)
|
|
74
77
|
${gray("--json")} JSON for status, start/up, restart, update and stats
|
|
78
|
+
${gray("--adopt")} Take over a Hindsight container not created by aih (update only)
|
|
75
79
|
${gray("--repo")} Open GitHub repositories instead of dashboards (open only)
|
|
76
80
|
${gray("--gpt")} Start the configured Windows Astra chain (start/up only)
|
|
77
81
|
${gray("-ui, --ui")} Open the interactive dashboard (status only, requires a TTY)
|
|
@@ -345,12 +349,13 @@ export async function main({ updateSelfImpl = updateSelf } = {}) {
|
|
|
345
349
|
case "update": {
|
|
346
350
|
const ids = [...new Set(targets)];
|
|
347
351
|
const invalid = ids.filter(id => !SERVICE_IDS.includes(id));
|
|
348
|
-
const
|
|
352
|
+
const adopt = rawArgs.includes("--adopt");
|
|
353
|
+
const invalidOption = rawArgs.find(arg => arg.startsWith("-") && arg !== "--json" && arg !== "--adopt");
|
|
349
354
|
let results;
|
|
350
355
|
// Validate the entire request before updating anything, including the
|
|
351
356
|
// service-first alias (which accepts exactly one service).
|
|
352
357
|
if (invalidOption) {
|
|
353
|
-
results = [{ id: targets[0] || null, success: false, message: `Unsupported update option '${invalidOption}'. Only --json
|
|
358
|
+
results = [{ id: targets[0] || null, success: false, message: `Unsupported update option '${invalidOption}'. Only --json and --adopt are supported.` }];
|
|
354
359
|
} else if (serviceFirstUpdate && positionalArgs.length !== 2) {
|
|
355
360
|
results = [{ id: targets[0], success: false, message: `Usage: ${CLI_NAME} <service> update [--json]; use '${CLI_NAME} update <services...>' for multiple services.` }];
|
|
356
361
|
} else if (invalid.length) {
|
|
@@ -364,7 +369,7 @@ export async function main({ updateSelfImpl = updateSelf } = {}) {
|
|
|
364
369
|
if (!jsonOutput) process.stdout.write(` Updating ${bold(id)}... `);
|
|
365
370
|
let result;
|
|
366
371
|
try {
|
|
367
|
-
result = id === CLI_NAME ? await updateSelfImpl() : await manager.updateService(id);
|
|
372
|
+
result = id === CLI_NAME ? await updateSelfImpl() : await manager.updateService(id, { adopt });
|
|
368
373
|
} catch (err) {
|
|
369
374
|
result = { success: false, message: err?.message || String(err) };
|
|
370
375
|
}
|
package/src/manager.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, readFileSync, writeFileSync, statSync, openSync, readSync, closeSync } from "node:fs";
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync, statSync, openSync, readSync, closeSync, readdirSync, rmSync } from "node:fs";
|
|
2
2
|
import { execFileSync, spawn } from "node:child_process";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { join } from "node:path";
|
|
@@ -11,6 +11,8 @@ import { installServiceUpdate, serviceStartCommand } from "./service-updates.js"
|
|
|
11
11
|
import { configureCodexForProxy } from "./codex-routing.js";
|
|
12
12
|
import {
|
|
13
13
|
HINDSIGHT_DEFAULTS,
|
|
14
|
+
HINDSIGHT_REPOSITORY,
|
|
15
|
+
adoptedHindsightSettings,
|
|
14
16
|
describeHealth,
|
|
15
17
|
dockerInfo,
|
|
16
18
|
dockerLogged,
|
|
@@ -62,8 +64,8 @@ export class ServiceManager {
|
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
/** Explicit update: fresh check, stop if needed, install, verify, restore runtime state. */
|
|
65
|
-
async updateService(serviceId, { getVersions = getServiceVersions, install = installServiceUpdate } = {}) {
|
|
66
|
-
if (isDocker(serviceId)) return this.updateDockerService(serviceId);
|
|
67
|
+
async updateService(serviceId, { getVersions = getServiceVersions, install = installServiceUpdate, adopt = false } = {}) {
|
|
68
|
+
if (isDocker(serviceId)) return this.updateDockerService(serviceId, { getVersions, adopt });
|
|
67
69
|
if (!Object.hasOwn(SERVICE_PACKAGES, serviceId)) {
|
|
68
70
|
return { success: false, message: serviceId === "proxy"
|
|
69
71
|
? "The built-in proxy is part of ai-helper; update ai-helper itself to update it."
|
|
@@ -557,39 +559,71 @@ export class ServiceManager {
|
|
|
557
559
|
return { success: true, message: `Hindsight container '${config.containerName}' stopped (data kept)` };
|
|
558
560
|
}
|
|
559
561
|
|
|
560
|
-
/**
|
|
561
|
-
|
|
562
|
+
/**
|
|
563
|
+
* Pull, back up the data, then recreate. Only containers aih created are replaced:
|
|
564
|
+
* any other container belongs to its owner until the user explicitly adopts it.
|
|
565
|
+
*/
|
|
566
|
+
async updateDockerService(id, { getVersions = getServiceVersions, adopt = false } = {}) {
|
|
562
567
|
if (this.updatesInProgress.has(id)) return { success: false, message: `An update for '${id}' is already in progress` };
|
|
563
568
|
this.updatesInProgress.add(id);
|
|
564
569
|
try {
|
|
565
570
|
const config = this.hindsightConfig(this.readState(), { strict: true });
|
|
571
|
+
const name = config.containerName;
|
|
566
572
|
const docker = await dockerInfo(this.runDocker);
|
|
567
573
|
if (!docker.available) return { success: false, message: `Docker is not available (${docker.error}). Start Docker Desktop and retry.` };
|
|
568
|
-
const container = await inspectContainer(
|
|
574
|
+
const container = await inspectContainer(name, this.runDocker, 10000);
|
|
569
575
|
if (container && !container.managed) {
|
|
570
|
-
|
|
576
|
+
if (adopt) return await this.adoptDockerService(id, name, container);
|
|
577
|
+
return { success: false, adoptable: true, message: `Container '${name}' was not created by aih${container.composeProject ? ` (docker compose project '${container.composeProject}')` : ""}. To adopt it run 'aih update ${id} --adopt' (dashboard: press u again): aih imports its settings and recreates it on the same data volume.` };
|
|
571
578
|
}
|
|
572
579
|
const previousVersion = container?.running ? (await this.probeHealth(hindsightUrls(config).api)).version ?? null : null;
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
580
|
+
// A release tag of the official image moves to the latest GitHub release; any other tag is pulled again.
|
|
581
|
+
const tag = config.image.startsWith(HINDSIGHT_REPOSITORY + ":") ? config.image.slice(HINDSIGHT_REPOSITORY.length + 1) : "";
|
|
582
|
+
let image = config.image;
|
|
583
|
+
if (compareVersions(tag, tag) !== null) {
|
|
584
|
+
const latest = (await getVersions([{ id }]))[id]?.latestVersion;
|
|
585
|
+
const comparison = compareVersions(tag, latest);
|
|
586
|
+
if (comparison === null) return { success: false, previousVersion, message: "Cannot determine the latest Hindsight release; no changes made. Check your connection and retry." };
|
|
587
|
+
if (comparison >= 0) {
|
|
588
|
+
return { success: true, updated: false, alreadyUpToDate: true, previousVersion, version: previousVersion,
|
|
589
|
+
message: `'${id}' is already up to date (${config.image})` };
|
|
590
|
+
}
|
|
591
|
+
image = HINDSIGHT_REPOSITORY + ":" + latest;
|
|
592
|
+
}
|
|
593
|
+
const pulled = await dockerLogged(id, ["pull", image], this.runDocker, 30 * 60000);
|
|
594
|
+
if (pulled.exitCode !== 0) return { success: false, previousVersion, message: `docker pull ${image} failed: ${dockerError(pulled)}` };
|
|
595
|
+
const saveImage = () => { if (image !== config.image) this.saveHindsightConfiguration({ config: { ...config, image } }); };
|
|
596
|
+
if (!container) {
|
|
597
|
+
saveImage();
|
|
598
|
+
return { success: true, updated: true, previousVersion, message: `Image ${image} pulled; the container is created on the next start` };
|
|
599
|
+
}
|
|
600
|
+
const pulledImage = await imageId(image, this.runDocker);
|
|
601
|
+
if (image === config.image && pulledImage && pulledImage === container.imageId) {
|
|
578
602
|
return { success: true, updated: false, alreadyUpToDate: true, previousVersion, version: previousVersion,
|
|
579
|
-
message: `'${id}' already uses the latest ${
|
|
603
|
+
message: `'${id}' already uses the latest ${image}` };
|
|
580
604
|
}
|
|
581
|
-
|
|
582
|
-
|
|
605
|
+
// Stop first: the archive needs a quiet database, and 'rm -f' would SIGKILL it.
|
|
606
|
+
const stopped = await dockerLogged(id, ["stop", name], this.runDocker, 60000);
|
|
607
|
+
const backup = stopped.exitCode === 0 ? await this.backupHindsightVolume(id, config.volume, container.imageId)
|
|
608
|
+
: { success: false, message: dockerError(stopped) };
|
|
609
|
+
const removed = backup.success ? await dockerLogged(id, ["rm", name], this.runDocker, 60000) : null;
|
|
610
|
+
if (removed?.exitCode !== 0) {
|
|
611
|
+
if (container.running) await dockerLogged(id, ["start", name], this.runDocker, 120000);
|
|
612
|
+
await this.syncState();
|
|
613
|
+
return { success: false, previousVersion, message: (backup.success ? `Cannot replace the container: ${dockerError(removed)}`
|
|
614
|
+
: `Data backup failed: ${backup.message}`) + "; the current container is kept" };
|
|
615
|
+
}
|
|
616
|
+
saveImage();
|
|
583
617
|
if (!container.running) {
|
|
584
618
|
await this.syncState();
|
|
585
|
-
return { success: true, updated: true, restarted: false, previousVersion,
|
|
586
|
-
message: `Image ${
|
|
619
|
+
return { success: true, updated: true, restarted: false, previousVersion, backup: backup.path,
|
|
620
|
+
message: `Image ${image} updated; the container is recreated on the next start (data backup: ${backup.path})` };
|
|
587
621
|
}
|
|
588
622
|
const started = await this.startDockerService(id);
|
|
589
623
|
const version = started.health?.version ?? null;
|
|
590
|
-
return { ...started, updated: true, restarted: started.success, previousVersion, version,
|
|
591
|
-
message: started.success ? `'${id}' updated${previousVersion && version ? ` from ${previousVersion} to ${version}` : ""} and restarted`
|
|
592
|
-
: `Image updated, but restart failed: ${started.message}` };
|
|
624
|
+
return { ...started, updated: true, restarted: started.success, previousVersion, version, backup: backup.path,
|
|
625
|
+
message: (started.success ? `'${id}' updated${previousVersion && version ? ` from ${previousVersion} to ${version}` : ""} and restarted`
|
|
626
|
+
: `Image updated, but restart failed: ${started.message}`) + ` (data backup: ${backup.path})` };
|
|
593
627
|
} catch (error) {
|
|
594
628
|
return { success: false, message: `Update failed: ${error.message}` };
|
|
595
629
|
} finally {
|
|
@@ -597,6 +631,58 @@ export class ServiceManager {
|
|
|
597
631
|
}
|
|
598
632
|
}
|
|
599
633
|
|
|
634
|
+
/** Archives a stopped container's data volume in ~/.aih/backups, keeping the two most recent archives. */
|
|
635
|
+
async backupHindsightVolume(id, volume, image) {
|
|
636
|
+
const dir = join(getAihDir(), "backups");
|
|
637
|
+
ensureDir(dir);
|
|
638
|
+
const file = `hindsight-${new Date().toISOString().replace(/[-:]|\..*/g, "")}.tgz`;
|
|
639
|
+
// The container's own image already has tar: nothing else to pull. Root, so the archive is writable on Linux.
|
|
640
|
+
const result = await dockerLogged(id, ["run", "--rm", "--user", "0", "--entrypoint", "tar", "-v", volume + ":/d:ro", "-v", dir + ":/b",
|
|
641
|
+
image, "czf", "/b/" + file, "-C", "/d", "."], this.runDocker, 30 * 60000);
|
|
642
|
+
if (result.exitCode !== 0) {
|
|
643
|
+
rmSync(join(dir, file), { force: true });
|
|
644
|
+
return { success: false, message: dockerError(result) };
|
|
645
|
+
}
|
|
646
|
+
for (const old of readdirSync(dir).filter(f => /^hindsight-.*\.tgz$/.test(f)).sort().slice(0, -2)) rmSync(join(dir, old), { force: true });
|
|
647
|
+
return { success: true, path: join(dir, file) };
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/** Takes over a container aih did not create: imports its settings, then recreates it on the same volume. */
|
|
651
|
+
async adoptDockerService(id, name, container) {
|
|
652
|
+
const refuse = reason => ({ success: false, message: `Cannot adopt container '${name}': ${reason}. Nothing was changed.` });
|
|
653
|
+
// Variables already defined by the image are not settings: importing them would pin them across updates.
|
|
654
|
+
const imageEnv = await this.runDocker("docker", ["image", "inspect", "--format", "{{json .Config.Env}}", container.imageId], 10000);
|
|
655
|
+
if (imageEnv.exitCode !== 0) return refuse(dockerError(imageEnv));
|
|
656
|
+
let adopted;
|
|
657
|
+
try { adopted = adoptedHindsightSettings(name, container, JSON.parse(imageEnv.stdout) || []); }
|
|
658
|
+
catch (error) { return refuse(error.message); }
|
|
659
|
+
if (!adopted.key && !readHindsightApiKey().key && requiresApiKey(adopted.config)) {
|
|
660
|
+
return refuse(`it has no LLM API key for provider '${adopted.config.llmProvider}'; save one in setup (c) first`);
|
|
661
|
+
}
|
|
662
|
+
const envFile = adopted.extra.length ? join(getAihDir(), "hindsight-extra.env") : "";
|
|
663
|
+
if (envFile) {
|
|
664
|
+
ensureDir(getAihDir());
|
|
665
|
+
writeFileSync(envFile, adopted.extra.join("\n") + "\n", { encoding: "utf8", mode: 0o600 });
|
|
666
|
+
}
|
|
667
|
+
this.saveHindsightConfiguration({ config: { ...adopted.config, envFile }, apiKey: adopted.key });
|
|
668
|
+
const stopped = await dockerLogged(id, ["stop", name], this.runDocker, 60000);
|
|
669
|
+
const removed = stopped.exitCode === 0 ? await dockerLogged(id, ["rm", name], this.runDocker, 60000) : stopped;
|
|
670
|
+
if (removed.exitCode !== 0) {
|
|
671
|
+
if (container.running) await dockerLogged(id, ["start", name], this.runDocker, 120000);
|
|
672
|
+
await this.syncState();
|
|
673
|
+
return { success: false, message: `Settings imported, but the container could not be replaced: ${dockerError(removed)}` };
|
|
674
|
+
}
|
|
675
|
+
const kept = `data volume '${adopted.config.volume}' kept`;
|
|
676
|
+
if (!container.running) {
|
|
677
|
+
await this.syncState();
|
|
678
|
+
return { success: true, adopted: true, message: `Container '${name}' adopted; aih recreates it on the next start (${kept})` };
|
|
679
|
+
}
|
|
680
|
+
const started = await this.startDockerService(id);
|
|
681
|
+
return { ...started, adopted: true, message: started.success
|
|
682
|
+
? `Container '${name}' adopted and recreated by aih (${kept}; ${describeHealth(started.health)}). Update it with u or 'aih update ${id}'.`
|
|
683
|
+
: `Settings imported and old container removed, but the new one failed: ${started.message}` };
|
|
684
|
+
}
|
|
685
|
+
|
|
600
686
|
/** Non-secret settings go to state.json; the key only to ~/.aih/hindsight.env. */
|
|
601
687
|
saveHindsightConfiguration({ config, apiKey = "" } = {}) {
|
|
602
688
|
const normalized = normalizeHindsightConfig(config);
|
|
@@ -618,7 +704,7 @@ export class ServiceManager {
|
|
|
618
704
|
catch (error) { return { success: true, warning: true, message: `Hindsight settings saved; Docker not available (${error.message})` }; }
|
|
619
705
|
if (!container?.running) return { success: true, message: "Hindsight settings saved for its next start" };
|
|
620
706
|
if (!container.managed) {
|
|
621
|
-
return { success: true, warning: true, message: `Hindsight settings saved; container '${config.containerName}' was not created by aih and is left unchanged` };
|
|
707
|
+
return { success: true, warning: true, message: `Hindsight settings saved; container '${config.containerName}' was not created by aih and is left unchanged (adopt it with u on the dashboard)` };
|
|
622
708
|
}
|
|
623
709
|
if (container.configHash === hindsightConfigHash(config, readHindsightApiKey().key)) return { success: true, message: "Hindsight unchanged" };
|
|
624
710
|
const stopped = await this.stopDockerService("hindsight");
|
package/src/tui-proxy-setup.js
CHANGED
|
@@ -14,8 +14,8 @@ const STARTUP_FIELD = { key: "startServices", group: "Startup", label: "Services
|
|
|
14
14
|
|
|
15
15
|
/** Shown only when Hindsight is selected for start/up. "config" maps to the saved settings. */
|
|
16
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." },
|
|
18
|
-
{ key: "hindsightImage", config: "image", group: "Hindsight", label: "Docker image", help: "Default ghcr.io/vectorize-io/hindsight:latest. Pin a
|
|
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
19
|
{ key: "hindsightApiPort", config: "apiPort", group: "Hindsight", label: "API port", help: "Loopback port for the Hindsight API and its /health check. Default 8888." },
|
|
20
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
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." },
|
|
@@ -216,7 +216,7 @@ export class ProxySetupWizard {
|
|
|
216
216
|
const after = this.draft[f.key];
|
|
217
217
|
return wrap(`${before !== after ? "* " : " "}${f.label}: ${after || "(none)"}${before !== after ? ` (previous: ${before || "empty"})` : ""}`);
|
|
218
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."));
|
|
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
220
|
}
|
|
221
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."));
|
|
222
222
|
body.push(...this.checks.flatMap(wrap));
|
package/src/tui.js
CHANGED
|
@@ -685,6 +685,9 @@ export class Dashboard {
|
|
|
685
685
|
return;
|
|
686
686
|
}
|
|
687
687
|
|
|
688
|
+
// Adopting a container needs a second, consecutive u on the same service.
|
|
689
|
+
const adopt = key === "u" && this.adoptPending === this.currentService()?.id;
|
|
690
|
+
this.adoptPending = null;
|
|
688
691
|
switch (key) {
|
|
689
692
|
case "c":
|
|
690
693
|
if (this.busy) return;
|
|
@@ -717,7 +720,9 @@ export class Dashboard {
|
|
|
717
720
|
return this.runAction("Update", async svc => {
|
|
718
721
|
this.invalidateVersions();
|
|
719
722
|
try {
|
|
720
|
-
|
|
723
|
+
const result = await manager.updateService(svc.id, { adopt });
|
|
724
|
+
if (result.adoptable) this.adoptPending = svc.id;
|
|
725
|
+
return result;
|
|
721
726
|
} finally {
|
|
722
727
|
// Drop pre-update and mid-update checks even if installation failed.
|
|
723
728
|
this.invalidateVersions();
|