@tiens.nguyen/gu-cli 1.0.931 → 1.0.935

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/gu-cli.mjs CHANGED
@@ -21,7 +21,7 @@ requireSupportedNode();
21
21
  import { mkdir, mkdtemp, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
22
22
  import { execFile as execFileCallback, spawn } from "node:child_process";
23
23
  import { createHash, randomUUID } from "node:crypto";
24
- import { writeFileSync, unlinkSync } from "node:fs";
24
+ import { readFileSync, writeFileSync, unlinkSync } from "node:fs";
25
25
  import { createTaskPool, createFolderQueue } from "./job-pools.mjs";
26
26
  import { NO_STEP, foldStepTokens, withStepTokens, NO_CACHE, foldTurnCache, withTurnCache } from "./step-tokens.mjs";
27
27
  import { agentStoppedLine, jobEnded, jobStatusFrom409, stopGracefully } from "./agent-stop.mjs";
@@ -44,6 +44,7 @@ import { MINIMAL_RUN_LOGS, agentLogProfile, currentLevel, installLevelFilter, le
44
44
  import { skillsForTurn } from "./skills.mjs";
45
45
  import { mcpForTurn } from "./mcp-config.mjs";
46
46
  import { hooksForTurn, sessionForTurn } from "./hooks-config.mjs";
47
+ import { allLanguages, lspExtensions, lspStatePath, readLspState } from "./lsp-registry.mjs";
47
48
 
48
49
  /** The `skills` / `mcp` fields of one job's agent config (task #269). A broken config is a log
49
50
  * line, never a failed turn — the turn runs without them. */
@@ -752,7 +753,10 @@ const currentApiVersion = async () => {
752
753
  // /api/health answers without touching MongoDB, so this line said "up" on a machine whose
753
754
  // mongod was gone while every real request timed out. The handshake is the live check.
754
755
  let mongoDown = "";
755
- if (probe.ok) {
756
+ // gudb (GH #534) has no MongoDB to ask: on a computer without one the probe would report
757
+ // "MongoDB is NOT answering" about a database the API never uses (GH #557 follow-up).
758
+ const onGudb = probe.body?.storage === "gudb" || ce.readLocalDb() === "gudb";
759
+ if (probe.ok && !onGudb) {
756
760
  try {
757
761
  const { localMongoDown } = await import("./client-expert-run.mjs");
758
762
  mongoDown = await localMongoDown(ce.localMongoUri());
@@ -763,7 +767,7 @@ const currentApiVersion = async () => {
763
767
  ? `\n API is running on ${ce.LOCAL_API_BASE}` + (running ? ` (${running})` : "") +
764
768
  `, but MongoDB is NOT answering at ${mongoDown}\n — every request will hang until it does. Run \`gu\`: it says how to start it.`
765
769
  : probe.ok
766
- ? `\n API is up on ${ce.LOCAL_API_BASE} (mongo, persistence on)` +
770
+ ? `\n API is up on ${ce.LOCAL_API_BASE} (${probe.body?.storage === "gudb" ? `built-in database: ${ce.gudbFile()}` : "mongo"}, persistence on)` +
767
771
  (running ? ` — running ${running}` : "")
768
772
  : `\n API is not usable: ${probe.why}\n Log: ${ce.API_LOG_FILE}`
769
773
  );
@@ -1250,7 +1254,7 @@ if (args[0] === "fix-powershell") {
1250
1254
  // stops the profile loading — the same "said Done, changed nothing" mistake one branch along.
1251
1255
  const blockedBy = () => {
1252
1256
  try {
1253
- return pa.profileBlockedBy(runPs(host, "Get-ExecutionPolicy"));
1257
+ return pa.profileBlockedBy(runPs(host, pa.SAVED_POLICY_EXPR)); // not this process's override
1254
1258
  } catch {
1255
1259
  return "";
1256
1260
  }
@@ -1334,7 +1338,10 @@ if (args[0] === "clear" || args[0] === "reset") {
1334
1338
  // Client Expert keeps everything in a local database, which is state this command has to
1335
1339
  // account for — see localDatabaseItem.
1336
1340
  const { readdir: _readdir } = await import("node:fs/promises");
1337
- const dbItem = cs.localDatabaseItem({ apiBase: process.env.GONEXT_API_BASE ?? "" });
1341
+ const dbItem = cs.localDatabaseItem({
1342
+ apiBase: process.env.GONEXT_API_BASE ?? "",
1343
+ localDb: (await import("./client-expert-api.mjs")).readLocalDb(),
1344
+ });
1338
1345
  let apiDirForDrop = "";
1339
1346
  if (dbItem) {
1340
1347
  const base = join(homedir(), ".gonext", "api");
@@ -1862,6 +1869,52 @@ if (args[0] === "deps") {
1862
1869
  process.exit(ok ? 0 : 1);
1863
1870
  }
1864
1871
 
1872
+ if (args[0] === "prepare") {
1873
+ // GH #561: the installers run this right after npm. Everything the first `gu` would install without asking
1874
+ // anything — Python (Windows), the agent framework, the document tools, the API, its dependencies, the database —
1875
+ // is done NOW, while the user expects to wait, and the API is started once as a check. Running the installer is
1876
+ // the agreement, so nothing is asked here (`curl | sh` may have no keyboard); every change is said (#557).
1877
+ // worker.env is never written: the first `gu` still asks its real questions (mode, license, promo code, models).
1878
+ const md = await import("./model-doctor.mjs");
1879
+ process.env.GU_PIP_QUIET = "1";
1880
+ console.log("\n Getting gu ready …");
1881
+ // Running the installer agrees to the agent framework and the document tools. Installing PYTHON itself is still
1882
+ // asked (Windows): with no keyboard (`curl | sh`) it is not installed now unless GU_INSTALL_YES=1, and the first
1883
+ // gu asks.
1884
+ const { askLine } = await import("./agent-deps-preflight.mjs");
1885
+ const out = await md.ensureAgentDeps({
1886
+ log: (l) => console.log(l),
1887
+ confirm: async (q, defYes = true) => {
1888
+ if (!process.stdin.isTTY) return process.env.GU_INSTALL_YES === "1";
1889
+ const typed = String(await askLine(" " + q + (defYes ? " [Y/n]: " : " [y/N]: "))).trim().toLowerCase();
1890
+ return typed === "" ? defYes : /^y(es)?$/.test(typed);
1891
+ },
1892
+ modelHost: false,
1893
+ agreed: ["agentLibs", "skillsLibs"],
1894
+ skillsPack: true,
1895
+ });
1896
+ const depsOk = out.agentLibs === "installed" || out.agentLibs === "present";
1897
+
1898
+ let stack = { ok: true, skipped: "" };
1899
+ if (!args.includes("--no-api")) {
1900
+ const { prepareLocalStack } = await import("./client-expert-run.mjs");
1901
+ stack = await prepareLocalStack({});
1902
+ }
1903
+ // One plain line on screen; the reason something was left goes to ~/.gonext/logs/prepare.log (GH #564), for support.
1904
+ if (!(depsOk && stack.ok)) {
1905
+ try {
1906
+ const { appendFileSync, mkdirSync } = await import("node:fs");
1907
+ const { join } = await import("node:path");
1908
+ const { homedir } = await import("node:os");
1909
+ const dir = join(homedir(), ".gonext", "logs");
1910
+ mkdirSync(dir, { recursive: true });
1911
+ appendFileSync(join(dir, "prepare.log"), `${new Date().toISOString()} agent=${out.agentLibs ?? "?"} python=${out.python3 ?? "?"} api=${stack.ok ? "ok" : "left"}: ${stack.skipped || "-"}\n`);
1912
+ } catch { /* the log is a convenience */ }
1913
+ }
1914
+ console.log(depsOk && stack.ok ? " ✓ gu is ready" : " · A few things are left — the first gu finishes them.");
1915
+ process.exit(depsOk && stack.ok ? 0 : 1);
1916
+ }
1917
+
1865
1918
  if (args[0] === "doctor") {
1866
1919
  // Task #304: a Mac whose MongoDB runs in Docker is told the native option, once, up front —
1867
1920
  // a working Docker MongoDB is left alone (it is a choice), a silent one is named as stuck.
@@ -2035,7 +2088,7 @@ if (args[0] === "doctor") {
2035
2088
  // NOW Mongo matters, and it goes first: the API waits on it without a timeout of its
2036
2089
  // own, so starting the API against a dead database produces exactly the "listening but
2037
2090
  // never answers" state that sends people here.
2038
- const mongo = await run.probeMongo();
2091
+ const mongo = ce.readLocalDb() === "gudb" ? { running: true, how: `built in: ${ce.gudbFile()}` } : await run.probeMongo();
2039
2092
  if (mongo.running) {
2040
2093
  console.log(` · MongoDB is running (${mongo.how}), so the API is the problem.`);
2041
2094
  } else {
@@ -3991,6 +4044,32 @@ function _runBetweenTurnsIndex({ python, pythonArgs, scriptPath, input, jobActiv
3991
4044
  console.log(`[gu-worker] between-turns index started for ${jobActiveWorkspace}`);
3992
4045
  }
3993
4046
 
4047
+ // GH #540: the language-server host, started the first time a terminal turn needs it and kept for
4048
+ // the life of this worker — servers are slow to start, turns are short. See lsp-host.mjs.
4049
+ let _lspHost = null;
4050
+ function getLspHost() {
4051
+ if (!_lspHost) {
4052
+ _lspHost = import("./lsp-host.mjs")
4053
+ .then((m) => m.serveLspHost({ home: homedir(), log: (s) => console.log(`[gu-worker] ${s}`) }))
4054
+ .catch((e) => {
4055
+ console.log(`[gu-worker] [lsp] host not started: ${e?.message || e}`);
4056
+ _lspHost = null;
4057
+ return null;
4058
+ });
4059
+ }
4060
+ return _lspHost;
4061
+ }
4062
+
4063
+ /** The languages the user turned on — the agent's `lsp` tool is offered only when there is one (GH #541). */
4064
+ function lspLanguagesOn() {
4065
+ try {
4066
+ const st = readLspState(readFileSync(lspStatePath(homedir()), "utf8"));
4067
+ return Object.entries(st.languages).filter(([, v]) => v === "on").map(([k]) => k);
4068
+ } catch {
4069
+ return [];
4070
+ }
4071
+ }
4072
+
3994
4073
  async function runAgentChatJob(job) {
3995
4074
  const { jobId, payload } = job;
3996
4075
  const start = Date.now();
@@ -4292,6 +4371,11 @@ async function runAgentChatJob(job) {
4292
4371
  // REPL's "I can show a picker" signal (else python keeps hard-blocking, as on web).
4293
4372
  jobId,
4294
4373
  interactiveApproval: payload?.interactiveApproval === true,
4374
+ // GH #540: where the agent asks for a file's errors after an edit — only for a terminal turn
4375
+ // whose folder is on THIS computer (a language server reads the project from disk).
4376
+ lsp: terminalActiveWs && wsExistsHere
4377
+ ? await getLspHost().then((h) => (h ? { url: h.url, token: h.token, exts: lspExtensions(allLanguages({ home: homedir(), workspace: terminalActiveWs })), on: lspLanguagesOn() } : null))
4378
+ : null,
4295
4379
  // "accept edits" (task #179). Absent ⇒ true: only the terminal sends it, and only when
4296
4380
  // the user has turned it OFF, so every other caller keeps today's behaviour.
4297
4381
  acceptEdits: payload?.acceptEdits !== false,