wechatbridge-cli 1.8.0__tar.gz → 1.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. wechatbridge_cli-1.10.0/PKG-INFO +421 -0
  2. wechatbridge_cli-1.10.0/README.md +389 -0
  3. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/pyproject.toml +2 -2
  4. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_codex.py +15 -6
  5. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_hardening.py +2082 -62
  6. wechatbridge_cli-1.10.0/tests/test_pig.py +1941 -0
  7. wechatbridge_cli-1.10.0/wechatbridge/__init__.py +2 -0
  8. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/agy.py +216 -66
  9. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/codex.py +72 -93
  10. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/config.py +86 -2
  11. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/dsh.py +56 -50
  12. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/grok.py +58 -10
  13. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/main.py +280 -20
  14. wechatbridge_cli-1.10.0/wechatbridge/pig.py +1557 -0
  15. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/price.py +22 -1
  16. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/runner_common.py +1092 -25
  17. wechatbridge_cli-1.10.0/wechatbridge_cli.egg-info/PKG-INFO +421 -0
  18. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge_cli.egg-info/SOURCES.txt +2 -0
  19. wechatbridge_cli-1.8.0/PKG-INFO +0 -365
  20. wechatbridge_cli-1.8.0/README.md +0 -333
  21. wechatbridge_cli-1.8.0/wechatbridge/__init__.py +0 -2
  22. wechatbridge_cli-1.8.0/wechatbridge_cli.egg-info/PKG-INFO +0 -365
  23. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/LICENSE +0 -0
  24. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/setup.cfg +0 -0
  25. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_codex_auth.py +0 -0
  26. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_dsh.py +0 -0
  27. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_grok_auth.py +0 -0
  28. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/tests/test_price.py +0 -0
  29. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/__main__.py +0 -0
  30. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/ilink.py +0 -0
  31. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge/update_check.py +0 -0
  32. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge_cli.egg-info/dependency_links.txt +0 -0
  33. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge_cli.egg-info/entry_points.txt +0 -0
  34. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge_cli.egg-info/requires.txt +0 -0
  35. {wechatbridge_cli-1.8.0 → wechatbridge_cli-1.10.0}/wechatbridge_cli.egg-info/top_level.txt +0 -0
@@ -0,0 +1,421 @@
1
+ Metadata-Version: 2.4
2
+ Name: wechatbridge-cli
3
+ Version: 1.10.0
4
+ Summary: Bridge WeChat messages to agy, Grok Build, Codex, dsh, or pig CLIs — text/image/file/voice in, CLI replies and generated files back.
5
+ Author: WeChatBridge contributors
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/dorokuma/wechatbridge
8
+ Project-URL: Repository, https://github.com/dorokuma/wechatbridge
9
+ Project-URL: Issues, https://github.com/dorokuma/wechatbridge/issues
10
+ Project-URL: Changelog, https://github.com/dorokuma/wechatbridge/blob/main/CHANGELOG.md
11
+ Keywords: wechat,bot,bridge,agy,grok,codex,pig
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Natural Language :: Chinese (Simplified)
17
+ Classifier: Operating System :: POSIX :: Linux
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Topic :: Communications :: Chat
23
+ Requires-Python: >=3.10
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: httpx
27
+ Requires-Dist: qrcode
28
+ Requires-Dist: Pillow
29
+ Requires-Dist: cryptography
30
+ Requires-Dist: PyYAML
31
+ Dynamic: license-file
32
+
33
+ # WeChatBridge
34
+
35
+ [English](README.md) | [简体中文](README.zh-CN.md)
36
+
37
+ ![license](https://img.shields.io/badge/license-MIT-blue.svg)
38
+ ![python](https://img.shields.io/badge/python-3.10+-blue.svg)
39
+
40
+ WeChatBridge puts your terminal coding agent in your pocket. Bring your own CLI — agy, grok, codex, or dsh — and talk to it from WeChat: text, images, files, and voice (transcribed to text). You get replies back, and generated files can come back to you over the WeChat CDN. Each user can switch backends with `/backend`, no restart needed.
41
+
42
+ ```
43
+ WeChat (phone) ⇄ iLink bot API ⇄ WeChatBridge ⇄ agy / grok / codex / dsh CLI
44
+ (the bridge) (runs tools)
45
+ ```
46
+
47
+ WeChatBridge runs in the background and keeps asking WeChat for new messages. When you send something that needs the AI, it starts the CLI once, waits for the answer, then closes it again.
48
+
49
+ Commands like `/help`, `/backend`, or `/persona` are answered by the bridge itself and never start a CLI. A generated file only comes back to WeChat if the bridge can see it under your allowed session paths.
50
+
51
+ ## Quick start
52
+
53
+ Five steps, about 10 minutes from zero to your first reply in WeChat.
54
+
55
+ 1. **Install one CLI backend and sign in.** I recommend [agy](https://antigravity.google/docs/cli) (Google Antigravity), since that is the backend I run daily. [grok](https://docs.x.ai/build/overview), [codex](https://developers.openai.com/codex/cli), and [dsh](https://github.com/deepseek-ai/deepseek-harness/blob/master/apps/cli/README.md) also work, but check the status notes under **CLI Backends** before you pick one. After installing, log in once in that CLI (for example `grok login`, `codex login`, or `dsh login`).
56
+
57
+ 2. **Install WeChatBridge:**
58
+
59
+ ```bash
60
+ pipx install wechatbridge-cli
61
+ wechatbridge --version
62
+ ```
63
+
64
+ 3. **Write a minimal config:**
65
+
66
+ ```bash
67
+ mkdir -p ~/.config/wechatbridge
68
+ curl -o ~/.config/wechatbridge/.env https://raw.githubusercontent.com/dorokuma/wechatbridge/main/deploy/wechatbridge.env.example
69
+ ```
70
+
71
+ Then edit `~/.config/wechatbridge/.env` and, for the first run, use the open mode:
72
+
73
+ ```bash
74
+ WECHATBRIDGE_ALLOWED_SENDERS=*
75
+ ```
76
+
77
+ Why `*`: the bridge fails closed. With both `WECHATBRIDGE_ALLOWED_SENDERS` and `WECHATBRIDGE_ADMINS` empty it denies everyone, and you don't know your own `wxid` yet.
78
+
79
+ If the backend you installed in step 1 is not `agy`, also set the global default backend in `.env`; `agy` is the default and can be omitted:
80
+
81
+ ```bash
82
+ WECHATBRIDGE_BACKEND=grok # or codex / dsh
83
+ ```
84
+
85
+ 4. **Start it and scan the QR code to bind.** The ClawBot / iLink channel is Tencent's official, free personal-account bot channel: no application, review, qualification, or payment required — and you don't need to install anything extra: the bridge handles binding itself. Run `wechatbridge`; it prints a QR code in the terminal, and you scan it with WeChat on your phone. The bridge waits for a scan with a default poll timeout of 180 seconds; on timeout it automatically fetches a fresh QR code and keeps waiting — the process does not exit and does not need a restart. Only if you stop it yourself do you rerun `wechatbridge`.
86
+
87
+ 5. **Get your own wxid and tighten the whitelist.** In WeChat, send the bot any message, then find that message's sender ID in the bridge log. It appears as `from=<id>`. Put that ID into `WECHATBRIDGE_ALLOWED_SENDERS` in place of `*` and restart. `*` is for first-run debugging only; tighten it for long-term use.
88
+
89
+ See **Configure** below for every variable, and **Deploy** for running it as a service.
90
+
91
+ ## Features
92
+
93
+ - You can send text, images, files, and voice (transcribed by WeChat's servers) to the active backend: `agy`, `grok`, `codex`, or `dsh`. See **CLI Backends** for how mature each one is.
94
+ - Files the CLI produces inside your allowed directory can come back to WeChat, up to a size cap. Not every file it touches gets sent.
95
+ - Each WeChat user gets their own workspace. Model, effort, and mode settings are remembered per backend.
96
+ - You can switch backends at runtime with `/backend agy`, `/backend grok`, `/backend codex`, or `/backend dsh`. Switching clears that backend's continuation state (the agy/grok continuation flag and the codex `thread_id`/resume state), so the next CLI turn starts a fresh session. History files on disk are not wiped right away.
97
+ - Slash commands for model, session reset, persona, and more (see below).
98
+ - A dangerous-prompt gate checks messages against a keyword list of destructive patterns and asks for confirmation first.
99
+ - Sender whitelist via `WECHATBRIDGE_ALLOWED_SENDERS`: non-empty means listed users only, `*` means everyone, empty means admins only (nobody if there are no admins either).
100
+ - `/mcp` returns short usage text. `/agent` rewrites your request into a natural-language subagent prompt for the CLI; it is not a native MCP bridge.
101
+ - Media go over the WeChat CDN with AES-128-ECB encrypt/decrypt.
102
+ - Multi-instance: one codebase, set `WECHATBRIDGE_INSTANCE` per process, and state / session / QR paths derive from it.
103
+ - Deploy templates for systemd (Linux), launchd (macOS), and Task Scheduler (Windows).
104
+
105
+ ## Platform Support
106
+
107
+ - **Linux**: primary (systemd unit included)
108
+ - **macOS**: supported (launchd plist included)
109
+ - **Windows**: supported (Task Scheduler guide included)
110
+
111
+ Default data paths expand from `~` (for example `~/.local/share/wechatbridge/<instance>/`).
112
+
113
+ ## CLI Backends
114
+
115
+ agy is the backend I use every day. The other three are implemented and tested, but I have not run them in production for long. Verify a non-agy backend in your own setup before you rely on it.
116
+
117
+ - **agy** (default): Google Antigravity CLI. **Status: this is the backend the author uses daily.**
118
+ - **grok**: xAI Grok Build CLI. **Status: implemented and tested, but not battle-tested by the author — please verify it in your own setup before relying on it.**
119
+ - **codex**: OpenAI Codex CLI. **Status: implemented and tested, but not battle-tested by the author — please verify it in your own setup before relying on it.**
120
+ - **dsh**: DeepSeek Harness CLI (one-shot `headless` profile). **Status: contributed by [@KOP-XIAO](https://github.com/KOP-XIAO) in #3 and #4, and verified on real hardware by the contributor (macOS, including cross-turn memory). The repo author has not run dsh in long-term production use — verify it in your own setup before relying on it.**
121
+
122
+ You switch per user with `/backend agy`, `/backend grok`, `/backend codex`, or `/backend dsh`. Each backend keeps its own model, effort, mode, and persona files. `WECHATBRIDGE_BACKEND` sets the global default.
123
+
124
+ ### dsh backend notes
125
+
126
+ - **Status: contributed by [@KOP-XIAO](https://github.com/KOP-XIAO) in #3 and #4, and verified on real hardware by the contributor (macOS, including cross-turn memory). The repo author has not run dsh in long-term production use — verify it in your own setup before relying on it.**
127
+ - **What dsh is:** the DeepSeek Harness CLI (`dsh`), running one-shot tasks through its `headless` profile.
128
+ - **Enable or switch:** set `WECHATBRIDGE_BACKEND=dsh` in `.env`, or send `/backend dsh` in WeChat.
129
+ - **PyYAML dependency:** dsh needs `PyYAML` to parse profile plugin configurations. Without it, `/backend dsh` shows a notice and keeps your current backend. Install it with `pip install PyYAML` or `pipx inject wechatbridge-cli PyYAML`.
130
+ - **Window memory (default):** `headless` starts a fresh session (`session-<uuid>`) on every call, so the bridge keeps the conversation memory instead. Successful turns are appended to `dsh_memory.jsonl`; on each call the bridge reads the most recent turns (10 pairs by default, capped at `WECHATBRIDGE_DSH_MEMORY_CHARS` chars) and injects them into the prompt as context — that cap limits the injected read window, not the file size on disk. `/clear` or `/new` wipes this memory to start fresh.
131
+ - **Persistent resume mode:** set `WECHATBRIDGE_DSH_RESUME=true` for real persistent sessions, codex-style. This needs the `dsh-bridge-runner` plugin (`dsh_bridge_runner.py`, which reads `DSH_BRIDGE_SESSION_ID` and `DSH_BRIDGE_TASK` from the environment) mounted in your headless profile bundle. Context then accumulates natively across turns, with no windowing. `/clear` or `/new` deletes the stored session ID to start a fresh session. Window memory injection is skipped while resume mode is on.
132
+ - **Workspace and state isolation:** in the default windowed mode, dsh runs as `dsh --profile headless -- <prompt>` with `cwd` set to your per-user session directory (`WECHATBRIDGE_SESSION_DIR/<user_id>`). In resume mode there is no `-- <prompt>` argv: the task is passed via the `DSH_BRIDGE_TASK` environment variable (and the profile stays configurable via `DSH_PROFILE`). Bridge-private state (`dsh_memory.jsonl` and the persistent session ID) lives under `WECHATBRIDGE_DSH_STATE_DIR` (`~/.local/share/wechatbridge/<instance>/dsh_state/`), outside the child's session tree. That blocks one-level `../` traversal into private state.
133
+ - **Who can hurt whom:** every dsh child runs as the same Unix user as the bridge, with no container in between. One-level `../` cannot reach private state, but two-level traversal (`../../dsh_state/<user_id>`) still can on the filesystem, and that is accepted on purpose. Only let people you trust use this.
134
+ - **Attachments:** images and files are merged into the prompt text as `@/absolute/path` mentions. I checked dsh v0.1.1-rc.2: `headless` does not open or inline `@`-mentioned files itself. It passes your prompt text straight to the model. The model gets system guidance about @-paths and calls its `read` tool when it needs a file. The bridge rewrites out-of-bounds mentions (`@/abs`, `@~/x`, `@file://`) to `[blocked-path]`. That filter only edits prompt text. It is not a security boundary.
135
+ - **Auth and profiles are machine-wide.** Resolution order: `WECHATBRIDGE_DSH_HOME` > `WECHATBRIDGE_HOST_HOME/.dsh` > `~/.dsh`, the same host-fallback model as grok and codex. The child's `HOME` points at the per-user session dir, so the bridge always passes `DSH_HOME` explicitly. Set `WECHATBRIDGE_DSH_HOME` for a dedicated service home with automatic session retention cleanup. Leave it unset and it falls back to `$WECHATBRIDGE_HOST_HOME/.dsh` (if set) or `~/.dsh`, with no automatic cleanup; the operator manages sessions. If the bridge runs as a dedicated system user (for example `wechatbridge`), set `WECHATBRIDGE_HOST_HOME` to the operator's home directory. That way CLI credentials and profiles are found. `DSH_BIN_PATH`, `DSH_PROFILE`, and `DSH_TIMEOUT` are all configurable.
136
+ - **Testing caveat:** like codex, the tests ran against a fake CLI; on top of that, the contributor verified the real `dsh` CLI on their own hardware (macOS, including cross-turn memory resume). The author still has not run `dsh` in long-term production use — verify it in your own setup before relying on it.
137
+ - **Plugin files:** resume mode needs the `dsh-bridge-runner` plugin mounted in your own dsh profile bundle. This repository does not ship plugin files.
138
+
139
+ ### Grok backend notes
140
+
141
+ - **Status: implemented and tested, but not battle-tested by the author — please verify it in your own setup before relying on it.**
142
+ - **Isolation:** each WeChat user runs with `HOME` pointed at their own session directory. Conversation state stays there; login is machine-wide.
143
+ - **Auth:** the session links to the host `~/.grok/auth.json` (copied as a fallback), so it reuses your host `grok login`. The grok CLI rewrites `auth.json` with a temp file plus rename, which turns the session symlink into a regular file. The bridge copies such session files back to the host atomically and re-links them, so refreshed tokens never get overwritten by the revoked host copy. You can also set `XAI_API_KEY` in the bridge process environment; the grok child gets that key after env sanitizing. Being signed in to a grok-remote TUI session is not the same as the host CLI login. No key or token values are stored in this repository.
144
+
145
+ ### Codex backend notes
146
+
147
+ - **Status: implemented and tested, but not battle-tested by the author — please verify it in your own setup before relying on it.**
148
+ - Each turn runs `codex exec --json`. Continuation uses `codex exec resume <thread_id> <prompt>`, with the thread id persisted per user.
149
+ - **Isolation:** each WeChat user runs with `HOME` pointed at their own per-user session directory (`session_dir`), and `CODEX_HOME` pointed at `session_dir/.codex`. Sessions, logs, and caches never cross users.
150
+ - **Auth:** the per-user session links to the host `~/.codex/auth.json` (copied as a fallback), so it reuses your host `codex login`. The codex CLI rewrites `auth.json` with a temp file plus rename, which turns the session symlink into a regular file. The bridge copies such session files back to the host atomically and re-links them, so refreshed tokens never get overwritten by the revoked host copy. You can also set `CODEX_API_KEY` in the bridge process environment to authenticate. No key or token values are stored in this repository.
151
+ - **Testing caveat:** I do not have a real Codex subscription or CLI to test against. This backend was built from source research, a JSONL fixture, and tests against a fake CLI, which pass. Final acceptance needs a real user running it against the actual Codex CLI.
152
+
153
+ ## Prerequisites
154
+
155
+ - At least one CLI installed and signed in:
156
+ - `agy` on `PATH`, or set `AGY_BIN_PATH`
157
+ - and/or `grok` on `PATH`, or set `GROK_BIN_PATH`
158
+ - and/or `codex` on `PATH`, or set `CODEX_BIN_PATH`
159
+ - and/or `dsh` on `PATH`, or set `DSH_BIN_PATH` (DeepSeek Harness; run `dsh login` first)
160
+ - Antigravity is Google's terminal coding agent (the successor to Gemini CLI). Grok Build is xAI's equivalent, Codex is OpenAI's, and dsh is DeepSeek Harness' CLI.
161
+ - A WeChat account with a [ClawBot / iLink](https://ilinkai.weixin.qq.com) bot (QR bind on first run)
162
+ - Python 3.10+
163
+
164
+ ## Install
165
+
166
+ The recommended way is [pipx](https://pypa.github.io/pipx/) (Python >= 3.10 required):
167
+
168
+ ```bash
169
+ pipx install wechatbridge-cli
170
+ ```
171
+
172
+ After installation, verify:
173
+
174
+ ```bash
175
+ wechatbridge --version
176
+ ```
177
+
178
+ ### Install pipx
179
+
180
+ **Debian / Ubuntu:**
181
+
182
+ ```bash
183
+ sudo apt install pipx
184
+ ```
185
+
186
+ **Other systems (or to get the latest version):**
187
+
188
+ ```bash
189
+ python3 -m pip install --user pipx && python3 -m pipx ensurepath
190
+ ```
191
+
192
+ Then start a new shell or re-source your shell config so `pipx` is on `PATH`.
193
+
194
+ ### Developers
195
+
196
+ If you want to work on the source:
197
+
198
+ ```bash
199
+ git clone https://github.com/dorokuma/wechatbridge.git
200
+ cd wechatbridge
201
+ pip install -e .
202
+ ```
203
+
204
+ ## Configure
205
+
206
+ WeChatBridge loads config from the first location it finds:
207
+
208
+ 1. `$WECHATBRIDGE_ENV_FILE`, an explicit path
209
+ 2. `$XDG_CONFIG_HOME/wechatbridge/<instance>.env` (defaults to `~/.config/wechatbridge/<instance>.env`)
210
+ 3. `$XDG_CONFIG_HOME/wechatbridge/.env` (defaults to `~/.config/wechatbridge/.env`)
211
+ 4. `.env` in the repository root: **deprecated** (prints a warning on startup)
212
+
213
+ The instance name defaults to `default`; override it with `WECHATBRIDGE_INSTANCE`.
214
+
215
+ Get the example config:
216
+
217
+ ```bash
218
+ mkdir -p ~/.config/wechatbridge
219
+ curl -o ~/.config/wechatbridge/.env https://raw.githubusercontent.com/dorokuma/wechatbridge/main/deploy/wechatbridge.env.example
220
+ ```
221
+
222
+ Then edit `~/.config/wechatbridge/.env` with your settings. **First install:** if you leave both `WECHATBRIDGE_ALLOWED_SENDERS` and `WECHATBRIDGE_ADMINS` empty, the bridge denies every user (fail-closed). You cannot know your own `wxid` before the first run, so start with `WECHATBRIDGE_ALLOWED_SENDERS=*`. Then send the bot a message, read your sender ID (`from=<id>`) in the bridge log, and replace `*` with that wxid. Optionally set `WECHATBRIDGE_ADMINS=<your wxid>` too, and restart.
223
+
224
+ Key variables (all have defaults):
225
+
226
+ | Variable | Default | Purpose |
227
+ |---|---|---|
228
+ | `AGY_BIN_PATH` | `agy` | path to the agy binary |
229
+ | `GROK_BIN_PATH` | `grok` | path to the grok binary |
230
+ | `CODEX_BIN_PATH` | `codex` | path to the codex binary |
231
+ | `DSH_BIN_PATH` | `dsh` | path to the dsh binary |
232
+ | `DSH_PROFILE` | `headless` | dsh profile booted for one-shot tasks |
233
+ | `DSH_TIMEOUT` | `600` | dsh CLI run timeout in seconds |
234
+ | `WECHATBRIDGE_DSH_MEMORY_TURNS` | `10` | dsh backend: recent user+assistant turns injected as context |
235
+ | `WECHATBRIDGE_DSH_MEMORY_CHARS` | `6000` | dsh backend: max chars of injected memory context |
236
+ | `WECHATBRIDGE_DSH_RESUME` | `false` | dsh backend: true persistent-session mode, one dsh session per user resumed on every message (codex-style); requires the dsh-bridge-runner plugin in the headless profile; skips windowed memory when enabled |
237
+ | `WECHATBRIDGE_DSH_HOME` | _empty_ | explicit `DSH_HOME` passed to the dsh child. Explicitly set = dedicated home + auto session cleanup; unset = fallback to `WECHATBRIDGE_HOST_HOME/.dsh` or `~/.dsh` without auto cleanup (precedence: `WECHATBRIDGE_DSH_HOME` > `WECHATBRIDGE_HOST_HOME/.dsh` > `~/.dsh`) |
238
+ | `WECHATBRIDGE_HOST_HOME` | _empty_ | host home directory override for service user deployments; used as fallback base for grok (`~/.grok`), codex (`~/.codex`), and dsh (`~/.dsh`) |
239
+ | `WECHATBRIDGE_DSH_STATE_DIR` | _derived_ | bridge-private dsh state directory (memory JSONL and persistent session IDs; defaults to `~/.local/share/wechatbridge/<instance>/dsh_state`) |
240
+ | `WECHATBRIDGE_BACKEND` | `agy` | global default backend (`agy` / `grok` / `codex` / `dsh`; overridable per user via `/backend`) |
241
+ | `WECHATBRIDGE_INSTANCE` | `default` | instance name; state / session / QR paths derive from it |
242
+ | `WECHATBRIDGE_ALLOWED_SENDERS` | _empty_ | comma-separated WeChat IDs; `*` = allow all; empty = only `WECHATBRIDGE_ADMINS` users (deny all if that is empty too) |
243
+ | `AGY_TIMEOUT` | `600` | CLI run timeout in seconds (agy / grok / codex backends) |
244
+ | `WECHATBRIDGE_MAX_OUTBOUND_BYTES` | `104857600` | max file size sent back to WeChat (100 MB) |
245
+ | `WECHATBRIDGE_MAX_INBOUND_BYTES` | `20971520` | max inbound image/file after download (20 MB) |
246
+ | `WECHATBRIDGE_MAX_CONCURRENT` | `4` | global concurrent process slots; same user serial (queue does not hold a slot); extras get a busy reply (on small RAM hosts, lower this to avoid concurrent CLI memory spikes; see [Memory limits](#memory-limits)) |
247
+ | `WECHATBRIDGE_QUEUE_NOTICE` | `true` | one-shot "queued" notice when the user's previous message is still running; set `0`/`false`/`no` to disable |
248
+ | `WECHATBRIDGE_PROGRESS_NOTICE` | `true` | one-shot "still working" notice after a message runs past `WECHATBRIDGE_PROGRESS_NOTICE_DELAY`; set `0`/`false`/`no` to disable |
249
+ | `WECHATBRIDGE_PROGRESS_NOTICE_DELAY` | `30` | seconds before the progress notice fires |
250
+ | `WECHATBRIDGE_CONFIRM_TOKEN` | `y` | reply this token to approve a gated dangerous prompt |
251
+ | `WECHATBRIDGE_ENABLE_MCP` | `true` | enable the `/mcp` help text command |
252
+ | `WECHATBRIDGE_ENABLE_SUBAGENT` | `true` | enable the `/agent` prompt-rewrite command |
253
+ | `WECHATBRIDGE_ADMINS` | _empty_ | comma-separated wxid list; admins receive WeChat notification when a new version is detected |
254
+ | `WECHATBRIDGE_UPDATE_CHECK` | `true` | check PyPI for new versions on startup and every 24h; failures are silent |
255
+ | `WECHATBRIDGE_UPDATE_CHECK_INTERVAL` | `86400` | update check interval in seconds |
256
+
257
+ Full list: [`deploy/wechatbridge.env.example`](deploy/wechatbridge.env.example).
258
+
259
+ > **Why the new config location?** With pipx the package is installed globally, so a `.env` next to the source no longer makes sense. The XDG base directory layout keeps your config separate and instance-aware.
260
+
261
+ ## Run
262
+
263
+ ```bash
264
+ wechatbridge
265
+ ```
266
+
267
+ On first run the bridge prints a QR code (and saves a PNG under the instance data dir). Scan it with WeChat to bind, then it starts polling for messages.
268
+
269
+ ## Upgrading
270
+
271
+ ```bash
272
+ pipx upgrade wechatbridge-cli
273
+ sudo systemctl restart wechatbridge
274
+ ```
275
+
276
+ Or use the upgrade script. No clone needed; fetch it with curl:
277
+
278
+ ```bash
279
+ curl -fsSL https://raw.githubusercontent.com/dorokuma/wechatbridge/main/deploy/update.sh | sudo bash
280
+ ```
281
+
282
+ The script upgrades the pipx install and restarts the service. If the service runs as a dedicated system user (for example `wechatbridge`), running it as root automatically runs pipx as that user. Override with `WECHATBRIDGE_USER=<user>`.
283
+
284
+ Your data lives under `~/.local/share/wechatbridge/<instance>/` (sessions, SQLite history, QR codes, login state), and the upgrade does not touch it. Your bots stay logged in and conversations survive.
285
+
286
+ Before a **major** or **minor** upgrade (for example 1.2 → 1.3), check [`CHANGELOG.md`](CHANGELOG.md) for breaking changes and migration steps.
287
+
288
+ > **Before upgrading to 1.8.0:** set `WECHATBRIDGE_ADMINS` or `WECHATBRIDGE_ALLOWED_SENDERS` in your `.env`. Since 1.8.0 the whitelist denies everyone when both are empty. Set `WECHATBRIDGE_ALLOWED_SENDERS=*` if you want to keep allowing all users.
289
+
290
+ ## Deploy
291
+
292
+ ### Linux (systemd)
293
+
294
+ First, install the bridge under the `wechatbridge` system user:
295
+
296
+ ```bash
297
+ sudo -u wechatbridge pipx install wechatbridge-cli
298
+ ```
299
+
300
+ Then deploy the service unit:
301
+
302
+ ```bash
303
+ sudo cp deploy/wechatbridge.service /etc/systemd/system/
304
+ sudo systemctl enable --now wechatbridge
305
+ ```
306
+
307
+ **Multi-instance:** copy the template `deploy/wechatbridge@.service` and enable instances:
308
+
309
+ ```bash
310
+ sudo cp deploy/wechatbridge@.service /etc/systemd/system/
311
+ sudo systemctl enable --now wechatbridge@bot2
312
+ sudo systemctl enable --now wechatbridge@bot3
313
+ ```
314
+
315
+ Each instance reads its own config file (`~/.config/wechatbridge/bot2.env`) and keeps state under its own data directory (`~/.local/share/wechatbridge/bot2/`).
316
+
317
+ #### Memory limits
318
+
319
+ The multi-instance template (`deploy/wechatbridge@.service`) sets cgroup memory limits:
320
+
321
+ ```ini
322
+ MemoryAccounting=yes
323
+ MemoryHigh=450M
324
+ MemoryMax=512M
325
+ ```
326
+
327
+ - **Why these numbers:** backend CLI children (like `agy`) can reach ~276 MB+ RSS in a single turn. Image recognition or long sessions spike even higher. On a ~1 GB RAM host, a hard cap of 300 MB causes heavy page thrashing and deep swap latency. It also breaks streaming (`subscriber fell behind updates`). `450M` (`MemoryHigh`) reclaims memory gently; `512M` (`MemoryMax`) is the hard ceiling that prevents a full-host OOM.
328
+ - **Override per instance with a drop-in**, without editing the base template:
329
+ ```bash
330
+ sudo mkdir -p /etc/systemd/system/wechatbridge@bot2.service.d/
331
+ cat << 'EOF' | sudo tee /etc/systemd/system/wechatbridge@bot2.service.d/memory.conf
332
+ [Service]
333
+ # for hosts with >= 2 GB RAM
334
+ MemoryHigh=600M
335
+ MemoryMax=768M
336
+ EOF
337
+ sudo systemctl daemon-reload
338
+ sudo systemctl restart wechatbridge@bot2
339
+ ```
340
+ Check the active limits:
341
+ ```bash
342
+ systemctl show wechatbridge@bot2 -p MemoryHigh,MemoryMax
343
+ ```
344
+ - **Sizing:** the defaults are tuned for small hosts (~1 GB RAM). On bigger hosts you can relax them. Each instance runs in its own service cgroup, so budget for the worst case of N × MemoryMax across all running instances.
345
+ - **Concurrency interplay:** several heavy turns at once (for example multiple users sending images) multiply backend CLI memory use. On small hosts, lower `WECHATBRIDGE_MAX_CONCURRENT` (to `2` or `1`) before you raise the memory caps.
346
+ - **Swap:** do not tighten `MemorySwapMax`. Swap headroom is a safety buffer: it slows the process under pressure instead of letting the cgroup OOM-kill it right away.
347
+ - **Single-instance deployments:** `deploy/wechatbridge.service` ships the same built-in limits (`MemoryHigh=450M` and `MemoryMax=512M`). Adjust them with a drop-in under `/etc/systemd/system/wechatbridge.service.d/`, and use `wechatbridge` as the unit name in the `systemctl` commands above. The limits take effect when the unit file is installed or re-installed; `update.sh` upgrades do not touch unit files.
348
+ - **Platform scope:** these cgroup limits only exist on Linux systemd. The macOS (launchd) and Windows (Task Scheduler) paths have no equivalent per-instance cap.
349
+
350
+ ### macOS (launchd)
351
+
352
+ ```bash
353
+ cp deploy/wechatbridge.plist ~/Library/LaunchAgents/com.wechatbridge.plist
354
+ # edit WorkingDirectory and ProgramArguments in the plist
355
+ launchctl load ~/Library/LaunchAgents/com.wechatbridge.plist
356
+ ```
357
+
358
+ ### Windows (Task Scheduler)
359
+
360
+ See [`deploy/wechatbridge-windows.md`](deploy/wechatbridge-windows.md).
361
+
362
+ ## Slash commands
363
+
364
+ | Command | Action |
365
+ |---|---|
366
+ | `/help` | list supported commands for the active backend |
367
+ | `/backend <agy\|grok\|codex\|dsh>` | switch CLI backend for this WeChat user (on a real change it clears that backend's continuation state: agy/grok flag, codex `thread_id`/resume, or dsh memory/session ID, so the next turn starts a fresh session; history files may remain until retention cleanup) |
368
+ | `/clear` or `/new` | start a new conversation (agy/grok: drops continue flag; codex: clears thread ID; dsh: clears window memory or persistent session ID) |
369
+ | `/model <name>` | set model (all backends validate against a live list: agy/grok via CLI `models`; codex via `codex debug models` then `--bundled`; unknown names or list-fetch failures are refused and nothing is written; see `/models`) |
370
+ | `/models` | list models (agy/grok/codex all query the live CLI; codex uses `debug models` and falls back to a built-in reference note only if the live list cannot be fetched) |
371
+ | `/fast` | fast-mode toggle: low reasoning effort (accepts `on`/`off`; a second bare `/fast` turns it off) |
372
+ | `/planning` | planning-mode toggle (accepts `on`/`off`; a second bare `/planning` turns it off) |
373
+ | `/add-dir <path>` | **agy/codex:** pass `--add-dir` on later runs if path is allowed. **grok:** recorded only; not passed to the CLI yet. No-arg shows the current list, `/add-dir clear` empties it (dsh unsupported) |
374
+ | `/agents` | list agents via the active CLI |
375
+ | `/persona <text>` | set persona (`show` / `clear` / `reset` subcommands) |
376
+ | `/price <link/token/keyword>` | JD/Taobao coupon & price lookup (requires `JDUNION_*` or `DATAOKE_*` keys) |
377
+ | `/version` | show current version, instance name, and backend; if a newer version is available, show upgrade hint |
378
+ | `/mcp` | short MCP **usage hint** text (can disable with `WECHATBRIDGE_ENABLE_MCP`) |
379
+ | `/agent <name> <task>` | craft a "invoke subagent …" prompt and run the CLI (can disable with `WECHATBRIDGE_ENABLE_SUBAGENT`) |
380
+
381
+ Other `/…` commands are either rejected (for example `/exit`), reported as unsupported on WeChat (TUI-only panels), or passed through to the active CLI.
382
+
383
+ `/add-dir` only accepts paths under the user's session directory or roots listed in `WECHATBRIDGE_ADD_DIR_ROOTS`.
384
+
385
+ ## Ops & security (what the bridge actually enforces)
386
+
387
+ - **Whitelist first.** Empty `WECHATBRIDGE_ALLOWED_SENDERS` means only `WECHATBRIDGE_ADMINS` members can use the bridge. `*` means everyone. Both empty means nobody.
388
+ - **Auto-approve CLIs.** agy runs with `--dangerously-skip-permissions`, grok with `--always-approve` (unless planning mode is on), and dsh tools run without host path restrictions. Only give this bot to people you trust; it is not a multi-tenant sandbox.
389
+ - **The danger gate matches keywords.** It does not understand intent. The defaults look for concrete patterns: `rm -rf /`, pipe-to-shell, `mkfs`, `format c:`, and a few heavy Chinese phrases. Everyday wording like a bare "delete" is **not** gated. Override the list with `WECHATBRIDGE_CONFIRM_KEYWORDS`. You confirm with `WECHATBRIDGE_CONFIRM_TOKEN` (default `y`), within `WECHATBRIDGE_PENDING_TTL`.
390
+ - **Inbound media** are size-capped (default 20 MB), streamed, and CDN hosts are allowlisted. A missing `aes_key` returns a clear error.
391
+ - **Outbound artifacts** can only leave from the allowed per-user tree (agy: session scratch; grok and dsh: under the session dir). The bridge `realpath`-checks them first and caps them at `WECHATBRIDGE_MAX_OUTBOUND_BYTES`.
392
+ - **Concurrency:** a global process-slot cap (`WECHATBRIDGE_MAX_CONCURRENT`, default 4). One user's messages run serially and do **not** hold a global slot while waiting on a previous message. Different users run in parallel up to the cap. Heavy concurrent tasks multiply subprocess memory; see [Memory limits](#memory-limits).
393
+ - **Long replies** get split into chunks (`WECHATBRIDGE_MESSAGE_CHUNK`, default 2000 characters).
394
+ - **Data layout:** instance data lives under `~/.local/share/wechatbridge/<instance>/` (override with env). Runtime dirs prefer `0700`; token and QR files prefer `0600` (on Unix; Windows relies on NTFS ACLs).
395
+ - **Retention:** session temps and dialogue history use separate TTLs (`WECHATBRIDGE_SESSION_RETENTION_DAYS`, `WECHATBRIDGE_HISTORY_RETENTION_DAYS`). Prefs and auth are kept.
396
+ - **Child env** is sanitized (common secret-style variable names are stripped), and `HOME` (plus `USERPROFILE` on Windows) points at the per-user session dir.
397
+
398
+ ## Limitations
399
+
400
+ - Not a standalone agent. You need agy and/or grok and/or codex and/or dsh.
401
+ - **codex** is implemented and tested, but not battle-tested by me. I have no real Codex subscription or CLI to check against. I built it from source research, a JSONL fixture, and tests against a fake CLI, which pass. Confirm it in your own setup before you rely on it.
402
+ - **dsh** has both windowed memory (default, last `WECHATBRIDGE_DSH_MEMORY_TURNS` turns) and true persistent sessions (`WECHATBRIDGE_DSH_RESUME=true`). Tests cover the logic, but I have not run it long-term either. Resume mode also needs you to mount the `dsh-bridge-runner` plugin in your own dsh profile; this repo does not ship plugin files.
403
+ - On dsh, the model/effort/mode/persona slash commands are not wired yet. `/model`, `/fast`, `/planning`, `/persona`, and `/add-dir` return a "not supported" notice.
404
+ - Voice is WeChat speech-to-text only; no local ASR. An empty transcript gets a "type instead" reply.
405
+ - No video send or receive, and no native WeChat voice-bubble replies (no silk encode).
406
+ - One WeChat binding per process. Multiple accounts need multiple instances (`WECHATBRIDGE_INSTANCE`).
407
+ - Artifact send-back is best-effort: files detected under allowed paths get sent, not every file the CLI created anywhere.
408
+ - `/mcp` and `/agent` do not run MCP protocol or process supervisors inside the bridge. They only guide or rephrase for the CLI.
409
+ - Keep it behind a whitelist for trusted users.
410
+
411
+ ## Contributors
412
+
413
+ - [@KOP-XIAO](https://github.com/KOP-XIAO) — the dsh backend ([#3](https://github.com/dorokuma/wechatbridge/pull/3)) and its long-term memory plus resume support ([#4](https://github.com/dorokuma/wechatbridge/pull/4)), verified on real hardware.
414
+
415
+ ## Contributing
416
+
417
+ See [`CONTRIBUTING.md`](CONTRIBUTING.md). Versions follow SemVer starting at 1.0.0; changes go into [`CHANGELOG.md`](CHANGELOG.md).
418
+
419
+ ## License
420
+
421
+ MIT. See [`LICENSE`](LICENSE).