@linjz/opencode-qqbot 0.3.2 → 0.3.3
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 +22 -3
- package/bin/openqq-setup.mjs +50 -0
- package/dist/opencode-qqbot-plugin.js +172 -0
- package/dist/openqq-bot.js +7190 -0
- package/package.json +19 -4
- package/src/bridge.ts +74 -9
- package/src/core/bot-launcher.ts +105 -0
- package/src/core/context-buffer.ts +26 -0
- package/src/core/delivery.ts +21 -0
- package/src/core/inflight-journal.ts +149 -0
- package/src/core/instance-lock.ts +114 -0
- package/src/index.ts +23 -1
- package/src/plugin-entry.ts +99 -0
- package/src/qq/api.ts +46 -31
- package/src/qq/gateway.ts +6 -0
- package/src/qq/types.ts +6 -0
package/README.md
CHANGED
|
@@ -37,10 +37,19 @@
|
|
|
37
37
|
### 安装
|
|
38
38
|
|
|
39
39
|
```bash
|
|
40
|
-
#
|
|
40
|
+
# 方式零:opencode 插件形态(最省事)——在 opencode.json(c) 的 plugins 数组加入:
|
|
41
|
+
# "@linjz/opencode-qqbot"
|
|
42
|
+
# 或本机直装(自包含目录,官方插件规范):
|
|
43
|
+
# bun run deploy:plugin → ~/.config/opencode/plugins/opencode-qqbot/(index.js+bot.js+package.json)
|
|
44
|
+
# serve 服务自动加载;插件每 5 分钟巡检 bot 存活,不在就以独立进程拉起(bot 不受
|
|
45
|
+
# serve 重载/重启影响,拉起的是插件同目录的 bot.js,不依赖任何外部路径)。
|
|
46
|
+
# 凭据缺失时插件只在 opencode 日志里提示,不会乱拉。
|
|
47
|
+
# 注意:此形态要求 PATH 里有 bun 或 node;bot 的开机自启依赖 serve 服务自启。
|
|
48
|
+
|
|
49
|
+
# 方式一:从 GitHub 直装(不依赖 npm registry,推荐独立部署)
|
|
41
50
|
npm i -g github:linjz3376/opencode-qqbot
|
|
42
51
|
|
|
43
|
-
# 方式二:npm registry
|
|
52
|
+
# 方式二:npm registry
|
|
44
53
|
npm install -g @linjz/opencode-qqbot
|
|
45
54
|
|
|
46
55
|
# 方式三:源码
|
|
@@ -50,7 +59,13 @@ cd opencode-qqbot && bun install
|
|
|
50
59
|
|
|
51
60
|
### 配置(`~/.openqq/.env`,单点密钥)
|
|
52
61
|
|
|
53
|
-
|
|
62
|
+
首次运行缺凭据会交互式引导写入;也可用手机 QQ 扫码绑定(需先 `npm i -g @tencent-connect/qqbot-connector`):
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
openqq-setup # 终端弹二维码,扫码后凭据自动写入 ~/.openqq/.env
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
或手工创建:
|
|
54
69
|
|
|
55
70
|
```ini
|
|
56
71
|
QQ_APP_ID=你的AppID
|
|
@@ -71,6 +86,8 @@ openqq # npm 全局安装后
|
|
|
71
86
|
|
|
72
87
|
Windows 常驻:`deploy/` 目录内有任务计划(开机自启、看门狗心跳守护)脚本与说明。
|
|
73
88
|
|
|
89
|
+
> **单实例**:bot 启动时持文件锁(`~/.openqq/openqq-<appId>.lock`,10s 心跳、30s 僵尸判定)——双开时后到实例待命,宿主退出后自动接管,避免同 bot 多实例被 QQ 网关互踢。`QQ_INSTANCE_LOCK=false` 可关闭。
|
|
90
|
+
|
|
74
91
|
> **跨平台**:bot 本体零 OS 分支(纯 TS/Bun + HTTP/WS,与 QQ 官方 SDK 同形态)。Linux/macOS 常驻用 `systemd --user` 或 `nohup bun run src/index.ts &` 即可;端口自动发现在三个系统都可用(Linux 走 `ss -tln`,macOS 走 `netstat -an -p tcp`,Windows 走 `netstat -ano -p tcp`)。
|
|
75
92
|
|
|
76
93
|
## 指令一览
|
|
@@ -98,6 +115,8 @@ Windows 常驻:`deploy/` 目录内有任务计划(开机自启、看门狗
|
|
|
98
115
|
| `QQ_STREAM_STAGGER_MS` | 300 | 多片回复片间错峰(0=一次性全发) |
|
|
99
116
|
| `QQ_SESSION_IDLE_HOURS` | 6 | 闲置会话回收阈值 |
|
|
100
117
|
| `QQ_INTERACTION_WATCH_SECONDS` | 12 | 常驻交互监听轮询间隔(0=关闭) |
|
|
118
|
+
| `QQ_REDISPATCH_MAX_AGE_MS` | 7200000 | 启动对账时丢失消息的自动重派窗口(0=关闭自动重派只通知;受台账 24h 清账线封顶) |
|
|
119
|
+
| `QQ_EVENT_PROBE` | 关 | 插件侧事件探针(`true` 时全量事件类型落 `~/.openqq/opencode-events.jsonl`,诊断用) |
|
|
101
120
|
| `OPENCODE_TURN_TIMEOUT_MINUTES` | 30 | 前台回合时限(超时转后台等待) |
|
|
102
121
|
| `QQ_DIGEST_ANSWER_MAX_CHARS` | 0(全文) | 摘要卡答案截断上限 |
|
|
103
122
|
| `QQ_HISTORY_USER_CLIP` / `QQ_HISTORY_TEXT_CLIP` | 500 / 1200 | /history 用户/AI 行截断 |
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// F 项(2026-10-30,吸收竞品 opencode-qq bin/setup.mjs 的零依赖思路):
|
|
3
|
+
// 扫码绑定引导——动态 import 官方 @tencent-connect/qqbot-connector(不进
|
|
4
|
+
// dependencies,守红线:官方包只做参考/用户自选安装),手机 QQ 扫码拿到
|
|
5
|
+
// appId/appSecret 后合并写入 ~/.openqq/.env(保留其余既有配置行)。
|
|
6
|
+
import fs from "node:fs"
|
|
7
|
+
import path from "node:path"
|
|
8
|
+
import os from "node:os"
|
|
9
|
+
|
|
10
|
+
const CONFIG_DIR = path.join(os.homedir(), ".openqq")
|
|
11
|
+
const ENV_FILE = path.join(CONFIG_DIR, ".env")
|
|
12
|
+
|
|
13
|
+
let connector
|
|
14
|
+
try {
|
|
15
|
+
connector = await import("@tencent-connect/qqbot-connector")
|
|
16
|
+
} catch {
|
|
17
|
+
console.error("缺少依赖 @tencent-connect/qqbot-connector")
|
|
18
|
+
console.error("请先执行: npm install -g @tencent-connect/qqbot-connector")
|
|
19
|
+
process.exit(1)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
console.log("请使用手机 QQ 扫描终端二维码完成绑定…")
|
|
23
|
+
const credsList = await connector.qrConnect({ source: "opencode-qqbot" })
|
|
24
|
+
const creds = credsList?.[0]
|
|
25
|
+
if (!creds?.appId || !creds?.appSecret) {
|
|
26
|
+
console.error("扫码未返回凭据")
|
|
27
|
+
process.exit(1)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 合并写入 .env:替换已有 QQ_APP_ID/QQ_APP_SECRET 行,其余行原样保留
|
|
31
|
+
let lines = []
|
|
32
|
+
try {
|
|
33
|
+
lines = fs.readFileSync(ENV_FILE, "utf8").split("\n")
|
|
34
|
+
} catch {
|
|
35
|
+
/* 首次运行无文件 */
|
|
36
|
+
}
|
|
37
|
+
const seen = new Set()
|
|
38
|
+
const merged = lines.map((line) => {
|
|
39
|
+
const m = /^\s*(QQ_APP_ID|QQ_APP_SECRET)\s*=/.exec(line)
|
|
40
|
+
if (!m) return line
|
|
41
|
+
seen.add(m[1])
|
|
42
|
+
return m[1] === "QQ_APP_ID" ? `QQ_APP_ID=${creds.appId}` : `QQ_APP_SECRET=${creds.appSecret}`
|
|
43
|
+
})
|
|
44
|
+
if (!seen.has("QQ_APP_ID")) merged.push(`QQ_APP_ID=${creds.appId}`)
|
|
45
|
+
if (!seen.has("QQ_APP_SECRET")) merged.push(`QQ_APP_SECRET=${creds.appSecret}`)
|
|
46
|
+
|
|
47
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true })
|
|
48
|
+
fs.writeFileSync(ENV_FILE, merged.join("\n"))
|
|
49
|
+
console.log(`凭据已写入 ${ENV_FILE}`)
|
|
50
|
+
console.log("现在可以运行 openqq 启动 bot 了。")
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/plugin-entry.ts
|
|
3
|
+
import { Plugin } from "@opencode/plugin";
|
|
4
|
+
import { join as join3 } from "path";
|
|
5
|
+
import { homedir as homedir3 } from "os";
|
|
6
|
+
import { appendFileSync, existsSync as existsSync2, renameSync, statSync as statSync2 } from "fs";
|
|
7
|
+
import { fileURLToPath } from "url";
|
|
8
|
+
|
|
9
|
+
// src/core/instance-lock.ts
|
|
10
|
+
import { openSync, writeFileSync, closeSync, unlinkSync, statSync, utimesSync, mkdirSync } from "fs";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
import { homedir } from "os";
|
|
13
|
+
var LOCK_STALE_MS = 30000;
|
|
14
|
+
function lockPathOf(appId) {
|
|
15
|
+
return join(homedir(), ".openqq", `openqq-${appId}.lock`);
|
|
16
|
+
}
|
|
17
|
+
function isInstanceAlive(appId, staleMs = LOCK_STALE_MS) {
|
|
18
|
+
return isInstanceAliveAt(lockPathOf(appId), staleMs);
|
|
19
|
+
}
|
|
20
|
+
function isInstanceAliveAt(path, staleMs = LOCK_STALE_MS) {
|
|
21
|
+
try {
|
|
22
|
+
return Date.now() - statSync(path).mtimeMs <= staleMs;
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/core/bot-launcher.ts
|
|
29
|
+
import { spawn, spawnSync } from "child_process";
|
|
30
|
+
import { existsSync, mkdirSync as mkdirSync2, openSync as openSync2, readFileSync } from "fs";
|
|
31
|
+
import { join as join2, dirname } from "path";
|
|
32
|
+
import { homedir as homedir2 } from "os";
|
|
33
|
+
function resolveRuntime(probe = defaultProbe) {
|
|
34
|
+
if (probe("bun"))
|
|
35
|
+
return "bun";
|
|
36
|
+
if (probe("node"))
|
|
37
|
+
return "node";
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function defaultProbe(cmd) {
|
|
41
|
+
try {
|
|
42
|
+
const r = spawnSync(cmd, ["--version"], { stdio: "ignore" });
|
|
43
|
+
return r.status === 0;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function resolveBotEntry(pluginFilePath, exists = existsSync) {
|
|
49
|
+
const dir = dirname(pluginFilePath);
|
|
50
|
+
const candidates = [
|
|
51
|
+
join2(dir, "bot.js"),
|
|
52
|
+
join2(homedir2(), ".openqq", "plugin-payload", "bot.js"),
|
|
53
|
+
join2(dir, "..", "dist", "openqq-bot.js"),
|
|
54
|
+
join2(dir, "..", "src", "index.ts")
|
|
55
|
+
];
|
|
56
|
+
for (const c of candidates)
|
|
57
|
+
if (exists(c))
|
|
58
|
+
return c;
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
function buildLaunchPlan(runtime, entry) {
|
|
62
|
+
const env = { ...process.env, QQ_INSTANCE_LOCK: "try" };
|
|
63
|
+
if (runtime === "bun")
|
|
64
|
+
return { command: "bun", args: ["run", entry], env };
|
|
65
|
+
return { command: "node", args: [entry], env };
|
|
66
|
+
}
|
|
67
|
+
function botLogPath() {
|
|
68
|
+
return join2(homedir2(), ".openqq", "bot.out.log");
|
|
69
|
+
}
|
|
70
|
+
function spawnBotDetached(plan, logPath = botLogPath()) {
|
|
71
|
+
mkdirSync2(join2(logPath, ".."), { recursive: true });
|
|
72
|
+
const out = openSync2(logPath, "a");
|
|
73
|
+
const child = spawn(plan.command, plan.args, {
|
|
74
|
+
detached: true,
|
|
75
|
+
stdio: ["ignore", out, out],
|
|
76
|
+
env: plan.env,
|
|
77
|
+
windowsHide: true
|
|
78
|
+
});
|
|
79
|
+
child.unref();
|
|
80
|
+
if (typeof child.pid !== "number")
|
|
81
|
+
throw new Error("spawn \u672A\u8FD4\u56DE pid");
|
|
82
|
+
return child.pid;
|
|
83
|
+
}
|
|
84
|
+
function readDotEnvField(envPath, key) {
|
|
85
|
+
try {
|
|
86
|
+
if (!existsSync(envPath))
|
|
87
|
+
return null;
|
|
88
|
+
for (const line of readFileSync(envPath, "utf-8").split(`
|
|
89
|
+
`)) {
|
|
90
|
+
const trimmed = line.trim();
|
|
91
|
+
if (!trimmed || trimmed.startsWith("#"))
|
|
92
|
+
continue;
|
|
93
|
+
const eq = trimmed.indexOf("=");
|
|
94
|
+
if (eq < 0)
|
|
95
|
+
continue;
|
|
96
|
+
if (trimmed.slice(0, eq).trim() === key)
|
|
97
|
+
return trimmed.slice(eq + 1).trim();
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/plugin-entry.ts
|
|
106
|
+
var ENV_FILE = join3(homedir3(), ".openqq", ".env");
|
|
107
|
+
var CHECK_INTERVAL_MS = 5 * 60 * 1000;
|
|
108
|
+
var PROBE_FILE = join3(homedir3(), ".openqq", "opencode-events.jsonl");
|
|
109
|
+
var PROBE_ROTATE_BYTES = 5 * 1024 * 1024;
|
|
110
|
+
function probeAppend(line) {
|
|
111
|
+
try {
|
|
112
|
+
if (existsSync2(PROBE_FILE) && statSync2(PROBE_FILE).size > PROBE_ROTATE_BYTES) {
|
|
113
|
+
renameSync(PROBE_FILE, PROBE_FILE + ".1");
|
|
114
|
+
}
|
|
115
|
+
appendFileSync(PROBE_FILE, line + `
|
|
116
|
+
`);
|
|
117
|
+
} catch {}
|
|
118
|
+
}
|
|
119
|
+
var plugin_entry_default = Plugin.define({
|
|
120
|
+
id: "opencode-qqbot",
|
|
121
|
+
setup(_ctx) {
|
|
122
|
+
const ensure = () => {
|
|
123
|
+
try {
|
|
124
|
+
const appId = readDotEnvField(ENV_FILE, "QQ_APP_ID");
|
|
125
|
+
if (!appId) {
|
|
126
|
+
console.warn("[opencode-qqbot] \u672A\u53D1\u73B0 ~/.openqq/.env \u51ED\u636E\u2014\u2014\u5148\u8FD0\u884C npx @linjz/opencode-qqbot setup \u626B\u7801\u7ED1\u5B9A\uFF08\u6216\u53C2\u8003 README \u624B\u5199 .env\uFF09\uFF0C\u5B8C\u6210\u540E\u63D2\u4EF6\u5C06\u5728\u4E0B\u6B21\u5DE1\u68C0\u81EA\u52A8\u62C9\u8D77 bot");
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (isInstanceAlive(appId))
|
|
130
|
+
return;
|
|
131
|
+
const entry = resolveBotEntry(fileURLToPath(import.meta.url));
|
|
132
|
+
if (!entry) {
|
|
133
|
+
console.warn("[opencode-qqbot] \u672A\u627E\u5230 bot \u5165\u53E3\uFF08bot.js / dist/openqq-bot.js / src/index.ts \u5168\u7F3A\uFF09\u2014\u2014\u63D2\u4EF6\u5B89\u88C5\u4E0D\u5B8C\u6574");
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const runtime = resolveRuntime();
|
|
137
|
+
if (!runtime) {
|
|
138
|
+
console.warn("[opencode-qqbot] \u672A\u627E\u5230 bun/node \u8FD0\u884C\u65F6\uFF0C\u65E0\u6CD5\u62C9\u8D77 bot\u2014\u2014\u8BF7\u5B89\u88C5\u5176\u4E2D\u4E4B\u4E00\uFF08https://bun.sh\uFF09");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const pid = spawnBotDetached(buildLaunchPlan(runtime, entry));
|
|
142
|
+
console.log(`[opencode-qqbot] bot \u5DF2\u62C9\u8D77\uFF08pid=${pid}\uFF0Cruntime=${runtime}\uFF0Centry=${entry}\uFF09`);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
console.error("[opencode-qqbot] \u62C9\u8D77\u5931\u8D25:", error instanceof Error ? error.message : error);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
ensure();
|
|
148
|
+
const timer = setInterval(ensure, CHECK_INTERVAL_MS);
|
|
149
|
+
let probeAbort = null;
|
|
150
|
+
if (readDotEnvField(ENV_FILE, "QQ_EVENT_PROBE") === "true") {
|
|
151
|
+
probeAbort = new AbortController;
|
|
152
|
+
const signal = probeAbort.signal;
|
|
153
|
+
(async () => {
|
|
154
|
+
probeAppend(JSON.stringify({ ts: new Date().toISOString(), type: "probe.started", sessionID: null }));
|
|
155
|
+
try {
|
|
156
|
+
for await (const event of _ctx.event.subscribe({ signal })) {
|
|
157
|
+
const ev = event;
|
|
158
|
+
probeAppend(JSON.stringify({ ts: new Date().toISOString(), type: ev.type, sessionID: ev.data?.sessionID ?? null }));
|
|
159
|
+
}
|
|
160
|
+
} catch {}
|
|
161
|
+
})();
|
|
162
|
+
console.log("[opencode-qqbot] \u4E8B\u4EF6\u63A2\u9488\u5DF2\u5F00\u542F\uFF08QQ_EVENT_PROBE=true\uFF09");
|
|
163
|
+
}
|
|
164
|
+
return () => {
|
|
165
|
+
clearInterval(timer);
|
|
166
|
+
probeAbort?.abort();
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
export {
|
|
171
|
+
plugin_entry_default as default
|
|
172
|
+
};
|