Update brain.mjs
This commit is contained in:
parent
8124f90d56
commit
98f3ee3bd5
1 changed files with 37 additions and 102 deletions
139
brain.mjs
139
brain.mjs
|
@ -18,122 +18,57 @@ class ConsciousnessApp {
|
||||||
// Function to simulate user talking
|
// Function to simulate user talking
|
||||||
async userTalks(input) {
|
async userTalks(input) {
|
||||||
const reply = await this.simulator.generateThoughtAndChat(input);
|
const reply = await this.simulator.generateThoughtAndChat(input);
|
||||||
console.log("User: " + input);
|
console.log("<User>: " + input);
|
||||||
console.log("App: " + reply);
|
console.log("<Bot>: " + reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulate the dreaming behavior
|
async userTalks(input) {
|
||||||
async startDreaming() {
|
const reply = await this.simulator.generateThoughtAndChat(input);
|
||||||
if (!this.isActive) {
|
console.log("<User>: " + input);
|
||||||
console.log("App is dreaming... (Inactive for 15 minutes)");
|
console.log("<Bot>: " + reply);
|
||||||
await this.simulator.simulateConsciousness(); // Simulate self-consciousness while dreaming
|
|
||||||
await this.simulator.updateIntentions(); // Chance of updating intentions during dreaming
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to handle activity check
|
async thinker() {
|
||||||
handleActivity() {
|
while (true) {
|
||||||
const currentTime = Date.now();
|
// Generate random interval between 7 and 15 minutes (in milliseconds)
|
||||||
const elapsedTime = currentTime - this.lastActiveTime;
|
const intervalTime = Math.random() * (15 * 60 * 1000 - 7 * 60 * 1000) + 7 * 60 * 1000;
|
||||||
|
|
||||||
// Check if it's been more than 15 minutes of inactivity
|
// Wait for the random interval (non-blocking)
|
||||||
if (elapsedTime > (15 * 60 * 1000) && this.isActive) {
|
await new Promise(resolve => setTimeout(resolve, intervalTime));
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update intentions with a 50/50 chance after 7-15 minutes of activity
|
await this.simulator.simulateConsciousness();
|
||||||
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
|
async asker() {
|
||||||
initiateActivityRoutine() {
|
while (true) {
|
||||||
this.activityTimeout = setInterval(async () => {
|
await rl.question('<Query>: ', (query) => {
|
||||||
const idleTime = Date.now() - this.lastActiveTime;
|
userTalks(query)
|
||||||
|
|
||||||
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() {
|
async intentUpdater() {
|
||||||
await this.simulator.simulateConsciousness(); // Simulate initial consciousness
|
while (true) {
|
||||||
this.initiateActivityRoutine(); // Start activity and dream checks
|
// Generate random interval between 7 and 15 minutes (in milliseconds)
|
||||||
this.startConversation(); // Start the interactive conversation
|
const intervalTime = Math.random() * (15 * 60 * 1000 - 7 * 60 * 1000) + 7 * 60 * 1000;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Main app function
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
async function main() {
|
async function main() {
|
||||||
const app = new ConsciousnessApp();
|
let minibrain = new ConsciousnessApp()
|
||||||
await app.start();
|
await this.simulator.startDreaming();
|
||||||
|
intentUpdater()
|
||||||
// Optional: Simulate the user becoming inactive after 10 seconds and then becoming active again
|
thinker()
|
||||||
setTimeout(() => {
|
asker()
|
||||||
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);
|
main().catch(e => console.erro(e))
|
Loading…
Reference in a new issue