@myrialabs/clopen 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,6 +57,7 @@ A complete development environment designed around AI-assisted workflows, built
57
57
  - **Cloudflare Tunnel** — One-click public HTTPS URL for your local dev server. Built-in QR code for instant mobile access. Share your work without deploying
58
58
  - **MCP Support** — Full Model Context Protocol integration. Connect AI tools, external APIs, and custom capabilities to your AI agents with zero friction
59
59
  - **Flexible Authentication** — No Login or With Login mode with admin/member roles, invite links, rate-limited login, and CLI token recovery
60
+ - **System Tools Installer** — Install Git, Claude Code, OpenCode, and Chrome straight from the admin Settings UI, with live install output streamed to the browser via xterm.js
60
61
  - **Background Processing** — Chat, terminal, and other processes continue running even when you close the browser — come back later and pick up where you left off
61
62
  - **Database Management** — Browse tables, run queries, and inspect your database directly from the workspace *(coming soon)*
62
63
 
@@ -87,7 +88,7 @@ Or manually via Bun:
87
88
  bun add -g @myrialabs/clopen
88
89
  ```
89
90
 
90
- You can also update from the **Settings > System > Updates** section in the web UI.
91
+ You can also update from the **Settings > Maintenance > Updates** section in the web UI.
91
92
 
92
93
  ### Usage
93
94
 
@@ -26,7 +26,13 @@ export const ADMIN_ONLY_ROUTES = new Set([
26
26
  'auth:revoke-invite',
27
27
  'auth:list-users',
28
28
  'auth:remove-user',
29
- 'settings:update-system'
29
+ 'settings:update-system',
30
+ // System Tools — binary installation is an admin-only operation.
31
+ 'system-tools:status',
32
+ 'system-tools:status-all',
33
+ 'system-tools:install-start',
34
+ 'system-tools:install-cancel',
35
+ 'system-tools:install-session'
30
36
  ]);
31
37
 
32
38
  /**
@@ -16,6 +16,7 @@ import type { Subprocess } from 'bun';
16
16
  import { getOpenCodeMcpConfig } from '../../../mcp';
17
17
  import { settingsQueries } from '../../../database/queries';
18
18
  import { generateOpenCodeProviderConfig } from './config';
19
+ import { resolveBinaryWithRefresh } from '$backend/utils/cli';
19
20
  import { debug } from '$shared/utils/logger';
20
21
 
21
22
  const OPENCODE_HOST = '127.0.0.1';
@@ -117,8 +118,10 @@ async function init(): Promise<void> {
117
118
  settingsQueries.delete(DB_KEY);
118
119
  }
119
120
 
120
- // 2. Spawn a new server via Bun.spawn with absolute binary path
121
- const command = Bun.which('opencode');
121
+ // 2. Spawn a new server via Bun.spawn with absolute binary path.
122
+ // Re-harvest PATH and fall back to known install locations so freshly
123
+ // installed binaries are picked up without a clopen restart.
124
+ const command = await resolveBinaryWithRefresh('opencode');
122
125
  if (!command) throw new Error('opencode binary not found on PATH');
123
126
  const args = [command, 'serve', `--hostname=${OPENCODE_HOST}`, '--port=0'];
124
127
 
@@ -0,0 +1,450 @@
1
+ /**
2
+ * System Tool Install Recipes
3
+ *
4
+ * Data-driven registry of install commands for system tools that clopen
5
+ * depends on (Git, Claude Code, OpenCode, Chrome for puppeteer). Each
6
+ * recipe is platform-aware and privilege-aware: when the runner cannot
7
+ * reasonably complete the install non-interactively (e.g. `apt` without
8
+ * root, `choco` without Administrator), the recipe is marked
9
+ * `autoInstallable: false` and the frontend renders a copy-command
10
+ * fallback instead of the install button.
11
+ *
12
+ * Recipes also carry manual instructions so the frontend can always
13
+ * surface a copy-able command and a docs link, regardless of platform.
14
+ */
15
+
16
+ import { homedir } from 'node:os';
17
+ import { existsSync, readdirSync } from 'node:fs';
18
+ import { join } from 'node:path';
19
+ import { isElevated } from '$backend/utils/privilege';
20
+ import { getClopenDir } from '$backend/utils/paths';
21
+ import { resolveBinary, resolveBinaryWithRefresh } from '$backend/utils/cli';
22
+
23
+ export type ToolId = 'git' | 'claude' | 'opencode' | 'chrome' | 'curl';
24
+
25
+ export interface ManualInstruction {
26
+ label: string;
27
+ command: string;
28
+ docs?: string;
29
+ }
30
+
31
+ export interface Recipe {
32
+ tool: ToolId;
33
+ autoInstallable: boolean;
34
+ /** Reason displayed when autoInstallable is false. */
35
+ unavailableReason?: string;
36
+ /** Spawn arg vector when autoInstallable. */
37
+ command?: string[];
38
+ /** Optional shell to wrap command (e.g. sh -c for pipe chains). */
39
+ shell?: { program: string; args: string[] };
40
+ /** Human-readable command string for confirmation dialog preview. */
41
+ displayCommand?: string;
42
+ /** Extra env vars for the install subprocess. */
43
+ env?: Record<string, string>;
44
+ /** Missing prerequisites (other tools that must be installed first). */
45
+ missingPrereqs: ToolId[];
46
+ /** Manual-install options shown regardless of autoInstallable. */
47
+ manualInstructions: ManualInstruction[];
48
+ }
49
+
50
+ export interface ToolStatus {
51
+ tool: ToolId;
52
+ installed: boolean;
53
+ version: string | null;
54
+ /** Where the binary was found (e.g. "/usr/bin/git", "system", "~/.clopen/bin"). */
55
+ source: string | null;
56
+ }
57
+
58
+ // ─────────────────────────────────────────────────────────────────────────────
59
+ // Package manager detection
60
+ // ─────────────────────────────────────────────────────────────────────────────
61
+
62
+ type LinuxPkgMgr = 'apt' | 'dnf' | 'pacman' | 'apk' | 'zypper';
63
+ type WindowsPkgMgr = 'winget' | 'scoop' | 'choco';
64
+
65
+ function detectLinuxPkgMgr(): LinuxPkgMgr | null {
66
+ if (resolveBinary('apt-get')) return 'apt';
67
+ if (resolveBinary('dnf')) return 'dnf';
68
+ if (resolveBinary('pacman')) return 'pacman';
69
+ if (resolveBinary('apk')) return 'apk';
70
+ if (resolveBinary('zypper')) return 'zypper';
71
+ return null;
72
+ }
73
+
74
+ function detectWindowsPkgMgr(): WindowsPkgMgr | null {
75
+ if (resolveBinary('winget')) return 'winget';
76
+ if (resolveBinary('scoop')) return 'scoop';
77
+ if (resolveBinary('choco')) return 'choco';
78
+ return null;
79
+ }
80
+
81
+ function detectMacPkgMgr(): 'brew' | null {
82
+ return resolveBinary('brew') ? 'brew' : null;
83
+ }
84
+
85
+ // ─────────────────────────────────────────────────────────────────────────────
86
+ // Chrome detection via puppeteer cache layout
87
+ // ─────────────────────────────────────────────────────────────────────────────
88
+
89
+ /**
90
+ * Resolve the Chrome executable path under ~/.clopen/bin using the
91
+ * @puppeteer/browsers cache layout. Checks common Chrome build IDs by
92
+ * scanning the cache directory.
93
+ */
94
+ export function resolveClopenChromePath(): string | null {
95
+ const cacheDir = join(getClopenDir(), 'bin', 'chrome');
96
+ if (!existsSync(cacheDir)) return null;
97
+
98
+ try {
99
+ const entries = readdirSync(cacheDir);
100
+ for (const entry of entries) {
101
+ const buildDir = join(cacheDir, entry);
102
+ let candidate: string;
103
+ if (process.platform === 'darwin') {
104
+ candidate = join(buildDir, 'chrome-mac-arm64', 'Google Chrome for Testing.app', 'Contents', 'MacOS', 'Google Chrome for Testing');
105
+ if (existsSync(candidate)) return candidate;
106
+ candidate = join(buildDir, 'chrome-mac-x64', 'Google Chrome for Testing.app', 'Contents', 'MacOS', 'Google Chrome for Testing');
107
+ if (existsSync(candidate)) return candidate;
108
+ } else if (process.platform === 'win32') {
109
+ candidate = join(buildDir, 'chrome-win64', 'chrome.exe');
110
+ if (existsSync(candidate)) return candidate;
111
+ candidate = join(buildDir, 'chrome-win32', 'chrome.exe');
112
+ if (existsSync(candidate)) return candidate;
113
+ } else {
114
+ candidate = join(buildDir, 'chrome-linux64', 'chrome');
115
+ if (existsSync(candidate)) return candidate;
116
+ }
117
+ }
118
+ } catch {
119
+ // Fall through
120
+ }
121
+ return null;
122
+ }
123
+
124
+ function detectSystemChrome(): string | null {
125
+ if (process.platform === 'darwin') {
126
+ const paths = [
127
+ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
128
+ '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary',
129
+ '/Applications/Chromium.app/Contents/MacOS/Chromium'
130
+ ];
131
+ for (const p of paths) if (existsSync(p)) return p;
132
+ return null;
133
+ }
134
+ if (process.platform === 'win32') {
135
+ const programFiles = process.env['PROGRAMFILES'] ?? 'C:\\Program Files';
136
+ const programFilesX86 = process.env['PROGRAMFILES(X86)'] ?? 'C:\\Program Files (x86)';
137
+ const localAppData = process.env['LOCALAPPDATA'] ?? join(homedir(), 'AppData', 'Local');
138
+ const paths = [
139
+ join(programFiles, 'Google', 'Chrome', 'Application', 'chrome.exe'),
140
+ join(programFilesX86, 'Google', 'Chrome', 'Application', 'chrome.exe'),
141
+ join(localAppData, 'Google', 'Chrome', 'Application', 'chrome.exe')
142
+ ];
143
+ for (const p of paths) if (existsSync(p)) return p;
144
+ return null;
145
+ }
146
+ return resolveBinary('google-chrome')
147
+ ?? resolveBinary('google-chrome-stable')
148
+ ?? resolveBinary('chromium')
149
+ ?? resolveBinary('chromium-browser');
150
+ }
151
+
152
+ /**
153
+ * Preferred executable path for puppeteer. Clopen-managed install wins
154
+ * over system Chrome so users can keep the bundled version consistent.
155
+ */
156
+ export function getChromeExecutablePath(): string | null {
157
+ return resolveClopenChromePath() ?? detectSystemChrome();
158
+ }
159
+
160
+ // ─────────────────────────────────────────────────────────────────────────────
161
+ // Status detection
162
+ // ─────────────────────────────────────────────────────────────────────────────
163
+
164
+ async function runVersion(binary: string, versionFlag = '--version'): Promise<string | null> {
165
+ try {
166
+ const proc = Bun.spawn([binary, versionFlag], { stdout: 'pipe', stderr: 'pipe' });
167
+ const exitCode = await proc.exited;
168
+ if (exitCode !== 0) return null;
169
+ const stdout = await new Response(proc.stdout).text();
170
+ const first = stdout.trim().split('\n')[0]?.trim() ?? '';
171
+ return first || null;
172
+ } catch {
173
+ return null;
174
+ }
175
+ }
176
+
177
+ export async function getToolStatus(tool: ToolId): Promise<ToolStatus> {
178
+ if (tool === 'chrome') {
179
+ const resolved = getChromeExecutablePath();
180
+ if (!resolved) return { tool, installed: false, version: null, source: null };
181
+ const version = await runVersion(resolved);
182
+ const source = resolveClopenChromePath() ? 'clopen' : 'system';
183
+ return { tool, installed: true, version, source };
184
+ }
185
+
186
+ const resolved = await resolveBinaryWithRefresh(tool);
187
+ if (!resolved) return { tool, installed: false, version: null, source: null };
188
+ const version = await runVersion(resolved);
189
+ if (!version) return { tool, installed: false, version: null, source: null };
190
+ return { tool, installed: true, version, source: resolved };
191
+ }
192
+
193
+ // ─────────────────────────────────────────────────────────────────────────────
194
+ // Recipe resolution
195
+ // ─────────────────────────────────────────────────────────────────────────────
196
+
197
+ function needsCurl(): boolean {
198
+ return !resolveBinary('curl');
199
+ }
200
+
201
+ async function resolveGitRecipe(): Promise<Recipe> {
202
+ const base: Recipe = {
203
+ tool: 'git',
204
+ autoInstallable: false,
205
+ missingPrereqs: [],
206
+ manualInstructions: []
207
+ };
208
+
209
+ if (process.platform === 'darwin') {
210
+ const mgr = detectMacPkgMgr();
211
+ base.manualInstructions.push({
212
+ label: 'Homebrew',
213
+ command: 'brew install git',
214
+ docs: 'https://git-scm.com/download/mac'
215
+ });
216
+ if (!mgr) {
217
+ base.unavailableReason = 'Homebrew not found. Install Homebrew first.';
218
+ base.manualInstructions.push({
219
+ label: 'Install Homebrew',
220
+ command: '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"',
221
+ docs: 'https://brew.sh'
222
+ });
223
+ return base;
224
+ }
225
+ base.autoInstallable = true;
226
+ base.command = ['brew', 'install', 'git'];
227
+ base.displayCommand = 'brew install git';
228
+ return base;
229
+ }
230
+
231
+ if (process.platform === 'win32') {
232
+ const mgr = detectWindowsPkgMgr();
233
+ base.manualInstructions.push(
234
+ { label: 'winget', command: 'winget install --id Git.Git -e', docs: 'https://git-scm.com/download/win' },
235
+ { label: 'scoop', command: 'scoop install git' },
236
+ { label: 'Chocolatey', command: 'choco install git -y' }
237
+ );
238
+ if (!mgr) {
239
+ base.unavailableReason = 'No supported package manager found (winget / scoop / choco).';
240
+ return base;
241
+ }
242
+ if (mgr === 'winget') {
243
+ base.autoInstallable = true;
244
+ base.command = ['winget', 'install', '--id', 'Git.Git', '-e', '--accept-source-agreements', '--accept-package-agreements'];
245
+ base.displayCommand = 'winget install --id Git.Git -e';
246
+ return base;
247
+ }
248
+ if (mgr === 'scoop') {
249
+ base.autoInstallable = true;
250
+ base.command = ['scoop', 'install', 'git'];
251
+ base.displayCommand = 'scoop install git';
252
+ return base;
253
+ }
254
+ // choco — requires admin
255
+ const elevated = await isElevated();
256
+ if (!elevated) {
257
+ base.unavailableReason = 'Chocolatey requires Administrator. Run clopen as Administrator or use winget/scoop.';
258
+ return base;
259
+ }
260
+ base.autoInstallable = true;
261
+ base.command = ['choco', 'install', 'git', '-y'];
262
+ base.displayCommand = 'choco install git -y';
263
+ return base;
264
+ }
265
+
266
+ // Linux
267
+ const mgr = detectLinuxPkgMgr();
268
+ base.manualInstructions.push(
269
+ { label: 'apt (Debian/Ubuntu)', command: 'sudo apt update && sudo apt install -y git' },
270
+ { label: 'dnf (Fedora/RHEL)', command: 'sudo dnf install -y git' },
271
+ { label: 'pacman (Arch)', command: 'sudo pacman -S --noconfirm git' },
272
+ { label: 'apk (Alpine)', command: 'sudo apk add git' }
273
+ );
274
+ if (!mgr) {
275
+ base.unavailableReason = 'No supported Linux package manager found.';
276
+ return base;
277
+ }
278
+ const elevated = await isElevated();
279
+ if (!elevated) {
280
+ base.unavailableReason = 'Linux package install requires root. Run the command manually with sudo, or run clopen as root.';
281
+ return base;
282
+ }
283
+ base.autoInstallable = true;
284
+ if (mgr === 'apt') {
285
+ base.shell = { program: 'sh', args: ['-c'] };
286
+ base.command = ['apt-get update && apt-get install -y git'];
287
+ base.displayCommand = 'apt-get update && apt-get install -y git';
288
+ } else if (mgr === 'dnf') {
289
+ base.command = ['dnf', 'install', '-y', 'git'];
290
+ base.displayCommand = 'dnf install -y git';
291
+ } else if (mgr === 'pacman') {
292
+ base.command = ['pacman', '-S', '--noconfirm', 'git'];
293
+ base.displayCommand = 'pacman -S --noconfirm git';
294
+ } else if (mgr === 'apk') {
295
+ base.command = ['apk', 'add', 'git'];
296
+ base.displayCommand = 'apk add git';
297
+ } else if (mgr === 'zypper') {
298
+ base.command = ['zypper', '--non-interactive', 'install', 'git'];
299
+ base.displayCommand = 'zypper --non-interactive install git';
300
+ }
301
+ return base;
302
+ }
303
+
304
+ async function resolveCurlRecipe(): Promise<Recipe> {
305
+ const base: Recipe = {
306
+ tool: 'curl',
307
+ autoInstallable: false,
308
+ missingPrereqs: [],
309
+ manualInstructions: [
310
+ { label: 'apt (Debian/Ubuntu)', command: 'sudo apt update && sudo apt install -y curl' },
311
+ { label: 'dnf (Fedora/RHEL)', command: 'sudo dnf install -y curl' },
312
+ { label: 'pacman (Arch)', command: 'sudo pacman -S --noconfirm curl' },
313
+ { label: 'apk (Alpine)', command: 'sudo apk add curl' }
314
+ ]
315
+ };
316
+ if (process.platform === 'win32' || process.platform === 'darwin') {
317
+ // curl ships with both — if it's missing, defer to manual.
318
+ base.unavailableReason = 'curl is expected to be available on this platform.';
319
+ return base;
320
+ }
321
+ const mgr = detectLinuxPkgMgr();
322
+ if (!mgr) {
323
+ base.unavailableReason = 'No supported Linux package manager found.';
324
+ return base;
325
+ }
326
+ const elevated = await isElevated();
327
+ if (!elevated) {
328
+ base.unavailableReason = 'Linux package install requires root. Run the command manually with sudo.';
329
+ return base;
330
+ }
331
+ base.autoInstallable = true;
332
+ if (mgr === 'apt') {
333
+ base.shell = { program: 'sh', args: ['-c'] };
334
+ base.command = ['apt-get update && apt-get install -y curl'];
335
+ base.displayCommand = 'apt-get update && apt-get install -y curl';
336
+ } else if (mgr === 'dnf') {
337
+ base.command = ['dnf', 'install', '-y', 'curl'];
338
+ base.displayCommand = 'dnf install -y curl';
339
+ } else if (mgr === 'pacman') {
340
+ base.command = ['pacman', '-S', '--noconfirm', 'curl'];
341
+ base.displayCommand = 'pacman -S --noconfirm curl';
342
+ } else if (mgr === 'apk') {
343
+ base.command = ['apk', 'add', 'curl'];
344
+ base.displayCommand = 'apk add curl';
345
+ } else if (mgr === 'zypper') {
346
+ base.command = ['zypper', '--non-interactive', 'install', 'curl'];
347
+ base.displayCommand = 'zypper --non-interactive install curl';
348
+ }
349
+ return base;
350
+ }
351
+
352
+ async function resolveClaudeRecipe(): Promise<Recipe> {
353
+ const base: Recipe = {
354
+ tool: 'claude',
355
+ autoInstallable: false,
356
+ missingPrereqs: [],
357
+ manualInstructions: []
358
+ };
359
+
360
+ if (process.platform === 'win32') {
361
+ base.autoInstallable = true;
362
+ base.shell = { program: 'powershell.exe', args: ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command'] };
363
+ base.command = ['irm https://claude.ai/install.ps1 | iex'];
364
+ base.displayCommand = 'irm https://claude.ai/install.ps1 | iex';
365
+ base.manualInstructions.push({
366
+ label: 'PowerShell',
367
+ command: 'irm https://claude.ai/install.ps1 | iex',
368
+ docs: 'https://docs.claude.com/en/docs/claude-code/overview'
369
+ });
370
+ return base;
371
+ }
372
+
373
+ // macOS / Linux
374
+ base.manualInstructions.push({
375
+ label: 'curl + bash',
376
+ command: 'curl -fsSL https://claude.ai/install.sh | bash',
377
+ docs: 'https://docs.claude.com/en/docs/claude-code/overview'
378
+ });
379
+ if (needsCurl()) {
380
+ base.missingPrereqs.push('curl');
381
+ base.unavailableReason = 'curl is required to run the install script.';
382
+ return base;
383
+ }
384
+ base.autoInstallable = true;
385
+ base.shell = { program: 'bash', args: ['-c'] };
386
+ base.command = ['curl -fsSL https://claude.ai/install.sh | bash'];
387
+ base.displayCommand = 'curl -fsSL https://claude.ai/install.sh | bash';
388
+ return base;
389
+ }
390
+
391
+ async function resolveOpenCodeRecipe(): Promise<Recipe> {
392
+ const base: Recipe = {
393
+ tool: 'opencode',
394
+ autoInstallable: false,
395
+ missingPrereqs: [],
396
+ manualInstructions: [{
397
+ label: 'curl + bash',
398
+ command: 'curl -fsSL https://opencode.ai/install | bash',
399
+ docs: 'https://opencode.ai'
400
+ }]
401
+ };
402
+
403
+ if (process.platform === 'win32') {
404
+ base.unavailableReason = 'OpenCode install script is not available on Windows via PowerShell. Use WSL or the manual instructions.';
405
+ return base;
406
+ }
407
+
408
+ if (needsCurl()) {
409
+ base.missingPrereqs.push('curl');
410
+ base.unavailableReason = 'curl is required to run the install script.';
411
+ return base;
412
+ }
413
+ base.autoInstallable = true;
414
+ base.shell = { program: 'bash', args: ['-c'] };
415
+ base.command = ['curl -fsSL https://opencode.ai/install | bash'];
416
+ base.displayCommand = 'curl -fsSL https://opencode.ai/install | bash';
417
+ return base;
418
+ }
419
+
420
+ async function resolveChromeRecipe(): Promise<Recipe> {
421
+ const cacheDir = join(getClopenDir(), 'bin');
422
+ const base: Recipe = {
423
+ tool: 'chrome',
424
+ autoInstallable: true,
425
+ missingPrereqs: [],
426
+ manualInstructions: [{
427
+ label: 'Puppeteer browsers CLI',
428
+ command: `bun x @puppeteer/browsers install chrome@stable --path "${cacheDir}"`,
429
+ docs: 'https://pptr.dev/browsers-api'
430
+ }],
431
+ command: ['bun', 'x', '@puppeteer/browsers', 'install', 'chrome@stable', '--path', cacheDir],
432
+ displayCommand: `bun x @puppeteer/browsers install chrome@stable --path ${cacheDir}`
433
+ };
434
+ return base;
435
+ }
436
+
437
+ /**
438
+ * Resolve the install recipe for a tool on the current platform.
439
+ * Result is platform- and privilege-aware: recipes that would fail
440
+ * non-interactively are marked autoInstallable=false.
441
+ */
442
+ export async function resolveRecipe(tool: ToolId): Promise<Recipe> {
443
+ switch (tool) {
444
+ case 'git': return resolveGitRecipe();
445
+ case 'curl': return resolveCurlRecipe();
446
+ case 'claude': return resolveClaudeRecipe();
447
+ case 'opencode': return resolveOpenCodeRecipe();
448
+ case 'chrome': return resolveChromeRecipe();
449
+ }
450
+ }