@dmsdc-ai/aigentry-telepty 0.1.24 → 0.1.26

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/cli.js +15 -12
  2. package/daemon.js +37 -22
  3. package/package.json +1 -1
package/cli.js CHANGED
@@ -725,23 +725,26 @@ async function main() {
725
725
  let lastInjectTextTime = 0;
726
726
  const MAX_RECONNECT_DELAY = 30000;
727
727
 
728
- function connectDaemonWs() {
728
+ async function connectDaemonWs() {
729
+ // Re-register session BEFORE WebSocket connect (daemon rejects WS if session unknown)
730
+ if (reconnectAttempts > 0) {
731
+ try {
732
+ await fetchWithAuth(`${DAEMON_URL}/api/sessions/register`, {
733
+ method: 'POST',
734
+ headers: { 'Content-Type': 'application/json' },
735
+ body: JSON.stringify({ session_id: sessionId, command, cwd: process.cwd() })
736
+ });
737
+ } catch (e) {
738
+ // Registration may fail if session already exists or daemon not ready
739
+ }
740
+ }
741
+
729
742
  daemonWs = new WebSocket(wsUrl);
730
743
 
731
- daemonWs.on('open', async () => {
744
+ daemonWs.on('open', () => {
732
745
  wsReady = true;
733
746
  if (reconnectAttempts > 0) {
734
747
  console.error(`\n\x1b[32m⚡ Reconnected to daemon. Inject restored.\x1b[0m`);
735
- // Re-register session on reconnect
736
- try {
737
- await fetchWithAuth(`${DAEMON_URL}/api/sessions/register`, {
738
- method: 'POST',
739
- headers: { 'Content-Type': 'application/json' },
740
- body: JSON.stringify({ session_id: sessionId, command, cwd: process.cwd() })
741
- });
742
- } catch (e) {
743
- // Registration may fail if session already exists, that's fine
744
- }
745
748
  }
746
749
  reconnectAttempts = 0;
747
750
  });
package/daemon.js CHANGED
@@ -981,29 +981,44 @@ wss.on('connection', (ws, req) => {
981
981
  const session = sessions[sessionId];
982
982
 
983
983
  if (!session) {
984
- ws.close(1008, 'Session not found');
985
- return;
984
+ // Auto-register wrapped session on WS connect (supports reconnect after daemon restart)
985
+ const autoSession = {
986
+ id: sessionId,
987
+ type: 'wrapped',
988
+ ptyProcess: null,
989
+ ownerWs: ws,
990
+ command: 'wrapped',
991
+ cwd: process.cwd(),
992
+ createdAt: new Date().toISOString(),
993
+ clients: new Set([ws]),
994
+ isClosing: false
995
+ };
996
+ sessions[sessionId] = autoSession;
997
+ console.log(`[WS] Auto-registered wrapped session ${sessionId} on reconnect`);
998
+ // Skip to message/close handlers below (ownerWs already set)
999
+ } else {
1000
+ session.clients.add(ws);
986
1001
  }
987
1002
 
988
- session.clients.add(ws);
1003
+ const activeSession = sessions[sessionId];
989
1004
 
990
1005
  // For wrapped sessions, first connector becomes the owner
991
- if (session.type === 'wrapped' && !session.ownerWs) {
992
- session.ownerWs = ws;
993
- console.log(`[WS] Wrap owner connected for session ${sessionId} (Total: ${session.clients.size})`);
1006
+ if (activeSession.type === 'wrapped' && !activeSession.ownerWs) {
1007
+ activeSession.ownerWs = ws;
1008
+ console.log(`[WS] Wrap owner connected for session ${sessionId} (Total: ${activeSession.clients.size})`);
994
1009
  } else {
995
- console.log(`[WS] Client attached to session ${sessionId} (Total: ${session.clients.size})`);
1010
+ console.log(`[WS] Client attached to session ${sessionId} (Total: ${activeSession.clients.size})`);
996
1011
  }
997
1012
 
998
1013
  ws.on('message', (message) => {
999
1014
  try {
1000
1015
  const { type, data, cols, rows } = JSON.parse(message);
1001
1016
 
1002
- if (session.type === 'wrapped') {
1003
- if (ws === session.ownerWs) {
1017
+ if (activeSession.type === 'wrapped') {
1018
+ if (ws === activeSession.ownerWs) {
1004
1019
  // Owner sending output -> broadcast to other clients
1005
1020
  if (type === 'output') {
1006
- session.clients.forEach(client => {
1021
+ activeSession.clients.forEach(client => {
1007
1022
  if (client !== ws && client.readyState === 1) {
1008
1023
  client.send(JSON.stringify({ type: 'output', data }));
1009
1024
  }
@@ -1011,18 +1026,18 @@ wss.on('connection', (ws, req) => {
1011
1026
  }
1012
1027
  } else {
1013
1028
  // Non-owner client input -> forward to owner as inject
1014
- if (type === 'input' && session.ownerWs && session.ownerWs.readyState === 1) {
1015
- session.ownerWs.send(JSON.stringify({ type: 'inject', data }));
1016
- } else if (type === 'resize' && session.ownerWs && session.ownerWs.readyState === 1) {
1017
- session.ownerWs.send(JSON.stringify({ type: 'resize', cols, rows }));
1029
+ if (type === 'input' && activeSession.ownerWs && activeSession.ownerWs.readyState === 1) {
1030
+ activeSession.ownerWs.send(JSON.stringify({ type: 'inject', data }));
1031
+ } else if (type === 'resize' && activeSession.ownerWs && activeSession.ownerWs.readyState === 1) {
1032
+ activeSession.ownerWs.send(JSON.stringify({ type: 'resize', cols, rows }));
1018
1033
  }
1019
1034
  }
1020
1035
  } else {
1021
1036
  // Existing spawned session logic
1022
1037
  if (type === 'input') {
1023
- session.ptyProcess.write(data);
1038
+ activeSession.ptyProcess.write(data);
1024
1039
  } else if (type === 'resize') {
1025
- session.ptyProcess.resize(cols, rows);
1040
+ activeSession.ptyProcess.resize(cols, rows);
1026
1041
  }
1027
1042
  }
1028
1043
  } catch (e) {
@@ -1031,17 +1046,17 @@ wss.on('connection', (ws, req) => {
1031
1046
  });
1032
1047
 
1033
1048
  ws.on('close', () => {
1034
- session.clients.delete(ws);
1035
- if (session.type === 'wrapped' && ws === session.ownerWs) {
1036
- session.ownerWs = null;
1037
- console.log(`[WS] Wrap owner disconnected from session ${sessionId} (Total: ${session.clients.size})`);
1049
+ activeSession.clients.delete(ws);
1050
+ if (activeSession.type === 'wrapped' && ws === activeSession.ownerWs) {
1051
+ activeSession.ownerWs = null;
1052
+ console.log(`[WS] Wrap owner disconnected from session ${sessionId} (Total: ${activeSession.clients.size})`);
1038
1053
  // Clean up wrapped session when owner disconnects and no other clients
1039
- if (session.clients.size === 0 && !session.isClosing) {
1054
+ if (activeSession.clients.size === 0 && !activeSession.isClosing) {
1040
1055
  delete sessions[sessionId];
1041
1056
  console.log(`[CLEANUP] Wrapped session ${sessionId} removed (owner disconnected)`);
1042
1057
  }
1043
1058
  } else {
1044
- console.log(`[WS] Client detached from session ${sessionId} (Total: ${session.clients.size})`);
1059
+ console.log(`[WS] Client detached from session ${sessionId} (Total: ${activeSession.clients.size})`);
1045
1060
  }
1046
1061
  });
1047
1062
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dmsdc-ai/aigentry-telepty",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "main": "daemon.js",
5
5
  "bin": {
6
6
  "aigentry-telepty": "install.js",