@wangyf397/session_del 1.1.0 → 1.1.1

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 (2) hide show
  1. package/client.js +53 -49
  2. package/package.json +1 -1
package/client.js CHANGED
@@ -7,8 +7,9 @@
7
7
  * 删除动作通过 ctx.remote.commands.execute(sessionId, "/session-del delete <id>")
8
8
  * 交给 Host 的 /session-del 命令完成——UI 与 Agent 工具共用同一套操作。
9
9
  *
10
- * 不引用任何 @deepseek-ai 客户端包,也不写全局 DOM/样式:视觉全部用内联样式
11
- * 和主题 token(--dsw-alias-*),把出错面压到最小。
10
+ * 本组件只依赖 React 和运行时服务的惰性读取(与本 profile 里可用的
11
+ * thinking-dial 插件同一套写法),不依赖 slot 提供的 hook、不写全局 DOM/样式,
12
+ * 把出错面压到最小。任何缺服务/缺会话都会在行内显示原因,而不是静默失败。
12
13
  *
13
14
  * @module @wangyf397/session_del/client
14
15
  */
@@ -24,7 +25,7 @@ window.__ModuleLoader__.load({
24
25
 
25
26
  /** 中英文案。 */
26
27
  const zh = {
27
- 'menu.delete': '彻底删除会话',
28
+ 'menu.delete': '彻底删除会话(不可恢复)',
28
29
  'menu.confirm': '再点一次,彻底删除',
29
30
  'menu.busy': '正在删除…',
30
31
  'menu.done': '已彻底删除',
@@ -45,9 +46,34 @@ window.__ModuleLoader__.load({
45
46
  'error.unknownCommand': 'The Host has no /session-del command (is the plugin active?)',
46
47
  }
47
48
 
49
+ /** 当前 client 上下文(惰性读取服务,与 provider 顺序无关)。 */
50
+ let pluginCtx
51
+
52
+ /**
53
+ * 从一个 Cordis 上下文里读服务,服务尚未提供时返回 undefined 而不抛错。
54
+ * @param {object|undefined} host 上下文,apply 之前为 undefined。
55
+ * @param {string} name 服务名。
56
+ * @returns {*} 服务实例或 undefined。
57
+ */
58
+ function readService(host, name) {
59
+ if (host === undefined || host === null) return undefined
60
+ try {
61
+ const viaGet = host.get(name)
62
+ if (viaGet !== undefined) return viaGet
63
+ } catch {
64
+ /* 走属性形式 */
65
+ }
66
+ try {
67
+ return host[name]
68
+ } catch {
69
+ return undefined
70
+ }
71
+ }
72
+
48
73
  /** 当前主视图里的会话 id:命令需要一个 agent 作为执行者。 */
49
- function mainViewSessionId(state, excludeId) {
50
- const byId = state?.byId ?? {}
74
+ function mainViewSessionId(sessions, excludeId) {
75
+ const byId = sessions?.list?.getSnapshot?.()?.byId
76
+ if (!byId) return undefined
51
77
  for (const row of Object.values(byId)) {
52
78
  if (!row || String(row.id) === String(excludeId)) continue
53
79
  if ((row.retainedBy?.mainView ?? 0) > 0) return String(row.id)
@@ -95,52 +121,40 @@ window.__ModuleLoader__.load({
95
121
 
96
122
  /**
97
123
  * 侧栏会话菜单里的一行:idle → confirm → busy → done/failed。
98
- * @param {object} props sessionId / displayTitle / useMenuOpenState / useSessions / t
124
+ * @param {object} props sessionId / displayTitle / t
99
125
  */
100
126
  function DeleteMenuItem(props) {
101
- const { sessionId, displayTitle, useMenuOpenState, useSessions, t } = props
102
- const translate = typeof t === 'function' ? t : (key, vars) => (vars ? `${key} ${JSON.stringify(vars)}` : key)
103
- const [, setMenuOpen] = useMenuOpenState()
104
- const sessionState = useSessions((state) => state)
127
+ const { sessionId, displayTitle, t } = props
128
+ const id = String(sessionId ?? '')
129
+ const tt = typeof t === 'function' ? t : (key, vars) => (vars ? `${key} ${JSON.stringify(vars)}` : key)
105
130
 
106
131
  const [phase, setPhase] = React.useState('idle')
107
132
  const [reason, setReason] = React.useState('')
108
133
  const [hover, setHover] = React.useState(false)
109
134
  const [focused, setFocused] = React.useState(false)
110
- const timerRef = React.useRef(null)
111
135
 
112
- React.useEffect(
113
- () => () => {
114
- if (timerRef.current !== null) clearTimeout(timerRef.current)
115
- },
116
- []
117
- )
118
-
119
- /** 执行删除。 */
136
+ /** 执行删除(直接读已注入的 remote.commands 命名空间与 sessions 服务)。 */
120
137
  const run = async () => {
121
- const remote = ctxRef?.get?.('remote') ?? ctxRef?.remote
122
- if (!remote?.commands?.execute) {
138
+ const commands = readService(pluginCtx, 'remote.commands')
139
+ if (!commands || typeof commands.execute !== 'function') {
123
140
  setPhase('failed')
124
- setReason(translate('error.noRemote'))
141
+ setReason(tt('error.noRemote'))
125
142
  return
126
143
  }
127
- const runner = mainViewSessionId(sessionState, sessionId)
144
+ const sessions = readService(pluginCtx, 'sessions')
145
+ const runner = mainViewSessionId(sessions, id)
128
146
  if (!runner) {
129
147
  setPhase('failed')
130
- setReason(translate('error.noRunner'))
148
+ setReason(tt('error.noRunner'))
131
149
  return
132
150
  }
133
151
  setPhase('busy')
134
152
  try {
135
- const result = await remote.commands.execute(runner, `/session-del delete ${String(sessionId)}`, [])
136
- const outcome = readResult(result, translate)
153
+ const result = await commands.execute(runner, `/session-del delete ${id}`, [])
154
+ const outcome = readResult(result, tt)
137
155
  if (outcome.ok) {
138
156
  setPhase('done')
139
157
  setReason('')
140
- timerRef.current = setTimeout(() => {
141
- timerRef.current = null
142
- setMenuOpen(false)
143
- }, 1200)
144
158
  } else {
145
159
  setPhase('failed')
146
160
  setReason(outcome.text || 'error')
@@ -153,14 +167,14 @@ window.__ModuleLoader__.load({
153
167
 
154
168
  const label =
155
169
  phase === 'confirm'
156
- ? translate('menu.confirm')
170
+ ? tt('menu.confirm')
157
171
  : phase === 'busy'
158
- ? translate('menu.busy')
172
+ ? tt('menu.busy')
159
173
  : phase === 'done'
160
- ? translate('menu.done')
174
+ ? tt('menu.done')
161
175
  : phase === 'failed'
162
- ? translate('menu.failed', { reason })
163
- : translate('menu.delete')
176
+ ? tt('menu.failed', { reason })
177
+ : tt('menu.delete')
164
178
 
165
179
  const danger = phase === 'confirm' || phase === 'failed'
166
180
  const busy = phase === 'busy'
@@ -190,13 +204,6 @@ window.__ModuleLoader__.load({
190
204
  outlineOffset: focused ? -2 : 0,
191
205
  }
192
206
 
193
- const labelStyle = {
194
- minWidth: 0,
195
- overflow: 'hidden',
196
- whiteSpace: 'nowrap',
197
- textOverflow: 'ellipsis',
198
- }
199
-
200
207
  return h(
201
208
  'button',
202
209
  {
@@ -205,7 +212,7 @@ window.__ModuleLoader__.load({
205
212
  style,
206
213
  title: [displayTitle, phase === 'failed' ? reason : undefined].filter(Boolean).join(' — ') || undefined,
207
214
  disabled: busy,
208
- 'aria-label': translate('menu.delete'),
215
+ 'aria-label': tt('menu.delete'),
209
216
  onMouseEnter: () => setHover(true),
210
217
  onMouseLeave: () => setHover(false),
211
218
  onFocus: () => setFocused(true),
@@ -221,17 +228,14 @@ window.__ModuleLoader__.load({
221
228
  },
222
229
  },
223
230
  h('span', { key: 'icon', style: { display: 'inline-flex', flex: 'none', width: 14, height: 14 } }, TrashIcon()),
224
- h('span', { key: 'label', style: labelStyle }, label)
231
+ h('span', { key: 'label', style: { minWidth: 0, overflow: 'hidden', whiteSpace: 'nowrap', textOverflow: 'ellipsis' } }, label)
225
232
  )
226
233
  }
227
234
 
228
- /** 当前 client 上下文(供点击时按需取用 ctx.remote)。 */
229
- let ctxRef = null
230
-
231
235
  return {
232
- inject: ['slots', 'locale'],
236
+ inject: ['slots', 'locale', 'remote', 'remote.commands', 'sessions'],
233
237
  apply(ctx) {
234
- ctxRef = ctx
238
+ pluginCtx = ctx
235
239
  ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'session_del: dictionaries')
236
240
 
237
241
  ctx.slots.inject('sidebar.workspaces.session.menu.item', () =>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wangyf397/session_del",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "彻底删除 DeepSeek Harness 历史对话:侧栏会话「…」菜单 + /session-del 命令 + session_del 工具,删除会话日志、投影缓存与归档/置顶记录(不是归档隐藏)。",
5
5
  "type": "module",
6
6
  "main": "index.js",