Repository navigation
Expand file tree
/
Copy pathnative-protocol.mjs
More file actions
54 lines (50 loc) · 2.74 KB
/
Copy pathnative-protocol.mjs
File metadata and controls
54 lines (50 loc) · 2.74 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
export const maxBridgeMessageBytes = 8 * 1024 * 1024;
const parameters = {
list_apps: [],
get_app_state: ['include_screenshot', 'verbose', 'max_nodes', 'max_depth', 'max_width', 'max_height', 'max_bytes', 'scale', 'format', 'quality'],
screenshot: ['max_width', 'max_height', 'max_bytes', 'scale', 'format', 'quality'],
click: ['x', 'y', 'button', 'click_count', 'relative'],
scroll: ['x', 'y', 'direction', 'pages', 'relative'],
press_key: ['key'],
type_text: ['text'],
};
function validateParameters(params) {
const integer = (name, minimum, maximum) => {
if (params[name] !== undefined && (!Number.isInteger(params[name]) || params[name] < minimum || params[name] > maximum)) {
throw new Error(`Invalid native ${name} parameter`);
}
};
if (params.include_screenshot !== undefined && typeof params.include_screenshot !== 'boolean') throw new Error('Invalid native include_screenshot parameter');
if (params.verbose !== undefined && typeof params.verbose !== 'boolean') throw new Error('Invalid native verbose parameter');
integer('max_nodes', 1, 10000);
integer('max_depth', 0, 64);
integer('max_width', 1, 4096);
integer('max_height', 1, 4096);
integer('max_bytes', 1024, 4 * 1024 * 1024);
integer('quality', 1, 95);
if (params.scale !== undefined && (typeof params.scale !== 'number' || !Number.isFinite(params.scale) || params.scale <= 0 || params.scale > 1)) throw new Error('Invalid native scale parameter');
if (params.format !== undefined && !['png', 'jpeg'].includes(params.format)) throw new Error('Invalid native format parameter');
}
export function validateNativeRequest(input) {
const { method, app, params = {} } = input ?? {};
if (!Object.hasOwn(parameters, method)) throw new Error('This native Linux Computer Use operation is not supported');
if (!params || typeof params !== 'object' || Array.isArray(params) || Object.keys(params).some(key => !parameters[method].includes(key))) throw new Error('Unsupported native operation parameter');
validateParameters(params);
if (method === 'screenshot' && app === undefined) throw new Error('A non-empty native app id is required');
if (app !== undefined) {
if (typeof app !== 'string' || !app.trim()) throw new Error('A non-empty native app id is required');
if (app.startsWith('linux-window:')) {
const id = app.slice('linux-window:'.length);
if (!/^\d+$/.test(id) || BigInt(id) > 18446744073709551615n) throw new Error('Invalid native window id');
}
}
return { method, app, params };
}
export function nativeTarget(app) {
if (app === undefined) return {};
if (app.startsWith('linux-window:')) {
const id = app.slice('linux-window:'.length);
return { window_id: JSON.rawJSON(BigInt(id).toString()) };
}
return { app_id: app };
}