Spaces:
Sleeping
Sleeping
| import random | |
| from dataclasses import dataclass, field | |
| from typing import List, Dict, Any | |
| class Slot: | |
| key: str | |
| content: str | |
| salience: float | |
| class Workspace: | |
| max_slots: int = 7 | |
| slots: List[Slot] = field(default_factory=list) | |
| history: List[Dict[str, Any]] = field(default_factory=list) | |
| def commit(self, key: str, content: str, salience: float): | |
| evicted = None | |
| if len(self.slots) >= self.max_slots: | |
| self.slots.sort(key=lambda s: s.salience) | |
| evicted = self.slots.pop(0) | |
| self.slots.append(Slot(key=key, content=content, salience=salience)) | |
| self.history.append({"event":"commit","key":key,"salience":salience,"evicted":evicted.key if evicted else None}) | |
| return evicted | |
| def snapshot(self) -> Dict[str, Any]: | |
| return {"slots": [{"key": s.key, "content": s.content, "salience": s.salience} for s in self.slots]} | |
| def randomize(self): | |
| random.shuffle(self.slots) | |
| def clear(self): | |
| self.slots.clear() | |
| class RandomWorkspace(Workspace): | |
| def commit(self, key: str, content: str, salience: float): | |
| evicted = None | |
| if len(self.slots) >= self.max_slots: | |
| idx = random.randrange(len(self.slots)) | |
| evicted = self.slots.pop(idx) | |
| idx = random.randrange(len(self.slots)+1) if self.slots else 0 | |
| self.slots.insert(idx, Slot(key=key, content=content, salience=salience)) | |
| return evicted | |