@dmsdc-ai/aigentry-telepty 0.1.49 → 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.
- package/daemon.js +80 -68
- 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,72 +1323,8 @@ busWss.on('connection', (ws, req) => {
|
|
|
1247
1323
|
}
|
|
1248
1324
|
});
|
|
1249
1325
|
|
|
1250
|
-
// Auto-
|
|
1251
|
-
|
|
1252
|
-
if (isRoutable) {
|
|
1253
|
-
console.log(`[BUS-ROUTE] Received routable event: type=${msg.type} target=${msg.target_session_id || msg.target || msg.session_id}`);
|
|
1254
|
-
const rawTarget = msg.target_session_id || msg.target || msg.session_id;
|
|
1255
|
-
const targetId = resolveSessionAlias(rawTarget);
|
|
1256
|
-
const targetSession = targetId ? sessions[targetId] : null;
|
|
1257
|
-
if (targetSession) {
|
|
1258
|
-
const prompt = msg.content || msg.prompt || JSON.stringify(msg);
|
|
1259
|
-
const inject_id = crypto.randomUUID();
|
|
1260
|
-
|
|
1261
|
-
// Write to session (kitty primary, WS fallback)
|
|
1262
|
-
const sock = findKittySocket();
|
|
1263
|
-
if (!targetSession.kittyWindowId && sock) targetSession.kittyWindowId = findKittyWindowId(sock, targetId);
|
|
1264
|
-
const wid = targetSession.kittyWindowId;
|
|
1265
|
-
let delivered = false;
|
|
1266
|
-
|
|
1267
|
-
if (wid && sock && targetSession.type === 'wrapped') {
|
|
1268
|
-
try {
|
|
1269
|
-
const escaped = prompt.replace(/\\/g, '\\\\').replace(/'/g, "'\\''");
|
|
1270
|
-
require('child_process').execSync(`kitty @ --to unix:${sock} send-text --match id:${wid} '${escaped}'`, {
|
|
1271
|
-
timeout: 5000, stdio: ['pipe', 'pipe', 'pipe']
|
|
1272
|
-
});
|
|
1273
|
-
setTimeout(() => {
|
|
1274
|
-
try {
|
|
1275
|
-
require('child_process').execSync(`kitty @ --to unix:${sock} send-key --match id:${wid} Return`, {
|
|
1276
|
-
timeout: 3000, stdio: ['pipe', 'pipe', 'pipe']
|
|
1277
|
-
});
|
|
1278
|
-
} catch {}
|
|
1279
|
-
}, 500);
|
|
1280
|
-
delivered = true;
|
|
1281
|
-
} catch {}
|
|
1282
|
-
}
|
|
1283
|
-
if (!delivered) {
|
|
1284
|
-
// WS fallback
|
|
1285
|
-
if (targetSession.type === 'wrapped' && targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
|
|
1286
|
-
targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: prompt }));
|
|
1287
|
-
setTimeout(() => {
|
|
1288
|
-
if (targetSession.ownerWs && targetSession.ownerWs.readyState === 1) {
|
|
1289
|
-
targetSession.ownerWs.send(JSON.stringify({ type: 'inject', data: '\r' }));
|
|
1290
|
-
}
|
|
1291
|
-
}, 300);
|
|
1292
|
-
delivered = true;
|
|
1293
|
-
} else if (targetSession.ptyProcess) {
|
|
1294
|
-
targetSession.ptyProcess.write(prompt);
|
|
1295
|
-
setTimeout(() => targetSession.ptyProcess.write('\r'), 300);
|
|
1296
|
-
delivered = true;
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
|
|
1300
|
-
// Emit inject_written ack
|
|
1301
|
-
const ackMsg = JSON.stringify({
|
|
1302
|
-
type: 'inject_written',
|
|
1303
|
-
inject_id,
|
|
1304
|
-
sender: 'daemon',
|
|
1305
|
-
target_agent: targetId,
|
|
1306
|
-
source_type: 'bus_auto_route',
|
|
1307
|
-
delivered,
|
|
1308
|
-
timestamp: new Date().toISOString()
|
|
1309
|
-
});
|
|
1310
|
-
busClients.forEach(client => {
|
|
1311
|
-
if (client.readyState === 1) client.send(ackMsg);
|
|
1312
|
-
});
|
|
1313
|
-
console.log(`[BUS-ROUTE] turn_request → ${targetId}: ${delivered ? 'delivered' : 'failed'}`);
|
|
1314
|
-
}
|
|
1315
|
-
}
|
|
1326
|
+
// Auto-route turn_request events (shared logic with HTTP publish)
|
|
1327
|
+
busAutoRoute(msg);
|
|
1316
1328
|
} catch (e) {
|
|
1317
1329
|
console.error('[BUS] Invalid message format', e);
|
|
1318
1330
|
}
|