-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess-runner.js
More file actions
76 lines (66 loc) · 2.03 KB
/
Copy pathprocess-runner.js
File metadata and controls
76 lines (66 loc) · 2.03 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
function createProcessRunner({ sessionsDir } = {}) {
const defaultDir = sessionsDir || process.env.SESSIONS_DIR || path.join(__dirname, '..', 'opencode-sessions');
function runOpenCodeInBackground({ sessionId, prompt, cwd, logDir }) {
const logPath = path.join(logDir || defaultDir, `.${sessionId}.run.log`);
const out = fs.openSync(logPath, 'a');
const err = fs.openSync(logPath, 'a');
const child = spawn('opencode', [
'run', prompt.trim(),
'-s', sessionId,
'--dangerously-skip-permissions',
'--format', 'json',
'--dir', cwd
], {
detached: true,
stdio: ['ignore', out, err]
});
child.unref();
fs.closeSync(out);
fs.closeSync(err);
return child;
}
function openTerminal({ cwd, command, terminal = 'ptyxis', shell = 'bash' }) {
const safeCwd = cwd.replace(/'/g, "'\\''");
const cmdStr = `cd '${safeCwd}' && ${command}; exec ${shell}`;
let args;
switch (terminal) {
case 'kitty':
args = [shell, '-c', cmdStr];
break;
case 'alacritty':
case 'xterm':
case 'konsole':
args = ['-e', shell, '-c', cmdStr];
break;
case 'wezterm':
args = ['start', '--', shell, '-c', cmdStr];
break;
case 'gnome-terminal':
case 'ptyxis':
default:
args = ['--', shell, '-c', cmdStr];
break;
}
const child = spawn(terminal, args, {
detached: true,
stdio: 'ignore'
});
child.unref();
return child;
}
function openSessionInTerminal({ sessionId, cwd, terminal, shell }) {
return openTerminal({ cwd, command: `opencode -s ${sessionId}`, terminal, shell });
}
function openNewSessionInTerminal({ cwd, terminal, shell }) {
return openTerminal({ cwd, command: 'opencode', terminal, shell });
}
return {
runOpenCodeInBackground,
openSessionInTerminal,
openNewSessionInTerminal
};
}
module.exports = { createProcessRunner };