unaiverse 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of unaiverse might be problematic. Click here for more details.

Files changed (45) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2008 -0
  3. unaiverse/agent_basics.py +1844 -0
  4. unaiverse/clock.py +186 -0
  5. unaiverse/dataprops.py +1209 -0
  6. unaiverse/hsm.py +1880 -0
  7. unaiverse/modules/__init__.py +18 -0
  8. unaiverse/modules/cnu/__init__.py +17 -0
  9. unaiverse/modules/cnu/cnus.py +536 -0
  10. unaiverse/modules/cnu/layers.py +261 -0
  11. unaiverse/modules/cnu/psi.py +60 -0
  12. unaiverse/modules/hl/__init__.py +15 -0
  13. unaiverse/modules/hl/hl_utils.py +411 -0
  14. unaiverse/modules/networks.py +1509 -0
  15. unaiverse/modules/utils.py +680 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1265 -0
  19. unaiverse/networking/node/node.py +2203 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +259 -0
  23. unaiverse/networking/p2p/golibp2p.py +18 -0
  24. unaiverse/networking/p2p/golibp2p.pyi +135 -0
  25. unaiverse/networking/p2p/lib.go +2495 -0
  26. unaiverse/networking/p2p/lib_types.py +312 -0
  27. unaiverse/networking/p2p/message_pb2.py +63 -0
  28. unaiverse/networking/p2p/messages.py +265 -0
  29. unaiverse/networking/p2p/mylogger.py +77 -0
  30. unaiverse/networking/p2p/p2p.py +963 -0
  31. unaiverse/streamlib/__init__.py +15 -0
  32. unaiverse/streamlib/streamlib.py +210 -0
  33. unaiverse/streams.py +763 -0
  34. unaiverse/utils/__init__.py +16 -0
  35. unaiverse/utils/ask_lone_wolf.json +27 -0
  36. unaiverse/utils/lone_wolf.json +19 -0
  37. unaiverse/utils/misc.py +305 -0
  38. unaiverse/utils/sandbox.py +293 -0
  39. unaiverse/utils/server.py +435 -0
  40. unaiverse/world.py +175 -0
  41. unaiverse-0.1.0.dist-info/METADATA +363 -0
  42. unaiverse-0.1.0.dist-info/RECORD +45 -0
  43. unaiverse-0.1.0.dist-info/WHEEL +5 -0
  44. unaiverse-0.1.0.dist-info/licenses/LICENSE +43 -0
  45. unaiverse-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,259 @@
1
+ """
2
+ █████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
3
+ ░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
4
+ ░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
5
+ ░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
6
+ ░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
7
+ ░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
8
+ ░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
9
+ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
10
+ A Collectionless AI Project (https://collectionless.ai)
11
+ Registration/Login: https://unaiverse.io
12
+ Code Repositories: https://github.com/collectionlessai/
13
+ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
14
+ """
15
+ from . import messages
16
+ from . import p2p
17
+ from . import golibp2p
18
+ from . import lib_types
19
+ import os
20
+ import sys
21
+ import ctypes
22
+ import platform
23
+ import requests
24
+ import subprocess
25
+ from typing import cast
26
+ from .messages import Msg
27
+ from .p2p import P2P, P2PError
28
+ from .golibp2p import GoLibP2P # Your stub interface definition
29
+ from .lib_types import TypeInterface # Assuming TypeInterface handles the void* results
30
+
31
+
32
+ # --- Setup and Pre-build Checks ---
33
+
34
+ # Define paths and library names
35
+ lib_dir = os.path.dirname(__file__)
36
+ go_mod_file = os.path.join(lib_dir, "go.mod")
37
+ go_source_file = os.path.join(lib_dir, "lib.go")
38
+ lib_name = "lib"
39
+
40
+ # Determine the correct library file extension based on the OS
41
+ if platform.system() == "Windows":
42
+ lib_url = "https://github.com/collectionlessai/unaiverse-misc/raw/main/precompiled/lib.dll"
43
+ lib_ext = ".dll"
44
+ elif platform.system() == "Darwin": # MacOS
45
+ lib_url = "https://github.com/collectionlessai/unaiverse-misc/raw/main/precompiled/lib.dylib"
46
+ lib_ext = ".dylib"
47
+ else: # Linux and other Unix-like
48
+ lib_url = "https://github.com/collectionlessai/unaiverse-misc/raw/main/precompiled/lib.so"
49
+ lib_ext = ".so"
50
+
51
+ lib_filename = f"{lib_name}{lib_ext}"
52
+ lib_path = os.path.join(lib_dir, lib_filename)
53
+ downloaded_shared_lib = None
54
+
55
+ if not os.path.exists(lib_path):
56
+ print(f"INFO: '{lib_filename}' not found. Attempting to automatically download it and save to '{lib_dir}'...")
57
+ download_was_successful = False
58
+ try:
59
+ headers = {
60
+ "User-Agent": "python-requests/2.31.0" # Any browser-like agent also works
61
+ }
62
+ response = requests.get(lib_url, headers=headers, allow_redirects=True)
63
+ with open(lib_path, "wb") as f:
64
+ f.write(response.content)
65
+ download_was_successful = True
66
+ print(f"INFO: Download complete")
67
+ except Exception:
68
+ print(f"INFO: Download failed, attempting to compile from source (requires a Go compiler)...")
69
+
70
+ if download_was_successful:
71
+ try:
72
+ downloaded_shared_lib = ctypes.CDLL(lib_path)
73
+ except OSError as e:
74
+ downloaded_shared_lib = None
75
+ if os.path.exists(lib_path):
76
+ os.remove(lib_path)
77
+ print(f"INFO: The downloaded library was not compatible with this platform and was deleted. "
78
+ f"Attempting to compile from source (requires a Go compiler)...")
79
+
80
+ # --- Automatically initialize Go module if needed ---
81
+ if downloaded_shared_lib is None:
82
+ if not os.path.exists(go_mod_file):
83
+ print(f"INFO: 'go.mod' not found. Initializing Go module in '{lib_dir}'...")
84
+ try:
85
+
86
+ # Define a module path. This can be anything, but a path-like name is conventional.
87
+ module_path = "unaiverse/networking/p2p/lib"
88
+
89
+ # Run 'go mod init'
90
+ subprocess.run(
91
+ ["go", "mod", "init", module_path],
92
+ cwd=lib_dir, # Run the command in the directory containing lib.go
93
+ check=True, # Raise an exception if the command fails
94
+ capture_output=True, # Capture stdout/stderr
95
+ text=True
96
+ )
97
+
98
+ # Run 'go mod tidy' to find dependencies and create go.sum
99
+ print("INFO: Go module initialized. Running 'go mod tidy'...")
100
+ subprocess.run(
101
+ ["go", "mod", "tidy"],
102
+ cwd=lib_dir,
103
+ check=True,
104
+ capture_output=True,
105
+ text=True
106
+ )
107
+ print("INFO: 'go.mod' and 'go.sum' created successfully.")
108
+ except (subprocess.CalledProcessError, FileNotFoundError) as e:
109
+ print("FATAL: Failed to initialize Go module.", file=sys.stderr)
110
+ print("Please ensure Go is installed and in your system's PATH.", file=sys.stderr)
111
+
112
+ # If 'go mod' failed, print its output for debugging
113
+ if isinstance(e, subprocess.CalledProcessError):
114
+ print(f"Go command stderr:\n{e.stderr}", file=sys.stderr)
115
+ raise e
116
+
117
+ # --- Automatically build the shared library if it's missing or outdated ---
118
+ rebuild_needed = False
119
+ reason = ""
120
+
121
+ if downloaded_shared_lib is None:
122
+ if not os.path.exists(lib_path):
123
+ rebuild_needed = True
124
+ reason = f"the shared library '{lib_filename}' was not found."
125
+ elif os.path.getmtime(go_source_file) > os.path.getmtime(lib_path):
126
+ rebuild_needed = True
127
+ reason = f"the last modification to '{go_source_file}' is more recent than the '{lib_filename}' last build."
128
+
129
+ if rebuild_needed:
130
+ print(f"INFO: Rebuilding shared library because {reason}")
131
+ try:
132
+ build_command = ["go", "build", "-buildmode=c-shared", "-ldflags", "-s -w", "-o", lib_filename, "lib.go"]
133
+ print(f"Running command: {' '.join(build_command)}")
134
+ result = subprocess.run(
135
+ build_command,
136
+ cwd=lib_dir,
137
+ check=True,
138
+ capture_output=True,
139
+ text=True
140
+ )
141
+ if result.stdout:
142
+ print(f"Go build stdout:\n{result.stdout}")
143
+ print(f"INFO: Successfully built '{lib_filename}'.")
144
+
145
+ except (subprocess.CalledProcessError, FileNotFoundError) as e:
146
+ print(f"FATAL: Failed to build Go shared library.", file=sys.stderr)
147
+ print("Please ensure Go is installed and in your system's PATH.", file=sys.stderr)
148
+ if isinstance(e, subprocess.CalledProcessError):
149
+ print(f"Go compiler stderr:\n{e.stderr}", file=sys.stderr)
150
+ raise e
151
+
152
+ # --- Library Loading ---
153
+ if downloaded_shared_lib is None:
154
+ try:
155
+ _shared_lib = ctypes.CDLL(lib_path)
156
+
157
+ # Print(f"Successfully loaded Go library: {lib_path}")
158
+ except OSError as e:
159
+ print(f"Error loading shared library at {lib_path}: {e}", file=sys.stderr)
160
+ raise
161
+ else:
162
+ _shared_lib = downloaded_shared_lib
163
+
164
+ # --- Function Prototypes (argtypes and restype) ---
165
+ # Using void* for returned C strings, requiring TypeInterface for conversion/freeing.
166
+
167
+ # Define argtypes for the Go init function here
168
+ _shared_lib.InitializeLibrary.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int]
169
+ _shared_lib.InitializeLibrary.restype = None
170
+
171
+ # Node Lifecycle & Info
172
+ _shared_lib.CreateNode.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_int,
173
+ ctypes.c_int, ctypes.c_int]
174
+ _shared_lib.CreateNode.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
175
+
176
+ _shared_lib.CloseNode.argtypes = [ctypes.c_int]
177
+ _shared_lib.CloseNode.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
178
+
179
+ _shared_lib.GetNodeAddresses.argtypes = [ctypes.c_int, ctypes.c_char_p] # Input is still a Python string -> C string
180
+ _shared_lib.GetNodeAddresses.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
181
+
182
+ _shared_lib.GetConnectedPeers.argtypes = [ctypes.c_int]
183
+ _shared_lib.GetConnectedPeers.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
184
+
185
+ _shared_lib.GetRendezvousPeers.argtypes = [ctypes.c_int]
186
+ _shared_lib.GetRendezvousPeers.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
187
+
188
+ # Peer Connection
189
+ _shared_lib.ConnectTo.argtypes = [ctypes.c_int, ctypes.c_char_p]
190
+ _shared_lib.ConnectTo.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
191
+
192
+ _shared_lib.DisconnectFrom.argtypes = [ctypes.c_int, ctypes.c_char_p]
193
+ _shared_lib.DisconnectFrom.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
194
+
195
+ # Direct Messaging
196
+ _shared_lib.SendMessageToPeer.argtypes = [
197
+ ctypes.c_int, # Instance
198
+ ctypes.c_char_p, # Channel
199
+ ctypes.c_char_p, # Data buffer
200
+ ctypes.c_int, # Data length
201
+ ]
202
+ _shared_lib.SendMessageToPeer.restype = ctypes.c_void_p # Returns status code, not pointer
203
+
204
+ # Message Queue
205
+ _shared_lib.MessageQueueLength.argtypes = [ctypes.c_int]
206
+ _shared_lib.MessageQueueLength.restype = ctypes.c_int # Returns length, not pointer
207
+
208
+ _shared_lib.PopMessages.argtypes = [ctypes.c_int]
209
+ _shared_lib.PopMessages.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
210
+
211
+ # PubSub
212
+ _shared_lib.SubscribeToTopic.argtypes = [ctypes.c_int, ctypes.c_char_p]
213
+ _shared_lib.SubscribeToTopic.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
214
+
215
+ _shared_lib.UnsubscribeFromTopic.argtypes = [ctypes.c_int, ctypes.c_char_p]
216
+ _shared_lib.UnsubscribeFromTopic.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
217
+
218
+ # Relay Client
219
+ _shared_lib.ReserveOnRelay.argtypes = [ctypes.c_int, ctypes.c_char_p]
220
+ _shared_lib.ReserveOnRelay.restype = ctypes.c_void_p # Treat returned *C.char as opaque pointer
221
+
222
+ # Memory Management
223
+ # FreeString now accepts the opaque pointer directly
224
+ _shared_lib.FreeString.argtypes = [ctypes.c_void_p]
225
+ _shared_lib.FreeString.restype = None # Void return
226
+
227
+ _shared_lib.FreeInt.argtypes = [ctypes.POINTER(ctypes.c_int)] # Still expects a pointer to int
228
+ _shared_lib.FreeInt.restype = None # Void return
229
+
230
+ # --- Python Interface Setup ---
231
+
232
+ # Import necessary components
233
+ # IMPORTANT: TypeInterface (or equivalent logic) MUST now handle converting
234
+ # the c_char_p results back to strings/JSON before freeing.
235
+ # Ensure TypeInterface methods like from_go_string_to_json are adapted for this.
236
+
237
+ # Import the stub type for type checking
238
+ try:
239
+ from .golibp2p import GoLibP2P # Your stub interface definition
240
+ except ImportError:
241
+ print("Warning: GoLibP2P stub not found. Type checking will be limited.", file=sys.stderr)
242
+ GoLibP2P = ctypes.CDLL
243
+
244
+ # Cast the loaded library object to the stub type
245
+ _shared_lib_typed = cast(GoLibP2P, _shared_lib)
246
+
247
+
248
+ # Attach the typed shared library object to the P2P class
249
+ P2P.libp2p = _shared_lib_typed
250
+ TypeInterface.libp2p = _shared_lib_typed # Attach to TypeInterface if needed
251
+
252
+ # Attach the typed shared library object to the P2PError class
253
+
254
+ # Define the public API of this package
255
+ __all__ = [
256
+ "P2P",
257
+ "P2PError",
258
+ "TypeInterface" # Expose TypeInterface if users need its conversion helpers directly
259
+ ]
@@ -0,0 +1,18 @@
1
+ """
2
+ █████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
3
+ ░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
4
+ ░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
5
+ ░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
6
+ ░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
7
+ ░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
8
+ ░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
9
+ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
10
+ A Collectionless AI Project (https://collectionless.ai)
11
+ Registration/Login: https://unaiverse.io
12
+ Code Repositories: https://github.com/collectionlessai/
13
+ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
14
+ """
15
+
16
+
17
+ class GoLibP2P:
18
+ pass
@@ -0,0 +1,135 @@
1
+ from typing import List
2
+
3
+
4
+ class GoLibP2P:
5
+ def InitializeLibrary(self, max_instances: int, max_num_channels: int, max_queue_per_channel: int, max_msg_size: int, enable_logging: int) -> None:
6
+ """
7
+ InitializeLibrary(max_instances: int, max_queue_per_channel: int, max_num_channels: int, max_msg_size: int, enable_logging: int) -> bytes
8
+
9
+ Configures the P2P library.
10
+ """
11
+ ...
12
+
13
+ def CreateNode(self, instance: int, port: int, ips: List[str], enable_relay_client: int, enable_relay_service: int, wait_public_reachability: int, max_connections: int) -> bytes:
14
+ """
15
+ CreateNode(instance: int, port: int, ips: List[str], enable_relay_client: int, enable_relay_service: int, wait_public_reachability: int, max_connections: int) -> bytes
16
+
17
+ Creates a node in the P2P network and returns a JSON string with node information.
18
+ """
19
+ ...
20
+
21
+ def ConnectTo(self, instance: int, multiaddrs_json: bytes) -> bytes:
22
+ """
23
+ ConnectTo(instance: int, multiaddrs_json: bytes) -> bytes
24
+
25
+ Connects to a peer using the provided multiaddress. Returns a JSON string with the result.
26
+ """
27
+ ...
28
+
29
+ def ReserveOnRelay(self, instance: int, relay_peer_id: bytes) -> bytes:
30
+ """
31
+ ReserveOnRelay(instance: int, relay_peer_id: bytes) -> bytes
32
+
33
+ Reserves a relay port on the network. Returns a JSON result.
34
+ """
35
+ ...
36
+
37
+ def DisconnectFrom(self, instance: int, peer_id: bytes) -> bytes:
38
+ """
39
+ DisconnectFrom(instance: int, peer_id: bytes) -> bytes
40
+
41
+ Disconnects from the given peer id. Returns a JSON result.
42
+ """
43
+ ...
44
+
45
+ def GetConnectedPeers(self, instance: int) -> bytes:
46
+ """
47
+ GetConnectedPeers(instance: int) -> bytes
48
+
49
+ Returns a JSON string listing connected peers.
50
+ """
51
+ ...
52
+
53
+ def GetRendezvousPeers(self, instance: int) -> bytes:
54
+ """
55
+ GetRendezvousPeers(instance: int) -> bytes
56
+
57
+ Returns a JSON string listing rendezvous peers.
58
+ """
59
+ ...
60
+
61
+ def GetNodeAddresses(self, instance: int, arg: bytes) -> bytes:
62
+ """
63
+ GetNodeAddresses(instance: int, arg: bytes) -> bytes
64
+
65
+ Returns the node addresses in a JSON string.
66
+ """
67
+ ...
68
+
69
+ def SendMessageToPeer(
70
+ self,
71
+ instance: int,
72
+ channel: bytes,
73
+ data: bytes,
74
+ data_len: int,
75
+ ) -> bytes:
76
+ """
77
+ SendMessageToPeer(instance: int, channel: bytes, data: bytes, data_len: int) -> bytes
78
+ """
79
+ ...
80
+
81
+ def SubscribeToTopic(self, instance: int, topic_composite_key: bytes) -> bytes:
82
+ """
83
+ SubscribeToTopic(instance: int, topic_composite_key: bytes) -> bytes
84
+
85
+ Subscribes to a topic and returns a JSON string with the result.
86
+ """
87
+ ...
88
+
89
+ def UnsubscribeFromTopic(self, instance: int, topic_composite_key: bytes) -> bytes:
90
+ """
91
+ UnsubscribeFromTopic(instance: int, topic_composite_key: bytes) -> bytes
92
+
93
+ Unsubscribe from a topic and returns a JSON string with the result.
94
+ """
95
+ ...
96
+
97
+ def MessageQueueLength(self, instance: int) -> int:
98
+ """
99
+ MessageQueueLength(instance: int) -> int
100
+
101
+ Returns the current length of the message queue.
102
+ """
103
+ ...
104
+
105
+ def PopMessages(self, instance: int) -> bytes:
106
+ """
107
+ PopNMessages(instance: int) -> bytes
108
+
109
+ Pops the first message in each channel queue and returns them as a list.
110
+ """
111
+ ...
112
+
113
+ def CloseNode(self, instance: int) -> bytes:
114
+ """
115
+ CloseNode(instance: int) -> bytes
116
+
117
+ Closes the node and frees all resources.
118
+ """
119
+ ...
120
+
121
+ def FreeString(self, arg: bytes) -> None:
122
+ """
123
+ FreeString(arg: bytes) -> None
124
+
125
+ Frees a string previously allocated by the shared library.
126
+ """
127
+ ...
128
+
129
+ def FreeInt(self, arg: int) -> None:
130
+ """
131
+ FreeInt(arg: int) -> None
132
+
133
+ Frees an integer previously allocated by the shared library.
134
+ """
135
+ ...