4.OpenClaw源码解析_路由的概念

上节课我们学习了通道的概念,openclaw能够接受来自不同通道(平台)的消息,然后统一封装成InboundMessage的格式,然后再交给run_agent_turn进行Agent的调用。今天我们来学习路由的概念,通过路由的设置,我们能够设置不同的agent来处理不同层级的消息。

1. 概念解析

1.1. 路由表( BindingTable)

首先是Binding,负责绑定入站消息和agent对应的规则,即每条规则定义了什么情况下由哪位agent响应,BingingTable在响应入站消息时,会按照tier从小到大的顺序遍历规则。

复制代码
@dataclass
class Binding:
    agent_id: str
    tier: int           # 1-5, 越小越具体
    match_key: str      # "peer_id" | "guild_id" | "account_id" | "channel" | "default"
    match_value: str    # 例如 "telegram:12345", "discord", "*"
    priority: int = 0   # 同层内, 越大越优先

参数说明:

agent_id: 目标agent的唯一id

tier: 匹配的层级,数字是1-5, 越小越优先

match_key: 匹配字段名,从peer_id, channel, default, guild_id和account_id中指定

match_value: 字段的匹配值

priority: 优先级

比如有如下绑定规则:

复制代码
bt = BindingTable()
bt.add(Binding(agent_id="luna", tier=5, match_key="default", match_value="*"))
bt.add(Binding(agent_id="sage", tier=4, match_key="channel", match_value="telegram"))
bt.add(Binding(agent_id="sage", tier=1, match_key="peer_id",
               match_value="discord:admin-001", priority=10))

则有如下解析规则:

复制代码
bt.add(Binding(agent_id="luna", tier=5, match_key="default", match_value="*"))

如果前面1~4层都没有任何匹配规则,则默认使用luna作为响应agent。

复制代码
Binding(agent_id="sage", tier=4, match_key="channel", match_value="telegram")

通道级别匹配,如果入站消息是来自telegram的,并且前1~3层级没有更加具体的规则时,使用sage作为响应agent

复制代码
Binding(agent_id="sage", tier=1, match_key="peer_id", match_value="telegram:admin-001", priority=10)

peer级别的匹配,如果入站消息是用户id为telegram:admin-001发出的,那么我们直接使用sage作为响应的Agent。

1.2. 路由解析

下面是路由解析的匹配规则,可以看到,规则是按照tier进行由小到大进行匹配的。

复制代码
def resolve(self, channel: str = "", account_id: str = "",
                guild_id: str = "", peer_id: str = "") -> tuple[str | None, Binding | None]:
    """遍历第1-5层, 第一个匹配的获胜。返回 (agent_id, matched_binding)。"""
    for b in self._bindings:
        if b.tier == 1 and b.match_key == "peer_id":
            if ":" in b.match_value:
                if b.match_value == f"{channel}:{peer_id}":
                    return b.agent_id, b
            elif b.match_value == peer_id:
                return b.agent_id, b
        elif b.tier == 2 and b.match_key == "guild_id" and b.match_value == guild_id:
            return b.agent_id, b
        elif b.tier == 3 and b.match_key == "account_id" and b.match_value == account_id:
            return b.agent_id, b
        elif b.tier == 4 and b.match_key == "channel" and b.match_value == channel:
            return b.agent_id, b
        elif b.tier == 5 and b.match_key == "default":
            return b.agent_id, b
    return None, None

1.3. 会话隔离

通过指定不同的dm_scope,达到不同粒度的私聊隔离。

其实就是创建和加载不同的session.json文件,再序列化成消息格式发送给llm

复制代码
# ---------------------------------------------------------------------------
# 会话键构建
# ---------------------------------------------------------------------------
# dm_scope 控制私聊隔离粒度:
#   main                      -> agent:{id}:main
#   per-peer                  -> agent:{id}:direct:{peer}
#   per-channel-peer          -> agent:{id}:{ch}:direct:{peer}
#   per-account-channel-peer  -> agent:{id}:{ch}:{acc}:direct:{peer}

def build_session_key(agent_id: str, channel: str = "", account_id: str = "",
                      peer_id: str = "", dm_scope: str = "per-peer") -> str:
    aid = normalize_agent_id(agent_id)
    ch = (channel or "unknown").strip().lower()
    acc = (account_id or "default").strip().lower()
    pid = (peer_id or "").strip().lower()
    if dm_scope == "per-account-channel-peer" and pid:
        return f"agent:{aid}:{ch}:{acc}:direct:{pid}"
    if dm_scope == "per-channel-peer" and pid:
        return f"agent:{aid}:{ch}:direct:{pid}"
    if dm_scope == "per-peer" and pid:
        return f"agent:{aid}:direct:{pid}"
    return f"agent:{aid}:main"

2. 流程设计

下面是代码的流程图,可以看到有两个输入,一个是cli,另一个是websocket客户端。

如果指定了force_agent,那么直接使用该agent进行回答,否则将进行路由表解析。通过查询BindingTable,获得响应的agent,然后使用build_session_key生成sk,决定不同的用户/通道的消息是否共享同一个对话历史。

相关推荐
刹那芳华199210 分钟前
循环神经网络的从零开始实现(RNN)
人工智能·rnn·深度学习
阿童木写作20 分钟前
跨境图片翻译工具多合一,批量图片视频字幕翻译加智能抠图
人工智能·python·音视频·语音识别
科技之门20 分钟前
AI3D从建模到贴图、绑骨和动画的完整流程怎么做?V2Fun完整工作流指南
人工智能·3d·贴图
宇宙第一小趴菜21 分钟前
二、机器学习的应用领域和发展史
人工智能·机器学习
前端开发江鸟27 分钟前
我写过 MCP Server,却一直以为 MCP 只有 Tool
人工智能
天国梦1 小时前
自习室智能化升级避坑指南:天学网AI智习室方案实测与选型建议
大数据·人工智能
阿里云云原生1 小时前
可用性从 99.9% 跃升至 99.995%:畅捷通如何用 AI 重塑运维底座?
运维·网络·人工智能
ZhengEnCi1 小时前
MoE(Mixture of Experts,混合专家模型)深度解析:从路由机制到专家专业化的迷思
人工智能
shxjnpl2 小时前
Qwen3-ASR 从 PyTorch 迁移到 vLLM:一次信创环境下的推理路径改造实录
人工智能·pytorch·vllm
阿童木写作2 小时前
Python实现Temu图片批量翻译自动化教程
运维·人工智能·python·自动化