From 000f73424b98636b129ec3aadbc531eb85a73de4 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 07:10:13 +0000 Subject: [PATCH] Add limbo gateway: Telegram and personal WeChat channels A long-running `limbo gateway` connects chat apps to agents. Each chat gets its own Conversation (daily posture by default), owners-only access, streamed replies where the channel can edit, attachments in both directions (send_file tool), and per-chat session switching. - conversation.rs: frontend-neutral Conversation extracted from headless, shared by headless and gateway workers - channels/: Channel trait, Markdown rendering/splitting, credential and cursor store, hand-written Telegram Bot API client, WeChat iLink client (QR login, AES-128-ECB CDN media) - gateway/: router (dedup, owners, group addressing), per-chat workers, renderer, outbox (rate limits, retries, context parking), send_file, launchd (macOS) and systemd (Linux) service install - background: per-session routing so subagent events reach the right chat - docs/gateway.md, AGENTS.md, CONTEXT.md, README Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014BrrmXNfubbsTMK4xeEmP5 --- AGENTS.md | 11 +- CONTEXT.md | 19 + Cargo.lock | 49 +++ Cargo.toml | 4 + README.md | 13 + docs/gateway.md | 161 +++++++++ src/background.rs | 50 +++ src/capability.rs | 5 + src/channels/markdown.rs | 371 +++++++++++++++++++ src/channels/mock.rs | 95 +++++ src/channels/mod.rs | 324 +++++++++++++++++ src/channels/store.rs | 139 ++++++++ src/channels/telegram.rs | 668 +++++++++++++++++++++++++++++++++++ src/channels/weixin/api.rs | 297 ++++++++++++++++ src/channels/weixin/cdn.rs | 260 ++++++++++++++ src/channels/weixin/login.rs | 171 +++++++++ src/channels/weixin/mod.rs | 446 +++++++++++++++++++++++ src/config.rs | 108 ++++++ src/conversation.rs | 568 +++++++++++++++++++++++++++++ src/gateway/access.rs | 75 ++++ src/gateway/mod.rs | 488 +++++++++++++++++++++++++ src/gateway/outbox.rs | 185 ++++++++++ src/gateway/render.rs | 497 ++++++++++++++++++++++++++ src/gateway/send_file.rs | 130 +++++++ src/gateway/service.rs | 259 ++++++++++++++ src/gateway/sessions.rs | 110 ++++++ src/gateway/worker.rs | 551 +++++++++++++++++++++++++++++ src/headless.rs | 590 ++++--------------------------- src/main.rs | 70 +++- src/subagents/supervisor.rs | 53 ++- 30 files changed, 6234 insertions(+), 533 deletions(-) create mode 100644 docs/gateway.md create mode 100644 src/channels/markdown.rs create mode 100644 src/channels/mock.rs create mode 100644 src/channels/mod.rs create mode 100644 src/channels/store.rs create mode 100644 src/channels/telegram.rs create mode 100644 src/channels/weixin/api.rs create mode 100644 src/channels/weixin/cdn.rs create mode 100644 src/channels/weixin/login.rs create mode 100644 src/channels/weixin/mod.rs create mode 100644 src/conversation.rs create mode 100644 src/gateway/access.rs create mode 100644 src/gateway/mod.rs create mode 100644 src/gateway/outbox.rs create mode 100644 src/gateway/render.rs create mode 100644 src/gateway/send_file.rs create mode 100644 src/gateway/service.rs create mode 100644 src/gateway/sessions.rs create mode 100644 src/gateway/worker.rs diff --git a/AGENTS.md b/AGENTS.md index 73e6eec..496af89 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,7 +19,7 @@ Runtime dependencies: `rg` (ripgrep) for `grep`; `python3` only for Code Mode (` ``` src/ -├── main.rs # CLI (clap): args, subcommands (models/auth/sessions/daily), launch +├── main.rs # CLI (clap): args, subcommands (models/auth/sessions/daily/gateway/channels), launch ├── models.rs # Message, ToolCall, ToolResult, LlmEvent (serde, the shared vocabulary) ├── config.rs # ~/.limbo/config.toml: every section has defaults; toml_edit rewrites keep comments ├── auth.rs # ~/.limbo/auth.json (0600) @@ -50,6 +50,13 @@ src/ ├── recent_models.rs # Recently used models for the picker ├── integrations.rs # Herdr + OSC 7501 lifecycle reporting ├── headless.rs # --headless / --json / -p frontends on the same pump +├── conversation.rs # Frontend-neutral Conversation: job/cancel/steer/slash commands, shared by headless and gateway +├── channels/ # mod.rs (Channel trait, PeerKey, Inbound/OutboundMessage, ChannelCaps), markdown.rs (render/split), +│ # store.rs (~/.limbo/channels: credentials 0600, cursors, gateway lock), telegram.rs (hand-written Bot API), +│ # weixin/ (api.rs iLink protocol, cdn.rs AES-128-ECB media, login.rs QR login) +├── gateway/ # mod.rs (router: dedup, access, one worker per chat; `limbo gateway`/`limbo channels`), access.rs (owners), +│ # sessions.rs (chat → session map), worker.rs (per-chat Conversation), render.rs (AgentEvent → chat messages), +│ # outbox.rs (rate limits, edit coalescing, retries), send_file.rs (tool), service.rs (launchd / systemd) ├── subagents/ # mod.rs (task board), supervisor.rs (scheduler, runners, notices), │ # tools.rs (plan_tasks/task_*/answer; child: checkpoint/need/ask_main/finish/propose_task), │ # profiles.rs (agent profiles + scan), policies.rs (read-only/no-edit/ @@ -110,6 +117,8 @@ Subagents (docs/subagents.md): the main agent's `plan_tasks` creates tasks on th Daily mode (`limbo daily` / `--daily` / `/daily-mode on|off`, docs/daily-mode.md): a session posture like Code Mode — same loop, a general-assistant system prompt, plus the daily tools; persisted as `SessionMeta.daily_mode`. Memory is a snapshot taken when the agent is built: `USER.md` and `NOTES.md` enter daily prompts only; `memory` writes reach disk at once but the prompt changes only next session (prefix cache stays intact). `/learn` (and the automatic trigger in `learn.rs`) runs a `skill-reviewer` fork subagent whose only write tool is `skill_manage`. +Gateway (`limbo gateway`, docs/gateway.md): one process serves Telegram and personal WeChat (iLink) chats. Each `Channel` adapter long-polls and hands `InboundMessage`s to the router, which drops duplicates, strangers (owners only, per channel) and unaddressed group messages, then sends each chat to its own worker (one `Conversation` per `PeerKey`, loaded lazily, unloaded after `idle_unload_secs`). A message while busy steers the running turn. `Renderer` turns `AgentEvent`s into `OutItem`s (streamed previews where the channel can edit, tool-progress slot, `ask_user` as numbered choices); the per-chat outbox applies `ChannelCaps` (rate limits, chunking, retries, parking on expired WeChat context). Background events reach the right worker through `background::route(session, tx)`. Chats default to daily posture; `send_file` (provider `channel`) is registered only there. `limbo gateway install` writes a launchd agent (macOS) or systemd user unit (Linux). + ## Key Decisions - tokio everywhere; tools are `async_trait` objects (`Arc`), blocking work goes through `spawn_blocking` or child processes diff --git a/CONTEXT.md b/CONTEXT.md index 32cdb38..614773c 100644 --- a/CONTEXT.md +++ b/CONTEXT.md @@ -130,6 +130,25 @@ proposals should name modules with these terms. one `` steer item; questions and alerts wake an idle main agent, results wait until the wave is done. +## Gateway + +- **Gateway** — the long-running process (`limbo gateway`) that connects + chat apps to agents (`gateway/`). One per machine (`gateway.lock`). +- **Channel** — one chat platform account (Telegram bot, WeChat iLink + bot) behind the `Channel` trait: receive, send, fetch media, and + `ChannelCaps` (text limit, edit support, Markdown dialect, send rate). +- **Peer** — one chat on one channel (`PeerKey`: channel, account, chat, + thread). Every peer has its own current session and session history. +- **Owner** — a platform user id allowed to talk to the gateway. There + are no guests: everyone else is ignored. +- **Worker** — the task that owns one peer's Conversation, renderer and + outbox. +- **Outbox** — a worker's ordered sender: coalesces edits, respects rate + limits, retries, and parks messages while a WeChat context token is + expired. +- **Provider `channel`** — a capability that reaches the user's chat + (`send_file`). + ## Safety Limbo has no safety fences: file tools are not scoped to the workdir, diff --git a/Cargo.lock b/Cargo.lock index 13e28fa..1e859a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -287,6 +298,16 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clap" version = "4.6.7" @@ -1147,6 +1168,15 @@ dependencies = [ "rustversion", ] +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", +] + [[package]] name = "instability" version = "0.3.14" @@ -1246,6 +1276,7 @@ dependencies = [ name = "limbo" version = "0.4.2" dependencies = [ + "aes", "anyhow", "async-trait", "base64 0.22.1", @@ -1259,7 +1290,9 @@ dependencies = [ "ignore", "include_dir", "libc", + "md-5", "pulldown-cmark", + "qrcode", "rand 0.9.5", "ratatui", "regex", @@ -1346,6 +1379,16 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.8.3" @@ -1730,6 +1773,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "qrcode" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec" + [[package]] name = "quinn" version = "0.11.12" diff --git a/Cargo.toml b/Cargo.toml index a156536..4bfc3eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,10 @@ regex = "1" include_dir = "0.7" sha1_smol = "1" url = "2" +# chat gateway (WeChat iLink media: AES-128-ECB + MD5; terminal QR code for login) +aes = "0.8" +md-5 = "0.10" +qrcode = { version = "0.14", default-features = false } [dev-dependencies] tempfile = "3" diff --git a/README.md b/README.md index eb11bfd..24bb71e 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,17 @@ non-coding work, in its own working directory: See [docs/daily-mode.md](docs/daily-mode.md). +### From Telegram and WeChat + +```bash +limbo channels login telegram --token # then message the bot once +limbo channels login weixin # scan the QR code with WeChat +limbo gateway install # launchd on macOS, systemd on Linux +``` + +Each chat gets its own daily-mode session; only the owners recorded at +login can talk to it. See [docs/gateway.md](docs/gateway.md). + ### Scriptable `limbo --headless` gives the same agent as a line-oriented REPL, and @@ -305,6 +316,8 @@ Limbo borrows openly from other agents. Thank you to: self-learning skills - [`docs/subagents.md`](docs/subagents.md): background subagents, the task board and how to steer them +- [`docs/gateway.md`](docs/gateway.md): talk to limbo from Telegram and + WeChat; run it as a macOS launchd or Linux systemd service - [`docs/learning-rust.md`](docs/learning-rust.md): code map and reading order for learning Rust from this codebase - [`design/`](design/): RFCs for headless mode, bang commands, and the UI diff --git a/docs/gateway.md b/docs/gateway.md new file mode 100644 index 0000000..1acbfac --- /dev/null +++ b/docs/gateway.md @@ -0,0 +1,161 @@ +# Gateway: limbo in Telegram and WeChat + +`limbo gateway` is a long-running process that lets you talk to limbo from +chat apps. It speaks two channels: + +- **Telegram**, through the Bot API (long polling; the client is written by + hand on `reqwest`, no Telegram crate). +- **Personal WeChat**, through Tencent's iLink bot protocol + (`ilinkai.weixin.qq.com`), the same one the official WeChat ClawBot plugin + uses. You log in by scanning a QR code; no phone emulator, no hooking. + +Every chat gets its own session, in **daily** posture by default (the +general assistant from [daily-mode.md](daily-mode.md)). Only the **owners** +recorded at login can talk to the gateway; messages from anyone else are +ignored without a reply. + +## Quick start + +```bash +# 1. Telegram: create a bot with @BotFather, then +limbo channels login telegram --token 123456:ABC... +# and send your bot any private message within 5 minutes: that makes +# you its owner. + +# 2. WeChat: show a QR code in the terminal and scan it with WeChat +limbo channels login weixin +# The account that scans is the owner. + +limbo channels status # what is configured +limbo gateway # run in the foreground (Ctrl-C stops) +limbo gateway install # or run it as a background service +``` + +The gateway needs an LLM key like any other limbo run (`limbo auth login`). + +## In the chat + +Write as you would in the TUI. While a reply is running, a new message +*steers* the running turn; `/queue ` waits for it to finish instead. +The usual slash commands work (`/new`, `/compact`, `/model`, `/btw`, +`/daily-mode`, `/help`, …), plus a few chat-only ones: + +| Command | What it does | +|---|---| +| `/stop` | interrupt the running turn | +| `/sessions` | list this chat's sessions | +| `/resume ` | switch back to an older session of this chat | +| `/status` | session id, model, posture, tokens | + +Photos, files and voice messages are downloaded into +`/media//` and attached to your message (images go +inline to vision models). WeChat voice arrives as the server's transcript. +The agent can send files back with the `send_file` tool ("send me the +report as a file"). + +When the agent asks a multiple-choice question (`ask_user`), Telegram shows +buttons; WeChat shows a numbered list and you answer with the number. + +### Differences between the channels + +| | Telegram | WeChat | +|---|---|---| +| Streaming | the reply is edited in place as it streams | sent in pieces when each paragraph is done (no editing) | +| Formatting | Markdown rendered as Telegram HTML | plain text | +| Message length | 4096 characters, split above | about 2000 characters, split above | +| Groups | `channels.telegram.groups = "mention"`: reply when mentioned or replied to | private chats only | +| Files | up to 50 MB | through the encrypted CDN | + +## Configuration + +All settings have defaults; add only what you want to change to +`~/.limbo/config.toml`: + +```toml +[gateway] +workdir = "~/limbo-chat" # working directory of chat sessions +posture = "daily" # or "coding" +model = "" # empty = [llm] model +idle_unload_secs = 1800 # drop an idle chat's agent from memory (0 = never) +max_attachment_mb = 20 +show_tools = true # tool progress line (Telegram) + +[channels.telegram] +enabled = true +owners = [] # extra owner ids, added to those from login +proxy = "" # e.g. "socks5://127.0.0.1:7890" where Telegram is blocked +groups = "mention" # "mention" or "off" + +[channels.weixin] +enabled = true +owners = [] +``` + +State lives under `~/.limbo/channels/`: + +- `credentials.json` (mode 0600): bot tokens and owners from login; +- `sessions.json`: which session each chat is on, and its history; +- `cursor-*`: long-polling positions, so a restart neither loses nor + repeats messages; +- `gateway.lock`: only one gateway runs per machine; +- `gateway.log`: service output. + +`limbo channels logout ` removes a channel's credentials. + +## Running as a service + +`limbo gateway install` registers the gateway with the system's service +manager, using the current binary and any `--config` / `--session-dir` +given, and starts it. `limbo gateway uninstall` stops and removes it. + +**macOS** — a launchd user agent, `~/Library/LaunchAgents/dev.limbo.gateway.plist` +(`RunAtLoad`, `KeepAlive`), loaded with `launchctl bootstrap gui/` +(falling back to `launchctl load -w` on older systems). It runs while you +are logged in. Check it with: + +```bash +launchctl print gui/$(id -u)/dev.limbo.gateway +tail -f ~/.limbo/channels/gateway.log +``` + +**Linux** — a systemd user unit, `~/.config/systemd/user/limbo-gateway.service`, +enabled with `systemctl --user enable --now`. To keep it running after you +log out, run `loginctl enable-linger $USER` once. + +```bash +systemctl --user status limbo-gateway +journalctl --user -u limbo-gateway -f +``` + +The service gets `PATH`, `LANG`, `LIMBO_HOME`, `LIMBO_AUTH_FILE`, the proxy +variables and `SSL_CERT_FILE` from the shell you install it from; reinstall +after changing them. + +## Risks and limits + +- **WeChat iLink is not a public API.** It is the protocol behind + Tencent's own ClawBot plugin, documented only by that plugin's source. + Tencent can change it or restrict the account at any time. Use an account + you can afford to lose access to the bot feature on. +- WeChat replies need a recent message from you: the server hands out a + per-chat context token that expires. When it expires, the gateway holds + the pending replies and delivers them after your next message. +- When the WeChat login expires (`-14`), the gateway says so in the log; + run `limbo channels login weixin` again. +- Anyone who controls an owner account controls limbo on this machine, + including `bash` in coding posture. Limbo has no sandbox (see the + README's Safety section); keep `posture = "daily"` unless you need more. +- Bot tokens are stored in plain text in `credentials.json` (0600). + +## How it fits together + +``` +Telegram adapter ─┐ ┌─ worker (chat A) ─ Conversation ─ Agent + ├─ inbox ─ router ─ dedup ─ owners ─┤ +WeChat adapter ──┘ └─ worker (chat B) ─ … + │ + Renderer (AgentEvent → OutItem) ─ outbox ─ Channel::send +``` + +See `AGENTS.md` (Gateway) and `CONTEXT.md` (Gateway glossary) for the +code map. diff --git a/src/background.rs b/src/background.rs index 17f6fc5..d72f058 100644 --- a/src/background.rs +++ b/src/background.rs @@ -1,7 +1,13 @@ //! Events that happen outside a running turn (background subagents, //! skill reviews). One process-wide channel: producers call [`emit`], the //! frontend takes the receiver once at startup. +//! +//! A process that hosts several conversations at once (the chat gateway) +//! registers a **route** per main session: [`emit_for`] delivers an event +//! of that session to its route, and falls back to the shared channel when +//! nobody registered one (TUI / headless never do). +use std::collections::HashMap; use std::sync::{Mutex, OnceLock}; use tokio::sync::mpsc; @@ -47,7 +53,51 @@ pub fn emit(ev: BackgroundEvent) { let _ = chan().0.send(ev); } +type Routes = Mutex>>; + +fn routes() -> &'static Routes { + static ROUTES: OnceLock = OnceLock::new(); + ROUTES.get_or_init(|| Mutex::new(HashMap::new())) +} + +/// An event that belongs to main session `session` (subagent notices). +pub fn emit_for(session: &str, ev: BackgroundEvent) { + let routed = routes().lock().unwrap().get(session).cloned(); + match routed { + Some(tx) if !tx.is_closed() => { + let _ = tx.send(ev); + } + _ => emit(ev), + } +} + +/// Deliver `session`'s events to `tx` from now on (replacing an older route). +pub fn route(session: &str, tx: mpsc::UnboundedSender) { + routes().lock().unwrap().insert(session.to_string(), tx); +} + +/// Stop routing `session` (its events go to the shared channel again). +pub fn unroute(session: &str) { + routes().lock().unwrap().remove(session); +} + /// The receiving end (the first caller gets it). pub fn take_receiver() -> Option> { chan().1.lock().unwrap().take() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn routed_events_reach_their_session_only() { + let (tx, mut rx) = mpsc::unbounded_channel(); + route("route-test-a", tx); + emit_for("route-test-a", BackgroundEvent::MainWake); + assert!(matches!(rx.try_recv(), Ok(BackgroundEvent::MainWake))); + unroute("route-test-a"); + emit_for("route-test-a", BackgroundEvent::MainWake); + assert!(rx.try_recv().is_err()); + } +} diff --git a/src/capability.rs b/src/capability.rs index 515d18f..e550222 100644 --- a/src/capability.rs +++ b/src/capability.rs @@ -33,6 +33,9 @@ pub enum Provider { Human, /// The harness itself, not a model call: `/goal` verify commands. Runtime, + /// The chat the session is attached to (`limbo gateway`): `send_file` + /// delivers to the person on Telegram / WeChat. + Channel, } impl Provider { @@ -44,6 +47,7 @@ impl Provider { Provider::Subagent => "subagent", Provider::Human => "human", Provider::Runtime => "runtime", + Provider::Channel => "channel", } } } @@ -92,6 +96,7 @@ mod tests { Provider::Subagent, Provider::Human, Provider::Runtime, + Provider::Channel, ] { assert_eq!(serde_json::to_value(p).unwrap(), p.label()); } diff --git a/src/channels/markdown.rs b/src/channels/markdown.rs new file mode 100644 index 0000000..db57e61 --- /dev/null +++ b/src/channels/markdown.rs @@ -0,0 +1,371 @@ +//! Model markdown → what a chat platform renders: Telegram's HTML subset or +//! plain text (WeChat), plus splitting long answers into message-sized +//! chunks without cutting a code block in half. +//! +//! 🦀 Rust notes +//! - `pulldown_cmark::Parser` is an iterator of `Event`s (Start(tag), +//! Text, End(tag), …); the converters fold it into a `String`. +//! - `match` on `(&Event, mode)` pairs keeps both dialects in one walker. + +use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd}; + +use super::MarkdownDialect; + +fn escape_html(s: &str) -> String { + s.replace('&', "&") + .replace('<', "<") + .replace('>', ">") +} + +/// Render `md` in `dialect`. +pub fn render(md: &str, dialect: MarkdownDialect) -> String { + let html = dialect == MarkdownDialect::TelegramHtml; + let mut out = String::new(); + // Stack of list counters (None = bullet list). + let mut lists: Vec> = Vec::new(); + let mut link_url: Vec = Vec::new(); + // The open code block has a `` inside its `
`.
+    let mut pre_code = false;
+    let opts = Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TABLES;
+    for ev in Parser::new_ext(md, opts) {
+        match ev {
+            Event::Start(tag) => match tag {
+                Tag::Paragraph => {}
+                Tag::Heading { .. } => {
+                    if html {
+                        out.push_str("");
+                    }
+                }
+                Tag::BlockQuote(_) => {
+                    if html {
+                        out.push_str("
"); + } else { + out.push_str("> "); + } + } + Tag::CodeBlock(kind) => { + if html { + match kind { + CodeBlockKind::Fenced(lang) if !lang.is_empty() => { + pre_code = true; + out.push_str(&format!( + "
",
+                                    escape_html(&lang)
+                                ))
+                            }
+                            _ => {
+                                pre_code = false;
+                                out.push_str("
")
+                            }
+                        }
+                    }
+                }
+                Tag::List(start) => lists.push(start),
+                Tag::Item => {
+                    let depth = lists.len().saturating_sub(1);
+                    out.push_str(&"  ".repeat(depth));
+                    match lists.last_mut() {
+                        Some(Some(n)) => {
+                            out.push_str(&format!("{n}. "));
+                            *n += 1;
+                        }
+                        _ => out.push_str("• "),
+                    }
+                }
+                Tag::Emphasis => {
+                    if html {
+                        out.push_str("");
+                    }
+                }
+                Tag::Strong => {
+                    if html {
+                        out.push_str("");
+                    }
+                }
+                Tag::Strikethrough => {
+                    if html {
+                        out.push_str("");
+                    }
+                }
+                Tag::Link { dest_url, .. } => {
+                    if html {
+                        out.push_str(&format!("", escape_html(&dest_url)));
+                    }
+                    link_url.push(dest_url.to_string());
+                }
+                Tag::Image { dest_url, .. } => link_url.push(dest_url.to_string()),
+                Tag::TableCell => out.push_str("| "),
+                _ => {}
+            },
+            Event::End(tag) => match tag {
+                TagEnd::Paragraph => out.push_str("\n\n"),
+                TagEnd::Heading(_) => {
+                    if html {
+                        out.push_str("");
+                    }
+                    out.push_str("\n\n");
+                }
+                TagEnd::BlockQuote(_) => {
+                    trim_end_newlines(&mut out);
+                    if html {
+                        out.push_str("
"); + } + out.push_str("\n\n"); + } + TagEnd::CodeBlock => { + if out.ends_with('\n') { + out.pop(); + } + if html { + out.push_str(if pre_code { "
" } else { "" }); + } + out.push_str("\n\n"); + } + TagEnd::List(_) => { + lists.pop(); + if lists.is_empty() { + trim_end_newlines(&mut out); + out.push_str("\n\n"); + } + } + TagEnd::Item => { + trim_end_newlines(&mut out); + out.push('\n'); + } + TagEnd::Emphasis => { + if html { + out.push_str(""); + } + } + TagEnd::Strong => { + if html { + out.push_str(""); + } + } + TagEnd::Strikethrough => { + if html { + out.push_str(""); + } + } + TagEnd::Link => { + let url = link_url.pop().unwrap_or_default(); + if html { + out.push_str(""); + } else if !url.is_empty() && !out.ends_with(&url) { + out.push_str(&format!(" ({url})")); + } + } + TagEnd::Image => { + let url = link_url.pop().unwrap_or_default(); + out.push_str(&format!(" ({url})")); + } + TagEnd::TableCell => out.push(' '), + TagEnd::TableRow | TagEnd::TableHead => out.push_str("|\n"), + TagEnd::Table => out.push('\n'), + _ => {} + }, + Event::Text(t) => { + if html { + out.push_str(&escape_html(&t)); + } else { + out.push_str(&t); + } + } + Event::Code(t) => { + if html { + out.push_str(&format!("{}", escape_html(&t))); + } else { + out.push_str(&t); + } + } + Event::SoftBreak | Event::HardBreak => out.push('\n'), + Event::Rule => out.push_str("──────\n\n"), + Event::Html(t) | Event::InlineHtml(t) => { + if html { + out.push_str(&escape_html(&t)); + } else { + out.push_str(&t); + } + } + Event::TaskListMarker(done) => out.push_str(if done { "☑ " } else { "☐ " }), + _ => {} + } + } + out.trim_end().to_string() +} + +fn trim_end_newlines(s: &mut String) { + while s.ends_with('\n') { + s.pop(); + } +} + +/// Split markdown source into chunks of at most `max` characters, at +/// paragraph boundaries first, then lines; a code block that has to be cut +/// is closed and reopened so each chunk renders on its own. +pub fn split(md: &str, max: usize) -> Vec { + let max = max.max(64); + // Blocks: runs of lines separated by blank lines, fences kept whole. + let mut blocks: Vec = Vec::new(); + let mut cur = String::new(); + let mut in_fence = false; + for line in md.lines() { + let fence = line.trim_start().starts_with("```"); + if fence { + in_fence = !in_fence; + } + if !in_fence && !fence && line.trim().is_empty() { + if !cur.is_empty() { + blocks.push(std::mem::take(&mut cur)); + } + continue; + } + if !cur.is_empty() { + cur.push('\n'); + } + cur.push_str(line); + } + if !cur.is_empty() { + blocks.push(cur); + } + let mut pieces: Vec = Vec::new(); + for b in blocks { + if b.chars().count() <= max { + pieces.push(b); + } else { + pieces.extend(split_block(&b, max)); + } + } + // Pack pieces into chunks. + let mut chunks: Vec = Vec::new(); + let mut cur = String::new(); + for p in pieces { + let extra = if cur.is_empty() { 0 } else { 2 }; + if !cur.is_empty() && cur.chars().count() + extra + p.chars().count() > max { + chunks.push(std::mem::take(&mut cur)); + } + if !cur.is_empty() { + cur.push_str("\n\n"); + } + cur.push_str(&p); + } + if !cur.is_empty() { + chunks.push(cur); + } + chunks +} + +/// One oversized block: by lines; inside a fence, every piece gets its own +/// opening and closing fence line. +fn split_block(block: &str, max: usize) -> Vec { + let first = block.lines().next().unwrap_or(""); + let fenced = first.trim_start().starts_with("```"); + let open = if fenced { + first.to_string() + } else { + String::new() + }; + let close = if fenced { "```" } else { "" }; + let overhead = if fenced { + open.chars().count() + close.len() + 2 + } else { + 0 + }; + let budget = max.saturating_sub(overhead).max(16); + let body_lines: Vec<&str> = if fenced { + let mut v: Vec<&str> = block.lines().skip(1).collect(); + if v.last().is_some_and(|l| l.trim_start().starts_with("```")) { + v.pop(); + } + v + } else { + block.lines().collect() + }; + let mut out = Vec::new(); + let mut cur = String::new(); + let flush = |cur: &mut String, out: &mut Vec| { + if cur.is_empty() { + return; + } + if fenced { + out.push(format!("{open}\n{cur}\n{close}")); + } else { + out.push(cur.clone()); + } + cur.clear(); + }; + for line in body_lines { + // A single line longer than the budget is cut by characters. + let mut rest: Vec = line.chars().collect(); + loop { + let take = rest.len().min(budget); + let piece: String = rest.drain(..take).collect(); + let extra = if cur.is_empty() { 0 } else { 1 }; + if !cur.is_empty() && cur.chars().count() + extra + piece.chars().count() > budget { + flush(&mut cur, &mut out); + } + if !cur.is_empty() { + cur.push('\n'); + } + cur.push_str(&piece); + if rest.is_empty() { + break; + } + } + } + flush(&mut cur, &mut out); + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn telegram_html_subset() { + let html = render( + "# Title\n\nSome **bold** and `a` and [link](https://x.y).\n\n- one\n- two\n\n```rust\nlet a = 1 < 2;\n```", + MarkdownDialect::TelegramHtml, + ); + assert!(html.starts_with("Title")); + assert!(html.contains("bold")); + assert!(html.contains("a<b>")); + assert!(html.contains("link")); + assert!(html.contains("• one\n• two")); + assert!(html.contains("
let a = 1 < 2;
")); + } + + #[test] + fn plain_text_drops_markup() { + let t = render( + "## Steps\n\n1. **Read** it\n2. Edit [docs](https://d.e)\n\n```\ncode\n```", + MarkdownDialect::Plain, + ); + assert_eq!(t, "Steps\n\n1. Read it\n2. Edit docs (https://d.e)\n\ncode"); + } + + #[test] + fn split_keeps_paragraphs_and_fences() { + let md = format!( + "{}\n\n{}\n\n```\n{}\n```", + "a".repeat(50), + "b".repeat(50), + (0..40) + .map(|i| format!("line {i}")) + .collect::>() + .join("\n") + ); + let chunks = split(&md, 120); + assert!(chunks.len() >= 3); + for c in &chunks { + assert!(c.chars().count() <= 120, "{c}"); + assert_eq!(c.matches("```").count() % 2, 0, "unbalanced fence: {c}"); + } + assert!(chunks[0].starts_with("aaaa")); + } + + #[test] + fn short_text_is_one_chunk() { + assert_eq!(split("hello\n\nworld", 100), vec!["hello\n\nworld"]); + } +} diff --git a/src/channels/mock.rs b/src/channels/mock.rs new file mode 100644 index 0000000..badba0b --- /dev/null +++ b/src/channels/mock.rs @@ -0,0 +1,95 @@ +//! An in-memory channel for tests: records what the gateway sends. + +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use async_trait::async_trait; +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +use super::{ + Channel, ChannelCaps, ChannelKind, InboundMessage, MarkdownDialect, MediaRef, MsgHandle, + OutboundMessage, PeerKey, ReplyCtx, SendError, +}; + +#[derive(Default, Clone)] +pub struct MockChannel { + pub sent: Arc>>, +} + +impl MockChannel { + /// Plain texts sent so far (typing indicators left out). + pub fn texts(&self) -> Vec { + self.sent + .lock() + .unwrap() + .iter() + .filter_map(|m| match m { + OutboundMessage::Text { text, .. } | OutboundMessage::Edit { text, .. } => { + Some(text.clone()) + } + OutboundMessage::File { path, .. } => Some(format!("file:{}", path.display())), + _ => None, + }) + .collect() + } + + /// Wait until some sent text satisfies `f` (or time out). + pub async fn wait_for(&self, f: impl Fn(&str) -> bool) -> bool { + for _ in 0..200 { + if self.texts().iter().any(|t| f(t)) { + return true; + } + tokio::time::sleep(Duration::from_millis(25)).await; + } + false + } +} + +#[async_trait] +impl Channel for MockChannel { + fn kind(&self) -> ChannelKind { + ChannelKind::Mock + } + fn account(&self) -> String { + "mockbot".into() + } + fn caps(&self) -> ChannelCaps { + ChannelCaps { + max_text_chars: 1000, + edit: false, + markdown: MarkdownDialect::Plain, + min_send_interval: Duration::ZERO, + typing_ttl: Duration::from_secs(60), + } + } + async fn run( + &self, + _inbox: mpsc::Sender, + cancel: CancellationToken, + ) -> anyhow::Result<()> { + cancel.cancelled().await; + Ok(()) + } + async fn send( + &self, + _peer: &PeerKey, + _ctx: &ReplyCtx, + msg: OutboundMessage, + ) -> Result, SendError> { + self.sent.lock().unwrap().push(msg); + Ok(None) + } + async fn fetch_media( + &self, + media: &MediaRef, + dir: &Path, + _max: u64, + ) -> anyhow::Result { + std::fs::create_dir_all(dir)?; + let p = dir.join(media.name.clone().unwrap_or_else(|| "f.txt".into())); + std::fs::write(&p, "attached content")?; + Ok(p) + } +} diff --git a/src/channels/mod.rs b/src/channels/mod.rs new file mode 100644 index 0000000..47025dd --- /dev/null +++ b/src/channels/mod.rs @@ -0,0 +1,324 @@ +//! Chat channels: the platform half of the chat gateway (`limbo gateway`). +//! +//! A [`Channel`] adapter only speaks one platform's protocol — it long-polls +//! for messages, sends text / files / typing indicators and downloads media. +//! Everything else (who may talk, which session a chat maps to, how agent +//! events become chat messages) lives in `gateway/` and never sees a +//! Telegram or WeChat field name, the same way `llm/usage.rs` hides each +//! provider's usage fields from the agent loop. +//! +//! Two adapters exist: [`telegram`] (official Bot API) and [`weixin`] +//! (personal WeChat through Tencent's iLink bot API). [`mock`] is an +//! in-memory adapter for tests. +//! +//! 🦀 Rust notes +//! - `#[async_trait]` lets a trait have `async fn`s that work behind +//! `Arc` (a trait object); the gateway keeps one such +//! object per configured channel. +//! - `thiserror` derives `std::error::Error` + `Display` for [`SendError`] +//! from the `#[error(..)]` attributes. + +pub mod markdown; +#[cfg(test)] +pub mod mock; +pub mod store; +pub mod telegram; +pub mod weixin; + +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use async_trait::async_trait; +use serde::{Deserialize, Serialize}; +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +/// Which platform a chat lives on. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)] +#[serde(rename_all = "lowercase")] +pub enum ChannelKind { + Telegram, + Weixin, + #[cfg(test)] + Mock, +} + +impl ChannelKind { + pub fn as_str(self) -> &'static str { + match self { + ChannelKind::Telegram => "telegram", + ChannelKind::Weixin => "weixin", + #[cfg(test)] + ChannelKind::Mock => "mock", + } + } + + pub fn parse(s: &str) -> Option { + match s.to_ascii_lowercase().as_str() { + "telegram" | "tg" => Some(ChannelKind::Telegram), + "weixin" | "wechat" | "wx" => Some(ChannelKind::Weixin), + _ => None, + } + } +} + +/// One chat, globally unique: which channel, which bot account, which chat +/// (and which forum topic, for Telegram topic groups). +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)] +pub struct PeerKey { + pub channel: ChannelKind, + pub account: String, + pub chat: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub thread: Option, +} + +impl PeerKey { + /// `telegram::[:]` — the key in `sessions.json`. + pub fn key(&self) -> String { + let mut k = format!("{}:{}:{}", self.channel.as_str(), self.account, self.chat); + if let Some(t) = &self.thread { + k.push(':'); + k.push_str(t); + } + k + } +} + +/// What kind of media a message carried. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum MediaKind { + Image, + File, + Voice, + Video, +} + +/// A media item of an inbound message — only a reference; the gateway +/// downloads it with [`Channel::fetch_media`] after admission. +#[derive(Debug, Clone)] +pub struct MediaRef { + pub kind: MediaKind, + /// The file name, when the platform tells it. + pub name: Option, + pub size: Option, + /// Adapter-private data (Telegram `file_id`, WeChat CDN parameters). + pub data: serde_json::Value, +} + +/// Opaque per-message reply context. WeChat needs the inbound message's +/// `context_token` to answer; Telegram needs nothing. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct ReplyCtx(pub Option); + +/// One message from a chat. +#[derive(Debug, Clone)] +pub struct InboundMessage { + pub peer: PeerKey, + /// Stable platform user id of the sender (whitelist key). + pub sender: String, + /// A display name for logs. + pub sender_name: String, + /// Platform message id (deduplication). + pub msg_id: String, + pub text: String, + pub media: Vec, + pub reply_ctx: ReplyCtx, + /// Sent in a group / channel (not a private chat). + pub is_group: bool, + /// The message addresses the bot (mention, reply to it, private chat). + pub addressed: bool, +} + +/// How a text message is formatted. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TextFormat { + /// Send verbatim. + Plain, + /// Model markdown: converted to the channel's dialect by the adapter. + Markdown, +} + +/// A platform message the gateway may edit later. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct MsgHandle(pub String); + +/// Something to send to a chat. +#[derive(Debug, Clone)] +pub enum OutboundMessage { + Text { + text: String, + format: TextFormat, + }, + /// Replace the text of an earlier message (only when `caps.edit`). + Edit { + handle: MsgHandle, + text: String, + format: TextFormat, + }, + File { + path: PathBuf, + caption: Option, + }, + Typing, + /// A question with options (`ask_user`); buttons when `caps.buttons`. + Choices { + question: String, + options: Vec, + }, +} + +/// The markdown dialect a channel renders. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MarkdownDialect { + /// Telegram's HTML subset (`parse_mode = HTML`). + TelegramHtml, + /// No formatting at all (WeChat). + Plain, +} + +/// What a channel can do; the renderer adapts to it. +#[derive(Debug, Clone)] +pub struct ChannelCaps { + /// Hard limit of one text message, in characters. + pub max_text_chars: usize, + /// Messages can be edited (streaming answers by editing one message). + pub edit: bool, + pub markdown: MarkdownDialect, + /// Minimum gap between two sends / edits to the same chat. + pub min_send_interval: Duration, + /// How long a typing indicator lasts before it must be re-sent. + pub typing_ttl: Duration, +} + +/// Why a send failed — the outbox decides from this whether to retry. +#[derive(Debug, thiserror::Error)] +pub enum SendError { + #[error("rate limited, retry after {0:?}")] + RateLimited(Duration), + /// WeChat: the reply window for this chat is closed; resend after the + /// user's next message. + #[error("reply context expired")] + ContextExpired, + /// The bot token is no longer valid (log in again). + #[error("authorization expired: {0}")] + AuthExpired(String), + /// Network trouble or a 5xx: worth a retry. + #[error("transient: {0}")] + Transient(String), + #[error("{0}")] + Other(String), +} + +/// A platform adapter. +#[async_trait] +pub trait Channel: Send + Sync { + fn kind(&self) -> ChannelKind; + /// The bot account this adapter runs as (part of every [`PeerKey`]). + fn account(&self) -> String; + fn caps(&self) -> ChannelCaps; + /// Long-poll until `cancel`; push every message into `inbox`. Persists + /// its own cursor after the message was handed over. + async fn run( + &self, + inbox: mpsc::Sender, + cancel: CancellationToken, + ) -> anyhow::Result<()>; + /// Send one message; `Some(handle)` for messages that can be edited. + async fn send( + &self, + peer: &PeerKey, + ctx: &ReplyCtx, + msg: OutboundMessage, + ) -> Result, SendError>; + /// Download one media item into `dir`; returns the file written. + async fn fetch_media( + &self, + media: &MediaRef, + dir: &Path, + max_bytes: u64, + ) -> anyhow::Result; +} + +/// A file name that is safe to create inside a media directory. +pub fn safe_file_name(name: &str) -> String { + let cleaned: String = name + .chars() + .map(|c| { + if c.is_alphanumeric() || matches!(c, '.' | '-' | '_') { + c + } else { + '_' + } + }) + .collect(); + let trimmed = cleaned.trim_start_matches('.'); + if trimmed.is_empty() { + "file".into() + } else { + trimmed.chars().take(120).collect() + } +} + +/// Build a multipart/form-data body by hand (reqwest's `multipart` feature +/// is not enabled): text fields, then one file field. +pub fn multipart_body( + fields: &[(&str, String)], + file_field: &str, + file_name: &str, + content: &[u8], +) -> (String, Vec) { + let boundary = format!("limbo-{}", crate::agent::new_session_id().replace('-', "")); + let mut body = Vec::with_capacity(content.len() + 512); + for (k, v) in fields { + body.extend_from_slice( + format!("--{boundary}\r\nContent-Disposition: form-data; name=\"{k}\"\r\n\r\n{v}\r\n") + .as_bytes(), + ); + } + let fname = file_name.replace(['"', '\r', '\n'], "_"); + body.extend_from_slice( + format!( + "--{boundary}\r\nContent-Disposition: form-data; name=\"{file_field}\"; filename=\"{fname}\"\r\nContent-Type: application/octet-stream\r\n\r\n" + ) + .as_bytes(), + ); + body.extend_from_slice(content); + body.extend_from_slice(format!("\r\n--{boundary}--\r\n").as_bytes()); + (format!("multipart/form-data; boundary={boundary}"), body) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn peer_key_strings() { + let p = PeerKey { + channel: ChannelKind::Telegram, + account: "42".into(), + chat: "-100".into(), + thread: Some("7".into()), + }; + assert_eq!(p.key(), "telegram:42:-100:7"); + } + + #[test] + fn safe_names() { + assert_eq!(safe_file_name("../../etc/passwd"), "_.._etc_passwd"); + assert_eq!(safe_file_name("报告 v2.pdf"), "报告_v2.pdf"); + assert_eq!(safe_file_name(""), "file"); + } + + #[test] + fn multipart_has_fields_and_file() { + let (ct, body) = multipart_body(&[("chat_id", "1".into())], "document", "a.txt", b"hi"); + let b = String::from_utf8(body).unwrap(); + let boundary = ct.split("boundary=").nth(1).unwrap(); + assert!(b.starts_with(&format!("--{boundary}\r\n"))); + assert!(b.contains("name=\"chat_id\"\r\n\r\n1\r\n")); + assert!(b.contains("filename=\"a.txt\"")); + assert!(b.ends_with(&format!("\r\n--{boundary}--\r\n"))); + } +} diff --git a/src/channels/store.rs b/src/channels/store.rs new file mode 100644 index 0000000..ffe6b23 --- /dev/null +++ b/src/channels/store.rs @@ -0,0 +1,139 @@ +//! On-disk state of the chat gateway, under `~/.limbo/channels/`: +//! +//! - `credentials.json` (0600): bot tokens from `limbo channels login` and +//! the owners recorded at login; +//! - `cursor--`: each adapter's long-poll cursor (Telegram +//! `offset`, WeChat `get_updates_buf`); +//! - `gateway.lock`: one gateway per machine (a bot token allows one poller). +//! +//! 🦀 Rust notes +//! - `flock(2)` through `libc` works the same on Linux and macOS; the lock +//! is released by the kernel when the process dies, so a crash never +//! leaves a stale lock behind. + +use std::fs::File; +use std::io::Write; +use std::os::unix::fs::OpenOptionsExt; +use std::os::unix::io::AsRawFd; +use std::path::{Path, PathBuf}; + +use serde::{Deserialize, Serialize}; + +use crate::config::limbo_home; + +pub fn channels_dir() -> PathBuf { + limbo_home().join("channels") +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct TelegramCred { + pub token: String, + #[serde(default)] + pub bot_id: String, + #[serde(default)] + pub bot_username: String, + /// User ids recorded by `limbo channels login telegram`. + #[serde(default)] + pub owners: Vec, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct WeixinCred { + pub token: String, + /// `ilink_bot_id`. + pub bot_id: String, + /// API host the login was confirmed on. + #[serde(default)] + pub base_url: String, + /// The `ilink_user_id` of whoever scanned the QR code. + #[serde(default)] + pub owners: Vec, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct Credentials { + #[serde(default, skip_serializing_if = "Option::is_none")] + pub telegram: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub weixin: Option, +} + +pub fn credentials_path() -> PathBuf { + channels_dir().join("credentials.json") +} + +pub fn load_credentials() -> Credentials { + std::fs::read_to_string(credentials_path()) + .ok() + .and_then(|t| serde_json::from_str(&t).ok()) + .unwrap_or_default() +} + +pub fn save_credentials(c: &Credentials) -> std::io::Result<()> { + let text = serde_json::to_string_pretty(c)?; + write_private(&credentials_path(), text.as_bytes()) +} + +/// Write atomically with 0600 permissions (tokens are secrets). +pub fn write_private(path: &Path, data: &[u8]) -> std::io::Result<()> { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + let tmp = path.with_extension("tmp"); + let mut f = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .mode(0o600) + .open(&tmp)?; + f.write_all(data)?; + f.sync_all()?; + drop(f); + std::fs::rename(&tmp, path) +} + +fn cursor_path(name: &str) -> PathBuf { + channels_dir().join(format!("cursor-{}", super::safe_file_name(name))) +} + +pub fn read_cursor(name: &str) -> Option { + std::fs::read_to_string(cursor_path(name)) + .ok() + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()) +} + +pub fn write_cursor(name: &str, value: &str) { + if let Err(e) = write_private(&cursor_path(name), value.as_bytes()) { + eprintln!("warning: cannot save cursor {name}: {e}"); + } +} + +/// Held for the lifetime of the gateway process. +pub struct GatewayLock { + _file: File, +} + +impl GatewayLock { + /// `Err` with a readable message when another gateway holds the lock. + pub fn acquire() -> anyhow::Result { + let path = channels_dir().join("gateway.lock"); + std::fs::create_dir_all(channels_dir())?; + let mut file = std::fs::OpenOptions::new() + .create(true) + .truncate(false) + .write(true) + .open(&path)?; + // SAFETY: flock on a file descriptor we own; no memory is shared. + let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) }; + if rc != 0 { + anyhow::bail!( + "another limbo gateway is already running ({} is locked)", + path.display() + ); + } + let _ = file.set_len(0); + let _ = writeln!(file, "{}", std::process::id()); + Ok(Self { _file: file }) + } +} diff --git a/src/channels/telegram.rs b/src/channels/telegram.rs new file mode 100644 index 0000000..fdfc7a8 --- /dev/null +++ b/src/channels/telegram.rs @@ -0,0 +1,668 @@ +//! Telegram through the official Bot API (`https://core.telegram.org/bots/api`), +//! hand-written on reqwest: `getUpdates` long polling, `sendMessage` / +//! `editMessageText` (HTML parse mode), `sendChatAction`, `sendDocument`, +//! `getFile` and inline-keyboard callbacks for `ask_user` options. +//! +//! Rate limits are reported by Telegram itself (`429` + `retry_after`) and +//! become [`SendError::RateLimited`]; the gateway's outbox waits and retries. +//! +//! 🦀 Rust notes +//! - Responses are parsed as `serde_json::Value` and read with `.get()` / +//! `.as_*()`: only a dozen fields are used, so typed structs for the whole +//! Bot API would be mostly dead weight. + +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use async_trait::async_trait; +use serde_json::{Value, json}; +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +use super::{ + Channel, ChannelCaps, ChannelKind, InboundMessage, MarkdownDialect, MediaKind, MediaRef, + MsgHandle, OutboundMessage, PeerKey, ReplyCtx, SendError, TextFormat, markdown, store, +}; + +pub const DEFAULT_API_BASE: &str = "https://api.telegram.org"; +const POLL_TIMEOUT_SECS: u64 = 30; + +pub struct Telegram { + http: reqwest::Client, + api_base: String, + token: String, + bot_id: String, + bot_username: String, + /// Accept group messages that mention the bot (`[channels.telegram] groups`). + groups: bool, +} + +/// The parts of a Bot API reply the adapter needs. +fn parse_reply(status: reqwest::StatusCode, body: &str) -> Result { + let v: Value = serde_json::from_str(body) + .map_err(|_| SendError::Transient(format!("HTTP {status}: {}", truncate(body, 200))))?; + if v.get("ok").and_then(Value::as_bool) == Some(true) { + return Ok(v.get("result").cloned().unwrap_or(Value::Null)); + } + let code = v.get("error_code").and_then(Value::as_i64).unwrap_or(0); + let desc = v + .get("description") + .and_then(Value::as_str) + .unwrap_or("unknown error") + .to_string(); + Err(match code { + 429 => SendError::RateLimited(Duration::from_secs( + v.pointer("/parameters/retry_after") + .and_then(Value::as_u64) + .unwrap_or(3), + )), + 401 | 404 if desc.contains("Unauthorized") || desc.contains("Not Found") => { + SendError::AuthExpired(desc) + } + c if c >= 500 => SendError::Transient(desc), + _ => SendError::Other(desc), + }) +} + +fn truncate(s: &str, n: usize) -> String { + s.chars().take(n).collect() +} + +/// Build the reqwest client (optional proxy: `api.telegram.org` is often +/// blocked and needs one). +pub fn http_client(proxy: Option<&str>, timeout: Duration) -> anyhow::Result { + let mut b = reqwest::Client::builder() + .timeout(timeout) + .connect_timeout(Duration::from_secs(15)); + if let Some(p) = proxy.filter(|p| !p.trim().is_empty()) { + b = b.proxy(reqwest::Proxy::all(p.trim())?); + } + Ok(b.build()?) +} + +/// `getMe`: (bot id, username). Used by login and at gateway start. +pub async fn get_me( + http: &reqwest::Client, + api_base: &str, + token: &str, +) -> Result<(String, String), SendError> { + let url = format!("{}/bot{token}/getMe", api_base.trim_end_matches('/')); + let resp = http + .get(&url) + .send() + .await + .map_err(|e| SendError::Transient(e.without_url().to_string()))?; + let status = resp.status(); + let body = resp.text().await.unwrap_or_default(); + let me = parse_reply(status, &body)?; + Ok(( + me.get("id").map(id_string).unwrap_or_default(), + me.get("username") + .and_then(Value::as_str) + .unwrap_or("") + .to_string(), + )) +} + +fn id_string(v: &Value) -> String { + match v { + Value::Number(n) => n.to_string(), + Value::String(s) => s.clone(), + _ => String::new(), + } +} + +impl Telegram { + pub fn new( + http: reqwest::Client, + api_base: &str, + token: &str, + bot_id: &str, + bot_username: &str, + groups: bool, + ) -> Self { + Self { + http, + api_base: api_base.trim_end_matches('/').to_string(), + token: token.to_string(), + bot_id: bot_id.to_string(), + bot_username: bot_username.to_string(), + groups, + } + } + + fn url(&self, method: &str) -> String { + format!("{}/bot{}/{method}", self.api_base, self.token) + } + + async fn call(&self, method: &str, body: Value, timeout: Duration) -> Result { + let resp = self + .http + .post(self.url(method)) + .timeout(timeout) + .json(&body) + .send() + .await + // without_url: the URL carries the bot token. + .map_err(|e| SendError::Transient(e.without_url().to_string()))?; + let status = resp.status(); + let text = resp + .text() + .await + .map_err(|e| SendError::Transient(e.without_url().to_string()))?; + parse_reply(status, &text) + } + + fn cursor_name(&self) -> String { + format!("telegram-{}", self.bot_id) + } + + /// Turn one `Update` into an inbound message (None: not for us). + pub fn parse_update(&self, u: &Value) -> Option { + if let Some(cb) = u.get("callback_query") { + return self.parse_callback(cb); + } + let m = u.get("message").or_else(|| u.get("edited_message"))?; + let chat = m.get("chat")?; + let chat_id = id_string(chat.get("id")?); + let chat_type = chat.get("type").and_then(Value::as_str).unwrap_or(""); + let is_group = chat_type != "private"; + let from = m.get("from")?; + if from.get("is_bot").and_then(Value::as_bool) == Some(true) { + return None; + } + let sender = id_string(from.get("id")?); + let sender_name = from + .get("username") + .or_else(|| from.get("first_name")) + .and_then(Value::as_str) + .unwrap_or("") + .to_string(); + let thread = (m.get("is_topic_message").and_then(Value::as_bool) == Some(true)) + .then(|| m.get("message_thread_id").map(id_string)) + .flatten(); + let mut text = m + .get("text") + .or_else(|| m.get("caption")) + .and_then(Value::as_str) + .unwrap_or("") + .to_string(); + let mention = format!("@{}", self.bot_username); + let mentioned = !self.bot_username.is_empty() && text.contains(&mention); + if mentioned { + text = text.replace(&mention, "").trim().to_string(); + } + let replied_to_bot = m + .pointer("/reply_to_message/from/id") + .map(id_string) + .is_some_and(|id| id == self.bot_id); + // `/cmd@thisbot` in groups. + if let Some(rest) = text.strip_prefix('/') + && let Some((cmd, tail)) = rest.split_once(' ').or(Some((rest, ""))) + && let Some((c, bot)) = cmd.split_once('@') + && bot.eq_ignore_ascii_case(&self.bot_username) + { + text = format!("/{c} {tail}").trim().to_string(); + } + let mut media = Vec::new(); + if let Some(photos) = m.get("photo").and_then(Value::as_array) + && let Some(best) = photos.last() + { + media.push(MediaRef { + kind: MediaKind::Image, + name: None, + size: best.get("file_size").and_then(Value::as_u64), + data: json!({"file_id": best.get("file_id")}), + }); + } + for (key, kind) in [ + ("document", MediaKind::File), + ("voice", MediaKind::Voice), + ("audio", MediaKind::Voice), + ("video", MediaKind::Video), + ] { + if let Some(d) = m.get(key) { + media.push(MediaRef { + kind, + name: d.get("file_name").and_then(Value::as_str).map(String::from), + size: d.get("file_size").and_then(Value::as_u64), + data: json!({"file_id": d.get("file_id")}), + }); + } + } + if text.is_empty() && media.is_empty() { + return None; + } + Some(InboundMessage { + peer: PeerKey { + channel: ChannelKind::Telegram, + account: self.bot_id.clone(), + chat: chat_id, + thread, + }, + sender, + sender_name, + msg_id: format!( + "{}:{}", + m.pointer("/chat/id").map(id_string).unwrap_or_default(), + m.get("message_id").map(id_string).unwrap_or_default() + ), + text, + media, + reply_ctx: ReplyCtx::default(), + is_group, + addressed: !is_group || (self.groups && (mentioned || replied_to_bot)), + }) + } + + /// An inline-keyboard press: becomes the option number as text, the + /// same answer `ask_user` accepts when typed. + fn parse_callback(&self, cb: &Value) -> Option { + let data = cb.get("data").and_then(Value::as_str)?.to_string(); + let m = cb.get("message")?; + let chat = m.get("chat")?; + let is_group = chat.get("type").and_then(Value::as_str) != Some("private"); + let from = cb.get("from")?; + let thread = (m.get("is_topic_message").and_then(Value::as_bool) == Some(true)) + .then(|| m.get("message_thread_id").map(id_string)) + .flatten(); + Some(InboundMessage { + peer: PeerKey { + channel: ChannelKind::Telegram, + account: self.bot_id.clone(), + chat: id_string(chat.get("id")?), + thread, + }, + sender: id_string(from.get("id")?), + sender_name: from + .get("username") + .and_then(Value::as_str) + .unwrap_or("") + .to_string(), + msg_id: format!("cb:{}", cb.get("id").map(id_string).unwrap_or_default()), + text: data, + media: vec![], + reply_ctx: ReplyCtx(cb.get("id").map(id_string)), + is_group, + addressed: true, + }) + } + + fn base_body(peer: &PeerKey) -> serde_json::Map { + let mut b = serde_json::Map::new(); + b.insert("chat_id".into(), json!(peer.chat)); + if let Some(t) = &peer.thread { + b.insert( + "message_thread_id".into(), + json!(t.parse::().unwrap_or(0)), + ); + } + b + } + + /// Send or edit text; on an HTML parse error, retry once as plain text. + async fn send_text( + &self, + peer: &PeerKey, + edit: Option<&MsgHandle>, + text: &str, + format: TextFormat, + ) -> Result, SendError> { + let mut body = Self::base_body(peer); + let method = match edit { + Some(h) => { + body.remove("message_thread_id"); + body.insert("message_id".into(), json!(h.0.parse::().unwrap_or(0))); + "editMessageText" + } + None => "sendMessage", + }; + body.insert("link_preview_options".into(), json!({"is_disabled": true})); + let html = format == TextFormat::Markdown; + let rendered = if html { + markdown::render(text, MarkdownDialect::TelegramHtml) + } else { + text.to_string() + }; + body.insert("text".into(), json!(rendered)); + if html { + body.insert("parse_mode".into(), json!("HTML")); + } + let r = match self + .call(method, Value::Object(body.clone()), Duration::from_secs(20)) + .await + { + Err(SendError::Other(d)) if html && d.contains("can't parse entities") => { + body.remove("parse_mode"); + body.insert( + "text".into(), + json!(markdown::render(text, MarkdownDialect::Plain)), + ); + self.call(method, Value::Object(body), Duration::from_secs(20)) + .await + } + // Editing to identical text is not an error for us. + Err(SendError::Other(d)) if d.contains("message is not modified") => { + return Ok(edit.cloned()); + } + r => r, + }?; + Ok(r.get("message_id") + .map(id_string) + .map(MsgHandle) + .or_else(|| edit.cloned())) + } + + async fn send_file( + &self, + peer: &PeerKey, + path: &Path, + caption: Option<&str>, + ) -> Result, SendError> { + let bytes = tokio::fs::read(path) + .await + .map_err(|e| SendError::Other(format!("cannot read {}: {e}", path.display())))?; + if bytes.len() > 50 * 1024 * 1024 { + return Err(SendError::Other( + "Telegram bots can send files up to 50 MB".into(), + )); + } + let name = path + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_else(|| "file".into()); + let mut fields = vec![("chat_id", peer.chat.clone())]; + if let Some(t) = &peer.thread { + fields.push(("message_thread_id", t.clone())); + } + if let Some(c) = caption.filter(|c| !c.is_empty()) { + fields.push(("caption", truncate(c, 1000))); + } + let (ct, body) = super::multipart_body(&fields, "document", &name, &bytes); + let resp = self + .http + .post(self.url("sendDocument")) + .timeout(Duration::from_secs(300)) + .header(reqwest::header::CONTENT_TYPE, ct) + .body(body) + .send() + .await + .map_err(|e| SendError::Transient(e.without_url().to_string()))?; + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + let r = parse_reply(status, &text)?; + Ok(r.get("message_id").map(id_string).map(MsgHandle)) + } +} + +#[async_trait] +impl Channel for Telegram { + fn kind(&self) -> ChannelKind { + ChannelKind::Telegram + } + fn account(&self) -> String { + self.bot_id.clone() + } + fn caps(&self) -> ChannelCaps { + ChannelCaps { + max_text_chars: 4096, + edit: true, + markdown: MarkdownDialect::TelegramHtml, + min_send_interval: Duration::from_millis(1100), + typing_ttl: Duration::from_secs(5), + } + } + + async fn run( + &self, + inbox: mpsc::Sender, + cancel: CancellationToken, + ) -> anyhow::Result<()> { + let cursor = self.cursor_name(); + let mut offset: i64 = store::read_cursor(&cursor) + .and_then(|s| s.parse().ok()) + .unwrap_or(0); + let mut backoff = 1u64; + loop { + let body = json!({ + "offset": offset, + "timeout": POLL_TIMEOUT_SECS, + "allowed_updates": ["message", "callback_query"], + }); + let poll = self.call( + "getUpdates", + body, + Duration::from_secs(POLL_TIMEOUT_SECS + 15), + ); + let res = tokio::select! { + _ = cancel.cancelled() => return Ok(()), + r = poll => r, + }; + let updates = match res { + Ok(v) => { + backoff = 1; + v.as_array().cloned().unwrap_or_default() + } + Err(SendError::AuthExpired(d)) => { + anyhow::bail!( + "Telegram rejected the bot token ({d}); run `limbo channels login telegram`" + ) + } + Err(SendError::RateLimited(d)) => { + tokio::time::sleep(d).await; + continue; + } + Err(e) => { + eprintln!("telegram: getUpdates failed: {e}; retrying in {backoff}s"); + tokio::select! { + _ = cancel.cancelled() => return Ok(()), + _ = tokio::time::sleep(Duration::from_secs(backoff)) => {} + } + backoff = (backoff * 2).min(60); + continue; + } + }; + for u in updates { + let id = u.get("update_id").and_then(Value::as_i64).unwrap_or(0); + if let Some(cb) = u.get("callback_query") + && let Some(cid) = cb.get("id") + { + let _ = self + .call( + "answerCallbackQuery", + json!({"callback_query_id": cid}), + Duration::from_secs(10), + ) + .await; + } + if let Some(msg) = self.parse_update(&u) + && inbox.send(msg).await.is_err() + { + return Ok(()); + } + // The update is in the gateway's hands: never fetch it again. + offset = offset.max(id + 1); + store::write_cursor(&cursor, &offset.to_string()); + } + } + } + + async fn send( + &self, + peer: &PeerKey, + _ctx: &ReplyCtx, + msg: OutboundMessage, + ) -> Result, SendError> { + match msg { + OutboundMessage::Text { text, format } => { + self.send_text(peer, None, &text, format).await + } + OutboundMessage::Edit { + handle, + text, + format, + } => self.send_text(peer, Some(&handle), &text, format).await, + OutboundMessage::File { path, caption } => { + self.send_file(peer, &path, caption.as_deref()).await + } + OutboundMessage::Typing => { + let mut body = Self::base_body(peer); + body.insert("action".into(), json!("typing")); + self.call( + "sendChatAction", + Value::Object(body), + Duration::from_secs(10), + ) + .await + .map(|_| None) + } + OutboundMessage::Choices { question, options } => { + let mut body = Self::base_body(peer); + body.insert("text".into(), json!(question)); + let rows: Vec = options + .iter() + .enumerate() + .map(|(i, o)| { + json!([{"text": truncate(&format!("{}. {o}", i + 1), 60), + "callback_data": (i + 1).to_string()}]) + }) + .collect(); + body.insert("reply_markup".into(), json!({"inline_keyboard": rows})); + let r = self + .call("sendMessage", Value::Object(body), Duration::from_secs(20)) + .await?; + Ok(r.get("message_id").map(id_string).map(MsgHandle)) + } + } + } + + async fn fetch_media( + &self, + media: &MediaRef, + dir: &Path, + max_bytes: u64, + ) -> anyhow::Result { + if media.size.is_some_and(|s| s > max_bytes) { + anyhow::bail!("file too large ({} bytes)", media.size.unwrap_or(0)); + } + let file_id = media + .data + .get("file_id") + .and_then(Value::as_str) + .ok_or_else(|| anyhow::anyhow!("missing file_id"))?; + let f = self + .call( + "getFile", + json!({"file_id": file_id}), + Duration::from_secs(20), + ) + .await?; + let fpath = f + .get("file_path") + .and_then(Value::as_str) + .ok_or_else(|| anyhow::anyhow!("getFile returned no file_path"))?; + let url = format!("{}/file/bot{}/{fpath}", self.api_base, self.token); + let resp = self + .http + .get(&url) + .timeout(Duration::from_secs(300)) + .send() + .await + .map_err(|e| anyhow::anyhow!("download failed: {}", e.without_url()))?; + if !resp.status().is_success() { + anyhow::bail!("download failed: HTTP {}", resp.status()); + } + let bytes = resp + .bytes() + .await + .map_err(|e| anyhow::anyhow!("download failed: {}", e.without_url()))?; + if bytes.len() as u64 > max_bytes { + anyhow::bail!("file too large ({} bytes)", bytes.len()); + } + let name = media.name.clone().unwrap_or_else(|| { + Path::new(fpath) + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_else(|| "file".into()) + }); + std::fs::create_dir_all(dir)?; + let dest = dir.join(super::safe_file_name(&name)); + tokio::fs::write(&dest, &bytes).await?; + Ok(dest) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn bot() -> Telegram { + Telegram::new( + reqwest::Client::new(), + DEFAULT_API_BASE, + "T", + "999", + "limbo_bot", + true, + ) + } + + #[test] + fn private_text_message() { + let u = json!({"update_id": 5, "message": { + "message_id": 11, "chat": {"id": 42, "type": "private"}, + "from": {"id": 42, "is_bot": false, "username": "jack"}, "text": "hello"}}); + let m = bot().parse_update(&u).unwrap(); + assert_eq!(m.peer.key(), "telegram:999:42"); + assert_eq!(m.sender, "42"); + assert_eq!(m.msg_id, "42:11"); + assert!(m.addressed && !m.is_group); + } + + #[test] + fn group_needs_mention_and_strips_it() { + let plain = json!({"update_id": 6, "message": { + "message_id": 12, "chat": {"id": -100, "type": "supergroup"}, + "from": {"id": 42, "is_bot": false}, "text": "just chatting"}}); + assert!(!bot().parse_update(&plain).unwrap().addressed); + let mention = json!({"update_id": 7, "message": { + "message_id": 13, "chat": {"id": -100, "type": "supergroup"}, + "from": {"id": 42, "is_bot": false}, "text": "@limbo_bot fix the build"}}); + let m = bot().parse_update(&mention).unwrap(); + assert!(m.addressed); + assert_eq!(m.text, "fix the build"); + let cmd = json!({"update_id": 8, "message": { + "message_id": 14, "chat": {"id": -100, "type": "supergroup"}, + "from": {"id": 42, "is_bot": false}, "text": "/new@limbo_bot"}}); + assert_eq!(bot().parse_update(&cmd).unwrap().text, "/new"); + } + + #[test] + fn photo_and_callback() { + let u = json!({"update_id": 9, "message": { + "message_id": 15, "chat": {"id": 42, "type": "private"}, + "from": {"id": 42, "is_bot": false}, "caption": "看这个", + "photo": [{"file_id": "small", "file_size": 10}, {"file_id": "big", "file_size": 99}]}}); + let m = bot().parse_update(&u).unwrap(); + assert_eq!(m.text, "看这个"); + assert_eq!(m.media[0].data["file_id"], "big"); + let cb = json!({"update_id": 10, "callback_query": {"id": "c1", "data": "2", + "from": {"id": 42}, "message": {"message_id": 3, "chat": {"id": 42, "type": "private"}}}}); + let m = bot().parse_update(&cb).unwrap(); + assert_eq!(m.text, "2"); + } + + #[test] + fn errors_map_to_send_errors() { + let r = parse_reply( + reqwest::StatusCode::TOO_MANY_REQUESTS, + r#"{"ok":false,"error_code":429,"description":"Too Many Requests","parameters":{"retry_after":7}}"#, + ); + assert!(matches!(r, Err(SendError::RateLimited(d)) if d == Duration::from_secs(7))); + let r = parse_reply( + reqwest::StatusCode::UNAUTHORIZED, + r#"{"ok":false,"error_code":401,"description":"Unauthorized"}"#, + ); + assert!(matches!(r, Err(SendError::AuthExpired(_)))); + } +} diff --git a/src/channels/weixin/api.rs b/src/channels/weixin/api.rs new file mode 100644 index 0000000..bc299b7 --- /dev/null +++ b/src/channels/weixin/api.rs @@ -0,0 +1,297 @@ +//! The iLink bot HTTP API (`https://ilinkai.weixin.qq.com/ilink/bot/...`): +//! JSON over POST, `Authorization: Bearer `. Mirrors the +//! endpoints of Tencent's own reference plugin (`@tencent-weixin/openclaw-weixin`, +//! MIT): `getupdates` (long poll), `sendmessage`, `sendtyping`, `getconfig`, +//! `getuploadurl`, and the QR-code login pair `get_bot_qrcode` / +//! `get_qrcode_status`. + +use std::time::Duration; + +use base64::Engine; +use serde_json::{Value, json}; + +use crate::channels::SendError; + +pub const DEFAULT_BASE_URL: &str = "https://ilinkai.weixin.qq.com"; +pub const CDN_BASE_URL: &str = "https://novac2c.cdn.weixin.qq.com/c2c"; +/// The server's "bot token expired" error code. +pub const STALE_TOKEN_ERRCODE: i64 = -14; +/// `MessageType`: messages from a user (bots' own messages are 2). +pub const MESSAGE_TYPE_USER: i64 = 1; +pub const MESSAGE_TYPE_BOT: i64 = 2; +pub const MESSAGE_STATE_FINISH: i64 = 2; +/// `MessageItemType`. +pub const ITEM_TEXT: i64 = 1; +pub const ITEM_IMAGE: i64 = 2; +pub const ITEM_VOICE: i64 = 3; +pub const ITEM_FILE: i64 = 4; +pub const ITEM_VIDEO: i64 = 5; +/// `UploadMediaType`. +pub const UPLOAD_IMAGE: i64 = 1; +pub const UPLOAD_FILE: i64 = 3; + +pub const LONG_POLL_TIMEOUT: Duration = Duration::from_secs(35); +const API_TIMEOUT: Duration = Duration::from_secs(15); + +/// Request identity sent with every call (`[channels.weixin]`). +#[derive(Debug, Clone)] +pub struct Identity { + /// `iLink-App-Id` header. + pub app_id: String, + /// `iLink-App-ClientVersion` header (`major<<16 | minor<<8 | patch`). + pub client_version: u32, + /// `base_info.bot_agent`: who is calling, for Tencent's logs. + pub bot_agent: String, + pub channel_version: String, +} + +impl Identity { + pub fn new(app_id: &str, client_version: &str) -> Self { + Self { + app_id: app_id.to_string(), + client_version: encode_version(client_version), + bot_agent: format!("limbo/{}", env!("CARGO_PKG_VERSION")), + channel_version: env!("CARGO_PKG_VERSION").to_string(), + } + } + + fn base_info(&self) -> Value { + json!({"channel_version": self.channel_version, "bot_agent": self.bot_agent}) + } +} + +/// `"2.4.10"` → `0x02040A`. +pub fn encode_version(v: &str) -> u32 { + let mut parts = v + .split('.') + .map(|p| p.trim().parse::().unwrap_or(0) & 0xff); + let a = parts.next().unwrap_or(0); + let b = parts.next().unwrap_or(0); + let c = parts.next().unwrap_or(0); + (a << 16) | (b << 8) | c +} + +/// `X-WECHAT-UIN`: a random uint32, decimal, base64-encoded. +fn random_uin() -> String { + let n: u32 = rand::random(); + base64::engine::general_purpose::STANDARD.encode(n.to_string()) +} + +pub struct Api { + pub http: reqwest::Client, + pub base_url: String, + pub token: String, + pub id: Identity, +} + +impl Api { + fn endpoint(&self, path: &str) -> String { + format!("{}/{path}", self.base_url.trim_end_matches('/')) + } + + fn request(&self, rb: reqwest::RequestBuilder) -> reqwest::RequestBuilder { + let mut rb = rb + .header("AuthorizationType", "ilink_bot_token") + .header("X-WECHAT-UIN", random_uin()) + .header("iLink-App-Id", &self.id.app_id) + .header( + "iLink-App-ClientVersion", + self.id.client_version.to_string(), + ); + if !self.token.is_empty() { + rb = rb.bearer_auth(&self.token); + } + rb + } + + /// POST `body` (+ `base_info`) and parse the JSON reply. + pub async fn post( + &self, + path: &str, + mut body: Value, + timeout: Duration, + ) -> Result { + if let Some(o) = body.as_object_mut() { + o.insert("base_info".into(), self.id.base_info()); + } + let resp = self + .request(self.http.post(self.endpoint(path))) + .timeout(timeout) + .json(&body) + .send() + .await + .map_err(|e| { + if e.is_timeout() { + SendError::Transient("timeout".into()) + } else { + SendError::Transient(e.to_string()) + } + })?; + let status = resp.status(); + let text = resp + .text() + .await + .map_err(|e| SendError::Transient(e.to_string()))?; + if status.is_server_error() { + return Err(SendError::Transient(format!("HTTP {status}"))); + } + if !status.is_success() { + return Err(SendError::Other(format!( + "HTTP {status}: {}", + text.chars().take(200).collect::() + ))); + } + let v: Value = serde_json::from_str(&text) + .map_err(|_| SendError::Transient(format!("bad JSON from {path}")))?; + check_ret(&v)?; + Ok(v) + } + + pub async fn get_updates(&self, buf: &str) -> Result { + match self + .post( + "ilink/bot/getupdates", + json!({"get_updates_buf": buf}), + LONG_POLL_TIMEOUT + Duration::from_secs(5), + ) + .await + { + // A client-side timeout of the long poll is an empty answer. + Err(SendError::Transient(t)) if t == "timeout" => { + Ok(json!({"ret": 0, "msgs": [], "get_updates_buf": buf})) + } + r => r, + } + } + + pub async fn send_items( + &self, + to: &str, + context_token: Option<&str>, + items: Vec, + ) -> Result { + let client_id = format!("limbo-{}", crate::agent::new_session_id()); + let mut msg = json!({ + "from_user_id": "", + "to_user_id": to, + "client_id": client_id, + "message_type": MESSAGE_TYPE_BOT, + "message_state": MESSAGE_STATE_FINISH, + "item_list": items, + }); + if let Some(t) = context_token { + msg["context_token"] = json!(t); + } + self.post("ilink/bot/sendmessage", json!({"msg": msg}), API_TIMEOUT) + .await + } + + pub async fn get_config( + &self, + user: &str, + context_token: Option<&str>, + ) -> Result { + self.post( + "ilink/bot/getconfig", + json!({"ilink_user_id": user, "context_token": context_token}), + Duration::from_secs(10), + ) + .await + } + + pub async fn send_typing(&self, user: &str, ticket: &str) -> Result { + self.post( + "ilink/bot/sendtyping", + json!({"ilink_user_id": user, "typing_ticket": ticket, "status": 1}), + Duration::from_secs(10), + ) + .await + } + + pub async fn get_upload_url(&self, req: Value) -> Result { + self.post("ilink/bot/getuploadurl", req, API_TIMEOUT).await + } + + /// Unauthenticated GET (QR login). + pub async fn get(&self, path_and_query: &str, timeout: Duration) -> Result { + let resp = self + .http + .get(self.endpoint(path_and_query)) + .header("iLink-App-Id", &self.id.app_id) + .header( + "iLink-App-ClientVersion", + self.id.client_version.to_string(), + ) + .timeout(timeout) + .send() + .await + .map_err(|e| { + if e.is_timeout() { + SendError::Transient("timeout".into()) + } else { + SendError::Transient(e.to_string()) + } + })?; + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + if !status.is_success() { + return Err(SendError::Other(format!("HTTP {status}"))); + } + serde_json::from_str(&text).map_err(|_| SendError::Other("bad JSON".into())) + } +} + +/// `ret` / `errcode` → an error. +pub fn check_ret(v: &Value) -> Result<(), SendError> { + let ret = v.get("ret").and_then(Value::as_i64).unwrap_or(0); + let errcode = v.get("errcode").and_then(Value::as_i64).unwrap_or(0); + if ret == 0 && errcode == 0 { + return Ok(()); + } + let msg = v + .get("errmsg") + .and_then(Value::as_str) + .unwrap_or("") + .to_string(); + if ret == STALE_TOKEN_ERRCODE || errcode == STALE_TOKEN_ERRCODE { + return Err(SendError::AuthExpired(format!("bot token expired ({msg})"))); + } + let lower = msg.to_lowercase(); + if lower.contains("context") || (lower.contains("token") && lower.contains("expire")) { + return Err(SendError::ContextExpired); + } + if lower.contains("freq") || lower.contains("limit") || msg.contains("频") { + return Err(SendError::RateLimited(Duration::from_secs(5))); + } + Err(SendError::Other(format!( + "ret={ret} errcode={errcode} {msg}" + ))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn versions_encode_like_the_reference_plugin() { + assert_eq!(encode_version("2.4.10"), 0x02040A); + assert_eq!(encode_version("1.0"), 0x010000); + } + + #[test] + fn ret_codes() { + assert!(check_ret(&json!({"ret": 0})).is_ok()); + assert!(matches!( + check_ret(&json!({"ret": -14, "errmsg": "session timeout"})), + Err(SendError::AuthExpired(_)) + )); + assert!(matches!( + check_ret(&json!({"ret": 1, "errmsg": "context token invalid"})), + Err(SendError::ContextExpired) + )); + assert!(matches!( + check_ret(&json!({"ret": 3})), + Err(SendError::Other(_)) + )); + } +} diff --git a/src/channels/weixin/cdn.rs b/src/channels/weixin/cdn.rs new file mode 100644 index 0000000..72f4fe3 --- /dev/null +++ b/src/channels/weixin/cdn.rs @@ -0,0 +1,260 @@ +//! WeChat media lives on Tencent's CDN, encrypted with AES-128-ECB +//! (PKCS#7 padding) under a per-file random key: uploads encrypt before +//! `POST`, downloads decrypt after `GET`. +//! +//! 🦀 Rust notes +//! - The `aes` crate only provides the block cipher; ECB "mode" is just +//! encrypting every 16-byte block on its own, done here by hand. +//! - `GenericArray::from_mut_slice` views a `[u8]` chunk as the fixed-size +//! block type the cipher wants, without copying. + +use aes::Aes128; +use aes::cipher::generic_array::GenericArray; +use aes::cipher::{BlockDecrypt, BlockEncrypt, KeyInit}; +use base64::Engine; +use md5::{Digest, Md5}; +use serde_json::{Value, json}; + +use super::api::{Api, CDN_BASE_URL}; + +pub fn encrypt(plain: &[u8], key: &[u8; 16]) -> Vec { + let cipher = Aes128::new(GenericArray::from_slice(key)); + let pad = 16 - plain.len() % 16; + let mut buf = Vec::with_capacity(plain.len() + pad); + buf.extend_from_slice(plain); + buf.extend(std::iter::repeat_n(pad as u8, pad)); + for block in buf.chunks_mut(16) { + cipher.encrypt_block(GenericArray::from_mut_slice(block)); + } + buf +} + +pub fn decrypt(data: &[u8], key: &[u8; 16]) -> anyhow::Result> { + if data.is_empty() || !data.len().is_multiple_of(16) { + anyhow::bail!("ciphertext length {} is not a multiple of 16", data.len()); + } + let cipher = Aes128::new(GenericArray::from_slice(key)); + let mut buf = data.to_vec(); + for block in buf.chunks_mut(16) { + cipher.decrypt_block(GenericArray::from_mut_slice(block)); + } + let pad = *buf.last().unwrap_or(&0) as usize; + if pad == 0 + || pad > 16 + || buf.len() < pad + || !buf[buf.len() - pad..].iter().all(|b| *b as usize == pad) + { + anyhow::bail!("bad padding (wrong key?)"); + } + buf.truncate(buf.len() - pad); + Ok(buf) +} + +/// Ciphertext size for `n` plaintext bytes. +#[cfg(test)] +pub fn padded_size(n: usize) -> usize { + (n / 16 + 1) * 16 +} + +fn hex(bytes: &[u8]) -> String { + bytes.iter().map(|b| format!("{b:02x}")).collect() +} + +fn unhex(s: &str) -> Option> { + if !s.len().is_multiple_of(2) { + return None; + } + (0..s.len()) + .step_by(2) + .map(|i| u8::from_str_radix(s.get(i..i + 2)?, 16).ok()) + .collect() +} + +/// A media key as the API sends it: base64 of 16 raw bytes, base64 of a +/// 32-char hex string, or (image_item.aeskey) the hex string itself. +pub fn parse_key(s: &str) -> Option<[u8; 16]> { + let raw = if s.len() == 32 && s.chars().all(|c| c.is_ascii_hexdigit()) { + unhex(s)? + } else { + let d = base64::engine::general_purpose::STANDARD.decode(s).ok()?; + if d.len() == 32 && d.iter().all(u8::is_ascii_hexdigit) { + unhex(std::str::from_utf8(&d).ok()?)? + } else { + d + } + }; + raw.try_into().ok() +} + +/// Download (and decrypt, with a key) one CDN media reference. +pub async fn download( + http: &reqwest::Client, + media: &Value, + key: Option<[u8; 16]>, + max_bytes: u64, +) -> anyhow::Result> { + let url = match media.get("full_url").and_then(Value::as_str) { + Some(u) if !u.is_empty() => u.to_string(), + _ => { + let q = media + .get("encrypt_query_param") + .and_then(Value::as_str) + .ok_or_else(|| anyhow::anyhow!("media has no download parameters"))?; + let mut u = url::Url::parse(&format!("{CDN_BASE_URL}/download"))?; + u.query_pairs_mut().append_pair("encrypted_query_param", q); + u.to_string() + } + }; + let resp = http + .get(&url) + .timeout(std::time::Duration::from_secs(300)) + .send() + .await?; + if !resp.status().is_success() { + anyhow::bail!("CDN download failed: HTTP {}", resp.status()); + } + let bytes = resp.bytes().await?; + if bytes.len() as u64 > max_bytes + 16 { + anyhow::bail!("file too large ({} bytes)", bytes.len()); + } + match key { + Some(k) => decrypt(&bytes, &k), + None => Ok(bytes.to_vec()), + } +} + +/// What a finished upload is referenced by in a message item. +pub struct Uploaded { + pub download_param: String, + pub aes_key: [u8; 16], + pub raw_size: usize, + pub cipher_size: usize, +} + +impl Uploaded { + /// The `media` object of an image / file / video item. + pub fn media_json(&self) -> Value { + json!({ + "encrypt_query_param": self.download_param, + // The reference plugin sends base64 of the *hex* key. + "aes_key": base64::engine::general_purpose::STANDARD.encode(hex(&self.aes_key)), + "encrypt_type": 1, + }) + } +} + +/// Encrypt `plain` and upload it for `to_user`. +pub async fn upload( + api: &Api, + to_user: &str, + media_type: i64, + plain: &[u8], +) -> anyhow::Result { + let key: [u8; 16] = rand::random(); + let filekey = hex(&rand::random::<[u8; 16]>()); + let md5 = hex(&Md5::digest(plain)); + let cipher = encrypt(plain, &key); + let req = json!({ + "filekey": filekey, + "media_type": media_type, + "to_user_id": to_user, + "rawsize": plain.len(), + "rawfilemd5": md5, + "filesize": cipher.len(), + "no_need_thumb": true, + "aeskey": hex(&key), + }); + let r = api.get_upload_url(req).await?; + let url = match r + .get("upload_full_url") + .and_then(Value::as_str) + .map(str::trim) + { + Some(u) if !u.is_empty() => u.to_string(), + _ => { + let p = r + .get("upload_param") + .and_then(Value::as_str) + .ok_or_else(|| anyhow::anyhow!("getuploadurl returned no upload URL"))?; + let mut u = url::Url::parse(&format!("{CDN_BASE_URL}/upload"))?; + u.query_pairs_mut() + .append_pair("encrypted_query_param", p) + .append_pair("filekey", &filekey); + u.to_string() + } + }; + let mut last = String::new(); + for attempt in 1..=3 { + let resp = api + .http + .post(&url) + .header(reqwest::header::CONTENT_TYPE, "application/octet-stream") + .timeout(std::time::Duration::from_secs(300)) + .body(cipher.clone()) + .send() + .await; + match resp { + Ok(r) if r.status().is_success() => { + let param = r + .headers() + .get("x-encrypted-param") + .and_then(|h| h.to_str().ok()) + .map(String::from) + .ok_or_else(|| anyhow::anyhow!("CDN upload: no x-encrypted-param header"))?; + return Ok(Uploaded { + download_param: param, + aes_key: key, + raw_size: plain.len(), + cipher_size: cipher.len(), + }); + } + Ok(r) if r.status().is_client_error() => { + anyhow::bail!("CDN upload rejected: HTTP {}", r.status()) + } + Ok(r) => last = format!("HTTP {}", r.status()), + Err(e) => last = e.to_string(), + } + tokio::time::sleep(std::time::Duration::from_secs(attempt)).await; + } + anyhow::bail!("CDN upload failed: {last}") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn aes_ecb_round_trip_with_pkcs7() { + let key = [7u8; 16]; + for n in [0usize, 1, 15, 16, 17, 100] { + let plain: Vec = (0..n as u8).collect(); + let c = encrypt(&plain, &key); + assert_eq!(c.len(), padded_size(n)); + assert_eq!(decrypt(&c, &key).unwrap(), plain); + } + assert!(decrypt(&encrypt(b"x", &key), &[8u8; 16]).is_err()); + } + + #[test] + fn aes_known_vector() { + // FIPS-197 appendix C.1 (one block, no padding check: compare prefix). + let key: [u8; 16] = unhex("000102030405060708090a0b0c0d0e0f") + .unwrap() + .try_into() + .unwrap(); + let plain = unhex("00112233445566778899aabbccddeeff").unwrap(); + let c = encrypt(&plain, &key); + assert_eq!(hex(&c[..16]), "69c4e0d86a7b0430d8cdb78070b4c55a"); + } + + #[test] + fn keys_in_every_encoding() { + let raw = [0xabu8; 16]; + let b64_raw = base64::engine::general_purpose::STANDARD.encode(raw); + let b64_hex = base64::engine::general_purpose::STANDARD.encode(hex(&raw)); + assert_eq!(parse_key(&b64_raw), Some(raw)); + assert_eq!(parse_key(&b64_hex), Some(raw)); + assert_eq!(parse_key(&hex(&raw)), Some(raw)); + assert_eq!(parse_key("nope"), None); + } +} diff --git a/src/channels/weixin/login.rs b/src/channels/weixin/login.rs new file mode 100644 index 0000000..059c47b --- /dev/null +++ b/src/channels/weixin/login.rs @@ -0,0 +1,171 @@ +//! `limbo channels login weixin`: show a QR code in the terminal, wait for +//! the phone to scan and confirm, and return the bot credentials. The +//! status values follow the reference plugin's `get_qrcode_status` loop. + +use std::io::Write; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; + +use super::api::{Api, DEFAULT_BASE_URL, Identity}; +use crate::channels::SendError; +use crate::channels::store::WeixinCred; + +/// `bot_type` of the QR code request. +const BOT_TYPE: &str = "3"; +const MAX_REFRESH: u32 = 3; + +/// Render `content` as a terminal QR code (two rows per character line). +pub fn qr_text(content: &str) -> Option { + let code = qrcode::QrCode::new(content.as_bytes()).ok()?; + Some( + code.render::() + .dark_color(qrcode::render::unicode::Dense1x2::Light) + .light_color(qrcode::render::unicode::Dense1x2::Dark) + .quiet_zone(true) + .build(), + ) +} + +async fn fetch_qr(api: &Api) -> anyhow::Result<(String, String)> { + let r = api + .post( + &format!("ilink/bot/get_bot_qrcode?bot_type={BOT_TYPE}"), + json!({"local_token_list": []}), + Duration::from_secs(15), + ) + .await?; + let qrcode = r + .get("qrcode") + .and_then(Value::as_str) + .ok_or_else(|| anyhow::anyhow!("get_bot_qrcode returned no qrcode"))?; + let content = r + .get("qrcode_img_content") + .and_then(Value::as_str) + .unwrap_or(""); + Ok((qrcode.to_string(), content.to_string())) +} + +fn show(content: &str) { + println!(); + match qr_text(content) { + Some(q) => println!("{q}"), + None => println!("(无法在终端绘制二维码)"), + } + println!("用手机微信扫描上面的二维码;扫不了的话在微信里打开这个链接:\n{content}\n"); +} + +fn read_line(prompt: &str) -> String { + print!("{prompt}"); + let _ = std::io::stdout().flush(); + let mut s = String::new(); + let _ = std::io::stdin().read_line(&mut s); + s.trim().to_string() +} + +pub async fn login(http: reqwest::Client, id: Identity) -> anyhow::Result { + let mut api = Api { + http, + base_url: DEFAULT_BASE_URL.to_string(), + token: String::new(), + id, + }; + let (mut qrcode, content) = fetch_qr(&api).await?; + show(&content); + let deadline = Instant::now() + Duration::from_secs(480); + let mut refreshes = 0; + let mut verify: Option = None; + let mut scanned = false; + while Instant::now() < deadline { + let mut q = format!("ilink/bot/get_qrcode_status?qrcode={}", urlencode(&qrcode)); + if let Some(v) = &verify { + q.push_str(&format!("&verify_code={}", urlencode(v))); + } + let r = match api.get(&q, Duration::from_secs(35)).await { + Ok(v) => v, + Err(SendError::Transient(_)) => continue, + Err(e) => anyhow::bail!("{e}"), + }; + match r.get("status").and_then(Value::as_str).unwrap_or("wait") { + "wait" => {} + "scaned" => { + verify = None; + if !std::mem::replace(&mut scanned, true) { + println!("已扫码,请在手机上确认…"); + } + } + "need_verifycode" => { + let p = if verify.is_some() { + "数字不匹配,请重新输入手机微信显示的数字:" + } else { + "输入手机微信显示的数字:" + }; + verify = Some(read_line(p)); + } + "scaned_but_redirect" => { + if let Some(h) = r.get("redirect_host").and_then(Value::as_str) { + api.base_url = format!("https://{h}"); + } + } + "expired" | "verify_code_blocked" => { + refreshes += 1; + if refreshes > MAX_REFRESH { + anyhow::bail!("二维码多次失效,请稍后再试"); + } + println!("二维码已失效,正在刷新…"); + let (q2, c2) = fetch_qr(&api).await?; + qrcode = q2; + verify = None; + scanned = false; + show(&c2); + } + "binded_redirect" => { + anyhow::bail!("这个微信已经绑定过一个 bot;如需重新登录请先在微信里解绑") + } + "confirmed" => { + let token = r.get("bot_token").and_then(Value::as_str).unwrap_or(""); + let bot_id = r.get("ilink_bot_id").and_then(Value::as_str).unwrap_or(""); + if token.is_empty() || bot_id.is_empty() { + anyhow::bail!("登录失败:服务器没有返回 bot_token / ilink_bot_id"); + } + let base = r + .get("baseurl") + .and_then(Value::as_str) + .filter(|b| !b.is_empty()) + .map(String::from) + .unwrap_or_else(|| api.base_url.clone()); + let owner = r + .get("ilink_user_id") + .and_then(Value::as_str) + .unwrap_or("") + .to_string(); + return Ok(WeixinCred { + token: token.to_string(), + bot_id: bot_id.to_string(), + base_url: base, + owners: if owner.is_empty() { + vec![] + } else { + vec![owner] + }, + }); + } + other => eprintln!("未知的登录状态:{other}"), + } + tokio::time::sleep(Duration::from_millis(500)).await; + } + anyhow::bail!("登录超时(8 分钟)") +} + +fn urlencode(s: &str) -> String { + url::form_urlencoded::byte_serialize(s.as_bytes()).collect() +} + +#[cfg(test)] +mod tests { + #[test] + fn qr_renders() { + let q = super::qr_text("https://example.com/x").unwrap(); + assert!(q.lines().count() > 10); + } +} diff --git a/src/channels/weixin/mod.rs b/src/channels/weixin/mod.rs new file mode 100644 index 0000000..e0265ac --- /dev/null +++ b/src/channels/weixin/mod.rs @@ -0,0 +1,446 @@ +//! Personal WeChat through Tencent's official iLink bot channel (the +//! protocol behind the "微信 ClawBot" plugin): QR-code login, long-polled +//! `getupdates`, replies that carry the inbound message's `context_token`, +//! and AES-encrypted CDN media. +//! +//! Limits of the channel, not of this adapter: private chats only (group +//! messages are dropped), no message editing, and replies are only possible +//! for a while after the user's last message — a reply that comes too late +//! fails with [`SendError::ContextExpired`] and the gateway parks it until +//! the user writes again. +//! +//! The channel was built for OpenClaw; third-party clients use it at their +//! own risk (see the design doc). Requests identify themselves honestly as +//! `limbo/` in `base_info.bot_agent`. + +pub mod api; +pub mod cdn; +pub mod login; + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::Mutex; +use std::time::Duration; + +use async_trait::async_trait; +use serde_json::{Value, json}; +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +use super::{ + Channel, ChannelCaps, ChannelKind, InboundMessage, MarkdownDialect, MediaKind, MediaRef, + MsgHandle, OutboundMessage, PeerKey, ReplyCtx, SendError, TextFormat, markdown, store, +}; +use api::{Api, ITEM_FILE, ITEM_IMAGE, ITEM_TEXT, ITEM_VIDEO, ITEM_VOICE, MESSAGE_TYPE_USER}; + +pub struct Weixin { + api: Api, + bot_id: String, + /// `typing_ticket` per user (from `getconfig`). + tickets: Mutex>, + /// The freshest `context_token` per user: replies use it when the + /// gateway has none of its own (e.g. a message parked across restarts). + contexts: Mutex>, +} + +impl Weixin { + pub fn new( + http: reqwest::Client, + base_url: &str, + token: &str, + bot_id: &str, + id: api::Identity, + ) -> Self { + Self { + api: Api { + http, + base_url: if base_url.is_empty() { + api::DEFAULT_BASE_URL.to_string() + } else { + base_url.to_string() + }, + token: token.to_string(), + id, + }, + bot_id: bot_id.to_string(), + tickets: Mutex::new(HashMap::new()), + contexts: Mutex::new(HashMap::new()), + } + } + + fn cursor_name(&self) -> String { + format!("weixin-{}", self.bot_id) + } + + /// One `WeixinMessage` → an inbound message (None: not a user message). + pub fn parse_message(&self, m: &Value) -> Option { + if m.get("message_type").and_then(Value::as_i64) != Some(MESSAGE_TYPE_USER) { + return None; + } + let from = m.get("from_user_id").and_then(Value::as_str)?.to_string(); + let group = m + .get("group_id") + .and_then(Value::as_str) + .filter(|g| !g.is_empty()); + let items = m + .get("item_list") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default(); + let mut texts: Vec = Vec::new(); + let mut media = Vec::new(); + for it in &items { + if let Some(q) = it + .pointer("/ref_msg/message_item/text_item/text") + .and_then(Value::as_str) + .filter(|q| !q.trim().is_empty()) + { + let quoted: Vec = q.lines().map(|l| format!("> {l}")).collect(); + texts.push(quoted.join("\n")); + } + match it.get("type").and_then(Value::as_i64).unwrap_or(0) { + ITEM_TEXT => { + if let Some(t) = it.pointer("/text_item/text").and_then(Value::as_str) { + texts.push(t.to_string()); + } + } + ITEM_VOICE => { + // The server's speech-to-text, when it has one. + match it.pointer("/voice_item/text").and_then(Value::as_str) { + Some(t) if !t.trim().is_empty() => texts.push(t.to_string()), + _ => media.push(MediaRef { + kind: MediaKind::Voice, + name: Some("voice.silk".into()), + size: None, + data: it.get("voice_item").cloned().unwrap_or(Value::Null), + }), + } + } + ITEM_IMAGE => media.push(MediaRef { + kind: MediaKind::Image, + name: Some("image.jpg".into()), + size: it.pointer("/image_item/mid_size").and_then(Value::as_u64), + data: it.get("image_item").cloned().unwrap_or(Value::Null), + }), + ITEM_FILE => media.push(MediaRef { + kind: MediaKind::File, + name: it + .pointer("/file_item/file_name") + .and_then(Value::as_str) + .map(String::from), + size: it + .pointer("/file_item/len") + .and_then(|v| v.as_str().and_then(|s| s.parse().ok()).or(v.as_u64())), + data: it.get("file_item").cloned().unwrap_or(Value::Null), + }), + ITEM_VIDEO => media.push(MediaRef { + kind: MediaKind::Video, + name: Some("video.mp4".into()), + size: it.pointer("/video_item/video_size").and_then(Value::as_u64), + data: it.get("video_item").cloned().unwrap_or(Value::Null), + }), + _ => {} + } + } + let text = texts.join("\n").trim().to_string(); + if text.is_empty() && media.is_empty() { + return None; + } + let ctx = m + .get("context_token") + .and_then(Value::as_str) + .map(String::from); + if let Some(c) = &ctx { + self.contexts + .lock() + .unwrap() + .insert(from.clone(), c.clone()); + } + let id = m + .get("message_id") + .or_else(|| m.get("seq")) + .map(|v| match v { + Value::String(s) => s.clone(), + other => other.to_string(), + }) + .unwrap_or_default(); + Some(InboundMessage { + peer: PeerKey { + channel: ChannelKind::Weixin, + account: self.bot_id.clone(), + chat: group.map(String::from).unwrap_or_else(|| from.clone()), + thread: None, + }, + sender: from, + sender_name: String::new(), + msg_id: id, + text, + media, + reply_ctx: ReplyCtx(ctx), + is_group: group.is_some(), + // The iLink channel is private-chat only. + addressed: group.is_none(), + }) + } + + fn context_for(&self, peer: &PeerKey, ctx: &ReplyCtx) -> Option { + ctx.0 + .clone() + .or_else(|| self.contexts.lock().unwrap().get(&peer.chat).cloned()) + } + + async fn typing(&self, user: &str, ctx: Option<&str>) -> Result<(), SendError> { + let cached = self.tickets.lock().unwrap().get(user).cloned(); + let ticket = match cached { + Some(t) => t, + None => { + let r = self.api.get_config(user, ctx).await?; + let t = r + .get("typing_ticket") + .and_then(Value::as_str) + .unwrap_or("") + .to_string(); + if t.is_empty() { + return Ok(()); + } + self.tickets + .lock() + .unwrap() + .insert(user.to_string(), t.clone()); + t + } + }; + self.api.send_typing(user, &ticket).await.map(|_| ()) + } + + async fn send_file( + &self, + to: &str, + ctx: Option<&str>, + path: &Path, + caption: Option<&str>, + ) -> Result<(), SendError> { + let bytes = tokio::fs::read(path) + .await + .map_err(|e| SendError::Other(format!("cannot read {}: {e}", path.display())))?; + if bytes.len() > 100 * 1024 * 1024 { + return Err(SendError::Other("file larger than 100 MB".into())); + } + let name = path + .file_name() + .map(|n| n.to_string_lossy().into_owned()) + .unwrap_or_else(|| "file".into()); + let is_image = crate::attachments::attachment_for(path).kind == "image"; + let media_type = if is_image { + api::UPLOAD_IMAGE + } else { + api::UPLOAD_FILE + }; + let up = cdn::upload(&self.api, to, media_type, &bytes) + .await + .map_err(|e| SendError::Transient(e.to_string()))?; + if let Some(c) = caption.filter(|c| !c.is_empty()) { + self.api + .send_items( + to, + ctx, + vec![json!({"type": ITEM_TEXT, "text_item": {"text": c}})], + ) + .await?; + } + let item = if is_image { + json!({"type": ITEM_IMAGE, "image_item": {"media": up.media_json(), "mid_size": up.cipher_size}}) + } else { + json!({"type": ITEM_FILE, "file_item": {"media": up.media_json(), "file_name": name, "len": up.raw_size.to_string()}}) + }; + self.api.send_items(to, ctx, vec![item]).await.map(|_| ()) + } +} + +#[async_trait] +impl Channel for Weixin { + fn kind(&self) -> ChannelKind { + ChannelKind::Weixin + } + fn account(&self) -> String { + self.bot_id.clone() + } + fn caps(&self) -> ChannelCaps { + ChannelCaps { + max_text_chars: 2000, + edit: false, + markdown: MarkdownDialect::Plain, + min_send_interval: Duration::from_millis(1500), + typing_ttl: Duration::from_secs(8), + } + } + + async fn run( + &self, + inbox: mpsc::Sender, + cancel: CancellationToken, + ) -> anyhow::Result<()> { + let cursor = self.cursor_name(); + let mut buf = store::read_cursor(&cursor).unwrap_or_default(); + let mut backoff = 1u64; + loop { + let res = tokio::select! { + _ = cancel.cancelled() => return Ok(()), + r = self.api.get_updates(&buf) => r, + }; + let resp = match res { + Ok(v) => { + backoff = 1; + v + } + Err(SendError::AuthExpired(d)) => { + anyhow::bail!("WeChat: {d}; run `limbo channels login weixin` again") + } + Err(e) => { + eprintln!("weixin: getupdates failed: {e}; retrying in {backoff}s"); + tokio::select! { + _ = cancel.cancelled() => return Ok(()), + _ = tokio::time::sleep(Duration::from_secs(backoff)) => {} + } + backoff = (backoff * 2).min(60); + continue; + } + }; + for m in resp + .get("msgs") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default() + { + if let Some(msg) = self.parse_message(&m) + && inbox.send(msg).await.is_err() + { + return Ok(()); + } + } + if let Some(next) = resp.get("get_updates_buf").and_then(Value::as_str) + && next != buf + { + buf = next.to_string(); + store::write_cursor(&cursor, &buf); + } + } + } + + async fn send( + &self, + peer: &PeerKey, + ctx: &ReplyCtx, + msg: OutboundMessage, + ) -> Result, SendError> { + let to = peer.chat.as_str(); + let ctx = self.context_for(peer, ctx); + let ctx = ctx.as_deref(); + let text_item = |t: &str| json!({"type": ITEM_TEXT, "text_item": {"text": t}}); + match msg { + OutboundMessage::Text { text, format } | OutboundMessage::Edit { text, format, .. } => { + let t = match format { + TextFormat::Markdown => markdown::render(&text, MarkdownDialect::Plain), + TextFormat::Plain => text, + }; + self.api.send_items(to, ctx, vec![text_item(&t)]).await?; + Ok(None) + } + OutboundMessage::Choices { question, options } => { + let mut t = question; + for (i, o) in options.iter().enumerate() { + t.push_str(&format!("\n{}. {o}", i + 1)); + } + if !options.is_empty() { + t.push_str("\n回复编号选择,或直接回答"); + } + self.api.send_items(to, ctx, vec![text_item(&t)]).await?; + Ok(None) + } + OutboundMessage::Typing => self.typing(to, ctx).await.map(|_| None), + OutboundMessage::File { path, caption } => self + .send_file(to, ctx, &path, caption.as_deref()) + .await + .map(|_| None), + } + } + + async fn fetch_media( + &self, + media: &MediaRef, + dir: &Path, + max_bytes: u64, + ) -> anyhow::Result { + if media.size.is_some_and(|s| s > max_bytes) { + anyhow::bail!("file too large ({} bytes)", media.size.unwrap_or(0)); + } + let m = media + .data + .get("media") + .ok_or_else(|| anyhow::anyhow!("media item without CDN reference"))?; + // image_item.aeskey (hex) wins over media.aes_key (base64). + let key = media + .data + .get("aeskey") + .and_then(Value::as_str) + .or_else(|| m.get("aes_key").and_then(Value::as_str)) + .and_then(cdn::parse_key); + let bytes = cdn::download(&self.api.http, m, key, max_bytes).await?; + let name = media.name.clone().unwrap_or_else(|| "file".into()); + std::fs::create_dir_all(dir)?; + let dest = dir.join(super::safe_file_name(&name)); + tokio::fs::write(&dest, &bytes).await?; + Ok(dest) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn wx() -> Weixin { + Weixin::new( + reqwest::Client::new(), + "", + "tok", + "bot1", + api::Identity::new("bot", "2.4.10"), + ) + } + + #[test] + fn user_text_with_quote() { + let m = json!({"message_id": 7_000_000_000_000_000_001u64, "from_user_id": "u1@im.wechat", + "message_type": 1, "context_token": "ctx1", + "item_list": [{"type": 1, "text_item": {"text": "再改一下"}, + "ref_msg": {"message_item": {"type": 1, "text_item": {"text": "旧回答"}}}}]}); + let msg = wx().parse_message(&m).unwrap(); + assert_eq!(msg.text, "> 旧回答\n再改一下"); + assert_eq!(msg.msg_id, "7000000000000000001"); + assert_eq!(msg.peer.key(), "weixin:bot1:u1@im.wechat"); + assert_eq!(msg.reply_ctx, ReplyCtx(Some("ctx1".into()))); + assert!(msg.addressed); + } + + #[test] + fn bot_echoes_and_groups_are_not_addressed() { + let echo = json!({"from_user_id": "bot", "message_type": 2, + "item_list": [{"type": 1, "text_item": {"text": "hi"}}]}); + assert!(wx().parse_message(&echo).is_none()); + let group = json!({"from_user_id": "u2", "group_id": "g1", "message_type": 1, + "item_list": [{"type": 1, "text_item": {"text": "hi"}}]}); + assert!(!wx().parse_message(&group).unwrap().addressed); + } + + #[test] + fn voice_uses_server_transcript_and_files_become_media() { + let m = json!({"from_user_id": "u1", "message_type": 1, "item_list": [ + {"type": 3, "voice_item": {"text": "帮我看看日志"}}, + {"type": 4, "file_item": {"file_name": "a.log", "len": "12", "media": {"encrypt_query_param": "q"}}}]}); + let msg = wx().parse_message(&m).unwrap(); + assert_eq!(msg.text, "帮我看看日志"); + assert_eq!(msg.media.len(), 1); + assert_eq!(msg.media[0].name.as_deref(), Some("a.log")); + assert_eq!(msg.media[0].size, Some(12)); + } +} diff --git a/src/config.rs b/src/config.rs index 98ad724..01a93b9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -277,6 +277,112 @@ impl Default for QueueSettings { } } +/// `[gateway]`: `limbo gateway`, the chat front end (Telegram / WeChat). +#[derive(Debug, Clone, Deserialize)] +#[serde(default)] +pub struct GatewaySettings { + /// Workdir of chat sessions. + pub workdir: String, + /// Capability set of new chat sessions: "daily" or "coding". + pub posture: String, + /// Model for chat sessions (empty = `[llm] model`). + pub model: String, + /// Unload an idle chat's agent after this many seconds (0 = never). + pub idle_unload_secs: u64, + /// Largest attachment downloaded from a chat, in MB. + pub max_attachment_mb: u64, + /// Show tool progress lines in chats that can edit messages. + pub show_tools: bool, +} + +impl Default for GatewaySettings { + fn default() -> Self { + Self { + workdir: "~/limbo-chat".into(), + posture: "daily".into(), + model: String::new(), + idle_unload_secs: 1800, + max_attachment_mb: 20, + show_tools: true, + } + } +} + +/// Ids in config files may be written as numbers (`owners = [12345]`) or +/// strings; both become strings. +fn de_ids<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { + let raw: Vec = Deserialize::deserialize(d)?; + Ok(raw + .into_iter() + .filter_map(|v| match v { + toml::Value::Integer(i) => Some(i.to_string()), + toml::Value::String(s) => Some(s), + _ => None, + }) + .collect()) +} + +/// `[channels.telegram]`. +#[derive(Debug, Clone, Deserialize)] +#[serde(default)] +pub struct TelegramSettings { + pub enabled: bool, + /// Telegram user ids allowed to talk to the bot (besides the one + /// recorded by `limbo channels login telegram`). + #[serde(deserialize_with = "de_ids")] + pub owners: Vec, + /// HTTP(S)/SOCKS proxy for api.telegram.org. + pub proxy: String, + pub api_base: String, + /// Groups: "off" (ignore) or "mention" (answer @mentions and replies). + pub groups: String, +} + +impl Default for TelegramSettings { + fn default() -> Self { + Self { + enabled: true, + owners: Vec::new(), + proxy: String::new(), + api_base: crate::channels::telegram::DEFAULT_API_BASE.into(), + groups: "mention".into(), + } + } +} + +/// `[channels.weixin]`. +#[derive(Debug, Clone, Deserialize)] +#[serde(default)] +pub struct WeixinSettings { + pub enabled: bool, + /// iLink user ids allowed besides whoever scanned the login QR code. + #[serde(deserialize_with = "de_ids")] + pub owners: Vec, + /// `iLink-App-Id` header. + pub app_id: String, + /// `iLink-App-ClientVersion` (the protocol version the server expects). + pub client_version: String, +} + +impl Default for WeixinSettings { + fn default() -> Self { + Self { + enabled: true, + owners: Vec::new(), + app_id: "bot".into(), + client_version: "2.4.10".into(), + } + } +} + +/// `[channels]`. +#[derive(Debug, Clone, Default, Deserialize)] +#[serde(default)] +pub struct ChannelsSettings { + pub telegram: TelegramSettings, + pub weixin: WeixinSettings, +} + #[derive(Debug, Clone, Default, Deserialize)] #[serde(default)] pub struct Config { @@ -289,6 +395,8 @@ pub struct Config { pub queue: QueueSettings, pub subagents: SubagentSettings, pub daily: DailySettings, + pub gateway: GatewaySettings, + pub channels: ChannelsSettings, pub providers: HashMap, } diff --git a/src/conversation.rs b/src/conversation.rs new file mode 100644 index 0000000..c5e7163 --- /dev/null +++ b/src/conversation.rs @@ -0,0 +1,568 @@ +//! The **Conversation** (see `CONTEXT.md`): the live session a frontend +//! drives — the current Agent + Turn pump pair, slash commands, session +//! switching (`/new`, `/fork`, `/handoff`), lifecycle reporting around each +//! run and the `/goal` verify-proposal flow. +//! +//! It knows nothing about where lines come from or go to: output goes +//! through a [`Sink`], and the frontend owns the loop that feeds it input, +//! agent events and background events. The headless REPL (stdin/stdout) and +//! the chat gateway (`gateway/worker.rs`, one Conversation per chat) are +//! its two adapters. +//! +//! 🦀 Rust notes +//! - `Conversation` is *generic* over its output: each frontend +//! gets its own monomorphized copy, so calling `self.sink.line(..)` is a +//! direct call, not a virtual one (`Box` would also work). +//! - The Agent moves *into* the spawned task while a turn runs and comes +//! back as the task's return value (`JoinHandle`): no lock is ever +//! held across a turn, and "busy" is simply `self.job.is_some()`. + +use std::sync::Arc; + +use tokio::sync::mpsc; +use tokio::task::JoinHandle; +use tokio_util::sync::CancellationToken; + +use crate::agent::{Agent, AgentEvent, FOLLOW_UP, STEER, SteerQueue}; +use crate::background::BackgroundEvent; +use crate::commands::{self as cmds, parse_toggle}; +use crate::goal::{GoalCommand, parse_goal_args}; +use crate::integrations::{Reporters, State, TurnOutcome}; +use crate::models::Attachment; +use crate::pump; + +pub const HELP_TEXT: &str = "可用命令:/goal <目标>|clear 目标闭环 · /queue <文本> 任务完成后再发 · /compact 压缩上下文 · \ +/model [id] 切换模型 · /new 新会话 · /fork [n] 分叉 · /handoff <说明> 交接 · /plan on|off 计划模式 · \ +/code-mode on|off · /daily-mode on|off · /btw <问题> 侧问 · /agents [scan|init] 子代理 · /task 任务板 · @名字 <消息> · /tracing-viewer [id|off] · /help · /quit"; +pub const DAILY_HELP_TEXT: &str = " · /memory [edit] 查看记忆 · /learn 复盘写进 skill"; + +/// Where a Conversation's output goes. +pub trait Sink: Send { + /// One finished line for the user. + fn line(&mut self, text: &str); + /// A streamed piece of a `/btw` answer. + fn btw_chunk(&mut self, text: &str); + /// The `/btw` answer is complete. + fn btw_end(&mut self) {} + /// A task finished and the agent is idle again. + fn idle(&mut self) {} + /// The session changed (`/new`, `/fork`, `/handoff`): reset counters. + fn session_changed(&mut self, _agent: &Agent) {} + /// A new Agent value replaced the old one (`/fork` resumes the child + /// file): frontends re-apply what they add to every agent. + fn agent_replaced(&mut self, _agent: &mut Agent) {} + /// Whether `/quit` may end the frontend. + fn allows_quit(&self) -> bool { + true + } +} + +/// What a started job does. +pub enum Job { + Task(String, Vec), + Compact, +} + +pub struct Conversation { + pub agent: Option, + pub job: Option>, + pub cancel: CancellationToken, + pub steer: Arc, + /// Agent events of the running job; the frontend owns the receiver. + pub tx: mpsc::UnboundedSender, + pub sink: S, + /// The task that just ran was a /goal proposal round. + proposing: bool, + /// Blocked on the user's answer to a verify proposal. + pub proposals: Option>, + pub quit: bool, + viewer: crate::trace_viewer::Viewer, + /// The board while the agent is away running a task. + sup_handle: Option>, + /// Herdr / OSC 7501 lifecycle reports. + pub reporters: Arc, + outcome: TurnOutcome, + reported_session: String, +} + +/// Report the session identity when it changed since the last report. +fn note_session(last: &mut String, reporters: &Reporters, agent: &Agent) { + if agent.session_id() != last.as_str() { + *last = agent.session_id().to_string(); + reporters.report_session(last, agent.session_path()); + } +} + +pub fn first_line(s: &str) -> &str { + s.lines().next().unwrap_or("") +} + +impl Conversation { + pub fn new( + agent: Agent, + sink: S, + tx: mpsc::UnboundedSender, + reporters: Arc, + ) -> Self { + let mut c = Self { + steer: agent.steer.clone(), + viewer: crate::trace_viewer::Viewer::new( + agent.session_dir.clone(), + Some(agent.workdir.clone()), + ), + agent: Some(agent), + job: None, + cancel: CancellationToken::new(), + tx, + sink, + proposing: false, + proposals: None, + quit: false, + sup_handle: None, + reporters, + outcome: TurnOutcome::default(), + reported_session: String::new(), + }; + if let Some(a) = &c.agent { + note_session(&mut c.reported_session, &c.reporters, a); + } + c + } + + pub fn busy(&self) -> bool { + self.job.is_some() + } + + /// Feed an agent event of the running job through (outcome tracking). + pub fn observe(&mut self, ev: &AgentEvent) { + self.outcome.observe(ev); + } + + pub fn start(&mut self, job: Job) { + let Some(mut agent) = self.agent.take() else { + return; + }; + self.cancel = CancellationToken::new(); + let cancel = self.cancel.clone(); + let tx = self.tx.clone(); + self.sup_handle = agent.supervisor.clone(); + note_session(&mut self.reported_session, &self.reporters, &agent); + self.outcome = TurnOutcome::default(); + self.reporters.report(State::Working, None); + self.job = Some(tokio::spawn(async move { + match job { + Job::Task(text, atts) => pump::run_task(&mut agent, text, atts, &tx, &cancel).await, + Job::Compact => { + agent.compact("manual", &tx, &cancel).await; + agent.checkpoint(); + } + } + agent + })); + } + + /// Wait for the running job (if any); `None` when idle. Frontends call + /// it inside `tokio::select!` guarded by [`Conversation::busy`]. + pub async fn join(&mut self) -> Option> { + match self.job.as_mut() { + Some(j) => Some(j.await), + None => None, + } + } + + /// Interrupt the running job (Esc / Ctrl+C / `/stop`). + pub fn interrupt(&mut self) { + if self.busy() { + self.cancel.cancel(); + } + } + + pub fn job_done(&mut self, agent: Agent) { + self.sup_handle = agent.supervisor.clone(); + self.agent = Some(agent); + self.job = None; + self.sink.idle(); + let o = std::mem::take(&mut self.outcome); + self.reporters.report(o.state, o.message.as_deref()); + if std::mem::take(&mut self.proposing) { + self.confirm_proposal(); + if self.proposals.is_some() { + self.reporters + .report(State::Blocked, Some("等待确认 /goal 验收命令")); + } + } + self.maybe_wake_main(); + } + + /// Stop the job and hand the agent back for a final checkpoint. + pub async fn shutdown(&mut self) { + if let Some(job) = self.job.take() { + self.cancel.cancel(); + if let Ok(a) = job.await { + self.agent = Some(a); + } + } + if let Some(a) = self.agent.as_mut() { + a.checkpoint(); + a.trace.flush(); + } + self.viewer.stop(); + if let Some(sup) = self.sup() { + sup.stop(); + } + } + + fn confirm_proposal(&mut self) { + let Some(agent) = &self.agent else { return }; + if pump::live_goal(agent).is_none() { + return; + } + match pump::take_proposal(agent) { + Some(cmds_) if cmds_.is_empty() => self.sink.line(cmds::NO_VERIFY_GATE), + found => { + let list = found.unwrap_or_default(); + if list.is_empty() { + self.sink.line("未能解析模型的验收提议。"); + } else { + self.sink.line("模型提议的验收命令:"); + for (i, c) in list.iter().enumerate() { + self.sink.line(&format!(" {}. {c}", i + 1)); + } + } + self.sink + .line("回复序号采纳;或直接输入一行作为验收命令;/skip 跳过自动验收"); + self.proposals = Some(list); + } + } + } + + fn answer_verify(&mut self, text: &str, proposals: Vec) { + let t = text.trim(); + if t.is_empty() || t == "/skip" { + self.sink.line(cmds::VERIFY_SKIPPED); + self.reporters.report(State::Idle, None); + return; + } + if t.starts_with('/') { + self.sink + .line("正在等待验收确认:回复序号、验收命令,或 /skip 跳过"); + self.proposals = Some(proposals); + return; + } + let command = match t.parse::() { + Ok(i) if (1..=proposals.len()).contains(&i) => proposals[i - 1].clone(), + Ok(_) => { + self.sink.line(&format!( + "序号超出范围(1–{});或直接输入验收命令,/skip 跳过", + proposals.len() + )); + self.proposals = Some(proposals); + return; + } + Err(_) => t.to_string(), + }; + let Some(agent) = self.agent.as_mut() else { + return; + }; + if let Some(prompt) = pump::set_verify(agent, &command) { + self.sink.line(&format!("验收方式已确认:{command}")); + self.start(Job::Task(prompt, vec![])); + } + } + + /// One line of user input (plus attachments from a chat message). + pub async fn submit(&mut self, line: String, attachments: Vec) { + if let Some(proposals) = self.proposals.take() { + return self.answer_verify(&line, proposals); + } + let trimmed = line.trim(); + if trimmed.is_empty() && attachments.is_empty() { + return; + } + let (name, arg) = match trimmed.strip_prefix('/') { + Some(rest) if !line.starts_with(' ') => rest + .split_once(char::is_whitespace) + .map_or((rest, ""), |(c, a)| (c, a.trim())), + _ => ("", ""), + }; + let name = name.to_lowercase(); + // Board commands and @name never touch the main history: fine mid-turn. + if matches!(name.as_str(), "agents" | "task") { + let msg = match self.sup() { + None => "子代理已关闭(config.toml 的 [subagents] enabled = false)".to_string(), + Some(sup) if name == "agents" => crate::subagents::agents_command(&sup, arg), + Some(sup) => crate::subagents::task_command(&sup, arg), + }; + self.sink.line(&msg); + return; + } + if name.is_empty() + && let Some(sup) = self.sup() + && let Some(reply) = crate::subagents::direct_message(&sup, &line) + { + self.sink.line(&format!("➤ {reply}")); + return; + } + if self.busy() { + match name.as_str() { + "" => { + self.steer + .push_item(line.clone(), attachments, STEER, "user", vec![]); + self.sink + .line(&format!("➤ 已排队插话:{}", first_line(&line))); + } + "queue" if !arg.is_empty() => { + self.steer + .push_item(arg.to_string(), attachments, FOLLOW_UP, "user", vec![]); + self.sink.line(&format!( + "➤ 已排队,当前任务完成后处理:{}", + first_line(arg) + )); + } + "help" => self.sink.line(HELP_TEXT), + "tracing-viewer" => { + let msg = self.viewer.command(arg, Some(&self.reported_session)); + self.sink.line(&msg); + } + _ => self + .sink + .line("当前任务进行中,该命令暂不可用(/stop 或 Ctrl+C 中断,或等待完成)"), + } + return; + } + if name.is_empty() { + return self.start(Job::Task(line, attachments)); + } + self.command(&name, arg).await; + if let Some(a) = &self.agent { + note_session(&mut self.reported_session, &self.reporters, a); + } + } + + async fn command(&mut self, name: &str, arg: &str) { + let Some(agent) = self.agent.as_mut() else { + return; + }; + match name { + "quit" | "exit" if self.sink.allows_quit() => self.quit = true, + "help" => { + let daily = agent.daily_mode(); + self.sink.line(&format!( + "{HELP_TEXT}{}", + if daily { DAILY_HELP_TEXT } else { "" } + )); + } + "queue" => { + if arg.is_empty() { + self.sink.line("用法:/queue <文本>"); + } else { + self.start(Job::Task(arg.to_string(), vec![])); + } + } + "new" => { + agent.new_session(); + self.sink.session_changed(agent); + self.sink.line("已开始新会话"); + } + "compact" => self.start(Job::Compact), + "model" => { + if arg.is_empty() { + let m = agent.client.model_id().to_string(); + self.sink.line(&format!("当前模型:{m}")); + } else { + let (model, level) = crate::llm::catalog::split_model_arg(arg); + match agent.set_model(&model) { + Ok(()) => { + if level.is_some() { + agent.set_thinking(level); + } + let m = agent.client.model_id().to_string(); + self.sink.line(&format!("已切换到 {m}")); + } + Err(e) => self.sink.line(&format!("✗ {e}")), + } + } + } + "fork" => { + if !arg.is_empty() && arg.parse::().is_err() { + return self + .sink + .line("用法:/fork [n](n = 保留前 n 轮对话,省略则全部保留)"); + } + let parent = agent.session_id().to_string(); + let dir = agent.session_dir.clone(); + match agent.fork(arg.parse().ok()) { + Ok(path) => match Agent::resume(agent.config.clone(), &path, &dir) { + Ok(mut a) => { + self.sink.agent_replaced(&mut a); + self.steer = a.steer.clone(); + let id = a.session_id().to_string(); + self.sink.session_changed(&a); + self.agent = Some(a); + self.sink + .line(&format!("已从会话 {parent} 分叉,新会话 {id}")); + } + Err(e) => self.sink.line(&format!("✗ {e}")), + }, + Err(e) => self.sink.line(&format!("✗ {e}")), + } + } + "handoff" => { + if arg.is_empty() { + return self.sink.line("用法:/handoff <交接说明>"); + } + let parent = agent.session_id().to_string(); + let prompt = agent.handoff_prompt(arg); + let meta = agent.handoff_meta(); + agent.reset_to(meta); + self.sink.session_changed(agent); + let id = agent.session_id().to_string(); + self.sink + .line(&format!("已交接:新会话 {id},关联原会话 {parent}")); + self.start(Job::Task(prompt, vec![])); + } + "plan" => match parse_toggle(arg, agent.plan_mode()) { + None => self.sink.line(cmds::PLAN_USAGE), + Some(on) if agent.set_plan_mode(on) => self.sink.line(cmds::plan_notice(on)), + Some(on) => self.sink.line(&cmds::unchanged_notice("计划模式", on)), + }, + "code-mode" => match parse_toggle(arg, agent.code_mode()) { + None => self.sink.line(cmds::CODE_MODE_USAGE), + Some(on) if agent.set_code_mode(on) => self.sink.line(cmds::code_mode_notice(on)), + Some(on) => self.sink.line(&cmds::unchanged_notice("Code mode ", on)), + }, + "daily-mode" => match parse_toggle(arg, agent.daily_mode()) { + None => self.sink.line(cmds::DAILY_USAGE), + Some(on) if agent.set_daily_mode(on) => { + self.sink.line(cmds::daily_toggle_notice(on)); + if let Some(n) = cmds::web_search_missing_notice(agent) { + self.sink.line(&n); + } + } + Some(on) => self.sink.line(&cmds::unchanged_notice("Daily mode ", on)), + }, + "memory" | "learn" if !agent.daily_mode() => self + .sink + .line(&cmds::daily_only_notice(&format!("/{name}"))), + "memory" => match crate::memory::parse_memory_command(arg) { + Err(()) => self.sink.line(crate::memory::MEMORY_USAGE), + Ok(target) => { + let store = agent.memory_store(); + match (target, &store) { + (Some(t), Some(s)) => self.sink.line(&format!( + "直接编辑 {},从下一个会话起生效", + s.path(&t).display() + )), + _ => { + let text = + crate::memory::describe(store.as_ref(), agent.memory.as_ref()); + self.sink.line(&text); + } + } + } + }, + "learn" => { + let msg = crate::learn::start_review(agent, false); + self.sink.line(&msg); + } + "btw" => { + if arg.is_empty() { + return self.sink.line("用法:/btw <问题>"); + } + let mut rx = cmds::btw_query(&agent.client, &agent.messages, arg); + self.sink.line(&format!("💬 btw · {arg}")); + while let Some(item) = rx.recv().await { + match item { + Ok(t) => self.sink.btw_chunk(&t), + Err(e) => { + self.sink.btw_end(); + self.sink.line(&format!("✗ {e}")); + } + } + } + self.sink.btw_end(); + self.sink.line("(侧问答案不会进入会话)"); + } + "goal" => match parse_goal_args(arg) { + GoalCommand::Query => match pump::live_goal(agent) { + None => self.sink.line("当前没有 goal"), + Some(g) => { + let t = cmds::goal_status_text(g); + self.sink.line(&t); + } + }, + GoalCommand::Clear => { + if pump::clear_goal(agent) { + self.sink.line("已退出 goal 模式"); + } else { + self.sink.line("当前没有 goal"); + } + } + GoalCommand::Set(text) => { + let prompt = pump::set_goal(agent, &text); + self.proposing = true; + self.start(Job::Task(prompt, vec![])); + } + }, + "tracing-viewer" => { + let id = agent.session_id().to_string(); + let msg = self.viewer.command(arg, Some(&id)); + self.sink.line(&msg); + } + other => self + .sink + .line(&format!("未知命令 /{other}(/help 查看可用命令)")), + } + } + + /// The user-facing lines for a background event (empty: nothing to show). + pub fn background_lines(&self, ev: &BackgroundEvent) -> Vec { + match ev { + BackgroundEvent::Notice { kind, text } if !text.is_empty() => { + let icon = match kind.as_str() { + "result" => "✓", + "alert" => "⚠", + "question" => "?", + _ => "◆", + }; + vec![format!("{icon} {text}")] + } + BackgroundEvent::PlanProposed(_) => { + let Some(sup) = self.sup() else { + return vec![]; + }; + let mut out = vec!["◆ 子任务计划待确认:".to_string()]; + out.extend(sup.status_text(None).lines().map(|l| format!(" {l}"))); + out.push(" /task approve all 开始 · /task reject all 放弃".into()); + out + } + _ => vec![], + } + } + + /// After a background event was shown: wake an idle main agent. + pub fn after_background(&mut self, ev: &BackgroundEvent) { + if matches!(ev, BackgroundEvent::MainWake) { + self.maybe_wake_main(); + } + } + + pub fn sup(&self) -> Option> { + self.agent + .as_ref() + .and_then(|a| a.supervisor.clone()) + .or_else(|| self.sup_handle.clone()) + } + + /// Start a main-agent turn for subagent notices queued while idle. + pub fn maybe_wake_main(&mut self) { + if self.busy() || self.proposals.is_some() || self.quit { + return; + } + let Some(text) = self.sup().and_then(|s| s.take_wake_input()) else { + return; + }; + self.sink.line("◆ 子代理有新进展,交给主 agent 处理"); + self.start(Job::Task(text, vec![])); + } +} diff --git a/src/gateway/access.rs b/src/gateway/access.rs new file mode 100644 index 0000000..be62887 --- /dev/null +++ b/src/gateway/access.rs @@ -0,0 +1,75 @@ +//! Who may talk to the gateway: owners only. A chat front end is a remote +//! shell into this machine (limbo has no safety fences), so everyone not on +//! the list is dropped silently — no reply that would confirm a bot exists. +//! +//! Owners come from two places: `owners = [...]` in `[channels.]` and +//! the account recorded at `limbo channels login` (the Telegram user who +//! sent the first message, the WeChat user who scanned the QR code). + +use std::collections::{HashMap, HashSet}; + +use crate::channels::{ChannelKind, InboundMessage}; + +#[derive(Debug, Default, Clone)] +pub struct Access { + owners: HashMap>, +} + +impl Access { + pub fn new() -> Self { + Self::default() + } + + pub fn allow(&mut self, channel: ChannelKind, ids: impl IntoIterator) { + self.owners.entry(channel).or_default().extend( + ids.into_iter() + .map(|s| s.trim().to_string()) + .filter(|s| !s.is_empty()), + ); + } + + pub fn owner_count(&self, channel: ChannelKind) -> usize { + self.owners.get(&channel).map_or(0, HashSet::len) + } + + /// The sender is an owner of this channel. + pub fn admits(&self, msg: &InboundMessage) -> bool { + self.owners + .get(&msg.peer.channel) + .is_some_and(|o| o.contains(&msg.sender)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::channels::{PeerKey, ReplyCtx}; + + fn msg(channel: ChannelKind, sender: &str) -> InboundMessage { + InboundMessage { + peer: PeerKey { + channel, + account: "b".into(), + chat: sender.into(), + thread: None, + }, + sender: sender.into(), + sender_name: String::new(), + msg_id: "1".into(), + text: "hi".into(), + media: vec![], + reply_ctx: ReplyCtx::default(), + is_group: false, + addressed: true, + } + } + + #[test] + fn owners_are_per_channel() { + let mut a = Access::new(); + a.allow(ChannelKind::Telegram, ["42".to_string()]); + assert!(a.admits(&msg(ChannelKind::Telegram, "42"))); + assert!(!a.admits(&msg(ChannelKind::Telegram, "43"))); + assert!(!a.admits(&msg(ChannelKind::Weixin, "42"))); + } +} diff --git a/src/gateway/mod.rs b/src/gateway/mod.rs new file mode 100644 index 0000000..cd19e93 --- /dev/null +++ b/src/gateway/mod.rs @@ -0,0 +1,488 @@ +//! `limbo gateway`: talk to limbo from Telegram and personal WeChat. +//! +//! ```text +//! channels::telegram ─┐ ┌─ worker (chat A) ── Conversation ── Agent +//! channels::weixin ─┼─ inbox ── router ────────┼─ worker (chat B) ── … +//! │ dedupe · owners only └─ … +//! └──────────── outbox (per chat) ◄── renderer ◄── AgentEvents +//! ``` +//! +//! - adapters (`channels/`) long-poll their platform and push +//! [`InboundMessage`]s into one inbox; +//! - the router drops duplicates, strangers and group chatter not aimed at +//! the bot, then hands each message to its chat's worker (started on +//! demand); +//! - each worker (`worker.rs`) owns one Conversation, renders its events +//! (`render.rs`) and sends through its own outbox (`outbox.rs`). +//! +//! Also here: `limbo channels login|logout|status` and the background +//! service files for macOS (launchd) and Linux (systemd --user). + +pub mod access; +pub mod outbox; +pub mod render; +pub mod send_file; +pub mod service; +pub mod sessions; +pub mod worker; + +use std::collections::{HashMap, HashSet, VecDeque}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +use crate::channels::store::{self, Credentials, TelegramCred}; +use crate::channels::telegram::{self, Telegram}; +use crate::channels::weixin::{self, Weixin}; +use crate::channels::{Channel, ChannelKind, InboundMessage}; +use crate::config::Config; +use access::Access; +use sessions::SessionMap; +use worker::WorkerEnv; + +/// Remember this many message ids per channel for deduplication. +const DEDUP: usize = 2048; + +fn expand_home(raw: &str) -> PathBuf { + match raw.strip_prefix("~/") { + Some(rest) => dirs::home_dir().unwrap_or_default().join(rest), + None if raw == "~" => dirs::home_dir().unwrap_or_default(), + None => PathBuf::from(raw), + } +} + +/// Recently seen message ids (bounded). +#[derive(Default)] +pub struct Dedup { + seen: HashSet, + order: VecDeque, +} + +impl Dedup { + /// `true` the first time `key` is seen. + pub fn first(&mut self, key: String) -> bool { + if !self.seen.insert(key.clone()) { + return false; + } + self.order.push_back(key); + if self.order.len() > DEDUP + && let Some(old) = self.order.pop_front() + { + self.seen.remove(&old); + } + true + } +} + +/// What the router does with one message. +#[derive(Debug, PartialEq, Eq)] +pub enum Route { + Deliver, + Duplicate, + Stranger, + NotAddressed, +} + +pub fn classify(msg: &InboundMessage, access: &Access, dedup: &mut Dedup) -> Route { + if !dedup.first(format!("{}:{}", msg.peer.key(), msg.msg_id)) { + return Route::Duplicate; + } + if !access.admits(msg) { + return Route::Stranger; + } + if !msg.addressed { + return Route::NotAddressed; + } + Route::Deliver +} + +/// Build every configured, logged-in channel. +fn build_channels( + config: &Config, + creds: &Credentials, + access: &mut Access, +) -> anyhow::Result>> { + let mut out: Vec> = Vec::new(); + let tg = &config.channels.telegram; + if tg.enabled + && let Some(c) = &creds.telegram + { + let http = telegram::http_client(Some(&tg.proxy), Duration::from_secs(60))?; + access.allow( + ChannelKind::Telegram, + tg.owners.iter().cloned().chain(c.owners.iter().cloned()), + ); + out.push(Arc::new(Telegram::new( + http, + &tg.api_base, + &c.token, + &c.bot_id, + &c.bot_username, + tg.groups != "off", + ))); + } + let wx = &config.channels.weixin; + if wx.enabled + && let Some(c) = &creds.weixin + { + let http = reqwest::Client::builder() + .connect_timeout(Duration::from_secs(15)) + .build()?; + access.allow( + ChannelKind::Weixin, + wx.owners.iter().cloned().chain(c.owners.iter().cloned()), + ); + out.push(Arc::new(Weixin::new( + http, + &c.base_url, + &c.token, + &c.bot_id, + weixin::api::Identity::new(&wx.app_id, &wx.client_version), + ))); + } + Ok(out) +} + +/// `limbo gateway`: run until Ctrl+C / SIGTERM. +pub async fn run(mut config: Config, session_dir: &Path) -> anyhow::Result { + let _lock = store::GatewayLock::acquire()?; + let g = config.gateway.clone(); + if !g.model.is_empty() { + config.llm.model = g.model.clone(); + } + let spec = crate::llm::catalog::resolve_model(&config.llm.model); + if crate::llm::catalog::resolve_api_key(&spec, &config).is_none() { + anyhow::bail!( + "{}", + crate::llm::catalog::missing_api_key_message(&spec, &config) + ); + } + let workdir = expand_home(&g.workdir); + std::fs::create_dir_all(&workdir) + .map_err(|e| anyhow::anyhow!("cannot create gateway workdir {}: {e}", workdir.display()))?; + let workdir = std::fs::canonicalize(&workdir)?; + let creds = store::load_credentials(); + let mut access = Access::new(); + let channels = build_channels(&config, &creds, &mut access)?; + if channels.is_empty() { + anyhow::bail!( + "no channel is logged in: run `limbo channels login telegram` or `limbo channels login weixin` first" + ); + } + for ch in &channels { + if access.owner_count(ch.kind()) == 0 { + eprintln!( + "warning: {} has no owners: nobody can talk to it (set owners in [channels.{}] or log in again)", + ch.kind().as_str(), + ch.kind().as_str() + ); + } + } + let env = WorkerEnv { + config: config.clone(), + session_dir: session_dir.to_path_buf(), + workdir: workdir.clone(), + daily: g.posture != "coding", + sessions: Arc::new(Mutex::new(SessionMap::load( + &store::channels_dir().join("sessions.json"), + ))), + idle_unload: (g.idle_unload_secs > 0).then(|| Duration::from_secs(g.idle_unload_secs)), + max_attachment: g.max_attachment_mb.max(1) * 1024 * 1024, + show_tools: g.show_tools, + }; + let cancel = CancellationToken::new(); + let (inbox_tx, mut inbox) = mpsc::channel::(256); + let mut by_kind: HashMap> = HashMap::new(); + for ch in &channels { + by_kind.insert(ch.kind(), ch.clone()); + let (ch2, tx, c) = (ch.clone(), inbox_tx.clone(), cancel.clone()); + tokio::spawn(async move { + if let Err(e) = ch2.run(tx, c).await { + eprintln!("✗ {} stopped: {e:#}", ch2.kind().as_str()); + } + }); + } + drop(inbox_tx); + println!( + "limbo gateway · {} · workdir {} · {} · channels: {}", + config.llm.model, + workdir.display(), + if env.daily { "daily" } else { "coding" }, + channels + .iter() + .map(|c| format!("{} ({})", c.kind().as_str(), c.account())) + .collect::>() + .join(", ") + ); + let mut workers: HashMap> = HashMap::new(); + let mut dedup = Dedup::default(); + let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?; + loop { + let msg = tokio::select! { + _ = tokio::signal::ctrl_c() => break, + _ = term.recv() => break, + m = inbox.recv() => match m { + Some(m) => m, + None => { + eprintln!("all channels stopped"); + break; + } + }, + }; + match classify(&msg, &access, &mut dedup) { + Route::Deliver => {} + Route::Stranger => { + eprintln!( + "gateway: ignored a message from {} ({}) on {} — not an owner", + msg.sender, + msg.sender_name, + msg.peer.channel.as_str() + ); + continue; + } + _ => continue, + } + let key = msg.peer.key(); + let Some(ch) = by_kind.get(&msg.peer.channel).cloned() else { + continue; + }; + let tx = workers.entry(key.clone()).or_insert_with(|| { + worker::spawn( + env.clone(), + ch.clone(), + msg.peer.clone(), + cancel.child_token(), + ) + }); + if let Err(mpsc::error::SendError(m)) = tx.send(msg) { + // The worker ended (it never does on its own); start a new one. + let tx = worker::spawn(env.clone(), ch, m.peer.clone(), cancel.child_token()); + let _ = tx.send(m); + workers.insert(key, tx); + } + } + println!("gateway stopping…"); + cancel.cancel(); + // Give workers a moment to checkpoint their sessions. + tokio::time::sleep(Duration::from_millis(800)).await; + Ok(0) +} + +// -- `limbo channels …` -------------------------------------------------------- + +pub async fn login(config: &Config, channel: &str, token: Option) -> anyhow::Result { + match ChannelKind::parse(channel) { + Some(ChannelKind::Telegram) => login_telegram(config, token).await, + Some(ChannelKind::Weixin) => { + let wx = &config.channels.weixin; + let http = reqwest::Client::builder() + .connect_timeout(Duration::from_secs(15)) + .build()?; + let cred = weixin::login::login( + http, + weixin::api::Identity::new(&wx.app_id, &wx.client_version), + ) + .await?; + let mut creds = store::load_credentials(); + println!( + "✓ 微信已连接(bot {});主人:{}", + cred.bot_id, + cred.owners + .first() + .map(String::as_str) + .unwrap_or("(未返回,请在 [channels.weixin] owners 里配置)") + ); + creds.weixin = Some(cred); + store::save_credentials(&creds)?; + println!("运行 `limbo gateway` 开始接收消息。"); + Ok(0) + } + _ => anyhow::bail!("unknown channel {channel:?} (telegram | weixin)"), + } +} + +async fn login_telegram(config: &Config, token: Option) -> anyhow::Result { + let tg = &config.channels.telegram; + let token = match token { + Some(t) => t, + None => { + println!( + "在 Telegram 里找 @BotFather,/newbot 创建机器人,把它给你的 token 粘贴到这里:" + ); + let mut s = String::new(); + std::io::stdin().read_line(&mut s)?; + s + } + }; + let token = token.trim().to_string(); + if token.is_empty() { + anyhow::bail!("empty token"); + } + let http = telegram::http_client(Some(&tg.proxy), Duration::from_secs(60))?; + let (bot_id, username) = telegram::get_me(&http, &tg.api_base, &token) + .await + .map_err(|e| anyhow::anyhow!("token check failed: {e}"))?; + println!("✓ bot @{username}(id {bot_id})"); + let mut cred = TelegramCred { + token: token.clone(), + bot_id: bot_id.clone(), + bot_username: username.clone(), + owners: store::load_credentials() + .telegram + .filter(|c| c.bot_id == bot_id) + .map(|c| c.owners) + .unwrap_or_default(), + }; + if cred.owners.is_empty() && tg.owners.is_empty() { + println!( + "现在用你自己的 Telegram 账号给 @{username} 发一条消息(例如 /start),把你登记为主人…" + ); + let bot = Telegram::new(http, &tg.api_base, &token, &bot_id, &username, false); + let (tx, mut rx) = mpsc::channel(8); + let cancel = CancellationToken::new(); + let c2 = cancel.clone(); + let poll = tokio::spawn(async move { bot.run(tx, c2).await }); + let first = tokio::time::timeout(Duration::from_secs(300), async { + loop { + match rx.recv().await { + Some(m) if !m.is_group => return Some(m), + Some(_) => continue, + None => return None, + } + } + }) + .await; + cancel.cancel(); + let _ = poll.await; + match first { + Ok(Some(m)) => { + println!("✓ 主人:{} ({})", m.sender, m.sender_name); + cred.owners.push(m.sender); + } + _ => println!( + "没有收到消息;之后可以在 config.toml 的 [channels.telegram] owners 里填你的 user id。" + ), + } + } + let mut creds = store::load_credentials(); + creds.telegram = Some(cred); + store::save_credentials(&creds)?; + println!( + "已保存到 {};运行 `limbo gateway` 开始接收消息。", + store::credentials_path().display() + ); + Ok(0) +} + +pub fn logout(channel: &str) -> anyhow::Result { + let mut creds = store::load_credentials(); + match ChannelKind::parse(channel) { + Some(ChannelKind::Telegram) => creds.telegram = None, + Some(ChannelKind::Weixin) => creds.weixin = None, + _ => anyhow::bail!("unknown channel {channel:?} (telegram | weixin)"), + } + store::save_credentials(&creds)?; + println!("已删除 {channel} 的登录信息"); + Ok(0) +} + +pub fn status(config: &Config) -> i32 { + let creds = store::load_credentials(); + let line = |name: &str, + enabled: bool, + logged: Option<(String, Vec)>, + cfg_owners: &[String]| { + let state = match (&logged, enabled) { + (None, _) => "未登录".to_string(), + (Some(_), false) => "已登录(config 中已停用)".to_string(), + (Some((bot, _)), true) => format!("已登录 · bot {bot}"), + }; + let mut owners: Vec = cfg_owners.to_vec(); + if let Some((_, o)) = &logged { + owners.extend(o.iter().cloned()); + } + println!( + "{name:9} {state} · 主人 {}", + if owners.is_empty() { + "(无)".into() + } else { + owners.join(", ") + } + ); + }; + line( + "telegram", + config.channels.telegram.enabled, + creds + .telegram + .map(|c| (format!("@{} ({})", c.bot_username, c.bot_id), c.owners)), + &config.channels.telegram.owners, + ); + line( + "weixin", + config.channels.weixin.enabled, + creds.weixin.map(|c| (c.bot_id, c.owners)), + &config.channels.weixin.owners, + ); + println!( + "会话映射:{}", + store::channels_dir().join("sessions.json").display() + ); + println!("后台服务:{}", service::describe()); + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::channels::{PeerKey, ReplyCtx}; + + fn msg(sender: &str, id: &str, addressed: bool) -> InboundMessage { + InboundMessage { + peer: PeerKey { + channel: ChannelKind::Telegram, + account: "b".into(), + chat: "c".into(), + thread: None, + }, + sender: sender.into(), + sender_name: String::new(), + msg_id: id.into(), + text: "hi".into(), + media: vec![], + reply_ctx: ReplyCtx::default(), + is_group: !addressed, + addressed, + } + } + + #[test] + fn router_rules() { + let mut a = Access::new(); + a.allow(ChannelKind::Telegram, ["42".to_string()]); + let mut d = Dedup::default(); + assert_eq!(classify(&msg("42", "1", true), &a, &mut d), Route::Deliver); + assert_eq!( + classify(&msg("42", "1", true), &a, &mut d), + Route::Duplicate + ); + assert_eq!(classify(&msg("7", "2", true), &a, &mut d), Route::Stranger); + assert_eq!( + classify(&msg("42", "3", false), &a, &mut d), + Route::NotAddressed + ); + } + + #[test] + fn dedup_is_bounded() { + let mut d = Dedup::default(); + for i in 0..(DEDUP + 10) { + assert!(d.first(i.to_string())); + } + assert!(d.seen.len() <= DEDUP); + assert!(d.first("0".into()), "oldest ids are forgotten"); + } +} diff --git a/src/gateway/outbox.rs b/src/gateway/outbox.rs new file mode 100644 index 0000000..e553ae1 --- /dev/null +++ b/src/gateway/outbox.rs @@ -0,0 +1,185 @@ +//! One sender task per chat: delivers [`OutItem`]s in order, spaced by the +//! channel's `min_send_interval`, and decides what a failure means: +//! +//! - rate limited → wait `retry_after`, try again (up to 5 times); +//! - transient (network, 5xx) → back off 1/2/4 s, then give up on the item; +//! - reply context expired (WeChat) → **park** the item until the user +//! writes again ([`OutItem::Resume`]), so a long task's answer is not lost; +//! - token expired → stop sending for this chat and log it once. +//! +//! Edits address *slots* (see `render.rs`); the platform handle of each slot +//! is known only here, after its first send. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use tokio::sync::mpsc; + +use super::render::OutItem; +use crate::channels::{Channel, MsgHandle, OutboundMessage, PeerKey, ReplyCtx, SendError}; + +const MAX_PARKED: usize = 50; + +/// The latest reply context of a chat, shared by worker and sender. +pub type SharedCtx = Arc>; + +pub fn spawn( + channel: Arc, + peer: PeerKey, + ctx: SharedCtx, +) -> mpsc::UnboundedSender { + let (tx, rx) = mpsc::unbounded_channel(); + tokio::spawn(run(channel, peer, ctx, rx)); + tx +} + +async fn run( + channel: Arc, + peer: PeerKey, + ctx: SharedCtx, + mut rx: mpsc::UnboundedReceiver, +) { + let interval = channel.caps().min_send_interval; + let mut handles: HashMap = HashMap::new(); + let mut parked: Vec = Vec::new(); + let mut last: Option = None; + let mut dead = false; + while let Some(item) = rx.recv().await { + // Coalesce queued edits of the same slot: only the newest counts. + let mut batch = vec![item]; + while let Ok(next) = rx.try_recv() { + match (batch.last(), &next) { + (Some(OutItem::Edit { slot: a, .. }), OutItem::Edit { slot: b, .. }) if a == b => { + *batch.last_mut().unwrap() = next; + } + _ => batch.push(next), + } + } + for item in batch { + if matches!(item, OutItem::Resume) { + dead = false; + let again = std::mem::take(&mut parked); + for p in again { + deliver( + &*channel, + &peer, + &ctx, + p, + &mut handles, + &mut parked, + &mut last, + interval, + &mut dead, + ) + .await; + } + continue; + } + if dead { + continue; + } + deliver( + &*channel, + &peer, + &ctx, + item, + &mut handles, + &mut parked, + &mut last, + interval, + &mut dead, + ) + .await; + } + } +} + +#[allow(clippy::too_many_arguments)] +async fn deliver( + channel: &dyn Channel, + peer: &PeerKey, + ctx: &SharedCtx, + item: OutItem, + handles: &mut HashMap, + parked: &mut Vec, + last: &mut Option, + interval: Duration, + dead: &mut bool, +) { + let (msg, slot) = match &item { + OutItem::Send { msg, slot } => (msg.clone(), *slot), + OutItem::Edit { slot, text, format } => match handles.get(slot) { + Some(h) => ( + OutboundMessage::Edit { + handle: h.clone(), + text: text.clone(), + format: *format, + }, + None, + ), + // Never sent (or the send failed): send it now. + None => ( + OutboundMessage::Text { + text: text.clone(), + format: *format, + }, + Some(*slot), + ), + }, + OutItem::Resume => return, + }; + // Typing indicators are best effort: never retried, never parked. + let typing = matches!(msg, OutboundMessage::Typing); + let mut attempt = 0u32; + loop { + if let Some(t) = *last { + let since = t.elapsed(); + if since < interval && !typing { + tokio::time::sleep(interval - since).await; + } + } + let c = ctx.lock().unwrap().clone(); + let res = channel.send(peer, &c, msg.clone()).await; + if !typing { + *last = Some(Instant::now()); + } + match res { + Ok(h) => { + if let (Some(s), Some(h)) = (slot, h) { + handles.insert(s, h); + } + return; + } + Err(_) if typing => return, + Err(SendError::RateLimited(d)) if attempt < 5 => { + attempt += 1; + tokio::time::sleep(d).await; + } + Err(SendError::Transient(e)) if attempt < 3 => { + attempt += 1; + eprintln!("gateway: send to {} failed ({e}), retrying", peer.key()); + tokio::time::sleep(Duration::from_secs(1 << (attempt - 1))).await; + } + Err(SendError::ContextExpired) => { + if parked.len() < MAX_PARKED { + parked.push(item); + } + return; + } + Err(SendError::AuthExpired(e)) => { + eprintln!( + "gateway: {} — {e}; messages to {} are dropped until you log in again", + peer.channel.as_str(), + peer.key() + ); + *dead = true; + return; + } + Err(e) => { + eprintln!("gateway: send to {} failed: {e}", peer.key()); + return; + } + } + } +} diff --git a/src/gateway/render.rs b/src/gateway/render.rs new file mode 100644 index 0000000..ede3112 --- /dev/null +++ b/src/gateway/render.rs @@ -0,0 +1,497 @@ +//! Agent events → chat messages. A small state machine, free of I/O so it +//! can be tested with plain event lists: +//! +//! - **answer text** accumulates per segment (a segment ends at a tool call, +//! a notice or the turn end). Channels that can edit (Telegram) see it +//! stream: one message is sent and then edited at most once per +//! `min_send_interval` as plain text, and replaced by the formatted +//! markdown when the segment is complete. Other channels (WeChat) get the +//! finished segment only. +//! - **tool calls** become one progress message, edited as calls come in +//! (editing channels, `[gateway] show_tools`); elsewhere a typing +//! indicator is all the user sees while tools run. +//! - `ask_user` becomes a question with options (buttons on Telegram). +//! +//! Output goes to the outbox as [`OutItem`]s; editable messages are named +//! by *slots* (small numbers) because the platform's message id is only +//! known once the outbox has sent them. + +use std::time::{Duration, Instant}; + +use crate::agent::AgentEvent; +use crate::channels::{ChannelCaps, MarkdownDialect, OutboundMessage, TextFormat, markdown}; + +/// Something for the outbox to deliver. +#[derive(Debug, Clone)] +pub enum OutItem { + /// Send; when `slot` is set, remember the message for later edits. + Send { + msg: OutboundMessage, + slot: Option, + }, + /// Replace the text of the message in `slot` (sent fresh if unknown). + Edit { + slot: u32, + text: String, + format: TextFormat, + }, + /// A new inbound message arrived: retry parked sends. + Resume, +} + +impl OutItem { + pub fn text(text: impl Into) -> Self { + OutItem::Send { + msg: OutboundMessage::Text { + text: text.into(), + format: TextFormat::Plain, + }, + slot: None, + } + } +} + +const MAX_TOOL_LINES: usize = 8; +const CURSOR: &str = " ▍"; + +pub struct Renderer { + caps: ChannelCaps, + show_tools: bool, + text: String, + /// Streaming preview of the current segment: (slot, text last sent). + live: Option<(u32, String)>, + tools: Vec, + tool_count: usize, + tool_slot: Option, + tools_dirty: bool, + next_slot: u32, + last_flush: Option, +} + +impl Renderer { + pub fn new(caps: ChannelCaps, show_tools: bool) -> Self { + Self { + caps, + show_tools, + text: String::new(), + live: None, + tools: vec![], + tool_count: 0, + tool_slot: None, + tools_dirty: false, + next_slot: 1, + last_flush: None, + } + } + + fn slot(&mut self) -> u32 { + let s = self.next_slot; + self.next_slot += 1; + s + } + + /// Room for one chunk of markdown source: Telegram's limit counts the + /// rendered text, and HTML tags / entities add a little. + fn chunk_budget(&self) -> usize { + match self.caps.markdown { + MarkdownDialect::TelegramHtml => self.caps.max_text_chars * 3 / 4, + MarkdownDialect::Plain => self.caps.max_text_chars, + } + } + + pub fn on_event(&mut self, ev: &AgentEvent, now: Instant) -> Vec { + let mut out = Vec::new(); + if let Some(line) = crate::commands::goal_event_line(ev) { + self.finish_segment(&mut out); + out.push(OutItem::text(line)); + return out; + } + match ev { + AgentEvent::TurnStart => { + self.tools.clear(); + self.tool_count = 0; + self.tool_slot = None; + self.tools_dirty = false; + } + AgentEvent::Text(t) => { + self.text.push_str(t); + // A very long answer: hand complete chunks over right away. + if self.text.chars().count() > self.chunk_budget() { + let mut chunks = markdown::split(&self.text, self.chunk_budget()); + let last = chunks.pop().unwrap_or_default(); + for c in chunks { + self.emit_final(&mut out, c); + } + self.text = last; + } + } + AgentEvent::Retry { .. } => { + // The streamed half is void; the live message is overwritten. + self.text.clear(); + } + AgentEvent::ToolCall { + name, arguments, .. + } => { + self.finish_segment(&mut out); + if name == "ask_user" { + let question = arguments + .get("question") + .and_then(|q| q.as_str()) + .unwrap_or("") + .to_string(); + let options = arguments + .get("options") + .and_then(|o| o.as_array()) + .map(|a| { + a.iter() + .filter_map(|v| v.as_str().map(String::from)) + .collect() + }) + .unwrap_or_default(); + out.push(OutItem::Send { + msg: OutboundMessage::Choices { question, options }, + slot: None, + }); + } else if self.caps.edit && self.show_tools { + self.tool_count += 1; + let desc = if name == "run_code" { + arguments + .get("description") + .and_then(|d| d.as_str()) + .map(|d| format!("run_code · {}", d.lines().next().unwrap_or(""))) + .unwrap_or_else(|| crate::subagents::describe_call(name, arguments)) + } else { + crate::subagents::describe_call(name, arguments) + }; + self.tools + .push(format!("⚙ {}", crate::llm::truncate_chars(&desc, 80))); + if self.tools.len() > MAX_TOOL_LINES { + self.tools.remove(0); + } + self.tools_dirty = true; + } + } + AgentEvent::ToolResult { success, .. } => { + if !success + && let Some(l) = self.tools.last_mut() + && l.starts_with('⚙') + { + *l = l.replacen('⚙', "✗", 1); + self.tools_dirty = true; + } + } + AgentEvent::Info(t) => { + self.finish_segment(&mut out); + out.push(OutItem::text(t.clone())); + } + AgentEvent::Warning(t) => { + self.finish_segment(&mut out); + out.push(OutItem::text(format!("⚠ {t}"))); + } + AgentEvent::Error(t) => { + self.finish_segment(&mut out); + out.push(OutItem::text(format!("✗ {t}"))); + } + AgentEvent::Interrupted => { + self.finish_segment(&mut out); + out.push(OutItem::text("⏹ 已打断")); + } + AgentEvent::Compaction { + compacted: true, + tokens_before, + tokens_after, + .. + } => out.push(OutItem::text(format!( + "已压缩上下文:约 {tokens_before} → {tokens_after} tokens" + ))), + AgentEvent::TurnEnd => { + self.finish_segment(&mut out); + self.flush_tools(&mut out, true); + } + _ => {} + } + if self.tools_dirty && self.due(now) { + self.flush_tools(&mut out, false); + self.last_flush = Some(now); + } + out + } + + fn due(&self, now: Instant) -> bool { + self.last_flush + .is_none_or(|t| now.duration_since(t) >= self.caps.min_send_interval) + } + + /// Periodic work: stream the answer preview and the tool list. + pub fn tick(&mut self, now: Instant) -> Vec { + let mut out = Vec::new(); + if !self.caps.edit || !self.due(now) { + return out; + } + let mut flushed = false; + if self.tools_dirty { + self.flush_tools(&mut out, false); + flushed = true; + } + let preview = self.text.trim_end(); + if !preview.is_empty() { + let shown = format!("{preview}{CURSOR}"); + match &self.live { + Some((_, last)) if *last == shown => {} + Some((slot, _)) => { + let slot = *slot; + out.push(OutItem::Edit { + slot, + text: shown.clone(), + format: TextFormat::Plain, + }); + self.live = Some((slot, shown)); + flushed = true; + } + None => { + let slot = self.slot(); + out.push(OutItem::Send { + msg: OutboundMessage::Text { + text: shown.clone(), + format: TextFormat::Plain, + }, + slot: Some(slot), + }); + self.live = Some((slot, shown)); + flushed = true; + } + } + } + if flushed { + self.last_flush = Some(now); + } + out + } + + fn flush_tools(&mut self, out: &mut Vec, done: bool) { + if !self.tools_dirty && !(done && self.tool_slot.is_some()) { + return; + } + self.tools_dirty = false; + if self.tools.is_empty() { + return; + } + let mut text = self.tools.join("\n"); + if self.tool_count > self.tools.len() { + text = format!("…(共 {} 次工具调用)\n{text}", self.tool_count); + } + if done { + text.push_str(&format!("\n✓ 本轮调用工具 {} 次", self.tool_count)); + } + match self.tool_slot { + Some(slot) => out.push(OutItem::Edit { + slot, + text, + format: TextFormat::Plain, + }), + None => { + let slot = self.slot(); + self.tool_slot = Some(slot); + out.push(OutItem::Send { + msg: OutboundMessage::Text { + text, + format: TextFormat::Plain, + }, + slot: Some(slot), + }); + } + } + if done { + self.tool_slot = None; + self.tools.clear(); + } + } + + /// The current segment is complete: send it formatted. + fn finish_segment(&mut self, out: &mut Vec) { + let text = std::mem::take(&mut self.text); + if text.trim().is_empty() { + if let Some((slot, _)) = self.live.take() { + out.push(OutItem::Edit { + slot, + text: "…".into(), + format: TextFormat::Plain, + }); + } + return; + } + for chunk in markdown::split(text.trim(), self.chunk_budget()) { + self.emit_final(out, chunk); + } + } + + /// One finished chunk: replaces the live preview if there is one. + fn emit_final(&mut self, out: &mut Vec, chunk: String) { + match self.live.take() { + Some((slot, _)) => out.push(OutItem::Edit { + slot, + text: chunk, + format: TextFormat::Markdown, + }), + None => out.push(OutItem::Send { + msg: OutboundMessage::Text { + text: chunk, + format: TextFormat::Markdown, + }, + slot: None, + }), + } + } + + /// How often the worker should call [`Renderer::tick`]. + pub fn tick_interval(&self) -> Duration { + self.caps + .min_send_interval + .clamp(Duration::from_millis(50), Duration::from_millis(500)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + fn caps(edit: bool) -> ChannelCaps { + ChannelCaps { + max_text_chars: 400, + edit, + markdown: MarkdownDialect::Plain, + min_send_interval: Duration::from_secs(1), + typing_ttl: Duration::from_secs(5), + } + } + + fn texts(items: &[OutItem]) -> Vec { + items + .iter() + .map(|i| match i { + OutItem::Send { + msg: OutboundMessage::Text { text, .. }, + slot, + } => format!( + "send{}:{text}", + slot.map(|s| format!("#{s}")).unwrap_or_default() + ), + OutItem::Edit { slot, text, .. } => format!("edit#{slot}:{text}"), + OutItem::Send { + msg: OutboundMessage::Choices { question, options }, + .. + } => format!("ask:{question}:{}", options.join("|")), + other => format!("{other:?}"), + }) + .collect() + } + + #[test] + fn non_editing_channel_gets_finished_segments_only() { + let mut r = Renderer::new(caps(false), true); + let t0 = Instant::now(); + let mut out = vec![]; + out.extend(r.on_event(&AgentEvent::TurnStart, t0)); + out.extend(r.on_event(&AgentEvent::Text("Hel".into()), t0)); + out.extend(r.tick(t0 + Duration::from_secs(5))); + out.extend(r.on_event(&AgentEvent::Text("lo".into()), t0)); + out.extend(r.on_event( + &AgentEvent::ToolCall { + id: "1".into(), + name: "read".into(), + arguments: json!({"path": "a.rs"}), + provider: crate::capability::Provider::Tool, + }, + t0, + )); + out.extend(r.on_event(&AgentEvent::Text("Done.".into()), t0)); + out.extend(r.on_event(&AgentEvent::TurnEnd, t0)); + assert_eq!(texts(&out), vec!["send:Hello", "send:Done."]); + } + + #[test] + fn editing_channel_streams_then_formats() { + let mut r = Renderer::new(caps(true), true); + let t0 = Instant::now(); + let mut out = vec![]; + out.extend(r.on_event(&AgentEvent::TurnStart, t0)); + out.extend(r.on_event(&AgentEvent::Text("**Hi**".into()), t0)); + out.extend(r.tick(t0)); + out.extend(r.on_event(&AgentEvent::Text(" there".into()), t0)); + out.extend(r.tick(t0 + Duration::from_millis(200))); // throttled + out.extend(r.tick(t0 + Duration::from_secs(2))); + out.extend(r.on_event(&AgentEvent::TurnEnd, t0 + Duration::from_secs(3))); + assert_eq!( + texts(&out), + vec![ + "send#1:**Hi** ▍", + "edit#1:**Hi** there ▍", + "edit#1:**Hi** there" + ] + ); + assert!(matches!( + out.last(), + Some(OutItem::Edit { + format: TextFormat::Markdown, + .. + }) + )); + } + + #[test] + fn tool_progress_is_one_edited_message() { + let mut r = Renderer::new(caps(true), true); + let t0 = Instant::now(); + let call = |n: &str| AgentEvent::ToolCall { + id: n.into(), + name: "read".into(), + arguments: json!({"path": n}), + provider: crate::capability::Provider::Tool, + }; + let mut out = vec![]; + out.extend(r.on_event(&AgentEvent::TurnStart, t0)); + out.extend(r.on_event(&call("a.rs"), t0)); + out.extend(r.on_event(&call("b.rs"), t0)); // throttled + out.extend(r.tick(t0 + Duration::from_secs(2))); + out.extend(r.on_event(&AgentEvent::TurnEnd, t0 + Duration::from_secs(4))); + let t = texts(&out); + assert_eq!(t.len(), 3); + assert!(t[0].starts_with("send#1:⚙")); + assert!(t[1].starts_with("edit#1:") && t[1].contains("b.rs")); + assert!(t[2].ends_with("✓ 本轮调用工具 2 次")); + } + + #[test] + fn ask_user_becomes_choices() { + let mut r = Renderer::new(caps(false), true); + let out = r.on_event( + &AgentEvent::ToolCall { + id: "q".into(), + name: "ask_user".into(), + arguments: json!({"question": "Which?", "options": ["A", "B"]}), + provider: crate::capability::Provider::Human, + }, + Instant::now(), + ); + assert_eq!(texts(&out), vec!["ask:Which?:A|B"]); + } + + #[test] + fn long_answers_are_chunked() { + let mut r = Renderer::new(caps(false), true); + let t0 = Instant::now(); + let mut out = vec![]; + for i in 0..30 { + out.extend(r.on_event( + &AgentEvent::Text(format!("paragraph {i} {}\n\n", "x".repeat(40))), + t0, + )); + } + out.extend(r.on_event(&AgentEvent::TurnEnd, t0)); + assert!(out.len() > 3); + for t in texts(&out) { + assert!(t.chars().count() <= 400 + 5, "{t}"); + } + } +} diff --git a/src/gateway/send_file.rs b/src/gateway/send_file.rs new file mode 100644 index 0000000..c5ed034 --- /dev/null +++ b/src/gateway/send_file.rs @@ -0,0 +1,130 @@ +//! `send_file`: deliver a local file to the person on the chat this session +//! is attached to. Registered only in gateway sessions. +//! +//! Effect `mutate` (it reaches outside the machine, and plan mode blocks +//! it), recovery `inspect` (a crash must not send the file twice), provider +//! `channel`. + +use std::path::Path; + +use async_trait::async_trait; +use serde_json::{Value, json}; +use tokio::sync::mpsc; + +use super::render::OutItem; +use crate::capability::Provider; +use crate::channels::OutboundMessage; +use crate::effect::{Effect, Recovery}; +use crate::models::ToolResult; +use crate::tools::{Tool, ToolContext, arg_str, resolve_path}; + +pub struct SendFileTool { + pub out: mpsc::UnboundedSender, + /// Display name of the chat ("Telegram" / "微信"). + pub channel: &'static str, + pub max_bytes: u64, +} + +#[async_trait] +impl Tool for SendFileTool { + fn name(&self) -> &'static str { + "send_file" + } + fn description(&self) -> String { + format!( + "Send a local file (report, image, log, patch) to the user in this {} chat. \ + Use it when the user asks for a file, or when a result is too long to read as a message.", + self.channel + ) + } + fn parameters(&self) -> Value { + json!({ + "type": "object", + "properties": { + "path": {"type": "string", "description": "File to send (relative to the workdir, or absolute)."}, + "caption": {"type": "string", "description": "Optional one-line caption."}, + }, + "required": ["path"], + }) + } + fn effect(&self) -> Effect { + Effect::Mutate + } + fn recovery(&self) -> Recovery { + Recovery::Inspect + } + fn provider(&self) -> Provider { + Provider::Channel + } + fn program_callable(&self) -> bool { + false + } + async fn run(&self, args: Value, ctx: &ToolContext) -> ToolResult { + let Some(raw) = arg_str(&args, "path").filter(|p| !p.trim().is_empty()) else { + return ToolResult::err("path must be a non-empty string"); + }; + let path = resolve_path(&ctx.workdir, raw); + check(&path, self.max_bytes) + .map(|size| { + let caption = arg_str(&args, "caption").map(String::from); + let _ = self.out.send(OutItem::Send { + msg: OutboundMessage::File { + path: path.clone(), + caption, + }, + slot: None, + }); + ToolResult::ok(format!( + "Queued {} ({size} bytes) for delivery to the user's {} chat.", + path.display(), + self.channel + )) + }) + .unwrap_or_else(ToolResult::err) + } +} + +fn check(path: &Path, max: u64) -> Result { + let meta = + std::fs::metadata(path).map_err(|e| format!("cannot send {}: {e}", path.display()))?; + if !meta.is_file() { + return Err(format!("{} is not a file", path.display())); + } + if meta.len() > max { + return Err(format!( + "{} is {} bytes; the chat accepts at most {max}", + path.display(), + meta.len() + )); + } + Ok(meta.len()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + async fn queues_existing_files_only() { + let dir = tempfile::tempdir().unwrap(); + std::fs::write(dir.path().join("r.txt"), "report").unwrap(); + let (tx, mut rx) = mpsc::unbounded_channel(); + let tool = SendFileTool { + out: tx, + channel: "Telegram", + max_bytes: 1024, + }; + let ctx = ToolContext::simple(dir.path()); + let r = tool + .run(json!({"path": "r.txt", "caption": "日报"}), &ctx) + .await; + assert!(r.success, "{:?}", r.error); + assert!(matches!( + rx.try_recv(), + Ok(OutItem::Send { msg: OutboundMessage::File { caption: Some(c), .. }, .. }) if c == "日报" + )); + let r = tool.run(json!({"path": "missing.txt"}), &ctx).await; + assert!(!r.success); + assert!(rx.try_recv().is_err()); + } +} diff --git a/src/gateway/service.rs b/src/gateway/service.rs new file mode 100644 index 0000000..705c07d --- /dev/null +++ b/src/gateway/service.rs @@ -0,0 +1,259 @@ +//! Keep `limbo gateway` running in the background: a launchd agent on +//! macOS (`~/Library/LaunchAgents/dev.limbo.gateway.plist`) or a systemd +//! user unit on Linux (`~/.config/systemd/user/limbo-gateway.service`). +//! +//! Both start at login, restart the gateway when it exits and log to +//! `~/.limbo/channels/gateway.log`. The service inherits this shell's +//! `PATH` (the agent's tools need `rg`, `python3`, …; launchd's default +//! PATH has neither) and proxy variables. +//! +//! 🦀 Rust notes +//! - `cfg!(target_os = "macos")` is a compile-time constant in an ordinary +//! `if`: both branches are type-checked on every platform, so the macOS +//! path is compiled (and its file generation unit-tested) on Linux CI too. + +use std::path::{Path, PathBuf}; +use std::process::Command; + +use crate::channels::store::channels_dir; + +pub const LAUNCHD_LABEL: &str = "dev.limbo.gateway"; +pub const SYSTEMD_UNIT: &str = "limbo-gateway.service"; + +/// Environment variables carried into the service. +const KEEP_ENV: [&str; 9] = [ + "PATH", + "LANG", + "LIMBO_HOME", + "LIMBO_AUTH_FILE", + "HTTPS_PROXY", + "HTTP_PROXY", + "ALL_PROXY", + "NO_PROXY", + "SSL_CERT_FILE", +]; + +fn kept_env() -> Vec<(String, String)> { + KEEP_ENV + .iter() + .filter_map(|k| std::env::var(k).ok().map(|v| (k.to_string(), v))) + .collect() +} + +fn xml_escape(s: &str) -> String { + s.replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) +} + +/// The launchd property list. +pub fn launchd_plist(exe: &Path, args: &[String], log: &Path, env: &[(String, String)]) -> String { + let mut program = format!( + " {}\n", + xml_escape(&exe.display().to_string()) + ); + for a in args { + program.push_str(&format!(" {}\n", xml_escape(a))); + } + let mut envs = String::new(); + for (k, v) in env { + envs.push_str(&format!( + " {}\n {}\n", + xml_escape(k), + xml_escape(v) + )); + } + let log = xml_escape(&log.display().to_string()); + format!( + r#" + + + + Label + {LAUNCHD_LABEL} + ProgramArguments + +{program} + EnvironmentVariables + +{envs} + RunAtLoad + + KeepAlive + + SuccessfulExit + + + ThrottleInterval + 10 + ProcessType + Background + StandardOutPath + {log} + StandardErrorPath + {log} + + +"# + ) +} + +/// The systemd user unit. +pub fn systemd_unit(exe: &Path, args: &[String], log: &Path, env: &[(String, String)]) -> String { + let quote = |s: &str| format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\"")); + let mut cmd = quote(&exe.display().to_string()); + for a in args { + cmd.push(' '); + cmd.push_str("e(a)); + } + let envs: String = env + .iter() + .map(|(k, v)| format!("Environment={}\n", quote(&format!("{k}={v}")))) + .collect(); + format!( + "[Unit]\nDescription=limbo chat gateway (Telegram / WeChat)\nAfter=network-online.target\nWants=network-online.target\n\n\ +[Service]\nExecStart={cmd}\n{envs}Restart=on-failure\nRestartSec=10\nStandardOutput=append:{log}\nStandardError=append:{log}\n\n\ +[Install]\nWantedBy=default.target\n", + log = log.display() + ) +} + +fn home() -> PathBuf { + dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")) +} + +pub fn service_path() -> PathBuf { + if cfg!(target_os = "macos") { + home() + .join("Library/LaunchAgents") + .join(format!("{LAUNCHD_LABEL}.plist")) + } else { + home().join(".config/systemd/user").join(SYSTEMD_UNIT) + } +} + +pub fn log_path() -> PathBuf { + channels_dir().join("gateway.log") +} + +pub fn describe() -> String { + let p = service_path(); + if p.exists() { + format!("已安装({})· 日志 {}", p.display(), log_path().display()) + } else { + "未安装(`limbo gateway install` 开机自启)".into() + } +} + +fn run(cmd: &mut Command) -> bool { + match cmd.status() { + Ok(s) => s.success(), + Err(e) => { + eprintln!("warning: {cmd:?}: {e}"); + false + } + } +} + +fn uid() -> u32 { + // SAFETY: getuid has no failure mode and touches no memory. + unsafe { libc::getuid() } +} + +/// Write and start the service. `extra_args`: global flags to keep +/// (`--config`, `--session-dir`). +pub fn install(extra_args: &[String]) -> anyhow::Result { + let exe = std::env::current_exe()?; + let mut args: Vec = extra_args.to_vec(); + args.push("gateway".into()); + let log = log_path(); + std::fs::create_dir_all(channels_dir())?; + let path = service_path(); + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir)?; + } + let env = kept_env(); + if cfg!(target_os = "macos") { + let target = format!("gui/{}", uid()); + // Replace a loaded older copy. + let _ = Command::new("launchctl") + .args(["bootout", &format!("{target}/{LAUNCHD_LABEL}")]) + .stderr(std::process::Stdio::null()) + .status(); + std::fs::write(&path, launchd_plist(&exe, &args, &log, &env))?; + let p = path.display().to_string(); + if !run(Command::new("launchctl").args(["bootstrap", &target, &p])) { + // Older macOS releases. + run(Command::new("launchctl").args(["load", "-w", &p])); + } + println!("✓ 已安装 launchd 服务 {LAUNCHD_LABEL}:{}", path.display()); + } else { + std::fs::write(&path, systemd_unit(&exe, &args, &log, &env))?; + run(Command::new("systemctl").args(["--user", "daemon-reload"])); + run(Command::new("systemctl").args(["--user", "enable", "--now", SYSTEMD_UNIT])); + println!( + "✓ 已安装 systemd 用户服务 {SYSTEMD_UNIT}:{}", + path.display() + ); + println!(" 注销后也保持运行:loginctl enable-linger $USER"); + } + println!(" 日志:{}", log.display()); + Ok(0) +} + +pub fn uninstall() -> anyhow::Result { + let path = service_path(); + if cfg!(target_os = "macos") { + let target = format!("gui/{}", uid()); + if !run(Command::new("launchctl").args(["bootout", &format!("{target}/{LAUNCHD_LABEL}")])) { + run(Command::new("launchctl").args(["unload", "-w", &path.display().to_string()])); + } + } else { + run(Command::new("systemctl").args(["--user", "disable", "--now", SYSTEMD_UNIT])); + } + if path.exists() { + std::fs::remove_file(&path)?; + println!("✓ 已移除 {}", path.display()); + } else { + println!("服务未安装"); + } + Ok(0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn plist_is_complete_and_escaped() { + let p = launchd_plist( + Path::new("/opt/limbo & co/limbo"), + &["gateway".into()], + Path::new("/Users/me/.limbo/channels/gateway.log"), + &[("PATH".into(), "/opt/homebrew/bin:/usr/bin".into())], + ); + assert!(p.contains("dev.limbo.gateway")); + assert!( + p.contains("/opt/limbo & co/limbo\n gateway") + ); + assert!(p.contains("PATH\n /opt/homebrew/bin:/usr/bin")); + assert!(p.contains("RunAtLoad\n ")); + assert_eq!(p.matches("").count(), p.matches("").count()); + } + + #[test] + fn systemd_unit_quotes_arguments() { + let u = systemd_unit( + Path::new("/usr/local/bin/limbo"), + &["--config".into(), "/a b/c.toml".into(), "gateway".into()], + Path::new("/h/.limbo/channels/gateway.log"), + &[("PATH".into(), "/usr/bin".into())], + ); + assert!(u.contains( + "ExecStart=\"/usr/local/bin/limbo\" \"--config\" \"/a b/c.toml\" \"gateway\"" + )); + assert!(u.contains("Environment=\"PATH=/usr/bin\"")); + assert!(u.contains("Restart=on-failure")); + } +} diff --git a/src/gateway/sessions.rs b/src/gateway/sessions.rs new file mode 100644 index 0000000..6c88ecb --- /dev/null +++ b/src/gateway/sessions.rs @@ -0,0 +1,110 @@ +//! Which limbo session each chat is talking to: `~/.limbo/channels/sessions.json`, +//! `peer key → { current session, earlier sessions }`. A chat always has at +//! most one live session; `/new` starts another and keeps the old one in the +//! chat's history for `/resume`. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use serde::{Deserialize, Serialize}; + +use crate::channels::PeerKey; + +const HISTORY: usize = 20; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PeerEntry { + pub peer: PeerKey, + pub current: String, + /// Earlier sessions of this chat, newest first. + #[serde(default)] + pub history: Vec, +} + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct SessionMap { + #[serde(default)] + pub peers: BTreeMap, + #[serde(skip)] + path: PathBuf, +} + +impl SessionMap { + pub fn load(path: &Path) -> Self { + let mut m: SessionMap = std::fs::read_to_string(path) + .ok() + .and_then(|t| serde_json::from_str(&t).ok()) + .unwrap_or_default(); + m.path = path.to_path_buf(); + m + } + + fn save(&self) { + if let Ok(text) = serde_json::to_string_pretty(self) + && let Err(e) = crate::channels::store::write_private(&self.path, text.as_bytes()) + { + eprintln!("warning: cannot save {}: {e}", self.path.display()); + } + } + + pub fn current(&self, peer: &PeerKey) -> Option { + self.peers.get(&peer.key()).map(|e| e.current.clone()) + } + + /// Sessions this chat may `/resume` (current first). + pub fn known(&self, peer: &PeerKey) -> Vec { + self.peers + .get(&peer.key()) + .map(|e| { + let mut v = vec![e.current.clone()]; + v.extend(e.history.iter().cloned()); + v + }) + .unwrap_or_default() + } + + /// Make `session` the chat's current session. + pub fn set(&mut self, peer: &PeerKey, session: &str) { + let entry = self.peers.entry(peer.key()).or_insert_with(|| PeerEntry { + peer: peer.clone(), + current: String::new(), + history: vec![], + }); + if entry.current == session { + return; + } + let old = std::mem::replace(&mut entry.current, session.to_string()); + entry.history.retain(|s| s != session && s != &old); + if !old.is_empty() { + entry.history.insert(0, old); + } + entry.history.truncate(HISTORY); + self.save(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::channels::ChannelKind; + + #[test] + fn current_and_history_survive_reload() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("sessions.json"); + let peer = PeerKey { + channel: ChannelKind::Telegram, + account: "1".into(), + chat: "2".into(), + thread: None, + }; + let mut m = SessionMap::load(&path); + assert_eq!(m.current(&peer), None); + m.set(&peer, "s1"); + m.set(&peer, "s2"); + m.set(&peer, "s1"); + let m = SessionMap::load(&path); + assert_eq!(m.current(&peer).as_deref(), Some("s1")); + assert_eq!(m.known(&peer), vec!["s1", "s2"]); + } +} diff --git a/src/gateway/worker.rs b/src/gateway/worker.rs new file mode 100644 index 0000000..247a679 --- /dev/null +++ b/src/gateway/worker.rs @@ -0,0 +1,551 @@ +//! One worker per chat: a tokio task that owns that chat's +//! [`Conversation`] and turns its traffic into chat messages. +//! +//! The loop mirrors the headless REPL's (`headless::run_repl`): inbound +//! messages, agent events of the running turn, the turn finishing, +//! background events of this session (subagents) and a ticker for streaming +//! previews, typing indicators and unloading an idle agent. +//! +//! - idle + message → a new turn (`pump::run_task` via the Conversation); +//! - busy + message → *steer* into the running turn (`/queue` waits); +//! - `/stop` interrupts; `/sessions` and `/resume ` switch between this +//! chat's sessions; everything else is the usual slash commands. +//! +//! 🦀 Rust notes +//! - `conv: Option>`: the agent is loaded lazily on the +//! first message and dropped again after `idle_unload_secs`, so a +//! gateway with many quiet chats holds no agents in memory. + +use std::path::PathBuf; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use tokio::sync::mpsc; +use tokio_util::sync::CancellationToken; + +use super::outbox::{self, SharedCtx}; +use super::render::{OutItem, Renderer}; +use super::send_file::SendFileTool; +use super::sessions::SessionMap; +use crate::agent::{Agent, AgentEvent}; +use crate::background::{self, BackgroundEvent}; +use crate::channels::{Channel, ChannelKind, InboundMessage, MediaKind, OutboundMessage, PeerKey}; +use crate::config::Config; +use crate::conversation::{Conversation, Sink}; +use crate::integrations::Reporters; +use crate::models::Attachment; + +/// Everything a worker needs besides its chat. +#[derive(Clone)] +pub struct WorkerEnv { + pub config: Config, + pub session_dir: PathBuf, + pub workdir: PathBuf, + pub daily: bool, + pub sessions: Arc>, + pub idle_unload: Option, + pub max_attachment: u64, + pub show_tools: bool, +} + +/// The Conversation's output in a chat: lines become plain messages. +struct ChatSink { + out: mpsc::UnboundedSender, + btw: String, + channel: ChannelKind, + max_attachment: u64, +} + +impl ChatSink { + fn install_tools(&self, agent: &mut Agent) { + agent.registry.add(Arc::new(SendFileTool { + out: self.out.clone(), + channel: match self.channel { + ChannelKind::Telegram => "Telegram", + ChannelKind::Weixin => "WeChat", + #[cfg(test)] + ChannelKind::Mock => "mock", + }, + max_bytes: self.max_attachment.max(50 * 1024 * 1024), + })); + agent.init_system_message(); + } +} + +impl Sink for ChatSink { + fn line(&mut self, text: &str) { + let _ = self.out.send(OutItem::text(text)); + } + fn btw_chunk(&mut self, text: &str) { + self.btw.push_str(text); + } + fn btw_end(&mut self) { + let t = std::mem::take(&mut self.btw); + if !t.trim().is_empty() { + let _ = self.out.send(OutItem::Send { + msg: OutboundMessage::Text { + text: t, + format: crate::channels::TextFormat::Markdown, + }, + slot: None, + }); + } + } + fn agent_replaced(&mut self, agent: &mut Agent) { + self.install_tools(agent); + } + fn allows_quit(&self) -> bool { + false + } +} + +pub const CHAT_HELP: &str = "聊天里额外可用:/stop 打断当前任务 · /sessions 本聊天的会话 · /resume 切回旧会话 · /status 当前状态"; + +/// Start the worker of `peer`; returns its mailbox. +pub fn spawn( + env: WorkerEnv, + channel: Arc, + peer: PeerKey, + cancel: CancellationToken, +) -> mpsc::UnboundedSender { + let (tx, rx) = mpsc::unbounded_channel(); + tokio::spawn(async move { + Worker::new(env, channel, peer).run(rx, cancel).await; + }); + tx +} + +struct Worker { + env: WorkerEnv, + channel: Arc, + peer: PeerKey, + ctx: SharedCtx, + out: mpsc::UnboundedSender, + conv: Option>, + ev_rx: Option>, + bg_tx: mpsc::UnboundedSender, + bg_rx: mpsc::UnboundedReceiver, + renderer: Renderer, + routed: String, + last_activity: Instant, + last_typing: Option, +} + +impl Worker { + fn new(env: WorkerEnv, channel: Arc, peer: PeerKey) -> Self { + let ctx: SharedCtx = Arc::new(Mutex::new(Default::default())); + let out = outbox::spawn(channel.clone(), peer.clone(), ctx.clone()); + let (bg_tx, bg_rx) = mpsc::unbounded_channel(); + let renderer = Renderer::new(channel.caps(), env.show_tools); + Self { + env, + channel, + peer, + ctx, + out, + conv: None, + ev_rx: None, + bg_tx, + bg_rx, + renderer, + routed: String::new(), + last_activity: Instant::now(), + last_typing: None, + } + } + + fn say(&self, text: impl Into) { + let _ = self.out.send(OutItem::text(text)); + } + + fn busy(&self) -> bool { + self.conv.as_ref().is_some_and(|c| c.busy()) + } + + /// Load (resume or create) this chat's agent. + fn load(&mut self, session: Option<&str>) -> anyhow::Result<()> { + let current = match session { + Some(s) => Some(s.to_string()), + None => self.env.sessions.lock().unwrap().current(&self.peer), + }; + let resumed = current + .as_deref() + .and_then(|id| crate::session::find_session(&self.env.session_dir, id).ok()) + .and_then(|p| Agent::resume(self.env.config.clone(), &p, &self.env.session_dir).ok()); + let mut agent = match resumed { + Some(a) => a, + None => { + if session.is_some() { + anyhow::bail!("找不到会话 {}", session.unwrap_or("")); + } + Agent::create( + self.env.config.clone(), + &self.env.workdir, + &self.env.session_dir, + self.env.daily, + )? + } + }; + let sink = ChatSink { + out: self.out.clone(), + btw: String::new(), + channel: self.peer.channel, + max_attachment: self.env.max_attachment, + }; + sink.install_tools(&mut agent); + let (tx, rx) = mpsc::unbounded_channel(); + // Chat sessions report nothing to terminal integrations. + let reporters = Arc::new(Reporters::default()); + reporters.release(); + self.conv = Some(Conversation::new(agent, sink, tx, reporters)); + self.ev_rx = Some(rx); + self.sync_session(); + Ok(()) + } + + /// Keep the session map, the origin tag and the background route in + /// step with the agent's current session. + fn sync_session(&mut self) { + let Some(agent) = self.conv.as_mut().and_then(|c| c.agent.as_mut()) else { + return; + }; + let id = agent.session_id().to_string(); + if id == self.routed { + return; + } + if !self.routed.is_empty() { + background::unroute(&self.routed); + } + background::route(&id, self.bg_tx.clone()); + self.routed = id.clone(); + agent + .meta + .extra + .insert("origin".into(), serde_json::json!(self.peer.key())); + agent.persist_meta(); + self.env.sessions.lock().unwrap().set(&self.peer, &id); + } + + async fn unload(&mut self) { + if let Some(mut c) = self.conv.take() { + c.shutdown().await; + } + self.ev_rx = None; + if !self.routed.is_empty() { + background::unroute(&self.routed); + self.routed.clear(); + } + } + + async fn run( + mut self, + mut inbox: mpsc::UnboundedReceiver, + cancel: CancellationToken, + ) { + let mut ticker = tokio::time::interval(self.renderer.tick_interval()); + ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); + loop { + let busy = self.busy(); + let has_events = self.ev_rx.is_some(); + // Disjoint borrows for the branch futures; the handlers below run + // after those futures are dropped and may use `self` freely. + let Worker { + ev_rx, conv, bg_rx, .. + } = &mut self; + tokio::select! { + _ = cancel.cancelled() => break, + m = inbox.recv() => match m { + Some(m) => self.on_message(m).await, + None => break, + }, + Some(ev) = async { ev_rx.as_mut().unwrap().recv().await }, if has_events => { + self.on_event(ev); + } + Some(done) = async { conv.as_mut().unwrap().join().await }, if busy => { + self.turn_done(done); + } + Some(ev) = bg_rx.recv() => { + if let Some(c) = self.conv.as_mut() { + for l in c.background_lines(&ev) { + c.sink.line(&l); + } + c.after_background(&ev); + } + } + _ = ticker.tick() => self.on_tick().await, + } + } + self.unload().await; + } + + fn turn_done(&mut self, done: Result) { + // Events emitted just before the task returned. + while let Some(ev) = self.ev_rx.as_mut().and_then(|r| r.try_recv().ok()) { + self.on_event(ev); + } + match done { + Ok(agent) => { + if let Some(c) = self.conv.as_mut() { + c.job_done(agent); + } + self.sync_session(); + } + Err(e) => { + self.say(format!("✗ 内部错误:{e}")); + self.conv = None; + self.ev_rx = None; + } + } + self.last_activity = Instant::now(); + } + + fn on_event(&mut self, ev: AgentEvent) { + if let Some(c) = self.conv.as_mut() { + c.observe(&ev); + } + for item in self.renderer.on_event(&ev, Instant::now()) { + let _ = self.out.send(item); + } + } + + async fn on_tick(&mut self) { + let now = Instant::now(); + for item in self.renderer.tick(now) { + let _ = self.out.send(item); + } + if self.busy() { + let ttl = self.channel.caps().typing_ttl; + if self + .last_typing + .is_none_or(|t| now.duration_since(t) >= ttl) + { + self.last_typing = Some(now); + let _ = self.out.send(OutItem::Send { + msg: OutboundMessage::Typing, + slot: None, + }); + } + self.last_activity = now; + return; + } + self.last_typing = None; + if let Some(limit) = self.env.idle_unload + && self + .conv + .as_ref() + .is_some_and(|c| c.proposals.is_none() && !c.sup().is_some_and(|s| s.working())) + && now.duration_since(self.last_activity) >= limit + { + self.unload().await; + } + } + + async fn on_message(&mut self, m: InboundMessage) { + self.last_activity = Instant::now(); + if m.reply_ctx.0.is_some() { + *self.ctx.lock().unwrap() = m.reply_ctx.clone(); + } + let _ = self.out.send(OutItem::Resume); + let text = m.text.trim().to_string(); + let (cmd, arg) = match text.strip_prefix('/') { + Some(rest) => rest + .split_once(char::is_whitespace) + .map_or((rest, ""), |(c, a)| (c, a.trim())), + None => ("", ""), + }; + match cmd.to_lowercase().as_str() { + "stop" => { + match self.conv.as_mut() { + Some(c) if c.busy() => c.interrupt(), + _ => self.say("当前没有进行中的任务"), + } + return; + } + "start" if self.conv.is_none() && arg.is_empty() => { + self.say(format!( + "limbo 已连接。直接发消息开始;/help 查看命令。\n{CHAT_HELP}" + )); + return; + } + "sessions" => { + let known = self.env.sessions.lock().unwrap().known(&self.peer); + if known.is_empty() { + self.say("这个聊天还没有会话"); + } else { + let mut lines = vec!["本聊天的会话(第一个是当前会话):".to_string()]; + lines.extend(known.iter().map(|s| format!("• {s}"))); + lines.push("/resume 切换".into()); + self.say(lines.join("\n")); + } + return; + } + "resume" => { + return self.resume(arg).await; + } + "status" => { + let s = match self.conv.as_ref().and_then(|c| c.agent.as_ref()) { + Some(a) => format!( + "会话 {} · 模型 {} · {} · 累计 {} tokens", + a.session_id(), + a.client.model_id(), + if a.daily_mode() { "daily" } else { "coding" }, + a.session_total_tokens() + ), + None if self.busy() => "任务进行中".into(), + None => "空闲(下一条消息时加载会话)".into(), + }; + self.say(s); + return; + } + _ => {} + } + if self.conv.is_none() + && let Err(e) = self.load(None) + { + self.say(format!("✗ 无法启动会话:{e}")); + return; + } + let attachments = self.download(&m).await; + if text.is_empty() && attachments.is_empty() { + return; + } + let Some(c) = self.conv.as_mut() else { return }; + c.submit(text.clone(), attachments).await; + if cmd.eq_ignore_ascii_case("help") { + self.say(CHAT_HELP); + } + self.sync_session(); + } + + async fn resume(&mut self, arg: &str) { + if self.busy() { + return self.say("当前任务进行中;先 /stop 或等待完成"); + } + let known = self.env.sessions.lock().unwrap().known(&self.peer); + let matches: Vec<&String> = known + .iter() + .filter(|s| !arg.is_empty() && s.starts_with(arg)) + .collect(); + let id = match matches.as_slice() { + [one] => (*one).clone(), + [] => return self.say("用法:/resume (/sessions 查看本聊天的会话)"), + _ => return self.say("前缀对应多个会话,请写长一点"), + }; + self.unload().await; + match self.load(Some(&id)) { + Ok(()) => self.say(format!("已切换到会话 {id}")), + Err(e) => { + self.say(format!("✗ {e}")); + let _ = self.load(None); + } + } + } + + /// Download the message's media next to the session file. + async fn download(&self, m: &InboundMessage) -> Vec { + if m.media.is_empty() { + return vec![]; + } + let dir = self + .env + .session_dir + .join("media") + .join(crate::channels::safe_file_name(&self.routed)) + .join(crate::channels::safe_file_name(&m.msg_id)); + let mut out = Vec::new(); + for media in &m.media { + match self + .channel + .fetch_media(media, &dir, self.env.max_attachment) + .await + { + Ok(p) => out.push(crate::attachments::attachment_for(&p)), + Err(e) => { + let what = match media.kind { + MediaKind::Image => "图片", + MediaKind::File => "文件", + MediaKind::Voice => "语音", + MediaKind::Video => "视频", + }; + self.say(format!("✗ {what}下载失败:{e}")) + } + } + } + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::agent::tests::{fake_server, sse, test_config}; + use crate::channels::ReplyCtx; + use crate::channels::mock::MockChannel; + use serde_json::json; + + fn msg(peer: &PeerKey, id: &str, text: &str) -> InboundMessage { + InboundMessage { + peer: peer.clone(), + sender: "42".into(), + sender_name: "owner".into(), + msg_id: id.into(), + text: text.into(), + media: vec![], + reply_ctx: ReplyCtx(None), + is_group: false, + addressed: true, + } + } + + /// A chat message runs a turn end to end: the reply reaches the chat, + /// the chat → session mapping is recorded, and `/status` answers. + #[tokio::test] + async fn message_runs_a_turn_and_records_the_session() { + let dir = tempfile::tempdir().unwrap(); + let reply = sse(&[ + json!({"choices":[{"delta":{"content":"你好,我在。"},"finish_reason":"stop"}]}), + json!({"choices":[],"usage":{"prompt_tokens":10,"total_tokens":12}}), + ]); + let (base, seen) = fake_server(vec![reply]).await; + let workdir = dir.path().join("work"); + std::fs::create_dir_all(&workdir).unwrap(); + let sessions = Arc::new(Mutex::new(SessionMap::load( + &dir.path().join("sessions.json"), + ))); + let env = WorkerEnv { + config: test_config(&base), + session_dir: dir.path().join("sessions"), + workdir, + daily: true, + sessions: sessions.clone(), + idle_unload: None, + max_attachment: 1024 * 1024, + show_tools: true, + }; + let mock = MockChannel::default(); + let peer = PeerKey { + channel: ChannelKind::Mock, + account: "mockbot".into(), + chat: "42".into(), + thread: None, + }; + let cancel = CancellationToken::new(); + let tx = spawn(env, Arc::new(mock.clone()), peer.clone(), cancel.clone()); + tx.send(msg(&peer, "1", "在吗")).unwrap(); + assert!( + mock.wait_for(|t| t.contains("你好,我在。")).await, + "{:?}", + mock.texts() + ); + assert_eq!(seen.lock().unwrap().len(), 1); + assert!(sessions.lock().unwrap().current(&peer).is_some()); + + tx.send(msg(&peer, "2", "/status")).unwrap(); + assert!( + mock.wait_for(|t| t.contains("模型 test-model")).await, + "{:?}", + mock.texts() + ); + cancel.cancel(); + } +} diff --git a/src/headless.rs b/src/headless.rs index 1a6f983..6f4b144 100644 --- a/src/headless.rs +++ b/src/headless.rs @@ -8,28 +8,21 @@ //! policy — lives in [`crate::pump`]; this module only translates lines into //! calls and events into text. -use crate::integrations::{Reporters, State, TurnOutcome}; +use crate::integrations::Reporters; use std::io::Write; -use std::sync::Arc; use serde_json::json; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::sync::mpsc; -use tokio::task::JoinHandle; use tokio_util::sync::CancellationToken; -use crate::agent::{Agent, AgentEvent, FOLLOW_UP, STEER, SteerQueue}; +use crate::agent::{Agent, AgentEvent}; use crate::background::BackgroundEvent; -use crate::commands::{self as cmds, parse_toggle}; -use crate::goal::{GoalCommand, parse_goal_args}; +use crate::commands as cmds; +use crate::conversation::{Conversation, Sink}; use crate::pump; use crate::subagents::describe_call; -pub const HELP_TEXT: &str = "可用命令:/goal <目标>|clear 目标闭环 · /queue <文本> 任务完成后再发 · /compact 压缩上下文 · \ -/model [id] 切换模型 · /new 新会话 · /fork [n] 分叉 · /handoff <说明> 交接 · /plan on|off 计划模式 · \ -/code-mode on|off · /daily-mode on|off · /btw <问题> 侧问 · /agents [scan|init] 子代理 · /task 任务板 · @名字 <消息> · /tracing-viewer [id|off] · /help · /quit"; -pub const DAILY_HELP_TEXT: &str = " · /memory [edit] 查看记忆 · /learn 复盘写进 skill"; - fn event_json(ev: &AgentEvent) -> serde_json::Value { match ev { AgentEvent::TurnStart => json!({"type": "turn_start"}), @@ -259,415 +252,64 @@ pub async fn run_once(mut agent: Agent, prompt: String, json_mode: bool) -> i32 if agent.last_run_failed { 1 } else { 0 } } -enum Job { - Task(String), - Compact, +impl Sink for Printer { + fn line(&mut self, text: &str) { + Printer::line(self, text); + } + fn btw_chunk(&mut self, text: &str) { + print!("{text}"); + let _ = std::io::stdout().flush(); + } + fn btw_end(&mut self) { + println!(); + } + fn idle(&mut self) { + Printer::idle(self); + } + fn session_changed(&mut self, agent: &Agent) { + self.total_tokens = agent.session_total_tokens(); + } } +/// The stdin/stdout adapter around a [`Conversation`]. struct Repl { - agent: Option, - job: Option>, - cancel: CancellationToken, - steer: Arc, - tx: mpsc::UnboundedSender, - p: Printer, - /// The task that just ran was a /goal proposal round. - proposing: bool, - /// Blocked on the user's answer to a verify proposal. - proposals: Option>, - quit: bool, + conv: Conversation, eof: bool, eof_waiting: bool, - viewer: crate::trace_viewer::Viewer, - /// The board while the agent is away running a task. - sup_handle: Option>, - /// Herdr / OSC 7501 lifecycle reports. - reporters: std::sync::Arc, - outcome: TurnOutcome, - reported_session: String, -} - -/// Report the session identity when it changed since the last report. -fn note_session(last: &mut String, reporters: &Reporters, agent: &Agent) { - if agent.session_id() != last.as_str() { - *last = agent.session_id().to_string(); - reporters.report_session(last, agent.session_path()); - } } impl Repl { - fn busy(&self) -> bool { - self.job.is_some() - } - - fn start(&mut self, job: Job) { - let Some(mut agent) = self.agent.take() else { - return; - }; - self.cancel = CancellationToken::new(); - let cancel = self.cancel.clone(); - let tx = self.tx.clone(); - self.sup_handle = agent.supervisor.clone(); - note_session(&mut self.reported_session, &self.reporters, &agent); - self.outcome = TurnOutcome::default(); - self.reporters.report(State::Working, None); - self.job = Some(tokio::spawn(async move { - match job { - Job::Task(text) => pump::run_task(&mut agent, text, vec![], &tx, &cancel).await, - Job::Compact => { - agent.compact("manual", &tx, &cancel).await; - agent.checkpoint(); - } - } - agent - })); - } - fn job_done(&mut self, agent: Agent) { - self.sup_handle = agent.supervisor.clone(); - self.agent = Some(agent); - self.job = None; - self.p.idle(); - let o = std::mem::take(&mut self.outcome); - self.reporters.report(o.state, o.message.as_deref()); - if std::mem::take(&mut self.proposing) { - self.confirm_proposal(); - if self.proposals.is_some() { - self.reporters - .report(State::Blocked, Some("等待确认 /goal 验收命令")); - } - } - self.maybe_wake_main(); + self.conv.job_done(agent); self.finish_if_eof(); } /// After stdin closed: quit once the main agent is idle and no subagent /// is still working (their results would otherwise be lost). fn finish_if_eof(&mut self) { - if !self.eof || self.busy() || self.quit { + let c = &mut self.conv; + if !self.eof || c.busy() || c.quit { return; } - if self.proposals.take().is_some() { - self.p.line("输入已关闭,跳过验收确认(保持单轮模式)"); + if c.proposals.take().is_some() { + c.sink.line("输入已关闭,跳过验收确认(保持单轮模式)"); } // Results that already arrived go to the main agent first. - self.maybe_wake_main(); - if self.busy() { + c.maybe_wake_main(); + if c.busy() { return; } - if self.sup().is_some_and(|s| s.working()) { + if c.sup().is_some_and(|s| s.working()) { if !std::mem::replace(&mut self.eof_waiting, true) { - self.p.line("输入已关闭,等待子代理完成…(Ctrl+C 退出)"); - } - return; - } - self.quit = true; - } - - fn confirm_proposal(&mut self) { - let Some(agent) = &self.agent else { return }; - if pump::live_goal(agent).is_none() { - return; - } - match pump::take_proposal(agent) { - Some(cmds_) if cmds_.is_empty() => self.p.line(cmds::NO_VERIFY_GATE), - found => { - let list = found.unwrap_or_default(); - if list.is_empty() { - self.p.line("未能解析模型的验收提议。"); - } else { - self.p.line("模型提议的验收命令:"); - for (i, c) in list.iter().enumerate() { - self.p.line(&format!(" {}. {c}", i + 1)); - } - } - self.p - .line("回复序号采纳;或直接输入一行作为验收命令;/skip 跳过自动验收"); - self.proposals = Some(list); - } - } - } - - fn answer_verify(&mut self, text: &str, proposals: Vec) { - let t = text.trim(); - if t.is_empty() || t == "/skip" { - self.p.line(cmds::VERIFY_SKIPPED); - self.reporters.report(State::Idle, None); - return; - } - if t.starts_with('/') { - self.p - .line("正在等待验收确认:回复序号、验收命令,或 /skip 跳过"); - self.proposals = Some(proposals); - return; - } - let command = match t.parse::() { - Ok(i) if (1..=proposals.len()).contains(&i) => proposals[i - 1].clone(), - Ok(_) => { - self.p.line(&format!( - "序号超出范围(1–{});或直接输入验收命令,/skip 跳过", - proposals.len() - )); - self.proposals = Some(proposals); - return; + c.sink.line("输入已关闭,等待子代理完成…(Ctrl+C 退出)"); } - Err(_) => t.to_string(), - }; - let Some(agent) = self.agent.as_mut() else { - return; - }; - if let Some(prompt) = pump::set_verify(agent, &command) { - self.p.line(&format!("验收方式已确认:{command}")); - self.start(Job::Task(prompt)); - } - } - - async fn submit(&mut self, line: String) { - if let Some(proposals) = self.proposals.take() { - return self.answer_verify(&line, proposals); - } - let trimmed = line.trim(); - if trimmed.is_empty() { - return; - } - let (name, arg) = match trimmed.strip_prefix('/') { - Some(rest) if !line.starts_with(' ') => rest - .split_once(char::is_whitespace) - .map_or((rest, ""), |(c, a)| (c, a.trim())), - _ => ("", ""), - }; - let name = name.to_lowercase(); - // Board commands and @name never touch the main history: fine mid-turn. - if matches!(name.as_str(), "agents" | "task") { - let msg = match self.sup() { - None => "子代理已关闭(config.toml 的 [subagents] enabled = false)".to_string(), - Some(sup) if name == "agents" => crate::subagents::agents_command(&sup, arg), - Some(sup) => crate::subagents::task_command(&sup, arg), - }; - self.p.line(&msg); - return; - } - if name.is_empty() - && let Some(sup) = self.sup() - && let Some(reply) = crate::subagents::direct_message(&sup, &line) - { - self.p.line(&format!("➤ {reply}")); return; } - if self.busy() { - match name.as_str() { - "" => { - self.steer - .push_item(line.clone(), vec![], STEER, "user", vec![]); - self.p.line(&format!("➤ 已排队插话:{}", first_line(&line))); - } - "queue" if !arg.is_empty() => { - self.steer - .push_item(arg.to_string(), vec![], FOLLOW_UP, "user", vec![]); - self.p.line(&format!( - "➤ 已排队,当前任务完成后处理:{}", - first_line(arg) - )); - } - "help" => self.p.line(HELP_TEXT), - "tracing-viewer" => { - let msg = self.viewer.command(arg, Some(&self.reported_session)); - self.p.line(&msg); - } - _ => self - .p - .line("当前任务进行中,该命令暂不可用(Esc/Ctrl+C 中断,或等待完成)"), - } - return; - } - if name.is_empty() { - return self.start(Job::Task(line)); - } - self.command(&name, arg).await; - if let Some(a) = &self.agent { - note_session(&mut self.reported_session, &self.reporters, a); - } - } - - async fn command(&mut self, name: &str, arg: &str) { - let Some(agent) = self.agent.as_mut() else { - return; - }; - match name { - "quit" | "exit" => self.quit = true, - "help" => { - let daily = agent.daily_mode(); - self.p.line(&format!( - "{HELP_TEXT}{}", - if daily { DAILY_HELP_TEXT } else { "" } - )); - } - "queue" => { - if arg.is_empty() { - self.p.line("用法:/queue <文本>"); - } else { - self.start(Job::Task(arg.to_string())); - } - } - "new" => { - agent.new_session(); - self.p.total_tokens = 0; - self.p.line("已开始新会话"); - } - "compact" => self.start(Job::Compact), - "model" => { - if arg.is_empty() { - let m = agent.client.model_id().to_string(); - self.p.line(&format!("当前模型:{m}")); - } else { - let (model, level) = crate::llm::catalog::split_model_arg(arg); - match agent.set_model(&model) { - Ok(()) => { - if level.is_some() { - agent.set_thinking(level); - } - let m = agent.client.model_id().to_string(); - self.p.line(&format!("已切换到 {m}")); - } - Err(e) => self.p.line(&format!("✗ {e}")), - } - } - } - "fork" => { - if !arg.is_empty() && arg.parse::().is_err() { - return self - .p - .line("用法:/fork [n](n = 保留前 n 轮对话,省略则全部保留)"); - } - let parent = agent.session_id().to_string(); - let dir = agent.session_dir.clone(); - match agent.fork(arg.parse().ok()) { - Ok(path) => match Agent::resume(agent.config.clone(), &path, &dir) { - Ok(a) => { - self.steer = a.steer.clone(); - let id = a.session_id().to_string(); - self.agent = Some(a); - self.p.line(&format!("已从会话 {parent} 分叉,新会话 {id}")); - } - Err(e) => self.p.line(&format!("✗ {e}")), - }, - Err(e) => self.p.line(&format!("✗ {e}")), - } - } - "handoff" => { - if arg.is_empty() { - return self.p.line("用法:/handoff <交接说明>"); - } - let parent = agent.session_id().to_string(); - let prompt = agent.handoff_prompt(arg); - let meta = agent.handoff_meta(); - agent.reset_to(meta); - let id = agent.session_id().to_string(); - self.p - .line(&format!("已交接:新会话 {id},关联原会话 {parent}")); - self.start(Job::Task(prompt)); - } - "plan" => match parse_toggle(arg, agent.plan_mode()) { - None => self.p.line(cmds::PLAN_USAGE), - Some(on) if agent.set_plan_mode(on) => self.p.line(cmds::plan_notice(on)), - Some(on) => self.p.line(&cmds::unchanged_notice("计划模式", on)), - }, - "code-mode" => match parse_toggle(arg, agent.code_mode()) { - None => self.p.line(cmds::CODE_MODE_USAGE), - Some(on) if agent.set_code_mode(on) => self.p.line(cmds::code_mode_notice(on)), - Some(on) => self.p.line(&cmds::unchanged_notice("Code mode ", on)), - }, - "daily-mode" => match parse_toggle(arg, agent.daily_mode()) { - None => self.p.line(cmds::DAILY_USAGE), - Some(on) if agent.set_daily_mode(on) => { - self.p.line(cmds::daily_toggle_notice(on)); - if let Some(n) = cmds::web_search_missing_notice(agent) { - self.p.line(&n); - } - } - Some(on) => self.p.line(&cmds::unchanged_notice("Daily mode ", on)), - }, - "memory" | "learn" if !agent.daily_mode() => { - self.p.line(&cmds::daily_only_notice(&format!("/{name}"))) - } - "memory" => match crate::memory::parse_memory_command(arg) { - Err(()) => self.p.line(crate::memory::MEMORY_USAGE), - Ok(target) => { - let store = agent.memory_store(); - match (target, &store) { - (Some(t), Some(s)) => self.p.line(&format!( - "直接编辑 {},从下一个会话起生效", - s.path(&t).display() - )), - _ => { - let text = - crate::memory::describe(store.as_ref(), agent.memory.as_ref()); - self.p.line(&text); - } - } - } - }, - "learn" => { - let msg = crate::learn::start_review(agent, false); - self.p.line(&msg); - } - "btw" => { - if arg.is_empty() { - return self.p.line("用法:/btw <问题>"); - } - let mut rx = cmds::btw_query(&agent.client, &agent.messages, arg); - self.p.line(&format!("💬 btw · {arg}")); - while let Some(item) = rx.recv().await { - match item { - Ok(t) => { - print!("{t}"); - let _ = std::io::stdout().flush(); - } - Err(e) => { - println!(); - self.p.line(&format!("✗ {e}")); - } - } - } - println!(); - self.p.line("(侧问答案不会进入会话)"); - } - "goal" => match parse_goal_args(arg) { - GoalCommand::Query => match pump::live_goal(agent) { - None => self.p.line("当前没有 goal"), - Some(g) => { - let t = cmds::goal_status_text(g); - self.p.line(&t); - } - }, - GoalCommand::Clear => { - if pump::clear_goal(agent) { - self.p.line("已退出 goal 模式"); - } else { - self.p.line("当前没有 goal"); - } - } - GoalCommand::Set(text) => { - let prompt = pump::set_goal(agent, &text); - self.proposing = true; - self.start(Job::Task(prompt)); - } - }, - "tracing-viewer" => { - let id = agent.session_id().to_string(); - let msg = self.viewer.command(arg, Some(&id)); - self.p.line(&msg); - } - other => self - .p - .line(&format!("未知命令 /{other}(/help 查看可用命令)")), - } + c.quit = true; } -} -impl Repl { fn on_background(&mut self, ev: BackgroundEvent) { - if self.p.json { + if self.conv.sink.json { let v = match &ev { BackgroundEvent::Notice { kind, text } => { json!({"type": "subagent_notice", "kind": kind, "text": text}) @@ -683,57 +325,13 @@ impl Repl { }; println!("{v}"); } else { - match &ev { - BackgroundEvent::Notice { kind, text } if !text.is_empty() => { - let icon = match kind.as_str() { - "result" => "✓", - "alert" => "⚠", - "question" => "?", - _ => "◆", - }; - self.p.line(&format!("{icon} {text}")); - } - BackgroundEvent::PlanProposed(_) => { - if let Some(sup) = self.sup() { - self.p.line("◆ 子任务计划待确认:"); - for l in sup.status_text(None).lines() { - self.p.line(&format!(" {l}")); - } - self.p - .line(" /task approve all 开始 · /task reject all 放弃"); - } - } - _ => {} + for l in self.conv.background_lines(&ev) { + self.conv.sink.line(&l); } } - if matches!(ev, BackgroundEvent::MainWake) { - self.maybe_wake_main(); - } + self.conv.after_background(&ev); self.finish_if_eof(); } - - fn sup(&self) -> Option> { - self.agent - .as_ref() - .and_then(|a| a.supervisor.clone()) - .or_else(|| self.sup_handle.clone()) - } - - /// Start a main-agent turn for subagent notices queued while idle. - fn maybe_wake_main(&mut self) { - if self.busy() || self.proposals.is_some() || self.quit { - return; - } - let Some(text) = self.sup().and_then(|s| s.take_wake_input()) else { - return; - }; - self.p.line("◆ 子代理有新进展,交给主 agent 处理"); - self.start(Job::Task(text)); - } -} - -fn first_line(s: &str) -> &str { - s.lines().next().unwrap_or("") } pub async fn run_repl(agent: Agent, json_mode: bool) -> i32 { @@ -745,54 +343,35 @@ pub async fn run_repl(agent: Agent, json_mode: bool) -> i32 { )); crate::integrations::install_exit_hooks(reporters.clone()); let (tx, mut rx) = mpsc::unbounded_channel(); + let printer = Printer { + json: json_mode, + in_text: false, + total_tokens: agent.session_total_tokens(), + }; let mut repl = Repl { - steer: agent.steer.clone(), - viewer: crate::trace_viewer::Viewer::new( - agent.session_dir.clone(), - Some(agent.workdir.clone()), - ), - agent: Some(agent), - job: None, - cancel: CancellationToken::new(), - tx, - p: Printer { - json: json_mode, - in_text: false, - total_tokens: 0, - }, - proposing: false, - proposals: None, - quit: false, + conv: Conversation::new(agent, printer, tx, reporters.clone()), eof: false, eof_waiting: false, - sup_handle: None, - reporters: reporters.clone(), - outcome: TurnOutcome::default(), - reported_session: String::new(), }; - if let Some(a) = &repl.agent { - note_session(&mut repl.reported_session, &repl.reporters, a); - } let mut bg = crate::background::take_receiver(); - if let Some(a) = &repl.agent { - repl.p.total_tokens = a.session_total_tokens(); - if !json_mode { - println!( - "limbo (headless) · {} · {}", - a.client.model_id(), - a.workdir.display() - ); - println!( - "session {} ({}) · /help 查看命令 · /quit 退出", - a.session_id(), - a.session_path().display() - ); - if let Some(g) = pump::live_goal(a) { - println!("🎯 goal:{}({})", g.text, g.status); - } - if let Some(n) = cmds::web_search_missing_notice(a) { - println!("{n}"); - } + if let Some(a) = &repl.conv.agent + && !json_mode + { + println!( + "limbo (headless) · {} · {}", + a.client.model_id(), + a.workdir.display() + ); + println!( + "session {} ({}) · /help 查看命令 · /quit 退出", + a.session_id(), + a.session_path().display() + ); + if let Some(g) = pump::live_goal(a) { + println!("🎯 goal:{}({})", g.text, g.status); + } + if let Some(n) = cmds::web_search_missing_notice(a) { + println!("{n}"); } } let (line_tx, mut lines) = mpsc::unbounded_channel::>(); @@ -812,32 +391,32 @@ pub async fn run_repl(agent: Agent, json_mode: bool) -> i32 { } } }); - while !repl.quit { + while !repl.conv.quit { tokio::select! { Some(ev) = rx.recv() => { - repl.outcome.observe(&ev); - repl.p.event(&ev); + repl.conv.observe(&ev); + repl.conv.sink.event(&ev); } - done = async { repl.job.as_mut().unwrap().await }, if repl.job.is_some() => { + Some(done) = repl.conv.join(), if repl.conv.busy() => { // Flush events emitted just before the task returned. while let Ok(ev) = rx.try_recv() { - repl.outcome.observe(&ev); - repl.p.event(&ev); + repl.conv.observe(&ev); + repl.conv.sink.event(&ev); } match done { Ok(agent) => repl.job_done(agent), Err(e) => { - repl.p.line(&format!("✗ 内部错误:{e}")); + repl.conv.sink.line(&format!("✗ 内部错误:{e}")); return 1; } } } line = lines.recv(), if !repl.eof => match line.flatten() { - Some(l) => repl.submit(l).await, + Some(l) => repl.conv.submit(l, vec![]).await, None => { repl.eof = true; - if repl.busy() { - repl.p.line("输入已关闭,等待当前任务完成…"); + if repl.conv.busy() { + repl.conv.sink.line("输入已关闭,等待当前任务完成…"); } else { repl.finish_if_eof(); } @@ -845,30 +424,17 @@ pub async fn run_repl(agent: Agent, json_mode: bool) -> i32 { }, Some(ev) = async { bg.as_mut().unwrap().recv().await }, if bg.is_some() => repl.on_background(ev), _ = tokio::signal::ctrl_c() => { - if repl.proposals.take().is_some() { - repl.p.line(cmds::VERIFY_SKIPPED); - } else if repl.busy() { - repl.cancel.cancel(); + if repl.conv.proposals.take().is_some() { + repl.conv.sink.line(cmds::VERIFY_SKIPPED); + } else if repl.conv.busy() { + repl.conv.interrupt(); } else { - repl.quit = true; + repl.conv.quit = true; } } } } - if let Some(job) = repl.job.take() { - repl.cancel.cancel(); - if let Ok(a) = job.await { - repl.agent = Some(a); - } - } - if let Some(a) = repl.agent.as_mut() { - a.checkpoint(); - a.trace.flush(); - } - repl.viewer.stop(); - if let Some(sup) = repl.sup() { - sup.stop(); - } - repl.reporters.release(); + repl.conv.shutdown().await; + reporters.release(); 0 } diff --git a/src/main.rs b/src/main.rs index db6eef2..9334122 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,11 +17,14 @@ mod attachments; mod auth; mod background; mod capability; +mod channels; mod cli_auth; mod commands; mod compaction; mod config; +mod conversation; mod effect; +mod gateway; mod goal; mod headless; mod history; @@ -98,6 +101,16 @@ enum Command { #[command(subcommand)] action: AuthAction, }, + /// Chat gateway: talk to limbo from Telegram / personal WeChat + Gateway { + #[command(subcommand)] + action: Option, + }, + /// Log the gateway's chat channels in and out (telegram | weixin) + Channels { + #[command(subcommand)] + action: ChannelsAction, + }, /// List sessions for the working directory Sessions { /// Show sessions of every directory @@ -106,6 +119,31 @@ enum Command { }, } +#[derive(Subcommand, Debug)] +enum GatewayAction { + /// Run in the foreground (the default) + Run, + /// Install a background service (launchd on macOS, systemd --user on Linux) + Install, + /// Stop and remove the background service + Uninstall, +} + +#[derive(Subcommand, Debug)] +enum ChannelsAction { + /// Connect a channel: telegram (bot token) or weixin (QR code) + Login { + channel: String, + /// Telegram bot token (default: prompt) + #[arg(long)] + token: Option, + }, + /// Forget a channel's credentials + Logout { channel: String }, + /// Show logged-in channels, owners and the service state + Status, +} + #[derive(Subcommand, Debug)] enum AuthAction { /// Store a provider's key (asks interactively when --key is missing) @@ -187,6 +225,33 @@ fn real_main() -> anyhow::Result { AuthAction::Logout { provider } => cli_auth::logout(provider), }); } + Some(Command::Channels { action }) => { + return match action { + ChannelsAction::Login { channel, token } => { + rt.block_on(gateway::login(&config, channel, token.clone())) + } + ChannelsAction::Logout { channel } => gateway::logout(channel), + ChannelsAction::Status => Ok(gateway::status(&config)), + }; + } + Some(Command::Gateway { action }) => { + return match action { + None | Some(GatewayAction::Run) => rt.block_on(gateway::run(config, &session_dir)), + Some(GatewayAction::Install) => { + let mut keep = Vec::new(); + if let Some(c) = &cli.config { + keep.push("--config".to_string()); + keep.push(std::fs::canonicalize(c)?.display().to_string()); + } + if let Some(d) = &cli.session_dir { + keep.push("--session-dir".to_string()); + keep.push(std::fs::canonicalize(d)?.display().to_string()); + } + gateway::service::install(&keep) + } + Some(GatewayAction::Uninstall) => gateway::service::uninstall(), + }; + } _ => {} } let workdir = match &cli.workdir { @@ -207,7 +272,10 @@ fn real_main() -> anyhow::Result { }; match cli.command { - Some(Command::Models { .. }) | Some(Command::Auth { .. }) => unreachable!(), + Some(Command::Models { .. }) + | Some(Command::Auth { .. }) + | Some(Command::Channels { .. }) + | Some(Command::Gateway { .. }) => unreachable!(), Some(Command::Sessions { all }) => { for m in session::list_sessions(&session_dir, (!all).then_some(workdir.as_path())) { println!( diff --git a/src/subagents/supervisor.rs b/src/subagents/supervisor.rs index 20a04ff..ae4ac89 100644 --- a/src/subagents/supervisor.rs +++ b/src/subagents/supervisor.rs @@ -467,7 +467,10 @@ impl Supervisor { (r, changed) }; for (id, status, title) in changed { - background::emit(BackgroundEvent::TaskChanged { id, status, title }); + background::emit_for( + &self.main_id, + BackgroundEvent::TaskChanged { id, status, title }, + ); } r } @@ -1136,10 +1139,13 @@ impl Supervisor { headline.into() }, }; - background::emit(BackgroundEvent::Notice { - kind: kind.into(), - text: n.headline.clone(), - }); + background::emit_for( + &self.main_id, + BackgroundEvent::Notice { + kind: kind.into(), + text: n.headline.clone(), + }, + ); if DELIVERED.contains(&kind) { self.pending.lock().unwrap().push(n); } @@ -1345,7 +1351,7 @@ impl Supervisor { .unwrap() .retain(|n| !ids.contains(&n.id)); if wake { - background::emit(BackgroundEvent::MainWake); + background::emit_for(&self.main_id, BackgroundEvent::MainWake); } } @@ -1504,7 +1510,7 @@ impl Supervisor { .join("\n"); let ids: Vec = created.iter().map(|t| t.id.clone()).collect(); if status == "proposed" { - background::emit(BackgroundEvent::PlanProposed(ids.clone())); + background::emit_for(&self.main_id, BackgroundEvent::PlanProposed(ids.clone())); return PlanResult { message: format!( "计划已提交给用户确认:\n{listing}\n用户确认前任务不会开始。现在结束本轮,简短告诉用户计划的要点即可。" @@ -1890,20 +1896,26 @@ impl Supervisor { && t.created_by != "user" && let Some(n) = self.result_notice(&t) { - background::emit(BackgroundEvent::Notice { - kind: n.kind.clone(), - text: n.headline.clone(), - }); + background::emit_for( + &self.main_id, + BackgroundEvent::Notice { + kind: n.kind.clone(), + text: n.headline.clone(), + }, + ); self.pending.lock().unwrap().push(n); } else if let Some(t) = self.board.lock().unwrap().get(id).cloned() && t.created_by == "user" && let Some(n) = self.result_notice(&t) { // User-started tasks (e.g. /learn) report to the user only. - background::emit(BackgroundEvent::Notice { - kind: n.kind, - text: n.headline, - }); + background::emit_for( + &self.main_id, + BackgroundEvent::Notice { + kind: n.kind, + text: n.headline, + }, + ); } "已交付,任务结束。".into() } @@ -1923,10 +1935,13 @@ impl Supervisor { }); if let Some((t, q)) = q { let n = Self::question_notice(&t, &q); - background::emit(BackgroundEvent::Notice { - kind: "question".into(), - text: n.headline.clone(), - }); + background::emit_for( + &self.main_id, + BackgroundEvent::Notice { + kind: "question".into(), + text: n.headline.clone(), + }, + ); self.pending.lock().unwrap().push(n); } "问题已提交,你会在得到回答后继续。现在结束本轮。".into()