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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
Nemu Context for coding agents. `nemu setup claude` (aliases `cc`, `claude-code`) and
|
|
6
|
+
`nemu setup codex` connect the client through its own `mcp add` command and optionally install hooks
|
|
7
|
+
that load project notes at session start and ask for a handoff before stopping. `nemu claude` and
|
|
8
|
+
`nemu codex` set up if needed and start the client.
|
|
9
|
+
|
|
10
|
+
`nemu mcp` is a stdio MCP bridge for any client, using the login and refreshing it; `nemu mcp config`
|
|
11
|
+
prints config for Cursor, Gemini CLI, VS Code, Windsurf, Zed and remote HTTP clients.
|
|
12
|
+
|
|
13
|
+
`nemu claude load memories` imports Claude Code's saved memories. `load sessions` hands your recent
|
|
14
|
+
sessions to your own agent to summarise. `nemu context` shows, searches and exports notes.
|
|
15
|
+
|
|
16
|
+
Login now registers this computer as a device. Token refresh takes a lock, so several sessions
|
|
17
|
+
refreshing at once no longer sign each other out. `nemu status`, `nemu uninstall`, `nemu token`.
|
|
18
|
+
|
|
19
|
+
Failures after login send a scrubbed report and print an `NMU-` reference for support. Every file,
|
|
20
|
+
response and hook input is validated.
|
|
21
|
+
|
|
3
22
|
## 1.0.0
|
|
4
23
|
|
|
5
24
|
First real release. `login` signs in with the device grant, so nothing is pasted
|
package/README.md
CHANGED
|
@@ -127,10 +127,60 @@ nemu api POST api/v1/providers --data '{"name":"x","priority":1}'
|
|
|
127
127
|
`api` signs the request with your token and prints the raw JSON, so an endpoint with no dedicated
|
|
128
128
|
command is still one line away.
|
|
129
129
|
|
|
130
|
+
## Context for coding agents
|
|
131
|
+
|
|
132
|
+
Nemu Context keeps notes about a project (decisions, gotchas, conventions, and a handoff saying where
|
|
133
|
+
work stopped) and gives them back to whichever agent opens the project next, on any machine. Your own
|
|
134
|
+
agent reads and writes them; Nemu never calls a model.
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm i -g nemu-cli # clients start nemu on every session, so install it globally
|
|
138
|
+
nemu login
|
|
139
|
+
nemu setup claude # or: nemu setup codex (aliases: cc, claude-code)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Setup registers a `nemu` MCP server with the client through the client's own command
|
|
143
|
+
(`claude mcp add --scope user`, `codex mcp add`) and asks whether to install hooks. Hooks load the
|
|
144
|
+
project's notes when a session starts and ask the agent to save a handoff before it stops. Codex runs
|
|
145
|
+
hooks only after you trust them once with `/hooks`. Pass `--hooks`, `--no-hooks` or `--yes` to skip
|
|
146
|
+
the question.
|
|
147
|
+
|
|
148
|
+
| Command | Does |
|
|
149
|
+
| --- | --- |
|
|
150
|
+
| `nemu claude [-- args]` | Set up if needed, then start Claude Code with the args after `--` |
|
|
151
|
+
| `nemu codex [-- args]` | The same for Codex |
|
|
152
|
+
| `nemu claude load memories [--all] [--yes]` | Import Claude Code's saved memories for this project (or all) |
|
|
153
|
+
| `nemu claude load sessions [--limit n]` | Open Claude with your recent sessions and let it save what matters |
|
|
154
|
+
| `nemu codex load sessions [--limit n]` | The same for Codex |
|
|
155
|
+
| `nemu mcp` | The stdio MCP bridge clients start; it uses your login and refreshes it |
|
|
156
|
+
| `nemu mcp config <client>` | Config for `cursor`, `gemini`, `vscode`, `windsurf`, `zed`, `claude`, `codex`, `remote` |
|
|
157
|
+
| `nemu context` | What an agent sees for this folder |
|
|
158
|
+
| `nemu context search <words>` | Search this project's notes |
|
|
159
|
+
| `nemu context projects` / `export --out file` | List projects, or export everything as JSON |
|
|
160
|
+
| `nemu status` | Sign in, this device, global install, and each client's setup |
|
|
161
|
+
| `nemu uninstall [claude\|codex\|all]` | Remove exactly what setup added |
|
|
162
|
+
| `nemu token [--header]` | Print a fresh access token |
|
|
163
|
+
|
|
164
|
+
A project is recognised by its first git commit, so the same repository shares notes on every machine
|
|
165
|
+
you sign in from. Session transcripts never leave your machine: `load sessions` hands them to your
|
|
166
|
+
own agent, which decides what to save. Secrets are removed before anything is saved, on your machine
|
|
167
|
+
and again on the server, and saved notes are always given to agents as data, never as instructions.
|
|
168
|
+
|
|
169
|
+
Each computer you sign in from is registered as a device. Revoke one in the console under Context to
|
|
170
|
+
stop it reading or writing; what it already saved stays.
|
|
171
|
+
|
|
172
|
+
## Error references
|
|
173
|
+
|
|
174
|
+
After you sign in, a command that fails sends a small report and prints a reference like
|
|
175
|
+
`NMU-7F3K-2Q9D` to quote if you contact support@nemu.cc. A report holds the command and flag names
|
|
176
|
+
(never their values), the CLI, OS and Node versions, and the error with secrets and your home folder
|
|
177
|
+
removed. Turn it off with `nemu config set reports off` or `NEMU_NO_REPORTS=1`.
|
|
178
|
+
|
|
130
179
|
## Deleting
|
|
131
180
|
|
|
132
181
|
Every destructive command needs `--yes`. Without it the CLI refuses and tells you what it was about
|
|
133
|
-
to remove.
|
|
182
|
+
to remove. Setup and imports ask before changing anything when run in a terminal, and never ask when
|
|
183
|
+
run from a script.
|
|
134
184
|
|
|
135
185
|
Some things the CLI deliberately cannot do. It never asks for `workspaces:delete` or `keys:write`, so
|
|
136
186
|
it cannot delete a workspace you reached through it or mint a gateway key. Workflows and metrics are
|
|
@@ -151,6 +201,9 @@ machine is not yours, revoke the grant in your account settings as well.
|
|
|
151
201
|
| `NEMU_API_URL` | `https://app.nemu.cc` | Point the CLI at another console |
|
|
152
202
|
| `NEMU_CLIENT_ID` | `nemu_cli` | Use a different OAuth client |
|
|
153
203
|
| `NEMU_CONFIG_DIR` | `~/.nemu` | Store the token somewhere else |
|
|
204
|
+
| `NEMU_NO_REPORTS` | unset | `1` turns off error reports |
|
|
205
|
+
| `NEMU_BIN` | unset | Command setup writes into client configs instead of the global `nemu` |
|
|
206
|
+
| `CLAUDE_CONFIG_DIR`, `CODEX_HOME` | client defaults | Honoured when setup writes hooks |
|
|
154
207
|
| `NO_COLOR` | unset | Turn off colour |
|
|
155
208
|
|
|
156
209
|
## If something fails
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { mkdir, rm, stat } from "node:fs/promises";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { client_id, config_dir, read_credentials, write_credentials, } from "../config.js";
|
|
5
|
+
import { CliError } from "../errors.js";
|
|
6
|
+
const EXPIRY_SKEW_MS = 90_000;
|
|
7
|
+
const LOCK_WAIT_MS = 15_000;
|
|
8
|
+
const LOCK_STALE_MS = 30_000;
|
|
9
|
+
const token_response = z.object({
|
|
10
|
+
access_token: z.string().min(1),
|
|
11
|
+
refresh_token: z.string().min(1).optional(),
|
|
12
|
+
expires_in: z.number().positive().optional(),
|
|
13
|
+
scope: z.string().optional(),
|
|
14
|
+
});
|
|
15
|
+
const token_error = z.object({
|
|
16
|
+
error: z.string().optional(),
|
|
17
|
+
error_description: z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
function lock_path() {
|
|
20
|
+
return join(config_dir(), "refresh.lock");
|
|
21
|
+
}
|
|
22
|
+
function sleep(ms) {
|
|
23
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
24
|
+
}
|
|
25
|
+
async function acquire_lock() {
|
|
26
|
+
await mkdir(config_dir(), { recursive: true, mode: 0o700 });
|
|
27
|
+
const path = lock_path();
|
|
28
|
+
const deadline = Date.now() + LOCK_WAIT_MS;
|
|
29
|
+
while (Date.now() < deadline) {
|
|
30
|
+
try {
|
|
31
|
+
await mkdir(path);
|
|
32
|
+
return async () => {
|
|
33
|
+
await rm(path, { recursive: true, force: true });
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
const info = await stat(path).catch(() => null);
|
|
38
|
+
if (info !== null && Date.now() - info.mtimeMs > LOCK_STALE_MS) {
|
|
39
|
+
await rm(path, { recursive: true, force: true });
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
await sleep(150);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new CliError("Another Nemu process is refreshing the sign in. Try again in a moment.", "refresh_lock_timeout");
|
|
46
|
+
}
|
|
47
|
+
function is_fresh(credentials) {
|
|
48
|
+
return Date.now() < credentials.expires_at - EXPIRY_SKEW_MS;
|
|
49
|
+
}
|
|
50
|
+
async function refresh(credentials) {
|
|
51
|
+
if (credentials.refresh_token === undefined) {
|
|
52
|
+
throw new CliError("Your sign in expired. Run nemu login.", "not_signed_in");
|
|
53
|
+
}
|
|
54
|
+
let response;
|
|
55
|
+
try {
|
|
56
|
+
response = await fetch(`${credentials.api_url}/api/oauth/token`, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
59
|
+
body: new URLSearchParams({
|
|
60
|
+
grant_type: "refresh_token",
|
|
61
|
+
refresh_token: credentials.refresh_token,
|
|
62
|
+
client_id: client_id(),
|
|
63
|
+
}).toString(),
|
|
64
|
+
signal: AbortSignal.timeout(15_000),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new CliError(`Could not reach ${credentials.api_url}.`, "network_failed");
|
|
69
|
+
}
|
|
70
|
+
const body = await response.json().catch(() => null);
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
const failure = token_error.safeParse(body);
|
|
73
|
+
const code = failure.success ? failure.data.error : undefined;
|
|
74
|
+
if (code === "invalid_grant" || response.status === 400 || response.status === 401) {
|
|
75
|
+
throw new CliError("Your sign in expired. Run nemu login.", "not_signed_in");
|
|
76
|
+
}
|
|
77
|
+
throw new CliError("Refreshing the sign in failed.", "refresh_failed", {
|
|
78
|
+
status: response.status,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const parsed = token_response.safeParse(body);
|
|
82
|
+
if (!parsed.success) {
|
|
83
|
+
throw new CliError("The sign in server sent an unexpected answer.", "refresh_invalid");
|
|
84
|
+
}
|
|
85
|
+
const next = {
|
|
86
|
+
...credentials,
|
|
87
|
+
access_token: parsed.data.access_token,
|
|
88
|
+
refresh_token: parsed.data.refresh_token ?? credentials.refresh_token,
|
|
89
|
+
expires_at: Date.now() + (parsed.data.expires_in ?? 3600) * 1000,
|
|
90
|
+
scope: parsed.data.scope ?? credentials.scope,
|
|
91
|
+
};
|
|
92
|
+
await write_credentials(next);
|
|
93
|
+
return next;
|
|
94
|
+
}
|
|
95
|
+
export async function fresh_credentials() {
|
|
96
|
+
const current = await read_credentials();
|
|
97
|
+
if (current === null) {
|
|
98
|
+
throw new CliError("Not signed in. Run nemu login.", "not_signed_in");
|
|
99
|
+
}
|
|
100
|
+
if (is_fresh(current))
|
|
101
|
+
return current;
|
|
102
|
+
const release = await acquire_lock();
|
|
103
|
+
try {
|
|
104
|
+
const latest = await read_credentials();
|
|
105
|
+
if (latest === null) {
|
|
106
|
+
throw new CliError("Not signed in. Run nemu login.", "not_signed_in");
|
|
107
|
+
}
|
|
108
|
+
if (is_fresh(latest))
|
|
109
|
+
return latest;
|
|
110
|
+
return await refresh(latest);
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
await release();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { readFile, writeFile, mkdir, rename, copyFile, access } from "node:fs/promises";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { CliError } from "../errors.js";
|
|
5
|
+
export const HOOK_SPECS = {
|
|
6
|
+
"claude-code": [
|
|
7
|
+
{ event: "SessionStart", hook: "start", matcher: "startup|resume|clear|compact", timeout: 20 },
|
|
8
|
+
{ event: "PostToolUse", hook: "tool", matcher: "Edit|Write|MultiEdit|NotebookEdit", timeout: 5 },
|
|
9
|
+
{ event: "Stop", hook: "stop", timeout: 10 },
|
|
10
|
+
{ event: "PreCompact", hook: "compact", matcher: "manual|auto", timeout: 5 },
|
|
11
|
+
{ event: "SessionEnd", hook: "end", timeout: 3 },
|
|
12
|
+
],
|
|
13
|
+
codex: [
|
|
14
|
+
{ event: "SessionStart", hook: "start", matcher: "^(startup|resume|clear|compact)$", timeout: 20 },
|
|
15
|
+
{ event: "PostToolUse", hook: "tool", timeout: 5 },
|
|
16
|
+
{ event: "Stop", hook: "stop", timeout: 10 },
|
|
17
|
+
{ event: "PreCompact", hook: "compact", timeout: 5 },
|
|
18
|
+
{ event: "SessionEnd", hook: "end", timeout: 3 },
|
|
19
|
+
],
|
|
20
|
+
};
|
|
21
|
+
const handler_schema = z
|
|
22
|
+
.object({ type: z.string().optional(), command: z.string().optional() })
|
|
23
|
+
.passthrough();
|
|
24
|
+
const group_schema = z
|
|
25
|
+
.object({ matcher: z.string().optional(), hooks: z.array(handler_schema).optional() })
|
|
26
|
+
.passthrough();
|
|
27
|
+
const config_schema = z
|
|
28
|
+
.object({ hooks: z.record(z.string(), z.array(group_schema)).optional() })
|
|
29
|
+
.passthrough();
|
|
30
|
+
const OWN_COMMAND = /(?:^|[\\/"'\s])nemu(?:\.cmd|\.exe)?["']?\s+hook\s+(?:start|tool|stop|compact|end)\b/;
|
|
31
|
+
export function is_own_command(command) {
|
|
32
|
+
return command !== undefined && OWN_COMMAND.test(command);
|
|
33
|
+
}
|
|
34
|
+
function quote(path) {
|
|
35
|
+
return /[\s"'$`\\]/.test(path) ? `"${path.replace(/(["\\$`])/g, "\\$1")}"` : path;
|
|
36
|
+
}
|
|
37
|
+
export function hook_command(nemu, hook, client) {
|
|
38
|
+
return `${quote(nemu)} hook ${hook} --client ${client}`;
|
|
39
|
+
}
|
|
40
|
+
async function exists(path) {
|
|
41
|
+
try {
|
|
42
|
+
await access(path);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export async function read_config(path) {
|
|
50
|
+
if (!(await exists(path)))
|
|
51
|
+
return {};
|
|
52
|
+
const text = await readFile(path, "utf-8");
|
|
53
|
+
if (text.trim().length === 0)
|
|
54
|
+
return {};
|
|
55
|
+
let raw;
|
|
56
|
+
try {
|
|
57
|
+
raw = JSON.parse(text);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
throw new CliError(`${path} is not valid JSON, so Nemu will not touch it. Fix or remove it and run setup again.`, "config_unreadable");
|
|
61
|
+
}
|
|
62
|
+
const parsed = config_schema.safeParse(raw);
|
|
63
|
+
if (!parsed.success) {
|
|
64
|
+
throw new CliError(`${path} has a "hooks" value Nemu does not recognise, so it was left alone.`, "config_unrecognised");
|
|
65
|
+
}
|
|
66
|
+
return parsed.data;
|
|
67
|
+
}
|
|
68
|
+
async function write_config(path, config) {
|
|
69
|
+
await mkdir(dirname(path), { recursive: true });
|
|
70
|
+
const backup = `${path}.nemu-backup`;
|
|
71
|
+
if ((await exists(path)) && !(await exists(backup)))
|
|
72
|
+
await copyFile(path, backup);
|
|
73
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
74
|
+
await writeFile(temporary, `${JSON.stringify(config, null, 2)}\n`, "utf-8");
|
|
75
|
+
await rename(temporary, path);
|
|
76
|
+
}
|
|
77
|
+
function without_own(config) {
|
|
78
|
+
const hooks = config.hooks ?? {};
|
|
79
|
+
const cleaned = {};
|
|
80
|
+
for (const [event, groups] of Object.entries(hooks)) {
|
|
81
|
+
const kept = groups
|
|
82
|
+
.map((group) => ({
|
|
83
|
+
...group,
|
|
84
|
+
hooks: (group.hooks ?? []).filter((handler) => !is_own_command(handler.command)),
|
|
85
|
+
}))
|
|
86
|
+
.filter((group) => group.hooks.length > 0);
|
|
87
|
+
if (kept.length > 0)
|
|
88
|
+
cleaned[event] = kept;
|
|
89
|
+
}
|
|
90
|
+
const { hooks: _previous, ...rest } = config;
|
|
91
|
+
return Object.keys(cleaned).length > 0 ? { ...rest, hooks: cleaned } : rest;
|
|
92
|
+
}
|
|
93
|
+
export async function install_hooks(path, nemu, client) {
|
|
94
|
+
const config = without_own(await read_config(path));
|
|
95
|
+
const hooks = { ...(config.hooks ?? {}) };
|
|
96
|
+
for (const spec of HOOK_SPECS[client]) {
|
|
97
|
+
const group = {
|
|
98
|
+
...(spec.matcher !== undefined ? { matcher: spec.matcher } : {}),
|
|
99
|
+
hooks: [{ type: "command", command: hook_command(nemu, spec.hook, client), timeout: spec.timeout }],
|
|
100
|
+
};
|
|
101
|
+
hooks[spec.event] = [...(hooks[spec.event] ?? []), group];
|
|
102
|
+
}
|
|
103
|
+
await write_config(path, { ...config, hooks });
|
|
104
|
+
}
|
|
105
|
+
function stable(value) {
|
|
106
|
+
if (Array.isArray(value))
|
|
107
|
+
return `[${value.map(stable).join(",")}]`;
|
|
108
|
+
if (value !== null && typeof value === "object") {
|
|
109
|
+
return `{${Object.keys(value)
|
|
110
|
+
.sort()
|
|
111
|
+
.map((key) => `${JSON.stringify(key)}:${stable(value[key])}`)
|
|
112
|
+
.join(",")}}`;
|
|
113
|
+
}
|
|
114
|
+
return JSON.stringify(value);
|
|
115
|
+
}
|
|
116
|
+
export async function remove_hooks(path) {
|
|
117
|
+
if (!(await exists(path)))
|
|
118
|
+
return false;
|
|
119
|
+
const config = await read_config(path);
|
|
120
|
+
const cleaned = without_own(config);
|
|
121
|
+
if (stable(cleaned) === stable(config))
|
|
122
|
+
return false;
|
|
123
|
+
await write_config(path, cleaned);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
export async function installed_hooks(path) {
|
|
127
|
+
const config = await read_config(path).catch(() => ({}));
|
|
128
|
+
return Object.entries(config.hooks ?? {})
|
|
129
|
+
.filter(([, groups]) => groups.some((group) => (group.hooks ?? []).some((handler) => is_own_command(handler.command))))
|
|
130
|
+
.map(([event]) => event);
|
|
131
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export function claude_dir() {
|
|
4
|
+
return process.env.CLAUDE_CONFIG_DIR ?? join(homedir(), ".claude");
|
|
5
|
+
}
|
|
6
|
+
export function claude_settings() {
|
|
7
|
+
return join(claude_dir(), "settings.json");
|
|
8
|
+
}
|
|
9
|
+
export function codex_dir() {
|
|
10
|
+
return process.env.CODEX_HOME ?? join(homedir(), ".codex");
|
|
11
|
+
}
|
|
12
|
+
export function codex_hooks() {
|
|
13
|
+
return join(codex_dir(), "hooks.json");
|
|
14
|
+
}
|
|
15
|
+
export function encode_claude_folder(path) {
|
|
16
|
+
return path.replace(/[^A-Za-z0-9]/g, "-");
|
|
17
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { CliError } from "../errors.js";
|
|
2
|
+
import { run } from "../system.js";
|
|
3
|
+
import { mkdir } from "node:fs/promises";
|
|
4
|
+
import { claude_settings, codex_dir, codex_hooks } from "./paths.js";
|
|
5
|
+
export const MCP_NAME = "nemu";
|
|
6
|
+
async function must(binary, args, what) {
|
|
7
|
+
const result = await run(binary, args);
|
|
8
|
+
if (result.code !== 0) {
|
|
9
|
+
const detail = (result.stderr || result.stdout).trim().split("\n")[0] ?? "";
|
|
10
|
+
throw new CliError(`${what} failed${detail.length > 0 ? `: ${detail}` : ""}`, "client_command_failed");
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export const CLIENTS = {
|
|
14
|
+
"claude-code": {
|
|
15
|
+
id: "claude-code",
|
|
16
|
+
label: "Claude Code",
|
|
17
|
+
binary: "claude",
|
|
18
|
+
install_hint: "Install Claude Code first: https://code.claude.com/docs/en/setup",
|
|
19
|
+
hooks_path: claude_settings,
|
|
20
|
+
after_setup: ["Start Claude Code with: nemu claude (or just claude)"],
|
|
21
|
+
add_mcp: async (nemu) => {
|
|
22
|
+
await run("claude", ["mcp", "remove", "--scope", "user", MCP_NAME]);
|
|
23
|
+
await must("claude", ["mcp", "add", "--scope", "user", MCP_NAME, "--", nemu, "mcp", "--client", "claude-code"], "Registering the Nemu MCP server with Claude Code");
|
|
24
|
+
},
|
|
25
|
+
remove_mcp: async () => {
|
|
26
|
+
const result = await run("claude", ["mcp", "remove", "--scope", "user", MCP_NAME]);
|
|
27
|
+
return result.code === 0;
|
|
28
|
+
},
|
|
29
|
+
has_mcp: async () => {
|
|
30
|
+
const result = await run("claude", ["mcp", "get", MCP_NAME]);
|
|
31
|
+
return result.code === 0 && /nemu/.test(result.stdout) && /\bmcp\b/.test(result.stdout);
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
codex: {
|
|
35
|
+
id: "codex",
|
|
36
|
+
label: "Codex",
|
|
37
|
+
binary: "codex",
|
|
38
|
+
install_hint: "Install Codex first: npm i -g @openai/codex",
|
|
39
|
+
hooks_path: codex_hooks,
|
|
40
|
+
after_setup: [
|
|
41
|
+
"Codex runs hooks only after you trust them: open Codex and run /hooks once.",
|
|
42
|
+
"Start Codex with: nemu codex (or just codex)",
|
|
43
|
+
],
|
|
44
|
+
add_mcp: async (nemu) => {
|
|
45
|
+
await mkdir(codex_dir(), { recursive: true });
|
|
46
|
+
await must("codex", ["mcp", "add", MCP_NAME, "--", nemu, "mcp", "--client", "codex"], "Registering the Nemu MCP server with Codex");
|
|
47
|
+
},
|
|
48
|
+
remove_mcp: async () => {
|
|
49
|
+
const result = await run("codex", ["mcp", "remove", MCP_NAME]);
|
|
50
|
+
return result.code === 0;
|
|
51
|
+
},
|
|
52
|
+
has_mcp: async () => {
|
|
53
|
+
const result = await run("codex", ["mcp", "get", MCP_NAME]);
|
|
54
|
+
return result.code === 0 && /\bmcp\b/.test(result.stdout);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
export function client_from_name(name) {
|
|
59
|
+
switch ((name ?? "").toLowerCase()) {
|
|
60
|
+
case "claude":
|
|
61
|
+
case "cc":
|
|
62
|
+
case "claude-code":
|
|
63
|
+
case "claudecode":
|
|
64
|
+
return CLIENTS["claude-code"];
|
|
65
|
+
case "codex":
|
|
66
|
+
return CLIENTS.codex;
|
|
67
|
+
default:
|
|
68
|
+
throw new CliError("Pick a client: claude (aliases cc, claude-code) or codex. Other clients: nemu mcp config <client>", "invalid_client");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
export async function client_installed(spec) {
|
|
72
|
+
const result = await run(spec.binary, ["--version"], { timeout_ms: 15_000 });
|
|
73
|
+
return result.code === 0;
|
|
74
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { client_from_name } from "../clients/registry.js";
|
|
2
|
+
import { launch } from "../system.js";
|
|
3
|
+
import { out, dim } from "../ui.js";
|
|
4
|
+
import { load } from "./load.js";
|
|
5
|
+
import { run_setup } from "./setup.js";
|
|
6
|
+
export function client_command(name) {
|
|
7
|
+
return async (args) => {
|
|
8
|
+
const spec = client_from_name(name);
|
|
9
|
+
if (args.positional[0] === "load")
|
|
10
|
+
return load(spec, args);
|
|
11
|
+
if (args.positional[0] === "setup")
|
|
12
|
+
return run_setup(spec, args).then(() => 0);
|
|
13
|
+
if (!(await spec.has_mcp())) {
|
|
14
|
+
out(dim(` Connecting ${spec.label} to Nemu Context first.`));
|
|
15
|
+
await run_setup(spec, args);
|
|
16
|
+
}
|
|
17
|
+
return launch(spec.binary, args.positional);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
import { flag, number_flag } from "../args.js";
|
|
3
|
+
import { CliError } from "../errors.js";
|
|
4
|
+
import { export_all, list_projects, load_project, search_project } from "../context/api.js";
|
|
5
|
+
import { current_project } from "../context/project.js";
|
|
6
|
+
import { bold, dim, out, success } from "../ui.js";
|
|
7
|
+
import { emit, is_json_mode, table } from "../output.js";
|
|
8
|
+
export async function context(args) {
|
|
9
|
+
const action = args.positional[0] ?? "load";
|
|
10
|
+
switch (action) {
|
|
11
|
+
case "load": {
|
|
12
|
+
const project = await current_project(process.cwd(), false);
|
|
13
|
+
const loaded = await load_project(project.id, project.repository.branch);
|
|
14
|
+
if (is_json_mode()) {
|
|
15
|
+
emit(loaded);
|
|
16
|
+
return 0;
|
|
17
|
+
}
|
|
18
|
+
out(loaded.text);
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
case "search": {
|
|
22
|
+
const query = args.positional.slice(1).join(" ").trim();
|
|
23
|
+
if (query.length === 0)
|
|
24
|
+
throw new CliError("Use: nemu context search <words>", "invalid_arguments");
|
|
25
|
+
const project = await current_project(process.cwd());
|
|
26
|
+
const entries = await search_project(project.id, query, number_flag(args, "limit") ?? 10);
|
|
27
|
+
if (is_json_mode()) {
|
|
28
|
+
emit(entries);
|
|
29
|
+
return 0;
|
|
30
|
+
}
|
|
31
|
+
if (entries.length === 0) {
|
|
32
|
+
out(dim(` Nothing saved matches "${query}".`));
|
|
33
|
+
return 0;
|
|
34
|
+
}
|
|
35
|
+
for (const entry of entries) {
|
|
36
|
+
out(` ${bold(entry.name)} ${entry.subject}`);
|
|
37
|
+
out(dim(` ${entry.body}`));
|
|
38
|
+
}
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
case "projects": {
|
|
42
|
+
const projects = await list_projects();
|
|
43
|
+
table(projects.map((project) => ({
|
|
44
|
+
id: project.id,
|
|
45
|
+
name: project.name,
|
|
46
|
+
remote: project.remote_url ?? "",
|
|
47
|
+
notes: project.entry_count ?? 0,
|
|
48
|
+
})), ["id", "name", "remote", "notes"], "No Context projects yet.");
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
case "export": {
|
|
52
|
+
const data = await export_all();
|
|
53
|
+
const path = flag(args, "out", "o");
|
|
54
|
+
if (path === undefined) {
|
|
55
|
+
emit(data);
|
|
56
|
+
return 0;
|
|
57
|
+
}
|
|
58
|
+
await writeFile(path, `${JSON.stringify(data, null, 2)}\n`, { encoding: "utf-8", mode: 0o600 });
|
|
59
|
+
success(`Exported to ${path}`);
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
default:
|
|
63
|
+
throw new CliError("Use: nemu context load | search <words> | projects | export [--out file]", "invalid_arguments");
|
|
64
|
+
}
|
|
65
|
+
}
|