diff --git a/brain.mjs b/brain.mjs new file mode 100644 index 0000000..4f770bc --- /dev/null +++ b/brain.mjs @@ -0,0 +1,121 @@ +import { ConsciousnessSimulator } from './brainapi.mjs'; +import readline from 'readline'; + +class ConsciousnessApp { + constructor() { + this.simulator = new ConsciousnessSimulator(); + this.isActive = true; // User starts active + this.lastActiveTime = Date.now(); + this.dreamTimeout = null; + this.activityTimeout = null; + this.rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }); + } + + // Function to simulate user talking + async userTalks(input) { + const reply = await this.simulator.generateThoughtAndChat(input); + console.log("User: " + input); + console.log("App: " + reply); + } + + // Simulate the dreaming behavior + async startDreaming() { + if (!this.isActive) { + console.log("App is dreaming... (Inactive for 15 minutes)"); + await this.simulator.simulateConsciousness(); // Simulate self-consciousness while dreaming + await this.simulator.updateIntentions(); // Chance of updating intentions during dreaming + } + } + + // Function to handle activity check + handleActivity() { + this.lastActiveTime = Date.now(); + if (!this.isActive) { + this.isActive = true; + console.log("User is active again. Waking up from dream..."); + this.simulator.setUserActive(true); // Wake up from dreaming + clearTimeout(this.dreamTimeout); // Cancel dream timeout + this.initiateActivityRoutine(); // Start activity routines again + } + } + + // Update intentions with a 50/50 chance after 7-15 minutes of activity + async checkIntentions() { + const currentTime = Date.now(); + const elapsedTime = currentTime - this.lastActiveTime; + + // 50/50 chance to update intentions every 7-15 minutes of activity + if (elapsedTime > (7 * 60 * 1000) && elapsedTime < (15 * 60 * 1000)) { + if (Math.random() > 0.5) { + console.log("Updating intentions..."); + await this.simulator.updateIntentions(); + } + } + } + + // Routine to check activity every minute + initiateActivityRoutine() { + this.activityTimeout = setInterval(async () => { + const idleTime = Date.now() - this.lastActiveTime; + + if (idleTime > (15 * 60 * 1000)) { + this.isActive = false; + console.log("No activity for 15 minutes. Entering dream state..."); + this.startDreaming(); + clearInterval(this.activityTimeout); // Stop activity checks while dreaming + this.startDreamingTimeout(); + } else { + await this.checkIntentions(); + } + }, 60000); // Check every minute + } + + // Handle dream timeout (15 minutes of inactivity) + startDreamingTimeout() { + this.dreamTimeout = setTimeout(() => { + this.isActive = true; + console.log("Waking up after dreaming..."); + this.simulator.setUserActive(true); + this.initiateActivityRoutine(); // Restart activity checks + }, 15 * 60 * 1000); // Dream timeout after 15 minutes of inactivity + } + + // Start interactive conversation with the user + async startConversation() { + this.rl.question("You can start talking to the app. Type your message: ", async (input) => { + this.handleActivity(); // Update activity when the user types + + await this.userTalks(input); // App replies to user input + this.startConversation(); // Continue the conversation loop + }); + } + + async start() { + await this.simulator.simulateConsciousness(); // Simulate initial consciousness + this.initiateActivityRoutine(); // Start activity and dream checks + this.startConversation(); // Start the interactive conversation + } +} + +// Main app function +async function main() { + const app = new ConsciousnessApp(); + await app.start(); + + // Optional: Simulate the user becoming inactive after 10 seconds and then becoming active again + setTimeout(() => { + app.isActive = false; + app.simulator.setUserActive(false); // User inactive (dreaming) + console.log("User is inactive... dreaming..."); + }, 10000); + + // Simulate user becoming active again after 30 seconds + setTimeout(() => { + app.handleActivity(); + }, 30000); +} + +main().catch(console.error);