π΄ unaiverse.networking.p2p.messages
What this module does π΄
Defines the Msg wrapper that builds, serializes and parses protobuf-backed P2P messages including JSON, stream-sample and stats-update payloads.
messages
¶
βββββ βββββ ββββββ βββββ βββββ βββββ βββββ ββββββββββ βββββββββββ βββββββββ ββββββββββ
βββββ βββββ ββββββββ βββββ βββββ βββββ βββββ ββββββββββββββββββββββββ ββββββββββββββββββββββ
ββββ ββββ ββββββββ ββββ ββββββ ββββ ββββ ββββ ββββ β β ββββ ββββ ββββ βββ ββββ β β
ββββ ββββ βββββββββββββ ββββββββ ββββ ββββ ββββ βββββββ βββββββββββ βββββββββββ βββββββ
ββββ ββββ ββββ ββββββββ βββββββ ββββ βββββ βββ βββββββ ββββββββββββ βββββββββββ βββββββ
ββββ ββββ ββββ βββββββ ββββββββ ββββ βββββββββ ββββ β β ββββ ββββ βββ ββββ ββββ β β
ββββββββββ βββββ βββββββββββββββββ βββββ βββββ ββββββββββ βββββ ββββββββββββββββ ββββββββββ
ββββββββ βββββ βββββ ββββββββ βββββ βββ ββββββββββ βββββ βββββ βββββββββ ββββββββββ
A Collectionless AI Project (https://collectionless.ai)
Registration/Login: https://unaiverse.io
Code Repositories: https://github.com/collectionlessai/
Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
Msg
¶
Msg(sender: str | None = None, content: any = None, timestamp_net: str | None = None, channel: str | None = None, content_type: str = MISC, piggyback: str | None = None, _proto_msg: Message = None)
A structured network message exchanged between peers in the UNaIVERSE network.
Msg is the fundamental unit of communication between agents, worlds, and any other
nodes in the system. It wraps a Protobuf Message (defined in message_pb2.py)
and exposes a clean Python API for constructing, reading, and serializing messages
without requiring callers to interact with Protobuf directly.
Each message carries:
- a
senderpeer ID, - a
content_typethat identifies the message's purpose (one ofCONTENT_TYPES), - a
channelstring identifying the logical communication channel, - an optional
piggybackfield for lightweight out-of-band data, - a
timestamp_netrecording when the message was created, - a
contentpayload whose encoding depends oncontent_type:STREAM_SAMPLEmessages pack tensors, images, text, or file data into a typed Protobuf sub-message;STATS_UPDATEmessages use a dedicatedStatBatchsub-message; all other types serialize their payload as a JSON string.
Instances are typically created by passing keyword arguments to the constructor.
Received byte streams are parsed with from_bytes. The content property decodes
the payload on first access and caches the result.
Note
The class-level constants (PROFILE, WORLD_APPROVAL, etc.) are the
canonical string values for content_type. Always use these constants rather
than raw strings to avoid typos and to benefit from IDE completion.
Initialize a new Msg from field values, or wrap an existing Protobuf message.
Two usage modes are supported:
-
New message: supply
senderand any combination of the other keyword arguments. The constructor validates the inputs, builds the internal Protobuf object, and routescontentto the appropriate encoder depending oncontent_type. Iftimestamp_netisNone, the current UTC time is used. IfchannelisNone, the channel is set to"<unknown>". -
Parsed message: supply only
_proto_msgwith the Protobuf object already populated (typically byfrom_bytes). All other arguments must beNoneor the defaultMISCvalue; mixing them raisesValueError.
When content is None or the sentinel "<empty>", no payload field is
set in the Protobuf oneof and Msg.content will return "<empty>". For
STREAM_SAMPLE messages the payload is encoded via _build_stream_sample_content;
for STATS_UPDATE messages via _build_stats_update_content; all other types
use the generic JSON encoder _build_json_content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sender
|
str | None
|
The peer ID string of the message originator. Must be provided and
must be a |
None
|
content
|
any
|
The payload to encode. The expected type depends on |
None
|
timestamp_net
|
str | None
|
An ISO-like UTC timestamp string for the message. If None, the current UTC time is used. Defaults to None. |
None
|
channel
|
str | None
|
The logical channel identifier for routing. If None, defaults to
|
None
|
content_type
|
str
|
One of the string constants defined in |
MISC
|
piggyback
|
str | None
|
An optional string carrying lightweight out-of-band data alongside the main payload. Defaults to None (stored as empty string). |
None
|
_proto_msg
|
Message
|
A pre-built |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
AssertionError
|
If |
Source code in unaiverse/networking/p2p/messages.py
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 | |
CONSOLE_AND_BEHAV_STATUS
class-attribute
instance-attribute
¶
CONTENT_TYPES
class-attribute
instance-attribute
¶
CONTENT_TYPES = {PROFILE, WORLD_APPROVAL, AGENT_APPROVAL, PROFILE_REQUEST, ADDRESS_UPDATE, STREAM_SAMPLE, ROLE_SUGGESTION, HSM, MISC, GET_CV_FROM_ROOT, BADGE_SUGGESTIONS, INSPECT_ON, INSPECT_CMD, WORLD_AGENTS_LIST, CONSOLE_AND_BEHAV_STATUS, STATS_UPDATE, STATS_REQUEST, STATS_RESPONSE, INTERACTION, INTERACTION_STATUS}
sender
property
writable
¶
Return the peer ID of the message originator.
Reads directly from the underlying Protobuf field. The value is always a
non-None string; an empty string indicates the sender was not set.
Returns:
| Type | Description |
|---|---|
|
The sender peer ID string stored in the Protobuf message. |
content_type
property
writable
¶
Return the content type identifier of the message.
The value is one of the string constants defined on Msg (for example
Msg.PROFILE, Msg.STREAM_SAMPLE, Msg.STATS_UPDATE). It controls how
the content property decodes the payload and how receiving code dispatches
the message.
Returns:
| Type | Description |
|---|---|
|
The content type string stored in the Protobuf message. |
channel
property
writable
¶
Return the logical channel identifier of the message.
The channel string is used by routing code to identify which logical
communication channel a message belongs to. A value of "<unknown>"
indicates no channel was specified at construction time.
Returns:
| Type | Description |
|---|---|
|
The channel string stored in the Protobuf message. |
piggyback
property
writable
¶
Return the piggyback field of the message.
The piggyback field carries a small auxiliary string alongside the main payload. It is intended for lightweight out-of-band data that does not warrant a separate message. An empty string indicates no piggyback data was attached.
Returns:
| Type | Description |
|---|---|
|
The piggyback string stored in the Protobuf message. |
timestamp_net
property
writable
¶
Return the network timestamp of the message.
The timestamp records when the message was created, formatted as a UTC
string ("%Y-%m-%d %H:%M:%S.%f"). If not set explicitly at construction
time, it is populated automatically with the current UTC time.
Returns:
| Type | Description |
|---|---|
|
The timestamp string stored in the Protobuf message. |
content
property
¶
Return the decoded payload of the message, with result caching.
On the first access the Protobuf oneof field is inspected to determine the
payload kind, and the appropriate parser is called:
"stream_sample"->_parse_stream_sample_content, returns adictmapping stream names to sample dicts (each with"data","data_tag", and"data_uuid"keys)."stats_update"->_parse_stats_update_content, returns alistof stat dicts (each with"group_key","stat_name","timestamp", and"value"keys)."json_content"->_parse_json_content, returns the JSON-decoded object (typically adict).- No payload set -> returns the sentinel string
"<empty>".
The decoded result is cached in _decoded_content so subsequent accesses are
free. The cache is never invalidated; if the Protobuf message is mutated after
the first access, the cached value will be stale.
Returns:
| Type | Description |
|---|---|
any
|
The decoded payload. The type depends on |
any
|
most message types, a |
any
|
|
to_bytes
¶
Serialize the internal Protobuf message to a byte string.
Delegates directly to SerializeToString on the underlying pb.Message
object. The resulting bytes can be transmitted over the network and later
reconstructed with from_bytes.
Returns:
| Type | Description |
|---|---|
bytes
|
The binary representation of the Protobuf message as a |
Source code in unaiverse/networking/p2p/messages.py
from_bytes
classmethod
¶
from_bytes(msg_bytes: bytes) -> Msg
Deserialize a byte string into a new Msg instance.
Parses msg_bytes as a Protobuf Message and wraps it in a new Msg
without performing any additional decoding. The content property will decode
the payload lazily on first access. This is the inverse of to_bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg_bytes
|
bytes
|
The raw bytes produced by a previous call to |
required |
Returns:
| Type | Description |
|---|---|
Msg
|
A new |
Raises:
| Type | Description |
|---|---|
DecodeError
|
If |