create-insight 0.8.0 → 0.22.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 (2) hide show
  1. package/create-insight.mjs +103 -40
  2. package/package.json +1 -1
@@ -2,11 +2,11 @@
2
2
 
3
3
  // installer/create-insight.mjs
4
4
  import { spawnSync } from "node:child_process";
5
- import { createHash, randomBytes } from "node:crypto";
6
- import { appendFileSync, copyFileSync, createWriteStream, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync2, readdirSync, renameSync, rmSync, statSync, writeFileSync as writeFileSync2 } from "node:fs";
5
+ import { createHash, randomBytes as randomBytes2 } from "node:crypto";
6
+ import { appendFileSync, copyFileSync, createWriteStream, existsSync as existsSync3, mkdirSync, readFileSync as readFileSync3, readdirSync, renameSync, rmSync, statSync, writeFileSync as writeFileSync2 } from "node:fs";
7
7
  import { createServer } from "node:net";
8
8
  import { arch as hostArch, homedir, userInfo } from "node:os";
9
- import { basename, join as join2, relative, resolve } from "node:path";
9
+ import { basename, join as join3, relative, resolve } from "node:path";
10
10
  import { createInterface } from "node:readline";
11
11
  import { Readable } from "node:stream";
12
12
  import { pipeline } from "node:stream/promises";
@@ -29,11 +29,26 @@ ${help}`);
29
29
  }
30
30
 
31
31
  // src/auth/keys.ts
32
- import { generateKeyPairSync, randomUUID } from "node:crypto";
32
+ import { generateKeyPairSync, randomBytes, randomUUID } from "node:crypto";
33
33
  import { chmodSync, existsSync, readFileSync, writeFileSync } from "node:fs";
34
34
  import { join } from "node:path";
35
35
  var SIGNING_FILE = "insight-signing-key.json";
36
36
  var PUBLIC_FILE = "insight-public-keys.json";
37
+ var APP_SECRET_FILE = "insight-app-secret";
38
+ function readAppSecret(dir2) {
39
+ const file = join(dir2, APP_SECRET_FILE);
40
+ if (!existsSync(file)) return null;
41
+ const secret = readFileSync(file, "utf8").trim();
42
+ return secret || null;
43
+ }
44
+ function ensureAppSecret(dir2) {
45
+ const kept = readAppSecret(dir2);
46
+ if (kept) return { secret: kept, made: false };
47
+ const secret = randomBytes(32).toString("hex");
48
+ writeFileSync(join(dir2, APP_SECRET_FILE), `${secret}
49
+ `, { mode: 384 });
50
+ return { secret, made: true };
51
+ }
37
52
  var KeysError = class extends Error {
38
53
  };
39
54
  function makeKeys(dir2, options = {}) {
@@ -56,11 +71,40 @@ function makeKeys(dir2, options = {}) {
56
71
  }
57
72
 
58
73
  // src/init/compose.ts
74
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
75
+ import { join as join2 } from "node:path";
59
76
  function hostFromContainer(engine2) {
60
77
  return engine2 === "podman" ? "host.containers.internal" : "host.docker.internal";
61
78
  }
62
- function defaultAppUrl(engine2) {
63
- return `http://${hostFromContainer(engine2)}:3000`;
79
+ function defaultAppUrl(engine2, port2 = 3e3) {
80
+ return `http://${hostFromContainer(engine2)}:${port2}`;
81
+ }
82
+ var INSIGHT_COMPOSE = "docker-compose.insight.yml";
83
+ function composeFile(dir2) {
84
+ if (existsSync2(join2(dir2, INSIGHT_COMPOSE))) return INSIGHT_COMPOSE;
85
+ const main = join2(dir2, "docker-compose.yml");
86
+ if (!existsSync2(main)) return "docker-compose.yml";
87
+ return /\/insight:\S+/.test(readFileSync2(main, "utf8")) ? "docker-compose.yml" : INSIGHT_COMPOSE;
88
+ }
89
+ function appPort(dir2) {
90
+ const ok = (n) => Number.isInteger(n) && n > 0 && n < 65536;
91
+ try {
92
+ const scripts = JSON.parse(readFileSync2(join2(dir2, "package.json"), "utf8")).scripts ?? {};
93
+ for (const name2 of ["dev", "start"]) {
94
+ const m = /(?:^|\s)(?:-p|--port)[ =]?(\d{2,5})\b|\bPORT=(\d{2,5})\b/.exec(scripts[name2] ?? "");
95
+ const n = Number(m?.[1] ?? m?.[2]);
96
+ if (m && ok(n)) return { port: n, from: `package.json's ${name2} script` };
97
+ }
98
+ } catch {
99
+ }
100
+ for (const file of [".env.local", ".env"]) {
101
+ try {
102
+ const m = /^\s*PORT\s*=\s*["']?(\d{2,5})/m.exec(readFileSync2(join2(dir2, file), "utf8"));
103
+ if (m && ok(Number(m[1]))) return { port: Number(m[1]), from: file };
104
+ } catch {
105
+ }
106
+ }
107
+ return null;
64
108
  }
65
109
  function containerNames(project2) {
66
110
  return { insight: `${project2}-insight`, db: `${project2}-db` };
@@ -155,10 +199,10 @@ var stop = (line) => {
155
199
  process.exit(1);
156
200
  };
157
201
  var run = (cmd, args, opts = {}) => spawnSync(cmd, args, { cwd: dir, encoding: "utf8", ...opts });
158
- var envFile = join2(dir, "insight.env");
159
- var readEnv = () => Object.fromEntries(readFileSync2(envFile, "utf8").split("\n").filter((l) => /^[A-Z_]+=/.test(l)).map((l) => [l.slice(0, l.indexOf("=")), l.slice(l.indexOf("=") + 1)]));
160
- if (upgrading && !existsSync2(envFile)) stop("There\u2019s no Insight here to upgrade (no insight.env). Run npx create-insight to set it up.");
161
- if (!upgrading && existsSync2(envFile)) stop("Insight is already set up here (insight.env exists). To bring it up to date: npx create-insight upgrade");
202
+ var envFile = join3(dir, "insight.env");
203
+ var readEnv = () => Object.fromEntries(readFileSync3(envFile, "utf8").split("\n").filter((l) => /^[A-Z_]+=/.test(l)).map((l) => [l.slice(0, l.indexOf("=")), l.slice(l.indexOf("=") + 1)]));
204
+ if (upgrading && !existsSync3(envFile)) stop("There\u2019s no Insight here to upgrade (no insight.env). Run npx create-insight to set it up.");
205
+ if (!upgrading && existsSync3(envFile)) stop("Insight is already set up here (insight.env exists). To bring it up to date: npx create-insight upgrade");
162
206
  var saved = upgrading ? readEnv() : {};
163
207
  var key = (values.key ?? process.env.KALEYLA_KEY ?? saved.INSIGHT_LICENCE_KEY ?? "").trim();
164
208
  if (!key && !values.yes && process.stdin.isTTY) key = (await askHidden("Your Kaleyla key (KLY-\u2026): ")).trim();
@@ -166,7 +210,7 @@ if (!key) stop("Insight needs your Kaleyla key: give it with --key, or run this
166
210
  if (!key.startsWith("KLY-")) stop("That isn\u2019t a Kaleyla key: those start with KLY-.");
167
211
  if (!upgrading && !values["no-package"]) {
168
212
  for (const d of [dir, ...(values.also ?? []).map((a) => resolve(dir, a))]) {
169
- if (!existsSync2(join2(d, "package.json"))) stop(`${relative(dir, d) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
213
+ if (!existsSync3(join3(d, "package.json"))) stop(`${relative(dir, d) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
170
214
  }
171
215
  }
172
216
  var engine = detectEngine();
@@ -184,7 +228,7 @@ try {
184
228
  var version = release.version;
185
229
  var image = `${IMAGE}:${version}`;
186
230
  if (upgrading) {
187
- const current = /image:\s*\S+:(\S+)/.exec(readFileSync2(join2(dir, "docker-compose.yml"), "utf8"))?.[1];
231
+ const current = /image:\s*\S+:(\S+)/.exec(readFileSync3(join3(dir, composeFile(dir)), "utf8"))?.[1];
188
232
  if (current === version) {
189
233
  done(`Insight is already at ${values.release ? "release" : "the latest release,"} ${version}`);
190
234
  process.exit(0);
@@ -195,7 +239,7 @@ var cpu = engineArch();
195
239
  var imageFile = release.files.find((f) => f.kind === "image" && f.platform === `linux/${cpu}`);
196
240
  if (!imageFile) stop(`Release ${version} has no image for this machine\u2019s CPU (${cpu}). Tell Kaleyla.`);
197
241
  var appFile = release.files.find((f) => f.kind === "app-package");
198
- var cache = join2(process.env.XDG_CACHE_HOME ?? join2(homedir(), ".cache"), "kaleyla", "insight", version);
242
+ var cache = join3(process.env.XDG_CACHE_HOME ?? join3(homedir(), ".cache"), "kaleyla", "insight", version);
199
243
  mkdirSync(cache, { recursive: true });
200
244
  var imagePath = await download(imageFile);
201
245
  var appPath = appFile ? await download(appFile) : null;
@@ -205,9 +249,19 @@ done(`Loaded Insight ${version} into ${engine.name === "podman" ? "Podman" : "Do
205
249
  var packageDirs = upgrading ? (saved.INSIGHT_APP_PACKAGES ?? "").split(",").filter(Boolean).map((d) => resolve(dir, d)) : values["no-package"] ? [] : [dir, ...(values.also ?? []).map((d) => resolve(dir, d))];
206
250
  for (const target of packageDirs) addPackage(target);
207
251
  if (upgrading) {
208
- const compose = join2(dir, "docker-compose.yml");
209
- writeFileSync2(compose, readFileSync2(compose, "utf8").replace(/(image:\s*)\S+\/insight:\S+/, `$1${image}`));
210
- const up2 = run(engine.compose[0], [...engine.compose.slice(1), "up", "-d"], { stdio: ["ignore", "ignore", "pipe"] });
252
+ if (!saved.INSIGHT_APP_SECRET) {
253
+ const keysDir2 = join3(dir, "keys");
254
+ mkdirSync(keysDir2, { recursive: true });
255
+ const { secret } = ensureAppSecret(keysDir2);
256
+ appendFileSync(envFile, `${readFileSync3(envFile, "utf8").endsWith("\n") ? "" : "\n"}# Watches: the secret Insight sends your app's token route (the same as keys/insight-app-secret)
257
+ INSIGHT_APP_SECRET=${secret}
258
+ `);
259
+ done("Added the watch secret to insight.env and keys/insight-app-secret");
260
+ }
261
+ const file = composeFile(dir);
262
+ const compose = join3(dir, file);
263
+ writeFileSync2(compose, readFileSync3(compose, "utf8").replace(/(image:\s*)\S+\/insight:\S+/, `$1${image}`));
264
+ const up2 = run(engine.compose[0], [...engine.compose.slice(1), ...file === "docker-compose.yml" ? [] : ["-f", file], "up", "-d"], { stdio: ["ignore", "ignore", "pipe"] });
211
265
  if (up2.status !== 0) stop(`${tool} up didn\u2019t start:
212
266
  ${(up2.stderr ?? "").trim()}`);
213
267
  const env = readEnv();
@@ -222,11 +276,16 @@ if (!Number.isInteger(port) || port < 1 || port > 65535) stop(`--port ${values.p
222
276
  var name = values.name ?? (run("git", ["config", "user.name"]).stdout?.trim() || userInfo().username);
223
277
  var project = projectName(basename(dir));
224
278
  var names = containerNames(project);
225
- var appUrl = values["app-url"] ?? defaultAppUrl(engine.name);
226
- var dbPassword = randomBytes(18).toString("hex");
279
+ var found = values["app-url"] ? null : appPort(dir);
280
+ var appUrl = values["app-url"] ?? defaultAppUrl(engine.name, found?.port ?? 3e3);
281
+ var composeName = composeFile(dir);
282
+ var composeArgs = composeName === "docker-compose.yml" ? [] : ["-f", composeName];
283
+ var dbPassword = randomBytes2(18).toString("hex");
227
284
  var consoleUrl = `http://127.0.0.1:${port}`;
228
- mkdirSync(join2(dir, "insight-config"), { recursive: true });
229
- writeFileSync2(join2(dir, "docker-compose.yml"), composeYaml({ engine: engine.name, selinux: engine.selinux, project, image, port }));
285
+ mkdirSync(join3(dir, "keys"), { recursive: true });
286
+ var appSecret = ensureAppSecret(join3(dir, "keys")).secret;
287
+ mkdirSync(join3(dir, "insight-config"), { recursive: true });
288
+ writeFileSync2(join3(dir, composeName), composeYaml({ engine: engine.name, selinux: engine.selinux, project, image, port }));
230
289
  writeFileSync2(
231
290
  envFile,
232
291
  [
@@ -234,9 +293,11 @@ writeFileSync2(
234
293
  "INSIGHT_CONFIG=/config/insight.yaml",
235
294
  "INSIGHT_TRUSTED_KEYS=/config/insight-public-keys.json",
236
295
  "INSIGHT_SETUP_HOST_DIR=./insight-config",
237
- `INSIGHT_CONSOLE_SECRET=${randomBytes(32).toString("hex")}`,
296
+ `INSIGHT_CONSOLE_SECRET=${randomBytes2(32).toString("hex")}`,
238
297
  `INSIGHT_CONSOLE_URL=${consoleUrl}`,
239
298
  `INSIGHT_READ_URL=${appUrl.replace(/\/+$/, "")}/api/insight/read`,
299
+ // Watches: the secret Insight sends your app's token route (the same as keys/insight-app-secret).
300
+ `INSIGHT_APP_SECRET=${appSecret}`,
240
301
  // Insight's model, through Kaleyla, opened by the Kaleyla key below. Another model: the console, Settings › AI models.
241
302
  `INSIGHT_MODEL_URL=${init_defaults_default.modelUrl}`,
242
303
  `INSIGHT_MODEL=${init_defaults_default.model}`,
@@ -255,21 +316,23 @@ writeFileSync2(
255
316
  ].join("\n"),
256
317
  { mode: 384 }
257
318
  );
258
- var ignore = join2(dir, ".gitignore");
259
- var ignored = existsSync2(ignore) ? readFileSync2(ignore, "utf8").split("\n").map((l) => l.trim()) : [];
319
+ var ignore = join3(dir, ".gitignore");
320
+ var ignored = existsSync3(ignore) ? readFileSync3(ignore, "utf8").split("\n").map((l) => l.trim()) : [];
260
321
  var add = ["insight.env", "keys/"].filter((l) => !ignored.includes(l) && !ignored.includes(`/${l}`));
261
322
  if (add.length) appendFileSync(ignore, `${ignored.length && ignored.at(-1) !== "" ? "\n" : ""}# Insight: secrets and the app's signing key
262
323
  ${add.join("\n")}
263
324
  `);
264
- done("Wrote docker-compose.yml, insight.env (secrets, kept out of git) and insight-config/");
265
- var keysDir = join2(dir, "keys");
325
+ done(`Wrote ${composeName}, insight.env (secrets, kept out of git) and insight-config/`);
326
+ if (composeName !== "docker-compose.yml") say(` Your app has its own docker-compose.yml, left as it is. Insight's is ${composeName}: start it with ${tool} -f ${composeName} up -d`);
327
+ if (!values["app-url"]) say(` Insight reads your app at port ${found?.port ?? 3e3} (${found ? `from ${found.from}` : "nothing in package.json or .env said, so the usual 3000"}). If that's wrong, change INSIGHT_READ_URL in insight.env.`);
328
+ var keysDir = join3(dir, "keys");
266
329
  mkdirSync(keysDir, { recursive: true });
267
- var existingKey = join2(keysDir, "insight-signing-key.json");
268
- var keyPair = existsSync2(existingKey) ? { kid: JSON.parse(readFileSync2(existingKey, "utf8")).kid, publicPath: join2(keysDir, "insight-public-keys.json") } : makeKeys(keysDir, { name: project.replace(/^insight-/, "") });
269
- copyFileSync(keyPair.publicPath, join2(dir, "insight-config", "insight-public-keys.json"));
270
- done(`Made the app\u2019s signing key (${keyPair.kid}) in keys/; Insight trusts its public half`);
330
+ var existingKey = join3(keysDir, "insight-signing-key.json");
331
+ var keyPair = existsSync3(existingKey) ? { kid: JSON.parse(readFileSync3(existingKey, "utf8")).kid, publicPath: join3(keysDir, "insight-public-keys.json") } : makeKeys(keysDir, { name: project.replace(/^insight-/, "") });
332
+ copyFileSync(keyPair.publicPath, join3(dir, "insight-config", "insight-public-keys.json"));
333
+ done(`Made the app\u2019s signing key (${keyPair.kid}) in keys/; Insight trusts its public half. keys/ also holds the watch secret Insight shares with your app`);
271
334
  say(" Starting Insight and its database\u2026");
272
- var up = run(engine.compose[0], [...engine.compose.slice(1), "-p", project, "up", "-d"], { stdio: ["ignore", "ignore", "pipe"] });
335
+ var up = run(engine.compose[0], [...engine.compose.slice(1), ...composeArgs, "-p", project, "up", "-d"], { stdio: ["ignore", "ignore", "pipe"] });
273
336
  if (up.status !== 0) stop(`${tool} up didn\u2019t start:
274
337
  ${(up.stderr ?? "").trim()}`);
275
338
  if (!await healthy(`${consoleUrl}/v1/health`, version, 12e4)) stop(`Insight didn\u2019t answer at ${consoleUrl} within 2 minutes. See why with: ${cli} logs ${names.insight}`);
@@ -283,7 +346,7 @@ done(opened ? "Opened the console, signed in as its Owner" : `Sign in to the con
283
346
  say();
284
347
  say("Your app\u2019s server needs two settings (in its .env, say):");
285
348
  say(` INSIGHT_URL=${consoleUrl}`);
286
- say(` INSIGHT_KEYS_DIR=${keysDir}`);
349
+ say(` INSIGHT_KEYS_DIR=${keysDir} (deploy this folder with the app: its signing key and watch secret)`);
287
350
  if (packageDirs.length) say(`Commit vendor/kaleyla-insight-${version}.tgz with package.json: your CI installs Insight\u2019s package from it, with no key.`);
288
351
  say("Next, in the console: Your app \u203A Connection. Choose how people will ask, then give the prompt to your coding agent.");
289
352
  say(`Later: ${tool} down to stop, ${tool} up -d to start again, npx create-insight upgrade for a new release.`);
@@ -299,7 +362,7 @@ function detectEngine() {
299
362
  if (!ok(cli2, ["info"])) stop(name2 === "podman" ? "Podman isn\u2019t running. On a Mac: podman machine start. On Linux, check podman info." : "Docker isn\u2019t running. Start Docker Desktop (or the Docker service), then run this again.");
300
363
  const compose = name2 === "docker" ? ok("docker", ["compose", "version"]) ? ["docker", "compose"] : null : ok(cli2, ["compose", "version"]) ? [cli2, "compose"] : ok("podman-compose", ["version"]) ? ["podman-compose"] : null;
301
364
  if (!compose) stop(name2 === "docker" ? "Docker Compose isn\u2019t available. It comes with Docker Desktop; on Linux, install the docker-compose-plugin." : "Podman needs a compose tool: install podman-compose, then run this again.");
302
- const selinux = process.platform === "linux" && existsSync2("/sys/fs/selinux/enforce") && readFileSync2("/sys/fs/selinux/enforce", "utf8").trim() === "1";
365
+ const selinux = process.platform === "linux" && existsSync3("/sys/fs/selinux/enforce") && readFileSync3("/sys/fs/selinux/enforce", "utf8").trim() === "1";
303
366
  return { name: name2, cli: cli2, compose, selinux };
304
367
  }
305
368
  function engineArch() {
@@ -308,8 +371,8 @@ function engineArch() {
308
371
  return /arm64|aarch64/i.test(a) ? "arm64" : /x86_64|amd64|x64/i.test(a) ? "amd64" : a;
309
372
  }
310
373
  async function download(file) {
311
- const target = join2(cache, file.name);
312
- if (existsSync2(target) && statSync(target).size === file.size && sha256(target) === file.sha256) {
374
+ const target = join3(cache, file.name);
375
+ if (existsSync3(target) && statSync(target).size === file.size && sha256(target) === file.sha256) {
313
376
  done(`${file.name} (already downloaded)`);
314
377
  return target;
315
378
  }
@@ -340,17 +403,17 @@ async function download(file) {
340
403
  return target;
341
404
  }
342
405
  function sha256(path) {
343
- return createHash("sha256").update(readFileSync2(path)).digest("hex");
406
+ return createHash("sha256").update(readFileSync3(path)).digest("hex");
344
407
  }
345
408
  function addPackage(target) {
346
- if (!existsSync2(join2(target, "package.json"))) stop(`${relative(dir, target) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
347
- const vendor = join2(target, "vendor");
409
+ if (!existsSync3(join3(target, "package.json"))) stop(`${relative(dir, target) || "This folder"} has no package.json. Run this from your app\u2019s folder, or add --no-package for an app that isn\u2019t on Node.js.`);
410
+ const vendor = join3(target, "vendor");
348
411
  mkdirSync(vendor, { recursive: true });
349
- for (const old of readdirSync(vendor).filter((n) => /^kaleyla-insight-.+\.tgz$/.test(n))) rmSync(join2(vendor, old));
412
+ for (const old of readdirSync(vendor).filter((n) => /^kaleyla-insight-.+\.tgz$/.test(n))) rmSync(join3(vendor, old));
350
413
  const file = `kaleyla-insight-${version}.tgz`;
351
- copyFileSync(appPath, join2(vendor, file));
414
+ copyFileSync(appPath, join3(vendor, file));
352
415
  const spec = `file:vendor/${file}`;
353
- const lock = (n) => existsSync2(join2(target, n));
416
+ const lock = (n) => existsSync3(join3(target, n));
354
417
  const [pm, args] = lock("pnpm-lock.yaml") ? ["pnpm", ["add", `@kaleyla/insight@${spec}`]] : lock("yarn.lock") ? ["yarn", ["add", `@kaleyla/insight@${spec}`]] : lock("bun.lockb") || lock("bun.lock") ? ["bun", ["add", `@kaleyla/insight@${spec}`]] : ["npm", ["install", "--save-exact", `@kaleyla/insight@${spec}`]];
355
418
  const r = spawnSync(pm, args, { cwd: target, encoding: "utf8", shell: process.platform === "win32" });
356
419
  if (r.status !== 0) stop(`${pm} couldn\u2019t add @kaleyla/insight in ${relative(dir, target) || "this folder"}:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-insight",
3
- "version": "0.8.0",
3
+ "version": "0.22.0",
4
4
  "description": "Sets Insight up beside your app, with your Kaleyla key: npx create-insight",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",