-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
322 lines (271 loc) · 8.55 KB
/
server.js
File metadata and controls
322 lines (271 loc) · 8.55 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { spawn } = require("node:child_process");
const ipc = require("node-ipc").default;
const fs = require('fs')
ipc.config.id = "world";
ipc.config.retry = 1500;
ipc.config.rawBuffer = true;
ipc.config.encoding = "utf-8";
ipc.config.silent = true;
ipc.config.logDepth = 0; //default
ipc.config.logger = () => {};
const ci =
"The next prompts will be a list of instructions. Consider the following notes for each instruction: If opening a browser, prioritize using google chrome in fullscreen unless otherwise instructed. Go as fast as you can. Your working directory is /Users/ec2-user/actions-runner/_work/testdriver/testdriver, check for files and code there first. DO NOT USE `screenshot.show()`. DO NOT USE APPLESCRIPT. It is perfectly fine to use coordinates as long as the coordinates were determined through a search and not a guess.";
function markdownToListArray(markdown) {
// Normalize line breaks
const normalizedMarkdown = markdown.replace(/\\n/g, "\n");
console.log("normalizedMarkdown", normalizedMarkdown);
// Split into lines, filter out non-list items, and remove the leading number and period
const listItems = normalizedMarkdown
.split("\n")
.filter((line) => line.match(/^\d+\. /))
.map((item) => {
item = item.replace(/^\d+\. /, "");
item = item.replace(/\\r/g, "");
return item;
}); // Remove the leading numbers and period
return listItems;
}
ipc.serve(function () {
ipc.server.on("connect", function (socket) {
ipc.server.emit(
socket,
JSON.stringify({
method: "status",
message: "connected",
})
);
});
ipc.server.on("data", async (data, socket) => {
console.log(JSON.parse(data.toString()));
await spawnShell(data, socket).catch((e) => {
console.error(e)
});
spawnInterpreter(data, socket);
});
});
let i = 0;
const spawnInterpreter = function (data, socket) {
let child;
let text;
let step = 0;
try {
console.log(data.toString());
const args = JSON.parse(data.toString());
text = args[0];
console.log("args1", args[1]);
child = spawn(
`interpreter`,
["--os", "-ci", `"${ci}"`, "--api_key", args[1]],
{
env: { ...process.env }, // FORCE_COLOR: true, will enable advanced rendering
shell: true,
windowsHide: true,
}
);
} catch (e) {
console.log("caught", e);
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: e.toString(),
})
);
}
child.on("error", function (e) {
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: e.toString(),
})
);
});
child.stdout.on("data", async (data) => {
let dataToSend = data.toString();
if (stripAnsi(last(dataToSend.split("\n"))) === "> ") {
console.log("!!!!!! > Detected");
let data = text.split(" ");
console.log("text is", text);
console.log(text);
list = markdownToListArray(text);
// list.unshift(
// "The next prompts will be a list of instructions. Consider the following notes for each instruction: If opening a browser, prioritize using google chrome in fullscreen unless otherwise instructed. Go as fast as you can. Your working directory is /Users/ec2-user/actions-runner/_work/testdriver/testdriver, check for files and code there first."
// );
list.push(
'Summarize the result of the the previous steps. You do not need to strictly comply with instruction steps for the test to pass. Workarounds are irrelevant to the test passing or failing. Say either "The test failed." or "The test passed.". Then in a new paragraph that is 3 sentences long explain how you came to that conclusion. Save this result into /tmp/oiResult.log'
);
console.log("!!!!!! list", list);
if (!list[i]) {
child.stdin.end();
child.stdout.destroy();
child.stderr.destroy();
child.kill();
} else {
console.log("RUNNING COMMAND ", i);
let command = list[i];
child.stdin.write(`${command}\n`);
dataToSend += command;
i++;
}
step += 1;
}
ipc.server.emit(
socket,
JSON.stringify({
method: "stdout",
message: dataToSend,
})
);
});
child.stderr.on("data", (data) => {
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: data.toString(),
})
);
});
child.on("close", (code) => {
ipc.server.emit(
socket,
JSON.stringify({
method: "close",
message: `child process exited with code ${code}\n`,
})
);
});
};
const spawnShell = function (data, socket) {
let child;
let text;
let step = 0;
return new Promise((resolve, reject) => {
try {
const args = JSON.parse(data.toString());
console.log(args)
text = args[0];
key = args[1];
prerun = args[2];
// example input 'rm ~/Desktop/WITH-LOVE-FROM-AMERICA.txt \\n npm install dashcam-chrome --save \\n /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --start-maximized --load-extension=./node_modules/dashcam-chrome/build/ 1>/dev/null 2>&1 & \\n exit'
console.log('PRERUN SCRIPT')
console.log(prerun)
let prerunFilePath = `/tmp/testdriver-prerun.sh`;
// Check if the prerun.sh file doesn't exist
// this can happen if the repo supplies this file within `.testdriver/prerun.sh`
// mostly for backward compatibility
console.log('prerunFilePath', prerunFilePath)
if (prerun) { // this should be swapped, prerun should take over
// Write prerun to the prerun.sh file
console.log('writing ', prerun, 'to', prerunFilePath)
try {fs.writeFileSync(prerunFilePath, prerun.replace(/\\n/g, '\n'), {flag: 'w+'});} catch (e) {
console.error(e)
}
console.log(`Written to ${prerunFilePath}`);
} else {
console.log(`${prerunFilePath} already exists.`);
}
console.log(
"spawning ",
`source ${prerunFilePath}`
);
if (fs.existsSync(prerunFilePath)) {
child = spawn(
`source`,
[prerunFilePath],
{
env: { ...process.env }, // FORCE_COLOR: true, will enable advanced rendering
shell: true,
windowsHide: true,
}
);
} else {
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: `Prerun.sh file does not exist at ${prerunFilePath}`,
})
);
resolve();
}
} catch (e) {
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: e.toString(),
})
);
resolve();
}
child.on("close", function (exitCode) {
console.log("close", exitCode);
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: "Child process exited with code " + exitCode,
})
);
resolve();
});
child.on("error", function (e) {
console.log("error", e);
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: e.toString(),
})
);
resolve();
});
child.stdout.on("end", function () {
ipc.server.emit(
socket,
JSON.stringify({
method: "stderr",
message: "Prerun.sh process end\n",
})
);
resolve();
});
child.stdout.on("data", async (data) => {
let dataToSend = data.toString();
console.log("dataToSend", dataToSend);
ipc.server.emit(
socket,
JSON.stringify({
method: "stdout",
message: dataToSend,
})
);
});
child.stderr.on("data", (data) => {
ipc.server.emit(
socket,
JSON.stringify({
method: "stdout",
message: data.toString(),
})
);
});
});
};
ipc.server.start();
const ansiRegex = (({ onlyFirst = false } = {}) => {
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))",
].join("|");
return new RegExp(pattern, onlyFirst ? undefined : "g");
})();
function stripAnsi(string) {
if (typeof string !== "string") {
throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
}
return string.replace(ansiRegex, "");
}
const last = (arr) => arr[arr.length - 1];