remote-cli-agent 0.5.0 → 0.7.0
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/dist/connection.d.ts +1 -0
- package/dist/connection.js +37 -5
- package/dist/index.js +31 -2
- package/package.json +1 -1
package/dist/connection.d.ts
CHANGED
package/dist/connection.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
-
import { getSessionList, writeToSession, resizeSession, attachSession, spawnSession, setOutputHandler, setSessionsChangeHandler } from './terminal.js';
|
|
2
|
+
import { getSessionList, writeToSession, resizeSession, attachSession, spawnSession, createTmuxSession, attachTmuxSession, setOutputHandler, setSessionsChangeHandler } from './terminal.js';
|
|
3
3
|
let ws = null;
|
|
4
4
|
let reconnectTimer = null;
|
|
5
5
|
let reconnectAttempts = 0;
|
|
6
6
|
const MAX_RECONNECT_DELAY = 30000;
|
|
7
7
|
const BASE_RECONNECT_DELAY = 1000;
|
|
8
|
+
let pingInterval = null;
|
|
8
9
|
let serverUrl = '';
|
|
9
10
|
let agentToken = '';
|
|
10
11
|
let isConnected = false;
|
|
12
|
+
let watchInterval = null;
|
|
11
13
|
export function connect(url, token) {
|
|
12
14
|
serverUrl = url;
|
|
13
15
|
agentToken = token;
|
|
@@ -34,6 +36,14 @@ function doConnect() {
|
|
|
34
36
|
reconnectAttempts = 0;
|
|
35
37
|
// Register current sessions
|
|
36
38
|
sendMessage({ type: 'register', sessions: getSessionList() });
|
|
39
|
+
// Keep connection alive with periodic pings (every 30s)
|
|
40
|
+
if (pingInterval)
|
|
41
|
+
clearInterval(pingInterval);
|
|
42
|
+
pingInterval = setInterval(() => {
|
|
43
|
+
if (ws && ws.readyState === WebSocket.OPEN) {
|
|
44
|
+
ws.ping();
|
|
45
|
+
}
|
|
46
|
+
}, 30000);
|
|
37
47
|
});
|
|
38
48
|
ws.on('message', (data) => {
|
|
39
49
|
try {
|
|
@@ -78,10 +88,20 @@ function handleMessage(msg) {
|
|
|
78
88
|
break;
|
|
79
89
|
}
|
|
80
90
|
case 'spawn': {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
91
|
+
const name = msg.name || `session-${Date.now()}`;
|
|
92
|
+
// Create a tmux session so it can be shared locally
|
|
93
|
+
const tmuxName = createTmuxSession(name);
|
|
94
|
+
if (tmuxName) {
|
|
95
|
+
const session = attachTmuxSession(tmuxName);
|
|
96
|
+
if (session) {
|
|
97
|
+
console.log(`Created tmux session: ${tmuxName} (${session.id})`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// Fallback to PTY
|
|
102
|
+
const session = spawnSession(msg.command);
|
|
103
|
+
console.log(`Spawned session: ${session.name} (${session.id})`);
|
|
104
|
+
}
|
|
85
105
|
sendMessage({ type: 'sessions_update', sessions: getSessionList() });
|
|
86
106
|
break;
|
|
87
107
|
}
|
|
@@ -111,6 +131,14 @@ function scheduleReconnect() {
|
|
|
111
131
|
}, delay);
|
|
112
132
|
}
|
|
113
133
|
export function disconnect() {
|
|
134
|
+
if (pingInterval) {
|
|
135
|
+
clearInterval(pingInterval);
|
|
136
|
+
pingInterval = null;
|
|
137
|
+
}
|
|
138
|
+
if (watchInterval) {
|
|
139
|
+
clearInterval(watchInterval);
|
|
140
|
+
watchInterval = null;
|
|
141
|
+
}
|
|
114
142
|
if (reconnectTimer) {
|
|
115
143
|
clearTimeout(reconnectTimer);
|
|
116
144
|
reconnectTimer = null;
|
|
@@ -122,6 +150,10 @@ export function disconnect() {
|
|
|
122
150
|
}
|
|
123
151
|
isConnected = false;
|
|
124
152
|
}
|
|
153
|
+
// Notify server of session changes (exposed for external watchers)
|
|
154
|
+
export function notifySessionsUpdate(sessions) {
|
|
155
|
+
sendMessage({ type: 'sessions_update', sessions });
|
|
156
|
+
}
|
|
125
157
|
export function getConnectionStatus() {
|
|
126
158
|
return isConnected;
|
|
127
159
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { Command } from 'commander';
|
|
3
|
-
import { connect, disconnect } from './connection.js';
|
|
3
|
+
import { connect, disconnect, notifySessionsUpdate } from './connection.js';
|
|
4
4
|
import { listTmuxSessions, attachTmuxSession, createTmuxSession, spawnSession, cleanupAll, getSessionList } from './terminal.js';
|
|
5
5
|
import fs from 'fs';
|
|
6
6
|
import path from 'path';
|
|
@@ -96,6 +96,35 @@ program
|
|
|
96
96
|
}
|
|
97
97
|
// Connect to server
|
|
98
98
|
connect(wsUrl, token);
|
|
99
|
+
// Watch for new tmux sessions
|
|
100
|
+
if (useTmux) {
|
|
101
|
+
const knownTmux = new Set();
|
|
102
|
+
for (const s of getSessionList()) {
|
|
103
|
+
if (s.name?.startsWith('tmux: ')) {
|
|
104
|
+
knownTmux.add(s.name.replace('tmux: ', ''));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
setInterval(() => {
|
|
108
|
+
const current = listTmuxSessions();
|
|
109
|
+
let changed = false;
|
|
110
|
+
for (const name of current) {
|
|
111
|
+
if (!knownTmux.has(name)) {
|
|
112
|
+
console.log(`New tmux session detected: ${name}`);
|
|
113
|
+
attachTmuxSession(name);
|
|
114
|
+
knownTmux.add(name);
|
|
115
|
+
changed = true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
for (const name of knownTmux) {
|
|
119
|
+
if (!current.includes(name)) {
|
|
120
|
+
knownTmux.delete(name);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (changed) {
|
|
124
|
+
notifySessionsUpdate(getSessionList());
|
|
125
|
+
}
|
|
126
|
+
}, 3000);
|
|
127
|
+
}
|
|
99
128
|
// Handle shutdown
|
|
100
129
|
const shutdown = () => {
|
|
101
130
|
console.log('\nShutting down...');
|
|
@@ -106,7 +135,7 @@ program
|
|
|
106
135
|
process.on('SIGINT', shutdown);
|
|
107
136
|
process.on('SIGTERM', shutdown);
|
|
108
137
|
// Keep process alive
|
|
109
|
-
setInterval(() => { },
|
|
138
|
+
setInterval(() => { }, 60000);
|
|
110
139
|
});
|
|
111
140
|
program
|
|
112
141
|
.command('attach <session>')
|