forked from doctly/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrigger-context.js
More file actions
56 lines (53 loc) · 1.79 KB
/
Copy pathtrigger-context.js
File metadata and controls
56 lines (53 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// trigger-context.js — see .ai/contexts/trigger-watcher.md
'use strict';
// Local session handle — see .ai/contexts/trigger-watcher.md, "Session handle".
function createLocalSessionHandle(ptyProcess) {
return {
write(data) { ptyProcess.write(data); },
isAlive() {
if (!ptyProcess || typeof ptyProcess.pid !== 'number') return false;
try {
process.kill(ptyProcess.pid, 0);
return true;
} catch (e) {
return e.code === 'EPERM';
}
},
};
}
/**
* Build the `ctx` object trigger-watcher's `start(ctx)` expects.
*
* @param {object} deps
* @param {Map} deps.activeSessions
* @param {object} deps.log electron-log compatible logger
* @param {function} [deps.isPtyAlive] (ptyProcess) => boolean
* @returns {object} ctx
*/
function createTriggerContext({ activeSessions, log, isPtyAlive }) {
const ctx = {
log,
getPtyForSession(sessionId) {
const session = activeSessions.get(sessionId);
if (!session || session.exited) return null;
const handle = (session.host == null)
? createLocalSessionHandle(session.pty)
: session.handle;
// cwd: see .ai/contexts/trigger-watcher.md, "Target guard"
return { ptyProcess: session.pty, cwd: session.cwd, handle };
},
isSessionBusy(sessionId) {
const session = activeSessions.get(sessionId);
return session ? !!session._cliBusy : false;
},
getComposerState(sessionId) {
const session = activeSessions.get(sessionId);
if (!session || session.exited || !session.composerState) return null;
const { pending, lastInputAt } = session.composerState;
return { pending, lastInputAt };
},
};
if (isPtyAlive) ctx.isPtyAlive = isPtyAlive;
return ctx;
}
module.exports = { createTriggerContext, createLocalSessionHandle };