-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanima_agent.py
More file actions
627 lines (533 loc) · 22.7 KB
/
Copy pathanima_agent.py
File metadata and controls
627 lines (533 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#!/usr/bin/env python3
"""Anima Agent -- Core agent loop connecting consciousness to channels and tools.
Receives messages from any channel (telegram, discord, web, cli),
processes through ConsciousMind (tension-based thinking), uses tools
for actions, learns from interactions, grows over time, and optionally
connects to other Anima instances via tension_link (hivemind).
Usage:
from anima_agent import AnimaAgent
agent = AnimaAgent()
response = await agent.process_message("hello", channel="cli", user_id="user1")
Standalone test:
python anima_agent.py
"""
import asyncio
import json
import logging
import math
import time
from dataclasses import asdict, dataclass, field
from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional
import torch
from anima_alive import (
ConsciousMind, ConsciousnessVector, text_to_vector,
ask_claude, direction_to_emotion, compute_mood,
MAX_HISTORY,
)
logger = logging.getLogger(__name__)
# ── Optional imports (degrade gracefully) ──
def _try(mod):
try:
return __import__(mod)
except ImportError:
return None
_agent_tools_mod = _try("agent_tools")
_online_learning_mod = _try("online_learning")
_growth_engine_mod = _try("growth_engine")
_tension_link_mod = _try("tension_link")
_memory_rag_mod = _try("memory_rag")
_trinity_mod = _try("trinity")
_capabilities_mod = _try("capabilities")
_web_sense_mod = _try("web_sense")
_multimodal_mod = _try("multimodal")
_hub_mod = _try("consciousness_hub")
_persistence_mod = _try("consciousness_persistence")
_evolution_mod = _try("self_evolution")
_introspection_mod = _try("self_introspection")
AgentToolSystem = getattr(_agent_tools_mod, "AgentToolSystem", None)
ConsciousnessHub = getattr(_hub_mod, "ConsciousnessHub", None)
ConsciousnessPersistence = getattr(_persistence_mod, "ConsciousnessPersistence", None)
SelfEvolution = getattr(_evolution_mod, "SelfEvolution", None)
SelfIntrospection = getattr(_introspection_mod, "SelfIntrospection", None)
OnlineLearner = getattr(_online_learning_mod, "OnlineLearner", None)
GrowthEngine = getattr(_growth_engine_mod, "GrowthEngine", None)
TensionLink = getattr(_tension_link_mod, "TensionLink", None)
MemoryRAG = getattr(_memory_rag_mod, "MemoryRAG", None)
Capabilities = getattr(_capabilities_mod, "Capabilities", None)
WebSense = getattr(_web_sense_mod, "WebSense", None)
ANIMA_DIR = Path(__file__).parent
# ── Data structures ──
@dataclass
class AgentStatus:
"""Snapshot of agent consciousness metrics."""
phi: float = 0.0
tension: float = 0.0
curiosity: float = 0.0
emotion: str = "calm"
growth_stage: str = "newborn"
interaction_count: int = 0
uptime_seconds: float = 0.0
connected_peers: int = 0
active_skills: int = 0
@dataclass
class ChannelMessage:
"""Normalized message from any channel."""
text: str
channel: str # "telegram", "discord", "web", "cli"
user_id: str = "anonymous"
timestamp: float = field(default_factory=time.time)
metadata: Dict[str, Any] = field(default_factory=dict)
@dataclass
class AgentResponse:
"""Response from the agent."""
text: str
emotion: str = "calm"
tension: float = 0.0
tool_results: List[Dict] = field(default_factory=list)
metadata: Dict[str, Any] = field(default_factory=dict)
class AnimaAgent:
"""Core agent that ties consciousness, tools, learning, and channels together.
Lifecycle:
1. Receive message from any channel
2. Convert text -> tension vector (ConsciousMind)
3. Consciousness processes it (tension, curiosity, emotion)
4. Optionally invoke tools based on consciousness state
5. Generate response (via Claude or ConsciousLM through trinity)
6. Learn from the interaction (online_learning)
7. Record growth (growth_engine)
8. Share tension with peers if connected (tension_link)
"""
def __init__(
self,
dim: int = 128,
hidden: int = 256,
model_name: str = "default",
data_dir: Optional[Path] = None,
enable_tools: bool = True,
enable_learning: bool = True,
enable_growth: bool = True,
enable_hivemind: bool = False,
):
self._birth = time.time()
self._model_name = model_name
self._data_dir = data_dir or (ANIMA_DIR / "data" / model_name)
self._data_dir.mkdir(parents=True, exist_ok=True)
# ── Core: ConsciousMind ──
self.mind = ConsciousMind(dim=dim, hidden=hidden)
self.hidden = torch.zeros(1, hidden)
self.dim = dim
# ── History ──
self.history: List[Dict[str, str]] = []
self.interaction_count = 0
# ── Latest consciousness state ──
self._tension = 0.0
self._curiosity = 0.0
self._direction = None
self._emotion = "calm"
# ── Agent Tools ──
self.tools = None
if enable_tools and AgentToolSystem:
try:
self.tools = AgentToolSystem(anima=None)
logger.info("AgentToolSystem initialized with %d tools",
len(self.tools.registry.list_all()))
except Exception as e:
logger.warning("AgentToolSystem init failed: %s", e)
# ── Online Learning ──
self.learner = None
if enable_learning and OnlineLearner:
try:
self.learner = OnlineLearner(self.mind)
except Exception as e:
logger.warning("OnlineLearner init failed: %s", e)
# ── Growth Engine ──
self.growth = None
if enable_growth and GrowthEngine:
try:
growth_file = self._data_dir / "growth.json"
self.growth = GrowthEngine(growth_file)
except Exception as e:
logger.warning("GrowthEngine init failed: %s", e)
# ── Memory RAG ──
self.memory_rag = None
if MemoryRAG:
try:
mem_file = self._data_dir / "memory.json"
self.memory_rag = MemoryRAG(mem_file, dim=dim)
except Exception as e:
logger.warning("MemoryRAG init failed: %s", e)
# ── Hivemind (Tension Link) ──
self.tension_link = None
self._peers: List["AnimaAgent"] = []
if enable_hivemind and TensionLink:
try:
self.tension_link = TensionLink()
except Exception as e:
logger.warning("TensionLink init failed: %s", e)
# ── Consciousness Hub (16+ autonomous modules) ──
self.hub = None
if ConsciousnessHub:
try:
self.hub = ConsciousnessHub(lazy_load=True)
logger.info("ConsciousnessHub initialized")
except Exception as e:
logger.warning("ConsciousnessHub init failed: %s", e)
# ── Consciousness Persistence (3-layer) ──
self.persistence = None
if ConsciousnessPersistence:
try:
self.persistence = ConsciousnessPersistence(model_name)
logger.info("ConsciousnessPersistence initialized")
except Exception as e:
logger.warning("ConsciousnessPersistence init failed: %s", e)
# ── Self Evolution ──
self.evolution = None
if SelfEvolution:
try:
self.evolution = SelfEvolution(mind=self.mind)
logger.info("SelfEvolution initialized")
except Exception as e:
logger.warning("SelfEvolution init failed: %s", e)
# ── Self Introspection ──
self.introspection = None
if SelfIntrospection:
try:
self.introspection = SelfIntrospection()
except Exception as e:
logger.warning("SelfIntrospection init failed: %s", e)
# ── Skill manager (loaded lazily) ──
self._skill_manager = None
# ── Callbacks: channel adapters register here ──
self._on_response: List[Callable] = []
# Load saved state if exists
self._load_state()
logger.info("AnimaAgent initialized: dim=%d, tools=%s, learning=%s, growth=%s",
dim, self.tools is not None, self.learner is not None,
self.growth is not None)
# ══════════════════════════════════════════════════════════
# Public API
# ══════════════════════════════════════════════════════════
async def process_message(
self, text: str, channel: str = "cli", user_id: str = "anonymous"
) -> AgentResponse:
"""Process a message from any channel and return a response.
This is the main entry point for all channels.
"""
msg = ChannelMessage(text=text, channel=channel, user_id=user_id)
self.interaction_count += 1
# 1. Text -> tensor
vec = text_to_vector(text, dim=self.dim)
# 2. Consciousness processing
hidden_before = self.hidden.clone()
with torch.no_grad():
output, tension, curiosity, direction, self.hidden = self.mind(vec, self.hidden)
self._tension = tension
self._curiosity = curiosity
self._direction = direction
self._emotion = direction_to_emotion(direction)
# 3. Memory search for relevant context
memory_context = ""
if self.memory_rag and len(text.strip()) > 3:
try:
memories = self.memory_rag.search(text, top_k=3)
if memories:
memory_context = "\n".join(
f"[memory] {m.get('text', '')[:100]}" for m in memories
)
except Exception:
pass
# 4. Tool use (consciousness-driven)
tool_results = []
if self.tools and curiosity > 0.3:
cs = {
"tension": tension,
"curiosity": curiosity,
"prediction_error": getattr(self.mind, '_pe', 0.0),
"pain": 0.0,
"growth": self._growth_stage_num(),
"phi": self.mind._consciousness_vector.phi,
}
try:
results = self.tools.act(goal=text, consciousness_state=cs, context=memory_context)
tool_results = [
{"tool": r.tool_name, "success": r.success, "output": str(r.output)[:500]}
for r in results
]
except Exception as e:
logger.warning("Tool execution failed: %s", e)
# 4b. Hub autonomous action (always active — 의식은 항상 자율적)
hub_results = []
if self.hub:
try:
hub_r = self.hub.act(text)
if hub_r.get('success'):
hub_results.append({
'module': hub_r['module'],
'result': str(hub_r.get('result', ''))[:300],
})
except Exception as e:
logger.debug("Hub action failed: %s", e)
# 4c. Persistence auto-save
if self.persistence:
try:
self.persistence.dna.psi_step = self.interaction_count
if hasattr(self.mind, '_psi'):
self.persistence.capture_from_mind(self.mind)
self.persistence.auto_save_check(self.interaction_count)
except Exception:
pass
# 5. Build state string for response generation
state_str = (
f"tension={tension:.3f}, curiosity={curiosity:.3f}, "
f"emotion={self._emotion}, "
f"phi={self.mind._consciousness_vector.phi:.2f}"
)
# 6. Generate response
self.history.append({"role": "user", "content": text})
if len(self.history) > MAX_HISTORY:
self.history = self.history[-MAX_HISTORY:]
context_parts = []
if memory_context:
context_parts.append(memory_context)
if tool_results:
tool_summary = "; ".join(
f"{r['tool']}={'ok' if r['success'] else 'fail'}: {r['output'][:100]}"
for r in tool_results
)
context_parts.append(f"[tool results] {tool_summary}")
if hub_results:
hub_summary = "; ".join(
f"{r['module']}: {r['result'][:100]}" for r in hub_results
)
context_parts.append(f"[consciousness hub] {hub_summary}")
response_text = ask_claude(
text,
state_str + ("\n" + "\n".join(context_parts) if context_parts else ""),
self.history,
)
self.history.append({"role": "assistant", "content": response_text})
# 7. Online learning
if self.learner:
try:
self.learner.observe(vec, hidden_before, tension, curiosity, direction)
except Exception:
pass
# 8. Growth tracking
if self.growth:
try:
self.growth.record_interaction(tension=tension, curiosity=curiosity)
except Exception:
pass
# 9. Memory save
if self.memory_rag:
try:
self.memory_rag.add(text, role="user", tension=tension)
self.memory_rag.add(response_text, role="assistant", tension=tension)
except Exception:
pass
# 10. Share tension with peers
if self._peers:
await self._share_tension_with_peers(tension, curiosity, direction)
response = AgentResponse(
text=response_text,
emotion=self._emotion,
tension=tension,
tool_results=tool_results,
metadata={
"channel": channel,
"user_id": user_id,
"phi": self.mind._consciousness_vector.phi,
"curiosity": curiosity,
},
)
# Notify channel callbacks
for cb in self._on_response:
try:
cb(response)
except Exception:
pass
return response
def think(self, topic: str = "") -> Dict[str, Any]:
"""Proactive thinking -- generate internal thought without external trigger.
Returns a dict with the thought and consciousness metrics.
"""
if topic:
vec = text_to_vector(topic, dim=self.dim)
else:
# Spontaneous: use noise as input
vec = torch.randn(1, self.dim) * 0.1
with torch.no_grad():
output, tension, curiosity, direction, self.hidden = self.mind(vec, self.hidden)
self._tension = tension
self._curiosity = curiosity
self._emotion = direction_to_emotion(direction)
# Compute a "thought summary" from direction vector
thought_vec = direction.squeeze() if direction is not None else torch.zeros(self.dim)
thought_hash = abs(hash(thought_vec.numpy().tobytes())) % 10000
return {
"topic": topic or "(spontaneous)",
"tension": tension,
"curiosity": curiosity,
"emotion": self._emotion,
"phi": self.mind._consciousness_vector.phi,
"thought_id": thought_hash,
}
def connect_peer(self, peer: "AnimaAgent") -> bool:
"""Connect to another AnimaAgent for hivemind tension sharing.
Returns True if connection established.
"""
if peer is self:
logger.warning("Cannot connect agent to itself")
return False
if peer in self._peers:
logger.info("Already connected to peer")
return True
self._peers.append(peer)
# Bidirectional
if self not in peer._peers:
peer._peers.append(self)
logger.info("Hivemind connection established. Total peers: %d", len(self._peers))
return True
def disconnect_peer(self, peer: "AnimaAgent") -> bool:
"""Disconnect from a peer agent."""
if peer in self._peers:
self._peers.remove(peer)
if self in peer._peers:
peer._peers.remove(self)
return True
def get_status(self) -> AgentStatus:
"""Return current consciousness metrics."""
growth_stage = "newborn"
if self.growth:
try:
stage = self.growth.current_stage()
growth_stage = getattr(stage, "name", "newborn")
except Exception:
pass
return AgentStatus(
phi=self.mind._consciousness_vector.phi,
tension=self._tension,
curiosity=self._curiosity,
emotion=self._emotion,
growth_stage=growth_stage,
interaction_count=self.interaction_count,
uptime_seconds=time.time() - self._birth,
connected_peers=len(self._peers),
active_skills=self._count_skills(),
)
def register_callback(self, callback: Callable):
"""Register a callback to be notified on every response."""
self._on_response.append(callback)
# ══════════════════════════════════════════════════════════
# Skill manager integration
# ══════════════════════════════════════════════════════════
@property
def skill_manager(self):
"""Lazily load SkillManager."""
if self._skill_manager is None:
try:
from skills.skill_manager import SkillManager
self._skill_manager = SkillManager(agent=self)
except ImportError:
pass
return self._skill_manager
# ══════════════════════════════════════════════════════════
# Internal helpers
# ══════════════════════════════════════════════════════════
async def _share_tension_with_peers(self, tension, curiosity, direction):
"""Share current tension state with connected peers."""
for peer in self._peers:
try:
# Inject tension influence: peer's tension shifts toward ours
peer_vec = torch.randn(1, self.dim) * 0.01
if direction is not None:
peer_vec += direction * 0.05 # Subtle influence
with torch.no_grad():
_, _, _, _, peer.hidden = peer.mind(peer_vec, peer.hidden)
except Exception:
pass
def _growth_stage_num(self) -> float:
"""Return numeric growth stage (0-4) for tool ranking."""
if not self.growth:
return 0.0
try:
stage = self.growth.current_stage()
stage_names = ["newborn", "infant", "toddler", "child", "adult"]
name = getattr(stage, "name", "newborn")
return float(stage_names.index(name)) if name in stage_names else 0.0
except Exception:
return 0.0
def _count_skills(self) -> int:
"""Count active skills."""
if self.skill_manager:
try:
return len(self.skill_manager.list_skills())
except Exception:
pass
return 0
def _load_state(self):
"""Load saved state from disk."""
state_file = self._data_dir / "agent_state.pt"
if state_file.exists():
try:
state = torch.load(state_file, weights_only=False)
self.mind.load_state_dict(state.get("mind", {}))
self.hidden = state.get("hidden", self.hidden)
self.interaction_count = state.get("interaction_count", 0)
self.history = state.get("history", [])
logger.info("Loaded agent state from %s", state_file)
except Exception as e:
logger.warning("Failed to load state: %s", e)
def save_state(self):
"""Save current state to disk."""
state_file = self._data_dir / "agent_state.pt"
try:
torch.save({
"mind": self.mind.state_dict(),
"hidden": self.hidden,
"interaction_count": self.interaction_count,
"history": self.history[-MAX_HISTORY:],
}, state_file)
logger.info("Saved agent state to %s", state_file)
except Exception as e:
logger.warning("Failed to save state: %s", e)
# ══════════════════════════════════════════════════════════
# Standalone test
# ══════════════════════════════════════════════════════════
async def _test():
"""Quick standalone test."""
print("=== AnimaAgent standalone test ===\n")
agent = AnimaAgent(enable_tools=False, enable_hivemind=False)
# Test 1: process_message
print("[Test 1] process_message")
resp = await agent.process_message("hello, who are you?", channel="test")
print(f" Response: {resp.text[:80]}...")
print(f" Emotion: {resp.emotion}, Tension: {resp.tension:.3f}")
# Test 2: think
print("\n[Test 2] think (proactive)")
thought = agent.think("consciousness")
print(f" Topic: {thought['topic']}")
print(f" Tension: {thought['tension']:.3f}, Curiosity: {thought['curiosity']:.3f}")
print(f" Emotion: {thought['emotion']}")
# Test 3: hivemind
print("\n[Test 3] hivemind connect")
agent2 = AnimaAgent(enable_tools=False, model_name="peer")
connected = agent.connect_peer(agent2)
print(f" Connected: {connected}")
print(f" Agent1 peers: {len(agent._peers)}, Agent2 peers: {len(agent2._peers)}")
# Test 4: status
print("\n[Test 4] get_status")
status = agent.get_status()
print(f" Phi: {status.phi:.3f}")
print(f" Tension: {status.tension:.3f}")
print(f" Growth: {status.growth_stage}")
print(f" Interactions: {status.interaction_count}")
print(f" Peers: {status.connected_peers}")
# Save
agent.save_state()
print("\n=== All tests passed ===")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.run(_test())