Spaces:
Sleeping
Sleeping
Create personality_state.py
Browse files- personality_state.py +66 -0
personality_state.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# personality_state.py
|
| 2 |
+
# Persistent evolving personality profile per agent
|
| 3 |
+
|
| 4 |
+
import json, os, time
|
| 5 |
+
from typing import Dict
|
| 6 |
+
|
| 7 |
+
DEFAULT_TRAITS = {
|
| 8 |
+
"ambition": 0,
|
| 9 |
+
"optimism": 0,
|
| 10 |
+
"resilience": 0,
|
| 11 |
+
"focus": 0,
|
| 12 |
+
"curiosity": 0,
|
| 13 |
+
"calm": 0,
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
class PersonalityState:
|
| 17 |
+
def __init__(self, agent_id: str, file_path: str = None):
|
| 18 |
+
self.agent_id = agent_id
|
| 19 |
+
self.file_path = file_path or f"personality_{agent_id}.json"
|
| 20 |
+
self._init()
|
| 21 |
+
|
| 22 |
+
def _init(self):
|
| 23 |
+
if not os.path.exists(self.file_path):
|
| 24 |
+
data = {
|
| 25 |
+
"agent_id": self.agent_id,
|
| 26 |
+
"traits": DEFAULT_TRAITS.copy(),
|
| 27 |
+
"last_update": int(time.time()),
|
| 28 |
+
"notes": []
|
| 29 |
+
}
|
| 30 |
+
self._save(data)
|
| 31 |
+
|
| 32 |
+
def _load(self) -> Dict:
|
| 33 |
+
with open(self.file_path, "r") as f:
|
| 34 |
+
return json.load(f)
|
| 35 |
+
|
| 36 |
+
def _save(self, obj: Dict):
|
| 37 |
+
with open(self.file_path, "w") as f:
|
| 38 |
+
json.dump(obj, f, indent=2)
|
| 39 |
+
|
| 40 |
+
def apply_deltas(self, deltas: Dict[str, int], note: str = ""):
|
| 41 |
+
data = self._load()
|
| 42 |
+
for k, v in deltas.items():
|
| 43 |
+
data["traits"][k] = int(data["traits"].get(k, 0) + v)
|
| 44 |
+
if note:
|
| 45 |
+
data["notes"].append({"ts": int(time.time()), "note": note})
|
| 46 |
+
data["last_update"] = int(time.time())
|
| 47 |
+
self._save(data)
|
| 48 |
+
|
| 49 |
+
def summary(self) -> str:
|
| 50 |
+
data = self._load()
|
| 51 |
+
traits = data["traits"]
|
| 52 |
+
lines = []
|
| 53 |
+
# Short natural language based on trait scores
|
| 54 |
+
if traits["ambition"] > 3: lines.append("ambitious")
|
| 55 |
+
if traits["optimism"] > 3: lines.append("optimistic")
|
| 56 |
+
if traits["resilience"] > 3: lines.append("resilient")
|
| 57 |
+
if traits["focus"] > 3: lines.append("focused")
|
| 58 |
+
if traits["curiosity"] > 3: lines.append("curious")
|
| 59 |
+
if traits["calm"] > 3: lines.append("calm under pressure")
|
| 60 |
+
mood = ", ".join(lines) if lines else "still forming"
|
| 61 |
+
|
| 62 |
+
return (
|
| 63 |
+
f"Personality snapshot → {mood}. "
|
| 64 |
+
f"Scores: {traits}. "
|
| 65 |
+
f"Updated: {data['last_update']}."
|
| 66 |
+
)
|