create-insight 0.21.1 → 0.23.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 +74 -39
  2. package/package.json +1 -1
@@ -3,10 +3,10 @@
3
3
  // installer/create-insight.mjs
4
4
  import { spawnSync } from "node:child_process";
5
5
  import { createHash, randomBytes as randomBytes2 } 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";
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";
@@ -71,11 +71,40 @@ function makeKeys(dir2, options = {}) {
71
71
  }
72
72
 
73
73
  // src/init/compose.ts
74
+ import { existsSync as existsSync2, readFileSync as readFileSync2 } from "node:fs";
75
+ import { join as join2 } from "node:path";
74
76
  function hostFromContainer(engine2) {
75
77
  return engine2 === "podman" ? "host.containers.internal" : "host.docker.internal";
76
78
  }
77
- function defaultAppUrl(engine2) {
78
- 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;
79
108
  }
80
109
  function containerNames(project2) {
81
110
  return { insight: `${project2}-insight`, db: `${project2}-db` };
@@ -170,10 +199,10 @@ var stop = (line) => {
170
199
  process.exit(1);
171
200
  };
172
201
  var run = (cmd, args, opts = {}) => spawnSync(cmd, args, { cwd: dir, encoding: "utf8", ...opts });
173
- var envFile = join2(dir, "insight.env");
174
- 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)]));
175
- if (upgrading && !existsSync2(envFile)) stop("There\u2019s no Insight here to upgrade (no insight.env). Run npx create-insight to set it up.");
176
- 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");
177
206
  var saved = upgrading ? readEnv() : {};
178
207
  var key = (values.key ?? process.env.KALEYLA_KEY ?? saved.INSIGHT_LICENCE_KEY ?? "").trim();
179
208
  if (!key && !values.yes && process.stdin.isTTY) key = (await askHidden("Your Kaleyla key (KLY-\u2026): ")).trim();
@@ -181,7 +210,7 @@ if (!key) stop("Insight needs your Kaleyla key: give it with --key, or run this
181
210
  if (!key.startsWith("KLY-")) stop("That isn\u2019t a Kaleyla key: those start with KLY-.");
182
211
  if (!upgrading && !values["no-package"]) {
183
212
  for (const d of [dir, ...(values.also ?? []).map((a) => resolve(dir, a))]) {
184
- 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.`);
185
214
  }
186
215
  }
187
216
  var engine = detectEngine();
@@ -199,7 +228,7 @@ try {
199
228
  var version = release.version;
200
229
  var image = `${IMAGE}:${version}`;
201
230
  if (upgrading) {
202
- 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];
203
232
  if (current === version) {
204
233
  done(`Insight is already at ${values.release ? "release" : "the latest release,"} ${version}`);
205
234
  process.exit(0);
@@ -210,7 +239,7 @@ var cpu = engineArch();
210
239
  var imageFile = release.files.find((f) => f.kind === "image" && f.platform === `linux/${cpu}`);
211
240
  if (!imageFile) stop(`Release ${version} has no image for this machine\u2019s CPU (${cpu}). Tell Kaleyla.`);
212
241
  var appFile = release.files.find((f) => f.kind === "app-package");
213
- 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);
214
243
  mkdirSync(cache, { recursive: true });
215
244
  var imagePath = await download(imageFile);
216
245
  var appPath = appFile ? await download(appFile) : null;
@@ -221,17 +250,18 @@ var packageDirs = upgrading ? (saved.INSIGHT_APP_PACKAGES ?? "").split(",").filt
221
250
  for (const target of packageDirs) addPackage(target);
222
251
  if (upgrading) {
223
252
  if (!saved.INSIGHT_APP_SECRET) {
224
- const keysDir2 = join2(dir, "keys");
253
+ const keysDir2 = join3(dir, "keys");
225
254
  mkdirSync(keysDir2, { recursive: true });
226
255
  const { secret } = ensureAppSecret(keysDir2);
227
- appendFileSync(envFile, `${readFileSync2(envFile, "utf8").endsWith("\n") ? "" : "\n"}# Watches: the secret Insight sends your app's token route (the same as keys/insight-app-secret)
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)
228
257
  INSIGHT_APP_SECRET=${secret}
229
258
  `);
230
259
  done("Added the watch secret to insight.env and keys/insight-app-secret");
231
260
  }
232
- const compose = join2(dir, "docker-compose.yml");
233
- writeFileSync2(compose, readFileSync2(compose, "utf8").replace(/(image:\s*)\S+\/insight:\S+/, `$1${image}`));
234
- const up2 = run(engine.compose[0], [...engine.compose.slice(1), "up", "-d"], { stdio: ["ignore", "ignore", "pipe"] });
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"] });
235
265
  if (up2.status !== 0) stop(`${tool} up didn\u2019t start:
236
266
  ${(up2.stderr ?? "").trim()}`);
237
267
  const env = readEnv();
@@ -246,13 +276,16 @@ if (!Number.isInteger(port) || port < 1 || port > 65535) stop(`--port ${values.p
246
276
  var name = values.name ?? (run("git", ["config", "user.name"]).stdout?.trim() || userInfo().username);
247
277
  var project = projectName(basename(dir));
248
278
  var names = containerNames(project);
249
- var appUrl = values["app-url"] ?? defaultAppUrl(engine.name);
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];
250
283
  var dbPassword = randomBytes2(18).toString("hex");
251
284
  var consoleUrl = `http://127.0.0.1:${port}`;
252
- mkdirSync(join2(dir, "keys"), { recursive: true });
253
- var appSecret = ensureAppSecret(join2(dir, "keys")).secret;
254
- mkdirSync(join2(dir, "insight-config"), { recursive: true });
255
- 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 }));
256
289
  writeFileSync2(
257
290
  envFile,
258
291
  [
@@ -283,21 +316,23 @@ writeFileSync2(
283
316
  ].join("\n"),
284
317
  { mode: 384 }
285
318
  );
286
- var ignore = join2(dir, ".gitignore");
287
- 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()) : [];
288
321
  var add = ["insight.env", "keys/"].filter((l) => !ignored.includes(l) && !ignored.includes(`/${l}`));
289
322
  if (add.length) appendFileSync(ignore, `${ignored.length && ignored.at(-1) !== "" ? "\n" : ""}# Insight: secrets and the app's signing key
290
323
  ${add.join("\n")}
291
324
  `);
292
- done("Wrote docker-compose.yml, insight.env (secrets, kept out of git) and insight-config/");
293
- 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");
294
329
  mkdirSync(keysDir, { recursive: true });
295
- var existingKey = join2(keysDir, "insight-signing-key.json");
296
- var keyPair = existsSync2(existingKey) ? { kid: JSON.parse(readFileSync2(existingKey, "utf8")).kid, publicPath: join2(keysDir, "insight-public-keys.json") } : makeKeys(keysDir, { name: project.replace(/^insight-/, "") });
297
- copyFileSync(keyPair.publicPath, join2(dir, "insight-config", "insight-public-keys.json"));
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"));
298
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`);
299
334
  say(" Starting Insight and its database\u2026");
300
- 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"] });
301
336
  if (up.status !== 0) stop(`${tool} up didn\u2019t start:
302
337
  ${(up.stderr ?? "").trim()}`);
303
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}`);
@@ -313,7 +348,7 @@ say("Your app\u2019s server needs two settings (in its .env, say):");
313
348
  say(` INSIGHT_URL=${consoleUrl}`);
314
349
  say(` INSIGHT_KEYS_DIR=${keysDir} (deploy this folder with the app: its signing key and watch secret)`);
315
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.`);
316
- say("Next, in the console: Your app \u203A Connection. Choose how people will ask, then give the prompt to your coding agent.");
351
+ say("Next, in the console: Your app \u203A Connection. Choose how people will ask and which AI model answers (nothing reaches a model before that), then give the prompt to your coding agent.");
317
352
  say(`Later: ${tool} down to stop, ${tool} up -d to start again, npx create-insight upgrade for a new release.`);
318
353
  function detectEngine() {
319
354
  const ok = (cmd, args) => run(cmd, args, { stdio: "ignore" }).status === 0;
@@ -327,7 +362,7 @@ function detectEngine() {
327
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.");
328
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;
329
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.");
330
- 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";
331
366
  return { name: name2, cli: cli2, compose, selinux };
332
367
  }
333
368
  function engineArch() {
@@ -336,8 +371,8 @@ function engineArch() {
336
371
  return /arm64|aarch64/i.test(a) ? "arm64" : /x86_64|amd64|x64/i.test(a) ? "amd64" : a;
337
372
  }
338
373
  async function download(file) {
339
- const target = join2(cache, file.name);
340
- 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) {
341
376
  done(`${file.name} (already downloaded)`);
342
377
  return target;
343
378
  }
@@ -368,17 +403,17 @@ async function download(file) {
368
403
  return target;
369
404
  }
370
405
  function sha256(path) {
371
- return createHash("sha256").update(readFileSync2(path)).digest("hex");
406
+ return createHash("sha256").update(readFileSync3(path)).digest("hex");
372
407
  }
373
408
  function addPackage(target) {
374
- 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.`);
375
- 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");
376
411
  mkdirSync(vendor, { recursive: true });
377
- 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));
378
413
  const file = `kaleyla-insight-${version}.tgz`;
379
- copyFileSync(appPath, join2(vendor, file));
414
+ copyFileSync(appPath, join3(vendor, file));
380
415
  const spec = `file:vendor/${file}`;
381
- const lock = (n) => existsSync2(join2(target, n));
416
+ const lock = (n) => existsSync3(join3(target, n));
382
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}`]];
383
418
  const r = spawnSync(pm, args, { cwd: target, encoding: "utf8", shell: process.platform === "win32" });
384
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.21.1",
3
+ "version": "0.23.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",