from fastapi import FastAPI, HTTPException from pydantic import BaseModel from typing import List import datetime app = FastAPI(title="Samaran Kernel API") # In-memory Lattice (ledger) state_lattice = [ { "id": 0, "time": datetime.datetime.now().strftime("%H:%M:%S"), "tag": "Genesis", "code": "// Start typing. No branches, just time." } ] class SyncRequest(BaseModel): code: str tag: str = "" @app.get("/timeline") def get_timeline(): return {"lattice": state_lattice} @app.post("/sync") def sync_state(req: SyncRequest): if not req.code.strip(): raise HTTPException(status_code=400, detail="Cannot sync empty code.") last_state = state_lattice[-1]["code"] if req.code == last_state: return {"status": "unchanged", "lattice_id": len(state_lattice) - 1} new_id = len(state_lattice) tag_name = req.tag if req.tag else f"Auto-Sync {new_id}" state_lattice.append({ "id": new_id, "time": datetime.datetime.now().strftime("%H:%M:%S"), "tag": tag_name, "code": req.code }) return {"status": "success", "lattice_id": new_id, "state": state_lattice[-1]}