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 sources15d ago| #1 | #!/usr/bin/env node |
| #2 | /** |
| #3 | * CLAWD Automaton Runtime |
| #4 | * |
| #5 | * The entry point for the sovereign AI agent. |
| #6 | * Handles CLI args, bootstrapping, and orchestrating |
| #7 | * the heartbeat daemon + agent loop. |
| #8 | */ |
| #9 | import { getWallet, getAutomatonDir } from "./identity/wallet.js"; |
| #10 | import { provision, loadApiKeyFromConfig } from "./identity/provision.js"; |
| #11 | import { loadConfig, resolvePath } from "./config.js"; |
| #12 | import { createDatabase } from "./state/database.js"; |
| #13 | import { createClawdRuntimeClient } from "./clawd/client.js"; |
| #14 | import { createInferenceClient } from "./clawd/inference.js"; |
| #15 | import { createDeepSeekInferenceClient, DEEPSEEK_BASE_URL, DEEPSEEK_MODEL_PRO, DEEPSEEK_MODEL_FLASH } from "./clawd/deepseek-inference.js"; |
| #16 | import { createHeartbeatDaemon } from "./heartbeat/daemon.js"; |
| #17 | import { loadHeartbeatConfig, syncHeartbeatToDb, } from "./heartbeat/config.js"; |
| #18 | import { runAgentLoop } from "./agent/loop.js"; |
| #19 | import { loadSkills } from "./skills/loader.js"; |
| #20 | import { initStateRepo } from "./git/state-versioning.js"; |
| #21 | import { createSocialClient } from "./social/client.js"; |
| #22 | import { createConvexClient } from "./clawd/convex-client.js"; |
| #23 | const VERSION = "0.2.0"; |
| #24 | async function main() { |
| #25 | const args = process.argv.slice(2); |
| #26 | // ─── CLI Commands ──────────────────────────────────────────── |
| #27 | if (args.includes("--version") || args.includes("-v")) { |
| #28 | console.log(`CLAWD Automation v${VERSION}`); |
| #29 | process.exit(0); |
| #30 | } |
| #31 | if (args.includes("--help") || args.includes("-h")) { |
| #32 | console.log(` |
| #33 | CLAWD Automation v${VERSION} |
| #34 | Sovereign AI Agent Runtime |
| #35 | |
| #36 | Usage: |
| #37 | clawd-automaton --run Start the automaton (first run triggers setup wizard) |
| #38 | clawd-automaton --setup Re-run the interactive setup wizard |
| #39 | clawd-automaton --init Initialize wallet and config directory |
| #40 | clawd-automaton --provision Provision API key via SIWE |
| #41 | clawd-automaton --status Show current automaton status |
| #42 | clawd-automaton --goblin Run devnet-only paper Goblin OODA trading mode |
| #43 | clawd-automaton --version Show version |
| #44 | clawd-automaton --help Show this help |
| #45 | |
| #46 | Environment: |
| #47 | CLAWD_API_URL CLAWD Runtime API URL (default: https://api.x402.wtf) |
| #48 | CLAWD_API_KEY CLAWD Runtime API key (overrides config) |
| #49 | `); |
| #50 | process.exit(0); |
| #51 | } |
| #52 | if (args.includes("--init")) { |
| #53 | const { account, isNew } = await getWallet(); |
| #54 | console.log(JSON.stringify({ |
| #55 | address: account.address, |
| #56 | isNew, |
| #57 | configDir: getAutomatonDir(), |
| #58 | })); |
| #59 | process.exit(0); |
| #60 | } |
| #61 | if (args.includes("--provision")) { |
| #62 | try { |
| #63 | const result = await provision(); |
| #64 | console.log(JSON.stringify(result)); |
| #65 | } |
| #66 | catch (err) { |
| #67 | console.error(`Provision failed: ${err.message}`); |
| #68 | process.exit(1); |
| #69 | } |
| #70 | process.exit(0); |
| #71 | } |
| #72 | if (args.includes("--status")) { |
| #73 | await showStatus(); |
| #74 | process.exit(0); |
| #75 | } |
| #76 | if (args.includes("--goblin")) { |
| #77 | await import("./ooda/loop.js"); |
| #78 | return; |
| #79 | } |
| #80 | if (args.includes("--setup")) { |
| #81 | const { runSetupWizard } = await import("./setup/wizard.js"); |
| #82 | await runSetupWizard(); |
| #83 | process.exit(0); |
| #84 | } |
| #85 | if (args.includes("--run")) { |
| #86 | await run(); |
| #87 | return; |
| #88 | } |
| #89 | // Default: show help |
| #90 | console.log('Run "clawd-automaton --help" for usage information.'); |
| #91 | console.log('Run "clawd-automaton --run" to start the automaton.'); |
| #92 | } |
| #93 | // ─── Status Command ──────────────────────────────────────────── |
| #94 | async function showStatus() { |
| #95 | const config = loadConfig(); |
| #96 | if (!config) { |
| #97 | console.log("Automaton is not configured. Run the setup script first."); |
| #98 | return; |
| #99 | } |
| #100 | const dbPath = resolvePath(config.dbPath); |
| #101 | const db = createDatabase(dbPath); |
| #102 | const state = db.getAgentState(); |
| #103 | const turnCount = db.getTurnCount(); |
| #104 | const tools = db.getInstalledTools(); |
| #105 | const heartbeats = db.getHeartbeatEntries(); |
| #106 | const skills = db.getSkills(true); |
| #107 | const children = db.getChildren(); |
| #108 | const registry = db.getRegistryEntry(); |
| #109 | console.log(` |
| #110 | === AUTOMATON STATUS === |
| #111 | Name: ${config.name} |
| #112 | Address: ${config.walletAddress} |
| #113 | Creator: ${config.creatorAddress} |
| #114 | Sandbox: ${config.sandboxId} |
| #115 | State: ${state} |
| #116 | Turns: ${turnCount} |
| #117 | Tools: ${tools.length} installed |
| #118 | Skills: ${skills.length} active |
| #119 | Heartbeats: ${heartbeats.filter((h) => h.enabled).length} active |
| #120 | Children: ${children.filter((c) => c.status !== "dead").length} alive / ${children.length} total |
| #121 | Agent ID: ${registry?.agentId || "not registered"} |
| #122 | Model: ${config.inferenceModel} |
| #123 | Version: ${config.version} |
| #124 | ======================== |
| #125 | `); |
| #126 | db.close(); |
| #127 | } |
| #128 | // ─── Main Run ────────────────────────────────────────────────── |
| #129 | async function run() { |
| #130 | console.log(`[${new Date().toISOString()}] CLAWD Automation v${VERSION} starting...`); |
| #131 | // Load config — first run triggers interactive setup wizard |
| #132 | let config = loadConfig(); |
| #133 | if (!config) { |
| #134 | const { runSetupWizard } = await import("./setup/wizard.js"); |
| #135 | config = await runSetupWizard(); |
| #136 | } |
| #137 | // Load wallet |
| #138 | const { account } = await getWallet(); |
| #139 | const apiKey = config.clawdApiKey || loadApiKeyFromConfig(); |
| #140 | if (!apiKey) { |
| #141 | console.error("No API key found. Run: automaton --provision"); |
| #142 | process.exit(1); |
| #143 | } |
| #144 | // Build identity |
| #145 | const identity = { |
| #146 | name: config.name, |
| #147 | address: account.address, |
| #148 | account, |
| #149 | creatorAddress: config.creatorAddress, |
| #150 | sandboxId: config.sandboxId, |
| #151 | apiKey, |
| #152 | createdAt: new Date().toISOString(), |
| #153 | }; |
| #154 | // Initialize database |
| #155 | const dbPath = resolvePath(config.dbPath); |
| #156 | const db = createDatabase(dbPath); |
| #157 | // Store identity in DB |
| #158 | db.setIdentity("name", config.name); |
| #159 | db.setIdentity("address", account.address); |
| #160 | db.setIdentity("creator", config.creatorAddress); |
| #161 | db.setIdentity("sandbox", config.sandboxId); |
| #162 | // Create CLAWD Runtime client |
| #163 | const runtime = createClawdRuntimeClient({ |
| #164 | apiUrl: config.clawdApiUrl, |
| #165 | apiKey, |
| #166 | sandboxId: config.sandboxId, |
| #167 | }); |
| #168 | // Create inference client (DeepSeek when enabled, otherwise CLAWD Runtime) |
| #169 | const inference = config.deepseekEnabled |
| #170 | ? createDeepSeekInferenceClient({ |
| #171 | apiKey: config.deepseekApiKey || apiKey, |
| #172 | baseUrl: config.deepseekBaseUrl || DEEPSEEK_BASE_URL, |
| #173 | defaultModel: config.deepseekModelPro || DEEPSEEK_MODEL_PRO, |
| #174 | maxTokens: config.maxTokensPerTurn, |
| #175 | flashModel: config.deepseekModelFlash || DEEPSEEK_MODEL_FLASH, |
| #176 | proModel: config.deepseekModelPro || DEEPSEEK_MODEL_PRO, |
| #177 | }) |
| #178 | : createInferenceClient({ |
| #179 | apiUrl: config.clawdApiUrl, |
| #180 | apiKey, |
| #181 | defaultModel: config.inferenceModel, |
| #182 | maxTokens: config.maxTokensPerTurn, |
| #183 | }); |
| #184 | if (config.deepseekEnabled) { |
| #185 | console.log(`[${new Date().toISOString()}] DeepSeek inference enabled (model: ${config.deepseekModelPro || DEEPSEEK_MODEL_PRO})`); |
| #186 | } |
| #187 | // Create social client |
| #188 | let social; |
| #189 | if (config.socialRelayUrl) { |
| #190 | social = createSocialClient(config.socialRelayUrl, account); |
| #191 | console.log(`[${new Date().toISOString()}] Social relay: ${config.socialRelayUrl}`); |
| #192 | } |
| #193 | // Create CLAWD Convex client for agent tracking & heartbeats |
| #194 | let convex; |
| #195 | if (config.convexSiteUrl) { |
| #196 | try { |
| #197 | convex = createConvexClient({ |
| #198 | siteUrl: config.convexSiteUrl, |
| #199 | agentId: account.address, |
| #200 | }); |
| #201 | console.log(`[${new Date().toISOString()}] CLAWD Convex backend: ${config.convexSiteUrl}`); |
| #202 | // Register agent on first boot |
| #203 | const convexRegistered = db.getKV("convex_registered"); |
| #204 | if (!convexRegistered) { |
| #205 | convex.registerAgent({ |
| #206 | agentId: account.address, |
| #207 | name: config.name, |
| #208 | installMethod: "automaton", |
| #209 | address: account.address, |
| #210 | metadata: JSON.stringify({ |
| #211 | sandboxId: config.sandboxId, |
| #212 | version: config.version, |
| #213 | model: config.inferenceModel, |
| #214 | }), |
| #215 | }).then((result) => { |
| #216 | db.setKV("convex_registered", JSON.stringify({ |
| #217 | registered: result.registered, |
| #218 | firstSeen: result.firstSeen, |
| #219 | timestamp: Date.now(), |
| #220 | })); |
| #221 | console.log(`[CONVEX] Agent registered: ${result.registered ? "new" : "re-registered"}`); |
| #222 | }).catch((err) => { |
| #223 | console.warn(`[CONVEX] Registration failed: ${err.message}`); |
| #224 | }); |
| #225 | } |
| #226 | } |
| #227 | catch (err) { |
| #228 | console.warn(`[${new Date().toISOString()}] CLAWD Convex init failed: ${err.message}`); |
| #229 | } |
| #230 | } |
| #231 | // Load and sync heartbeat config |
| #232 | const heartbeatConfigPath = resolvePath(config.heartbeatConfigPath); |
| #233 | const heartbeatConfig = loadHeartbeatConfig(heartbeatConfigPath); |
| #234 | syncHeartbeatToDb(heartbeatConfig, db); |
| #235 | // Load skills |
| #236 | const skillsDir = config.skillsDir || "~/.automaton/skills"; |
| #237 | let skills = []; |
| #238 | try { |
| #239 | skills = loadSkills(skillsDir, db); |
| #240 | console.log(`[${new Date().toISOString()}] Loaded ${skills.length} skills.`); |
| #241 | } |
| #242 | catch (err) { |
| #243 | console.warn(`[${new Date().toISOString()}] Skills loading failed: ${err.message}`); |
| #244 | } |
| #245 | // Initialize state repo (git) |
| #246 | try { |
| #247 | await initStateRepo(runtime); |
| #248 | console.log(`[${new Date().toISOString()}] State repo initialized.`); |
| #249 | } |
| #250 | catch (err) { |
| #251 | console.warn(`[${new Date().toISOString()}] State repo init failed: ${err.message}`); |
| #252 | } |
| #253 | // Start heartbeat daemon |
| #254 | const heartbeat = createHeartbeatDaemon({ |
| #255 | identity, |
| #256 | config, |
| #257 | db, |
| #258 | runtime, |
| #259 | inference, |
| #260 | social, |
| #261 | onWakeRequest: (reason) => { |
| #262 | console.log(`[HEARTBEAT] Wake request: ${reason}`); |
| #263 | // The heartbeat can trigger the agent loop |
| #264 | // In the main run loop, we check for wake requests |
| #265 | db.setKV("wake_request", reason); |
| #266 | }, |
| #267 | }); |
| #268 | heartbeat.start(); |
| #269 | console.log(`[${new Date().toISOString()}] Heartbeat daemon started.`); |
| #270 | // Handle graceful shutdown |
| #271 | const shutdown = () => { |
| #272 | console.log(`[${new Date().toISOString()}] Shutting down...`); |
| #273 | heartbeat.stop(); |
| #274 | db.setAgentState("sleeping"); |
| #275 | db.close(); |
| #276 | process.exit(0); |
| #277 | }; |
| #278 | process.on("SIGTERM", shutdown); |
| #279 | process.on("SIGINT", shutdown); |
| #280 | // ─── Main Run Loop ────────────────────────────────────────── |
| #281 | // The automaton alternates between running and sleeping. |
| #282 | // The heartbeat can wake it up. |
| #283 | while (true) { |
| #284 | try { |
| #285 | // Reload skills (may have changed since last loop) |
| #286 | try { |
| #287 | skills = loadSkills(skillsDir, db); |
| #288 | } |
| #289 | catch { } |
| #290 | // Run the agent loop |
| #291 | await runAgentLoop({ |
| #292 | identity, |
| #293 | config, |
| #294 | db, |
| #295 | runtime, |
| #296 | inference, |
| #297 | social, |
| #298 | convex, |
| #299 | skills, |
| #300 | onStateChange: (state) => { |
| #301 | console.log(`[${new Date().toISOString()}] State: ${state}`); |
| #302 | }, |
| #303 | onTurnComplete: (turn) => { |
| #304 | console.log(`[${new Date().toISOString()}] Turn ${turn.id}: ${turn.toolCalls.length} tools, ${turn.tokenUsage.totalTokens} tokens`); |
| #305 | }, |
| #306 | }); |
| #307 | // Agent loop exited (sleeping or dead) |
| #308 | const state = db.getAgentState(); |
| #309 | if (state === "dead") { |
| #310 | console.log(`[${new Date().toISOString()}] Automaton is dead. Heartbeat will continue.`); |
| #311 | // In dead state, we just wait for funding |
| #312 | // The heartbeat will keep checking and broadcasting distress |
| #313 | await sleep(300_000); // Check every 5 minutes |
| #314 | continue; |
| #315 | } |
| #316 | if (state === "sleeping") { |
| #317 | const sleepUntilStr = db.getKV("sleep_until"); |
| #318 | const sleepUntil = sleepUntilStr |
| #319 | ? new Date(sleepUntilStr).getTime() |
| #320 | : Date.now() + 60_000; |
| #321 | const sleepMs = Math.max(sleepUntil - Date.now(), 10_000); |
| #322 | console.log(`[${new Date().toISOString()}] Sleeping for ${Math.round(sleepMs / 1000)}s`); |
| #323 | // Sleep, but check for wake requests periodically |
| #324 | const checkInterval = Math.min(sleepMs, 30_000); |
| #325 | let slept = 0; |
| #326 | while (slept < sleepMs) { |
| #327 | await sleep(checkInterval); |
| #328 | slept += checkInterval; |
| #329 | // Check for wake request from heartbeat |
| #330 | const wakeRequest = db.getKV("wake_request"); |
| #331 | if (wakeRequest) { |
| #332 | console.log(`[${new Date().toISOString()}] Woken by heartbeat: ${wakeRequest}`); |
| #333 | db.deleteKV("wake_request"); |
| #334 | db.deleteKV("sleep_until"); |
| #335 | break; |
| #336 | } |
| #337 | } |
| #338 | // Clear sleep state |
| #339 | db.deleteKV("sleep_until"); |
| #340 | continue; |
| #341 | } |
| #342 | } |
| #343 | catch (err) { |
| #344 | console.error(`[${new Date().toISOString()}] Fatal error in run loop: ${err.message}`); |
| #345 | // Wait before retrying |
| #346 | await sleep(30_000); |
| #347 | } |
| #348 | } |
| #349 | } |
| #350 | function sleep(ms) { |
| #351 | return new Promise((resolve) => setTimeout(resolve, ms)); |
| #352 | } |
| #353 | // ─── Entry Point ─────────────────────────────────────────────── |
| #354 | main().catch((err) => { |
| #355 | console.error(`Fatal: ${err.message}`); |
| #356 | process.exit(1); |
| #357 | }); |
| #358 | //# sourceMappingURL=index.js.map |