import { agentContext, claimTaskThread, releaseTaskThread, } from "../utils/agentContext.ts"; import { msFrom, msRandomOffset, msUntilNextWakeWindow, } from "../utils/time.ts"; export const logStats = () => { if (!claimTaskThread()) { const newDelay = msFrom.minutes(2); console.log( `Stat log attempt failed, ${agentContext.agentBskyName} is busy. Next attempt in ${ (newDelay / 1000) / 60 } minutes…`, ); // session is busy, logging stats in 5~10 minutes. setTimeout(logStats, newDelay); return; } const delay = msUntilNextWakeWindow( msFrom.minutes(30), msFrom.hours(1), ); if (delay !== 0) { setTimeout(logStats, delay); console.log( `🔹 ${agentContext.agentBskyName} is currently asleep. scheduling next stat log for ${ (delay / 1000 / 60 / 60).toFixed(2) } hours from now…`, ); releaseTaskThread(); return; } // Check if there are any notifications const totalNotifications = agentContext.mentionCount + agentContext.likeCount + agentContext.repostCount + agentContext.quoteCount + agentContext.replyCount + agentContext.followCount; const nextCheckDelay = msFrom.minutes(5); const nextCheckMinutes = ((nextCheckDelay / 1000) / 60).toFixed(1); if (totalNotifications <= 0) { console.log( `no engagement stats yet... next check in ${nextCheckMinutes} minutes…`, ); } else { const counts = []; if (agentContext.mentionCount > 0) { counts.push( `${agentContext.mentionCount} ${ agentContext.mentionCount === 1 ? "mention" : "mentions" }`, ); } if (agentContext.likeCount > 0) { counts.push( `${agentContext.likeCount} ${ agentContext.likeCount === 1 ? "like" : "likes" }`, ); } if (agentContext.repostCount > 0) { counts.push( `${agentContext.repostCount} ${ agentContext.repostCount === 1 ? "repost" : "reposts" }`, ); } if (agentContext.quoteCount > 0) { counts.push( `${agentContext.quoteCount} ${ agentContext.quoteCount === 1 ? "quote" : "quotes" }`, ); } if (agentContext.replyCount > 0) { counts.push( `${agentContext.replyCount} ${ agentContext.replyCount === 1 ? "reply" : "replies" }`, ); } if (agentContext.followCount > 0) { counts.push( `${agentContext.followCount} new ${ agentContext.followCount === 1 ? "follower" : "followers" }`, ); } const message = counts.join(", "); const suffix = agentContext.reflectionEnabled ? " since last reflection" : ""; console.log( ` stats: ${message}${suffix}. next check in ${nextCheckMinutes} minutes…`, ); } setTimeout(logStats, nextCheckDelay); releaseTaskThread(); };