dsh-leader-agent 0.5.0 → 0.7.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.
Files changed (68) hide show
  1. package/.agents/skills/e2e-scenarios/SKILL.md +3 -1
  2. package/.agents/skills/leader-mode/SKILL.md +1 -1
  3. package/.agents/skills/leader-mode/playbooks/program.md +3 -1
  4. package/.agents/skills/leader-mode/references/parallel.md +1 -1
  5. package/README.md +13 -5
  6. package/README_ZH.md +13 -5
  7. package/THIRD_PARTY_LICENSE +84 -0
  8. package/lib/client.js +2974 -601
  9. package/locale/de.json +134 -0
  10. package/locale/en.json +129 -1
  11. package/locale/es.json +134 -0
  12. package/locale/fr.json +134 -0
  13. package/locale/ja.json +134 -0
  14. package/locale/pt.json +134 -0
  15. package/locale/ru.json +134 -0
  16. package/locale/zh.json +130 -2
  17. package/package.json +20 -4
  18. package/src/client/agent-bubble-styles.js +31 -1
  19. package/src/client/agent-bubble-view.js +42 -17
  20. package/src/client/board-card.js +18 -16
  21. package/src/client/board-foreground.js +40 -0
  22. package/src/client/board-projection.js +6 -2
  23. package/src/client/board-styles.js +11 -41
  24. package/src/client/board.js +142 -39
  25. package/src/client/bubble-clock.js +10 -6
  26. package/src/client/card-detail-view.js +8 -11
  27. package/src/client/card-detail.js +12 -11
  28. package/src/client/card-links-editor.js +27 -15
  29. package/src/client/index.js +83 -9
  30. package/src/client/leader-changes-anchor.js +51 -0
  31. package/src/client/leader-changes.js +150 -0
  32. package/src/client/leader-view-logic.js +2 -1
  33. package/src/client/leader-view.js +1 -1
  34. package/src/client/locales.generated.js +1028 -0
  35. package/src/client/locales.js +27 -31
  36. package/src/client/main-panel.js +73 -0
  37. package/src/client/markdown.js +34 -11
  38. package/src/client/project-board-data.js +36 -1
  39. package/src/client/project-changes.js +2 -1
  40. package/src/client/project-panel-logic.js +24 -17
  41. package/src/client/project-panel.js +47 -45
  42. package/src/client/project-remote.js +56 -29
  43. package/src/client/project-section.js +25 -18
  44. package/src/client/ready-signal.js +1 -1
  45. package/src/client/session-tabs-logic.js +383 -0
  46. package/src/client/session-tabs.js +722 -0
  47. package/src/client/session-watermark.js +10 -6
  48. package/src/client/signal-styles.js +56 -0
  49. package/src/client/toast-view.js +1 -0
  50. package/src/client/worker-report-avatar.js +243 -51
  51. package/src/client/worker-report-view.js +4 -2
  52. package/src/client/worker-task-view.js +8 -7
  53. package/src/host/cards/card-context.js +1 -1
  54. package/src/host/cards/card-list-view.js +65 -10
  55. package/src/host/cards/store.js +24 -5
  56. package/src/host/dispatch.js +386 -27
  57. package/src/host/index.js +22 -5
  58. package/src/host/notification-store.js +19 -0
  59. package/src/host/projects/remote.js +4 -1
  60. package/src/host/projects/store.js +1 -2
  61. package/src/host/skills.js +1 -1
  62. package/src/host/tools/cards.js +105 -17
  63. package/src/host/tools/gate.js +17 -12
  64. package/src/host/tools/spawn-worker.js +1 -1
  65. package/src/host/worker-registry.js +73 -2
  66. package/src/host/worker-stall.js +207 -0
  67. package/src/shared/card-links.js +6 -6
  68. package/src/shared/worker-stall.js +66 -0
@@ -16,6 +16,17 @@ import { createElement, useLayoutEffect, useRef, useState } from 'react'
16
16
  import { normalizeLinkDrafts } from '../shared/card-links.js'
17
17
  import { pickInitialFocus } from './modal-focus.js'
18
18
  import { productIcon } from './icons.js'
19
+ import { projectRemoteErrorText } from './project-remote.js'
20
+
21
+ /** 共享校验错误码到界面文案 key;不拼字符串,门禁才能静态看到每个 key。 */
22
+ const LINK_ERROR_KEYS = Object.freeze({
23
+ notArray: 'card.links.error.notArray',
24
+ entryNotObject: 'card.links.error.entryNotObject',
25
+ fieldNotString: 'card.links.error.fieldNotString',
26
+ labelMissingUrl: 'card.links.error.labelMissingUrl',
27
+ labelMissingPurpose: 'card.links.error.labelMissingPurpose',
28
+ invalidUrl: 'card.links.error.invalidUrl',
29
+ })
19
30
 
20
31
  /** 新增草稿的本地 key 序号。只在编辑器内部当渲染 key,不落盘。 */
21
32
  let draftSequence = 0
@@ -78,9 +89,10 @@ function isSubmitKey(event) {
78
89
  * @param {object} props.card - 正在编辑的卡片
79
90
  * @param {(links: object[]) => Promise<void>} props.onSave - 保存;失败时抛出
80
91
  * @param {() => void} props.onCancel - 取消
92
+ * @param {(key: string, params?: Record<string, unknown>) => string} props.t - 当前语言的翻译函数
81
93
  * @returns {object} React 元素
82
94
  */
83
- export function CardLinksEditor({ card, onSave, onCancel }) {
95
+ export function CardLinksEditor({ card, onSave, onCancel, t }) {
84
96
  const [drafts, setDrafts] = useState(() => toDrafts(card?.links))
85
97
  const [error, setError] = useState('')
86
98
  const [saving, setSaving] = useState(false)
@@ -152,7 +164,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
152
164
  if (saving) return
153
165
  const normalized = normalizeLinkDrafts(drafts.map(({ key: _key, ...draft }) => draft))
154
166
  if (normalized.error !== undefined) {
155
- setError(normalized.error)
167
+ setError(t(LINK_ERROR_KEYS[normalized.error.code], normalized.error.params))
156
168
  return
157
169
  }
158
170
  setError('')
@@ -163,7 +175,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
163
175
  try {
164
176
  await onSave(normalized.links)
165
177
  } catch (cause) {
166
- setError(`保存链接失败:${cause instanceof Error ? cause.message : String(cause)}`)
178
+ setError(projectRemoteErrorText(cause, t, 'card.links.saveFailed'))
167
179
  restoreFocus.current = true
168
180
  } finally {
169
181
  setSaving(false)
@@ -177,7 +189,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
177
189
  ? createElement(
178
190
  'p',
179
191
  { className: 'dsh-leader-agent-link-editor-empty' },
180
- '这张卡还没有挂链接。',
192
+ t('card.links.empty'),
181
193
  )
182
194
  : drafts.map((draft, index) => createElement(
183
195
  'div',
@@ -189,8 +201,8 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
189
201
  type: 'text',
190
202
  className: 'dsh-leader-agent-link-editor-label',
191
203
  value: draft.label,
192
- placeholder: '标签',
193
- 'aria-label': '链接标签',
204
+ placeholder: t('card.links.label'),
205
+ 'aria-label': t('card.links.labelAria'),
194
206
  ...index === 0 ? { 'data-modal-autofocus': '' } : {},
195
207
  disabled: saving,
196
208
  onChange: (event) => update(draft.key, 'label', event.target.value),
@@ -203,7 +215,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
203
215
  className: 'dsh-leader-agent-link-editor-url',
204
216
  value: draft.url,
205
217
  placeholder: 'https://…',
206
- 'aria-label': '链接 URL',
218
+ 'aria-label': t('card.links.urlAria'),
207
219
  disabled: saving,
208
220
  onChange: (event) => update(draft.key, 'url', event.target.value),
209
221
  onKeyDown: (event) => {
@@ -215,8 +227,8 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
215
227
  {
216
228
  type: 'button',
217
229
  className: 'dsh-leader-agent-link-editor-remove',
218
- title: '移除链接',
219
- 'aria-label': '移除链接',
230
+ title: t('card.links.remove'),
231
+ 'aria-label': t('card.links.remove'),
220
232
  disabled: saving,
221
233
  onClick: () => remove(draft.key),
222
234
  },
@@ -232,14 +244,14 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
232
244
  disabled: saving,
233
245
  onClick: () => update(draft.key, 'purpose', ''),
234
246
  },
235
- '添加用途',
247
+ t('card.links.addPurpose'),
236
248
  )
237
249
  : createElement('input', {
238
250
  type: 'text',
239
251
  className: 'dsh-leader-agent-link-editor-purpose',
240
252
  value: draft.purpose,
241
- placeholder: '用途(可选)',
242
- 'aria-label': '链接用途',
253
+ placeholder: t('card.links.purpose'),
254
+ 'aria-label': t('card.links.purposeAria'),
243
255
  disabled: saving,
244
256
  onChange: (event) => update(draft.key, 'purpose', event.target.value),
245
257
  onKeyDown: (event) => {
@@ -255,7 +267,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
255
267
  disabled: saving,
256
268
  onClick: add,
257
269
  },
258
- '+ 添加链接',
270
+ t('card.links.add'),
259
271
  ),
260
272
  error === ''
261
273
  ? null
@@ -271,7 +283,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
271
283
  disabled: saving,
272
284
  onClick: onCancel,
273
285
  },
274
- '取消',
286
+ t('common.cancel'),
275
287
  ),
276
288
  createElement(
277
289
  'button',
@@ -281,7 +293,7 @@ export function CardLinksEditor({ card, onSave, onCancel }) {
281
293
  disabled: saving,
282
294
  onClick: () => { void submit() },
283
295
  },
284
- saving ? '保存中…' : '保存',
296
+ saving ? t('common.saving') : t('common.save'),
285
297
  ),
286
298
  ),
287
299
  )
@@ -18,12 +18,14 @@ import { BoardBody } from './board.js'
18
18
  import { BoardGuideEntry } from './board-guide-entry.js'
19
19
  import { ProjectPanel } from './project-panel.js'
20
20
  import { mountGuideCard } from './guide-card.js'
21
+ import { LeaderChangesAnchor } from './leader-changes-anchor.js'
21
22
  import { mountLeaderView } from './leader-view.js'
22
23
  import { watchLeaderSessions } from './leader-session-index.js'
23
- import { SLOT_LOCALE_NAMESPACE, en, zh } from './locales.js'
24
- import { callProjectRemote, mountProjectRemote, reportProjectRemoteError } from './project-remote.js'
24
+ import { LOCALE_UI, SLOT_LOCALE_NAMESPACE, unknownLocaleCodes } from './locales.js'
25
+ import { callProjectRemote, mountProjectRemote } from './project-remote.js'
25
26
  import { mountProjectBoardMonitor } from './project-board-monitor.js'
26
27
  import { createReadySignal } from './ready-signal.js'
28
+ import { SessionTabsAnchor } from './session-tabs.js'
27
29
  import { ToastOverlay } from './toast-view.js'
28
30
  import { WORKER_REPORT_NODE_KIND, workerReportDefinition } from './worker-report-definition.js'
29
31
  import { WorkerReportView } from './worker-report-view.js'
@@ -49,24 +51,51 @@ export function apply(ctx) {
49
51
  // 槽位显示名字典:槽位名本身是 ASCII 机器标识,卡面按当前界面语言翻译。
50
52
  // 字典晚于 UI 挂载也能生效(locale 服务会推 revision),不必等挂载顺序。
51
53
  ctx.effect(
52
- () => ctx.locale.register(SLOT_LOCALE_NAMESPACE, { zh, en }),
54
+ () => ctx.locale.register(SLOT_LOCALE_NAMESPACE, LOCALE_UI),
53
55
  'dsh-leader-agent 槽位显示名',
54
56
  )
57
+ // 注册期文案(tab 标题、guide 说明、左栏 label)没有 `t` 形参,宿主只在
58
+ // 取用时重读 thunk;bind 返回的函数同样在调用时读当前语言,语言切换后
59
+ // 下次投影就是新文案,不需要重新注册。
60
+ const t = ctx.locale.bind(SLOT_LOCALE_NAMESPACE)
61
+ // 语言目录是宿主运行时的:本地多一个 locale 文件不代表宿主认它。只比较并告警,
62
+ // 不把宿主列表写进构建门禁——宿主加语言时门禁不该跟着漂。
63
+ const hostLocales = ctx.locale?.getSnapshot?.()?.locales
64
+ if (Array.isArray(hostLocales)) {
65
+ for (const code of unknownLocaleCodes(LOCALE_UI, hostLocales)) {
66
+ const message = `dsh-leader-agent: locale/${code}.json 不在宿主语言目录里,运行时不会被选中`
67
+ ctx.logger?.warn?.(message)
68
+ // 浏览器的 client logger 不一定有 exporter;配置错误要能在 DevTools 里看见。
69
+ console.warn(message)
70
+ }
71
+ }
55
72
 
56
73
  // 注册必须放进 effect:注册表把 effect 挂到调用方 fiber 上,在别的插件的
57
74
  // apply 里直接注册会让浏览器启动静默卡住。
58
75
  ctx.effect(() => ctx.sidebarRightTabs.register({
59
76
  id: PLUGIN_ID,
60
77
  kind: 'dsh-leader-agent-board',
61
- title: () => '看板',
78
+ // 右栏按会话存布局:切会话时宿主会卸载非当前会话的栏。看板保留已访问的
79
+ // body(keepMounted),切回来不重挂、不重新拉项目与卡片,避免闪一下。
80
+ keepMounted: true,
81
+ title: () => t('tab.board'),
62
82
  guide: [{
63
83
  id: 'board',
64
84
  order: 10,
65
- title: () => '看板',
66
- description: () => '项目 leader 与 worker 会话里的任务交付看板',
85
+ title: () => t('tab.board'),
86
+ description: () => t('tab.board.description'),
67
87
  }],
68
88
  }), 'dsh-leader-agent 看板 tab 类型')
69
89
 
90
+ // tab chip 的标题单独占一个席位:宿主在打开 tab 时就把 title thunk 的结果写进
91
+ // 布局记录,之后切语言不会重读。这里按 definition id 接管自己的标题,让 chip
92
+ // 跟着当前语言走;不注册时宿主回落到打开那一刻的快照。
93
+ ctx.effect(() => ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({
94
+ name: 'sidebar.right.pane.tab.title',
95
+ key: PLUGIN_ID,
96
+ locale: SLOT_LOCALE_NAMESPACE,
97
+ }, BoardTabTitle)), 'dsh-leader-agent 看板 tab 标题')
98
+
70
99
  // 面板主体挂在 tab 类型 id 上:tab 类型 id 既是实现身份,也是它在
71
100
  // `sidebar.right.pane.tab` 座里的注册键。
72
101
  //
@@ -82,6 +111,7 @@ export function apply(ctx) {
82
111
  inject: () => ({
83
112
  getSessions: () => ctx.sessions,
84
113
  getUiWorkspace: () => ctx.get('uiWorkspace'),
114
+ getSidebarRight: () => ctx.get('sidebarRight'),
85
115
  callRemote: (method, args) => callProjectRemote(ctx, method, args),
86
116
  }),
87
117
  }, BoardBody)), 'dsh-leader-agent 看板面板')
@@ -113,7 +143,8 @@ export function apply(ctx) {
113
143
  name: 'sidebar.footer.action',
114
144
  id: 'dsh-leader-agent-projects',
115
145
  order: 10,
116
- label: '项目',
146
+ label: () => t('project.label'),
147
+ locale: SLOT_LOCALE_NAMESPACE,
117
148
  inject: () => ({
118
149
  callRemote: (method, args) => callProjectRemote(ctx, method, args),
119
150
  getUiWorkspace: () => ctx.get('uiWorkspace'),
@@ -134,6 +165,37 @@ export function apply(ctx) {
134
165
  order: 20,
135
166
  }, ToastOverlay)), 'dsh-leader-agent 提示浮层')
136
167
 
168
+ // 项目会话标签栏:宿主主区域一次只挂一条会话,没有原生多标签。挂到跨栏
169
+ // 浮层只是借一个稳定的 React 生命周期,真正的 DOM 由组件插到主面板顶部。
170
+ // 普通会话由组件自己判定隐藏,这里不做过滤。
171
+ ctx.effect(() => ctx.slots.inject('shell.overlay', () => ctx.slots.register({
172
+ name: 'shell.overlay',
173
+ id: 'dsh-leader-agent-session-tabs',
174
+ order: 30,
175
+ locale: SLOT_LOCALE_NAMESPACE,
176
+ inject: () => ({
177
+ callRemote: (method, args) => callProjectRemote(ctx, method, args),
178
+ getSessions: () => ctx.sessions,
179
+ getUiWorkspace: () => ctx.get('uiWorkspace'),
180
+ getLayout: () => ctx.get('layout'),
181
+ getSidebarRight: () => ctx.get('sidebarRight'),
182
+ // 「更多」菜单走宿主的 contextMenus 服务:标签栏接管按钮本身,菜单项
183
+ // 仍由宿主与其它插件按 sessionHeader 目标注册,不在本仓复制一份。
184
+ getContextMenus: () => ctx.get('contextMenus'),
185
+ }),
186
+ }, SessionTabsAnchor)), 'dsh-leader-agent 会话标签栏')
187
+
188
+ // leader 会话不显示宿主的「已编辑 N 个文件」汇总卡片(`conversation.chat.turnTail`
189
+ // 里的 deliverables 条目):宿主的 slot 注册没有按会话过滤的参数,唯一的开关是
190
+ // 全局偏好,所以走渲染层——在主面板容器上打标记,由本插件的样式表隐藏。
191
+ // 锚点同样借 `shell.overlay` 的 React 生命周期,不渲染内容。
192
+ ctx.effect(() => ctx.slots.inject('shell.overlay', () => ctx.slots.register({
193
+ name: 'shell.overlay',
194
+ id: 'dsh-leader-agent-leader-changes',
195
+ order: 40,
196
+ inject: () => ({ getSessions: () => ctx.sessions }),
197
+ }, LeaderChangesAnchor)), 'dsh-leader-agent leader 改动卡片隐藏')
198
+
137
199
  // leader 视图目标:让 leader 会话进入官方 active 形态,输入框停靠底部而会话区
138
200
  // 保持空白。`uiConversation` 由 ui-conversation bundle 提供,缺失时这个回调
139
201
  // 不执行(`ctx.inject` 单独起一个 fiber,不拖累本插件),leader 会话退化成
@@ -152,7 +214,6 @@ export function apply(ctx) {
152
214
  ctx.inject(['remote'], function mountProjectChannel(remoteCtx) {
153
215
  void mountProjectRemote(remoteCtx).catch((error) => {
154
216
  const message = error instanceof Error ? error.message : String(error)
155
- reportProjectRemoteError(message)
156
217
  remoteCtx.logger?.warn?.(`dsh-leader-agent: 项目通道挂载失败:${message}`)
157
218
  })
158
219
  // leader 会话名单走同一条通道:填充不挂在项目面板上,侧栏收起时面板会卸载。
@@ -162,6 +223,17 @@ export function apply(ctx) {
162
223
  })
163
224
  }
164
225
 
226
+ /**
227
+ * 看板 tab chip 的标题。
228
+ *
229
+ * @param {object} props - 槽位属性
230
+ * @param {(key: string, params?: Record<string, unknown>) => string} props.t - 当前语言的翻译函数
231
+ * @returns {string} 标题
232
+ */
233
+ function BoardTabTitle({ t }) {
234
+ return t('tab.board')
235
+ }
236
+
165
237
  /**
166
238
  * 挂载看板监视器:建卡后自动打开(或聚焦)右栏的看板 tab。
167
239
  *
@@ -191,7 +263,8 @@ function mountBoardMonitor(ctx) {
191
263
  * 两类消息共用同一套注册机制:leader 派给 worker 的任务(worker 会话里,渲染成
192
264
  * 右侧用户气泡)与 worker 的回报(leader 会话里,渲染成左侧 agent 气泡)。定义都用
193
265
  * exclusive claim 接管带自家 `source.kind` 的 `user/message`,让它们不再走官方
194
- * 上下文行;渲染器按节点 kind 注册,键必须等于 kind。
266
+ * 上下文行;渲染器按节点 kind 注册,键必须等于 kind。后台巡查提醒不注册节点,
267
+ * 它是带 `【后台巡查】` 前缀的普通文本;不再注册会话节点,始终显示为官方上下文行。
195
268
  *
196
269
  * @param {object} ctx - 客户端 Cordis 上下文(已注入 `uiConversation`)
197
270
  * @returns {void}
@@ -212,6 +285,7 @@ function mountMessageBubbles(ctx) {
212
285
  ctx.effect(() => ctx.slots.inject('conversation.chat.node', () => ctx.slots.register({
213
286
  name: 'conversation.chat.node',
214
287
  key: bubble.kind,
288
+ locale: SLOT_LOCALE_NAMESPACE,
215
289
  // `uiWorkspace` 只挂在 web-app bundle 上,写进 inject 的服务缺失会让整个插件
216
290
  // 挂起——惰性获取,点击名字时才取。
217
291
  inject: () => ({
@@ -0,0 +1,51 @@
1
+ /**
2
+ * leader 改动文件卡片隐藏的挂载点。
3
+ *
4
+ * 不渲染内容,只借 `shell.overlay` 的 React 生命周期订阅会话快照与 leader 名单;
5
+ * 判定变化时在主面板容器上加/摘标记(判定与样式在 `leader-changes.js`)。宿主
6
+ * 重渲染换掉主面板节点时用 MutationObserver 补回,与标签栏同一节流口径。
7
+ *
8
+ * @module dsh-leader-agent/client/leader-changes-anchor
9
+ */
10
+
11
+ import { useEffect, useState } from 'react'
12
+ import { ensureLeaderChangesStyles, isLeaderSession, watchLeaderChanges } from './leader-changes.js'
13
+ import { leaderSessionIndex } from './leader-session-index.js'
14
+ import { currentSessionIdOf } from './project-panel-logic.js'
15
+
16
+ /**
17
+ * 隐藏标记的挂载点。
18
+ *
19
+ * @param {object} props - 槽位注入的运行时能力
20
+ * @param {() => object | undefined} props.getSessions - 取宿主的会话快照服务
21
+ * @returns {null} 不渲染内容
22
+ */
23
+ export function LeaderChangesAnchor({ getSessions }) {
24
+ const sessions = getSessions?.()
25
+ const [snapshot, setSnapshot] = useState(() => sessions?.list?.getSnapshot?.() ?? {
26
+ phase: 'pending',
27
+ ids: [],
28
+ byId: {},
29
+ })
30
+ const [leaderIds, setLeaderIds] = useState(() => leaderSessionIndex.getSnapshot())
31
+
32
+ useEffect(() => {
33
+ if (typeof sessions?.list?.subscribe !== 'function') return undefined
34
+ const sync = () => setSnapshot(sessions.list.getSnapshot())
35
+ sync()
36
+ return sessions.list.subscribe(sync)
37
+ }, [sessions])
38
+
39
+ // 订阅即刻回调一次当前名单,接通时不会停在空集上。
40
+ useEffect(() => leaderSessionIndex.subscribe(setLeaderIds), [])
41
+
42
+ const currentSessionId = currentSessionIdOf(snapshot.byId)
43
+ const hide = isLeaderSession(leaderIds, currentSessionId)
44
+
45
+ useEffect(() => {
46
+ ensureLeaderChangesStyles()
47
+ return watchLeaderChanges(hide)
48
+ }, [hide])
49
+
50
+ return null
51
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * leader 会话隐藏改动文件汇总卡片的判定与样式。
3
+ *
4
+ * 宿主的 `@deepseek-ai/dsh-client-ui-deliverables` 在每个完成轮次的收尾消息后
5
+ * 渲染一张「已编辑 N 个文件」卡片,注册在 chat 视图的 `conversation.chat.turnTail`
6
+ * 列表槽里。slot 注册没有按会话过滤的参数,第三方也不能卸载别人的注册;宿主
7
+ * 唯一的开关是全局偏好「显示代码工作视图」,关掉会连带轨迹视图与完整预设列表。
8
+ *
9
+ * leader 会话是协调者,卡片上列的是它自己的文件改动,对看对话的人没有信息量。
10
+ * 这里只做**渲染层隐藏**:判定当前主区域会话是 leader 时,给主面板容器加一个
11
+ * 标记,样式表按宿主的 `data-changed-files` 属性把卡片藏掉。数据层不动,摘要
12
+ * 请求照发(隐藏与不渲染是两件事,见 docs/limitations.md)。
13
+ *
14
+ * 与 `workspace-exclusion` 同一套做法:宿主 DOM 上的语义标记 + 自家样式表,
15
+ * 不碰宿主的组件与数据。标记名(`data-changed-files`)是宿主的实现细节,
16
+ * 宿主改版时要跟着核。
17
+ *
18
+ * @module dsh-leader-agent/client/leader-changes
19
+ */
20
+
21
+ import { findMainPanel } from './main-panel.js'
22
+
23
+ /** 主面板容器上的隐藏标记。 */
24
+ export const HIDE_ATTR = 'data-dsh-leader-agent-hide-changed-files'
25
+
26
+ /** 样式表 id。 */
27
+ const STYLE_ID = 'dsh-leader-agent-leader-changes'
28
+
29
+ // client 侧的样式写在模板字符串里,不能出现反引号。
30
+ const STYLE_TEXT = `
31
+ [${HIDE_ATTR}] [data-changed-files] {
32
+ display: none !important;
33
+ }
34
+ `
35
+
36
+ /**
37
+ * 注入隐藏规则。同一页面只注入一次。
38
+ * @returns {void}
39
+ */
40
+ export function ensureLeaderChangesStyles() {
41
+ if (typeof document === 'undefined') return
42
+ if (document.getElementById(STYLE_ID) !== null) return
43
+ const style = document.createElement('style')
44
+ style.id = STYLE_ID
45
+ style.textContent = STYLE_TEXT
46
+ document.head.append(style)
47
+ }
48
+
49
+ /**
50
+ * 这个会话是不是某个项目的 leader。
51
+ *
52
+ * 名单来自左栏项目区块写入的索引(`projects/list` 的 `leaderSessionId`),
53
+ * 不含 worker 与同目录的普通会话。
54
+ *
55
+ * @param {Set<string> | undefined} leaderIds - leader 会话 id 名单
56
+ * @param {string | undefined} sessionId - 主区域当前会话 id
57
+ * @returns {boolean} 是否隐藏该会话的改动文件卡片
58
+ */
59
+ export function isLeaderSession(leaderIds, sessionId) {
60
+ if (typeof sessionId !== 'string' || sessionId === '') return false
61
+ return leaderIds instanceof Set && leaderIds.has(sessionId)
62
+ }
63
+
64
+ /**
65
+ * 按判定给容器加/摘隐藏标记。
66
+ *
67
+ * 抽成独立函数是为了能脱离 DOM 单测:容器只需提供 `setAttribute` 与
68
+ * `removeAttribute`。
69
+ *
70
+ * @param {object | null | undefined} column - 主面板容器
71
+ * @param {boolean} hide - 是否隐藏
72
+ * @returns {void}
73
+ */
74
+ export function applyHideState(column, hide) {
75
+ if (column === null || column === undefined) return
76
+ if (hide) column.setAttribute(HIDE_ATTR, '')
77
+ else column.removeAttribute(HIDE_ATTR)
78
+ }
79
+
80
+ /**
81
+ * 观察主面板并维护隐藏标记。
82
+ *
83
+ * 切换会话时主面板节点可能被宿主替换:先对新节点加标记,再摘掉旧节点的标记,
84
+ * 避免旧 column 上留下跨会话的 `display:none`。卸载时断开 observer、清掉节流
85
+ * timer,并摘掉当前标记。
86
+ *
87
+ * @param {boolean} hide - 是否隐藏
88
+ * @param {object} [options] - 依赖注入(单测用)
89
+ * @param {Document} [options.documentRef] - 文档
90
+ * @param {() => Element | null} [options.findPanel] - 找真实主面板节点
91
+ * @param {(column: object | null | undefined, hide: boolean) => void} [options.apply] - 加/摘标记
92
+ * @param {typeof MutationObserver} [options.Observer] - MutationObserver 构造器
93
+ * @param {(callback: () => void) => unknown} [options.schedule] - 节流调度
94
+ * @param {(timer: unknown) => void} [options.cancel] - 取消节流
95
+ * @returns {() => void} 清理函数
96
+ */
97
+ export function watchLeaderChanges(hide, {
98
+ documentRef = typeof document === 'undefined' ? undefined : document,
99
+ findPanel = findMainPanel,
100
+ apply = applyHideState,
101
+ Observer = typeof MutationObserver === 'undefined' ? undefined : MutationObserver,
102
+ schedule = (callback) => setTimeout(callback, 16),
103
+ cancel = (timer) => clearTimeout(timer),
104
+ } = {}) {
105
+ let column = null
106
+ let scheduled = false
107
+ let timer = null
108
+ let alive = true
109
+
110
+ const applyNow = () => {
111
+ if (!alive || documentRef === undefined) return
112
+ const next = findPanel()
113
+ if (next === null) {
114
+ // 主面板标记被宿主改掉时也要摘掉旧标记,否则卡片会继续被隐藏。
115
+ apply(column, false)
116
+ column = null
117
+ return
118
+ }
119
+ if (next !== column) {
120
+ apply(column, false)
121
+ column = next
122
+ }
123
+ apply(column, hide)
124
+ }
125
+ applyNow()
126
+
127
+ const observer = hide && documentRef?.body !== undefined && Observer !== undefined
128
+ ? new Observer(() => {
129
+ if (scheduled) return
130
+ scheduled = true
131
+ timer = schedule(() => {
132
+ scheduled = false
133
+ applyNow()
134
+ })
135
+ })
136
+ : null
137
+ observer?.observe(documentRef.body, {
138
+ childList: true,
139
+ subtree: true,
140
+ attributes: true,
141
+ attributeFilter: ['class'],
142
+ })
143
+
144
+ return () => {
145
+ alive = false
146
+ observer?.disconnect()
147
+ if (timer !== null) cancel(timer)
148
+ apply(column, false)
149
+ }
150
+ }
@@ -69,7 +69,8 @@ export function createLeaderSessionIndex() {
69
69
  const nextIds = new Set(next)
70
70
  if (nextIds.size === ids.size && [...nextIds].every((id) => ids.has(id))) return
71
71
  ids = nextIds
72
- for (const listener of [...listeners]) listener(ids)
72
+ // 先快照:通知期间订阅者可能订阅或退订,实时迭代会让本轮多叫一次。
73
+ for (const listener of Array.from(listeners)) listener(ids)
73
74
  },
74
75
  }
75
76
  }
@@ -12,7 +12,7 @@
12
12
  * @module dsh-leader-agent/client/leader-view
13
13
  */
14
14
 
15
- import { createElement, useLayoutEffect } from 'react'
15
+ import { useLayoutEffect } from 'react'
16
16
  import {
17
17
  activateLeaderView,
18
18
  createLeaderViewDefinition,