diff --git a/brain.mjs b/brain.mjs index fdc9efa..f4930a3 100644 --- a/brain.mjs +++ b/brain.mjs @@ -13,6 +13,7 @@ class ConsciousnessApp { input: process.stdin, output: process.stdout, }); + this.asking = false; // Track if a question is being asked } // Function to simulate user talking @@ -22,12 +23,6 @@ class ConsciousnessApp { console.log(": " + reply); } - async userTalks(input) { - const reply = await this.simulator.generateThoughtAndChat(input); - console.log(": " + input); - console.log(": " + reply); - } - async thinker() { while (true) { // Generate random interval between 7 and 15 minutes (in milliseconds) @@ -37,22 +32,29 @@ class ConsciousnessApp { await new Promise(resolve => setTimeout(resolve, intervalTime)); await this.simulator.simulateConsciousness(); - } + } } - asking = false + + // Non-blocking question asking function async asker() { while (true) { - if (asking == false) { - asking = true - await this.rl.question(': ', (query) => { - userTalks(query) - asking = false - }); + if (!this.asking) { + this.asking = true; + await this.askQuestion(); + this.asking = false; } - } } + // Helper function for asking questions + async askQuestion() { + return new Promise((resolve) => { + this.rl.question(': ', (query) => { + this.userTalks(query).then(resolve); // Call userTalks and resolve when done + }); + }); + } + async intentUpdater() { while (true) { // Generate random interval between 7 and 15 minutes (in milliseconds) @@ -65,15 +67,20 @@ class ConsciousnessApp { if (Math.random() < 0.5) { await this.simulator.updateIntentions(); } - } } -} -async function main() { - let minibrain = new ConsciousnessApp() - await minibrain.simulator.startDreaming(); - minibrain.intentUpdater() - minibrain.thinker() - minibrain.asker() + } } -main().catch(e => console.error(e)) \ No newline at end of file +async function main() { + const minibrain = new ConsciousnessApp(); + await minibrain.simulator.startDreaming(); + + // Run all asynchronous functions concurrently + await Promise.all([ + minibrain.intentUpdater(), + minibrain.thinker(), + minibrain.asker() + ]); +} + +main().catch(e => console.error(e));