dsh-fish-soul 0.19.2 → 0.19.5
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 +1 -1
- package/lib/behavior.js +6 -4
- package/lib/channels/douyin-pure.js +68 -0
- package/lib/channels/douyin.js +86 -92
- package/lib/channels/vendor/douyin-bridge/browser.mjs +1 -2
- package/lib/channels/vendor/douyin-bridge/douyin-api.mjs +54 -16
- package/lib/channels/vendor/douyin-bridge/douyin-comment.js +65 -5
- package/lib/channels/vendor/douyin-bridge/douyin-dm.mjs +153 -94
- package/lib/channels/vendor/douyin-bridge/douyin-panel.mjs +5 -2
- package/lib/channels/vendor/douyin-bridge/media.mjs +8 -1
- package/lib/client.douyin.js +3 -1
- package/lib/config-platforms.js +3 -1
- package/lib/douyin-adapter.js +27 -22
- package/lib/douyin-browse.js +92 -15
- package/lib/douyin-delivery.js +1 -1
- package/lib/douyin-share-inline.js +103 -2
- package/lib/douyin-social.js +147 -37
- package/lib/gateway-dispatch.js +5 -2
- package/lib/gateway-media.js +10 -2
- package/lib/gateway-session.js +3 -1
- package/lib/gateway.js +59 -16
- package/lib/index-tools.js +36 -3
- package/lib/index.js +5 -4
- package/lib/routes-douyin.js +1 -1
- package/lib/social.js +3 -3
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/behavior.js
CHANGED
|
@@ -290,18 +290,20 @@ export function normSentText(s) {
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
/**
|
|
293
|
-
*
|
|
293
|
+
* 跨条复读守卫(纯逻辑):模型遇到相近的输入时偶尔把自己上一条消息一字不差或换个近义说法再发一遍
|
|
294
294
|
* (2026-11-06 QQ 真实翻车:同一段内容隔 17 秒 / 43 秒各发了一次)。
|
|
295
295
|
* history: [{ at, norm }](最近发出去的,norm 已规整化);paragraphs 里规整化后
|
|
296
|
-
* ≥ minLen 字、且跟窗口 windowMs
|
|
296
|
+
* ≥ minLen 字、且跟窗口 windowMs 内某条完全一致或二元组相似度 ≥ threshold 的,返回它们的下标。
|
|
297
297
|
* 短句允许重复("哈哈哈哈"这种不该误杀)。
|
|
298
298
|
*/
|
|
299
|
-
export function findRepeatedParagraphs(history, paragraphs, { now = Date.now(), windowMs = 120000, minLen = 12 } = {}) {
|
|
299
|
+
export function findRepeatedParagraphs(history, paragraphs, { now = Date.now(), windowMs = 120000, minLen = 12, threshold = 0.8 } = {}) {
|
|
300
300
|
const recent = (Array.isArray(history) ? history : []).filter((h) => now - Number(h?.at ?? 0) <= windowMs);
|
|
301
301
|
const dups = [];
|
|
302
302
|
(Array.isArray(paragraphs) ? paragraphs : []).forEach((p, i) => {
|
|
303
303
|
const n = normSentText(p);
|
|
304
|
-
if ([...n].length >= minLen && recent.some((h) => h.norm === n)
|
|
304
|
+
if ([...n].length >= minLen && recent.some((h) => h.norm === n || ([...String(h?.norm ?? '')].length >= minLen && bigramDice(h.norm, n) >= threshold))) {
|
|
305
|
+
dups.push(i);
|
|
306
|
+
}
|
|
305
307
|
});
|
|
306
308
|
return dups;
|
|
307
309
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
2
|
import { parseTime } from './vendor/douyin-bridge/douyin-util.mjs';
|
|
3
|
+
import { parseSingleConversation } from '../douyin-conversation.js';
|
|
3
4
|
|
|
4
5
|
export const DOUYIN_HOME = 'https://www.douyin.com/';
|
|
5
6
|
/**
|
|
@@ -266,3 +267,70 @@ export function selfUidFromKv(raw) {
|
|
|
266
267
|
export function stripQuotes(raw) {
|
|
267
268
|
return String(raw ?? '').trim().replace(/^["'](.*)["']$/, '$1').trim();
|
|
268
269
|
}
|
|
270
|
+
|
|
271
|
+
/** 从私信/协议/分享卡字段提取入站展示文案、图片和链接。 */
|
|
272
|
+
export function formatDouyinMedia(m) {
|
|
273
|
+
const media = m.media ?? {};
|
|
274
|
+
const images = [];
|
|
275
|
+
const links = [];
|
|
276
|
+
let text = String(m.text ?? '').trim();
|
|
277
|
+
if (m.type === 'image') {
|
|
278
|
+
const src = String(media.data || media.src || '');
|
|
279
|
+
const w = Number(media.width) || 0;
|
|
280
|
+
const h = Number(media.height) || 0;
|
|
281
|
+
const mime = String(media.mime || media.format || media.type || '').toLowerCase();
|
|
282
|
+
const looksLikeSticker = (w > 0 && h > 0 && Math.max(w, h) <= 300) || /gif|webp/.test(mime);
|
|
283
|
+
if (src) images.push(looksLikeSticker ? { url: src, sticker: true } : { url: src });
|
|
284
|
+
text = looksLikeSticker ? '【表情】' : '【图片】';
|
|
285
|
+
} else if (m.type === 'emoji') {
|
|
286
|
+
const n = String(media.name ?? '').trim();
|
|
287
|
+
const src = String(media.data || media.src || '');
|
|
288
|
+
if (src) images.push({ url: src, sticker: true });
|
|
289
|
+
text = n ? `[${n}]` : '【表情】';
|
|
290
|
+
} else if (m.type === 'voice') {
|
|
291
|
+
text = `【语音${media.seconds ? ` ${media.seconds} 秒` : ''}(听不了)】`;
|
|
292
|
+
} else if (m.type === 'video') {
|
|
293
|
+
const src = String(media.data || media.src || '');
|
|
294
|
+
if (src) images.push({ url: src });
|
|
295
|
+
text = '【视频】';
|
|
296
|
+
} else if (m.type === 'location' || m.mediaType === 'location') {
|
|
297
|
+
const loc = String(media.name || m.mediaHint || '').trim();
|
|
298
|
+
text = loc ? `【位置:${loc}】` : '【位置】';
|
|
299
|
+
} else if (m.mediaType === 'emoji') {
|
|
300
|
+
text = m.mediaHint ? `[${m.mediaHint}]` : (text || '【表情】');
|
|
301
|
+
} else if (m.mediaType === 'voice') {
|
|
302
|
+
text = `【语音${m.mediaHint ? ` ${m.mediaHint} 秒` : ''}(听不了)】`;
|
|
303
|
+
} else if (m.mediaType === 'image' && text === '') {
|
|
304
|
+
text = '【图片】';
|
|
305
|
+
} else {
|
|
306
|
+
text = text.replace(/【/g, '〔').replace(/】/g, '〕');
|
|
307
|
+
}
|
|
308
|
+
if (m.shareAweme) {
|
|
309
|
+
links.push(`https://www.douyin.com/video/${m.shareAweme}`);
|
|
310
|
+
const meta = [m.shareAuthor, m.shareTitle].filter(Boolean).join(' · ').slice(0, 80);
|
|
311
|
+
if (meta && !text.includes(meta.slice(0, 12))) text = text ? `${text}(${meta})` : meta;
|
|
312
|
+
if (!/分享/.test(text)) text = `【分享视频】${text}`;
|
|
313
|
+
}
|
|
314
|
+
return { text, images, links };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** 从单聊消息推导出自己的 uid(严格分离显式 fromMe 与未知 null)。 */
|
|
318
|
+
export function inferSelfUidFromMessages(convId, msgs) {
|
|
319
|
+
const pair = parseSingleConversation(convId);
|
|
320
|
+
if (pair === null) return null;
|
|
321
|
+
const sidesOf = (pred) => {
|
|
322
|
+
const set = new Set();
|
|
323
|
+
for (const m of msgs) {
|
|
324
|
+
const sid = String(m?.senderId ?? '').trim();
|
|
325
|
+
if (sid !== '' && (sid === pair.a || sid === pair.b) && pred(m)) set.add(sid);
|
|
326
|
+
}
|
|
327
|
+
return set.size === 1 ? [...set][0] : '';
|
|
328
|
+
};
|
|
329
|
+
const peer = sidesOf((m) => m?.fromMe === false);
|
|
330
|
+
if (peer !== '') return { uid: peer === pair.a ? pair.b : pair.a, via: 'peer' };
|
|
331
|
+
const mine = sidesOf((m) => m?.fromMe === true);
|
|
332
|
+
if (mine !== '') return { uid: mine, via: 'mine' };
|
|
333
|
+
if (msgs.some((m) => typeof m?.fromMe === 'boolean')) return null;
|
|
334
|
+
const unknownPeer = sidesOf(() => true);
|
|
335
|
+
return unknownPeer !== '' ? { uid: unknownPeer === pair.a ? pair.b : pair.a, via: 'peer' } : null;
|
|
336
|
+
}
|
package/lib/channels/douyin.js
CHANGED
|
@@ -2,7 +2,8 @@ import { sleep } from '../time-util.js';
|
|
|
2
2
|
import { DouyinInbox } from '../douyin-inbox.js';
|
|
3
3
|
import {
|
|
4
4
|
parseSingleConversation, isSingleConversation, conversationIdentity,
|
|
5
|
-
senderBelongsToConversation, peerUidFromConversation, isDouyinConversationId
|
|
5
|
+
senderBelongsToConversation, peerUidFromConversation, isDouyinConversationId,
|
|
6
|
+
GROUP_CONV_RE
|
|
6
7
|
} from '../douyin-conversation.js';
|
|
7
8
|
/**
|
|
8
9
|
* douyin.js —— 抖音小号:专用浏览器(Edge / Chrome …,CDP 控制)+ 网页版私信渠道。
|
|
@@ -27,7 +28,7 @@ import {
|
|
|
27
28
|
DOUYIN_HOME, DOUYIN_IDENTITY_BLOCKED, errText, sha1,
|
|
28
29
|
convTimeMs, messageFingerprint, newFrom, occurrenceIndexes, messageIdFor,
|
|
29
30
|
isGroupConversation, pushFrameVerdict, matchConversationByPreview, groupSender,
|
|
30
|
-
selfUidFromKv, stripQuotes
|
|
31
|
+
selfUidFromKv, stripQuotes, formatDouyinMedia, inferSelfUidFromMessages
|
|
31
32
|
} from './douyin-pure.js';
|
|
32
33
|
import { DouyinHost } from './douyin-host.js';
|
|
33
34
|
|
|
@@ -92,44 +93,10 @@ export function normalizeDouyinMessage(m, { name, timeMs, messageId, group = fal
|
|
|
92
93
|
// 那种按「对方发的」处理(保持原有契约:不知道就当成要处理的消息)。
|
|
93
94
|
// 只有**明确是自己发的**(true)才拒。注入那侧已收紧(eligible 只认 === false)。
|
|
94
95
|
if (!m || m.fromMe === true || m.system || m.unsupported) return null;
|
|
95
|
-
|
|
96
|
-
const images = [];
|
|
97
|
-
const links = [];
|
|
98
|
-
let text = String(m.text ?? '').trim();
|
|
99
|
-
if (m.type === 'image') {
|
|
100
|
-
const src = String(media.data || media.src || '');
|
|
101
|
-
// 🔴 抖音的「图片表情」(那个柯基狗那种)在协议里就是 type:'image',
|
|
102
|
-
// 和普通照片分不开(decodeMessageNode 解出的 media 只有 {uri,width,height},
|
|
103
|
-
// DOM 路靠 CSS class 区分,而图片表情渲染在 MessageItemImage* 下)。
|
|
104
|
-
// 所以这里用启发式:小图 / gif / webp 大概率是表情包,标成 sticker,
|
|
105
|
-
// 让模型知道「这可能是个表情,想收就写 [收藏表情:名字]」。
|
|
106
|
-
// 阈值 300px 是拍的(真机没有尺寸样本可校准)——宁可多标(模型自己判断收不收),
|
|
107
|
-
// 也不要漏标(漏了它永远不知道能收藏)。
|
|
108
|
-
const w = Number(media.width) || 0;
|
|
109
|
-
const h = Number(media.height) || 0;
|
|
110
|
-
const mime = String(media.mime || media.format || media.type || '').toLowerCase();
|
|
111
|
-
const looksLikeSticker = (w > 0 && h > 0 && Math.max(w, h) <= 300) || /gif|webp/.test(mime);
|
|
112
|
-
if (src) images.push(looksLikeSticker ? { url: src, sticker: true } : { url: src });
|
|
113
|
-
text = looksLikeSticker ? '【表情】' : '【图片】';
|
|
114
|
-
} else if (m.type === 'emoji') {
|
|
115
|
-
const n = String(media.name ?? '').trim();
|
|
116
|
-
// 表情的图**一并推给模型**:她得先"看见"这张表情,才会想着收藏 / 以后发出来。
|
|
117
|
-
// 带名字的(面板里的比心 / 捂脸…)以前只变成一行 [比心] 文本,模型看不到图。
|
|
118
|
-
// 标成表情则是给收藏那条路认 —— 收藏时优先挑表情,面板上也能认出来。
|
|
119
|
-
const src = String(media.data || media.src || '');
|
|
120
|
-
if (src) images.push({ url: src, sticker: true });
|
|
121
|
-
if (n) text = `[${n}]`;
|
|
122
|
-
else text = '【表情】';
|
|
123
|
-
} else if (m.type === 'voice') {
|
|
124
|
-
text = `【语音${media.seconds ? ` ${media.seconds} 秒` : ''}(听不了)】`;
|
|
125
|
-
} else {
|
|
126
|
-
text = text.replace(/【/g, '〔').replace(/】/g, '〕');
|
|
127
|
-
}
|
|
128
|
-
if (m.shareAweme) {
|
|
129
|
-
links.push(`https://www.douyin.com/video/${m.shareAweme}`);
|
|
130
|
-
if (!/分享/.test(text)) text = `【分享视频】${text}`;
|
|
131
|
-
}
|
|
96
|
+
let { text, images, links } = formatDouyinMedia(m);
|
|
132
97
|
if (text === '' && images.length === 0) return null;
|
|
98
|
+
const refText = String(m.refText ?? '').replace(/\s+/g, ' ').trim().slice(0, 160);
|
|
99
|
+
const replyId = m.refMsgId ? String(m.refMsgId) : null;
|
|
133
100
|
if (group) {
|
|
134
101
|
const who = groupSender(m, members);
|
|
135
102
|
// 认不出是谁说的(DOM 里的媒体消息、接口没带发送者):不能把整个群当成一个人,丢掉
|
|
@@ -142,6 +109,7 @@ export function normalizeDouyinMessage(m, { name, timeMs, messageId, group = fal
|
|
|
142
109
|
channelId: `douyin-group:${name}`,
|
|
143
110
|
targetId: name,
|
|
144
111
|
groupId: name,
|
|
112
|
+
convId: convId || String(m?.convId ?? ''),
|
|
145
113
|
// 🔴 身份键必须是 `douyin:<数字uid>`(2026-11-19 真机修)。
|
|
146
114
|
// 网关的群消息入口按 isIdentityKey(userId) 判身份 —— 昵称 / `uid:<id>`
|
|
147
115
|
// 都过不了 ⇒ 群里每条消息都被「群成员身份未确认」拒掉,一条都回不了。
|
|
@@ -158,7 +126,8 @@ export function normalizeDouyinMessage(m, { name, timeMs, messageId, group = fal
|
|
|
158
126
|
subType: '',
|
|
159
127
|
messageId,
|
|
160
128
|
mentioned: false,
|
|
161
|
-
replyId
|
|
129
|
+
replyId,
|
|
130
|
+
...(refText ? { refText } : {}),
|
|
162
131
|
text: text.slice(0, 1000),
|
|
163
132
|
images,
|
|
164
133
|
links,
|
|
@@ -199,7 +168,8 @@ export function normalizeDouyinMessage(m, { name, timeMs, messageId, group = fal
|
|
|
199
168
|
subType: '',
|
|
200
169
|
messageId,
|
|
201
170
|
mentioned: true,
|
|
202
|
-
replyId
|
|
171
|
+
replyId,
|
|
172
|
+
...(refText ? { refText } : {}),
|
|
203
173
|
text: text.slice(0, 1000),
|
|
204
174
|
images,
|
|
205
175
|
links,
|
|
@@ -219,9 +189,9 @@ function pageDriver(host) {
|
|
|
219
189
|
read: (name, options = {}) => readDM({ browser: b(), uname: name, limit: READ_LIMIT, ...options }),
|
|
220
190
|
// expectConvId:发送前用它校验「当前打开的会话确实是目标会话」。
|
|
221
191
|
// 昵称只能用来找会话,不能用来确认身份。
|
|
222
|
-
sendText: (name, text, expectConvId) => sendDM({ browser: b(), text, uname: name, expectConvId }),
|
|
223
|
-
sendImage: (name, file, expectConvId) => sendImage({ browser: b(), file, uname: name, expectConvId }),
|
|
224
|
-
sendEmoji: (name, emoji, expectConvId) => sendEmoji({ browser: b(), name: emoji, uname: name, expectConvId }),
|
|
192
|
+
sendText: (name, text, expectConvId, opts) => sendDM({ browser: b(), text, uname: name, expectConvId, ...opts }),
|
|
193
|
+
sendImage: (name, file, expectConvId, opts) => sendImage({ browser: b(), file, uname: name, expectConvId, ...opts }),
|
|
194
|
+
sendEmoji: (name, emoji, expectConvId, opts) => sendEmoji({ browser: b(), name: emoji, uname: name, expectConvId, ...opts }),
|
|
225
195
|
// 已知的表情名(给提示词用)。被动攒的:只有读完私信发现有不认识的表情才会去抓面板,
|
|
226
196
|
// 所以刚起来时可能是空的 —— 调用方按空处理
|
|
227
197
|
emojiNames: (limit) => knownEmojiNames(limit),
|
|
@@ -406,7 +376,7 @@ export class DouyinChannel {
|
|
|
406
376
|
since = previous + 1;
|
|
407
377
|
this.pushLastEvent = browser.events.at(-1);
|
|
408
378
|
}
|
|
409
|
-
const frames = drainPushFrames(browser, since);
|
|
379
|
+
const frames = drainPushFrames(browser, since, this.selfSecUid ?? '');
|
|
410
380
|
// droppedNoIdentity / droppedNoName 是「丢了多少」的账 —— 以前这两条
|
|
411
381
|
// 是静默 continue,丢了什么都查不到,只能靠用户报「消息不回复」。
|
|
412
382
|
// `parked` 是「先攒着等昵称」的条数,和「真丢了」分开记。
|
|
@@ -419,10 +389,32 @@ export class DouyinChannel {
|
|
|
419
389
|
this.parkedPush = this.parkedPush ?? new Map();
|
|
420
390
|
// uid → 昵称:从轮询路存的游标里反查
|
|
421
391
|
const nameByIdentity = this.pushNames();
|
|
392
|
+
const groupCursors = this.pushGroupCursors();
|
|
422
393
|
if (frames.length) await this.learnPushNames(frames, nameByIdentity);
|
|
423
394
|
for (const { convId, messages } of frames) {
|
|
424
395
|
this.learnSelfUid(convId, messages);
|
|
425
|
-
const
|
|
396
|
+
const selfIds = this.selfUids();
|
|
397
|
+
if (!this.selfSecUid && selfIds.length > 0) {
|
|
398
|
+
const mineMsg = messages.find((m) => selfIds.includes(String(m?.senderId ?? '').trim()) && m?.secUid);
|
|
399
|
+
if (mineMsg) {
|
|
400
|
+
this.selfSecUid = mineMsg.secUid;
|
|
401
|
+
try { setKv(this.db, 'douyin.selfSecUid', mineMsg.secUid); } catch {}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
const tagged = this.selfSecUid
|
|
405
|
+
? messages.map((m) => (m.fromMe == null && m.secUid ? { ...m, fromMe: m.secUid === this.selfSecUid } : m))
|
|
406
|
+
: messages;
|
|
407
|
+
if (GROUP_CONV_RE.test(String(convId ?? '').trim())) {
|
|
408
|
+
const g = groupCursors.get(String(convId).trim());
|
|
409
|
+
if (!g) continue;
|
|
410
|
+
try {
|
|
411
|
+
this.storePushMessages({ name: g.name, convId, identity: '', messages: tagged, group: true, members: g.members });
|
|
412
|
+
} catch (error) {
|
|
413
|
+
this.log('warn', `[fish-soul] 抖音群推送帧入库失败:${errText(error)}`);
|
|
414
|
+
}
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const identity = conversationIdentity(convId, selfIds);
|
|
426
418
|
// 🔴 这两条以前是**静默 continue**:消息在入库之前就没了,
|
|
427
419
|
// 连 unknown / gap 都进不去,日志和界面都看不见 ——
|
|
428
420
|
// 表现就是「uid 和 convId 都解出来了,但就是不回复」。
|
|
@@ -431,7 +423,7 @@ export class DouyinChannel {
|
|
|
431
423
|
identity,
|
|
432
424
|
name: identity === '' ? '' : nameByIdentity.get(identity),
|
|
433
425
|
convId,
|
|
434
|
-
selfUidCount:
|
|
426
|
+
selfUidCount: selfIds.length,
|
|
435
427
|
});
|
|
436
428
|
if (!verdict.ok) {
|
|
437
429
|
// 🔴 缺昵称**不丢**:陌生人第一次发私信时这里必然缺(没有游标、
|
|
@@ -441,7 +433,7 @@ export class DouyinChannel {
|
|
|
441
433
|
// ⇒ 所以这条**不记在 droppedNoName 上**:攒着不是丢,
|
|
442
434
|
// 真丢是「攒到过期还学不到」(见 flushParkedPush)。
|
|
443
435
|
if (verdict.reason === 'noName') {
|
|
444
|
-
this.parkPushFrame(identity, convId,
|
|
436
|
+
this.parkPushFrame(identity, convId, tagged);
|
|
445
437
|
continue;
|
|
446
438
|
}
|
|
447
439
|
this.pushStats[verdict.stat] = (this.pushStats[verdict.stat] ?? 0) + 1;
|
|
@@ -449,7 +441,7 @@ export class DouyinChannel {
|
|
|
449
441
|
continue;
|
|
450
442
|
}
|
|
451
443
|
try {
|
|
452
|
-
this.storePushMessages({ name: verdict.name, convId, identity, messages });
|
|
444
|
+
this.storePushMessages({ name: verdict.name, convId, identity, messages: tagged });
|
|
453
445
|
} catch (error) {
|
|
454
446
|
this.log('warn', `[fish-soul] 推送帧入库失败:${errText(error)}`);
|
|
455
447
|
}
|
|
@@ -466,6 +458,21 @@ export class DouyinChannel {
|
|
|
466
458
|
});
|
|
467
459
|
}
|
|
468
460
|
|
|
461
|
+
/** 已知群会话的 convId → {name, members}(从轮询路游标里反查)。 */
|
|
462
|
+
pushGroupCursors() {
|
|
463
|
+
const map = new Map();
|
|
464
|
+
try {
|
|
465
|
+
const cursors = this.db.prepare("SELECT value FROM kv WHERE key LIKE 'douyin.conv.%' AND key NOT LIKE 'douyin.convId.%'").all();
|
|
466
|
+
for (const r of cursors) {
|
|
467
|
+
const v = JSON.parse(r.value);
|
|
468
|
+
if (v?.group === true && v?.convId && v?.name) {
|
|
469
|
+
map.set(String(v.convId), { name: String(v.name), members: v.members ?? {} });
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
} catch { /* 反查失败就当没有 */ }
|
|
473
|
+
return map;
|
|
474
|
+
}
|
|
475
|
+
|
|
469
476
|
/** uid → 昵称:轮询路游标里带的 name + 推送路自己学到的 `douyin.name.<identity>`。 */
|
|
470
477
|
pushNames() {
|
|
471
478
|
const nameByIdentity = new Map();
|
|
@@ -623,7 +630,7 @@ export class DouyinChannel {
|
|
|
623
630
|
* @param {{name: string, convId: string, identity: string, messages: Array<object>, skipBefore?: number}} p
|
|
624
631
|
* @returns {number} 入库条数
|
|
625
632
|
*/
|
|
626
|
-
storePushMessages({ name, convId, identity, messages, skipBefore = 0 }) {
|
|
633
|
+
storePushMessages({ name, convId, identity, messages, skipBefore = 0, group = false, members = {} }) {
|
|
627
634
|
const selfIds = new Set(this.selfUids());
|
|
628
635
|
const fresh = messages.filter((m) => m.fromMe !== true && !selfIds.has(String(m.senderId ?? '').trim()));
|
|
629
636
|
if (!fresh.length) return 0;
|
|
@@ -640,23 +647,23 @@ export class DouyinChannel {
|
|
|
640
647
|
const { timeMs, messageId } = messageIdFor(m, { occ: occurrences[i], dayKey });
|
|
641
648
|
// 轮询路读的时候这条已经在窗口里了 → 已经收过,别重复收
|
|
642
649
|
if (skipBefore > 0 && timeMs <= skipBefore) continue;
|
|
643
|
-
const norm = normalizeDouyinMessage(m, { name, timeMs, messageId, identity, convId });
|
|
650
|
+
const norm = normalizeDouyinMessage(m, { name, timeMs, messageId, group, members, identity, convId });
|
|
644
651
|
if (norm !== null) { norm.idConfidence = m.seq ? 'platform' : 'heuristic'; normalized.push(norm); }
|
|
645
652
|
}
|
|
646
653
|
if (!normalized.length) return 0;
|
|
647
654
|
const key = `douyin.conv.${sha1(name).slice(0, 16)}`;
|
|
648
|
-
this.inbox.record(key, this.mergeCursor(key, { name, convId, identity }), normalized, null);
|
|
655
|
+
this.inbox.record(key, this.mergeCursor(key, { name, convId, identity, ...(group ? { group: true } : {}) }), normalized, null);
|
|
649
656
|
this.markReadOk(key);
|
|
650
657
|
this.pushStats.stored += normalized.length;
|
|
651
658
|
return normalized.length;
|
|
652
659
|
}
|
|
653
660
|
|
|
654
661
|
/** 推送路写游标时保留轮询路那一份的窗口 / sig(只补身份字段)。 */
|
|
655
|
-
mergeCursor(key, { name, convId, identity }) {
|
|
662
|
+
mergeCursor(key, { name, convId, identity, group }) {
|
|
656
663
|
let prev = null;
|
|
657
664
|
try { prev = getKv(this.db, key, null); } catch { prev = null; }
|
|
658
665
|
const base = prev !== null && typeof prev === 'object' && !Array.isArray(prev) ? prev : {};
|
|
659
|
-
return { ...base, name, convId, identity };
|
|
666
|
+
return group === true ? { ...base, name, convId, identity, group: true } : { ...base, name, convId, identity };
|
|
660
667
|
}
|
|
661
668
|
|
|
662
669
|
/**
|
|
@@ -911,39 +918,13 @@ export class DouyinChannel {
|
|
|
911
918
|
* 学到一次就存进 kv,之后不用再猜。学不到不报错 —— 只是这一轮处理不了。
|
|
912
919
|
*/
|
|
913
920
|
learnSelfUid(convId, msgs) {
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
// fromMe 未知(推送帧里没有 sec_uid 可比对,解码出来是 null)也算:
|
|
922
|
-
// 单聊两侧只有我和对方,senderId 落在其中一侧,另一侧就是我。
|
|
923
|
-
const theirs = new Set();
|
|
924
|
-
for (const m of msgs) {
|
|
925
|
-
const sid = String(m?.senderId ?? '').trim();
|
|
926
|
-
if (sid === '' || m?.fromMe === true) continue;
|
|
927
|
-
if (sid === pair.a || sid === pair.b) theirs.add(sid);
|
|
928
|
-
}
|
|
929
|
-
if (theirs.size === 1) {
|
|
930
|
-
const peer = [...theirs][0];
|
|
931
|
-
setKv(this.db, 'douyin.selfUid', peer === pair.a ? pair.b : pair.a);
|
|
932
|
-
this.log('info', '[fish-soul] 从会话 ID 学到自己的抖音 uid(之后靠会话 ID 认人,不靠昵称)');
|
|
933
|
-
return;
|
|
934
|
-
}
|
|
935
|
-
|
|
936
|
-
// ② 退一步:用「我发的消息」反推(对方从没说过话时只能这样)
|
|
937
|
-
const mine = new Set();
|
|
938
|
-
for (const m of msgs) {
|
|
939
|
-
const sid = String(m?.senderId ?? '').trim();
|
|
940
|
-
if (sid === '' || m?.fromMe !== true) continue;
|
|
941
|
-
if (sid === pair.a || sid === pair.b) mine.add(sid);
|
|
942
|
-
}
|
|
943
|
-
if (mine.size !== 1) return; // 没有 / 有歧义都不学
|
|
944
|
-
const uid = [...mine][0];
|
|
945
|
-
setKv(this.db, 'douyin.selfUid', uid);
|
|
946
|
-
this.log('info', `[fish-soul] 学到自己的抖音 uid(之后靠会话 ID 认人,不靠昵称)`);
|
|
921
|
+
if (selfUidFromKv(getKv(this.db, 'douyin.selfUid', '')) !== '') return;
|
|
922
|
+
const hit = inferSelfUidFromMessages(convId, msgs);
|
|
923
|
+
if (hit === null) return;
|
|
924
|
+
setKv(this.db, 'douyin.selfUid', hit.uid);
|
|
925
|
+
this.log('info', hit.via === 'peer'
|
|
926
|
+
? '[fish-soul] 从会话 ID 学到自己的抖音 uid(之后靠会话 ID 认人,不靠昵称)'
|
|
927
|
+
: '[fish-soul] 学到自己的抖音 uid(之后靠会话 ID 认人,不靠昵称)');
|
|
947
928
|
}
|
|
948
929
|
|
|
949
930
|
/**
|
|
@@ -1356,16 +1337,28 @@ export class DouyinChannel {
|
|
|
1356
1337
|
}
|
|
1357
1338
|
let allOk = true;
|
|
1358
1339
|
let delivered = false;
|
|
1340
|
+
let verifiedTarget = false;
|
|
1359
1341
|
const details = [];
|
|
1360
1342
|
/** 发一段(表情面板里没这个名字就退回文字)。整条流程都在锁里,直接调驱动。 */
|
|
1361
1343
|
const sendPart = async (part) => {
|
|
1362
|
-
|
|
1344
|
+
const opts = verifiedTarget ? { alreadyVerified: true } : undefined;
|
|
1345
|
+
if (part.type === 'image') {
|
|
1346
|
+
return opts
|
|
1347
|
+
? await this.driver.sendImage(name, part.path, convId, opts)
|
|
1348
|
+
: await this.driver.sendImage(name, part.path, convId);
|
|
1349
|
+
}
|
|
1363
1350
|
if (part.type === 'emoji') {
|
|
1364
|
-
const e =
|
|
1365
|
-
|
|
1366
|
-
|
|
1351
|
+
const e = opts
|
|
1352
|
+
? await this.driver.sendEmoji(name, part.name, convId, opts)
|
|
1353
|
+
: await this.driver.sendEmoji(name, part.name, convId);
|
|
1354
|
+
// 表情面板里没有这个名字(且没点出去):当文字发;点出去了但没回读到不要重复发文字
|
|
1355
|
+
if (e?.ok) return e;
|
|
1356
|
+
if (e?.submitted === true) return e;
|
|
1357
|
+
return await this.driver.sendText(name, `[${part.name}]`, convId);
|
|
1367
1358
|
}
|
|
1368
|
-
return
|
|
1359
|
+
return opts
|
|
1360
|
+
? await this.driver.sendText(name, part.text, convId, opts)
|
|
1361
|
+
: await this.driver.sendText(name, part.text, convId);
|
|
1369
1362
|
};
|
|
1370
1363
|
/**
|
|
1371
1364
|
* 🔴 整条发送流程(所有段 + 打字停顿 + 进会话)必须**独占浏览器锁**。
|
|
@@ -1386,6 +1379,7 @@ export class DouyinChannel {
|
|
|
1386
1379
|
for (const part of parts) {
|
|
1387
1380
|
await sleep(this.typingDelay(part.type === 'text' ? part.text : ''));
|
|
1388
1381
|
const r = await sendPart(part).catch((error) => ({ ok: false, error: errText(error) }));
|
|
1382
|
+
if (r?.ok === true || r?.verified === true || r?.submitted === true) verifiedTarget = true;
|
|
1389
1383
|
// 回读确认:文字(sendDM)/ 图片(sendImage)/ 表情(sendEmoji)三条都会
|
|
1390
1384
|
// 在发完之后数一遍气泡,真的多出来才回 verified。所以这里统一认 verified。
|
|
1391
1385
|
// ⚠️ 表情走文字兜底(面板里没这个名字)时就没有 verified 了 ——
|
|
@@ -617,8 +617,7 @@ export class Browser {
|
|
|
617
617
|
// 🔴 同 eval():不在这里启动浏览器(见上面的说明)
|
|
618
618
|
if (!this.ready) throw new Error('浏览器没连上(连接已断),需要重新获取浏览器')
|
|
619
619
|
this.events = []
|
|
620
|
-
/**
|
|
621
|
-
this.seq = 0
|
|
620
|
+
/** 事件序号(只增不减,goto 不清零)。`events` 会被裁剪 / 清空,调用方必须用它当游标,不能用长度。 */
|
|
622
621
|
this.messageResponses.clear()
|
|
623
622
|
|
|
624
623
|
// 🔴 整个导航的上限(给了才生效)。页面 renderer 卡死但 CDP 连接还在时,
|
|
@@ -62,12 +62,12 @@ const KIND_TEXT = 7
|
|
|
62
62
|
export const MESSAGE_URL_RE = /get_by_conversation|get_message_by_init|get_user_message|message\/list/i
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
* 会话 ID 文本:单聊 `0:1:<uid>:<uid
|
|
65
|
+
* 会话 ID 文本:单聊 `0:1:<uid>:<uid>`,老群聊 `0:2:<uid>:<uid>`,新群聊 `<digits>:<digits>:<digits>...`。
|
|
66
66
|
*
|
|
67
67
|
* ⚠️ 这里只负责「别把它当成 protobuf 子树」,单聊/群聊的判定交给
|
|
68
68
|
* `douyin-conversation.js`(`isSingleConversation` 只认 0:1)。
|
|
69
69
|
*/
|
|
70
|
-
const CONV_ID_TEXT_RE = /^0:[12]:\d+:\d
|
|
70
|
+
const CONV_ID_TEXT_RE = /^(?:0:[12]:\d+:\d+|[1-9]\d+(?::\d+){2,})$/
|
|
71
71
|
|
|
72
72
|
/** sec_uid 的形状(`MS4wLjAB…`) */
|
|
73
73
|
const SEC_UID_TEXT_RE = /^MS4wLjAB[\w-]{8,}$/
|
|
@@ -361,22 +361,27 @@ export async function selfInfo(browser) {
|
|
|
361
361
|
return { secUid: '', nickname: '' }
|
|
362
362
|
}
|
|
363
363
|
|
|
364
|
-
/** 这个 buffer 是不是一条明文会话 ID
|
|
364
|
+
/** 这个 buffer 是不是一条明文会话 ID(单聊或群聊) */
|
|
365
365
|
function isConvIdBuffer(buf) {
|
|
366
366
|
if (!Buffer.isBuffer(buf) || buf.length < 7 || buf.length > 80) return false
|
|
367
|
-
if (buf[0]
|
|
367
|
+
if (buf[0] < 0x30 || buf[0] > 0x39 || !buf.includes(0x3a /* ':' */)) return false
|
|
368
368
|
return CONV_ID_TEXT_RE.test(buf.toString('latin1'))
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
+
/** 这个 buffer 是不是一条明文 sec_uid(`MS4wLjAB…` 会因首字节 `0x4d` 被 `parseTree` 误当成 wire=5 消息) */
|
|
372
|
+
function isSecUidBuffer(buf) {
|
|
373
|
+
if (!Buffer.isBuffer(buf) || buf.length < 16 || buf.length > 160) return false
|
|
374
|
+
return buf[0] === 0x4d /* 'M' */ && SEC_UID_TEXT_RE.test(buf.toString('latin1'))
|
|
375
|
+
}
|
|
376
|
+
|
|
371
377
|
/**
|
|
372
378
|
* `parseFields()` 解出的一层字段 → `parseTree()` 的节点形状。
|
|
373
379
|
*
|
|
374
|
-
* 🔴 为什么非要多这一步:会话 ID 字符串(`0:1:uid:uid
|
|
375
|
-
* protobuf 字段序列** —— `
|
|
376
|
-
*
|
|
377
|
-
* `getString(c, 1)` 永远拿到 '',会话 ID 就这么丢了(旧结构同样中招)。
|
|
380
|
+
* 🔴 为什么非要多这一步:会话 ID 字符串(`0:1:uid:uid`)和 `sec_uid`(`MS4wLjAB…`)
|
|
381
|
+
* 的字节**碰巧也是合法的容错 protobuf 字段序列** —— 于是 `parseTree()` 会把它们
|
|
382
|
+
* 当成嵌套消息树,`getString(c, 1)` / `getString(c, 14)` 永远拿到 ''。
|
|
378
383
|
*
|
|
379
|
-
* 这里对「看起来就是会话 ID」的字段直接按字符串处理;消息正文 JSON 同理
|
|
384
|
+
* 这里对「看起来就是会话 ID / sec_uid」的字段直接按字符串处理;消息正文 JSON 同理
|
|
380
385
|
* (`{` = 0x7b 会被当成 group 解成残的)。
|
|
381
386
|
*/
|
|
382
387
|
function fieldsToNodes(fields) {
|
|
@@ -395,7 +400,7 @@ function fieldsToNodes(fields) {
|
|
|
395
400
|
out.push({ field: f.field, kind: 'bytes', value: Buffer.alloc(0) })
|
|
396
401
|
continue
|
|
397
402
|
}
|
|
398
|
-
if (isConvIdBuffer(sub)) {
|
|
403
|
+
if (isConvIdBuffer(sub) || isSecUidBuffer(sub)) {
|
|
399
404
|
out.push({ field: f.field, kind: 'string', value: sub.toString('utf8') })
|
|
400
405
|
continue
|
|
401
406
|
}
|
|
@@ -544,6 +549,12 @@ function decodeMessageNode(c, selfSecUid = '', secByUid = null) {
|
|
|
544
549
|
let text = ''
|
|
545
550
|
let tips = ''
|
|
546
551
|
let shareAweme = null
|
|
552
|
+
let shareTitle = ''
|
|
553
|
+
let shareAuthor = ''
|
|
554
|
+
let refText = ''
|
|
555
|
+
let refMsgId = ''
|
|
556
|
+
let mediaType = ''
|
|
557
|
+
let mediaHint = ''
|
|
547
558
|
let rawJson = null
|
|
548
559
|
const j = getString(c, 8)
|
|
549
560
|
if (j) {
|
|
@@ -559,18 +570,39 @@ function decodeMessageNode(c, selfSecUid = '', secByUid = null) {
|
|
|
559
570
|
// 认不出就 shareAweme 为空,filterUseful 会把整条分享消息丢掉。
|
|
560
571
|
const shareId = o.aweme_id ?? o.awemeId ?? o.itemId
|
|
561
572
|
if (shareId) shareAweme = String(shareId)
|
|
573
|
+
shareTitle = String(o.desc ?? o.title ?? o.content_title ?? o.aweme_title ?? '').trim()
|
|
574
|
+
shareAuthor = String(o.author ?? o.nickname ?? o.author_name ?? o.authorName ?? '').trim()
|
|
575
|
+
const refObj = o.reference_info ?? o.ref_info ?? o.quote_info ?? null
|
|
576
|
+
if (refObj && typeof refObj === 'object') {
|
|
577
|
+
refText = String(refObj.hint ?? refObj.text ?? refObj.content ?? '').trim()
|
|
578
|
+
refMsgId = String(refObj.ref_message_id ?? refObj.message_id ?? refObj.server_message_id ?? '').trim()
|
|
579
|
+
}
|
|
580
|
+
if (o.poi_name || o.address) {
|
|
581
|
+
mediaType = 'location'
|
|
582
|
+
mediaHint = String(o.poi_name || o.address).trim()
|
|
583
|
+
} else if (o.display_name || (o.sticker_type != null && (o.name || o.text))) {
|
|
584
|
+
mediaType = 'emoji'
|
|
585
|
+
mediaHint = String(o.display_name || o.name || o.text || '').trim()
|
|
586
|
+
} else if ((o.duration || o.duration_ms) && (o.voice_uri || o.uri || kind === 17)) {
|
|
587
|
+
mediaType = 'voice'
|
|
588
|
+
const sec = Math.round(Number(o.duration_ms ? o.duration_ms / 1000 : o.duration))
|
|
589
|
+
mediaHint = sec > 0 ? String(sec) : ''
|
|
590
|
+
} else if (o.width && o.height && (o.uri || o.origin_url || o.preview_url) && !shareId) {
|
|
591
|
+
mediaType = 'image'
|
|
592
|
+
}
|
|
562
593
|
} catch {
|
|
563
594
|
text = j // 不是 JSON 就当纯文本
|
|
564
595
|
rawJson = { __text: j }
|
|
565
596
|
}
|
|
566
597
|
}
|
|
567
598
|
// 有些消息正文在 #9 的属性里(key 叫 content / text)
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
599
|
+
for (const a of c.filter((x) => x.field === 9 && x.kind === 'message')) {
|
|
600
|
+
const k = getString(a.children, 1)
|
|
601
|
+
const v = getString(a.children, 2)
|
|
602
|
+
if (!v) continue
|
|
603
|
+
if (!text && /^(content|text|s:content|s:text)$/i.test(k)) text = v
|
|
604
|
+
else if (!refMsgId && /^(?:a:ref_msg_id|ref_message_id)$/i.test(k)) refMsgId = v
|
|
605
|
+
else if (!refText && /^(?:s:ref_text|ref_text)$/i.test(k)) refText = v
|
|
574
606
|
}
|
|
575
607
|
|
|
576
608
|
return {
|
|
@@ -583,6 +615,12 @@ function decodeMessageNode(c, selfSecUid = '', secByUid = null) {
|
|
|
583
615
|
text,
|
|
584
616
|
tips,
|
|
585
617
|
shareAweme,
|
|
618
|
+
shareTitle,
|
|
619
|
+
shareAuthor,
|
|
620
|
+
refText,
|
|
621
|
+
refMsgId,
|
|
622
|
+
mediaType,
|
|
623
|
+
mediaHint,
|
|
586
624
|
/** 消息载荷的完整 JSON(按类型不同字段不同:text / uri / duration…) */
|
|
587
625
|
raw: rawJson,
|
|
588
626
|
/** 是不是文本消息(能直接回) */
|