dsh-session-editor 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/LICENSE +21 -0
- package/cordis.patch.yml +3 -0
- package/package.json +61 -4
- package//345/233/276/346/240/207.svg +8 -0
- package//345/256/277/344/270/273.js +782 -0
- package//347/225/214/351/235/242.js +804 -0
- package//350/257/273/346/210/221.md +71 -0
- package/README.md +0 -3
|
@@ -0,0 +1,804 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-session-editor',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react');
|
|
5
|
+
const h = React.createElement;
|
|
6
|
+
|
|
7
|
+
const 文案 = {
|
|
8
|
+
删除: '删除这一轮对话',
|
|
9
|
+
编辑: '编辑后立即重新提问',
|
|
10
|
+
确认删除: '再次点击确认删除',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const 图标 = {
|
|
14
|
+
编辑: '<svg viewBox="0 0 16 16" width="22" height="22" fill="none" stroke="currentColor"'
|
|
15
|
+
+ ' stroke-width="1.35" stroke-linecap="round" stroke-linejoin="round">'
|
|
16
|
+
+ '<path d="M11.2 2.4a1.45 1.45 0 0 1 2.05 2.05L5.4 12.3l-2.75.6.6-2.75z"/></svg>',
|
|
17
|
+
删除: '<svg viewBox="0 0 16 16" width="22" height="22" fill="none" stroke="currentColor"'
|
|
18
|
+
+ ' stroke-width="1.35" stroke-linecap="round" stroke-linejoin="round">'
|
|
19
|
+
+ '<path d="M2.6 4.4h10.8M6.2 4.4V2.9h3.6v1.5M4 4.4l.65 8.3h6.7L12 4.4M6.6 6.9v3.4M9.4 6.9v3.4"/></svg>',
|
|
20
|
+
菜单删除: '<svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor"'
|
|
21
|
+
+ ' stroke-width="1.35" stroke-linecap="round" stroke-linejoin="round">'
|
|
22
|
+
+ '<path d="M2.6 4.4h10.8M6.2 4.4V2.9h3.6v1.5M4 4.4l.65 8.3h6.7L12 4.4M6.6 6.9v3.4M9.4 6.9v3.4"/></svg>',
|
|
23
|
+
菜单移动: '<svg viewBox="0 0 16 16" width="16" height="16" fill="none" stroke="currentColor"'
|
|
24
|
+
+ ' stroke-width="1.35" stroke-linecap="round" stroke-linejoin="round">'
|
|
25
|
+
+ '<path d="M2.4 8h8.6M8.2 4.9 11.3 8l-3.1 3.1"/><path d="M13.6 3.4v9.2"/></svg>',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const 行容器样式 = {
|
|
29
|
+
display: 'inline-flex',
|
|
30
|
+
alignItems: 'center',
|
|
31
|
+
// 原生 .actions 的兄弟间距是 8px(MessageIconActions.module.css 的 gap)
|
|
32
|
+
gap: '8px',
|
|
33
|
+
order: '-1',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** 与原生图标行同规格的按钮外观(28px 方形、6px 内边距、圆角、悬停变色)。 */
|
|
37
|
+
const 图标按钮样式 = {
|
|
38
|
+
display: 'inline-flex',
|
|
39
|
+
alignItems: 'center',
|
|
40
|
+
justifyContent: 'center',
|
|
41
|
+
width: 'calc(28px + var(--dsh-content-font-delta, 0px))',
|
|
42
|
+
height: 'calc(28px + var(--dsh-content-font-delta, 0px))',
|
|
43
|
+
padding: '6px',
|
|
44
|
+
border: 'none',
|
|
45
|
+
borderRadius: 'var(--dsw-radius-sm)',
|
|
46
|
+
background: 'transparent',
|
|
47
|
+
color: 'var(--dsw-alias-label-tertiary)',
|
|
48
|
+
cursor: 'pointer',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** 编辑面板:贴着图标行向上弹出,限制在聊天列宽度内,避免压到左侧会话列表。 */
|
|
52
|
+
const 编辑面板样式 = {
|
|
53
|
+
position: 'absolute',
|
|
54
|
+
bottom: 'calc(100% + 8px)',
|
|
55
|
+
right: 0,
|
|
56
|
+
zIndex: 1000,
|
|
57
|
+
width: 'min(520px, calc(100vw - 380px))',
|
|
58
|
+
maxWidth: 'calc(100vw - 380px)',
|
|
59
|
+
maxHeight: '60vh',
|
|
60
|
+
overflow: 'auto',
|
|
61
|
+
padding: '10px',
|
|
62
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
63
|
+
borderRadius: 'var(--dsw-radius-md, 8px)',
|
|
64
|
+
background: 'var(--dsw-alias-bg-base)',
|
|
65
|
+
boxShadow: 'var(--dsw-elevation-panel, 0 8px 24px rgba(0, 0, 0, 0.28))',
|
|
66
|
+
textAlign: 'left',
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const 文本域样式 = {
|
|
70
|
+
width: '100%',
|
|
71
|
+
boxSizing: 'border-box',
|
|
72
|
+
resize: 'vertical',
|
|
73
|
+
padding: '8px',
|
|
74
|
+
font: 'inherit',
|
|
75
|
+
fontSize: 'var(--dsh-content-font-size-secondary, 13px)',
|
|
76
|
+
lineHeight: 1.5,
|
|
77
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
78
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
79
|
+
borderRadius: 'var(--dsw-radius-sm)',
|
|
80
|
+
background: 'var(--dsw-alias-bg-base)',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const 次要按钮样式 = {
|
|
84
|
+
padding: '5px 12px',
|
|
85
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
86
|
+
borderRadius: 'var(--dsw-radius-sm)',
|
|
87
|
+
background: 'transparent',
|
|
88
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
89
|
+
cursor: 'pointer',
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const 主要按钮样式 = {
|
|
93
|
+
...次要按钮样式,
|
|
94
|
+
border: 'none',
|
|
95
|
+
// 不依赖可能缺失/低对比度的主题 token,保证文字始终可读。
|
|
96
|
+
background: '#2563eb',
|
|
97
|
+
color: '#ffffff',
|
|
98
|
+
fontWeight: 600,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const 危险按钮样式 = {
|
|
102
|
+
...次要按钮样式,
|
|
103
|
+
border: 'none',
|
|
104
|
+
background: 'var(--dsw-alias-label-error, #d93025)',
|
|
105
|
+
color: '#fff',
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** 行内错误提示:不使用 window.alert(桌面渲染器里会被静默吞掉)。 */
|
|
109
|
+
const 错误样式 = {
|
|
110
|
+
position: 'absolute',
|
|
111
|
+
bottom: 'calc(100% + 6px)',
|
|
112
|
+
right: 0,
|
|
113
|
+
zIndex: 31,
|
|
114
|
+
maxWidth: '360px',
|
|
115
|
+
padding: '6px 8px',
|
|
116
|
+
borderRadius: 'var(--dsw-radius-sm)',
|
|
117
|
+
background: 'var(--dsw-alias-label-error, #d93025)',
|
|
118
|
+
color: '#fff',
|
|
119
|
+
fontSize: '12px',
|
|
120
|
+
lineHeight: 1.4,
|
|
121
|
+
whiteSpace: 'pre-wrap',
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** 行内删除确认条。 */
|
|
125
|
+
const 确认条样式 = {
|
|
126
|
+
position: 'absolute',
|
|
127
|
+
bottom: 'calc(100% + 6px)',
|
|
128
|
+
right: 0,
|
|
129
|
+
zIndex: 30,
|
|
130
|
+
display: 'flex',
|
|
131
|
+
alignItems: 'center',
|
|
132
|
+
gap: '8px',
|
|
133
|
+
padding: '6px 8px',
|
|
134
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
135
|
+
borderRadius: 'var(--dsw-radius-sm)',
|
|
136
|
+
background: 'var(--dsw-alias-bg-base)',
|
|
137
|
+
boxShadow: 'var(--dsw-elevation-panel)',
|
|
138
|
+
whiteSpace: 'nowrap',
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** 带悬停反馈的图标按钮。 */
|
|
142
|
+
function 图标按钮(属性) {
|
|
143
|
+
const [悬停中, 设置悬停] = React.useState(false);
|
|
144
|
+
const 启用 = !属性.禁用;
|
|
145
|
+
const 样式 = {
|
|
146
|
+
...图标按钮样式,
|
|
147
|
+
background: 悬停中 && 启用 ? 'var(--dsw-alias-interactive-bg-hover)' : 'transparent',
|
|
148
|
+
color: 悬停中 && 启用 ? 'var(--dsw-alias-label-secondary)' : 'var(--dsw-alias-label-tertiary)',
|
|
149
|
+
...(启用 ? {} : { opacity: '0.5', cursor: 'default' }),
|
|
150
|
+
};
|
|
151
|
+
return h('button', {
|
|
152
|
+
type: 'button',
|
|
153
|
+
title: 属性.标题,
|
|
154
|
+
'aria-label': 属性.标题,
|
|
155
|
+
style: 样式,
|
|
156
|
+
disabled: 属性.禁用,
|
|
157
|
+
onClick: 属性.点击,
|
|
158
|
+
onMouseEnter: () => 设置悬停(true),
|
|
159
|
+
onMouseLeave: () => 设置悬停(false),
|
|
160
|
+
dangerouslySetInnerHTML: { __html: 属性.图形 },
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 通过斜杠命令通道调用一次 Host 侧操作。 */
|
|
165
|
+
async function 调用宿主(ctx, 会话ID, 载荷) {
|
|
166
|
+
const 命令行 = '/se ' + encodeURIComponent(JSON.stringify(载荷));
|
|
167
|
+
const 执行命令 = ctx.remote && ctx.remote.commands && ctx.remote.commands.execute;
|
|
168
|
+
if (typeof 执行命令 === 'function') {
|
|
169
|
+
try {
|
|
170
|
+
const 结果 = await 执行命令(会话ID, 命令行, []);
|
|
171
|
+
if (结果 && typeof 结果 === 'object' && 'ok' in 结果) {
|
|
172
|
+
if (!结果.ok) {
|
|
173
|
+
const 失败 = 结果.error || {};
|
|
174
|
+
return { kind: 'error', text: String(失败.message || 失败.code || '请求失败') };
|
|
175
|
+
}
|
|
176
|
+
const 执行结果 = 结果.value && 结果.value.result;
|
|
177
|
+
if (!执行结果) {
|
|
178
|
+
return { kind: 'error', text: 'Host 没有执行该命令(命令未注册、被策略拒绝,或会话不是活动状态)' };
|
|
179
|
+
}
|
|
180
|
+
return 执行结果;
|
|
181
|
+
}
|
|
182
|
+
if (结果 && typeof 结果 === 'object' && 'result' in 结果) return 结果.result;
|
|
183
|
+
if (结果 === undefined || 结果 === null) {
|
|
184
|
+
return { kind: 'error', text: 'Host 没有执行该命令(未找到 se 命令)' };
|
|
185
|
+
}
|
|
186
|
+
} catch {
|
|
187
|
+
// 落到下面的会话门面备用通道。
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
const 绑定 = ctx.sessions && ctx.sessions.binding && ctx.sessions.binding(会话ID);
|
|
191
|
+
const 会话 = 绑定 && 绑定.session;
|
|
192
|
+
if (!会话 || typeof 会话.command !== 'function') {
|
|
193
|
+
return { kind: 'error', text: '无法连到 Host' };
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
const 命令结果 = await 会话.command(命令行);
|
|
197
|
+
if (命令结果 && 命令结果.ok === false) {
|
|
198
|
+
const 失败 = 命令结果.error || {};
|
|
199
|
+
return { kind: 'error', text: String(失败.message || 失败.code || '会话命令执行失败') };
|
|
200
|
+
}
|
|
201
|
+
const 值 = 命令结果 && 命令结果.value;
|
|
202
|
+
if (值 && 值.matched === false) {
|
|
203
|
+
return { kind: 'error', text: 'Host 上没有找到 se 命令(matched=false)' };
|
|
204
|
+
}
|
|
205
|
+
return { kind: 'success' };
|
|
206
|
+
} catch (错误) {
|
|
207
|
+
return { kind: 'error', text: String((错误 && 错误.message) || 错误) };
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* 被删除的消息标识(UUID 全局唯一)。客户端的讲义是 append-only 的:
|
|
213
|
+
* Host 的 surfaceOp 替换只影响模型上下文,不会删掉聊天界面里的旧行。
|
|
214
|
+
* 这里注册一个“隐藏定义”,在 chat 目标上独占这些事件并返回空节点,
|
|
215
|
+
* 于是重建时对应行不再生成;标识/时间戳持久化到 localStorage,刷新后仍隐藏。
|
|
216
|
+
*/
|
|
217
|
+
const 已删消息标识 = new Set();
|
|
218
|
+
/** 被删轮次内所有事件的 time(毫秒级全局唯一),用于隐藏工具、过程、完成状态等非消息行。 */
|
|
219
|
+
const 已删事件时间 = new Set();
|
|
220
|
+
/** 本插件内部 `se` 命令的 commandId;这些传输命令不应显示成聊天记录里的命令行。 */
|
|
221
|
+
const 内部命令 = new Set();
|
|
222
|
+
const 已删存储键 = 'session-editor.deleted-messages';
|
|
223
|
+
const 已删时间键 = 'session-editor.deleted-times';
|
|
224
|
+
try {
|
|
225
|
+
const 已存 = JSON.parse(localStorage.getItem(已删存储键) || '[]');
|
|
226
|
+
if (Array.isArray(已存)) {
|
|
227
|
+
for (const 标识 of 已存) if (typeof 标识 === 'string') 已删消息标识.add(标识);
|
|
228
|
+
}
|
|
229
|
+
const 已存时间 = JSON.parse(localStorage.getItem(已删时间键) || '[]');
|
|
230
|
+
if (Array.isArray(已存时间)) {
|
|
231
|
+
for (const 时间 of 已存时间) if (typeof 时间 === 'number') 已删事件时间.add(时间);
|
|
232
|
+
}
|
|
233
|
+
} catch {
|
|
234
|
+
// 本地存储不可用时只影响刷新后的隐藏持久性。
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const 隐藏定义 = {
|
|
238
|
+
kind: 'session-editor.hidden-message',
|
|
239
|
+
target: 'chat',
|
|
240
|
+
match(event) {
|
|
241
|
+
// 内部 `se` 命令只是界面到 Host 的传输通道,不生成聊天行。
|
|
242
|
+
if (event.type === 'command/run' && event.data && event.data.name === 'se') {
|
|
243
|
+
const 命令ID = String(event.data.commandId ?? '');
|
|
244
|
+
if (命令ID !== '') 内部命令.add(命令ID);
|
|
245
|
+
return { id: 'session-editor-hidden-command-' + 命令ID, role: 'start', exclusive: true };
|
|
246
|
+
}
|
|
247
|
+
if (event.type === 'command/done' && event.data && 内部命令.has(String(event.data.commandId))) {
|
|
248
|
+
return { id: 'session-editor-hidden-command-' + event.data.commandId, role: 'start', exclusive: true };
|
|
249
|
+
}
|
|
250
|
+
// 被删轮次里的一切事件(含工具调用/结果、step/turn 过程、完成用时)按时间戳整体隐藏。
|
|
251
|
+
if (typeof event.time === 'number' && 已删事件时间.has(event.time)) {
|
|
252
|
+
return { id: 'session-editor-hidden-time-' + event.time, role: 'start', exclusive: true };
|
|
253
|
+
}
|
|
254
|
+
if (event.surfaceOp !== 'append') return null;
|
|
255
|
+
let 标识;
|
|
256
|
+
if (event.type === 'user/message') 标识 = event.data && event.data.id;
|
|
257
|
+
else if (event.type === 'assistant/message') 标识 = event.data && event.data.message && event.data.message.id;
|
|
258
|
+
if (typeof 标识 !== 'string' || !已删消息标识.has(标识)) return null;
|
|
259
|
+
return { id: 'session-editor-hidden-' + 标识, role: 'start', exclusive: true };
|
|
260
|
+
},
|
|
261
|
+
start: (_context, match) => ({ seq: match.event.seq }),
|
|
262
|
+
update: (context) => context.state,
|
|
263
|
+
buildViewNode: () => null,
|
|
264
|
+
};
|
|
265
|
+
let 隐藏注销 = null;
|
|
266
|
+
function 注册隐藏(ctx) {
|
|
267
|
+
try {
|
|
268
|
+
if (隐藏注销 === null && ctx.uiConversation && ctx.uiConversation.events
|
|
269
|
+
&& typeof ctx.uiConversation.events.register === 'function') {
|
|
270
|
+
隐藏注销 = ctx.uiConversation.events.register(隐藏定义);
|
|
271
|
+
}
|
|
272
|
+
} catch (错误) {
|
|
273
|
+
console.error('[session-editor] 隐藏定义注册失败', 错误);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/** 记下被删轮次的标识与时间戳,并触发一次定义重建,让已渲染的节点立即消失。 */
|
|
277
|
+
function 隐藏已删消息(ctx, 删除信息) {
|
|
278
|
+
const 标识列表 = (删除信息 && 删除信息.ids) || [];
|
|
279
|
+
const 时间列表 = (删除信息 && 删除信息.times) || [];
|
|
280
|
+
let 变化 = false;
|
|
281
|
+
for (const 标识 of 标识列表) {
|
|
282
|
+
if (typeof 标识 === 'string' && !已删消息标识.has(标识)) {
|
|
283
|
+
已删消息标识.add(标识);
|
|
284
|
+
变化 = true;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const 时间 of 时间列表) {
|
|
288
|
+
if (typeof 时间 === 'number' && !已删事件时间.has(时间)) {
|
|
289
|
+
已删事件时间.add(时间);
|
|
290
|
+
变化 = true;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (!变化) return;
|
|
294
|
+
try {
|
|
295
|
+
localStorage.setItem(已删存储键, JSON.stringify([...已删消息标识]));
|
|
296
|
+
localStorage.setItem(已删时间键, JSON.stringify([...已删事件时间]));
|
|
297
|
+
} catch {
|
|
298
|
+
// 写不进去时仅失去刷新后的隐藏持久性。
|
|
299
|
+
}
|
|
300
|
+
if (隐藏注销 !== null) {
|
|
301
|
+
try { 隐藏注销(); } catch { /* 旧定义已经释放 */ }
|
|
302
|
+
隐藏注销 = null;
|
|
303
|
+
}
|
|
304
|
+
注册隐藏(ctx);
|
|
305
|
+
}
|
|
306
|
+
/** 把 Host 回来的一轮结构(删除和改写共用)解析成整轮隐藏所需的信息。 */
|
|
307
|
+
function 解析整轮(结果) {
|
|
308
|
+
try {
|
|
309
|
+
const 值 = JSON.parse(String((结果 && 结果.text) || ''));
|
|
310
|
+
return {
|
|
311
|
+
ids: Array.isArray(值.ids) ? 值.ids : [],
|
|
312
|
+
times: Array.isArray(值.times) ? 值.times : [],
|
|
313
|
+
prompt: typeof 值.prompt === 'string' ? 值.prompt : '',
|
|
314
|
+
messageId: typeof 值.messageId === 'string' ? 值.messageId : undefined,
|
|
315
|
+
sessionId: typeof 值.sessionId === 'string' ? 值.sessionId : undefined,
|
|
316
|
+
prompts: Array.isArray(值.prompts) ? 值.prompts : [],
|
|
317
|
+
};
|
|
318
|
+
} catch {
|
|
319
|
+
return { ids: [], times: [], prompt: '', prompts: [] };
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** 已知的轮次原文:按助手回答的消息 id 记录,供编辑面板预填与刷新后恢复。 */
|
|
324
|
+
const 已知原文 = new Map();
|
|
325
|
+
const 原文存储键 = 'session-editor.round-prompts';
|
|
326
|
+
try {
|
|
327
|
+
const 已存原文 = JSON.parse(localStorage.getItem(原文存储键) || '{}');
|
|
328
|
+
if (已存原文 !== null && typeof 已存原文 === 'object') {
|
|
329
|
+
for (const [标识, 文本] of Object.entries(已存原文)) {
|
|
330
|
+
if (typeof 文本 === 'string') 已知原文.set(标识, 文本);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
} catch {
|
|
334
|
+
// 本地存储不可用时仅影响刷新后的预填。
|
|
335
|
+
}
|
|
336
|
+
function 取已记原文(消息标识) {
|
|
337
|
+
return 已知原文.get(消息标识) || '';
|
|
338
|
+
}
|
|
339
|
+
function 记住原文(消息标识, 原文) {
|
|
340
|
+
if (typeof 消息标识 !== 'string' || 原文 === '' || 已知原文.get(消息标识) === 原文) return;
|
|
341
|
+
已知原文.set(消息标识, 原文);
|
|
342
|
+
try {
|
|
343
|
+
localStorage.setItem(原文存储键, JSON.stringify(Object.fromEntries(已知原文)));
|
|
344
|
+
} catch {
|
|
345
|
+
// 写不进去时仅失去刷新后的预填。
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** 每个会话只补齐一次时间戳(刷新后重新补齐)。 */
|
|
350
|
+
const 已补齐会话 = new Set();
|
|
351
|
+
async function 补齐被删时间戳(ctx, 会话ID) {
|
|
352
|
+
if (typeof 会话ID !== 'string' || 已补齐会话.has(会话ID)) return;
|
|
353
|
+
已补齐会话.add(会话ID);
|
|
354
|
+
if (已删消息标识.size === 0) return;
|
|
355
|
+
try {
|
|
356
|
+
const 结果 = await 调用宿主(ctx, 会话ID, { op: 'deleted-times', sessionId: 会话ID });
|
|
357
|
+
if (!结果 || 结果.kind !== 'success') return;
|
|
358
|
+
const 值 = JSON.parse(String(结果.text || ''));
|
|
359
|
+
if (Array.isArray(值.prompts)) {
|
|
360
|
+
for (const 条目 of 值.prompts) {
|
|
361
|
+
if (条目 && typeof 条目.id === 'string' && typeof 条目.prompt === 'string') 记住原文(条目.id, 条目.prompt);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (Array.isArray(值.times) && 值.times.length > 0) {
|
|
365
|
+
隐藏已删消息(ctx, { ids: [], times: 值.times });
|
|
366
|
+
}
|
|
367
|
+
} catch {
|
|
368
|
+
// 补齐失败时保持现状,不阻塞界面。
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function 取会话(ctx, 会话ID) {
|
|
373
|
+
const 绑定 = ctx.sessions && ctx.sessions.binding && ctx.sessions.binding(会话ID);
|
|
374
|
+
return (绑定 && 绑定.session) || undefined;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** 从一段已加载窗口里取纯文本。 */
|
|
378
|
+
function 内容文本(内容) {
|
|
379
|
+
if (!Array.isArray(内容)) return '';
|
|
380
|
+
return 内容.filter((块) => 块 && 块.type === 'text' && typeof 块.text === 'string')
|
|
381
|
+
.map((块) => 块.text)
|
|
382
|
+
.join('\n');
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* 读出当前已加载窗口里的“回答 id → 该轮提问原文”。
|
|
387
|
+
* 只用本地快照,不发任何命令,因此不会在会话日志里留下记录。
|
|
388
|
+
*/
|
|
389
|
+
async function 记录已加载消息(ctx, 会话ID) {
|
|
390
|
+
const 结果 = new Map();
|
|
391
|
+
try {
|
|
392
|
+
const 绑定 = ctx.sessions && ctx.sessions.binding && ctx.sessions.binding(会话ID);
|
|
393
|
+
const 源 = 绑定 && 绑定.eventSource;
|
|
394
|
+
if (源 === undefined) return 结果;
|
|
395
|
+
const 窗口 = typeof 源.getSnapshot === 'function' ? 源.getSnapshot() : undefined;
|
|
396
|
+
const 条目 = (窗口 && 窗口.entries) || [];
|
|
397
|
+
let 提问 = '';
|
|
398
|
+
for (const 项 of 条目) {
|
|
399
|
+
const 事件 = (项 && 项.event) || 项;
|
|
400
|
+
if (!事件 || typeof 事件 !== 'object') continue;
|
|
401
|
+
if (事件.type === 'turn/start') { 提问 = ''; continue; }
|
|
402
|
+
if (事件.type === 'user/message') {
|
|
403
|
+
const 消息 = 事件.data && (事件.data.message || 事件.data);
|
|
404
|
+
const 文本 = 内容文本(消息 && 消息.content);
|
|
405
|
+
if (文本 !== '' && !提问) 提问 = 文本;
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (事件.type === 'assistant/message' && 提问 !== '') {
|
|
409
|
+
const 标识 = 事件.data && 事件.data.message && 事件.data.message.id;
|
|
410
|
+
if (typeof 标识 === 'string') 结果.set(标识, { prompt: 提问 });
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
} catch (错误) {
|
|
414
|
+
console.error('[session-editor] 读取本地窗口失败', 错误);
|
|
415
|
+
}
|
|
416
|
+
return 结果;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* 助手回答图标行里的编辑/删除按钮(assistant-actions 座位,与复制、点赞同一行)。
|
|
421
|
+
* Client 上下文必须显式传入:座位注册为 `(属性) => 回合操作(属性, ctx)`。
|
|
422
|
+
* @param 属性 - 座位给的 messageId(ownerProps)与 sessionId(Session 标准属性)。
|
|
423
|
+
* @param ctx - Client 插件上下文,用于命令通道与 Session 门面。
|
|
424
|
+
*/
|
|
425
|
+
function 回合操作(属性, ctx) {
|
|
426
|
+
const 消息ID = 属性.messageId;
|
|
427
|
+
const 会话ID = 属性.sessionId;
|
|
428
|
+
const [忙碌中, 设置忙碌] = React.useState(false);
|
|
429
|
+
const [编辑中, 设置编辑中] = React.useState(false);
|
|
430
|
+
const [草稿, 设置草稿] = React.useState('');
|
|
431
|
+
const [确认删除, 设置确认删除] = React.useState(false);
|
|
432
|
+
const [错误, 设置错误] = React.useState(null);
|
|
433
|
+
React.useEffect(() => { void 补齐被删时间戳(ctx, 会话ID); }, [会话ID]);
|
|
434
|
+
if (typeof 会话ID !== 'string' || typeof 消息ID !== 'string') return null;
|
|
435
|
+
|
|
436
|
+
const 执行 = async (操作) => {
|
|
437
|
+
if (忙碌中) return;
|
|
438
|
+
设置忙碌(true);
|
|
439
|
+
设置错误(null);
|
|
440
|
+
try {
|
|
441
|
+
await 操作();
|
|
442
|
+
} catch (错误对象) {
|
|
443
|
+
// 座位组件里的同步/异步异常不会自己冒出来,这里必须显式暴露(行内红字,不依赖原生弹窗)。
|
|
444
|
+
const 文本 = String((错误对象 && 错误对象.message) || 错误对象);
|
|
445
|
+
console.error('[session-editor] 操作失败', 错误对象);
|
|
446
|
+
设置错误(文本);
|
|
447
|
+
} finally {
|
|
448
|
+
设置忙碌(false);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
// Electron 的桌面渲染器不支持原生 confirm,直接返回 false;所以删除用行内二次确认。
|
|
453
|
+
const 删除 = () => {
|
|
454
|
+
if (!确认删除) {
|
|
455
|
+
设置确认删除(true);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
设置确认删除(false);
|
|
459
|
+
void 执行(async () => {
|
|
460
|
+
const 结果 = await 调用宿主(ctx, 会话ID, { op: 'delete-round', sessionId: 会话ID, messageId: 消息ID });
|
|
461
|
+
if (结果 && 结果.kind === 'error') { 设置错误(结果.text); return; }
|
|
462
|
+
const 整轮 = 解析整轮(结果);
|
|
463
|
+
记住原文(整轮.messageId || 消息ID, 整轮.prompt);
|
|
464
|
+
隐藏已删消息(ctx, 整轮);
|
|
465
|
+
});
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
// 编辑面板要预填“这一轮的原文”。原文只从**本地已加载的会话窗口**里读:
|
|
469
|
+
// 列表里存的原文优先,其次从协议事件里找提问消息。绝不走 `se` 命令通道——
|
|
470
|
+
// 命令通道会把每次调用写进会话日志,制造出与对话无关的残留记录。
|
|
471
|
+
const 编辑 = () => void 执行(async () => {
|
|
472
|
+
let 原文 = 取已记原文(消息ID);
|
|
473
|
+
if (原文 === '') {
|
|
474
|
+
const 记录 = await 记录已加载消息(ctx, 会话ID);
|
|
475
|
+
const 命中 = 记录.get(消息ID);
|
|
476
|
+
原文 = 命中 && typeof 命中.prompt === 'string' ? 命中.prompt : '';
|
|
477
|
+
}
|
|
478
|
+
if (原文 === '') {
|
|
479
|
+
设置错误('没读到这一轮的原文:请刷新页面后重试,或直接在上方输入新内容');
|
|
480
|
+
}
|
|
481
|
+
记住原文(消息ID, 原文);
|
|
482
|
+
设置草稿(原文);
|
|
483
|
+
设置编辑中(true);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// 改写与删除走同一套整轮清理:Host 把这一轮在模型表面上整体替换成新提问,
|
|
487
|
+
// 并把该轮全部事件的消息标识/时间戳回来,界面据此隐藏问题、回答、工具与用时,
|
|
488
|
+
// 随后用新文本重新提问,会话释放时旧轮从磁盘物理清除。
|
|
489
|
+
const 提交编辑 = () => void 执行(async () => {
|
|
490
|
+
const 文本 = 草稿.trim();
|
|
491
|
+
if (文本 === '') return;
|
|
492
|
+
const 结果 = await 调用宿主(ctx, 会话ID, { op: 'edit-round', sessionId: 会话ID, messageId: 消息ID, text: 文本 });
|
|
493
|
+
if (结果 && 结果.kind === 'error') {
|
|
494
|
+
设置错误(结果.text);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
const 整轮 = 解析整轮(结果);
|
|
498
|
+
记住原文(整轮.messageId || 消息ID, 整轮.prompt);
|
|
499
|
+
隐藏已删消息(ctx, 整轮);
|
|
500
|
+
设置编辑中(false);
|
|
501
|
+
// 先隐藏再提问:新提问落进 Inbox,成为这一轮新的开头。
|
|
502
|
+
const 目标会话ID = 整轮.sessionId || 会话ID;
|
|
503
|
+
const 会话 = 取会话(ctx, 目标会话ID);
|
|
504
|
+
if (会话 && typeof 会话.prompt === 'function') {
|
|
505
|
+
const 回复 = await 会话.prompt([{ type: 'text', text: 文本 }], 'queue');
|
|
506
|
+
if (回复 && 回复.ok === false) {
|
|
507
|
+
设置错误(String(回复.error?.message || '重新提问被拒绝'));
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
设置错误('已改写完成,但找不到该会话的界面通道,请手动重新发送');
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
const 编辑面板 = !编辑中 ? null : h('div', { style: 编辑面板样式 },
|
|
515
|
+
h('div', { style: { fontSize: '12px', color: 'var(--dsw-alias-label-tertiary)', marginBottom: '6px' } },
|
|
516
|
+
'改写这一轮的提问,保存后立即用新内容重新提问:'),
|
|
517
|
+
h('textarea', {
|
|
518
|
+
value: 草稿,
|
|
519
|
+
autoFocus: true,
|
|
520
|
+
rows: 4,
|
|
521
|
+
style: 文本域样式,
|
|
522
|
+
onChange: (事件) => 设置草稿(事件.target.value),
|
|
523
|
+
onKeyDown: (事件) => {
|
|
524
|
+
if (事件.key === 'Escape') { 事件.preventDefault(); 设置编辑中(false); }
|
|
525
|
+
if (事件.key === 'Enter' && (事件.ctrlKey || 事件.metaKey)) { 事件.preventDefault(); 提交编辑(); }
|
|
526
|
+
},
|
|
527
|
+
}),
|
|
528
|
+
h('div', { style: { display: 'flex', justifyContent: 'flex-end', gap: '8px', marginTop: '6px' } },
|
|
529
|
+
h('button', { type: 'button', style: 次要按钮样式, onClick: () => 设置编辑中(false), disabled: 忙碌中 }, '取消'),
|
|
530
|
+
h('button', { type: 'button', style: 主要按钮样式, onClick: 提交编辑, disabled: 忙碌中 }, '保存并重新提问'),
|
|
531
|
+
),
|
|
532
|
+
);
|
|
533
|
+
|
|
534
|
+
return h('span', { style: { position: 'relative', display: 'inline-flex' } },
|
|
535
|
+
h('span', { style: 行容器样式 },
|
|
536
|
+
h(图标按钮, {
|
|
537
|
+
标题: 文案.编辑,
|
|
538
|
+
图形: 图标.编辑,
|
|
539
|
+
禁用: 忙碌中,
|
|
540
|
+
点击: 编辑,
|
|
541
|
+
}),
|
|
542
|
+
h(图标按钮, {
|
|
543
|
+
标题: 确认删除 ? 文案.确认删除 : 文案.删除,
|
|
544
|
+
图形: 图标.删除,
|
|
545
|
+
禁用: 忙碌中,
|
|
546
|
+
危险: 确认删除,
|
|
547
|
+
点击: 删除,
|
|
548
|
+
}),
|
|
549
|
+
),
|
|
550
|
+
编辑面板,
|
|
551
|
+
错误 === null ? null : h('div', { style: 错误样式 }, 错误),
|
|
552
|
+
确认删除 === false ? null : h('div', { style: 确认条样式 },
|
|
553
|
+
h('span', { style: { color: 'var(--dsw-alias-label-secondary)', fontSize: '12px' } }, '删除这一轮对话?'),
|
|
554
|
+
h('button', { type: 'button', style: 危险按钮样式, onClick: 删除, disabled: 忙碌中 }, '确认'),
|
|
555
|
+
h('button', { type: 'button', style: 次要按钮样式, onClick: () => 设置确认删除(false), disabled: 忙碌中 }, '取消'),
|
|
556
|
+
),
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/** 菜单图标渲染器(contextMenu.icon 是 keyed 插槽)。 */
|
|
561
|
+
function 菜单图标(属性) {
|
|
562
|
+
const 图 = 属性 && 属性.icon && 属性.icon.key;
|
|
563
|
+
const 内容 = 图 === 'session-editor.移动' ? 图标.菜单移动 : 图标.菜单删除;
|
|
564
|
+
return h('span', {
|
|
565
|
+
style: { display: 'inline-flex', alignItems: 'center', width: '16px', height: '16px' },
|
|
566
|
+
dangerouslySetInnerHTML: { __html: 内容 },
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/** 取当前 Client 仍然持有引用的第一个会话,用它作为会话级调用的传输通道。 */
|
|
571
|
+
function 活动会话ID(ctx) {
|
|
572
|
+
let 状态;
|
|
573
|
+
try {
|
|
574
|
+
状态 = ctx.sessions && ctx.sessions.list && ctx.sessions.list.get();
|
|
575
|
+
} catch {
|
|
576
|
+
return undefined;
|
|
577
|
+
}
|
|
578
|
+
const 条目列表 = (状态 && (状态.items || 状态.sessions)) || [];
|
|
579
|
+
const 候选 = [];
|
|
580
|
+
for (const 条目 of 条目列表) {
|
|
581
|
+
const 标识 = (条目 && (条目.id || 条目.sessionId)) || 条目;
|
|
582
|
+
if (typeof 标识 === 'string') 候选.push(标识);
|
|
583
|
+
}
|
|
584
|
+
for (const 标识 of 候选) {
|
|
585
|
+
try {
|
|
586
|
+
const 信息 = ctx.sessions.retainInfo(标识).get();
|
|
587
|
+
if (信息 && 信息.referenceCount > 0) return 标识;
|
|
588
|
+
} catch {
|
|
589
|
+
// 读不到的条目直接跳过。
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return undefined;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function 工作区源(ctx) {
|
|
596
|
+
const 服务 = ctx.workspaces;
|
|
597
|
+
return 服务 && (服务.list || 服务);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function 工作区列表(ctx) {
|
|
601
|
+
try {
|
|
602
|
+
const 源 = 工作区源(ctx);
|
|
603
|
+
if (!源 || typeof 源.getSnapshot !== 'function') return [];
|
|
604
|
+
const 快照 = 源.getSnapshot();
|
|
605
|
+
const 条目 = (快照 && 快照.items) || [];
|
|
606
|
+
return 条目.filter((项) => 项 && typeof 项.id === 'string');
|
|
607
|
+
} catch {
|
|
608
|
+
return [];
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** 工作区列表缓存:快照尚未到达时短暂等待推送,不通过聊天命令查询。 */
|
|
613
|
+
let 工作区缓存 = { 时间: 0, 列表: [] };
|
|
614
|
+
|
|
615
|
+
async function 取工作区(ctx, _目标会话) {
|
|
616
|
+
const 本地 = 工作区列表(ctx);
|
|
617
|
+
if (本地.length > 0) {
|
|
618
|
+
工作区缓存 = { 时间: Date.now(), 列表: 本地 };
|
|
619
|
+
return 本地;
|
|
620
|
+
}
|
|
621
|
+
if (工作区缓存.列表.length > 0) return 工作区缓存.列表;
|
|
622
|
+
// 等工作区快照首次到达:用命令通道查询会把 JSON 结果作为一行命令记录
|
|
623
|
+
// 留在当前对话里,所以这里只订阅客户端快照。
|
|
624
|
+
const 源 = 工作区源(ctx);
|
|
625
|
+
if (源 && typeof 源.subscribe === 'function') {
|
|
626
|
+
const 列表 = await new Promise((resolve) => {
|
|
627
|
+
let 完成 = false;
|
|
628
|
+
let 计时器;
|
|
629
|
+
let 取消 = () => {};
|
|
630
|
+
const 结束 = (值) => {
|
|
631
|
+
if (完成) return;
|
|
632
|
+
完成 = true;
|
|
633
|
+
try { 取消(); } catch { /* 订阅可能已失效 */ }
|
|
634
|
+
if (计时器 !== undefined) clearTimeout(计时器);
|
|
635
|
+
resolve(值);
|
|
636
|
+
};
|
|
637
|
+
try {
|
|
638
|
+
取消 = 源.subscribe(() => {
|
|
639
|
+
const 当前 = 工作区列表(ctx);
|
|
640
|
+
if (当前.length > 0) 结束(当前);
|
|
641
|
+
});
|
|
642
|
+
} catch {
|
|
643
|
+
// 订阅失败就只等超时。
|
|
644
|
+
}
|
|
645
|
+
计时器 = setTimeout(() => 结束(工作区列表(ctx)), 800);
|
|
646
|
+
const 立即 = 工作区列表(ctx);
|
|
647
|
+
if (立即.length > 0) 结束(立即);
|
|
648
|
+
});
|
|
649
|
+
if (Array.isArray(列表) && 列表.length > 0) {
|
|
650
|
+
工作区缓存 = { 时间: Date.now(), 列表 };
|
|
651
|
+
return 列表;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return 工作区缓存.列表;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
async function 删除会话(ctx, 目标) {
|
|
658
|
+
const 会话ID = (目标 && (目标.sessionId || 目标.id)) || undefined;
|
|
659
|
+
if (typeof 会话ID !== 'string') {
|
|
660
|
+
window.alert('无法识别该会话');
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const 标题 = (目标 && (目标.title || 目标.displayTitle || 目标.name)) || 会话ID;
|
|
664
|
+
const 已确认 = window.confirm(
|
|
665
|
+
'删除会话「' + 标题 + '」?\n\n'
|
|
666
|
+
+ '该会话的所有记录将从磁盘删除,不可恢复。'
|
|
667
|
+
);
|
|
668
|
+
if (!已确认) return;
|
|
669
|
+
// 重启前先给个可见的标记:把标题改成「【待删除】…」。会话不在内存时可能改名失败,忽略即可。
|
|
670
|
+
const 新标题 = '【待删除】' + 标题;
|
|
671
|
+
try {
|
|
672
|
+
const 改名 = ctx.remote && ctx.remote.session && ctx.remote.session.rename;
|
|
673
|
+
if (typeof 改名 === 'function') await 改名({ sessionId: 会话ID, title: 新标题 });
|
|
674
|
+
} catch {
|
|
675
|
+
// 改不了名不影响删除。
|
|
676
|
+
}
|
|
677
|
+
const 传输会话 = 活动会话ID(ctx) || 会话ID;
|
|
678
|
+
const 结果 = await 调用宿主(ctx, 传输会话, { op: 'delete-session', sessionId: 会话ID });
|
|
679
|
+
if (结果 && 结果.kind === 'error') {
|
|
680
|
+
window.alert(结果.text);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
// 客户端本地也立刻摘除,不等远程事件到达。
|
|
684
|
+
try {
|
|
685
|
+
if (ctx.sessions && typeof ctx.sessions.handleSessionRemoved === 'function') {
|
|
686
|
+
ctx.sessions.handleSessionRemoved(会话ID);
|
|
687
|
+
}
|
|
688
|
+
} catch {
|
|
689
|
+
// 本地摘除失败不影响宿主已经完成的删除。
|
|
690
|
+
}
|
|
691
|
+
try {
|
|
692
|
+
if (ctx.sessions && typeof ctx.sessions.refresh === 'function') await ctx.sessions.refresh();
|
|
693
|
+
} catch {
|
|
694
|
+
// 列表刷新失败不影响已登记的删除。
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
async function 移动会话(ctx, 会话ID, 工作区ID, 工作区名) {
|
|
699
|
+
const 已确认 = window.confirm('把该会话移动到工作区「' + 工作区名 + '」?\n\n会话内容不变,只更换它所属的工作区。');
|
|
700
|
+
if (!已确认) return;
|
|
701
|
+
const 传输会话 = 活动会话ID(ctx) || 会话ID;
|
|
702
|
+
const 结果 = await 调用宿主(ctx, 传输会话, { op: 'move-session', sessionId: 会话ID, workspaceId: 工作区ID });
|
|
703
|
+
if (结果 && 结果.kind === 'error') {
|
|
704
|
+
window.alert(结果.text);
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
try {
|
|
708
|
+
if (ctx.sessions && typeof ctx.sessions.refresh === 'function') await ctx.sessions.refresh();
|
|
709
|
+
} catch {
|
|
710
|
+
// 列表刷新失败不影响已完成移动。
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/** 目标会话当前所属的工作区 id。 */
|
|
715
|
+
function 所属工作区(工作区, 会话ID) {
|
|
716
|
+
for (const 项 of 工作区) {
|
|
717
|
+
const 集合 = 项.sessionIds || 项.sessions || [];
|
|
718
|
+
if (Array.isArray(集合) && 集合.includes(会话ID)) return 项.id;
|
|
719
|
+
}
|
|
720
|
+
return undefined;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
return {
|
|
724
|
+
inject: ['slots', 'sessions', 'remote', 'remote.commands', 'remote.session', 'contextMenus', 'workspaces', 'uiConversation'],
|
|
725
|
+
apply(ctx) {
|
|
726
|
+
// 先装隐藏定义:本会话或历史里已被删除的消息不再生成聊天行。
|
|
727
|
+
注册隐藏(ctx);
|
|
728
|
+
// 工作区快照是懒加载流:先订阅一次,菜单解析时才有数据。
|
|
729
|
+
try {
|
|
730
|
+
const 源 = ctx.workspaces && (ctx.workspaces.list || ctx.workspaces);
|
|
731
|
+
if (源 && typeof 源.subscribe === 'function') {
|
|
732
|
+
const 取消订阅 = 源.subscribe(() => {});
|
|
733
|
+
if (typeof ctx.effect === 'function') ctx.effect(() => () => { if (typeof 取消订阅 === 'function') 取消订阅(); });
|
|
734
|
+
}
|
|
735
|
+
} catch (错误) {
|
|
736
|
+
console.error('[session-editor] 工作区快照订阅失败', 错误);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
ctx.slots.inject('conversation.chat.assistant-actions', () => ctx.slots.register({
|
|
740
|
+
name: 'conversation.chat.assistant-actions',
|
|
741
|
+
id: 'session-editor.round-actions',
|
|
742
|
+
order: 10,
|
|
743
|
+
}, (属性) => 回合操作(属性, ctx)));
|
|
744
|
+
|
|
745
|
+
try {
|
|
746
|
+
ctx.slots.inject('contextMenu.icon', () => {
|
|
747
|
+
ctx.slots.register({ name: 'contextMenu.icon', key: 'session-editor.删除' }, 菜单图标);
|
|
748
|
+
ctx.slots.register({ name: 'contextMenu.icon', key: 'session-editor.移动' }, 菜单图标);
|
|
749
|
+
});
|
|
750
|
+
} catch (错误) {
|
|
751
|
+
console.error('[session-editor] 菜单图标注册失败', 错误);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
ctx.contextMenus.register({
|
|
755
|
+
id: 'session-editor.session-actions',
|
|
756
|
+
target: 'session',
|
|
757
|
+
order: 700,
|
|
758
|
+
resolve: async (目标) => {
|
|
759
|
+
const 会话ID = (目标 && (目标.sessionId || 目标.id)) || undefined;
|
|
760
|
+
if (typeof 会话ID !== 'string') return [];
|
|
761
|
+
const 全部工作区 = await 取工作区(ctx, 会话ID);
|
|
762
|
+
const 当前 = 所属工作区(全部工作区, 会话ID);
|
|
763
|
+
const 其它 = 全部工作区.filter((项) => 项.id !== 当前);
|
|
764
|
+
const 动作 = [{
|
|
765
|
+
id: 'delete-session',
|
|
766
|
+
label: '删除会话',
|
|
767
|
+
icon: { key: 'session-editor.删除' },
|
|
768
|
+
danger: true,
|
|
769
|
+
separatorBefore: true,
|
|
770
|
+
afterContent: true,
|
|
771
|
+
run: () => 删除会话(ctx, 目标),
|
|
772
|
+
}];
|
|
773
|
+
if (其它.length > 0) {
|
|
774
|
+
动作.unshift({
|
|
775
|
+
id: 'move-session',
|
|
776
|
+
label: '移动会话',
|
|
777
|
+
icon: { key: 'session-editor.移动' },
|
|
778
|
+
afterContent: true,
|
|
779
|
+
children: 其它.map((项) => ({
|
|
780
|
+
id: 'move-to-' + 项.id,
|
|
781
|
+
label: 项.title || 项.path || 项.id,
|
|
782
|
+
run: () => 移动会话(ctx, 会话ID, 项.id, 项.title || 项.path || 项.id),
|
|
783
|
+
})),
|
|
784
|
+
});
|
|
785
|
+
} else {
|
|
786
|
+
动作.unshift({
|
|
787
|
+
id: 'move-session',
|
|
788
|
+
label: '移动会话',
|
|
789
|
+
icon: { key: 'session-editor.移动' },
|
|
790
|
+
afterContent: true,
|
|
791
|
+
children: [{
|
|
792
|
+
id: 'move-none',
|
|
793
|
+
label: 全部工作区.length === 0 ? '(工作区列表尚未加载)' : '(没有其它工作区)',
|
|
794
|
+
disabled: true,
|
|
795
|
+
}],
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
return 动作;
|
|
799
|
+
},
|
|
800
|
+
});
|
|
801
|
+
},
|
|
802
|
+
};
|
|
803
|
+
},
|
|
804
|
+
});
|