dsh-fish-soul 0.18.12 → 0.18.14
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/browse.js +4 -0
- package/lib/channels/douyin.js +73 -15
- package/lib/client.js +2 -2
- package/lib/diag-log.js +9 -2
- package/lib/douyin-adapter.js +189 -7
- package/lib/douyin-conversation.js +35 -0
- package/lib/douyin-inbox.js +32 -7
- package/lib/douyin-share-inline.js +24 -0
- package/lib/douyin-short-link.js +48 -0
- package/lib/gateway-media.js +2 -2
- package/lib/gateway.js +13 -2
- package/lib/index.js +18 -10
- package/lib/routes.js +1 -1
- package/lib/soul-memory.js +21 -0
- package/lib/time-awareness.js +67 -0
- package/package.json +3 -3
package/README.md
CHANGED
package/lib/browse.js
CHANGED
|
@@ -424,6 +424,8 @@ export class BiliBrowser {
|
|
|
424
424
|
new Promise((_, reject) => { timer = setTimeout(() => reject(new Error('评论发帖超时')), 15_000); timer.unref?.(); })
|
|
425
425
|
]);
|
|
426
426
|
setKv(this.db, this.commentDayKey(), this.commentedToday() + 1);
|
|
427
|
+
this.db.prepare('INSERT INTO social_post (platform, post_id, content, created_at, created_real_at) VALUES (?, ?, ?, ?, ?)')
|
|
428
|
+
.run('bili', `comment-${v.aid}-${Date.now()}`, `在《${oneLine(v.title, 40)}》评论区留了一句:${text}`, this.clock.now(), Date.now());
|
|
427
429
|
this.log('info', `[fish-soul] 在《${oneLine(v.title, 30)}》评论区留了一句:${text}`);
|
|
428
430
|
return text;
|
|
429
431
|
} catch (error) {
|
|
@@ -468,6 +470,8 @@ export class BiliBrowser {
|
|
|
468
470
|
await this.client.replyComment({ type: 1, oid: v.aid, root: pick.rpid, parent: pick.rpid, message: text });
|
|
469
471
|
setKv(this.db, this.replyDayKey(), this.repliedToday() + 1);
|
|
470
472
|
setKv(this.db, 'bili.reply.last', { bvid: v.bvid, title: oneLine(v.title, 40), name: pick.name, text, root: pick.rpid, at: Date.now() });
|
|
473
|
+
this.db.prepare('INSERT INTO social_post (platform, post_id, content, created_at, created_real_at) VALUES (?, ?, ?, ?, ?)')
|
|
474
|
+
.run('bili', `reply-${v.aid}-${pick.rpid}`, `在《${oneLine(v.title, 40)}》回了 @${pick.name}:${text}`, this.clock.now(), Date.now());
|
|
471
475
|
this.log('info', `[fish-soul] 回了 @${pick.name} 的评论:${text}`);
|
|
472
476
|
return text;
|
|
473
477
|
} catch (error) {
|
package/lib/channels/douyin.js
CHANGED
|
@@ -2,7 +2,7 @@ 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
|
|
5
|
+
senderBelongsToConversation, peerUidFromConversation, isDouyinConversationId
|
|
6
6
|
} from '../douyin-conversation.js';
|
|
7
7
|
/**
|
|
8
8
|
* douyin.js —— 抖音小号:专用浏览器(Edge / Chrome …,CDP 控制)+ 网页版私信渠道。
|
|
@@ -382,13 +382,24 @@ export function normalizeDouyinMessage(m, { name, timeMs, messageId, group = fal
|
|
|
382
382
|
const who = groupSender(m, members);
|
|
383
383
|
// 认不出是谁说的(DOM 里的媒体消息、接口没带发送者):不能把整个群当成一个人,丢掉
|
|
384
384
|
if (who.userId === '') return null;
|
|
385
|
+
const senderUid = String(m?.senderId || m?.secUid || '').trim();
|
|
386
|
+
const groupIdentity = /^\d+$/.test(senderUid) ? `douyin:${senderUid}` : '';
|
|
385
387
|
return {
|
|
386
388
|
channel: 'douyin',
|
|
387
389
|
chatType: 'group',
|
|
388
390
|
channelId: `douyin-group:${name}`,
|
|
389
391
|
targetId: name,
|
|
390
392
|
groupId: name,
|
|
391
|
-
|
|
393
|
+
// 🔴 身份键必须是 `douyin:<数字uid>`(2026-11-19 真机修)。
|
|
394
|
+
// 网关的群消息入口按 isIdentityKey(userId) 判身份 —— 昵称 / `uid:<id>`
|
|
395
|
+
// 都过不了 ⇒ 群里每条消息都被「群成员身份未确认」拒掉,一条都回不了。
|
|
396
|
+
// 跟私信对齐(2026-11-10 那次「私信不用昵称」的同一条道理):
|
|
397
|
+
// 昵称可改名、可同名冒充,认出「是谁」不该靠它;昵称留在 nickname 里。
|
|
398
|
+
// `groupSender` 的 userId 是**昵称**(它是有契约的纯函数,不动它),
|
|
399
|
+
// 所以身份键在这儿用**消息自己的 senderId** 拼。
|
|
400
|
+
// 认不出数字 uid 的退回昵称 —— 网关会按「身份未确认」拒掉,
|
|
401
|
+
// 但**不猜**:昵称不能当身份(可同名冒充)。
|
|
402
|
+
userId: groupIdentity !== '' ? groupIdentity : who.userId,
|
|
392
403
|
nickname: who.nickname,
|
|
393
404
|
// 群聊的「会话名」是整个群,不是发言的人 —— 跟私信的语义对齐,出口不用改
|
|
394
405
|
peerName: name,
|
|
@@ -1711,8 +1722,16 @@ export class DouyinChannel {
|
|
|
1711
1722
|
async readConversation({ c, name, key, st, sig, unread }) {
|
|
1712
1723
|
this.inbox.beginRead(key, `${st?.group === true ? 'douyin-group' : 'douyin'}:${name}`);
|
|
1713
1724
|
let res;
|
|
1725
|
+
// 🔴 已固定的会话要钉住,不能被别的会话顶掉(串会话的防线)。
|
|
1726
|
+
// 单聊:用「现在还能不能推出同一个身份」来验证游标还有效 ——
|
|
1727
|
+
// 换号后推不出身份 ⇒ 旧身份作废 ⇒ 不沿用旧会话 ID
|
|
1728
|
+
// (test/douyin-read-cache.mjs 守着这条契约:换号后不沿用)。
|
|
1729
|
+
// 群聊:没有「会话对端」可言,但游标里记着它是哪个群
|
|
1730
|
+
// (identity 字段 = `group:<convId>`)⇒ 记着同一个群就沿用。
|
|
1714
1731
|
const priorIdentity = conversationIdentity(st?.convId, this.selfUids());
|
|
1715
|
-
const
|
|
1732
|
+
const priorIsSingle = priorIdentity !== '' && priorIdentity === st?.identity;
|
|
1733
|
+
const priorIsGroup = st?.group === true && st?.identity === `group:${String(st?.convId ?? '').trim()}`;
|
|
1734
|
+
const expectConvId = (priorIsSingle || priorIsGroup) ? String(st?.convId ?? '').trim() : '';
|
|
1716
1735
|
// 🔴 driver.read 内部是一串 browser.eval / captureImBodies / enterChat,**都没有超时**。
|
|
1717
1736
|
// CDP 会话 hang 住 / 页面卡住时它会永远等下去 —— 而 exclusive() 的 maxMs 只让
|
|
1718
1737
|
// 调用方拿到超时错误,fn() 还在锁里跑 ⇒ 锁永不释放 ⇒ host.busy 一直显示「读私信」、
|
|
@@ -1753,10 +1772,17 @@ export class DouyinChannel {
|
|
|
1753
1772
|
* 每轮重试 → gap 死循环。有真新消息(unread)会绕过熔断重读,不会「卡死收不到」。
|
|
1754
1773
|
*/
|
|
1755
1774
|
const convId = String(res.convId ?? '').trim();
|
|
1756
|
-
|
|
1775
|
+
// 🔴 **群会话也要放行**(2026-11-19 真机修)。
|
|
1776
|
+
// 这道闸门原来是「必须是单聊形状」—— 群会话 ID 是 `<数字>:<数字>:...`,
|
|
1777
|
+
// 于是群里的话一句都过不来(读失败 → 不入库 → 不推进游标)。
|
|
1778
|
+
// 群会话的身份靠**会话 ID 本身**(群号),不靠「谁是对端」。
|
|
1779
|
+
// ⚠️ 但「已固定的会话不能被换掉」这条对群同样成立 ——
|
|
1780
|
+
// 上面 expectConvId 现在从游标取(不再靠 identity),所以这里统一判。
|
|
1781
|
+
const groupConv = !isSingleConversation(convId);
|
|
1782
|
+
if (convId === '' || (expectConvId && convId !== expectConvId)) {
|
|
1757
1783
|
this.inbox.failRead(key, '读取失败:会话身份缺失或与已验证目标不符');
|
|
1758
1784
|
this.markReadFailed(key);
|
|
1759
|
-
this.log('warn', `[fish-soul] 读「${name}
|
|
1785
|
+
this.log('warn', `[fish-soul] 读「${name}」拿不到会话 ID(${convId || '空'}),身份无法确认,5 分钟内不再重试`);
|
|
1760
1786
|
return;
|
|
1761
1787
|
}
|
|
1762
1788
|
// 🔴 身份过滤要区分两种消息:
|
|
@@ -1788,6 +1814,10 @@ export class DouyinChannel {
|
|
|
1788
1814
|
// senderBelongsToConversation 会因「没有 sender」拒掉它们 —— 但它们的身份
|
|
1789
1815
|
// 靠「在当前会话里看到的 + convId 已确认」成立,直接放行
|
|
1790
1816
|
if (String(m?.senderId ?? '').trim() === '' && m?.type && m.type !== 'text') return true;
|
|
1817
|
+
// 🔴 群会话(convType=2 或会话 ID 不是 0:1: 形状)里的发言人是**群成员**,
|
|
1818
|
+
// 不在「会话两侧」里 —— 单聊那套判据会把群消息整批丢掉。
|
|
1819
|
+
// 群的身份靠「这条消息的 convId 就是当前会话」(上面已经校验过)。
|
|
1820
|
+
if (Number(m?.convType) === 2 || !isSingleConversation(convId)) return true;
|
|
1791
1821
|
return senderBelongsToConversation(m, selfIds);
|
|
1792
1822
|
});
|
|
1793
1823
|
const prints = msgs.map(messageFingerprint);
|
|
@@ -1834,18 +1864,31 @@ export class DouyinChannel {
|
|
|
1834
1864
|
// 从这批消息里学「我自己的数字 uid」,然后算出这个会话的稳定身份。
|
|
1835
1865
|
this.learnSelfUid(convId, msgs);
|
|
1836
1866
|
const identity = conversationIdentity(convId, this.selfUids());
|
|
1837
|
-
|
|
1867
|
+
// 🔴 群聊**不套**单聊的身份闸门(2026-11-19 真机修)。
|
|
1868
|
+
// 群会话 ID 不是 0:1:<我>:<对方> 的形状(是 <数字>:<数字>:...),
|
|
1869
|
+
// `conversationIdentity` 必然返回空 —— 以前这里直接 return,
|
|
1870
|
+
// 于是群里的话一条都进不了库、也永远不推进游标,
|
|
1871
|
+
// 每轮轮询都判「窗口对不齐」⇒ 日志每分钟刷一条「私信可能有遗漏」。
|
|
1872
|
+
// 群里认人靠的是**发言人**(`groupSender` + members 昵称表),不靠会话对端,
|
|
1873
|
+
// 所以群聊这条路上 identity 本来就用不上。
|
|
1874
|
+
if (identity === '' && !group) {
|
|
1838
1875
|
// 有会话 ID 但推不出对端(不知道自己是谁):这一轮不处理,也不推进游标。
|
|
1839
1876
|
this.inbox.failRead(key);
|
|
1840
1877
|
this.markReadFailed(key);
|
|
1841
1878
|
this.log('warn', `[fish-soul] 会话 ${convId} 推不出对方身份(还不知道自己的 uid),5 分钟内不再重试`);
|
|
1842
1879
|
return;
|
|
1843
1880
|
}
|
|
1881
|
+
// 群聊没有「会话对端」可言,identity 用会话 ID 自己当标识(只作游标标记,不作身份)。
|
|
1882
|
+
const sessionKey = identity !== '' ? identity : `group:${convId}`;
|
|
1844
1883
|
// 记下「这个身份对应哪个会话 ID」:转达 / 主动私信要靠它找到目标会话,
|
|
1845
1884
|
// 不能按昵称猜(昵称会变、会重名)。同名身份只留第一次,避免被后来者覆盖。
|
|
1846
1885
|
try {
|
|
1847
|
-
|
|
1848
|
-
|
|
1886
|
+
// 🔴 只有单聊才记「身份 → 会话 ID」(转达 / 主动私信要靠它找目标会话)。
|
|
1887
|
+
// 群聊记了也没用:群里没有唯一的「对方」。
|
|
1888
|
+
if (identity !== '') {
|
|
1889
|
+
const k = `douyin.convId.${identity}`;
|
|
1890
|
+
if (String(getKv(this.db, k, '') ?? '') === '') setKv(this.db, k, convId);
|
|
1891
|
+
}
|
|
1849
1892
|
} catch { /* 记不下来不影响这一轮 */ }
|
|
1850
1893
|
// 判据保持「这次读到的消息一条都不在上次窗口里」= 窗口对不齐。
|
|
1851
1894
|
// ⚠️ 这条在「正常来新消息」时也会命中(抖音只返回最近几条,旧的滚出去了),
|
|
@@ -1855,7 +1898,7 @@ export class DouyinChannel {
|
|
|
1855
1898
|
// 取舍:宁可误报(只多记一条缺口,7 天后自动清理),不能漏报(真丢消息)。
|
|
1856
1899
|
const hasWindowGap = st !== null && Array.isArray(st.window) && st.window.length > 0 && prints.length > 0 && !prints.some(p => st.window.includes(p));
|
|
1857
1900
|
// 游标带上会话身份:以后哪怕昵称变了,也知道这仍是同一个会话。
|
|
1858
|
-
const cursor = { name, sig, convId, identity, window: prints.slice(-WINDOW_KEEP), readAt: Date.now(), ...(group ? { group: true, members } : {}) };
|
|
1901
|
+
const cursor = { name, sig, convId, identity: sessionKey, window: prints.slice(-WINDOW_KEEP), readAt: Date.now(), ...(group ? { group: true, members } : {}) };
|
|
1859
1902
|
// 缓存只交接接口消息;下一轮仍需读取页面媒体,不能把预览当作完整收取。
|
|
1860
1903
|
if (res.needsPageRead) cursor.needsPageRead = true;
|
|
1861
1904
|
const normalized = [];
|
|
@@ -2003,7 +2046,12 @@ export class DouyinChannel {
|
|
|
2003
2046
|
* 做包含比对,同名或名字互相包含时会把消息发给错的人(而且是静默的)。
|
|
2004
2047
|
* 拿不到 convId 说明这条会话的身份没确认过,宁可失败也不猜。
|
|
2005
2048
|
*/
|
|
2006
|
-
|
|
2049
|
+
// 🔴 群会话的 convId 不是 `0:1:` 形状(`<数字>:<数字>:...`),
|
|
2050
|
+
// 以前这里把群全拒了 ⇒ 群里回不了话(2026-11-19 真机修)。
|
|
2051
|
+
// 判据改成「必须非空」:有没有会话 ID 是身份确认的关键,
|
|
2052
|
+
// 形状不是。驱动的 sendDM 拿 expectConvId 做**字符串相等**比对,
|
|
2053
|
+
// 群会话 ID 一样能比。
|
|
2054
|
+
if (!isDouyinConversationId(convId)) {
|
|
2007
2055
|
throw new Error('这个抖音会话没有确认过身份(缺少会话 ID),不发,以免发错人');
|
|
2008
2056
|
}
|
|
2009
2057
|
let allOk = true;
|
|
@@ -2072,8 +2120,15 @@ export class DouyinChannel {
|
|
|
2072
2120
|
*
|
|
2073
2121
|
* 不是发链接、也不是下载文件再上传 —— 是抖音私信里那条点开就播的原生卡片。
|
|
2074
2122
|
*
|
|
2075
|
-
* 🔴
|
|
2076
|
-
*
|
|
2123
|
+
* 🔴 收件人只认**会话身份确认过的对端**(`0:1:<我的uid>:<对端uid>`,对端从会话 ID 推出来)。
|
|
2124
|
+
* 以前这里还额外要求「对端必须是设置页填的主人」—— 现在不要求了:
|
|
2125
|
+
* 主人要求「聊天里发抖音链接对所有会话都发原生卡片」。
|
|
2126
|
+
*
|
|
2127
|
+
* 为什么放开仍然安全:
|
|
2128
|
+
* ① 收件人 uid **只**来自会话 ID,而会话 ID 来自抖音自己的帧,不是用户输入;
|
|
2129
|
+
* ② 卡片按 **uid** 分享(分享面板里 `data-userid` 唯一可见才点),不按昵称;
|
|
2130
|
+
* ③ 面板里没有这个人 ⇒ 分享失败 ⇒ 调用方退回文字,不会发错人。
|
|
2131
|
+
* ⚠️ 调用方传了 peerUid 就必须和会话推出的对端一致 —— 不一致说明它认错了人,宁可不发。
|
|
2077
2132
|
*
|
|
2078
2133
|
* @param {{videoId: string, peerUid: string, convId: string, friendName: string}} p 收件人身份与作品
|
|
2079
2134
|
* @param {() => void} [onSubmitted] 点击**之前**落盘用(提交了就可能已经发出去了)
|
|
@@ -2087,9 +2142,12 @@ export class DouyinChannel {
|
|
|
2087
2142
|
// 会话 ID 的对端 = 收件人;自己不在会话里、或会话 ID 不认识,都不发。
|
|
2088
2143
|
const peer = peerUidFromConversation(convId, this.selfUids());
|
|
2089
2144
|
if (peer === '') return { ok: false, verified: false, submitted: false, error: '缺少明确收件人会话身份,未分享' };
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2145
|
+
// 传了 peerUid 就必须和会话 ID 推出的对端一致 —— 不一致说明调用方认错了人,宁可不发。
|
|
2146
|
+
// 不传(普通私信里发链接)就以会话 ID 为准:能推出对端就说明身份是确认过的。
|
|
2147
|
+
// ⚠️ 以前这里还要求 peer === masterUid,于是**只给主人发得出卡片**;
|
|
2148
|
+
// 现在所有会话都发卡片,主人这道限制去掉,但「对端必须能从 convId 推出来」留着。
|
|
2149
|
+
if (peerUid !== undefined && peerUid !== null && String(peerUid).trim() !== '' && peer !== String(peerUid).trim()) {
|
|
2150
|
+
return { ok: false, verified: false, submitted: false, error: '收件人和会话身份对不上,未分享' };
|
|
2093
2151
|
}
|
|
2094
2152
|
const browser = this.host.browser;
|
|
2095
2153
|
if (browser === null || browser === undefined) return { ok: false, verified: false, submitted: false, error: '抖音浏览器没在运行' };
|
package/lib/client.js
CHANGED
|
@@ -738,14 +738,14 @@ window.__ModuleLoader__.load({
|
|
|
738
738
|
const post = b.post ?? {};
|
|
739
739
|
const cm = b.comments ?? {};
|
|
740
740
|
const socialLog = [
|
|
741
|
-
...(so.post?.recent ?? []).map((x) => ({ key: `p${x.id}`, tag: Tag("动态", "outline"), text: x.content, at: x.at })),
|
|
741
|
+
...(so.post?.recent ?? []).map((x) => ({ key: `p${x.id}`, tag: Tag(/^(comment|reply)-/.test(x.id) ? "评论" : "动态", "outline"), text: x.content, at: x.at })),
|
|
742
742
|
...(so.comments?.recent ?? []).map((x, i) => ({ key: `c${i}${x.at}`, tag: Tag(x.status === "replied" ? "回了" : x.status === "failed" ? "没发出" : "没回", x.status === "failed" ? "danger" : undefined),
|
|
743
743
|
text: `${x.name}:${x.content}${x.reply ? ` → ${x.reply}` : ""}`, at: x.at }))
|
|
744
744
|
].sort((x, y) => y.at - x.at).slice(0, 10);
|
|
745
745
|
// #132:加载更多的旧记录合并进面板给的最近几条后面
|
|
746
746
|
const socialLogAll = [...socialLog, ...olderLog.items.map((x) => ({
|
|
747
747
|
key: x.key,
|
|
748
|
-
tag: x.kind === "post" ? Tag("动态", "outline")
|
|
748
|
+
tag: x.kind === "post" ? Tag(/^(comment|reply)-/.test(x.pid ?? "") ? "评论" : "动态", "outline")
|
|
749
749
|
: Tag(x.status === "replied" ? "回了" : x.status === "failed" ? "没发出" : "没回", x.status === "failed" ? "danger" : undefined),
|
|
750
750
|
text: x.text, at: x.at
|
|
751
751
|
}))].sort((x, y) => y.at - x.at);
|
package/lib/diag-log.js
CHANGED
|
@@ -20,8 +20,15 @@ import { getKv, setKv } from './db.js';
|
|
|
20
20
|
|
|
21
21
|
/** 内存里最多留多少条。 */
|
|
22
22
|
const LIMIT = 200;
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* 同一个 key 在这个窗口内重复出现就合并计数(毫秒)。
|
|
25
|
+
*
|
|
26
|
+
* ⚠️ 这个窗口必须**大于轮询间隔的上界**,否则去重结构上不可能生效:
|
|
27
|
+
* 抖音轮询间隔是 [30s, 60s] 随机,窗口正好 60s 时,间隔落在上界的那几轮
|
|
28
|
+
* 每次都超出窗口 ⇒ 每轮新增一条 ⇒ 同一个问题刷出几十条日志(真机 2026-11-19)。
|
|
29
|
+
* 取 5 分钟:足够把「一个持续的问题」并成一条带计数的记录。
|
|
30
|
+
*/
|
|
31
|
+
const DEDUPE_MS = 5 * 60_000;
|
|
25
32
|
|
|
26
33
|
/**
|
|
27
34
|
* 每条日志的中文说法。key 是稳定的机器标识,人话在这里查。
|
package/lib/douyin-adapter.js
CHANGED
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
|
|
15
15
|
import { createHash } from 'node:crypto';
|
|
16
16
|
import { getKv, setKv, resolveOrCreatePlatformPerson, cleanupMergedDouyinNames } from './db.js';
|
|
17
|
-
import { isIdentityKey } from './douyin-conversation.js';
|
|
17
|
+
import { isIdentityKey, isDouyinConversationId } from './douyin-conversation.js';
|
|
18
18
|
import { findDouyinRefs } from './douyin-browse.js';
|
|
19
19
|
import { douyinVideoIds, deliverDouyinCards } from './douyin-delivery.js';
|
|
20
|
+
import { ALONE_LINK } from './douyin-share-inline.js';
|
|
21
|
+
import { resolveDouyinShortLink } from './douyin-short-link.js';
|
|
20
22
|
import { splitChannelId, clampReplyWindow } from './gateway-util.js';
|
|
21
23
|
import { sanitizeName } from './channels/onebot.js';
|
|
22
24
|
|
|
@@ -400,23 +402,84 @@ export class DouyinAdapter {
|
|
|
400
402
|
return '';
|
|
401
403
|
}
|
|
402
404
|
|
|
405
|
+
/**
|
|
406
|
+
* 内联卡片(聊天里发链接变的那张)的交付标识。
|
|
407
|
+
*
|
|
408
|
+
* 🔴 两个要求同时成立:
|
|
409
|
+
* ① **同一轮重试**必须得到同一个标识 —— 否则账本认不出「上次发过没有」
|
|
410
|
+
* 就会重复分享(对方收到两张一样的卡)。
|
|
411
|
+
* ② **不同轮**必须是不同标识 —— 否则她过几天再说一次同一个视频,
|
|
412
|
+
* 会被账本永久挡住,永远发不出第二张。
|
|
413
|
+
*
|
|
414
|
+
* 所以用 `scope`(网关传进来的 sessionId,每轮一个 UUID、重试不变)+ 作品 ID。
|
|
415
|
+
* 拿不到 scope 时退回 `convId:videoId`(老行为,宁可能重发也不能不发)。
|
|
416
|
+
*
|
|
417
|
+
* 与主动分享那条路的 `pending:<pendingId>:<channelId>` 前缀不同,不会撞。
|
|
418
|
+
*/
|
|
419
|
+
inlineDeliveryId(convId, videoId, scope = '') {
|
|
420
|
+
const tail = `${String(convId ?? '').trim()}:${String(videoId ?? '').trim()}`;
|
|
421
|
+
const s = String(scope ?? '').trim();
|
|
422
|
+
return s === '' ? `inline:${tail}` : `inline:${s}:${tail}`;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* 这个会话能不能发原生卡片(普通私信里发抖音链接时要走这条路)。
|
|
427
|
+
*
|
|
428
|
+
* 与 `shareTarget()` 的区别:那个是「分享给主人」的判定(要有 masterUid / masterName),
|
|
429
|
+
* 这个只问「这条会话的身份确认过没有」—— 有会话 ID、能推出对端,就发得出卡片。
|
|
430
|
+
*
|
|
431
|
+
* @returns {{convId: string, name: string}|null} null = 这条会话发不了卡片(退回文字)
|
|
432
|
+
*/
|
|
433
|
+
/**
|
|
434
|
+
* 短链 → 作品 ID(`v.douyin.com/xxx`)。拿不到返回空串,不猜。
|
|
435
|
+
*
|
|
436
|
+
* 抽成方法是为了测试能替换它 —— 真解析要发网络请求。
|
|
437
|
+
*/
|
|
438
|
+
async resolveShortLink(url) {
|
|
439
|
+
return await resolveDouyinShortLink(url);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
shareTargetFor(name, convId) {
|
|
443
|
+
const cfg = this.douyinConfig();
|
|
444
|
+
if (cfg.enabled !== true) return null;
|
|
445
|
+
// 🔴 「发原生卡片」是设置页里的开关(douyin.shareCard)。用户关了就不能在聊天里发。
|
|
446
|
+
// 以前只有「分享给主人」那条路看这个开关,聊天里发链接是纯文字、不受影响;
|
|
447
|
+
// 现在聊天里也走卡片,就必须在这里也判 —— 不然开关形同虚设。
|
|
448
|
+
if (cfg.shareCard !== true) return null;
|
|
449
|
+
const target = String(convId ?? '').trim();
|
|
450
|
+
if (!/^0:1:\d+:\d+$/.test(target)) return null;
|
|
451
|
+
const friendName = String(name ?? '').trim();
|
|
452
|
+
if (friendName === '') return null;
|
|
453
|
+
return { convId: target, name: friendName };
|
|
454
|
+
}
|
|
455
|
+
|
|
403
456
|
/**
|
|
404
457
|
* 抖音私信:文字按空行拆条;单独一段 [名字] 发成抖音自带表情;图片(≤2 张)跟在最后。
|
|
405
458
|
*
|
|
459
|
+
* **单独成段的抖音链接会改发原生卡片**(见上面 shareTargetFor 的判定)。
|
|
460
|
+
*
|
|
406
461
|
* @returns {Promise<{ok: boolean, delivered: boolean, parts: Array<object>}>}
|
|
407
|
-
* `ok` =
|
|
462
|
+
* `ok` = 内容发出去了;`delivered` = 至少一条回读到证据。
|
|
408
463
|
* 🔴 两者要分开报:抖音风控静默吞消息时 `ok` 仍然是 true,
|
|
409
464
|
* 但对方根本收不到 —— 只报 ok 等于撒谎。
|
|
410
465
|
*/
|
|
411
|
-
|
|
466
|
+
|
|
467
|
+
async send(row, text, images = [], convId = '', deliveryScope = '') {
|
|
412
468
|
const channel = this.channel();
|
|
413
469
|
if (channel === null || channel === undefined || typeof channel.send !== 'function') {
|
|
414
470
|
return { ok: false, delivered: false, parts: [], error: '抖音通道没接上' };
|
|
415
471
|
}
|
|
416
472
|
// 🔴 没有目标会话 ID 就不发:昵称只能用来「找到会话」,证明不了「打开的就是这个人」。
|
|
417
473
|
// 通道层还会再挡一道(DouyinChannel.send),这里是给上层一个明确原因。
|
|
474
|
+
// ⚠️ 群会话的 convId 不是 `0:1:` 形状(是 `<数字>:<数字>:...`),
|
|
475
|
+
// 以前这里把群全拒了 ⇒ 群里的回复一条也发不出去(2026-11-19 真机修)。
|
|
476
|
+
// 判据从「必须是单聊形状」改成「必须非空」—— 身份由会话 ID 本身承担,
|
|
477
|
+
// 单聊那边的 expectConvId 一致性检查在通道层仍然守着。
|
|
418
478
|
const target = String(convId ?? '').trim();
|
|
419
|
-
|
|
479
|
+
// 🔴 形状不对的会话 ID 必须拒发(单聊 0:1:数字:数字 / 群聊 数字:数字:数字…)。
|
|
480
|
+
// 只判「非空」会把 abc、0:2:1:2 这类垃圾放进来 —— 会话 ID 是身份,
|
|
481
|
+
// 认不出形状就不能用(test/douyin-send.mjs 守着这条契约)。
|
|
482
|
+
if (!isDouyinConversationId(target)) {
|
|
420
483
|
return { ok: false, delivered: false, parts: [], error: '没有确认过这个抖音会话的身份(缺会话 ID),不发,以免发错人' };
|
|
421
484
|
}
|
|
422
485
|
// 会话名只用来找会话:channel_id 是 `douyin:<名字>`,名字从 row 上取。
|
|
@@ -424,10 +487,27 @@ export class DouyinAdapter {
|
|
|
424
487
|
if (name === '') return { ok: false, delivered: false, parts: [], error: '不知道要发给哪个会话' };
|
|
425
488
|
|
|
426
489
|
const parts = [];
|
|
427
|
-
|
|
490
|
+
// 单独成段的抖音链接 → 原生卡片段(整段就是 https://www.douyin.com/video/<id> 的那种)。
|
|
491
|
+
// 🔴 只处理「单独成段」的:链接和句子混在一段里时不能整段变卡片,前面那半句会跟着消失。
|
|
492
|
+
const shareTarget = this.shareTargetFor(name, target);
|
|
493
|
+
const paras = String(text ?? '').split(/\n{2,}/).map((x) => x.trim()).filter((x) => x !== '');
|
|
494
|
+
for (const para of paras) {
|
|
428
495
|
// 单独一段 [名字] → 抖音自带表情
|
|
429
496
|
const m = /^\[([^\]\n]{1,12})\]$/.exec(para);
|
|
430
497
|
if (m !== null) { parts.push({ type: 'emoji', name: m[1] }); continue; }
|
|
498
|
+
const ids = (douyinVideoIds(para) ?? []).filter((id) => /^\d{8,20}$/.test(String(id)));
|
|
499
|
+
if (shareTarget !== null && ALONE_LINK.test(para)) {
|
|
500
|
+
// 🔴 短链(`v.douyin.com/xxx`)也要能变卡片(2026-11-19 修):
|
|
501
|
+
// 以前只认 `douyin.com/video/<数字>`,短链取不到作品 ID ⇒ 留在文字里。
|
|
502
|
+
// 短链要跳一次才知道是哪个作品,所以这里 await;拿不到就照旧当文字
|
|
503
|
+
// (**不猜** —— 猜错就是发错视频的卡片,比不发严重)。
|
|
504
|
+
let videoId = ids.length === 1 ? String(ids[0]) : '';
|
|
505
|
+
if (videoId === '') videoId = await this.resolveShortLink(para);
|
|
506
|
+
if (/^\d{8,20}$/.test(videoId)) {
|
|
507
|
+
parts.push({ type: 'card', videoId, fallbackText: para });
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
431
511
|
parts.push({ type: 'text', text: para });
|
|
432
512
|
}
|
|
433
513
|
for (const img of images.slice(0, 2)) {
|
|
@@ -435,8 +515,110 @@ export class DouyinAdapter {
|
|
|
435
515
|
if (file !== '') parts.push({ type: 'image', path: file });
|
|
436
516
|
}
|
|
437
517
|
if (parts.length === 0) return { ok: false, delivered: false, parts: [], error: '没有可发的内容' };
|
|
438
|
-
|
|
439
|
-
|
|
518
|
+
// 🔴 **按原顺序交错发送**(2026-11-19 真机修)。
|
|
519
|
+
// 以前卡片全先发、文字后发 ⇒ 「文字/链接/文字/链接」变成「卡、卡、文字、文字」,
|
|
520
|
+
// 文字和链接的配对关系没了。现在逐段发:文字段 → channel.send,卡片段 → shareCard。
|
|
521
|
+
// 连续的文字段合并成一段(每张卡都要走一遍分享面板,占浏览器独占锁;
|
|
522
|
+
// 不合并的话每张卡之间都要进一次会话)。
|
|
523
|
+
const textParts = [];
|
|
524
|
+
let cardFailed = '';
|
|
525
|
+
let cardSubmitted = false;
|
|
526
|
+
const sentParts = [];
|
|
527
|
+
const flushText = async () => {
|
|
528
|
+
if (textParts.length === 0) return null;
|
|
529
|
+
const chunk = textParts.splice(0, textParts.length);
|
|
530
|
+
const r = await channel.send(name, chunk, target);
|
|
531
|
+
sentParts.push(...(r?.parts ?? []));
|
|
532
|
+
return r;
|
|
533
|
+
};
|
|
534
|
+
let lastTextResult = null;
|
|
535
|
+
for (const part of parts) {
|
|
536
|
+
if (part.type === 'card') {
|
|
537
|
+
// 先把手头攒着的文字段发出去(保持「文字 → 卡片」的原顺序)
|
|
538
|
+
const flushed = await flushText();
|
|
539
|
+
if (flushed) lastTextResult = flushed;
|
|
540
|
+
if (shareTarget === null || typeof channel.shareCard !== 'function') {
|
|
541
|
+
cardFailed = shareTarget === null ? '这条会话发不了卡片' : '这个通道不支持发卡片';
|
|
542
|
+
textParts.push({ type: 'text', text: part.fallbackText });
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
// 🔴 走交付账本(2026-11-19 修)。
|
|
546
|
+
// 以前内联卡片**完全不记账** ⇒ 同一封私信被判「待核实」后主人点重试,
|
|
547
|
+
// 会再发一张卡(对方收到两张一模一样的)。
|
|
548
|
+
// 现在跟主动分享共用同一套 `douyin.cardDelivery.*` 账本:
|
|
549
|
+
// 提交过就绝不自动重发(`unknown` 状态会直接拦住)。
|
|
550
|
+
const sendCard = (id, onSubmitted) => channel.shareCard({
|
|
551
|
+
videoId: id, convId: shareTarget.convId, friendName: shareTarget.name,
|
|
552
|
+
}, onSubmitted);
|
|
553
|
+
// 🔴 账本写不进去时(库坏了 / 老打桩没给 kv 表)**降级成不记账照常发**:
|
|
554
|
+
// 宁可这一次没账本,也别因为记账失败就不发卡片。
|
|
555
|
+
const delivery = await deliverDouyinCards({
|
|
556
|
+
db: this.db,
|
|
557
|
+
deliveryId: this.inlineDeliveryId(shareTarget.convId, part.videoId, deliveryScope),
|
|
558
|
+
videoIds: [part.videoId],
|
|
559
|
+
// 说明文字由外面统一发(这里只发卡片)
|
|
560
|
+
caption: '',
|
|
561
|
+
sendCard,
|
|
562
|
+
sendText: async () => ({ ok: true, delivered: true }),
|
|
563
|
+
log: (stage, info) => this.log('info', `[fish-soul] ${stage} ${JSON.stringify(info)}`),
|
|
564
|
+
}).catch((error) => {
|
|
565
|
+
this.log('warn', `[fish-soul] 内联卡片的交付账本不可用(${String(error?.message || error)}),这次不记账照常发`);
|
|
566
|
+
return { ok: null, ledgerBroken: true, error: String(error?.message || error) };
|
|
567
|
+
});
|
|
568
|
+
if (delivery?.ledgerBroken === true) {
|
|
569
|
+
// 账本坏了:直接发,按卡片自己的结果判定(没有「提交过就别重发」的保护)
|
|
570
|
+
const card = await sendCard(part.videoId, () => {})
|
|
571
|
+
.catch((error) => ({ ok: false, verified: false, error: String(error?.message || error) }));
|
|
572
|
+
if (card?.ok === true && card?.verified === true) { part.cardOk = true; continue; }
|
|
573
|
+
if (card?.submitted === true) {
|
|
574
|
+
part.cardUnknown = true;
|
|
575
|
+
cardSubmitted = true;
|
|
576
|
+
cardFailed = String(card?.error ?? '卡片已提交但没回读到,可能已发出');
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
cardFailed = String(card?.error ?? '卡片没发出去');
|
|
580
|
+
textParts.push({ type: 'text', text: part.fallbackText });
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
if (delivery?.ok === true && delivery?.verified === true) { part.cardOk = true; continue; }
|
|
584
|
+
// 🔴 提交过但没回读到 ⇒ **可能已经发出去了**。这时绝不能把链接当文字再发一遍
|
|
585
|
+
// (对方会同时收到卡片和同一条链接)。也不能说「没发出去」。
|
|
586
|
+
if (delivery?.unknown === true) {
|
|
587
|
+
part.cardUnknown = true;
|
|
588
|
+
cardSubmitted = true;
|
|
589
|
+
cardFailed = String(delivery?.error ?? '卡片已提交但没回读到,可能已发出');
|
|
590
|
+
continue;
|
|
591
|
+
}
|
|
592
|
+
cardFailed = String(delivery?.error ?? '卡片没发出去');
|
|
593
|
+
textParts.push({ type: 'text', text: part.fallbackText });
|
|
594
|
+
} else {
|
|
595
|
+
textParts.push(part);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const flushed = await flushText();
|
|
599
|
+
if (flushed) lastTextResult = flushed;
|
|
600
|
+
const r = lastTextResult ?? (sentParts.length === 0
|
|
601
|
+
? { ok: true, delivered: false, parts: [] }
|
|
602
|
+
: { ok: true, delivered: false, parts: sentParts });
|
|
603
|
+
// 🔴 ok 的语义是「**对方收到内容了**」,不是「每一步都按我想要的方式成功了」。
|
|
604
|
+
// 网关据此决定收件箱状态(settle):ok=false 会推进到「待核实」,
|
|
605
|
+
// 主人会被提示「回复没发出去」—— 可实际上文字已经送到对方了。
|
|
606
|
+
// 所以:卡片失败但退回的文字发出去了 ⇒ ok 仍然是 true,卡片的问题走 cardFailed 单独报。
|
|
607
|
+
const cardParts = parts.filter((p2) => p2.type === 'card');
|
|
608
|
+
const cardOk = cardParts.every((p2) => p2.cardOk === true);
|
|
609
|
+
const textOk = r?.ok === true;
|
|
610
|
+
// 提交过但没回读到:内容**大概率**已经在那儿了,但不能当成确认。
|
|
611
|
+
// 这时 ok 仍按「有内容发出去了」算 true,但 delivered 只在真确认过时为 true。
|
|
612
|
+
const contentOut = cardOk || cardSubmitted || textOk;
|
|
613
|
+
const delivered = r?.delivered === true || cardParts.some((p2) => p2.cardOk === true);
|
|
614
|
+
return {
|
|
615
|
+
ok: contentOut,
|
|
616
|
+
delivered,
|
|
617
|
+
parts: r?.parts ?? [],
|
|
618
|
+
cardOk,
|
|
619
|
+
cardFailed,
|
|
620
|
+
error: contentOut ? '' : (r?.error || cardFailed || '发送失败')
|
|
621
|
+
};
|
|
440
622
|
}
|
|
441
623
|
|
|
442
624
|
// ───────────── 路由信息(PR3) ─────────────
|
|
@@ -146,3 +146,38 @@ export function senderBelongsToConversation(message, selfIds) {
|
|
|
146
146
|
// 发送者必须是会话两侧之一
|
|
147
147
|
return mine.has(sender) || sender === pair.a || sender === pair.b;
|
|
148
148
|
}
|
|
149
|
+
|
|
150
|
+
// ───────────────────────── 会话 ID 形状校验(单聊 + 群聊) ─────────────────────────
|
|
151
|
+
//
|
|
152
|
+
// 为什么单独抽出来:会话 ID 是**身份**,形状不对的字符串(「abc」「0:1:x:2」)
|
|
153
|
+
// 绝不能被当成会话 —— 那是「拿昵称猜人」那类洞的入口。
|
|
154
|
+
//
|
|
155
|
+
// 单聊:`0:1:<我的uid>:<对方uid>`
|
|
156
|
+
// 群聊:`<数字>:<数字>:<数字>...`(抖音平台给的 conversation_id,形状跟单聊不一样)
|
|
157
|
+
//
|
|
158
|
+
// ⚠️ 群的形状比单聊宽:如果只判「非空」,`'abc'`、`'0:2:1:2'` 这类垃圾会被放进去
|
|
159
|
+
// (2026-11-19 审查发现:test/douyin-send.mjs 的「形状不对必须拒发」契约被打破了)。
|
|
160
|
+
// 所以这里两种都认,其余一律拒。
|
|
161
|
+
|
|
162
|
+
/** 单聊形状:`0:1:<数字>:<数字>`。 */
|
|
163
|
+
export const SINGLE_CONV_RE = /^0:1:\d+:\d+$/;
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 群聊形状:`<数字>:<数字>:<数字>` 起步的会话 ID。
|
|
167
|
+
*
|
|
168
|
+
* 抖音的群 conversation_id 形如 `7512345678901234567:987654321098765432:0`。
|
|
169
|
+
* 段数可能不止三个,所以这里只要求「至少三段、每段都是数字」。
|
|
170
|
+
*/
|
|
171
|
+
export const GROUP_CONV_RE = /^[1-9]\d+(?::\d+){2,}$/;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 这个会话 ID 是不是我们认得的形状(单聊或群聊)。
|
|
175
|
+
*
|
|
176
|
+
* @param {unknown} convId
|
|
177
|
+
* @returns {boolean}
|
|
178
|
+
*/
|
|
179
|
+
export function isDouyinConversationId(convId) {
|
|
180
|
+
const s = String(convId ?? '').trim();
|
|
181
|
+
if (s === '') return false;
|
|
182
|
+
return SINGLE_CONV_RE.test(s) || GROUP_CONV_RE.test(s);
|
|
183
|
+
}
|
package/lib/douyin-inbox.js
CHANGED
|
@@ -89,29 +89,44 @@ export class DouyinInbox {
|
|
|
89
89
|
for (const r of left) this.noteGap(r.channel_id, GAP_INTERRUPTED);
|
|
90
90
|
}
|
|
91
91
|
beginRead(key, channelId) {
|
|
92
|
+
// 声明在 try 块外:事务结束后还要用它决定要不要记日志。
|
|
93
|
+
let left = null;
|
|
92
94
|
this.db.exec('BEGIN');
|
|
93
95
|
try {
|
|
94
96
|
// 上一次这个会话没读完(读取意图还留着)→ 可能漏了消息
|
|
95
|
-
|
|
97
|
+
left = this.db.prepare('SELECT channel_id FROM douyin_inbox_read_intent WHERE cursor_key=?').get(key);
|
|
96
98
|
this.db.prepare("INSERT INTO douyin_inbox_gap(channel_id,reason,created_at) SELECT i.channel_id,?,? FROM douyin_inbox_read_intent i WHERE i.cursor_key=? AND NOT EXISTS (SELECT 1 FROM douyin_inbox_gap g WHERE g.channel_id=i.channel_id AND g.acknowledged=0)").run(GAP_STALE, Date.now(), key);
|
|
97
99
|
this.db.prepare('INSERT OR REPLACE INTO douyin_inbox_read_intent(cursor_key,channel_id,started_at) VALUES(?,?,?)').run(key,channelId,Date.now());
|
|
98
100
|
this.db.exec('COMMIT');
|
|
99
|
-
if (left) this.noteGap(left.channel_id, GAP_STALE);
|
|
100
101
|
} catch(e) { this.db.exec('ROLLBACK'); throw e; }
|
|
102
|
+
// 🔴 记日志在**事务之外**(2026-11-19 真机修):诊断日志器一旦抛错,
|
|
103
|
+
// catch 会去 ROLLBACK 一个**已经提交**的事务 ⇒ 「no transaction is active」
|
|
104
|
+
// ⇒ 整条读取流程抛出去 ⇒ 读取被一条日志搞崩。
|
|
105
|
+
try {
|
|
106
|
+
// 去重交给 DiagLog(它按 key 合并计数,窗口比轮询间隔大)。
|
|
107
|
+
if (left) this.noteGap(left.channel_id, GAP_STALE);
|
|
108
|
+
} catch { /* 记日志失败不影响读取流程 */ }
|
|
101
109
|
}
|
|
102
110
|
failRead(key, cause = '读取失败') {
|
|
103
111
|
const reason = `${cause},${GAP_UNREAD_TAIL}`;
|
|
112
|
+
// 声明在 try 块外:事务结束后还要用它决定要不要记日志。
|
|
113
|
+
let intent = null;
|
|
104
114
|
this.db.exec('BEGIN');
|
|
105
115
|
try {
|
|
106
116
|
// 读之前记过读取意图:这里拿得到是哪个会话
|
|
107
|
-
|
|
117
|
+
intent = this.db.prepare('SELECT channel_id FROM douyin_inbox_read_intent WHERE cursor_key=?').get(key);
|
|
108
118
|
this.db.prepare("INSERT INTO douyin_inbox_gap(channel_id,reason,created_at) SELECT i.channel_id,?,? FROM douyin_inbox_read_intent i WHERE i.cursor_key=? AND NOT EXISTS (SELECT 1 FROM douyin_inbox_gap g WHERE g.channel_id=i.channel_id AND g.acknowledged=0)").run(reason,Date.now(),key);
|
|
109
119
|
this.db.prepare('DELETE FROM douyin_inbox_read_intent WHERE cursor_key=?').run(key);
|
|
110
120
|
this.db.exec('COMMIT');
|
|
121
|
+
} catch(e) { this.db.exec('ROLLBACK'); throw e; }
|
|
122
|
+
// 🔴 记日志在**事务之外**(2026-11-19 真机修):诊断日志器一旦抛错,
|
|
123
|
+
// catch 会去 ROLLBACK 一个**已经提交**的事务 ⇒ 「no transaction is active」
|
|
124
|
+
// ⇒ 整条读取流程抛出去 ⇒ 读取被一条日志搞崩。
|
|
125
|
+
try {
|
|
111
126
|
// 表里那条提示可能因为「这个会话已经有未确认的提示」而没插进去,
|
|
112
127
|
// 但这次读确实失败了 —— 日志记的是**这件事**,不是表里多了一行。
|
|
113
128
|
if (intent) this.noteGap(intent.channel_id, reason);
|
|
114
|
-
} catch
|
|
129
|
+
} catch { /* 记日志失败不影响读取流程 */ }
|
|
115
130
|
}
|
|
116
131
|
record(key, cursor, messages, gap = null) {
|
|
117
132
|
this.db.exec('BEGIN');
|
|
@@ -121,13 +136,23 @@ export class DouyinInbox {
|
|
|
121
136
|
for(const m of messages) stmt.run(m.channelId,m.messageId,JSON.stringify(m),now,now);
|
|
122
137
|
// 同一会话的同一类读取缺口在用户确认前只保留一条;轮询会反复看到
|
|
123
138
|
// 相同的窗口错位,不能把一条问题放大成几十条提示。
|
|
124
|
-
|
|
139
|
+
// 缺口表自带去重(同一会话 + 同一原因,未确认的只留一条)。
|
|
140
|
+
// 运行日志的去重交给 DiagLog(它按 key 合并计数,窗口比轮询间隔大)。
|
|
141
|
+
if (gap) {
|
|
142
|
+
this.db.prepare("INSERT INTO douyin_inbox_gap(channel_id,reason,created_at) SELECT ?,?,? WHERE NOT EXISTS (SELECT 1 FROM douyin_inbox_gap WHERE channel_id=? AND reason=? AND acknowledged=0)").run(gap.channelId,gap.reason,now,gap.channelId,gap.reason);
|
|
143
|
+
}
|
|
125
144
|
setKv(this.db,key,cursor);
|
|
126
145
|
this.db.prepare('DELETE FROM douyin_inbox_read_intent WHERE cursor_key=?').run(key);
|
|
127
146
|
this.db.exec('COMMIT');
|
|
128
|
-
// 窗口对不齐这件事本身要记(表里那条可能被「已有未确认的提示」挡掉)
|
|
129
|
-
if (gap) this.noteGap(gap.channelId, gap.reason);
|
|
130
147
|
} catch(e) { this.db.exec('ROLLBACK'); throw e; }
|
|
148
|
+
// 🔴 记日志在**事务之外**(2026-11-19 真机修)。
|
|
149
|
+
// 以前 noteGap 在 try 块里、COMMIT 之后:诊断日志器一旦抛错,
|
|
150
|
+
// catch 会去 ROLLBACK 一个**已经提交**的事务 ⇒ 「no transaction is active」
|
|
151
|
+
// ⇒ 整条 record 抛出去 ⇒ 消息入库被一条日志搞崩。
|
|
152
|
+
// 日志记不进去不该影响消息入库 —— 两件事要分开。
|
|
153
|
+
try {
|
|
154
|
+
if (gap) this.noteGap(gap.channelId, gap.reason);
|
|
155
|
+
} catch { /* 记日志失败不影响已经入库的消息 */ }
|
|
131
156
|
}
|
|
132
157
|
/**
|
|
133
158
|
* 收件表保留期。暂停期间每轮轮询都会继续入库,而 UI 只显示前 10 条,
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// 聊天里发的抖音链接 → 原生卡片。
|
|
2
|
+
//
|
|
3
|
+
// 问题(2026-11-19 真机):主人问「有没有有趣的视频分享给我」,她用**文字**回了两条链接。
|
|
4
|
+
// 原生卡片只在「她自己起意的分享待办」那条路才发(`origin='share'` → `deliverShare`);
|
|
5
|
+
// 普通聊天回复走 `send` → 纯文本 ⇒ 链接就是链接,卡片没了。
|
|
6
|
+
//
|
|
7
|
+
// 修法:在出站(`DouyinAdapter.send`)这一层把文本拆段,**单独成段的链接**改发卡片,其余照旧。
|
|
8
|
+
//
|
|
9
|
+
// 拆段形态(真机那句话就是典型):
|
|
10
|
+
// 「猫跳舞那个…笑半天」\n\n<链接1>\n\n「还有个桌宠小鲸鱼的…」\n\n<链接2>
|
|
11
|
+
// ⇒ [文字, 卡片, 文字, 卡片]
|
|
12
|
+
//
|
|
13
|
+
// 🔴 三个边界,一个都不能省:
|
|
14
|
+
// ① 卡片发失败要**退回文字**,不能让那句话凭空消失(宁可退回链接,也不能丢内容)
|
|
15
|
+
// ② 一段里既有文字又有链接(「看这个 https://… 挺好笑」)不能整段变卡片 ——
|
|
16
|
+
// 那样前面那半句会跟着消失。只处理「单独成段」的链接,所以这条正则必须是整段匹配。
|
|
17
|
+
// ③ 卡片发送只认「会话 ID 能推出对端」的会话(`shareTargetFor`),不按昵称猜人。
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 整段就是一条抖音链接(前后没有别的字)。
|
|
21
|
+
*
|
|
22
|
+
* 注意是 `^...$` 全匹配,不是 `test(包含)` —— 见上面边界 ②。
|
|
23
|
+
*/
|
|
24
|
+
export const ALONE_LINK = /^https?:\/\/(?:www\.)?(?:douyin\.com\/video\/|v\.douyin\.com\/)\S*$/;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// 抖音短链 → 作品 ID。
|
|
2
|
+
//
|
|
3
|
+
// 为什么单独抽出来:`v.douyin.com/xxx` 只有跳转一次才知道是哪个作品,
|
|
4
|
+
// 而这件事在 `DouyinBrowser`(刷视频那条路)和 `DouyinAdapter`(发消息那条路)
|
|
5
|
+
// 都要用。放浏览器里的话,发消息那条路就得凭空依赖一个浏览器实例。
|
|
6
|
+
//
|
|
7
|
+
// ⚠️ 拿不到就返回空串,**不猜**:猜错就是发错视频的卡片,比不发严重。
|
|
8
|
+
|
|
9
|
+
const MOBILE_UA = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
|
|
10
|
+
|
|
11
|
+
/** 跳转地址里带的作品 ID(`/video/<数字>` 或 `?modal_id=<数字>` 两种都见过)。 */
|
|
12
|
+
const ID_IN_URL = /(?:video\/|modal_id=|aweme_id=)(\d{8,20})(?!\d)/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 跟随 `v.douyin.com` 短链,拿作品 ID。
|
|
16
|
+
*
|
|
17
|
+
* @param {string} url 短链(也接受完整 `douyin.com/video/<id>` —— 那种直接返回 id)
|
|
18
|
+
* @param {{fetchImpl?: Function, timeoutMs?: number}} [opts]
|
|
19
|
+
* @returns {Promise<string>} 作品 ID;拿不到返回 ''
|
|
20
|
+
*/
|
|
21
|
+
export async function resolveDouyinShortLink(url, { fetchImpl, timeoutMs = 5000 } = {}) {
|
|
22
|
+
const start = String(url ?? '').trim();
|
|
23
|
+
if (start === '') return '';
|
|
24
|
+
// 已经是完整链接:不用发请求
|
|
25
|
+
const direct = ID_IN_URL.exec(start);
|
|
26
|
+
if (direct !== null) return direct[1];
|
|
27
|
+
|
|
28
|
+
const doFetch = fetchImpl ?? ((...a) => fetch(...a));
|
|
29
|
+
let cur = start;
|
|
30
|
+
for (let i = 0; i < 3; i++) {
|
|
31
|
+
try {
|
|
32
|
+
const res = await doFetch(cur, {
|
|
33
|
+
redirect: 'manual',
|
|
34
|
+
headers: { 'User-Agent': MOBILE_UA },
|
|
35
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
36
|
+
});
|
|
37
|
+
const loc = res?.headers?.get?.('location') ?? '';
|
|
38
|
+
if (loc === '') return '';
|
|
39
|
+
const hit = ID_IN_URL.exec(loc);
|
|
40
|
+
if (hit !== null) return hit[1];
|
|
41
|
+
cur = new URL(loc, cur).href;
|
|
42
|
+
} catch {
|
|
43
|
+
// 超时 / 断网 / 地址不合法:一律当「拿不到」,不猜
|
|
44
|
+
return '';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return '';
|
|
48
|
+
}
|
package/lib/gateway-media.js
CHANGED
|
@@ -547,8 +547,8 @@ export const mediaMethods = {
|
|
|
547
547
|
* 🔴 两者要分开报:抖音风控静默吞消息时 `ok` 仍然是 true,
|
|
548
548
|
* 但对方根本收不到 —— 只报 ok 等于撒谎。
|
|
549
549
|
*/
|
|
550
|
-
async sendDouyin(row, text, images = [], convId = '') {
|
|
551
|
-
return await adapterOf(this).send(row, text, images, convId);
|
|
550
|
+
async sendDouyin(row, text, images = [], convId = '', deliveryScope = '') {
|
|
551
|
+
return await adapterOf(this).send(row, text, images, convId, deliveryScope);
|
|
552
552
|
},
|
|
553
553
|
|
|
554
554
|
/**
|
package/lib/gateway.js
CHANGED
|
@@ -1860,23 +1860,34 @@ export class ChatGateway {
|
|
|
1860
1860
|
let ok;
|
|
1861
1861
|
let delivered;
|
|
1862
1862
|
let detail = '';
|
|
1863
|
+
// 卡片没发出去的原因(只有抖音那条路有)。QQ / B站 没有这个概念。
|
|
1864
|
+
let cardFailed = '';
|
|
1863
1865
|
if (platform === 'bili') {
|
|
1864
1866
|
ok = await this.sendBili(row, dmText, images);
|
|
1865
1867
|
delivered = ok; // B站 那条是直连接口,onMessage 就带 message_id,没有「静默被吞」这回事
|
|
1866
1868
|
} else {
|
|
1867
1869
|
// convId 是目标会话 ID:sendDouyin 拿它确认收件人,拿不到就拒绝发送。
|
|
1868
|
-
|
|
1870
|
+
// deliveryScope = 本轮的 sessionId:同一轮重试账本认得出(不重复发卡),
|
|
1871
|
+
// 下一轮是新 id(她再说一次同一个视频能发出第二张)。
|
|
1872
|
+
const r = await this.sendDouyin(row, dmText, images, convId, sessionId);
|
|
1869
1873
|
ok = r.ok === true;
|
|
1870
1874
|
delivered = r.delivered === true;
|
|
1871
1875
|
// 具体原因要留下来。以前写死「发送失败」,真机上分不清是身份校验拒的、
|
|
1872
1876
|
// 进不去会话,还是风控吞的(2026-11-11 连着三轮都只看到这四个字)。
|
|
1873
1877
|
detail = r?.error || '';
|
|
1878
|
+
// 卡片失败(浏览器没跑 / 收件人核验不过):内容退回文字发出去了,但卡片没发。
|
|
1879
|
+
// 这两件事要分开:ok 说的是「内容送到没有」,卡片的问题单独记。
|
|
1880
|
+
cardFailed = String(r?.cardFailed ?? '');
|
|
1874
1881
|
}
|
|
1875
1882
|
// 🔴 「动作做了」和「对方真能看见」分开记:
|
|
1876
1883
|
// 抖音风控会静默吞消息(Enter 按了、UI 没报错、消息没进会话)。
|
|
1877
1884
|
// 只记 sent 会让动态页/日记看起来一切正常,实际上对方什么都没收到。
|
|
1885
|
+
// 🔴 卡片没发出去但**退回的链接已经发过去了**:内容是收到了的,不能记「发送失败」
|
|
1886
|
+
// (那会让收件箱推进到「待核实」,主人被提示回复没发出去,其实对方收到了链接)。
|
|
1887
|
+
// 卡片的问题单独写进 reason,不影响 sent / unconfirmed 的判定。
|
|
1888
|
+
const cardNote = cardFailed ? `(原生卡片没发出去:${cardFailed.slice(0, 40)})` : '';
|
|
1878
1889
|
const kind = !ok ? 'failed' : (delivered ? 'sent' : 'unconfirmed');
|
|
1879
|
-
const reason = !ok ? (detail || '发送失败') : (delivered ?
|
|
1890
|
+
const reason = !ok ? (detail || '发送失败') : (delivered ? cardNote : ('发出去了但回读没看到,可能被风控吞了' + cardNote));
|
|
1880
1891
|
this.note(kind, { channelId: row.channel_id, title: row.title, text: dmText || '[图片]', reason, memories, ...recallNote });
|
|
1881
1892
|
if (ok && dmText) this.soul.journalOut?.(row.channel_id, dmText, { ownerId: st.journalOwnerId ?? null });
|
|
1882
1893
|
return ok;
|
package/lib/index.js
CHANGED
|
@@ -62,6 +62,7 @@ import { GroupList, groupListPromptLine } from './group-list.js';
|
|
|
62
62
|
import { tokenizeForBm25 } from './segment.js';
|
|
63
63
|
import { loadPersona, matchLore, applyMacros } from './persona.js';
|
|
64
64
|
import { memoryLabel as labelMemory } from './memory-label.js';
|
|
65
|
+
import { stateLineText, weekdayOf } from './time-awareness.js';
|
|
65
66
|
import { WorldBooks, loreConfig } from './worldinfo.js';
|
|
66
67
|
|
|
67
68
|
/** 记忆段标题后面带一次:对得上的就说出具体的事,让对方感觉到被记住 */
|
|
@@ -368,7 +369,10 @@ async function startup(ctx, config, { log, gate }) {
|
|
|
368
369
|
const douyinHttp = new DouyinHttp({ getConfig: douyinCfg, log, cdpPort: hook.douyinCdpPort ?? douyinHost.port ?? 47890, fetchImpl: hook.fetch });
|
|
369
370
|
const douyin = new DouyinChannel({
|
|
370
371
|
host: douyinHost, db, getConfig: douyinCfg, log, notify: notifyMaster,
|
|
371
|
-
|
|
372
|
+
// 🔴 群消息要走**群**的入口(2026-11-19 真机修)。
|
|
373
|
+
// 以前不管群里群里一律进 onDouyinMessage ⇒ 群消息按单聊的规矩处理,
|
|
374
|
+
// userId 形状对不上 ⇒ 每条都被「身份未确认」拒掉。
|
|
375
|
+
onMessage: (m) => (m?.chatType === 'group' ? gateway?.onDouyinGroupMessage(m) : gateway?.onDouyinMessage(m)),
|
|
372
376
|
// 「私信可能遗漏 / 私信待核实」设置页不显示了,落到系统页「运行日志」里
|
|
373
377
|
diag,
|
|
374
378
|
driver: hook.driver?.(douyinHost), pollRangeMs: hook.pollRangeMs, typingMs: hook.typingMs, firstPollMs: hook.firstPollMs
|
|
@@ -501,6 +505,9 @@ async function startup(ctx, config, { log, gate }) {
|
|
|
501
505
|
/** 网关在把一批 QQ 消息 / 主动触发递进会话之前调用:登记本轮说话人、预取向量、取到期约定。 */
|
|
502
506
|
function beginTurn(sessionId, { channel: ch, channelId, replyChannel, speakers, workspacePath, backlog = [], interject = false, voiceIn = false, proactive = false, desktop = false }) {
|
|
503
507
|
const recentLines = journal.recent(channelId, 4);
|
|
508
|
+
// 距上次说话多久:必须在本轮 journal.add **之前**取,否则 lastAt 刚被刷新成"现在"
|
|
509
|
+
const lastSpokeAt = journal.lastSpokeAt(channelId);
|
|
510
|
+
const idleMs = lastSpokeAt > 0 ? Math.max(0, Date.now() - lastSpokeAt) : null;
|
|
504
511
|
const sp = (speakers ?? []).map((s) => ({
|
|
505
512
|
ownerId: ownerOf(s.person), person: s.person ?? null, userId: String(s.userId ?? ''),
|
|
506
513
|
displayName: s.displayName || s.person?.display_name || '对方',
|
|
@@ -524,7 +531,7 @@ async function startup(ctx, config, { log, gate }) {
|
|
|
524
531
|
// 只有「回复规则」按它给;记忆 / 聊天日志 / 认人仍然按 channelId(话是在哪儿说的就记在哪儿)。
|
|
525
532
|
sessions.set(String(sessionId), {
|
|
526
533
|
channel: ch, channelId, replyChannel: replyChannel || channelId, speakers: sp, dueReasons, workspacePath: workspacePath ?? '',
|
|
527
|
-
atReal: Date.now(), backlog, interject, voiceIn, desktop, recalled: [], recentLines,
|
|
534
|
+
atReal: Date.now(), backlog, interject, voiceIn, desktop, recalled: [], recentLines, idleMs,
|
|
528
535
|
// 动态页据此区分「瞬态块没跑」「认不出说话人」「跑了但没召回到」—— 空着的时候看不出来是哪一种(#78)
|
|
529
536
|
recalledAt: 0,
|
|
530
537
|
speakerUnknown: sp.length > 0 && sp.every((x) => x.ownerId === UNKNOWN_OWNER)
|
|
@@ -636,15 +643,16 @@ async function startup(ctx, config, { log, gate }) {
|
|
|
636
643
|
return `现在是 ${life.stateSnapshot().clock.virtualIsoLocal.slice(0, 16)}`;
|
|
637
644
|
}
|
|
638
645
|
|
|
639
|
-
function stateLine() {
|
|
646
|
+
function stateLine(session = null) {
|
|
640
647
|
const snap = life.stateSnapshot();
|
|
641
|
-
const slot = snap.today.currentSlot;
|
|
642
648
|
// #75:日程是"她自己想的安排",别让她当成已经发生过的事(过去式日程会变成假记忆)
|
|
643
|
-
return
|
|
644
|
-
|
|
645
|
-
(
|
|
646
|
-
|
|
647
|
-
|
|
649
|
+
return stateLineText({
|
|
650
|
+
virtualIsoLocal: snap.clock.virtualIsoLocal,
|
|
651
|
+
weekday: weekdayOf(snap.clock.virtualDate),
|
|
652
|
+
idleMs: session?.idleMs ?? null,
|
|
653
|
+
tone: snap.today.dailyMoodTone,
|
|
654
|
+
slot: snap.today.currentSlot
|
|
655
|
+
});
|
|
648
656
|
}
|
|
649
657
|
|
|
650
658
|
// 一条记忆给模型看的样子(含 evidence = 这条是怎么来的)。
|
|
@@ -713,7 +721,7 @@ async function startup(ctx, config, { log, gate }) {
|
|
|
713
721
|
*/
|
|
714
722
|
function buildContextBlock(arg) {
|
|
715
723
|
const session = sessionFor(arg?.agent?.id ?? arg?.scope?.id ?? arg?.sessionId);
|
|
716
|
-
const lines = [stateLine()];
|
|
724
|
+
const lines = [stateLine(session)];
|
|
717
725
|
const desktopTurn = session?.desktop === true;
|
|
718
726
|
if (desktopTurn && currentConfig.voice?.desktopTts === true && voice.readyFor('tts')) {
|
|
719
727
|
lines.push('[DSH 本地语音] 要用语音说的话单独一段、开头写 [语音],不要带表情或链接;只生成本地文件,不会发到 QQ。');
|
package/lib/routes.js
CHANGED
|
@@ -940,7 +940,7 @@ export function registerRoutes(ctx, deps) {
|
|
|
940
940
|
const limit = Math.min(50, Math.max(1, Math.floor(Number(url.searchParams.get('limit')) || 20)));
|
|
941
941
|
const posts = db.prepare(`SELECT id, post_id, content, created_real_at FROM social_post
|
|
942
942
|
WHERE platform = 'bili' AND created_real_at < ? ORDER BY created_real_at DESC LIMIT ?`).all(before, limit + 1)
|
|
943
|
-
.map((r) => ({ key: `mp${r.id}`, kind: 'post', text: r.content, at: r.created_real_at }));
|
|
943
|
+
.map((r) => ({ key: `mp${r.id}`, kind: 'post', pid: r.post_id, text: r.content, at: r.created_real_at }));
|
|
944
944
|
const replies = db.prepare(`SELECT rowid, from_name, subject, content, reply, status, created_real_at FROM social_reply
|
|
945
945
|
WHERE platform = 'bili' AND status != 'backlog' AND created_real_at < ? ORDER BY created_real_at DESC LIMIT ?`).all(before, limit + 1)
|
|
946
946
|
.map((r) => ({ key: `mc${r.rowid}`, kind: 'reply', status: r.status, text: `${r.from_name}:${r.content}${r.reply ? ` → ${r.reply}` : ''}`, at: r.created_real_at }));
|
package/lib/soul-memory.js
CHANGED
|
@@ -338,6 +338,27 @@ export class ChatJournal {
|
|
|
338
338
|
c.lastAt = l.at;
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
+
/**
|
|
342
|
+
* 这个通道上一次有人说话的时刻(真实毫秒),没人说过返回 0。
|
|
343
|
+
*
|
|
344
|
+
* 读的是 add() 维护的 lastAt —— 所以调用方必须在**本轮 add 之前**取,
|
|
345
|
+
* 否则读到的永远是"刚刚"(beginTurn 里 recentLines 和它同一个位置取)。
|
|
346
|
+
*/
|
|
347
|
+
lastSpokeAt(channelId) {
|
|
348
|
+
// 只认**对方**说的:lastAt 会被她自己的发言刷新(journalOut 也走 add),
|
|
349
|
+
// 拿它当"距上次说话"会低报对方的沉默时长 —— 主人 20 小时没说话、
|
|
350
|
+
// 她 6 小时前回过一句,就变成"距上次说话 6 小时"。
|
|
351
|
+
// SillyTavern 的 {{idleDuration}} 也是"since the last *user* message"。
|
|
352
|
+
const recent = this.chats.get(channelId)?.recent ?? [];
|
|
353
|
+
for (let i = recent.length - 1; i >= 0; i--) {
|
|
354
|
+
const one = recent[i];
|
|
355
|
+
if (one?.self === true) continue;
|
|
356
|
+
const at = Number(one?.at ?? 0);
|
|
357
|
+
if (at > 0) return at;
|
|
358
|
+
}
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
|
|
341
362
|
/** 最近 n 句("名字:内容",她自己是"我")。 */
|
|
342
363
|
recent(channelId, n = 4) {
|
|
343
364
|
return (this.chats.get(channelId)?.recent ?? []).slice(-n).map((l) => `${l.self ? '我' : l.who}:${l.text}`);
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// 「此刻状态」这一行怎么拼 —— 抽成纯函数才测得动。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要带星期和「距上次说话多久」:
|
|
4
|
+
// 原来这行只有「虚拟时间 2026-11-19 18:11」,她能读出几点,但读不出「周几」和
|
|
5
|
+
// 「上一条是 5 分钟前还是 3 天前」。前者让「周末」「周一上班」这类话题无从接起,
|
|
6
|
+
// 后者让「好久不见」永远不出现 —— 两处都是上下文里从来没有过的信息。
|
|
7
|
+
//
|
|
8
|
+
// 参考过的做法(2026-11-19 调研):
|
|
9
|
+
// - SillyTavern:`{{weekday}}` + `{{idleDuration}}`(明确是「距最后一条**用户**消息」)
|
|
10
|
+
// - AstrBot:「现实世界时间感知」附带日期、时间、时区、星期
|
|
11
|
+
// - MaiBot:只给 `当前时间:YYYY-MM-DD HH:MM:SS`
|
|
12
|
+
// ⇒ 星期取前两家的做法,idleDuration 取 SillyTavern 的语义,MaiBot 那套我们已有。
|
|
13
|
+
//
|
|
14
|
+
// 时间戳一律放在瞬态块里,**不进 system 段** —— 秒级时间戳进稳定前缀会让每一轮
|
|
15
|
+
// 缓存全部失效(DESIGN-fish-soul.md 的注入位置约束也是同一条)。
|
|
16
|
+
|
|
17
|
+
const WEEKDAY = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
|
|
18
|
+
|
|
19
|
+
/** 同一段对话里报「刚才」是噪音,短于这个数整段不写。经验阈值,跟主动开口的护栏无关。 */
|
|
20
|
+
export const IDLE_SILENT_MS = 30 * 60_000;
|
|
21
|
+
/** 再久也只是「很久没说话」,具体几天没有信息量。 */
|
|
22
|
+
const IDLE_CAP_DAYS = 7;
|
|
23
|
+
|
|
24
|
+
/** 角色时区(UTC+8)下的星期名。只认 YYYY-MM-DD;认不出返回空串(不猜)。 */
|
|
25
|
+
export function weekdayOf(dateStr) {
|
|
26
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})/.exec(String(dateStr ?? ''));
|
|
27
|
+
if (m === null) return '';
|
|
28
|
+
const y = Number(m[1]);
|
|
29
|
+
const mo = Number(m[2]);
|
|
30
|
+
const d = Number(m[3]);
|
|
31
|
+
if (mo < 1 || mo > 12 || d < 1 || d > 31) return '';
|
|
32
|
+
return WEEKDAY[new Date(Date.UTC(y, mo - 1, d)).getUTCDay()] ?? '';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 「距上次说话多久」的人话。null = 不值得说(同一个话头里,说了是噪音)。
|
|
37
|
+
*/
|
|
38
|
+
export function idleLabel(ms) {
|
|
39
|
+
if (!Number.isFinite(ms) || ms < IDLE_SILENT_MS) return null;
|
|
40
|
+
const min = Math.floor(ms / 60_000);
|
|
41
|
+
if (min < 60) return `${min} 分钟`;
|
|
42
|
+
const hour = Math.floor(min / 60);
|
|
43
|
+
if (hour < 24) return `${hour} 小时`;
|
|
44
|
+
const day = Math.floor(hour / 24);
|
|
45
|
+
if (day >= IDLE_CAP_DAYS) return '很久';
|
|
46
|
+
return `${day} 天`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 拼「此刻状态」那一行。
|
|
51
|
+
*
|
|
52
|
+
* @param {object} p
|
|
53
|
+
* @param {string} p.virtualIsoLocal 角色时区的「YYYY-MM-DD HH:MM:SS」
|
|
54
|
+
* @param {string} [p.weekday] 星期名,空就不写
|
|
55
|
+
* @param {number|null} [p.idleMs] 距对方上次说话的真实毫秒,null/过短就不写
|
|
56
|
+
* @param {string|null} [p.tone] 当日基调
|
|
57
|
+
* @param {object|null} [p.slot] 当前时段 {range, availability, activity}
|
|
58
|
+
*/
|
|
59
|
+
export function stateLineText({ virtualIsoLocal, weekday = '', idleMs = null, tone = null, slot = null }) {
|
|
60
|
+
const parts = [`虚拟时间 ${virtualIsoLocal}${weekday ? `(${weekday})` : ''}`];
|
|
61
|
+
const idle = idleLabel(idleMs);
|
|
62
|
+
if (idle !== null) parts.push(`距上次说话 ${idle}`);
|
|
63
|
+
parts.push(`基调 ${tone ?? '未生成'}`);
|
|
64
|
+
if (slot) parts.push(`时段 ${slot.range ?? ''}(${slot.availability ?? '?'})她今天大概这么打算:${slot.activity ?? '未知'}`);
|
|
65
|
+
else parts.push('今天没排日程');
|
|
66
|
+
return `[此刻状态] ${parts.join(';')}`;
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-fish-soul",
|
|
3
|
-
"version": "0.18.
|
|
4
|
-
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu ,v0.18.
|
|
3
|
+
"version": "0.18.14",
|
|
4
|
+
"description": "鱼 · 人格、按人记忆(可纠正、有印象、能导入旧聊天记录)、日程自走与主动开口,直连 SnowLuma 收发 QQ(群里会自己插话),B站 / 抖音小号私信与自己刷视频;按功能选模型并统计用量花费,能听会说 QQ 语音、收藏表情包、学群里的梗,可选上网查;心情影响说话、去 AI 味、每个群的设定、按聊天选模型、每晚写日记、每周记忆存档与可选周记(DSH 插件,单角色 feiyu ,v0.18.14)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"files": [
|
|
@@ -64,7 +64,7 @@
|
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"scripts": {
|
|
67
|
-
"test": "node test/douyin-send.mjs && node test/douyin-mechanism.mjs && node test/douyin-route.mjs && node test/douyin-adapter.mjs && node test/douyin-contract.mjs && node test/memory-bulk-move.mjs && node test/safe-download.mjs && node test/douyin-exact-id.mjs && node test/douyin-pb-init.mjs && node test/douyin-conversation.mjs && node test/douyin-inbox.mjs && node test/douyin-quarantine.mjs && node test/douyin-send-target.mjs && node test/douyin-send-exclusive.mjs && node test/douyin-gap-dedupe.mjs && node test/douyin-message-id-dedupe.mjs && node test/douyin-share-card-dedupe.mjs && node test/douyin-share-card-dom.mjs && node test/session-overflow-recycle.mjs && node test/douyin-read-fail-retry.mjs && node test/douyin-read-cache.mjs && node test/douyin-dm-media-conv.mjs && node test/douyin-read-fail-retry-poll.mjs && node test/douyin-inbox-contract.mjs && node test/person-attribution.mjs && node test/memory-owner-reassign.mjs && node test/douyin-poll-and-timer-safety.mjs && node test/p2-limits.mjs && node test/resource-safety.mjs && node test/leftover-edge-cases.mjs && node test/douyin-master-person-merge.mjs && node test/douyin-master-uid.mjs && node test/douyin-identity-e2e.mjs && node test/douyin-post-jobs.mjs && node test/retrieval-filtered-vectors.mjs && node test/security-boundaries.mjs && node test/douyin-cdp-cursor.mjs && node test/douyin-nav-timeout.mjs && node test/persona-isolation.mjs && node test/smoke.mjs && node test/douyin-browser.mjs && node test/douyin-page-alive.mjs && node test/douyin-eval-timeout.mjs && node test/douyin-bridge-timeout.mjs && node test/douyin-goto-budget.mjs && node test/douyin-music.mjs && node test/startup-gate.mjs && node test/outbox.mjs && node test/import-coverage.mjs && node test/update-check.mjs && node test/issue-fixes.mjs && node test/import-resume.mjs && node test/maibot-extras.mjs && node test/slang-global.mjs && node test/worldinfo.mjs && node test/memory-bulk.mjs && node test/memory-tiers.mjs && node test/memory-weekly.mjs && node test/issue-98-self-archive.mjs && node test/issue-99-memory-order.mjs && node test/issue-115-memory-date.mjs && node test/issue-119-memory-page.mjs && node test/issue-125-memory-filter.mjs && node test/issue-78-diagnostic.mjs && node test/memory-storage.mjs && node test/memory-storage-restore.mjs && node test/backup-restore.mjs && node test/douyin-group.mjs && node test/review-fixes.mjs && node test/at-reads-video.mjs && node test/search-source.mjs && node test/followups.mjs && node test/detail-config.mjs && node test/qq-send-any-image.mjs && node test/qq-read-attachment.mjs && node test/qq-batch-window.mjs && node test/comment-video.mjs && node test/frames.mjs && node test/face-name.mjs && node test/comment-reply.mjs && node test/chat-comment.mjs && node test/at-target.mjs && node test/quota-caps.mjs && node test/client-lint.mjs && node test/client-panels.mjs && node test/dsh-desktop.mjs && node test/group-mention-only.mjs && node test/qq-private-allowlist.mjs && node test/issue-59-interject.mjs && node test/issue-59-diagnose.mjs && node test/diag-douyin-running.mjs && node test/sticker-backfill.mjs && node test/issue-118-sticker-diagnostics.mjs && node test/sticker-manage.mjs && node test/bili-emotes.mjs && node test/qq-outbound-sanitize.mjs && node test/person-delete.mjs && node test/qq-group-admin.mjs && node test/douyin-recovery.mjs && node test/session-preset.mjs && node test/qq-connect-error.mjs && node test/douyin-comment-bridge.mjs && node test/douyin-comment-write-unknown.mjs && node test/douyin-reply-verify-status.mjs && node test/bili-comment-emoji.mjs && node test/douyin-state-echo.mjs && node test/issue-75-proactive.mjs && node test/issue-76-capability.mjs && node test/douyin-http-browse.mjs && node test/douyin-verify-backoff.mjs && node test/douyin-digg-bridge.mjs && node test/memory-evidence-transcript.mjs && node test/chat-archive-default.mjs && node test/issue-113-desktop-archive.mjs && node test/dream-transcript-wire.mjs && node test/douyin-frames-collect.mjs && node test/master-qq-uin-rebind.mjs && node test/douyin-session-health.mjs && node test/douyin-delivered.mjs && node test/douyin-card-share.mjs && node test/douyin-push-drop.mjs && node test/douyin-stranger-dm.mjs && node test/douyin-uid-binding.mjs && node test/image-webp-fallback.mjs && node test/douyin-emoji-fallback.mjs && node test/relay-other-platform.mjs && node test/relay-to-friend.mjs && node test/relay-config-route.mjs && node test/share-fallback.mjs && node test/share-quota-refund.mjs && node test/issue-101-voice-files.mjs && node test/voice-volcengine.mjs && node test/voice-minimax-dashscope.mjs && node test/bili-switch-persist.mjs && node test/cross-channel-context.mjs && node test/reply-target.mjs && node test/shared-session.mjs && node test/memory-source-label.mjs && node test/pending-delivery.mjs && node test/recall-window-78.mjs && node test/memory-recall-recent-78.mjs && node test/client-syntax.mjs && node test/diag-log.mjs && node test/check-rings.mjs && node test/release-workflow.mjs && node test/sticker-import-gif.mjs && node test/sticker-real-gif.mjs && node test/sticker-upload-binary.mjs && node test/sticker-vision-tags.mjs && node test/sticker-manual-tags.mjs && node test/sticker-auto-name.mjs && node test/self-update.mjs && node test/group-list.mjs && node test/douyin-inbox-noise.mjs && node test/douyin-inbox-diag.mjs && node test/douyin-inbox-stuck.mjs && node test/reply-window.mjs && node test/douyin-sticker-heuristic.mjs && node test/douyin-image-boundary.mjs && node test/media-image-boundary.mjs && node test/douyin-comment-identity-cid.mjs && node test/qq-mention-only-echo.mjs && node test/bili-panel-pagination.mjs && node test/bili-allowlist.mjs && node test/douyin-master-uid-mismatch.mjs && node test/image-inbound-fail.mjs && node test/group-backlog-images.mjs && node test/check-test-chain.mjs && node test/check-route-guard.mjs && node test/check-client-imports.mjs && node test/chat-archive-fts.mjs && node test/chat-search-tool.mjs && node test/memory-invalidate-kind.mjs && node test/mood-guard.mjs && node test/tts-strip-emoji.mjs && node test/check-secrets.mjs",
|
|
67
|
+
"test": "node test/douyin-send.mjs && node test/douyin-mechanism.mjs && node test/douyin-route.mjs && node test/douyin-adapter.mjs && node test/douyin-contract.mjs && node test/memory-bulk-move.mjs && node test/safe-download.mjs && node test/douyin-exact-id.mjs && node test/douyin-pb-init.mjs && node test/douyin-conversation.mjs && node test/douyin-inbox.mjs && node test/douyin-quarantine.mjs && node test/douyin-send-target.mjs && node test/douyin-send-exclusive.mjs && node test/douyin-gap-dedupe.mjs && node test/douyin-message-id-dedupe.mjs && node test/douyin-share-card-dedupe.mjs && node test/douyin-share-card-dom.mjs && node test/douyin-inline-share.mjs && node test/douyin-short-link.mjs && node test/douyin-inline-ledger.mjs && node test/douyin-group-chat.mjs && node test/douyin-group-read.mjs && node test/douyin-group-reply.mjs && node test/session-overflow-recycle.mjs && node test/douyin-read-fail-retry.mjs && node test/douyin-read-cache.mjs && node test/douyin-dm-media-conv.mjs && node test/douyin-read-fail-retry-poll.mjs && node test/douyin-inbox-contract.mjs && node test/person-attribution.mjs && node test/memory-owner-reassign.mjs && node test/douyin-poll-and-timer-safety.mjs && node test/p2-limits.mjs && node test/resource-safety.mjs && node test/leftover-edge-cases.mjs && node test/douyin-master-person-merge.mjs && node test/douyin-master-uid.mjs && node test/douyin-identity-e2e.mjs && node test/douyin-post-jobs.mjs && node test/retrieval-filtered-vectors.mjs && node test/security-boundaries.mjs && node test/douyin-cdp-cursor.mjs && node test/douyin-nav-timeout.mjs && node test/persona-isolation.mjs && node test/smoke.mjs && node test/douyin-browser.mjs && node test/douyin-page-alive.mjs && node test/douyin-eval-timeout.mjs && node test/douyin-bridge-timeout.mjs && node test/douyin-goto-budget.mjs && node test/douyin-music.mjs && node test/startup-gate.mjs && node test/outbox.mjs && node test/import-coverage.mjs && node test/update-check.mjs && node test/issue-fixes.mjs && node test/import-resume.mjs && node test/maibot-extras.mjs && node test/slang-global.mjs && node test/worldinfo.mjs && node test/memory-bulk.mjs && node test/memory-tiers.mjs && node test/memory-weekly.mjs && node test/issue-98-self-archive.mjs && node test/issue-99-memory-order.mjs && node test/issue-115-memory-date.mjs && node test/issue-119-memory-page.mjs && node test/issue-125-memory-filter.mjs && node test/issue-78-diagnostic.mjs && node test/memory-storage.mjs && node test/memory-storage-restore.mjs && node test/backup-restore.mjs && node test/douyin-group.mjs && node test/review-fixes.mjs && node test/at-reads-video.mjs && node test/search-source.mjs && node test/followups.mjs && node test/detail-config.mjs && node test/qq-send-any-image.mjs && node test/qq-read-attachment.mjs && node test/qq-batch-window.mjs && node test/comment-video.mjs && node test/frames.mjs && node test/face-name.mjs && node test/comment-reply.mjs && node test/chat-comment.mjs && node test/at-target.mjs && node test/quota-caps.mjs && node test/client-lint.mjs && node test/client-panels.mjs && node test/dsh-desktop.mjs && node test/group-mention-only.mjs && node test/qq-private-allowlist.mjs && node test/issue-59-interject.mjs && node test/issue-59-diagnose.mjs && node test/diag-douyin-running.mjs && node test/sticker-backfill.mjs && node test/issue-118-sticker-diagnostics.mjs && node test/sticker-manage.mjs && node test/bili-emotes.mjs && node test/qq-outbound-sanitize.mjs && node test/person-delete.mjs && node test/qq-group-admin.mjs && node test/douyin-recovery.mjs && node test/session-preset.mjs && node test/qq-connect-error.mjs && node test/douyin-comment-bridge.mjs && node test/douyin-comment-write-unknown.mjs && node test/douyin-reply-verify-status.mjs && node test/bili-comment-emoji.mjs && node test/douyin-state-echo.mjs && node test/issue-75-proactive.mjs && node test/issue-76-capability.mjs && node test/douyin-http-browse.mjs && node test/douyin-verify-backoff.mjs && node test/douyin-digg-bridge.mjs && node test/memory-evidence-transcript.mjs && node test/chat-archive-default.mjs && node test/issue-113-desktop-archive.mjs && node test/dream-transcript-wire.mjs && node test/douyin-frames-collect.mjs && node test/master-qq-uin-rebind.mjs && node test/douyin-session-health.mjs && node test/douyin-delivered.mjs && node test/douyin-card-share.mjs && node test/douyin-push-drop.mjs && node test/douyin-stranger-dm.mjs && node test/douyin-uid-binding.mjs && node test/image-webp-fallback.mjs && node test/douyin-emoji-fallback.mjs && node test/relay-other-platform.mjs && node test/relay-to-friend.mjs && node test/relay-config-route.mjs && node test/share-fallback.mjs && node test/share-quota-refund.mjs && node test/issue-101-voice-files.mjs && node test/voice-volcengine.mjs && node test/voice-minimax-dashscope.mjs && node test/bili-switch-persist.mjs && node test/cross-channel-context.mjs && node test/reply-target.mjs && node test/shared-session.mjs && node test/memory-source-label.mjs && node test/pending-delivery.mjs && node test/recall-window-78.mjs && node test/memory-recall-recent-78.mjs && node test/client-syntax.mjs && node test/diag-log.mjs && node test/check-rings.mjs && node test/release-workflow.mjs && node test/sticker-import-gif.mjs && node test/sticker-real-gif.mjs && node test/sticker-upload-binary.mjs && node test/sticker-vision-tags.mjs && node test/sticker-manual-tags.mjs && node test/sticker-auto-name.mjs && node test/self-update.mjs && node test/group-list.mjs && node test/douyin-inbox-noise.mjs && node test/douyin-inbox-diag.mjs && node test/douyin-inbox-stuck.mjs && node test/reply-window.mjs && node test/douyin-sticker-heuristic.mjs && node test/douyin-image-boundary.mjs && node test/media-image-boundary.mjs && node test/douyin-comment-identity-cid.mjs && node test/qq-mention-only-echo.mjs && node test/bili-panel-pagination.mjs && node test/bili-comment-visibility.mjs && node test/bili-allowlist.mjs && node test/douyin-master-uid-mismatch.mjs && node test/image-inbound-fail.mjs && node test/group-backlog-images.mjs && node test/check-test-chain.mjs && node test/check-route-guard.mjs && node test/check-client-imports.mjs && node test/chat-archive-fts.mjs && node test/chat-search-tool.mjs && node test/memory-invalidate-kind.mjs && node test/mood-guard.mjs && node test/time-awareness.mjs && node test/tts-strip-emoji.mjs && node test/check-secrets.mjs",
|
|
68
68
|
"test:douyin": "node test/parallel.mjs 4 --only douyin",
|
|
69
69
|
"test:fast": "node test/parallel.mjs 4 --skip \"douyin|browser\""
|
|
70
70
|
}
|