thinknagent 0.1.5 → 0.1.6

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/lib/agent.js +50 -53
  2. package/package.json +1 -1
package/lib/agent.js CHANGED
@@ -38,65 +38,62 @@ class Agent {
38
38
  this._active = false;
39
39
  }
40
40
 
41
- start() {
42
- const cfg = store.read();
43
- if (!cfg.serverUrl) {
44
- console.error('[agent] No serverUrl configured. Run: thinknagent init');
45
- process.exit(1);
46
- }
47
-
48
- console.log(`[agent] Connecting to ${cfg.serverUrl}...`);
49
- this.conn.connect();
41
+ start() {
42
+ const cfg = store.read();
43
+ if (!cfg.serverUrl) {
44
+ console.error('[agent] Not initialized.');
45
+ process.exit(1);
46
+ }
50
47
 
51
- // Wire metrics → alert engine
52
- this.conn.socket.on('agent:metrics_internal', (m) => this.alerts.evaluate(m));
48
+ console.log(`[agent] Connecting to ${cfg.serverUrl}...`);
49
+
50
+ // ✅ Pehle connect karo
51
+ this.conn.connect();
52
+
53
+ // ✅ Socket ready hone ke baad events bind karo
54
+ // conn.socket ab available hai kyunki connect() sync mein socket banata hai
55
+ this.conn.socket.on('agent:rules_updated', ({ rules }) => {
56
+ store.set('alerts', rules);
57
+ this.alerts.reloadRules(rules);
58
+ });
59
+
60
+ this.conn.socket.on('agent:send_metrics_now', () => {
61
+ console.log('[agent] Metrics poll requested');
62
+ this.metrics.pollNow();
63
+ });
64
+
65
+ this.conn.socket.on('agent:logs_updated', ({ logs }) => {
66
+ store.set('logs', logs);
67
+ this.logs.stop();
68
+ this.logs = new LogWatcher({ connection: this.conn, logPaths: logs });
69
+ if (this._active) this.logs.start(); // ✅ sirf tab start karo agar active ho
70
+ });
53
71
 
54
- // Allow server to push updated alert rules at runtime (Owner changed thresholds)
55
- this.conn.socket.on('agent:rules_updated', ({ rules }) => {
56
- store.set('alerts', rules);
57
- this.alerts.reloadRules(rules);
58
- });
72
+ process.on('SIGTERM', () => this._shutdown('SIGTERM'));
73
+ process.on('SIGINT', () => this._shutdown('SIGINT'));
74
+ }
59
75
 
60
- // Allow server to push updated log paths at runtime
61
- this.conn.socket.on('agent:logs_updated', ({ logs }) => {
62
- store.set('logs', logs);
63
- this.logs.stop();
64
- this.logs = new LogWatcher({ connection: this.conn, logPaths: logs });
65
- this.logs.start();
66
- });
76
+ _onReady(role, roomId) {
77
+ if (this._active) {
78
+ // Reconnect case — metrics/logs already chal rahe hain
79
+ console.log(`[agent] Reconnected. Role: ${role}`);
80
+ return;
81
+ }
67
82
 
68
- this.conn.socket.on('agent:send_metrics_now', () => {
69
- console.log('[agent] send_metrics_now received polling now');
70
- this.metrics.pollNow();
71
- });
83
+ this._active = true;
84
+ console.log(`[agent] Active! Role: ${role} | Room: ${roomId}`);
72
85
 
73
- // Graceful shutdown
74
- process.on('SIGTERM', () => this._shutdown('SIGTERM'));
75
- process.on('SIGINT', () => this._shutdown('SIGINT'));
76
- }
86
+ // Metrics emit ko alert engine se connect karo
87
+ const origEmit = this.conn.emit.bind(this.conn);
88
+ this.conn.emit = (event, data) => {
89
+ if (event === 'agent:metrics') this.alerts.evaluate(data);
90
+ origEmit(event, data);
91
+ };
77
92
 
78
- _onReady(role, roomId) {
79
- if (this._active) return; // reconnect — already running
80
- this._active = true;
81
- console.log(`[agent] Active. Role: ${role} | Room: ${roomId}`);
82
-
83
- this.metrics.start();
84
- this.logs.start();
85
- this.shell.start();
86
-
87
- // Patch metrics poller to also feed alert engine
88
- const origEmit = this.conn.emit.bind(this.conn);
89
- this.conn.emit = (event, data) => {
90
- if (event === 'agent:metrics') this.alerts.evaluate(data);
91
- origEmit(event, data);
92
- };
93
-
94
- this.conn.emit('agent:ready', {
95
- hostname: require('os').hostname(),
96
- platform: process.platform,
97
- nodeVersion: process.version,
98
- });
99
- }
93
+ this.metrics.start();
94
+ this.logs.start();
95
+ this.shell.start();
96
+ }
100
97
 
101
98
 
102
99
  _onDisconnect(reason) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thinknagent",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "ThinkNCollab server agent — metrics, logs, alerts, shell bridge",
5
5
  "main": "lib/agent.js",
6
6
  "bin": {