AS AgentScopeAPI 手册 / API Reference

ChannelRouter

public final class ChannelRouter
源码Source io/agentscope/harness/agent/gateway/channel/ChannelRouter.java:62–222

渠道路由器,承载智能体的部署、调度与服务化运行。位于 io.agentscope.harness.agent.gateway.channel 包。核心成员包括 resolveRoute、ChannelRouter。

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)

  • explicit — InboundMessage#preferredAgentId() short-circuit when the caller has already nominated a specific agent. Bindings are still consulted to determine sessionScope and outbound addressing.

  • peer — exact Peer#key() match (e.g. "direct:u_42")

  • peer.parent — exact match on the thread-parent peer's key

  • guild + roles — guild id matches AND sender holds at least one of the binding's roles

  • guild — guild id matches (no role constraint)

  • team — team id matches

  • account — account id matches

  • channel — channel id matches

  • 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 Summary

构造器Constructor说明Description
public ChannelRouter(String globalDefaultAgentId)
初始化 `ChannelRouter` 实例。
Creates a new instance.

方法摘要Method Summary

方法Method说明Description
public RouteResult resolveRoute(ChannelConfig config, InboundMessage msg)
解析路由。
Evaluates bindings in priority order and returns a `RouteResult` ready for gateway execution.

构造器详情Constructor Detail

ChannelRouter

public ChannelRouter(String globalDefaultAgentId)

初始化 ChannelRouter 实例。

Creates a new instance.

参数Parameters
名称Name类型Type说明Description
globalDefaultAgentIdStringglobal 默认智能体标识(类型 `String`)。fallback agent id when no binding and no channel-level default match; typically the id of the agent registered via `io.agentscope.harness.agent.gateway.Gateway#bindMainAgent`
源码Source io/agentscope/harness/agent/gateway/channel/ChannelRouter.java:71–73

方法详情Method Detail

resolveRoute

public RouteResult resolveRoute(ChannelConfig config, InboundMessage msg)

解析路由。

Evaluates bindings in priority order and returns a RouteResult ready for gateway execution.

参数Parameters
名称Name类型Type说明Description
configChannelConfig配置对象(类型 `ChannelConfig`)。channel-level routing config (bindings + dmScope + channel default agent)
msgInboundMessage消息(类型 `InboundMessage`)。normalized inbound message
返回Returns RouteResult
返回RouteResult。Returns route result.
源码Source io/agentscope/harness/agent/gateway/channel/ChannelRouter.java:82–118
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.