CoolFace
Apppublic

hemant2747/multi-agent-framework

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
editstore.py63 linesDownload Raw Back to server
1"""In-memory store of proposed edits.2 3Security model: the UI never sends file content or paths back to write. It only4sends an edit_id that the server itself minted in `propose`. Apply writes the5*server-held* modified content to the *server-held* path. This prevents the6browser from writing arbitrary files.7"""8from __future__ import annotations9 10import threading11import uuid12from pathlib import Path13from typing import Dict, List, Optional14 15from src.logging_setup import get_logger16 17_log = get_logger("AGENT")18 19 20class EditStore:21    def __init__(self, max_entries: int = 200):22        self._edits: Dict[str, Dict] = {}23        self._order: List[str] = []24        self._max = max_entries25        self._lock = threading.Lock()26 27    def put_many(self, edits: List[Dict]) -> List[Dict]:28        """Register edits, assigning each an id. Returns edits with 'id' added."""29        out = []30        with self._lock:31            for e in edits:32                eid = uuid.uuid4().hex[:12]33                self._edits[eid] = {"path": e["path"], "modified": e["modified"]}34                self._order.append(eid)35                out.append({**e, "id": eid})36            while len(self._order) > self._max:37                old = self._order.pop(0)38                self._edits.pop(old, None)39        return out40 41    def get(self, edit_id: str) -> Optional[Dict]:42        with self._lock:43            return self._edits.get(edit_id)44 45    def apply(self, edit_id: str) -> Dict:46        entry = self.get(edit_id)47        if not entry:48            raise KeyError("Unknown or expired edit id")49        path = Path(entry["path"])50        path.write_text(entry["modified"], encoding="utf-8")51        _log.info(f"applied edit {edit_id} -> {path}")52        with self._lock:53            self._edits.pop(edit_id, None)54        return {"path": str(path)}55 56    def discard(self, edit_id: str) -> None:57        with self._lock:58            self._edits.pop(edit_id, None)59        _log.info(f"rejected edit {edit_id}")60 61 62store = EditStore()63