glidercli 0.4.0 → 0.4.2

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 (2) hide show
  1. package/bin/glider.js +115 -24
  2. package/package.json +1 -1
package/bin/glider.js CHANGED
@@ -1939,14 +1939,18 @@ async function cmdExplore(url, opts = []) {
1939
1939
  let outputDir = '/tmp/glider-explore';
1940
1940
  let harFile = null;
1941
1941
  let sessionId = null;
1942
+ let freshTab = false;
1943
+ let closeOnDone = false;
1942
1944
 
1943
1945
  for (let i = 0; i < opts.length; i++) {
1944
1946
  if (opts[i] === '--depth' || opts[i] === '-d') depth = parseInt(opts[++i], 10);
1945
1947
  else if (opts[i] === '--output' || opts[i] === '-o') outputDir = opts[++i];
1946
1948
  else if (opts[i] === '--har') harFile = opts[++i];
1947
1949
  else if (opts[i] === '--session-id' || opts[i] === '--session') sessionId = opts[++i];
1950
+ else if (opts[i] === '--fresh-tab') freshTab = true;
1951
+ else if (opts[i] === '--close-on-done') closeOnDone = true;
1948
1952
  }
1949
- if (!sessionId && activeSessionId) sessionId = activeSessionId;
1953
+ if (!sessionId && !freshTab && activeSessionId) sessionId = activeSessionId;
1950
1954
 
1951
1955
  try {
1952
1956
  assertUrlAllowed(url, allowedDomainList, 'explore');
@@ -1956,6 +1960,29 @@ async function cmdExplore(url, opts = []) {
1956
1960
  process.exit(1);
1957
1961
  }
1958
1962
 
1963
+ // v0.4.2: --fresh-tab sidesteps H4 (CSS.enable forwardCDPCommand timeout
1964
+ // on long-lived session reuse). Opens a new tab, waits for target attach,
1965
+ // uses the new session-id, and optionally closes on done.
1966
+ let freshTargetId = null;
1967
+ if (freshTab) {
1968
+ const { WindowManager } = require(path.join(LIB_DIR, 'bwindow.js'));
1969
+ const wm = new WindowManager();
1970
+ await wm.connect();
1971
+ await wm.init();
1972
+ log.info(`[explore] Opening fresh tab for capture...`);
1973
+ const result = await wm.createTab(url);
1974
+ freshTargetId = result.targetId;
1975
+ await new Promise(r => setTimeout(r, 2500));
1976
+ try {
1977
+ const targetsRes = await httpGet('/targets');
1978
+ const targets = (targetsRes && targetsRes.targets) || targetsRes || [];
1979
+ const t = (Array.isArray(targets) ? targets : []).find(x => x.targetId === freshTargetId);
1980
+ if (t && t.sessionId) sessionId = t.sessionId;
1981
+ } catch {}
1982
+ if (!sessionId) log.info('[explore] fresh-tab created but sessionId not resolved (bexplore will auto-pin)');
1983
+ else log.ok(`[explore] fresh-tab session: ${sessionId}`);
1984
+ }
1985
+
1959
1986
  log.info(`Exploring: ${url} (depth: ${depth})`);
1960
1987
 
1961
1988
  // Use the bexplore.js library
@@ -1969,9 +1996,24 @@ async function cmdExplore(url, opts = []) {
1969
1996
  const child = spawn('node', spawnArgs, {
1970
1997
  stdio: 'inherit'
1971
1998
  });
1972
- await new Promise((resolve, reject) => {
1973
- child.on('close', code => code === 0 ? resolve() : reject(new Error(`Exit code: ${code}`)));
1974
- });
1999
+ try {
2000
+ await new Promise((resolve, reject) => {
2001
+ child.on('close', code => code === 0 ? resolve() : reject(new Error(`Exit code: ${code}`)));
2002
+ });
2003
+ } finally {
2004
+ if (freshTab && closeOnDone && freshTargetId) {
2005
+ try {
2006
+ const { WindowManager } = require(path.join(LIB_DIR, 'bwindow.js'));
2007
+ const wm = new WindowManager();
2008
+ await wm.connect();
2009
+ await wm.init();
2010
+ await wm.closeTarget(freshTargetId);
2011
+ log.ok(`[explore] fresh-tab closed: ${freshTargetId}`);
2012
+ } catch (e) {
2013
+ log.info(`[explore] fresh-tab close failed: ${e.message}`);
2014
+ }
2015
+ }
2016
+ }
1975
2017
  } else {
1976
2018
  // Fallback: simple exploration
1977
2019
  await cmdGoto(url);
@@ -3447,33 +3489,82 @@ async function cmdUpload(argv) {
3447
3489
  } catch (e) { log.fail(`upload failed: ${e.message}`); process.exit(1); }
3448
3490
  }
3449
3491
 
3450
- // --- WAVE 3: cmdHar (start/stop/dump/replay) ---
3451
- let HAR_BUFFER = null;
3492
+ // --- WAVE 3: cmdHar (start/stop/dump/status) ---
3493
+ // v0.4.1: rewired to bexplore subprocess (was Potemkin stub - help advertised
3494
+ // the verb but dump always returned 0 entries; "server-side subscription
3495
+ // not implemented"). Now delegates to the same code path `explore --har` uses.
3452
3496
  async function cmdHar(argv) {
3453
3497
  const sub = argv[0];
3454
- await _guardAndConnect('har');
3455
- const HAR_STATE = path.join(os.homedir(), '.glider', 'har-buffer.json');
3498
+ const STATE_PATH = path.join(os.homedir(), '.glider', 'har-state.json');
3499
+ const BUF_HAR = path.join(os.homedir(), '.glider', 'har-buffer.har');
3500
+ const BUF_OUT = path.join(os.homedir(), '.glider', 'har-buffer-out');
3501
+ const readState = () => { try { return JSON.parse(fs.readFileSync(STATE_PATH, 'utf8')); } catch { return null; } };
3502
+ const writeState = (st) => { try { fs.mkdirSync(path.dirname(STATE_PATH), { recursive: true }); } catch {} fs.writeFileSync(STATE_PATH, JSON.stringify(st, null, 2)); };
3503
+ const pidAlive = (pid) => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
3504
+
3456
3505
  if (sub === 'start') {
3457
- await httpPost('/cdp', { method: 'Network.enable', params: {} });
3458
- HAR_BUFFER = { started: new Date().toISOString(), entries: [] };
3459
- try { fs.mkdirSync(path.dirname(HAR_STATE), { recursive: true }); } catch {}
3460
- fs.writeFileSync(HAR_STATE, JSON.stringify(HAR_BUFFER));
3461
- // Note: full HAR requires event subscription (persistent server); here we mark state; user can dump devtools separately if needed.
3462
- if (jsonOutput) emitJson(true, { started: true, state_file: HAR_STATE });
3463
- else log.ok(`HAR capture started; state at ${HAR_STATE} (server-side subscription not implemented - stub)`);
3506
+ const url = argv[1];
3507
+ if (!url) { log.fail('Usage: glider har start <url> [--session-id id]'); process.exit(1); }
3508
+ const existing = readState();
3509
+ if (existing && pidAlive(existing.pid)) {
3510
+ log.fail(`HAR capture already running (pid=${existing.pid}, url=${existing.url}). Run 'glider har stop' first.`);
3511
+ process.exit(1);
3512
+ }
3513
+ let sessionId = null;
3514
+ for (let i = 2; i < argv.length; i++) {
3515
+ if (argv[i] === '--session-id' || argv[i] === '--session') sessionId = argv[++i];
3516
+ }
3517
+ if (!sessionId && activeSessionId) sessionId = activeSessionId;
3518
+ await _guardAndConnect('har');
3519
+ try { fs.mkdirSync(BUF_OUT, { recursive: true }); } catch {}
3520
+ try { fs.unlinkSync(BUF_HAR); } catch {}
3521
+ const bexplorePath = path.join(LIB_DIR, 'bexplore.js');
3522
+ const spawnArgs = [bexplorePath, url, '--depth', '0', '--output', BUF_OUT, '--har', BUF_HAR];
3523
+ if (sessionId) spawnArgs.push('--session-id', sessionId);
3524
+ const child = spawn('node', spawnArgs, { detached: true, stdio: ['ignore', 'ignore', 'ignore'] });
3525
+ child.unref();
3526
+ const state = { pid: child.pid, har: BUF_HAR, url, session_id: sessionId, started_at: new Date().toISOString() };
3527
+ writeState(state);
3528
+ if (jsonOutput) emitJson(true, state);
3529
+ else log.ok(`HAR capture started (pid=${child.pid}, url=${url}); dump with 'glider har dump [path]'`);
3464
3530
  } else if (sub === 'stop') {
3465
- await httpPost('/cdp', { method: 'Network.disable', params: {} });
3466
- if (jsonOutput) emitJson(true, { stopped: true });
3467
- else log.ok('HAR capture stopped');
3531
+ const st = readState();
3532
+ if (!st) { log.fail('No HAR capture in progress'); process.exit(1); }
3533
+ if (pidAlive(st.pid)) { try { process.kill(st.pid, 'SIGTERM'); } catch {} }
3534
+ try { fs.unlinkSync(STATE_PATH); } catch {}
3535
+ if (jsonOutput) emitJson(true, { stopped: true, pid: st.pid });
3536
+ else log.ok(`HAR capture stopped (pid=${st.pid})`);
3468
3537
  } else if (sub === 'dump') {
3469
3538
  const p = argv[1] || `/tmp/glider-${Date.now()}.har`;
3470
- let buf; try { buf = JSON.parse(fs.readFileSync(HAR_STATE, 'utf8')); } catch { buf = { entries: [] }; }
3471
- const har = { log: { version: '1.2', creator: { name: 'glider', version: require('../package.json').version }, entries: buf.entries } };
3472
- fs.writeFileSync(p, JSON.stringify(har, null, 2));
3473
- if (jsonOutput) emitJson(true, { path: p, entries: buf.entries.length });
3474
- else log.ok(`HAR dumped to ${p} (${buf.entries.length} entries - event stream not yet wired)`);
3539
+ const st = readState();
3540
+ if (!st) { log.fail('No HAR capture in progress. Run: glider har start <url>'); process.exit(1); }
3541
+ const timeoutMs = 60000;
3542
+ const started = Date.now();
3543
+ while (pidAlive(st.pid) && (Date.now() - started) < timeoutMs) {
3544
+ await new Promise(r => setTimeout(r, 500));
3545
+ }
3546
+ if (pidAlive(st.pid)) {
3547
+ log.fail(`HAR child still running after ${timeoutMs}ms; run 'glider har stop' or wait`);
3548
+ process.exit(1);
3549
+ }
3550
+ if (!fs.existsSync(st.har)) {
3551
+ log.fail(`HAR buffer not found at ${st.har} (capture may have failed)`);
3552
+ process.exit(1);
3553
+ }
3554
+ fs.copyFileSync(st.har, p);
3555
+ let entryCount = 0;
3556
+ try { entryCount = JSON.parse(fs.readFileSync(p, 'utf8')).log.entries.length; } catch {}
3557
+ try { fs.unlinkSync(STATE_PATH); } catch {}
3558
+ if (jsonOutput) emitJson(true, { path: p, entries: entryCount });
3559
+ else log.ok(`HAR dumped to ${p} (${entryCount} entries)`);
3560
+ } else if (sub === 'status') {
3561
+ const st = readState();
3562
+ if (!st) { if (jsonOutput) emitJson(true, { running: false }); else console.log('idle'); return; }
3563
+ const alive = pidAlive(st.pid);
3564
+ if (jsonOutput) emitJson(true, { running: alive, ...st });
3565
+ else console.log(`${alive ? 'RUNNING' : 'DONE'} pid=${st.pid} url=${st.url} started=${st.started_at}`);
3475
3566
  } else {
3476
- log.fail('Usage: glider har start | stop | dump [PATH] | replay <entry-id>'); process.exit(1);
3567
+ log.fail('Usage: glider har start <url> [--session-id id] | stop | dump [PATH] | status'); process.exit(1);
3477
3568
  }
3478
3569
  }
3479
3570
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glidercli",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Browser automation CLI. Control Chrome from terminal via CDP, run YAML task files, autonomous loops until completion.",
5
5
  "main": "index.js",
6
6
  "bin": {