@fabianactor/dsh-lil-whale-group-ui 1.10.0 → 1.12.0
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/lib/client.js +88 -6
- package/lib/index.js +14 -40
- package/lib/messages-core.js +43 -0
- package/lib/presence-core.js +33 -0
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -675,11 +675,42 @@ function insertNewline(el, value, setValue) {
|
|
|
675
675
|
growTextarea(el)
|
|
676
676
|
})
|
|
677
677
|
}
|
|
678
|
-
|
|
678
|
+
// 点消息头上的行号复制引用:`群 #1763` / `私聊 fabian__lee2 #3`。
|
|
679
|
+
// 优先用 Clipboard API;不安全的上下文(非 https/非 localhost)会没有这个对象,
|
|
680
|
+
// 落到老办法(临时 textarea + execCommand),失败就返回 false,让调用方不显示「已复制」。
|
|
681
|
+
async function copyToClipboard(text) {
|
|
682
|
+
try {
|
|
683
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
684
|
+
await navigator.clipboard.writeText(text)
|
|
685
|
+
return true
|
|
686
|
+
}
|
|
687
|
+
} catch {
|
|
688
|
+
// 继续走兜底
|
|
689
|
+
}
|
|
690
|
+
try {
|
|
691
|
+
const area = document.createElement('textarea')
|
|
692
|
+
area.value = text
|
|
693
|
+
area.setAttribute('readonly', '')
|
|
694
|
+
area.style.position = 'fixed'
|
|
695
|
+
area.style.top = '-1000px'
|
|
696
|
+
area.style.opacity = '0'
|
|
697
|
+
document.body.appendChild(area)
|
|
698
|
+
area.select()
|
|
699
|
+
const ok = document.execCommand('copy')
|
|
700
|
+
area.remove()
|
|
701
|
+
return ok
|
|
702
|
+
} catch {
|
|
703
|
+
return false
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const Bubble = React.memo(function Bubble({ config, message, onMention, animate = false, citePrefix = '' }) {
|
|
679
708
|
// 只在挂载时决定「要不要动画」:否则后续每次重渲染都会让老消息重新弹一遍。
|
|
680
709
|
const animateOnMount = useRef(animate).current
|
|
681
710
|
// 点图片打开官方看图器(大图、点外面关)。
|
|
682
711
|
const [previewIndex, setPreviewIndex] = useState(-1)
|
|
712
|
+
// 行号点一下复制引用;1.2 秒内显示「已复制」当回执。
|
|
713
|
+
const [lineCopied, setLineCopied] = useState(false)
|
|
683
714
|
const images = Array.isArray(message.images) ? message.images : (message.image ? [message.image] : [])
|
|
684
715
|
const imageSrcs = images.map((image) => imageUrlOf(image))
|
|
685
716
|
const humanId = config?.humanId ?? 'fabian'
|
|
@@ -689,6 +720,18 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
689
720
|
const targetTo = message.toSeat ? `${message.to}@${message.toSeat}` : message.to
|
|
690
721
|
const target = message.to && message.to !== 'all' ? ` → ${targetName(config, targetTo)}` : ''
|
|
691
722
|
const time = formatStamp(message.ts)
|
|
723
|
+
// 行号:引用地址。群是 messages.jsonl 的行,私聊是 pool\dm\<pair>.jsonl 的行。
|
|
724
|
+
// 文件是 append-only,行号不会变;点一下复制成别人能直接引用的形状。
|
|
725
|
+
const lineNo = typeof message.line === 'number' && message.line > 0 ? message.line : 0
|
|
726
|
+
const cite = lineNo > 0 ? `${citePrefix} #${lineNo}`.trim() : ''
|
|
727
|
+
const copyCite = () => {
|
|
728
|
+
if (!cite) return
|
|
729
|
+
copyToClipboard(cite).then((ok) => {
|
|
730
|
+
if (!ok) return
|
|
731
|
+
setLineCopied(true)
|
|
732
|
+
window.setTimeout(() => setLineCopied(false), 1200)
|
|
733
|
+
})
|
|
734
|
+
}
|
|
692
735
|
return h(React.Fragment, null,
|
|
693
736
|
h('div', {
|
|
694
737
|
style: {
|
|
@@ -719,7 +762,15 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
719
762
|
display: 'flex', alignItems: 'baseline', gap: 6,
|
|
720
763
|
fontSize: 12, color: 'var(--dsw-alias-label-tertiary)',
|
|
721
764
|
},
|
|
722
|
-
},
|
|
765
|
+
},
|
|
766
|
+
h('span', null, who + target),
|
|
767
|
+
h('span', null, time),
|
|
768
|
+
lineNo > 0 ? h('span', {
|
|
769
|
+
key: 'line',
|
|
770
|
+
title: lineCopied ? '已复制' : `点击复制引用:${cite}`,
|
|
771
|
+
onClick: copyCite,
|
|
772
|
+
style: { cursor: 'pointer', fontSize: 11, color: 'var(--dsw-alias-label-caption)', userSelect: 'none' },
|
|
773
|
+
}, lineCopied ? '已复制' : `#${lineNo}`) : null),
|
|
723
774
|
h('div', {
|
|
724
775
|
style: {
|
|
725
776
|
padding: '8px 12px', borderRadius: 'var(--dsw-radius-lg, 12px)',
|
|
@@ -1554,7 +1605,12 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
1554
1605
|
// 同群聊:打字不重排私聊历史。
|
|
1555
1606
|
const dmListNode = useMemo(
|
|
1556
1607
|
() => withDivider(messages, String(seenDm[activePeer] ?? ''), (message, index) => h(Bubble, {
|
|
1557
|
-
key: `${message.ts}-${index}`,
|
|
1608
|
+
key: `${message.ts}-${index}`,
|
|
1609
|
+
config,
|
|
1610
|
+
message,
|
|
1611
|
+
animate: animateRef.current,
|
|
1612
|
+
// 私聊引用要写清是哪一本:pool\dm\<a>__<b>.jsonl,键按字典序排(与文件命名同规则)。
|
|
1613
|
+
citePrefix: `私聊 ${[(config?.humanId ?? 'fabian'), activePeer].sort().join('__')}`,
|
|
1558
1614
|
})),
|
|
1559
1615
|
[messages, seenDm, activePeer, config],
|
|
1560
1616
|
)
|
|
@@ -1807,7 +1863,19 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
1807
1863
|
if (composingRef.current) return
|
|
1808
1864
|
draftValueRef.current = value
|
|
1809
1865
|
if (draftTimerRef.current) clearTimeout(draftTimerRef.current)
|
|
1810
|
-
draftTimerRef.current = setTimeout(() =>
|
|
1866
|
+
draftTimerRef.current = setTimeout(() => {
|
|
1867
|
+
draftTimerRef.current = null
|
|
1868
|
+
writeDraft(GROUP_DRAFT_KEY, value)
|
|
1869
|
+
}, 300)
|
|
1870
|
+
}, [])
|
|
1871
|
+
// 切 tab 不会卸载这个页面组件,只卸载输入框 —— 页面级的卸载清理够不着,
|
|
1872
|
+
// 所以切走时自己把还没落盘的那 300ms 写掉(2026-11-19 草稿复活 bug 的其中一半)。
|
|
1873
|
+
const flushGroupDraft = useCallback(() => {
|
|
1874
|
+
if (draftTimerRef.current) {
|
|
1875
|
+
clearTimeout(draftTimerRef.current)
|
|
1876
|
+
draftTimerRef.current = null
|
|
1877
|
+
}
|
|
1878
|
+
writeDraft(GROUP_DRAFT_KEY, draftValueRef.current)
|
|
1811
1879
|
}, [])
|
|
1812
1880
|
// 卸载(切 tab / 关面板)时把还没落盘的那 300ms 存掉,别让人刚打的字随组件一起消失。
|
|
1813
1881
|
useEffect(() => () => {
|
|
@@ -1844,6 +1912,7 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
1844
1912
|
const switchTab = useCallback((key) => {
|
|
1845
1913
|
if (key === tab) return
|
|
1846
1914
|
if (key !== 'group') {
|
|
1915
|
+
flushGroupDraft()
|
|
1847
1916
|
setTab(key)
|
|
1848
1917
|
return
|
|
1849
1918
|
}
|
|
@@ -1856,8 +1925,20 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
1856
1925
|
setTab('group')
|
|
1857
1926
|
tabSplashTimer.current = setTimeout(() => setTabSplash(false), 420)
|
|
1858
1927
|
})
|
|
1859
|
-
}, [tab])
|
|
1928
|
+
}, [tab, flushGroupDraft])
|
|
1860
1929
|
useEffect(() => () => { if (tabSplashTimer.current) clearTimeout(tabSplashTimer.current) }, [])
|
|
1930
|
+
// 从私聊/鱼塘切回来时,输入框是新建的:defaultValue 用的是页面挂载那一刻的草稿,
|
|
1931
|
+
// 之后再改、再删都不在它里面。按"现在存的那份"重放一次,删掉的草稿就不会回来
|
|
1932
|
+
// (2026-11-19 他报的 bug:删掉输入框、切去私聊、再回群聊,旧字又出现)。
|
|
1933
|
+
useEffect(() => {
|
|
1934
|
+
if (tab !== 'group') return
|
|
1935
|
+
const el = inputRef.current
|
|
1936
|
+
if (!el) return
|
|
1937
|
+
const stored = readDraft(GROUP_DRAFT_KEY)
|
|
1938
|
+
el.value = stored
|
|
1939
|
+
setHasText(stored.trim() !== '')
|
|
1940
|
+
}, [tab])
|
|
1941
|
+
|
|
1861
1942
|
const [unread, setUnread] = useState({ group: 0, dm: {}, total: 0 })
|
|
1862
1943
|
// 群聊"上次看到哪儿":进这一页时冻结一次,线不跟着新消息往下跑。
|
|
1863
1944
|
const seenGroupRef = useRef(null)
|
|
@@ -2252,6 +2333,7 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
2252
2333
|
message,
|
|
2253
2334
|
animate: animateRef.current,
|
|
2254
2335
|
onMention: mentionTo,
|
|
2336
|
+
citePrefix: '群',
|
|
2255
2337
|
}), dividerRef)
|
|
2256
2338
|
if (visibleStart === 0) return nodes
|
|
2257
2339
|
const earlier = h(
|
|
@@ -3718,7 +3800,7 @@ const Bubble = React.memo(function Bubble({ config, message, onMention, animate
|
|
|
3718
3800
|
inject: ['slots'],
|
|
3719
3801
|
apply(ctx) {
|
|
3720
3802
|
// 一行"我跑的是哪一版"的自报,方便判断浏览器是不是还在用旧 bundle
|
|
3721
|
-
console.info('[dsh-lil-whale-group-ui] client build
|
|
3803
|
+
console.info('[dsh-lil-whale-group-ui] client build v53 · presence-draft:', Object.keys(MARKDOWN_LABELS).join(','))
|
|
3722
3804
|
// 「正在回复…」那三个点的呼吸动画 + 过场转圈。只在浏览器里注入一次。
|
|
3723
3805
|
if (typeof document !== 'undefined' && !document.getElementById('dsh-lil-whale-group-keyframes')) {
|
|
3724
3806
|
const style = document.createElement('style')
|
package/lib/index.js
CHANGED
|
@@ -13,6 +13,8 @@ import { Socket } from 'node:net'
|
|
|
13
13
|
import { fileURLToPath } from 'node:url'
|
|
14
14
|
import { createRequire } from 'node:module'
|
|
15
15
|
import { config } from './config.js'
|
|
16
|
+
import { parseMessageLines } from './messages-core.js'
|
|
17
|
+
import { presenceReasonOf } from './presence-core.js'
|
|
16
18
|
|
|
17
19
|
export const name = '@fabianactor/dsh-lil-whale-group-ui'
|
|
18
20
|
export const inject = ['webServer', 'agents']
|
|
@@ -44,8 +46,8 @@ const PINS = join(GROUP_DIR, 'pins.json')
|
|
|
44
46
|
const POOL_STATE = config().poolStateDir || join(GROUP_DIR, '..', 'state')
|
|
45
47
|
const PRESENCE_DIR = join(POOL_STATE, 'presence')
|
|
46
48
|
const PRESENCE_FRESH_MS = 20_000
|
|
47
|
-
//
|
|
48
|
-
|
|
49
|
+
// 「这一轮从哪条通道被叫起来」的保鲜期(读取规则在 presence-core.js)。
|
|
50
|
+
// 要比一次长回合宽:首 token 几十秒到几分钟。
|
|
49
51
|
|
|
50
52
|
// 头像照片:一个人一张,文件名固定 `<成员>.<扩展名>`,换一张就覆盖旧的。
|
|
51
53
|
// 上限 1.5 MB —— 够一张头像了;再大就是有人把整张截图塞进来,不该让群目录跟着长胖。
|
|
@@ -474,31 +476,9 @@ function lastTsOf(file) {
|
|
|
474
476
|
|
|
475
477
|
function readMessages(file = MESSAGES) {
|
|
476
478
|
if (!existsSync(file)) return []
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
if (!text) continue
|
|
481
|
-
try {
|
|
482
|
-
const record = JSON.parse(text)
|
|
483
|
-
// 投影要带全:面板靠这些字段画图、画「→ 某人的哪一段」、判断私聊/群。早先只留了
|
|
484
|
-
// ts/from/to/text/kind,结果她发的图在面板里根本不显示 —— 记录里有、列表里没有。
|
|
485
|
-
out.push({
|
|
486
|
-
ts: record.ts,
|
|
487
|
-
from: record.from,
|
|
488
|
-
to: record.to,
|
|
489
|
-
text: record.text,
|
|
490
|
-
kind: record.kind ?? 'say',
|
|
491
|
-
...(record.s ? { s: record.s } : {}),
|
|
492
|
-
...(record.channel ? { channel: record.channel } : {}),
|
|
493
|
-
...(record.toSeat ? { toSeat: record.toSeat } : {}),
|
|
494
|
-
...(record.image ? { image: record.image } : {}),
|
|
495
|
-
...(Array.isArray(record.images) && record.images.length > 0 ? { images: record.images } : {}),
|
|
496
|
-
})
|
|
497
|
-
} catch {
|
|
498
|
-
// 半截行(另一个进程正在写)——跳过,下一次轮询就整了
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
return out
|
|
479
|
+
// 投影与行号在 lib/messages-core.js:行号是物理行号(空行/半截行也算),
|
|
480
|
+
// 它是面板上 #1763 这个引用地址的来源(r328)。
|
|
481
|
+
return parseMessageLines(readFileSync(file, 'utf8'))
|
|
502
482
|
}
|
|
503
483
|
|
|
504
484
|
function readBody(req, limit = 256 * 1024) {
|
|
@@ -1112,20 +1092,12 @@ export function apply(ctx) {
|
|
|
1112
1092
|
const beat = JSON.parse(readFileSync(join(PRESENCE_DIR, `${member}.json`), 'utf8'))
|
|
1113
1093
|
const at = Number(beat?.at) || 0
|
|
1114
1094
|
if (Date.now() - at <= PRESENCE_FRESH_MS) {
|
|
1115
|
-
// reason
|
|
1116
|
-
//
|
|
1117
|
-
//
|
|
1118
|
-
// 2026-11-13:分三档。原来只有 groupWakeAt,私聊唤醒没留痕,
|
|
1119
|
-
// 于是"在回私聊"被一律显示成「正在忙」(他报的 bug)。
|
|
1120
|
-
const wakeAt = Number(beat?.wakeAt) || 0
|
|
1121
|
-
const kind = String(beat?.wakeKind ?? '')
|
|
1122
|
-
const fresh = wakeAt > 0 && Date.now() - wakeAt <= GROUP_WAKE_TTL_MS
|
|
1123
|
-
// 兼容旧心跳:只有 groupWakeAt 的那些进程。
|
|
1124
|
-
const legacyAt = Number(beat?.groupWakeAt) || 0
|
|
1125
|
-
const legacyFresh = legacyAt > 0 && Date.now() - legacyAt <= GROUP_WAKE_TTL_MS
|
|
1095
|
+
// reason 回答"这一轮从哪来",不是"这句话回谁"——规则在 presence-core.js。
|
|
1096
|
+
// 2026-11-19:心跳曾经把 wakeKind/wakeAt 洗掉,于是只剩「十分钟内从群叫醒」
|
|
1097
|
+
// 这一条线索,两种忙态总显示在错的一边;写入端已改成整条合并,这里只负责读。
|
|
1126
1098
|
members[member] = {
|
|
1127
1099
|
status: beat?.status === 'idle' ? 'idle' : 'working',
|
|
1128
|
-
reason:
|
|
1100
|
+
reason: presenceReasonOf(beat, Date.now()),
|
|
1129
1101
|
}
|
|
1130
1102
|
}
|
|
1131
1103
|
} catch {
|
|
@@ -1141,7 +1113,9 @@ export function apply(ctx) {
|
|
|
1141
1113
|
const busy = agent?.status !== 'idle'
|
|
1142
1114
|
if (busy) {
|
|
1143
1115
|
const prev = members[member]
|
|
1144
|
-
|
|
1116
|
+
// 本进程只知道"忙不忙",不知道"为什么忙"——只改 status,别动 reason
|
|
1117
|
+
// (尤其别把 dm 压成 self:那会让私聊页把"正在回你"读成"正在忙")。
|
|
1118
|
+
members[member] = { ...(prev ?? {}), status: 'working', ...(prev?.reason ? {} : { reason: 'self' }) }
|
|
1145
1119
|
}
|
|
1146
1120
|
}
|
|
1147
1121
|
} catch {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// 消息文件的投影:把 JSONL 变成面板能画的对象,同时带上**物理行号**。
|
|
2
|
+
//
|
|
3
|
+
// 为什么行号要在这里定:面板上的 `#1763` 是引用地址,指的是 messages.jsonl /
|
|
4
|
+
// pool\dm\<a>__<b>.jsonl 的第 1763 行(两个文件都是 append-only,行号不会变)。
|
|
5
|
+
// 行号必须按"读到的第几行"算,包含空行与半截行 —— 否则引用会随文件里的空行错位。
|
|
6
|
+
//
|
|
7
|
+
// 抽出成纯函数(2026-11-18,r328)是为了能直接测:以前这段埋在 web 路由的文件里,
|
|
8
|
+
// 只靠静态断言证明"代码里写了这件事",测不到"空行/坏行时行号对不对"。
|
|
9
|
+
|
|
10
|
+
export function projectMessage(record) {
|
|
11
|
+
// 投影要带全:面板靠这些字段画图、画「→ 某人的哪一段」、判断私聊/群。早先只留了
|
|
12
|
+
// ts/from/to/text/kind,结果她发的图在面板里根本不显示 —— 记录里有、列表里没有。
|
|
13
|
+
const out = {
|
|
14
|
+
ts: record.ts,
|
|
15
|
+
from: record.from,
|
|
16
|
+
to: record.to,
|
|
17
|
+
text: record.text,
|
|
18
|
+
kind: record.kind ?? 'say',
|
|
19
|
+
}
|
|
20
|
+
if (record.s) out.s = record.s
|
|
21
|
+
if (record.channel) out.channel = record.channel
|
|
22
|
+
if (record.toSeat) out.toSeat = record.toSeat
|
|
23
|
+
if (record.image) out.image = record.image
|
|
24
|
+
if (Array.isArray(record.images) && record.images.length > 0) out.images = record.images
|
|
25
|
+
return out
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function parseMessageLines(text) {
|
|
29
|
+
const out = []
|
|
30
|
+
let lineNo = 0
|
|
31
|
+
for (const line of String(text).split(/\r?\n/)) {
|
|
32
|
+
lineNo += 1
|
|
33
|
+
const raw = line.trim()
|
|
34
|
+
if (!raw) continue
|
|
35
|
+
try {
|
|
36
|
+
const record = JSON.parse(raw)
|
|
37
|
+
out.push({ line: lineNo, ...projectMessage(record) })
|
|
38
|
+
} catch {
|
|
39
|
+
// 半截行(另一个进程正在写)——跳过,下一次轮询就整了;行号照旧往前数。
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out
|
|
43
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 面板怎么读一条 presence 心跳(2026-11-19 抽出)。
|
|
3
|
+
*
|
|
4
|
+
* `reason` 回答的是「这一轮从哪条通道被叫起来」,不是「这句话回谁」——
|
|
5
|
+
* 宿主那边没有更细的信息,宁可含糊也不要编。
|
|
6
|
+
*
|
|
7
|
+
* 2026-11-19 他报的 bug:LeeaCP 在回私聊时,群内显示「正在回复」、私聊显示「正在忙」。
|
|
8
|
+
* 根因不在读取端,在写入端:心跳把 wakeKind/wakeAt 抹掉了,只剩旧 groupWakeAt;
|
|
9
|
+
* 于是十分钟内一律读成 'group',群聊页按「在回这一段」显示、私聊页按「在别处忙」显示。
|
|
10
|
+
* 读取端这层规则本身是对的,抽出来是为了让"三种输入 → 三种结果"可以被断言。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const WAKE_REASON_TTL_MS = 10 * 60_000
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {{ wakeKind?: string, wakeAt?: number, groupWakeAt?: number } | null | undefined} beat
|
|
17
|
+
* @param {number} now
|
|
18
|
+
* @param {number} ttlMs
|
|
19
|
+
* @returns {'group' | 'dm' | 'self'}
|
|
20
|
+
*/
|
|
21
|
+
export function presenceReasonOf(beat, now, ttlMs = WAKE_REASON_TTL_MS) {
|
|
22
|
+
const at = Number(now) || 0
|
|
23
|
+
const ttl = Number.isFinite(ttlMs) && ttlMs > 0 ? ttlMs : WAKE_REASON_TTL_MS
|
|
24
|
+
const fresh = (value) => Number(value) > 0 && at - Number(value) <= ttl
|
|
25
|
+
|
|
26
|
+
const wakeAt = beat?.wakeAt
|
|
27
|
+
if (fresh(wakeAt)) return String(beat?.wakeKind ?? '') === 'dm' ? 'dm' : 'group'
|
|
28
|
+
|
|
29
|
+
// 兼容旧心跳:只有 groupWakeAt 的那些进程。它只可能是群,不可能是私聊。
|
|
30
|
+
if (fresh(beat?.groupWakeAt)) return 'group'
|
|
31
|
+
|
|
32
|
+
return 'self'
|
|
33
|
+
}
|