Spaces:
Sleeping
Sleeping
Update identity_core.py
Browse files- identity_core.py +66 -1
identity_core.py
CHANGED
|
@@ -1,4 +1,69 @@
|
|
| 1 |
import uuid
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def create_agent_identity():
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import uuid
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# ---------- TELEMETRY ----------
|
| 8 |
+
class Telemetry:
|
| 9 |
+
@staticmethod
|
| 10 |
+
def log(event, status, data=None):
|
| 11 |
+
"""Simple logger for system events."""
|
| 12 |
+
print(f"[Telemetry] {event} - {status} | {data or {}}")
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# ---------- MEMORY ----------
|
| 16 |
+
class MemoryManager:
|
| 17 |
+
def __init__(self, agent_id):
|
| 18 |
+
self.agent_id = agent_id
|
| 19 |
+
self.file_path = f"memory_{agent_id}.json"
|
| 20 |
+
if not os.path.exists(self.file_path):
|
| 21 |
+
with open(self.file_path, "w") as f:
|
| 22 |
+
json.dump({"history": []}, f)
|
| 23 |
+
|
| 24 |
+
def save(self, data):
|
| 25 |
+
"""Save each interaction into memory."""
|
| 26 |
+
with open(self.file_path, "r") as f:
|
| 27 |
+
memory = json.load(f)
|
| 28 |
+
memory["history"].append(data)
|
| 29 |
+
with open(self.file_path, "w") as f:
|
| 30 |
+
json.dump(memory, f, indent=2)
|
| 31 |
+
print(f"[Memory] {self.agent_id} learned {data}")
|
| 32 |
+
|
| 33 |
+
def recall(self):
|
| 34 |
+
"""Return the memory history."""
|
| 35 |
+
with open(self.file_path, "r") as f:
|
| 36 |
+
memory = json.load(f)
|
| 37 |
+
return memory["history"]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# ---------- IDENTITY ----------
|
| 41 |
def create_agent_identity():
|
| 42 |
+
"""Generate a unique ID for every agent instance."""
|
| 43 |
+
return str(uuid.uuid4())
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# ---------- AGENT CORE ----------
|
| 47 |
+
class AgentCore:
|
| 48 |
+
def __init__(self, model="gpt-4o-mini"):
|
| 49 |
+
"""Initialize agent with memory + telemetry."""
|
| 50 |
+
self.agent_id = create_agent_identity()
|
| 51 |
+
self.model = model
|
| 52 |
+
self.memory = MemoryManager(self.agent_id)
|
| 53 |
+
Telemetry.log("init", "success", {"agent_id": self.agent_id})
|
| 54 |
+
print(f"[INIT] Agent {self.agent_id} initialized with model {model}")
|
| 55 |
+
|
| 56 |
+
def run(self, prompt):
|
| 57 |
+
"""Main logic for handling a user prompt."""
|
| 58 |
+
Telemetry.log("run_start", "in_progress", {"prompt": prompt})
|
| 59 |
+
time.sleep(0.5)
|
| 60 |
+
|
| 61 |
+
# Simulated processing (replace later with AI model)
|
| 62 |
+
response = f"Agent {self.agent_id} processed: {prompt}"
|
| 63 |
+
|
| 64 |
+
# Save to memory
|
| 65 |
+
self.memory.save({"prompt": prompt, "response": response})
|
| 66 |
+
Telemetry.log("run_complete", "success", {"response": response})
|
| 67 |
+
|
| 68 |
+
print(f"[RUN] {response}")
|
| 69 |
+
return response
|