dsh-chat-groups 0.0.0-stage → 0.1.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/README.md +31 -2
- package/client.js +568 -0
- package/cordis.patch.yml +3 -0
- package/groups.js +171 -0
- package/host.js +142 -0
- package/package.json +45 -4
package/README.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-chat-groups
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
在 DeepSeek Harness(DSH)工作区内按任务整理会话。分组由 Host 保存。会话标题、归档和对话内容仍由 DSH 管理。
|
|
4
|
+
|
|
5
|
+
## 安装与停用
|
|
6
|
+
|
|
7
|
+
已在 macOS 的 DSH Web `0.2.2-rc.5` 验证。可在仓库根目录或插件目录运行以下命令,将 Bundle 安装到 `web` profile。
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
dsh plugin --profile web add "$(git rev-parse --show-toplevel)/dsh-chat-groups"
|
|
11
|
+
dsh web
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
启动后,展开左侧栏,点工作区工具栏「添加工作区」右侧的「切换到会话分组视图」图标。点「返回默认视图」可回到原生工作区列表。要停用整个 Bundle,在 DSH 的「插件」页面关闭 `dsh-chat-groups`。停用会恢复原生侧栏并保留分组数据。启用了配置热重载的 profile 会立即应用停用,其他 profile 在重启后应用。
|
|
15
|
+
|
|
16
|
+
## 使用
|
|
17
|
+
|
|
18
|
+
将鼠标移到工作区、会话或分组行上,即可显示该行的操作图标。
|
|
19
|
+
|
|
20
|
+
- 点工作区行末的「新增分组」图标。可以折叠、重命名或删除分组。确认删除后,成员会回到工作区直属列表,会话仍可打开。
|
|
21
|
+
- 点会话行末的「调整分组」图标加入分组、移到同一工作区的其他组,或移回直属列表。工作区和会话各自保留原生操作。点击会话会打开 DSH 原生对话。
|
|
22
|
+
- 点组旁的「新建会话」图标创建原生会话。工作区挂接或分组归属保存失败时,可点「重试保存」图标。刷新页面后重新进入分组视图,仍可继续处理同一条会话。
|
|
23
|
+
|
|
24
|
+
第一版的分组功能向 DSH Host 所有者开放。归档会话可以在默认视图里管理。
|
|
25
|
+
|
|
26
|
+
## 验收
|
|
27
|
+
|
|
28
|
+
运行 `npm --prefix dsh-chat-groups test` 执行 10 项数据规则测试。
|
|
29
|
+
|
|
30
|
+
在隔离的 DSH Web `0.2.2-rc.5` profile 安装真实 Bundle 后,验证了侧栏往返、会话加入/移动/移出、组内新建、取消和确认删除非空组、跨工作区拒绝、归档与失效会话过滤、Host 重启后保留分组,以及停用后恢复原生列表和搜索快捷键并保留当前会话。另模拟一次原生工作区挂接失败,确认刷新后仍能用原会话 ID 完成重试。分组归属写入失败由单元测试覆盖。Desktop 图形界面尚未单独验收。
|
|
31
|
+
|
|
32
|
+
[初版离线交互原型](docs/prototype.html)可直接在浏览器中打开,用于查看分组操作。
|
package/client.js
ADDED
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
// DSH's browser loader supplies React from the application runtime.
|
|
2
|
+
window.__ModuleLoader__.load({
|
|
3
|
+
id: 'dsh-chat-groups',
|
|
4
|
+
factory(require) {
|
|
5
|
+
const { createElement: h, Fragment, useCallback, useEffect, useRef, useState, useSyncExternalStore } = require('react')
|
|
6
|
+
const { createPortal } = require('react-dom')
|
|
7
|
+
const {
|
|
8
|
+
Button, Modal, Tooltip, IconEllipsisOutlineRegular, IconFlatListOutlineRegular,
|
|
9
|
+
IconFolderCloseRegular, IconFolderOpenRegular,
|
|
10
|
+
IconNewChatOutlineRegular, IconProjectAddOutlineRegular, IconWorkspaceTreeOutlineRegular,
|
|
11
|
+
} = require('@deepseek-ai/dsh-client-ui-primitives')
|
|
12
|
+
const slot = 'sidebar.workspaces'
|
|
13
|
+
const sessionActionSlot = 'sidebar.workspaces.session.row.action'
|
|
14
|
+
const toolbarMount = 'shell.overlay'
|
|
15
|
+
const workspaceHeader = '[data-sidebar-workspace-header]'
|
|
16
|
+
const switchLabel = '切换到会话分组视图'
|
|
17
|
+
const returnLabel = '返回默认视图'
|
|
18
|
+
const toolbarStyle = `
|
|
19
|
+
.dsh-chat-groups-toggle {
|
|
20
|
+
display: inline-flex; flex: none; align-items: center; justify-content: center;
|
|
21
|
+
width: 28px; height: 28px; border: 0; padding: 0; border-radius: var(--dsw-radius-sm, 6px);
|
|
22
|
+
background: transparent; color: var(--dsw-alias-label-secondary, currentColor); cursor: pointer;
|
|
23
|
+
}
|
|
24
|
+
.dsh-chat-groups-toggle:hover { background: var(--dsw-alias-interactive-bg-hover, #8882); }
|
|
25
|
+
.dsh-chat-groups-toggle:focus-visible {
|
|
26
|
+
outline: 2px solid var(--dsw-focus-ring-color, #5d79cc); outline-offset: -2px;
|
|
27
|
+
}
|
|
28
|
+
.dsh-chat-groups-toggle:disabled { opacity: .55; cursor: default; }
|
|
29
|
+
${workspaceHeader}:not(:has(input)) > .dsh-chat-groups-toggle,
|
|
30
|
+
${workspaceHeader}:has(input[tabindex="0"]) > .dsh-chat-groups-toggle { display: none; }
|
|
31
|
+
`
|
|
32
|
+
const style = `
|
|
33
|
+
.dsh-chat-groups { display: flex; flex-direction: column; max-height: 100cqh; min-height: 0; font-size: 13px; }
|
|
34
|
+
.dsh-chat-groups-list { min-height: 0; overflow-y: auto; padding: 0 6px 8px; }
|
|
35
|
+
.dsh-chat-groups-heading, .dsh-chat-groups-row { display: flex; gap: 3px; align-items: center; min-width: 0; }
|
|
36
|
+
.dsh-chat-groups-heading { justify-content: space-between; padding: 4px 8px; }
|
|
37
|
+
.dsh-chat-groups-branch { margin-left: 10px; padding-left: 5px; border-inline-start: 1px solid var(--dsw-alias-interactive-bg-hover, #8885); }
|
|
38
|
+
.dsh-chat-groups-label, .dsh-chat-groups-icon {
|
|
39
|
+
font: inherit; color: inherit; background: transparent; border: 0;
|
|
40
|
+
border-radius: var(--dsw-radius-xs, 6px); cursor: pointer;
|
|
41
|
+
}
|
|
42
|
+
.dsh-chat-groups-label { padding: 5px 6px; text-align: start; }
|
|
43
|
+
.dsh-chat-groups-label:hover, .dsh-chat-groups-icon:hover { background: var(--dsw-alias-interactive-bg-hover, #8882); }
|
|
44
|
+
.dsh-chat-groups-label:focus-visible, .dsh-chat-groups-icon:focus-visible { outline: 2px solid var(--dsw-focus-ring-color, #5d79cc); outline-offset: -2px; }
|
|
45
|
+
.dsh-chat-groups-icon:disabled, .dsh-chat-groups-label:disabled { opacity: .55; cursor: default; }
|
|
46
|
+
.dsh-chat-groups-row > .dsh-chat-groups-label { flex: 1; min-width: 0; display: flex; align-items: center; gap: 5px; }
|
|
47
|
+
.dsh-chat-groups-label > svg { flex: none; }
|
|
48
|
+
.dsh-chat-groups-label > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
49
|
+
.dsh-chat-groups-actions { flex: none; display: flex; align-items: center; gap: 5px; padding-inline-end: 3px; }
|
|
50
|
+
.dsh-chat-groups-icon { flex: none; display: inline-flex; width: 24px; height: 24px; align-items: center; justify-content: center; padding: 0; color: var(--dsw-alias-label-tertiary); }
|
|
51
|
+
.dsh-chat-groups-icon svg { width: 14px; height: 14px; }
|
|
52
|
+
.dsh-chat-groups-native-actions { display: contents; }
|
|
53
|
+
.dsh-chat-groups-row[aria-current=true] { border-radius: 6px; background: var(--dsw-alias-interactive-bg-hover, #8883); }
|
|
54
|
+
.dsh-chat-groups-note { margin: 3px 8px; opacity: .8; overflow-wrap: anywhere; }
|
|
55
|
+
@media (hover: hover) {
|
|
56
|
+
.dsh-chat-groups-row > .dsh-chat-groups-actions { display: none; }
|
|
57
|
+
.dsh-chat-groups-row:is(:hover, :has(button[aria-haspopup=menu][aria-expanded=true])) > .dsh-chat-groups-actions,
|
|
58
|
+
html[data-input-modality='keyboard'] .dsh-chat-groups-row:focus-within > .dsh-chat-groups-actions { display: flex; }
|
|
59
|
+
}
|
|
60
|
+
.dsh-chat-groups-field {
|
|
61
|
+
box-sizing: border-box; width: 100%; height: 44px; padding: 7px 14px;
|
|
62
|
+
border: 0.5px solid var(--dsw-alias-border-l4); border-radius: var(--dsw-radius-lg);
|
|
63
|
+
background: transparent; color: var(--dsw-alias-label-primary); font: inherit;
|
|
64
|
+
font-size: 14px; line-height: 22px;
|
|
65
|
+
}
|
|
66
|
+
.dsh-chat-groups-field:focus { outline: 1px solid var(--dsw-focus-ring-color, currentColor); }
|
|
67
|
+
.dsh-chat-groups-menu { display: grid; gap: 8px; }
|
|
68
|
+
.dsh-chat-groups-error { margin-top: 8px; font-size: 12px; color: var(--dsw-alias-state-error-primary); }
|
|
69
|
+
.dsh-chat-groups-danger:not(:disabled) { color: var(--dsw-alias-state-error-primary); }
|
|
70
|
+
`
|
|
71
|
+
|
|
72
|
+
function useSource(source) {
|
|
73
|
+
return useSyncExternalStore(
|
|
74
|
+
useCallback(listener => source.subscribe(listener), [source]),
|
|
75
|
+
useCallback(() => source.getSnapshot(), [source]),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
const currentId = catalog => Object.values(catalog.byId)
|
|
79
|
+
.find(session => (session.retainedBy.mainView ?? 0) > 0)?.id
|
|
80
|
+
const message = error => error instanceof Error ? error.message : String(error)
|
|
81
|
+
const button = (label, onClick, attributes = {}) => h('button', {
|
|
82
|
+
type: 'button', className: 'dsh-chat-groups-label', onClick, ...attributes,
|
|
83
|
+
}, label)
|
|
84
|
+
|
|
85
|
+
function iconButton(Icon, label, onClick, disabled) {
|
|
86
|
+
return h(Tooltip, { label, side: 'bottom', align: 'end', delayMs: 500, portal: true },
|
|
87
|
+
h('button', {
|
|
88
|
+
type: 'button', className: 'dsh-chat-groups-icon', 'aria-label': label, onClick, disabled,
|
|
89
|
+
}, h(Icon, { size: 14, 'aria-hidden': true })))
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function RetryIcon() {
|
|
93
|
+
return h('svg', {
|
|
94
|
+
viewBox: '0 0 16 16', fill: 'none', stroke: 'currentColor',
|
|
95
|
+
strokeWidth: 1.3, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true,
|
|
96
|
+
}, h('path', { d: 'M13 6a5 5 0 1 0 .3 3M13 2.5V6H9.5' }))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function Dialog({ dialog, pending, error, close, submit, t }) {
|
|
100
|
+
const [value, setValue] = useState(dialog.input?.value ?? dialog.choices?.[0]?.[0] ?? '')
|
|
101
|
+
const blocked = pending || (dialog.input && (!value.trim() || dialog.input.blocked?.(value.trim())))
|
|
102
|
+
const field = {
|
|
103
|
+
className: 'dsh-chat-groups-field', 'data-modal-autofocus': true, value, disabled: pending,
|
|
104
|
+
onChange: event => setValue(event.target.value),
|
|
105
|
+
}
|
|
106
|
+
function confirm() {
|
|
107
|
+
if (!blocked) submit(value)
|
|
108
|
+
}
|
|
109
|
+
return h(Modal, {
|
|
110
|
+
open: true, onClose: () => { if (!pending) close() }, closeLabel: t('close'),
|
|
111
|
+
title: dialog.title, description: dialog.description,
|
|
112
|
+
footer: h(Fragment, null,
|
|
113
|
+
h(Button, { variant: 'outline', disabled: pending, onClick: close }, t(dialog.submit ? 'cancel' : 'close')),
|
|
114
|
+
dialog.submit && h(Button, {
|
|
115
|
+
variant: dialog.danger ? 'outline' : 'primary',
|
|
116
|
+
className: dialog.danger ? 'dsh-chat-groups-danger' : undefined,
|
|
117
|
+
disabled: !!blocked, onClick: confirm,
|
|
118
|
+
}, dialog.submit)),
|
|
119
|
+
}, dialog.input && h('input', {
|
|
120
|
+
...field, 'aria-label': dialog.input.label ?? '分组名称', maxLength: dialog.input.maxLength,
|
|
121
|
+
onFocus: event => event.currentTarget.select(),
|
|
122
|
+
onKeyDown(event) {
|
|
123
|
+
if (event.key === 'Enter' && !event.nativeEvent.isComposing && event.keyCode !== 229) {
|
|
124
|
+
event.preventDefault()
|
|
125
|
+
confirm()
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
}),
|
|
129
|
+
dialog.choices && h('select', { ...field, 'aria-label': '选择分组' },
|
|
130
|
+
dialog.choices.map(([id, name]) => h('option', { key: id, value: id }, name))),
|
|
131
|
+
dialog.actions && h('div', { className: 'dsh-chat-groups-menu' },
|
|
132
|
+
dialog.actions.map(([label, run]) => h(Button, { key: label, variant: 'outline', onClick: run }, label))),
|
|
133
|
+
error && h('div', { className: 'dsh-chat-groups-error', role: 'alert' }, error))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function apply(ctx) {
|
|
137
|
+
const sessions = ctx.get('sessions')
|
|
138
|
+
const workspaces = ctx.get('workspaces')
|
|
139
|
+
const connection = ctx.get('connection')
|
|
140
|
+
const uiWorkspace = ctx.get('uiWorkspace')
|
|
141
|
+
let stopView
|
|
142
|
+
let originWorkspace
|
|
143
|
+
let nativeWorkspace
|
|
144
|
+
let nativeSessionActions
|
|
145
|
+
|
|
146
|
+
async function rpc(method, args, signal) {
|
|
147
|
+
const result = await connection.rpc.call('/api', `chatGroups/${method}`, { args }, signal)
|
|
148
|
+
if (!result.ok) throw new Error(result.error.message)
|
|
149
|
+
return result.value
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function exit() {
|
|
153
|
+
stopView?.()
|
|
154
|
+
stopView = undefined
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function WorkspaceToolbarButton() {
|
|
158
|
+
const { items } = useSource(workspaces.list)
|
|
159
|
+
const [container, setContainer] = useState(null)
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
// rc.5 exposes this toolbar marker but has no slot for trailing actions.
|
|
162
|
+
// The native toolbar remounts when the grouped view releases the sidebar slot.
|
|
163
|
+
let header = null
|
|
164
|
+
function findHeader() {
|
|
165
|
+
if (header?.isConnected) return
|
|
166
|
+
const next = document.querySelector(workspaceHeader)
|
|
167
|
+
if (next === header) return
|
|
168
|
+
header = next
|
|
169
|
+
setContainer(header)
|
|
170
|
+
}
|
|
171
|
+
const observer = new MutationObserver(findHeader)
|
|
172
|
+
observer.observe(document.body, { childList: true, subtree: true })
|
|
173
|
+
findHeader()
|
|
174
|
+
return () => observer.disconnect()
|
|
175
|
+
}, [])
|
|
176
|
+
if (container === null) return null
|
|
177
|
+
return createPortal([
|
|
178
|
+
h('style', { key: 'style' }, toolbarStyle),
|
|
179
|
+
h('button', {
|
|
180
|
+
key: 'toggle', type: 'button', className: 'dsh-chat-groups-toggle',
|
|
181
|
+
title: switchLabel, 'aria-label': switchLabel, disabled: items.length === 0,
|
|
182
|
+
onClick() {
|
|
183
|
+
try {
|
|
184
|
+
enter()
|
|
185
|
+
} catch (error) {
|
|
186
|
+
ctx.contextMenus.notify('无法切换到会话分组视图。')
|
|
187
|
+
console.warn('dsh-chat-groups: unable to replace the workspace sidebar', error)
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
}, h('svg', {
|
|
191
|
+
width: 16, height: 16, viewBox: '0 0 16 16', fill: 'none',
|
|
192
|
+
stroke: 'currentColor', strokeWidth: 1.25, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
193
|
+
'aria-hidden': true,
|
|
194
|
+
}, h('path', { d: 'M8 2.5 14 5.5 8 8.5 2 5.5Z M2 8l6 3 6-3 M2 10.5l6 3 6-3' }))),
|
|
195
|
+
], container)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function NativeWorkspaceActions({ target, renderFactorySlot, useWorkspaceAccess, t }) {
|
|
199
|
+
const { workspaceId, displayTitle } = target.value
|
|
200
|
+
const canCreate = useWorkspaceAccess(workspaceId, 'session/create')
|
|
201
|
+
return h('span', { className: 'dsh-chat-groups-native-actions' },
|
|
202
|
+
renderFactorySlot('contextMenu.button', {
|
|
203
|
+
target, label: t('actions.workspace.aria', { name: displayTitle }), className: 'dsh-chat-groups-icon',
|
|
204
|
+
}),
|
|
205
|
+
canCreate && iconButton(IconNewChatOutlineRegular,
|
|
206
|
+
t('actions.newSession.aria', { name: displayTitle }), () => nativeWorkspace.startSession(workspaceId)),
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function GroupedSidebar({
|
|
211
|
+
wide, expandSidebar, useSectionScroller, useContextMenu, useSessionAccess, useWorkspaceAccess,
|
|
212
|
+
renderFactorySlot, t,
|
|
213
|
+
}) {
|
|
214
|
+
const workspaceList = useSource(workspaces.list)
|
|
215
|
+
const catalog = useSource(sessions.list)
|
|
216
|
+
const generation = useSource(connection.generation)
|
|
217
|
+
const workspaceAction = useSource(nativeWorkspace.hooks.workspaceActionRequest)
|
|
218
|
+
const { bindScrollerRef } = useSectionScroller()
|
|
219
|
+
const { getTriggerProps } = useContextMenu()
|
|
220
|
+
const [data, setData] = useState(null)
|
|
221
|
+
const [collapsedWorkspaces, setCollapsedWorkspaces] = useState(() => new Set(
|
|
222
|
+
workspaces.list.getSnapshot().items
|
|
223
|
+
.filter(workspace => workspace.workspaceId !== originWorkspace)
|
|
224
|
+
.map(workspace => workspace.workspaceId),
|
|
225
|
+
))
|
|
226
|
+
const [collapsedGroups, setCollapsedGroups] = useState(() => new Set())
|
|
227
|
+
const [groupDialog, setDialog] = useState(null)
|
|
228
|
+
const [dialogError, setDialogError] = useState('')
|
|
229
|
+
const [status, setStatus] = useState('')
|
|
230
|
+
const [pending, setPending] = useState(false)
|
|
231
|
+
const operation = useRef(false)
|
|
232
|
+
const revision = useRef(0)
|
|
233
|
+
const lifetime = useRef(null)
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
const controller = new AbortController()
|
|
236
|
+
lifetime.current = controller
|
|
237
|
+
return () => controller.abort()
|
|
238
|
+
}, [])
|
|
239
|
+
useEffect(() => {
|
|
240
|
+
if (generation === undefined) return
|
|
241
|
+
const controller = new AbortController()
|
|
242
|
+
async function refresh() {
|
|
243
|
+
if (operation.current) return
|
|
244
|
+
const version = ++revision.current
|
|
245
|
+
try {
|
|
246
|
+
const snapshot = await rpc('snapshot', {}, controller.signal)
|
|
247
|
+
if (!controller.signal.aborted && version === revision.current) {
|
|
248
|
+
setData(snapshot)
|
|
249
|
+
setStatus('')
|
|
250
|
+
}
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (!controller.signal.aborted && version === revision.current) setStatus(message(error))
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
void refresh()
|
|
256
|
+
window.addEventListener('focus', refresh)
|
|
257
|
+
return () => {
|
|
258
|
+
controller.abort()
|
|
259
|
+
window.removeEventListener('focus', refresh)
|
|
260
|
+
}
|
|
261
|
+
}, [generation])
|
|
262
|
+
|
|
263
|
+
const active = currentId(catalog)
|
|
264
|
+
const listed = new Set(catalog.ids)
|
|
265
|
+
const archived = new Set(workspaceList.archivedSessionIds)
|
|
266
|
+
const groupBySession = data?.membership ?? {}
|
|
267
|
+
const shown = new Set()
|
|
268
|
+
const allGroups = Object.entries(data?.groups ?? {})
|
|
269
|
+
const groupsByWorkspace = new Map()
|
|
270
|
+
for (const entry of allGroups) {
|
|
271
|
+
const workspaceId = entry[1].workspaceId
|
|
272
|
+
if (!groupsByWorkspace.has(workspaceId)) groupsByWorkspace.set(workspaceId, [])
|
|
273
|
+
groupsByWorkspace.get(workspaceId).push(entry)
|
|
274
|
+
}
|
|
275
|
+
const openDialog = next => {
|
|
276
|
+
setDialogError('')
|
|
277
|
+
setDialog(next)
|
|
278
|
+
}
|
|
279
|
+
const workspaceLabel = title => title === 'default-workspace' ? t('workspace.defaultName') : title
|
|
280
|
+
const sessionLabel = session => session.blank ? t('session.new') : (session.title?.trim() || t('session.untitled'))
|
|
281
|
+
const dialog = workspaceAction === null ? groupDialog : workspaceActionDialog()
|
|
282
|
+
function closeDialog() {
|
|
283
|
+
if (workspaceAction === null) setDialog(null)
|
|
284
|
+
else nativeWorkspace.settleWorkspaceAction()
|
|
285
|
+
setDialogError('')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function workspaceActionDialog() {
|
|
289
|
+
const { action, workspaceId, displayTitle, storedTitle } = workspaceAction
|
|
290
|
+
const key = `workspace:${action}:${workspaceId}`
|
|
291
|
+
if (action === 'rename') return {
|
|
292
|
+
key, title: t('rename.workspace.title'), submit: t('rename'),
|
|
293
|
+
input: {
|
|
294
|
+
value: displayTitle, label: t('field.workspaceName'),
|
|
295
|
+
blocked: value => value === storedTitle || workspaceList.items.some(
|
|
296
|
+
item => item.workspaceId !== workspaceId && workspaceLabel(item.title) === value,
|
|
297
|
+
),
|
|
298
|
+
},
|
|
299
|
+
save: value => nativeWorkspace.renameWorkspace(workspaceId, value.trim()),
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
key, title: t('delete.workspace'), submit: t('delete.workspace'), danger: true,
|
|
303
|
+
description: t('delete.desc', { name: displayTitle }),
|
|
304
|
+
save: () => nativeWorkspace.deleteWorkspace(workspaceId),
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function viewFold(setter, id, forceOpen = false) {
|
|
309
|
+
setter(previous => {
|
|
310
|
+
const next = new Set(previous)
|
|
311
|
+
if (forceOpen || next.has(id)) next.delete(id)
|
|
312
|
+
else next.add(id)
|
|
313
|
+
return next
|
|
314
|
+
})
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function run(action, fromDialog = false) {
|
|
318
|
+
if (operation.current) return
|
|
319
|
+
const { signal } = lifetime.current
|
|
320
|
+
operation.current = true
|
|
321
|
+
revision.current += 1
|
|
322
|
+
setPending(true)
|
|
323
|
+
setStatus('')
|
|
324
|
+
setDialogError('')
|
|
325
|
+
try {
|
|
326
|
+
await action(signal)
|
|
327
|
+
if (!signal.aborted && fromDialog) closeDialog()
|
|
328
|
+
} catch (error) {
|
|
329
|
+
if (!signal.aborted) (fromDialog ? setDialogError : setStatus)(message(error))
|
|
330
|
+
} finally {
|
|
331
|
+
operation.current = false
|
|
332
|
+
if (!signal.aborted) setPending(false)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function change(request, signal) {
|
|
337
|
+
const result = await rpc('change', { request }, signal)
|
|
338
|
+
if (!signal.aborted) setData(result)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function assign(workspaceId, sessionId, groupId, signal) {
|
|
342
|
+
return change({ action: 'assign', workspaceId, sessionId, groupId }, signal)
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
function showName(workspace, groupId, group) {
|
|
346
|
+
openDialog({
|
|
347
|
+
key: groupId ?? workspace.workspaceId,
|
|
348
|
+
title: group ? '重命名分组' : `新增分组 · ${workspaceLabel(workspace.title)}`,
|
|
349
|
+
input: { value: group?.name ?? '', maxLength: 80 },
|
|
350
|
+
submit: '保存',
|
|
351
|
+
save: (name, signal) => change(group ? {
|
|
352
|
+
action: 'rename', workspaceId: workspace.workspaceId, groupId, name,
|
|
353
|
+
} : { action: 'create', workspaceId: workspace.workspaceId, name }, signal),
|
|
354
|
+
})
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function showGroupMenu(workspace, groupId, group, count) {
|
|
358
|
+
openDialog({ key: `group:${groupId}`, title: group.name, actions: [
|
|
359
|
+
['重命名分组', () => showName(workspace, groupId, group)],
|
|
360
|
+
['删除分组', () => openDialog({
|
|
361
|
+
key: `delete:${groupId}`, title: `删除「${group.name}」?`,
|
|
362
|
+
description: `组内可见的 ${count} 条会话将移回「${workspaceLabel(workspace.title)}」。删除分组会保留全部会话。`,
|
|
363
|
+
submit: '仅删除分组',
|
|
364
|
+
danger: true,
|
|
365
|
+
save: async (_, signal) => {
|
|
366
|
+
await change({ action: 'delete', workspaceId: workspace.workspaceId, groupId }, signal)
|
|
367
|
+
viewFold(setCollapsedGroups, groupId, true)
|
|
368
|
+
},
|
|
369
|
+
})],
|
|
370
|
+
] })
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function adjustSessionGroup(workspaceId, session, groupId, groups) {
|
|
374
|
+
const choices = groups.filter(([id]) => id !== groupId).map(([id, group]) => [id, group.name])
|
|
375
|
+
if (groupId !== undefined) choices.push(['', '移出分组,返回工作区'])
|
|
376
|
+
openDialog({
|
|
377
|
+
key: `assign:${session.id}`, title: '调整分组',
|
|
378
|
+
...(choices.length === 0 ? { description: '当前工作区暂无可选分组。' } : {
|
|
379
|
+
choices,
|
|
380
|
+
description: sessionLabel(session), submit: '确定',
|
|
381
|
+
save: async (id, signal) => {
|
|
382
|
+
await assign(workspaceId, session.id, id || null, signal)
|
|
383
|
+
if (id && !signal.aborted) viewFold(setCollapsedGroups, id, true)
|
|
384
|
+
},
|
|
385
|
+
}),
|
|
386
|
+
})
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function createInGroup(workspaceId, groupId, retrySessionId) {
|
|
390
|
+
void run(async signal => {
|
|
391
|
+
const before = currentId(sessions.list.getSnapshot())
|
|
392
|
+
let result
|
|
393
|
+
try {
|
|
394
|
+
result = await rpc('createSession', { request: { workspaceId, groupId, retrySessionId } }, signal)
|
|
395
|
+
} catch (error) {
|
|
396
|
+
const snapshot = await rpc('snapshot', {}, signal).catch(() => undefined)
|
|
397
|
+
if (!signal.aborted && snapshot !== undefined) {
|
|
398
|
+
setData(snapshot)
|
|
399
|
+
if (snapshot.pendingSessions[groupId] !== undefined) {
|
|
400
|
+
throw new Error(`会话还未加入分组,可用「重试保存」继续。 ${message(error)}`)
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
throw error
|
|
404
|
+
}
|
|
405
|
+
if (signal.aborted) return
|
|
406
|
+
setData(result.state)
|
|
407
|
+
viewFold(setCollapsedGroups, groupId, true)
|
|
408
|
+
if (currentId(sessions.list.getSnapshot()) === before) uiWorkspace.openSession(result.sessionId)
|
|
409
|
+
})
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function openSession(id) {
|
|
413
|
+
const latest = sessions.list.getSnapshot()
|
|
414
|
+
const currentWorkspaces = workspaces.list.getSnapshot()
|
|
415
|
+
if (!latest.ids.includes(id) || currentWorkspaces.archivedSessionIds.includes(id)
|
|
416
|
+
|| !currentWorkspaces.items.some(workspace => workspace.sessionIds.includes(id))) {
|
|
417
|
+
setStatus('会话当前不可打开。')
|
|
418
|
+
return
|
|
419
|
+
}
|
|
420
|
+
uiWorkspace.openSession(id)
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function sessionRow(workspaceId, session, groupId, groups) {
|
|
424
|
+
const displayTitle = sessionLabel(session)
|
|
425
|
+
const target = session.blank ? undefined : {
|
|
426
|
+
kind: 'session', value: { sessionId: session.id, displayTitle: session.title?.trim() ?? '' },
|
|
427
|
+
}
|
|
428
|
+
return h('div', {
|
|
429
|
+
key: session.id, className: 'dsh-chat-groups-row', 'data-session-id': session.id,
|
|
430
|
+
'aria-current': session.id === active ? 'true' : undefined, ...getTriggerProps(target),
|
|
431
|
+
},
|
|
432
|
+
button(h('span', null, displayTitle), () => openSession(session.id), { title: displayTitle }),
|
|
433
|
+
h('span', { className: 'dsh-chat-groups-actions' },
|
|
434
|
+
target && h('span', { className: 'dsh-chat-groups-native-actions' },
|
|
435
|
+
renderFactorySlot('contextMenu.button', {
|
|
436
|
+
target, label: t('actions.session.aria', { name: displayTitle }), className: 'dsh-chat-groups-icon',
|
|
437
|
+
}),
|
|
438
|
+
nativeSessionActions.map(({ key, Component, callbacks, hooks }) => h(Component, {
|
|
439
|
+
key, ...callbacks, t, useSessionAccess, sessionId: session.id, displayTitle: target.value.displayTitle,
|
|
440
|
+
useArchived: selector => selector(useSource(hooks.archived)),
|
|
441
|
+
usePinned: hooks.pinned && (selector => selector(useSource(hooks.pinned))),
|
|
442
|
+
})),
|
|
443
|
+
),
|
|
444
|
+
iconButton(IconWorkspaceTreeOutlineRegular, `调整「${displayTitle}」的分组`,
|
|
445
|
+
() => adjustSessionGroup(workspaceId, session, groupId, groups), pending || data === null),
|
|
446
|
+
))
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function workspaceRows(workspace) {
|
|
450
|
+
const id = workspace.workspaceId
|
|
451
|
+
const groups = groupsByWorkspace.get(id) ?? []
|
|
452
|
+
const workspaceGroupIds = new Set(groups.map(([groupId]) => groupId))
|
|
453
|
+
const members = new Map()
|
|
454
|
+
for (const sessionId of workspace.sessionIds) {
|
|
455
|
+
if (shown.has(sessionId) || archived.has(sessionId) || !listed.has(sessionId)) continue
|
|
456
|
+
const session = catalog.byId[sessionId]
|
|
457
|
+
if (!session || session.parentId || session.origin === 'subagent') continue
|
|
458
|
+
shown.add(sessionId)
|
|
459
|
+
const assigned = groupBySession[sessionId]
|
|
460
|
+
const groupId = workspaceGroupIds.has(assigned) ? assigned : undefined
|
|
461
|
+
if (!members.has(groupId)) members.set(groupId, [])
|
|
462
|
+
members.get(groupId).push(session)
|
|
463
|
+
}
|
|
464
|
+
const expanded = !collapsedWorkspaces.has(id)
|
|
465
|
+
const title = workspaceLabel(workspace.title)
|
|
466
|
+
const target = { kind: 'workspace', value: { workspaceId: id, path: workspace.path, displayTitle: title } }
|
|
467
|
+
return h('section', { key: id },
|
|
468
|
+
h('div', { className: 'dsh-chat-groups-row', ...getTriggerProps(target) },
|
|
469
|
+
button(h(Fragment, null,
|
|
470
|
+
h(expanded ? IconFolderOpenRegular : IconFolderCloseRegular, { size: 14, 'aria-hidden': true }),
|
|
471
|
+
h('span', null, title)),
|
|
472
|
+
() => viewFold(setCollapsedWorkspaces, id), { 'aria-expanded': expanded, title }),
|
|
473
|
+
h('span', { className: 'dsh-chat-groups-actions' },
|
|
474
|
+
h(NativeWorkspaceActions, { target, renderFactorySlot, useWorkspaceAccess, t }),
|
|
475
|
+
iconButton(IconProjectAddOutlineRegular, `在「${title}」中新增分组`,
|
|
476
|
+
() => showName(workspace), pending || data === null),
|
|
477
|
+
),
|
|
478
|
+
),
|
|
479
|
+
expanded && h('div', { className: 'dsh-chat-groups-branch' },
|
|
480
|
+
members.get(undefined)?.map(session => sessionRow(id, session, undefined, groups)),
|
|
481
|
+
groups.map(([groupId, group]) => {
|
|
482
|
+
const rows = members.get(groupId) ?? []
|
|
483
|
+
const open = !collapsedGroups.has(groupId)
|
|
484
|
+
const retrySessionId = data.pendingSessions[groupId]
|
|
485
|
+
return h('section', { key: groupId },
|
|
486
|
+
h('div', { className: 'dsh-chat-groups-row' },
|
|
487
|
+
button(h('span', null, `${open ? '▾' : '▸'} ${group.name}`), () => viewFold(setCollapsedGroups, groupId),
|
|
488
|
+
{ 'aria-expanded': open, title: group.name }),
|
|
489
|
+
h('span', { className: 'dsh-chat-groups-actions' },
|
|
490
|
+
iconButton(retrySessionId ? RetryIcon : IconNewChatOutlineRegular,
|
|
491
|
+
retrySessionId ? `重试保存到${group.name}` : `在${group.name}新建会话`,
|
|
492
|
+
() => createInGroup(id, groupId, retrySessionId), pending),
|
|
493
|
+
iconButton(IconEllipsisOutlineRegular, `${group.name}的操作`,
|
|
494
|
+
() => showGroupMenu(workspace, groupId, group, rows.length), pending),
|
|
495
|
+
),
|
|
496
|
+
),
|
|
497
|
+
open && h('div', { className: 'dsh-chat-groups-branch' }, rows.length
|
|
498
|
+
? rows.map(session => sessionRow(id, session, groupId, groups))
|
|
499
|
+
: h('p', { className: 'dsh-chat-groups-note' }, '暂无会话')),
|
|
500
|
+
)
|
|
501
|
+
}),
|
|
502
|
+
),
|
|
503
|
+
)
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return h('div', { className: 'dsh-chat-groups' },
|
|
507
|
+
h('style', null, style),
|
|
508
|
+
wide ? h('div', { className: 'dsh-chat-groups-heading' },
|
|
509
|
+
h('strong', null, '会话分组'), iconButton(IconFlatListOutlineRegular, returnLabel, exit, pending),
|
|
510
|
+
) : iconButton(IconWorkspaceTreeOutlineRegular, '展开会话分组', expandSidebar),
|
|
511
|
+
wide && h('nav', { ref: bindScrollerRef, className: 'dsh-chat-groups-list', 'aria-label': '工作区与会话分组' },
|
|
512
|
+
status && h('p', { className: 'dsh-chat-groups-note', role: 'status' }, status),
|
|
513
|
+
data === null && !status && h('p', { className: 'dsh-chat-groups-note', role: 'status' }, '正在加载会话分组…'),
|
|
514
|
+
workspaceList.items.map(workspaceRows),
|
|
515
|
+
),
|
|
516
|
+
dialog && h(Dialog, { key: dialog.key, dialog, pending, error: dialogError, t,
|
|
517
|
+
close: closeDialog,
|
|
518
|
+
submit: value => { if (dialog.save) void run(signal => dialog.save(value, signal), true) },
|
|
519
|
+
}),
|
|
520
|
+
)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function enter() {
|
|
524
|
+
if (stopView) return
|
|
525
|
+
const { items } = workspaces.list.getSnapshot()
|
|
526
|
+
const active = currentId(sessions.list.getSnapshot())
|
|
527
|
+
const workspace = items.find(item => item.sessionIds.includes(active)) ?? items[0]
|
|
528
|
+
if (workspace === undefined) return
|
|
529
|
+
// The original row actions and workspace dialog requests stay live
|
|
530
|
+
// while the grouped browser shadows the native sidebar registration.
|
|
531
|
+
nativeWorkspace = ctx.slots.entriesOfSlot(slot)[0]?.inject?.()
|
|
532
|
+
if (!nativeWorkspace?.hooks?.workspaceActionRequest) throw new Error('未找到 DSH 原生工作区操作。')
|
|
533
|
+
nativeSessionActions = ctx.slots.entriesOfSlot(sessionActionSlot)
|
|
534
|
+
.filter(entry => ['archive', 'pin'].includes(entry.options.id))
|
|
535
|
+
.sort((left, right) => left.options.order - right.options.order)
|
|
536
|
+
.map(({ component: Component, options, inject }) => {
|
|
537
|
+
const { hooks, ...callbacks } = inject()
|
|
538
|
+
return { Component, key: options.id, hooks, callbacks }
|
|
539
|
+
})
|
|
540
|
+
originWorkspace = workspace.workspaceId
|
|
541
|
+
const withdraw = ctx.slots.inject(slot,
|
|
542
|
+
() => ctx.slots.register({ name: slot, locale: 'workspace', priority: -10 }, GroupedSidebar))
|
|
543
|
+
if (ctx.slots.entriesOfSlot(slot)[0]?.component === GroupedSidebar) {
|
|
544
|
+
stopView = withdraw
|
|
545
|
+
} else {
|
|
546
|
+
withdraw()
|
|
547
|
+
throw new Error('会话分组视图无法接管侧栏。')
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
ctx.remoteAccess.whenAccess('chatGroups/snapshot', () => {
|
|
552
|
+
const unregister = ctx.slots.inject(toolbarMount, () => ctx.slots.register({
|
|
553
|
+
name: toolbarMount, id: 'chat-groups-workspace-toolbar',
|
|
554
|
+
}, WorkspaceToolbarButton))
|
|
555
|
+
return () => {
|
|
556
|
+
exit()
|
|
557
|
+
unregister()
|
|
558
|
+
}
|
|
559
|
+
})
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
return {
|
|
563
|
+
name: 'dsh-chat-groups',
|
|
564
|
+
inject: ['contextMenus', 'slots', 'sessions', 'workspaces', 'connection', 'uiWorkspace', 'remoteAccess'],
|
|
565
|
+
apply,
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
})
|
package/cordis.patch.yml
ADDED
package/groups.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
export class GroupError extends Error {
|
|
4
|
+
constructor(code, message) {
|
|
5
|
+
super(message)
|
|
6
|
+
this.code = `chat-groups/${code}`
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function emptyGroups() {
|
|
11
|
+
return { groups: {}, membership: {}, pendingSessions: {} }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function requireGroup(state, workspaceId, groupId) {
|
|
15
|
+
const group = Object.hasOwn(state.groups, groupId) ? state.groups[groupId] : undefined
|
|
16
|
+
if (group === undefined) throw new GroupError('group-missing', '分组已不存在。')
|
|
17
|
+
if (group.workspaceId !== workspaceId) {
|
|
18
|
+
throw new GroupError('workspace-mismatch', '只能在会话所属工作区内分组。')
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function updateGroups(state, request) {
|
|
23
|
+
const { action, workspaceId, groupId } = request
|
|
24
|
+
if (action === 'create') {
|
|
25
|
+
return {
|
|
26
|
+
...state,
|
|
27
|
+
groups: { ...state.groups, [randomUUID()]: { workspaceId, name: request.name } },
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (groupId !== null) requireGroup(state, workspaceId, groupId)
|
|
31
|
+
if (action === 'rename') {
|
|
32
|
+
return {
|
|
33
|
+
...state,
|
|
34
|
+
groups: { ...state.groups, [groupId]: { workspaceId, name: request.name } },
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (action === 'delete') {
|
|
38
|
+
const groups = { ...state.groups }
|
|
39
|
+
delete groups[groupId]
|
|
40
|
+
const membership = Object.fromEntries(
|
|
41
|
+
Object.entries(state.membership).filter(([, memberOf]) => memberOf !== groupId),
|
|
42
|
+
)
|
|
43
|
+
const pendingSessions = { ...state.pendingSessions }
|
|
44
|
+
delete pendingSessions[groupId]
|
|
45
|
+
return { groups, membership, pendingSessions }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { sessionId } = request
|
|
49
|
+
const membership = { ...state.membership }
|
|
50
|
+
if (groupId === null) {
|
|
51
|
+
const previous = Object.hasOwn(membership, sessionId) ? membership[sessionId] : undefined
|
|
52
|
+
if (previous !== undefined && state.groups[previous]?.workspaceId !== workspaceId) {
|
|
53
|
+
throw new GroupError('workspace-mismatch', '只能在会话所属工作区内移出分组。')
|
|
54
|
+
}
|
|
55
|
+
delete membership[sessionId]
|
|
56
|
+
} else {
|
|
57
|
+
membership[sessionId] = groupId
|
|
58
|
+
}
|
|
59
|
+
return { ...state, membership }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createGroupOperations({ read, write, workspace, available, archived, createNativeSession }) {
|
|
63
|
+
let writes = Promise.resolve()
|
|
64
|
+
function inOrder(operation) {
|
|
65
|
+
const task = writes.then(operation)
|
|
66
|
+
writes = task.then(() => {}, () => {})
|
|
67
|
+
return task
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function requireWorkspace(workspaceId) {
|
|
71
|
+
const target = workspace(workspaceId)
|
|
72
|
+
if (target === undefined) throw new GroupError('workspace-missing', '工作区已不存在。')
|
|
73
|
+
return target
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function requireMembership({ workspaceId, sessionId }) {
|
|
77
|
+
if (!requireWorkspace(workspaceId).sessionIds.includes(sessionId)) {
|
|
78
|
+
throw new GroupError('workspace-mismatch', '只能整理当前工作区的会话。')
|
|
79
|
+
}
|
|
80
|
+
if (archived(sessionId)) {
|
|
81
|
+
throw new GroupError('session-archived', '请先在默认视图中取消归档。')
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
snapshot() {
|
|
87
|
+
const state = read()
|
|
88
|
+
const groups = Object.fromEntries(Object.entries(state.groups).filter(
|
|
89
|
+
([, group]) => workspace(group.workspaceId) !== undefined,
|
|
90
|
+
))
|
|
91
|
+
const sessionsByWorkspace = new Map()
|
|
92
|
+
const membership = Object.fromEntries(Object.entries(state.membership).filter(([sessionId, groupId]) => {
|
|
93
|
+
if (!Object.hasOwn(groups, groupId)) return false
|
|
94
|
+
const { workspaceId } = groups[groupId]
|
|
95
|
+
if (!sessionsByWorkspace.has(workspaceId)) {
|
|
96
|
+
sessionsByWorkspace.set(workspaceId, new Set(workspace(workspaceId).sessionIds))
|
|
97
|
+
}
|
|
98
|
+
return sessionsByWorkspace.get(workspaceId).has(sessionId)
|
|
99
|
+
}))
|
|
100
|
+
const pendingSessions = Object.fromEntries(Object.entries(state.pendingSessions).filter(
|
|
101
|
+
([groupId]) => Object.hasOwn(groups, groupId),
|
|
102
|
+
))
|
|
103
|
+
return { groups, membership, pendingSessions }
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
change(request) {
|
|
107
|
+
return inOrder(async () => {
|
|
108
|
+
if (request.action === 'assign') {
|
|
109
|
+
requireMembership(request)
|
|
110
|
+
if (!await available(request.sessionId)) {
|
|
111
|
+
throw new GroupError('session-missing', '会话已不存在。')
|
|
112
|
+
}
|
|
113
|
+
requireMembership(request)
|
|
114
|
+
} else {
|
|
115
|
+
requireWorkspace(request.workspaceId)
|
|
116
|
+
}
|
|
117
|
+
await write(updateGroups(read(), request))
|
|
118
|
+
return this.snapshot()
|
|
119
|
+
})
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
createSession({ workspaceId, groupId, retrySessionId }) {
|
|
123
|
+
return inOrder(async () => {
|
|
124
|
+
let state = read()
|
|
125
|
+
const target = requireWorkspace(workspaceId)
|
|
126
|
+
requireGroup(state, workspaceId, groupId)
|
|
127
|
+
|
|
128
|
+
function reserve(sessionId) {
|
|
129
|
+
state = { ...state, pendingSessions: { ...state.pendingSessions, [groupId]: sessionId } }
|
|
130
|
+
return write(state)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let sessionId = state.pendingSessions[groupId]
|
|
134
|
+
if (retrySessionId !== undefined && retrySessionId !== sessionId) {
|
|
135
|
+
if (state.membership[retrySessionId] !== groupId) {
|
|
136
|
+
throw new GroupError('retry-stale', '该会话的归属已变化,请重新加载分组。')
|
|
137
|
+
}
|
|
138
|
+
if (!await available(retrySessionId)) throw new GroupError('session-missing', '会话已不存在。')
|
|
139
|
+
requireMembership({ workspaceId, sessionId: retrySessionId })
|
|
140
|
+
return { state: this.snapshot(), sessionId: retrySessionId }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Persist the requested native ID before the first call. A failed
|
|
144
|
+
// workspace attachment can then be retried without calling create.
|
|
145
|
+
if (sessionId === undefined) {
|
|
146
|
+
sessionId = `session-${randomUUID()}`
|
|
147
|
+
await reserve(sessionId)
|
|
148
|
+
}
|
|
149
|
+
if (!await available(sessionId)) {
|
|
150
|
+
const created = await createNativeSession({ workspaceId, sessionId })
|
|
151
|
+
if (created.sessionId !== sessionId) {
|
|
152
|
+
sessionId = created.sessionId
|
|
153
|
+
await reserve(sessionId)
|
|
154
|
+
}
|
|
155
|
+
if (created.attachmentError !== undefined) throw created.attachmentError
|
|
156
|
+
} else if (!target.sessionIds.includes(sessionId)) {
|
|
157
|
+
if (archived(sessionId)) throw new GroupError('session-archived', '请先在默认视图中取消归档。')
|
|
158
|
+
await target.attachSession(sessionId)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const request = { action: 'assign', workspaceId, groupId, sessionId }
|
|
162
|
+
requireMembership(request)
|
|
163
|
+
const pendingSessions = { ...state.pendingSessions }
|
|
164
|
+
delete pendingSessions[groupId]
|
|
165
|
+
const updated = updateGroups({ ...state, pendingSessions }, request)
|
|
166
|
+
await write(updated)
|
|
167
|
+
return { state: this.snapshot(), sessionId }
|
|
168
|
+
})
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
package/host.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Service } from '@deepseek-ai/cordis'
|
|
2
|
+
import { defineDomain, domainTable } from '@deepseek-ai/dsh-storage-domain'
|
|
3
|
+
import { RemoteError, bindTypertRemote, remoteErrorOf } from '@deepseek-ai/dsh-typert-protocol'
|
|
4
|
+
import { z } from 'zod'
|
|
5
|
+
import { createGroupOperations, emptyGroups, GroupError } from './groups.js'
|
|
6
|
+
|
|
7
|
+
const id = z.string().min(1).max(256)
|
|
8
|
+
const nameSchema = z.string().trim().min(1).max(80)
|
|
9
|
+
const stateSchema = z.object({
|
|
10
|
+
groups: z.record(z.uuid(), z.object({ workspaceId: id, name: nameSchema })),
|
|
11
|
+
membership: z.record(id, z.uuid()),
|
|
12
|
+
pendingSessions: z.record(z.uuid(), id).default({}),
|
|
13
|
+
})
|
|
14
|
+
const requestSchema = z.discriminatedUnion('action', [
|
|
15
|
+
z.strictObject({ action: z.literal('create'), workspaceId: id, name: nameSchema }),
|
|
16
|
+
z.strictObject({ action: z.literal('rename'), workspaceId: id, groupId: z.uuid(), name: nameSchema }),
|
|
17
|
+
z.strictObject({ action: z.literal('delete'), workspaceId: id, groupId: z.uuid() }),
|
|
18
|
+
z.strictObject({ action: z.literal('assign'), workspaceId: id, sessionId: id, groupId: z.uuid().nullable() }),
|
|
19
|
+
])
|
|
20
|
+
const createSessionSchema = z.strictObject({
|
|
21
|
+
workspaceId: id,
|
|
22
|
+
groupId: z.uuid(),
|
|
23
|
+
retrySessionId: id.optional(),
|
|
24
|
+
})
|
|
25
|
+
const createdSessionSchema = z.object({ state: stateSchema, sessionId: id })
|
|
26
|
+
const domainSpec = defineDomain({
|
|
27
|
+
name: 'chat_groups',
|
|
28
|
+
version: 1,
|
|
29
|
+
tables: { state: domainTable(stateSchema) },
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const packageName = 'dsh-chat-groups'
|
|
33
|
+
const serviceKey = 'chatGroups'
|
|
34
|
+
const codec = (type, schema) => ({
|
|
35
|
+
mode: 'strict',
|
|
36
|
+
typeSymbol: `${packageName}#${type}`,
|
|
37
|
+
create: () => schema,
|
|
38
|
+
})
|
|
39
|
+
const invocations = [
|
|
40
|
+
{ method: 'snapshot', result: codec('Groups', stateSchema) },
|
|
41
|
+
{ method: 'change', request: codec('GroupRequest', requestSchema), result: codec('Groups', stateSchema) },
|
|
42
|
+
{
|
|
43
|
+
method: 'createSession',
|
|
44
|
+
request: codec('CreateSessionRequest', createSessionSchema),
|
|
45
|
+
result: codec('CreatedSession', createdSessionSchema),
|
|
46
|
+
},
|
|
47
|
+
].map(({ method, request, result }) => ({
|
|
48
|
+
id: `${packageName}:${method}`,
|
|
49
|
+
service: serviceKey,
|
|
50
|
+
namespace: serviceKey,
|
|
51
|
+
method,
|
|
52
|
+
invocation: { kind: 'direct' },
|
|
53
|
+
parameters: request === undefined ? [] : [{
|
|
54
|
+
name: 'request',
|
|
55
|
+
wire: 'request',
|
|
56
|
+
source: 'json',
|
|
57
|
+
codec: request,
|
|
58
|
+
}],
|
|
59
|
+
result,
|
|
60
|
+
}))
|
|
61
|
+
|
|
62
|
+
export const name = packageName
|
|
63
|
+
export const inject = [
|
|
64
|
+
'typert', 'remoteAccess', 'storageDomain', 'workspaceRegistry', 'sessionPersistence', 'sessions', 'sessionController',
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
class ChatGroups extends Service {
|
|
68
|
+
constructor(ctx, table) {
|
|
69
|
+
super(ctx, serviceKey)
|
|
70
|
+
this.typertRemote = bindTypertRemote(this, serviceKey)
|
|
71
|
+
this.operations = createGroupOperations({
|
|
72
|
+
read: () => table.get('groups') ?? emptyGroups(),
|
|
73
|
+
write: state => table.put('groups', state),
|
|
74
|
+
workspace: workspaceId => ctx.workspaceRegistry.get(workspaceId),
|
|
75
|
+
available: async sessionId => ctx.sessions.get(sessionId) !== undefined
|
|
76
|
+
|| (await ctx.sessionPersistence.stat(sessionId)) !== undefined,
|
|
77
|
+
archived: sessionId => ctx.workspaceRegistry.archivedSessionIds.includes(sessionId),
|
|
78
|
+
async createNativeSession(request) {
|
|
79
|
+
try {
|
|
80
|
+
return await ctx.sessionController.create(request)
|
|
81
|
+
} catch (error) {
|
|
82
|
+
const failure = remoteErrorOf(error)
|
|
83
|
+
if (failure?.code !== 'session/workspace-attach-failed'
|
|
84
|
+
|| failure.details.workspaceId !== request.workspaceId) throw error
|
|
85
|
+
return { sessionId: failure.details.sessionId, attachmentError: error }
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
requireOwner() {
|
|
92
|
+
if (this.ctx.invocation?.peer.principal.kind !== 'owner') {
|
|
93
|
+
throw new RemoteError('chat-groups/access-denied', '当前用户无权管理会话分组。', {})
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
snapshot() {
|
|
98
|
+
this.requireOwner()
|
|
99
|
+
return this.operations.snapshot()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async change(request) {
|
|
103
|
+
return this.run(() => this.operations.change(request))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async createSession(request) {
|
|
107
|
+
return this.run(() => this.operations.createSession(request))
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async run(operation) {
|
|
111
|
+
this.requireOwner()
|
|
112
|
+
try {
|
|
113
|
+
return await operation()
|
|
114
|
+
} catch (error) {
|
|
115
|
+
if (!(error instanceof GroupError)) throw error
|
|
116
|
+
throw new RemoteError(error.code, error.message, {})
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function apply(ctx) {
|
|
122
|
+
ctx.effect(async () => {
|
|
123
|
+
const domain = await ctx.storageDomain.open(domainSpec)
|
|
124
|
+
try {
|
|
125
|
+
new ChatGroups(ctx, domain.table('state'))
|
|
126
|
+
const unregister = ctx.typert.register({
|
|
127
|
+
package: packageName,
|
|
128
|
+
face: 'host',
|
|
129
|
+
schemas: [],
|
|
130
|
+
model: { services: [], events: [], objects: [] },
|
|
131
|
+
invocations,
|
|
132
|
+
})
|
|
133
|
+
return async () => {
|
|
134
|
+
await unregister()
|
|
135
|
+
await domain.close()
|
|
136
|
+
}
|
|
137
|
+
} catch (error) {
|
|
138
|
+
await domain.close()
|
|
139
|
+
throw error
|
|
140
|
+
}
|
|
141
|
+
}, 'chat-groups storage')
|
|
142
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-chat-groups",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "在 DSH 工作区内按任务整理会话",
|
|
6
|
+
"main": "./host.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./host.js",
|
|
9
|
+
"./client": "./client.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"host.js",
|
|
14
|
+
"groups.js",
|
|
15
|
+
"client.js",
|
|
16
|
+
"cordis.patch.yml"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"dsh": "0.2.2-rc.5"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@deepseek-ai/cordis": "~4.1.0",
|
|
23
|
+
"@deepseek-ai/dsh-storage-domain": "0.2.2-rc.5",
|
|
24
|
+
"@deepseek-ai/dsh-typert-protocol": "0.2.2-rc.5",
|
|
25
|
+
"zod": "^4.0.0"
|
|
26
|
+
},
|
|
27
|
+
"dsh": {
|
|
28
|
+
"bundle": {
|
|
29
|
+
"patch": "./cordis.patch.yml"
|
|
30
|
+
},
|
|
31
|
+
"client": {
|
|
32
|
+
"inject": [
|
|
33
|
+
"@deepseek-ai/dsh-api-remote-access",
|
|
34
|
+
"@deepseek-ai/dsh-api-session-controller",
|
|
35
|
+
"@deepseek-ai/dsh-api-workspace-controller",
|
|
36
|
+
"@deepseek-ai/dsh-client-connection",
|
|
37
|
+
"@deepseek-ai/dsh-client-ui-context-menu",
|
|
38
|
+
"@deepseek-ai/dsh-client-ui-renderer",
|
|
39
|
+
"@deepseek-ai/dsh-client-ui-workspace"
|
|
40
|
+
],
|
|
41
|
+
"platform": "web"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "node --test"
|
|
46
|
+
}
|
|
47
|
+
}
|