Overview
TheChannel trait defines the core async communication interface for OneClaw’s Layer 5. All channel implementations must implement this trait to provide unified message handling across different communication protocols.
Location: crates/oneclaw-core/src/channel/traits.rs
Channel Trait
Methods
name()
Returns the unique identifier for this channel instance.
receive()
Receives the next incoming message from this channel (async, non-blocking).
Ok(Some(IncomingMessage))- A message was receivedOk(None)- No message currently available (caller should poll again later)Err(OneClawError)- Channel error occurred
- Non-blocking: Returns immediately if no message is ready
- Async: Can be awaited efficiently without blocking the runtime
- Sequential polling: ChannelManager polls channels in order
send()
Sends an outgoing message through this channel (async).
message: &OutgoingMessage- The message to send
Ok(())- Message sent successfullyErr(OneClawError)- Send failed
Message Types
IncomingMessage
Represents a message received from an external source.source- Source identifier (e.g., “cli”, “tcp:8080”, “telegram:12345”, “mqtt:sensors/temp”)content- Text content of the messagetimestamp- UTC timestamp when the message was received
OutgoingMessage
Represents a message to be sent to an external destination.destination- Destination identifier (channel-specific routing)content- Text content of the message
Channel Lifecycle
- Construction - Channel is created and configured
- Registration - Channel is added to ChannelManager via
add_channel() - Polling Loop - ChannelManager repeatedly calls
receive()on all channels - Message Handling - First channel with a message wins (sequential polling)
- Response Routing - Responses sent back via
send()using source/destination mapping
NoopChannel
A no-operation channel that discards all sends and never receives messages.- Testing and development
- Placeholder when no channels are configured
- Disabling I/O during unit tests
Implementation Requirements
Thread Safety
Send + Sync- Channels must be safe to share across threads- Internal mutability via
Mutexor similar for state management
Non-Blocking Design
receive()should return immediately if no message is ready- Use timeouts (1-100ms) for network operations
- Buffer incoming messages for sequential delivery
Error Handling
- Return
Ok(None)for transient “no data” conditions - Return
Err()for fatal errors (connection loss, invalid config) - Log warnings for recoverable errors (dropped messages, timeouts)
Example Implementation Pattern
See Also
- ChannelManager - Multi-channel coordination
- Built-in Channels - CLI, TCP, Telegram, MQTT implementations