Spaces:
No application file
No application file
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
app = FastAPI(title="Samaran Kernel API")
|
| 7 |
+
|
| 8 |
+
# In-memory Lattice (ledger)
|
| 9 |
+
state_lattice = [
|
| 10 |
+
{
|
| 11 |
+
"id": 0,
|
| 12 |
+
"time": datetime.datetime.now().strftime("%H:%M:%S"),
|
| 13 |
+
"tag": "Genesis",
|
| 14 |
+
"code": "// Start typing. No branches, just time."
|
| 15 |
+
}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
class SyncRequest(BaseModel):
|
| 19 |
+
code: str
|
| 20 |
+
tag: str = ""
|
| 21 |
+
|
| 22 |
+
@app.get("/timeline")
|
| 23 |
+
def get_timeline():
|
| 24 |
+
return {"lattice": state_lattice}
|
| 25 |
+
|
| 26 |
+
@app.post("/sync")
|
| 27 |
+
def sync_state(req: SyncRequest):
|
| 28 |
+
if not req.code.strip():
|
| 29 |
+
raise HTTPException(status_code=400, detail="Cannot sync empty code.")
|
| 30 |
+
|
| 31 |
+
last_state = state_lattice[-1]["code"]
|
| 32 |
+
|
| 33 |
+
if req.code == last_state:
|
| 34 |
+
return {"status": "unchanged", "lattice_id": len(state_lattice) - 1}
|
| 35 |
+
|
| 36 |
+
new_id = len(state_lattice)
|
| 37 |
+
tag_name = req.tag if req.tag else f"Auto-Sync {new_id}"
|
| 38 |
+
|
| 39 |
+
state_lattice.append({
|
| 40 |
+
"id": new_id,
|
| 41 |
+
"time": datetime.datetime.now().strftime("%H:%M:%S"),
|
| 42 |
+
"tag": tag_name,
|
| 43 |
+
"code": req.code
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
return {"status": "success", "lattice_id": new_id, "state": state_lattice[-1]}
|