// A scripted stand-in for `speak_to_me serve`: the JSON-RPC methods and events // Noko uses, with the shapes the real engine emits (see the speak_to_me client // guide and current pipeline RPC), and no audio, model or device at all. // // A test bundle's bin/speak_to_me is a wrapper that names its bundle and // revision in NOKO_ENGINE_BUNDLE and NOKO_ENGINE_REVISION. What recognition and wake // detection hear comes from the JSON file NOKO_SCENARIO names, read at each // start: {"recognition": "speak" | "silent" | "die" | "wake-speech", "wake": "hit" | "wait", // "finals": [first, last]}; "speak" finals default to "Hello world." and "And the rest.". // Every request is appended to NOKO_CALLS as one JSON line. A bundle holding // `missing` reports model assets missing until `pipeline.prepare` ran for it. import { appendFileSync, existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { createInterface } from 'node:readline'; const bundle = process.env.NOKO_ENGINE_BUNDLE; const revision = process.env.NOKO_ENGINE_REVISION; const root = process.argv[process.argv.indexOf('--data-root') + 1]; const prepared = join(root, 'models', `${revision}.prepared`); const note = record => appendFileSync(process.env.NOKO_CALLS, JSON.stringify({ revision, ...record }) + '\n'); const scenario = () => { try { return JSON.parse(readFileSync(process.env.NOKO_SCENARIO, 'utf8')); } catch { return {}; } }; const missing = () => existsSync(join(bundle, 'missing')) && !existsSync(prepared) ? [{ id: 'nemotron-en-160ms-int8', bytes: 463945198, output_bytes: 463945198, acquisition: 'download', description: 'Download nemotron-en-160ms-int8' }] : []; let seq = 0; let next = 0; const operations = new Map(); const send = message => process.stdout.write(JSON.stringify({ jsonrpc: '2.0', ...message }) + '\n'); const event = (operation, method, data) => send({ method, params: { instance: 'scripted', seq: ++seq, resource: 'pipeline', epoch: 1, operation, data } }); const later = (ms, action) => setTimeout(action, ms); function end(operation, outcome) { const state = operations.get(operation); if (!state || state.ended) return; state.ended = true; for (const timer of state.timers) clearTimeout(timer); event(operation, 'pipeline.ended', { outcome, error: outcome === 'failed' ? { code: -32000, message: 'scripted failure' } : null, native_settled: true, wall_ms: 1 }); } function start(kind) { const operation = `pipeline_${++next}`; operations.set(operation, { kind, timers: [], pending: new Set(), finishing: false, ended: false }); return operation; } function schedule(operation, ms, action) { operations.get(operation).timers.push(later(ms, action)); } function final(operation, segment, text) { const state = operations.get(operation); state.pending.add(segment); event(operation, 'pipeline.final', { raw_text: text, text, segment, start_frame: 0, end_frame: 32000, input_frames: 32000, partial_count: 1 }); } function settleIfDone(operation) { const state = operations.get(operation); if (state && state.finishing && state.pending.size === 0) end(operation, 'completed'); } function recognition(operation, params) { const plan = scenario().recognition || 'speak'; const finals = scenario().finals || ['Hello world.', 'And the rest.']; operations.get(operation).plan = plan; operations.get(operation).finals = finals; note({ method: 'recognition.start', hotwords: params.hotwords, punctuation: params.punctuation, plan }); schedule(operation, 30, () => event(operation, 'pipeline.input.ready', { mode: 'transcribe', pre_roll: false, retained_input: false, sample_rate: 16000 })); if (plan === 'speak') { schedule(operation, 60, () => event(operation, 'pipeline.partial', { text: 'hello', segment: 'segment-1', input_frames: 4000, input_complete: false, revisable: true })); schedule(operation, 90, () => event(operation, 'pipeline.partial', { text: 'hello wor', segment: 'segment-1', input_frames: 8000, input_complete: false, revisable: true })); schedule(operation, 120, () => final(operation, 'segment-1', finals[0])); schedule(operation, 150, () => event(operation, 'pipeline.partial', { text: 'and the', segment: 'segment-2', input_frames: 36000, input_complete: false, revisable: true })); } else if (plan === 'wake-speech') { schedule(operation, 60, () => event(operation, 'pipeline.vad', { speech: true, input_frames: 4000, meaning: 'activity evidence, not turn commitment', source: 'native_silero_vad', live_input: true })); schedule(operation, 90, () => event(operation, 'pipeline.partial', { text: 'take a note', segment: 'segment-1', input_frames: 8000, input_complete: false, revisable: true })); schedule(operation, 150, () => event(operation, 'pipeline.vad', { speech: false, input_frames: 9000, meaning: 'activity evidence, not turn commitment', source: 'native_silero_vad', live_input: true })); schedule(operation, 170, () => final(operation, 'segment-1', 'Take a note.')); } else if (plan === 'die') { schedule(operation, 100, () => process.exit(1)); } } function finish(operation, params) { const state = operations.get(operation); note({ method: 'recognition.finish', discard: params.discard === true, plan: state.plan }); state.finishing = true; if (state.plan === 'speak' && !params.discard) final(operation, 'segment-2', state.finals[1]); settleIfDone(operation); } const methods = { 'resource.open': () => ({ resource: 'pipeline', epoch: 1, ready: true }), 'pipeline.plan': params => { const gaps = missing(); note({ method: 'pipeline.plan', action: params.action, roles: params.roles, missing: gaps.length }); return { plan: { action: params.action, missing: gaps, download_bytes: gaps.reduce((sum, item) => sum + item.bytes, 0), optional_missing: [], roles: params.roles } }; }, 'pipeline.prepare': params => { const operation = start('prepare'); note({ method: 'pipeline.prepare', roles: params.roles }); schedule(operation, 30, () => event(operation, 'pipeline.asset', { asset: 'nemotron-en-160ms-int8', state: 'downloading', bytes: 463945198, received_bytes: 231972599, total_bytes: 463945198 })); schedule(operation, 60, () => event(operation, 'pipeline.asset', { asset: 'nemotron-en-160ms-int8', state: 'ready', bytes: 463945198, verified: true })); schedule(operation, 90, () => { mkdirSync(dirname(prepared), { recursive: true }); writeFileSync(prepared, ''); end(operation, 'completed'); }); return { operation, resource: 'pipeline', epoch: 1 }; }, 'recognition.start': params => { const operation = start('recognition'); recognition(operation, params); return { operation, resource: 'pipeline', epoch: 1 }; }, 'recognition.finish': params => { finish(params.operation, params); return { operation: params.operation, finishing: true, native_settled: false }; }, 'recognition.ack': params => { note({ method: 'recognition.ack', segment: params.segment }); operations.get(params.operation)?.pending.delete(params.segment); settleIfDone(params.operation); return { acknowledged: true, operation: params.operation, segment: params.segment }; }, 'wake.start': params => { const operation = start('wake'); const plan = scenario().wake || 'wait'; note({ method: 'wake.start', phrases: params.phrases, plan }); schedule(operation, 30, () => event(operation, 'pipeline.input.ready', { mode: 'wake', pre_roll: false })); if (plan === 'hit') { writeFileSync(process.env.NOKO_SCENARIO, JSON.stringify({ ...scenario(), wake: 'wait' })); schedule(operation, 120, () => event(operation, 'pipeline.keyword', { phrase: params.phrases[0], label: 'keyword_0', start_seconds: 0, policy: 'evidence only; no actions executed' })); schedule(operation, 130, () => end(operation, 'completed')); } return { operation, resource: 'pipeline', epoch: 1 }; }, 'operation.cancel': params => { note({ method: 'operation.cancel', kind: operations.get(params.operation)?.kind }); end(params.operation, 'cancelled'); return { operation: params.operation, native_settled: true }; }, 'service.shutdown': () => { note({ method: 'service.shutdown' }); for (const operation of operations.keys()) end(operation, 'cancelled'); return { closed: true }; }, }; note({ method: 'serve' }); const lines = createInterface({ input: process.stdin }); // Serial admission, as the engine opens without duplex: a second operation // while one runs is refused Busy. const starts = new Set(['recognition.start', 'wake.start', 'pipeline.prepare']); lines.on('line', line => { const request = JSON.parse(line); if (starts.has(request.method) && [...operations.values()].some(state => !state.ended)) { note({ method: 'busy', refused: request.method }); send({ id: request.id, error: { code: -32000, message: 'Busy' } }); return; } const handler = methods[request.method]; if (!handler) { send({ id: request.id, error: { code: -32601, message: `unknown method ${request.method}` } }); return; } send({ id: request.id, result: handler(request.params || {}) }); }); lines.on('close', () => { note({ method: 'exit' }); process.exit(0); });