File size: 5,073 Bytes
3d46076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, Optional
from src.tools.base import ToolConnector
from src.brain.runtime import BrainRuntime

class ActInEnvironmentConnector(ToolConnector):
    def __init__(self):
        super().__init__(
            name="act_in_environment",
            description="Executes a physical or simulated locomotion/foraging action in the virtual ecosystem.",
            timeout_sec=5.0
        )
        self.agent_pos = [0.0, 0.0]
        self.heading_rad = 0.0

    @property
    def input_schema(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "action_type": {"type": "string", "enum": ["move_forward", "turn_left", "turn_right", "forage"]},
                "magnitude": {"type": "number", "default": 1.0}
            },
            "required": ["action_type"]
        }

    @property
    def output_schema(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "new_position": {"type": "array"},
                "heading": {"type": "number"},
                "foraged_reward": {"type": "number"}
            },
            "required": ["new_position", "heading", "foraged_reward"]
        }

    def _execute(self, params: Dict[str, Any], execution_id: str) -> Dict[str, Any]:
        act = params["action_type"]
        mag = float(params.get("magnitude", 1.0))
        reward = 0.0

        if act == "move_forward":
            self.agent_pos[0] += mag * 0.1
            self.agent_pos[1] += mag * 0.1
        elif act == "turn_left":
            self.heading_rad += 0.1 * mag
        elif act == "turn_right":
            self.heading_rad -= 0.1 * mag
        elif act == "forage":
            reward = 0.5 * mag

        return {
            "new_position": [round(p, 3) for p in self.agent_pos],
            "heading": round(self.heading_rad, 3),
            "foraged_reward": reward
        }

class InspectSelfConnector(ToolConnector):
    def __init__(self, brain: BrainRuntime):
        super().__init__(
            name="inspect_self",
            description="Inspects internal computational state, drives, attention, and spike telemetry of the brain.",
            timeout_sec=5.0
        )
        self.brain = brain

    @property
    def input_schema(self) -> Dict[str, Any]:
        return {"type": "object", "properties": {}}

    @property
    def output_schema(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "step": {"type": "integer"},
                "total_spikes": {"type": "integer"},
                "energy": {"type": "number"},
                "curiosity": {"type": "number"},
                "social": {"type": "number"},
                "integrity": {"type": "number"},
                "num_neurons": {"type": "integer"},
                "num_synapses": {"type": "integer"}
            },
            "required": ["step", "energy", "num_neurons", "num_synapses"]
        }

    def _execute(self, params: Dict[str, Any], execution_id: str) -> Dict[str, Any]:
        return {
            "step": self.brain.state.step_count,
            "total_spikes": self.brain.state.total_spikes,
            "energy": round(self.brain.state.drives.energy, 4),
            "curiosity": round(self.brain.state.drives.curiosity, 4),
            "social": round(self.brain.state.drives.social, 4),
            "integrity": round(self.brain.state.drives.integrity, 4),
            "num_neurons": self.brain.graph.num_neurons,
            "num_synapses": self.brain.graph.num_synapses
        }

class SleepConnector(ToolConnector):
    def __init__(self, brain: BrainRuntime):
        super().__init__(
            name="sleep",
            description="Enters sleep/quiescent state to replenish homeostatic energy and repair neural integrity.",
            timeout_sec=5.0
        )
        self.brain = brain

    @property
    def input_schema(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "duration_cycles": {"type": "integer", "default": 5}
            }
        }

    @property
    def output_schema(self) -> Dict[str, Any]:
        return {
            "type": "object",
            "properties": {
                "energy_restored": {"type": "number"},
                "current_energy": {"type": "number"}
            },
            "required": ["energy_restored", "current_energy"]
        }

    def _execute(self, params: Dict[str, Any], execution_id: str) -> Dict[str, Any]:
        cycles = int(params.get("duration_cycles", 5))
        before_e = self.brain.state.drives.energy
        restored = min(1.0 - before_e, 0.1 * cycles)
        self.brain.state.drives.energy = min(1.0, before_e + restored)
        self.brain.state.drives.integrity = min(1.0, self.brain.state.drives.integrity + 0.05 * cycles)
        return {
            "energy_restored": round(restored, 4),
            "current_energy": round(self.brain.state.drives.energy, 4)
        }