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, callQvac, cancelQvacRequest, qvacStatus } from './qvac-master.js'; import { PrivacyLog } from './privacy-log.js'; import { VoiceLoop } from './voice-loop.js'; import { QvacVoiceAdapter } from './voice-adapters.js'; import { createWakeEngine } from './wake-engine.js'; import { DesktopObserver } from '../computer-use/observer.js'; import { QvacPerception } from './perception.js'; import { ComputerAudit } from '../computer-use/audit.js'; import { PortalInputBackend } from '../computer-use/portal-input.js'; import { ComputerActuator } from '../computer-use/actuator.js'; import { RuntimeTelemetry } from './telemetry.js'; import { StateRecovery } from './recovery.js'; export class JarvisDaemon extends EventEmitter { constructor() { super(); this.recovery = new StateRecovery(); const restored = this.recovery.load(); this.state = restored.state; this.mode = restored.mode; this.voice = new VoiceStateMachine(); this.scheduler = new QvacScheduler({ concurrency: 1 }); this.audit = new ComputerAudit(); this.computer = new ComputerUseSession({ audit: this.audit }); this.input = new PortalInputBackend(); this.perception = new QvacPerception(); this.observer = new DesktopObserver({ ocr: (image) => this.perception.ocr(image) }); this.actuator = new ComputerActuator({ session: this.computer, input: this.input, find: ({ ref }) => this.observer.lastTree.filter((node) => node.ref === ref), atspiAction: (target, action) => this.observer.atspi.action(target, action), highlight: async (target, action) => this.emit('ComputerHighlight', JSON.stringify({ rect: target?.rect || null, label: `${action} ${target?.name || ''}` })) , audit: this.audit }); this.harness = new HarnessBridge({ cwd: process.cwd(), computer: this.computer, observer: this.observer, actuator: this.actuator }); this.log = new PrivacyLog(); this.locked = false; this.lastReply = ''; this.voiceLoop = null; this._idleTimer = setInterval(() => this.tickIdle(), 30_000); this._idleTimer.unref?.(); this.telemetry = new RuntimeTelemetry(); this._telemetryTimer = setInterval(() => this.telemetry.sample(), 60_000); this._telemetryTimer.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; try { this.recovery.save({ state, mode: this.mode }); } catch (error) { this.emit('Error', 'RECOVERY_WRITE', error.message); } 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.lastReply = String(text); this.emit('Reply', this.lastReply); } async ask(text) { this.voice.utterance(); this.setState('THINKING'); try { const startedAt = Date.now(); const reply = await this.scheduler.run(() => this.harness.ask(text), { lane: 'voice' }); this.telemetry.record('llm', startedAt, { success: true }); this.voice.speak(); this.setState('SPEAKING'); this.lastReply = String(reply || ''); this.emit('Reply', this.lastReply); this.voiceLoop?.speak(this.lastReply).catch((error) => this.emit('Error', 'TTS', error.message)); return reply; } catch (error) { this.telemetry.record('llm', Date.now(), { success: false }); 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 })); this.input.grant({ persist }).then((backend) => { this.computer.setBackend(backend.backend); this.emit('ComputerStep', JSON.stringify({ action: 'backend', ...backend })); }).catch((error) => this.emit('Error', 'CU_GRANT', error.message)); return result; } computerRevoke() { this.input.revoke(); this.computer.revoke(); this.emit('ComputerStep', JSON.stringify({ action: 'revoke' })); } async startVoice() { if (this.voiceLoop) return; const voiceIO = new QvacVoiceAdapter(); this.voiceLoop = new VoiceLoop({ daemon: this, wake: createWakeEngine(), asr: voiceIO, tts: voiceIO }); try { await this.voiceLoop.start(); } catch (error) { this.voiceLoop = null; this.emit('Error', 'VOICE_UNAVAILABLE', error.message); throw error; } } setPushToTalk(pressed) { this.voiceLoop?.setPushToTalk(pressed); this.emit('PushToTalk', Boolean(pressed)); } runtimeStatus() { return JSON.stringify({ local: true, qvac: qvacStatus(), scheduler: this.scheduler.status(), scheduler_metrics: this.scheduler.metrics(), telemetry: this.telemetry.snapshot(), computer: this.computer.status(), voice: this.voiceLoop?.metrics?.snapshot?.() || null, p2p: { enabled: process.env.JARVIS_P2P_ENABLE === '1', inference: false, memorySync: false } }); } async assessModelFit(model) { return JSON.stringify(await callQvac('assessModelFit', { modelSrc: String(model) })); } async downloadModel(model) { return JSON.stringify(await callQvac('downloadAsset', { modelSrc: String(model) })); } async cancelModel(model) { return JSON.stringify(await cancelQvacRequest({ modelId: String(model) })); } async wipeComputerTraces() { await this.audit.wipeTemp(); return true; } 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); clearInterval(this._telemetryTimer); this.computerRevoke(); this.recovery.save({ state: 'ARMED', mode: this.mode }); await this.voiceLoop?.stop?.(); await this.harness.close(); } } if (import.meta.url === `file://${process.argv[1]}`) { const daemon = new JarvisDaemon(); daemon.startVoice().catch((error) => console.error(`jarvisd: voice unavailable: ${error.message}`)); 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 ready; QVAC inference remains GPU-gated'); }