Update brain.mjs

This commit is contained in:
Arma-Damna-Dillo 2025-02-08 03:58:07 +00:00
parent 8124f90d56
commit 98f3ee3bd5

139
brain.mjs
View file

@ -18,122 +18,57 @@ class ConsciousnessApp {
// Function to simulate user talking
async userTalks(input) {
const reply = await this.simulator.generateThoughtAndChat(input);
console.log("User: " + input);
console.log("App: " + reply);
console.log("<User>: " + input);
console.log("<Bot>: " + 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
async userTalks(input) {
const reply = await this.simulator.generateThoughtAndChat(input);
console.log("<User>: " + input);
console.log("<Bot>: " + reply);
}
async thinker() {
while (true) {
// Generate random interval between 7 and 15 minutes (in milliseconds)
const intervalTime = Math.random() * (15 * 60 * 1000 - 7 * 60 * 1000) + 7 * 60 * 1000;
// Wait for the random interval (non-blocking)
await new Promise(resolve => setTimeout(resolve, intervalTime));
await this.simulator.simulateConsciousness();
}
}
// Function to handle activity check
handleActivity() {
const currentTime = Date.now();
const elapsedTime = currentTime - this.lastActiveTime;
// Check if it's been more than 15 minutes of inactivity
if (elapsedTime > (15 * 60 * 1000) && this.isActive) {
this.isActive = false;
console.log("No activity for 15 minutes. Entering dream state...");
this.simulator.setUserActive(false); // Simulate dreaming
clearTimeout(this.dreamTimeout); // Cancel any existing timeout
this.startDreaming(); // Start dreaming state
}
// If user becomes active again
if (this.isActive === false && elapsedTime < (15 * 60 * 1000)) {
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(); // Resume activity checks
async asker() {
while (true) {
await rl.question('<Query>: ', (query) => {
userTalks(query)
});
}
}
// Update intentions with a 50/50 chance after 7-15 minutes of activity
async checkIntentions() {
const currentTime = Date.now();
const elapsedTime = currentTime - this.lastActiveTime;
async intentUpdater() {
while (true) {
// Generate random interval between 7 and 15 minutes (in milliseconds)
const intervalTime = Math.random() * (15 * 60 * 1000 - 7 * 60 * 1000) + 7 * 60 * 1000;
// 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...");
// Wait for the random interval (non-blocking)
await new Promise(resolve => setTimeout(resolve, intervalTime));
// 50% chance to execute the function
if (Math.random() < 0.5) {
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() {
while (this.isActive) {
const input = await new Promise((resolve) => {
this.rl.question("You can start talking to the app. Type your message: ", resolve);
});
this.handleActivity(); // Update activity when the user types
if (input.trim() !== '') { // Only respond if the input is not empty
await this.userTalks(input); // App replies to user input
}
}
}
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);
let minibrain = new ConsciousnessApp()
await this.simulator.startDreaming();
intentUpdater()
thinker()
asker()
}
main().catch(console.error);
main().catch(e => console.erro(e))