Move sendTranscriptEvent to redis object

This commit is contained in:
Apher 2026-07-06 23:46:12 +00:00
parent 14f5a85bd8
commit 990589a776
2 changed files with 19 additions and 45 deletions

View File

@ -10,8 +10,6 @@ redis.client = Redis.createClient({
}); });
redis.ensureGroup = async (stream, group) => { redis.ensureGroup = async (stream, group) => {
await redis.client.connect().catch(console.error);
try { try {
await redis.client.xGroupCreate(stream, group, "0", {MKSTREAM: true}); await redis.client.xGroupCreate(stream, group, "0", {MKSTREAM: true});
@ -23,7 +21,19 @@ redis.ensureGroup = async (stream, group) => {
console.error(err); console.error(err);
} }
} }
await redis.client.quit(); }
redis.sendTranscriptEvent = async (streamKey, device_id, text) => {
try {
await redis.client.xAdd(streamKey, "*", {
text,
device_id: device_id,
});
console.log("Transcript added to stream");
} catch (err) {
console.error(err);
}
} }
module.exports = { module.exports = {

View File

@ -14,22 +14,7 @@ const transConfig = require("./config/transcribe.config")
const py = spawn('python', ['./src/scripts/py/transcribe.py', JSON.stringify(transConfig)], { const py = spawn('python', ['./src/scripts/py/transcribe.py', JSON.stringify(transConfig)], {
stdio: ['ignore', 'pipe', 'pipe'] stdio: ['ignore', 'pipe', 'pipe']
}); });
const { handlePythonOutput } = require("./scripts/py/pythonOutput");
let buffer = '';
// Send Speech to Text to Redis Stream
const sendTranscriptEvent = async (streamKey, device_id, text) => {
await redis.client.xAdd(
streamKey,
'*',
{
text: text,
device_id: device_id
}
)
console.log("Transcript added to stream");
}
// Read Redis Stream group // Read Redis Stream group
async function processor (group, consumer, streamKey) { async function processor (group, consumer, streamKey) {
@ -57,33 +42,12 @@ async function main() {
await redis.client.connect().catch(console.error); await redis.client.connect().catch(console.error);
await redis.ensureGroup("commands", "assistant"); await redis.ensureGroup("commands", "assistant");
py.stdout.on('data', (data) => { py.stdout.on("data", (data) => {
buffer += data.toString(); handlePythonOutput(data.toString(), async (msg) => {
const lines = buffer.split('\n'); if (msg.event === "text" || msg.event === "final") {
buffer = lines.pop(); await redis.sendTranscriptEvent("transcripts", config.DEVICE_ID, msg.text);
for (const line of lines) {
if (!line.trim()) continue;
try {
const msg = JSON.parse(line);
if (msg.event === 'ready') {
console.log('Python ready');
} else if (msg.event === 'wakeword') {
console.log(`Wakeword detected: ${msg.model} (${msg.score})`);
} else if (msg.event === 'partial') {
console.log('Partial:', msg.text);
} else if (msg.event === 'text' || msg.event === 'final') {
console.log('Transcript:', msg.text);
sendTranscriptEvent("transcripts", config.DEVICE_ID, msg.text);
} else if (msg.event === 'done') {
console.log('Back to wakeword mode');
} }
} catch { });
console.log('[PY]', line);
}
}
}); });
py.stderr.on("data", (data) => { py.stderr.on("data", (data) => {