Merge pull request #441 from uukelele-scratch/main

Added speech functionality for bots
This commit is contained in:
Max Robinson 2025-03-13 14:30:34 -05:00 committed by GitHub
commit a077ed4151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

View file

@ -30,7 +30,7 @@ export default
"load_memory": false, // load memory from previous session
"init_message": "Respond with hello world and your name", // sends to all on spawn
"only_chat_with": [], // users that the bots listen to and send general messages to. if empty it will chat publicly
"speak": false, // allows all bots to speak through system text-to-speech. tested on windows, should work on mac, on linux you may need to `apt install espeak`
"language": "en", // translate to/from this language. Supports these language names: https://cloud.google.com/translate/docs/languages
"show_bot_views": false, // show bot's view in browser at localhost:3000, 3001...

View file

@ -14,6 +14,7 @@ import { addViewer } from './viewer.js';
import settings from '../../settings.js';
import { serverProxy } from './agent_proxy.js';
import { Task } from './tasks.js';
import { say } from './speak.js';
export class Agent {
async start(profile_fp, load_mem=false, init_message=null, count_id=0, task_path=null, task_id=null) {
@ -351,6 +352,9 @@ export class Agent {
}
}
else {
if (settings.speak) {
say(message);
}
this.bot.chat(message);
}
}

42
src/agent/speak.js Normal file
View file

@ -0,0 +1,42 @@
import { exec } from 'child_process';
let speakingQueue = [];
let isSpeaking = false;
export function say(textToSpeak) {
speakingQueue.push(textToSpeak);
if (!isSpeaking) {
processQueue();
}
}
function processQueue() {
if (speakingQueue.length === 0) {
isSpeaking = false;
return;
}
isSpeaking = true;
const textToSpeak = speakingQueue.shift();
const isWin = process.platform === "win32";
const isMac = process.platform === "darwin";
let command;
if (isWin) {
command = `powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(\\"${textToSpeak}\\")"`;
} else if (isMac) {
command = `say "${textToSpeak}"`;
} else {
command = `espeak "${textToSpeak}"`;
}
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
console.error(`${error.stack}`);
} else if (stderr) {
console.error(`Error: ${stderr}`);
processQueue(); // Continue with the next message in the queue
});
}