-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.js
More file actions
131 lines (119 loc) · 3.56 KB
/
Copy pathcommon.js
File metadata and controls
131 lines (119 loc) · 3.56 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const fs = require('fs');
const http = require('http');
const path = require('path');
const { spawnSync } = require('child_process');
const { PROJECT_ROOT } = require('../../proxy/shared');
const { spawnFileInputSync } = require('../fs_ipc');
function hookBlock(reason) {
return { stdout: JSON.stringify({ decision: 'block', reason }), stderr: ' ', exit_code: 2 };
}
function allow(stdout = '', stderr = ' ') {
return { stdout, stderr, exit_code: 0 };
}
function parse(stdinJson) {
try { return JSON.parse(stdinJson || '{}'); } catch (_e) { return {}; }
}
function toolInput(stdinJson) {
const payload = parse(stdinJson);
return payload.tool_input || {};
}
function appendUnique(file, line) {
fs.mkdirSync(path.dirname(file), { recursive: true });
let text = '';
try { text = fs.readFileSync(file, 'utf8'); } catch (_e) { /* missing */ }
if (!text.split(/\r?\n/).includes(line)) fs.appendFileSync(file, `${line}\n`);
}
function extractBgOutputPath(payload) {
const result = payload.tool_result || payload.tool_response || '';
const text = Array.isArray(result)
? result.map((x) => (x && typeof x === 'object') ? (x.text || '') : String(x)).join(' ')
: (typeof result === 'object' ? JSON.stringify(result) : String(result || ''));
const m = text.match(/Output is being written to: (\S+)/);
return m ? m[1] : '';
}
function runPython(args, input = '', timeoutMs = 30_000, label = 'python') {
if (input) {
return spawnFileInputSync('python3', args, {
input,
timeoutMs,
cwd: PROJECT_ROOT,
env: { PROJECT_ROOT },
label,
});
}
const result = spawnSync('python3', args, {
cwd: PROJECT_ROOT,
env: { ...process.env, PROJECT_ROOT },
encoding: 'utf8',
timeout: timeoutMs,
});
return {
stdout: result.stdout || '',
stderr: result.stderr || '',
exit_code: Number.isInteger(result.status) ? result.status : (result.error ? -1 : 0),
signal: result.signal || null,
error: result.error || null,
};
}
function runNodeTool(command, args, timeoutMs = 20_000) {
const result = spawnSync(command, args, {
cwd: PROJECT_ROOT,
env: { ...process.env, PROJECT_ROOT },
encoding: 'utf8',
timeout: timeoutMs,
});
return {
stdout: result.stdout || '',
stderr: result.stderr || '',
exit_code: Number.isInteger(result.status) ? result.status : (result.error ? -1 : 0),
signal: result.signal || null,
error: result.error || null,
};
}
function httpGetOk(port, route) {
return new Promise((resolve) => {
const req = http.request({ host: '127.0.0.1', port, path: route, timeout: 1000 }, (res) => {
res.resume();
resolve(res.statusCode >= 200 && res.statusCode < 300);
});
req.on('error', () => resolve(false));
req.on('timeout', () => req.destroy());
req.end();
});
}
function httpPostJson(port, route, body) {
const payload = Buffer.from(JSON.stringify(body));
return new Promise((resolve) => {
const req = http.request({
host: '127.0.0.1',
port,
path: route,
method: 'POST',
timeout: 2000,
headers: { 'Content-Type': 'application/json', 'Content-Length': payload.length },
}, (res) => {
res.resume();
resolve(res.statusCode >= 200 && res.statusCode < 300);
});
req.on('error', () => resolve(false));
req.on('timeout', () => req.destroy());
req.write(payload);
req.end();
});
}
module.exports = {
PROJECT_ROOT,
allow,
appendUnique,
extractBgOutputPath,
fs,
hookBlock,
httpGetOk,
httpPostJson,
parse,
path,
runNodeTool,
runPython,
toolInput,
};