Files
gnome-jarvis/computer-use/portal-input.js
T
snxraven 5b7c2b3b6d
Rolling release / release (push) Successful in 9m40s
Fix libei
2026-09-12 11:07:40 -04:00

53 lines
2.9 KiB
JavaScript

import { spawn } from 'node:child_process';
import path from 'node:path';
/** Wayland input boundary. The helper owns portal consent and the EIS fd;
* Node sends only bounded JSON actions and never uses hidden uinput. */
export class PortalInputBackend {
constructor({ command = process.env.JARVIS_EI_HELPER || path.resolve(new URL('./py/portal_remote_desktop.py', import.meta.url).pathname), python = 'python3', spawnImpl = spawn, timeoutMs = 120_000 } = {}) {
this.command = command; this.python = python; this.spawnImpl = spawnImpl; this.timeoutMs = timeoutMs;
this.process = null; this.available = false; this._grant = null;
}
grant({ persist = false, monitors = 'focused' } = {}) {
if (this._grant) return this._grant;
const child = this.process = this.spawnImpl(this.python, [this.command], { stdio: ['pipe', 'pipe', 'pipe'] });
this._grant = new Promise((resolve, reject) => {
let buffer = ''; let settled = false;
const timer = setTimeout(() => fail(new Error('portal input consent timed out')), this.timeoutMs);
const fail = (error) => {
clearTimeout(timer);
if (this.process === child) { this.available = false; this.process = null; this._grant = null; }
if (!settled) { settled = true; reject(error); }
child.kill('SIGTERM');
};
this._cancelGrant = () => fail(new Error('portal input grant revoked'));
child.on('error', fail);
child.once('close', () => {
clearTimeout(timer);
if (this.process === child) { this.available = false; this.process = null; this._grant = null; }
if (!settled) { settled = true; reject(new Error('portal input helper exited before readiness')); }
});
child.stdin?.on('error', fail);
child.stderr?.on('data', () => {});
child.stdout.on('data', (data) => {
buffer += String(data);
if (buffer.length > 64 * 1024) { fail(new Error('portal helper output too large')); return; }
let index;
while ((index = buffer.indexOf('\n')) >= 0) {
const line = buffer.slice(0, index); buffer = buffer.slice(index + 1);
let event;
try { event = JSON.parse(line); } catch { continue; }
if (event.type === 'error') { fail(new Error(event.reason || 'portal input unavailable')); return; }
if (event.type === 'ready' && this.process === child && !settled) {
clearTimeout(timer); settled = true; this.available = true;
resolve({ restore_token_present: Boolean(event.restore_token_present), monitors, backend: event.backend || 'portal-ei' });
}
}
});
});
return this._grant;
}
send(action) { if (!this.available || !this.process?.stdin?.writable) throw new Error('portal EIS input backend is unavailable'); this.process.stdin.write(`${JSON.stringify(action)}\n`); }
revoke() { this._cancelGrant?.(); this._cancelGrant = null; this.available = false; this.process = null; this._grant = null; }
}