P4
This commit is contained in:
+16
-3
@@ -5,6 +5,9 @@ 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';
|
||||
import { VoiceLoop } from './voice-loop.js';
|
||||
import { QvacVoiceAdapter } from './voice-adapters.js';
|
||||
import { createWakeEngine } from './wake-engine.js';
|
||||
|
||||
export class JarvisDaemon extends EventEmitter {
|
||||
constructor() {
|
||||
@@ -16,6 +19,8 @@ export class JarvisDaemon extends EventEmitter {
|
||||
this.harness = new HarnessBridge({ cwd: process.cwd(), computer: this.computer });
|
||||
this.log = new PrivacyLog();
|
||||
this.locked = false;
|
||||
this.lastReply = '';
|
||||
this.voiceLoop = null;
|
||||
this._idleTimer = setInterval(() => this.tickIdle(), 30_000);
|
||||
this._idleTimer.unref?.();
|
||||
this.harness.on('agent_message_chunk', (ev) => this.emit('Token', ev?.text || ev?.delta || ''));
|
||||
@@ -26,12 +31,12 @@ export class JarvisDaemon extends EventEmitter {
|
||||
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)); }
|
||||
say(text) { this.lastReply = String(text); this.emit('Reply', this.lastReply); }
|
||||
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;
|
||||
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.emit('Error', 'QVAC', error.message); this.voice.cancel(); this.setState('ARMED'); throw error;
|
||||
}
|
||||
@@ -39,13 +44,21 @@ export class JarvisDaemon extends EventEmitter {
|
||||
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' })); }
|
||||
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)); }
|
||||
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(); }
|
||||
async close() { clearInterval(this._idleTimer); this.computerRevoke(); 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}`);
|
||||
|
||||
Reference in New Issue
Block a user