# SPDX-License-Identifier: MulanPSL-2.0
"""Python dataclass mirrors of atlas.proto types.
Keeps the rest of robonix_api free of raw protobuf access. Frozen +
slotted to keep the runtime cost negligible.
"""
from __future__ import annotations
from copy import deepcopy
from dataclasses import dataclass, field
from enum import IntEnum
from typing import Any
[docs]
class Transport(IntEnum):
UNSPECIFIED = 0
GRPC = 1
ROS2 = 2
MCP = 3
[docs]
class Kind(IntEnum):
"""Closed set of CapabilityProvider kinds. Internal: developers
interact with the concrete `Primitive` / `Service` / `Skill`
classes, not with this enum directly."""
UNSPECIFIED = 0
PRIMITIVE = 1
SERVICE = 2
SKILL = 3
[docs]
class LifecycleState(IntEnum):
UNSPECIFIED = 0
REGISTERED = 1
INACTIVE = 2
ACTIVE = 3
ERROR = 4
TERMINATED = 5
[docs]
@dataclass(frozen=True, slots=True)
class GrpcParams:
proto_file: str = "robonix_contracts.proto"
service_name: str = ""
method: str = ""
[docs]
@dataclass(frozen=True, slots=True)
class Ros2Params:
qos_profile: str = ""
[docs]
@dataclass(frozen=True, slots=True)
class McpParams:
input_schema_json: str = "{}"
[docs]
@dataclass(frozen=True, slots=True)
class CapabilityTimeDescriptor:
"""Static binding between one sensor capability and its clock domain."""
domain_id: str
timestamp_semantics: str
is_hardware_timestamped: bool = False
pipeline_delay_ns: int | None = None
pipeline_uncertainty_ns: int = 0
supports_per_point_timestamp: bool = False
[docs]
@dataclass(frozen=True, slots=True)
class FieldSpec:
name: str
type_name: str
is_primitive: bool = False
is_array: bool = False
array_size: int = 0 # 0 == unbounded
[docs]
@dataclass(frozen=True, slots=True)
class Capability:
"""One declared Capability on a CapabilityProvider, mirrored from
`pb::Capability`. Carries provider_id / provider_kind so consumers can
flatten without rebuilding the relationship from outer providers.
`endpoint` is omitted on purpose (see ConnectCapability)."""
provider_id: str
provider_kind: Kind
contract_id: str
transport: Transport
description: str = ""
namespace_mismatch: bool = False
params: GrpcParams | Ros2Params | McpParams | None = None
time_descriptor: CapabilityTimeDescriptor | None = None
[docs]
@dataclass(frozen=True, slots=True)
class CapabilityProvider:
"""One registered Primitive / Service / Skill and the Capabilities
it currently offers. Mirrored from `pb::CapabilityProvider`."""
id: str
kind: Kind
namespace: str
capability_md_path: str = ""
# CAPABILITY.md content as registered (portable; the path is debug
# provenance only). Empty when the provider shipped no markdown.
capability_md: str = ""
last_heartbeat_ms: int = 0
state: LifecycleState = LifecycleState.UNSPECIFIED
state_detail: str = ""
# Opaque nonce replaced on each successful Atlas Register generation.
registration_id: str = ""
capabilities: tuple[Capability, ...] = ()
[docs]
@dataclass(frozen=True, slots=True)
class ContractDescriptor:
id: str
version: str = ""
kind: Kind = Kind.UNSPECIFIED
mode: str = ""
io_msg_type: str = ""
io_srv_type: str = ""
source_toml_path: str = ""
description: str = ""
cross_namespace: bool = False
llm_callable: bool = True
msg_fields: tuple[FieldSpec, ...] = ()
srv_request_fields: tuple[FieldSpec, ...] = ()
srv_response_fields: tuple[FieldSpec, ...] = ()
[docs]
@dataclass
class Channel:
"""Atlas allocation with optional execution ownership from `connect_channel`.
`connect_capability` preserves endpoint-only behavior. Execution-owning
channels confirm termination before releasing their allocation.
"""
provider_id: str
contract_id: str
transport: Transport
endpoint: str
channel_id: str
params: GrpcParams | Ros2Params | McpParams | None = None
time_descriptor: CapabilityTimeDescriptor | None = None
_closer: Any = field(default=None, repr=False)
_closed: bool = field(default=False, repr=False)
_execution: Any = field(default=None, repr=False, compare=False)
@property
def run_id(self) -> str | None:
return self._execution.run_id if self._execution else None
@property
def raw_response(self) -> dict | None:
"""Decoded business response; protobuf responses include scalar defaults."""
if self._execution is None:
return None
with self._execution.lock:
return deepcopy(self._execution.raw_response)
[docs]
def call(self, request, *, timeout: float | None = None):
return self._managed().call(request, timeout=timeout)
[docs]
def publish(self, message) -> None:
self._managed().publish(message)
[docs]
def subscribe(self, callback) -> None:
self._managed().subscribe(callback)
[docs]
def status(self, *, timeout: float = 30.0):
return self._managed().companion("status", timeout=timeout)
[docs]
def cancel(self, *, timeout: float = 30.0):
return self._managed().companion("cancel", timeout=timeout)
def _managed(self):
if self._execution is None:
raise RuntimeError("Use connect_channel() for capability execution")
return self._execution
def __enter__(self) -> "Channel":
return self
def __exit__(self, *exc) -> None:
self.close()
[docs]
def close(self, *, timeout: float = 5.0) -> None:
if self._closed:
return
if self._execution is not None:
self._execution.close(timeout=timeout)
self._closed = True
return
object.__setattr__(self, "_closed", True)
if self._closer is not None:
try:
self._closer(self.channel_id)
except Exception: # noqa: BLE001 -- closing is best-effort
pass
# -- pb -> dataclass converters --------------------------------------------
# Kept out of the dataclass bodies so atlas_types.py has no proto import
# dependency at module-load time.
[docs]
def from_pb_field_spec(pb_f) -> FieldSpec:
return FieldSpec(
name=pb_f.name,
type_name=pb_f.type_name,
is_primitive=pb_f.is_primitive,
is_array=pb_f.is_array,
array_size=int(pb_f.array_size),
)
[docs]
def from_pb_params(
transport: Transport, pb_params
) -> GrpcParams | Ros2Params | McpParams | None:
if pb_params is None:
return None
if transport == Transport.GRPC and pb_params.HasField("grpc"):
g = pb_params.grpc
return GrpcParams(
proto_file=g.proto_file or "robonix_contracts.proto",
service_name=g.service_name,
method=g.method,
)
if transport == Transport.ROS2 and pb_params.HasField("ros2"):
return Ros2Params(qos_profile=pb_params.ros2.qos_profile)
if transport == Transport.MCP and pb_params.HasField("mcp"):
return McpParams(input_schema_json=pb_params.mcp.input_schema_json or "{}")
return None
[docs]
def from_pb_capability(pb_cap) -> Capability:
transport = Transport(pb_cap.transport)
return Capability(
provider_id=pb_cap.provider_id,
provider_kind=Kind(pb_cap.provider_kind),
contract_id=pb_cap.contract_id,
transport=transport,
description=pb_cap.description,
namespace_mismatch=getattr(pb_cap, "namespace_mismatch", False),
params=from_pb_params(transport, pb_cap.params),
time_descriptor=from_pb_time_descriptor(
pb_cap.time_descriptor
if hasattr(pb_cap, "time_descriptor")
and pb_cap.HasField("time_descriptor")
else None
),
)
[docs]
def from_pb_time_descriptor(pb_time) -> CapabilityTimeDescriptor | None:
"""Convert optional timing metadata without requiring a new Atlas stub."""
if pb_time is None:
return None
has_delay = False
try:
has_delay = pb_time.HasField("pipeline_delay_ns")
except (ValueError, AttributeError):
has_delay = False
return CapabilityTimeDescriptor(
domain_id=pb_time.domain_id,
timestamp_semantics=pb_time.timestamp_semantics,
is_hardware_timestamped=pb_time.is_hardware_timestamped,
pipeline_delay_ns=pb_time.pipeline_delay_ns if has_delay else None,
pipeline_uncertainty_ns=int(pb_time.pipeline_uncertainty_ns),
supports_per_point_timestamp=pb_time.supports_per_point_timestamp,
)
[docs]
def from_pb_provider(pb_rec) -> CapabilityProvider:
return CapabilityProvider(
id=pb_rec.id,
kind=Kind(pb_rec.kind),
namespace=pb_rec.namespace,
capability_md_path=pb_rec.capability_md_path,
# `getattr` guard: a package built against an older atlas.proto ships
# a stale atlas_pb2 whose CapabilityProvider has no `capability_md`
# field. Atlas still sends it on the wire (forward-compatible), but
# accessing the attribute on the stale class raises AttributeError.
# Degrade to "" so mixed-version packages can still read atlas.
capability_md=getattr(pb_rec, "capability_md", ""),
last_heartbeat_ms=int(pb_rec.last_heartbeat_ms),
state=LifecycleState(pb_rec.state),
state_detail=pb_rec.state_detail,
registration_id=getattr(pb_rec, "registration_id", ""),
capabilities=tuple(from_pb_capability(c) for c in pb_rec.capabilities),
)
[docs]
def from_pb_contract(pb_c) -> ContractDescriptor:
# Proto presence matters here: an older Atlas omits this field and must
# retain the historical behavior in which contracts are model-visible.
try:
llm_callable = bool(pb_c.llm_callable) if pb_c.HasField("llm_callable") else True
except (AttributeError, ValueError):
llm_callable = True
return ContractDescriptor(
id=pb_c.id,
version=pb_c.version,
kind=Kind(pb_c.kind),
mode=pb_c.mode,
io_msg_type=pb_c.io_msg_type,
io_srv_type=pb_c.io_srv_type,
source_toml_path=pb_c.source_toml_path,
description=pb_c.description,
cross_namespace=getattr(pb_c, "cross_namespace", False),
llm_callable=llm_callable,
msg_fields=tuple(from_pb_field_spec(f) for f in pb_c.msg_fields),
srv_request_fields=tuple(
from_pb_field_spec(f) for f in pb_c.srv_request_fields
),
srv_response_fields=tuple(
from_pb_field_spec(f) for f in pb_c.srv_response_fields
),
)