agentlytics 0.2.2 → 0.2.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 (3) hide show
  1. package/index.js +52 -18
  2. package/package.json +1 -1
  3. package/server.js +4 -0
package/index.js CHANGED
@@ -139,10 +139,9 @@ if (!collectOnly && !fs.existsSync(publicIndex) && fs.existsSync(uiDir)) {
139
139
  console.log(chalk.cyan(' ⟳ Building dashboard UI (first run)...'));
140
140
  try {
141
141
  const uiModules = path.join(uiDir, 'node_modules');
142
- if (!fs.existsSync(uiModules)) {
143
- console.log(chalk.dim(' Installing UI dependencies...'));
144
- execSync('npm install --no-audit --no-fund', { cwd: uiDir, stdio: 'pipe' });
145
- }
142
+ if (fs.existsSync(uiModules)) fs.rmSync(uiModules, { recursive: true, force: true });
143
+ console.log(chalk.dim(' Installing UI dependencies...'));
144
+ execSync('npm install --no-audit --no-fund', { cwd: uiDir, stdio: 'pipe' });
146
145
  console.log(chalk.dim(' Compiling frontend...'));
147
146
  execSync('npm run build', { cwd: uiDir, stdio: 'pipe' });
148
147
  console.log(chalk.green(' ✓ UI built successfully'));
@@ -278,20 +277,55 @@ const BOT_STYLES = [
278
277
  process.exit(0);
279
278
  }
280
279
 
281
- // Start server
280
+ // Start server (kill stale agentlytics or find free port)
282
281
  const app = require('./server');
283
- app.listen(PORT, () => {
284
- const url = `http://localhost:${PORT}`;
285
- console.log(chalk.green(` ✓ Dashboard ready at ${chalk.bold.white(url)}`));
286
- console.log('');
287
- console.log(chalk.dim(' 💡 Share sessions with your team:'));
288
- console.log(chalk.dim(` npx agentlytics --relay Start a relay server`));
289
- console.log(chalk.dim(` npx agentlytics --join <host:port> --username Join a relay server`));
290
- console.log('');
291
- console.log(chalk.dim(' Press Ctrl+C to stop\n'));
282
+ const http = require('http');
283
+ const net = require('net');
284
+
285
+ function isPortFree(port) {
286
+ return new Promise((resolve) => {
287
+ const tester = net.createServer()
288
+ .once('error', () => resolve(false))
289
+ .once('listening', () => tester.close(() => resolve(true)))
290
+ .listen(port, '0.0.0.0');
291
+ });
292
+ }
292
293
 
293
- // Auto-open browser
294
- const open = require('open');
295
- open(url).catch(() => {});
296
- });
294
+ async function startServer(port) {
295
+ const free = await isPortFree(port);
296
+ if (!free) {
297
+ // Port in use — check if it's a previous agentlytics instance
298
+ try {
299
+ const data = await new Promise((resolve, reject) => {
300
+ const req = http.get(`http://127.0.0.1:${port}/api/ping`, { timeout: 2000 }, (res) => {
301
+ let body = '';
302
+ res.on('data', (d) => body += d);
303
+ res.on('end', () => { try { resolve(JSON.parse(body)); } catch { reject(); } });
304
+ });
305
+ req.on('error', reject);
306
+ });
307
+ if (data.app === 'agentlytics' && data.pid) {
308
+ console.log(chalk.yellow(` ⟳ Killing previous Agentlytics instance (PID ${data.pid})...`));
309
+ try { process.kill(data.pid, 'SIGTERM'); } catch {}
310
+ await new Promise(r => setTimeout(r, 1000));
311
+ return startServer(port);
312
+ }
313
+ } catch {}
314
+ console.log(chalk.yellow(` ⚠ Port ${port} is in use by another app, trying ${port + 1}...`));
315
+ return startServer(port + 1);
316
+ }
317
+
318
+ app.listen(port, '0.0.0.0', () => {
319
+ const url = `http://localhost:${port}`;
320
+ console.log(chalk.green(` ✓ Dashboard ready at ${chalk.bold.white(url)}`));
321
+ console.log('');
322
+ console.log(chalk.dim(' Press Ctrl+C to stop\n'));
323
+
324
+ // Auto-open browser
325
+ const open = require('open');
326
+ open(url).catch(() => {});
327
+ });
328
+ }
329
+
330
+ startServer(parseInt(PORT));
297
331
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agentlytics",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Comprehensive analytics dashboard for AI coding agents — Cursor, Windsurf, Claude Code, VS Code Copilot, Zed, Antigravity, OpenCode, Command Code",
5
5
  "main": "index.js",
6
6
  "bin": {
package/server.js CHANGED
@@ -41,6 +41,10 @@ function parseDateOpts(query) {
41
41
  return opts;
42
42
  }
43
43
 
44
+ app.get('/api/ping', (req, res) => {
45
+ res.json({ app: 'agentlytics', pid: process.pid });
46
+ });
47
+
44
48
  app.get('/api/mode', (req, res) => {
45
49
  res.json({ mode: 'local' });
46
50
  });