@ryuu-64/dsh-session-tools 0.8.0 → 0.8.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.
- package/LICENSE +21 -21
- package/README.md +21 -67
- package/README.zh-CN.md +39 -0
- package/cordis.patch.yml +4 -4
- package/lib/client.js +623 -528
- package/lib/communication-permissions.js +144 -101
- package/lib/communication-remote.js +75 -64
- package/lib/index.js +906 -894
- package/lib/message-receipts.js +156 -121
- package/lib/session-activity.js +135 -135
- package/lib/session-discovery.js +213 -213
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +27 -15
package/lib/client.js
CHANGED
|
@@ -1,528 +1,623 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser half: render the `session_create` tool card as something the person
|
|
3
|
-
* can act on. The host half persists an explicit target in presentationMeta;
|
|
4
|
-
* this half uses that durable identity, never an id guessed from the receipt.
|
|
5
|
-
*
|
|
6
|
-
* Loaded by the browser module table as a CJS closure factory; `require` resolves
|
|
7
|
-
* only platform seeds and other registered bundles.
|
|
8
|
-
*/
|
|
9
|
-
window.__ModuleLoader__.load({
|
|
10
|
-
id: "@ryuu-64/dsh-session-tools",
|
|
11
|
-
factory: (require) => {
|
|
12
|
-
var module = { exports: {} };
|
|
13
|
-
var exports = module.exports;
|
|
14
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
15
|
-
|
|
16
|
-
const react = require("react");
|
|
17
|
-
const { MenuItemButton, Modal, Button, Switch, ReferenceIconRegular } = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
18
|
-
|
|
19
|
-
/** Slots this client half needs before it can register anything. */
|
|
20
|
-
const inject = ["slots", "sessions"];
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Flatten a settled result's content blocks, mirroring how `ui-tool` reads
|
|
24
|
-
* them: text blocks verbatim, anything else as JSON.
|
|
25
|
-
* @param node - a candidate settled result node.
|
|
26
|
-
* @returns the flattened text, or "" when this is not a result node.
|
|
27
|
-
*/
|
|
28
|
-
function resultText(node) {
|
|
29
|
-
if (node === null || typeof node !== "object" || !Array.isArray(node.content)) return "";
|
|
30
|
-
return node.content
|
|
31
|
-
.map((part) => (part !== null && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
32
|
-
? part.text
|
|
33
|
-
: JSON.stringify(part)))
|
|
34
|
-
.join("\n");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Read only the successful ToolResultNode's durable presentation metadata.
|
|
39
|
-
* rc.2 projects event.data.meta here; canonical value is not persisted.
|
|
40
|
-
* Old receipts cannot prove identity, even when only one id is visible.
|
|
41
|
-
* @param block - the running or settled tool call block handed to the slot.
|
|
42
|
-
* @returns the session id and optional title, or undefined.
|
|
43
|
-
*/
|
|
44
|
-
function readCreatedSession(block) {
|
|
45
|
-
if (block === null || typeof block !== "object" || block.kind !== "tool-result" || block.isError !== false) return undefined;
|
|
46
|
-
const meta = block.meta;
|
|
47
|
-
if (meta === null || typeof meta !== "object" || Array.isArray(meta)) return undefined;
|
|
48
|
-
if (typeof meta.sessionId !== "string" || !/^session-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u.test(meta.sessionId)) return undefined;
|
|
49
|
-
return { sessionId: meta.sessionId, title: typeof meta.title === "string" ? meta.title : undefined };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* One row for a finished `session_create` call: what was created, and a
|
|
54
|
-
* button that opens it in the conversation area.
|
|
55
|
-
* @param props - slot owner props plus the injected `openSession`.
|
|
56
|
-
* @returns the row element.
|
|
57
|
-
*/
|
|
58
|
-
function SessionCreateRow(props) {
|
|
59
|
-
const openSession = props.openSession ?? props.slot?.injected?.openSession;
|
|
60
|
-
const created = readCreatedSession(props.block);
|
|
61
|
-
if (created === undefined) {
|
|
62
|
-
// Keep errors and legacy receipts readable, without claiming a
|
|
63
|
-
// successful creation or inventing a clickable navigation target.
|
|
64
|
-
const text = resultText(props.block);
|
|
65
|
-
const fallback = props.block?.kind === "tool-result" ? "没有可用的会话创建结果" : "正在创建会话…";
|
|
66
|
-
return react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere" } }, text || fallback);
|
|
67
|
-
}
|
|
68
|
-
const title = created.title === undefined || created.title === "" ? "会话" : created.title;
|
|
69
|
-
const canOpen = typeof openSession === "function";
|
|
70
|
-
const onOpen = () => {
|
|
71
|
-
if (!canOpen) return;
|
|
72
|
-
try {
|
|
73
|
-
openSession(created.sessionId);
|
|
74
|
-
} catch (error) {
|
|
75
|
-
console.error("[session-tools] opening the session failed:", error);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
return react.createElement(
|
|
79
|
-
"div",
|
|
80
|
-
{
|
|
81
|
-
style: {
|
|
82
|
-
display: "flex",
|
|
83
|
-
alignItems: "center",
|
|
84
|
-
gap: "8px",
|
|
85
|
-
flexWrap: "wrap",
|
|
86
|
-
fontSize: "13px",
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
react.createElement("span", null, "已创建会话"),
|
|
90
|
-
react.createElement(
|
|
91
|
-
"button",
|
|
92
|
-
{
|
|
93
|
-
type: "button",
|
|
94
|
-
onClick: onOpen,
|
|
95
|
-
disabled: !canOpen,
|
|
96
|
-
title: created.sessionId,
|
|
97
|
-
style: {
|
|
98
|
-
border: "1px solid var(--dsh-border, #3a4150)",
|
|
99
|
-
background: "transparent",
|
|
100
|
-
color: "inherit",
|
|
101
|
-
borderRadius: "999px",
|
|
102
|
-
padding: "1px 10px",
|
|
103
|
-
cursor: canOpen ? "pointer" : "default",
|
|
104
|
-
font: "inherit",
|
|
105
|
-
opacity: canOpen ? "1" : "0.55",
|
|
106
|
-
},
|
|
107
|
-
},
|
|
108
|
-
title,
|
|
109
|
-
),
|
|
110
|
-
react.createElement("span", { style: { opacity: 0.55, fontFamily: "ui-monospace, monospace", fontSize: "11px" } }, created.sessionId),
|
|
111
|
-
react.createElement("div", { style: { flexBasis: "100%" } }, react.createElement(MessageStateRow, { block: props.block })),
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const statusLabels = {
|
|
117
|
-
queued: "已排队", delivered: "已交给目标", turnCompleted: "目标本轮结束(不等于任务成功)",
|
|
118
|
-
discarded: "取消", cancelled: "取消", interrupted: "中断", failed: "失败", incomplete: "未完整结束",
|
|
119
|
-
blocked: "未完整结束", unknown: "暂无法确认",
|
|
120
|
-
};
|
|
121
|
-
function MessageStateRow({ block }) {
|
|
122
|
-
const text = resultText(block);
|
|
123
|
-
if (block?.kind !== "tool-result") return react.createElement("span", { role: "status" }, "正在处理…");
|
|
124
|
-
const meta = block.meta;
|
|
125
|
-
if (block.isError || meta?.scope === "sessionOverview" || typeof meta?.messageId !== "string") {
|
|
126
|
-
return react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere" } }, text || "暂无法确认");
|
|
127
|
-
}
|
|
128
|
-
return react.createElement("div", { style: { display: "grid", gap: 6 } },
|
|
129
|
-
react.createElement("strong", null, statusLabels[meta.messageStatus] ?? "暂无法确认"),
|
|
130
|
-
meta.waitStatus === "timedOut" && react.createElement("div", null, "等待超时。没有重发,也没有取消;请用同一消息编号继续查询。"),
|
|
131
|
-
meta.waitStatus === "callerCancelled" && react.createElement("div", null, "已停止等待,目标会话继续处理。"),
|
|
132
|
-
react.createElement("details", null, react.createElement("summary", null, "消息编号与结果详情"),
|
|
133
|
-
react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere", color: "var(--dsw-alias-label-secondary)" } }, text)));
|
|
134
|
-
}
|
|
135
|
-
const remoteContribution = (() => {
|
|
136
|
-
// TypertSchema requires parse(value); keep these strict schemas local to
|
|
137
|
-
// this browser bundle because Zod is not a platform module seed.
|
|
138
|
-
const scalar = check => value => {
|
|
139
|
-
if (!check(value)) throw new TypeError("Invalid session communication field");
|
|
140
|
-
return value;
|
|
141
|
-
};
|
|
142
|
-
const object = fields => ({ parse(value) {
|
|
143
|
-
if (!value || typeof value !== "object" || Array.isArray(value)
|
|
144
|
-
|| Object.keys(value).some(key => !Object.hasOwn(fields, key))
|
|
145
|
-
|| Object.keys(fields).some(key => !Object.hasOwn(value, key))) {
|
|
146
|
-
throw new TypeError("Invalid session communication object");
|
|
147
|
-
}
|
|
148
|
-
return Object.fromEntries(Object.
|
|
149
|
-
} });
|
|
150
|
-
const sessionId = scalar(value => typeof value === "string" && value.length >= 1 && value.length <= 1024);
|
|
151
|
-
const boolean = scalar(value => typeof value === "boolean");
|
|
152
|
-
const revision = scalar(value => Number.isSafeInteger(value) && value >= 0);
|
|
153
|
-
const getSchema = object({ sessionId });
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
if (
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
function
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Browser half: render the `session_create` tool card as something the person
|
|
3
|
+
* can act on. The host half persists an explicit target in presentationMeta;
|
|
4
|
+
* this half uses that durable identity, never an id guessed from the receipt.
|
|
5
|
+
*
|
|
6
|
+
* Loaded by the browser module table as a CJS closure factory; `require` resolves
|
|
7
|
+
* only platform seeds and other registered bundles.
|
|
8
|
+
*/
|
|
9
|
+
window.__ModuleLoader__.load({
|
|
10
|
+
id: "@ryuu-64/dsh-session-tools",
|
|
11
|
+
factory: (require) => {
|
|
12
|
+
var module = { exports: {} };
|
|
13
|
+
var exports = module.exports;
|
|
14
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
|
|
16
|
+
const react = require("react");
|
|
17
|
+
const { MenuItemButton, Modal, Button, Switch, Tag, ReferenceIconRegular, SettingsForm, SettingsFormModel } = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
18
|
+
|
|
19
|
+
/** Slots this client half needs before it can register anything. */
|
|
20
|
+
const inject = ["slots", "sessions"];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Flatten a settled result's content blocks, mirroring how `ui-tool` reads
|
|
24
|
+
* them: text blocks verbatim, anything else as JSON.
|
|
25
|
+
* @param node - a candidate settled result node.
|
|
26
|
+
* @returns the flattened text, or "" when this is not a result node.
|
|
27
|
+
*/
|
|
28
|
+
function resultText(node) {
|
|
29
|
+
if (node === null || typeof node !== "object" || !Array.isArray(node.content)) return "";
|
|
30
|
+
return node.content
|
|
31
|
+
.map((part) => (part !== null && typeof part === "object" && part.type === "text" && typeof part.text === "string"
|
|
32
|
+
? part.text
|
|
33
|
+
: JSON.stringify(part)))
|
|
34
|
+
.join("\n");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Read only the successful ToolResultNode's durable presentation metadata.
|
|
39
|
+
* rc.2 projects event.data.meta here; canonical value is not persisted.
|
|
40
|
+
* Old receipts cannot prove identity, even when only one id is visible.
|
|
41
|
+
* @param block - the running or settled tool call block handed to the slot.
|
|
42
|
+
* @returns the session id and optional title, or undefined.
|
|
43
|
+
*/
|
|
44
|
+
function readCreatedSession(block) {
|
|
45
|
+
if (block === null || typeof block !== "object" || block.kind !== "tool-result" || block.isError !== false) return undefined;
|
|
46
|
+
const meta = block.meta;
|
|
47
|
+
if (meta === null || typeof meta !== "object" || Array.isArray(meta)) return undefined;
|
|
48
|
+
if (typeof meta.sessionId !== "string" || !/^session-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u.test(meta.sessionId)) return undefined;
|
|
49
|
+
return { sessionId: meta.sessionId, title: typeof meta.title === "string" ? meta.title : undefined };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* One row for a finished `session_create` call: what was created, and a
|
|
54
|
+
* button that opens it in the conversation area.
|
|
55
|
+
* @param props - slot owner props plus the injected `openSession`.
|
|
56
|
+
* @returns the row element.
|
|
57
|
+
*/
|
|
58
|
+
function SessionCreateRow(props) {
|
|
59
|
+
const openSession = props.openSession ?? props.slot?.injected?.openSession;
|
|
60
|
+
const created = readCreatedSession(props.block);
|
|
61
|
+
if (created === undefined) {
|
|
62
|
+
// Keep errors and legacy receipts readable, without claiming a
|
|
63
|
+
// successful creation or inventing a clickable navigation target.
|
|
64
|
+
const text = resultText(props.block);
|
|
65
|
+
const fallback = props.block?.kind === "tool-result" ? "没有可用的会话创建结果" : "正在创建会话…";
|
|
66
|
+
return react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere" } }, text || fallback);
|
|
67
|
+
}
|
|
68
|
+
const title = created.title === undefined || created.title === "" ? "会话" : created.title;
|
|
69
|
+
const canOpen = typeof openSession === "function";
|
|
70
|
+
const onOpen = () => {
|
|
71
|
+
if (!canOpen) return;
|
|
72
|
+
try {
|
|
73
|
+
openSession(created.sessionId);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error("[session-tools] opening the session failed:", error);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
return react.createElement(
|
|
79
|
+
"div",
|
|
80
|
+
{
|
|
81
|
+
style: {
|
|
82
|
+
display: "flex",
|
|
83
|
+
alignItems: "center",
|
|
84
|
+
gap: "8px",
|
|
85
|
+
flexWrap: "wrap",
|
|
86
|
+
fontSize: "13px",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
react.createElement("span", null, "已创建会话"),
|
|
90
|
+
react.createElement(
|
|
91
|
+
"button",
|
|
92
|
+
{
|
|
93
|
+
type: "button",
|
|
94
|
+
onClick: onOpen,
|
|
95
|
+
disabled: !canOpen,
|
|
96
|
+
title: created.sessionId,
|
|
97
|
+
style: {
|
|
98
|
+
border: "1px solid var(--dsh-border, #3a4150)",
|
|
99
|
+
background: "transparent",
|
|
100
|
+
color: "inherit",
|
|
101
|
+
borderRadius: "999px",
|
|
102
|
+
padding: "1px 10px",
|
|
103
|
+
cursor: canOpen ? "pointer" : "default",
|
|
104
|
+
font: "inherit",
|
|
105
|
+
opacity: canOpen ? "1" : "0.55",
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
title,
|
|
109
|
+
),
|
|
110
|
+
react.createElement("span", { style: { opacity: 0.55, fontFamily: "ui-monospace, monospace", fontSize: "11px" } }, created.sessionId),
|
|
111
|
+
react.createElement("div", { style: { flexBasis: "100%" } }, react.createElement(MessageStateRow, { block: props.block })),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
const statusLabels = {
|
|
117
|
+
queued: "已排队", delivered: "已交给目标", turnCompleted: "目标本轮结束(不等于任务成功)",
|
|
118
|
+
discarded: "取消", cancelled: "取消", interrupted: "中断", failed: "失败", incomplete: "未完整结束",
|
|
119
|
+
blocked: "未完整结束", unknown: "暂无法确认",
|
|
120
|
+
};
|
|
121
|
+
function MessageStateRow({ block }) {
|
|
122
|
+
const text = resultText(block);
|
|
123
|
+
if (block?.kind !== "tool-result") return react.createElement("span", { role: "status" }, "正在处理…");
|
|
124
|
+
const meta = block.meta;
|
|
125
|
+
if (block.isError || meta?.scope === "sessionOverview" || typeof meta?.messageId !== "string") {
|
|
126
|
+
return react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere" } }, text || "暂无法确认");
|
|
127
|
+
}
|
|
128
|
+
return react.createElement("div", { style: { display: "grid", gap: 6 } },
|
|
129
|
+
react.createElement("strong", null, statusLabels[meta.messageStatus] ?? "暂无法确认"),
|
|
130
|
+
meta.waitStatus === "timedOut" && react.createElement("div", null, "等待超时。没有重发,也没有取消;请用同一消息编号继续查询。"),
|
|
131
|
+
meta.waitStatus === "callerCancelled" && react.createElement("div", null, "已停止等待,目标会话继续处理。"),
|
|
132
|
+
react.createElement("details", null, react.createElement("summary", null, "消息编号与结果详情"),
|
|
133
|
+
react.createElement("div", { style: { whiteSpace: "pre-wrap", overflowWrap: "anywhere", color: "var(--dsw-alias-label-secondary)" } }, text)));
|
|
134
|
+
}
|
|
135
|
+
const remoteContribution = (() => {
|
|
136
|
+
// TypertSchema requires parse(value); keep these strict schemas local to
|
|
137
|
+
// this browser bundle because Zod is not a platform module seed.
|
|
138
|
+
const scalar = check => value => {
|
|
139
|
+
if (!check(value)) throw new TypeError("Invalid session communication field");
|
|
140
|
+
return value;
|
|
141
|
+
};
|
|
142
|
+
const object = (fields, optional = {}) => ({ parse(value) {
|
|
143
|
+
if (!value || typeof value !== "object" || Array.isArray(value)
|
|
144
|
+
|| Object.keys(value).some(key => !Object.hasOwn(fields, key) && !Object.hasOwn(optional, key))
|
|
145
|
+
|| Object.keys(fields).some(key => !Object.hasOwn(value, key))) {
|
|
146
|
+
throw new TypeError("Invalid session communication object");
|
|
147
|
+
}
|
|
148
|
+
return Object.fromEntries(Object.keys(value).map(key => [key, (fields[key] ?? optional[key])(value[key])]));
|
|
149
|
+
} });
|
|
150
|
+
const sessionId = scalar(value => typeof value === "string" && value.length >= 1 && value.length <= 1024);
|
|
151
|
+
const boolean = scalar(value => typeof value === "boolean");
|
|
152
|
+
const revision = scalar(value => Number.isSafeInteger(value) && value >= 0);
|
|
153
|
+
const getSchema = object({ sessionId });
|
|
154
|
+
const defaultsSchema = object({ send: boolean, receive: boolean });
|
|
155
|
+
const setSchema = object({ sessionId, send: boolean, receive: boolean, expectedRevision: revision }, {
|
|
156
|
+
expectedDefaults: value => defaultsSchema.parse(value),
|
|
157
|
+
});
|
|
158
|
+
const recordSchema = object({ send: boolean, receive: boolean, revision });
|
|
159
|
+
const setResultSchema = object({ status: scalar(value => value === "saved" || value === "conflict"), current: value => recordSchema.parse(value) });
|
|
160
|
+
return {
|
|
161
|
+
package: "@ryuu-64/dsh-session-tools",
|
|
162
|
+
descriptors: ["get", "set"].map(method => ({
|
|
163
|
+
id: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}`,
|
|
164
|
+
service: "sessionCommunication", namespace: "sessionCommunication", method,
|
|
165
|
+
invocation: { kind: "direct" },
|
|
166
|
+
parameters: [{ name: "request", wire: "request", source: "json", codec: {
|
|
167
|
+
mode: "strict", typeSymbol: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}:request`,
|
|
168
|
+
create: () => method === "get" ? getSchema : setSchema,
|
|
169
|
+
} }],
|
|
170
|
+
result: {
|
|
171
|
+
mode: "strict", typeSymbol: `@ryuu-64/dsh-session-tools:sessionCommunication.${method}:result`,
|
|
172
|
+
create: () => method === "get" ? recordSchema : setResultSchema,
|
|
173
|
+
},
|
|
174
|
+
})),
|
|
175
|
+
};
|
|
176
|
+
})();
|
|
177
|
+
function validRecord(value) {
|
|
178
|
+
return value && typeof value.send === "boolean" && typeof value.receive === "boolean" && Number.isSafeInteger(value.revision) && value.revision >= 0;
|
|
179
|
+
}
|
|
180
|
+
// One authoritative cache per client, shared by every pane and the menu.
|
|
181
|
+
// Other windows are reconciled by bounded polling and focus/visibility reads.
|
|
182
|
+
function createCommunicationStore() {
|
|
183
|
+
let api;
|
|
184
|
+
const entries = new Map();
|
|
185
|
+
const entryFor = id => {
|
|
186
|
+
if (!entries.has(id)) entries.set(id, { listeners: new Set(), active: 0, token: 0, reading: false, saving: false,
|
|
187
|
+
value: { record: null, busy: false, failed: false, notice: "正在读取设置…" } });
|
|
188
|
+
return entries.get(id);
|
|
189
|
+
};
|
|
190
|
+
const publish = (entry, patch) => {
|
|
191
|
+
entry.value = { ...entry.value, ...patch };
|
|
192
|
+
entry.listeners.forEach(listener => listener());
|
|
193
|
+
};
|
|
194
|
+
const read = async (id, verifyPending = false) => {
|
|
195
|
+
const entry = entryFor(id);
|
|
196
|
+
if (!api || entry.reading || (entry.saving && !verifyPending)) return;
|
|
197
|
+
entry.reading = true;
|
|
198
|
+
if (!entry.value.record) publish(entry, { busy: true });
|
|
199
|
+
const token = ++entry.token;
|
|
200
|
+
let timer;
|
|
201
|
+
try {
|
|
202
|
+
const result = await Promise.race([api.get({ sessionId: id }), new Promise((_, reject) => {
|
|
203
|
+
timer = setTimeout(() => reject(new Error("read timeout")), 12000);
|
|
204
|
+
})]);
|
|
205
|
+
if (token !== entry.token) return;
|
|
206
|
+
if (!result.ok || !validRecord(result.value)) throw new Error("unavailable");
|
|
207
|
+
if (entry.value.record && result.value.revision < entry.value.record.revision) return;
|
|
208
|
+
const previous = entry.value.record;
|
|
209
|
+
const changed = previous && (result.value.revision !== previous.revision
|
|
210
|
+
|| result.value.send !== previous.send || result.value.receive !== previous.receive);
|
|
211
|
+
publish(entry, { record: result.value, failed: changed ? false : !!entry.value.record && entry.value.failed,
|
|
212
|
+
notice: entry.saving ? "已读取当前设置,保存请求仍未结束;暂不能再次更改。" : changed ? "设置已在其他页面更改,已同步最新状态。" : entry.value.record ? entry.value.notice : "已读取最新设置。更改后立即生效。" });
|
|
213
|
+
} catch {
|
|
214
|
+
if (token === entry.token) publish(entry, { record: null, failed: true, notice: "状态未知,暂时无法读取设置。请重试。" });
|
|
215
|
+
} finally {
|
|
216
|
+
clearTimeout(timer); entry.reading = false;
|
|
217
|
+
if (token === entry.token) publish(entry, { busy: entry.saving });
|
|
218
|
+
if (!entry.listeners.size && !entry.saving) entries.delete(id);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
const change = async (id, key, next) => {
|
|
222
|
+
const entry = entryFor(id), record = entry.value.record;
|
|
223
|
+
if (!api || !record || entry.saving) return;
|
|
224
|
+
const owner = api;
|
|
225
|
+
entry.saving = true; ++entry.token;
|
|
226
|
+
publish(entry, { busy: true, failed: false, notice: "正在保存…" });
|
|
227
|
+
// A timeout cannot cancel an accepted write. Keep the write lock until
|
|
228
|
+
// it settles; never replay it or offer a misleading rollback.
|
|
229
|
+
let timedOut = false, staleResponse = false;
|
|
230
|
+
const timer = setTimeout(() => {
|
|
231
|
+
if (api !== owner) return;
|
|
232
|
+
timedOut = true;
|
|
233
|
+
publish(entry, { record: null, failed: true, notice: "保存结果尚未确认,仍在等待。不会重复提交;关闭面板不会取消更改。" });
|
|
234
|
+
void read(id, true);
|
|
235
|
+
}, 12000);
|
|
236
|
+
try {
|
|
237
|
+
const result = await owner.set({
|
|
238
|
+
sessionId: id, send: record.send, receive: record.receive, [key]: next,
|
|
239
|
+
expectedRevision: record.revision,
|
|
240
|
+
...(record.revision === 0 && { expectedDefaults: { send: record.send, receive: record.receive } }),
|
|
241
|
+
});
|
|
242
|
+
if (api !== owner) return;
|
|
243
|
+
if (!result.ok || !validRecord(result.value?.current) || !["saved", "conflict"].includes(result.value?.status)) throw new Error("unconfirmed");
|
|
244
|
+
if (entry.value.record && result.value.current.revision < entry.value.record.revision) { staleResponse = true; return; }
|
|
245
|
+
publish(entry, { record: result.value.current, failed: result.value.status === "conflict",
|
|
246
|
+
notice: result.value.status === "saved" ? "已保存,立即生效。" : "设置已在其他页面更改。已显示最新设置,请确认后再操作。" });
|
|
247
|
+
} catch {
|
|
248
|
+
if (api !== owner) return;
|
|
249
|
+
publish(entry, { record: null, failed: true, notice: "保存结果暂无法确认,正在重新读取;不会自动重复保存。" });
|
|
250
|
+
} finally {
|
|
251
|
+
clearTimeout(timer); ++entry.token; entry.saving = false; publish(entry, { busy: false });
|
|
252
|
+
if (api !== owner || timedOut || staleResponse || !entry.value.record) void read(id);
|
|
253
|
+
else if (!entry.listeners.size && !entry.reading) entries.delete(id);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const refresh = () => { if (document.visibilityState === "hidden") return; for (const [id, entry] of entries) if (entry.active) void read(id); };
|
|
257
|
+
return {
|
|
258
|
+
connect(value) {
|
|
259
|
+
api = value; refresh();
|
|
260
|
+
return () => {
|
|
261
|
+
if (api !== value) return;
|
|
262
|
+
api = undefined;
|
|
263
|
+
for (const entry of entries.values()) {
|
|
264
|
+
++entry.token;
|
|
265
|
+
publish(entry, { record: null, busy: entry.saving, failed: true, notice: "连接已断开,状态未知。恢复连接后会重新读取。" });
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
},
|
|
269
|
+
get: id => entryFor(id).value, read, change,
|
|
270
|
+
subscribe(id, listener, active) {
|
|
271
|
+
const entry = entryFor(id); entry.listeners.add(listener);
|
|
272
|
+
if (active) entry.active++;
|
|
273
|
+
if (active && entry.active === 1) {
|
|
274
|
+
entry.timer = setInterval(() => { if (document.visibilityState !== "hidden") void read(id); }, 3000);
|
|
275
|
+
window.addEventListener("focus", refresh);
|
|
276
|
+
document.addEventListener("visibilitychange", refresh);
|
|
277
|
+
}
|
|
278
|
+
if (active) void read(id);
|
|
279
|
+
return () => {
|
|
280
|
+
entry.listeners.delete(listener);
|
|
281
|
+
if (active) entry.active--;
|
|
282
|
+
if (!entry.active) clearInterval(entry.timer);
|
|
283
|
+
if (![...entries.values()].some(item => item.active)) {
|
|
284
|
+
window.removeEventListener("focus", refresh); document.removeEventListener("visibilitychange", refresh);
|
|
285
|
+
}
|
|
286
|
+
if (!entry.listeners.size && !entry.reading && !entry.saving) entries.delete(id);
|
|
287
|
+
};
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function CommunicationControls({ sessionId, store, compact = false, visible = true }) {
|
|
292
|
+
const subscribe = react.useCallback(listener => store.subscribe(sessionId, listener, visible), [store, sessionId, visible]);
|
|
293
|
+
const snapshot = react.useCallback(() => store.get(sessionId), [store, sessionId]);
|
|
294
|
+
const { record, busy, failed, notice } = react.useSyncExternalStore(subscribe, snapshot);
|
|
295
|
+
react.useEffect(() => { if (visible) void store.read(sessionId); }, [store, sessionId, visible]);
|
|
296
|
+
const row = (key, label, description) => react.createElement("div", { key, style: { display: "flex", alignItems: "center", justifyContent: "space-between", gap: compact ? 8 : 24, padding: compact ? "10px 0" : "16px 0", borderBottom: "0.5px solid var(--dsw-alias-border-l2)" } },
|
|
297
|
+
react.createElement("div", { title: description, style: { flex: 1, minWidth: 0, fontSize: compact ? 13 : 14, lineHeight: "20px", color: "var(--dsw-alias-label-primary)" } }, label,
|
|
298
|
+
!compact && react.createElement("div", { style: { marginTop: 4, fontSize: 12, lineHeight: "18px", color: "var(--dsw-alias-label-secondary)" } }, description)),
|
|
299
|
+
react.createElement("span", { "aria-hidden": true, style: { flexShrink: 0, color: "var(--dsw-alias-label-secondary)", fontSize: 12 } }, record ? record[key] ? "开启" : "关闭" : "未知"),
|
|
300
|
+
record && react.createElement(Switch, { checked: record[key], onChange: value => void store.change(sessionId, key, value), label, disabled: busy }));
|
|
301
|
+
return react.createElement("section", { "aria-label": "会话通信", "aria-busy": busy, "data-communication-session": sessionId, style: { padding: compact ? "0 6px 12px" : 0, minWidth: 0 } },
|
|
302
|
+
row("receive", "接收其他会话消息", "关闭会拒绝新的插件消息:未送达、未排队;已接收消息继续处理。"),
|
|
303
|
+
row("send", "向其他会话发送", "也控制创建带首条消息的新会话。"),
|
|
304
|
+
(!compact || record?.receive === false) && react.createElement("p", { style: { color: "var(--dsw-alias-label-secondary)", fontSize: 12, lineHeight: "18px" } }, "仅控制本插件的新消息。关闭接收后,新消息未送达、未排队;已经接收的消息继续处理,历史仍可查看。"),
|
|
305
|
+
react.createElement("div", { role: failed ? "alert" : "status", "aria-live": "polite", style: { marginTop: 8, fontSize: 12, lineHeight: "18px", color: failed ? "var(--dsw-alias-state-error-primary)" : "var(--dsw-alias-label-secondary)" } }, notice),
|
|
306
|
+
!record && !busy && react.createElement(Button, { variant: "outline", onClick: () => void store.read(sessionId) }, "重新读取"));
|
|
307
|
+
}
|
|
308
|
+
function CommunicationForm({ request, store, close }) {
|
|
309
|
+
return react.createElement(Modal, { open: true, onClose: close, closeLabel: "关闭", title: "会话通信", description: request.title || request.sessionId,
|
|
310
|
+
footer: react.createElement(Button, { variant: "outline", onClick: close }, "关闭"),
|
|
311
|
+
}, react.createElement(CommunicationControls, { sessionId: request.sessionId, store }));
|
|
312
|
+
}
|
|
313
|
+
/** Direct ordinary children from the public, durable host catalog.
|
|
314
|
+
* The client controller maps header.parentSession to summary.parentId.
|
|
315
|
+
* Never infer lineage from workspace, title or message text. Native
|
|
316
|
+
* subagents keep their existing catalog and continuation semantics.
|
|
317
|
+
*/
|
|
318
|
+
function RelatedConversationRows({ sessionId, useSessions, useWorkspaces, openSession }) {
|
|
319
|
+
const catalog = useSessions(state => state);
|
|
320
|
+
const workspaces = useWorkspaces(state => state);
|
|
321
|
+
const [expandedSession, setExpandedSession] = react.useState(null);
|
|
322
|
+
const expanded = expandedSession === sessionId;
|
|
323
|
+
const scrollRegion = react.useRef(null);
|
|
324
|
+
const firstAdded = react.useRef(null);
|
|
325
|
+
const expansion = react.useRef(null);
|
|
326
|
+
react.useLayoutEffect(() => {
|
|
327
|
+
const pending = expansion.current, region = scrollRegion.current;
|
|
328
|
+
if (!expanded || !pending || !region) return;
|
|
329
|
+
expansion.current = null;
|
|
330
|
+
region.scrollTop = pending.scrollTop;
|
|
331
|
+
if (pending.keyboard && firstAdded.current) {
|
|
332
|
+
firstAdded.current.focus({ preventScroll: true });
|
|
333
|
+
const item = firstAdded.current.getBoundingClientRect(), bounds = region.getBoundingClientRect();
|
|
334
|
+
if (item.height > bounds.height) region.scrollTop += item.top - bounds.top;
|
|
335
|
+
else if (item.bottom > bounds.bottom) region.scrollTop += item.bottom - bounds.bottom;
|
|
336
|
+
else if (item.top < bounds.top) region.scrollTop -= bounds.top - item.top;
|
|
337
|
+
}
|
|
338
|
+
}, [expanded]);
|
|
339
|
+
// Identity order is stable across activity/title updates and remounts.
|
|
340
|
+
const children = catalog.ids.map(id => catalog.byId[id]).filter(row =>
|
|
341
|
+
row && row.id !== sessionId && row.parentId === sessionId && row.origin !== "subagent" && !workspaces.archivedSessionIds.includes(row.id))
|
|
342
|
+
.sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
343
|
+
const parentId = catalog.byId[sessionId]?.parentId;
|
|
344
|
+
const parent = parentId && parentId !== sessionId ? catalog.byId[parentId] : null;
|
|
345
|
+
const workspaceOf = id => workspaces.items.find(item => item.sessionIds.includes(id));
|
|
346
|
+
const currentWorkspace = workspaceOf(sessionId);
|
|
347
|
+
const titleOf = row => row.title?.trim() ? row.title : row.id;
|
|
348
|
+
const visible = expanded ? children : children.slice(0, 3);
|
|
349
|
+
const listId = "session-tools-children-" + sessionId;
|
|
350
|
+
if ((!parentId || parentId === sessionId) && children.length === 0) return react.createElement("p", { style: { margin: 8, color: "var(--dsw-alias-label-secondary)", fontSize: 13 } }, "当前对话还没有相关对话。");
|
|
351
|
+
return react.createElement("section", {
|
|
352
|
+
"aria-label": "相关对话", ref: scrollRegion,
|
|
353
|
+
style: { padding: "4px 6px 6px", minWidth: 0, minHeight: 0, overflowY: "auto" },
|
|
354
|
+
},
|
|
355
|
+
parentId && parentId !== sessionId ? react.createElement(Button, {
|
|
356
|
+
variant: "ghost", disabled: workspaces.archivedSessionIds.includes(parentId), onClick: () => openSession(parentId),
|
|
357
|
+
style: { width: "100%", justifyContent: "flex-start", height: "auto", padding: "6px 8px", textAlign: "left", whiteSpace: "normal", overflowWrap: "anywhere" },
|
|
358
|
+
}, (workspaces.archivedSessionIds.includes(parentId) ? "父对话已归档 · " : "返回父对话 · ") + (parent ? titleOf(parent) : parentId)) : null,
|
|
359
|
+
children.length ? react.createElement(react.Fragment, null,
|
|
360
|
+
react.createElement("div", { style: { padding: "2px 8px", fontSize: 12, lineHeight: "18px", color: "var(--dsw-alias-label-secondary)" } }, "创建的对话 · " + children.length),
|
|
361
|
+
react.createElement("div", { id: listId }, visible.map((child, index) => {
|
|
362
|
+
const workspace = workspaceOf(child.id);
|
|
363
|
+
const crossWorkspace = workspaces.phase === "ready" && workspaces.state !== "error" && workspace?.workspaceId !== currentWorkspace?.workspaceId;
|
|
364
|
+
return react.createElement(Button, {
|
|
365
|
+
key: child.id, ref: index === 3 ? firstAdded : undefined, variant: "ghost", title: child.id, onClick: () => openSession(child.id),
|
|
366
|
+
style: { width: "100%", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12, padding: "6px 8px", height: "auto", minWidth: 0, textAlign: "left" },
|
|
367
|
+
},
|
|
368
|
+
react.createElement("span", { style: { minWidth: 0, overflowWrap: "anywhere", whiteSpace: "normal", fontSize: 13, lineHeight: "20px" } },
|
|
369
|
+
titleOf(child),
|
|
370
|
+
crossWorkspace ? react.createElement("span", { style: { display: "block", fontSize: 11, color: "var(--dsw-alias-label-secondary)" } }, "跨工作区 · " + (workspace?.title || workspace?.path || "未分组")) : null),
|
|
371
|
+
react.createElement("span", { style: { flexShrink: 0, color: "var(--dsw-alias-label-secondary)", fontSize: 12 } }, child.running === true ? "运行中" : child.running === false ? "空闲" : "状态未知"));
|
|
372
|
+
})),
|
|
373
|
+
children.length > 3 ? react.createElement(Button, {
|
|
374
|
+
variant: "ghost", "aria-expanded": expanded, "aria-controls": listId,
|
|
375
|
+
onClick: event => {
|
|
376
|
+
expansion.current = expanded ? null : { keyboard: event.detail === 0, scrollTop: scrollRegion.current?.scrollTop || 0 };
|
|
377
|
+
setExpandedSession(expanded ? null : sessionId);
|
|
378
|
+
},
|
|
379
|
+
style: { width: "100%", fontSize: 12, height: "auto", padding: "6px 8px" },
|
|
380
|
+
}, expanded ? "收起列表" : "展开更多(" + (children.length - 3) + ")") : null) : null);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const RELATIONS_TAB = "@ryuu-64/dsh-session-tools/relations";
|
|
384
|
+
const RELATIONS_KIND = "session-tools.relations";
|
|
385
|
+
// Only the automatic first reveal is plugin-owned. All pane geometry,
|
|
386
|
+
// collapse, movement, docking and layout persistence belong to DSH.
|
|
387
|
+
function hasRelations(sessionId, catalog, workspaces) {
|
|
388
|
+
const archived = workspaces.archivedSessionIds;
|
|
389
|
+
const parentId = catalog.byId[sessionId]?.parentId;
|
|
390
|
+
return (!!parentId && parentId !== sessionId && !!catalog.byId[parentId] && !archived.includes(parentId)) ||
|
|
391
|
+
catalog.ids.some(id => {
|
|
392
|
+
const row = catalog.byId[id];
|
|
393
|
+
return row && id !== sessionId && row.parentId === sessionId && row.origin !== "subagent" && !archived.includes(id);
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
function RelationsTab(props) {
|
|
397
|
+
const { tab } = props.useTabInfo();
|
|
398
|
+
react.useLayoutEffect(() => {
|
|
399
|
+
props.visibility.set(tab.id, tab.visible);
|
|
400
|
+
return () => props.visibility.delete(tab.id);
|
|
401
|
+
}, [props.visibility, tab.id, tab.visible]);
|
|
402
|
+
return react.createElement("div", {
|
|
403
|
+
"data-session-tools-tab": props.sessionId,
|
|
404
|
+
style: { height: "100%", minHeight: 0, overflow: "auto", padding: 8, boxSizing: "border-box" },
|
|
405
|
+
}, react.createElement(CommunicationControls, { key: props.sessionId, sessionId: props.sessionId, store: props.communication, compact: true, visible: tab.visible }),
|
|
406
|
+
react.createElement(RelatedConversationRows, { ...props, key: tab.id }));
|
|
407
|
+
}
|
|
408
|
+
function RelationsEntry(props) {
|
|
409
|
+
const catalog = props.useSessions(state => state);
|
|
410
|
+
const workspaces = props.useWorkspaces(state => state);
|
|
411
|
+
const mounted = react.useSyncExternalStore(props.sidebar?.mounted.subscribe ?? (() => () => {}), props.sidebar?.mounted.getSnapshot ?? (() => undefined));
|
|
412
|
+
const available = workspaces.phase === "ready" && hasRelations(props.sessionId, catalog, workspaces);
|
|
413
|
+
react.useEffect(() => {
|
|
414
|
+
if (!props.sidebar || !available || mounted !== props.sessionId || props.wasShown(props.sessionId)) return;
|
|
415
|
+
// This is synchronous and rechecks the host's owner before opening.
|
|
416
|
+
// Record success only after the native tab actually exists.
|
|
417
|
+
props.reveal(props.sessionId, true);
|
|
418
|
+
}, [available, mounted, props.sessionId, props.reveal, props.wasShown]);
|
|
419
|
+
if (!props.sessionId) return null;
|
|
420
|
+
return react.createElement(Button, {
|
|
421
|
+
variant: "ghost", size: "sm", title: props.sidebar ? "会话通信与相关对话" : "会话工具不可用:宿主未提供会话工具停靠接口", "aria-label": props.sidebar ? "会话通信与相关对话" : "会话工具不可用:宿主未提供会话工具停靠接口", disabled: !props.sidebar,
|
|
422
|
+
style: { flex: "none", width: 28, padding: 0, color: "var(--dsw-alias-label-secondary)" },
|
|
423
|
+
onClick: () => props.reveal(props.sessionId, false),
|
|
424
|
+
}, react.createElement(ReferenceIconRegular, { kind: "session", size: 16 }));
|
|
425
|
+
}
|
|
426
|
+
function registerCommunicationUI(ctx, store) {
|
|
427
|
+
let request = null;
|
|
428
|
+
const listeners = new Set();
|
|
429
|
+
const publish = value => { request = value; for (const listener of listeners) listener(); };
|
|
430
|
+
const subscribe = listener => { listeners.add(listener); return () => listeners.delete(listener); };
|
|
431
|
+
function Overlay() {
|
|
432
|
+
const current = react.useSyncExternalStore(subscribe, () => request);
|
|
433
|
+
return current ? react.createElement(CommunicationForm, { key: current.sessionId, request: current, store, close: () => publish(null) }) : null;
|
|
434
|
+
}
|
|
435
|
+
ctx.slots.inject("shell.overlay", () => ctx.slots.register({ name: "shell.overlay", id: "session-tools.communication" }, Overlay));
|
|
436
|
+
ctx.slots.inject("sidebar.workspaces.session.menu.item", () => ctx.slots.register({ name: "sidebar.workspaces.session.menu.item", id: "session-tools.communication", order: 350 },
|
|
437
|
+
({ sessionId, displayTitle, useMenuOpenState }) => {
|
|
438
|
+
const [, setMenuOpen] = useMenuOpenState();
|
|
439
|
+
return react.createElement(MenuItemButton, { onSelect: () => { setMenuOpen(false); publish({ sessionId, title: displayTitle }); } }, "会话通信…");
|
|
440
|
+
}));
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
const settingsEntry = "session-tools-bundle";
|
|
444
|
+
const settingsLocale = "session-tools.settings";
|
|
445
|
+
const settingsFields = ["defaultSend", "defaultReceive"];
|
|
446
|
+
const settingsCopy = {
|
|
447
|
+
zh: {
|
|
448
|
+
summary: "设置尚未单独配置的会话能否发送和接收消息。",
|
|
449
|
+
defaultSend: "默认允许向其他会话发送",
|
|
450
|
+
defaultSendHint: "关闭后,未单独设置的会话无法发送消息或创建带首条消息的会话。",
|
|
451
|
+
defaultReceive: "默认允许接收其他会话消息",
|
|
452
|
+
defaultReceiveHint: "关闭后,未单独设置的会话不会收到新的跨会话消息,插件也无法代建并投递首条消息。",
|
|
453
|
+
overridden: "已覆盖",
|
|
454
|
+
reset: "恢复默认",
|
|
455
|
+
unavailable: "宿主未提供会话通信默认设置。",
|
|
456
|
+
readOnly: "当前配置为只读。",
|
|
457
|
+
saveFailed: "无法保存。请检查当前值后重试。",
|
|
458
|
+
save: "保存",
|
|
459
|
+
saving: "正在保存…",
|
|
460
|
+
},
|
|
461
|
+
en: {
|
|
462
|
+
summary: "Choose whether sessions without their own settings can send and receive messages.",
|
|
463
|
+
defaultSend: "Allow sending to other sessions by default",
|
|
464
|
+
defaultSendHint: "When off, sessions without their own settings cannot send messages or create a session with a first message.",
|
|
465
|
+
defaultReceive: "Allow receiving from other sessions by default",
|
|
466
|
+
defaultReceiveHint: "When off, sessions without their own settings cannot receive new messages, including the first message in a session created by this plugin.",
|
|
467
|
+
overridden: "Overridden",
|
|
468
|
+
reset: "Reset to default",
|
|
469
|
+
unavailable: "The host does not expose session communication defaults.",
|
|
470
|
+
readOnly: "This configuration is read-only.",
|
|
471
|
+
saveFailed: "Could not save. Check the current values and try again.",
|
|
472
|
+
save: "Save",
|
|
473
|
+
saving: "Saving…",
|
|
474
|
+
},
|
|
475
|
+
};
|
|
476
|
+
const settingsRow = { display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 16,
|
|
477
|
+
fontSize: 13, lineHeight: 1.5, color: "var(--dsw-alias-label-primary)" };
|
|
478
|
+
const settingsActions = { display: "flex", alignItems: "center", flexWrap: "wrap", justifyContent: "flex-end", gap: 8 };
|
|
479
|
+
const settingsHint = { margin: 0, fontSize: 12, lineHeight: 1.5, color: "var(--dsw-alias-label-tertiary)" };
|
|
480
|
+
const settingsReset = { height: "auto", padding: 0, fontSize: 12, lineHeight: 1.5, color: "var(--dsw-alias-label-secondary)" };
|
|
481
|
+
|
|
482
|
+
function registerDefaultSettings(ctx) {
|
|
483
|
+
ctx.inject(["configForms", "locale"], scope => {
|
|
484
|
+
scope.effect(() => scope.locale.register(settingsLocale, settingsCopy));
|
|
485
|
+
const model = new SettingsFormModel(scope.configForms.get(settingsEntry), settingsFields.map(field => ({
|
|
486
|
+
field,
|
|
487
|
+
format: value => typeof value === "boolean" ? String(value) : "",
|
|
488
|
+
parse: text => text === "true" || text === "false" ? { kind: "set", value: text === "true" } : undefined,
|
|
489
|
+
})));
|
|
490
|
+
scope.effect(() => () => model.dispose());
|
|
491
|
+
const store = model.bind(() => ({
|
|
492
|
+
...model.shell(),
|
|
493
|
+
fields: Object.fromEntries(settingsFields.map(field => [field, model.field(field)])),
|
|
494
|
+
}));
|
|
495
|
+
const { edit, resetField, save, discard } = model.actions();
|
|
496
|
+
function DefaultSettings({ t, view }) {
|
|
497
|
+
const state = react.useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot);
|
|
498
|
+
if (view === "summary") return t("summary");
|
|
499
|
+
const disabled = !state.writable || state.saving;
|
|
500
|
+
const labels = Object.fromEntries(["unavailable", "readOnly", "saveFailed", "save", "saving"].map(key => [key, t(key)]));
|
|
501
|
+
return react.createElement(SettingsForm, { labels, state: { ...state, dirty: state.dirty && state.writable }, onSave: save, onDiscard: discard },
|
|
502
|
+
settingsFields.map((field, index) => {
|
|
503
|
+
const value = state.fields[field];
|
|
504
|
+
const label = t(field);
|
|
505
|
+
return react.createElement("div", { key: field, style: { display: "grid", gap: 6, padding: "12px 0",
|
|
506
|
+
borderTop: index ? "0.5px solid var(--dsw-alias-border-l2)" : undefined } },
|
|
507
|
+
react.createElement("div", { style: settingsRow },
|
|
508
|
+
react.createElement("span", { style: { flex: 1, minWidth: 0 } }, label),
|
|
509
|
+
react.createElement("div", { style: settingsActions },
|
|
510
|
+
value.overridden && react.createElement(Tag, { tone: "neutral" }, t("overridden")),
|
|
511
|
+
value.overridden && react.createElement(Button, {
|
|
512
|
+
type: "button", variant: "ghost", size: "sm", disabled, style: settingsReset,
|
|
513
|
+
"aria-label": `${t("reset")} · ${label}`, onClick: () => resetField(field),
|
|
514
|
+
}, t("reset")),
|
|
515
|
+
react.createElement(Switch, { checked: value.text === "true", disabled, label,
|
|
516
|
+
onChange: checked => edit(field, String(checked)) }))),
|
|
517
|
+
react.createElement("p", { style: settingsHint }, t(`${field}Hint`)));
|
|
518
|
+
}));
|
|
519
|
+
}
|
|
520
|
+
scope.effect(() => scope.configForms.whileServed([settingsEntry], () => scope.slots.inject("plugins.row.config", () => scope.slots.register({
|
|
521
|
+
name: "plugins.row.config",
|
|
522
|
+
key: "@ryuu-64/dsh-session-tools#" + settingsEntry,
|
|
523
|
+
locale: settingsLocale,
|
|
524
|
+
}, DefaultSettings))));
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Wait for the tool view slot list to exist, then register a keyed row that
|
|
530
|
+
* replaces the default card for `session_create`.
|
|
531
|
+
* @param ctx - the browser plugin context.
|
|
532
|
+
*/
|
|
533
|
+
function apply(ctx) {
|
|
534
|
+
registerDefaultSettings(ctx);
|
|
535
|
+
const communication = createCommunicationStore();
|
|
536
|
+
ctx.inject(["remote"], async scope => {
|
|
537
|
+
try {
|
|
538
|
+
await scope.remote.$mount(remoteContribution);
|
|
539
|
+
scope.inject(["remote.sessionCommunication"], ui => {
|
|
540
|
+
ui.effect(() => communication.connect(ui.remote.sessionCommunication));
|
|
541
|
+
registerCommunicationUI(ui, communication);
|
|
542
|
+
});
|
|
543
|
+
} catch(error) { console.error("[session-tools] communication UI initialization failed", error); throw error; }
|
|
544
|
+
});
|
|
545
|
+
ctx.slots.inject("tool.call.toolview", function* () {
|
|
546
|
+
for (const key of ["session_send", "session_wait"]) yield ctx.slots.register({ name: "tool.call.toolview", key }, MessageStateRow);
|
|
547
|
+
});
|
|
548
|
+
const register = (scope, openSession) => {
|
|
549
|
+
let capabilities = {};
|
|
550
|
+
const listeners = new Set();
|
|
551
|
+
const subscribe = listener => { listeners.add(listener); return () => listeners.delete(listener); };
|
|
552
|
+
const snapshot = () => capabilities;
|
|
553
|
+
scope.slots.inject("conversation.session.header.utilities", () => scope.slots.register({
|
|
554
|
+
name: "conversation.session.header.utilities", id: "session-tools.relations",
|
|
555
|
+
}, props => react.createElement(RelationsEntry, { ...props, ...react.useSyncExternalStore(subscribe, snapshot) })));
|
|
556
|
+
scope.inject(["sidebarRight", "sidebarRightTabs"], ui => {
|
|
557
|
+
const sidebar = ui.sidebarRight;
|
|
558
|
+
if (!["openTab", "tabsIn", "focus"].every(key => typeof sidebar[key] === "function") || typeof sidebar.mounted?.getSnapshot !== "function" || typeof sidebar.mounted?.subscribe !== "function" || typeof sidebar.tabDomain?.occurrence !== "function" || typeof ui.sidebarRightTabs.register !== "function") return;
|
|
559
|
+
const shown = new Set(), visibility = new Map();
|
|
560
|
+
const storageKey = id => "dsh-session-tools:relations-shown:" + id;
|
|
561
|
+
const wasShown = id => {
|
|
562
|
+
if (shown.has(id)) return true;
|
|
563
|
+
try { return localStorage.getItem(storageKey(id)) === "1"; } catch { return false; }
|
|
564
|
+
};
|
|
565
|
+
const markShown = id => {
|
|
566
|
+
shown.add(id);
|
|
567
|
+
try { localStorage.setItem(storageKey(id), "1"); } catch { /* In-memory suppression still respects a close this run. */ }
|
|
568
|
+
};
|
|
569
|
+
const reveal = (id, automatic) => {
|
|
570
|
+
if (ui.sidebarRight.mounted.getSnapshot() !== id) return;
|
|
571
|
+
const existing = ui.sidebarRight.tabsIn(id).find(tab => tab.kind === RELATIONS_KIND);
|
|
572
|
+
if (existing) {
|
|
573
|
+
// A restored layout must keep the user's selected tab and collapsed state.
|
|
574
|
+
if (!automatic) {
|
|
575
|
+
const occurrence = sidebar.tabDomain.occurrence(id, { id: existing.id });
|
|
576
|
+
if (typeof sidebar.revealOccurrence === "function") sidebar.revealOccurrence(occurrence);
|
|
577
|
+
else if (visibility.get(existing.id)) sidebar.focus(existing.id);
|
|
578
|
+
else occurrence.tabActions.openTab(RELATIONS_KIND);
|
|
579
|
+
}
|
|
580
|
+
} else ui.sidebarRight.openTab(RELATIONS_KIND);
|
|
581
|
+
if (ui.sidebarRight.tabsIn(id).some(tab => tab.kind === RELATIONS_KIND)) markShown(id);
|
|
582
|
+
};
|
|
583
|
+
ui.effect(() => ui.sidebarRightTabs.register({
|
|
584
|
+
id: RELATIONS_TAB, kind: RELATIONS_KIND, keepMounted: true,
|
|
585
|
+
title: () => "会话",
|
|
586
|
+
}));
|
|
587
|
+
ui.slots.inject("sidebar.right.pane.tab", () => ui.slots.register({
|
|
588
|
+
name: "sidebar.right.pane.tab", key: RELATIONS_TAB, inject: () => ({ openSession, visibility, communication }),
|
|
589
|
+
}, RelationsTab));
|
|
590
|
+
ui.effect(() => {
|
|
591
|
+
capabilities = { sidebar, reveal, wasShown };
|
|
592
|
+
listeners.forEach(listener => listener());
|
|
593
|
+
return () => { capabilities = {}; listeners.forEach(listener => listener()); };
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
return scope.slots.inject("tool.call.toolview", () => scope.slots.register(
|
|
597
|
+
{
|
|
598
|
+
name: "tool.call.toolview",
|
|
599
|
+
key: "session_create",
|
|
600
|
+
inject: () => ({ openSession }),
|
|
601
|
+
},
|
|
602
|
+
SessionCreateRow,
|
|
603
|
+
));
|
|
604
|
+
};
|
|
605
|
+
// Pre-0.2 hosts put UI selection on sessions. In 0.2 the Session
|
|
606
|
+
// Controller only owns data/references; UIWorkspace owns mainView
|
|
607
|
+
// retention, selection, panel reveal and release. Do not retain a
|
|
608
|
+
// second reference here or await a history load before navigating.
|
|
609
|
+
if (typeof ctx.sessions.open === "function") {
|
|
610
|
+
register(ctx, (sessionId) => ctx.sessions.open(sessionId));
|
|
611
|
+
} else {
|
|
612
|
+
// Package client.inject is not a startup-order guarantee. Wait for
|
|
613
|
+
// the actual service and bind the row to that service's lifetime.
|
|
614
|
+
ctx.inject(["uiWorkspace"], (scope) => register(scope,
|
|
615
|
+
(sessionId) => scope.uiWorkspace.openSession(sessionId)));
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
exports.apply = apply;
|
|
620
|
+
exports.inject = inject;
|
|
621
|
+
return module.exports;
|
|
622
|
+
},
|
|
623
|
+
});
|