dsh-wechat-access 0.0.0-stage → 0.0.2
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 +56 -0
- package/LICENSE +675 -0
- package/README.md +278 -2
- package/cordis.patch.yml +5 -0
- package/examples/config.example.json +98 -0
- package/index.mjs +123 -0
- package/lib/access.mjs +131 -0
- package/lib/anysearch.mjs +143 -0
- package/lib/capabilities.mjs +312 -0
- package/lib/client.js +2532 -0
- package/lib/config.mjs +509 -0
- package/lib/emotion-store.mjs +352 -0
- package/lib/emotion.mjs +428 -0
- package/lib/endocrine/LICENSE +21 -0
- package/lib/endocrine/README.md +39 -0
- package/lib/endocrine/diseases.js +791 -0
- package/lib/endocrine/engine.mjs +131 -0
- package/lib/endocrine/store.mjs +253 -0
- package/lib/endocrine/upstream.mjs +1230 -0
- package/lib/group-gate.mjs +305 -0
- package/lib/imagegen.mjs +360 -0
- package/lib/lock.mjs +204 -0
- package/lib/meme-classify.mjs +190 -0
- package/lib/memes.mjs +442 -0
- package/lib/memory.mjs +437 -0
- package/lib/normalize.mjs +230 -0
- package/lib/parser.mjs +602 -0
- package/lib/reply-settled.mjs +130 -0
- package/lib/runtime.mjs +3367 -0
- package/lib/startup.mjs +70 -0
- package/lib/steam-binding.mjs +144 -0
- package/lib/steam.mjs +334 -0
- package/lib/store.mjs +259 -0
- package/lib/thoughts.mjs +149 -0
- package/lib/time-context.mjs +182 -0
- package/lib/tools.mjs +583 -0
- package/lib/ui.mjs +997 -0
- package/lib/util.mjs +489 -0
- package/lib/virtual-life-generate.mjs +421 -0
- package/lib/virtual-life.mjs +971 -0
- package/lib/weather.mjs +172 -0
- package/lib/wechat-client.mjs +476 -0
- package/lib/wechat-events.mjs +269 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「这条入站消息已经放弃回复」的小记录。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:
|
|
5
|
+
* 回合超时(agent.whenIdle() 超过上限)时,这一轮什么都没发出去。
|
|
6
|
+
* 恢复逻辑 restorePending 只比较归档里「最后一条入站」和「最后一条出站」的时间,
|
|
7
|
+
* 超时没有出站记录,于是每一轮刷新都判定「这条还没回」,把同一条消息再送进模型 ——
|
|
8
|
+
* 在 10 分钟窗口里反复烧额度,而且可能收到几条互相矛盾的回复。
|
|
9
|
+
*
|
|
10
|
+
* 为什么不写进聊天归档:
|
|
11
|
+
* 归档会被当成「她说过的话」注入下一轮上下文。写一条假的出站消息进去,
|
|
12
|
+
* 等于让她以为自己已经回过这句话了,比不回复更糟。
|
|
13
|
+
* 所以单独放一个小文件,只用来回答一个问题:这条入站是不是已经处理过了。
|
|
14
|
+
*
|
|
15
|
+
* 存储形状(dataDir/reply-settled.json):
|
|
16
|
+
* { "version": 1, "entries": { "<kind>:<peerId>:<messageId>": { "reason": "timeout", "at": 1794... } } }
|
|
17
|
+
*/
|
|
18
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
const VERSION = 1;
|
|
22
|
+
/** 最多留多少条。这是「最近放弃过什么」的短期记忆,不需要永久保留。 */
|
|
23
|
+
const MAX_ENTRIES = 2000;
|
|
24
|
+
/** 超过这个时长的记录可以直接丢:恢复窗口本身只有 10 分钟。 */
|
|
25
|
+
const MAX_AGE_MS = 86_400_000;
|
|
26
|
+
|
|
27
|
+
export class ReplySettledStore {
|
|
28
|
+
constructor({ dataDir, logger = console } = {}) {
|
|
29
|
+
this.file = join(dataDir, 'reply-settled.json');
|
|
30
|
+
this.logger = logger;
|
|
31
|
+
this.entries = {};
|
|
32
|
+
this.loaded = false;
|
|
33
|
+
this.writeChain = Promise.resolve();
|
|
34
|
+
// 合并写盘:整份 entries 每次都全量重写,单次最大 2000 条。
|
|
35
|
+
// 补漏那批消息是一条条进来的(一次轮询最多 10 条),逐条写就会把
|
|
36
|
+
// 同一个文件连着重写十几次,纯属浪费。攒一拍再写一次。
|
|
37
|
+
this.dirty = false;
|
|
38
|
+
this.saveScheduled = false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async init() {
|
|
42
|
+
if (this.loaded) return this;
|
|
43
|
+
await mkdir(dirname(this.file), { recursive: true });
|
|
44
|
+
try {
|
|
45
|
+
const raw = JSON.parse(await readFile(this.file, 'utf8'));
|
|
46
|
+
if (raw?.version === VERSION && raw.entries && typeof raw.entries === 'object') {
|
|
47
|
+
this.entries = raw.entries;
|
|
48
|
+
}
|
|
49
|
+
} catch { /* 文件不存在或坏了就从空开始 —— 这一层是锦上添花,不能反过来拦住启动 */ }
|
|
50
|
+
this.#prune();
|
|
51
|
+
this.loaded = true;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#key(kind, peerId, messageId) {
|
|
56
|
+
return `${String(kind ?? '')}:${String(peerId ?? '')}:${String(messageId ?? '')}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#prune() {
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
const keep = Object.entries(this.entries)
|
|
62
|
+
.filter(([, v]) => v && now - Number(v.at ?? 0) < MAX_AGE_MS)
|
|
63
|
+
.sort((a, b) => Number(b[1].at ?? 0) - Number(a[1].at ?? 0))
|
|
64
|
+
.slice(0, MAX_ENTRIES);
|
|
65
|
+
this.entries = Object.fromEntries(keep);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#save() {
|
|
69
|
+
this.writeChain = this.writeChain.then(async () => {
|
|
70
|
+
await mkdir(dirname(this.file), { recursive: true });
|
|
71
|
+
const tmp = this.file + '.tmp';
|
|
72
|
+
await writeFile(tmp, JSON.stringify({ version: VERSION, entries: this.entries }, null, 2) + '\n');
|
|
73
|
+
await rename(tmp, this.file);
|
|
74
|
+
}).catch((error) => this.logger.warn?.('回复放弃记录写入失败:' + error.message));
|
|
75
|
+
return this.writeChain;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 这条入站是不是已经决定不回了。 */
|
|
79
|
+
has(kind, peerId, messageId) {
|
|
80
|
+
if (messageId === undefined || messageId === null || String(messageId) === '') return false;
|
|
81
|
+
return Boolean(this.entries[this.#key(kind, peerId, messageId)]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 记一条「放弃回复」。
|
|
86
|
+
*
|
|
87
|
+
* 写盘失败只记日志,不抛 —— 调用点在超时收尾路径上,
|
|
88
|
+
* 那里再抛一次异常只会让状态更乱。写失败的最坏结果是多问一轮模型,
|
|
89
|
+
* 比让整轮回复崩掉要轻。
|
|
90
|
+
*/
|
|
91
|
+
async mark(kind, peerId, messageId, reason = 'timeout') {
|
|
92
|
+
if (messageId === undefined || messageId === null || String(messageId) === '') return false;
|
|
93
|
+
// 同一秒内重复标记同一条是可能的(超时收尾 + 恢复逻辑),幂等即可。
|
|
94
|
+
this.entries[this.#key(kind, peerId, messageId)] = { reason: String(reason).slice(0, 40), at: Date.now() };
|
|
95
|
+
this.#prune();
|
|
96
|
+
// 内存已生效,has() 立刻能看到;写盘攒一拍。
|
|
97
|
+
// 代价是进程被强杀时可能丢最后几条 —— 这只影响「多问一轮模型」,不影响正确性。
|
|
98
|
+
this.#scheduleSave();
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 攒一小段时间再写一次,同一批 mark 只落一次盘。
|
|
104
|
+
*
|
|
105
|
+
* 不能用 queueMicrotask:accept() 每条消息之间都隔着 await(归档写盘),
|
|
106
|
+
* microtask 在下一个 await 前就跑了,等于每条各写一次,完全没合并到。
|
|
107
|
+
* 50ms 足够把一次轮询里那 10 条攒成一笔。
|
|
108
|
+
*/
|
|
109
|
+
#scheduleSave() {
|
|
110
|
+
this.dirty = true;
|
|
111
|
+
if (this.saveScheduled) return;
|
|
112
|
+
this.saveScheduled = true;
|
|
113
|
+
const timer = setTimeout(() => {
|
|
114
|
+
this.saveScheduled = false;
|
|
115
|
+
if (!this.dirty) return;
|
|
116
|
+
this.dirty = false;
|
|
117
|
+
this.#save();
|
|
118
|
+
}, 50);
|
|
119
|
+
timer.unref?.();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async flush() {
|
|
123
|
+
// 有攒着没写的就先写一次,否则 stop() 之后这几条会丢。
|
|
124
|
+
if (this.dirty) {
|
|
125
|
+
this.dirty = false;
|
|
126
|
+
this.#save();
|
|
127
|
+
}
|
|
128
|
+
await this.writeChain;
|
|
129
|
+
}
|
|
130
|
+
}
|