Switched to original server implementation & included server shim
This commit is contained in:
120
V3.1/build/hub-shim/test-client.js
Normal file
120
V3.1/build/hub-shim/test-client.js
Normal file
@@ -0,0 +1,120 @@
|
||||
/* eslint-disable no-console */
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
function uuid() {
|
||||
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
|
||||
return [4, 2, 2, 2, 6].map((len) => crypto.randomBytes(len).toString('hex')).join('-');
|
||||
}
|
||||
|
||||
function requireWs() {
|
||||
try {
|
||||
return require('ws');
|
||||
} catch (_) {
|
||||
return require('/opt/jibo/Jibo/Skills/@be/be/node_modules/ws');
|
||||
}
|
||||
}
|
||||
|
||||
const WebSocket = requireWs();
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = { url: '', mode: 'client_asr', text: 'yes I liked it', rules: ['globals/yes_no'] };
|
||||
for (let i = 2; i < argv.length; i += 1) {
|
||||
const a = argv[i];
|
||||
if (!a) continue;
|
||||
if (a.startsWith('ws://') || a.startsWith('wss://')) {
|
||||
args.url = a;
|
||||
continue;
|
||||
}
|
||||
if (a === '--stt') {
|
||||
args.mode = 'stt';
|
||||
continue;
|
||||
}
|
||||
if (a === '--client_asr') {
|
||||
args.mode = 'client_asr';
|
||||
continue;
|
||||
}
|
||||
if (a.indexOf('--mode=') === 0) {
|
||||
args.mode = String(a.split('=')[1] || '').trim() || args.mode;
|
||||
continue;
|
||||
}
|
||||
if (a.indexOf('--text=') === 0) {
|
||||
args.text = String(a.split('=')[1] || '');
|
||||
continue;
|
||||
}
|
||||
if (a.indexOf('--rules=') === 0) {
|
||||
const r = String(a.split('=')[1] || '');
|
||||
args.rules = r ? r.split(',').map((s) => s.trim()).filter(Boolean) : [];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
const args = parseArgs(process.argv);
|
||||
const url = args.url || 'ws://127.0.0.1:9000/v1/listen';
|
||||
|
||||
const ws = new WebSocket(url, {
|
||||
headers: {
|
||||
'x-jibo-transid': 'test-transid',
|
||||
},
|
||||
});
|
||||
|
||||
ws.on('open', () => {
|
||||
console.log('connected', url);
|
||||
ws.send(JSON.stringify({
|
||||
type: 'CONTEXT',
|
||||
msgID: uuid(),
|
||||
ts: Date.now(),
|
||||
data: { runtime: {}, skill: {}, general: {} },
|
||||
}));
|
||||
if (String(args.mode).toLowerCase() === 'stt') {
|
||||
console.log('mode=stt: waiting for robot ASR via shim');
|
||||
ws.send(JSON.stringify({
|
||||
type: 'LISTEN',
|
||||
msgID: uuid(),
|
||||
ts: Date.now(),
|
||||
data: { hotphrase: false, rules: args.rules },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('mode=client_asr:', JSON.stringify({ text: args.text, rules: args.rules }));
|
||||
ws.send(JSON.stringify({
|
||||
type: 'LISTEN',
|
||||
msgID: uuid(),
|
||||
ts: Date.now(),
|
||||
data: { hotphrase: false, rules: args.rules, mode: 'CLIENT_ASR' },
|
||||
}));
|
||||
ws.send(JSON.stringify({
|
||||
type: 'CLIENT_ASR',
|
||||
msgID: uuid(),
|
||||
ts: Date.now(),
|
||||
data: { text: args.text },
|
||||
}));
|
||||
});
|
||||
|
||||
ws.on('message', (data) => {
|
||||
let msg;
|
||||
try {
|
||||
msg = JSON.parse(data.toString('utf8'));
|
||||
} catch (e) {
|
||||
console.log('binary/unknown', data);
|
||||
return;
|
||||
}
|
||||
console.log('rx', msg.type, msg.final ? '(final)' : '', msg.data ? '' : '');
|
||||
if (msg.type === 'LISTEN') {
|
||||
console.log(JSON.stringify(msg, null, 2));
|
||||
ws.close();
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', () => {
|
||||
console.log('closed');
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
ws.on('error', (e) => {
|
||||
console.error('error', e && (e.stack || e.message || e));
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user