Skip to main content
The Channel Router and Inbound Pipeline constitute the entry point for all external messaging traffic into the Palyra daemon. This subsystem is responsible for normalizing messages from diverse platforms (Discord, Slack, etc.), applying routing policies, managing conversation state, and coalescing rapid-fire messages into logical “turns” for the agent loop.

Inbound Message Lifecycle

The inbound pipeline follows a strict progression from raw connector event to a dispatched agent run.

1. Admission and Matching

When a connector (e.g., Discord) receives a message, it is passed to the ChannelRouter::route_inbound function crates/palyra-daemon/src/channel_router.rs#1-10. The router evaluates the message against ChannelRoutingRule sets crates/palyra-daemon/src/channel_router.rs#195-210.

2. Session Resolution

Once admitted, the router determines the session_id. If isolate_session_by_sender is true, a unique session is created per user; otherwise, the conversation ID from the platform is used crates/palyra-daemon/src/channel_router.rs#204.

3. Inbound Coalescing

To prevent “message storms” from triggering excessive runs, the pipeline uses an InboundCoalescer (logic managed within the gateway runtime). This component merges multiple rapid-fire messages from the same conversation into a single buffer before dispatching a “Channel Turn.”

4. Dispatch

Accepted messages result in a RouteInboundResult crates/palyra-connectors/tests/simulator_harness.rs#53-77. This result contains the route_key and session_id required by the GatewayRuntime to initiate an agent run. Inbound Data Flow Diagram Sources: crates/palyra-daemon/src/channel_router.rs#1-210, crates/palyra-connectors/tests/simulator_harness.rs#47-78, crates/palyra-daemon/src/channels.rs#1-175

Channel Router Implementation

The ChannelRouter is a policy-driven engine that manages in-memory state for routing decisions, including rate limiting and quarantine for “poison” messages crates/palyra-daemon/src/channel_router.rs#1-10.

Key Structures

Class/StructResponsibility
ChannelRouterPrimary entry point for routing logic and policy enforcement.
ChannelRoutingRuleDefines how a specific channel behaves (mentions, targets, etc.).
DirectMessagePolicyEnum defining Deny, Pairing, or Allow for DMs.
BroadcastStrategyControls how fan-out requests are handled per channel.

Direct Message Pairing

For channels using DirectMessagePolicy::Pairing, the router manages a “handshake” flow:
  1. Code Generation: An operator generates a PairingCodeRecord crates/palyra-daemon/src/channel_router.rs#107-113.
  2. Consumption: The user sends the code via DM. The router creates a PairingPendingRecord crates/palyra-daemon/src/channel_router.rs#118-125.
  3. Approval: An operator approves the request, creating a PairingGrantRecord crates/palyra-daemon/src/channel_router.rs#129-135.
Code Entity Mapping: Routing and Pairing Sources: crates/palyra-daemon/src/channel_router.rs#38-144, crates/palyra-daemon/src/channel_router.rs#195-210

Outbound Lifecycle

Outbound messages follow a “Delivery Intent” pattern to ensure reliability across unstable network conditions.
  1. Enqueue: The agent or a tool requests an outbound send. This is captured as an OutboundMessageRequest crates/palyra-daemon/src/channels/discord.rs#150-165.
  2. Persistence: The request is stored in the outbox via the ConnectorSupervisor crates/palyra-daemon/src/channels/discord.rs#166.
  3. Drain: A background worker or immediate trigger calls drain_due_outbox_for_connector crates/palyra-daemon/src/channels/discord.rs#168-173.
  4. Delivery: The specific adapter (e.g., Discord) executes the HTTPS/WebSocket request.
  5. Receipt: If successful, a native_message_id is returned and associated with the internal envelope_id crates/palyra-daemon/src/channels/discord.rs#174-177.

Outbound Mutation and Approval

Mutations (Edit, Delete, Reactions) are subject to ApprovalRiskLevel checks crates/palyra-daemon/src/channels/discord.rs#15. Outbound Delivery Flow Sources: crates/palyra-daemon/src/channels/discord.rs#150-188, crates/palyra-connectors/tests/simulator_harness.rs#129-170, crates/palyra-daemon/src/channels.rs#27-31

Conversation Bindings

Palyra maintains a mapping between external platform entities and internal agent constructs.

Implementation Reference

FeatureCode EntityFile Path
Routing EntryChannelRouter::route_inboundcrates/palyra-daemon/src/channel_router.rs#1-10
Platform FacadeChannelPlatformcrates/palyra-daemon/src/channels.rs#169-174
Outbound Sendsubmit_discord_test_sendcrates/palyra-daemon/src/channels/discord.rs#99-103
Mutation RiskApprovalRiskLevelcrates/palyra-daemon/src/channels/discord.rs#15
Admin APIadmin_channels_list_handlercrates/palyra-daemon/src/transport/http/handlers/admin/channels/mod.rs#26-30
Sources: crates/palyra-daemon/src/channel_router.rs#1-210, crates/palyra-daemon/src/channels.rs#1-175, crates/palyra-daemon/src/channels/discord.rs#1-188