File size: 1,219 Bytes
9a81c93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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]}