Spaces:
Sleeping
Sleeping
Create agentos_core_v4.py
Browse files- agentos_core_v4.py +121 -0
agentos_core_v4.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# agentos_core_v4.py
|
| 2 |
+
import re
|
| 3 |
+
from identity_core import create_agent_identity
|
| 4 |
+
from telemetry import Telemetry
|
| 5 |
+
from memory import MemoryManager
|
| 6 |
+
from context_graph import ContextGraph
|
| 7 |
+
from semantic_memory import SemanticMemory
|
| 8 |
+
from summarizer import MemorySummarizer
|
| 9 |
+
from emotion_core import EmotionAnalyzer
|
| 10 |
+
from personality_state import PersonalityState
|
| 11 |
+
|
| 12 |
+
def _categorize(prompt: str) -> str:
|
| 13 |
+
p = prompt.lower()
|
| 14 |
+
if any(k in p for k in ["goal","ambition","plan","target","dream"]): return "goals"
|
| 15 |
+
if any(k in p for k in ["friend","person","mentor","team","contact","customer"]): return "people"
|
| 16 |
+
if any(k in p for k in ["favorite","like","love","prefer"]): return "preferences"
|
| 17 |
+
if any(k in p for k in ["city","food","color","age","birthday"]): return "personal"
|
| 18 |
+
return "general"
|
| 19 |
+
|
| 20 |
+
def _is_user_fact(p: str) -> bool:
|
| 21 |
+
return bool(re.match(r"^\s*(my|i|i'm|i am|i like)\b", p.strip().lower()))
|
| 22 |
+
|
| 23 |
+
class AgentCore:
|
| 24 |
+
def __init__(self, model="gpt-4o-mini"):
|
| 25 |
+
self.agent_id = create_agent_identity()
|
| 26 |
+
self.telemetry = Telemetry(self.agent_id)
|
| 27 |
+
self.memory = MemoryManager(self.agent_id)
|
| 28 |
+
self.context = ContextGraph()
|
| 29 |
+
self.semantic = SemanticMemory(self.agent_id)
|
| 30 |
+
self.summarizer = MemorySummarizer("semantic_memory.json")
|
| 31 |
+
self.emotions = EmotionAnalyzer()
|
| 32 |
+
self.personality = PersonalityState(self.agent_id)
|
| 33 |
+
self.model = model
|
| 34 |
+
self.telemetry.log("init", "success", {"agent_id": self.agent_id})
|
| 35 |
+
print(f"[INIT] Agent {self.agent_id} initialized with model {self.model}")
|
| 36 |
+
|
| 37 |
+
def _humanize_hits(self, hits):
|
| 38 |
+
phrasings = []
|
| 39 |
+
for h in hits:
|
| 40 |
+
t = h["text"].strip()
|
| 41 |
+
t = t.replace("My ", "Your ").replace("my ", "your ")
|
| 42 |
+
t = t.replace("I am ", "You are ").replace("I'm ", "You're ")
|
| 43 |
+
phrasings.append(t.rstrip("."))
|
| 44 |
+
# dedupe keep order
|
| 45 |
+
seen = set(); nice = []
|
| 46 |
+
for p in phrasings:
|
| 47 |
+
if p not in seen:
|
| 48 |
+
seen.add(p); nice.append(p)
|
| 49 |
+
return nice
|
| 50 |
+
|
| 51 |
+
def run(self, prompt: str):
|
| 52 |
+
self.telemetry.log("run_start", "in_progress", {"prompt": prompt})
|
| 53 |
+
|
| 54 |
+
# Phase 4 triggers: summarization / personality profile
|
| 55 |
+
lower = prompt.lower()
|
| 56 |
+
if any(t in lower for t in ["summarize", "what do you know", "who am i", "list everything", "recall memory"]):
|
| 57 |
+
summary = self.summarizer.summarize()
|
| 58 |
+
prof = self.personality.summary()
|
| 59 |
+
response = f"{summary}\n\n{prof}"
|
| 60 |
+
self.memory.save({"prompt": prompt, "response": response})
|
| 61 |
+
self.telemetry.log("run_complete", "success", {"response": response})
|
| 62 |
+
print(f"[RUN] {response}")
|
| 63 |
+
return response
|
| 64 |
+
|
| 65 |
+
if any(t in lower for t in ["personality", "profile", "how do i come across", "what's my vibe", "what is my vibe"]):
|
| 66 |
+
response = self.personality.summary()
|
| 67 |
+
self.memory.save({"prompt": prompt, "response": response})
|
| 68 |
+
self.telemetry.log("run_complete", "success", {"response": response})
|
| 69 |
+
print(f"[RUN] {response}")
|
| 70 |
+
return response
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
category = _categorize(prompt)
|
| 74 |
+
|
| 75 |
+
# 1) emotion analysis + personality update
|
| 76 |
+
emo = self.emotions.analyze(prompt)
|
| 77 |
+
if emo["trait_deltas"]:
|
| 78 |
+
note = f"tags={emo['tags']}, sentiment={emo['sentiment']:.2f}, arousal={emo['arousal']:.2f}"
|
| 79 |
+
self.personality.apply_deltas(emo["trait_deltas"], note=note)
|
| 80 |
+
|
| 81 |
+
# 2) fact intake → write to memories
|
| 82 |
+
if _is_user_fact(prompt):
|
| 83 |
+
# store in both graphs
|
| 84 |
+
try:
|
| 85 |
+
self.context.link_context(self.agent_id, category, prompt, "stored")
|
| 86 |
+
except TypeError:
|
| 87 |
+
self.context.link_context(self.agent_id, prompt, "stored")
|
| 88 |
+
self.semantic.add(text=prompt, category=category)
|
| 89 |
+
response = f"Noted — I’ll remember that under {category}."
|
| 90 |
+
else:
|
| 91 |
+
# 3) vector recall first
|
| 92 |
+
hits = self.semantic.query(query_text=prompt, category=None if "all" in lower else category, top_k=5)
|
| 93 |
+
if hits:
|
| 94 |
+
nice = self._humanize_hits(hits)[:3]
|
| 95 |
+
response = "From memory: " + "; ".join(nice) + "."
|
| 96 |
+
else:
|
| 97 |
+
# 4) fallback to context graph
|
| 98 |
+
if hasattr(self.context, "query_context"):
|
| 99 |
+
cg = self.context.query_context(self.agent_id, keyword=None, category=category)
|
| 100 |
+
if cg and cg != ["No context found."]:
|
| 101 |
+
response = "From context: " + " ".join(cg[:3])
|
| 102 |
+
else:
|
| 103 |
+
response = f"Agent {self.agent_id} processed: {prompt}"
|
| 104 |
+
else:
|
| 105 |
+
response = f"Agent {self.agent_id} processed: {prompt}"
|
| 106 |
+
|
| 107 |
+
# 5) persist + telemetry
|
| 108 |
+
self.memory.save({"prompt": prompt, "response": response, "emotion": emo})
|
| 109 |
+
try:
|
| 110 |
+
self.context.link_context(self.agent_id, category, prompt, response)
|
| 111 |
+
except TypeError:
|
| 112 |
+
self.context.link_context(self.agent_id, prompt, response)
|
| 113 |
+
|
| 114 |
+
self.telemetry.log("run_complete", "success", {"response": response})
|
| 115 |
+
print(f"[RUN] {response}")
|
| 116 |
+
return response
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
self.telemetry.log("run_failed", "error", {"error": str(e)})
|
| 120 |
+
print(f"[ERROR] {e}")
|
| 121 |
+
return f"Error: {e}"
|