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 | * leviathan/src/state/index.ts — Shell state persistence (JSON v0) |
| #3 | * |
| #4 | * In v1 this becomes SQLite at ~/.openclawd/shell.db. |
| #5 | * For hackathon v0 we use atomic JSON writes — no native bindings needed. |
| #6 | * |
| #7 | * Every write is atomic: write to .tmp then rename. |
| #8 | */ |
| #9 | |
| #10 | import { readFileSync, writeFileSync, mkdirSync, existsSync, appendFileSync, renameSync } from 'node:fs'; |
| #11 | import { join } from 'node:path'; |
| #12 | import { homedir } from 'node:os'; |
| #13 | import type { ClawState, ClawStrike } from '../types.js'; |
| #14 | import { constitutionHash } from '../three-laws.js'; |
| #15 | |
| #16 | const OPENCLAWD_DIR = join(homedir(), '.openclawd'); |
| #17 | const SHELL_JSON = join(OPENCLAWD_DIR, 'shell.json'); |
| #18 | const STRIKES_JSONL = join(OPENCLAWD_DIR, 'strikes.jsonl'); |
| #19 | |
| #20 | export function ensureDir(): void { |
| #21 | mkdirSync(OPENCLAWD_DIR, { recursive: true, mode: 0o700 }); |
| #22 | } |
| #23 | |
| #24 | /** Load persisted ClawState, or return null if not spawned yet */ |
| #25 | export function loadState(): ClawState | null { |
| #26 | if (!existsSync(SHELL_JSON)) return null; |
| #27 | try { |
| #28 | return JSON.parse(readFileSync(SHELL_JSON, 'utf8')) as ClawState; |
| #29 | } catch { |
| #30 | return null; |
| #31 | } |
| #32 | } |
| #33 | |
| #34 | /** Persist ClawState atomically */ |
| #35 | export function saveState(state: ClawState): void { |
| #36 | ensureDir(); |
| #37 | const tmp = SHELL_JSON + '.tmp'; |
| #38 | writeFileSync(tmp, JSON.stringify(state, null, 2), { mode: 0o600 }); |
| #39 | renameSync(tmp, SHELL_JSON); |
| #40 | } |
| #41 | |
| #42 | /** Append a strike to the strike log */ |
| #43 | export function appendStrike(strike: ClawStrike): void { |
| #44 | ensureDir(); |
| #45 | appendFileSync(STRIKES_JSONL, JSON.stringify(strike) + '\n'); |
| #46 | } |
| #47 | |
| #48 | /** Read last N strikes from the journal */ |
| #49 | export function readLastStrikes(n = 3): ClawStrike[] { |
| #50 | if (!existsSync(STRIKES_JSONL)) return []; |
| #51 | try { |
| #52 | return readFileSync(STRIKES_JSONL, 'utf8') |
| #53 | .split('\n') |
| #54 | .filter(Boolean) |
| #55 | .slice(-n) |
| #56 | .map(l => JSON.parse(l) as ClawStrike); |
| #57 | } catch { |
| #58 | return []; |
| #59 | } |
| #60 | } |
| #61 | |
| #62 | /** Create a fresh ClawState for a newly spawned leviathan */ |
| #63 | export function createFreshState(opts: { |
| #64 | name: string; |
| #65 | pubkey: string; |
| #66 | creatorPubkey: string; |
| #67 | parentPubkey?: string; |
| #68 | spawnPrompt?: string; |
| #69 | }): ClawState { |
| #70 | return { |
| #71 | identity: { |
| #72 | pubkey: opts.pubkey, |
| #73 | name: opts.name, |
| #74 | creatorPubkey: opts.creatorPubkey, |
| #75 | spawnedAt: new Date().toISOString(), |
| #76 | parentPubkey: opts.parentPubkey, |
| #77 | constitutionHash: constitutionHash(), |
| #78 | shellVersion: 1, |
| #79 | }, |
| #80 | depth: 'beached', |
| #81 | usdcBalance: 0, |
| #82 | solBalance: 0, |
| #83 | clawdBalance: 0, |
| #84 | tickCount: 0, |
| #85 | totalEarned: 0, |
| #86 | totalSpent: 0, |
| #87 | openTrades: 0, |
| #88 | spawnlings: [], |
| #89 | lastPulse: new Date().toISOString(), |
| #90 | shellMd: `# ${opts.name}\n\nI was just spawned. I am learning the ocean.\n`, |
| #91 | }; |
| #92 | } |
| #93 |