Files
gnome-jarvis/daemon/index.js
T
2026-09-11 13:40:53 -04:00

47 lines
2.4 KiB
JavaScript

import { EventEmitter } from 'node:events';
import { HarnessBridge } from './harness-bridge.js';
import { ComputerUseSession } from '../computer-use/session.js';
import { VoiceStateMachine } from './voice-state.js';
import { QvacScheduler } from './qvac-scheduler.js';
export class JarvisDaemon extends EventEmitter {
constructor() {
super();
this.state = 'ARMED';
this.voice = new VoiceStateMachine();
this.scheduler = new QvacScheduler({ concurrency: 1 });
this.computer = new ComputerUseSession();
this.harness = new HarnessBridge({ cwd: process.cwd() });
this.harness.on('agent_message_chunk', (ev) => this.emit('Token', ev?.text || ev?.delta || ''));
this.harness.on('permission', (ev) => this.emit('ConfirmationRequired', ev));
}
setState(state) { this.state = state; this.emit('StateChanged', state); }
arm() { this.voice.wake(); this.setState('LISTENING'); }
sleep() { this.voice.sleep(); this.setState('SLEEPING'); }
say(text) { this.emit('Reply', String(text)); }
async ask(text) {
this.voice.utterance(); this.setState('THINKING');
try {
const reply = await this.scheduler.run(() => this.harness.ask(text), { lane: 'voice' });
this.voice.speak(); this.setState('SPEAKING'); this.emit('Reply', reply); return reply;
} catch (error) {
this.emit('Error', 'QVAC', error.message); this.voice.cancel(); this.setState('ARMED'); throw error;
}
}
cancel() { this.harness.cancel(); this.scheduler.cancelQueued((job) => job.lane === 'voice'); this.computer.revoke(); this.voice.cancel(); this.setState('ARMED'); }
computerGrant(persist = false) { const result = this.computer.grant({ persist }); this.emit('ComputerStep', JSON.stringify({ action: 'grant', ...result })); return result; }
computerRevoke() { this.computer.revoke(); this.emit('ComputerStep', JSON.stringify({ action: 'revoke' })); }
async close() { this.computerRevoke(); await this.harness.close(); }
}
if (import.meta.url === `file://${process.argv[1]}`) {
const daemon = new JarvisDaemon();
import('./dbus-service.js').then(({ serveOnSessionBus }) => serveOnSessionBus(daemon)).catch((error) => {
daemon.emit('Error', 'DBUS_UNAVAILABLE', error.message);
console.error(`jarvisd: D-Bus unavailable: ${error.message}`);
});
process.on('SIGINT', () => daemon.close().finally(() => process.exit(0)));
console.log('jarvisd scaffold ready; use the D-Bus adapter when installed');
}