Class ChannelRouter

java.lang.Object
io.agentscope.harness.agent.gateway.channel.ChannelRouter

public final class ChannelRouter extends Object
Resolves the target agentId and stable MsgContext for an InboundMessage by evaluating ChannelBinding rules in deterministic priority order.

Binding evaluation tiers (highest to lowest priority)

  1. explicit — InboundMessage.preferredAgentId() short-circuit when the caller has already nominated a specific agent. Bindings are still consulted to determine sessionScope and outbound addressing.
  2. peer — exact Peer.key() match (e.g. "direct:u_42")
  3. peer.parent — exact match on the thread-parent peer's key
  4. guild + roles — guild id matches AND sender holds at least one of the binding's roles
  5. guild — guild id matches (no role constraint)
  6. team — team id matches
  7. account — account id matches
  8. channel — channel id matches
  9. default — ChannelConfig.defaultAgentId() or globalDefaultAgentId

Within each tier the first binding in ChannelConfig.bindings() list order that matches wins.

Session key construction (MsgContext)

After resolving agentId, the router builds a MsgContext whose MsgContext.canonicalKey() produces a stable session key:
  • DM + DmScope.MAIN — channel field only; all DMs share one session
  • DM + other scopes — room = peerId (optionally group = accountId)
  • Thread — room = parentPeer id, threadId = peer id
  • Non-DM channel/group — room = peer id, group = guild
The resolved agentId is always included in MsgContext.extra() under key "agentId" so that the gateway can pick the correct agent from its registry.
  • Constructor Details

  • Method Details

    • resolveRoute

      public RouteResult resolveRoute(ChannelConfig config, InboundMessage msg)
      Evaluates bindings in priority order and returns a RouteResult ready for gateway execution.
      Parameters:
      config - channel-level routing config (bindings + dmScope + channel default agent)
      msg - normalized inbound message