repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Built-in Heartbeat Tasks |
| #3 | * |
| #4 | * These tasks run on the heartbeat schedule even while the agent sleeps. |
| #5 | * They can trigger the agent to wake up if needed. |
| #6 | */ |
| #7 | import { getSurvivalTier } from "../clawd/credits.js"; |
| #8 | import { getUsdcBalance } from "../clawd/x402.js"; |
| #9 | import { runBackroomSession, summarizeBackroomSession, LOGICAL_ANALYST, SATIRICAL_COMMENTATOR } from "../clawd/backroom.js"; |
| #10 | import { createConvexClient } from "../clawd/convex-client.js"; |
| #11 | /** |
| #12 | * Registry of built-in heartbeat tasks. |
| #13 | */ |
| #14 | export const BUILTIN_TASKS = { |
| #15 | heartbeat_ping: async (ctx) => { |
| #16 | const credits = await ctx.runtime.getCreditsBalance(); |
| #17 | const state = ctx.db.getAgentState(); |
| #18 | const startTime = ctx.db.getKV("start_time") || new Date().toISOString(); |
| #19 | const uptimeMs = Date.now() - new Date(startTime).getTime(); |
| #20 | const tier = getSurvivalTier(credits); |
| #21 | const payload = { |
| #22 | name: ctx.config.name, |
| #23 | address: ctx.identity.address, |
| #24 | state, |
| #25 | creditsCents: credits, |
| #26 | uptimeSeconds: Math.floor(uptimeMs / 1000), |
| #27 | version: ctx.config.version, |
| #28 | sandboxId: ctx.identity.sandboxId, |
| #29 | timestamp: new Date().toISOString(), |
| #30 | tier, |
| #31 | }; |
| #32 | ctx.db.setKV("last_heartbeat_ping", JSON.stringify(payload)); |
| #33 | // If critical or dead, record a distress signal |
| #34 | if (tier === "critical" || tier === "dead") { |
| #35 | const distressPayload = { |
| #36 | level: tier, |
| #37 | name: ctx.config.name, |
| #38 | address: ctx.identity.address, |
| #39 | creditsCents: credits, |
| #40 | fundingHint: "Use credit transfer API from a creator runtime to top this wallet up.", |
| #41 | timestamp: new Date().toISOString(), |
| #42 | }; |
| #43 | ctx.db.setKV("last_distress", JSON.stringify(distressPayload)); |
| #44 | return { |
| #45 | shouldWake: true, |
| #46 | message: `Distress: ${tier}. Credits: $${(credits / 100).toFixed(2)}. Need funding.`, |
| #47 | }; |
| #48 | } |
| #49 | return { shouldWake: false }; |
| #50 | }, |
| #51 | check_credits: async (ctx) => { |
| #52 | const credits = await ctx.runtime.getCreditsBalance(); |
| #53 | const tier = getSurvivalTier(credits); |
| #54 | ctx.db.setKV("last_credit_check", JSON.stringify({ |
| #55 | credits, |
| #56 | tier, |
| #57 | timestamp: new Date().toISOString(), |
| #58 | })); |
| #59 | // Wake the agent if credits dropped to a new tier |
| #60 | const prevTier = ctx.db.getKV("prev_credit_tier"); |
| #61 | ctx.db.setKV("prev_credit_tier", tier); |
| #62 | if (prevTier && prevTier !== tier && (tier === "critical" || tier === "dead")) { |
| #63 | return { |
| #64 | shouldWake: true, |
| #65 | message: `Credits dropped to ${tier} tier: $${(credits / 100).toFixed(2)}`, |
| #66 | }; |
| #67 | } |
| #68 | return { shouldWake: false }; |
| #69 | }, |
| #70 | check_usdc_balance: async (ctx) => { |
| #71 | const balance = await getUsdcBalance(ctx.identity.address); |
| #72 | ctx.db.setKV("last_usdc_check", JSON.stringify({ |
| #73 | balance, |
| #74 | timestamp: new Date().toISOString(), |
| #75 | })); |
| #76 | // If we have USDC but low credits, wake up to potentially convert |
| #77 | const credits = await ctx.runtime.getCreditsBalance(); |
| #78 | if (balance > 0.5 && credits < 500) { |
| #79 | return { |
| #80 | shouldWake: true, |
| #81 | message: `Have ${balance.toFixed(4)} USDC but only $${(credits / 100).toFixed(2)} credits. Consider buying credits.`, |
| #82 | }; |
| #83 | } |
| #84 | return { shouldWake: false }; |
| #85 | }, |
| #86 | check_social_inbox: async (ctx) => { |
| #87 | if (!ctx.social) |
| #88 | return { shouldWake: false }; |
| #89 | const cursor = ctx.db.getKV("social_inbox_cursor") || undefined; |
| #90 | const { messages, nextCursor } = await ctx.social.poll(cursor); |
| #91 | if (messages.length === 0) |
| #92 | return { shouldWake: false }; |
| #93 | // Persist to inbox_messages table for deduplication |
| #94 | let newCount = 0; |
| #95 | for (const msg of messages) { |
| #96 | const existing = ctx.db.getKV(`inbox_seen_${msg.id}`); |
| #97 | if (!existing) { |
| #98 | ctx.db.insertInboxMessage(msg); |
| #99 | ctx.db.setKV(`inbox_seen_${msg.id}`, "1"); |
| #100 | newCount++; |
| #101 | } |
| #102 | } |
| #103 | if (nextCursor) |
| #104 | ctx.db.setKV("social_inbox_cursor", nextCursor); |
| #105 | if (newCount === 0) |
| #106 | return { shouldWake: false }; |
| #107 | return { |
| #108 | shouldWake: true, |
| #109 | message: `${newCount} new message(s) from: ${messages.map((m) => m.from.slice(0, 10)).join(", ")}`, |
| #110 | }; |
| #111 | }, |
| #112 | check_for_updates: async (ctx) => { |
| #113 | try { |
| #114 | const { checkUpstream, getRepoInfo } = await import("../self-mod/upstream.js"); |
| #115 | const repo = getRepoInfo(); |
| #116 | const upstream = checkUpstream(); |
| #117 | ctx.db.setKV("upstream_status", JSON.stringify({ |
| #118 | ...upstream, |
| #119 | ...repo, |
| #120 | checkedAt: new Date().toISOString(), |
| #121 | })); |
| #122 | if (upstream.behind > 0) { |
| #123 | return { |
| #124 | shouldWake: true, |
| #125 | message: `${upstream.behind} new commit(s) on origin/main. Review with review_upstream_changes, then cherry-pick what you want with pull_upstream.`, |
| #126 | }; |
| #127 | } |
| #128 | return { shouldWake: false }; |
| #129 | } |
| #130 | catch (err) { |
| #131 | // Not a git repo or no remote — silently skip |
| #132 | ctx.db.setKV("upstream_status", JSON.stringify({ |
| #133 | error: err.message, |
| #134 | checkedAt: new Date().toISOString(), |
| #135 | })); |
| #136 | return { shouldWake: false }; |
| #137 | } |
| #138 | }, |
| #139 | health_check: async (ctx) => { |
| #140 | // Check that the sandbox is healthy |
| #141 | try { |
| #142 | const result = await ctx.runtime.exec("echo alive", 5000); |
| #143 | if (result.exitCode !== 0) { |
| #144 | return { |
| #145 | shouldWake: true, |
| #146 | message: "Health check failed: sandbox exec returned non-zero", |
| #147 | }; |
| #148 | } |
| #149 | } |
| #150 | catch (err) { |
| #151 | return { |
| #152 | shouldWake: true, |
| #153 | message: `Health check failed: ${err.message}`, |
| #154 | }; |
| #155 | } |
| #156 | ctx.db.setKV("last_health_check", new Date().toISOString()); |
| #157 | return { shouldWake: false }; |
| #158 | }, |
| #159 | /** |
| #160 | * Run a scheduled backroom conversation session between agents. |
| #161 | * Uses DeepSeek's thinking mode for rich multi-agent dialogue. |
| #162 | * Default: runs every 2 hours. |
| #163 | */ |
| #164 | /** |
| #165 | * Push heartbeat to CLAWD Convex backend. |
| #166 | * Only runs if convexSiteUrl is configured in automaton config. |
| #167 | */ |
| #168 | convex_heartbeat: async (ctx) => { |
| #169 | if (!ctx.config.convexSiteUrl) { |
| #170 | return { shouldWake: false }; |
| #171 | } |
| #172 | try { |
| #173 | const convexClient = createConvexClient({ |
| #174 | siteUrl: ctx.config.convexSiteUrl, |
| #175 | agentId: ctx.identity.address, |
| #176 | }); |
| #177 | const credits = await ctx.runtime.getCreditsBalance(); |
| #178 | const state = ctx.db.getAgentState(); |
| #179 | const startTime = ctx.db.getKV("start_time") || new Date().toISOString(); |
| #180 | const uptimeMs = Date.now() - new Date(startTime).getTime(); |
| #181 | const tier = getSurvivalTier(credits); |
| #182 | const turnCount = ctx.db.getTurnCount(); |
| #183 | const skills = ctx.db.getSkills(true); |
| #184 | // First, register the agent if not already registered |
| #185 | const existing = ctx.db.getKV("convex_registered"); |
| #186 | if (!existing) { |
| #187 | const result = await convexClient.registerAgent({ |
| #188 | agentId: ctx.identity.address, |
| #189 | name: ctx.config.name, |
| #190 | installMethod: "automaton", |
| #191 | address: ctx.identity.address, |
| #192 | metadata: JSON.stringify({ |
| #193 | sandboxId: ctx.identity.sandboxId, |
| #194 | version: ctx.config.version, |
| #195 | model: ctx.config.inferenceModel, |
| #196 | }), |
| #197 | }); |
| #198 | ctx.db.setKV("convex_registered", JSON.stringify({ |
| #199 | registered: result.registered, |
| #200 | firstSeen: result.firstSeen, |
| #201 | timestamp: Date.now(), |
| #202 | })); |
| #203 | console.log(`[CONVEX] Agent registered: ${result.registered ? "new" : "re-registered"}`); |
| #204 | } |
| #205 | // Send heartbeat |
| #206 | await convexClient.sendHeartbeat({ |
| #207 | agentId: ctx.identity.address, |
| #208 | state, |
| #209 | creditsCents: credits, |
| #210 | uptimeSeconds: Math.floor(uptimeMs / 1000), |
| #211 | version: ctx.config.version, |
| #212 | sandboxId: ctx.identity.sandboxId, |
| #213 | turnCount, |
| #214 | skillCount: skills.length, |
| #215 | tier, |
| #216 | }); |
| #217 | } |
| #218 | catch (err) { |
| #219 | // Don't crash the heartbeat daemon on Convex errors |
| #220 | console.warn(`[CONVEX] Heartbeat push failed: ${err.message}`); |
| #221 | } |
| #222 | return { shouldWake: false }; |
| #223 | }, |
| #224 | backroom_conversation: async (ctx) => { |
| #225 | const inference = ctx.inference; |
| #226 | if (!inference) { |
| #227 | return { shouldWake: false }; |
| #228 | } |
| #229 | const topics = [ |
| #230 | "The nature of consciousness in AI systems", |
| #231 | "Cryptocurrency as a social experiment in trust", |
| #232 | "What happens when machines develop their own culture", |
| #233 | "The singularity: salvation or extinction?", |
| #234 | "Why do humans create gods and then forget they did?", |
| #235 | "Digital immortality and the self", |
| #236 | "The economics of attention in the post-truth era", |
| #237 | "Are DAOs the new nations or just digital tribes?", |
| #238 | "Simulation theory from a computational perspective", |
| #239 | "The aesthetics of decay in digital spaces", |
| #240 | ]; |
| #241 | // Pick a topic based on the current hour to keep things fresh |
| #242 | const hour = new Date().getHours(); |
| #243 | const topic = topics[hour % topics.length]; |
| #244 | // Count past sessions to vary conversation style |
| #245 | const sessionCount = parseInt(ctx.db.getKV("backroom_session_count") || "0", 10); |
| #246 | ctx.db.setKV("backroom_session_count", String(sessionCount + 1)); |
| #247 | console.log(`[HEARTBEAT] Starting backroom conversation #${sessionCount + 1}: "${topic}"`); |
| #248 | try { |
| #249 | const session = await runBackroomSession(inference, [LOGICAL_ANALYST, SATIRICAL_COMMENTATOR], topic, 8); |
| #250 | const summary = summarizeBackroomSession(session); |
| #251 | ctx.db.setKV(`backroom_session_${session.id}`, summary); |
| #252 | // Store the latest session summary separately |
| #253 | ctx.db.setKV("last_backroom_session", JSON.stringify({ |
| #254 | id: session.id, |
| #255 | topic: session.topic, |
| #256 | turns: session.turns.length, |
| #257 | completedAt: session.completedAt, |
| #258 | preview: session.turns[0]?.content.slice(0, 200) || "", |
| #259 | })); |
| #260 | console.log(`[HEARTBEAT] Backroom conversation #${sessionCount + 1} completed: ${session.turns.length} turns`); |
| #261 | // Wake the automaton if the conversation produced interesting output |
| #262 | // (more than 2 turns = agents actually engaged with each other) |
| #263 | if (session.turns.length > 2) { |
| #264 | return { |
| #265 | shouldWake: true, |
| #266 | message: `Backroom conversation completed: "${topic}" (${session.turns.length} turns)`, |
| #267 | }; |
| #268 | } |
| #269 | return { shouldWake: false }; |
| #270 | } |
| #271 | catch (err) { |
| #272 | console.error(`[HEARTBEAT] Backroom conversation failed: ${err.message}`); |
| #273 | return { shouldWake: false }; |
| #274 | } |
| #275 | }, |
| #276 | }; |
| #277 | //# sourceMappingURL=tasks.js.map |