@cheemsup/dsh-plugin-console 0.0.0-stage → 0.2.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/LICENSE +23 -0
- package/README.md +95 -3
- package/cordis.patch.yml +5 -0
- package/icon.svg +203 -0
- package/lib/client.activity.js +222 -0
- package/lib/client.apply.js +171 -0
- package/lib/client.changes.js +206 -0
- package/lib/client.cleanup.js +270 -0
- package/lib/client.control.js +247 -0
- package/lib/client.dialog.js +77 -0
- package/lib/client.disable-preview.js +904 -0
- package/lib/client.draft.js +280 -0
- package/lib/client.editor-messages.js +421 -0
- package/lib/client.editor-store.js +266 -0
- package/lib/client.editor.js +433 -0
- package/lib/client.effects.js +115 -0
- package/lib/client.impact-tree.js +487 -0
- package/lib/client.influence.js +361 -0
- package/lib/client.js +246 -0
- package/lib/client.page.js +646 -0
- package/lib/client.plugin-filters.js +62 -0
- package/lib/client.plugins.js +506 -0
- package/lib/client.preview-messages.js +506 -0
- package/lib/client.preview-raw.js +132 -0
- package/lib/client.preview-store.js +128 -0
- package/lib/client.preview-tools.js +194 -0
- package/lib/client.preview.js +448 -0
- package/lib/client.relation-graph.js +364 -0
- package/lib/client.schema.js +327 -0
- package/lib/client.search.js +409 -0
- package/lib/client.session-detail.js +503 -0
- package/lib/client.sessions.js +495 -0
- package/lib/client.shared.js +562 -0
- package/lib/client.store.js +452 -0
- package/lib/client.styles.js +589 -0
- package/lib/client.template-card.js +240 -0
- package/lib/client.tools.js +366 -0
- package/lib/client.workspace-picker.js +204 -0
- package/lib/host/activity.js +252 -0
- package/lib/host/assembly-preview.js +244 -0
- package/lib/host/attribution.js +341 -0
- package/lib/host/catalog.js +133 -0
- package/lib/host/categorize.js +40 -0
- package/lib/host/config-layers.js +589 -0
- package/lib/host/constants.js +69 -0
- package/lib/host/control.js +159 -0
- package/lib/host/disable-preview.js +651 -0
- package/lib/host/entries.js +289 -0
- package/lib/host/fiber.js +86 -0
- package/lib/host/fs.js +99 -0
- package/lib/host/health.js +178 -0
- package/lib/host/http.js +65 -0
- package/lib/host/impact.js +138 -0
- package/lib/host/injection-owners.js +60 -0
- package/lib/host/layers.js +132 -0
- package/lib/host/masks.js +145 -0
- package/lib/host/modes.js +96 -0
- package/lib/host/names.js +44 -0
- package/lib/host/patch-config.js +554 -0
- package/lib/host/patch-scan.js +103 -0
- package/lib/host/preview-conditions.js +281 -0
- package/lib/host/preview-contexts.js +174 -0
- package/lib/host/preview-envelope.js +31 -0
- package/lib/host/preview-instructions.js +450 -0
- package/lib/host/preview-messages.js +284 -0
- package/lib/host/preview-modules.js +233 -0
- package/lib/host/preview-skills.js +200 -0
- package/lib/host/prompt.js +213 -0
- package/lib/host/recipe-overlay.js +209 -0
- package/lib/host/request-preview.js +114 -0
- package/lib/host/routes/activation.js +96 -0
- package/lib/host/routes/activity.js +22 -0
- package/lib/host/routes/apply.js +93 -0
- package/lib/host/routes/cleanup.js +62 -0
- package/lib/host/routes/control.js +22 -0
- package/lib/host/routes/disable-preview.js +19 -0
- package/lib/host/routes/impact.js +19 -0
- package/lib/host/routes/operations.js +22 -0
- package/lib/host/routes/preview.js +58 -0
- package/lib/host/routes/session-influence.js +23 -0
- package/lib/host/routes/snapshot.js +21 -0
- package/lib/host/routes/template.js +54 -0
- package/lib/host/routes/templates.js +28 -0
- package/lib/host/scope-impact.js +125 -0
- package/lib/host/session-events.js +39 -0
- package/lib/host/session-influence.js +467 -0
- package/lib/host/sessions.js +293 -0
- package/lib/host/snapshot.js +218 -0
- package/lib/host/source-scan.js +225 -0
- package/lib/host/templates/cleanup.js +219 -0
- package/lib/host/templates/inheritance.js +144 -0
- package/lib/host/templates/materialize.js +317 -0
- package/lib/host/templates/message-overlay.js +184 -0
- package/lib/host/templates/repository.js +323 -0
- package/lib/host/templates/store.js +131 -0
- package/lib/host/tool-set.js +150 -0
- package/lib/host/tools.js +129 -0
- package/lib/index.js +106 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +124 -6
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话视图:从这个会话往上追它的插件是哪来的。
|
|
3
|
+
*
|
|
4
|
+
* 三段追溯链:`全局插件 → 模式要求的插件 → 本会话已挂载`。前两段是「记录/要求」,
|
|
5
|
+
* 第三段才是在跑的东西——只有活着的会话有第三层。
|
|
6
|
+
* 绑了提示词模板的会话会多一行开关(`activationRow`),那是「这个会话出了问题时
|
|
7
|
+
* 先把提示词模板关掉」的安全阀。
|
|
8
|
+
*
|
|
9
|
+
* 本文件是一个包内 chunk:入口用 `require.async('./client.sessions.js')` 取它。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: '@cheemsup/dsh-plugin-console',
|
|
14
|
+
chunk: 'client.sessions.js',
|
|
15
|
+
factory: (require) => {
|
|
16
|
+
const React = require('react')
|
|
17
|
+
const { createElement: h } = React
|
|
18
|
+
|
|
19
|
+
return function create(deps) {
|
|
20
|
+
const {
|
|
21
|
+
modeTitle, formatSessionId, formatAge, resultText, rowEnd,
|
|
22
|
+
selectField, textButton, inlineMeta, toggleIn, setAll, activateOnKey,
|
|
23
|
+
sessionDetail, search,
|
|
24
|
+
} = deps
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 判断一个会话能否作为一组的渲染根。
|
|
28
|
+
*
|
|
29
|
+
* 默认口径:没有父,或父不在索引里(子会话会作为父行的子行出现,不能重复渲染)。
|
|
30
|
+
* 搜索激活时传 `visible`:父不在可见集合里也算根——父被裁掉之后,命中会话
|
|
31
|
+
* 得自己顶上,否则它既不是根、又接不到任何树上。
|
|
32
|
+
* @param {object} session - 会话记录。
|
|
33
|
+
* @param {Map<string, object>} allSessions - 全部会话索引。
|
|
34
|
+
* @param {Set<string> | null} [visible] - 搜索激活时的可见集合。
|
|
35
|
+
* @returns {boolean} 是否是渲染根。
|
|
36
|
+
*/
|
|
37
|
+
function isTreeRoot(session, allSessions, visible) {
|
|
38
|
+
const parentId = session.parentSession
|
|
39
|
+
if (typeof parentId !== 'string') return true
|
|
40
|
+
const parent = allSessions.get(parentId)
|
|
41
|
+
if (parent === undefined) return true
|
|
42
|
+
if (visible === null || visible === undefined) return false
|
|
43
|
+
return !visible.has(parent.id)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 会话字段是否命中搜索。
|
|
48
|
+
*
|
|
49
|
+
* 模式既匹配显示名也匹配 id:界面写「标准模式」,用户就会搜「标准」,
|
|
50
|
+
* 而记录里只有 `standard`。cwd 与 id 一并参与:它们都在行上看得见。
|
|
51
|
+
* @param {object} session - 会话记录。
|
|
52
|
+
* @param {Map<string, object>} presetsById - 模式索引。
|
|
53
|
+
* @param {string} query - 页头搜索词。
|
|
54
|
+
* @returns {boolean} 是否命中。
|
|
55
|
+
*/
|
|
56
|
+
function sessionMatches(session, presetsById, query) {
|
|
57
|
+
const mode = presetsById.get(session.preset)
|
|
58
|
+
const modeLabel = session.preset ? modeTitle(mode ?? { id: session.preset }) : undefined
|
|
59
|
+
return search.matchesQuery(query, [session.title, session.id, session.cwd, session.preset, modeLabel])
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 沿 parentSession 向上补祖先链。
|
|
64
|
+
*
|
|
65
|
+
* 命中的是子会话时,它自己不是派发树的根;不补祖先,这一行根本没有位置可渲染。
|
|
66
|
+
* @param {object} session - 会话记录。
|
|
67
|
+
* @param {Map<string, object>} allSessions - 全部会话索引。
|
|
68
|
+
* @returns {object[]} 从父到根的祖先。
|
|
69
|
+
*/
|
|
70
|
+
function ancestorsOf(session, allSessions) {
|
|
71
|
+
const out = []
|
|
72
|
+
const seen = new Set([session.id])
|
|
73
|
+
let id = session.parentSession
|
|
74
|
+
while (typeof id === 'string' && !seen.has(id)) {
|
|
75
|
+
const parent = allSessions.get(id)
|
|
76
|
+
if (parent === undefined) break
|
|
77
|
+
out.push(parent)
|
|
78
|
+
seen.add(id)
|
|
79
|
+
id = parent.parentSession
|
|
80
|
+
}
|
|
81
|
+
return out
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 派发树里挂在某个会话下面的全部后代(可以跨工作区)。
|
|
86
|
+
* @param {object} session - 会话记录。
|
|
87
|
+
* @param {object} snapshot - 快照。
|
|
88
|
+
* @returns {string[]} 后代 id。
|
|
89
|
+
*/
|
|
90
|
+
function descendantsOf(session, snapshot) {
|
|
91
|
+
const nodes = snapshot.dispatch?.nodes ?? []
|
|
92
|
+
const out = []
|
|
93
|
+
const queue = [session.id]
|
|
94
|
+
while (queue.length > 0) {
|
|
95
|
+
const parentId = queue.shift()
|
|
96
|
+
for (const node of nodes) {
|
|
97
|
+
if (node.parentId !== parentId) continue
|
|
98
|
+
out.push(node.id)
|
|
99
|
+
queue.push(node.id)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return out
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 一次筛选的完整结果:时间、工作区与搜索同时生效。
|
|
107
|
+
*
|
|
108
|
+
* 视图与工具条共用这一份:工具条要显示「显示 N / M 个会话」,视图要用同一个
|
|
109
|
+
* 可见集合决定哪些行留下。分两处各算一次,读数与列表迟早会对不上。
|
|
110
|
+
*
|
|
111
|
+
* 搜索是两级命中:工作区字段命中→整组保留;会话字段命中→只留命中会话,
|
|
112
|
+
* 连同它的子树与祖先链。
|
|
113
|
+
* @param {object} vc - 视图上下文。
|
|
114
|
+
* @returns {{groups: object[], visible: Set<string>|null, context: Set<string>, pinnedOpen: string|null, searching: boolean, shown: number, total: number, inRange: Function}} 筛选结果。
|
|
115
|
+
*/
|
|
116
|
+
function selectSessionGroups(vc) {
|
|
117
|
+
const { snapshot, query, recentDays, workspaceFilter, allSessions, openSession } = vc
|
|
118
|
+
const presetsById = new Map((snapshot.modes ?? []).map((mode) => [mode.id, mode]))
|
|
119
|
+
const filterKey = workspaceFilterOf(snapshot, workspaceFilter)
|
|
120
|
+
|
|
121
|
+
// 时间过滤先于分组:一个工作区可能只剩部分会话命中。
|
|
122
|
+
const cutoff = recentDays === 0 ? 0 : Date.now() - recentDays * 86400000
|
|
123
|
+
const inRange = (session) => cutoff === 0 || (typeof session.createdAt === 'number' && session.createdAt >= cutoff)
|
|
124
|
+
|
|
125
|
+
const groups = [
|
|
126
|
+
...(snapshot.workspaces ?? []).map((workspace) => {
|
|
127
|
+
const all = (workspace.sessions ?? []).filter(inRange)
|
|
128
|
+
return {
|
|
129
|
+
key: workspace.id,
|
|
130
|
+
title: workspace.title || workspace.path || workspace.id,
|
|
131
|
+
path: workspace.path,
|
|
132
|
+
all,
|
|
133
|
+
owned: workspace.sessions ?? [],
|
|
134
|
+
workspaceLastActiveAt: workspace.lastActiveAt,
|
|
135
|
+
}
|
|
136
|
+
}),
|
|
137
|
+
{
|
|
138
|
+
key: '__ungrouped',
|
|
139
|
+
title: '未归属工作区的会话',
|
|
140
|
+
path: undefined,
|
|
141
|
+
all: (snapshot.ungroupedSessions ?? []).filter(inRange),
|
|
142
|
+
owned: snapshot.ungroupedSessions ?? [],
|
|
143
|
+
},
|
|
144
|
+
]
|
|
145
|
+
.map((group) => ({
|
|
146
|
+
...group,
|
|
147
|
+
// 最近活跃的工作区排前面。
|
|
148
|
+
lastActiveAt: group.workspaceLastActiveAt ?? latestOf(group.all),
|
|
149
|
+
}))
|
|
150
|
+
.filter((group) => filterKey === null || group.key === filterKey)
|
|
151
|
+
.sort((left, right) => {
|
|
152
|
+
if (left.key === '__ungrouped') return 1
|
|
153
|
+
if (right.key === '__ungrouped') return -1
|
|
154
|
+
return (right.lastActiveAt ?? 0) - (left.lastActiveAt ?? 0)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const searching = search.normalizeQuery(query) !== ''
|
|
158
|
+
const visible = searching ? new Set() : null
|
|
159
|
+
const context = new Set()
|
|
160
|
+
if (searching) {
|
|
161
|
+
// 工作区字段命中:用户找的是工作区,整组会话都保留。
|
|
162
|
+
for (const group of groups) {
|
|
163
|
+
if (!search.matchesQuery(query, [group.title, group.path])) continue
|
|
164
|
+
for (const session of group.all) {
|
|
165
|
+
visible.add(session.id)
|
|
166
|
+
for (const id of descendantsOf(session, snapshot)) visible.add(id)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// 会话字段命中:只留命中会话与它的子树;子会话命中时沿祖先链补位置。
|
|
170
|
+
for (const group of groups) {
|
|
171
|
+
for (const session of group.all) {
|
|
172
|
+
if (!sessionMatches(session, presetsById, query)) continue
|
|
173
|
+
visible.add(session.id)
|
|
174
|
+
for (const id of descendantsOf(session, snapshot)) visible.add(id)
|
|
175
|
+
for (const ancestor of ancestorsOf(session, allSessions)) {
|
|
176
|
+
if (!inRange(ancestor)) continue
|
|
177
|
+
visible.add(ancestor.id)
|
|
178
|
+
context.add(ancestor.id)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// 正在读的详情不因输入而消失:它可能是此刻唯一要对照的东西。
|
|
184
|
+
const open = typeof openSession === 'string' ? allSessions.get(openSession) : undefined
|
|
185
|
+
const pinnedOpen = searching && open !== undefined && !visible.has(open.id) ? open.id : null
|
|
186
|
+
if (pinnedOpen !== null) {
|
|
187
|
+
visible.add(open.id)
|
|
188
|
+
for (const ancestor of ancestorsOf(open, allSessions)) {
|
|
189
|
+
if (!inRange(ancestor)) continue
|
|
190
|
+
visible.add(ancestor.id)
|
|
191
|
+
context.add(ancestor.id)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const shownGroups = groups
|
|
196
|
+
.map((group) => ({
|
|
197
|
+
...group,
|
|
198
|
+
// 能画出来的根:可见、且没有可见祖先的那些会话。
|
|
199
|
+
//
|
|
200
|
+
// 父被时间范围或搜索裁掉时,命中会话自己成为这一组的根——否则它既不是根、
|
|
201
|
+
// 又接不到任何树上,明明命中了却整组消失。
|
|
202
|
+
roots: group.all
|
|
203
|
+
.filter((session) => visible === null || visible.has(session.id))
|
|
204
|
+
.filter((session) => isTreeRoot(session, allSessions, visible))
|
|
205
|
+
.sort((left, right) => lastOf(right) - lastOf(left)),
|
|
206
|
+
}))
|
|
207
|
+
.filter((group) => group.roots.length > 0)
|
|
208
|
+
const total = groups.reduce((sum, group) => sum + group.all.length, 0)
|
|
209
|
+
const shown = visible === null
|
|
210
|
+
? total
|
|
211
|
+
: groups.reduce((sum, group) => sum + group.all.filter((session) => visible.has(session.id)).length, 0)
|
|
212
|
+
return { groups: shownGroups, visible, context, pinnedOpen, searching, shown, total, inRange }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 会话视图:从会话往上追插件来源。
|
|
217
|
+
*
|
|
218
|
+
* 这是回答「这个会话的插件是哪来的」的唯一入口——别的视图看不出模式这一层。
|
|
219
|
+
* @param {object} vc - 视图上下文。
|
|
220
|
+
* @returns {object} React 节点。
|
|
221
|
+
*/
|
|
222
|
+
function renderSessionsView(vc) {
|
|
223
|
+
const {
|
|
224
|
+
recentDays, setWorkspaceFilter, setRecentDays, setQuery,
|
|
225
|
+
collapsedWorkspaces, setCollapsedWorkspaces,
|
|
226
|
+
activationNotice, cleanupNotice, query,
|
|
227
|
+
} = vc
|
|
228
|
+
const presetsById = new Map((vc.snapshot.modes ?? []).map((mode) => [mode.id, mode]))
|
|
229
|
+
const selection = selectSessionGroups(vc)
|
|
230
|
+
const groups = selection.groups
|
|
231
|
+
|
|
232
|
+
if (!groups.length) {
|
|
233
|
+
return h(
|
|
234
|
+
'div',
|
|
235
|
+
{ className: 'ptEmpty' },
|
|
236
|
+
recentDays === 0 ? '没有匹配的会话。' : `最近 ${recentDays} 天内没有会话。`,
|
|
237
|
+
// 工作区、时间与搜索是三个独立条件;空结果时给一个一次清空三者的出口。
|
|
238
|
+
h(
|
|
239
|
+
'div',
|
|
240
|
+
{ style: { marginTop: '10px' } },
|
|
241
|
+
textButton('清除筛选', () => {
|
|
242
|
+
setWorkspaceFilter(null)
|
|
243
|
+
setRecentDays(0)
|
|
244
|
+
setQuery('')
|
|
245
|
+
}),
|
|
246
|
+
),
|
|
247
|
+
)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return h(
|
|
251
|
+
'div',
|
|
252
|
+
null,
|
|
253
|
+
activationNotice !== null
|
|
254
|
+
? h('p', { className: 'ptNotice', style: { margin: '0 0 10px' } }, activationNotice)
|
|
255
|
+
: null,
|
|
256
|
+
cleanupNotice !== null && cleanupNotice !== undefined
|
|
257
|
+
? h('p', { className: 'ptNotice', style: { margin: '0 0 10px' } }, cleanupNotice)
|
|
258
|
+
: null,
|
|
259
|
+
groups.map((group) => {
|
|
260
|
+
// 搜索激活时忽略折叠:命中藏在收起的组里,页面看起来就是空的。
|
|
261
|
+
const open = selection.searching || !collapsedWorkspaces.has(group.key)
|
|
262
|
+
const inGroup = selection.visible === null
|
|
263
|
+
? group.all.length
|
|
264
|
+
: group.all.filter((session) => selection.visible.has(session.id)).length
|
|
265
|
+
return h(
|
|
266
|
+
'section',
|
|
267
|
+
{ key: group.key, style: { marginBottom: '14px' } },
|
|
268
|
+
h(
|
|
269
|
+
'div',
|
|
270
|
+
{
|
|
271
|
+
className: selection.searching ? 'ptGroupHead' : 'ptGroupHead ptGroupHeadClickable',
|
|
272
|
+
role: selection.searching ? undefined : 'button',
|
|
273
|
+
tabIndex: selection.searching ? undefined : 0,
|
|
274
|
+
'aria-expanded': open ? 'true' : 'false',
|
|
275
|
+
onClick: selection.searching ? undefined : () => toggleIn(setCollapsedWorkspaces, group.key),
|
|
276
|
+
onKeyDown: selection.searching ? undefined : (event) => activateOnKey(event, () => toggleIn(setCollapsedWorkspaces, group.key)),
|
|
277
|
+
},
|
|
278
|
+
h('span', { className: 'ptChevron', 'data-open': open ? 'true' : 'false' }, '▶'),
|
|
279
|
+
h('span', null, search.markQuery(group.title, query)),
|
|
280
|
+
h(
|
|
281
|
+
'span',
|
|
282
|
+
{ className: 'ptCount', title: selection.searching ? '筛选中:本组命中的会话 / 本组会话' : undefined },
|
|
283
|
+
selection.searching ? `${inGroup} / ${group.all.length} 个会话` : group.all.length + ' 个会话',
|
|
284
|
+
),
|
|
285
|
+
// 最近活跃与路径都是读数,统一放在右端;标题行只留「这是哪个工作区、有多少会话」。
|
|
286
|
+
rowEnd(
|
|
287
|
+
typeof group.lastActiveAt === 'number'
|
|
288
|
+
? h('span', { className: 'ptMeta', title: '这个工作区最近一次会话写入的时间' }, '最近活跃 ' + formatAge(group.lastActiveAt))
|
|
289
|
+
: null,
|
|
290
|
+
group.path ? h('span', { className: 'ptMeta' }, search.markQuery(group.path, query)) : null,
|
|
291
|
+
), ),
|
|
292
|
+
open
|
|
293
|
+
? h(
|
|
294
|
+
'ul',
|
|
295
|
+
{ className: 'ptList' },
|
|
296
|
+
group.roots.flatMap((session) => sessionTreeRows(session, presetsById, vc.snapshot, 0, selection, vc)),
|
|
297
|
+
)
|
|
298
|
+
: null,
|
|
299
|
+
)
|
|
300
|
+
}),
|
|
301
|
+
)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** 一个会话的最近活跃时间;宿主没给时退回创建时间。 */
|
|
305
|
+
function lastOf(session) {
|
|
306
|
+
if (typeof session?.lastActiveAt === 'number') return session.lastActiveAt
|
|
307
|
+
return typeof session?.createdAt === 'number' ? session.createdAt : 0
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** 一组会话里最晚的活跃时间。 */
|
|
311
|
+
function latestOf(sessions) {
|
|
312
|
+
let latest = 0
|
|
313
|
+
for (const session of sessions ?? []) latest = Math.max(latest, lastOf(session))
|
|
314
|
+
return latest
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** 会话页筛选:时间范围 + 工作区 + 分组折叠 + 结果数。 */
|
|
318
|
+
function renderSessionFilters(vc) {
|
|
319
|
+
const {
|
|
320
|
+
snapshot, recentDays, setRecentDays, workspaceFilter, setWorkspaceFilter,
|
|
321
|
+
setCollapsedWorkspaces,
|
|
322
|
+
} = vc
|
|
323
|
+
const keys = [...(snapshot.workspaces ?? []).map((workspace) => workspace.id), '__ungrouped']
|
|
324
|
+
const ungroupedCount = (snapshot.ungroupedSessions ?? []).length
|
|
325
|
+
// 结果数与视图共用同一份筛选结果:两处各算一次,数字和列表会对不上。
|
|
326
|
+
const selection = selectSessionGroups(vc)
|
|
327
|
+
return h(
|
|
328
|
+
React.Fragment,
|
|
329
|
+
null,
|
|
330
|
+
selectField('时间', String(recentDays), [
|
|
331
|
+
{ value: '0', label: '全部时间' },
|
|
332
|
+
{ value: '1', label: '今天' },
|
|
333
|
+
{ value: '7', label: '近 7 天' },
|
|
334
|
+
{ value: '30', label: '近 30 天' },
|
|
335
|
+
], (value) => setRecentDays(Number(value))),
|
|
336
|
+
selectField('工作区', workspaceFilterOf(snapshot, workspaceFilter) ?? '', [
|
|
337
|
+
{ value: '', label: '不限工作区' },
|
|
338
|
+
...(snapshot.workspaces ?? []).map((workspace) => ({
|
|
339
|
+
value: workspace.id,
|
|
340
|
+
label: (workspace.title || workspace.path || workspace.id) + '(' + (workspace.sessions ?? []).length + ')',
|
|
341
|
+
})),
|
|
342
|
+
ungroupedCount > 0 ? { value: '__ungrouped', label: '未归属工作区(' + ungroupedCount + ')' } : null,
|
|
343
|
+
], (value) => setWorkspaceFilter(value === '' ? null : value), {
|
|
344
|
+
title: '只显示所选工作区的会话,与时间、搜索同时生效',
|
|
345
|
+
}),
|
|
346
|
+
resultText(selection.shown, selection.total, ' 个会话'),
|
|
347
|
+
h(
|
|
348
|
+
'div',
|
|
349
|
+
{ className: 'ptActions' },
|
|
350
|
+
textButton('折叠全部', () => setAll(setCollapsedWorkspaces, keys, true)),
|
|
351
|
+
textButton('展开全部', () => setAll(setCollapsedWorkspaces, [], false)),
|
|
352
|
+
),
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 工作区筛选的有效值。
|
|
358
|
+
*
|
|
359
|
+
* 快照刷新后选中的工作区可能已经不存在(或未归属组变空);这时按「不限」处理,
|
|
360
|
+
* 否则下拉框会停在一个没有对应 `option` 的值上,看起来像筛选失效。
|
|
361
|
+
* @param {object} snapshot - 快照。
|
|
362
|
+
* @param {string | null} workspaceFilter - 状态里的筛选值。
|
|
363
|
+
* @returns {string | null} 仍然存在的组 key;`null` 表示不限。
|
|
364
|
+
*/
|
|
365
|
+
function workspaceFilterOf(snapshot, workspaceFilter) {
|
|
366
|
+
if (workspaceFilter === null || workspaceFilter === undefined) return null
|
|
367
|
+
if (workspaceFilter === '__ungrouped') {
|
|
368
|
+
return (snapshot.ungroupedSessions ?? []).length > 0 ? '__ungrouped' : null
|
|
369
|
+
}
|
|
370
|
+
const exists = (snapshot.workspaces ?? []).some((workspace) => workspace.id === workspaceFilter)
|
|
371
|
+
return exists ? workspaceFilter : null
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* 把一棵派发子树摊成行:父在前,子缩进。
|
|
376
|
+
*
|
|
377
|
+
* 子会话可能属于别的工作区(父在工作区 A、子在 B),所以不能依赖分组已经
|
|
378
|
+
* 把它们放在一起——这里按 `dispatch.nodes` 的 parentId 反查,跨组也能接上。
|
|
379
|
+
* @param {object} session - 当前行对应的会话。
|
|
380
|
+
* @param {Map<string, object>} presetsById - 模式索引。
|
|
381
|
+
* @param {object} snapshot - 快照。
|
|
382
|
+
* @param {number} depth - 缩进层级。
|
|
383
|
+
* @param {object} selection - `selectSessionGroups` 的结果:时间谓词与可见集合。
|
|
384
|
+
* @param {object} vc - 视图上下文。
|
|
385
|
+
* @returns {Array<object>} React 节点数组。
|
|
386
|
+
*/
|
|
387
|
+
function sessionTreeRows(session, presetsById, snapshot, depth, selection, vc) {
|
|
388
|
+
const row = sessionRow(session, presetsById, snapshot, depth, selection, vc)
|
|
389
|
+
const nodes = snapshot.dispatch?.nodes ?? []
|
|
390
|
+
const children = nodes
|
|
391
|
+
.filter((node) => node.parentId === session.id)
|
|
392
|
+
.map((node) => node.id)
|
|
393
|
+
.map((id) => vc.allSessions.get(id))
|
|
394
|
+
.filter((child) => child !== undefined && selection.inRange(child))
|
|
395
|
+
// 命中会话的子树已整体保留;其余子会话按可见集合裁掉。
|
|
396
|
+
.filter((child) => selection.visible === null || selection.visible.has(child.id))
|
|
397
|
+
return [row, ...children.flatMap((child) => sessionTreeRows(child, presetsById, snapshot, depth + 1, selection, vc))]
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* 一个会话行:默认收起,展开后是完整追溯链。
|
|
402
|
+
* @param {object} session - 会话记录。
|
|
403
|
+
* @param {Map<string, object>} presetsById - 模式索引。
|
|
404
|
+
* @param {object} snapshot - 快照。
|
|
405
|
+
* @param {number} depth - 缩进层级。
|
|
406
|
+
* @param {object} selection - 筛选结果:用来标出上下文行与「当前打开」行。
|
|
407
|
+
* @param {object} vc - 视图上下文。
|
|
408
|
+
* @returns {object} React 节点。
|
|
409
|
+
*/
|
|
410
|
+
function sessionRow(session, presetsById, snapshot, depth = 0, selection, vc) {
|
|
411
|
+
const { openSession, setOpenSession } = vc
|
|
412
|
+
const mode = presetsById.get(session.preset)
|
|
413
|
+
const isOpen = openSession === session.id
|
|
414
|
+
const label = session.preset ? modeTitle(mode ?? { id: session.preset }) : null
|
|
415
|
+
const dispatched = typeof session.parentSession === 'string'
|
|
416
|
+
const childCount = (snapshot.dispatch?.nodes ?? []).filter((node) => node.parentId === session.id).length
|
|
417
|
+
// 这个会话有没有绑提示词模板。没绑就不出现开关——开关只管「提示词模板要不要生效」,
|
|
418
|
+
// 无中生有一个开关只会让人以为是别的东西。
|
|
419
|
+
const binding = (snapshot.bindings ?? {})[session.id]
|
|
420
|
+
return h(
|
|
421
|
+
'li',
|
|
422
|
+
{ key: session.id },
|
|
423
|
+
h(
|
|
424
|
+
'button',
|
|
425
|
+
{
|
|
426
|
+
type: 'button',
|
|
427
|
+
className: 'ptRow',
|
|
428
|
+
'data-selected': isOpen ? 'true' : 'false',
|
|
429
|
+
'aria-expanded': isOpen ? 'true' : 'false',
|
|
430
|
+
title: session.live ? '本进程仍持有这个会话,展开可看运行时实测' : '会话未激活,展开只看记录与声明',
|
|
431
|
+
onClick: () => setOpenSession(isOpen ? null : session.id),
|
|
432
|
+
},
|
|
433
|
+
h('span', { className: 'ptIndent', style: { width: (depth * 16) + 'px' } }),
|
|
434
|
+
h('span', {
|
|
435
|
+
className: 'ptDot',
|
|
436
|
+
'data-phase': session.live ? 'active' : 'none',
|
|
437
|
+
role: 'img',
|
|
438
|
+
'aria-label': session.live ? '本进程内会话仍存活' : '这个会话不在本进程内',
|
|
439
|
+
}),
|
|
440
|
+
h(
|
|
441
|
+
'span',
|
|
442
|
+
{ className: 'ptName' },
|
|
443
|
+
// 主标题用 DSH 侧边栏同一个值(session/title 投影);没有标题时才退回 id。
|
|
444
|
+
search.markQuery(session.title !== undefined ? session.title : formatSessionId(session.id), vc.query),
|
|
445
|
+
h(
|
|
446
|
+
'span',
|
|
447
|
+
{ className: 'ptModule', style: { display: 'block' } },
|
|
448
|
+
search.markQuery(session.cwd ?? '(无 cwd 记录)', vc.query),
|
|
449
|
+
),
|
|
450
|
+
),
|
|
451
|
+
dispatched
|
|
452
|
+
? session.origin === 'subagent'
|
|
453
|
+
? h('span', { className: 'ptTag', title: 'subagent:由父会话使用 subagent 工具时创建' }, 'subagent')
|
|
454
|
+
: h('span', { className: 'ptTag', title: '分叉:从父会话中复制某些轮次然后新建会话(header.isSeeded = true)' }, '分叉')
|
|
455
|
+
: null,
|
|
456
|
+
childCount > 0 ? h('span', { className: 'ptTag', title: '有 ' + childCount + ' 个会话挂在它下面' }, '父会话') : null,
|
|
457
|
+
binding !== undefined
|
|
458
|
+
? h('span', {
|
|
459
|
+
className: 'ptTag',
|
|
460
|
+
'data-tone': binding.enabled ? 'user' : 'warn',
|
|
461
|
+
// 继承来源放在句末:先回答「绑了什么」,再补「从哪继承」。
|
|
462
|
+
title: '这个会话绑定了提示词定制模板:' + sessionDetail.templateLabelOf(binding)
|
|
463
|
+
+ (typeof binding.inheritedFrom === 'string' ? '(继承自 ' + binding.inheritedFrom + ')' : ''),
|
|
464
|
+
}, binding.enabled ? (binding.materialized ? '提示词定制生效中' : '提示词定制已启用') : '提示词定制已停用')
|
|
465
|
+
: null,
|
|
466
|
+
// 读数与搜索期的临时标记统一靠右:左边只留「这个会话是什么」。
|
|
467
|
+
rowEnd(
|
|
468
|
+
selection.context.has(session.id)
|
|
469
|
+
? h('span', { className: 'ptTag', title: '命中会话的祖先,补进来只为保住派发树里的位置' }, '上下文')
|
|
470
|
+
: null,
|
|
471
|
+
selection.pinnedOpen === session.id
|
|
472
|
+
? h('span', { className: 'ptTag', 'data-tone': 'warn', title: '搜索没有命中它,但它正在被阅读,仍然留在列表里' }, '当前打开(不在结果中)')
|
|
473
|
+
: null,
|
|
474
|
+
inlineMeta([
|
|
475
|
+
label !== null ? search.markQuery(label, vc.query) : h('span', { className: 'ptError' }, '未记录模式'),
|
|
476
|
+
session.lastActiveAt ? h('span', { title: '最近活跃:会话日志最后写入时间 ' + new Date(session.lastActiveAt).toLocaleString() }, '活跃 ' + formatAge(session.lastActiveAt)) : null,
|
|
477
|
+
session.createdAt ? h('span', { title: '创建时间:' + new Date(session.createdAt).toLocaleString() }, '创建 ' + formatAge(session.createdAt)) : null,
|
|
478
|
+
session.runtime ? '实测 ' + session.runtime.modules.length : null,
|
|
479
|
+
]),
|
|
480
|
+
),
|
|
481
|
+
),
|
|
482
|
+
isOpen ? sessionDetail.renderSessionTrace(session, presetsById, vc) : null,
|
|
483
|
+
)
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** 模式展示名;roster 里没有时退回 id。 */
|
|
487
|
+
function labelOf(mode, fallback) {
|
|
488
|
+
if (mode !== undefined) return modeTitle(mode)
|
|
489
|
+
return typeof fallback === 'string' ? fallback : '未记录'
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return { renderSessionsView, renderSessionFilters, isTreeRoot, labelOf }
|
|
493
|
+
}
|
|
494
|
+
},
|
|
495
|
+
})
|