claude-rpc 1.5.2 → 1.5.4

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.
Files changed (46) hide show
  1. package/README.md +34 -24
  2. package/SECURITY.md +99 -20
  3. package/package.json +1 -1
  4. package/src/atomic-rename.js +45 -2
  5. package/src/badge.js +3 -4
  6. package/src/card.js +1 -22
  7. package/src/cli-path.js +286 -0
  8. package/src/cli.js +310 -383
  9. package/src/community.js +101 -60
  10. package/src/config.js +4 -1
  11. package/src/daemon.js +45 -47
  12. package/src/default-config.js +4 -3
  13. package/src/discord-ipc.js +24 -13
  14. package/src/doctor.js +63 -37
  15. package/src/ensure-daemon.js +11 -4
  16. package/src/fmt.js +8 -6
  17. package/src/format.js +8 -13
  18. package/src/gist.js +8 -18
  19. package/src/git.js +4 -1
  20. package/src/hook.js +27 -32
  21. package/src/insights.js +3 -4
  22. package/src/install.js +100 -79
  23. package/src/leaderboard.js +3 -2
  24. package/src/mcp.js +2 -8
  25. package/src/nudge.js +8 -3
  26. package/src/pricing.js +24 -2
  27. package/src/privacy.js +22 -7
  28. package/src/profile.js +0 -6
  29. package/src/recap.js +4 -17
  30. package/src/scanner.js +103 -71
  31. package/src/server/api.js +64 -36
  32. package/src/server/assets/dashboard.client.js +75 -44
  33. package/src/server/assets/dashboard.css +18 -18
  34. package/src/server/assets/dashboard.html +13 -15
  35. package/src/server/assets/wrapped.client.js +26 -11
  36. package/src/server/assets/wrapped.css +2 -2
  37. package/src/server/index.js +43 -32
  38. package/src/server/page.js +6 -3
  39. package/src/server/sse.js +90 -34
  40. package/src/session-card.js +3 -1
  41. package/src/state.js +14 -24
  42. package/src/tui.js +3 -12
  43. package/src/ui.js +7 -5
  44. package/src/verify.js +3 -7
  45. package/src/version.js +1 -1
  46. package/src/week.js +4 -4
@@ -0,0 +1,286 @@
1
+ // Puts a packaged install's `claude-rpc` on the shell's PATH.
2
+ //
3
+ // A packaged install (the release exe/binary, or the CLI the desktop app
4
+ // bundles) copies itself to CANONICAL_INSTALL_DIR (<config dir>/bin), which no
5
+ // shell searches. So every `claude-rpc <command>` that setup, doctor and the
6
+ // docs tell people to run came back "command not found" / "is not recognized".
7
+ // npm, Homebrew and install.sh installs are already on PATH and are left alone.
8
+ //
9
+ // Windows → append CANONICAL_INSTALL_DIR to the user PATH
10
+ // (HKCU\Environment), keeping the value's registry kind. New
11
+ // terminals see it; the one that ran setup doesn't.
12
+ // macOS/Linux → symlink claude-rpc → the canonical exe in ~/.local/bin (or,
13
+ // on macOS, /usr/local/bin when only that is on PATH). If
14
+ // neither is on PATH, say which line adds ~/.local/bin.
15
+ //
16
+ // CLAUDE_RPC_NO_PATH=1 skips all of it.
17
+
18
+ import {
19
+ accessSync,
20
+ constants,
21
+ existsSync,
22
+ lstatSync,
23
+ mkdirSync,
24
+ readlinkSync,
25
+ realpathSync,
26
+ statSync,
27
+ symlinkSync,
28
+ unlinkSync,
29
+ } from 'node:fs';
30
+ import { join } from 'node:path';
31
+ import { homedir } from 'node:os';
32
+ import { spawnSync } from 'node:child_process';
33
+ import { CANONICAL_EXE, CANONICAL_INSTALL_DIR, CANONICAL_EXE_NAME } from './paths.js';
34
+
35
+ export const POSIX_LINK_DIR = join(homedir(), '.local', 'bin');
36
+ export const POSIX_LINK = join(POSIX_LINK_DIR, 'claude-rpc');
37
+
38
+ const isWin = (platform) => platform === 'win32';
39
+ const pathSep = (platform) => (isWin(platform) ? ';' : ':');
40
+
41
+ function normDir(p, platform) {
42
+ let s = String(p)
43
+ .trim()
44
+ .replace(/^"(.*)"$/, '$1');
45
+ if (isWin(platform)) return s.replace(/\//g, '\\').replace(/\\+$/, '').toLowerCase();
46
+ return s.length > 1 ? s.replace(/\/+$/, '') : s;
47
+ }
48
+
49
+ // Is `dir` one of the entries of a PATH string? Case-insensitive on Windows,
50
+ // trailing separators and surrounding quotes ignored.
51
+ export function pathHasDir(pathValue, dir, platform = process.platform) {
52
+ const want = normDir(dir, platform);
53
+ return String(pathValue || '')
54
+ .split(pathSep(platform))
55
+ .some((e) => e.trim() && normDir(e, platform) === want);
56
+ }
57
+
58
+ function defaultIsFile(p) {
59
+ try {
60
+ return statSync(p).isFile();
61
+ } catch {
62
+ return false;
63
+ }
64
+ }
65
+
66
+ // The first `name` a shell would run for this PATH (PATHEXT on Windows), or null.
67
+ export function findOnPath(
68
+ name = 'claude-rpc',
69
+ {
70
+ PATH = process.env.PATH,
71
+ PATHEXT = process.env.PATHEXT,
72
+ platform = process.platform,
73
+ isFile = defaultIsFile,
74
+ } = {},
75
+ ) {
76
+ const win = isWin(platform);
77
+ const exts = win
78
+ ? String(PATHEXT || '.COM;.EXE;.BAT;.CMD')
79
+ .split(';')
80
+ .filter(Boolean)
81
+ : [''];
82
+ for (const raw of String(PATH || '').split(pathSep(platform))) {
83
+ const dir = raw.trim().replace(/^"(.*)"$/, '$1');
84
+ if (!dir) continue;
85
+ for (const ext of exts) {
86
+ const p = `${dir.replace(/[\\/]+$/, '')}${win ? '\\' : '/'}${name}${ext}`;
87
+ if (isFile(p)) return p;
88
+ }
89
+ }
90
+ return null;
91
+ }
92
+
93
+ // PowerShell that adds `dir` to (or removes it from) the user PATH. It reads
94
+ // the raw, unexpanded value and writes it back with its original registry
95
+ // kind: [Environment]::SetEnvironmentVariable would flatten REG_EXPAND_SZ
96
+ // entries such as %USERPROFILE%\bin into literal text. Setting and clearing a
97
+ // throwaway variable through that API afterwards broadcasts WM_SETTINGCHANGE,
98
+ // so new terminals pick the change up. Prints present | absent | changed.
99
+ export function windowsPathScript(dir, action) {
100
+ const q = (s) => `'${String(s).replace(/'/g, "''")}'`;
101
+ const same = `$_.TrimEnd('\\') -ieq $dir.TrimEnd('\\')`;
102
+ return [
103
+ `$ErrorActionPreference = 'Stop'`,
104
+ `$dir = ${q(dir)}`,
105
+ `$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true)`,
106
+ `$cur = [string]$key.GetValue('Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)`,
107
+ `$kind = if ($key.GetValueNames() -contains 'Path') { $key.GetValueKind('Path') } else { [Microsoft.Win32.RegistryValueKind]::ExpandString }`,
108
+ `$parts = @($cur -split ';' | Where-Object { $_ -ne '' })`,
109
+ action === 'add'
110
+ ? `if (@($parts | Where-Object { ${same} }).Count -gt 0) { 'present'; exit 0 }\n$new = (@($parts) + @($dir)) -join ';'`
111
+ : `$keep = @($parts | Where-Object { -not (${same}) })\nif ($keep.Count -eq $parts.Count) { 'absent'; exit 0 }\n$new = $keep -join ';'`,
112
+ `$key.SetValue('Path', $new, $kind)`,
113
+ `$key.Close()`,
114
+ `[Environment]::SetEnvironmentVariable('CLAUDE_RPC_PATH_SYNC', '1', 'User')`,
115
+ `[Environment]::SetEnvironmentVariable('CLAUDE_RPC_PATH_SYNC', $null, 'User')`,
116
+ `'changed'`,
117
+ ].join('\n');
118
+ }
119
+
120
+ function runWindowsPathScript(dir, action) {
121
+ const encoded = Buffer.from(windowsPathScript(dir, action), 'utf16le').toString('base64');
122
+ const r = spawnSync(
123
+ 'powershell.exe',
124
+ ['-NoProfile', '-NonInteractive', '-EncodedCommand', encoded],
125
+ {
126
+ windowsHide: true,
127
+ encoding: 'utf8',
128
+ timeout: 20_000,
129
+ },
130
+ );
131
+ if (r.error) return { ok: false, detail: r.error.message };
132
+ const out = String(r.stdout || '')
133
+ .trim()
134
+ .split(/\r?\n/)
135
+ .pop();
136
+ if (r.status !== 0 || !['present', 'absent', 'changed'].includes(out)) {
137
+ return {
138
+ ok: false,
139
+ detail: String(r.stderr || r.stdout || `powershell exit ${r.status}`)
140
+ .trim()
141
+ .split(/\r?\n/)[0],
142
+ };
143
+ }
144
+ return { ok: true, result: out };
145
+ }
146
+
147
+ const quote = (p) => `"${p}"`;
148
+
149
+ function samePath(a, b) {
150
+ try {
151
+ return realpathSync(a) === realpathSync(b);
152
+ } catch {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ // Make `claude-rpc` resolvable for the packaged exe at `exe`. Returns
158
+ // { status, cmd, detail, newTerminal }
159
+ // status: already | added / present (Windows user PATH, now / from an earlier
160
+ // setup) | linked (~/.local/bin) | needs-path (~/.local/bin isn't on
161
+ // PATH) | skipped | failed.
162
+ // cmd: what to type to run claude-rpc right now (the bare name when it
163
+ // resolves, else the full quoted path).
164
+ export function ensureCliOnPath({
165
+ exe = CANONICAL_EXE,
166
+ env = process.env,
167
+ platform = process.platform,
168
+ } = {}) {
169
+ const found = findOnPath('claude-rpc', { PATH: env.PATH, PATHEXT: env.PATHEXT, platform });
170
+ if (found) return { status: 'already', cmd: 'claude-rpc', detail: found, newTerminal: false };
171
+ if (env.CLAUDE_RPC_NO_PATH === '1') {
172
+ return {
173
+ status: 'skipped',
174
+ cmd: quote(exe),
175
+ detail: 'CLAUDE_RPC_NO_PATH=1',
176
+ newTerminal: false,
177
+ };
178
+ }
179
+
180
+ if (isWin(platform)) {
181
+ const r = runWindowsPathScript(CANONICAL_INSTALL_DIR, 'add');
182
+ if (!r.ok) return { status: 'failed', cmd: quote(exe), detail: r.detail, newTerminal: false };
183
+ // 'present': an earlier setup added it; this terminal just predates that.
184
+ return {
185
+ status: r.result === 'present' ? 'present' : 'added',
186
+ cmd: 'claude-rpc',
187
+ detail: CANONICAL_INSTALL_DIR,
188
+ newTerminal: true,
189
+ };
190
+ }
191
+
192
+ // Where the link goes: ~/.local/bin when it's on PATH; on macOS, where it
193
+ // usually isn't by default, /usr/local/bin when that is on PATH and writable
194
+ // (Intel Homebrew, or an admin user); otherwise ~/.local/bin anyway, plus a
195
+ // hint for adding it.
196
+ const linkDir = pickLinkDir(env, platform);
197
+ const link = join(linkDir, 'claude-rpc');
198
+ const onPath = pathHasDir(env.PATH, linkDir, platform);
199
+ try {
200
+ mkdirSync(linkDir, { recursive: true });
201
+ let st = null;
202
+ try {
203
+ st = lstatSync(link);
204
+ } catch {
205
+ /* nothing there yet */
206
+ }
207
+ if (st && !st.isSymbolicLink()) {
208
+ // A real file, e.g. install.sh's copy of the binary. Never overwritten.
209
+ return {
210
+ status: onPath ? 'already' : 'needs-path',
211
+ cmd: quote(exe),
212
+ detail: `${link} (existing file, left alone)`,
213
+ newTerminal: false,
214
+ };
215
+ }
216
+ if (st && !(readlinkSync(link) === exe || samePath(link, exe))) {
217
+ // Another link: replace it only when it's dangling (an old install).
218
+ if (existsSync(link)) {
219
+ return {
220
+ status: 'failed',
221
+ cmd: quote(exe),
222
+ detail: `${link} points elsewhere — left alone`,
223
+ newTerminal: false,
224
+ };
225
+ }
226
+ unlinkSync(link);
227
+ st = null;
228
+ }
229
+ if (!st) symlinkSync(exe, link);
230
+ } catch (e) {
231
+ return { status: 'failed', cmd: quote(exe), detail: e.message, newTerminal: false };
232
+ }
233
+ if (onPath) return { status: 'linked', cmd: 'claude-rpc', detail: link, newTerminal: false };
234
+ return { status: 'needs-path', cmd: '~/.local/bin/claude-rpc', detail: link, newTerminal: false };
235
+ }
236
+
237
+ const MAC_SYSTEM_LINK_DIR = '/usr/local/bin';
238
+
239
+ function writable(dir) {
240
+ try {
241
+ accessSync(dir, constants.W_OK);
242
+ return true;
243
+ } catch {
244
+ return false;
245
+ }
246
+ }
247
+
248
+ export function pickLinkDir(env = process.env, platform = process.platform, canWrite = writable) {
249
+ if (pathHasDir(env.PATH, POSIX_LINK_DIR, platform)) return POSIX_LINK_DIR;
250
+ if (
251
+ platform === 'darwin' &&
252
+ pathHasDir(env.PATH, MAC_SYSTEM_LINK_DIR, platform) &&
253
+ canWrite(MAC_SYSTEM_LINK_DIR)
254
+ ) {
255
+ return MAC_SYSTEM_LINK_DIR;
256
+ }
257
+ return POSIX_LINK_DIR;
258
+ }
259
+
260
+ // The shell line that puts ~/.local/bin on PATH for new terminals.
261
+ export function pathExportHint(platform = process.platform) {
262
+ const rc = platform === 'darwin' ? '~/.zprofile' : '~/.profile';
263
+ return `echo 'export PATH="$HOME/.local/bin:$PATH"' >> ${rc}`;
264
+ }
265
+
266
+ // Undo ensureCliOnPath: our claude-rpc link, or our user-PATH entry. Leaves
267
+ // anything that isn't ours alone. Returns true when something was removed.
268
+ export function removeCliFromPath({ exe = CANONICAL_EXE, platform = process.platform } = {}) {
269
+ if (isWin(platform)) {
270
+ if (!existsSync(join(CANONICAL_INSTALL_DIR, CANONICAL_EXE_NAME))) return false;
271
+ const r = runWindowsPathScript(CANONICAL_INSTALL_DIR, 'remove');
272
+ return r.ok && r.result === 'changed';
273
+ }
274
+ let removed = false;
275
+ for (const link of [POSIX_LINK, join(MAC_SYSTEM_LINK_DIR, 'claude-rpc')]) {
276
+ try {
277
+ if (lstatSync(link).isSymbolicLink() && readlinkSync(link) === exe) {
278
+ unlinkSync(link);
279
+ removed = true;
280
+ }
281
+ } catch {
282
+ /* no link here */
283
+ }
284
+ }
285
+ return removed;
286
+ }