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