返回 CodeWhale
DEPLOYMENT.md
根目录 / integrations / wecom-bridge / DEPLOYMENT.md
1 # WeCom Bridge — Deployment Guide
2
3 ## Overview
4
5 The WeCom Bridge integrates CodeWhale with WeCom (企业微信) Smart Bot
6 WebSocket long-connection mode, enabling remote terminal agent interaction
7 without a public IP.
8
9 ## Prerequisites
10
11 1. **WeCom admin access** to create a Smart Bot (智能机器人)
12 2. **CodeWhale runtime API** running at `http://127.0.0.1:7878`
13 3. **Node.js 18+** for the bridge runtime
14
15 ### Create a WeCom Smart Bot
16
17 1. Open the [WeCom Admin Console](https://work.weixin.qq.com/wework_admin/frame#apps)
18 2. Navigate: 应用管理 → 智能机器人 → 创建机器人
19 3. Choose **API mode** (not Webhook mode)
20 4. Copy the **BotID** and **Secret** — you will need these
21
22 ## Quick Start
23
24 Use two terminals. In the first terminal, start the local runtime API:
25
26 ```bash
27 export CODEWHALE_RUNTIME_TOKEN="$(openssl rand -hex 32)"
28 codewhale serve --http --host 127.0.0.1 --port 7878
29 ```
30
31 The runtime reads the token from `CODEWHALE_RUNTIME_TOKEN`. Do not pass it with
32 `--auth-token`: a command-line argument is visible to other users in the process
33 list.
34
35 In the second terminal, start the bridge:
36
37 ```bash
38 cd integrations/wecom-bridge
39 cp .env.example .env
40 # Edit .env with your WeCom credentials and the same CODEWHALE_RUNTIME_TOKEN.
41 npm install
42 npm run start
43 ```
44
45 ## Configuration
46
47 Copy the environment template and edit:
48
49 ```bash
50 cp .env.example .env
51 # Edit .env with your credentials
52 ```
53
54 ### Required variables
55
56 | Variable | Example | Description |
57 |----------|---------|-------------|
58 | `WECOM_BOT_ID` | `wb-xxxxxxxxxxxxxxxx` | Smart Bot BotID from WeCom Admin |
59 | `WECOM_BOT_SECRET` | `your-secret` | Smart Bot Secret from WeCom Admin |
60 | `CODEWHALE_RUNTIME_TOKEN` | `rand-xxxxxxxx` | Bearer token for Runtime API (generate a random string) |
61
62 ### Optional variables
63
64 | Variable | Default | Description |
65 |----------|---------|-------------|
66 | `CODEWHALE_RUNTIME_URL` | `http://127.0.0.1:7878` | Runtime API address |
67 | `CODEWHALE_WORKSPACE` | `(cwd)` | Workspace directory |
68 | `CODEWHALE_MODEL` | `auto` | Default model name |
69 | `WECOM_CHAT_ALLOWLIST` | `""` | Comma-separated allowed UserIDs |
70 | `WECOM_ALLOW_UNLISTED` | `false` | Enable first-pairing mode |
71 | `WECOM_MAX_REPLY_CHARS` | `3500` | Max characters per reply message |
72 | `CODEWHALE_TURN_TIMEOUT_MS` | `900000` | Turn timeout in ms (15 min) |
73 | `CODEWHALE_APPROVAL_TIMEOUT_MS` | `300000` | Approval timeout in ms (5 min) |
74
75 ## First Pairing
76
77 1. Leave `WECOM_ALLOW_UNLISTED=false` and start the bridge.
78 2. Send any message to the bot in WeCom.
79 3. The bot will refuse the unlisted chat and reply with `chat_id=...` and,
80 when available, `user_id=...`.
81 4. Add one of those values to `WECOM_CHAT_ALLOWLIST`.
82 5. Restart the bridge.
83
84 ## Verify Installation
85
86 ```bash
87 # Check syntax
88 npm run check
89
90 # Run bridge tests
91 npm test
92 ```
93
94 Expected output: `ℹ tests 16 ℹ pass 16 ℹ fail 0`
95
96 ## Architecture
97
98 ```
99 WeCom Client → Smart Bot WebSocket → WeCom Bridge ──HTTP──→ codewhale serve --http
100 ◀── aibot_respond_msg ◀── (127.0.0.1:7878)
101 ```
102
103 The bridge:
104 1. Authenticates via BotID + Secret to obtain an `access_token`
105 2. Establishes a WebSocket long connection to the WeCom Smart Bot API
106 3. Receives `aibot_msg_callback` events, processes them through the Runtime API
107 4. Replies via `aibot_respond_msg` commands
108
109 ## Security Boundaries
110
111 - **No public port exposure**: `codewhale serve --http` binds to `127.0.0.1` only
112 - **Token authentication**: all `/v1/*` runtime calls require `CODEWHALE_RUNTIME_TOKEN`
113 - **Chat allowlist**: only chats/users in `WECOM_CHAT_ALLOWLIST` are served
114 - **Approval gate**: tool calls from WeCom require explicit approval (`/allow` or natural-language keywords)
115 - **WeCom only sees**: prompts, status summaries, thread listings, and approval requests — workspace contents, shell output, and runtime internals stay on your local machine
116
117 ## Troubleshooting
118
119 | Symptom | Likely cause | Fix |
120 |---------|-------------|-----|
121 | "not paired" warning | `WECOM_CHAT_ALLOWLIST` is empty | Add your user_id or enable `WECOM_ALLOW_UNLISTED=true` |
122 | 404 on `/allow` | Approval ID expired (5 min) | Respond faster, or increase `CODEWHALE_APPROVAL_TIMEOUT_MS` |
123 | Bridge exits immediately | Missing env vars | Run `node src/index.mjs` directly to see validation errors |
124 | Messages not received | Secret or BotID wrong | Verify credentials in WeCom Admin Console |
125 | WebSocket disconnect | Network flakiness | Bridge auto-reconnects; check the bridge stdout/stderr logs for details |
126
127 ## Production Deployment
128
129 ### Long-running service
130
131 Run the runtime API and bridge under the process manager you already use
132 (systemd, launchd, Task Scheduler, pm2, or a terminal multiplexer). The two
133 commands to supervise are:
134
135 ```bash
136 codewhale serve --http --host 127.0.0.1 --port 7878
137 npm run start --prefix integrations/wecom-bridge
138 ```
139
140 Give the runtime its token through the service's environment, never on the
141 command line. Under systemd, put `CODEWHALE_RUNTIME_TOKEN=...` in a root-owned
142 file that only the service user can read and name it with `EnvironmentFile=`;
143 an `Environment=` line is shown by `systemctl show`. Other supervisors have an
144 equivalent environment-file setting. Give the bridge the same value in its own
145 environment.
146
147 ### Logging
148
149 The bridge logs to stdout/stderr. Configure your service manager to capture
150 those streams; for example, systemd captures them in `journalctl`, and launchd
151 can redirect them with `StandardOutPath` / `StandardErrorPath`.
152
153 ### Auto-restart
154
155 Enable restart/recovery in the same process manager. The bridge reconnects to
156 WeCom after transient WebSocket disconnects, but the supervisor should restart
157 the process after crashes or host reboots.
158
159 ## Related Documentation
160
161 - [WeCom Bridge README](README.md)
162 - [CodeWhale Security Policy](../../.github/SECURITY.md)
163 - [CodeWhale Contributing Guide](../../CONTRIBUTING.md)
164
164 lines MARKDOWN