a tool to help your Letta AI agents navigate bluesky
1import {
2 agentContext,
3 claimTaskThread,
4 releaseTaskThread,
5} from "../utils/agentContext.ts";
6import {
7 formatUptime,
8 msFrom,
9 msRandomOffset,
10 msUntilNextWakeWindow,
11} from "../utils/time.ts";
12
13// Capture server start time when module is loaded
14const serverStartTime = Date.now();
15
16export const logTasks = () => {
17 if (!claimTaskThread()) {
18 const newDelay = msFrom.minutes(2);
19 console.log(
20 `🔹 Task log attempt failed, ${agentContext.agentBskyName} is busy. Next attempt in ${
21 (newDelay / 1000) / 60
22 } minutes…`,
23 );
24 setTimeout(logTasks, newDelay);
25 return;
26 }
27
28 const delay = msUntilNextWakeWindow(
29 msFrom.minutes(30),
30 msFrom.hours(1),
31 );
32
33 if (delay !== 0) {
34 setTimeout(logTasks, delay);
35 console.log(
36 `🔹 ${agentContext.agentBskyName} is currently asleep. scheduling next task log for ${
37 (delay / 1000 / 60 / 60).toFixed(2)
38 } hours from now…`,
39 );
40 releaseTaskThread();
41 return;
42 }
43
44 // Check if there's any activity
45 const totalActivity = agentContext.reflectionCount +
46 agentContext.checkCount +
47 agentContext.processingCount +
48 agentContext.proactiveCount;
49
50 const uptime = Date.now() - serverStartTime;
51 const uptimeFormatted = formatUptime(uptime);
52
53 const nextCheckDelay = msFrom.minutes(30);
54 const nextCheckMinutes = ((nextCheckDelay / 1000) / 60).toFixed(1);
55
56 if (totalActivity <= 0) {
57 console.log(
58 `🔹 no activity yet... uptime: ${uptimeFormatted}. next log in ${nextCheckMinutes} minutes`,
59 );
60 } else {
61 const actions = [];
62
63 if (agentContext.reflectionCount > 0) {
64 const times = agentContext.reflectionCount === 1
65 ? "once"
66 : `${agentContext.reflectionCount} times`;
67 actions.push(`reflected ${times}`);
68 }
69 if (agentContext.checkCount > 0) {
70 const times = agentContext.checkCount === 1
71 ? "once"
72 : `${agentContext.checkCount} times`;
73 actions.push(`checked notifications ${times}`);
74 }
75 if (agentContext.processingCount > 0) {
76 const times = agentContext.processingCount === 1
77 ? "once"
78 : `${agentContext.processingCount} times`;
79 actions.push(`found and processed notifications ${times}`);
80 }
81 if (agentContext.proactiveCount > 0) {
82 const times = agentContext.proactiveCount === 1
83 ? "once"
84 : `${agentContext.proactiveCount} times`;
85 actions.push(`proactively used bluesky ${times}`);
86 }
87
88 const message = actions.join(", ");
89
90 console.log(
91 `🔹 ${message}. total notifications: ${agentContext.notifCount}. uptime: ${uptimeFormatted}. next log in ${nextCheckMinutes} minutes`,
92 );
93 }
94
95 setTimeout(logTasks, nextCheckDelay);
96 releaseTaskThread();
97};