@fabianactor/dsh-lil-whale-group-ui 1.12.2 → 1.13.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/bindings-core.js +293 -0
- package/lib/client.js +324 -54
- package/lib/index.js +213 -43
- package/lib/messages-core.js +30 -1
- package/lib/version-core.js +45 -0
- package/package.json +51 -51
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 绑定(pins)的规则与写入形状。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一层:这件事有三条入口——会话行的「⋯」菜单、面板的 HTTP 路由、命令行的
|
|
5
|
+
* `bind-member.mjs --unbind`。三个地方各写一遍"这段会话是不是这条鱼的",早晚会分叉;
|
|
6
|
+
* 分叉之后最糟的不是报错,是三个入口各认一个答案。
|
|
7
|
+
*
|
|
8
|
+
* 规则(fi-0017):
|
|
9
|
+
* - 只有 `kind === "fish"` 的成员能被绑;人类不行(他没有会话)。
|
|
10
|
+
* - 会话必须真的在那个成员的 `dshHome` 里:`<dshHome>\sessions\<项目>\<session-id>`。
|
|
11
|
+
* 绑一段别人的会话 = 把别人的名字顶到她头上,比绑不上难查得多。
|
|
12
|
+
* - 菜单里"绑"= 替换(pins[成员] = [这段会话]);一个成员多段会话这门语言还不支持。
|
|
13
|
+
* - 解绑 = pins[成员] = [],members.json 的 sessionId 一起清空;两处必须一起改。
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
|
16
|
+
import { join } from 'node:path'
|
|
17
|
+
|
|
18
|
+
/** 会话 id 的形状:`session-` + uuid。和 scripts/bind-member.mjs 用同一条。 */
|
|
19
|
+
export const SESSION_RE = /^session-[0-9a-f-]{20,}$/
|
|
20
|
+
|
|
21
|
+
/** 读进来的值可能带空白、可能不是字符串:合法就返回去空白后的 id,否则 null。 */
|
|
22
|
+
export function sessionIdOf(value) {
|
|
23
|
+
const text = typeof value === 'string' ? value.trim() : ''
|
|
24
|
+
return SESSION_RE.test(text) ? text : null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 这条鱼的家(dshHome 优先;老名册只有 home 时按 `<home>\dsh-home` 推)。 */
|
|
28
|
+
export function homeOfMember(member) {
|
|
29
|
+
if (!member) return null
|
|
30
|
+
if (typeof member.dshHome === 'string' && member.dshHome.trim()) return member.dshHome.trim()
|
|
31
|
+
if (typeof member.home === 'string' && member.home.trim()) return join(member.home.trim(), 'dsh-home')
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 这段会话在不在这个家里。
|
|
37
|
+
*
|
|
38
|
+
* 只认目录:`<dshHome>\sessions\**\session-<id>`。不读会话内容 —— 判"归属"不该顺带
|
|
39
|
+
* 把别人的话翻开(A.24 私聊那条同一条边界)。
|
|
40
|
+
*/
|
|
41
|
+
export function sessionExistsInHome(home, sessionId) {
|
|
42
|
+
const id = sessionIdOf(sessionId)
|
|
43
|
+
if (!home || !id) return false
|
|
44
|
+
const root = join(home, 'sessions')
|
|
45
|
+
if (!existsSync(root)) return false
|
|
46
|
+
let projects
|
|
47
|
+
try {
|
|
48
|
+
projects = readdirSync(root, { withFileTypes: true })
|
|
49
|
+
} catch {
|
|
50
|
+
return false
|
|
51
|
+
}
|
|
52
|
+
for (const project of projects) {
|
|
53
|
+
if (!project.isDirectory()) continue
|
|
54
|
+
const dir = join(root, project.name, id)
|
|
55
|
+
if (existsSync(join(dir, 'session.v4.jsonl.zstd'))) return true
|
|
56
|
+
// 会话目录存在但文件还没落(刚建的空会话):也算"在这里",否则第一次绑会莫名其妙被拒。
|
|
57
|
+
if (existsSync(dir)) return true
|
|
58
|
+
}
|
|
59
|
+
return false
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 名册里的鱼(按 kind 挑,不按名字猜)。保持名册顺序,菜单的次序就是出场的次序。 */
|
|
63
|
+
export function fishOf(roster) {
|
|
64
|
+
const members = roster?.members ?? roster ?? {}
|
|
65
|
+
return Object.entries(members)
|
|
66
|
+
.filter(([, m]) => m && m.kind === 'fish')
|
|
67
|
+
.map(([key, m]) => ({
|
|
68
|
+
key,
|
|
69
|
+
name: typeof m.name === 'string' && m.name.trim() ? m.name.trim().slice(0, 16) : key,
|
|
70
|
+
role: m.role === 'watcher' ? 'watcher' : 'member',
|
|
71
|
+
dshHome: homeOfMember(m),
|
|
72
|
+
}))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 这段会话现在绑给谁(pins 里允许一个成员多段,返回第一个命中的)。 */
|
|
76
|
+
export function findBinding(pins, sessionId) {
|
|
77
|
+
for (const [member, raw] of Object.entries(pins ?? {})) {
|
|
78
|
+
const list = Array.isArray(raw) ? raw : raw ? [raw] : []
|
|
79
|
+
if (list.includes(sessionId)) return { member, sessionId }
|
|
80
|
+
}
|
|
81
|
+
return null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 这段会话登记在哪个工作区(读 `<dshHome>\storages\workspace.json` 的 tables.workspaces)。
|
|
86
|
+
*
|
|
87
|
+
* 为什么要这一格:会话自带创建时的工作区,醒来时的 cwd 就是它。绑一段"在 default-workspace
|
|
88
|
+
* 或别人家里建的"会话,她醒来会站在错的地方——文件工具与沙箱按错的根走,写不进或写错处。
|
|
89
|
+
* 返回 null = 没有登记(未分组),由调用方决定放不放行。
|
|
90
|
+
*/
|
|
91
|
+
export function workspacePathOf(home, sessionId) {
|
|
92
|
+
const id = sessionIdOf(sessionId)
|
|
93
|
+
if (!home || !id) return null
|
|
94
|
+
const file = join(home, 'storages', 'workspace.json')
|
|
95
|
+
if (!existsSync(file)) return null
|
|
96
|
+
let data
|
|
97
|
+
try {
|
|
98
|
+
data = JSON.parse(readFileSync(file, 'utf8'))
|
|
99
|
+
} catch {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
102
|
+
const tables = data?.tables?.workspaces
|
|
103
|
+
if (!tables || typeof tables !== 'object') return null
|
|
104
|
+
for (const ws of Object.values(tables)) {
|
|
105
|
+
const ids = Array.isArray(ws?.sessionIds) ? ws.sessionIds : []
|
|
106
|
+
if (ids.includes(id)) return typeof ws.path === 'string' ? ws.path : null
|
|
107
|
+
}
|
|
108
|
+
return null
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 路径比较用:斜杠归一、去尾斜杠、大小写不敏感(Windows 上这两条路是同一个地方)。 */
|
|
112
|
+
export function normalizePath(value) {
|
|
113
|
+
return String(value ?? '')
|
|
114
|
+
.trim()
|
|
115
|
+
.replace(/[\\/]+/g, '\\')
|
|
116
|
+
.replace(/\\$/, '')
|
|
117
|
+
.toLowerCase()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function underRoot(value, root) {
|
|
121
|
+
const a = normalizePath(value)
|
|
122
|
+
const b = normalizePath(root)
|
|
123
|
+
if (!a || !b) return false
|
|
124
|
+
return a === b || a.startsWith(b + '\\')
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 能不能绑。返回 null 表示可以;否则给出给人看的理由(HTTP 状态 + 机器码 + 人话)。
|
|
129
|
+
* 人话里带上"这段会话不在谁家里",别只说 400。
|
|
130
|
+
*
|
|
131
|
+
* 工作区那一格(Fi-0017 的补充):
|
|
132
|
+
* - 普通鱼:会话登记的工作区必须就是她的家(`member.home`);不在自己家的拒绝。
|
|
133
|
+
* - 巡守(宿主自己):他的工作区是鱼塘根(`D:\…\lil-whale`),不是 `~\.dsh`,所以只要求
|
|
134
|
+
* 在鱼塘里面(`pondRoot`);会话文件仍在 `dshHome\sessions` 下这条不变。
|
|
135
|
+
* - 没登记工作区的会话:放行(r261 那种"未分组"),否则第一段会话反而绑不上。
|
|
136
|
+
*/
|
|
137
|
+
export function bindingProblem({ roster, memberKey, sessionId, pondRoot }) {
|
|
138
|
+
const id = sessionIdOf(sessionId)
|
|
139
|
+
if (!id) return { status: 400, error: 'bad-session-id', message: '会话 id 形如 session-xxxxxxxx-…' }
|
|
140
|
+
const member = (roster?.members ?? roster ?? {})[memberKey]
|
|
141
|
+
if (!member) return { status: 400, error: 'no-member', message: `名册里没有成员:${memberKey}` }
|
|
142
|
+
if (member.kind !== 'fish') return { status: 400, error: 'not-fish', message: `${memberKey} 不是鱼(kind=${member.kind}),不能绑会话` }
|
|
143
|
+
const home = homeOfMember(member)
|
|
144
|
+
const name = typeof member.name === 'string' && member.name.trim() ? member.name.trim() : memberKey
|
|
145
|
+
if (!home) return { status: 400, error: 'no-home', message: `名册里没有 ${memberKey} 的 dshHome` }
|
|
146
|
+
if (!sessionExistsInHome(home, id)) {
|
|
147
|
+
return { status: 400, error: 'session-not-in-home', message: `这段会话不在 ${name} 的家里(${home}),不绑` }
|
|
148
|
+
}
|
|
149
|
+
const wsPath = workspacePathOf(home, id)
|
|
150
|
+
if (wsPath) {
|
|
151
|
+
if (member.role === 'watcher') {
|
|
152
|
+
// 巡守的会话常建在鱼塘根的父目录里(例:default-workspace\lil-whale 的会话建在 default-workspace)。
|
|
153
|
+
// 2026-11-23 r439:允许"鱼塘根的祖先",但至少要有两级目录,免得把 C:\ 这种当成合法工作区。
|
|
154
|
+
const ancestorOk = underRoot(pondRoot, wsPath) && normalizePath(wsPath).split('\\').filter(Boolean).length >= 2
|
|
155
|
+
if (pondRoot && !underRoot(wsPath, pondRoot) && !ancestorOk) {
|
|
156
|
+
return { status: 409, error: 'session-workspace-outside-pond', message: `这段会话的工作区在鱼塘外面(${wsPath}),不绑给 ${name}` }
|
|
157
|
+
}
|
|
158
|
+
} else {
|
|
159
|
+
const own = typeof member.home === 'string' && member.home.trim() ? member.home.trim() : home
|
|
160
|
+
if (normalizePath(wsPath) !== normalizePath(own)) {
|
|
161
|
+
return { status: 409, error: 'session-workspace-mismatch', message: `这段会话登记在别的工作区(${wsPath}),醒来 cwd 不是 ${name} 的家(${own});换一段在她家里建的会话` }
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 能不能解绑:只查"现在是不是真绑着",不查会话还在不在(会话删了也要能解)。 */
|
|
169
|
+
export function unbindProblem({ pins, memberKey, sessionId }) {
|
|
170
|
+
const id = sessionIdOf(sessionId)
|
|
171
|
+
if (!id) return { status: 400, error: 'bad-session-id', message: '会话 id 形如 session-xxxxxxxx-…' }
|
|
172
|
+
const list = Array.isArray(pins?.[memberKey]) ? pins[memberKey] : pins?.[memberKey] ? [pins[memberKey]] : []
|
|
173
|
+
if (!list.includes(id)) {
|
|
174
|
+
return { status: 409, error: 'not-bound', message: `${memberKey} 现在没有绑这段会话(${id})` }
|
|
175
|
+
}
|
|
176
|
+
return null
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 绑:pins[成员] = [这段会话](替换)。同一段会话同时只归一个人 —— 别人手里的同一段要摘掉。 */
|
|
180
|
+
export function applyBind(pins, memberKey, sessionId) {
|
|
181
|
+
const id = sessionIdOf(sessionId)
|
|
182
|
+
const next = {}
|
|
183
|
+
for (const [member, raw] of Object.entries(pins ?? {})) {
|
|
184
|
+
const list = Array.isArray(raw) ? raw : raw ? [raw] : []
|
|
185
|
+
next[member] = member === memberKey ? [id] : list.filter((x) => x !== id)
|
|
186
|
+
}
|
|
187
|
+
if (!Object.prototype.hasOwnProperty.call(next, memberKey)) next[memberKey] = [id]
|
|
188
|
+
return next
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 解绑:pins[成员] = []。返回新对象。 */
|
|
192
|
+
export function applyUnbind(pins, memberKey) {
|
|
193
|
+
const next = { ...(pins ?? {}) }
|
|
194
|
+
next[memberKey] = []
|
|
195
|
+
return next
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 面板要的一份视图:鱼列表 + 当前 pins + 每段被绑会话到底还在不在盘上。
|
|
200
|
+
* `stale` 的会话照样列出来并标出来("已绑给 X(会话已不存在)"),不假装没这回事。
|
|
201
|
+
*/
|
|
202
|
+
export function bindingsView({ roster, pins, liveKeys = [], exists = () => true }) {
|
|
203
|
+
const fish = fishOf(roster).map((m) => ({ ...m, live: liveKeys.includes(m.key) }))
|
|
204
|
+
const bound = []
|
|
205
|
+
for (const [member, raw] of Object.entries(pins ?? {})) {
|
|
206
|
+
const list = Array.isArray(raw) ? raw : raw ? [raw] : []
|
|
207
|
+
for (const id of list) {
|
|
208
|
+
if (typeof id !== 'string') continue
|
|
209
|
+
bound.push({ member, sessionId: id, exists: Boolean(exists(id)) })
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return { members: fish, pins: { ...(pins ?? {}) }, bound }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** 面板上写短一点:`session-ab12cd34`。完整 id 太长,卡片一行放不下。 */
|
|
216
|
+
export function shortSessionId(id) {
|
|
217
|
+
const text = sessionIdOf(id)
|
|
218
|
+
if (!text) return ''
|
|
219
|
+
return text.length <= 17 ? text : `${text.slice(0, 17)}…`
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 这条鱼家里有哪些会话(只认目录名,不读内容)。
|
|
224
|
+
* 返回 [{ id, project, mtimeMs }],按 mtime 倒序;同一 id 出现在多个项目目录时只留一条。
|
|
225
|
+
*/
|
|
226
|
+
export function sessionsInHome(home) {
|
|
227
|
+
if (!home || !existsSync(home)) return []
|
|
228
|
+
const root = join(home, 'sessions')
|
|
229
|
+
if (!existsSync(root)) return []
|
|
230
|
+
const seen = new Map()
|
|
231
|
+
let projects = []
|
|
232
|
+
try {
|
|
233
|
+
projects = readdirSync(root, { withFileTypes: true })
|
|
234
|
+
} catch {
|
|
235
|
+
return []
|
|
236
|
+
}
|
|
237
|
+
for (const project of projects) {
|
|
238
|
+
if (!project.isDirectory()) continue
|
|
239
|
+
let entries = []
|
|
240
|
+
try {
|
|
241
|
+
entries = readdirSync(join(root, project.name), { withFileTypes: true })
|
|
242
|
+
} catch {
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
for (const entry of entries) {
|
|
246
|
+
if (!entry.isDirectory()) continue
|
|
247
|
+
const id = sessionIdOf(entry.name)
|
|
248
|
+
if (!id || seen.has(id)) continue
|
|
249
|
+
let mtimeMs = 0
|
|
250
|
+
try {
|
|
251
|
+
mtimeMs = statSync(join(root, project.name, entry.name)).mtimeMs
|
|
252
|
+
} catch {
|
|
253
|
+
mtimeMs = 0
|
|
254
|
+
}
|
|
255
|
+
seen.set(id, { id, project: project.name, mtimeMs })
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return [...seen.values()].sort((a, b) => b.mtimeMs - a.mtimeMs)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 换绑弹窗要的一份候选表:这条鱼家里的会话 + 每一条"能不能绑、现在归谁、是不是正被挂着"。
|
|
263
|
+
* 判定全落在 bindingProblem 上,别在界面里另写一套。
|
|
264
|
+
*/
|
|
265
|
+
export function sessionCandidatesOf({ roster, memberKey, pondRoot, pins = {}, liveSessionIds = [] }) {
|
|
266
|
+
const member = (roster?.members ?? roster ?? {})[memberKey]
|
|
267
|
+
const home = homeOfMember(member)
|
|
268
|
+
if (!member || !home) return { member: memberKey, home, sessions: [] }
|
|
269
|
+
const live = new Set(liveSessionIds)
|
|
270
|
+
const sessions = sessionsInHome(home).map((entry) => {
|
|
271
|
+
const problem = bindingProblem({ roster, memberKey, sessionId: entry.id, pondRoot })
|
|
272
|
+
const boundTo = findBinding(pins, entry.id)?.member ?? null
|
|
273
|
+
return {
|
|
274
|
+
id: entry.id,
|
|
275
|
+
short: shortSessionId(entry.id),
|
|
276
|
+
project: entry.project,
|
|
277
|
+
mtimeMs: entry.mtimeMs,
|
|
278
|
+
workspace: workspacePathOf(home, entry.id),
|
|
279
|
+
registered: workspacePathOf(home, entry.id) !== null,
|
|
280
|
+
eligible: problem === null,
|
|
281
|
+
reason: problem ? problem.message : null,
|
|
282
|
+
inUse: live.has(entry.id),
|
|
283
|
+
boundTo,
|
|
284
|
+
}
|
|
285
|
+
})
|
|
286
|
+
return { member: memberKey, home, sessions }
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** 账单里的一行。和 scripts/bind-member.mjs 的 bind/unbind 行同形,`by=` 说清是谁干的。 */
|
|
290
|
+
export function stateLogLine({ ts, action, memberKey, sessionId, by = 'panel' }) {
|
|
291
|
+
const id = sessionIdOf(sessionId) ?? String(sessionId ?? '')
|
|
292
|
+
return `${ts} | ${action} | fmt=1 member=${memberKey} session=${id} by=${by}`
|
|
293
|
+
}
|