-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
112 lines (101 loc) · 3.35 KB
/
utils.js
File metadata and controls
112 lines (101 loc) · 3.35 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
import {createReadStream} from 'fs';
import readline from 'readline';
import winston from 'winston';
export async function image_to_base64(image_file) {
return new Promise((resolve, reject) => {
const stream = createReadStream(image_file);
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('end', () => {
const buffer = Buffer.concat(chunks);
const base64Data = buffer.toString('base64');
const dataURI = `data:image/jpeg;base64,${base64Data}`;
resolve(dataURI);
});
stream.on('error', reject);
});
}
export async function input(text) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question(text, (answer) => {
rl.close();
resolve(answer);
});
});
}
export const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
export function extractJSONWithSchema(text) {
function findJSONObjects(str) {
const objects = [];
let depth = 0;
let start = -1;
let inString = false;
let escapeNext = false;
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (inString) {
if (char === '"' && !escapeNext) inString = false;
escapeNext = char === '\\' && !escapeNext;
} else {
if (char === '{') {
if (depth === 0) start = i;
depth++;
} else if (char === '}') {
depth--;
if (depth === 0 && start !== -1) {
objects.push(str.substring(start, i + 1));
start = -1;
}
} else if (char === '"') {
inString = true;
}
}
}
return objects;
}
function isValidSchema(obj) {
return obj &&
typeof obj === 'object' &&
Array.isArray(obj.actions) &&
obj.actions.every(action =>
action &&
typeof action === 'object' &&
['url', 'click', 'input'].includes(action.type) &&
typeof action.value === 'string' &&
(action.type !== 'click' && action.type !== 'input' || typeof action.elementId === 'number')
) &&
Array.isArray(obj.order_of_execution) &&
obj.order_of_execution.every(index =>
Number.isInteger(index) &&
index >= 0 &&
index < obj.actions.length
);
}
const jsonObjects = findJSONObjects(text);
return jsonObjects
.map(jsonStr => {
try {
return JSON.parse(jsonStr);
} catch {
return null;
}
})
.filter(isValidSchema);
}
export const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.printf(({timestamp, level, message}) => {
return `${timestamp} [${level}]: ${message}`;
})
),
transports: [
new winston.transports.Console(),
new winston.transports.File({filename: 'application.log'})
]
});