@dmsdc-ai/aigentry-telepty 0.1.50 → 0.1.51

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/daemon.js +80 -72
  2. package/package.json +1 -1
package/daemon.js CHANGED
@@ -824,15 +824,88 @@ app.delete('/api/sessions/:id', (req, res) => {
824
824
  }
825
825
  });
826
826
 
827
+ // Shared auto-router: handles turn_request events from any source (WS or HTTP)
828
+ function busAutoRoute(msg) {
829
+ const eventType = msg.type || msg.kind;
830
+ const isRoutable = (eventType === 'turn_request' || eventType === 'deliberation_route_turn') && (msg.target || msg.target_session_id);
831
+ if (!isRoutable) return;
832
+
833
+ const rawTarget = (msg.target || msg.target_session_id).split('@')[0];
834
+ console.log(`[BUS-ROUTE] Received ${eventType}: target=${rawTarget}`);
835
+ const targetId = resolveSessionAlias(rawTarget);
836
+ const targetSession = targetId ? sessions[targetId] : null;
837
+ if (!targetSession) {
838
+ console.log(`[BUS-ROUTE] Target ${rawTarget} not found`);
839
+ return;
840
+ }
841
+
842
+ const prompt = (msg.payload && msg.payload.prompt) || msg.content || msg.prompt || JSON.stringify(msg);
843
+ const inject_id = crypto.randomUUID();
844
+
845
+ // Write to session (kitty primary, WS fallback)
846
+ const sock = findKittySocket();
847
+ if (!targetSession.kittyWindowId && sock) targetSession.kittyWindowId = findKittyWindowId(sock, targetId);
848
+ const wid = targetSession.kittyWindowId;
849
+ let delivered = false;
850
+
851
+ if (wid && sock && targetSession.type === 'wrapped') {
852
+ try {
853
+ const escaped = prompt.replace(/\\/g, '\\\\').replace(/'/g, "'\\''");
854
+ require('child_process').execSync(`kitty @ --to unix:${sock} send-text --match id:${wid} '${escaped}'`, {
855
+ timeout: 5000, stdio: ['pipe', 'pipe', 'pipe']
856
+ });
857
+ setTimeout(() => {
858
+ try {
859
+ require('child_process').execSync(`kitty @ --to unix:${sock} send-key --match id:${wid} Return`, {
860
+ timeout: 3000, stdio: ['pipe', 'pipe', 'pipe']
861
+ });
862
+ } catch {}
863
+ }, 500);
864
+ delivered = true;
865
+ } catch {}
866
+ }
867
+ if (!delivered) {
868
+ if (targetSession.type === 'wrapped' && targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
869
+ targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: prompt }));
870
+ setTimeout(() => {
871
+ if (targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
872
+ targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: '\r' }));
873
+ }
874
+ }, 300);
875
+ delivered = true;
876
+ } else if (targetSession.ptyProcess) {
877
+ targetSession.ptyProcess.write(prompt);
878
+ setTimeout(() => targetSession.ptyProcess.write('\r'), 300);
879
+ delivered = true;
880
+ }
881
+ }
882
+
883
+ // Emit inject_written ack
884
+ const ackMsg = JSON.stringify({
885
+ type: 'inject_written',
886
+ inject_id,
887
+ sender: 'daemon',
888
+ target_agent: targetId,
889
+ source_type: 'bus_auto_route',
890
+ delivered,
891
+ timestamp: new Date().toISOString()
892
+ });
893
+ busClients.forEach(client => {
894
+ if (client.readyState === 1) client.send(ackMsg);
895
+ });
896
+ targetSession.lastActivityAt = new Date().toISOString();
897
+ console.log(`[BUS-ROUTE] ${eventType} → ${targetId}: ${delivered ? 'delivered' : 'failed'}`);
898
+ }
899
+
827
900
  app.post('/api/bus/publish', (req, res) => {
828
901
  const payload = req.body;
829
-
902
+
830
903
  if (!payload || typeof payload !== 'object') {
831
904
  return res.status(400).json({ error: 'Payload must be a JSON object' });
832
905
  }
833
906
 
834
907
  let deliveredCount = 0;
835
-
908
+
836
909
  busClients.forEach(client => {
837
910
  if (client.readyState === 1) { // WebSocket.OPEN
838
911
  client.send(JSON.stringify(payload));
@@ -840,6 +913,9 @@ app.post('/api/bus/publish', (req, res) => {
840
913
  }
841
914
  });
842
915
 
916
+ // Auto-route if this is a turn_request
917
+ busAutoRoute(payload);
918
+
843
919
  res.json({ success: true, delivered: deliveredCount });
844
920
  });
845
921
 
@@ -1247,76 +1323,8 @@ busWss.on('connection', (ws, req) => {
1247
1323
  }
1248
1324
  });
1249
1325
 
1250
- // Auto-router: turn_request inject to target session PTY
1251
- // Matches both msg.type and msg.kind (deliberation uses 'kind')
1252
- const eventType = msg.type || msg.kind;
1253
- const isRoutable = (eventType === 'turn_request' || eventType === 'deliberation_route_turn') && (msg.target || msg.target_session_id);
1254
- if (isRoutable) {
1255
- // Parse target: may include @host suffix (e.g. "session-001@100.x.y.z")
1256
- const rawTarget = (msg.target || msg.target_session_id).split('@')[0];
1257
- console.log(`[BUS-ROUTE] Received ${eventType}: target=${rawTarget}`);
1258
- const targetId = resolveSessionAlias(rawTarget);
1259
- const targetSession = targetId ? sessions[targetId] : null;
1260
- if (targetSession) {
1261
- // Extract prompt from payload.prompt (deliberation schema) or fallbacks
1262
- const prompt = (msg.payload && msg.payload.prompt) || msg.content || msg.prompt || JSON.stringify(msg);
1263
- const inject_id = crypto.randomUUID();
1264
-
1265
- // Write to session (kitty primary, WS fallback)
1266
- const sock = findKittySocket();
1267
- if (!targetSession.kittyWindowId && sock) targetSession.kittyWindowId = findKittyWindowId(sock, targetId);
1268
- const wid = targetSession.kittyWindowId;
1269
- let delivered = false;
1270
-
1271
- if (wid && sock && targetSession.type === 'wrapped') {
1272
- try {
1273
- const escaped = prompt.replace(/\\/g, '\\\\').replace(/'/g, "'\\''");
1274
- require('child_process').execSync(`kitty @ --to unix:${sock} send-text --match id:${wid} '${escaped}'`, {
1275
- timeout: 5000, stdio: ['pipe', 'pipe', 'pipe']
1276
- });
1277
- setTimeout(() => {
1278
- try {
1279
- require('child_process').execSync(`kitty @ --to unix:${sock} send-key --match id:${wid} Return`, {
1280
- timeout: 3000, stdio: ['pipe', 'pipe', 'pipe']
1281
- });
1282
- } catch {}
1283
- }, 500);
1284
- delivered = true;
1285
- } catch {}
1286
- }
1287
- if (!delivered) {
1288
- // WS fallback
1289
- if (targetSession.type === 'wrapped' && targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
1290
- targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: prompt }));
1291
- setTimeout(() => {
1292
- if (targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
1293
- targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: '\r' }));
1294
- }
1295
- }, 300);
1296
- delivered = true;
1297
- } else if (targetSession.ptyProcess) {
1298
- targetSession.ptyProcess.write(prompt);
1299
- setTimeout(() => targetSession.ptyProcess.write('\r'), 300);
1300
- delivered = true;
1301
- }
1302
- }
1303
-
1304
- // Emit inject_written ack
1305
- const ackMsg = JSON.stringify({
1306
- type: 'inject_written',
1307
- inject_id,
1308
- sender: 'daemon',
1309
- target_agent: targetId,
1310
- source_type: 'bus_auto_route',
1311
- delivered,
1312
- timestamp: new Date().toISOString()
1313
- });
1314
- busClients.forEach(client => {
1315
- if (client.readyState === 1) client.send(ackMsg);
1316
- });
1317
- console.log(`[BUS-ROUTE] turn_request → ${targetId}: ${delivered ? 'delivered' : 'failed'}`);
1318
- }
1319
- }
1326
+ // Auto-route turn_request events (shared logic with HTTP publish)
1327
+ busAutoRoute(msg);
1320
1328
  } catch (e) {
1321
1329
  console.error('[BUS] Invalid message format', e);
1322
1330
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dmsdc-ai/aigentry-telepty",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
4
4
  "main": "daemon.js",
5
5
  "bin": {
6
6
  "aigentry-telepty": "install.js",