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'; import { cancelQvac, resumeQvac, suspendQvac } from './qvac-master.js'; import { PrivacyLog } from './privacy-log.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(), computer: this.computer }); this.log = new PrivacyLog(); this.locked = false; this._idleTimer = setInterval(() => this.tickIdle(), 30_000); this._idleTimer.unref?.(); this.harness.on('agent_message_chunk', (ev) => this.emit('Token', ev?.text || ev?.delta || '')); this.harness.on('permission', (ev) => this.emit('ConfirmationRequired', ev)); this.harness.on('hud_sidecar', (ev) => this.emit('ChipOffered', 'sidecar', ev?.title || 'Suggested action', JSON.stringify(ev || {}))); } setState(state) { this.state = state; this.emit('StateChanged', state); this.log.record('state', { state }).catch(() => {}); } async arm() { if (this.locked) return; await resumeQvac().catch(() => {}); this.voice.wake(); this.setState('LISTENING'); } async sleep() { this.voice.sleep(); this.setState('SLEEPING'); await suspendQvac().catch((error) => this.emit('Error', 'QVAC_SUSPEND', error.message)); } 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'); cancelQvac().catch((error) => this.emit('Error', 'QVAC_CANCEL', error.message)); } 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' })); } handleLockScreen(locked) { this.locked = Boolean(locked); if (this.locked) { this.cancel(); this.setState('ARMED'); } this.emit('LockScreenChanged', this.locked); } tickIdle() { if (!this.locked && this.voice.expireIdle() === 'SLEEPING' && this.state !== 'SLEEPING') this.sleep(); } async close() { clearInterval(this._idleTimer); 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'); }