@linjz/opencode-qqbot 0.3.9 → 0.3.10
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/dist/openqq-bot.js +51 -21
- package/package.json +1 -1
- package/src/core/delivery.ts +52 -9
package/dist/openqq-bot.js
CHANGED
|
@@ -2939,16 +2939,16 @@ var require_websocket_server = __commonJS(function(exports, module) {
|
|
|
2939
2939
|
});
|
|
2940
2940
|
|
|
2941
2941
|
// src/core/file-log.ts
|
|
2942
|
-
import { appendFileSync, existsSync as existsSync9, mkdirSync as
|
|
2942
|
+
import { appendFileSync, existsSync as existsSync9, mkdirSync as mkdirSync10, renameSync as renameSync3, statSync as statSync5 } from "fs";
|
|
2943
2943
|
import { join as join12 } from "path";
|
|
2944
2944
|
function installFileLog(logPath) {
|
|
2945
2945
|
try {
|
|
2946
|
-
|
|
2946
|
+
mkdirSync10(join12(logPath, ".."), { recursive: true });
|
|
2947
2947
|
} catch {}
|
|
2948
2948
|
const write = (level, args) => {
|
|
2949
2949
|
try {
|
|
2950
2950
|
if (existsSync9(logPath) && statSync5(logPath).size > ROTATE_BYTES) {
|
|
2951
|
-
|
|
2951
|
+
renameSync3(logPath, `${logPath}.1`);
|
|
2952
2952
|
}
|
|
2953
2953
|
const line = args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
|
|
2954
2954
|
appendFileSync(logPath, `${new Date().toISOString()} [${level}] ${line}
|
|
@@ -3087,7 +3087,7 @@ function loadConfig() {
|
|
|
3087
3087
|
}
|
|
3088
3088
|
|
|
3089
3089
|
// src/index.ts
|
|
3090
|
-
import { mkdirSync as
|
|
3090
|
+
import { mkdirSync as mkdirSync11, writeFileSync as writeFileSync12 } from "fs";
|
|
3091
3091
|
import { join as join13 } from "path";
|
|
3092
3092
|
import { homedir as homedir11 } from "os";
|
|
3093
3093
|
|
|
@@ -4329,7 +4329,7 @@ async function startGateway(options) {
|
|
|
4329
4329
|
}
|
|
4330
4330
|
|
|
4331
4331
|
// src/bridge.ts
|
|
4332
|
-
import { existsSync as existsSync8, readFileSync as readFileSync10, mkdirSync as
|
|
4332
|
+
import { existsSync as existsSync8, readFileSync as readFileSync10, mkdirSync as mkdirSync8, writeFileSync as writeFileSync10 } from "fs";
|
|
4333
4333
|
import { join as join10 } from "path";
|
|
4334
4334
|
import { homedir as homedir9 } from "os";
|
|
4335
4335
|
|
|
@@ -5719,8 +5719,9 @@ function formatPermissionPrompt(request, mainSessionId) {
|
|
|
5719
5719
|
}
|
|
5720
5720
|
|
|
5721
5721
|
// src/core/delivery.ts
|
|
5722
|
-
import { existsSync as existsSync6, readFileSync as readFileSync8, writeFileSync as writeFileSync8 } from "fs";
|
|
5722
|
+
import { existsSync as existsSync6, readFileSync as readFileSync8, writeFileSync as writeFileSync8, mkdirSync as mkdirSync6, renameSync } from "fs";
|
|
5723
5723
|
import { join as join8 } from "path";
|
|
5724
|
+
import { createHash } from "node:crypto";
|
|
5724
5725
|
import { homedir as homedir7 } from "os";
|
|
5725
5726
|
|
|
5726
5727
|
// src/qq/sender.ts
|
|
@@ -6089,9 +6090,36 @@ function saveQuotaMap(m) {
|
|
|
6089
6090
|
}
|
|
6090
6091
|
var UNDELIVERED_NOTICE = `(此前有未能送达的消息)
|
|
6091
6092
|
`;
|
|
6092
|
-
|
|
6093
|
+
var DELIVERY_DEDUPE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
6094
|
+
var DELIVERY_DEDUPE_FILE = process.env.QQ_DELIVERY_DEDUPE_FILE || join8(homedir7(), ".openqq", "delivery-dedupe.json");
|
|
6095
|
+
function deliveryAttemptKey(userId, text) {
|
|
6096
|
+
return `${userId}:${createHash("sha256").update(text).digest("hex")}`;
|
|
6097
|
+
}
|
|
6098
|
+
function shouldSuppressDuplicate(previous, text, now, ttlMs = DELIVERY_DEDUPE_TTL_MS) {
|
|
6093
6099
|
return text.length >= 200 && previous?.text === text && now - previous.at < ttlMs;
|
|
6094
6100
|
}
|
|
6101
|
+
function loadDeliveryAttempts() {
|
|
6102
|
+
const out = new Map;
|
|
6103
|
+
try {
|
|
6104
|
+
if (!existsSync6(DELIVERY_DEDUPE_FILE))
|
|
6105
|
+
return out;
|
|
6106
|
+
const raw = JSON.parse(readFileSync8(DELIVERY_DEDUPE_FILE, "utf-8"));
|
|
6107
|
+
const now = Date.now();
|
|
6108
|
+
for (const entry of raw) {
|
|
6109
|
+
if (typeof entry?.key === "string" && Number.isFinite(entry.at) && now - entry.at < DELIVERY_DEDUPE_TTL_MS) {
|
|
6110
|
+
out.set(entry.key, { text: entry.key, at: entry.at });
|
|
6111
|
+
}
|
|
6112
|
+
}
|
|
6113
|
+
} catch {}
|
|
6114
|
+
return out;
|
|
6115
|
+
}
|
|
6116
|
+
function saveDeliveryAttempts(entries) {
|
|
6117
|
+
const raw = [...entries.values()].filter((entry) => Date.now() - entry.at < DELIVERY_DEDUPE_TTL_MS).map((entry) => ({ key: entry.text, at: entry.at }));
|
|
6118
|
+
mkdirSync6(join8(DELIVERY_DEDUPE_FILE, ".."), { recursive: true });
|
|
6119
|
+
const tmp = `${DELIVERY_DEDUPE_FILE}.tmp`;
|
|
6120
|
+
writeFileSync8(tmp, JSON.stringify(raw, null, 2), { mode: 384 });
|
|
6121
|
+
renameSync(tmp, DELIVERY_DEDUPE_FILE);
|
|
6122
|
+
}
|
|
6095
6123
|
function selectRideMsgId(last, ctx, now, windowMs) {
|
|
6096
6124
|
if (!last?.msgId)
|
|
6097
6125
|
return;
|
|
@@ -6118,7 +6146,7 @@ function createDelivery(config, lastCtx) {
|
|
|
6118
6146
|
})();
|
|
6119
6147
|
const pushQueues = new Map;
|
|
6120
6148
|
const undelivered = new Set;
|
|
6121
|
-
const recentDeliveries =
|
|
6149
|
+
const recentDeliveries = loadDeliveryAttempts();
|
|
6122
6150
|
const activeQuota = new Map;
|
|
6123
6151
|
const passiveLimiter = new ReplyLimiter;
|
|
6124
6152
|
for (const [uid, q] of Object.entries(loadQuotaMap()))
|
|
@@ -6161,18 +6189,20 @@ function createDelivery(config, lastCtx) {
|
|
|
6161
6189
|
}
|
|
6162
6190
|
async function deliver(ctx, text) {
|
|
6163
6191
|
const now = Date.now();
|
|
6164
|
-
|
|
6165
|
-
|
|
6192
|
+
const attemptKey = deliveryAttemptKey(ctx.userId, text);
|
|
6193
|
+
if (shouldSuppressDuplicate(recentDeliveries.get(attemptKey), attemptKey, now)) {
|
|
6194
|
+
console.log(`[bridge] 同文长回复已在 24h 内投递,跳过重复出站 user=${ctx.userId}`);
|
|
6166
6195
|
return;
|
|
6167
6196
|
}
|
|
6168
|
-
recentDeliveries.set(
|
|
6197
|
+
recentDeliveries.set(attemptKey, { text: attemptKey, at: now });
|
|
6169
6198
|
const hadNotice = undelivered.delete(ctx.userId);
|
|
6170
6199
|
const body = hadNotice ? UNDELIVERED_NOTICE + text : text;
|
|
6171
6200
|
try {
|
|
6172
6201
|
await deliverCore(ctx, body);
|
|
6202
|
+
saveDeliveryAttempts(recentDeliveries);
|
|
6173
6203
|
} catch (error) {
|
|
6174
|
-
if (recentDeliveries.get(
|
|
6175
|
-
recentDeliveries.delete(
|
|
6204
|
+
if (recentDeliveries.get(attemptKey)?.at === now)
|
|
6205
|
+
recentDeliveries.delete(attemptKey);
|
|
6176
6206
|
if (hadNotice)
|
|
6177
6207
|
undelivered.add(ctx.userId);
|
|
6178
6208
|
throw error;
|
|
@@ -6587,7 +6617,7 @@ function createFormBridge(deps) {
|
|
|
6587
6617
|
}
|
|
6588
6618
|
|
|
6589
6619
|
// src/core/inflight-journal.ts
|
|
6590
|
-
import { existsSync as existsSync7, readFileSync as readFileSync9, writeFileSync as writeFileSync9, mkdirSync as
|
|
6620
|
+
import { existsSync as existsSync7, readFileSync as readFileSync9, writeFileSync as writeFileSync9, mkdirSync as mkdirSync7, renameSync as renameSync2, openSync, closeSync, unlinkSync as unlinkSync2, statSync as statSync3 } from "fs";
|
|
6591
6621
|
import { join as join9 } from "path";
|
|
6592
6622
|
import { homedir as homedir8 } from "os";
|
|
6593
6623
|
var INFLIGHT_MAX_AGE_MS = 24 * 60 * 60 * 1000;
|
|
@@ -6613,11 +6643,11 @@ function load() {
|
|
|
6613
6643
|
}
|
|
6614
6644
|
function save(entries) {
|
|
6615
6645
|
const path = journalPath();
|
|
6616
|
-
|
|
6646
|
+
mkdirSync7(join9(path, ".."), { recursive: true });
|
|
6617
6647
|
const tmp = `${path}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
6618
6648
|
try {
|
|
6619
6649
|
writeFileSync9(tmp, JSON.stringify(entries, null, 2), { mode: 384 });
|
|
6620
|
-
|
|
6650
|
+
renameSync2(tmp, path);
|
|
6621
6651
|
} catch (error) {
|
|
6622
6652
|
try {
|
|
6623
6653
|
unlinkSync2(tmp);
|
|
@@ -6630,7 +6660,7 @@ var JOURNAL_LOCK_WAIT_MS = 5000;
|
|
|
6630
6660
|
var sleepArray = new Int32Array(new SharedArrayBuffer(4));
|
|
6631
6661
|
function withJournalLock(fn) {
|
|
6632
6662
|
const path = journalPath();
|
|
6633
|
-
|
|
6663
|
+
mkdirSync7(join9(path, ".."), { recursive: true });
|
|
6634
6664
|
const lockPath = `${path}.lock`;
|
|
6635
6665
|
const token = `${process.pid}:${Date.now()}:${Math.random().toString(36).slice(2)}`;
|
|
6636
6666
|
const deadline = Date.now() + JOURNAL_LOCK_WAIT_MS;
|
|
@@ -7354,7 +7384,7 @@ function loadGreetedSet() {
|
|
|
7354
7384
|
}
|
|
7355
7385
|
function saveGreetedSet(set) {
|
|
7356
7386
|
try {
|
|
7357
|
-
|
|
7387
|
+
mkdirSync8(join10(homedir9(), ".openqq"), { recursive: true });
|
|
7358
7388
|
writeFileSync10(GREETED_FILE, JSON.stringify([...set]));
|
|
7359
7389
|
} catch (error) {
|
|
7360
7390
|
console.error("[bridge] 保存 greeted 失败:", error);
|
|
@@ -7406,7 +7436,7 @@ function toErrorMessage2(error) {
|
|
|
7406
7436
|
}
|
|
7407
7437
|
|
|
7408
7438
|
// src/core/instance-lock.ts
|
|
7409
|
-
import { openSync as openSync2, writeFileSync as writeFileSync11, closeSync as closeSync2, unlinkSync as unlinkSync3, statSync as statSync4, utimesSync, mkdirSync as
|
|
7439
|
+
import { openSync as openSync2, writeFileSync as writeFileSync11, closeSync as closeSync2, unlinkSync as unlinkSync3, statSync as statSync4, utimesSync, mkdirSync as mkdirSync9 } from "fs";
|
|
7410
7440
|
import { join as join11 } from "path";
|
|
7411
7441
|
import { homedir as homedir10 } from "os";
|
|
7412
7442
|
var INSTANCE_LOCK_DISABLED_ENV = "QQ_INSTANCE_LOCK";
|
|
@@ -7422,7 +7452,7 @@ function touch(path) {
|
|
|
7422
7452
|
}
|
|
7423
7453
|
function acquireInstanceLockAt(path, staleMs = LOCK_STALE_MS) {
|
|
7424
7454
|
try {
|
|
7425
|
-
|
|
7455
|
+
mkdirSync9(join11(path, ".."), { recursive: true });
|
|
7426
7456
|
const fd = openSync2(path, "wx");
|
|
7427
7457
|
writeFileSync11(fd, JSON.stringify({ pid: process.pid, startedAt: Date.now() }));
|
|
7428
7458
|
closeSync2(fd);
|
|
@@ -7559,7 +7589,7 @@ async function main() {
|
|
|
7559
7589
|
const heartbeatFile = join13(homedir11(), ".openqq", "heartbeat");
|
|
7560
7590
|
const touchHeartbeat = () => {
|
|
7561
7591
|
try {
|
|
7562
|
-
|
|
7592
|
+
mkdirSync11(join13(homedir11(), ".openqq"), { recursive: true });
|
|
7563
7593
|
writeFileSync12(heartbeatFile, String(Date.now()));
|
|
7564
7594
|
} catch (error) {
|
|
7565
7595
|
console.error("[index] 心跳写入失败:", error);
|
package/package.json
CHANGED
package/src/core/delivery.ts
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// @output: createDelivery
|
|
3
3
|
// @pos: 根层/core - 投递域(O7c 2026-10-15 自 bridge.ts 拆出:统一投递
|
|
4
4
|
// 被动→主动降级、日配额记账与落盘、观察推送节流队列、主动/被动通道发送)
|
|
5
|
-
import { existsSync, readFileSync, writeFileSync } from "fs"
|
|
5
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from "fs"
|
|
6
6
|
import { join } from "path"
|
|
7
|
+
import { createHash } from "node:crypto"
|
|
7
8
|
import { homedir } from "os"
|
|
8
9
|
import type { Config } from "../config.js"
|
|
9
10
|
import type { MessageContext } from "../qq/types.js"
|
|
@@ -61,11 +62,51 @@ export interface DeliveryAttempt {
|
|
|
61
62
|
at: number
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
export const DELIVERY_DEDUPE_TTL_MS = 24 * 60 * 60 * 1000
|
|
66
|
+
const DELIVERY_DEDUPE_FILE = process.env.QQ_DELIVERY_DEDUPE_FILE || join(homedir(), ".openqq", "delivery-dedupe.json")
|
|
67
|
+
|
|
68
|
+
function deliveryAttemptKey(userId: string, text: string): string {
|
|
69
|
+
return `${userId}:${createHash("sha256").update(text).digest("hex")}`
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** AI 长回复的同文幂等:不同 location/重复回合可产生不同事件 ID,最终统一在出站拦截。 */
|
|
73
|
+
export function shouldSuppressDuplicate(
|
|
74
|
+
previous: DeliveryAttempt | undefined,
|
|
75
|
+
text: string,
|
|
76
|
+
now: number,
|
|
77
|
+
ttlMs = DELIVERY_DEDUPE_TTL_MS,
|
|
78
|
+
): boolean {
|
|
66
79
|
return text.length >= 200 && previous?.text === text && now - previous.at < ttlMs
|
|
67
80
|
}
|
|
68
81
|
|
|
82
|
+
// 仅存用户键+内容哈希,不把回复正文写进运行数据。跨重启也能识别同文回复。
|
|
83
|
+
function loadDeliveryAttempts(): Map<string, DeliveryAttempt> {
|
|
84
|
+
const out = new Map<string, DeliveryAttempt>()
|
|
85
|
+
try {
|
|
86
|
+
if (!existsSync(DELIVERY_DEDUPE_FILE)) return out
|
|
87
|
+
const raw = JSON.parse(readFileSync(DELIVERY_DEDUPE_FILE, "utf-8")) as Array<{ key: string; at: number }>
|
|
88
|
+
const now = Date.now()
|
|
89
|
+
for (const entry of raw) {
|
|
90
|
+
if (typeof entry?.key === "string" && Number.isFinite(entry.at) && now - entry.at < DELIVERY_DEDUPE_TTL_MS) {
|
|
91
|
+
out.set(entry.key, { text: entry.key, at: entry.at })
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} catch {
|
|
95
|
+
// 损坏则仅本轮无历史去重,不阻塞启动
|
|
96
|
+
}
|
|
97
|
+
return out
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function saveDeliveryAttempts(entries: ReadonlyMap<string, DeliveryAttempt>): void {
|
|
101
|
+
const raw = [...entries.values()]
|
|
102
|
+
.filter((entry) => Date.now() - entry.at < DELIVERY_DEDUPE_TTL_MS)
|
|
103
|
+
.map((entry) => ({ key: entry.text, at: entry.at }))
|
|
104
|
+
mkdirSync(join(DELIVERY_DEDUPE_FILE, ".."), { recursive: true })
|
|
105
|
+
const tmp = `${DELIVERY_DEDUPE_FILE}.tmp`
|
|
106
|
+
writeFileSync(tmp, JSON.stringify(raw, null, 2), { mode: 0o600 })
|
|
107
|
+
renameSync(tmp, DELIVERY_DEDUPE_FILE)
|
|
108
|
+
}
|
|
109
|
+
|
|
69
110
|
// B-msgid 纯函数(导出供测试,2026-10-29):挑选可"骑"的被动 msg_id——
|
|
70
111
|
// 同一聊天类型、时间戳在窗口内才有效;任何不对齐都返回 undefined(回落真主动)。
|
|
71
112
|
export function selectRideMsgId(
|
|
@@ -121,8 +162,8 @@ export function createDelivery(
|
|
|
121
162
|
const pushQueues = new Map<string, PushQueue>()
|
|
122
163
|
// P 项:彻底投递失败的用户集合(贴条状态),下次成功投递时消化
|
|
123
164
|
const undelivered = new Set<string>()
|
|
124
|
-
//
|
|
125
|
-
const recentDeliveries =
|
|
165
|
+
// 同文长回复幂等(跨重启 24h);尝试前占位防并发,失败立即释放允许重试。
|
|
166
|
+
const recentDeliveries = loadDeliveryAttempts()
|
|
126
167
|
const activeQuota = new Map<string, { day: string; count: number; warned: boolean }>()
|
|
127
168
|
// B1(2026-10-29,吸收自 openclaw-qqbot reply-limiter):被动回复限额预判——
|
|
128
169
|
// 单 msgId 4 次/1h 窗口,耗尽/过期直接转主动,省一次注定被拒的 API 往返。
|
|
@@ -173,18 +214,20 @@ export function createDelivery(
|
|
|
173
214
|
// 投递失败则把贴条放回(留待下次),成功才算消化。
|
|
174
215
|
async function deliver(ctx: MessageContext, text: string): Promise<void> {
|
|
175
216
|
const now = Date.now()
|
|
176
|
-
|
|
177
|
-
|
|
217
|
+
const attemptKey = deliveryAttemptKey(ctx.userId, text)
|
|
218
|
+
if (shouldSuppressDuplicate(recentDeliveries.get(attemptKey), attemptKey, now)) {
|
|
219
|
+
console.log(`[bridge] 同文长回复已在 24h 内投递,跳过重复出站 user=${ctx.userId}`)
|
|
178
220
|
return
|
|
179
221
|
}
|
|
180
|
-
recentDeliveries.set(
|
|
222
|
+
recentDeliveries.set(attemptKey, { text: attemptKey, at: now })
|
|
181
223
|
const hadNotice = undelivered.delete(ctx.userId)
|
|
182
224
|
const body = hadNotice ? UNDELIVERED_NOTICE + text : text
|
|
183
225
|
try {
|
|
184
226
|
await deliverCore(ctx, body)
|
|
227
|
+
saveDeliveryAttempts(recentDeliveries)
|
|
185
228
|
} catch (error) {
|
|
186
229
|
// 发送失败不占去重位,后续恢复/重试仍可送达。
|
|
187
|
-
if (recentDeliveries.get(
|
|
230
|
+
if (recentDeliveries.get(attemptKey)?.at === now) recentDeliveries.delete(attemptKey)
|
|
188
231
|
if (hadNotice) undelivered.add(ctx.userId)
|
|
189
232
|
throw error
|
|
190
233
|
}
|