nemu-cli 1.0.0 → 1.1.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 +19 -0
- package/README.md +54 -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 +48 -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 +68 -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/version.js +1 -0
- package/package.json +9 -4
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { createInterface } from "node:readline";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { flag } from "../args.js";
|
|
4
|
+
import { API_VERSION, read_settings } from "../config.js";
|
|
5
|
+
import { fresh_credentials } from "../auth/token.js";
|
|
6
|
+
import { CliError } from "../errors.js";
|
|
7
|
+
import { current_project } from "../context/project.js";
|
|
8
|
+
import { repository } from "../git.js";
|
|
9
|
+
import { VERSION } from "../version.js";
|
|
10
|
+
import { out } from "../ui.js";
|
|
11
|
+
const CLIENTS = ["claude-code", "codex", "cursor", "zed", "gemini", "mcp"];
|
|
12
|
+
const rpc_schema = z.object({
|
|
13
|
+
jsonrpc: z.literal("2.0"),
|
|
14
|
+
id: z.union([z.string(), z.number()]).optional(),
|
|
15
|
+
method: z.string(),
|
|
16
|
+
params: z.record(z.string(), z.unknown()).optional(),
|
|
17
|
+
});
|
|
18
|
+
const BRANCH_TOOLS = new Set(["context_load", "memory_write"]);
|
|
19
|
+
function write_line(value) {
|
|
20
|
+
process.stdout.write(`${JSON.stringify(value)}\n`);
|
|
21
|
+
}
|
|
22
|
+
function log(message) {
|
|
23
|
+
process.stderr.write(`[nemu mcp] ${message}\n`);
|
|
24
|
+
}
|
|
25
|
+
function rpc_failure(id, message, code = -32603) {
|
|
26
|
+
if (id === undefined)
|
|
27
|
+
return;
|
|
28
|
+
write_line({ jsonrpc: "2.0", id, error: { code, message } });
|
|
29
|
+
}
|
|
30
|
+
function client_from(args) {
|
|
31
|
+
const raw = (flag(args, "client") ?? process.env.NEMU_MCP_CLIENT ?? "mcp").toLowerCase();
|
|
32
|
+
return CLIENTS.includes(raw) ? raw : "mcp";
|
|
33
|
+
}
|
|
34
|
+
function resolve_project(state) {
|
|
35
|
+
if (state.resolving === null)
|
|
36
|
+
state.resolving = open_for_cwd(state);
|
|
37
|
+
return state.resolving;
|
|
38
|
+
}
|
|
39
|
+
async function open_for_cwd(state) {
|
|
40
|
+
try {
|
|
41
|
+
const project = await current_project(process.cwd());
|
|
42
|
+
state.project_id = project.id;
|
|
43
|
+
state.branch = project.repository.branch;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
47
|
+
log(`could not open the Context project for this folder: ${reason}`);
|
|
48
|
+
const info = await repository(process.cwd()).catch(() => null);
|
|
49
|
+
state.branch = info?.branch;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function with_branch(message, branch) {
|
|
53
|
+
if (branch === undefined || message.method !== "tools/call")
|
|
54
|
+
return message;
|
|
55
|
+
const name = message.params?.name;
|
|
56
|
+
const args = message.params?.arguments;
|
|
57
|
+
if (typeof name !== "string" || !BRANCH_TOOLS.has(name))
|
|
58
|
+
return message;
|
|
59
|
+
if (args === null || typeof args !== "object" || Array.isArray(args))
|
|
60
|
+
return message;
|
|
61
|
+
if ("branch" in args)
|
|
62
|
+
return message;
|
|
63
|
+
return {
|
|
64
|
+
...message,
|
|
65
|
+
params: { ...message.params, arguments: { ...args, branch } },
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async function forward(message, state, client) {
|
|
69
|
+
if (message.method === "tools/call")
|
|
70
|
+
await resolve_project(state);
|
|
71
|
+
const settings = await read_settings();
|
|
72
|
+
let credentials;
|
|
73
|
+
try {
|
|
74
|
+
credentials = await fresh_credentials();
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
if (message.method === "tools/call" && message.id !== undefined) {
|
|
78
|
+
write_line({
|
|
79
|
+
jsonrpc: "2.0",
|
|
80
|
+
id: message.id,
|
|
81
|
+
result: {
|
|
82
|
+
content: [{ type: "text", text: "Nemu is not signed in on this machine. Ask the user to run: nemu login" }],
|
|
83
|
+
isError: true,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
rpc_failure(message.id, error instanceof Error ? error.message : "Not signed in");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const headers = {
|
|
92
|
+
authorization: `Bearer ${credentials.access_token}`,
|
|
93
|
+
"content-type": "application/json",
|
|
94
|
+
accept: "application/json, text/event-stream",
|
|
95
|
+
"nemu-api-version": API_VERSION,
|
|
96
|
+
"user-agent": `nemu-cli/${VERSION}`,
|
|
97
|
+
"x-nemu-client": client,
|
|
98
|
+
};
|
|
99
|
+
if (settings.device_id !== undefined && settings.device_api_url === credentials.api_url) {
|
|
100
|
+
headers["x-nemu-device"] = settings.device_id;
|
|
101
|
+
}
|
|
102
|
+
if (state.project_id !== null)
|
|
103
|
+
headers["x-nemu-project"] = state.project_id;
|
|
104
|
+
let response;
|
|
105
|
+
try {
|
|
106
|
+
response = await fetch(`${credentials.api_url}/api/mcp`, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers,
|
|
109
|
+
body: JSON.stringify(with_branch(message, state.branch)),
|
|
110
|
+
signal: AbortSignal.timeout(30_000),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
rpc_failure(message.id, `Could not reach ${credentials.api_url}`);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (response.status === 202)
|
|
118
|
+
return;
|
|
119
|
+
const body = await response.json().catch(() => null);
|
|
120
|
+
if (body !== null && typeof body === "object" && "jsonrpc" in body) {
|
|
121
|
+
write_line(body);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const error = z.object({ error: z.string(), reference: z.string().optional() }).safeParse(body);
|
|
125
|
+
rpc_failure(message.id, error.success
|
|
126
|
+
? `${error.data.error}${error.data.reference !== undefined ? ` (reference ${error.data.reference})` : ""}`
|
|
127
|
+
: `Nemu answered with status ${response.status}`);
|
|
128
|
+
}
|
|
129
|
+
export async function mcp(args) {
|
|
130
|
+
if (args.positional[0] === "config")
|
|
131
|
+
return mcp_config(args);
|
|
132
|
+
const client = client_from(args);
|
|
133
|
+
const state = { project_id: null, branch: undefined, resolving: null };
|
|
134
|
+
const pending = new Set();
|
|
135
|
+
const lines = createInterface({ input: process.stdin, crlfDelay: Infinity });
|
|
136
|
+
for await (const line of lines) {
|
|
137
|
+
const trimmed = line.trim();
|
|
138
|
+
if (trimmed.length === 0)
|
|
139
|
+
continue;
|
|
140
|
+
let raw;
|
|
141
|
+
try {
|
|
142
|
+
raw = JSON.parse(trimmed);
|
|
143
|
+
}
|
|
144
|
+
catch {
|
|
145
|
+
write_line({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "Parse error" } });
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const parsed = rpc_schema.safeParse(raw);
|
|
149
|
+
if (!parsed.success) {
|
|
150
|
+
if (raw !== null && typeof raw === "object" && "result" in raw)
|
|
151
|
+
continue;
|
|
152
|
+
write_line({ jsonrpc: "2.0", id: null, error: { code: -32600, message: "Invalid request" } });
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const task = forward(parsed.data, state, client)
|
|
156
|
+
.catch((error) => {
|
|
157
|
+
rpc_failure(parsed.data.id, error instanceof CliError ? error.message : "Bridge error");
|
|
158
|
+
})
|
|
159
|
+
.finally(() => pending.delete(task));
|
|
160
|
+
pending.add(task);
|
|
161
|
+
}
|
|
162
|
+
await Promise.all(pending);
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
const SNIPPETS = {
|
|
166
|
+
claude: {
|
|
167
|
+
where: "Run in a terminal",
|
|
168
|
+
text: "claude mcp add --scope user nemu -- nemu mcp --client claude-code",
|
|
169
|
+
},
|
|
170
|
+
codex: {
|
|
171
|
+
where: "~/.codex/config.toml",
|
|
172
|
+
text: '[mcp_servers.nemu]\ncommand = "nemu"\nargs = ["mcp", "--client", "codex"]',
|
|
173
|
+
},
|
|
174
|
+
cursor: {
|
|
175
|
+
where: "~/.cursor/mcp.json",
|
|
176
|
+
text: JSON.stringify({ mcpServers: { nemu: { command: "nemu", args: ["mcp", "--client", "cursor"] } } }, null, 2),
|
|
177
|
+
},
|
|
178
|
+
gemini: {
|
|
179
|
+
where: "~/.gemini/settings.json",
|
|
180
|
+
text: JSON.stringify({ mcpServers: { nemu: { command: "nemu", args: ["mcp", "--client", "gemini"] } } }, null, 2),
|
|
181
|
+
},
|
|
182
|
+
vscode: {
|
|
183
|
+
where: ".vscode/mcp.json in your project",
|
|
184
|
+
text: JSON.stringify({ servers: { nemu: { type: "stdio", command: "nemu", args: ["mcp"] } } }, null, 2),
|
|
185
|
+
},
|
|
186
|
+
windsurf: {
|
|
187
|
+
where: "~/.codeium/windsurf/mcp_config.json",
|
|
188
|
+
text: JSON.stringify({ mcpServers: { nemu: { command: "nemu", args: ["mcp"] } } }, null, 2),
|
|
189
|
+
},
|
|
190
|
+
zed: {
|
|
191
|
+
where: "Zed settings.json",
|
|
192
|
+
text: JSON.stringify({ context_servers: { nemu: { command: "nemu", args: ["mcp", "--client", "zed"] } } }, null, 2),
|
|
193
|
+
},
|
|
194
|
+
remote: {
|
|
195
|
+
where: "Any client that supports remote MCP over HTTP (create a Context key in the dashboard first)",
|
|
196
|
+
text: JSON.stringify({ url: "https://app.nemu.cc/api/mcp", headers: { Authorization: "Bearer <your Context key>" } }, null, 2),
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
function mcp_config(args) {
|
|
200
|
+
const client = (args.positional[1] ?? "").toLowerCase();
|
|
201
|
+
const snippet = SNIPPETS[client === "claude-code" || client === "cc" ? "claude" : client];
|
|
202
|
+
if (snippet === undefined) {
|
|
203
|
+
throw new CliError(`Pick a client: ${Object.keys(SNIPPETS).join(", ")}. Example: nemu mcp config cursor`, "invalid_client");
|
|
204
|
+
}
|
|
205
|
+
out(` ${snippet.where}`);
|
|
206
|
+
out();
|
|
207
|
+
out(snippet.text);
|
|
208
|
+
out();
|
|
209
|
+
return 0;
|
|
210
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { read_settings, update_settings } from "../config.js";
|
|
2
|
+
import { CliError } from "../errors.js";
|
|
3
|
+
import { emit, is_json_mode } from "../output.js";
|
|
4
|
+
import { out, success } from "../ui.js";
|
|
5
|
+
export async function config_command(args) {
|
|
6
|
+
const [action, key, value] = args.positional;
|
|
7
|
+
if (action === undefined || action === "get") {
|
|
8
|
+
const settings = await read_settings();
|
|
9
|
+
if (is_json_mode()) {
|
|
10
|
+
emit(settings);
|
|
11
|
+
return 0;
|
|
12
|
+
}
|
|
13
|
+
out(` reports ${settings.reports ? "on" : "off"}`);
|
|
14
|
+
out(` device ${settings.device_id ?? "not registered"}`);
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
17
|
+
if (action === "set" && key === "reports" && (value === "on" || value === "off")) {
|
|
18
|
+
await update_settings({ reports: value === "on" });
|
|
19
|
+
success(`Error reports are ${value}`);
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
throw new CliError("Use: nemu config get | nemu config set reports on|off", "invalid_arguments");
|
|
23
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { bool_flag, parse_args } from "../args.js";
|
|
2
|
+
import { has_scope, read_credentials } from "../config.js";
|
|
3
|
+
import { CliError } from "../errors.js";
|
|
4
|
+
import { ensure_device } from "../device.js";
|
|
5
|
+
import { run, which } from "../system.js";
|
|
6
|
+
import { VERSION } from "../version.js";
|
|
7
|
+
import { bold, confirm, dim, is_interactive, out, success } from "../ui.js";
|
|
8
|
+
import { emit, is_json_mode } from "../output.js";
|
|
9
|
+
import { install_hooks } from "../clients/hooks.js";
|
|
10
|
+
import { client_from_name, client_installed } from "../clients/registry.js";
|
|
11
|
+
import { login } from "./login.js";
|
|
12
|
+
function newer_or_equal(found, wanted) {
|
|
13
|
+
const parse = (value) => value.split(/[.-]/).slice(0, 3).map((part) => Number.parseInt(part, 10) || 0);
|
|
14
|
+
const [a, b] = [parse(found), parse(wanted)];
|
|
15
|
+
for (let index = 0; index < 3; index += 1) {
|
|
16
|
+
if ((a[index] ?? 0) !== (b[index] ?? 0))
|
|
17
|
+
return (a[index] ?? 0) > (b[index] ?? 0);
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
export async function resolve_nemu_binary(interactive_install) {
|
|
22
|
+
const forced = process.env.NEMU_BIN;
|
|
23
|
+
if (forced !== undefined && forced.length > 0)
|
|
24
|
+
return forced;
|
|
25
|
+
const found = await which("nemu");
|
|
26
|
+
if (found !== null) {
|
|
27
|
+
const version = (await run(found, ["--version"], { timeout_ms: 15_000 })).stdout.trim();
|
|
28
|
+
if (newer_or_equal(version, VERSION))
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
const verb = found === null ? "install" : "update";
|
|
32
|
+
const accepted = interactive_install &&
|
|
33
|
+
(await confirm(`Claude and Codex start nemu on every session, so it needs a global install. ${verb === "install" ? "Install" : "Update"} it now with npm i -g nemu-cli@${VERSION}?`, true));
|
|
34
|
+
if (!accepted) {
|
|
35
|
+
throw new CliError(`Install the CLI globally first: npm i -g nemu-cli@${VERSION} (or bun add -g nemu-cli@${VERSION})`, "global_install_required");
|
|
36
|
+
}
|
|
37
|
+
const result = await run("npm", ["install", "-g", `nemu-cli@${VERSION}`], { timeout_ms: 180_000 });
|
|
38
|
+
if (result.code !== 0) {
|
|
39
|
+
throw new CliError(`npm could not install nemu-cli. Run it yourself: npm i -g nemu-cli@${VERSION}`, "global_install_failed");
|
|
40
|
+
}
|
|
41
|
+
const installed = await which("nemu");
|
|
42
|
+
if (installed === null) {
|
|
43
|
+
throw new CliError("nemu was installed but is not on your PATH. Open a new terminal and run setup again.", "global_install_path");
|
|
44
|
+
}
|
|
45
|
+
return installed;
|
|
46
|
+
}
|
|
47
|
+
async function ensure_signed_in() {
|
|
48
|
+
const credentials = await read_credentials();
|
|
49
|
+
const ready = credentials !== null && has_scope(credentials, "context:write");
|
|
50
|
+
if (ready)
|
|
51
|
+
return;
|
|
52
|
+
if (!is_interactive()) {
|
|
53
|
+
throw new CliError(credentials === null
|
|
54
|
+
? "Not signed in. Run nemu login first."
|
|
55
|
+
: "Your sign in predates Context. Run nemu login --force once.", "not_signed_in");
|
|
56
|
+
}
|
|
57
|
+
if (credentials !== null)
|
|
58
|
+
out(dim(" Your sign in needs the new Context permission. Signing in again."));
|
|
59
|
+
const code = await login(parse_args(credentials === null ? [] : ["--force"]));
|
|
60
|
+
if (code !== 0)
|
|
61
|
+
throw new CliError("Sign in did not finish.", "not_signed_in");
|
|
62
|
+
}
|
|
63
|
+
async function choose_hooks(args, spec) {
|
|
64
|
+
if (bool_flag(args, "no-hooks"))
|
|
65
|
+
return false;
|
|
66
|
+
if (bool_flag(args, "hooks") || bool_flag(args, "yes", "y"))
|
|
67
|
+
return true;
|
|
68
|
+
if (!is_interactive())
|
|
69
|
+
return false;
|
|
70
|
+
out();
|
|
71
|
+
out(` Hooks let ${spec.label} load this project's notes when a session starts and`);
|
|
72
|
+
out(" ask the agent to save a handoff before it stops, without you asking.");
|
|
73
|
+
return confirm("Install the Nemu hooks too?", true);
|
|
74
|
+
}
|
|
75
|
+
export async function run_setup(spec, args) {
|
|
76
|
+
if (!(await client_installed(spec))) {
|
|
77
|
+
throw new CliError(`${spec.label} is not installed. ${spec.install_hint}`, "client_missing");
|
|
78
|
+
}
|
|
79
|
+
await ensure_signed_in();
|
|
80
|
+
const device_id = await ensure_device(bool_flag(args, "new-device"));
|
|
81
|
+
const nemu = await resolve_nemu_binary(is_interactive() && !bool_flag(args, "no-install"));
|
|
82
|
+
await spec.add_mcp(nemu);
|
|
83
|
+
const hooks = await choose_hooks(args, spec);
|
|
84
|
+
if (hooks)
|
|
85
|
+
await install_hooks(spec.hooks_path(), nemu, spec.id);
|
|
86
|
+
return { client: spec.id, hooks, nemu, device_id };
|
|
87
|
+
}
|
|
88
|
+
export async function setup(args) {
|
|
89
|
+
const spec = client_from_name(args.positional[0]);
|
|
90
|
+
const result = await run_setup(spec, args);
|
|
91
|
+
if (is_json_mode()) {
|
|
92
|
+
emit({ ok: true, ...result });
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
out();
|
|
96
|
+
success(`${spec.label} is connected to Nemu Context`);
|
|
97
|
+
out(dim(` MCP server "nemu" registered${result.hooks ? `, hooks added to ${spec.hooks_path()}` : ", no hooks"}`));
|
|
98
|
+
for (const line of spec.after_setup)
|
|
99
|
+
out(` ${line}`);
|
|
100
|
+
if (!result.hooks)
|
|
101
|
+
out(dim(` Add hooks later with: nemu setup ${spec.binary} --hooks`));
|
|
102
|
+
out(dim(` Check it any time with: ${bold("nemu status")}`));
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { has_scope, read_credentials, read_settings, api_url } from "../config.js";
|
|
2
|
+
import { installed_hooks, HOOK_SPECS } from "../clients/hooks.js";
|
|
3
|
+
import { CLIENTS, client_installed } from "../clients/registry.js";
|
|
4
|
+
import { run, which } from "../system.js";
|
|
5
|
+
import { VERSION } from "../version.js";
|
|
6
|
+
import { bold, dim, green, out, red } from "../ui.js";
|
|
7
|
+
import { emit, is_json_mode } from "../output.js";
|
|
8
|
+
function mark(ok) {
|
|
9
|
+
return ok ? green("ok") : red("no");
|
|
10
|
+
}
|
|
11
|
+
export async function status() {
|
|
12
|
+
const credentials = await read_credentials();
|
|
13
|
+
const settings = await read_settings();
|
|
14
|
+
const global = await which("nemu");
|
|
15
|
+
const global_version = global === null ? null : (await run(global, ["--version"], { timeout_ms: 15_000 })).stdout.trim() || null;
|
|
16
|
+
const clients = await Promise.all(Object.values(CLIENTS).map(async (spec) => {
|
|
17
|
+
const installed = await client_installed(spec);
|
|
18
|
+
const mcp = installed ? await spec.has_mcp() : false;
|
|
19
|
+
const hooks = installed ? await installed_hooks(spec.hooks_path()) : [];
|
|
20
|
+
return {
|
|
21
|
+
id: spec.id,
|
|
22
|
+
label: spec.label,
|
|
23
|
+
installed,
|
|
24
|
+
mcp,
|
|
25
|
+
hooks,
|
|
26
|
+
hooks_complete: hooks.length === HOOK_SPECS[spec.id].length,
|
|
27
|
+
hooks_path: spec.hooks_path(),
|
|
28
|
+
};
|
|
29
|
+
}));
|
|
30
|
+
const report = {
|
|
31
|
+
version: VERSION,
|
|
32
|
+
api_url: credentials?.api_url ?? api_url(),
|
|
33
|
+
signed_in: credentials !== null,
|
|
34
|
+
account: credentials?.account ?? null,
|
|
35
|
+
context_access: credentials !== null && has_scope(credentials, "context:write"),
|
|
36
|
+
device_id: settings.device_id ?? null,
|
|
37
|
+
reports: settings.reports,
|
|
38
|
+
global_install: global,
|
|
39
|
+
global_version,
|
|
40
|
+
clients,
|
|
41
|
+
};
|
|
42
|
+
if (is_json_mode()) {
|
|
43
|
+
emit(report);
|
|
44
|
+
return 0;
|
|
45
|
+
}
|
|
46
|
+
out();
|
|
47
|
+
out(` ${bold("nemu")} ${dim(VERSION)} ${dim(report.api_url)}`);
|
|
48
|
+
out(` Signed in ${mark(report.signed_in)}${report.account !== null ? ` ${report.account}` : ""}`);
|
|
49
|
+
out(` Context access ${mark(report.context_access)}${report.signed_in && !report.context_access ? dim(" run nemu login --force") : ""}`);
|
|
50
|
+
out(` This device ${report.device_id ?? red("not registered")}`);
|
|
51
|
+
out(` Global install ${mark(global !== null)}${global !== null ? dim(` ${global} ${global_version ?? ""}`) : dim(" npm i -g nemu-cli")}`);
|
|
52
|
+
out(` Error reports ${report.reports ? "on" : "off"}`);
|
|
53
|
+
for (const client of clients) {
|
|
54
|
+
out();
|
|
55
|
+
out(` ${bold(client.label)}${client.installed ? "" : dim(" not installed")}`);
|
|
56
|
+
if (!client.installed)
|
|
57
|
+
continue;
|
|
58
|
+
out(` MCP server ${mark(client.mcp)}${client.mcp ? "" : dim(` nemu setup ${client.id === "codex" ? "codex" : "claude"}`)}`);
|
|
59
|
+
out(` Hooks ${client.hooks.length === 0 ? dim("none") : client.hooks_complete ? green("all") : `${client.hooks.join(", ")}`}`);
|
|
60
|
+
}
|
|
61
|
+
out();
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { fresh_credentials } from "../auth/token.js";
|
|
2
|
+
import { bool_flag } from "../args.js";
|
|
3
|
+
import { emit, is_json_mode } from "../output.js";
|
|
4
|
+
export async function token(args) {
|
|
5
|
+
const credentials = await fresh_credentials();
|
|
6
|
+
if (is_json_mode()) {
|
|
7
|
+
emit({ access_token: credentials.access_token, expires_at: credentials.expires_at });
|
|
8
|
+
return 0;
|
|
9
|
+
}
|
|
10
|
+
if (bool_flag(args, "header")) {
|
|
11
|
+
process.stdout.write(`${JSON.stringify({ Authorization: `Bearer ${credentials.access_token}` })}\n`);
|
|
12
|
+
return 0;
|
|
13
|
+
}
|
|
14
|
+
process.stdout.write(`${credentials.access_token}\n`);
|
|
15
|
+
return 0;
|
|
16
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { remove_hooks } from "../clients/hooks.js";
|
|
2
|
+
import { CLIENTS, client_from_name, client_installed } from "../clients/registry.js";
|
|
3
|
+
import { CliError } from "../errors.js";
|
|
4
|
+
import { dim, out, success } from "../ui.js";
|
|
5
|
+
import { emit, is_json_mode } from "../output.js";
|
|
6
|
+
async function remove_from(spec) {
|
|
7
|
+
const installed = await client_installed(spec);
|
|
8
|
+
const mcp = installed ? await spec.remove_mcp() : false;
|
|
9
|
+
const hooks = await remove_hooks(spec.hooks_path());
|
|
10
|
+
return { client: spec.id, mcp_removed: mcp, hooks_removed: hooks };
|
|
11
|
+
}
|
|
12
|
+
export async function uninstall(args) {
|
|
13
|
+
const target = args.positional[0] ?? "all";
|
|
14
|
+
const specs = target === "all" ? Object.values(CLIENTS) : [client_from_name(target)];
|
|
15
|
+
if (specs.length === 0)
|
|
16
|
+
throw new CliError("Nothing to uninstall.", "invalid_arguments");
|
|
17
|
+
const results = [];
|
|
18
|
+
for (const spec of specs)
|
|
19
|
+
results.push(await remove_from(spec));
|
|
20
|
+
if (is_json_mode()) {
|
|
21
|
+
emit({ ok: true, results });
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
|
24
|
+
for (const result of results) {
|
|
25
|
+
const label = CLIENTS[result.client].label;
|
|
26
|
+
if (!result.mcp_removed && !result.hooks_removed) {
|
|
27
|
+
out(dim(` ${label}: nothing from Nemu was installed`));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
success(`${label}: removed${result.mcp_removed ? " the MCP server" : ""}${result.mcp_removed && result.hooks_removed ? " and" : ""}${result.hooks_removed ? " the hooks" : ""}`);
|
|
31
|
+
}
|
|
32
|
+
out(dim(" Your saved Context stays in your Nemu account. Sign out with nemu logout."));
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
package/dist/config.js
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
import { homedir } from "node:os";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import { mkdir, readFile, writeFile, rm, chmod } from "node:fs/promises";
|
|
3
|
+
import { mkdir, readFile, writeFile, rm, chmod, rename } from "node:fs/promises";
|
|
4
|
+
import { z } from "zod";
|
|
4
5
|
export const DEFAULT_API_URL = "https://app.nemu.cc";
|
|
5
6
|
export const DEFAULT_CLIENT_ID = "nemu_cli";
|
|
6
7
|
export const API_VERSION = "2026-07-14";
|
|
8
|
+
const credentials_schema = z.object({
|
|
9
|
+
access_token: z.string().min(1),
|
|
10
|
+
refresh_token: z.string().min(1).optional(),
|
|
11
|
+
expires_at: z.number(),
|
|
12
|
+
scope: z.string(),
|
|
13
|
+
api_url: z.string().url(),
|
|
14
|
+
account: z.string().optional(),
|
|
15
|
+
});
|
|
16
|
+
const settings_schema = z.object({
|
|
17
|
+
device_id: z
|
|
18
|
+
.string()
|
|
19
|
+
.regex(/^dev_[a-z2-7]{12}$/)
|
|
20
|
+
.optional(),
|
|
21
|
+
device_api_url: z.string().url().optional(),
|
|
22
|
+
reports: z.boolean().default(true),
|
|
23
|
+
});
|
|
7
24
|
export function api_url() {
|
|
8
|
-
return process.env.NEMU_API_URL ?? DEFAULT_API_URL;
|
|
25
|
+
return (process.env.NEMU_API_URL ?? DEFAULT_API_URL).replace(/\/+$/, "");
|
|
9
26
|
}
|
|
10
27
|
export function client_id() {
|
|
11
28
|
return process.env.NEMU_CLIENT_ID ?? DEFAULT_CLIENT_ID;
|
|
@@ -16,32 +33,31 @@ export function config_dir() {
|
|
|
16
33
|
function credentials_path() {
|
|
17
34
|
return join(config_dir(), "auth.json");
|
|
18
35
|
}
|
|
19
|
-
|
|
36
|
+
function settings_path() {
|
|
37
|
+
return join(config_dir(), "settings.json");
|
|
38
|
+
}
|
|
39
|
+
export async function write_private(path, contents) {
|
|
40
|
+
await mkdir(config_dir(), { recursive: true, mode: 0o700 });
|
|
41
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
42
|
+
await writeFile(temporary, contents, { encoding: "utf-8", mode: 0o600 });
|
|
43
|
+
await chmod(temporary, 0o600).catch(() => undefined);
|
|
44
|
+
await rename(temporary, path);
|
|
45
|
+
}
|
|
46
|
+
async function read_json(path) {
|
|
20
47
|
try {
|
|
21
|
-
|
|
22
|
-
const parsed = JSON.parse(raw);
|
|
23
|
-
if (typeof parsed.access_token !== "string")
|
|
24
|
-
return null;
|
|
25
|
-
return parsed;
|
|
48
|
+
return JSON.parse(await readFile(path, "utf-8"));
|
|
26
49
|
}
|
|
27
50
|
catch {
|
|
28
51
|
return null;
|
|
29
52
|
}
|
|
30
53
|
}
|
|
54
|
+
export async function read_credentials() {
|
|
55
|
+
const parsed = credentials_schema.safeParse(await read_json(credentials_path()));
|
|
56
|
+
return parsed.success ? parsed.data : null;
|
|
57
|
+
}
|
|
31
58
|
export async function write_credentials(credentials) {
|
|
32
|
-
const directory = config_dir();
|
|
33
|
-
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
34
59
|
const path = credentials_path();
|
|
35
|
-
await
|
|
36
|
-
encoding: "utf-8",
|
|
37
|
-
mode: 0o600,
|
|
38
|
-
});
|
|
39
|
-
try {
|
|
40
|
-
await chmod(path, 0o600);
|
|
41
|
-
}
|
|
42
|
-
catch {
|
|
43
|
-
return path;
|
|
44
|
-
}
|
|
60
|
+
await write_private(path, JSON.stringify(credentials, null, 2));
|
|
45
61
|
return path;
|
|
46
62
|
}
|
|
47
63
|
export async function clear_credentials() {
|
|
@@ -53,6 +69,18 @@ export async function clear_credentials() {
|
|
|
53
69
|
return false;
|
|
54
70
|
}
|
|
55
71
|
}
|
|
72
|
+
export async function read_settings() {
|
|
73
|
+
const parsed = settings_schema.safeParse((await read_json(settings_path())) ?? {});
|
|
74
|
+
return parsed.success ? parsed.data : { reports: true };
|
|
75
|
+
}
|
|
76
|
+
export async function update_settings(change) {
|
|
77
|
+
const next = { ...(await read_settings()), ...change };
|
|
78
|
+
await write_private(settings_path(), JSON.stringify(next, null, 2));
|
|
79
|
+
return next;
|
|
80
|
+
}
|
|
56
81
|
export function is_expired(credentials) {
|
|
57
82
|
return Date.now() >= credentials.expires_at;
|
|
58
83
|
}
|
|
84
|
+
export function has_scope(credentials, scope) {
|
|
85
|
+
return credentials.scope.split(/\s+/).includes(scope);
|
|
86
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { api } from "../http.js";
|
|
3
|
+
const date = z.string().min(1);
|
|
4
|
+
export const project_schema = z.object({
|
|
5
|
+
id: z.string(),
|
|
6
|
+
kind: z.string(),
|
|
7
|
+
name: z.string(),
|
|
8
|
+
root_commit: z.string().nullable(),
|
|
9
|
+
remote_url: z.string().nullable(),
|
|
10
|
+
linked_computer_project_id: z.string().nullable(),
|
|
11
|
+
created_at: date,
|
|
12
|
+
updated_at: date,
|
|
13
|
+
last_used_at: date.nullable(),
|
|
14
|
+
entry_count: z.number().optional(),
|
|
15
|
+
});
|
|
16
|
+
export const entry_schema = z.object({
|
|
17
|
+
id: z.string(),
|
|
18
|
+
project_id: z.string(),
|
|
19
|
+
name: z.string(),
|
|
20
|
+
tag: z.string(),
|
|
21
|
+
subject: z.string(),
|
|
22
|
+
body: z.string(),
|
|
23
|
+
state: z.string(),
|
|
24
|
+
refs: z.array(z.string()),
|
|
25
|
+
branch: z.string().nullable(),
|
|
26
|
+
commit: z.string().nullable(),
|
|
27
|
+
origin: z.object({ kind: z.string(), id: z.string().nullable() }),
|
|
28
|
+
client: z.string().nullable(),
|
|
29
|
+
pinned: z.boolean(),
|
|
30
|
+
stale: z.boolean(),
|
|
31
|
+
created_at: date,
|
|
32
|
+
updated_at: date,
|
|
33
|
+
});
|
|
34
|
+
const load_schema = z.object({
|
|
35
|
+
project: z.object({ id: z.string(), name: z.string() }),
|
|
36
|
+
branch: z.string().nullable(),
|
|
37
|
+
handoff: entry_schema.nullable().optional(),
|
|
38
|
+
pinned: z.array(entry_schema).optional(),
|
|
39
|
+
index: z
|
|
40
|
+
.array(z.object({
|
|
41
|
+
name: z.string(),
|
|
42
|
+
stale: z.boolean(),
|
|
43
|
+
refs: z.array(z.string()).optional(),
|
|
44
|
+
commit: z.string().nullable().optional(),
|
|
45
|
+
}))
|
|
46
|
+
.optional(),
|
|
47
|
+
truncated: z.boolean(),
|
|
48
|
+
text: z.string(),
|
|
49
|
+
});
|
|
50
|
+
const device_schema = z.object({
|
|
51
|
+
id: z.string().regex(/^dev_[a-z2-7]{12}$/),
|
|
52
|
+
name: z.string(),
|
|
53
|
+
});
|
|
54
|
+
export async function open_project(identity) {
|
|
55
|
+
const result = await api("POST", "/api/v1/context/projects", z.object({ data: project_schema }), { body: identity });
|
|
56
|
+
return result.data;
|
|
57
|
+
}
|
|
58
|
+
export async function list_projects() {
|
|
59
|
+
const result = await api("GET", "/api/v1/context/projects", z.object({ data: z.array(project_schema) }));
|
|
60
|
+
return result.data;
|
|
61
|
+
}
|
|
62
|
+
export async function load_project(project_id, branch) {
|
|
63
|
+
const result = await api("GET", `/api/v1/context/projects/${encodeURIComponent(project_id)}/load`, z.object({ data: load_schema }), { query: { branch } });
|
|
64
|
+
return result.data;
|
|
65
|
+
}
|
|
66
|
+
export async function search_project(project_id, query, limit = 10) {
|
|
67
|
+
const result = await api("GET", `/api/v1/context/projects/${encodeURIComponent(project_id)}/search`, z.object({ data: z.array(entry_schema) }), { query: { q: query, limit } });
|
|
68
|
+
return result.data;
|
|
69
|
+
}
|
|
70
|
+
export async function write_batch(project_id, entries) {
|
|
71
|
+
const result = await api("POST", `/api/v1/context/projects/${encodeURIComponent(project_id)}/entries/batch`, z.object({
|
|
72
|
+
data: z.array(z.object({
|
|
73
|
+
name: z.string(),
|
|
74
|
+
status: z.enum(["created", "updated", "skipped"]),
|
|
75
|
+
reason: z.string().optional(),
|
|
76
|
+
redacted: z.boolean().optional(),
|
|
77
|
+
})),
|
|
78
|
+
}), { body: { entries } });
|
|
79
|
+
return result.data;
|
|
80
|
+
}
|
|
81
|
+
export async function register_device(input) {
|
|
82
|
+
const result = await api("POST", "/api/v1/context/devices", z.object({ data: device_schema }), { body: input, device: false });
|
|
83
|
+
return result.data;
|
|
84
|
+
}
|
|
85
|
+
export async function mark_stale(project_id, stale, fresh) {
|
|
86
|
+
if (stale.length === 0 && fresh.length === 0)
|
|
87
|
+
return;
|
|
88
|
+
await api("POST", `/api/v1/context/projects/${encodeURIComponent(project_id)}/stale`, z.object({ marked: z.number(), cleared: z.number() }), { body: { stale: stale.slice(0, 400), fresh: fresh.slice(0, 400) } });
|
|
89
|
+
}
|
|
90
|
+
export async function list_devices() {
|
|
91
|
+
const result = await api("GET", "/api/v1/context/devices", z.object({
|
|
92
|
+
data: z.array(z.object({ id: z.string(), name: z.string(), revoked_at: z.string().nullable() })),
|
|
93
|
+
}), { device: false });
|
|
94
|
+
return result.data;
|
|
95
|
+
}
|
|
96
|
+
export async function export_all() {
|
|
97
|
+
const result = await api("GET", "/api/v1/context/export", z.object({ data: z.unknown() }));
|
|
98
|
+
return result.data;
|
|
99
|
+
}
|