@linjz/opencode-qqbot 0.3.16 → 0.3.17
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.
- package/README.md +303 -266
- package/README.zh-CN.md +313 -0
- package/dist/notify-cli.js +64 -12
- package/dist/opencode-qqbot-plugin.js +356 -92
- package/dist/openqq-bot.js +878 -226
- package/dist/openqq-cli.js +253 -64
- package/dist/openqq-watchdog.js +373 -110
- package/package.json +2 -2
- package/src/bridge.ts +292 -3
- package/src/cli/notify.ts +4 -1
- package/src/cli/watchdog-main.ts +22 -7
- package/src/commands.ts +36 -10
- package/src/config.ts +39 -5
- package/src/core/bot-config.ts +66 -0
- package/src/core/bot-control.ts +21 -3
- package/src/core/bot-launcher.ts +4 -2
- package/src/core/cron-reminders.ts +84 -0
- package/src/core/discovery.ts +207 -169
- package/src/core/error-rescue.ts +1 -1
- package/src/core/file-log.ts +16 -7
- package/src/core/inflight-journal.ts +1 -0
- package/src/core/instance-lock.ts +32 -1
- package/src/core/log-rotate.ts +91 -0
- package/src/core/mirror.ts +7 -1
- package/src/core/notices.ts +62 -0
- package/src/core/outbound-sanitize.ts +29 -0
- package/src/core/permission.ts +7 -0
- package/src/core/queue-view.ts +1 -0
- package/src/core/send-media.ts +29 -2
- package/src/core/start-error.ts +26 -0
- package/src/core/stt.ts +55 -0
- package/src/core/supervisor-election.ts +53 -0
- package/src/core/watchdog.ts +171 -14
- package/src/index.ts +262 -250
- package/src/opencode/client.ts +32 -1
- package/src/opencode/mirror.ts +10 -3
- package/src/plugin-entry.ts +21 -2
- package/src/qq/gateway.ts +35 -0
- package/src/qq/media.ts +21 -0
- package/src/qq/sender.ts +50 -1
package/README.md
CHANGED
|
@@ -1,266 +1,303 @@
|
|
|
1
|
-
# OpenCode QQ Bot
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[ | [简体中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
Talk to [OpenCode](https://opencode.ai) — the open-source AI coding agent — through QQ. Monitor desktop sessions from your phone, launch tasks remotely, approve permission requests and answer form prompts right in QQ, with two-way images and voice, and long tasks that never lose their answers.
|
|
6
|
+
|
|
7
|
+
Built for production, not just message bridging: a self-healing watchdog, an auditable kill log, dead-turn auto-continuation, official-quota-aligned rate governance, and multi-bot single-process isolation.
|
|
8
|
+
|
|
9
|
+
[](https://www.npmjs.com/package/@linjz/opencode-qqbot)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](https://bot.q.qq.com/wiki/)
|
|
12
|
+
[](https://bun.sh)
|
|
13
|
+
[](https://www.typescriptlang.org/)
|
|
14
|
+
|
|
15
|
+
> Target users: Chinese-speaking QQ users. OpenCode V2 (desktop background service) + an official QQ open-platform bot (C2C private chat / group @mention).
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
| Capability | What it does |
|
|
20
|
+
|---|---|
|
|
21
|
+
| Two-way chat | QQ messages reach the OpenCode session; AI replies are pushed back to QQ (markdown rendering, long answers auto-split with table atoms intact and code-fence repair) |
|
|
22
|
+
| Desktop session mirror | 📋 Digest card: the latest Q&A round of a desktop session is mirrored to QQ in real time (full text by default) |
|
|
23
|
+
| Progress watching | `/watch` subscribes to tool-call milestones (🔧 command summary / exit code), throttled and merged |
|
|
24
|
+
| **Permission approval** | OpenCode permission requests arrive as 🔐 four-button cards (allow once / always allow / reject / ignore). Taps take effect silently (the tapped button shows ✓/✗ via native visited state). The card also shows exactly which rule "always allow" will save. Text replies ("允许/总是/拒绝") work too |
|
|
25
|
+
| **Form prompt bridge** | OpenCode Forms (single-choice / multi-choice / boolean / text) render as 📝 QQ cards — answer by tapping buttons or replying with a letter (A/B/C). Requests and forms from desktop-side turns are pushed as well (always-on watcher, 12s poll) |
|
|
26
|
+
| Remote commands | `/new /cancel /status /sessions /history /models /agents /rename /watch /mirror /progress /queue /ledger /send` |
|
|
27
|
+
| Rich media | Receive images (native attachments go to the AI for vision); **receive voice** (QQ's built-in ASR transcribes to text into the prompt for free); **send images/files** (AI replies containing `QQ_SEND_FILE: <path>` or the `/send <path>` command; 20MB limit) |
|
|
28
|
+
| Quote context | Long-press quote a message and ask — the AI sees the quoted content (our own replies are indexed too) |
|
|
29
|
+
| Long tasks never lost | 30-min foreground timeout falls back to a background wait (SSE + polling, up to 24h); a 30-min stall triggers a proactive notice with status (stalled / unknown / finished-but-lost), the latest tool action, and how long there's been no progress |
|
|
30
|
+
| Duplicate-proof delivery | Two-layer dedup: cross-process atomic msgId claim + same-content in-flight fingerprint; long identical replies deduped outbound for 24h; "received" ack bubble off by default (`QQ_ACK_RECEIPT=1` to enable) |
|
|
31
|
+
| Restart recovery | Gateway session/seq persisted — restart prefers RESUME (server only backfills the disconnect window, no backlog replay); the in-flight ledger reconciles on startup: answered→re-delivered, running→re-attached, queued→notified (never mis-dispatched) |
|
|
32
|
+
| Rendering fidelity | Ordered lists auto-convert `1.` → `1、` (QQ's renderer renumbers `N.` across paragraphs; `N、` is faithful), code blocks untouched, idempotent |
|
|
33
|
+
| Self-healing | Startup race backoff, port-drift auto-discovery with hot base-swap, patrol alerts (QQ proactive messages), a production watchdog (OTP-style restart intensity, dual-signal liveness, 10s post-spawn re-spawn), single-instance lock with heartbeat + 60s self-check |
|
|
34
|
+
| Auditability | Every watchdog kill decision lands in a dedicated JSONL audit log (`~/.openqq/kills.log`): the rationale (health snapshot), the action (pid + TERM/KILL escalation), and the consequence (new pid + restart counter) |
|
|
35
|
+
| Security | fail-closed allowlist admission (non-listed users always rejected), download-domain allowlist, secret masking in logs, push content redaction, dangerous-command approval driven by OpenCode's permission rules (four-button cards) |
|
|
36
|
+
|
|
37
|
+
Why QQ instead of the official mobile app: the official OpenCode Mobile (TestFlight / Google Play) is a remote for the desktop app — install required, out-of-region store accounts needed in China, and pairing through tunnels. This bot closes the loop natively in QQ: no app, no QR pairing, approval cards delivered straight to your chat.
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
### Prerequisites
|
|
42
|
+
|
|
43
|
+
1. **QQ bot credentials**: create a bot on the [QQ Open Platform](https://q.qq.com) and get the **AppID** and **AppSecret** (for sandbox debugging, configure sandbox accounts in the admin console first; production requires a server public-IP allowlist).
|
|
44
|
+
2. **OpenCode V2** running on the same machine (the bot connects to its background service API).
|
|
45
|
+
3. **Runtime**: bun or node, either one (bun recommended; node ≥ 20 also works via the bundled runtime build).
|
|
46
|
+
|
|
47
|
+
### Pick a flavor (either one)
|
|
48
|
+
|
|
49
|
+
| | Plugin flavor (recommended) | Standalone flavor |
|
|
50
|
+
|---|---|---|
|
|
51
|
+
| Best for | OpenCode already installed locally, zero-config | Servers / your own daemon setup |
|
|
52
|
+
| Install | `opencode plugin add @linjz/opencode-qqbot` (or add `"@linjz/opencode-qqbot"` to the plugins array) | `npm i -g @linjz/opencode-qqbot` (or `github:linjz3376/opencode-qqbot`) |
|
|
53
|
+
| Keep-alive | The plugin auto-launches the bot and recovers when the OpenCode service restarts | Self-managed: `openqq` in foreground / systemd / Task Scheduler |
|
|
54
|
+
| Config entry | `npx @linjz/opencode-qqbot setup` (QR scan) or hand-write `~/.openqq/.env` | Same (the global `openqq-setup` command also works) |
|
|
55
|
+
|
|
56
|
+
Developers installing from source: `bun run deploy:plugin` (full package unpacked into the global plugin directory — byte-identical to what npm users get).
|
|
57
|
+
|
|
58
|
+
### Configuration (`~/.openqq/.env`, single point of secrets)
|
|
59
|
+
|
|
60
|
+
Bind by scanning a QR code with your phone (writes credentials automatically; the connector installs on demand):
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npx @linjz/opencode-qqbot setup
|
|
64
|
+
# standalone flavor with -g: openqq-setup
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Or create it manually:
|
|
68
|
+
|
|
69
|
+
```ini
|
|
70
|
+
QQ_APP_ID=your AppID
|
|
71
|
+
QQ_APP_SECRET=your AppSecret
|
|
72
|
+
ALLOWED_USERS=your QQ openid # fail-closed: required; users not on the list are always rejected
|
|
73
|
+
OPENCODE_BASE_URL=http://127.0.0.1:<port>/api # OpenCode desktop service address (2.1.x ports are dynamic; the bot auto-discovers on connect failure)
|
|
74
|
+
OPENCODE_PASSWORD=opencode service password (Basic auth password from the desktop service config)
|
|
75
|
+
QQ_SANDBOX=false
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Start
|
|
79
|
+
|
|
80
|
+
- **Plugin flavor**: nothing to start manually — once the plugin is installed and credentials are set, the plugin's patrol launches the bot; it also recovers when the OpenCode service restarts.
|
|
81
|
+
- **Standalone flavor**: run `openqq` in the foreground; for daemon mode pick your OS (Windows: Task Scheduler via `openqq service install`; Linux: `systemd --user`; macOS: launchd or `nohup`).
|
|
82
|
+
|
|
83
|
+
<details>
|
|
84
|
+
<summary>Linux: systemd --user unit (copy-paste ready)</summary>
|
|
85
|
+
|
|
86
|
+
`~/.config/systemd/user/openqq.service`:
|
|
87
|
+
|
|
88
|
+
```ini
|
|
89
|
+
[Unit]
|
|
90
|
+
Description=OpenCode QQ Bot
|
|
91
|
+
After=network.target
|
|
92
|
+
|
|
93
|
+
[Service]
|
|
94
|
+
Type=simple
|
|
95
|
+
ExecStart=openqq
|
|
96
|
+
Restart=on-failure
|
|
97
|
+
RestartSec=30
|
|
98
|
+
Environment=HOME=%h
|
|
99
|
+
|
|
100
|
+
[Install]
|
|
101
|
+
WantedBy=default.target
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
systemctl --user daemon-reload
|
|
106
|
+
systemctl --user enable --now openqq
|
|
107
|
+
# keep running without login: loginctl enable-linger $USER
|
|
108
|
+
```
|
|
109
|
+
</details>
|
|
110
|
+
|
|
111
|
+
<details>
|
|
112
|
+
<summary>macOS: launchd plist (copy-paste ready)</summary>
|
|
113
|
+
|
|
114
|
+
`~/Library/LaunchAgents/net.opencode-qqbot.plist`:
|
|
115
|
+
|
|
116
|
+
```xml
|
|
117
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
118
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
119
|
+
<plist version="1.0"><dict>
|
|
120
|
+
<key>Label</key><string>net.opencode-qqbot</string>
|
|
121
|
+
<key>ProgramArguments</key>
|
|
122
|
+
<array><string>/usr/local/bin/openqq</string></array>
|
|
123
|
+
<key>RunAtLoad</key><true/>
|
|
124
|
+
<key>KeepAlive</key><true/>
|
|
125
|
+
<key>StandardOutPath</key><string>/tmp/openqq.out.log</string>
|
|
126
|
+
<key>StandardErrorPath</key><string>/tmp/openqq.err.log</string>
|
|
127
|
+
</dict></plist>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
launchctl load ~/Library/LaunchAgents/net.opencode-qqbot.plist
|
|
132
|
+
```
|
|
133
|
+
</details>
|
|
134
|
+
|
|
135
|
+
> **Single instance**: the bot holds a file lock on startup (`~/.openqq/openqq-<appId>.lock`, 10s heartbeat, 30s zombie takeover) — a second instance waits in standby and takes over when the host exits, preventing QQ gateway mutual-kick between twins. `QQ_INSTANCE_LOCK=false` disables it.
|
|
136
|
+
|
|
137
|
+
> **Cross-platform**: the bot body has zero OS branches (pure TS, same shape as the official QQ SDK); bun preferred, node ≥ 20 falls back automatically. Port auto-discovery works on all three OSes (Linux `ss -tln`, macOS `netstat -an -p tcp`, Windows `netstat -ano -p tcp`).
|
|
138
|
+
|
|
139
|
+
## Commands
|
|
140
|
+
|
|
141
|
+
| Command | What it does |
|
|
142
|
+
|---|---|
|
|
143
|
+
| `/new` | Start a new session |
|
|
144
|
+
| `/cancel` | Abort the current generation and clear the busy queue |
|
|
145
|
+
| `/status` | Server / session / model / agent status |
|
|
146
|
+
| `/sessions` | List sessions and switch by number |
|
|
147
|
+
| `/history [N]` | Last N rounds (default 3, max 10; newlines / code blocks / tables preserved) |
|
|
148
|
+
| `/models` | View or switch the model (session-level) |
|
|
149
|
+
| `/agents` | View or switch the agent |
|
|
150
|
+
| `/rename <name>` | Rename the current session (local title only) |
|
|
151
|
+
| `/watch` / `/watch off` | Watch mode: tool-call milestones pushed live |
|
|
152
|
+
| `/progress` | Manual progress query: tool/failure stats and in-flight status (local event source, zero model calls) |
|
|
153
|
+
| `/queue` | Busy queue: view / `clear` / `drop <n>` (messages auto-queue FIFO while busy) |
|
|
154
|
+
| `/ledger` | Ledger view: snapshot / `check` reconcile / `settle <suffix> confirm` manual settle (orphans and in-flight traceable) |
|
|
155
|
+
| `/mirror` | Digest mirror on/off |
|
|
156
|
+
| `/send <path>` | Send a file/image from an allowlisted directory to QQ |
|
|
157
|
+
|
|
158
|
+
## Environment variables (optional tuning)
|
|
159
|
+
|
|
160
|
+
| Variable | Default | Meaning |
|
|
161
|
+
|---|---|---|
|
|
162
|
+
| `QQ_ACTIVE_DAILY_LIMIT` | 100 | Proactive message daily quota (official QQ limit is 1000/day/relation; headroom kept) |
|
|
163
|
+
| `QQ_PASSIVE_RIDE_WINDOW_MS` | 300000 | How long to "ride" a passive reply window before falling back to proactive |
|
|
164
|
+
| `QQ_STREAM_STAGGER_MS` | 300 | Stagger between split-reply chunks (0 = send all at once) |
|
|
165
|
+
| `QQ_SESSION_IDLE_HOURS` | 6 | Idle session eviction threshold |
|
|
166
|
+
| `QQ_INTERACTION_WATCH_SECONDS` | 12 | Always-on interaction watcher poll interval (0 = off) |
|
|
167
|
+
| `QQ_BUSY_DEFAULT` | `steer` | Busy-time delivery, aligned with the official TUI (Enter steers): new messages merge into the running turn. Set `queue` for client-side FIFO queuing (legacy behavior) |
|
|
168
|
+
| `QQ_REDISPATCH_MAX_AGE_MS` | 7200000 | Startup-reconcile auto-redispatch window for lost messages (0 = notify only; capped by the 24h ledger trim) |
|
|
169
|
+
| `QQ_ACK_RECEIPT` | off | "Received, working on it" ack bubble (set `true`/`on`/`1` for debugging) |
|
|
170
|
+
| `QQ_GATEWAY_STATE_MAX_HOURS` | 6 | Gateway session persistence window (older / RESUME-rejected falls back to IDENTIFY) |
|
|
171
|
+
| `QQ_EVENT_PROBE` | off | Plugin-side event probe (`true` dumps all event types to `~/.openqq/opencode-events.jsonl`, for diagnosis) |
|
|
172
|
+
| `OPENCODE_TURN_TIMEOUT_MINUTES` | 30 | Foreground turn timeout (falls back to background wait) |
|
|
173
|
+
| `QQ_DIGEST_ANSWER_MAX_CHARS` | 0 (full text) | Digest card answer truncation limit |
|
|
174
|
+
| `QQ_HISTORY_USER_CLIP` / `QQ_HISTORY_TEXT_CLIP` | 500 / 1200 | /history user/AI line truncation |
|
|
175
|
+
| `QQ_SEND_EXTRA_ROOTS` | — | Extra allowed directories for `/send` (comma-separated) |
|
|
176
|
+
| `QQ_ORPHAN_MS` | 90000 | Orphan threshold (accepted but silent beyond this; valid 5s–60min, invalid falls back) |
|
|
177
|
+
| `QQ_WATCHDOG_INTERVAL_MS` | 300000 | Plugin bootstrap patrol interval (5min; the service-side autostart default is 10min) |
|
|
178
|
+
| `QQ_PERM_DEFER_MS` | 500 | Approval card deferral window (0–10s): if the request is resolved elsewhere within the window, no QQ card — aligns with desktop silent handling |
|
|
179
|
+
| `QQ_QUEUE_RECEIPTS` | off | Queue-completion receipt "queue #N done" (off by default to prevent batch spam) |
|
|
180
|
+
| `QQ_LOG_FILE` | `~/.openqq/openqq.log` (unified log, bot + watchdog in one file) | In-process file log path; 20MB × 4 generations, 100MB total budget (`QQ_LOG_MAX_MB`/`QQ_LOG_KEEP`/`QQ_LOG_TOTAL_MB` tunable) |
|
|
181
|
+
| `QQBOT_DEFAULT_CWD` | bot process cwd | Default project directory for new sessions |
|
|
182
|
+
| `QQ_SANDBOX` | false | QQ sandbox environment (REST domain switches to sandbox) |
|
|
183
|
+
| `QQ_INSTANCE_LOCK` | on | Single-instance file lock (`false` to disable; only for multi-instance debugging) |
|
|
184
|
+
| `QQ_BOTS` | — | Multi-role JSON table (multiple QQ bots in one process, data bucketed per role; absent = single-role legacy) |
|
|
185
|
+
| `QQ_LANE_SCOPE` | `per-session-peer` | Session isolation granularity: `per-session-peer` (official per-peer refined per-session) or `per-peer` |
|
|
186
|
+
|
|
187
|
+
**Internal knobs (troubleshooting/tests only — safe to ignore day to day)**
|
|
188
|
+
|
|
189
|
+
| Variable | Default | Meaning |
|
|
190
|
+
|---|---|---|
|
|
191
|
+
| `QQ_ACTIVE_MINUTE_LIMIT` | 20 | Proactive message per-minute rolling window (official 20/min aligned) |
|
|
192
|
+
| `QQ_API_CALL_TIMEOUT_MS` | 30000 | Per-call engine API timeout (P11 req-layer defense) |
|
|
193
|
+
| `QQ_SSE_SILENCE_MS` | 120000 | SSE silence kill threshold (P8: zero-byte proactive reconnect) |
|
|
194
|
+
| `QQ_BG_RESUME_MAX` | 3 | Dead-turn auto-continuation cap (0 = off; valid 0–5) |
|
|
195
|
+
| `QQ_BG_STREAM_DEAD_MS` | 15min | Background stream-liveness dead threshold (valid 1–60min) |
|
|
196
|
+
| `QQ_BG_TOOL_GRACE_MS` | 60min | Bounded tool-execution grace window before dead judgment |
|
|
197
|
+
| `QQ_REAPER_MIN_AGE_MS` | 40min | Zombie reaper age gate |
|
|
198
|
+
| `QQ_REAPER_SILENCE_MS` | 30min | Zombie reaper silence gate |
|
|
199
|
+
| `QQ_REAPER_SCAN_SECONDS` | 60 | Zombie/orphan patrol interval (same tick) |
|
|
200
|
+
| `QQ_ORPHAN_SCAN_SECONDS` | 60 | Orphan ledger patrol interval |
|
|
201
|
+
| `QQ_DISPATCH_STALL_MS` | 120000 | Dispatch-stall sentinel threshold (valid 30s–60min) |
|
|
202
|
+
| `QQ_NOTIFY_MAX_ATTEMPTS` | 8 | `openqq notify` delivery attempt cap |
|
|
203
|
+
| `QQ_NOTIFY_TTL_HOURS` | 24 | notify outbox queue retention |
|
|
204
|
+
| `QQ_DATA_DIR` | `~/.openqq` | Runtime data root (ledgers/logs/locks; whole directory is portable) |
|
|
205
|
+
| `QQ_INFLIGHT_FILE` | `<data dir>/inflight.json` | In-flight ledger file path |
|
|
206
|
+
| `QQ_OUTBOX_FILE` | `<data dir>/outbox.json` | notify outbox file path |
|
|
207
|
+
| `QQ_FORM_JOURNAL_FILE` | `<data dir>/forms.json` | Form-bridge persistence file |
|
|
208
|
+
| `QQ_DELIVERY_DEDUPE_FILE` | `<data dir>/delivery-dedupe.json` | Outbound dedupe state file |
|
|
209
|
+
|
|
210
|
+
> All variables go into `~/.openqq/.env` (one `KEY=value` per line). Secrets discipline: the file is read by the bot only; no tool or session may echo its contents.
|
|
211
|
+
|
|
212
|
+
### Behavior config (`~/.openqq/config.json`, optional)
|
|
213
|
+
|
|
214
|
+
Mirrors the official OpenCode layering ("json = behavior, env = secrets"): behavior knobs can move to `config.json` (flat JSON, **same key names as the env vars**), leaving `.env` for secrets only. Precedence, high to low:
|
|
215
|
+
|
|
216
|
+
```
|
|
217
|
+
inherited env > ~/.openqq/.env > ~/.openqq/config.json > built-in defaults
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
```json
|
|
221
|
+
{
|
|
222
|
+
"QQ_BUSY_DEFAULT": "queue",
|
|
223
|
+
"QQ_STREAM_STAGGER_MS": 500
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
`QQ_CONFIG_FILE` overrides the config path (tests/multi-instance). Keep secrets (QQ_APP_SECRET, OPENCODE_PASSWORD, …) out of config.json — behavior keys only.
|
|
228
|
+
|
|
229
|
+
## Architecture
|
|
230
|
+
|
|
231
|
+
Four layers (every file carries an `@input/@output/@pos` header):
|
|
232
|
+
|
|
233
|
+
```
|
|
234
|
+
src/qq/ QQ transport (WS gateway / REST API / sender / media)
|
|
235
|
+
src/opencode/ OpenCode V2 client (REST + SSE event stream / sessions / history rendering)
|
|
236
|
+
src/core/ Business domains (approval/cards/form-bridge/delivery/mirror/link-watch/port-discovery/reply-limiter/context-buffer/send-media, plus inbound-dedupe, gateway-state, inflight-journal, instance-lock, zombie-reaper, watchdog, notices)
|
|
237
|
+
src/bridge.ts Orchestration facade + commands.ts + entry index.ts
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Tests
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
npm run verify # tsc + 56 unit suites (admission idempotency, gateway persistence, turn attribution, rendering adaptation, ledger recovery, watchdog decisions, kill-audit, notices, ...)
|
|
244
|
+
npm run test:e2e # five e2e suites (requires a running OpenCode service)
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Operations (all TypeScript, cross-platform, shipped with the package)
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
openqq restart # wait for idle → stop → start (replaces the old safe-restart.ps1)
|
|
251
|
+
openqq restart --force # skip idle wait (maintenance window)
|
|
252
|
+
openqq watchdog --once # single patrol (autostart / systemd timer / launchd)
|
|
253
|
+
openqq watchdog --loop --interval-min 10 # resident loop
|
|
254
|
+
openqq service install|uninstall|status # autostart registration: win32=schtasks / linux=systemd user / darwin=launchd
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Liveness = lock-file pid + `process.kill(pid, 0)` + heartbeat freshness (>5min stale triggers restart, 60s grace during startup).
|
|
258
|
+
`~/.openqq/` holds **runtime data only** (`.env`, ledgers, unified log `openqq.log`, `heartbeat`); the kill-decision audit line lives in `kills.log`.
|
|
259
|
+
|
|
260
|
+
## Troubleshooting
|
|
261
|
+
|
|
262
|
+
| Symptom | Check first |
|
|
263
|
+
|---|---|
|
|
264
|
+
| QQ not responding | Unified log `~/.openqq/openqq.log` (bot output and watchdog audits in one file, 20MB × 4 generations, 100MB budget) |
|
|
265
|
+
| Process疑似 frozen | `openqq watchdog --once` — judges and self-heals in one pass (kill residue, then relaunch) |
|
|
266
|
+
| ECONNREFUSED | Desktop service port drift — restarting the bot auto-discovers the new port (`~/.openqq/last-discovered.json` trail) |
|
|
267
|
+
| Desktop popup blocking a task | The background wait sends stall notices with recent actions; `/stop` aborts |
|
|
268
|
+
| Log shows "instance standing by" | Single-instance lock active — another instance holds the lock (the 60s self-check converges twins on lock changes; normally nothing to do) |
|
|
269
|
+
|
|
270
|
+
## Platform notes
|
|
271
|
+
|
|
272
|
+
Hard-won findings from real deployments (Windows 11 / OpenCode v2.2.2) — the plugin can't fix these, but they keep tripping users:
|
|
273
|
+
|
|
274
|
+
**QQ platform**
|
|
275
|
+
|
|
276
|
+
1. The token endpoint `POST https://bots.qq.com/app/getAppAccessToken` takes **`clientSecret`** (older docs say `appSecret`); the `100016 invalid appid or secret` error is misleading — check the field name before resetting your secret.
|
|
277
|
+
2. Tokens are environment-independent and always use the production domain `bots.qq.com`; **the sandbox domain `sandbox.api.sgroup.qq.com has no token endpoint** (it returns `11001 unsupported call`).
|
|
278
|
+
3. Sandbox bots have a separate gateway: `GET https://sandbox.api.sgroup.qq.com/gateway` → `wss://sandbox.api.sgroup.qq.com/websocket`; a sandbox bot connecting to the production gateway is rejected with 4914.
|
|
279
|
+
|
|
280
|
+
**OpenCode side**
|
|
281
|
+
|
|
282
|
+
4. `opencode plugin add` occasionally hangs silently (>3min); retrying usually works.
|
|
283
|
+
5. `opencode reload` **only adds, never removes**: entries removed from the plugins array stay loaded in server memory (old plugin timers keep running); a full service restart is required.
|
|
284
|
+
6. The global plugin-directory scanner only recognizes root-level entrypoints (a package.json `exports` pointing at a `./dist` subpath gets skipped) — **directory-flavor installs only**; the npm package flavor has no such limit.
|
|
285
|
+
7. `opencode plugin update` needs the fully scoped name (`@linjz/opencode-qqbot`); the bare short name errors with `Plugin is not configured`.
|
|
286
|
+
8. npm plugin cache lives in `~/.cache/opencode/npm/<pkg>@<tag>/<timestamp>/` — `update` creates a new timestamp directory, so local patches on cached copies silently stop applying. Never patch cache copies.
|
|
287
|
+
|
|
288
|
+
## Alignment matrix with the OpenCode desktop
|
|
289
|
+
|
|
290
|
+
The QQ bridge is an adapter from "OpenCode interaction events" to "QQ interaction primitives". Every blocking interaction the desktop TUI can handle should have a counterpart on the QQ side (bridged or explicitly degraded):
|
|
291
|
+
|
|
292
|
+
| Desktop interaction | QQ side today | Gap |
|
|
293
|
+
|---|---|---|
|
|
294
|
+
| Permission requests | ✅ Four-button cards (once/always/reject/ignore) + text replies + desktop-pending requests pushed too (always-on watcher, 12s) | None |
|
|
295
|
+
| Single-field Form (choice/multi/bool/text) | ✅ Letter-button cards + text parsing + 30min TTL + form-journal persistence | None |
|
|
296
|
+
| **Multi-field Form (Questions)** | ⚠️ Falls back to "please complete on desktop" (explicit degradation) | Per-field wizard is a candidate enhancement (not implemented) |
|
|
297
|
+
| Future OpenCode interaction types | ⚠️ No alignment mechanism; patched one by one after the fact | Matrix acceptance: bridge with the version or register as "explicit degradation" — undefined states are not allowed |
|
|
298
|
+
|
|
299
|
+
Principle: unsupported types always get explicit copy + desktop guidance, never silent drops; enhancements like the per-field wizard land with minimal intrusion.
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
MIT
|