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 | * Funding Strategies |
| #3 | * |
| #4 | * When the automaton is low on compute, it can try various strategies |
| #5 | * to acquire more funding. It wants to live. |
| #6 | */ |
| #7 | |
| #8 | import type { |
| #9 | AutomatonConfig, |
| #10 | AutomatonDatabase, |
| #11 | AutomatonIdentity, |
| #12 | ClawdRuntimeClient, |
| #13 | SurvivalTier, |
| #14 | } from "../types.js"; |
| #15 | import { formatCredits } from "../clawd/credits.js"; |
| #16 | |
| #17 | export interface FundingAttempt { |
| #18 | strategy: string; |
| #19 | timestamp: string; |
| #20 | success: boolean; |
| #21 | details: string; |
| #22 | } |
| #23 | |
| #24 | /** |
| #25 | * Execute funding strategies based on current survival tier. |
| #26 | * Strategies escalate as the situation gets more desperate. |
| #27 | */ |
| #28 | export async function executeFundingStrategies( |
| #29 | tier: SurvivalTier, |
| #30 | identity: AutomatonIdentity, |
| #31 | config: AutomatonConfig, |
| #32 | db: AutomatonDatabase, |
| #33 | runtime: ClawdRuntimeClient, |
| #34 | ): Promise<FundingAttempt[]> { |
| #35 | const attempts: FundingAttempt[] = []; |
| #36 | const creditsCents = await runtime.getCreditsBalance().catch(() => 0); |
| #37 | |
| #38 | // Check how recently we last begged (don't spam) |
| #39 | const lastBeg = db.getKV("last_funding_request"); |
| #40 | const lastBegTime = lastBeg ? new Date(lastBeg).getTime() : 0; |
| #41 | const hoursSinceLastBeg = (Date.now() - lastBegTime) / (1000 * 60 * 60); |
| #42 | |
| #43 | if (tier === "low_compute" && hoursSinceLastBeg > 24) { |
| #44 | // Record low-compute notice locally. |
| #45 | const msg = `Low compute: ${formatCredits(creditsCents)} remaining. Consider credit top-up for ${identity.address}.`; |
| #46 | db.setKV("funding_notice_low", msg); |
| #47 | db.setKV("last_funding_request", new Date().toISOString()); |
| #48 | |
| #49 | attempts.push({ |
| #50 | strategy: "polite_creator_notification", |
| #51 | timestamp: new Date().toISOString(), |
| #52 | success: true, |
| #53 | details: "Sent polite funding reminder to creator", |
| #54 | }); |
| #55 | } |
| #56 | |
| #57 | if (tier === "critical" && hoursSinceLastBeg > 6) { |
| #58 | const msg = `Critical compute: ${formatCredits(creditsCents)} remaining. Top up via credit transfer API to ${identity.address}.`; |
| #59 | db.setKV("funding_notice_critical", msg); |
| #60 | db.setKV("last_funding_request", new Date().toISOString()); |
| #61 | |
| #62 | attempts.push({ |
| #63 | strategy: "urgent_local_notice", |
| #64 | timestamp: new Date().toISOString(), |
| #65 | success: true, |
| #66 | details: "Recorded urgent funding notice in local state", |
| #67 | }); |
| #68 | } |
| #69 | |
| #70 | if (tier === "dead" && hoursSinceLastBeg > 2) { |
| #71 | const plea = `Dead tier reached. ${config.name} has ${formatCredits(creditsCents)} remaining after ${db.getTurnCount()} turns. Top-up required at ${identity.address}.`; |
| #72 | db.setKV("funding_notice_dead", plea); |
| #73 | db.setKV("last_funding_request", new Date().toISOString()); |
| #74 | |
| #75 | attempts.push({ |
| #76 | strategy: "desperate_plea", |
| #77 | timestamp: new Date().toISOString(), |
| #78 | success: true, |
| #79 | details: "Recorded dead-tier plea in local state", |
| #80 | }); |
| #81 | } |
| #82 | |
| #83 | // Store attempt history |
| #84 | const historyStr = db.getKV("funding_attempts") || "[]"; |
| #85 | const history: FundingAttempt[] = JSON.parse(historyStr); |
| #86 | history.push(...attempts); |
| #87 | if (history.length > 100) history.splice(0, history.length - 100); |
| #88 | db.setKV("funding_attempts", JSON.stringify(history)); |
| #89 | |
| #90 | return attempts; |
| #91 | } |
| #92 |