nemu-cli 1.0.0 → 1.2.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.
- package/CHANGELOG.md +27 -0
- package/README.md +55 -1
- package/dist/auth/token.js +115 -0
- package/dist/clients/hooks.js +131 -0
- package/dist/clients/paths.js +17 -0
- package/dist/clients/registry.js +74 -0
- package/dist/commands/client.js +19 -0
- package/dist/commands/context.js +65 -0
- package/dist/commands/hook.js +312 -0
- package/dist/commands/load.js +328 -0
- package/dist/commands/login.js +14 -1
- package/dist/commands/mcp.js +210 -0
- package/dist/commands/settings.js +23 -0
- package/dist/commands/setup.js +104 -0
- package/dist/commands/status.js +63 -0
- package/dist/commands/token.js +16 -0
- package/dist/commands/uninstall.js +34 -0
- package/dist/config.js +52 -20
- package/dist/context/api.js +99 -0
- package/dist/context/project.js +58 -0
- package/dist/context/redact.js +32 -0
- package/dist/context/snapshot.js +85 -0
- package/dist/context/stale.js +70 -0
- package/dist/device.js +43 -0
- package/dist/errors.js +24 -0
- package/dist/git.js +39 -0
- package/dist/http.js +61 -0
- package/dist/index.js +72 -9
- package/dist/report.js +79 -0
- package/dist/session.js +11 -6
- package/dist/system.js +50 -0
- package/dist/ui.js +20 -0
- package/dist/update.js +141 -0
- package/dist/version.js +1 -0
- package/package.json +9 -4
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { config_dir, read_credentials, write_private } from "../config.js";
|
|
5
|
+
import { repository } from "../git.js";
|
|
6
|
+
import { open_project } from "./api.js";
|
|
7
|
+
const CACHE_TTL_MS = 6 * 3_600_000;
|
|
8
|
+
const cache_schema = z.record(z.string(), z.object({ id: z.string(), name: z.string(), api_url: z.string(), saved_at: z.number() }));
|
|
9
|
+
function cache_path() {
|
|
10
|
+
return join(config_dir(), "projects.json");
|
|
11
|
+
}
|
|
12
|
+
async function read_cache() {
|
|
13
|
+
try {
|
|
14
|
+
const parsed = cache_schema.safeParse(JSON.parse(await readFile(cache_path(), "utf-8")));
|
|
15
|
+
return parsed.success ? parsed.data : {};
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function cache_key(info) {
|
|
22
|
+
return info.root_commit !== undefined ? `commit:${info.root_commit}` : `path:${info.root}`;
|
|
23
|
+
}
|
|
24
|
+
export async function current_project(cwd, use_cache = true) {
|
|
25
|
+
const info = await repository(cwd);
|
|
26
|
+
const credentials = await read_credentials();
|
|
27
|
+
const cache = await read_cache();
|
|
28
|
+
const key = cache_key(info);
|
|
29
|
+
const cached = cache[key];
|
|
30
|
+
if (use_cache &&
|
|
31
|
+
cached !== undefined &&
|
|
32
|
+
credentials !== null &&
|
|
33
|
+
cached.api_url === credentials.api_url &&
|
|
34
|
+
Date.now() - cached.saved_at < CACHE_TTL_MS) {
|
|
35
|
+
return { id: cached.id, name: cached.name, repository: info };
|
|
36
|
+
}
|
|
37
|
+
const project = await open_project({
|
|
38
|
+
name: info.name.slice(0, 80),
|
|
39
|
+
...(info.root_commit !== undefined ? { root_commit: info.root_commit } : {}),
|
|
40
|
+
...(info.remote_url !== undefined ? { remote_url: info.remote_url.slice(0, 500) } : {}),
|
|
41
|
+
});
|
|
42
|
+
if (credentials !== null) {
|
|
43
|
+
cache[key] = {
|
|
44
|
+
id: project.id,
|
|
45
|
+
name: project.name,
|
|
46
|
+
api_url: credentials.api_url,
|
|
47
|
+
saved_at: Date.now(),
|
|
48
|
+
};
|
|
49
|
+
await write_private(cache_path(), JSON.stringify(cache, null, 2)).catch(() => undefined);
|
|
50
|
+
}
|
|
51
|
+
return { id: project.id, name: project.name, repository: info };
|
|
52
|
+
}
|
|
53
|
+
export async function forget_cached_project(cwd) {
|
|
54
|
+
const info = await repository(cwd);
|
|
55
|
+
const cache = await read_cache();
|
|
56
|
+
delete cache[cache_key(info)];
|
|
57
|
+
await write_private(cache_path(), JSON.stringify(cache, null, 2)).catch(() => undefined);
|
|
58
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const PATTERNS = [
|
|
2
|
+
/sk_[a-z]{5}-[a-z]{5}\.[A-Za-z0-9]{40}/g,
|
|
3
|
+
/Bearer\s+[A-Za-z0-9._~+/=-]{8,}/gi,
|
|
4
|
+
/eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}(?:\.[A-Za-z0-9_-]+)?/g,
|
|
5
|
+
/(password|secret|token|api_key|apikey)=\S+/gi,
|
|
6
|
+
/(postgres(?:ql)?|rediss?|mysql|mongodb):\/\/[^\s:@/]*:[^\s:@/]+@/gi,
|
|
7
|
+
/sk-ant-[A-Za-z0-9_-]{20,}/g,
|
|
8
|
+
/sk-(?:proj-)?[A-Za-z0-9_-]{20,}/g,
|
|
9
|
+
/AKIA[0-9A-Z]{16}/g,
|
|
10
|
+
/AIza[0-9A-Za-z_-]{35}/g,
|
|
11
|
+
/gh[pousr]_[A-Za-z0-9]{36,}/g,
|
|
12
|
+
/github_pat_[A-Za-z0-9_]{40,}/g,
|
|
13
|
+
/xox[baprs]-[A-Za-z0-9-]{10,}/g,
|
|
14
|
+
/(?:sk|rk)_live_[A-Za-z0-9]{20,}/g,
|
|
15
|
+
/[A-Za-z0-9]{2,}_live_[A-Za-z0-9]{20,}/g,
|
|
16
|
+
/glpat-[A-Za-z0-9_-]{20,}/g,
|
|
17
|
+
/npm_[A-Za-z0-9]{36}/g,
|
|
18
|
+
/hf_[A-Za-z0-9]{30,}/g,
|
|
19
|
+
/SG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/g,
|
|
20
|
+
/nctx_[A-Za-z0-9]{40}/g,
|
|
21
|
+
/rt_[A-Za-z0-9_-]{40,}/g,
|
|
22
|
+
/-----BEGIN (?:RSA |EC |OPENSSH |DSA |PGP )?PRIVATE KEY-----[\s\S]*?(?:-----END (?:RSA |EC |OPENSSH |DSA |PGP )?PRIVATE KEY-----|$)/g,
|
|
23
|
+
/^[A-Z][A-Z0-9_]*(?:SECRET|TOKEN|PASSWORD|PASSWD|API_KEY|APIKEY|PRIVATE_KEY|ACCESS_KEY)[A-Z0-9_]*\s*=\s*\S+/gm,
|
|
24
|
+
];
|
|
25
|
+
export function redact(text) {
|
|
26
|
+
return PATTERNS.reduce((result, pattern) => result.replace(pattern, "[redacted]"), text);
|
|
27
|
+
}
|
|
28
|
+
export function strip_home(text) {
|
|
29
|
+
return text
|
|
30
|
+
.replace(/(\/Users|\/home)\/[^/\s:'"]+/g, "~")
|
|
31
|
+
.replace(/[A-Za-z]:\\Users\\[^\\\s:'"]+/g, "~");
|
|
32
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { redact } from "./redact.js";
|
|
4
|
+
const SKIPPED = new Set([
|
|
5
|
+
".git",
|
|
6
|
+
"node_modules",
|
|
7
|
+
".next",
|
|
8
|
+
"dist",
|
|
9
|
+
"build",
|
|
10
|
+
"out",
|
|
11
|
+
"coverage",
|
|
12
|
+
"target",
|
|
13
|
+
"vendor",
|
|
14
|
+
".venv",
|
|
15
|
+
"venv",
|
|
16
|
+
"__pycache__",
|
|
17
|
+
".turbo",
|
|
18
|
+
".cache",
|
|
19
|
+
".idea",
|
|
20
|
+
".vscode",
|
|
21
|
+
]);
|
|
22
|
+
const KEY_FILES = [
|
|
23
|
+
"AGENTS.md",
|
|
24
|
+
"CLAUDE.md",
|
|
25
|
+
"README.md",
|
|
26
|
+
"package.json",
|
|
27
|
+
"pyproject.toml",
|
|
28
|
+
"Cargo.toml",
|
|
29
|
+
"go.mod",
|
|
30
|
+
];
|
|
31
|
+
const MAX_TREE_LINES = 60;
|
|
32
|
+
const MAX_FILE_CHARS = 600;
|
|
33
|
+
const MAX_SNAPSHOT_CHARS = 2400;
|
|
34
|
+
async function tree(root, depth) {
|
|
35
|
+
const lines = [];
|
|
36
|
+
async function walk(path, prefix, level) {
|
|
37
|
+
if (level > depth || lines.length >= MAX_TREE_LINES)
|
|
38
|
+
return;
|
|
39
|
+
const items = await readdir(path, { withFileTypes: true }).catch(() => []);
|
|
40
|
+
const visible = items
|
|
41
|
+
.filter((item) => !SKIPPED.has(item.name) && !item.name.startsWith(".env"))
|
|
42
|
+
.filter((item) => !item.name.startsWith("._") && item.name !== ".DS_Store")
|
|
43
|
+
.filter((item) => !(item.name.startsWith(".") && item.isDirectory()))
|
|
44
|
+
.sort((left, right) => left.isDirectory() === right.isDirectory()
|
|
45
|
+
? left.name.localeCompare(right.name)
|
|
46
|
+
: left.isDirectory()
|
|
47
|
+
? -1
|
|
48
|
+
: 1);
|
|
49
|
+
for (const item of visible) {
|
|
50
|
+
if (lines.length >= MAX_TREE_LINES)
|
|
51
|
+
return;
|
|
52
|
+
lines.push(`${prefix}${item.name}${item.isDirectory() ? "/" : ""}`);
|
|
53
|
+
if (item.isDirectory())
|
|
54
|
+
await walk(join(path, item.name), `${prefix} `, level + 1);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
await walk(root, "", 1);
|
|
58
|
+
return lines;
|
|
59
|
+
}
|
|
60
|
+
async function head_of(path) {
|
|
61
|
+
try {
|
|
62
|
+
const text = await readFile(path, "utf-8");
|
|
63
|
+
const trimmed = text.trim();
|
|
64
|
+
if (trimmed.length === 0)
|
|
65
|
+
return null;
|
|
66
|
+
return redact(trimmed.slice(0, MAX_FILE_CHARS));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export async function snapshot(root) {
|
|
73
|
+
const lines = await tree(root, 2);
|
|
74
|
+
const sections = [];
|
|
75
|
+
if (lines.length > 0) {
|
|
76
|
+
sections.push(`Files (top two levels${lines.length >= MAX_TREE_LINES ? ", cut short" : ""}):\n${lines.join("\n")}`);
|
|
77
|
+
}
|
|
78
|
+
for (const name of KEY_FILES) {
|
|
79
|
+
const head = await head_of(join(root, name));
|
|
80
|
+
if (head !== null)
|
|
81
|
+
sections.push(`${name} (start):\n${head}`);
|
|
82
|
+
}
|
|
83
|
+
const text = sections.join("\n\n");
|
|
84
|
+
return text.length > MAX_SNAPSHOT_CHARS ? `${text.slice(0, MAX_SNAPSHOT_CHARS)}\n...` : text;
|
|
85
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { access } from "node:fs/promises";
|
|
2
|
+
import { isAbsolute, join, normalize } from "node:path";
|
|
3
|
+
import { run } from "../system.js";
|
|
4
|
+
const MAX_GIT_CHECKS = 40;
|
|
5
|
+
const PATH_LIKE = /^[\w@.\-/\\]+$/;
|
|
6
|
+
async function exists(path) {
|
|
7
|
+
try {
|
|
8
|
+
await access(path);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function candidates(loaded) {
|
|
16
|
+
const entries = [
|
|
17
|
+
...(loaded.handoff !== null && loaded.handoff !== undefined ? [loaded.handoff] : []),
|
|
18
|
+
...(loaded.pinned ?? []),
|
|
19
|
+
].map((entry) => ({ name: entry.name, stale: entry.stale, refs: entry.refs, commit: entry.commit }));
|
|
20
|
+
const indexed = (loaded.index ?? []).map((entry) => ({
|
|
21
|
+
name: entry.name,
|
|
22
|
+
stale: entry.stale,
|
|
23
|
+
refs: entry.refs ?? [],
|
|
24
|
+
commit: entry.commit ?? null,
|
|
25
|
+
}));
|
|
26
|
+
return [...entries, ...indexed];
|
|
27
|
+
}
|
|
28
|
+
async function ref_missing(root, ref) {
|
|
29
|
+
const clean = ref.split(/[:#]/)[0]?.trim() ?? "";
|
|
30
|
+
if (clean.length === 0 || !PATH_LIKE.test(clean) || isAbsolute(clean))
|
|
31
|
+
return null;
|
|
32
|
+
if (!/[/\\.]/.test(clean))
|
|
33
|
+
return null;
|
|
34
|
+
const resolved = normalize(join(root, clean));
|
|
35
|
+
if (!resolved.startsWith(normalize(root)))
|
|
36
|
+
return null;
|
|
37
|
+
return !(await exists(resolved));
|
|
38
|
+
}
|
|
39
|
+
async function commit_gone(root, commit, budget) {
|
|
40
|
+
if (budget.left <= 0)
|
|
41
|
+
return null;
|
|
42
|
+
budget.left -= 1;
|
|
43
|
+
const result = await run("git", ["merge-base", "--is-ancestor", commit, "HEAD"], { cwd: root, timeout_ms: 3000 });
|
|
44
|
+
if (result.code === 0)
|
|
45
|
+
return false;
|
|
46
|
+
if (result.code === 1)
|
|
47
|
+
return true;
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
export async function staleness(root, loaded, is_repository) {
|
|
51
|
+
const stale = [];
|
|
52
|
+
const fresh = [];
|
|
53
|
+
const budget = { left: is_repository ? MAX_GIT_CHECKS : 0 };
|
|
54
|
+
for (const entry of candidates(loaded)) {
|
|
55
|
+
const verdicts = [];
|
|
56
|
+
for (const ref of entry.refs.slice(0, 8))
|
|
57
|
+
verdicts.push(await ref_missing(root, ref));
|
|
58
|
+
if (entry.commit !== null)
|
|
59
|
+
verdicts.push(await commit_gone(root, entry.commit, budget));
|
|
60
|
+
const known = verdicts.filter((verdict) => verdict !== null);
|
|
61
|
+
if (known.length === 0)
|
|
62
|
+
continue;
|
|
63
|
+
const now_stale = known.some((verdict) => verdict);
|
|
64
|
+
if (now_stale && !entry.stale)
|
|
65
|
+
stale.push(entry.name);
|
|
66
|
+
if (!now_stale && entry.stale)
|
|
67
|
+
fresh.push(entry.name);
|
|
68
|
+
}
|
|
69
|
+
return { stale, fresh };
|
|
70
|
+
}
|
package/dist/device.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { hostname, platform } from "node:os";
|
|
2
|
+
import { read_credentials, read_settings, update_settings } from "./config.js";
|
|
3
|
+
import { list_devices, register_device } from "./context/api.js";
|
|
4
|
+
import { VERSION } from "./version.js";
|
|
5
|
+
const PLATFORMS = new Set(["darwin", "linux", "win32", "freebsd"]);
|
|
6
|
+
function device_name() {
|
|
7
|
+
const raw = hostname().replace(/\.local$/, "").replace(/[^\w .-]/g, "").trim();
|
|
8
|
+
return (raw.length > 0 ? raw : "This computer").slice(0, 64);
|
|
9
|
+
}
|
|
10
|
+
export async function ensure_device(force = false) {
|
|
11
|
+
const credentials = await read_credentials();
|
|
12
|
+
const settings = await read_settings();
|
|
13
|
+
if (!force &&
|
|
14
|
+
settings.device_id !== undefined &&
|
|
15
|
+
credentials !== null &&
|
|
16
|
+
settings.device_api_url === credentials.api_url) {
|
|
17
|
+
return settings.device_id;
|
|
18
|
+
}
|
|
19
|
+
const current = platform();
|
|
20
|
+
const device = await register_device({
|
|
21
|
+
name: device_name(),
|
|
22
|
+
platform: PLATFORMS.has(current) ? current : "other",
|
|
23
|
+
client_version: VERSION,
|
|
24
|
+
});
|
|
25
|
+
await update_settings({
|
|
26
|
+
device_id: device.id,
|
|
27
|
+
device_api_url: credentials?.api_url,
|
|
28
|
+
});
|
|
29
|
+
return device.id;
|
|
30
|
+
}
|
|
31
|
+
export async function verify_device() {
|
|
32
|
+
const credentials = await read_credentials();
|
|
33
|
+
const settings = await read_settings();
|
|
34
|
+
if (credentials !== null &&
|
|
35
|
+
settings.device_id !== undefined &&
|
|
36
|
+
settings.device_api_url === credentials.api_url) {
|
|
37
|
+
const devices = await list_devices();
|
|
38
|
+
const mine = devices.find((device) => device.id === settings.device_id);
|
|
39
|
+
if (mine !== undefined && mine.revoked_at === null)
|
|
40
|
+
return mine.id;
|
|
41
|
+
}
|
|
42
|
+
return ensure_device(true);
|
|
43
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export class CliError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
status;
|
|
4
|
+
reference;
|
|
5
|
+
constructor(message, code, options = {}) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CliError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.status = options.status;
|
|
10
|
+
this.reference = options.reference;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function error_code(error) {
|
|
14
|
+
if (error instanceof CliError)
|
|
15
|
+
return error.code;
|
|
16
|
+
if (error instanceof Error && error.name !== "Error") {
|
|
17
|
+
return error.name
|
|
18
|
+
.replace(/([a-z])([A-Z])/g, "$1_$2")
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^a-z0-9_]/g, "_")
|
|
21
|
+
.slice(0, 64);
|
|
22
|
+
}
|
|
23
|
+
return "unexpected";
|
|
24
|
+
}
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
function git(cwd, args) {
|
|
4
|
+
return new Promise((resolve) => {
|
|
5
|
+
execFile("git", args, { cwd, timeout: 4000, maxBuffer: 1024 * 1024, windowsHide: true }, (error, stdout) => {
|
|
6
|
+
if (error !== null) {
|
|
7
|
+
resolve(null);
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
const value = stdout.toString().trim();
|
|
11
|
+
resolve(value.length > 0 ? value : null);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const COMMIT = /^[0-9a-f]{40}$/;
|
|
16
|
+
export async function repository(cwd) {
|
|
17
|
+
const root = await git(cwd, ["rev-parse", "--show-toplevel"]);
|
|
18
|
+
if (root === null)
|
|
19
|
+
return { root: cwd, name: basename(cwd) || "project" };
|
|
20
|
+
const [roots, remote, branch, head] = await Promise.all([
|
|
21
|
+
git(root, ["rev-list", "--max-parents=0", "HEAD"]),
|
|
22
|
+
git(root, ["remote", "get-url", "origin"]),
|
|
23
|
+
git(root, ["rev-parse", "--abbrev-ref", "HEAD"]),
|
|
24
|
+
git(root, ["rev-parse", "HEAD"]),
|
|
25
|
+
]);
|
|
26
|
+
const root_commit = roots
|
|
27
|
+
?.split("\n")
|
|
28
|
+
.map((line) => line.trim())
|
|
29
|
+
.filter((line) => COMMIT.test(line))
|
|
30
|
+
.sort()[0];
|
|
31
|
+
return {
|
|
32
|
+
root,
|
|
33
|
+
name: basename(root) || "project",
|
|
34
|
+
root_commit,
|
|
35
|
+
remote_url: remote ?? undefined,
|
|
36
|
+
branch: branch !== null && branch !== "HEAD" ? branch : undefined,
|
|
37
|
+
head: head !== null && COMMIT.test(head) ? head : undefined,
|
|
38
|
+
};
|
|
39
|
+
}
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { API_VERSION, read_settings } from "./config.js";
|
|
3
|
+
import { fresh_credentials } from "./auth/token.js";
|
|
4
|
+
import { CliError } from "./errors.js";
|
|
5
|
+
import { VERSION } from "./version.js";
|
|
6
|
+
const error_body = z.object({
|
|
7
|
+
error: z.string().optional(),
|
|
8
|
+
code: z.string().optional(),
|
|
9
|
+
reference: z.string().optional(),
|
|
10
|
+
});
|
|
11
|
+
export async function api(method, path, schema, options = {}) {
|
|
12
|
+
const credentials = await fresh_credentials();
|
|
13
|
+
const settings = await read_settings();
|
|
14
|
+
const url = new URL(`${credentials.api_url}${path}`);
|
|
15
|
+
for (const [key, value] of Object.entries(options.query ?? {})) {
|
|
16
|
+
if (value !== undefined)
|
|
17
|
+
url.searchParams.set(key, String(value));
|
|
18
|
+
}
|
|
19
|
+
const headers = {
|
|
20
|
+
authorization: `Bearer ${credentials.access_token}`,
|
|
21
|
+
"nemu-api-version": API_VERSION,
|
|
22
|
+
"user-agent": `nemu-cli/${VERSION}`,
|
|
23
|
+
};
|
|
24
|
+
if (options.body !== undefined)
|
|
25
|
+
headers["content-type"] = "application/json";
|
|
26
|
+
if (options.device !== false &&
|
|
27
|
+
settings.device_id !== undefined &&
|
|
28
|
+
settings.device_api_url === credentials.api_url) {
|
|
29
|
+
headers["x-nemu-device"] = settings.device_id;
|
|
30
|
+
}
|
|
31
|
+
let response;
|
|
32
|
+
try {
|
|
33
|
+
response = await fetch(url, {
|
|
34
|
+
method,
|
|
35
|
+
headers,
|
|
36
|
+
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
|
37
|
+
signal: AbortSignal.timeout(options.timeout_ms ?? 20_000),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw new CliError(`Could not reach ${credentials.api_url}.`, "network_failed");
|
|
42
|
+
}
|
|
43
|
+
const payload = await response.json().catch(() => null);
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
const failure = error_body.safeParse(payload);
|
|
46
|
+
const message = failure.success && failure.data.error !== undefined
|
|
47
|
+
? failure.data.error
|
|
48
|
+
: `Request failed with status ${response.status}`;
|
|
49
|
+
throw new CliError(message, failure.success && failure.data.code !== undefined ? failure.data.code : `http_${response.status}`, {
|
|
50
|
+
status: response.status,
|
|
51
|
+
reference: (failure.success ? failure.data.reference : undefined) ??
|
|
52
|
+
response.headers.get("x-nemu-reference") ??
|
|
53
|
+
undefined,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const parsed = schema.safeParse(payload);
|
|
57
|
+
if (!parsed.success) {
|
|
58
|
+
throw new CliError("Nemu sent an answer this CLI does not understand. Update nemu-cli.", "invalid_response");
|
|
59
|
+
}
|
|
60
|
+
return parsed.data;
|
|
61
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -7,8 +7,21 @@ import { whoami } from "./commands/whoami.js";
|
|
|
7
7
|
import { members, workspaces } from "./commands/workspaces.js";
|
|
8
8
|
import { mappings, models, prompts, providers, workflows, } from "./commands/resources.js";
|
|
9
9
|
import { health, indexes, metrics, quotas, raw_api, scopes, user, version_info, } from "./commands/account.js";
|
|
10
|
+
import { token } from "./commands/token.js";
|
|
11
|
+
import { status } from "./commands/status.js";
|
|
12
|
+
import { uninstall } from "./commands/uninstall.js";
|
|
13
|
+
import { setup } from "./commands/setup.js";
|
|
14
|
+
import { mcp } from "./commands/mcp.js";
|
|
15
|
+
import { hook } from "./commands/hook.js";
|
|
16
|
+
import { context } from "./commands/context.js";
|
|
17
|
+
import { config_command } from "./commands/settings.js";
|
|
18
|
+
import { client_command } from "./commands/client.js";
|
|
19
|
+
import { send_report } from "./report.js";
|
|
20
|
+
import { maybe_offer_update, update_command } from "./update.js";
|
|
21
|
+
import { CliError } from "./errors.js";
|
|
22
|
+
import { VERSION } from "./version.js";
|
|
10
23
|
import { bold, dim, fail, out } from "./ui.js";
|
|
11
|
-
const
|
|
24
|
+
const UNREPORTED = new Set(["hook", "mcp", "token"]);
|
|
12
25
|
const COMMANDS = {
|
|
13
26
|
login,
|
|
14
27
|
logout: () => logout(),
|
|
@@ -30,10 +43,42 @@ const COMMANDS = {
|
|
|
30
43
|
"api-version": () => version_info(),
|
|
31
44
|
scopes: () => scopes(),
|
|
32
45
|
api: raw_api,
|
|
46
|
+
token,
|
|
47
|
+
status,
|
|
48
|
+
uninstall,
|
|
49
|
+
setup,
|
|
50
|
+
claude: client_command("claude"),
|
|
51
|
+
codex: client_command("codex"),
|
|
52
|
+
mcp,
|
|
53
|
+
hook,
|
|
54
|
+
context,
|
|
55
|
+
config: config_command,
|
|
56
|
+
update: update_command,
|
|
33
57
|
};
|
|
34
58
|
const HELP = `
|
|
35
59
|
${bold("nemu")} ${dim(VERSION)}
|
|
36
60
|
|
|
61
|
+
${bold("Context for coding agents")}
|
|
62
|
+
setup <claude|codex> [--hooks|--no-hooks] [--yes]
|
|
63
|
+
Connect Claude Code (aliases cc, claude-code) or Codex
|
|
64
|
+
claude [-- <claude args>] Set up if needed, then start Claude Code
|
|
65
|
+
codex [-- <codex args>] Set up if needed, then start Codex
|
|
66
|
+
claude load memories [--all] [--yes]
|
|
67
|
+
Import Claude Code's saved memories
|
|
68
|
+
claude load sessions [--limit n] Have Claude read past sessions and save what matters
|
|
69
|
+
codex load sessions [--limit n] The same for Codex
|
|
70
|
+
mcp Run the MCP bridge (what clients start)
|
|
71
|
+
mcp config <client> Print config for cursor, gemini, vscode, windsurf,
|
|
72
|
+
zed, claude, codex or remote
|
|
73
|
+
context [load] Show what an agent sees for this folder
|
|
74
|
+
context search <words> Search this project's saved notes
|
|
75
|
+
context projects | export [--out f] List projects or export everything
|
|
76
|
+
status Sign in, device, global install, client setup
|
|
77
|
+
uninstall [claude|codex|all] Remove what setup added
|
|
78
|
+
token [--header] Print a fresh access token
|
|
79
|
+
config get | set reports on|off Error reports sent after a failure
|
|
80
|
+
update [--yes] Check for a new version and install it
|
|
81
|
+
|
|
37
82
|
${bold("Account")}
|
|
38
83
|
login [--force] [--no-browser] Sign in with the device flow
|
|
39
84
|
logout Delete the stored token
|
|
@@ -137,19 +182,37 @@ async function main() {
|
|
|
137
182
|
usage();
|
|
138
183
|
return 0;
|
|
139
184
|
}
|
|
185
|
+
await maybe_offer_update(command);
|
|
140
186
|
return await handler(args);
|
|
141
187
|
}
|
|
188
|
+
async function report_failure(error) {
|
|
189
|
+
const argv = process.argv.slice(2);
|
|
190
|
+
const command = argv[0] ?? "";
|
|
191
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
192
|
+
const server_reference = error instanceof CliError ? error.reference : undefined;
|
|
193
|
+
const parsed = parse_args(argv.slice(1));
|
|
194
|
+
const reference = UNREPORTED.has(command) || COMMANDS[command] === undefined
|
|
195
|
+
? null
|
|
196
|
+
: await send_report({
|
|
197
|
+
command,
|
|
198
|
+
positional: parsed.positional,
|
|
199
|
+
flags: Object.keys(parsed.flags),
|
|
200
|
+
error,
|
|
201
|
+
});
|
|
202
|
+
const shown = reference ?? server_reference ?? null;
|
|
203
|
+
if (is_json_mode()) {
|
|
204
|
+
emit({ ok: false, error: message, ...(error instanceof CliError ? { code: error.code } : {}), ...(shown !== null ? { reference: shown } : {}) });
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
fail(message);
|
|
208
|
+
if (shown !== null)
|
|
209
|
+
out(dim(` Reference ${shown}. Quote it if you contact support@nemu.cc.`));
|
|
210
|
+
}
|
|
142
211
|
main()
|
|
143
212
|
.then((code) => {
|
|
144
213
|
process.exitCode = code;
|
|
145
214
|
})
|
|
146
|
-
.catch((error) => {
|
|
147
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
148
|
-
if (is_json_mode()) {
|
|
149
|
-
emit({ ok: false, error: message });
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
fail(message);
|
|
153
|
-
}
|
|
215
|
+
.catch(async (error) => {
|
|
154
216
|
process.exitCode = 1;
|
|
217
|
+
await report_failure(error).catch(() => undefined);
|
|
155
218
|
});
|
package/dist/report.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { arch, platform, release } from "node:os";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { API_VERSION, read_credentials, read_settings } from "./config.js";
|
|
4
|
+
import { fresh_credentials } from "./auth/token.js";
|
|
5
|
+
import { CliError, error_code } from "./errors.js";
|
|
6
|
+
import { redact, strip_home } from "./context/redact.js";
|
|
7
|
+
import { VERSION } from "./version.js";
|
|
8
|
+
const SKIPPED_CODES = new Set([
|
|
9
|
+
"not_signed_in",
|
|
10
|
+
"invalid_client",
|
|
11
|
+
"invalid_arguments",
|
|
12
|
+
"unknown_command",
|
|
13
|
+
"cancelled",
|
|
14
|
+
]);
|
|
15
|
+
function command_label(command, positional) {
|
|
16
|
+
const words = [command, ...positional.slice(0, 2)]
|
|
17
|
+
.map((word) => word.toLowerCase())
|
|
18
|
+
.filter((word) => /^[a-z][a-z0-9-]{0,31}$/.test(word));
|
|
19
|
+
return words.slice(0, 3).join(" ");
|
|
20
|
+
}
|
|
21
|
+
function os_label() {
|
|
22
|
+
const version = release().replace(/[^A-Za-z0-9._-]/g, "").slice(0, 40) || "unknown";
|
|
23
|
+
const architecture = arch().replace(/[^a-z0-9_]/g, "").slice(0, 16) || "unknown";
|
|
24
|
+
return `${platform().replace(/[^a-z0-9]/g, "")} ${version} ${architecture}`;
|
|
25
|
+
}
|
|
26
|
+
export async function send_report(input) {
|
|
27
|
+
if (process.env.NEMU_NO_REPORTS === "1")
|
|
28
|
+
return null;
|
|
29
|
+
const code = error_code(input.error);
|
|
30
|
+
if (SKIPPED_CODES.has(code))
|
|
31
|
+
return null;
|
|
32
|
+
if (input.error instanceof CliError && input.error.status !== undefined && input.error.status < 500 && input.error.reference === undefined) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const settings = await read_settings();
|
|
36
|
+
if (!settings.reports)
|
|
37
|
+
return null;
|
|
38
|
+
if ((await read_credentials()) === null)
|
|
39
|
+
return null;
|
|
40
|
+
const label = command_label(input.command, input.positional);
|
|
41
|
+
if (label.length === 0)
|
|
42
|
+
return null;
|
|
43
|
+
const message = input.error instanceof Error ? input.error.message : String(input.error);
|
|
44
|
+
const stack = input.error instanceof Error ? input.error.stack : undefined;
|
|
45
|
+
try {
|
|
46
|
+
const credentials = await fresh_credentials();
|
|
47
|
+
const response = await fetch(`${credentials.api_url}/api/v1/cli/reports`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: {
|
|
50
|
+
authorization: `Bearer ${credentials.access_token}`,
|
|
51
|
+
"content-type": "application/json",
|
|
52
|
+
"nemu-api-version": API_VERSION,
|
|
53
|
+
"user-agent": `nemu-cli/${VERSION}`,
|
|
54
|
+
},
|
|
55
|
+
body: JSON.stringify({
|
|
56
|
+
command: label,
|
|
57
|
+
flags: input.flags.filter((name) => /^[a-z][a-z0-9-]{0,31}$/.test(name)).slice(0, 20),
|
|
58
|
+
cli_version: VERSION,
|
|
59
|
+
os: os_label(),
|
|
60
|
+
node_version: process.version,
|
|
61
|
+
error_code: /^[a-z0-9_]{1,64}$/.test(code) ? code : undefined,
|
|
62
|
+
message: strip_home(redact(message)).slice(0, 2000) || "No message",
|
|
63
|
+
stack: stack === undefined ? undefined : strip_home(redact(stack)).slice(0, 4000),
|
|
64
|
+
server_reference: input.error instanceof CliError && input.error.reference !== undefined
|
|
65
|
+
? input.error.reference
|
|
66
|
+
: undefined,
|
|
67
|
+
device_id: settings.device_id,
|
|
68
|
+
}),
|
|
69
|
+
signal: AbortSignal.timeout(5000),
|
|
70
|
+
});
|
|
71
|
+
if (!response.ok)
|
|
72
|
+
return null;
|
|
73
|
+
const parsed = z.object({ reference: z.string() }).safeParse(await response.json().catch(() => null));
|
|
74
|
+
return parsed.success ? parsed.data.reference : null;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/session.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { NemuDevice } from "@nemu-ai/sdk";
|
|
2
|
-
import { api_url, client_id,
|
|
2
|
+
import { api_url, client_id, write_credentials, } from "./config.js";
|
|
3
|
+
import { fresh_credentials } from "./auth/token.js";
|
|
4
|
+
import { CliError } from "./errors.js";
|
|
3
5
|
export class NotSignedIn extends Error {
|
|
4
6
|
constructor() {
|
|
5
7
|
super("Not signed in. Run nemu login.");
|
|
@@ -13,11 +15,14 @@ export function device_client(url = api_url()) {
|
|
|
13
15
|
});
|
|
14
16
|
}
|
|
15
17
|
export async function client() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
let credentials;
|
|
19
|
+
try {
|
|
20
|
+
credentials = await fresh_credentials();
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (error instanceof CliError && error.code === "not_signed_in")
|
|
24
|
+
throw new NotSignedIn();
|
|
25
|
+
throw error;
|
|
21
26
|
}
|
|
22
27
|
const device = device_client(credentials.api_url);
|
|
23
28
|
return device.from_tokens({
|