Spaces:
Build error
Build error
Update agentos_core.py
Browse files- agentos_core.py +14 -48
agentos_core.py
CHANGED
|
@@ -1,74 +1,40 @@
|
|
| 1 |
-
import uuid
|
| 2 |
-
import json
|
| 3 |
import os
|
| 4 |
import time
|
| 5 |
-
from identity_core import IdentityManager
|
| 6 |
-
from context_graph import ContextGraph
|
| 7 |
from telemetry import log_event
|
| 8 |
from memory_core import update_memory, load_memory
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
-
# --- Memory Manager ---
|
| 12 |
-
class MemoryManager:
|
| 13 |
-
def __init__(self, agent_id):
|
| 14 |
-
self.agent_id = agent_id
|
| 15 |
-
self.file_path = f"memory_{agent_id}.json"
|
| 16 |
-
self._init_memory()
|
| 17 |
-
|
| 18 |
-
def _init_memory(self):
|
| 19 |
-
if not os.path.exists(self.file_path):
|
| 20 |
-
with open(self.file_path, "w") as f:
|
| 21 |
-
json.dump({"history": []}, f)
|
| 22 |
-
|
| 23 |
-
def save(self, data):
|
| 24 |
-
with open(self.file_path, "r") as f:
|
| 25 |
-
memory = json.load(f)
|
| 26 |
-
memory["history"].append(data)
|
| 27 |
-
with open(self.file_path, "w") as f:
|
| 28 |
-
json.dump(memory, f, indent=2)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# --- Context Graph ---
|
| 32 |
-
class ContextGraph:
|
| 33 |
-
def __init__(self):
|
| 34 |
-
self.context = {}
|
| 35 |
-
|
| 36 |
-
def update(self, key, value):
|
| 37 |
-
self.context[key] = value
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# --- Agent Identity ---
|
| 41 |
-
def create_agent_identity():
|
| 42 |
-
return str(uuid.uuid4())
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# --- Agent Core ---
|
| 46 |
class AgentCore:
|
| 47 |
def __init__(self, model="gpt-4o-mini"):
|
| 48 |
self.identity = IdentityManager()
|
| 49 |
-
self.
|
| 50 |
-
self.
|
| 51 |
-
self.memory = MemoryManager(self.agent_id)
|
| 52 |
-
self.context_graph = ContextGraph()
|
| 53 |
|
| 54 |
-
self.memory.save({
|
| 55 |
-
"event": "init",
|
| 56 |
-
"details": f"Agent {self.agent_id} initialized."
|
| 57 |
-
})
|
| 58 |
log_event(self.agent_id, "init", "success", {"model": model})
|
|
|
|
| 59 |
|
| 60 |
def run(self, prompt):
|
| 61 |
log_event(self.agent_id, "run_start", "in_progress", {"prompt": prompt})
|
| 62 |
|
| 63 |
try:
|
| 64 |
-
#
|
| 65 |
response = f"Agent {self.agent_id} processed: {prompt}"
|
|
|
|
|
|
|
| 66 |
update_memory(self.agent_id, "last_prompt", prompt)
|
| 67 |
update_memory(self.agent_id, "last_response", response)
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
| 69 |
log_event(self.agent_id, "run_complete", "success", {"response": response})
|
|
|
|
| 70 |
return response
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
log_event(self.agent_id, "run_failed", "error", {"error": str(e)})
|
|
|
|
| 74 |
raise e
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
|
|
|
|
|
|
| 3 |
from telemetry import log_event
|
| 4 |
from memory_core import update_memory, load_memory
|
| 5 |
+
from identity_core import IdentityManager
|
| 6 |
+
from context_graph import ContextGraph
|
| 7 |
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
class AgentCore:
|
| 10 |
def __init__(self, model="gpt-4o-mini"):
|
| 11 |
self.identity = IdentityManager()
|
| 12 |
+
self.agent_id = self.identity.agent_id
|
| 13 |
+
self.context = ContextGraph(self.agent_id)
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
log_event(self.agent_id, "init", "success", {"model": model})
|
| 16 |
+
print(f"[INIT] Agent {self.agent_id} initialized with model {model}")
|
| 17 |
|
| 18 |
def run(self, prompt):
|
| 19 |
log_event(self.agent_id, "run_start", "in_progress", {"prompt": prompt})
|
| 20 |
|
| 21 |
try:
|
| 22 |
+
# Simulate model output
|
| 23 |
response = f"Agent {self.agent_id} processed: {prompt}"
|
| 24 |
+
|
| 25 |
+
# Save memory + context
|
| 26 |
update_memory(self.agent_id, "last_prompt", prompt)
|
| 27 |
update_memory(self.agent_id, "last_response", response)
|
| 28 |
+
prompt_node = self.context.add_node("prompt", prompt)
|
| 29 |
+
response_node = self.context.add_node("response", response)
|
| 30 |
+
self.context.connect(prompt_node, response_node)
|
| 31 |
|
| 32 |
+
# Telemetry success
|
| 33 |
log_event(self.agent_id, "run_complete", "success", {"response": response})
|
| 34 |
+
print(f"[RUN] {response}")
|
| 35 |
return response
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
log_event(self.agent_id, "run_failed", "error", {"error": str(e)})
|
| 39 |
+
print(f"[ERROR] {e}")
|
| 40 |
raise e
|