@linjz/opencode-qqbot 0.3.15 → 0.3.16
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 +42 -5
- package/bin/openqq.js +13 -0
- package/dist/notify-cli.js +28 -14
- package/dist/opencode-qqbot-plugin.js +934 -64
- package/dist/openqq-bot.js +3095 -677
- package/dist/openqq-cli.js +791 -0
- package/dist/openqq-watchdog.js +1063 -0
- package/package.json +8 -5
- package/src/bridge.ts +1310 -150
- package/src/cli/notify.ts +167 -158
- package/src/cli/openqq-main.ts +95 -0
- package/src/cli/service-main.ts +302 -0
- package/src/cli/watchdog-main.ts +77 -0
- package/src/commands.ts +644 -554
- package/src/config.ts +85 -0
- package/src/core/bot-control.ts +148 -0
- package/src/core/bot-launcher.ts +106 -13
- package/src/core/card-approval.ts +20 -9
- package/src/core/delivery.ts +145 -54
- package/src/core/discovery.ts +22 -4
- package/src/core/file-log.ts +35 -3
- package/src/core/form-bridge.ts +14 -5
- package/src/core/form-journal.ts +4 -4
- package/src/core/gateway-state.ts +3 -3
- package/src/core/inflight-journal.ts +214 -36
- package/src/core/instance-lock.ts +10 -2
- package/src/core/linkwatch.ts +3 -4
- package/src/core/markdown.ts +117 -0
- package/src/core/message-queue.ts +242 -91
- package/src/core/mirror.ts +222 -182
- package/src/core/notify-sink.ts +93 -96
- package/src/core/outbox.ts +10 -9
- package/src/core/permission.ts +627 -303
- package/src/core/question-form.ts +230 -0
- package/src/core/queue-view.ts +661 -0
- package/src/core/send-media.ts +32 -0
- package/src/core/storage.ts +34 -0
- package/src/core/watchdog.ts +513 -0
- package/src/core/zombie-reaper.ts +127 -0
- package/src/index.ts +140 -52
- package/src/opencode/client.ts +206 -27
- package/src/opencode/eventstream.ts +94 -12
- package/src/opencode/history.ts +4 -2
- package/src/opencode/mirror.ts +15 -2
- package/src/opencode/sessions.ts +135 -37
- package/src/plugin-entry.ts +120 -115
- package/src/qq/api.ts +165 -61
- package/src/qq/gateway.ts +79 -16
- package/src/qq/sender.ts +30 -18
- package/src/qq/types.ts +7 -0
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# OpenCode QQ Bot
|
|
1
|
+
# OpenCode QQ Bot
|
|
2
2
|
|
|
3
3
|
通过 QQ 机器人与 [OpenCode](https://opencode.ai) AI 编程助手对话——双向监控桌面会话、远程发起任务、权限审批与选择弹窗直达 QQ、图片语音双向、长任务不丢答案。
|
|
4
4
|
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
| 双向对话 | QQ 消息直达 opencode 会话,AI 回复推回 QQ(markdown 渲染、长文自动结构化分条:表格不拆散、代码块围栏自动修补) |
|
|
18
18
|
| 桌面会话镜像 | 📋 摘要卡:桌面会话的最近一轮问答实时推送到 QQ(默认全文) |
|
|
19
19
|
| 进度观察 | `/watch` 订阅工具调用里程碑(🔧 命令摘要/退出码),节流合并推送 |
|
|
20
|
-
| **权限审批** | opencode 发起权限请求 → QQ 收 🔐
|
|
20
|
+
| **权限审批** | opencode 发起权限请求 → QQ 收 🔐 四键卡片(允许一次/总是允许/拒绝/忽略),点按钮静默生效(点击后该按钮回显 ✓/✗,其余按钮保持原样),也可文字回复"允许/总是/拒绝" |
|
|
21
21
|
| **选择弹窗桥接** | opencode 的 Form(选择/多选/布尔/文本)→ QQ 📝 卡片,点按钮或回字母(A/B/C)作答;桌面端回合发起的审批/表单**同样**推送(常驻监听,12s 轮询,bot 空闲也收得到) |
|
|
22
|
-
| 远程指令 | `/new /stop /status /sessions /history /model /agent /rename /watch /mirror /progress /queue /send` |
|
|
22
|
+
| 远程指令 | `/new /stop /status /sessions /history /model /agent /rename /watch /mirror /progress /queue /ledger /send` |
|
|
23
23
|
| 富媒体 | 收图(原生附件给 AI 看图);**收语音**(QQ 内置 ASR 免费转文字进 prompt);**发图/发文件**(AI 回复含 `QQ_SEND_FILE: <路径>` 标记或 `/send <路径>` 指令,20MB 上限) |
|
|
24
24
|
| 引用上下文 | 长按引用某条消息提问,AI 能感知被引用的内容(我方回复也会入索引缓存) |
|
|
25
25
|
| 长任务不丢 | 前台 30min 超时 → 后台等待(SSE + 轮询,最长 24h);停滞 30min 主动提醒(带「已停滞/状态不明/已终结未回传」状态位 + 最近工具动作 + 无进展时长) |
|
|
@@ -141,10 +141,11 @@ launchctl load ~/Library/LaunchAgents/net.opencode-qqbot.plist
|
|
|
141
141
|
| `/history [N]` | 最近 N 轮历史(默认 3,上限 10;换行/代码块/表格完整保留) |
|
|
142
142
|
| `/model` | 查看或切换模型(session 级) |
|
|
143
143
|
| `/agent` | 查看或切换 Agent |
|
|
144
|
-
| `/rename <名>` |
|
|
144
|
+
| `/rename <名>` | 重命名当前会话(仅本地会话标题,不改服务端标题) |
|
|
145
145
|
| `/watch` / `/watch off` | 观察模式:工具调用里程碑实时推送 |
|
|
146
146
|
| `/progress` | 手动进度查询:工具调用/失败统计、当前在途状态(本地事件源,零模型调用) |
|
|
147
147
|
| `/queue` | 忙碌队列:查看 / `clear` 清空 / `drop <编号>` 定点删除(忙碌时消息自动 FIFO 排队) |
|
|
148
|
+
| `/ledger` | 台账视图:快照 / `check` 对账会话历史 / `settle <尾号> confirm` 人工结清(孤儿与在途可追溯) |
|
|
148
149
|
| `/mirror` | 摘要镜像开关 |
|
|
149
150
|
| `/send <路径>` | 把白名单目录内的文件/图片发到 QQ |
|
|
150
151
|
|
|
@@ -165,6 +166,14 @@ launchctl load ~/Library/LaunchAgents/net.opencode-qqbot.plist
|
|
|
165
166
|
| `QQ_DIGEST_ANSWER_MAX_CHARS` | 0(全文) | 摘要卡答案截断上限 |
|
|
166
167
|
| `QQ_HISTORY_USER_CLIP` / `QQ_HISTORY_TEXT_CLIP` | 500 / 1200 | /history 用户/AI 行截断 |
|
|
167
168
|
| `QQ_SEND_EXTRA_ROOTS` | — | `/send` 额外允许目录(逗号分隔) |
|
|
169
|
+
| `QQ_ORPHAN_MS` | 90000 | 孤儿判定阈值(受理后无下文超此时长记孤儿;合法 5s~60min,非法回退默认) |
|
|
170
|
+
| `QQ_WATCHDOG_INTERVAL_MS` | 300000 | 插件引导器巡检间隔(5min;计划任务侧由 service install 注册,默认 10min) |
|
|
171
|
+
| `QQ_PERM_DEFER_MS` | 500 | 审批弹卡延迟判定窗(0~10s):窗口内 ask 已被其他端处置则不推 QQ 卡,与桌面静默放行对齐 |
|
|
172
|
+
| `QQ_QUEUE_RECEIPTS` | 关 | 队列完成回执「队列 #N 完成」(批量排空防刷,默认关) |
|
|
173
|
+
| `QQ_LOG_FILE` | `~/.openqq/openqq.log`(detached 拉起为 `bot.out.log`) | 进程内文件日志路径 |
|
|
174
|
+
| `QQBOT_DEFAULT_CWD` | bot 进程工作目录 | 新会话默认项目目录(目录继承链兜底值) |
|
|
175
|
+
| `QQ_SANDBOX` | false | QQ 沙箱环境(REST 域切至 sandbox) |
|
|
176
|
+
| `QQ_INSTANCE_LOCK` | 开 | 单实例文件锁(`false` 关闭,仅排查多实例问题时临时用) |
|
|
168
177
|
|
|
169
178
|
## 架构
|
|
170
179
|
|
|
@@ -184,11 +193,25 @@ npm run verify # tsc + 28 个单元套件(受理幂等/网关持久化/
|
|
|
184
193
|
npm run test:e2e # 五套 e2e(需 opencode 服务在跑)
|
|
185
194
|
```
|
|
186
195
|
|
|
196
|
+
## 运维入口(全 TS,跨平台,随包分发)
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
openqq restart # 等空闲 → 停 → 拉(旧 safe-restart.ps1 的替代)
|
|
200
|
+
openqq restart --force # 跳过空闲等待(维护窗口)
|
|
201
|
+
openqq watchdog --once # 单次巡检(自启项 / systemd timer / launchd 调用)
|
|
202
|
+
openqq watchdog --loop --interval-min 10 # 常驻循环
|
|
203
|
+
openqq service install|uninstall|status # 自启注册:win32=schtasks / linux=systemd user / darwin=launchd
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
判活 = 锁文件 pid + `process.kill(pid, 0)` + 心跳新鲜度(>5 分钟触发重启,启动期有 60s 宽限)。
|
|
207
|
+
`~/.openqq/` **只放运行时数据**(`.env`、台账、日志、`heartbeat`、`watchdog.log`),不放任何脚本。
|
|
208
|
+
|
|
187
209
|
## 排障
|
|
188
210
|
|
|
189
211
|
| 症状 | 先看 |
|
|
190
212
|
|---|---|
|
|
191
|
-
| QQ 无响应 | 日志
|
|
213
|
+
| QQ 无响应 | 日志 `~/.openqq/bot.out.log`;看门狗 `~/.openqq/watchdog.log` |
|
|
214
|
+
| 进程疑似僵死 | `openqq watchdog --once` —— 一次判定并自愈(先杀残留再拉起) |
|
|
192
215
|
| ECONNREFUSED | 桌面服务端口漂移——重启 bot 会自动发现新端口(`~/.openqq/last-discovered.json` 留痕) |
|
|
193
216
|
| 桌面弹窗卡住任务 | 后台等待会发带任务的停滞通告(含最近动作与无进展时长);`/stop` 可终止 |
|
|
194
217
|
| 日志见「已有实例待命」 | 单实例锁生效中——另一实例持锁(60s 自检会在锁异动时收敛双活,正常无需处理) |
|
|
@@ -224,6 +247,20 @@ QQ 桥的本质是「opencode 交互事件 → QQ 交互原语」的适配器。
|
|
|
224
247
|
|
|
225
248
|
原则:未支持的类型永远给出明确文案 + 桌面端指引,不允许静默丢弃;多字段向导等增强按最小侵入接入。
|
|
226
249
|
|
|
250
|
+
|
|
251
|
+
## English Summary
|
|
252
|
+
|
|
253
|
+
**OpenCode QQ Bot** bridges [OpenCode](https://opencode.ai) (an open-source AI coding agent) into QQ (China's largest IM). Talk to your coding agent from your phone: send tasks, approve dangerous tool calls via interactive cards, dictate prompts with built-in voice-to-text, receive long-running results, and even send/receive files.
|
|
254
|
+
|
|
255
|
+
- **Approval cards & form prompts**: OpenCode permission requests and multi-step forms render as interactive QQ cards (tap to allow/deny, or reply by text)
|
|
256
|
+
- **Long tasks never lost**: 30-min foreground timeout falls back to a 24h background wait with status-aware stall notices; results are pushed back on completion
|
|
257
|
+
- **Duplicate-proof delivery**: cross-process msgId claim + same-content fingerprint admission; gateway session/seq persisted across restarts (RESUME, no backlog replay)
|
|
258
|
+
- **Notify channel**: cron / CI / any process can push results to QQ via `openqq notify` (file-locked outbox, TTL, idempotent by --id)
|
|
259
|
+
- **QQ rendering fix**: ordered lists auto-flatten to the `、` style — what you see on desktop matches what lands on QQ
|
|
260
|
+
|
|
261
|
+
Install: `opencode plugin add @linjz/opencode-qqbot` — Chinese README below, docs/review playbook in the repo.
|
|
262
|
+
|
|
227
263
|
## License
|
|
228
264
|
|
|
229
265
|
MIT
|
|
266
|
+
|
package/bin/openqq.js
CHANGED
|
@@ -12,6 +12,19 @@ import { join, dirname } from "node:path"
|
|
|
12
12
|
|
|
13
13
|
const root = join(dirname(fileURLToPath(import.meta.url)), "..")
|
|
14
14
|
|
|
15
|
+
// 2026-11-09 架构红线:看门狗/重启/自启注册都是包内 TS 产物,经本 shim 统一分发。
|
|
16
|
+
// 传参不改变无参数行为。
|
|
17
|
+
function runChild(cmd, args) {
|
|
18
|
+
const r = spawnSync(cmd, args, { stdio: "inherit" })
|
|
19
|
+
process.exit(r.status ?? 1)
|
|
20
|
+
}
|
|
21
|
+
if (argv[0] === "watchdog") {
|
|
22
|
+
runChild(detectBun() ?? "node", [join(root, "dist", "openqq-watchdog.js"), ...argv.slice(1)])
|
|
23
|
+
}
|
|
24
|
+
if (argv[0] === "restart" || argv[0] === "service") {
|
|
25
|
+
runChild("node", [join(root, "dist", "openqq-cli.js"), ...argv])
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
// notify 子命令分发(不改变无参数行为):CLI 只入队 ~openqq/outbox.json,
|
|
16
29
|
// 发送由 bot 进程 notify-sink 消费;CLI 不联网不取 token 不抢锁。
|
|
17
30
|
const argv = process.argv.slice(2)
|
package/dist/notify-cli.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
// src/cli/notify.ts
|
|
2
2
|
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "node:fs";
|
|
3
3
|
|
|
4
|
+
// src/core/storage.ts
|
|
5
|
+
import { homedir } from "os";
|
|
6
|
+
import { join } from "path";
|
|
7
|
+
var LEGACY_ROOT = join(homedir(), ".openqq");
|
|
8
|
+
function storageRoot(appId) {
|
|
9
|
+
const base = (process.env.QQ_DATA_DIR ?? "").trim() || LEGACY_ROOT;
|
|
10
|
+
return appId ? join(base, "roles", appId) : base;
|
|
11
|
+
}
|
|
12
|
+
function storageFile(name, appId) {
|
|
13
|
+
return join(storageRoot(appId), name);
|
|
14
|
+
}
|
|
15
|
+
|
|
4
16
|
// src/core/outbox.ts
|
|
5
17
|
import {
|
|
6
18
|
closeSync,
|
|
@@ -13,8 +25,7 @@ import {
|
|
|
13
25
|
unlinkSync,
|
|
14
26
|
writeFileSync
|
|
15
27
|
} from "node:fs";
|
|
16
|
-
import {
|
|
17
|
-
import { dirname, join } from "node:path";
|
|
28
|
+
import { dirname } from "node:path";
|
|
18
29
|
import { randomUUID } from "node:crypto";
|
|
19
30
|
var OUTBOX_SENT_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
20
31
|
var OUTBOX_SENT_RETENTION_LIMIT = 20;
|
|
@@ -81,7 +92,7 @@ function pruneOutboxEntries(entries, now = Date.now()) {
|
|
|
81
92
|
return indexed.filter(({ entry, index }) => entry.state !== "sent" || retainedSent.has(index)).sort((a, b) => a.entry.createdAt - b.entry.createdAt || a.index - b.index).map(({ entry }) => entry);
|
|
82
93
|
}
|
|
83
94
|
function createOutbox(options = {}) {
|
|
84
|
-
const filePath = options.filePath ?? process.env.QQ_OUTBOX_FILE ??
|
|
95
|
+
const filePath = options.filePath ?? process.env.QQ_OUTBOX_FILE ?? storageFile("outbox.json");
|
|
85
96
|
const pollIntervalMs = nonNegative(options.pollIntervalMs, DEFAULT_POLL_INTERVAL_MS, "pollIntervalMs");
|
|
86
97
|
const retryBaseMs = nonNegative(options.retryBaseMs, OUTBOX_DEFAULT_RETRY_BASE_MS, "retryBaseMs");
|
|
87
98
|
const retryMaxMs = nonNegative(options.retryMaxMs, OUTBOX_DEFAULT_RETRY_MAX_MS, "retryMaxMs");
|
|
@@ -377,16 +388,16 @@ function createOutbox(options = {}) {
|
|
|
377
388
|
async start(startOptions = {}) {
|
|
378
389
|
stopped = false;
|
|
379
390
|
if (startOptions.recover !== false)
|
|
380
|
-
await recover();
|
|
391
|
+
await recover({ maxAttempts: startOptions.maxAttempts });
|
|
381
392
|
if (pollIntervalMs === 0 || timer)
|
|
382
393
|
return;
|
|
383
394
|
timer = setInterval(() => {
|
|
384
395
|
if (stopped)
|
|
385
396
|
return;
|
|
386
|
-
flush().catch(() => {});
|
|
397
|
+
flush({ maxAttempts: startOptions.maxAttempts }).catch(() => {});
|
|
387
398
|
}, pollIntervalMs);
|
|
388
399
|
timer.unref?.();
|
|
389
|
-
flush().catch(() => {});
|
|
400
|
+
flush({ maxAttempts: startOptions.maxAttempts }).catch(() => {});
|
|
390
401
|
},
|
|
391
402
|
stop() {
|
|
392
403
|
stopped = true;
|
|
@@ -430,8 +441,6 @@ function loadEnvFile(path) {
|
|
|
430
441
|
}
|
|
431
442
|
|
|
432
443
|
// src/core/notify-sink.ts
|
|
433
|
-
import { homedir as homedir3 } from "node:os";
|
|
434
|
-
import { join as join3 } from "node:path";
|
|
435
444
|
function resolveNotifyRecipients(configured, explicit) {
|
|
436
445
|
const users = [...configured];
|
|
437
446
|
if (explicit)
|
|
@@ -439,7 +448,7 @@ function resolveNotifyRecipients(configured, explicit) {
|
|
|
439
448
|
return users.length === 1 ? users[0] : "";
|
|
440
449
|
}
|
|
441
450
|
function defaultOutboxPath() {
|
|
442
|
-
return
|
|
451
|
+
return storageFile("outbox.json");
|
|
443
452
|
}
|
|
444
453
|
|
|
445
454
|
// src/cli/notify.ts
|
|
@@ -531,14 +540,14 @@ async function runNotifyCli(argv, options = {}) {
|
|
|
531
540
|
].join(`
|
|
532
541
|
`);
|
|
533
542
|
console.log(help);
|
|
534
|
-
return { ok: true, queued: false, id: "", user: "", chars: 0, truncated: false, dryRun: true, reason: "help" };
|
|
543
|
+
return { ok: true, queued: false, id: "", user: "", chars: 0, truncated: false, dryRun: true, json: parsed.json, reason: "help" };
|
|
535
544
|
}
|
|
536
545
|
if (!options.env && existsSync3(ENV_FILE))
|
|
537
546
|
loadEnvFile(ENV_FILE);
|
|
538
547
|
const allowedUsers = options.env ? [...options.env.allowedUsers] : String(process.env.ALLOWED_USERS ?? "").split(/[,\s]+/).filter(Boolean);
|
|
539
548
|
const user = resolveUser(allowedUsers, parsed.user);
|
|
540
549
|
if (!user) {
|
|
541
|
-
throw new Error(allowedUsers.length === 0 ? "ALLOWED_USERS 为空——fail-closed 不允许无名单入队(先在 ~/.openqq/.env 配置)" : "多用户名单必须用 --user <openid> 显式指定收件人");
|
|
550
|
+
throw new Error(allowedUsers.length === 0 ? "ALLOWED_USERS 为空——fail-closed 不允许无名单入队(先在 ~/.openqq/.env 配置)" : parsed.user ? `收件人不在名单:${parsed.user}(ALLOWED_USERS 共 ${allowedUsers.length} 项,--user 必须精确命中;请核对 openid)` : "多用户名单必须用 --user <openid> 显式指定收件人");
|
|
542
551
|
}
|
|
543
552
|
const raw = readBody(parsed);
|
|
544
553
|
const max = parsed.maxChars;
|
|
@@ -546,7 +555,7 @@ async function runNotifyCli(argv, options = {}) {
|
|
|
546
555
|
const id = parsed.id || `notify-${now.toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
|
|
547
556
|
const filePath = options.filePath ?? defaultOutboxPath();
|
|
548
557
|
if (parsed.dryRun) {
|
|
549
|
-
return { ok: true, queued: false, id, user, chars: content.length, truncated: content.length !== raw.length, dryRun: true };
|
|
558
|
+
return { ok: true, queued: false, id, user, chars: content.length, truncated: content.length !== raw.length, dryRun: true, json: parsed.json };
|
|
550
559
|
}
|
|
551
560
|
const ttlHours = parsed.ttlHours ?? Number(process.env.QQ_NOTIFY_TTL_HOURS ?? 24);
|
|
552
561
|
const nextAttemptAt = parsed.ttlHours !== undefined && Number.isFinite(ttlHours) && ttlHours > 0 ? now + ttlHours * 60 * 60 * 1000 : now;
|
|
@@ -559,15 +568,20 @@ async function runNotifyCli(argv, options = {}) {
|
|
|
559
568
|
sessionId: "",
|
|
560
569
|
nextAttemptAt
|
|
561
570
|
});
|
|
562
|
-
return { ok: true, queued: true, id: entry.id, user, chars: content.length, truncated: content.length !== raw.length, dryRun: false };
|
|
571
|
+
return { ok: true, queued: true, id: entry.id, user, chars: content.length, truncated: content.length !== raw.length, dryRun: false, json: parsed.json };
|
|
563
572
|
}
|
|
564
573
|
async function main(argv) {
|
|
565
574
|
try {
|
|
566
575
|
const result = await runNotifyCli(argv);
|
|
567
576
|
if (result.reason === "help")
|
|
568
577
|
return 0;
|
|
569
|
-
if (result.
|
|
578
|
+
if (result.json) {
|
|
579
|
+
console.log(JSON.stringify(result));
|
|
580
|
+
} else if (result.queued && !result.dryRun) {
|
|
570
581
|
console.log(`[notify] 已入队 id=${result.id} user=${result.user} chars=${result.chars}${result.truncated ? "(已截断)" : ""}`);
|
|
582
|
+
} else if (result.dryRun) {
|
|
583
|
+
console.log(`[notify] dry-run:将入队 user=${result.user} chars=${result.chars}${result.truncated ? "(已截断)" : ""}`);
|
|
584
|
+
}
|
|
571
585
|
return result.ok ? 0 : 1;
|
|
572
586
|
} catch (error) {
|
|
573
587
|
console.error(`[notify] 入队失败:${error instanceof Error ? error.message : String(error)}`);
|