@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/dist/openqq-cli.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// src/cli/openqq-main.ts
|
|
2
|
-
import { existsSync as
|
|
3
|
-
import { dirname as
|
|
2
|
+
import { existsSync as existsSync6 } from "fs";
|
|
3
|
+
import { dirname as dirname4, join as join6 } from "path";
|
|
4
4
|
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
5
5
|
|
|
6
6
|
// src/core/bot-control.ts
|
|
7
|
-
import { existsSync as
|
|
8
|
-
import { dirname as
|
|
7
|
+
import { existsSync as existsSync4, readFileSync as readFileSync4 } from "fs";
|
|
8
|
+
import { dirname as dirname3, join as join4 } from "path";
|
|
9
9
|
import { homedir as homedir3 } from "os";
|
|
10
10
|
import { fileURLToPath } from "url";
|
|
11
11
|
|
|
@@ -140,7 +140,7 @@ function buildLaunchPlan(runtime, entry, logPath) {
|
|
|
140
140
|
return { command: resolveRuntimeCommand("node"), args: [entry], env };
|
|
141
141
|
}
|
|
142
142
|
function botLogPath() {
|
|
143
|
-
return join(homedir(), ".openqq", "
|
|
143
|
+
return join(homedir(), ".openqq", "openqq.log");
|
|
144
144
|
}
|
|
145
145
|
function spawnBotDetached(plan) {
|
|
146
146
|
const child = spawn(plan.command, plan.args, {
|
|
@@ -157,13 +157,104 @@ function spawnBotDetached(plan) {
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
// src/core/watchdog.ts
|
|
160
|
-
import { readFileSync as
|
|
161
|
-
import { join as
|
|
160
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3, statSync as statSync4, unlinkSync as unlinkSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
161
|
+
import { dirname as dirname2, join as join3 } from "path";
|
|
162
162
|
import { homedir as homedir2 } from "os";
|
|
163
|
+
|
|
164
|
+
// src/core/log-rotate.ts
|
|
165
|
+
import { existsSync as existsSync2, renameSync, rmSync, statSync as statSync2 } from "fs";
|
|
166
|
+
var MB = 1024 * 1024;
|
|
167
|
+
var num = (v, d) => {
|
|
168
|
+
const n = Number(v);
|
|
169
|
+
return Number.isFinite(n) && n > 0 ? n : d;
|
|
170
|
+
};
|
|
171
|
+
function logLimitsFromEnv(env = process.env) {
|
|
172
|
+
return {
|
|
173
|
+
maxBytes: num(env.QQ_LOG_MAX_MB, 20) * MB,
|
|
174
|
+
keepBackups: Math.floor(num(env.QQ_LOG_KEEP, 4)),
|
|
175
|
+
totalBudgetBytes: num(env.QQ_LOG_TOTAL_MB, 100) * MB
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function rotateLogIfNeeded(logPath, limits = logLimitsFromEnv()) {
|
|
179
|
+
try {
|
|
180
|
+
if (!existsSync2(logPath) || statSync2(logPath).size <= limits.maxBytes)
|
|
181
|
+
return false;
|
|
182
|
+
try {
|
|
183
|
+
rmSync(`${logPath}.${limits.keepBackups}`, { force: true });
|
|
184
|
+
} catch {}
|
|
185
|
+
for (let i = limits.keepBackups - 1;i >= 1; i--) {
|
|
186
|
+
const from = `${logPath}.${i}`;
|
|
187
|
+
if (!existsSync2(from))
|
|
188
|
+
continue;
|
|
189
|
+
try {
|
|
190
|
+
renameSync(from, `${logPath}.${i + 1}`);
|
|
191
|
+
} catch {}
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
renameSync(logPath, `${logPath}.1`);
|
|
195
|
+
} catch {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return true;
|
|
199
|
+
} catch {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
function enforceLogBudget(logPath, limits = logLimitsFromEnv()) {
|
|
204
|
+
try {
|
|
205
|
+
const sizes = new Map;
|
|
206
|
+
let total = 0;
|
|
207
|
+
for (let i = 0;i <= 64; i++) {
|
|
208
|
+
const p = i === 0 ? logPath : `${logPath}.${i}`;
|
|
209
|
+
if (!existsSync2(p))
|
|
210
|
+
break;
|
|
211
|
+
const s = statSync2(p).size;
|
|
212
|
+
sizes.set(i, s);
|
|
213
|
+
total += s;
|
|
214
|
+
}
|
|
215
|
+
for (let i = limits.keepBackups;i >= 1 && total > limits.totalBudgetBytes; i--) {
|
|
216
|
+
const s = sizes.get(i);
|
|
217
|
+
if (s === undefined)
|
|
218
|
+
continue;
|
|
219
|
+
try {
|
|
220
|
+
rmSync(`${logPath}.${i}`, { force: true });
|
|
221
|
+
total -= s;
|
|
222
|
+
} catch {}
|
|
223
|
+
}
|
|
224
|
+
} catch {}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/core/instance-lock.ts
|
|
228
|
+
import { openSync, readFileSync as readFileSync2, readdirSync, writeFileSync, closeSync, unlinkSync, statSync as statSync3, utimesSync, mkdirSync } from "fs";
|
|
229
|
+
import { join as join2 } from "path";
|
|
230
|
+
function releaseLocksOfPid(pid, dir) {
|
|
231
|
+
let names;
|
|
232
|
+
try {
|
|
233
|
+
names = readdirSync(dir);
|
|
234
|
+
} catch {
|
|
235
|
+
return [];
|
|
236
|
+
}
|
|
237
|
+
const cleared = [];
|
|
238
|
+
for (const name of names) {
|
|
239
|
+
if (!/^openqq-.+\.lock$/.test(name))
|
|
240
|
+
continue;
|
|
241
|
+
const p = join2(dir, name);
|
|
242
|
+
try {
|
|
243
|
+
const holder = JSON.parse(readFileSync2(p, "utf-8"));
|
|
244
|
+
if (holder.pid === pid) {
|
|
245
|
+
unlinkSync(p);
|
|
246
|
+
cleared.push(p);
|
|
247
|
+
}
|
|
248
|
+
} catch {}
|
|
249
|
+
}
|
|
250
|
+
return cleared;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/core/watchdog.ts
|
|
163
254
|
var HEARTBEAT_STALE_MS = 5 * 60000;
|
|
164
255
|
function botWatchdogPaths(appId) {
|
|
165
|
-
const dir =
|
|
166
|
-
return { lockPath:
|
|
256
|
+
const dir = join3(homedir2(), ".openqq");
|
|
257
|
+
return { lockPath: join3(dir, `openqq-${appId}.lock`), heartbeatPath: join3(dir, "heartbeat") };
|
|
167
258
|
}
|
|
168
259
|
function isPidAlive(pid) {
|
|
169
260
|
if (!Number.isInteger(pid) || pid <= 0)
|
|
@@ -177,7 +268,7 @@ function isPidAlive(pid) {
|
|
|
177
268
|
}
|
|
178
269
|
function ageOf(path, now) {
|
|
179
270
|
try {
|
|
180
|
-
return Math.max(0, now -
|
|
271
|
+
return Math.max(0, now - statSync4(path).mtimeMs);
|
|
181
272
|
} catch {
|
|
182
273
|
return null;
|
|
183
274
|
}
|
|
@@ -187,7 +278,7 @@ function readBotHealth(paths, now = Date.now()) {
|
|
|
187
278
|
let lockPid = null;
|
|
188
279
|
if (lockAgeMs !== null) {
|
|
189
280
|
try {
|
|
190
|
-
const raw = JSON.parse(
|
|
281
|
+
const raw = JSON.parse(readFileSync3(paths.lockPath, "utf-8"));
|
|
191
282
|
if (typeof raw.pid === "number" && Number.isInteger(raw.pid))
|
|
192
283
|
lockPid = raw.pid;
|
|
193
284
|
} catch {}
|
|
@@ -201,11 +292,27 @@ function readBotHealth(paths, now = Date.now()) {
|
|
|
201
292
|
}
|
|
202
293
|
function appendWatchdogLog(line) {
|
|
203
294
|
try {
|
|
204
|
-
const
|
|
205
|
-
|
|
295
|
+
const logPath = join3(homedir2(), ".openqq", "openqq.log");
|
|
296
|
+
const limits = logLimitsFromEnv();
|
|
297
|
+
if (existsSync3(logPath) && statSync4(logPath).size > limits.maxBytes) {
|
|
298
|
+
if (rotateLogIfNeeded(logPath, limits))
|
|
299
|
+
enforceLogBudget(logPath, limits);
|
|
300
|
+
}
|
|
301
|
+
writeFileSync2(logPath, `${new Date().toISOString()} ${line}
|
|
206
302
|
`, { flag: "a" });
|
|
207
303
|
} catch {}
|
|
208
304
|
}
|
|
305
|
+
function appendKillLog(entry, logPath = join3(homedir2(), ".openqq", "kills.log")) {
|
|
306
|
+
try {
|
|
307
|
+
writeFileSync2(logPath, `${JSON.stringify(entry)}
|
|
308
|
+
`, { flag: "a" });
|
|
309
|
+
const limits = logLimitsFromEnv();
|
|
310
|
+
if (existsSync3(logPath) && statSync4(logPath).size > limits.maxBytes) {
|
|
311
|
+
if (rotateLogIfNeeded(logPath, limits))
|
|
312
|
+
enforceLogBudget(logPath, limits);
|
|
313
|
+
}
|
|
314
|
+
} catch {}
|
|
315
|
+
}
|
|
209
316
|
var DEFAULT_SUPERVISOR_POLICY = {
|
|
210
317
|
cooldownMs: 2 * 60000,
|
|
211
318
|
maxRestarts: 3,
|
|
@@ -242,7 +349,7 @@ var defaultSupervisorKill = (pid, signal) => {
|
|
|
242
349
|
};
|
|
243
350
|
function readJson(path, fallback) {
|
|
244
351
|
try {
|
|
245
|
-
return { ...fallback, ...JSON.parse(
|
|
352
|
+
return { ...fallback, ...JSON.parse(readFileSync3(path, "utf-8")) };
|
|
246
353
|
} catch {
|
|
247
354
|
return fallback;
|
|
248
355
|
}
|
|
@@ -254,12 +361,12 @@ async function superviseOnce(deps) {
|
|
|
254
361
|
const sleep = deps.sleep ?? defaultSleep;
|
|
255
362
|
const kill = deps.kill ?? defaultSupervisorKill;
|
|
256
363
|
const isAlive = deps.isAlive ?? isPidAlive;
|
|
257
|
-
const statePath = deps.statePath ??
|
|
258
|
-
const claimPath = deps.claimPath ??
|
|
364
|
+
const statePath = deps.statePath ?? join3(homedir2(), ".openqq", "supervisor.json");
|
|
365
|
+
const claimPath = deps.claimPath ?? join3(homedir2(), ".openqq", "supervisor.lock");
|
|
259
366
|
const audit = (line) => {
|
|
260
367
|
if (deps.auditLogPath) {
|
|
261
368
|
try {
|
|
262
|
-
|
|
369
|
+
writeFileSync2(deps.auditLogPath, `${new Date().toISOString()} ${line}
|
|
263
370
|
`, { flag: "a" });
|
|
264
371
|
return;
|
|
265
372
|
} catch {}
|
|
@@ -271,13 +378,13 @@ async function superviseOnce(deps) {
|
|
|
271
378
|
return { action: "yield", detail: `监督者 pid=${claim.pid} 持锁中`, killedPid: null, spawnedPid: null };
|
|
272
379
|
}
|
|
273
380
|
try {
|
|
274
|
-
|
|
381
|
+
writeFileSync2(claimPath, JSON.stringify({ pid: process.pid, ts: now }));
|
|
275
382
|
} catch {
|
|
276
383
|
return { action: "yield", detail: "认领锁不可写", killedPid: null, spawnedPid: null };
|
|
277
384
|
}
|
|
278
385
|
const release = () => {
|
|
279
386
|
try {
|
|
280
|
-
|
|
387
|
+
writeFileSync2(claimPath, JSON.stringify({ pid: 0, ts: 0 }));
|
|
281
388
|
} catch {}
|
|
282
389
|
};
|
|
283
390
|
try {
|
|
@@ -311,15 +418,15 @@ async function superviseOnce(deps) {
|
|
|
311
418
|
return { action: "yield", detail: action.reason, killedPid: null, spawnedPid: null };
|
|
312
419
|
}
|
|
313
420
|
if (action.kind === "give-up") {
|
|
314
|
-
const message = `⚠️ QQBot
|
|
421
|
+
const message = `⚠️ QQBot 看门狗已达重启强度上限:${action.restarts} 次/${Math.round(policy.windowMs / 60000)} 分钟仍不健康(同因反复死亡)。降频重试继续——巡检不会停,强度窗口滑动后自动恢复,引擎/环境自愈即自行好转;若持续收到此告警,请检查 ~/.openqq/openqq.log(bot 与看门狗统一日志)。`;
|
|
315
422
|
audit(`give-up:${message}`);
|
|
316
|
-
log(`[supervisor] give-up:${action.restarts}
|
|
423
|
+
log(`[supervisor] give-up:${action.restarts} 次重启仍异常,降频重试继续并上报(不再永动猛拉)`);
|
|
317
424
|
const lastAlert = state.alertedAt ?? 0;
|
|
318
425
|
if (now - lastAlert > 30 * 60000) {
|
|
319
426
|
try {
|
|
320
427
|
deps.alert?.(message);
|
|
321
428
|
state.alertedAt = now;
|
|
322
|
-
|
|
429
|
+
writeFileSync2(statePath, JSON.stringify(state));
|
|
323
430
|
} catch {}
|
|
324
431
|
}
|
|
325
432
|
return { action: "give-up", detail: message, killedPid: null, spawnedPid: null };
|
|
@@ -360,61 +467,143 @@ async function superviseOnce(deps) {
|
|
|
360
467
|
killedPid = pid;
|
|
361
468
|
}
|
|
362
469
|
try {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
470
|
+
const locksDir = dirname2(deps.paths.lockPath);
|
|
471
|
+
let cleared = [];
|
|
472
|
+
if (killedPid !== null) {
|
|
473
|
+
cleared = releaseLocksOfPid(killedPid, locksDir);
|
|
474
|
+
} else if (health.pidAlive === false && health.lockPid !== null) {
|
|
475
|
+
cleared = releaseLocksOfPid(health.lockPid, locksDir);
|
|
476
|
+
}
|
|
477
|
+
if (existsSync3(deps.paths.lockPath)) {
|
|
478
|
+
try {
|
|
479
|
+
unlinkSync2(deps.paths.lockPath);
|
|
480
|
+
if (!cleared.includes(deps.paths.lockPath))
|
|
481
|
+
cleared.push(deps.paths.lockPath);
|
|
482
|
+
} catch {}
|
|
366
483
|
}
|
|
484
|
+
if (cleared.length > 0)
|
|
485
|
+
audit(`清锁:${cleared.join("、")}(${action.reason})`);
|
|
367
486
|
} catch {}
|
|
368
487
|
const spawnedPid = deps.spawn();
|
|
369
488
|
state.restartLog.push(Date.now());
|
|
370
489
|
state.restartLog = state.restartLog.filter((t) => t > Date.now() - policy.windowMs * 2);
|
|
371
490
|
try {
|
|
372
|
-
|
|
491
|
+
writeFileSync2(statePath, JSON.stringify(state));
|
|
373
492
|
} catch {
|
|
374
493
|
log("[supervisor] 强度账落盘失败(不影响本次拉起)");
|
|
375
494
|
}
|
|
376
|
-
|
|
495
|
+
let livenessVerdict = "alive";
|
|
496
|
+
let respawnPid = null;
|
|
497
|
+
if (deps.livenessCheck !== false) {
|
|
498
|
+
const graceWaitMs = 1e4;
|
|
499
|
+
await sleep(graceWaitMs);
|
|
500
|
+
const liveness = readBotHealth(deps.paths, Date.now());
|
|
501
|
+
if (!isPidAlive(spawnedPid) || liveness.lockPid === null) {
|
|
502
|
+
livenessVerdict = `dead-after-spawn(10s 复核:进程${isPidAlive(spawnedPid) ? "在但锁未建立" : "已退出"})`;
|
|
503
|
+
audit(`二次追击:拉起 pid=${spawnedPid} 失败(${livenessVerdict}),立即重拉`);
|
|
504
|
+
try {
|
|
505
|
+
const locksDir2 = dirname2(deps.paths.lockPath);
|
|
506
|
+
let cleared2 = [];
|
|
507
|
+
if (!isPidAlive(spawnedPid) && existsSync3(deps.paths.lockPath)) {
|
|
508
|
+
cleared2 = releaseLocksOfPid(spawnedPid, locksDir2);
|
|
509
|
+
}
|
|
510
|
+
if (existsSync3(deps.paths.lockPath)) {
|
|
511
|
+
try {
|
|
512
|
+
unlinkSync2(deps.paths.lockPath);
|
|
513
|
+
if (!cleared2.includes(deps.paths.lockPath))
|
|
514
|
+
cleared2.push(deps.paths.lockPath);
|
|
515
|
+
} catch {}
|
|
516
|
+
}
|
|
517
|
+
if (cleared2.length > 0)
|
|
518
|
+
audit(`清锁:${cleared2.join("、")}(二次追击前清场)`);
|
|
519
|
+
} catch {}
|
|
520
|
+
respawnPid = deps.spawn();
|
|
521
|
+
await sleep(graceWaitMs);
|
|
522
|
+
const liveness2 = readBotHealth(deps.paths, Date.now());
|
|
523
|
+
if (!isPidAlive(respawnPid) || liveness2.lockPid === null) {
|
|
524
|
+
livenessVerdict += `;二次拉起 pid=${respawnPid} 仍失败——交下轮巡检接力(强度账 ${state.restartLog.length}/${policy.maxRestarts})`;
|
|
525
|
+
} else {
|
|
526
|
+
livenessVerdict += `;二次拉起 pid=${respawnPid} 成功`;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
if (killedPid !== null || livenessVerdict !== "alive") {
|
|
531
|
+
appendKillLog({
|
|
532
|
+
kind: "decision",
|
|
533
|
+
reason: action.reason,
|
|
534
|
+
health: {
|
|
535
|
+
lockPid: health.lockPid,
|
|
536
|
+
pidAlive: health.pidAlive,
|
|
537
|
+
heartbeatAgeMs: health.heartbeatAgeMs,
|
|
538
|
+
lockAgeMs: health.lockAgeMs
|
|
539
|
+
},
|
|
540
|
+
killedPid,
|
|
541
|
+
killEscalation: killedPid !== null ? "TERM" : "n/a",
|
|
542
|
+
spawnedPid: respawnPid ?? spawnedPid,
|
|
543
|
+
restartIntensity: `${state.restartLog.length}/${policy.maxRestarts}`,
|
|
544
|
+
livenessVerdict: livenessVerdict !== "alive" ? livenessVerdict : "alive",
|
|
545
|
+
at: Date.now()
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
const detail = `${action.reason};${killedPid ? `已优雅停 pid=${killedPid};` : ""}已拉起 pid=${respawnPid ?? spawnedPid}${livenessVerdict !== "alive" ? `;${livenessVerdict}` : ""}`;
|
|
377
549
|
audit(killedPid ? `主动杀:${detail}` : `验尸拉新:${detail}`);
|
|
378
|
-
return { action: "restart", detail, killedPid, spawnedPid };
|
|
550
|
+
return { action: "restart", detail, killedPid, spawnedPid: respawnPid ?? spawnedPid };
|
|
379
551
|
} finally {
|
|
380
552
|
release();
|
|
381
553
|
}
|
|
382
554
|
}
|
|
383
555
|
|
|
384
556
|
// src/core/bot-control.ts
|
|
385
|
-
function resolveInstalledBotEntry(fromUrl, exists =
|
|
386
|
-
const here =
|
|
557
|
+
function resolveInstalledBotEntry(fromUrl, exists = existsSync4) {
|
|
558
|
+
const here = dirname3(fileURLToPath(fromUrl));
|
|
387
559
|
const candidates = [
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
560
|
+
join4(here, "openqq-bot.js"),
|
|
561
|
+
join4(here, "..", "dist", "openqq-bot.js"),
|
|
562
|
+
join4(here, "..", "src", "index.ts"),
|
|
563
|
+
join4(here, "..", "..", "src", "index.ts")
|
|
392
564
|
];
|
|
393
565
|
for (const c of candidates)
|
|
394
566
|
if (exists(c))
|
|
395
567
|
return c;
|
|
396
568
|
return null;
|
|
397
569
|
}
|
|
398
|
-
function readAppId(envPath =
|
|
570
|
+
function readAppId(envPath = join4(homedir3(), ".openqq", ".env")) {
|
|
571
|
+
let botsRaw = null;
|
|
399
572
|
try {
|
|
400
|
-
for (const line of
|
|
573
|
+
for (const line of readFileSync4(envPath, "utf-8").split(`
|
|
401
574
|
`)) {
|
|
402
575
|
const t = line.trim();
|
|
403
576
|
if (!t || t.startsWith("#"))
|
|
404
577
|
continue;
|
|
405
578
|
const eq = t.indexOf("=");
|
|
406
|
-
if (eq
|
|
407
|
-
|
|
579
|
+
if (eq < 0)
|
|
580
|
+
continue;
|
|
581
|
+
const key = t.slice(0, eq).trim();
|
|
582
|
+
if (key === "QQ_APP_ID") {
|
|
583
|
+
const v = t.slice(eq + 1).trim();
|
|
584
|
+
if (v)
|
|
585
|
+
return v;
|
|
586
|
+
}
|
|
587
|
+
if (key === "QQ_BOTS")
|
|
588
|
+
botsRaw = t.slice(eq + 1).trim();
|
|
408
589
|
}
|
|
409
590
|
} catch {}
|
|
591
|
+
if (botsRaw) {
|
|
592
|
+
try {
|
|
593
|
+
const arr = JSON.parse(botsRaw);
|
|
594
|
+
const first = Array.isArray(arr) ? arr[0]?.appId : undefined;
|
|
595
|
+
if (typeof first === "string" && first)
|
|
596
|
+
return first;
|
|
597
|
+
} catch {}
|
|
598
|
+
}
|
|
410
599
|
return null;
|
|
411
600
|
}
|
|
412
601
|
function inflightPath() {
|
|
413
|
-
return
|
|
602
|
+
return join4(homedir3(), ".openqq", "inflight.json");
|
|
414
603
|
}
|
|
415
604
|
function countInflight(path = inflightPath()) {
|
|
416
605
|
try {
|
|
417
|
-
const raw = JSON.parse(
|
|
606
|
+
const raw = JSON.parse(readFileSync4(path, "utf-8"));
|
|
418
607
|
if (!Array.isArray(raw))
|
|
419
608
|
return 0;
|
|
420
609
|
return raw.filter((e) => e.state === "inflight").length;
|
|
@@ -453,7 +642,7 @@ async function restartBot(opts) {
|
|
|
453
642
|
const log = opts.log ?? (() => {});
|
|
454
643
|
const appId = readAppId();
|
|
455
644
|
if (!appId)
|
|
456
|
-
throw new Error("缺少 ~/.openqq/.env
|
|
645
|
+
throw new Error("缺少 ~/.openqq/.env 的角色凭据(QQ_APP_ID 或 QQ_BOTS 首角色),无法定位实例锁");
|
|
457
646
|
if (!opts.force) {
|
|
458
647
|
const idle = await waitUntilIdle({
|
|
459
648
|
timeoutMs: opts.timeoutMs,
|
|
@@ -486,9 +675,9 @@ async function restartBot(opts) {
|
|
|
486
675
|
|
|
487
676
|
// src/cli/service-main.ts
|
|
488
677
|
import { spawnSync as spawnSync2 } from "child_process";
|
|
489
|
-
import { existsSync as
|
|
678
|
+
import { existsSync as existsSync5, mkdirSync as mkdirSync2, readFileSync as readFileSync5, rmSync as rmSync2, statSync as statSync5, writeFileSync as writeFileSync3 } from "fs";
|
|
490
679
|
import { homedir as homedir4, tmpdir } from "os";
|
|
491
|
-
import { join as
|
|
680
|
+
import { join as join5 } from "path";
|
|
492
681
|
var WIN_TASK_NAME = "OpenQQBotWatchdog";
|
|
493
682
|
var SYSTEMD_UNIT = "openqq-watchdog";
|
|
494
683
|
var LAUNCHD_LABEL = "com.linjp.opencode-qqbot.watchdog";
|
|
@@ -526,9 +715,9 @@ function buildSchtasksXmlArgs(xmlPath) {
|
|
|
526
715
|
function conhostPath(platform = process.platform) {
|
|
527
716
|
if (platform !== "win32")
|
|
528
717
|
return null;
|
|
529
|
-
const p =
|
|
718
|
+
const p = join5(process.env.SystemRoot ?? "C:\\Windows", "System32", "conhost.exe");
|
|
530
719
|
try {
|
|
531
|
-
return
|
|
720
|
+
return statSync5(p).isFile() ? p : null;
|
|
532
721
|
} catch {
|
|
533
722
|
return null;
|
|
534
723
|
}
|
|
@@ -600,10 +789,10 @@ function buildLaunchdPlist(spec) {
|
|
|
600
789
|
`);
|
|
601
790
|
}
|
|
602
791
|
function systemdDir() {
|
|
603
|
-
return
|
|
792
|
+
return join5(homedir4(), ".config", "systemd", "user");
|
|
604
793
|
}
|
|
605
794
|
function launchdPath() {
|
|
606
|
-
return
|
|
795
|
+
return join5(homedir4(), "Library", "LaunchAgents", `${LAUNCHD_LABEL}.plist`);
|
|
607
796
|
}
|
|
608
797
|
function registeredForm(platform = process.platform) {
|
|
609
798
|
if (platform !== "win32")
|
|
@@ -625,9 +814,9 @@ var FORM_LABEL = {
|
|
|
625
814
|
};
|
|
626
815
|
function serviceInstall(spec, platform = process.platform) {
|
|
627
816
|
if (platform === "win32") {
|
|
628
|
-
const xmlPath =
|
|
817
|
+
const xmlPath = join5(tmpdir(), `openqq-watchdog-task-${process.pid}.xml`);
|
|
629
818
|
try {
|
|
630
|
-
|
|
819
|
+
writeFileSync3(xmlPath, Buffer.concat([
|
|
631
820
|
new Uint8Array([255, 254]),
|
|
632
821
|
Buffer.from(buildSchtasksTaskXml(spec), "utf-16le")
|
|
633
822
|
]));
|
|
@@ -637,7 +826,7 @@ function serviceInstall(spec, platform = process.platform) {
|
|
|
637
826
|
注册形态: ${FORM_LABEL.s4u}` };
|
|
638
827
|
}
|
|
639
828
|
const xmlErr = res.detail;
|
|
640
|
-
|
|
829
|
+
rmSync2(xmlPath, { force: true });
|
|
641
830
|
const conhostRes = run("schtasks", buildSchtasksConhostArgs(spec));
|
|
642
831
|
if (conhostRes.ok) {
|
|
643
832
|
return { ok: true, detail: `${conhostRes.detail}
|
|
@@ -652,15 +841,15 @@ function serviceInstall(spec, platform = process.platform) {
|
|
|
652
841
|
};
|
|
653
842
|
} finally {
|
|
654
843
|
try {
|
|
655
|
-
|
|
844
|
+
rmSync2(xmlPath, { force: true });
|
|
656
845
|
} catch {}
|
|
657
846
|
}
|
|
658
847
|
}
|
|
659
848
|
if (platform === "linux") {
|
|
660
849
|
const dir = systemdDir();
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
850
|
+
mkdirSync2(dir, { recursive: true });
|
|
851
|
+
writeFileSync3(join5(dir, `${SYSTEMD_UNIT}.service`), buildSystemdService(spec));
|
|
852
|
+
writeFileSync3(join5(dir, `${SYSTEMD_UNIT}.timer`), buildSystemdTimer(spec.intervalMin));
|
|
664
853
|
const reload = run("systemctl", ["--user", "daemon-reload"]);
|
|
665
854
|
if (!reload.ok)
|
|
666
855
|
return reload;
|
|
@@ -673,8 +862,8 @@ function serviceInstall(spec, platform = process.platform) {
|
|
|
673
862
|
}
|
|
674
863
|
if (platform === "darwin") {
|
|
675
864
|
const path = launchdPath();
|
|
676
|
-
|
|
677
|
-
|
|
865
|
+
mkdirSync2(join5(homedir4(), "Library", "LaunchAgents"), { recursive: true });
|
|
866
|
+
writeFileSync3(path, buildLaunchdPlist(spec));
|
|
678
867
|
return run("launchctl", ["load", "-w", path]);
|
|
679
868
|
}
|
|
680
869
|
return { ok: false, detail: `不支持的平台:${platform}` };
|
|
@@ -684,14 +873,14 @@ function serviceUninstall(platform = process.platform) {
|
|
|
684
873
|
return run("schtasks", buildSchtasksDeleteArgs());
|
|
685
874
|
if (platform === "linux") {
|
|
686
875
|
run("systemctl", ["--user", "disable", "--now", `${SYSTEMD_UNIT}.timer`]);
|
|
687
|
-
|
|
688
|
-
|
|
876
|
+
rmSync2(join5(systemdDir(), `${SYSTEMD_UNIT}.timer`), { force: true });
|
|
877
|
+
rmSync2(join5(systemdDir(), `${SYSTEMD_UNIT}.service`), { force: true });
|
|
689
878
|
return run("systemctl", ["--user", "daemon-reload"]);
|
|
690
879
|
}
|
|
691
880
|
if (platform === "darwin") {
|
|
692
881
|
const path = launchdPath();
|
|
693
882
|
const res = run("launchctl", ["unload", "-w", path]);
|
|
694
|
-
|
|
883
|
+
rmSync2(path, { force: true });
|
|
695
884
|
return res;
|
|
696
885
|
}
|
|
697
886
|
return { ok: false, detail: `不支持的平台:${platform}` };
|
|
@@ -704,14 +893,14 @@ function serviceStatus(platform = process.platform) {
|
|
|
704
893
|
${form}` };
|
|
705
894
|
}
|
|
706
895
|
if (platform === "linux") {
|
|
707
|
-
const exists =
|
|
896
|
+
const exists = existsSync5(join5(systemdDir(), `${SYSTEMD_UNIT}.timer`));
|
|
708
897
|
const active = run("systemctl", ["--user", "is-active", `${SYSTEMD_UNIT}.timer`]);
|
|
709
898
|
return { ok: exists && active.ok, detail: `unit 文件存在=${exists}
|
|
710
899
|
${active.detail}` };
|
|
711
900
|
}
|
|
712
901
|
if (platform === "darwin") {
|
|
713
902
|
const path = launchdPath();
|
|
714
|
-
return { ok:
|
|
903
|
+
return { ok: existsSync5(path), detail: `${path} 存在=${existsSync5(path)}
|
|
715
904
|
${run("launchctl", ["list", LAUNCHD_LABEL]).detail}` };
|
|
716
905
|
}
|
|
717
906
|
return { ok: false, detail: `不支持的平台:${platform}` };
|
|
@@ -720,9 +909,9 @@ ${run("launchctl", ["list", LAUNCHD_LABEL]).detail}` };
|
|
|
720
909
|
// src/cli/openqq-main.ts
|
|
721
910
|
var DEFAULT_INTERVAL_MIN = 10;
|
|
722
911
|
function sibling(name) {
|
|
723
|
-
const here =
|
|
724
|
-
const candidates = [
|
|
725
|
-
return candidates.find((p) =>
|
|
912
|
+
const here = dirname4(fileURLToPath2(import.meta.url));
|
|
913
|
+
const candidates = [join6(here, name), join6(here, "..", "dist", name)];
|
|
914
|
+
return candidates.find((p) => existsSync6(p)) ?? candidates[0];
|
|
726
915
|
}
|
|
727
916
|
function runtimeCommand() {
|
|
728
917
|
const runtime = resolveRuntime();
|