dsh-qqbot-muu 0.1.15 → 0.1.21
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/CHANGELOG.md +168 -85
- package/client/qqbot-settings.js +13401 -12024
- package/config/modules.json +212 -0
- package/dist/channel-tools.js +21 -10
- package/dist/config.d.ts +14 -0
- package/dist/config.js +573 -449
- package/dist/features/contextless-store.js +247 -247
- package/dist/features/contextless-trim.js +455 -34
- package/dist/features/four-source.js +338 -336
- package/dist/features/group-join-request.js +289 -287
- package/dist/features/local-models.js +1494 -0
- package/dist/features/rotate-summarizer.js +438 -0
- package/dist/features/scenario-detect.js +321 -0
- package/dist/features/scheduler.js +8 -1
- package/dist/features/session-rotate.js +499 -0
- package/dist/features/session-title.js +199 -0
- package/dist/features/style-layer.js +501 -0
- package/dist/features/thinking-log.js +71 -69
- package/dist/gateway/bootstrap.js +1704 -1388
- package/dist/gateway/data-root.js +77 -3
- package/dist/gateway/middleware-setup.js +6 -3
- package/dist/index.js +4 -2
- package/dist/model/settings-reader.js +193 -102
- package/dist/session/session-manager.js +1730 -1587
- package/dist/shared/paths.js +128 -0
- package/dist/shared/settings-io.js +79 -0
- package/dist/transport/inbound.js +20 -1
- package/dist/transport/outbound.js +56 -4
- package/docs/RELEASE_CHECKLIST.md +24 -2
- package/dshqqbot/.qqbot-extensions/hooks/interview-panel.mjs +41 -1
- package/dshqqbot/.qqbot-extensions/inbound/00-vision.mjs +111 -0
- package/dshqqbot/.qqbot-extensions/inbound/aa-memory-recall.mjs +802 -689
- package/dshqqbot/.qqbot-extensions/inbound/ac-image-see.mjs +1 -1
- package/dshqqbot/.qqbot-extensions/inbound/az-feedback.mjs +113 -115
- package/dshqqbot/.qqbot-extensions/inbound/local-answer.mjs +689 -568
- package/dshqqbot/.qqbot-extensions/lib/.pet-form.json +1 -1
- package/dshqqbot/.qqbot-extensions/lib/draw-core.mjs +238 -7
- package/dshqqbot/.qqbot-extensions/lib/local-llm.mjs +455 -394
- package/dshqqbot/.qqbot-extensions/lib/memory-agent.mjs +930 -928
- package/dshqqbot/.qqbot-extensions/lib/memory-core.mjs +537 -537
- package/dshqqbot/.qqbot-extensions/lib/memory-db.mjs +2476 -2245
- package/dshqqbot/.qqbot-extensions/lib/memory-hippo.mjs +18 -0
- package/dshqqbot/.qqbot-extensions/lib/memory.mjs +191 -186
- package/dshqqbot/.qqbot-extensions/lib/plugin-dir.mjs +14 -4
- package/dshqqbot/.qqbot-extensions/lib/scope-util.mjs +84 -0
- package/dshqqbot/.qqbot-extensions/lib/vision.mjs +1 -1
- package/dshqqbot/.qqbot-extensions/outbound/aa-sanitize.mjs +158 -154
- package/dshqqbot/.qqbot-extensions/outbound/ac-media-fix.mjs +5 -1
- package/dshqqbot/.qqbot-extensions/outbound/memory-agent.mjs +121 -130
- package/dshqqbot/.qqbot-extensions/outbound/split-reply.mjs +18 -2
- package/dshqqbot/.qqbot-extensions/outbound/turn-memory.mjs +92 -101
- package/dshqqbot/.qqbot-extensions/tools/draw.mjs +22 -1
- package/dshqqbot/.qqbot-extensions/tools/memory.mjs +178 -120
- package/dshqqbot/.qqbot-extensions/tools/pet_card.mjs +3 -2
- package/dshqqbot/.qqbot-extensions/tools/storyboard.mjs +3 -1
- package/dshqqbot/.qqbot-extensions/vendor/abogus/test-abogus.mjs +128 -126
- package/package.json +119 -111
- package/scripts/__pycache__/fan-mode.cpython-311.pyc +0 -0
- package/scripts/__pycache__/memory-audit.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-ip-sync.cpython-311.pyc +0 -0
- package/scripts/__pycache__/qq-whitelist.cpython-311.pyc +0 -0
- package/scripts/_add_maintenance_cfg.py +20 -0
- package/scripts/_bench_keeplease.sh +10 -0
- package/scripts/_dbg_purge.py +15 -0
- package/scripts/_elev.sh +6 -0
- package/scripts/_fix_all_admin.cmd +16 -0
- package/scripts/_livecheck-image.mjs +33 -0
- package/scripts/_livecheck-llm.mjs +34 -0
- package/scripts/_livecheck-tts.mjs +37 -0
- package/scripts/_patch_curve.py +88 -0
- package/scripts/_patch_fan_thermal.py +24 -0
- package/scripts/_tag_tmp.py +13 -0
- package/scripts/adb-broker-client.mjs +87 -0
- package/scripts/adb-broker.mjs +205 -0
- package/scripts/ai-mode.cmd +6 -0
- package/scripts/ai-mode.py +387 -0
- package/scripts/ai-toolkit.py +607 -0
- package/scripts/apply-fan-gpu45.py +51 -0
- package/scripts/bench-fans.py +515 -0
- package/scripts/bench-load-direct.py +63 -0
- package/scripts/boot-tasks-hidden.vbs +20 -0
- package/scripts/boot-tasks.cmd +277 -0
- package/scripts/chat-archive/__pycache__/build-sft-v4.cpython-311.pyc +0 -0
- package/scripts/chat-archive/__pycache__/build-sft.cpython-311.pyc +0 -0
- package/scripts/chat-archive/build-sft-v4.py +566 -0
- package/scripts/chat-archive/build-sft.py +390 -0
- package/scripts/chat-archive/eval-sft.py +155 -0
- package/scripts/chat-archive/extract-memory.py +143 -0
- package/scripts/chat-archive/inventory.py +182 -0
- package/scripts/chat-archive/merge-sft.py +114 -0
- package/scripts/chat-archive/qce-pull.py +217 -0
- package/scripts/chat-archive/qqheader.py +110 -0
- package/scripts/chat-archive/qqkey2.py +113 -0
- package/scripts/chat-archive/qqprobe.py +153 -0
- package/scripts/chat-archive/screen-collect.py +163 -0
- package/scripts/chat-archive/sqlcipher_probe.py +444 -0
- package/scripts/comfy_gen.py +209 -0
- package/scripts/compose-video.py +168 -0
- package/scripts/dl_models.py +45 -0
- package/scripts/draw-now.cmd +18 -0
- package/scripts/dsh-instance-guard.mjs +20 -2
- package/scripts/elev-extra.cmd +25 -0
- package/scripts/episode.py +529 -0
- package/scripts/fan-curve.py +218 -0
- package/scripts/fan-guard.cmd +8 -0
- package/scripts/fan-mode.py +380 -0
- package/scripts/fan_manual_safe.py +33 -0
- package/scripts/fan_restore_manual.py +46 -0
- package/scripts/fc_curves.py +359 -0
- package/scripts/fix-fan-and-timers.cmd +39 -0
- package/scripts/fix_admin_all.py +94 -0
- package/scripts/flows.mjs +224 -0
- package/scripts/gateway.cmd +126 -0
- package/scripts/gen-image.py +420 -0
- package/scripts/gen-video.py +186 -0
- package/scripts/git-askpass.cmd +2 -2
- package/scripts/gpu-mode.py +195 -0
- package/scripts/hw-profile.py +175 -0
- package/scripts/install-schedule.cmd +37 -0
- package/scripts/install-watch.cmd +29 -0
- package/scripts/matte-cutout.py +106 -0
- package/scripts/media-enhance.py +402 -0
- package/scripts/memory-audit.py +581 -0
- package/scripts/migrate-scope.mjs +140 -139
- package/scripts/model-registry.py +519 -0
- package/scripts/openid-audit.mjs +219 -0
- package/scripts/pull-gguf.py +127 -0
- package/scripts/purge-standby.py +151 -0
- package/scripts/qq-connect-login.py +133 -0
- package/scripts/qq-ip-sync-hidden.vbs +5 -0
- package/scripts/qq-ip-sync.py +300 -0
- package/scripts/qq-login-once.cmd +20 -0
- package/scripts/qq-login-qr.py +72 -0
- package/scripts/qq-whitelist-fix.cmd +27 -0
- package/scripts/qq-whitelist-login.cmd +26 -0
- package/scripts/qq-whitelist.cmd +12 -0
- package/scripts/qq-whitelist.py +1452 -0
- package/scripts/quality-compare.py +134 -0
- package/scripts/release-all.mjs +636 -430
- package/scripts/release-check.mjs +244 -226
- package/scripts/resume-coder.cmd +38 -0
- package/scripts/secure-room/index.html +334 -0
- package/scripts/secure-room/server.py +582 -0
- package/scripts/security-check.mjs +164 -0
- package/scripts/selftest-local-llm.mjs +20 -0
- package/scripts/selftest-local-models.mjs +754 -0
- package/scripts/selftest-rotate-style.mjs +308 -0
- package/scripts/shutdown-s5-optional.cmd +21 -0
- package/scripts/sleep-2325.cmd +75 -0
- package/scripts/start-comfyui.cmd +5 -0
- package/scripts/start-strata.cmd +17 -0
- package/scripts/start-strata.vbs +11 -0
- package/scripts/start-watch-hidden.vbs +3 -0
- package/scripts/stickers/auto-tag.mjs +161 -0
- package/scripts/storage.mjs +236 -0
- package/scripts/sync-plugin-copies.mjs +98 -23
- package/scripts/tests/test-qq-whitelist.py +192 -0
- package/scripts/train/__pycache__/train-lora.cpython-311.pyc +0 -0
- package/scripts/train/gguf-quantize.py +92 -0
- package/scripts/train/gguf-to-hf.py +112 -0
- package/scripts/train/merge-lora.py +51 -0
- package/scripts/train/probe-mirrors.sh +12 -0
- package/scripts/train/probe-mirrors2.sh +20 -0
- package/scripts/train/setup-wsl.sh +23 -0
- package/scripts/train/setup-wsl2.sh +25 -0
- package/scripts/train/setup-wsl3.sh +22 -0
- package/scripts/train/setup-wsl4.sh +22 -0
- package/scripts/train/train-lora.py +319 -0
- package/scripts/vram-free.py +104 -0
- package/scripts/wechat-full-pull.mjs +1 -1
- package/settings-host.js +1047 -12
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// 临时实测脚本(2026-11-26):真调 local_llm 的 strata / gateway 分支
|
|
2
|
+
import { configureLocalModels, localLlm } from '../dist/features/local-models.js';
|
|
3
|
+
|
|
4
|
+
configureLocalModels({
|
|
5
|
+
enabled: true, llm: true,
|
|
6
|
+
llmBackend: 'auto',
|
|
7
|
+
gatewayUrl: 'http://127.0.0.1:8090/v1',
|
|
8
|
+
// 2026-11-29 安全:不再硬编码 key;留空由 resolveGatewayKey 从 clients.json / 环境变量取
|
|
9
|
+
gatewayKey: '',
|
|
10
|
+
gatewayModel: '',
|
|
11
|
+
llmTimeoutMs: 150000,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const which = process.argv[2] || 'strata';
|
|
15
|
+
const prompt = process.argv[3] || '用一句中文回答:深海鲸鱼娘此刻在想什么?';
|
|
16
|
+
const t0 = Date.now();
|
|
17
|
+
const r = await localLlm(prompt, { backend: which, maxTokens: 120, timeoutMs: 150000 });
|
|
18
|
+
const wall = Date.now() - t0;
|
|
19
|
+
const out = {
|
|
20
|
+
backend: which,
|
|
21
|
+
ok: r.ok,
|
|
22
|
+
code: r.code || null,
|
|
23
|
+
error: r.error || null,
|
|
24
|
+
model: r.model || null,
|
|
25
|
+
text: r.text || null,
|
|
26
|
+
textLen: r.text ? r.text.length : 0,
|
|
27
|
+
usage: r.usage || null,
|
|
28
|
+
upstream: r.upstream || null,
|
|
29
|
+
ms: r.ms || null,
|
|
30
|
+
wallMs: wall,
|
|
31
|
+
attempts: r.attempts || null,
|
|
32
|
+
};
|
|
33
|
+
console.log(JSON.stringify(out, null, 2));
|
|
34
|
+
if (!r.ok || !String(r.text || '').trim()) process.exitCode = 3;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// 临时实测脚本(2026-11-26):真跑 local_tts(含显存冲突策略)→ local_asr 回读
|
|
2
|
+
import { configureLocalModels, getLocalModelsConfig, localTts, localAsr } from '../dist/features/local-models.js';
|
|
3
|
+
|
|
4
|
+
configureLocalModels({ enabled: true, tts: true, asr: true, ttsIdleUnload: false, ttsNeedVramMb: 7000 });
|
|
5
|
+
const cfg = getLocalModelsConfig();
|
|
6
|
+
const text = process.argv[2] || '小蓝在深海里吐了个泡泡,说今天也要好好吃饭。';
|
|
7
|
+
const t0 = Date.now();
|
|
8
|
+
const tts = await localTts(text, { timeoutMs: 300000, outPath: process.argv[3] || '', unloadFirst: process.argv[4] === 'keep' ? false : true });
|
|
9
|
+
const wall = Date.now() - t0;
|
|
10
|
+
const out = {
|
|
11
|
+
input: text,
|
|
12
|
+
ok: tts.ok,
|
|
13
|
+
code: tts.code || null,
|
|
14
|
+
error: tts.error || null,
|
|
15
|
+
backend: tts.backend,
|
|
16
|
+
path: tts.path || null,
|
|
17
|
+
bytes: tts.bytes || null,
|
|
18
|
+
format: tts.format || null,
|
|
19
|
+
ms: tts.ms || null,
|
|
20
|
+
ttsMs: tts.ttsMs || null,
|
|
21
|
+
elapsedMs: tts.elapsedMs || null,
|
|
22
|
+
durationMs: tts.durationMs || null,
|
|
23
|
+
vramAllocMb: tts.vramAllocMb || null,
|
|
24
|
+
vramPeakMb: tts.vramPeakMb || null,
|
|
25
|
+
freeVramMb: tts.freeVramMb ?? null,
|
|
26
|
+
policy: tts.policy || null,
|
|
27
|
+
note: tts.note || null,
|
|
28
|
+
wallMs: wall,
|
|
29
|
+
endpoints: { localai: cfg.localaiUrl, compute: cfg.computeUrl },
|
|
30
|
+
};
|
|
31
|
+
if (tts.ok && tts.path) {
|
|
32
|
+
const a0 = Date.now();
|
|
33
|
+
const asr = await localAsr(tts.path, { lang: 'zh', timeoutMs: 180000 });
|
|
34
|
+
out.asr = { ok: asr.ok, code: asr.code || null, text: asr.text || null, duration: asr.duration || null, ms: asr.ms || null, wallMs: Date.now() - a0 };
|
|
35
|
+
}
|
|
36
|
+
console.log(JSON.stringify(out, null, 2));
|
|
37
|
+
if (!tts.ok) process.exitCode = 3;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""Patch FanControl userConfig.json with a temperature curve (must run elevated).
|
|
4
|
+
|
|
5
|
+
Usage:
|
|
6
|
+
python _patch_curve.py add # add guessed Linear curve + bind CPU Fan
|
|
7
|
+
python _patch_curve.py show # just print curve/control state
|
|
8
|
+
"""
|
|
9
|
+
import io
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import shutil
|
|
13
|
+
import sys
|
|
14
|
+
import time
|
|
15
|
+
|
|
16
|
+
CFG = r"C:\Program Files (x86)\FanControl\Configurations\userConfig.json"
|
|
17
|
+
BAKDIR = r"F:\qqbot\backups\fancontrol"
|
|
18
|
+
|
|
19
|
+
CURVE = {
|
|
20
|
+
"Name": "CPU温度曲线",
|
|
21
|
+
"IsHidden": False,
|
|
22
|
+
"CommandMode": 1,
|
|
23
|
+
"SelectedTempSensor": "/lpc/nct6797d/temperature/1",
|
|
24
|
+
"MinimumPercent": 20,
|
|
25
|
+
"MaximumPercent": 100,
|
|
26
|
+
"Points": [[40, 25], [55, 40], [70, 65], [80, 100]],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def load():
|
|
31
|
+
with io.open(CFG, encoding="utf-8") as f:
|
|
32
|
+
return json.load(f)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def save(d):
|
|
36
|
+
with io.open(CFG, "w", encoding="utf-8") as f:
|
|
37
|
+
f.write(json.dumps(d, ensure_ascii=False, indent=4))
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def show(d):
|
|
41
|
+
fc = d["FanControl"]
|
|
42
|
+
print("FanCurves:", json.dumps(fc["FanCurves"], ensure_ascii=False))
|
|
43
|
+
print("TemperatureSensors:", json.dumps(fc["TemperatureSensors"], ensure_ascii=False))
|
|
44
|
+
for c in fc["Controls"]:
|
|
45
|
+
print(
|
|
46
|
+
" ctl %-28s nick=%-12s en=%s curve=%r manual=%s val=%s"
|
|
47
|
+
% (
|
|
48
|
+
c.get("Identifier"),
|
|
49
|
+
c.get("NickName"),
|
|
50
|
+
c.get("Enable"),
|
|
51
|
+
c.get("SelectedFanCurve"),
|
|
52
|
+
c.get("ManualControl"),
|
|
53
|
+
c.get("ManualControlValue"),
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def main():
|
|
59
|
+
mode = sys.argv[1] if len(sys.argv) > 1 else "add"
|
|
60
|
+
d = load()
|
|
61
|
+
if mode == "show":
|
|
62
|
+
show(d)
|
|
63
|
+
return
|
|
64
|
+
os.makedirs(BAKDIR, exist_ok=True)
|
|
65
|
+
ts = time.strftime("%Y%m%d-%H%M%S")
|
|
66
|
+
bak = os.path.join(BAKDIR, "userConfig.json.bak-%s" % ts)
|
|
67
|
+
shutil.copy2(CFG, bak)
|
|
68
|
+
print("backup ->", bak)
|
|
69
|
+
fc = d["FanControl"]
|
|
70
|
+
found = False
|
|
71
|
+
for c in fc["FanCurves"]:
|
|
72
|
+
if c.get("Name") == CURVE["Name"]:
|
|
73
|
+
c.update(CURVE)
|
|
74
|
+
found = True
|
|
75
|
+
if not found:
|
|
76
|
+
fc["FanCurves"].append(dict(CURVE))
|
|
77
|
+
for c in fc["Controls"]:
|
|
78
|
+
if c.get("Identifier") == "/lpc/nct6797d/control/1":
|
|
79
|
+
c["SelectedFanCurve"] = CURVE["Name"]
|
|
80
|
+
c["ManualControl"] = False
|
|
81
|
+
c["Enable"] = True
|
|
82
|
+
save(d)
|
|
83
|
+
print("patched")
|
|
84
|
+
show(d)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
if __name__ == "__main__":
|
|
88
|
+
main()
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import io, json
|
|
2
|
+
p = r"C:\Program Files (x86)\FanControl\Configurations\userConfig.json"
|
|
3
|
+
d = json.load(io.open(p, encoding="utf-8"))
|
|
4
|
+
s = d["Sensors"]["LibreHardwareMonitorSettings"]
|
|
5
|
+
s.update({"Controller": True, "Cpu": True, "Motherboard": True, "Gpu": True, "Memory": True})
|
|
6
|
+
want = {
|
|
7
|
+
"/lpc/nct6797d/control/1": 30,
|
|
8
|
+
"/lpc/nct6797d/control/2": 24,
|
|
9
|
+
"/lpc/nct6797d/control/3": 30,
|
|
10
|
+
"/lpc/nct6797d/control/4": 24,
|
|
11
|
+
"/lpc/nct6797d/control/5": 30,
|
|
12
|
+
"/NVApiWrapper/0-TU102-A/control/0": 25,
|
|
13
|
+
}
|
|
14
|
+
for c in d["FanControl"]["Controls"]:
|
|
15
|
+
i = c.get("Identifier")
|
|
16
|
+
if i in want:
|
|
17
|
+
c["Enable"] = True
|
|
18
|
+
c["ManualControl"] = True
|
|
19
|
+
c["ManualControlValue"] = want[i]
|
|
20
|
+
c["MinimumPercent"] = 0
|
|
21
|
+
elif i in ("/lpc/nct6797d/control/0", "/lpc/nct6797d/control/6"):
|
|
22
|
+
c["Enable"] = False
|
|
23
|
+
json.dump(d, io.open(p, "w", encoding="utf-8"), ensure_ascii=False, indent=4)
|
|
24
|
+
print("PATCHED")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import io, json
|
|
2
|
+
p = r"C:\Program Files (x86)\FanControl\Configurations\userConfig.json"
|
|
3
|
+
d = json.load(io.open(p, encoding="utf-8"))
|
|
4
|
+
vals = {"/lpc/nct6797d/control/1": 17, "/lpc/nct6797d/control/2": 23, "/lpc/nct6797d/control/3": 29,
|
|
5
|
+
"/lpc/nct6797d/control/4": 35, "/lpc/nct6797d/control/5": 41, "/lpc/nct6797d/control/6": 47,
|
|
6
|
+
"NVApiWrapper/0-TU102-A/control/0": 53}
|
|
7
|
+
for c in d["FanControl"]["Controls"]:
|
|
8
|
+
c["ManualControl"] = True
|
|
9
|
+
c["SelectedFanCurve"] = None
|
|
10
|
+
if c["Identifier"] in vals:
|
|
11
|
+
c["ManualControlValue"] = vals[c["Identifier"]]
|
|
12
|
+
io.open(p, "w", encoding="utf-8").write(json.dumps(d, ensure_ascii=False, indent=2))
|
|
13
|
+
print("tagged", list(vals.values()))
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* adb 管家的客户端(P0-1)
|
|
3
|
+
*
|
|
4
|
+
* 通道侧只调这里,不直接执行 kill-server / start-server。
|
|
5
|
+
* broker 不在就拉起来;拉起失败返回 { ok:false },调用方走「只 reconnect、不杀守护」的兜底。
|
|
6
|
+
*/
|
|
7
|
+
import { spawn } from 'node:child_process'
|
|
8
|
+
import { existsSync } from 'node:fs'
|
|
9
|
+
import { dirname, join } from 'node:path'
|
|
10
|
+
import { fileURLToPath } from 'node:url'
|
|
11
|
+
|
|
12
|
+
const HERE = dirname(fileURLToPath(import.meta.url))
|
|
13
|
+
const PORT = Number(process.env.ADB_BROKER_PORT || 4355)
|
|
14
|
+
const BASE = `http://127.0.0.1:${PORT}`
|
|
15
|
+
let lastSpawnAt = 0
|
|
16
|
+
|
|
17
|
+
export async function brokerHealth(timeoutMs = 800) {
|
|
18
|
+
const ac = new AbortController()
|
|
19
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs)
|
|
20
|
+
try {
|
|
21
|
+
const r = await fetch(`${BASE}/health`, { signal: ac.signal })
|
|
22
|
+
return r.ok ? await r.json() : null
|
|
23
|
+
} catch {
|
|
24
|
+
return null
|
|
25
|
+
} finally {
|
|
26
|
+
clearTimeout(timer)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 拿一个能跑脚本的 node:Electron 的 process.execPath 会重新打开应用,不能直接用 */
|
|
31
|
+
function nodeBin() {
|
|
32
|
+
const override = String(process.env.ADB_BROKER_NODE || '').trim()
|
|
33
|
+
if (override) return override
|
|
34
|
+
if (/node(\.exe)?$/i.test(process.execPath)) return process.execPath
|
|
35
|
+
for (const p of ['F:/nodejs/node.exe', 'C:/Program Files/nodejs/node.exe']) {
|
|
36
|
+
try { if (existsSync(p)) return p } catch { /* ignore */ }
|
|
37
|
+
}
|
|
38
|
+
return 'node'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function ensureBroker() {
|
|
42
|
+
if (await brokerHealth()) return true
|
|
43
|
+
// 30 秒内不重复尝试拉起,避免健康检查失败时刷进程
|
|
44
|
+
if (Date.now() - lastSpawnAt > 30_000) {
|
|
45
|
+
lastSpawnAt = Date.now()
|
|
46
|
+
try {
|
|
47
|
+
const child = spawn(nodeBin(), [join(HERE, 'adb-broker.mjs')], {
|
|
48
|
+
detached: true,
|
|
49
|
+
windowsHide: true,
|
|
50
|
+
stdio: 'ignore',
|
|
51
|
+
env: process.env,
|
|
52
|
+
})
|
|
53
|
+
child.on('error', () => { /* 拉起失败由下面的轮询兜底 */ })
|
|
54
|
+
child.unref()
|
|
55
|
+
} catch { /* ignore */ }
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < 8; i += 1) {
|
|
58
|
+
await new Promise((r) => setTimeout(r, 300))
|
|
59
|
+
if (await brokerHealth()) return true
|
|
60
|
+
}
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 注册并确保端口存在(必要时由 broker 统一重建 adb 并恢复所有通道的端口)。
|
|
66
|
+
* @param {{id?:string, serial:string, wireless?:string, forward?:string[][], reverse?:string[][], timeoutMs?:number}} opts
|
|
67
|
+
*/
|
|
68
|
+
export async function ensureAdbRoutes({ id = 'default', serial, wireless = '', forward = [], reverse = [], timeoutMs = 25_000 } = {}) {
|
|
69
|
+
const up = await ensureBroker()
|
|
70
|
+
if (!up) return { ok: false, error: 'adb-broker unavailable' }
|
|
71
|
+
const ac = new AbortController()
|
|
72
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs)
|
|
73
|
+
try {
|
|
74
|
+
const r = await fetch(`${BASE}/ensure`, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: { 'Content-Type': 'application/json' },
|
|
77
|
+
body: JSON.stringify({ id, serial, wireless, forward, reverse }),
|
|
78
|
+
signal: ac.signal,
|
|
79
|
+
})
|
|
80
|
+
const json = await r.json().catch(() => null)
|
|
81
|
+
return { ok: r.ok && json?.ok !== false, ...(json || {}) }
|
|
82
|
+
} catch (e) {
|
|
83
|
+
return { ok: false, error: String(e?.message || e) }
|
|
84
|
+
} finally {
|
|
85
|
+
clearTimeout(timer)
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* adb 单点管家(P0-1)
|
|
4
|
+
*
|
|
5
|
+
* 背景:微信桥和小天才插件以前各自判断「设备不见了 → kill-server/start-server」。
|
|
6
|
+
* 两个进程共用一个 adb server、一台设备,任意一边重建就会把另一边的
|
|
7
|
+
* forward / reverse / frida 5555 转发一起掀掉,表现为「一个通道自愈、另一个掉线」。
|
|
8
|
+
*
|
|
9
|
+
* 本服务是唯一允许重建 adb 守护的进程:
|
|
10
|
+
* · 所有调用串行化,避免两个通道同时抢 adb;
|
|
11
|
+
* · 设备离线先 reconnect,只有连续失败才 kill-server;
|
|
12
|
+
* · 重建后按注册表恢复**所有通道**的 forward/reverse,而不是只管调用方自己。
|
|
13
|
+
*
|
|
14
|
+
* 接口(只监听 127.0.0.1):
|
|
15
|
+
* GET /health → { ok, pid, routes, lastRebuildAt, lastError }
|
|
16
|
+
* POST /ensure → { serial, forward:[[local,remote]], reverse:[[local,remote]] }
|
|
17
|
+
*
|
|
18
|
+
* 环境变量:ADB_BROKER_PORT(默认 4355)、ADB_PATH、ADB_SERIAL
|
|
19
|
+
*/
|
|
20
|
+
import { createServer } from 'node:http'
|
|
21
|
+
import { execFile } from 'node:child_process'
|
|
22
|
+
|
|
23
|
+
const HOST = '127.0.0.1'
|
|
24
|
+
const PORT = Number(process.env.ADB_BROKER_PORT || 4355)
|
|
25
|
+
const ADB = process.env.ADB_PATH || 'F:/tools/pt/platform-tools/adb.exe'
|
|
26
|
+
/** 同一时刻只允许一次重建;重建很重,且会把所有通道的隧道清掉 */
|
|
27
|
+
const REBUILD_COOLDOWN_MS = 60_000
|
|
28
|
+
|
|
29
|
+
/** id(通道名)→ { serial, forward: Map<local,remote>, reverse: Map<local,remote>, at }
|
|
30
|
+
* 以通道为单位整体替换,端口自愈后不会残留旧端口的转发。 */
|
|
31
|
+
const routes = new Map()
|
|
32
|
+
let lastRebuildAt = 0
|
|
33
|
+
let lastError = ''
|
|
34
|
+
let queue = Promise.resolve()
|
|
35
|
+
|
|
36
|
+
function run(args, timeoutMs = 20_000) {
|
|
37
|
+
return new Promise((resolve) => {
|
|
38
|
+
execFile(ADB, args, { timeout: timeoutMs, windowsHide: true }, (e, so, se) => {
|
|
39
|
+
resolve({ ok: !e, out: String(so || ''), err: String(se || e?.message || '').split('\n')[0] })
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 串行执行 adb,后到的请求排队而不是并发抢设备 */
|
|
45
|
+
function serialize(fn) {
|
|
46
|
+
const next = queue.then(fn, fn)
|
|
47
|
+
queue = next.then(() => undefined, () => undefined)
|
|
48
|
+
return next
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function parseDevices(out) {
|
|
52
|
+
const m = new Map()
|
|
53
|
+
for (const ln of String(out || '').split(/\r?\n/)) {
|
|
54
|
+
const s = ln.trim()
|
|
55
|
+
if (!s || /^List of devices/.test(s) || s.startsWith('*')) continue
|
|
56
|
+
const [id, state] = s.split(/\s+/)
|
|
57
|
+
if (id) m.set(id, state || 'unknown')
|
|
58
|
+
}
|
|
59
|
+
return m
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function deviceOnline(serial) {
|
|
63
|
+
const dv = await run(['devices'], 8000)
|
|
64
|
+
const m = parseDevices(dv.out)
|
|
65
|
+
if (!serial) return [...m.values()].some((v) => v === 'device')
|
|
66
|
+
return m.get(serial) === 'device'
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function connectIfTcp(serial) {
|
|
70
|
+
if (!serial.includes(':')) return
|
|
71
|
+
await run(['connect', serial], 12_000)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function applyRoutes(serial) {
|
|
75
|
+
const pre = serial ? ['-s', serial] : []
|
|
76
|
+
const applied = {}
|
|
77
|
+
for (const entry of routes.values()) {
|
|
78
|
+
if (entry.serial !== serial) continue
|
|
79
|
+
for (const [local, remote] of entry.forward) {
|
|
80
|
+
const q = await run([...pre, 'forward', `tcp:${local}`, `tcp:${remote}`], 12_000)
|
|
81
|
+
applied[`forward:${local}->${remote}`] = q.ok ? 'ok' : (q.err || 'fail')
|
|
82
|
+
}
|
|
83
|
+
for (const [local, remote] of entry.reverse) {
|
|
84
|
+
const q = await run([...pre, 'reverse', `tcp:${local}`, `tcp:${remote}`], 12_000)
|
|
85
|
+
applied[`reverse:${local}->${remote}`] = q.ok ? 'ok' : (q.err || 'fail')
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return applied
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 只负责把设备拉回来;不碰任何业务端口 */
|
|
92
|
+
async function recover(serial) {
|
|
93
|
+
await run(['start-server'], 15_000)
|
|
94
|
+
await new Promise((r) => setTimeout(r, 600))
|
|
95
|
+
if (await deviceOnline(serial)) return false
|
|
96
|
+
|
|
97
|
+
await connectIfTcp(serial)
|
|
98
|
+
await run(['reconnect'], 10_000)
|
|
99
|
+
await run(['reconnect', 'offline'], 10_000)
|
|
100
|
+
await new Promise((r) => setTimeout(r, 2500))
|
|
101
|
+
if (await deviceOnline(serial)) return false
|
|
102
|
+
|
|
103
|
+
if (Date.now() - lastRebuildAt < REBUILD_COOLDOWN_MS) return false
|
|
104
|
+
lastRebuildAt = Date.now()
|
|
105
|
+
lastError = 'device offline; rebuilt adb server'
|
|
106
|
+
await run(['kill-server'], 10_000)
|
|
107
|
+
await new Promise((r) => setTimeout(r, 1000))
|
|
108
|
+
await run(['start-server'], 15_000)
|
|
109
|
+
await new Promise((r) => setTimeout(r, 2500))
|
|
110
|
+
await connectIfTcp(serial)
|
|
111
|
+
await new Promise((r) => setTimeout(r, 1500))
|
|
112
|
+
|
|
113
|
+
// 关键:重建的是全局 adb server,必须把其他通道已注册的端口一起恢复
|
|
114
|
+
for (const s of new Set([...routes.values()].map((v) => v.serial))) await applyRoutes(s)
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** USB 序列号不在线时,尝试无线 adb(Tailscale 地址),返回实际可用的 serial */
|
|
119
|
+
async function resolveSerial(serial, wireless) {
|
|
120
|
+
if (await deviceOnline(serial)) return serial
|
|
121
|
+
const w = String(wireless || '').trim()
|
|
122
|
+
if (!w || w === serial) return serial
|
|
123
|
+
await connectIfTcp(w)
|
|
124
|
+
await new Promise((r) => setTimeout(r, 1200))
|
|
125
|
+
if (await deviceOnline(w)) return w
|
|
126
|
+
return serial
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function ensure(body) {
|
|
130
|
+
const configured = String(body?.serial || process.env.ADB_SERIAL || '').trim()
|
|
131
|
+
if (!configured) throw new Error('serial required')
|
|
132
|
+
const id = String(body?.id || 'default')
|
|
133
|
+
const serial = await resolveSerial(configured, body?.wireless)
|
|
134
|
+
const forward = new Map()
|
|
135
|
+
const reverse = new Map()
|
|
136
|
+
for (const pair of body.forward || []) {
|
|
137
|
+
const [local, remote] = pair || []
|
|
138
|
+
if (local && remote) forward.set(String(local), String(remote))
|
|
139
|
+
}
|
|
140
|
+
for (const pair of body.reverse || []) {
|
|
141
|
+
const [local, remote] = pair || []
|
|
142
|
+
if (local && remote) reverse.set(String(local), String(remote))
|
|
143
|
+
}
|
|
144
|
+
routes.set(id, { serial, forward, reverse, at: Date.now() })
|
|
145
|
+
|
|
146
|
+
await recover(serial)
|
|
147
|
+
const applied = await applyRoutes(serial)
|
|
148
|
+
const online = await deviceOnline(serial)
|
|
149
|
+
const routesOk = Object.values(applied).every((v) => v === 'ok')
|
|
150
|
+
if (online && routesOk) lastError = ''
|
|
151
|
+
else if (!online) lastError = lastError || 'device offline'
|
|
152
|
+
return { ok: online && routesOk, configured, serial, deviceOnline: online, applied, lastRebuildAt, lastError }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function reply(res, code, obj) {
|
|
156
|
+
res.writeHead(code, { 'Content-Type': 'application/json; charset=utf-8' })
|
|
157
|
+
res.end(JSON.stringify(obj))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const server = createServer((req, res) => {
|
|
161
|
+
if (req.method === 'GET' && req.url.startsWith('/health')) {
|
|
162
|
+
return reply(res, 200, {
|
|
163
|
+
ok: true,
|
|
164
|
+
pid: process.pid,
|
|
165
|
+
port: PORT,
|
|
166
|
+
adb: ADB,
|
|
167
|
+
routes: [...routes].map(([id, v]) => ({
|
|
168
|
+
id,
|
|
169
|
+
serial: v.serial,
|
|
170
|
+
forward: [...v.forward],
|
|
171
|
+
reverse: [...v.reverse],
|
|
172
|
+
at: v.at,
|
|
173
|
+
})),
|
|
174
|
+
lastRebuildAt,
|
|
175
|
+
lastError,
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
if (req.method === 'POST' && req.url.startsWith('/ensure')) {
|
|
179
|
+
let buf = ''
|
|
180
|
+
req.on('data', (d) => {
|
|
181
|
+
buf += d
|
|
182
|
+
if (buf.length > 64 * 1024) req.destroy()
|
|
183
|
+
})
|
|
184
|
+
req.on('end', () => {
|
|
185
|
+
let body
|
|
186
|
+
try { body = JSON.parse(buf || '{}') } catch { return reply(res, 400, { ok: false, error: 'bad json' }) }
|
|
187
|
+
serialize(() => ensure(body))
|
|
188
|
+
.then((r) => reply(res, 200, r))
|
|
189
|
+
.catch((e) => reply(res, 500, { ok: false, error: String(e?.message || e) }))
|
|
190
|
+
})
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
reply(res, 404, { ok: false, error: 'not found' })
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
server.on('error', (e) => {
|
|
197
|
+
// 已有健康 broker 时静默退出,由调用方复用;端口被别的程序占用才报错
|
|
198
|
+
if (e?.code === 'EADDRINUSE') process.exit(0)
|
|
199
|
+
console.error(`[adb-broker] ${e?.message || e}`)
|
|
200
|
+
process.exit(1)
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
server.listen(PORT, HOST, () => {
|
|
204
|
+
console.log(`[adb-broker] listening ${HOST}:${PORT} adb=${ADB}`)
|
|
205
|
+
})
|