@tiens.nguyen/gu-cli 1.0.939 → 1.0.940
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/README.md +17 -7
- package/agent-deps-preflight.mjs +1 -1
- package/autocompact.mjs +2 -2
- package/browser-bridge.mjs +2 -2
- package/browser-chrome.mjs +2 -2
- package/browser-command.mjs +4 -4
- package/browser-extension/README.md +6 -6
- package/clear-logs.mjs +3 -3
- package/clear-state.mjs +3 -3
- package/cli-token.mjs +8 -8
- package/client-expert-api.mjs +9 -9
- package/client-expert-run.mjs +7 -7
- package/client-expert-setup.mjs +4 -4
- package/command-allow.mjs +1 -1
- package/conversation-archive.mjs +2 -2
- package/credential-recovery.mjs +1 -1
- package/daemon-control.mjs +4 -4
- package/device-login.mjs +3 -3
- package/free-model.mjs +1 -1
- package/full-log-command.mjs +1 -1
- package/gonext_agent_chat.py +71 -71
- package/gu-cli.mjs +29 -28
- package/gu-home-move.mjs +193 -0
- package/gu-repl.mjs +54 -51
- package/gu_browser.py +3 -3
- package/gu_free_budget.py +2 -2
- package/gu_gif.py +1 -1
- package/gu_hooks.py +1 -1
- package/gu_turn_notes.py +2 -2
- package/hooks-config.mjs +4 -4
- package/image-paste.mjs +8 -8
- package/install-path.mjs +1 -1
- package/log-level.mjs +1 -1
- package/log-policy.mjs +1 -1
- package/lsp-host.mjs +2 -2
- package/lsp-registry.mjs +9 -9
- package/mcp-command.mjs +1 -1
- package/mcp-config.mjs +5 -5
- package/model-doctor.mjs +10 -10
- package/mongo-mac.mjs +2 -2
- package/mongo-windows.mjs +3 -3
- package/package.json +2 -1
- package/platform-tools.mjs +3 -3
- package/poll-errors.mjs +2 -2
- package/prompt_scaffold_check.py +1 -1
- package/proxy-store.mjs +1 -1
- package/python-unix.mjs +6 -6
- package/python-windows.mjs +1 -1
- package/rag-selftest.mjs +2 -2
- package/resume-memory.mjs +1 -1
- package/scripts/image-paste-check.mjs +1 -1
- package/setup-fixes.mjs +4 -4
- package/shell-details.mjs +2 -2
- package/skill-install.mjs +9 -9
- package/skill-package.mjs +10 -10
- package/skill-source.mjs +4 -4
- package/skills-copy.mjs +15 -15
- package/skills.mjs +4 -4
- package/support-bundle.mjs +6 -6
- package/support-report.mjs +5 -5
- package/terminal-layout.mjs +1 -1
- package/turn-notes.mjs +2 -2
- package/version-check.mjs +1 -1
- package/workspace-revert.mjs +2 -2
package/gu-cli.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Gu local worker:
|
|
4
4
|
* - `gu-cli set <workerKey> [--api-base URL]`
|
|
5
|
-
* writes ~/.
|
|
5
|
+
* writes ~/.gu/worker.env
|
|
6
6
|
* - `gu-cli ws-ping-test` — POST worker ws-ping (needs GONEXT_* env)
|
|
7
7
|
* - `gu-cli simulate-chat [text]` — claim next chat job, push fake reply like the real worker (needs GONEXT_* env)
|
|
8
8
|
* - `gu-cli embed --model <mlx-embed-model> [--port 8085]` — run the MLX embeddings server (RAG); use a separate terminal
|
|
@@ -11,11 +11,12 @@
|
|
|
11
11
|
*/
|
|
12
12
|
// FIRST, before anything that could itself fail on an old runtime: npm only WARNS about
|
|
13
13
|
// `engines`, so an unsupported Node installs fine and then breaks somewhere unrelated.
|
|
14
|
+
import "./gu-home-move.mjs"; // GH #582: FIRST — moves the old home folder to ~/.gu once, before anything uses it
|
|
14
15
|
import { localModeNodeProblem, requireSupportedNode } from "./node-version.mjs";
|
|
15
16
|
// The brand accent, shared with the REPL — see accent.mjs.
|
|
16
17
|
import { accent } from "./accent.mjs";
|
|
17
18
|
import { installInternalLabel } from "./cli-token.mjs";
|
|
18
|
-
installInternalLabel(); // our own machines only (GU_INTERNAL_LABEL / ~/.
|
|
19
|
+
installInternalLabel(); // our own machines only (GU_INTERNAL_LABEL / ~/.gu/internal-label)
|
|
19
20
|
import { paint } from "./theme.mjs";
|
|
20
21
|
requireSupportedNode();
|
|
21
22
|
import { mkdir, mkdtemp, readFile, rm, stat, unlink, writeFile } from "node:fs/promises";
|
|
@@ -95,7 +96,7 @@ const WORKER_VERSION = (() => {
|
|
|
95
96
|
/** Avoid `node:child_process/promises` — not available on some Node builds / older runtimes. */
|
|
96
97
|
const execFile = promisify(execFileCallback);
|
|
97
98
|
|
|
98
|
-
const ENV_FILE = join(homedir(), ".
|
|
99
|
+
const ENV_FILE = join(homedir(), ".gu", "worker.env");
|
|
99
100
|
const preloadedWorkerKey = String(process.env.GONEXT_WORKER_KEY ?? "").trim();
|
|
100
101
|
const envFileLoad = dotenv.config({ path: ENV_FILE });
|
|
101
102
|
dotenv.config();
|
|
@@ -121,7 +122,7 @@ gu-cli
|
|
|
121
122
|
|
|
122
123
|
Usage:
|
|
123
124
|
gu-cli
|
|
124
|
-
gu-cli login [--api-base <url>] # browser sign-in → writes ~/.
|
|
125
|
+
gu-cli login [--api-base <url>] # browser sign-in → writes ~/.gu/worker.env
|
|
125
126
|
gu-cli token [<token>] # show this computer's gu token, or save one pasted from
|
|
126
127
|
# the Account page (Client Expert machines)
|
|
127
128
|
gu-cli setup --provider <name|url> --key <key> [--model <name>] # first-run key setup in one line (deepseek, claude, openai, kimi, qwen-…)
|
|
@@ -146,7 +147,7 @@ Usage:
|
|
|
146
147
|
gu-cli mongo migrate-from-docker # macOS: move a Docker MongoDB to native (Homebrew)
|
|
147
148
|
gu-cli clear [--scratch|--keep-history] [--yes] # back to a fresh machine (asks first)
|
|
148
149
|
gu-cli support # send the support team a request with gu's logs (masked) attached
|
|
149
|
-
gu-cli support --no-send # only make the logs zip (~/.
|
|
150
|
+
gu-cli support --no-send # only make the logs zip (~/.gu/support/) to email yourself
|
|
150
151
|
gu-cli fix-path [--check] # put npm's global folder on PATH (Windows: check, then ask)
|
|
151
152
|
# (what the install does, if npm skipped it)
|
|
152
153
|
gu-cli fix-powershell [--check] # Windows: drop PowerShell's built-in \`gu\` alias
|
|
@@ -198,7 +199,7 @@ async function setConfig() {
|
|
|
198
199
|
GONEXT_WORKER_KEY: workerKey,
|
|
199
200
|
GONEXT_POLL_MS: "500",
|
|
200
201
|
};
|
|
201
|
-
await mkdir(join(homedir(), ".
|
|
202
|
+
await mkdir(join(homedir(), ".gu"), { recursive: true });
|
|
202
203
|
await writeFile(
|
|
203
204
|
ENV_FILE,
|
|
204
205
|
`GONEXT_API_BASE=${next.GONEXT_API_BASE}\nGONEXT_WORKER_KEY=${next.GONEXT_WORKER_KEY}\nGONEXT_POLL_MS=${next.GONEXT_POLL_MS}\n`,
|
|
@@ -243,7 +244,7 @@ if (args[0] === "token") {
|
|
|
243
244
|
// another server is refused here rather than turning into a warning later.
|
|
244
245
|
// NOT `gu tokens`: that is token USAGE, and stays exactly as it was.
|
|
245
246
|
const ct = await import("./cli-token.mjs");
|
|
246
|
-
const dir = join(homedir(), ".
|
|
247
|
+
const dir = join(homedir(), ".gu");
|
|
247
248
|
const pk = ct.publicKey();
|
|
248
249
|
const pasted = String(args[1] ?? "").trim();
|
|
249
250
|
if (!pk) {
|
|
@@ -304,7 +305,7 @@ if (args[0] === "setup") {
|
|
|
304
305
|
env: process.env,
|
|
305
306
|
api: sc.localApi({ apiBase, workerKey }),
|
|
306
307
|
say: (line, tone) => (tone === "err" ? console.error : console.log)(line),
|
|
307
|
-
backupDir: join(homedir(), ".
|
|
308
|
+
backupDir: join(homedir(), ".gu", "preset-backups"),
|
|
308
309
|
host: hostname(),
|
|
309
310
|
apiReady: Boolean(apiBase && workerKey),
|
|
310
311
|
});
|
|
@@ -623,7 +624,7 @@ if (["start", "stop", "status", "logs", "restart"].includes(args[0])) {
|
|
|
623
624
|
console.error(
|
|
624
625
|
`[gu-worker] the server on port ${hit.port} (pid ${hit.pid}) isn't writing to a log file —\n` +
|
|
625
626
|
" its output goes to the terminal that started it. To get a readable log, restart it\n" +
|
|
626
|
-
" with `gu-cli doctor` (gu logs it to ~/.
|
|
627
|
+
" with `gu-cli doctor` (gu logs it to ~/.gu/model.log), or redirect it\n" +
|
|
627
628
|
` yourself: mlx_lm.server --model <dir> --port ${hit.port} >> ~/mlx-${hit.port}.log 2>&1`
|
|
628
629
|
);
|
|
629
630
|
process.exit(1);
|
|
@@ -714,7 +715,7 @@ const currentApiVersion = async () => {
|
|
|
714
715
|
const { readdir } = await import("node:fs/promises");
|
|
715
716
|
const { existsSync } = await import("node:fs");
|
|
716
717
|
const { pickUsableVersion } = await import("./version-check.mjs");
|
|
717
|
-
const base = join(homedir(), ".
|
|
718
|
+
const base = join(homedir(), ".gu", "api");
|
|
718
719
|
const dirs = await readdir(base).catch(() => []);
|
|
719
720
|
// NEWEST USABLE, NOT NEWEST (task #181). This used to sort and pop, with no check that the
|
|
720
721
|
// chosen version could actually run — so an interrupted upgrade (source unpacked,
|
|
@@ -937,7 +938,7 @@ const currentApiVersion = async () => {
|
|
|
937
938
|
// …and distinguish "nothing installed" from "installed but not runnable": the second has
|
|
938
939
|
// a fix (`api upgrade` finishes the dependencies) and the first does not.
|
|
939
940
|
const { readdir } = await import("node:fs/promises");
|
|
940
|
-
const anyDirs = await readdir(join(homedir(), ".
|
|
941
|
+
const anyDirs = await readdir(join(homedir(), ".gu", "api")).catch(() => []);
|
|
941
942
|
console.log(
|
|
942
943
|
anyDirs.length
|
|
943
944
|
? `\n No RUNNABLE API here — ${anyDirs.join(", ")} installed but missing ` +
|
|
@@ -946,7 +947,7 @@ const currentApiVersion = async () => {
|
|
|
946
947
|
);
|
|
947
948
|
process.exit(1);
|
|
948
949
|
}
|
|
949
|
-
const apiDir = join(homedir(), ".
|
|
950
|
+
const apiDir = join(homedir(), ".gu", "api", version);
|
|
950
951
|
console.log(`\n Starting the API (${version})…`);
|
|
951
952
|
await ce.startLocalApi({ apiDir });
|
|
952
953
|
const health = await ce.waitForApi({});
|
|
@@ -1012,7 +1013,7 @@ const currentApiVersion = async () => {
|
|
|
1012
1013
|
console.log("\n No runnable API is installed here. Try: gu-cli api upgrade\n");
|
|
1013
1014
|
process.exit(1);
|
|
1014
1015
|
}
|
|
1015
|
-
const apiDir = join(homedir(), ".
|
|
1016
|
+
const apiDir = join(homedir(), ".gu", "api", version);
|
|
1016
1017
|
console.log(` Starting the API (${version})…`);
|
|
1017
1018
|
await ce.startLocalApi({ apiDir });
|
|
1018
1019
|
const health = await ce.waitForApi({});
|
|
@@ -1344,7 +1345,7 @@ if (args[0] === "clear" || args[0] === "reset") {
|
|
|
1344
1345
|
});
|
|
1345
1346
|
let apiDirForDrop = "";
|
|
1346
1347
|
if (dbItem) {
|
|
1347
|
-
const base = join(homedir(), ".
|
|
1348
|
+
const base = join(homedir(), ".gu", "api");
|
|
1348
1349
|
const versions = (await _readdir(base).catch(() => [])).sort((a, b) =>
|
|
1349
1350
|
a.localeCompare(b, undefined, { numeric: true, sensitivity: "base" })
|
|
1350
1351
|
);
|
|
@@ -1427,7 +1428,7 @@ if (args[0] === "clear" || args[0] === "reset") {
|
|
|
1427
1428
|
}
|
|
1428
1429
|
}
|
|
1429
1430
|
}
|
|
1430
|
-
// The DATABASE first, while ~/.
|
|
1431
|
+
// The DATABASE first, while ~/.gu/api still exists — dropping it needs the mongodb
|
|
1431
1432
|
// driver that ships inside the downloaded API, and clearState is about to delete that folder.
|
|
1432
1433
|
// GitHub #125: the API's mongodb 7 driver needs Node 20.19+. On Node 18 it died with a bare
|
|
1433
1434
|
// "crypto is not defined"; say what is wrong and what to do instead.
|
|
@@ -1900,13 +1901,13 @@ if (args[0] === "prepare") {
|
|
|
1900
1901
|
const { prepareLocalStack } = await import("./client-expert-run.mjs");
|
|
1901
1902
|
stack = await prepareLocalStack({});
|
|
1902
1903
|
}
|
|
1903
|
-
// One plain line on screen; the reason something was left goes to ~/.
|
|
1904
|
+
// One plain line on screen; the reason something was left goes to ~/.gu/logs/prepare.log (GH #564), for support.
|
|
1904
1905
|
if (!(depsOk && stack.ok)) {
|
|
1905
1906
|
try {
|
|
1906
1907
|
const { appendFileSync, mkdirSync } = await import("node:fs");
|
|
1907
1908
|
const { join } = await import("node:path");
|
|
1908
1909
|
const { homedir } = await import("node:os");
|
|
1909
|
-
const dir = join(homedir(), ".
|
|
1910
|
+
const dir = join(homedir(), ".gu", "logs");
|
|
1910
1911
|
mkdirSync(dir, { recursive: true });
|
|
1911
1912
|
appendFileSync(join(dir, "prepare.log"), `${new Date().toISOString()} agent=${out.agentLibs ?? "?"} python=${out.python3 ?? "?"} api=${stack.ok ? "ok" : "left"}: ${stack.skipped || "-"}\n`);
|
|
1912
1913
|
} catch { /* the log is a convenience */ }
|
|
@@ -1942,7 +1943,7 @@ if (args[0] === "doctor") {
|
|
|
1942
1943
|
* machine that will never use one.
|
|
1943
1944
|
*
|
|
1944
1945
|
* Two configuration signals, each already authoritative elsewhere:
|
|
1945
|
-
* · clientMode in ~/.
|
|
1946
|
+
* · clientMode in ~/.gu prefs — the flag `doctor --client` itself sets
|
|
1946
1947
|
* · a LOCAL api base — how the REPL identifies Client Expert (isLocalApiBase, and the
|
|
1947
1948
|
* banner's "client expert (API + database here · models remote)")
|
|
1948
1949
|
* Either one means the models live somewhere else.
|
|
@@ -2161,7 +2162,7 @@ const localHealthConcurrency = 4;
|
|
|
2161
2162
|
// 4, up from 2 (2026-09-25, the user's call: "make it 4 by default"). The 2 was sized for ONE
|
|
2162
2163
|
// local GPU shared by coder and embedder; production now targets hosted models, where the
|
|
2163
2164
|
// limit is this machine's RAM/CPU and the provider's rate limit, not a GPU queue. Still
|
|
2164
|
-
// bounded, and GONEXT_AGENT_CONCURRENCY in ~/.
|
|
2165
|
+
// bounded, and GONEXT_AGENT_CONCURRENCY in ~/.gu/worker.env still overrides it (set 1-2
|
|
2165
2166
|
// for a local Ollama/MLX coder).
|
|
2166
2167
|
const DEFAULT_AGENT_CONCURRENCY = 4;
|
|
2167
2168
|
const agentConcurrency = Math.max(
|
|
@@ -2301,7 +2302,7 @@ if (args[0] === "workspace") {
|
|
|
2301
2302
|
// gu-cli workspace add <path> [--name <name>] [--allow-run]
|
|
2302
2303
|
// gu-cli workspace remove <path-or-name>
|
|
2303
2304
|
// gu-cli workspace list
|
|
2304
|
-
const wsFile = join(homedir(), ".
|
|
2305
|
+
const wsFile = join(homedir(), ".gu", "workspaces.json");
|
|
2305
2306
|
const readWs = async () => {
|
|
2306
2307
|
try {
|
|
2307
2308
|
const raw = JSON.parse(await readFile(wsFile, "utf8"));
|
|
@@ -2360,7 +2361,7 @@ if (args[0] === "workspace") {
|
|
|
2360
2361
|
// gu-cli workspace revert <runId> → a specific run (see backups dir)
|
|
2361
2362
|
const { readdirSync, existsSync: ex } = await import("node:fs");
|
|
2362
2363
|
const { copyFile, unlink, rmdir } = await import("node:fs/promises");
|
|
2363
|
-
const backupsRoot = join(homedir(), ".
|
|
2364
|
+
const backupsRoot = join(homedir(), ".gu", "workspace-backups");
|
|
2364
2365
|
let runId = args[2] || "";
|
|
2365
2366
|
if (!runId) {
|
|
2366
2367
|
const runs = (() => {
|
|
@@ -4241,7 +4242,7 @@ async function runAgentChatJob(job) {
|
|
|
4241
4242
|
// Same isolation the web path gets: an empty scratch folder, no run_command. The turn
|
|
4242
4243
|
// still works for anything that isn't local-file work, and the agent tells the user why.
|
|
4243
4244
|
remoteWorkspace = terminalActiveWs;
|
|
4244
|
-
const webWork = join(homedir(), ".
|
|
4245
|
+
const webWork = join(homedir(), ".gu", "web-agent-work");
|
|
4245
4246
|
await mkdir(webWork, { recursive: true }).catch(() => {});
|
|
4246
4247
|
jobWorkspaces = [{ name: "remote-client", path: webWork, allowRun: false }];
|
|
4247
4248
|
jobActiveWorkspace = webWork;
|
|
@@ -4250,7 +4251,7 @@ async function runAgentChatJob(job) {
|
|
|
4250
4251
|
" machine — the terminal is a CLIENT. Running without workspace file tools."
|
|
4251
4252
|
);
|
|
4252
4253
|
} else if (terminalActiveWs) {
|
|
4253
|
-
jobWorkspaces = await readFile(join(homedir(), ".
|
|
4254
|
+
jobWorkspaces = await readFile(join(homedir(), ".gu", "workspaces.json"), "utf8")
|
|
4254
4255
|
.then((raw) => {
|
|
4255
4256
|
const parsed = JSON.parse(raw);
|
|
4256
4257
|
return Array.isArray(parsed?.workspaces) ? parsed.workspaces : [];
|
|
@@ -4258,7 +4259,7 @@ async function runAgentChatJob(job) {
|
|
|
4258
4259
|
.catch(() => []);
|
|
4259
4260
|
jobActiveWorkspace = terminalActiveWs;
|
|
4260
4261
|
} else {
|
|
4261
|
-
const webWork = join(homedir(), ".
|
|
4262
|
+
const webWork = join(homedir(), ".gu", "web-agent-work");
|
|
4262
4263
|
await mkdir(webWork, { recursive: true }).catch(() => {});
|
|
4263
4264
|
jobWorkspaces = [{ name: "web", path: webWork, allowRun: false }];
|
|
4264
4265
|
jobActiveWorkspace = webWork;
|
|
@@ -4425,7 +4426,7 @@ async function runAgentChatJob(job) {
|
|
|
4425
4426
|
// secrets away from anything run_command executes, and an inherited HTTPS_PROXY would
|
|
4426
4427
|
// hand the password to every build script the agent runs.
|
|
4427
4428
|
...(proxyDialString ? { proxyUrl: proxyDialString } : {}),
|
|
4428
|
-
// RAG storage backend for this folder (task #97): "local" = on disk under ~/.
|
|
4429
|
+
// RAG storage backend for this folder (task #97): "local" = on disk under ~/.gu
|
|
4429
4430
|
// (per-folder, no S3 creds), else "cloud" (the user's S3). Terminal sends the folder's
|
|
4430
4431
|
// choice; web never sets it → "cloud". python re-validates and defaults to cloud.
|
|
4431
4432
|
ragMode: payload?.ragMode === "local" ? "local" : "cloud",
|
|
@@ -5439,7 +5440,7 @@ async function runLocalHealthJob(job) {
|
|
|
5439
5440
|
: undefined,
|
|
5440
5441
|
// Registered coding workspaces (names/paths only — no secrets) so the web can
|
|
5441
5442
|
// show which folders the agent may read/edit on this Mac.
|
|
5442
|
-
workspaces: await readFile(join(homedir(), ".
|
|
5443
|
+
workspaces: await readFile(join(homedir(), ".gu", "workspaces.json"), "utf8")
|
|
5443
5444
|
.then((raw) => {
|
|
5444
5445
|
const parsed = JSON.parse(raw);
|
|
5445
5446
|
return Array.isArray(parsed?.workspaces)
|
|
@@ -5532,7 +5533,7 @@ const agentPool = createTaskPool({ limit: agentConcurrency });
|
|
|
5532
5533
|
* immediately instead of leaving a silence for a timer to discover.
|
|
5533
5534
|
*/
|
|
5534
5535
|
const inFlightJobs = new Map(); // jobId -> jobType, for the message
|
|
5535
|
-
// …and PUBLISHED to ~/.
|
|
5536
|
+
// …and PUBLISHED to ~/.gu/worker-busy.json (task #209 req 1), because the process that
|
|
5536
5537
|
// decides to stop this one is a different process and cannot read the map above. Best-effort
|
|
5537
5538
|
// and fire-and-forget: the bookkeeping must never delay or break the turn it describes.
|
|
5538
5539
|
const publishBusy = () => {
|
|
@@ -5755,7 +5756,7 @@ async function startLogPolicy() {
|
|
|
5755
5756
|
// THE LEVEL FILTER, last so it is the first thing a line meets (see log-level.mjs).
|
|
5756
5757
|
installLevelFilter(logLevelNow);
|
|
5757
5758
|
await logPolicy.refresh();
|
|
5758
|
-
const runLogs = join(homedir(), ".
|
|
5759
|
+
const runLogs = join(homedir(), ".gu", "run-logs");
|
|
5759
5760
|
const housekeep = async () => {
|
|
5760
5761
|
if (logLevelNow() === "minimal") {
|
|
5761
5762
|
await lp.rotateIfLarge(LOG_FILE);
|
package/gu-home-move.mjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// gu's folder is .gu (GH #582). It was .gonext — a name from before gu, and it confused people.
|
|
2
|
+
//
|
|
3
|
+
// A NEW install only ever reads and writes .gu: nothing here runs for it.
|
|
4
|
+
// A machine that already has .gonext gets it MOVED ONCE, at the first start of the new gu — renamed, so nothing
|
|
5
|
+
// is copied and nothing is lost:
|
|
6
|
+
// · ~/.gonext → ~/.gu (token, database, keys, settings, logs, gu's Python), and ~/.gonext is left as a LINK to
|
|
7
|
+
// ~/.gu, so what other programs saved (Claude Code's memory hooks, an old gu worker still running) keeps working.
|
|
8
|
+
// · saved absolute paths in gu's own files (mcp.json, settings.json, the Python venvs' scripts) and the entries
|
|
9
|
+
// gu's skills added to Claude Code (~/.claude.json, ~/.claude/settings.json) are rewritten to ~/.gu.
|
|
10
|
+
// · the project folder gu starts in (and its git root): ./.gonext → ./.gu. No link there — it is the project's.
|
|
11
|
+
//
|
|
12
|
+
// Imported FIRST by both entry points (gu-repl.mjs, gu-cli.mjs): an ES module's imports run in order, so this
|
|
13
|
+
// runs before any other module can create ~/.gu.
|
|
14
|
+
import { existsSync, lstatSync, readdirSync, readFileSync, renameSync, rmdirSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { homedir } from "node:os";
|
|
16
|
+
import { dirname, join } from "node:path";
|
|
17
|
+
|
|
18
|
+
const OLD = ".gonext";
|
|
19
|
+
const NEW = ".gu";
|
|
20
|
+
|
|
21
|
+
const isRealDir = (p) => {
|
|
22
|
+
try {
|
|
23
|
+
const s = lstatSync(p);
|
|
24
|
+
return s.isDirectory() && !s.isSymbolicLink();
|
|
25
|
+
} catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Move `<base>/.gonext` to `<base>/.gu`. Both there → what .gu lacks is moved in, and an emptied .gonext removed. */
|
|
31
|
+
export function moveFolder(base) {
|
|
32
|
+
const from = join(base, OLD);
|
|
33
|
+
const to = join(base, NEW);
|
|
34
|
+
if (!isRealDir(from)) return { moved: false };
|
|
35
|
+
if (!existsSync(to)) {
|
|
36
|
+
renameSync(from, to);
|
|
37
|
+
return { moved: true, from, to };
|
|
38
|
+
}
|
|
39
|
+
if (!isRealDir(to)) return { moved: false, error: `${to} is not a folder` };
|
|
40
|
+
const left = mergeInto(from, to);
|
|
41
|
+
return { moved: true, from, to, left };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Both there — a gu that was already running wrote a few things to .gu first (seen on the owner's Mac, GH #582):
|
|
46
|
+
* what .gu lacks is moved in; a folder on both sides is merged inside; a log on both sides keeps the old lines
|
|
47
|
+
* first. Any other clash keeps .gu's copy and leaves the old one where it was — nothing is ever overwritten.
|
|
48
|
+
* → the names left behind (empty → the old folder is removed).
|
|
49
|
+
*/
|
|
50
|
+
function mergeInto(from, to) {
|
|
51
|
+
for (const name of readdirSync(from)) {
|
|
52
|
+
const a = join(from, name);
|
|
53
|
+
const b = join(to, name);
|
|
54
|
+
if (!existsSync(b)) renameSync(a, b);
|
|
55
|
+
else if (isRealDir(a) && isRealDir(b)) {
|
|
56
|
+
if (!mergeInto(a, b).length) continue;
|
|
57
|
+
} else if (/\.log$/.test(name) && statSync(a).isFile() && statSync(b).isFile()) {
|
|
58
|
+
writeFileSync(a, readFileSync(b), { flag: "a" });
|
|
59
|
+
renameSync(a, b);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const left = readdirSync(from);
|
|
63
|
+
if (!left.length) rmdirSync(from);
|
|
64
|
+
return left;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The old prefix in every way a saved file can spell it: plain, and Windows' backslashes (also JSON-escaped). */
|
|
68
|
+
function prefixes(home) {
|
|
69
|
+
const out = [[join(home, OLD), join(home, NEW)]];
|
|
70
|
+
if (home.includes("\\")) {
|
|
71
|
+
const esc = (s) => s.replace(/\\/g, "\\\\");
|
|
72
|
+
out.push([esc(join(home, OLD)), esc(join(home, NEW))]);
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function rewriteFile(file, pairs) {
|
|
78
|
+
try {
|
|
79
|
+
const st = statSync(file);
|
|
80
|
+
if (!st.isFile() || st.size > 8 * 1024 * 1024) return false;
|
|
81
|
+
const s = readFileSync(file, "utf8");
|
|
82
|
+
if (s.includes("\u0000")) return false; // not text
|
|
83
|
+
let t = s;
|
|
84
|
+
for (const [a, b] of pairs) t = t.split(a + "/").join(b + "/").split(a + "\\").join(b + "\\").split(a + '"').join(b + '"');
|
|
85
|
+
if (t === s) return false;
|
|
86
|
+
writeFileSync(file, t, { mode: st.mode });
|
|
87
|
+
return true;
|
|
88
|
+
} catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Python venvs keep their own absolute path (script shebangs, activate, pyvenv.cfg): the venvs under ~/.gu. */
|
|
94
|
+
function venvFiles(root, depth = 3) {
|
|
95
|
+
const out = [];
|
|
96
|
+
const walk = (dir, d) => {
|
|
97
|
+
let names;
|
|
98
|
+
try {
|
|
99
|
+
names = readdirSync(dir, { withFileTypes: true });
|
|
100
|
+
} catch {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (names.some((n) => n.name === "pyvenv.cfg")) {
|
|
104
|
+
out.push(join(dir, "pyvenv.cfg"));
|
|
105
|
+
for (const bin of ["bin", "Scripts"]) {
|
|
106
|
+
try {
|
|
107
|
+
for (const f of readdirSync(join(dir, bin), { withFileTypes: true })) if (f.isFile()) out.push(join(dir, bin, f.name));
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (d <= 0) return;
|
|
113
|
+
for (const n of names) if (n.isDirectory() && !n.isSymbolicLink() && n.name !== "node_modules") walk(join(dir, n.name), d - 1);
|
|
114
|
+
};
|
|
115
|
+
walk(root, depth);
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** gu's own text files that hold absolute paths: the top of ~/.gu, its skills' and backups' JSON, the venvs. */
|
|
120
|
+
export function rewriteSavedPaths(home) {
|
|
121
|
+
const pairs = prefixes(home);
|
|
122
|
+
const root = join(home, NEW);
|
|
123
|
+
const files = [];
|
|
124
|
+
const topOf = (dir, re) => {
|
|
125
|
+
try {
|
|
126
|
+
for (const f of readdirSync(dir, { withFileTypes: true })) if (f.isFile() && re.test(f.name)) files.push(join(dir, f.name));
|
|
127
|
+
} catch {}
|
|
128
|
+
};
|
|
129
|
+
topOf(root, /\.(json|env|txt|md|sh|ps1|cmd)$|^[a-z-]+$/i);
|
|
130
|
+
for (const sub of ["skills", "workspace-backups"]) {
|
|
131
|
+
try {
|
|
132
|
+
for (const d of readdirSync(join(root, sub), { withFileTypes: true })) if (d.isDirectory()) topOf(join(root, sub, d.name), /\.json$/i);
|
|
133
|
+
} catch {}
|
|
134
|
+
}
|
|
135
|
+
files.push(...venvFiles(root));
|
|
136
|
+
// Entries gu's skills and memory runtime added to Claude Code. Only gu's old path is touched in them.
|
|
137
|
+
files.push(join(home, ".claude.json"), join(home, ".claude", "settings.json"));
|
|
138
|
+
return files.filter((f) => rewriteFile(f, pairs)).length;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The project's root: the nearest folder up from `cwd` with .git, else `cwd`. Never above (or at) home. */
|
|
142
|
+
export function projectRoots(cwd, home) {
|
|
143
|
+
const roots = new Set();
|
|
144
|
+
if (cwd && cwd !== home) roots.add(cwd);
|
|
145
|
+
for (let d = cwd; d && d !== home && d !== dirname(d); d = dirname(d)) {
|
|
146
|
+
if (existsSync(join(d, ".git"))) {
|
|
147
|
+
roots.add(d);
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return [...roots];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The one-time move of ~/.gonext. → what it did, for the line gu says. Never throws. */
|
|
155
|
+
export function moveToGu({ home = homedir() } = {}) {
|
|
156
|
+
const said = [];
|
|
157
|
+
try {
|
|
158
|
+
const r = moveFolder(home);
|
|
159
|
+
if (r.moved) {
|
|
160
|
+
if (!r.left?.length) {
|
|
161
|
+
try {
|
|
162
|
+
symlinkSync(join(home, NEW), join(home, OLD), process.platform === "win32" ? "junction" : "dir");
|
|
163
|
+
} catch {}
|
|
164
|
+
}
|
|
165
|
+
const n = rewriteSavedPaths(home);
|
|
166
|
+
said.push(`gu's folder is now ~/${NEW} (moved from ~/${OLD}${n ? `; ${n} saved path${n === 1 ? "" : "s"} updated` : ""}).`);
|
|
167
|
+
}
|
|
168
|
+
} catch (e) {
|
|
169
|
+
said.push(`gu could not move ~/${OLD} to ~/${NEW}: ${e?.message ?? e}. Close every other gu window and run gu again.`);
|
|
170
|
+
return { said, failed: true };
|
|
171
|
+
}
|
|
172
|
+
return { said };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `gu` in a project: its ./.gonext (and its git root's) → ./.gu. → the lines to say. Never throws. */
|
|
176
|
+
export function moveProjectToGu(cwd = process.cwd(), home = homedir()) {
|
|
177
|
+
const said = [];
|
|
178
|
+
for (const root of projectRoots(cwd, home)) {
|
|
179
|
+
try {
|
|
180
|
+
if (moveFolder(root).moved) said.push(`This project's ${OLD} folder is now ${NEW}.`);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
said.push(`gu could not rename ${join(root, OLD)} to ${NEW}: ${e?.message ?? e}.`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return said;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Runs on import, before anything else. GU_NO_HOME_MOVE=1 turns it off (tests that import gu modules).
|
|
189
|
+
if (process.env.GU_NO_HOME_MOVE !== "1") {
|
|
190
|
+
const { said, failed } = moveToGu();
|
|
191
|
+
for (const l of said) process.stderr.write(` ${l}\n`);
|
|
192
|
+
if (failed) process.exit(1);
|
|
193
|
+
}
|