thinknagent 0.1.4 → 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.
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/lib/connect.js CHANGED
@@ -31,22 +31,26 @@ class Connection {
31
31
  const cfg = store.read();
32
32
 
33
33
  this.socket = io(`${this.serverUrl}${NAMESPACE}`, {
34
- reconnection: true,
35
- reconnectionDelay: RECONNECT_DELAY,
34
+ reconnection: true,
35
+ reconnectionDelay: RECONNECT_DELAY,
36
36
  reconnectionAttempts: Infinity,
37
- auth: {
38
- agentId: cfg.agentId,
39
- agentToken: cfg.agentToken || null,
40
- name: cfg.name,
41
- hostname: require('os').hostname(),
42
- version: require('../package.json').version,
43
- roomId: cfg.roomId || null, // ← yeh add karo
37
+ auth: (cb) => {
38
+ // ← YE CHANGE KARO — har reconnect pe fresh config read karo
39
+ const freshCfg = store.read();
40
+ cb({
41
+ agentId: freshCfg.agentId,
42
+ agentToken: freshCfg.agentToken || null,
43
+ name: freshCfg.name,
44
+ hostname: require('os').hostname(),
45
+ version: require('../package.json').version,
46
+ roomId: freshCfg.roomId || null,
47
+ });
44
48
  }
45
49
  });
46
50
 
47
- this._bind();
48
- return this.socket;
49
- }
51
+ this._bind();
52
+ return this.socket;
53
+ }
50
54
 
51
55
  _bind() {
52
56
  const s = this.socket;
@@ -61,17 +65,18 @@ class Connection {
61
65
  });
62
66
 
63
67
  // Server says: "Owner approved, here is your token + assigned role"
64
- s.on('agent:approved', ({ agentToken, role, roomId }) => {
65
- store.set('agentToken', agentToken);
66
- store.set('agentId', this.agentId);
67
- store.set('role', role);
68
- store.set('roomId', roomId);
69
- this.agentToken = agentToken;
70
- this.role = role;
71
- console.log(`[thinknagent] Approved! Role: ${role} | Room: ${roomId}`);
72
- this.onReady?.({ role, roomId });
73
- });
74
-
68
+ s.on('agent:approved', ({ agentToken, role, roomId }) => {
69
+ store.set('agentToken', agentToken);
70
+ store.set('agentId', this.agentId);
71
+ store.set('role', role);
72
+ store.set('roomId', roomId);
73
+ this.agentToken = agentToken;
74
+ this.role = role;
75
+ console.log(`[thinknagent] Approved! Reconnecting with token...`);
76
+
77
+ // ← YE ADD KARO — disconnect karo, socket.io auto-reconnect karega naye token ke saath
78
+ this.socket.disconnect();
79
+ });
75
80
  // Server says: "Owner rejected this agent"
76
81
  s.on('agent:rejected', ({ reason }) => {
77
82
  console.error(`[thinknagent] Registration rejected: ${reason}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "thinknagent",
3
- "version": "0.1.4",
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": {