Skip to content

Commit b77ebfb

Browse files
committed
thread routing
1 parent 2323cd5 commit b77ebfb

4 files changed

Lines changed: 279 additions & 31 deletions

File tree

crates/openfang-api/src/channel_bridge.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,11 +1166,12 @@ pub async fn start_channel_bridge_with_config(
11661166
if let Some(ref tg_config) = config.telegram {
11671167
if let Some(token) = read_token(&tg_config.bot_token_env, "Telegram") {
11681168
let poll_interval = Duration::from_secs(tg_config.poll_interval_secs);
1169-
let adapter = Arc::new(TelegramAdapter::new(
1169+
let adapter = Arc::new(TelegramAdapter::with_thread_routes(
11701170
token,
11711171
tg_config.allowed_users.clone(),
11721172
poll_interval,
11731173
tg_config.api_url.clone(),
1174+
tg_config.thread_routes.clone(),
11741175
));
11751176
adapters.push((adapter, tg_config.default_agent.clone()));
11761177
}

crates/openfang-channels/src/bridge.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,13 +1191,33 @@ async fn dispatch_message(
11911191
// Use resolve_with_context so channel_id-scoped (and guild_id-scoped)
11921192
// bindings can route per channel — see binding_context_for() for the
11931193
// single-source-of-truth allowlist.
1194+
//
1195+
// Issue #780: when the adapter stamped a per-thread target agent in
1196+
// metadata (e.g. Telegram forum-topic routing via `thread_routes`), prefer
1197+
// it over the standard router so operators can scope topics to specific
1198+
// agents from config.toml.
1199+
let target_agent_name = message
1200+
.metadata
1201+
.get("target_agent_name")
1202+
.and_then(|v| v.as_str());
1203+
let routed_by_name = if let Some(name) = target_agent_name {
1204+
match handle.find_agent_by_name(name).await {
1205+
Ok(Some(id)) => Some(id),
1206+
_ => None,
1207+
}
1208+
} else {
1209+
None
1210+
};
1211+
11941212
let binding_ctx = binding_context_for(message);
1195-
let agent_id = router.resolve_with_context(
1196-
&message.channel,
1197-
sender_user_id(message),
1198-
message.sender.openfang_user.as_deref(),
1199-
&binding_ctx,
1200-
);
1213+
let agent_id = routed_by_name.or_else(|| {
1214+
router.resolve_with_context(
1215+
&message.channel,
1216+
sender_user_id(message),
1217+
message.sender.openfang_user.as_deref(),
1218+
&binding_ctx,
1219+
)
1220+
});
12011221

12021222
let agent_id = match agent_id {
12031223
Some(id) => id,

0 commit comments

Comments
 (0)