ilink-hub 0.2.8

iLink-compatible multiplexer hub for WeChat ClawBot — route one WeChat account to multiple AI agent backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
//! Hub command handling: the `/list`, `/use`, `/status`, `/help`, `/session …`
//! and broadcast commands the user can send as WeChat messages.

use std::sync::atomic::Ordering;
use std::sync::Arc;
use tracing::{debug, error, warn};

use crate::ilink::types::{SendMessageRequest, WeixinMessage};

// Hub-internal items (HubState, HubCommand, the `messages`/`quote_route` modules,
// …) plus the dispatch helpers via `super::dispatch::*`.
use super::*;

/// Resolve the vctx and currently routed vtoken for a Hub command from a given user.
/// Returns `None` if no backend is selected (broadcasts a NO_BACKEND message via the caller).
async fn resolve_vctx_and_vtoken(
    state: &HubState,
    real_ctx: &str,
    from_user_id: &str,
    group_id: Option<&str>,
) -> (String, Option<String>) {
    let vctx =
        super::dispatch::resolve_vctx_for_message(state, real_ctx, from_user_id, group_id, None)
            .await;
    let vtoken = state
        .routing
        .router
        .lock()
        .await
        .get_route(from_user_id)
        .map(str::to_string);
    (vctx, vtoken)
}

pub(super) async fn handle_hub_command(state: Arc<HubState>, msg: WeixinMessage, cmd: HubCommand) {
    let real_ctx = match msg.context_token.clone() {
        Some(ctx) if !ctx.is_empty() => ctx,
        _ => {
            warn!(
                ?cmd,
                "hub command message has no context_token, cannot reply"
            );
            return;
        }
    };
    let from_user_id = msg.from_user_id.as_deref().unwrap_or_default().to_string();
    debug!(?cmd, from_user_id, context_token = %real_ctx, "handling hub command");

    let reply_text = match cmd {
        HubCommand::List => handle_cmd_list(&state, &from_user_id).await,
        HubCommand::UseClient(ref name) => handle_cmd_use(&state, &from_user_id, name).await,
        HubCommand::Broadcast(ref text) => {
            handle_cmd_broadcast(&state, &from_user_id, &real_ctx, &msg, text).await
        }
        HubCommand::Status => handle_cmd_status(&state).await,
        HubCommand::Help => handle_cmd_help(),
        HubCommand::SessionList => {
            handle_cmd_session_list(&state, &from_user_id, &real_ctx, msg.group_id.as_deref()).await
        }
        HubCommand::SessionNew(ref session_name, ref initial_uuid) => {
            handle_cmd_session_new(
                &state,
                &from_user_id,
                &real_ctx,
                msg.group_id.as_deref(),
                session_name,
                initial_uuid,
            )
            .await
        }
        HubCommand::SessionUse(ref session_name) => {
            handle_cmd_session_use(
                &state,
                &from_user_id,
                &real_ctx,
                msg.group_id.as_deref(),
                session_name,
            )
            .await
        }
        HubCommand::SessionDelete(ref session_name) => {
            handle_cmd_session_delete(
                &state,
                &from_user_id,
                &real_ctx,
                msg.group_id.as_deref(),
                session_name,
            )
            .await
        }
    };

    debug!(to = %from_user_id, "sending hub command reply");
    let mut send_req = SendMessageRequest::reply(real_ctx, reply_text, &from_user_id);
    if let Some(m) = &mut send_req.msg {
        m.ensure_outbound();
        let index_hub_quote = matches!(
            &cmd,
            HubCommand::List
                | HubCommand::Status
                | HubCommand::Help
                | HubCommand::UseClient(_)
                | HubCommand::SessionList
                | HubCommand::SessionNew(_, _)
                | HubCommand::SessionUse(_)
                | HubCommand::SessionDelete(_)
        );
        if index_hub_quote {
            // Content path: works even though iLink never echoes the bot copy back.
            if let Some(text) = m.text().map(str::to_string) {
                state
                    .routing
                    .quote_index
                    .lock()
                    .await
                    .register_outbound_content(
                        &from_user_id,
                        &text,
                        quote_route::QuoteOrigin::Hub { cmd: cmd.clone() },
                    );
            }
        }
    }
    match state.ilink.upstream.send_message(send_req).await {
        Err(e) => error!(error = %e, "failed to send hub command reply"),
        Ok(resp) if resp.ret.map(|r| r != 0).unwrap_or(false) => {
            error!(ret = resp.ret, errmsg = ?resp.errmsg, "iLink rejected hub command reply");
        }
        Ok(_) => {
            debug!(?cmd, "hub command reply sent successfully");
        }
    }
}

pub(super) async fn handle_cmd_list(state: &HubState, from_user_id: &str) -> String {
    let registry = state.clients.registry.read().await;
    let mut clients = registry.all_clients();
    // Sort by name so the 1-based index shown here matches `get_by_alias`.
    clients.sort_by(|a, b| a.name.cmp(&b.name));
    if clients.is_empty() {
        "尚未注册任何后端客户端。".to_string()
    } else {
        let active_vtoken = {
            let router = state.routing.router.lock().await;
            router.get_route(from_user_id).map(str::to_string)
        };
        let active_name = active_vtoken.as_deref().and_then(|vt| {
            clients
                .iter()
                .find(|c| c.vtoken == vt)
                .map(|c| c.name.as_str())
        });
        let mut lines = vec!["**已注册的后端:**".to_string()];
        for (i, c) in clients.iter().enumerate() {
            let status = if c.online { "🟢" } else { "🔴" };
            let label = c.label.as_deref().unwrap_or(&c.name);
            let selected = if active_name == Some(c.name.as_str()) {
                ""
            } else {
                ""
            };
            lines.push(format!(
                "{} {}. `{}`{}{}",
                status,
                i + 1,
                c.name,
                selected,
                label
            ));
        }
        match active_name {
            Some(name) => lines.push(format!("\n当前选中:`{}`", name)),
            None => lines.push("\n当前未选中(广播模式)".to_string()),
        }
        lines.push(
            "用 `/use <名称或序号>`(或 `/u <名称或序号>`)切换后端,或发送 `@<名称或序号> <消息>` 直接发起临时会话。"
                .to_string(),
        );
        lines.join("\n")
    }
}

pub(super) async fn handle_cmd_use(state: &HubState, from_user_id: &str, name: &str) -> String {
    let registry = state.clients.registry.read().await;
    if let Some(client) = registry.get_by_alias(name) {
        let vtoken = client.vtoken.clone();
        let resolved_name = client.name.clone();
        drop(registry);

        if let Err(e) = state.store.set_route(from_user_id, &vtoken).await {
            warn!(error = %e, "failed to persist route to DB");
            format!(
                "⚠️ 切换到 `{}` 失败(数据库写入错误),请重试",
                resolved_name
            )
        } else {
            let mut router = state.routing.router.lock().await;
            router.set_route(from_user_id, vtoken.clone());
            format!("✅ 已切换到 `{}`", resolved_name)
        }
    } else {
        format!(
            "❌ 未找到名为 `{}` 的后端。用 `/list`(或 `/ls`)查看可用后端(支持用序号,如 `/use 1`)。",
            name
        )
    }
}

pub(super) async fn handle_cmd_broadcast(
    state: &HubState,
    from_user_id: &str,
    real_ctx: &str,
    msg: &WeixinMessage,
    text: &str,
) -> String {
    let online = {
        let registry = state.clients.registry.read().await;
        registry
            .online_clients()
            .iter()
            .map(|c| c.vtoken.clone())
            .collect::<Vec<_>>()
    };
    for vtoken in &online {
        let vctx = super::dispatch::resolve_vctx_for_message(
            state,
            real_ctx,
            from_user_id,
            msg.group_id.as_deref(),
            None,
        )
        .await;
        let mut m = msg.clone();
        let hub_ext =
            super::dispatch::build_hub_ext_for_vctx(&state.store, &vctx, vtoken, None).await;
        m.context_token = Some(vctx.clone());
        m.ilink_hub_ext = hub_ext;
        if let Some(items) = &mut m.item_list {
            let items_mut = std::sync::Arc::make_mut(items);
            if let Some(first) = items_mut.first_mut() {
                if let Some(ti) = &mut first.text_item {
                    ti.text = Some(text.to_string());
                }
            }
        }
        match state.clients.queue.push(vtoken, m).await {
            Ok(false) => {
                state
                    .metrics
                    .messages_dispatched
                    .fetch_add(1, Ordering::Relaxed);
            }
            Ok(true) => {
                state
                    .metrics
                    .messages_dropped
                    .fetch_add(1, Ordering::Relaxed);
            }
            Err(e) => {
                error!(error = %e, vtoken = %crate::redact_token(vtoken), "failed to push hub broadcast message");
                state
                    .metrics
                    .messages_dropped
                    .fetch_add(1, Ordering::Relaxed);
            }
        }
    }
    format!("📡 Broadcast to {} client(s)", online.len())
}

pub(super) async fn handle_cmd_status(state: &HubState) -> String {
    let (online, total, online_clients) = {
        let registry = state.clients.registry.read().await;
        let all = registry.all_clients();
        let online = registry.online_clients().len();
        let total = all.len();
        let online_clients: Vec<(String, String)> = all
            .iter()
            .filter(|c| c.online)
            .map(|c| (c.name.clone(), c.vtoken.clone()))
            .collect();
        (online, total, online_clients)
    };
    let vtokens: Vec<String> = online_clients.iter().map(|(_, vt)| vt.clone()).collect();
    let all_sessions_map = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        state.store.get_all_session_entries_per_vtoken(&vtokens),
    )
    .await
    .unwrap_or_else(|_| {
        warn!("get_all_session_entries_per_vtoken timed out in /status");
        Ok(std::collections::HashMap::new())
    })
    .unwrap_or_default();
    let client_sessions: Vec<(String, Vec<crate::store::SessionStatusEntry>)> = online_clients
        .into_iter()
        .map(|(name, vtoken)| {
            let sessions = all_sessions_map.get(&vtoken).cloned().unwrap_or_default();
            (name, sessions)
        })
        .collect();
    messages::hub_status(online, total, &client_sessions)
}

pub(super) fn handle_cmd_help() -> String {
    build_help_text()
}

pub(super) async fn handle_cmd_session_list(
    state: &HubState,
    from_user_id: &str,
    real_ctx: &str,
    group_id: Option<&str>,
) -> String {
    let (vctx, vtoken) = resolve_vctx_and_vtoken(state, real_ctx, from_user_id, group_id).await;
    match vtoken {
        None => messages::NO_BACKEND.to_string(),
        Some(vtoken) => {
            let backend_name = {
                let registry = state.clients.registry.read().await;
                registry
                    .all_clients()
                    .into_iter()
                    .find(|c| c.vtoken == vtoken)
                    .map(|c| c.name.clone())
                    .unwrap_or_else(|| crate::redact_token(&vtoken))
            };
            let active = state
                .store
                .get_active_session_name(&vctx, &vtoken)
                .await
                .unwrap_or_else(|_| "default".to_string());
            match state.store.list_backend_sessions(&vctx, &vtoken).await {
                Ok(sessions) if sessions.is_empty() => {
                    format!(
                        "当前后端 `{backend_name}` {}",
                        messages::SESSION_LIST_NO_SESSIONS
                    )
                }
                Ok(sessions) => {
                    let mut lines = vec![format!("**后端 `{backend_name}` 的 sessions:**")];
                    for s in &sessions {
                        let marker = if s.session_name == active { "" } else { "" };
                        let uuid_hint = if s.backend_session_id.is_empty() {
                            messages::SESSION_SLOT_NO_UUID.to_string()
                        } else {
                            format!(
                                "`{}`",
                                s.backend_session_id.chars().take(12).collect::<String>()
                            )
                        };
                        lines.push(format!("• `{}`{}{}", s.session_name, marker, uuid_hint));
                    }
                    lines.push(format!("\n当前活跃:`{}`", active));
                    lines.push(messages::SESSION_LIST_SWITCH_HINT.to_string());
                    lines.join("\n")
                }
                Err(e) => messages::session_list_failed(&e),
            }
        }
    }
}

pub(super) async fn handle_cmd_session_new(
    state: &HubState,
    from_user_id: &str,
    real_ctx: &str,
    group_id: Option<&str>,
    session_name: &str,
    initial_uuid: &str,
) -> String {
    let (vctx, vtoken) = resolve_vctx_and_vtoken(state, real_ctx, from_user_id, group_id).await;
    match vtoken {
        None => messages::NO_BACKEND.to_string(),
        Some(vtoken) => {
            match state
                .store
                .set_backend_session(&vctx, &vtoken, session_name, initial_uuid)
                .await
            {
                Ok(()) => {
                    let switch_result = state
                        .store
                        .set_active_session_name(&vctx, &vtoken, session_name)
                        .await;
                    match switch_result {
                        Ok(()) => messages::session_new_ok(session_name),
                        Err(e) => messages::session_new_created_switch_failed(session_name, &e),
                    }
                }
                Err(e) => messages::session_new_failed(&e),
            }
        }
    }
}

pub(super) async fn handle_cmd_session_use(
    state: &HubState,
    from_user_id: &str,
    real_ctx: &str,
    group_id: Option<&str>,
    session_name: &str,
) -> String {
    let (vctx, vtoken) = resolve_vctx_and_vtoken(state, real_ctx, from_user_id, group_id).await;
    match vtoken {
        None => messages::NO_BACKEND.to_string(),
        Some(vtoken) => {
            let ensure_result: Result<(), String> = match state
                .store
                .get_backend_session(&vctx, &vtoken, session_name)
                .await
            {
                Ok(None) => state
                    .store
                    .set_backend_session(&vctx, &vtoken, session_name, "")
                    .await
                    .map_err(|e| messages::session_use_slot_create_failed(&e)),
                Err(e) => Err(messages::session_use_query_failed(&e)),
                Ok(Some(_)) => Ok(()),
            };
            match ensure_result {
                Err(msg) => msg,
                Ok(()) => {
                    match state
                        .store
                        .set_active_session_name(&vctx, &vtoken, session_name)
                        .await
                    {
                        Ok(()) => messages::session_use_ok(session_name),
                        Err(e) => messages::session_use_failed(&e),
                    }
                }
            }
        }
    }
}

pub(super) async fn handle_cmd_session_delete(
    state: &HubState,
    from_user_id: &str,
    real_ctx: &str,
    group_id: Option<&str>,
    session_name: &str,
) -> String {
    let (vctx, vtoken) = resolve_vctx_and_vtoken(state, real_ctx, from_user_id, group_id).await;
    match vtoken {
        None => messages::NO_BACKEND.to_string(),
        Some(vtoken) => {
            let active = state
                .store
                .get_active_session_name(&vctx, &vtoken)
                .await
                .unwrap_or_else(|_| "default".to_string());
            if session_name == active {
                messages::session_delete_active_error(session_name)
            } else {
                match state
                    .store
                    .delete_backend_session(&vctx, &vtoken, session_name)
                    .await
                {
                    Ok(true) => messages::session_delete_ok(session_name),
                    Ok(false) => messages::session_delete_not_found(session_name),
                    Err(e) => messages::session_delete_failed(&e),
                }
            }
        }
    }
}

// ─── Static responder helpers ─────────────────────────────────────────────────

fn build_help_text() -> String {
    "iLink Hub 帮助\n\n\
     可用指令(括号内为缩写):\n\
     /status(/s)— 查看当前 Hub 状态\n\
     /list(/ls)— 列出所有已注册的 AI 后端\n\
     /use <名称或序号>(/u <名称或序号>)— 切换到指定的 AI 后端\n\
     /help(/h)— 显示此帮助\n\n\
     Session 管理(同一后端下的多会话):\n\
     /session list(/sl)— 列出当前对话的所有 sessions\n\
     /session new <名称> [UUID](/sn <名称>)— 创建新 session(可选初始 UUID)\n\
     /session use <名称>(/su <名称>)— 切换到指定 session\n\
     /session delete <名称>(/sd <名称>)— 删除指定 session\n\n\
     快捷 @ 后端:发送 `@<名称或序号> <消息>` 可临时在该后端上**新建一个会话**并发送此消息,不会改变你当前 /use 的后端和活跃 session(与引用回复类似的临时操作)。名称与 /use 使用的名称一致(可用 /list 查看,序号即 /list 中的编号);名称取第一个空格之前的部分,其余为消息内容。需要继续这个临时会话时,引用它的回复即可。\n\n\
     引用回复:引用某条机器人消息后发送的内容,会优先路由到发出该条消息的后端(或 Hub 指令结果),不必依赖当前 /use。\n\
     多后端时,各后端回复末尾可能带有「— 工作区名」展示行(仅**同时在线**的后端多于一个时默认追加;历史注册但离线的客户端不计入)。可用环境变量 ILINKHUB_OUTBOUND_ORIGIN_LABEL 强制关/开。\n\n\
     关于 iLink Hub:\n\
     本服务是一个消息路由中枢,可将您的微信消息转发给已接入的 AI 助手后端进行处理。\n\n\
     管理员接入指南:\n\
     1. 部署并启动 ilink-hub serve\n\
     2. 运行 ilink-hub register --name <名称> 注册后端\n\
     3. 将输出的 WEIXIN_TOKEN 配置到您的 AI 服务\n\
     4. AI 服务调用 /ilink/bot/getupdates 接收消息,并通过 /ilink/bot/sendmessage 回复"
        .to_string()
}