@tanzz/dsh-explore 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/README.md +151 -2
- package/client.js +3119 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +26 -0
- package/index.js +2 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +53 -4
package/client.js
ADDED
|
@@ -0,0 +1,3119 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@tanzz/dsh-explore',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react');
|
|
5
|
+
const h = React.createElement;
|
|
6
|
+
const { useState, useEffect, useRef, useMemo, useCallback } = React;
|
|
7
|
+
|
|
8
|
+
// ---------------------------------------------------------------- design
|
|
9
|
+
// Every colour is a DSH alias token, so the view follows the live theme:
|
|
10
|
+
// the shell flips `body[data-ds-dark-theme]` and the tokens move with it.
|
|
11
|
+
const C = {
|
|
12
|
+
accent: 'var(--dsw-alias-brand-primary)',
|
|
13
|
+
accentSoft: 'color-mix(in srgb, var(--dsw-alias-brand-primary) 12%, transparent)',
|
|
14
|
+
// The live-progress chip is the one blue accent in the view. It must use
|
|
15
|
+
// the business accent, not brand-primary: brand-primary is near-black in
|
|
16
|
+
// the light theme and would turn the chip grey.
|
|
17
|
+
accentEdge: 'color-mix(in srgb, var(--dsw-alias-state-business-primary) 18%, transparent)',
|
|
18
|
+
// `--dsw-alias-brand-primary` inverts between themes (near-black in light,
|
|
19
|
+
// near-white in dark), so text painted on it must use the paired
|
|
20
|
+
// foreground token instead of a literal white.
|
|
21
|
+
onAccent: 'var(--dsw-alias-label-primary-foreground)',
|
|
22
|
+
buttonFill: 'var(--dsw-alias-button-primary-fill)',
|
|
23
|
+
ink: 'var(--dsw-alias-label-primary)',
|
|
24
|
+
ink2: 'var(--dsw-alias-label-secondary)',
|
|
25
|
+
ink3: 'var(--dsw-alias-label-secondary)',
|
|
26
|
+
line: 'var(--dsw-alias-border-l2)',
|
|
27
|
+
card: 'var(--dsw-alias-bg-layer-1)',
|
|
28
|
+
cardEdge: 'var(--dsw-alias-border-l1)',
|
|
29
|
+
canvas: 'var(--dsw-alias-bg-base)',
|
|
30
|
+
code: 'var(--dsw-alias-markdown-code-block)',
|
|
31
|
+
// A `label-primary` tint keeps the chip visible on a white card (the
|
|
32
|
+
// markdown inline-code token is nearly white in the light theme).
|
|
33
|
+
codeInline: 'color-mix(in srgb, var(--dsw-alias-label-primary) 7%, transparent)',
|
|
34
|
+
scrim: 'var(--dsw-alias-bg-mask-1)',
|
|
35
|
+
warn: 'var(--dsw-alias-state-error-primary)',
|
|
36
|
+
ok: 'var(--dsw-alias-state-success-primary)',
|
|
37
|
+
};
|
|
38
|
+
const NODE_W = 320;
|
|
39
|
+
const NODE_H = 212;
|
|
40
|
+
const GAP_X = 44;
|
|
41
|
+
const GAP_Y = 76;
|
|
42
|
+
// Font families follow Settings → Appearance. DSH composes the user's list
|
|
43
|
+
// with its own default stack on body (`--dsw-font-family` /
|
|
44
|
+
// `--ds-font-family-code`), so reading the composed tokens keeps glyph
|
|
45
|
+
// fallback intact; the literal stacks are the fallback for a profile
|
|
46
|
+
// without the theme plugin.
|
|
47
|
+
const FONT = 'var(--dsw-font-family, system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif)';
|
|
48
|
+
const MONO = 'var(--ds-font-family-code, ui-monospace, SFMono-Regular, Menlo, monospace)';
|
|
49
|
+
|
|
50
|
+
// ------------------------------------------------------------------ locale
|
|
51
|
+
// Visible text must follow the shell language, so every string is a key
|
|
52
|
+
// looked up through the Client locale service. The zh set is the source of
|
|
53
|
+
// truth; en mirrors it key for key. Other registered languages fall back to
|
|
54
|
+
// English through the service's own fallback chain.
|
|
55
|
+
const NS = 'dsh-explore';
|
|
56
|
+
|
|
57
|
+
const DICT = {
|
|
58
|
+
zh: {
|
|
59
|
+
'view.tab': '无限探索',
|
|
60
|
+
'view.heading': '无限探索',
|
|
61
|
+
'view.nodes': '{count} 个节点',
|
|
62
|
+
'view.running': '{count} 条分支在跑',
|
|
63
|
+
'view.runningTitle': '跳到下一条在跑的节点',
|
|
64
|
+
'view.awaiting': '{count} 个在等你',
|
|
65
|
+
'view.awaitingTitle': '跳到正在等你的节点',
|
|
66
|
+
'view.loadOlder': '加载更早',
|
|
67
|
+
'view.restoreHidden': '已隐藏 {count} 个 · 恢复',
|
|
68
|
+
'view.restoreHiddenTitle': '恢复被删除的节点和分支',
|
|
69
|
+
'view.searchPlaceholder': '搜索节点',
|
|
70
|
+
'view.searchLabel': '搜索节点',
|
|
71
|
+
'view.zoomGroup': '画布缩放',
|
|
72
|
+
'view.zoomOut': '缩小画布',
|
|
73
|
+
'view.zoomIn': '放大画布',
|
|
74
|
+
'view.zoomLevel': '当前缩放比例',
|
|
75
|
+
'view.fit': '适应画布',
|
|
76
|
+
'view.fitTitle': '缩放到能看见整棵树',
|
|
77
|
+
'view.latest': '定位最新',
|
|
78
|
+
'view.latestTitle': '展开路径并定位到最新创建的节点',
|
|
79
|
+
'view.latestTitleEmpty': '还没有节点',
|
|
80
|
+
'view.empty.title': '这棵树还是空的',
|
|
81
|
+
'view.empty.hint': '回到 Chat 问一句,这里就会长出第一个节点。',
|
|
82
|
+
'view.empty.report': '若已经问过却是空的,把这一行告我:事件 {events} · 会话 {state}。',
|
|
83
|
+
|
|
84
|
+
'card.noQuestion': '(无问题)',
|
|
85
|
+
'card.questionReply': '(回答上一轮的提问)',
|
|
86
|
+
'card.latestBadge': '最新',
|
|
87
|
+
'card.latestTitle': '这是最新创建的节点',
|
|
88
|
+
'card.status.thinking': '思考中',
|
|
89
|
+
'card.status.streaming': '回答中',
|
|
90
|
+
'card.status.error': '出错',
|
|
91
|
+
'card.status.stopped': '已中止',
|
|
92
|
+
'card.status.done': '已完成',
|
|
93
|
+
'card.status.awaiting': '等待回答',
|
|
94
|
+
'card.status.awaitingApproval': '等待授权',
|
|
95
|
+
'card.stop': '停止',
|
|
96
|
+
'card.stopTitle': '停止这条分支',
|
|
97
|
+
'card.ask': '在这条分支上继续追问',
|
|
98
|
+
'card.more': '更多操作',
|
|
99
|
+
'card.delete': '删除这个节点',
|
|
100
|
+
'card.deleteTitle': '隐藏这个节点和它下面的所有分支',
|
|
101
|
+
'card.deleteConfirm': '删除这个节点和它下面的所有分支?(随时可恢复)',
|
|
102
|
+
'card.readTitle': '点击全屏阅读',
|
|
103
|
+
'card.expandTitle': '展开整段回答',
|
|
104
|
+
'card.collapseTitle': '收起这段回答',
|
|
105
|
+
'card.expand': '展开回答',
|
|
106
|
+
'card.collapse': '收起回答',
|
|
107
|
+
'card.fullscreen': '全屏阅读',
|
|
108
|
+
'card.fullscreenTitle': '全屏阅读完整回答',
|
|
109
|
+
'card.unfoldBranch': '展开这一枝',
|
|
110
|
+
'card.foldBranch': '收起这一枝',
|
|
111
|
+
'card.unfoldDescendants': '展开 {count} 个后代',
|
|
112
|
+
'card.foldDescendants': '收起 {count} 个后代',
|
|
113
|
+
'card.descendants': '{count} 个后代',
|
|
114
|
+
'card.truncated': '\n\n…(点开全屏看完整回答)',
|
|
115
|
+
'card.cost.elapsed': '耗时 {value}',
|
|
116
|
+
'card.cost.context': '上下文 {value}',
|
|
117
|
+
'card.cost.cached': '缓存 {value}',
|
|
118
|
+
|
|
119
|
+
'live.calling': '正在调用 {tool}',
|
|
120
|
+
'live.outputting': '正在输出…',
|
|
121
|
+
'live.thinking': '正在思考…',
|
|
122
|
+
'live.starting': '正在开始…',
|
|
123
|
+
'live.awaiting': '等你回答…',
|
|
124
|
+
'live.awaitingApproval': '等你授权…',
|
|
125
|
+
'live.step': '第 {step} 步',
|
|
126
|
+
|
|
127
|
+
'composer.placeholder': '在这条分支上继续问…(Enter 发送,Shift+Enter 换行)',
|
|
128
|
+
'composer.label': '追问内容',
|
|
129
|
+
'composer.from': '从「{question}…」出发',
|
|
130
|
+
'composer.cancel': '取消',
|
|
131
|
+
'composer.send': '发送',
|
|
132
|
+
|
|
133
|
+
'reader.close': '关闭(Esc)',
|
|
134
|
+
'reader.closeLabel': '关闭阅读视图',
|
|
135
|
+
'reader.noAnswer': '(还没有回答)',
|
|
136
|
+
'reader.hint': 'Esc 关闭 · 点背景关闭',
|
|
137
|
+
'reader.branch': '分支',
|
|
138
|
+
'reader.mainline': '主线',
|
|
139
|
+
|
|
140
|
+
'error.title': '树视图出错了',
|
|
141
|
+
'error.reload': '重新载入视图',
|
|
142
|
+
'error.hint': '画布数据没有丢,只是这一次渲染炸了。',
|
|
143
|
+
|
|
144
|
+
'error.sessionNotOpen': '会话还没打开,无法投影。',
|
|
145
|
+
'error.noBinding': '会话没有返回绑定',
|
|
146
|
+
'error.openFailed': '打不开会话:{reason}',
|
|
147
|
+
'error.forkFailed': '分叉失败:{reason}',
|
|
148
|
+
'error.branchUnopenable': '无法打开分支会话:{reason}',
|
|
149
|
+
'error.branchNoBinding': '打不开分支会话:{reason}',
|
|
150
|
+
'error.branchNoUsableBinding': '分支会话没有返回可用的绑定。',
|
|
151
|
+
'error.submitFailed': '提交失败:{reason}',
|
|
152
|
+
'error.loadOlderFailed': '加载更早历史失败:{reason}',
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
en: {
|
|
156
|
+
'view.tab': 'Infinite Exploration',
|
|
157
|
+
'view.heading': 'Infinite Exploration',
|
|
158
|
+
'view.nodes': '{count} nodes',
|
|
159
|
+
'view.running': '{count} branches running',
|
|
160
|
+
'view.runningTitle': 'Jump to the next running node',
|
|
161
|
+
'view.awaiting': '{count} waiting on you',
|
|
162
|
+
'view.awaitingTitle': 'Jump to the node waiting on you',
|
|
163
|
+
'view.loadOlder': 'Load earlier',
|
|
164
|
+
'view.restoreHidden': '{count} hidden · Restore',
|
|
165
|
+
'view.restoreHiddenTitle': 'Restore deleted nodes and branches',
|
|
166
|
+
'view.searchPlaceholder': 'Search nodes',
|
|
167
|
+
'view.searchLabel': 'Search nodes',
|
|
168
|
+
'view.zoomGroup': 'Canvas zoom',
|
|
169
|
+
'view.zoomOut': 'Zoom out',
|
|
170
|
+
'view.zoomIn': 'Zoom in',
|
|
171
|
+
'view.zoomLevel': 'Current zoom level',
|
|
172
|
+
'view.fit': 'Fit canvas',
|
|
173
|
+
'view.fitTitle': 'Zoom out to show the whole tree',
|
|
174
|
+
'view.latest': 'Jump to latest',
|
|
175
|
+
'view.latestTitle': 'Expand the path and jump to the newest node',
|
|
176
|
+
'view.latestTitleEmpty': 'No nodes yet',
|
|
177
|
+
'view.empty.title': 'This tree is still empty',
|
|
178
|
+
'view.empty.hint': 'Ask something in Chat and the first node will grow here.',
|
|
179
|
+
'view.empty.report': 'If you already asked and this is still empty, tell me: {events} events · session {state}.',
|
|
180
|
+
|
|
181
|
+
'card.noQuestion': '(no question)',
|
|
182
|
+
'card.questionReply': '(replying to the previous question)',
|
|
183
|
+
'card.latestBadge': 'Latest',
|
|
184
|
+
'card.latestTitle': 'The newest node in this tree',
|
|
185
|
+
'card.status.thinking': 'Thinking',
|
|
186
|
+
'card.status.streaming': 'Answering',
|
|
187
|
+
'card.status.error': 'Error',
|
|
188
|
+
'card.status.stopped': 'Stopped',
|
|
189
|
+
'card.status.done': 'Done',
|
|
190
|
+
'card.status.awaiting': 'Needs answer',
|
|
191
|
+
'card.status.awaitingApproval': 'Needs approval',
|
|
192
|
+
'card.stop': 'Stop',
|
|
193
|
+
'card.stopTitle': 'Stop this branch',
|
|
194
|
+
'card.ask': 'Follow up on this branch',
|
|
195
|
+
'card.more': 'More actions',
|
|
196
|
+
'card.delete': 'Delete this node',
|
|
197
|
+
'card.deleteTitle': 'Hide this node and every branch under it',
|
|
198
|
+
'card.deleteConfirm': 'Delete this node and all branches under it? (reversible)',
|
|
199
|
+
'card.readTitle': 'Click to read in full screen',
|
|
200
|
+
'card.expandTitle': 'Expand this answer',
|
|
201
|
+
'card.collapseTitle': 'Collapse this answer',
|
|
202
|
+
'card.expand': 'Expand answer',
|
|
203
|
+
'card.collapse': 'Collapse answer',
|
|
204
|
+
'card.fullscreen': 'Read full screen',
|
|
205
|
+
'card.fullscreenTitle': 'Read the complete answer in full screen',
|
|
206
|
+
'card.unfoldBranch': 'Expand this branch',
|
|
207
|
+
'card.foldBranch': 'Collapse this branch',
|
|
208
|
+
'card.unfoldDescendants': 'Expand {count} descendants',
|
|
209
|
+
'card.foldDescendants': 'Collapse {count} descendants',
|
|
210
|
+
'card.descendants': '{count} descendants',
|
|
211
|
+
'card.truncated': '\n\n… (open full screen for the complete answer)',
|
|
212
|
+
'card.cost.elapsed': 'took {value}',
|
|
213
|
+
'card.cost.context': 'context {value}',
|
|
214
|
+
'card.cost.cached': 'cached {value}',
|
|
215
|
+
|
|
216
|
+
'live.calling': 'Calling {tool}',
|
|
217
|
+
'live.outputting': 'Writing…',
|
|
218
|
+
'live.thinking': 'Thinking…',
|
|
219
|
+
'live.starting': 'Starting…',
|
|
220
|
+
'live.awaiting': 'Waiting for your answer…',
|
|
221
|
+
'live.awaitingApproval': 'Waiting for your approval…',
|
|
222
|
+
'live.step': 'step {step}',
|
|
223
|
+
|
|
224
|
+
'composer.placeholder': 'Ask a follow-up on this branch… (Enter to send, Shift+Enter for a new line)',
|
|
225
|
+
'composer.label': 'Follow-up text',
|
|
226
|
+
'composer.from': 'Starting from "{question}…"',
|
|
227
|
+
'composer.cancel': 'Cancel',
|
|
228
|
+
'composer.send': 'Send',
|
|
229
|
+
|
|
230
|
+
'reader.close': 'Close (Esc)',
|
|
231
|
+
'reader.closeLabel': 'Close the reader',
|
|
232
|
+
'reader.noAnswer': '(no answer yet)',
|
|
233
|
+
'reader.hint': 'Esc or click the backdrop to close',
|
|
234
|
+
'reader.branch': 'Branch',
|
|
235
|
+
'reader.mainline': 'Main line',
|
|
236
|
+
|
|
237
|
+
'error.title': 'The tree view crashed',
|
|
238
|
+
'error.reload': 'Reload the view',
|
|
239
|
+
'error.hint': 'The canvas data is safe; only this render failed.',
|
|
240
|
+
|
|
241
|
+
'error.sessionNotOpen': 'The session is not open yet, so it cannot be projected.',
|
|
242
|
+
'error.noBinding': 'The session returned no binding.',
|
|
243
|
+
'error.openFailed': 'Could not open the session: {reason}',
|
|
244
|
+
'error.forkFailed': 'Fork failed: {reason}',
|
|
245
|
+
'error.branchUnopenable': 'Could not open the branch session: {reason}',
|
|
246
|
+
'error.branchNoBinding': 'Could not open the branch session: {reason}',
|
|
247
|
+
'error.branchNoUsableBinding': 'The branch session returned no usable binding.',
|
|
248
|
+
'error.submitFailed': 'Submit failed: {reason}',
|
|
249
|
+
'error.loadOlderFailed': 'Could not load earlier history: {reason}',
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const localeListeners = new Set();
|
|
254
|
+
|
|
255
|
+
/** Look a key up in the built-in table when the locale service is absent. */
|
|
256
|
+
function fallbackTranslate(key, params) {
|
|
257
|
+
const lang = typeof document !== 'undefined' && document.documentElement.lang
|
|
258
|
+
? String(document.documentElement.lang).slice(0, 2).toLowerCase()
|
|
259
|
+
: 'zh';
|
|
260
|
+
const table = DICT[lang] || DICT.zh;
|
|
261
|
+
const template = table[key] ?? DICT.zh[key] ?? key;
|
|
262
|
+
if (!params) return template;
|
|
263
|
+
return template.replace(/\{(\w+)\}/g, (match, name) => (name in params ? String(params[name]) : match));
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** The live translator; `apply` upgrades it to the locale service. */
|
|
267
|
+
const localeBridge = {
|
|
268
|
+
translate: fallbackTranslate,
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/** Translate a key; call inside components so a locale switch re-renders. */
|
|
272
|
+
function T(key, params) {
|
|
273
|
+
return localeBridge.translate(key, params);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Re-render the caller whenever the active language changes. */
|
|
277
|
+
function useLocale() {
|
|
278
|
+
const [, force] = useState(0);
|
|
279
|
+
useEffect(() => {
|
|
280
|
+
const listener = () => force((n) => n + 1);
|
|
281
|
+
localeListeners.add(listener);
|
|
282
|
+
return () => localeListeners.delete(listener);
|
|
283
|
+
}, []);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
// ------------------------------------------------------------- utilities
|
|
288
|
+
function textOf(content) {
|
|
289
|
+
if (typeof content === 'string') return content;
|
|
290
|
+
if (!Array.isArray(content)) return '';
|
|
291
|
+
let out = '';
|
|
292
|
+
for (const block of content) {
|
|
293
|
+
if (block && block.type === 'text' && typeof block.text === 'string') out += block.text;
|
|
294
|
+
}
|
|
295
|
+
return out;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function seqOf(entry) {
|
|
299
|
+
const event = entry && entry.event;
|
|
300
|
+
return event && typeof event.seq === 'number' ? event.seq : 0;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function maxDurableSeq(entries) {
|
|
304
|
+
let max = 0;
|
|
305
|
+
for (const entry of entries) {
|
|
306
|
+
const event = entry && entry.event;
|
|
307
|
+
if (entry.type === 'event' && event && typeof event.seq === 'number' && event.seq > max) max = event.seq;
|
|
308
|
+
}
|
|
309
|
+
return max;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Cost of one session window's newest turn: wall clock + last-step context. */
|
|
313
|
+
function turnCost(entries, baselineSeq) {
|
|
314
|
+
if (!Array.isArray(entries)) return { ms: 0, tokens: 0, cached: 0, running: false };
|
|
315
|
+
const floor = typeof baselineSeq === 'number' && baselineSeq > 0 ? baselineSeq : 0;
|
|
316
|
+
let start = 0;
|
|
317
|
+
let end = 0;
|
|
318
|
+
let newest = 0;
|
|
319
|
+
let tokens = 0;
|
|
320
|
+
let cached = 0;
|
|
321
|
+
for (const entry of entries) {
|
|
322
|
+
const event = entry && entry.event;
|
|
323
|
+
if (!event || entry.type !== 'event') continue;
|
|
324
|
+
const seq = typeof event.seq === 'number' ? event.seq : 0;
|
|
325
|
+
if (seq <= floor) continue;
|
|
326
|
+
const time = typeof event.time === 'number' ? event.time : 0;
|
|
327
|
+
const data = event.data || {};
|
|
328
|
+
if (event.type === 'turn/start') {
|
|
329
|
+
// A window can hold several turns; only the newest one is measured.
|
|
330
|
+
start = time; end = 0; newest = time; tokens = 0; cached = 0;
|
|
331
|
+
} else if (event.type === 'assistant/message') {
|
|
332
|
+
// One turn is N steps, and every step repeats the whole context in
|
|
333
|
+
// `usage`: summing totalTokens over a turn bills it once per step
|
|
334
|
+
// (10x-62x in a real log). The final step is the footprint a fork
|
|
335
|
+
// inherits, so keep that and report cache reads beside it.
|
|
336
|
+
const usage = data.usage;
|
|
337
|
+
if (usage && typeof usage.totalTokens === 'number') tokens = usage.totalTokens;
|
|
338
|
+
if (usage && typeof usage.cacheReadTokens === 'number') cached = usage.cacheReadTokens;
|
|
339
|
+
if (time > newest) newest = time;
|
|
340
|
+
} else if (event.type === 'turn/end') {
|
|
341
|
+
if (time) end = time;
|
|
342
|
+
if (time > newest) newest = time;
|
|
343
|
+
} else if (!end && time > newest) {
|
|
344
|
+
newest = time;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const stop = end || newest;
|
|
348
|
+
return {
|
|
349
|
+
ms: start && stop > start ? stop - start : 0,
|
|
350
|
+
tokens: tokens,
|
|
351
|
+
cached: cached,
|
|
352
|
+
running: Boolean(start) && !end,
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Short numeric label for a cost: `12s · 18.3k tok`. Latin and units only. */
|
|
357
|
+
function formatCost(cost) {
|
|
358
|
+
if (!cost) return '';
|
|
359
|
+
const short = (value) => {
|
|
360
|
+
if (value >= 1000000) return (value / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
|
|
361
|
+
if (value >= 100000) return Math.round(value / 1000) + 'k';
|
|
362
|
+
if (value >= 1000) return (value / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
|
|
363
|
+
return String(Math.round(value));
|
|
364
|
+
};
|
|
365
|
+
const parts = [];
|
|
366
|
+
const ms = cost.ms > 0 ? Math.round(cost.ms) : 0;
|
|
367
|
+
const tokens = cost.tokens > 0 ? Math.round(cost.tokens) : 0;
|
|
368
|
+
if (ms > 0) {
|
|
369
|
+
const seconds = Math.max(1, Math.round(ms / 1000));
|
|
370
|
+
if (seconds < 60) parts.push(seconds + 's');
|
|
371
|
+
else if (seconds < 3600) parts.push(Math.floor(seconds / 60) + 'm' + ('0' + (seconds % 60)).slice(-2) + 's');
|
|
372
|
+
else parts.push(Math.floor(seconds / 3600) + 'h' + ('0' + Math.floor((seconds % 3600) / 60)).slice(-2) + 'm');
|
|
373
|
+
}
|
|
374
|
+
if (tokens > 0) parts.push(short(tokens) + ' tok');
|
|
375
|
+
return parts.join(' · ');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// The host names a human prompt `source.kind: 'user'` (the `user-rpc` source
|
|
379
|
+
// key carries that same kind, and a slash command is still `'user'`), and a
|
|
380
|
+
// steer that answers a pending question `'user-question-reply'`. The old
|
|
381
|
+
// list held `user-rpc` / `user_command` / `command`, which are not `kind`
|
|
382
|
+
// values the host ever emits, so those turns — every question reply included
|
|
383
|
+
// — were projected away entirely.
|
|
384
|
+
const PROMPT_KINDS = ['user', 'user-question-reply'];
|
|
385
|
+
|
|
386
|
+
function sourceKindOf(data) {
|
|
387
|
+
const source = data && data.source;
|
|
388
|
+
if (!source) return undefined;
|
|
389
|
+
return source.kind || source.type;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
function isRealPrompt(data) {
|
|
393
|
+
const kind = sourceKindOf(data);
|
|
394
|
+
if (kind === undefined) return false;
|
|
395
|
+
return PROMPT_KINDS.indexOf(kind) >= 0;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/** A question reply is a JSON envelope; surface the question, not the blob. */
|
|
399
|
+
function questionReplyTextOf(data) {
|
|
400
|
+
const raw = textOf(data.content).trim();
|
|
401
|
+
if (!raw) return '';
|
|
402
|
+
try {
|
|
403
|
+
const parsed = JSON.parse(raw);
|
|
404
|
+
const questions = parsed && parsed.questions;
|
|
405
|
+
if (Array.isArray(questions) && questions[0] && typeof questions[0].question === 'string') {
|
|
406
|
+
return questions[0].question;
|
|
407
|
+
}
|
|
408
|
+
} catch (error) {
|
|
409
|
+
/* not the envelope: keep whatever text the host wrote */
|
|
410
|
+
}
|
|
411
|
+
return raw;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** Fold a session's event window into question/answer turns. */
|
|
415
|
+
function projectTurns(entries, sessionRunning) {
|
|
416
|
+
const turns = [];
|
|
417
|
+
let current = null;
|
|
418
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
419
|
+
const entry = entries[index];
|
|
420
|
+
if (!entry || !entry.event) continue;
|
|
421
|
+
const event = entry.event;
|
|
422
|
+
const data = event.data || {};
|
|
423
|
+
if (entry.type === 'transient') {
|
|
424
|
+
// Live tokens are not durable events; they only feed the turn that is
|
|
425
|
+
// still being written, so a settled card keeps its final answer.
|
|
426
|
+
if (event.type === 'assistant/live-chunk' && current) {
|
|
427
|
+
const chunk = data.chunk;
|
|
428
|
+
if (chunk && chunk.type === 'text-delta' && typeof chunk.text === 'string') {
|
|
429
|
+
current.streamText = (current.streamText || '') + chunk.text;
|
|
430
|
+
current.answer = current.streamText;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
continue;
|
|
434
|
+
}
|
|
435
|
+
if (entry.type !== 'event') continue;
|
|
436
|
+
if (event.type === 'turn/start') {
|
|
437
|
+
current = {
|
|
438
|
+
question: '', answer: '', answerSeq: 0, qSeq: 0, status: 'running',
|
|
439
|
+
real: false, kind: '', costFrom: index, streamText: '',
|
|
440
|
+
step: 0, tool: '', startedAt: typeof event.time === 'number' ? event.time : 0,
|
|
441
|
+
awaiting: '', pendingQuestions: new Set(), pendingApprovals: new Set(),
|
|
442
|
+
};
|
|
443
|
+
turns.push(current);
|
|
444
|
+
} else if (event.type === 'step/start') {
|
|
445
|
+
if (current) current.step = typeof data.step === 'number' ? data.step : current.step + 1;
|
|
446
|
+
} else if (event.type === 'tool/call') {
|
|
447
|
+
if (current) {
|
|
448
|
+
if (data.name) current.tool = String(data.name);
|
|
449
|
+
// A pending ask_user_question leaves only this call in the log: the
|
|
450
|
+
// turn cannot end while the tool awaits the human, so the call is
|
|
451
|
+
// the whole signal that the branch is blocked on the user.
|
|
452
|
+
if (data.name === 'ask_user_question' && data.callId) {
|
|
453
|
+
current.pendingQuestions.add(String(data.callId));
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
} else if (event.type === 'tool/result') {
|
|
457
|
+
if (current) {
|
|
458
|
+
current.tool = '';
|
|
459
|
+
const settled = data.message && data.message.toolCallId;
|
|
460
|
+
if (settled) current.pendingQuestions.delete(String(settled));
|
|
461
|
+
else if (current.pendingQuestions.size) current.pendingQuestions.clear();
|
|
462
|
+
}
|
|
463
|
+
} else if (event.type === 'approval/asked') {
|
|
464
|
+
const target = current || turns[turns.length - 1];
|
|
465
|
+
if (target && data.id) target.pendingApprovals.add(String(data.id));
|
|
466
|
+
} else if (event.type === 'approval/decided') {
|
|
467
|
+
const target = current || turns[turns.length - 1];
|
|
468
|
+
if (target && data.id) target.pendingApprovals.delete(String(data.id));
|
|
469
|
+
} else if (event.type === 'user/message') {
|
|
470
|
+
const target = current || turns[turns.length - 1];
|
|
471
|
+
if (target) {
|
|
472
|
+
const text = textOf(data.content);
|
|
473
|
+
if (text.trim() && isRealPrompt(data)) {
|
|
474
|
+
const kind = sourceKindOf(data) || '';
|
|
475
|
+
target.question = kind === 'user-question-reply'
|
|
476
|
+
? (questionReplyTextOf(data) || T('card.questionReply'))
|
|
477
|
+
: text;
|
|
478
|
+
target.real = true;
|
|
479
|
+
target.kind = kind;
|
|
480
|
+
target.qSeq = typeof event.seq === 'number' ? event.seq : 0;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
} else if (event.type === 'assistant/message') {
|
|
484
|
+
const target = current || turns[turns.length - 1];
|
|
485
|
+
if (target && data.message) {
|
|
486
|
+
const text = textOf(data.message.content);
|
|
487
|
+
if (text.trim()) {
|
|
488
|
+
target.answer = text;
|
|
489
|
+
target.streamText = text;
|
|
490
|
+
}
|
|
491
|
+
target.answerSeq = typeof event.seq === 'number' ? event.seq : target.answerSeq;
|
|
492
|
+
}
|
|
493
|
+
} else if (event.type === 'turn/end') {
|
|
494
|
+
const target = current || turns[turns.length - 1];
|
|
495
|
+
if (target) {
|
|
496
|
+
target.status = target.answer ? 'done' : 'stopped';
|
|
497
|
+
target.tool = '';
|
|
498
|
+
target.streamText = '';
|
|
499
|
+
target.awaiting = '';
|
|
500
|
+
target.pendingQuestions.clear();
|
|
501
|
+
target.pendingApprovals.clear();
|
|
502
|
+
}
|
|
503
|
+
if (current && typeof current.costFrom === 'number') {
|
|
504
|
+
current.cost = turnCost(entries.slice(current.costFrom, index + 1), 0);
|
|
505
|
+
}
|
|
506
|
+
current = null;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
// A turn that has not ended yet still costs something: measure to the tail.
|
|
510
|
+
if (current && typeof current.costFrom === 'number') {
|
|
511
|
+
current.cost = turnCost(entries.slice(current.costFrom), 0);
|
|
512
|
+
}
|
|
513
|
+
// An open turn only means "回答中" while the session is actually running;
|
|
514
|
+
// a crashed or reloaded tail must not look like it is still thinking.
|
|
515
|
+
if (sessionRunning === false) {
|
|
516
|
+
for (const turn of turns) if (turn.status === 'running') turn.status = turn.answer ? 'done' : 'stopped';
|
|
517
|
+
}
|
|
518
|
+
// "Waiting on you" is only true while the turn is genuinely open: the host
|
|
519
|
+
// appends no turn/end while ask_user_question (or an approval) blocks, and
|
|
520
|
+
// keeps reporting the session as running.
|
|
521
|
+
for (const turn of turns) {
|
|
522
|
+
turn.awaiting = turn.status === 'running'
|
|
523
|
+
? (turn.pendingQuestions.size ? 'question' : turn.pendingApprovals.size ? 'approval' : '')
|
|
524
|
+
: '';
|
|
525
|
+
delete turn.pendingQuestions;
|
|
526
|
+
delete turn.pendingApprovals;
|
|
527
|
+
}
|
|
528
|
+
return turns.filter((turn) => turn.real && (turn.question.trim() || turn.answer.trim()));
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Read one branch session's own events (everything after `baseline`). */
|
|
532
|
+
function readBranch(entries, baseline) {
|
|
533
|
+
let text = '';
|
|
534
|
+
let answer = '';
|
|
535
|
+
let answerSeq = 0;
|
|
536
|
+
let ended = false;
|
|
537
|
+
let step = 0;
|
|
538
|
+
let tool = '';
|
|
539
|
+
let startedAt = 0;
|
|
540
|
+
const pendingQuestions = new Set();
|
|
541
|
+
const pendingApprovals = new Set();
|
|
542
|
+
const cost = turnCost(entries, baseline);
|
|
543
|
+
for (const entry of entries) {
|
|
544
|
+
const event = entry && entry.event;
|
|
545
|
+
if (!event) continue;
|
|
546
|
+
if (entry.type === 'transient') {
|
|
547
|
+
if (event.type === 'assistant/live-chunk' && seqOf(entry) > baseline) {
|
|
548
|
+
const chunk = event.data && event.data.chunk;
|
|
549
|
+
if (chunk && chunk.type === 'text-delta' && typeof chunk.text === 'string') text += chunk.text;
|
|
550
|
+
}
|
|
551
|
+
continue;
|
|
552
|
+
}
|
|
553
|
+
if (entry.type !== 'event') continue;
|
|
554
|
+
const seq = typeof event.seq === 'number' ? event.seq : 0;
|
|
555
|
+
if (seq <= baseline) continue;
|
|
556
|
+
if (event.type === 'turn/start') {
|
|
557
|
+
startedAt = typeof event.time === 'number' ? event.time : startedAt;
|
|
558
|
+
step = 0;
|
|
559
|
+
tool = '';
|
|
560
|
+
pendingQuestions.clear();
|
|
561
|
+
pendingApprovals.clear();
|
|
562
|
+
} else if (event.type === 'step/start') {
|
|
563
|
+
const data = event.data || {};
|
|
564
|
+
step = typeof data.step === 'number' ? data.step : step + 1;
|
|
565
|
+
} else if (event.type === 'tool/call') {
|
|
566
|
+
const data = event.data || {};
|
|
567
|
+
if (data.name) tool = String(data.name);
|
|
568
|
+
if (data.name === 'ask_user_question' && data.callId) pendingQuestions.add(String(data.callId));
|
|
569
|
+
} else if (event.type === 'tool/result') {
|
|
570
|
+
tool = '';
|
|
571
|
+
const data = event.data || {};
|
|
572
|
+
const settled = data.message && data.message.toolCallId;
|
|
573
|
+
if (settled) pendingQuestions.delete(String(settled));
|
|
574
|
+
else if (pendingQuestions.size) pendingQuestions.clear();
|
|
575
|
+
} else if (event.type === 'approval/asked') {
|
|
576
|
+
const data = event.data || {};
|
|
577
|
+
if (data.id) pendingApprovals.add(String(data.id));
|
|
578
|
+
} else if (event.type === 'approval/decided') {
|
|
579
|
+
const data = event.data || {};
|
|
580
|
+
if (data.id) pendingApprovals.delete(String(data.id));
|
|
581
|
+
} else if (event.type === 'assistant/message') {
|
|
582
|
+
const data = event.data || {};
|
|
583
|
+
if (data.message) {
|
|
584
|
+
const finalText = textOf(data.message.content);
|
|
585
|
+
if (finalText.trim()) {
|
|
586
|
+
answer = finalText;
|
|
587
|
+
text = finalText;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
answerSeq = seq;
|
|
591
|
+
} else if (event.type === 'turn/end') {
|
|
592
|
+
ended = true;
|
|
593
|
+
tool = '';
|
|
594
|
+
pendingQuestions.clear();
|
|
595
|
+
pendingApprovals.clear();
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const awaiting = ended ? '' : (pendingQuestions.size ? 'question' : pendingApprovals.size ? 'approval' : '');
|
|
599
|
+
return { text, answer, answerSeq, ended, cost, step, tool, startedAt, awaiting };
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
// ----------------------------------------------------------------- layout
|
|
603
|
+
/** Branches at this depth and deeper start folded, so a big tree opens readable. */
|
|
604
|
+
const DEFAULT_COLLAPSE_DEPTH = 2;
|
|
605
|
+
|
|
606
|
+
/** Statuses that mean the model is still working on this node. */
|
|
607
|
+
const RUNNING_STATUSES = ['thinking', 'streaming', 'running'];
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* A node is active while it runs or while it is blocked on the user. The
|
|
611
|
+
* two are separate signals: the host keeps the turn open and reports the
|
|
612
|
+
* session as running while `ask_user_question` waits, so `awaiting` is the
|
|
613
|
+
* only record that the wait is on the human.
|
|
614
|
+
*/
|
|
615
|
+
function isInProgressNode(node) {
|
|
616
|
+
if (!node) return false;
|
|
617
|
+
return RUNNING_STATUSES.indexOf(node.status) >= 0
|
|
618
|
+
|| node.awaiting === 'question'
|
|
619
|
+
|| node.awaiting === 'approval';
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** Group nodes by parent id; '@root' holds every top-level node. */
|
|
623
|
+
function childrenMapOf(nodes) {
|
|
624
|
+
const childrenOf = new Map();
|
|
625
|
+
for (const node of nodes) {
|
|
626
|
+
const key = node.parentId === null || node.parentId === undefined ? '@root' : node.parentId;
|
|
627
|
+
if (!childrenOf.has(key)) childrenOf.set(key, []);
|
|
628
|
+
childrenOf.get(key).push(node);
|
|
629
|
+
}
|
|
630
|
+
return childrenOf;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/** Depth of every node: 0 for a tree root, one more per hop down. */
|
|
634
|
+
function depthMapOf(nodes) {
|
|
635
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
636
|
+
const depthOf = new Map();
|
|
637
|
+
for (const node of nodes) {
|
|
638
|
+
let depth = 0;
|
|
639
|
+
let cursor = node;
|
|
640
|
+
while (cursor && cursor.parentId !== null && cursor.parentId !== undefined && depth < 200) {
|
|
641
|
+
cursor = byId.get(cursor.parentId);
|
|
642
|
+
depth += 1;
|
|
643
|
+
}
|
|
644
|
+
depthOf.set(node.id, depth);
|
|
645
|
+
}
|
|
646
|
+
return depthOf;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/**
|
|
650
|
+
* Fold the tri-state `collapsed` map into plain booleans.
|
|
651
|
+
* undefined follows the depth default; true/false are the user's own choice.
|
|
652
|
+
*
|
|
653
|
+
* `keepOpen` holds nodes that sit on the path to an active node. A folded
|
|
654
|
+
* node is pruned out of the layout walk entirely, so the card below it has
|
|
655
|
+
* no position and is never drawn — which is how a running branch used to
|
|
656
|
+
* vanish behind a "⋯ N descendants" chip. The path to something in progress
|
|
657
|
+
* is therefore never folded by default; a manual fold is still respected.
|
|
658
|
+
*/
|
|
659
|
+
function effectiveCollapsedOf(nodes, collapsed, keepOpen) {
|
|
660
|
+
const depthOf = depthMapOf(nodes);
|
|
661
|
+
const childrenOf = childrenMapOf(nodes);
|
|
662
|
+
const choices = collapsed || {};
|
|
663
|
+
const result = {};
|
|
664
|
+
for (const node of nodes) {
|
|
665
|
+
const manual = choices[node.id];
|
|
666
|
+
if (manual === true || manual === false) {
|
|
667
|
+
result[node.id] = manual;
|
|
668
|
+
} else {
|
|
669
|
+
const hasChildren = (childrenOf.get(node.id) || []).length > 0;
|
|
670
|
+
result[node.id] = hasChildren
|
|
671
|
+
&& (depthOf.get(node.id) || 0) >= DEFAULT_COLLAPSE_DEPTH
|
|
672
|
+
&& !(keepOpen && keepOpen.has(node.id));
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return result;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/** Ancestors of every active node: the folds that must stay open to draw it. */
|
|
679
|
+
function activeAncestorsOf(nodes, activeIds) {
|
|
680
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
681
|
+
const keep = new Set();
|
|
682
|
+
for (const node of nodes) {
|
|
683
|
+
if (!activeIds.has(node.id)) continue;
|
|
684
|
+
let cursor = node.parentId === null || node.parentId === undefined ? undefined : byId.get(node.parentId);
|
|
685
|
+
let guard = 0;
|
|
686
|
+
while (cursor && guard < 500) {
|
|
687
|
+
keep.add(cursor.id);
|
|
688
|
+
cursor = cursor.parentId === null || cursor.parentId === undefined ? undefined : byId.get(cursor.parentId);
|
|
689
|
+
guard += 1;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return keep;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** How many descendants sit under every node, for the folded chip. */
|
|
696
|
+
function descendantCountsOf(nodes, childrenOf) {
|
|
697
|
+
const map = childrenOf || childrenMapOf(nodes);
|
|
698
|
+
const counts = new Map();
|
|
699
|
+
const visit = (node) => {
|
|
700
|
+
let total = 0;
|
|
701
|
+
for (const child of map.get(node.id) || []) total += visit(child) + 1;
|
|
702
|
+
counts.set(node.id, total);
|
|
703
|
+
return total;
|
|
704
|
+
};
|
|
705
|
+
for (const root of map.get('@root') || []) visit(root);
|
|
706
|
+
return counts;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
/** A node id plus every id below it. */
|
|
710
|
+
function subtreeIdsOf(nodes, id) {
|
|
711
|
+
const childrenOf = childrenMapOf(nodes);
|
|
712
|
+
const result = new Set([id]);
|
|
713
|
+
const stack = [id];
|
|
714
|
+
while (stack.length) {
|
|
715
|
+
const current = stack.pop();
|
|
716
|
+
for (const child of childrenOf.get(current) || []) {
|
|
717
|
+
if (result.has(child.id)) continue;
|
|
718
|
+
result.add(child.id);
|
|
719
|
+
stack.push(child.id);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return result;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/** Ids masked by the tombstone overlay: hidden itself or under a hidden ancestor. */
|
|
726
|
+
function hiddenSetOf(nodes, hidden) {
|
|
727
|
+
const marks = hidden || {};
|
|
728
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
729
|
+
const memo = new Map();
|
|
730
|
+
const resolve = (node) => {
|
|
731
|
+
const chain = [];
|
|
732
|
+
let cursor = node;
|
|
733
|
+
let verdict = false;
|
|
734
|
+
let guard = 0;
|
|
735
|
+
while (cursor && guard < 500) {
|
|
736
|
+
if (memo.has(cursor.id)) {
|
|
737
|
+
verdict = memo.get(cursor.id);
|
|
738
|
+
break;
|
|
739
|
+
}
|
|
740
|
+
if (marks[cursor.id] === true) {
|
|
741
|
+
verdict = true;
|
|
742
|
+
chain.push(cursor);
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
chain.push(cursor);
|
|
746
|
+
cursor = cursor.parentId === null || cursor.parentId === undefined ? undefined : byId.get(cursor.parentId);
|
|
747
|
+
guard += 1;
|
|
748
|
+
}
|
|
749
|
+
for (const item of chain) memo.set(item.id, verdict);
|
|
750
|
+
return verdict;
|
|
751
|
+
};
|
|
752
|
+
const result = new Set();
|
|
753
|
+
for (const node of nodes) if (resolve(node)) result.add(node.id);
|
|
754
|
+
return result;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/** A, B, C… labels, numbered in the same walk the layout uses. */
|
|
758
|
+
function labelMapOf(nodes, collapsed) {
|
|
759
|
+
const childrenOf = childrenMapOf(nodes);
|
|
760
|
+
const result = new Map();
|
|
761
|
+
let counter = 0;
|
|
762
|
+
const letter = (n) => String.fromCharCode(65 + (n % 26)) + (n >= 26 ? String(Math.floor(n / 26)) : '');
|
|
763
|
+
const visit = (node) => {
|
|
764
|
+
result.set(node.id, letter(counter));
|
|
765
|
+
counter += 1;
|
|
766
|
+
if (!collapsed[node.id]) for (const child of childrenOf.get(node.id) || []) visit(child);
|
|
767
|
+
};
|
|
768
|
+
for (const root of childrenOf.get('@root') || []) visit(root);
|
|
769
|
+
return result;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
function layoutTree(nodes, collapsed, heights) {
|
|
773
|
+
const childrenOf = childrenMapOf(nodes);
|
|
774
|
+
const kids = (node) => (collapsed[node.id] ? [] : childrenOf.get(node.id) || []);
|
|
775
|
+
const roots = childrenOf.get('@root') || [];
|
|
776
|
+
const widths = new Map();
|
|
777
|
+
const depthOf = depthMapOf(nodes);
|
|
778
|
+
|
|
779
|
+
const rowHeight = [];
|
|
780
|
+
for (const node of nodes) {
|
|
781
|
+
const depth = depthOf.get(node.id);
|
|
782
|
+
const height = heights[node.id] || NODE_H;
|
|
783
|
+
rowHeight[depth] = Math.max(rowHeight[depth] || 0, height);
|
|
784
|
+
}
|
|
785
|
+
const rowTop = [];
|
|
786
|
+
let top = 0;
|
|
787
|
+
for (let depth = 0; depth < rowHeight.length; depth += 1) {
|
|
788
|
+
rowTop[depth] = top;
|
|
789
|
+
top += rowHeight[depth] + GAP_Y;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
function measure(node) {
|
|
793
|
+
const list = kids(node);
|
|
794
|
+
if (list.length === 0) {
|
|
795
|
+
widths.set(node.id, NODE_W);
|
|
796
|
+
return NODE_W;
|
|
797
|
+
}
|
|
798
|
+
let total = 0;
|
|
799
|
+
list.forEach((child, index) => {
|
|
800
|
+
total += measure(child) + (index > 0 ? GAP_X : 0);
|
|
801
|
+
});
|
|
802
|
+
const width = Math.max(NODE_W, total);
|
|
803
|
+
widths.set(node.id, width);
|
|
804
|
+
return width;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
const positions = new Map();
|
|
808
|
+
|
|
809
|
+
function place(node, left) {
|
|
810
|
+
const list = kids(node);
|
|
811
|
+
const depth = depthOf.get(node.id) || 0;
|
|
812
|
+
if (list.length === 0) {
|
|
813
|
+
positions.set(node.id, { x: left + (widths.get(node.id) - NODE_W) / 2, y: rowTop[depth] || 0 });
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
let cursor = left;
|
|
817
|
+
for (const child of list) {
|
|
818
|
+
place(child, cursor);
|
|
819
|
+
cursor += widths.get(child.id) + GAP_X;
|
|
820
|
+
}
|
|
821
|
+
const first = positions.get(list[0].id).x + NODE_W / 2;
|
|
822
|
+
const last = positions.get(list[list.length - 1].id).x + NODE_W / 2;
|
|
823
|
+
positions.set(node.id, { x: (first + last) / 2 - NODE_W / 2, y: rowTop[depth] || 0 });
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
let x = 0;
|
|
827
|
+
for (const root of roots) {
|
|
828
|
+
measure(root);
|
|
829
|
+
place(root, x);
|
|
830
|
+
x += widths.get(root.id) + GAP_X * 2;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
const edges = [];
|
|
834
|
+
for (const node of nodes) {
|
|
835
|
+
const parentPos = positions.get(node.id);
|
|
836
|
+
if (!parentPos) continue;
|
|
837
|
+
const parentHeight = heights[node.id] || NODE_H;
|
|
838
|
+
for (const kidNode of kids(node)) {
|
|
839
|
+
const kidPos = positions.get(kidNode.id);
|
|
840
|
+
if (!kidPos) continue;
|
|
841
|
+
edges.push({
|
|
842
|
+
id: node.id + '>' + kidNode.id,
|
|
843
|
+
x1: parentPos.x + NODE_W / 2,
|
|
844
|
+
y1: parentPos.y + parentHeight,
|
|
845
|
+
x2: kidPos.x + NODE_W / 2,
|
|
846
|
+
y2: kidPos.y,
|
|
847
|
+
});
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
let maxX = 0;
|
|
852
|
+
let maxY = 0;
|
|
853
|
+
for (const [id, pos] of positions) {
|
|
854
|
+
maxX = Math.max(maxX, pos.x + NODE_W);
|
|
855
|
+
maxY = Math.max(maxY, pos.y + (heights[id] || NODE_H));
|
|
856
|
+
}
|
|
857
|
+
for (const node of nodes) {
|
|
858
|
+
const pos = positions.get(node.id);
|
|
859
|
+
if (pos) maxY = Math.max(maxY, pos.y + (heights[node.id] || NODE_H));
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return { positions, edges, width: maxX, height: maxY, roots };
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function pathOf(nodes, id) {
|
|
866
|
+
const byId = new Map(nodes.map((node) => [node.id, node]));
|
|
867
|
+
const path = new Set();
|
|
868
|
+
let cursor = byId.get(id);
|
|
869
|
+
let guard = 0;
|
|
870
|
+
while (cursor && guard < 500) {
|
|
871
|
+
path.add(cursor.id);
|
|
872
|
+
cursor = cursor.parentId === null || cursor.parentId === undefined ? undefined : byId.get(cursor.parentId);
|
|
873
|
+
guard += 1;
|
|
874
|
+
}
|
|
875
|
+
return path;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// Prefer creation time, never completion time (parallel branches may finish out of order).
|
|
879
|
+
function latestNodeOf(nodes) {
|
|
880
|
+
let latest = null;
|
|
881
|
+
let latestTime = -1;
|
|
882
|
+
for (const node of nodes) {
|
|
883
|
+
const legacy = /^b-([0-9a-z]+)-/.exec(node.id || '');
|
|
884
|
+
const time = Number(node.createdAt || node.startedAt) || (legacy ? parseInt(legacy[1], 36) : 0);
|
|
885
|
+
if (time >= latestTime) { latest = node; latestTime = time; }
|
|
886
|
+
}
|
|
887
|
+
return latest;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// ------------------------------------------------------------------ store
|
|
891
|
+
const STORE_PREFIX = 'dsh.treeConversations.v2:';
|
|
892
|
+
const INDEX_KEY = 'dsh.treeConversations.v2:index';
|
|
893
|
+
|
|
894
|
+
function readIndex() {
|
|
895
|
+
try {
|
|
896
|
+
return JSON.parse(window.localStorage.getItem(INDEX_KEY) || '{}') || {};
|
|
897
|
+
} catch (error) {
|
|
898
|
+
return {};
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
function writeIndex(index) {
|
|
903
|
+
try {
|
|
904
|
+
window.localStorage.setItem(INDEX_KEY, JSON.stringify(index));
|
|
905
|
+
} catch (error) {
|
|
906
|
+
/* ignore quota failures */
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
function createStore(rootId) {
|
|
911
|
+
const listeners = new Set();
|
|
912
|
+
const state = {
|
|
913
|
+
nodes: [],
|
|
914
|
+
collapsed: {},
|
|
915
|
+
// Tombstone overlay: the session log is append-only, so deleting a node
|
|
916
|
+
// only masks it and its subtree here.
|
|
917
|
+
hidden: {},
|
|
918
|
+
archived: [],
|
|
919
|
+
selectedId: null,
|
|
920
|
+
composingId: null,
|
|
921
|
+
readingId: null,
|
|
922
|
+
draft: '',
|
|
923
|
+
loaded: false,
|
|
924
|
+
error: null,
|
|
925
|
+
meta: { events: 0, binding: false, hasMore: false, openState: '' },
|
|
926
|
+
heights: {},
|
|
927
|
+
};
|
|
928
|
+
const stops = new Map();
|
|
929
|
+
let revision = 0;
|
|
930
|
+
|
|
931
|
+
try {
|
|
932
|
+
const raw = window.localStorage.getItem(STORE_PREFIX + rootId);
|
|
933
|
+
if (raw) {
|
|
934
|
+
const saved = JSON.parse(raw) || {};
|
|
935
|
+
if (Array.isArray(saved.nodes)) state.nodes = saved.nodes;
|
|
936
|
+
if (saved.collapsed && typeof saved.collapsed === 'object') state.collapsed = saved.collapsed;
|
|
937
|
+
if (saved.hidden && typeof saved.hidden === 'object') state.hidden = saved.hidden;
|
|
938
|
+
if (Array.isArray(saved.archived)) state.archived = saved.archived;
|
|
939
|
+
}
|
|
940
|
+
} catch (error) {
|
|
941
|
+
/* ignore corrupt state */
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
function writeNow() {
|
|
945
|
+
try {
|
|
946
|
+
const payload = {
|
|
947
|
+
nodes: state.nodes.map((node) => ({
|
|
948
|
+
id: node.id,
|
|
949
|
+
parentId: node.parentId,
|
|
950
|
+
sessionId: node.sessionId,
|
|
951
|
+
question: node.question,
|
|
952
|
+
answer: node.answer,
|
|
953
|
+
status: node.status,
|
|
954
|
+
answerSeq: node.answerSeq,
|
|
955
|
+
kind: node.kind,
|
|
956
|
+
sourceKind: node.sourceKind,
|
|
957
|
+
baseSeq: node.baseSeq,
|
|
958
|
+
createdAt: node.createdAt,
|
|
959
|
+
startedAt: node.startedAt,
|
|
960
|
+
cost: node.cost,
|
|
961
|
+
})),
|
|
962
|
+
collapsed: state.collapsed,
|
|
963
|
+
hidden: state.hidden,
|
|
964
|
+
archived: state.archived,
|
|
965
|
+
};
|
|
966
|
+
window.localStorage.setItem(STORE_PREFIX + rootId, JSON.stringify(payload));
|
|
967
|
+
} catch (error) {
|
|
968
|
+
/* ignore quota failures */
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
let persistTimer = null;
|
|
973
|
+
function persist() {
|
|
974
|
+
if (persistTimer !== null) return;
|
|
975
|
+
persistTimer = window.setTimeout(() => {
|
|
976
|
+
persistTimer = null;
|
|
977
|
+
writeNow();
|
|
978
|
+
}, 400);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
function emit() {
|
|
982
|
+
revision += 1;
|
|
983
|
+
for (const listener of listeners) listener();
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
const api = {
|
|
987
|
+
rootId,
|
|
988
|
+
getSnapshot() {
|
|
989
|
+
return { ...state, revision };
|
|
990
|
+
},
|
|
991
|
+
subscribe(listener) {
|
|
992
|
+
listeners.add(listener);
|
|
993
|
+
return () => listeners.delete(listener);
|
|
994
|
+
},
|
|
995
|
+
getNode(id) {
|
|
996
|
+
return state.nodes.find((node) => node.id === id);
|
|
997
|
+
},
|
|
998
|
+
children(id) {
|
|
999
|
+
return state.nodes.filter((node) => node.parentId === id);
|
|
1000
|
+
},
|
|
1001
|
+
upsert(node) {
|
|
1002
|
+
const index = state.nodes.findIndex((existing) => existing.id === node.id);
|
|
1003
|
+
if (index >= 0) {
|
|
1004
|
+
state.nodes[index] = { ...state.nodes[index], ...node };
|
|
1005
|
+
} else {
|
|
1006
|
+
state.nodes.push({ ...node });
|
|
1007
|
+
}
|
|
1008
|
+
state.nodes = state.nodes.slice();
|
|
1009
|
+
persist();
|
|
1010
|
+
emit();
|
|
1011
|
+
},
|
|
1012
|
+
patch(id, changes) {
|
|
1013
|
+
const target = state.nodes.find((node) => node.id === id);
|
|
1014
|
+
if (!target) return;
|
|
1015
|
+
Object.assign(target, changes);
|
|
1016
|
+
state.nodes = state.nodes.slice();
|
|
1017
|
+
persist();
|
|
1018
|
+
emit();
|
|
1019
|
+
},
|
|
1020
|
+
ensureChain(turns) {
|
|
1021
|
+
let changed = false;
|
|
1022
|
+
const ids = turns.map((turn, index) => 'root-' + (turn.qSeq || turn.answerSeq || index));
|
|
1023
|
+
turns.forEach((turn, index) => {
|
|
1024
|
+
const id = ids[index];
|
|
1025
|
+
const existing = state.nodes.find((node) => node.id === id);
|
|
1026
|
+
// With a partial window the first visible turn is not the real root,
|
|
1027
|
+
// so keep the parent it already had instead of re-rooting the chain.
|
|
1028
|
+
const parentId = index === 0
|
|
1029
|
+
? (existing ? existing.parentId : null)
|
|
1030
|
+
: ids[index - 1];
|
|
1031
|
+
const next = {
|
|
1032
|
+
id,
|
|
1033
|
+
parentId,
|
|
1034
|
+
sessionId: rootId,
|
|
1035
|
+
question: turn.question,
|
|
1036
|
+
answer: turn.answer,
|
|
1037
|
+
status: turn.status,
|
|
1038
|
+
answerSeq: turn.answerSeq,
|
|
1039
|
+
kind: 'root',
|
|
1040
|
+
sourceKind: turn.kind || '',
|
|
1041
|
+
awaiting: turn.awaiting || '',
|
|
1042
|
+
step: turn.step || 0,
|
|
1043
|
+
tool: turn.tool || '',
|
|
1044
|
+
startedAt: turn.startedAt || 0,
|
|
1045
|
+
cost: turn.cost || { ms: 0, tokens: 0, cached: 0, running: false },
|
|
1046
|
+
};
|
|
1047
|
+
if (!existing) {
|
|
1048
|
+
state.nodes.push(next);
|
|
1049
|
+
changed = true;
|
|
1050
|
+
} else if (
|
|
1051
|
+
existing.parentId !== next.parentId ||
|
|
1052
|
+
existing.question !== next.question ||
|
|
1053
|
+
existing.answer !== next.answer ||
|
|
1054
|
+
existing.answerSeq !== next.answerSeq ||
|
|
1055
|
+
existing.status !== next.status ||
|
|
1056
|
+
(existing.awaiting || '') !== next.awaiting ||
|
|
1057
|
+
existing.step !== next.step ||
|
|
1058
|
+
existing.tool !== next.tool ||
|
|
1059
|
+
existing.startedAt !== next.startedAt ||
|
|
1060
|
+
!existing.cost ||
|
|
1061
|
+
existing.cost.ms !== next.cost.ms ||
|
|
1062
|
+
existing.cost.tokens !== next.cost.tokens ||
|
|
1063
|
+
existing.cost.cached !== next.cost.cached ||
|
|
1064
|
+
existing.cost.running !== next.cost.running
|
|
1065
|
+
) {
|
|
1066
|
+
Object.assign(existing, next);
|
|
1067
|
+
changed = true;
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
// Only prune when the whole history is in hand. A partial window would
|
|
1071
|
+
// otherwise delete older root turns and orphan the branches hanging off
|
|
1072
|
+
// them, which then vanish from the canvas without a trace.
|
|
1073
|
+
// Only prune when the projection actually produced turns. A freshly
|
|
1074
|
+
// re-bound session starts with an empty window until its first page
|
|
1075
|
+
// lands; pruning there used to erase the whole tree.
|
|
1076
|
+
const wanted = new Set(ids);
|
|
1077
|
+
const before = state.nodes.length;
|
|
1078
|
+
if (!state.meta.hasMore && turns.length > 0) {
|
|
1079
|
+
state.nodes = state.nodes.filter((node) => node.sessionId !== rootId || wanted.has(node.id));
|
|
1080
|
+
// A branch whose parent turn is gone would be unreachable; hoist it
|
|
1081
|
+
// to a root instead of silently dropping it from the canvas.
|
|
1082
|
+
const alive = new Set(state.nodes.map((node) => node.id));
|
|
1083
|
+
for (const node of state.nodes) {
|
|
1084
|
+
if (node.parentId && !alive.has(node.parentId)) {
|
|
1085
|
+
node.parentId = null;
|
|
1086
|
+
changed = true;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
if (state.nodes.length !== before) changed = true;
|
|
1091
|
+
if (changed) {
|
|
1092
|
+
state.nodes = state.nodes.slice();
|
|
1093
|
+
persist();
|
|
1094
|
+
emit();
|
|
1095
|
+
}
|
|
1096
|
+
},
|
|
1097
|
+
reveal(id) {
|
|
1098
|
+
const collapsed = { ...state.collapsed };
|
|
1099
|
+
for (const ancestor of pathOf(state.nodes, id)) {
|
|
1100
|
+
if (ancestor !== id) collapsed[ancestor] = false;
|
|
1101
|
+
}
|
|
1102
|
+
state.collapsed = collapsed;
|
|
1103
|
+
state.selectedId = id;
|
|
1104
|
+
persist();
|
|
1105
|
+
emit();
|
|
1106
|
+
},
|
|
1107
|
+
/** Open the path to a node without touching selection (auto-follow). */
|
|
1108
|
+
expandPath(id) {
|
|
1109
|
+
const collapsed = { ...state.collapsed };
|
|
1110
|
+
let changed = false;
|
|
1111
|
+
for (const ancestor of pathOf(state.nodes, id)) {
|
|
1112
|
+
if (ancestor === id || collapsed[ancestor] === false) continue;
|
|
1113
|
+
collapsed[ancestor] = false;
|
|
1114
|
+
changed = true;
|
|
1115
|
+
}
|
|
1116
|
+
if (!changed) return;
|
|
1117
|
+
state.collapsed = collapsed;
|
|
1118
|
+
persist();
|
|
1119
|
+
emit();
|
|
1120
|
+
},
|
|
1121
|
+
select(id) {
|
|
1122
|
+
state.selectedId = id;
|
|
1123
|
+
emit();
|
|
1124
|
+
},
|
|
1125
|
+
openReader(id) {
|
|
1126
|
+
state.readingId = id;
|
|
1127
|
+
emit();
|
|
1128
|
+
},
|
|
1129
|
+
closeReader() {
|
|
1130
|
+
state.readingId = null;
|
|
1131
|
+
emit();
|
|
1132
|
+
},
|
|
1133
|
+
toggleCollapse(id) {
|
|
1134
|
+
// Flip what the user sees, not the raw slot: a default-folded branch
|
|
1135
|
+
// must open on the first click and stay open afterwards.
|
|
1136
|
+
const effective = effectiveCollapsedOf(state.nodes, state.collapsed);
|
|
1137
|
+
state.collapsed = { ...state.collapsed, [id]: effective[id] === true ? false : true };
|
|
1138
|
+
persist();
|
|
1139
|
+
emit();
|
|
1140
|
+
},
|
|
1141
|
+
/** Hide a node and its whole subtree; the log itself is never touched. */
|
|
1142
|
+
hide(id) {
|
|
1143
|
+
const subtree = subtreeIdsOf(state.nodes, id);
|
|
1144
|
+
state.hidden = { ...state.hidden, [id]: true };
|
|
1145
|
+
if (state.selectedId && subtree.has(state.selectedId)) state.selectedId = null;
|
|
1146
|
+
if (state.composingId && subtree.has(state.composingId)) {
|
|
1147
|
+
state.composingId = null;
|
|
1148
|
+
state.draft = '';
|
|
1149
|
+
}
|
|
1150
|
+
persist();
|
|
1151
|
+
emit();
|
|
1152
|
+
},
|
|
1153
|
+
restoreHidden() {
|
|
1154
|
+
state.hidden = {};
|
|
1155
|
+
state.archived = [];
|
|
1156
|
+
persist();
|
|
1157
|
+
emit();
|
|
1158
|
+
},
|
|
1159
|
+
setArchived(ids) {
|
|
1160
|
+
state.archived = Array.isArray(ids) ? ids.slice() : [];
|
|
1161
|
+
persist();
|
|
1162
|
+
emit();
|
|
1163
|
+
},
|
|
1164
|
+
compose(id) {
|
|
1165
|
+
state.composingId = id;
|
|
1166
|
+
state.selectedId = id;
|
|
1167
|
+
state.draft = '';
|
|
1168
|
+
emit();
|
|
1169
|
+
},
|
|
1170
|
+
setDraft(value) {
|
|
1171
|
+
state.draft = value;
|
|
1172
|
+
emit();
|
|
1173
|
+
},
|
|
1174
|
+
cancelCompose() {
|
|
1175
|
+
state.composingId = null;
|
|
1176
|
+
state.draft = '';
|
|
1177
|
+
emit();
|
|
1178
|
+
},
|
|
1179
|
+
registerStop(id, stop) {
|
|
1180
|
+
stops.set(id, stop);
|
|
1181
|
+
},
|
|
1182
|
+
stop(id) {
|
|
1183
|
+
const stop = stops.get(id);
|
|
1184
|
+
if (stop) stop();
|
|
1185
|
+
},
|
|
1186
|
+
stopAll() {
|
|
1187
|
+
for (const stop of stops.values()) stop();
|
|
1188
|
+
},
|
|
1189
|
+
setError(message) {
|
|
1190
|
+
state.error = message;
|
|
1191
|
+
emit();
|
|
1192
|
+
},
|
|
1193
|
+
setMeta(meta) {
|
|
1194
|
+
state.meta = { ...state.meta, ...meta };
|
|
1195
|
+
emit();
|
|
1196
|
+
},
|
|
1197
|
+
setHeight(id, value) {
|
|
1198
|
+
if (!Number.isFinite(value) || value <= 0) return;
|
|
1199
|
+
const previous = state.heights[id];
|
|
1200
|
+
if (previous !== undefined && Math.abs(previous - value) < 2) return;
|
|
1201
|
+
state.heights = { ...state.heights, [id]: value };
|
|
1202
|
+
emit();
|
|
1203
|
+
},
|
|
1204
|
+
};
|
|
1205
|
+
|
|
1206
|
+
return api;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
// ------------------------------------------------------------------- api
|
|
1210
|
+
function createRuntime(ctx) {
|
|
1211
|
+
const stores = new Map();
|
|
1212
|
+
const rootSubscriptions = new Map();
|
|
1213
|
+
const branchSubscriptions = new Map();
|
|
1214
|
+
|
|
1215
|
+
function index() {
|
|
1216
|
+
return readIndex();
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function rootOf(sessionId) {
|
|
1220
|
+
const entry = index()[sessionId];
|
|
1221
|
+
return entry && entry.rootId ? entry.rootId : sessionId;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
function storeFor(rootId) {
|
|
1225
|
+
if (!stores.has(rootId)) stores.set(rootId, createStore(rootId));
|
|
1226
|
+
return stores.get(rootId);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
/** The workspaces client is optional: never let its absence break the tree. */
|
|
1230
|
+
function workspacesOf() {
|
|
1231
|
+
try {
|
|
1232
|
+
return ctx && ctx.workspaces ? ctx.workspaces : null;
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
return null;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
/**
|
|
1239
|
+
* Tombstone a node and its subtree. Archived sessions are remembered so
|
|
1240
|
+
* "恢复" can put them back into the session list.
|
|
1241
|
+
*/
|
|
1242
|
+
function deleteSubtree(rootId, nodeId) {
|
|
1243
|
+
const store = storeFor(rootId);
|
|
1244
|
+
const before = store.getSnapshot();
|
|
1245
|
+
if (!before.nodes.some((node) => node.id === nodeId)) return;
|
|
1246
|
+
const subtree = subtreeIdsOf(before.nodes, nodeId);
|
|
1247
|
+
store.hide(nodeId);
|
|
1248
|
+
|
|
1249
|
+
const after = store.getSnapshot();
|
|
1250
|
+
const hiddenIds = hiddenSetOf(after.nodes, after.hidden);
|
|
1251
|
+
const visibleSessions = new Set();
|
|
1252
|
+
for (const node of after.nodes) {
|
|
1253
|
+
if (hiddenIds.has(node.id) || !node.sessionId) continue;
|
|
1254
|
+
visibleSessions.add(node.sessionId);
|
|
1255
|
+
}
|
|
1256
|
+
const archived = Array.isArray(after.archived) ? after.archived.slice() : [];
|
|
1257
|
+
const workspaces = workspacesOf();
|
|
1258
|
+
let changed = false;
|
|
1259
|
+
for (const node of after.nodes) {
|
|
1260
|
+
if (!subtree.has(node.id)) continue;
|
|
1261
|
+
const sessionId = node.sessionId;
|
|
1262
|
+
// Never archive the root session, nor a session a visible node still points at.
|
|
1263
|
+
if (!sessionId || sessionId === rootId) continue;
|
|
1264
|
+
if (visibleSessions.has(sessionId) || archived.indexOf(sessionId) >= 0) continue;
|
|
1265
|
+
if (!workspaces || typeof workspaces.archiveSession !== 'function') continue;
|
|
1266
|
+
try {
|
|
1267
|
+
const result = workspaces.archiveSession(sessionId, { stopActivity: true });
|
|
1268
|
+
if (result && typeof result.catch === 'function') result.catch(() => undefined);
|
|
1269
|
+
archived.push(sessionId);
|
|
1270
|
+
changed = true;
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
/* archiving only quiets the session list; the tombstone already did the work */
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
if (changed) store.setArchived(archived);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/** Drop every tombstone and unarchive the sessions deletion had put away. */
|
|
1279
|
+
function restoreHidden(rootId) {
|
|
1280
|
+
const store = storeFor(rootId);
|
|
1281
|
+
const snapshot = store.getSnapshot();
|
|
1282
|
+
const archived = Array.isArray(snapshot.archived) ? snapshot.archived.slice() : [];
|
|
1283
|
+
const workspaces = workspacesOf();
|
|
1284
|
+
for (const sessionId of archived) {
|
|
1285
|
+
if (!workspaces || typeof workspaces.unarchiveSession !== 'function') continue;
|
|
1286
|
+
try {
|
|
1287
|
+
const result = workspaces.unarchiveSession(sessionId);
|
|
1288
|
+
if (result && typeof result.catch === 'function') result.catch(() => undefined);
|
|
1289
|
+
} catch (error) {
|
|
1290
|
+
/* ignore */
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
store.restoreHidden();
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
function remember(sessionId, rootId, nodeId) {
|
|
1297
|
+
const next = { ...index(), [sessionId]: { rootId, nodeId } };
|
|
1298
|
+
writeIndex(next);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
/** Mirror the root session's own turns into nodes, then keep them in sync. */
|
|
1302
|
+
function ensureProjection(rootId, store) {
|
|
1303
|
+
const binding = ctx.sessions.binding(rootId);
|
|
1304
|
+
if (!binding) {
|
|
1305
|
+
store.setError(T('error.sessionNotOpen'));
|
|
1306
|
+
store.setMeta({ binding: false, events: 0 });
|
|
1307
|
+
return;
|
|
1308
|
+
}
|
|
1309
|
+
const sessionSnapshot = () => {
|
|
1310
|
+
try {
|
|
1311
|
+
return binding.session && typeof binding.session.getSnapshot === 'function'
|
|
1312
|
+
? binding.session.getSnapshot()
|
|
1313
|
+
: null;
|
|
1314
|
+
} catch (error) {
|
|
1315
|
+
return null;
|
|
1316
|
+
}
|
|
1317
|
+
};
|
|
1318
|
+
const read = () => {
|
|
1319
|
+
const window = binding.eventSource.getSnapshot();
|
|
1320
|
+
const session = sessionSnapshot();
|
|
1321
|
+
store.setMeta({
|
|
1322
|
+
binding: true,
|
|
1323
|
+
events: window.entries.length,
|
|
1324
|
+
hasMore: Boolean(window.hasMore),
|
|
1325
|
+
openState: session && session.openState ? session.openState : '',
|
|
1326
|
+
});
|
|
1327
|
+
store.ensureChain(projectTurns(window.entries, session ? session.running : undefined));
|
|
1328
|
+
return { window, session };
|
|
1329
|
+
};
|
|
1330
|
+
// A retired scope drops its Session instance, and the next
|
|
1331
|
+
// materialisation mints a new one with a new event source
|
|
1332
|
+
// (ClientSessions.materializeScope). Keeping the old unsubscribe would
|
|
1333
|
+
// leave this view frozen on the empty window it saw first.
|
|
1334
|
+
const known = rootSubscriptions.get(rootId);
|
|
1335
|
+
if (!known || known.source !== binding.eventSource) {
|
|
1336
|
+
if (known && typeof known.unsubscribe === 'function') {
|
|
1337
|
+
try { known.unsubscribe(); } catch (error) { /* the dropped source is already gone */ }
|
|
1338
|
+
}
|
|
1339
|
+
rootSubscriptions.set(rootId, {
|
|
1340
|
+
source: binding.eventSource,
|
|
1341
|
+
unsubscribe: binding.eventSource.subscribe(read),
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
const first = read();
|
|
1345
|
+
if (store.getSnapshot().error) store.setError(null);
|
|
1346
|
+
// A cold window is not final: ask the session to open (idempotent) and
|
|
1347
|
+
// read again once its first page lands. A tab restored straight onto a
|
|
1348
|
+
// freshly materialised session would otherwise stay blank until
|
|
1349
|
+
// something else — the Chat view — filled the window first.
|
|
1350
|
+
if (first.window.entries.length === 0
|
|
1351
|
+
&& first.session && first.session.openState !== 'open'
|
|
1352
|
+
&& binding.session && typeof binding.session.open === 'function') {
|
|
1353
|
+
try {
|
|
1354
|
+
Promise.resolve(binding.session.open()).then(read).catch(() => undefined);
|
|
1355
|
+
} catch (error) {
|
|
1356
|
+
/* opening is best effort; the subscription still wakes us */
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
autoLoadEarlier(rootId, binding, 8);
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
/** Retain a session so its event window is open, then project it. */
|
|
1363
|
+
async function ensureOpen(rootId, store) {
|
|
1364
|
+
if (ctx.sessions.binding(rootId)) {
|
|
1365
|
+
ensureProjection(rootId, store);
|
|
1366
|
+
projectBranches(rootId, store);
|
|
1367
|
+
return true;
|
|
1368
|
+
}
|
|
1369
|
+
try {
|
|
1370
|
+
const reference = ctx.sessions.retain(rootId, { source: 'gateway' });
|
|
1371
|
+
const binding = await reference.ready;
|
|
1372
|
+
if (!binding) throw new Error(T('error.noBinding'));
|
|
1373
|
+
ensureProjection(rootId, store);
|
|
1374
|
+
projectBranches(rootId, store);
|
|
1375
|
+
return true;
|
|
1376
|
+
} catch (error) {
|
|
1377
|
+
store.setError(T('error.openFailed', { reason: String((error && error.message) || error) }));
|
|
1378
|
+
return false;
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
/** Each branch node lives in its own session; keep its card in sync too. */
|
|
1383
|
+
function projectBranches(rootId, store) {
|
|
1384
|
+
for (const node of store.getSnapshot().nodes) {
|
|
1385
|
+
if (node.kind !== 'branch' || !node.sessionId) continue;
|
|
1386
|
+
if (branchSubscriptions.has(node.id)) continue;
|
|
1387
|
+
let binding = ctx.sessions.binding(node.sessionId);
|
|
1388
|
+
if (!binding) {
|
|
1389
|
+
try {
|
|
1390
|
+
const reference = ctx.sessions.retain(node.sessionId, { source: 'gateway' });
|
|
1391
|
+
reference.ready.then((ready) => {
|
|
1392
|
+
if (!ready) return;
|
|
1393
|
+
watchBranch(node.id, ready, store);
|
|
1394
|
+
}).catch(() => undefined);
|
|
1395
|
+
branchSubscriptions.set(node.id, true);
|
|
1396
|
+
continue;
|
|
1397
|
+
} catch (error) {
|
|
1398
|
+
continue;
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
watchBranch(node.id, binding, store);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
function watchBranch(nodeId, binding, store) {
|
|
1406
|
+
if (branchSubscriptions.get(nodeId) === 'watching') return;
|
|
1407
|
+
branchSubscriptions.set(nodeId, 'watching');
|
|
1408
|
+
const node = store.getNode(nodeId);
|
|
1409
|
+
// A fork's own events start after its seed marker. Without a recorded
|
|
1410
|
+
// baseline, reading from 0 would show the parent's answer as this
|
|
1411
|
+
// branch's answer, so fall back to the seed seq instead.
|
|
1412
|
+
let baseline = node && typeof node.baseSeq === 'number' && node.baseSeq > 0 ? node.baseSeq : 0;
|
|
1413
|
+
if (!baseline) {
|
|
1414
|
+
const entries = binding.eventSource.getSnapshot().entries;
|
|
1415
|
+
for (const entry of entries) {
|
|
1416
|
+
const event = entry && entry.event;
|
|
1417
|
+
if (entry.type === 'event' && event && event.type === 'session/end-seed'
|
|
1418
|
+
&& event.data && event.data.inherited === true && typeof event.seq === 'number') {
|
|
1419
|
+
baseline = event.seq;
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
const read = () => {
|
|
1424
|
+
const window = binding.eventSource.getSnapshot();
|
|
1425
|
+
const read2 = readBranch(window.entries, baseline);
|
|
1426
|
+
let running;
|
|
1427
|
+
try {
|
|
1428
|
+
const session = binding.session && typeof binding.session.getSnapshot === 'function'
|
|
1429
|
+
? binding.session.getSnapshot()
|
|
1430
|
+
: null;
|
|
1431
|
+
running = session ? session.running : undefined;
|
|
1432
|
+
} catch (error) {
|
|
1433
|
+
running = undefined;
|
|
1434
|
+
}
|
|
1435
|
+
const changes = {};
|
|
1436
|
+
const current = store.getNode(nodeId);
|
|
1437
|
+
if (!current) return;
|
|
1438
|
+
if (read2.answer && read2.answer !== current.answer) changes.answer = read2.answer;
|
|
1439
|
+
else if (read2.text && read2.text !== current.answer) changes.answer = read2.text;
|
|
1440
|
+
if (read2.answerSeq) changes.answerSeq = read2.answerSeq;
|
|
1441
|
+
if (read2.step !== (current.step || 0)) changes.step = read2.step;
|
|
1442
|
+
if (read2.tool !== (current.tool || '')) changes.tool = read2.tool;
|
|
1443
|
+
const awaiting = read2.awaiting || '';
|
|
1444
|
+
if (awaiting !== (current.awaiting || '')) changes.awaiting = awaiting;
|
|
1445
|
+
if (read2.startedAt && read2.startedAt !== current.startedAt) changes.startedAt = read2.startedAt;
|
|
1446
|
+
// turnCost allocates a fresh object per event; compare before patching
|
|
1447
|
+
// so a streaming branch does not mark the store dirty on every chunk.
|
|
1448
|
+
const known = current.cost;
|
|
1449
|
+
if (!known
|
|
1450
|
+
|| known.ms !== read2.cost.ms
|
|
1451
|
+
|| known.tokens !== read2.cost.tokens
|
|
1452
|
+
|| known.cached !== read2.cost.cached
|
|
1453
|
+
|| known.running !== read2.cost.running) changes.cost = read2.cost;
|
|
1454
|
+
if (read2.ended || running === false) {
|
|
1455
|
+
changes.status = (read2.answer || read2.text || current.answer) ? 'done' : 'stopped';
|
|
1456
|
+
changes.tool = '';
|
|
1457
|
+
} else if (read2.text || read2.answer || read2.tool || awaiting) {
|
|
1458
|
+
changes.status = 'streaming';
|
|
1459
|
+
}
|
|
1460
|
+
if (Object.keys(changes).length) store.patch(nodeId, changes);
|
|
1461
|
+
};
|
|
1462
|
+
read();
|
|
1463
|
+
binding.eventSource.subscribe(read);
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
function autoLoadEarlier(rootId, binding, remaining) {
|
|
1467
|
+
if (remaining <= 0) return;
|
|
1468
|
+
const window = binding.eventSource.getSnapshot();
|
|
1469
|
+
if (!window.hasMore) return;
|
|
1470
|
+
Promise.resolve(binding.session.loadOlder())
|
|
1471
|
+
.then(() => autoLoadEarlier(rootId, binding, remaining - 1))
|
|
1472
|
+
.catch(() => undefined);
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
async function branch(rootId, parentNode, question) {
|
|
1476
|
+
const store = storeFor(rootId);
|
|
1477
|
+
if (!parentNode) return;
|
|
1478
|
+
const snapshot = store.getSnapshot();
|
|
1479
|
+
// A tombstoned node cannot be forked from even if a stale card asks.
|
|
1480
|
+
if (hiddenSetOf(snapshot.nodes, snapshot.hidden).has(parentNode.id)) return;
|
|
1481
|
+
const nodeId = 'b-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 6);
|
|
1482
|
+
let childId;
|
|
1483
|
+
try {
|
|
1484
|
+
childId = await ctx.sessions.fork({
|
|
1485
|
+
sessionId: parentNode.sessionId,
|
|
1486
|
+
atSeq: parentNode.answerSeq || undefined,
|
|
1487
|
+
});
|
|
1488
|
+
} catch (error) {
|
|
1489
|
+
store.setError(T('error.forkFailed', { reason: String((error && error.message) || error) }));
|
|
1490
|
+
return;
|
|
1491
|
+
}
|
|
1492
|
+
remember(childId, rootId, nodeId);
|
|
1493
|
+
store.upsert({
|
|
1494
|
+
id: nodeId,
|
|
1495
|
+
parentId: parentNode.id,
|
|
1496
|
+
sessionId: childId,
|
|
1497
|
+
question,
|
|
1498
|
+
answer: '',
|
|
1499
|
+
status: 'thinking',
|
|
1500
|
+
answerSeq: 0,
|
|
1501
|
+
kind: 'branch',
|
|
1502
|
+
createdAt: parseInt(nodeId.split('-')[1], 36),
|
|
1503
|
+
});
|
|
1504
|
+
// The new card sits below its parent: open the path so it is drawn even
|
|
1505
|
+
// when the fork point was behind a folded branch.
|
|
1506
|
+
store.expandPath(nodeId);
|
|
1507
|
+
store.compose(null);
|
|
1508
|
+
store.select(nodeId);
|
|
1509
|
+
|
|
1510
|
+
let reference;
|
|
1511
|
+
try {
|
|
1512
|
+
reference = ctx.sessions.retain(childId, { source: 'gateway' });
|
|
1513
|
+
} catch (error) {
|
|
1514
|
+
store.patch(nodeId, { status: 'error', answer: T('error.branchUnopenable', { reason: String((error && error.message) || error) }) });
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
let binding;
|
|
1519
|
+
try {
|
|
1520
|
+
binding = await reference.ready;
|
|
1521
|
+
} catch (error) {
|
|
1522
|
+
store.patch(nodeId, {
|
|
1523
|
+
status: 'error',
|
|
1524
|
+
answer: T('error.branchNoBinding', { reason: String((error && error.message) || error) }),
|
|
1525
|
+
});
|
|
1526
|
+
return;
|
|
1527
|
+
}
|
|
1528
|
+
if (!binding) {
|
|
1529
|
+
store.patch(nodeId, { status: 'error', answer: T('error.branchNoUsableBinding') });
|
|
1530
|
+
return;
|
|
1531
|
+
}
|
|
1532
|
+
const baseline = maxDurableSeq(binding.eventSource.getSnapshot().entries);
|
|
1533
|
+
let busy = false;
|
|
1534
|
+
|
|
1535
|
+
// Name the fork after its branch so the session list stays readable.
|
|
1536
|
+
const branchTitle = '⟂ ' + (parentNode.question || T('reader.branch')).replace(/\s+/g, ' ').slice(0, 26)
|
|
1537
|
+
+ ' · ' + question.replace(/\s+/g, ' ').slice(0, 22);
|
|
1538
|
+
try {
|
|
1539
|
+
await binding.session.rename(branchTitle);
|
|
1540
|
+
} catch (error) {
|
|
1541
|
+
/* a fork title is cosmetic; keep going */
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
store.patch(nodeId, { baseSeq: baseline });
|
|
1545
|
+
|
|
1546
|
+
const stop = () => {
|
|
1547
|
+
try {
|
|
1548
|
+
binding.session.cancel();
|
|
1549
|
+
} catch (error) {
|
|
1550
|
+
/* ignore */
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
store.registerStop(nodeId, stop);
|
|
1554
|
+
|
|
1555
|
+
const unsubscribe = binding.eventSource.subscribe(() => {
|
|
1556
|
+
if (busy) return;
|
|
1557
|
+
busy = true;
|
|
1558
|
+
try {
|
|
1559
|
+
const window = binding.eventSource.getSnapshot();
|
|
1560
|
+
const read = readBranch(window.entries, baseline);
|
|
1561
|
+
if (read.answer || read.text) {
|
|
1562
|
+
store.patch(nodeId, {
|
|
1563
|
+
answer: read.answer || read.text,
|
|
1564
|
+
status: read.ended ? 'done' : 'streaming',
|
|
1565
|
+
answerSeq: read.answerSeq || 0,
|
|
1566
|
+
});
|
|
1567
|
+
} else if (read.ended) {
|
|
1568
|
+
store.patch(nodeId, { status: 'done' });
|
|
1569
|
+
}
|
|
1570
|
+
const awaiting = read.awaiting || '';
|
|
1571
|
+
const node = store.getNode(nodeId);
|
|
1572
|
+
if (node && awaiting !== (node.awaiting || '')) store.patch(nodeId, { awaiting });
|
|
1573
|
+
if (read.ended) {
|
|
1574
|
+
unsubscribe();
|
|
1575
|
+
try {
|
|
1576
|
+
reference.release();
|
|
1577
|
+
} catch (error) {
|
|
1578
|
+
/* ignore */
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
} finally {
|
|
1582
|
+
busy = false;
|
|
1583
|
+
}
|
|
1584
|
+
});
|
|
1585
|
+
|
|
1586
|
+
store.patch(nodeId, { status: 'streaming' });
|
|
1587
|
+
try {
|
|
1588
|
+
const result = await binding.session.prompt([{ type: 'text', text: question }], 'queue');
|
|
1589
|
+
if (result && result.ok === false) {
|
|
1590
|
+
store.patch(nodeId, { status: 'error', answer: T('error.submitFailed', { reason: JSON.stringify(result.error) }) });
|
|
1591
|
+
}
|
|
1592
|
+
} catch (error) {
|
|
1593
|
+
store.patch(nodeId, { status: 'error', answer: T('error.submitFailed', { reason: String((error && error.message) || error) }) });
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
async function loadOlder(rootId) {
|
|
1597
|
+
const binding = ctx.sessions.binding(rootId);
|
|
1598
|
+
if (!binding) return;
|
|
1599
|
+
try {
|
|
1600
|
+
await binding.session.loadOlder();
|
|
1601
|
+
} catch (error) {
|
|
1602
|
+
storeFor(rootId).setError(T('error.loadOlderFailed', { reason: String((error && error.message) || error) }));
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
return { rootOf, storeFor, ensureProjection, ensureOpen, branch, loadOlder, deleteSubtree, restoreHidden };
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
// -------------------------------------------------------------- markdown
|
|
1610
|
+
const MD = {
|
|
1611
|
+
body: { fontSize: 12, lineHeight: 1.72, color: C.ink2, wordBreak: 'break-word' },
|
|
1612
|
+
h1: { fontSize: '1.14em', fontWeight: 700, color: C.ink, margin: '0.62em 0 0.28em', lineHeight: 1.4 },
|
|
1613
|
+
h2: { fontSize: '1.08em', fontWeight: 700, color: C.ink, margin: '0.58em 0 0.24em', lineHeight: 1.4 },
|
|
1614
|
+
h3: { fontSize: '1.02em', fontWeight: 600, color: C.ink, margin: '0.52em 0 0.22em', lineHeight: 1.4 },
|
|
1615
|
+
p: { margin: '0 0 0.5em' },
|
|
1616
|
+
ul: { margin: '0 0 0.5em', paddingLeft: '1.5em' },
|
|
1617
|
+
ol: { margin: '0 0 0.5em', paddingLeft: '1.5em' },
|
|
1618
|
+
li: { margin: '0 0 0.16em' },
|
|
1619
|
+
quote: {
|
|
1620
|
+
margin: '0 0 0.5em', paddingLeft: '0.75em', borderLeft: '2px solid ' + C.line,
|
|
1621
|
+
color: C.ink3,
|
|
1622
|
+
},
|
|
1623
|
+
hr: { border: 'none', borderTop: '1px solid ' + C.line, margin: '0.75em 0' },
|
|
1624
|
+
codeBlock: {
|
|
1625
|
+
margin: '0 0 0.6em', padding: '0.55em 0.75em', borderRadius: 6, background: C.code,
|
|
1626
|
+
border: '1px solid ' + C.line, fontFamily: MONO,
|
|
1627
|
+
fontSize: '0.9em', lineHeight: 1.6, whiteSpace: 'pre-wrap', overflowX: 'auto', color: C.ink,
|
|
1628
|
+
},
|
|
1629
|
+
inlineCode: {
|
|
1630
|
+
fontFamily: MONO, fontSize: '0.92em',
|
|
1631
|
+
background: C.codeInline, borderRadius: 3, padding: '0 0.28em', color: C.ink,
|
|
1632
|
+
},
|
|
1633
|
+
link: { color: C.accent, textDecoration: 'none' },
|
|
1634
|
+
strong: { fontWeight: 700, color: C.ink },
|
|
1635
|
+
};
|
|
1636
|
+
|
|
1637
|
+
function inlineNodes(text, keyBase) {
|
|
1638
|
+
const out = [];
|
|
1639
|
+
const pattern = /(\*\*[^*\n]+\*\*|__[^_\n]+__|`[^`\n]+`|\[[^\]\n]+\]\([^)\s]+\)|\*[^*\n]+\*)/g;
|
|
1640
|
+
let last = 0;
|
|
1641
|
+
let match;
|
|
1642
|
+
let index = 0;
|
|
1643
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
1644
|
+
if (match.index > last) out.push(text.slice(last, match.index));
|
|
1645
|
+
const token = match[0];
|
|
1646
|
+
const key = keyBase + '-' + index;
|
|
1647
|
+
index += 1;
|
|
1648
|
+
if (token.startsWith('**') || token.startsWith('__')) {
|
|
1649
|
+
out.push(h('strong', { key, style: MD.strong }, token.slice(2, -2)));
|
|
1650
|
+
} else if (token.startsWith('`')) {
|
|
1651
|
+
out.push(h('code', { key, style: MD.inlineCode }, token.slice(1, -1)));
|
|
1652
|
+
} else if (token.startsWith('[')) {
|
|
1653
|
+
const link = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(token);
|
|
1654
|
+
out.push(h('a', {
|
|
1655
|
+
key,
|
|
1656
|
+
href: link ? link[2] : '#',
|
|
1657
|
+
target: '_blank',
|
|
1658
|
+
rel: 'noreferrer noopener',
|
|
1659
|
+
style: MD.link,
|
|
1660
|
+
}, link ? link[1] : token));
|
|
1661
|
+
} else {
|
|
1662
|
+
out.push(h('em', { key }, token.slice(1, -1)));
|
|
1663
|
+
}
|
|
1664
|
+
last = match.index + token.length;
|
|
1665
|
+
}
|
|
1666
|
+
if (last < text.length) out.push(text.slice(last));
|
|
1667
|
+
return out;
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
/** Small block-level Markdown renderer: headings, lists, quotes, fences, rules, paragraphs. */
|
|
1671
|
+
function renderMarkdown(text) {
|
|
1672
|
+
const source = String(text || '').replace(/\r\n/g, '\n');
|
|
1673
|
+
if (!source.trim()) return null;
|
|
1674
|
+
const lines = source.split('\n');
|
|
1675
|
+
const blocks = [];
|
|
1676
|
+
let i = 0;
|
|
1677
|
+
let blockKey = 0;
|
|
1678
|
+
|
|
1679
|
+
while (i < lines.length) {
|
|
1680
|
+
const line = lines[i];
|
|
1681
|
+
const fence = /^\s*```(.*)$/.exec(line);
|
|
1682
|
+
if (fence) {
|
|
1683
|
+
const body = [];
|
|
1684
|
+
i += 1;
|
|
1685
|
+
while (i < lines.length && !/^\s*```/.test(lines[i])) {
|
|
1686
|
+
body.push(lines[i]);
|
|
1687
|
+
i += 1;
|
|
1688
|
+
}
|
|
1689
|
+
i += 1;
|
|
1690
|
+
blocks.push(h('pre', { key: 'b' + blockKey, style: MD.codeBlock }, body.join('\n')));
|
|
1691
|
+
blockKey += 1;
|
|
1692
|
+
continue;
|
|
1693
|
+
}
|
|
1694
|
+
if (/^\s*$/.test(line)) { i += 1; continue; }
|
|
1695
|
+
if (/^#{1,6}\s+/.test(line)) {
|
|
1696
|
+
const level = line.match(/^#+/)[0].length;
|
|
1697
|
+
const content = line.replace(/^#+\s+/, '');
|
|
1698
|
+
const style = level === 1 ? MD.h1 : level === 2 ? MD.h2 : MD.h3;
|
|
1699
|
+
blocks.push(h('div', { key: 'b' + blockKey, style }, inlineNodes(content, 'h' + blockKey)));
|
|
1700
|
+
blockKey += 1;
|
|
1701
|
+
i += 1;
|
|
1702
|
+
continue;
|
|
1703
|
+
}
|
|
1704
|
+
if (/^\s*([-*_])\s*\1\s*\1[\s\-*_]*$/.test(line) || /^\s*---+\s*$/.test(line)) {
|
|
1705
|
+
blocks.push(h('hr', { key: 'b' + blockKey, style: MD.hr }));
|
|
1706
|
+
blockKey += 1;
|
|
1707
|
+
i += 1;
|
|
1708
|
+
continue;
|
|
1709
|
+
}
|
|
1710
|
+
if (/^\s*[-*+]\s+/.test(line)) {
|
|
1711
|
+
const items = [];
|
|
1712
|
+
while (i < lines.length && /^\s*[-*+]\s+/.test(lines[i])) {
|
|
1713
|
+
items.push(lines[i].replace(/^\s*[-*+]\s+/, ''));
|
|
1714
|
+
i += 1;
|
|
1715
|
+
}
|
|
1716
|
+
blocks.push(h('ul', { key: 'b' + blockKey, style: MD.ul },
|
|
1717
|
+
items.map((item, n) => h('li', { key: 'l' + n, style: MD.li }, inlineNodes(item, 'b' + blockKey + '-' + n)))));
|
|
1718
|
+
blockKey += 1;
|
|
1719
|
+
continue;
|
|
1720
|
+
}
|
|
1721
|
+
if (/^\s*\d+[.)]\s+/.test(line)) {
|
|
1722
|
+
const items = [];
|
|
1723
|
+
while (i < lines.length && /^\s*\d+[.)]\s+/.test(lines[i])) {
|
|
1724
|
+
items.push(lines[i].replace(/^\s*\d+[.)]\s+/, ''));
|
|
1725
|
+
i += 1;
|
|
1726
|
+
}
|
|
1727
|
+
blocks.push(h('ol', { key: 'b' + blockKey, style: MD.ol },
|
|
1728
|
+
items.map((item, n) => h('li', { key: 'l' + n, style: MD.li }, inlineNodes(item, 'b' + blockKey + '-' + n)))));
|
|
1729
|
+
blockKey += 1;
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
if (/^\s*>\s?/.test(line)) {
|
|
1733
|
+
const quoted = [];
|
|
1734
|
+
while (i < lines.length && /^\s*>\s?/.test(lines[i])) {
|
|
1735
|
+
quoted.push(lines[i].replace(/^\s*>\s?/, ''));
|
|
1736
|
+
i += 1;
|
|
1737
|
+
}
|
|
1738
|
+
blocks.push(h('div', { key: 'b' + blockKey, style: MD.quote },
|
|
1739
|
+
inlineNodes(quoted.join(' '), 'q' + blockKey)));
|
|
1740
|
+
blockKey += 1;
|
|
1741
|
+
continue;
|
|
1742
|
+
}
|
|
1743
|
+
const paragraph = [];
|
|
1744
|
+
while (i < lines.length && !/^\s*$/.test(lines[i])
|
|
1745
|
+
&& !/^\s*```/.test(lines[i]) && !/^#{1,6}\s+/.test(lines[i])
|
|
1746
|
+
&& !/^\s*([-*+]|\d+[.)])\s+/.test(lines[i]) && !/^\s*>\s?/.test(lines[i])) {
|
|
1747
|
+
paragraph.push(lines[i]);
|
|
1748
|
+
i += 1;
|
|
1749
|
+
}
|
|
1750
|
+
blocks.push(h('div', { key: 'b' + blockKey, style: MD.p },
|
|
1751
|
+
inlineNodes(paragraph.join('\n'), 'p' + blockKey)));
|
|
1752
|
+
blockKey += 1;
|
|
1753
|
+
}
|
|
1754
|
+
return blocks;
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
function Chevron({ dir, size, color }) {
|
|
1758
|
+
const box = size || 10;
|
|
1759
|
+
const rotate = dir === 'down' ? 0 : dir === 'up' ? 180 : dir === 'right' ? -90 : 90;
|
|
1760
|
+
return h('svg', {
|
|
1761
|
+
width: box, height: box, viewBox: '0 0 16 16', 'aria-hidden': true,
|
|
1762
|
+
style: {
|
|
1763
|
+
display: 'block', flex: '0 0 auto', color: color || 'currentColor',
|
|
1764
|
+
transform: 'rotate(' + rotate + 'deg)',
|
|
1765
|
+
},
|
|
1766
|
+
}, h('path', {
|
|
1767
|
+
d: 'M3.5 6 L8 10.5 L12.5 6',
|
|
1768
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.7,
|
|
1769
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
1770
|
+
}));
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
// ------------------------------------------------------------- components
|
|
1774
|
+
const MOTION_EXIT_MS = 140;
|
|
1775
|
+
const MOTION_CAMERA_MS = 320;
|
|
1776
|
+
const reducedMotion = () => typeof window.matchMedia === 'function'
|
|
1777
|
+
&& window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
1778
|
+
|
|
1779
|
+
/** Keep closing content only for its exit; timers never control app state. */
|
|
1780
|
+
function Presence({ show, className, children, ...props }) {
|
|
1781
|
+
const [present, setPresent] = useState(show);
|
|
1782
|
+
useEffect(() => {
|
|
1783
|
+
if (show) { setPresent(true); return undefined; }
|
|
1784
|
+
if (reducedMotion()) { setPresent(false); return undefined; }
|
|
1785
|
+
const timer = window.setTimeout(() => setPresent(false), MOTION_EXIT_MS);
|
|
1786
|
+
return () => window.clearTimeout(timer);
|
|
1787
|
+
}, [show]);
|
|
1788
|
+
if (!show && !present) return null;
|
|
1789
|
+
return h('div', {
|
|
1790
|
+
...props,
|
|
1791
|
+
className: 'dsh-tree-presence ' + (className || ''),
|
|
1792
|
+
'data-open': show ? 'true' : 'false',
|
|
1793
|
+
inert: !show ? true : undefined,
|
|
1794
|
+
'aria-hidden': !show ? true : undefined,
|
|
1795
|
+
}, children);
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
/** Animate the actual camera so culling, dots and minimap stay in sync. */
|
|
1799
|
+
function useCanvasView() {
|
|
1800
|
+
const [view, setView] = useState({ x: 40, y: 40, z: 1 });
|
|
1801
|
+
const current = useRef(view);
|
|
1802
|
+
const motion = useRef(null);
|
|
1803
|
+
const commit = useCallback((next) => {
|
|
1804
|
+
current.current = next;
|
|
1805
|
+
setView(next);
|
|
1806
|
+
}, []);
|
|
1807
|
+
const cancel = useCallback(() => {
|
|
1808
|
+
if (motion.current) window.cancelAnimationFrame(motion.current.frame);
|
|
1809
|
+
motion.current = null;
|
|
1810
|
+
}, []);
|
|
1811
|
+
const move = useCallback((target, animate) => {
|
|
1812
|
+
cancel();
|
|
1813
|
+
const from = current.current;
|
|
1814
|
+
const next = typeof target === 'function' ? target(from) : target;
|
|
1815
|
+
if (!animate || reducedMotion() || typeof window.requestAnimationFrame !== 'function') {
|
|
1816
|
+
commit(next);
|
|
1817
|
+
return;
|
|
1818
|
+
}
|
|
1819
|
+
const run = { frame: null, start: null, target: next };
|
|
1820
|
+
const step = (time) => {
|
|
1821
|
+
if (motion.current !== run) return;
|
|
1822
|
+
if (run.start === null) run.start = time;
|
|
1823
|
+
const progress = Math.min(1, (time - run.start) / MOTION_CAMERA_MS);
|
|
1824
|
+
const eased = 1 - Math.pow(1 - progress, 3);
|
|
1825
|
+
commit({
|
|
1826
|
+
x: from.x + (next.x - from.x) * eased,
|
|
1827
|
+
y: from.y + (next.y - from.y) * eased,
|
|
1828
|
+
z: from.z + (next.z - from.z) * eased,
|
|
1829
|
+
});
|
|
1830
|
+
if (progress < 1) run.frame = window.requestAnimationFrame(step);
|
|
1831
|
+
else motion.current = null;
|
|
1832
|
+
};
|
|
1833
|
+
motion.current = run;
|
|
1834
|
+
run.frame = window.requestAnimationFrame(step);
|
|
1835
|
+
}, [cancel, commit]);
|
|
1836
|
+
useEffect(() => {
|
|
1837
|
+
const media = typeof window.matchMedia === 'function'
|
|
1838
|
+
? window.matchMedia('(prefers-reduced-motion: reduce)') : null;
|
|
1839
|
+
const onChange = () => {
|
|
1840
|
+
if (!media.matches || !motion.current) return;
|
|
1841
|
+
const target = motion.current.target;
|
|
1842
|
+
cancel();
|
|
1843
|
+
commit(target);
|
|
1844
|
+
};
|
|
1845
|
+
if (media && media.addEventListener) media.addEventListener('change', onChange);
|
|
1846
|
+
return () => {
|
|
1847
|
+
cancel();
|
|
1848
|
+
if (media && media.removeEventListener) media.removeEventListener('change', onChange);
|
|
1849
|
+
};
|
|
1850
|
+
}, [cancel, commit]);
|
|
1851
|
+
return [view, move, cancel];
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
function Badge({ label, tone }) {
|
|
1855
|
+
return h('span', {
|
|
1856
|
+
style: {
|
|
1857
|
+
fontSize: 10, letterSpacing: '0.06em', color: tone || C.ink3,
|
|
1858
|
+
textTransform: 'uppercase', fontVariantNumeric: 'tabular-nums',
|
|
1859
|
+
},
|
|
1860
|
+
}, label);
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
function StatusDot({ status, waiting }) {
|
|
1864
|
+
const color = status === 'done' ? C.ok : status === 'error' ? C.warn : C.accent;
|
|
1865
|
+
const pulse = waiting || status === 'thinking' || status === 'streaming' || status === 'running';
|
|
1866
|
+
return h('span', {
|
|
1867
|
+
style: {
|
|
1868
|
+
width: 6, height: 6, borderRadius: 3, background: color,
|
|
1869
|
+
boxShadow: pulse ? '0 0 0 3px ' + C.accentSoft : 'none',
|
|
1870
|
+
display: 'inline-block',
|
|
1871
|
+
animation: pulse ? 'dshTreePulse 1.4s ease-in-out infinite' : 'none',
|
|
1872
|
+
},
|
|
1873
|
+
});
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
/** What the model is doing right now: thinking, calling a tool, or writing. */
|
|
1877
|
+
function LiveProgress({ node }) {
|
|
1878
|
+
useLocale();
|
|
1879
|
+
const awaiting = node.awaiting === 'question' || node.awaiting === 'approval';
|
|
1880
|
+
const running = awaiting || node.status === 'thinking' || node.status === 'streaming' || node.status === 'running';
|
|
1881
|
+
const [, tick] = useState(0);
|
|
1882
|
+
useEffect(() => {
|
|
1883
|
+
if (!running) return undefined;
|
|
1884
|
+
const timer = setInterval(() => tick((n) => n + 1), 1000);
|
|
1885
|
+
return () => clearInterval(timer);
|
|
1886
|
+
}, [running]);
|
|
1887
|
+
if (!running) return null;
|
|
1888
|
+
const tool = node.tool || '';
|
|
1889
|
+
const step = node.step || 0;
|
|
1890
|
+
const elapsed = node.startedAt ? Math.max(0, Math.round((Date.now() - node.startedAt) / 1000)) : 0;
|
|
1891
|
+
// A pending question/approval is not "calling the tool": name the wait so
|
|
1892
|
+
// the card says the ball is in the user's court.
|
|
1893
|
+
const label = awaiting
|
|
1894
|
+
? T(node.awaiting === 'approval' ? 'live.awaitingApproval' : 'live.awaiting')
|
|
1895
|
+
: tool
|
|
1896
|
+
? T('live.calling', { tool })
|
|
1897
|
+
: node.answer
|
|
1898
|
+
? T('live.outputting')
|
|
1899
|
+
: step > 0 ? T('live.thinking') : T('live.starting');
|
|
1900
|
+
return h('div', {
|
|
1901
|
+
style: {
|
|
1902
|
+
marginTop: 7, padding: '6px 8px', borderRadius: 7,
|
|
1903
|
+
background: C.accentSoft, border: '1px solid ' + C.accentEdge,
|
|
1904
|
+
display: 'flex', alignItems: 'center', gap: 7,
|
|
1905
|
+
fontSize: 10.5, color: C.accent, lineHeight: 1.4,
|
|
1906
|
+
},
|
|
1907
|
+
},
|
|
1908
|
+
h('svg', {
|
|
1909
|
+
width: 12, height: 12, viewBox: '0 0 16 16', 'aria-hidden': true,
|
|
1910
|
+
style: {
|
|
1911
|
+
display: 'block', flex: '0 0 auto',
|
|
1912
|
+
animation: 'dshTreeSpin 0.75s linear infinite',
|
|
1913
|
+
},
|
|
1914
|
+
},
|
|
1915
|
+
h('circle', {
|
|
1916
|
+
cx: 8, cy: 8, r: 6, fill: 'none',
|
|
1917
|
+
stroke: 'currentColor', strokeWidth: 2, opacity: 0.22,
|
|
1918
|
+
}),
|
|
1919
|
+
h('path', {
|
|
1920
|
+
d: 'M8 2 A6 6 0 0 1 14 8',
|
|
1921
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 2,
|
|
1922
|
+
strokeLinecap: 'round',
|
|
1923
|
+
})),
|
|
1924
|
+
h('span', { style: { flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } }, label),
|
|
1925
|
+
step > 0 ? h('span', { style: { fontVariantNumeric: 'tabular-nums', opacity: 0.75 } }, T('live.step', { step })) : null,
|
|
1926
|
+
elapsed > 0 ? h('span', { style: { fontVariantNumeric: 'tabular-nums', opacity: 0.6 } }, elapsed + 's') : null);
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
function NodeCard({ node, selected, dimmed, index, store, api, rootId, match, collapsed, childCount, descendants, isLatest, entering }) {
|
|
1930
|
+
useLocale();
|
|
1931
|
+
const snapshot = store.getSnapshot();
|
|
1932
|
+
const composing = snapshot.composingId === node.id;
|
|
1933
|
+
const [expanded, setExpanded] = useState(false);
|
|
1934
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
1935
|
+
const cardRef = useRef(null);
|
|
1936
|
+
// Freeze this mount's entry decision; streamed tokens do not restart it.
|
|
1937
|
+
const animateEntry = useRef(entering);
|
|
1938
|
+
const isRunning = isInProgressNode(node);
|
|
1939
|
+
const awaiting = node.awaiting === 'question' || node.awaiting === 'approval';
|
|
1940
|
+
const answer = node.answer || '';
|
|
1941
|
+
const answerText = answer.length > 1400 ? answer.slice(0, 1400) + T('card.truncated') : answer;
|
|
1942
|
+
|
|
1943
|
+
useEffect(() => {
|
|
1944
|
+
const element = cardRef.current;
|
|
1945
|
+
if (!element || typeof ResizeObserver === 'undefined') return undefined;
|
|
1946
|
+
const report = () => store.setHeight(node.id, element.offsetHeight);
|
|
1947
|
+
report();
|
|
1948
|
+
const observer = new ResizeObserver(report);
|
|
1949
|
+
observer.observe(element);
|
|
1950
|
+
return () => observer.disconnect();
|
|
1951
|
+
}, [store, node.id]);
|
|
1952
|
+
|
|
1953
|
+
useEffect(() => {
|
|
1954
|
+
if (!selected) setMenuOpen(false);
|
|
1955
|
+
}, [selected]);
|
|
1956
|
+
|
|
1957
|
+
const requestDelete = () => {
|
|
1958
|
+
let ok = true;
|
|
1959
|
+
try {
|
|
1960
|
+
ok = window.confirm(T('card.deleteConfirm'));
|
|
1961
|
+
} catch (error) {
|
|
1962
|
+
ok = true;
|
|
1963
|
+
}
|
|
1964
|
+
if (ok) api.deleteSubtree(rootId, node.id);
|
|
1965
|
+
};
|
|
1966
|
+
|
|
1967
|
+
const statusLabel = awaiting
|
|
1968
|
+
? T(node.awaiting === 'approval' ? 'card.status.awaitingApproval' : 'card.status.awaiting')
|
|
1969
|
+
: node.status === 'thinking' ? T('card.status.thinking')
|
|
1970
|
+
: node.status === 'streaming' || node.status === 'running' ? T('card.status.streaming')
|
|
1971
|
+
: node.status === 'error' ? T('card.status.error')
|
|
1972
|
+
: node.status === 'stopped' ? T('card.status.stopped')
|
|
1973
|
+
: T('card.status.done');
|
|
1974
|
+
|
|
1975
|
+
return h('div', {
|
|
1976
|
+
ref: cardRef,
|
|
1977
|
+
className: 'dsh-tree-card' + (animateEntry.current ? ' dsh-tree-card--enter' : ''),
|
|
1978
|
+
onMouseDown: (event) => event.stopPropagation(),
|
|
1979
|
+
onClick: () => store.select(node.id),
|
|
1980
|
+
style: {
|
|
1981
|
+
position: 'absolute',
|
|
1982
|
+
left: 0,
|
|
1983
|
+
top: 0,
|
|
1984
|
+
width: NODE_W,
|
|
1985
|
+
minHeight: 132,
|
|
1986
|
+
background: C.card,
|
|
1987
|
+
border: '1px solid ' + (awaiting || match || selected ? C.accent : C.cardEdge),
|
|
1988
|
+
borderLeft: '3px solid ' + (selected || isRunning ? C.accent : dimmed ? C.cardEdge : C.line),
|
|
1989
|
+
borderRadius: 10,
|
|
1990
|
+
boxShadow: awaiting || selected
|
|
1991
|
+
? '0 0 0 4px ' + C.accentSoft + ', 0 6px 18px rgba(0,0,0,0.08)'
|
|
1992
|
+
: match
|
|
1993
|
+
? '0 0 0 3px ' + C.accentSoft + ', 0 2px 8px rgba(0,0,0,0.05)'
|
|
1994
|
+
: '0 2px 8px rgba(0,0,0,0.05)',
|
|
1995
|
+
padding: '10px 12px 9px',
|
|
1996
|
+
opacity: dimmed ? 0.72 : 1,
|
|
1997
|
+
boxSizing: 'border-box',
|
|
1998
|
+
cursor: 'pointer',
|
|
1999
|
+
},
|
|
2000
|
+
},
|
|
2001
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4, minHeight: 20 } },
|
|
2002
|
+
h(StatusDot, { status: node.status, waiting: awaiting }),
|
|
2003
|
+
h(Badge, { label: index, tone: C.accent }),
|
|
2004
|
+
isLatest
|
|
2005
|
+
? h('span', {
|
|
2006
|
+
title: T('card.latestTitle'),
|
|
2007
|
+
style: {
|
|
2008
|
+
fontSize: 9.5, letterSpacing: '0.04em', color: C.accent, background: C.accentSoft,
|
|
2009
|
+
borderRadius: 999, padding: '0 6px', lineHeight: '15px', whiteSpace: 'nowrap',
|
|
2010
|
+
},
|
|
2011
|
+
}, T('card.latestBadge'))
|
|
2012
|
+
: null,
|
|
2013
|
+
h(Badge, { label: statusLabel }),
|
|
2014
|
+
node.cost && (node.cost.ms || node.cost.tokens)
|
|
2015
|
+
? h('span', {
|
|
2016
|
+
title: [
|
|
2017
|
+
node.cost.ms ? T('card.cost.elapsed', { value: formatCost({ ms: node.cost.ms, tokens: 0 }) }) : '',
|
|
2018
|
+
node.cost.tokens ? T('card.cost.context', { value: formatCost({ ms: 0, tokens: node.cost.tokens }) }) : '',
|
|
2019
|
+
node.cost.cached ? T('card.cost.cached', { value: formatCost({ ms: 0, tokens: node.cost.cached }) }) : '',
|
|
2020
|
+
].filter(Boolean).join(' · '),
|
|
2021
|
+
style: {
|
|
2022
|
+
fontSize: 10.5, color: C.ink3, fontVariantNumeric: 'tabular-nums',
|
|
2023
|
+
whiteSpace: 'nowrap', letterSpacing: '0.02em',
|
|
2024
|
+
},
|
|
2025
|
+
}, formatCost(node.cost))
|
|
2026
|
+
: null,
|
|
2027
|
+
h('span', { style: { flex: 1 } }),
|
|
2028
|
+
isRunning
|
|
2029
|
+
? h('button', {
|
|
2030
|
+
onClick: (event) => {
|
|
2031
|
+
event.stopPropagation();
|
|
2032
|
+
store.stop(node.id);
|
|
2033
|
+
},
|
|
2034
|
+
title: T('card.stopTitle'),
|
|
2035
|
+
className: BTN + ' dsh-tree-btn--sm dsh-tree-btn--danger',
|
|
2036
|
+
}, T('card.stop'))
|
|
2037
|
+
: null,
|
|
2038
|
+
node.status === 'done' && node.answerSeq
|
|
2039
|
+
? h('button', {
|
|
2040
|
+
onClick: (event) => {
|
|
2041
|
+
event.stopPropagation();
|
|
2042
|
+
if (composing) store.cancelCompose();
|
|
2043
|
+
else store.compose(node.id);
|
|
2044
|
+
},
|
|
2045
|
+
title: composing ? T('composer.cancel') : T('card.ask'),
|
|
2046
|
+
'aria-label': composing ? T('composer.cancel') : T('card.ask'),
|
|
2047
|
+
'aria-expanded': composing,
|
|
2048
|
+
className: 'dsh-tree-btn dsh-tree-btn--icon dsh-tree-btn--sm dsh-tree-ask',
|
|
2049
|
+
style: { color: C.accent },
|
|
2050
|
+
}, h(Icon, { name: 'plus' }))
|
|
2051
|
+
: null,
|
|
2052
|
+
selected
|
|
2053
|
+
? h('span', { style: { position: 'relative', display: 'inline-flex' } },
|
|
2054
|
+
h('button', {
|
|
2055
|
+
onClick: (event) => {
|
|
2056
|
+
event.stopPropagation();
|
|
2057
|
+
setMenuOpen((value) => !value);
|
|
2058
|
+
},
|
|
2059
|
+
title: T('card.more'),
|
|
2060
|
+
'aria-label': T('card.more'),
|
|
2061
|
+
'aria-expanded': menuOpen,
|
|
2062
|
+
className: 'dsh-tree-btn dsh-tree-btn--icon dsh-tree-btn--sm',
|
|
2063
|
+
}, h(Icon, { name: 'more' })),
|
|
2064
|
+
h(Presence, {
|
|
2065
|
+
show: menuOpen,
|
|
2066
|
+
className: 'dsh-tree-menu',
|
|
2067
|
+
onKeyDown: (event) => {
|
|
2068
|
+
if (event.key === 'Escape') {
|
|
2069
|
+
event.stopPropagation();
|
|
2070
|
+
setMenuOpen(false);
|
|
2071
|
+
event.currentTarget.previousElementSibling.focus();
|
|
2072
|
+
}
|
|
2073
|
+
},
|
|
2074
|
+
onMouseDown: (event) => event.stopPropagation(),
|
|
2075
|
+
onClick: (event) => event.stopPropagation(),
|
|
2076
|
+
style: {
|
|
2077
|
+
position: 'absolute', top: 28, right: 0, zIndex: 5, background: C.card,
|
|
2078
|
+
border: '1px solid ' + C.cardEdge, borderRadius: 9, padding: 4,
|
|
2079
|
+
boxShadow: '0 8px 22px rgba(0,0,0,0.12)', minWidth: 132,
|
|
2080
|
+
},
|
|
2081
|
+
}, h('button', {
|
|
2082
|
+
onClick: () => {
|
|
2083
|
+
setMenuOpen(false);
|
|
2084
|
+
requestDelete();
|
|
2085
|
+
},
|
|
2086
|
+
title: T('card.deleteTitle'),
|
|
2087
|
+
className: 'dsh-tree-btn dsh-tree-btn--sm dsh-tree-btn--danger',
|
|
2088
|
+
style: { border: 'none', background: 'transparent', width: '100%', justifyContent: 'flex-start' },
|
|
2089
|
+
}, T('card.delete'))))
|
|
2090
|
+
: null),
|
|
2091
|
+
h('div', {
|
|
2092
|
+
style: {
|
|
2093
|
+
fontSize: 12.5, fontWeight: 600, lineHeight: 1.45, color: C.ink,
|
|
2094
|
+
display: '-webkit-box', WebkitLineClamp: 3, WebkitBoxOrient: 'vertical', overflow: 'hidden',
|
|
2095
|
+
},
|
|
2096
|
+
}, node.question || T('card.noQuestion')),
|
|
2097
|
+
h('div', {
|
|
2098
|
+
onClick: (event) => {
|
|
2099
|
+
if (!answer) return;
|
|
2100
|
+
event.stopPropagation();
|
|
2101
|
+
store.openReader(node.id);
|
|
2102
|
+
},
|
|
2103
|
+
title: answer ? T('card.readTitle') : undefined,
|
|
2104
|
+
style: {
|
|
2105
|
+
...MD.body,
|
|
2106
|
+
marginTop: 6,
|
|
2107
|
+
maxHeight: expanded ? 3000 : 146,
|
|
2108
|
+
overflow: 'hidden',
|
|
2109
|
+
cursor: answer ? 'zoom-in' : 'default',
|
|
2110
|
+
maskImage: (!expanded && answer.length > 320) ? 'linear-gradient(#000 82%, transparent 100%)' : 'none',
|
|
2111
|
+
WebkitMaskImage: (!expanded && answer.length > 320) ? 'linear-gradient(#000 82%, transparent 100%)' : 'none',
|
|
2112
|
+
},
|
|
2113
|
+
}, answer ? renderMarkdown(answerText) : null),
|
|
2114
|
+
h(LiveProgress, { node }),
|
|
2115
|
+
h(Presence, { show: composing, className: 'dsh-tree-composer' },
|
|
2116
|
+
h(Composer, { store, api, rootId, node, active: composing })),
|
|
2117
|
+
answer.length > 260 || childCount > 0
|
|
2118
|
+
? h('div', {
|
|
2119
|
+
style: {
|
|
2120
|
+
marginTop: 9, paddingTop: 7, borderTop: '1px solid ' + C.cardEdge,
|
|
2121
|
+
display: 'flex', alignItems: 'center', gap: 10, minHeight: 22,
|
|
2122
|
+
},
|
|
2123
|
+
},
|
|
2124
|
+
answer.length > 260
|
|
2125
|
+
? h('button', {
|
|
2126
|
+
onClick: (event) => {
|
|
2127
|
+
event.stopPropagation();
|
|
2128
|
+
setExpanded((value) => !value);
|
|
2129
|
+
},
|
|
2130
|
+
title: expanded ? T('card.collapseTitle') : T('card.expandTitle'),
|
|
2131
|
+
className: LINK,
|
|
2132
|
+
style: { color: C.accent },
|
|
2133
|
+
},
|
|
2134
|
+
expanded ? T('card.collapse') : T('card.expand'),
|
|
2135
|
+
h(Chevron, { dir: expanded ? 'up' : 'down', size: 9 }))
|
|
2136
|
+
: null,
|
|
2137
|
+
answer
|
|
2138
|
+
? h('button', {
|
|
2139
|
+
onClick: (event) => {
|
|
2140
|
+
event.stopPropagation();
|
|
2141
|
+
store.openReader(node.id);
|
|
2142
|
+
},
|
|
2143
|
+
title: T('card.fullscreenTitle'),
|
|
2144
|
+
className: LINK,
|
|
2145
|
+
style: { cursor: 'zoom-in' },
|
|
2146
|
+
}, T('card.fullscreen'),
|
|
2147
|
+
h('svg', {
|
|
2148
|
+
width: 9, height: 9, viewBox: '0 0 16 16', 'aria-hidden': true,
|
|
2149
|
+
style: { display: 'block', flex: '0 0 auto' },
|
|
2150
|
+
}, h('path', {
|
|
2151
|
+
d: 'M6 2.5H2.5V6 M10 2.5h3.5V6 M6 13.5H2.5V10 M10 13.5h3.5V10',
|
|
2152
|
+
fill: 'none', stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round',
|
|
2153
|
+
})))
|
|
2154
|
+
: null,
|
|
2155
|
+
h('span', { style: { flex: 1 } }),
|
|
2156
|
+
childCount > 0
|
|
2157
|
+
? h('button', {
|
|
2158
|
+
onClick: (event) => {
|
|
2159
|
+
event.stopPropagation();
|
|
2160
|
+
store.toggleCollapse(node.id);
|
|
2161
|
+
},
|
|
2162
|
+
title: collapsed ? T('card.unfoldBranch') : T('card.foldBranch'),
|
|
2163
|
+
className: collapsed ? CHIP : LINK,
|
|
2164
|
+
},
|
|
2165
|
+
h(Chevron, { dir: collapsed ? 'right' : 'down', size: 9 }),
|
|
2166
|
+
T(collapsed ? 'card.unfoldDescendants' : 'card.foldDescendants', { count: descendants || childCount }))
|
|
2167
|
+
: null)
|
|
2168
|
+
: null);
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
function Composer({ store, api, rootId, node, active }) {
|
|
2172
|
+
useLocale();
|
|
2173
|
+
const [value, setValue] = useState('');
|
|
2174
|
+
const ref = useRef(null);
|
|
2175
|
+
useEffect(() => {
|
|
2176
|
+
if (active && ref.current) ref.current.focus();
|
|
2177
|
+
if (!active) setValue('');
|
|
2178
|
+
}, [active]);
|
|
2179
|
+
|
|
2180
|
+
const send = () => {
|
|
2181
|
+
const question = value.trim();
|
|
2182
|
+
if (!question || store.getSnapshot().composingId !== node.id) return;
|
|
2183
|
+
store.cancelCompose();
|
|
2184
|
+
api.branch(rootId, node, question);
|
|
2185
|
+
};
|
|
2186
|
+
|
|
2187
|
+
return h('div', { style: { marginTop: 8, paddingTop: 8, borderTop: '1px dashed ' + C.line } },
|
|
2188
|
+
h('textarea', {
|
|
2189
|
+
ref,
|
|
2190
|
+
value,
|
|
2191
|
+
onChange: (event) => setValue(event.target.value),
|
|
2192
|
+
onKeyDown: (event) => {
|
|
2193
|
+
if (event.key === 'Enter' && !event.shiftKey) {
|
|
2194
|
+
event.preventDefault();
|
|
2195
|
+
send();
|
|
2196
|
+
} else if (event.key === 'Escape') {
|
|
2197
|
+
event.preventDefault();
|
|
2198
|
+
store.cancelCompose();
|
|
2199
|
+
}
|
|
2200
|
+
},
|
|
2201
|
+
placeholder: T('composer.placeholder'),
|
|
2202
|
+
'aria-label': T('composer.label'),
|
|
2203
|
+
rows: 2,
|
|
2204
|
+
className: 'dsh-tree-textarea',
|
|
2205
|
+
style: {
|
|
2206
|
+
width: '100%', boxSizing: 'border-box', resize: 'none', fontFamily: FONT,
|
|
2207
|
+
fontSize: 12.5, lineHeight: 1.6, padding: '8px 10px', borderRadius: 8,
|
|
2208
|
+
border: '1px solid ' + C.cardEdge, outline: 'none', color: C.ink, background: C.card,
|
|
2209
|
+
},
|
|
2210
|
+
}),
|
|
2211
|
+
h('div', { style: { display: 'flex', gap: 6, marginTop: 8, alignItems: 'center' } },
|
|
2212
|
+
h('span', { style: { fontSize: 10.5, color: C.ink3, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' } },
|
|
2213
|
+
T('composer.from', { question: (node.question || '').slice(0, 12) })),
|
|
2214
|
+
h('span', { style: { flex: 1 } }),
|
|
2215
|
+
h('button', {
|
|
2216
|
+
onClick: () => store.cancelCompose(),
|
|
2217
|
+
className: BTN + ' dsh-tree-btn--sm',
|
|
2218
|
+
}, T('composer.cancel')),
|
|
2219
|
+
h('button', {
|
|
2220
|
+
onClick: send,
|
|
2221
|
+
className: BTN + ' dsh-tree-btn--sm dsh-tree-btn--primary',
|
|
2222
|
+
}, T('composer.send'))));
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
function Canvas({ store, api, rootId, size }) {
|
|
2226
|
+
useLocale();
|
|
2227
|
+
const snapshot = store.getSnapshot();
|
|
2228
|
+
const nodes = snapshot.nodes;
|
|
2229
|
+
const [view, setView, cancelViewMotion] = useCanvasView();
|
|
2230
|
+
// All known nodes count as seen, including those outside the viewport.
|
|
2231
|
+
const seenNodes = useRef(new Set(nodes.map((node) => node.id)));
|
|
2232
|
+
useEffect(() => {
|
|
2233
|
+
for (const node of nodes) seenNodes.current.add(node.id);
|
|
2234
|
+
}, [nodes]);
|
|
2235
|
+
const [query, setQuery] = useState('');
|
|
2236
|
+
const drag = useRef(null);
|
|
2237
|
+
const wrapRef = useRef(null);
|
|
2238
|
+
const searchRef = useRef(null);
|
|
2239
|
+
const toolbarRef = useRef(null);
|
|
2240
|
+
const [pendingFocus, setPendingFocus] = useState(null);
|
|
2241
|
+
|
|
2242
|
+
/** Deleted nodes stay in the log but leave the canvas entirely. */
|
|
2243
|
+
const hiddenIds = useMemo(() => hiddenSetOf(nodes, snapshot.hidden), [nodes, snapshot.hidden]);
|
|
2244
|
+
const liveNodes = useMemo(
|
|
2245
|
+
() => (hiddenIds.size ? nodes.filter((node) => !hiddenIds.has(node.id)) : nodes),
|
|
2246
|
+
[nodes, hiddenIds]);
|
|
2247
|
+
const activeIds = useMemo(() => {
|
|
2248
|
+
const set = new Set();
|
|
2249
|
+
for (const node of liveNodes) if (isInProgressNode(node)) set.add(node.id);
|
|
2250
|
+
return set;
|
|
2251
|
+
}, [liveNodes]);
|
|
2252
|
+
// `defaultCollapsedMap` is the raw depth rule, `collapsedMap` is what is
|
|
2253
|
+
// actually laid out: the path to an active node stays open.
|
|
2254
|
+
const defaultCollapsedMap = useMemo(
|
|
2255
|
+
() => effectiveCollapsedOf(liveNodes, snapshot.collapsed),
|
|
2256
|
+
[liveNodes, snapshot.collapsed]);
|
|
2257
|
+
const collapsedMap = useMemo(
|
|
2258
|
+
() => effectiveCollapsedOf(liveNodes, snapshot.collapsed, activeAncestorsOf(liveNodes, activeIds)),
|
|
2259
|
+
[liveNodes, snapshot.collapsed, activeIds]);
|
|
2260
|
+
const counts = useMemo(() => {
|
|
2261
|
+
const childrenOf = childrenMapOf(liveNodes);
|
|
2262
|
+
const direct = new Map();
|
|
2263
|
+
for (const [key, list] of childrenOf) direct.set(key, list.length);
|
|
2264
|
+
return { direct, descendants: descendantCountsOf(liveNodes, childrenOf) };
|
|
2265
|
+
}, [liveNodes]);
|
|
2266
|
+
const layout = useMemo(
|
|
2267
|
+
() => layoutTree(liveNodes, collapsedMap, snapshot.heights),
|
|
2268
|
+
[liveNodes, collapsedMap, snapshot.heights]);
|
|
2269
|
+
const path = useMemo(
|
|
2270
|
+
() => (snapshot.selectedId ? pathOf(liveNodes, snapshot.selectedId) : new Set()),
|
|
2271
|
+
[liveNodes, snapshot.selectedId]);
|
|
2272
|
+
const matches = useMemo(() => {
|
|
2273
|
+
const needle = query.trim().toLowerCase();
|
|
2274
|
+
if (!needle) return null;
|
|
2275
|
+
const hit = new Set();
|
|
2276
|
+
for (const node of liveNodes) {
|
|
2277
|
+
const haystack = ((node.question || '') + '\n' + (node.answer || '')).toLowerCase();
|
|
2278
|
+
if (haystack.indexOf(needle) >= 0) hit.add(node.id);
|
|
2279
|
+
}
|
|
2280
|
+
return hit;
|
|
2281
|
+
}, [liveNodes, query]);
|
|
2282
|
+
const labels = useMemo(() => labelMapOf(liveNodes, collapsedMap), [liveNodes, collapsedMap]);
|
|
2283
|
+
const running = liveNodes.filter((node) => RUNNING_STATUSES.indexOf(node.status) >= 0).length;
|
|
2284
|
+
const waiting = liveNodes.filter((node) => node.awaiting === 'question' || node.awaiting === 'approval').length;
|
|
2285
|
+
const hiddenCount = hiddenIds.size;
|
|
2286
|
+
const tombstoned = Object.keys(snapshot.hidden || {}).length;
|
|
2287
|
+
|
|
2288
|
+
/** Only render cards near the viewport; the canvas itself is one transform. */
|
|
2289
|
+
const visibleNodes = useMemo(() => {
|
|
2290
|
+
if (liveNodes.length <= 40) return liveNodes;
|
|
2291
|
+
const margin = 400;
|
|
2292
|
+
const left = -view.x / view.z - margin;
|
|
2293
|
+
const top = -view.y / view.z - margin;
|
|
2294
|
+
const right = left + size.w / view.z + margin * 2;
|
|
2295
|
+
const bottom = top + size.h / view.z + margin * 2;
|
|
2296
|
+
const shown = [];
|
|
2297
|
+
for (const node of liveNodes) {
|
|
2298
|
+
const pos = layout.positions.get(node.id);
|
|
2299
|
+
if (!pos) continue;
|
|
2300
|
+
const height = snapshot.heights[node.id] || NODE_H;
|
|
2301
|
+
if (pos.x + NODE_W >= left && pos.x <= right && pos.y + height >= top && pos.y <= bottom) {
|
|
2302
|
+
shown.push(node);
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
return shown.length ? shown : liveNodes;
|
|
2306
|
+
}, [liveNodes, layout, view, size, snapshot.heights]);
|
|
2307
|
+
|
|
2308
|
+
/** Reveal first; position only after the expanded layout has committed. */
|
|
2309
|
+
const focusNode = useCallback((id) => {
|
|
2310
|
+
store.reveal(id);
|
|
2311
|
+
setPendingFocus(id);
|
|
2312
|
+
}, [store]);
|
|
2313
|
+
useEffect(() => {
|
|
2314
|
+
if (!pendingFocus) return;
|
|
2315
|
+
const pos = layout.positions.get(pendingFocus);
|
|
2316
|
+
if (!pos) { setPendingFocus(null); return; }
|
|
2317
|
+
const rect = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { width: 900, height: 600 };
|
|
2318
|
+
const top = (toolbarRef.current ? toolbarRef.current.offsetHeight : 44) + 28;
|
|
2319
|
+
const height = snapshot.heights[pendingFocus] || NODE_H;
|
|
2320
|
+
const z = Math.max(0.2, Math.min(1, (rect.width - 48) / NODE_W, (rect.height - top - 32) / height));
|
|
2321
|
+
setView({ z, x: rect.width / 2 - (pos.x + NODE_W / 2) * z,
|
|
2322
|
+
y: top + (rect.height - top) / 2 - (pos.y + height / 2) * z }, true);
|
|
2323
|
+
setPendingFocus(null);
|
|
2324
|
+
}, [pendingFocus, layout, snapshot.heights]);
|
|
2325
|
+
const latest = latestNodeOf(liveNodes);
|
|
2326
|
+
|
|
2327
|
+
/** Jump to the next card matching `predicate`, wrapping around. */
|
|
2328
|
+
const focusNext = useCallback((predicate) => {
|
|
2329
|
+
const ids = liveNodes.filter(predicate).map((node) => node.id);
|
|
2330
|
+
if (!ids.length) return;
|
|
2331
|
+
const currentIndex = ids.indexOf(snapshot.selectedId);
|
|
2332
|
+
focusNode(ids[currentIndex < 0 ? 0 : (currentIndex + 1) % ids.length]);
|
|
2333
|
+
}, [liveNodes, snapshot.selectedId, focusNode]);
|
|
2334
|
+
|
|
2335
|
+
// The in-progress card must never be reachable only by unfolding by hand.
|
|
2336
|
+
// Opening the path is enough for a running branch; the first branch that
|
|
2337
|
+
// starts waiting on the user is also brought into view, because that is
|
|
2338
|
+
// the one card the user has to act on.
|
|
2339
|
+
const openedRef = useRef(new Set());
|
|
2340
|
+
const announcedRef = useRef(new Set());
|
|
2341
|
+
// A different tree means a different id space; start the follow state over.
|
|
2342
|
+
useEffect(() => {
|
|
2343
|
+
openedRef.current = new Set();
|
|
2344
|
+
announcedRef.current = new Set();
|
|
2345
|
+
}, [store]);
|
|
2346
|
+
useEffect(() => {
|
|
2347
|
+
for (const node of liveNodes) {
|
|
2348
|
+
if (!isInProgressNode(node)) {
|
|
2349
|
+
openedRef.current.delete(node.id);
|
|
2350
|
+
announcedRef.current.delete(node.id);
|
|
2351
|
+
continue;
|
|
2352
|
+
}
|
|
2353
|
+
if (!openedRef.current.has(node.id)) {
|
|
2354
|
+
openedRef.current.add(node.id);
|
|
2355
|
+
const ancestors = [...pathOf(liveNodes, node.id)].filter((id) => id !== node.id);
|
|
2356
|
+
if (ancestors.some((id) => defaultCollapsedMap[id] === true)) store.expandPath(node.id);
|
|
2357
|
+
}
|
|
2358
|
+
const waitingNode = node.awaiting === 'question' || node.awaiting === 'approval';
|
|
2359
|
+
if (!waitingNode) {
|
|
2360
|
+
announcedRef.current.delete(node.id);
|
|
2361
|
+
continue;
|
|
2362
|
+
}
|
|
2363
|
+
if (announcedRef.current.has(node.id)) continue;
|
|
2364
|
+
const firstWait = announcedRef.current.size === 0;
|
|
2365
|
+
announcedRef.current.add(node.id);
|
|
2366
|
+
// Never yank the canvas out from under the reader overlay, and do not
|
|
2367
|
+
// steal the viewport when another branch is already waiting.
|
|
2368
|
+
if (firstWait && !store.getSnapshot().readingId) focusNode(node.id);
|
|
2369
|
+
}
|
|
2370
|
+
}, [liveNodes, defaultCollapsedMap, store, focusNode]);
|
|
2371
|
+
|
|
2372
|
+
/** Minimap projection: fit the whole tree, centred, with a viewport frame. */
|
|
2373
|
+
const minimapRef = useRef({ scale: 1, ox: 0, oy: 0 });
|
|
2374
|
+
const minimap = useMemo(() => {
|
|
2375
|
+
const w = 168;
|
|
2376
|
+
const h = 112;
|
|
2377
|
+
const scale = Math.min((w - 12) / Math.max(1, layout.width), (h - 12) / Math.max(1, layout.height));
|
|
2378
|
+
return {
|
|
2379
|
+
scale,
|
|
2380
|
+
ox: (w - layout.width * scale) / 2,
|
|
2381
|
+
oy: (h - layout.height * scale) / 2,
|
|
2382
|
+
};
|
|
2383
|
+
}, [layout.width, layout.height]);
|
|
2384
|
+
minimapRef.current = minimap;
|
|
2385
|
+
|
|
2386
|
+
/** Step to the next hit without leaving the current position. */
|
|
2387
|
+
const jumpToNextMatch = useCallback((direction) => {
|
|
2388
|
+
if (!matches || matches.size === 0) return;
|
|
2389
|
+
const hitIds = liveNodes.filter((node) => matches.has(node.id)).map((node) => node.id);
|
|
2390
|
+
if (hitIds.length === 0) return;
|
|
2391
|
+
const currentIndex = hitIds.indexOf(snapshot.selectedId);
|
|
2392
|
+
const nextIndex = currentIndex < 0
|
|
2393
|
+
? 0
|
|
2394
|
+
: (currentIndex + direction + hitIds.length) % hitIds.length;
|
|
2395
|
+
focusNode(hitIds[nextIndex]);
|
|
2396
|
+
}, [matches, liveNodes, snapshot.selectedId, focusNode]);
|
|
2397
|
+
|
|
2398
|
+
const onWheel = (event) => {
|
|
2399
|
+
event.preventDefault();
|
|
2400
|
+
const rect = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { left: 0, top: 0 };
|
|
2401
|
+
const px = event.clientX - rect.left;
|
|
2402
|
+
const py = event.clientY - rect.top;
|
|
2403
|
+
setView((current) => {
|
|
2404
|
+
const next = Math.min(2, Math.max(0.2, current.z * (event.deltaY < 0 ? 1.08 : 0.92)));
|
|
2405
|
+
const scale = next / current.z;
|
|
2406
|
+
return { z: next, x: px - (px - current.x) * scale, y: py - (py - current.y) * scale };
|
|
2407
|
+
});
|
|
2408
|
+
};
|
|
2409
|
+
|
|
2410
|
+
const onMouseDown = (event) => {
|
|
2411
|
+
if (event.button !== 0) return;
|
|
2412
|
+
cancelViewMotion();
|
|
2413
|
+
drag.current = { x: event.clientX, y: event.clientY, ox: view.x, oy: view.y };
|
|
2414
|
+
store.select(null);
|
|
2415
|
+
};
|
|
2416
|
+
const onMouseMove = (event) => {
|
|
2417
|
+
// Snapshot the drag start before scheduling state: React may run the
|
|
2418
|
+
// updater after mouseup has already cleared the ref, and reading
|
|
2419
|
+
// `drag.current.ox` at that point throws. Capturing here keeps the
|
|
2420
|
+
// handler independent of the ref's later lifetime.
|
|
2421
|
+
const start = drag.current;
|
|
2422
|
+
if (!start) return;
|
|
2423
|
+
const dx = event.clientX - start.x;
|
|
2424
|
+
const dy = event.clientY - start.y;
|
|
2425
|
+
setView((current) => ({ z: current.z, x: start.ox + dx, y: start.oy + dy }));
|
|
2426
|
+
};
|
|
2427
|
+
const endDrag = () => {
|
|
2428
|
+
drag.current = null;
|
|
2429
|
+
};
|
|
2430
|
+
|
|
2431
|
+
/** Height the floating toolbar occupies, so the tree starts below it. */
|
|
2432
|
+
const chromeHeight = () => (toolbarRef.current ? toolbarRef.current.offsetHeight : 48) + 22;
|
|
2433
|
+
|
|
2434
|
+
const fit = () => {
|
|
2435
|
+
const rect = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { width: 900, height: 600 };
|
|
2436
|
+
if (!layout.width || !layout.height) return;
|
|
2437
|
+
const chrome = chromeHeight();
|
|
2438
|
+
const availableW = Math.max(200, rect.width - 80);
|
|
2439
|
+
const availableH = Math.max(200, rect.height - chrome - 48);
|
|
2440
|
+
const z = Math.min(1, Math.min(availableW / layout.width, availableH / layout.height));
|
|
2441
|
+
const scale = Math.max(0.2, z);
|
|
2442
|
+
setView({
|
|
2443
|
+
z: scale,
|
|
2444
|
+
x: (rect.width - layout.width * scale) / 2,
|
|
2445
|
+
y: chrome,
|
|
2446
|
+
}, true);
|
|
2447
|
+
};
|
|
2448
|
+
|
|
2449
|
+
/** Zoom around the viewport centre so the middle of the picture stays put. */
|
|
2450
|
+
const zoomBy = (factor) => {
|
|
2451
|
+
const rect = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { width: 900, height: 600 };
|
|
2452
|
+
const cx = rect.width / 2;
|
|
2453
|
+
const cy = rect.height / 2;
|
|
2454
|
+
setView((current) => {
|
|
2455
|
+
const next = Math.min(2, Math.max(0.2, current.z * factor));
|
|
2456
|
+
const scale = next / current.z;
|
|
2457
|
+
return { z: next, x: cx - (cx - current.x) * scale, y: cy - (cy - current.y) * scale };
|
|
2458
|
+
}, true);
|
|
2459
|
+
};
|
|
2460
|
+
|
|
2461
|
+
const fittedRef = useRef(false);
|
|
2462
|
+
useEffect(() => {
|
|
2463
|
+
if (fittedRef.current || !liveNodes.length || !size.w) return;
|
|
2464
|
+
fittedRef.current = true;
|
|
2465
|
+
const rect = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { width: 900, height: 600 };
|
|
2466
|
+
const chrome = chromeHeight();
|
|
2467
|
+
const availableW = Math.max(200, rect.width - 96);
|
|
2468
|
+
const availableH = Math.max(200, rect.height - chrome - 56);
|
|
2469
|
+
const raw = Math.min(1, availableW / Math.max(1, layout.width), availableH / Math.max(1, layout.height));
|
|
2470
|
+
const scale = Math.max(0.62, raw);
|
|
2471
|
+
setView({
|
|
2472
|
+
z: scale,
|
|
2473
|
+
x: Math.max(28, (rect.width - layout.width * scale) / 2),
|
|
2474
|
+
y: chrome,
|
|
2475
|
+
});
|
|
2476
|
+
}, [liveNodes.length, size.w]);
|
|
2477
|
+
|
|
2478
|
+
// Ctrl/Cmd+F is the tree's own search while this view is mounted.
|
|
2479
|
+
useEffect(() => {
|
|
2480
|
+
const onKey = (event) => {
|
|
2481
|
+
if (!(event.metaKey || event.ctrlKey) || event.altKey) return;
|
|
2482
|
+
if (event.key !== 'f' && event.key !== 'F') return;
|
|
2483
|
+
event.preventDefault();
|
|
2484
|
+
if (searchRef.current) {
|
|
2485
|
+
searchRef.current.focus();
|
|
2486
|
+
if (typeof searchRef.current.select === 'function') searchRef.current.select();
|
|
2487
|
+
}
|
|
2488
|
+
};
|
|
2489
|
+
window.addEventListener('keydown', onKey);
|
|
2490
|
+
return () => window.removeEventListener('keydown', onKey);
|
|
2491
|
+
}, []);
|
|
2492
|
+
|
|
2493
|
+
return h('div', {
|
|
2494
|
+
ref: wrapRef,
|
|
2495
|
+
inert: snapshot.readingId ? true : undefined,
|
|
2496
|
+
onWheel,
|
|
2497
|
+
onMouseDown,
|
|
2498
|
+
onMouseMove,
|
|
2499
|
+
onMouseUp: endDrag,
|
|
2500
|
+
onMouseLeave: endDrag,
|
|
2501
|
+
style: {
|
|
2502
|
+
position: 'absolute', inset: 0, overflow: 'hidden', background: C.canvas,
|
|
2503
|
+
backgroundImage: 'radial-gradient(color-mix(in srgb, ' + C.line + ' 55%, transparent) 1px, transparent 1px)',
|
|
2504
|
+
backgroundSize: (15 * view.z) + 'px ' + (15 * view.z) + 'px',
|
|
2505
|
+
backgroundPosition: view.x + 'px ' + view.y + 'px',
|
|
2506
|
+
cursor: drag.current ? 'grabbing' : 'grab',
|
|
2507
|
+
userSelect: 'none',
|
|
2508
|
+
},
|
|
2509
|
+
},
|
|
2510
|
+
h('div', {
|
|
2511
|
+
style: {
|
|
2512
|
+
position: 'absolute', left: 0, top: 0,
|
|
2513
|
+
transform: 'translate(' + view.x + 'px, ' + view.y + 'px) scale(' + view.z + ')',
|
|
2514
|
+
transformOrigin: '0 0',
|
|
2515
|
+
width: Math.max(layout.width + 200, 600),
|
|
2516
|
+
height: Math.max(layout.height + 200, 400),
|
|
2517
|
+
},
|
|
2518
|
+
},
|
|
2519
|
+
h('svg', {
|
|
2520
|
+
width: Math.max(layout.width + 200, 600),
|
|
2521
|
+
height: Math.max(layout.height + 200, 400),
|
|
2522
|
+
style: { position: 'absolute', left: 0, top: 0, overflow: 'visible', pointerEvents: 'none' },
|
|
2523
|
+
},
|
|
2524
|
+
layout.edges.map((edge) => {
|
|
2525
|
+
const active = path.has(edge.id.split('>')[0]) && path.has(edge.id.split('>')[1]);
|
|
2526
|
+
return h('path', {
|
|
2527
|
+
key: edge.id,
|
|
2528
|
+
d: 'M' + edge.x1 + ' ' + edge.y1 + ' C' + edge.x1 + ' ' + (edge.y1 + 40) + ', ' + edge.x2 + ' ' + (edge.y2 - 40) + ', ' + edge.x2 + ' ' + edge.y2,
|
|
2529
|
+
fill: 'none',
|
|
2530
|
+
stroke: active ? C.accent : C.line,
|
|
2531
|
+
strokeWidth: active ? 2 : 1.4,
|
|
2532
|
+
opacity: active ? 1 : 0.85,
|
|
2533
|
+
});
|
|
2534
|
+
})),
|
|
2535
|
+
visibleNodes.map((node) => {
|
|
2536
|
+
const pos = layout.positions.get(node.id);
|
|
2537
|
+
if (!pos) return null;
|
|
2538
|
+
return h('div', {
|
|
2539
|
+
key: node.id,
|
|
2540
|
+
style: { position: 'absolute', left: pos.x, top: pos.y },
|
|
2541
|
+
}, h(NodeCard, {
|
|
2542
|
+
node, store, api, rootId,
|
|
2543
|
+
selected: snapshot.selectedId === node.id,
|
|
2544
|
+
dimmed: snapshot.selectedId !== null && !path.has(node.id),
|
|
2545
|
+
index: labels.get(node.id),
|
|
2546
|
+
match: matches ? matches.has(node.id) : false,
|
|
2547
|
+
collapsed: collapsedMap[node.id] === true,
|
|
2548
|
+
childCount: counts.direct.get(node.id) || 0,
|
|
2549
|
+
descendants: counts.descendants.get(node.id) || 0,
|
|
2550
|
+
isLatest: latest ? latest.id === node.id : false,
|
|
2551
|
+
entering: !seenNodes.current.has(node.id),
|
|
2552
|
+
}));
|
|
2553
|
+
})),
|
|
2554
|
+
|
|
2555
|
+
// toolbar
|
|
2556
|
+
h('div', {
|
|
2557
|
+
ref: toolbarRef,
|
|
2558
|
+
onMouseDown: (event) => event.stopPropagation(),
|
|
2559
|
+
onWheel: (event) => event.stopPropagation(),
|
|
2560
|
+
style: {
|
|
2561
|
+
position: 'absolute', top: 10, left: 12, right: 12, display: 'flex', gap: 8,
|
|
2562
|
+
flexWrap: 'wrap', padding: 8, borderRadius: 12, background: C.card,
|
|
2563
|
+
border: '1px solid ' + C.cardEdge, boxShadow: '0 2px 8px rgba(0,0,0,0.04)',
|
|
2564
|
+
alignItems: 'center', fontSize: 12, color: C.ink3,
|
|
2565
|
+
},
|
|
2566
|
+
},
|
|
2567
|
+
h('span', { style: { fontWeight: 600, color: C.ink, fontSize: 12 } }, T('view.heading')),
|
|
2568
|
+
h('span', null, '· ' + T('view.nodes', { count: liveNodes.length })),
|
|
2569
|
+
running > 0
|
|
2570
|
+
? h('button', {
|
|
2571
|
+
onClick: () => focusNext(isInProgressNode),
|
|
2572
|
+
title: T('view.runningTitle'),
|
|
2573
|
+
...pillProps(),
|
|
2574
|
+
}, T('view.running', { count: running }))
|
|
2575
|
+
: null,
|
|
2576
|
+
waiting > 0
|
|
2577
|
+
? h('button', {
|
|
2578
|
+
onClick: () => focusNext((node) => Boolean(node.awaiting)),
|
|
2579
|
+
title: T('view.awaitingTitle'),
|
|
2580
|
+
...pillProps({ animation: 'dshTreePulse 2.4s ease-in-out infinite' }),
|
|
2581
|
+
}, T('view.awaiting', { count: waiting }))
|
|
2582
|
+
: null,
|
|
2583
|
+
snapshot.error ? h('span', { style: { color: C.warn } }, '· ' + snapshot.error) : null,
|
|
2584
|
+
snapshot.meta.hasMore
|
|
2585
|
+
? h('button', { onClick: () => api.loadOlder(rootId), ...btnProps() }, T('view.loadOlder'))
|
|
2586
|
+
: null,
|
|
2587
|
+
tombstoned > 0
|
|
2588
|
+
? h('button', {
|
|
2589
|
+
onClick: () => api.restoreHidden(rootId),
|
|
2590
|
+
title: T('view.restoreHiddenTitle'),
|
|
2591
|
+
...pillProps(),
|
|
2592
|
+
}, T('view.restoreHidden', { count: Math.max(hiddenCount, tombstoned) }))
|
|
2593
|
+
: null,
|
|
2594
|
+
h('span', { style: { flex: 1 } }),
|
|
2595
|
+
h('div', {
|
|
2596
|
+
style: {
|
|
2597
|
+
display: 'flex', alignItems: 'center', gap: 6, border: '1px solid ' + C.cardEdge,
|
|
2598
|
+
background: C.canvas, borderRadius: 8, padding: '0 8px', minHeight: 32,
|
|
2599
|
+
boxSizing: 'border-box', color: C.ink3,
|
|
2600
|
+
},
|
|
2601
|
+
},
|
|
2602
|
+
h(Icon, { name: 'search' }),
|
|
2603
|
+
h('input', {
|
|
2604
|
+
ref: searchRef,
|
|
2605
|
+
value: query,
|
|
2606
|
+
onChange: (event) => setQuery(event.target.value),
|
|
2607
|
+
onKeyDown: (event) => {
|
|
2608
|
+
if (event.key === 'Enter') {
|
|
2609
|
+
event.preventDefault();
|
|
2610
|
+
jumpToNextMatch(event.shiftKey ? -1 : 1);
|
|
2611
|
+
} else if (event.key === 'Escape') {
|
|
2612
|
+
event.preventDefault();
|
|
2613
|
+
setQuery('');
|
|
2614
|
+
}
|
|
2615
|
+
},
|
|
2616
|
+
placeholder: T('view.searchPlaceholder'),
|
|
2617
|
+
'aria-label': T('view.searchLabel'),
|
|
2618
|
+
style: {
|
|
2619
|
+
border: 'none', outline: 'none', fontSize: 12, width: 92, padding: '5px 0',
|
|
2620
|
+
fontFamily: FONT, color: C.ink, background: 'transparent',
|
|
2621
|
+
},
|
|
2622
|
+
}),
|
|
2623
|
+
matches && matches.size > 0
|
|
2624
|
+
? h('span', { style: { fontSize: 11, color: C.accent, whiteSpace: 'nowrap', fontVariantNumeric: 'tabular-nums' } }, String(matches.size))
|
|
2625
|
+
: null),
|
|
2626
|
+
h('div', { role: 'group', 'aria-label': T('view.zoomGroup'), style: { display: 'flex', alignItems: 'center', gap: 4 } },
|
|
2627
|
+
h('button', {
|
|
2628
|
+
title: T('view.zoomOut'), 'aria-label': T('view.zoomOut'), disabled: view.z <= 0.21,
|
|
2629
|
+
onClick: () => zoomBy(1 / 1.15),
|
|
2630
|
+
className: BTN_ICON,
|
|
2631
|
+
}, h(Icon, { name: 'minus' })),
|
|
2632
|
+
h('span', {
|
|
2633
|
+
title: T('view.zoomLevel'),
|
|
2634
|
+
style: { minWidth: 44, textAlign: 'center', fontVariantNumeric: 'tabular-nums', color: C.ink2, fontSize: 11.5 },
|
|
2635
|
+
}, Math.round(view.z * 100) + '%'),
|
|
2636
|
+
h('button', {
|
|
2637
|
+
title: T('view.zoomIn'), 'aria-label': T('view.zoomIn'), disabled: view.z >= 1.99,
|
|
2638
|
+
onClick: () => zoomBy(1.15),
|
|
2639
|
+
className: BTN_ICON,
|
|
2640
|
+
}, h(Icon, { name: 'plus' }))),
|
|
2641
|
+
h('button', { onClick: fit, className: BTN, title: T('view.fitTitle') }, h(Icon, { name: 'fit' }), T('view.fit')),
|
|
2642
|
+
h('button', {
|
|
2643
|
+
disabled: !latest,
|
|
2644
|
+
onClick: () => { if (!latest) return; setQuery(''); focusNode(latest.id); },
|
|
2645
|
+
title: latest ? T('view.latestTitle') : T('view.latestTitleEmpty'),
|
|
2646
|
+
className: BTN_PRIMARY,
|
|
2647
|
+
}, h(Icon, { name: 'latest' }), T('view.latest'))),
|
|
2648
|
+
|
|
2649
|
+
// minimap
|
|
2650
|
+
layout.width > 0
|
|
2651
|
+
? h('div', {
|
|
2652
|
+
onMouseDown: (event) => event.stopPropagation(),
|
|
2653
|
+
style: {
|
|
2654
|
+
position: 'absolute', right: 12, bottom: 12, width: 168, height: 112,
|
|
2655
|
+
border: '1px solid ' + C.cardEdge, borderRadius: 8, background: C.card,
|
|
2656
|
+
boxShadow: '0 2px 10px rgba(0,0,0,0.06)', overflow: 'hidden', cursor: 'pointer',
|
|
2657
|
+
},
|
|
2658
|
+
onClick: (event) => {
|
|
2659
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
2660
|
+
const map = minimapRef.current;
|
|
2661
|
+
if (!map) return;
|
|
2662
|
+
// Invert the same scale+centering the preview uses, so a click
|
|
2663
|
+
// lands on the node under the cursor instead of near it.
|
|
2664
|
+
const localX = (event.clientX - rect.left - map.ox) / map.scale;
|
|
2665
|
+
const localY = (event.clientY - rect.top - map.oy) / map.scale;
|
|
2666
|
+
const wrapper = wrapRef.current ? wrapRef.current.getBoundingClientRect() : { width: 900, height: 600 };
|
|
2667
|
+
setView((current) => ({
|
|
2668
|
+
z: current.z,
|
|
2669
|
+
x: wrapper.width / 2 - localX * current.z,
|
|
2670
|
+
y: wrapper.height / 2 - localY * current.z,
|
|
2671
|
+
}), true);
|
|
2672
|
+
},
|
|
2673
|
+
},
|
|
2674
|
+
h('div', {
|
|
2675
|
+
style: {
|
|
2676
|
+
position: 'absolute', left: 0, top: 0, width: '100%', height: '100%',
|
|
2677
|
+
transform: 'translate(' + minimap.ox + 'px, ' + minimap.oy + 'px) scale(' + minimap.scale + ')',
|
|
2678
|
+
transformOrigin: '0 0',
|
|
2679
|
+
},
|
|
2680
|
+
},
|
|
2681
|
+
h('svg', {
|
|
2682
|
+
width: layout.width,
|
|
2683
|
+
height: layout.height,
|
|
2684
|
+
style: { position: 'absolute', left: 0, top: 0, overflow: 'visible' },
|
|
2685
|
+
},
|
|
2686
|
+
layout.edges.map((edge) => h('path', {
|
|
2687
|
+
key: edge.id,
|
|
2688
|
+
d: 'M' + edge.x1 + ' ' + edge.y1 + ' L' + edge.x2 + ' ' + edge.y2,
|
|
2689
|
+
fill: 'none', stroke: C.line, strokeWidth: 3,
|
|
2690
|
+
})),
|
|
2691
|
+
liveNodes.map((node) => {
|
|
2692
|
+
const pos = layout.positions.get(node.id);
|
|
2693
|
+
if (!pos) return null;
|
|
2694
|
+
const active = path.has(node.id);
|
|
2695
|
+
// A node waiting on the user is a filled dot on the minimap too,
|
|
2696
|
+
// so a blocked branch can be spotted without hunting for it.
|
|
2697
|
+
const waiting = node.awaiting === 'question' || node.awaiting === 'approval';
|
|
2698
|
+
return h('rect', {
|
|
2699
|
+
key: node.id,
|
|
2700
|
+
x: pos.x, y: pos.y, width: NODE_W,
|
|
2701
|
+
height: snapshot.heights[node.id] || NODE_H,
|
|
2702
|
+
rx: 10,
|
|
2703
|
+
fill: matches && matches.has(node.id) ? C.accent : waiting ? C.accent : active ? C.accentSoft : C.line,
|
|
2704
|
+
stroke: active ? C.accent : 'transparent',
|
|
2705
|
+
strokeWidth: 1 / minimap.scale,
|
|
2706
|
+
});
|
|
2707
|
+
})),
|
|
2708
|
+
// What the canvas is looking at right now.
|
|
2709
|
+
h('rect', {
|
|
2710
|
+
x: -view.x / view.z, y: -view.y / view.z,
|
|
2711
|
+
width: size.w / view.z, height: size.h / view.z,
|
|
2712
|
+
fill: C.accentSoft, fillOpacity: 0.45,
|
|
2713
|
+
stroke: C.accent, strokeWidth: 1.5 / minimap.scale,
|
|
2714
|
+
rx: 6,
|
|
2715
|
+
})))
|
|
2716
|
+
: null,
|
|
2717
|
+
|
|
2718
|
+
nodes.length === 0
|
|
2719
|
+
? h('div', {
|
|
2720
|
+
style: {
|
|
2721
|
+
position: 'absolute', inset: 0, display: 'flex', alignItems: 'center',
|
|
2722
|
+
justifyContent: 'center', pointerEvents: 'none',
|
|
2723
|
+
},
|
|
2724
|
+
}, h('div', { style: { textAlign: 'center', color: C.ink3 } },
|
|
2725
|
+
h('div', { style: { fontSize: 13, color: C.ink2, marginBottom: 6 } }, T('view.empty.title')),
|
|
2726
|
+
h('div', { style: { fontSize: 11.5, lineHeight: 1.7 } },
|
|
2727
|
+
T('view.empty.hint')),
|
|
2728
|
+
h('div', { style: { fontSize: 11, marginTop: 4 } },
|
|
2729
|
+
T('view.empty.report', {
|
|
2730
|
+
events: snapshot.meta.events,
|
|
2731
|
+
state: snapshot.meta.openState || '—',
|
|
2732
|
+
}))))
|
|
2733
|
+
: null);
|
|
2734
|
+
}
|
|
2735
|
+
|
|
2736
|
+
function Icon({ name }) {
|
|
2737
|
+
const paths = {
|
|
2738
|
+
plus: 'M8 3v10M3 8h10', minus: 'M3 8h10',
|
|
2739
|
+
search: 'M11 11l3 3M12 7a5 5 0 1 1-10 0 5 5 0 0 1 10 0',
|
|
2740
|
+
fit: 'M6 2H2v4M10 2h4v4M2 10v4h4M14 10v4h-4',
|
|
2741
|
+
latest: 'M8 1v3M8 12v3M1 8h3M12 8h3M12 8a4 4 0 1 1-8 0 4 4 0 0 1 8 0',
|
|
2742
|
+
more: 'M3 8h.01M8 8h.01M13 8h.01',
|
|
2743
|
+
close: 'M4 4l8 8M12 4l-8 8',
|
|
2744
|
+
};
|
|
2745
|
+
return h('svg', { width: 16, height: 16, viewBox: '0 0 16 16', 'aria-hidden': true,
|
|
2746
|
+
style: { display: 'block', flexShrink: 0 } }, h('path', {
|
|
2747
|
+
d: paths[name], fill: 'none', stroke: 'currentColor', strokeWidth: 1.7,
|
|
2748
|
+
strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
2749
|
+
}));
|
|
2750
|
+
}
|
|
2751
|
+
|
|
2752
|
+
// Buttons: all paint lives in CSS so :hover/:active/:focus-visible can exist.
|
|
2753
|
+
// Inline styles only carry layout that varies per call site.
|
|
2754
|
+
const BTN = 'dsh-tree-btn';
|
|
2755
|
+
const BTN_ICON = 'dsh-tree-btn dsh-tree-btn--icon';
|
|
2756
|
+
const BTN_PRIMARY = 'dsh-tree-btn dsh-tree-btn--primary';
|
|
2757
|
+
const CHIP = 'dsh-tree-chip';
|
|
2758
|
+
const LINK = 'dsh-tree-link';
|
|
2759
|
+
|
|
2760
|
+
function btnProps(extraStyle) {
|
|
2761
|
+
return { className: BTN, style: extraStyle };
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2764
|
+
function pillProps(extraStyle) {
|
|
2765
|
+
return { className: CHIP, style: extraStyle };
|
|
2766
|
+
}
|
|
2767
|
+
|
|
2768
|
+
class Boundary extends React.Component {
|
|
2769
|
+
constructor(props) {
|
|
2770
|
+
super(props);
|
|
2771
|
+
this.state = { error: null, stack: '' };
|
|
2772
|
+
}
|
|
2773
|
+
static getDerivedStateFromError(error) {
|
|
2774
|
+
return { error: String((error && error.message) || error) };
|
|
2775
|
+
}
|
|
2776
|
+
componentDidCatch(error) {
|
|
2777
|
+
if (window.console && window.console.error) window.console.error('[tree]', error);
|
|
2778
|
+
this.setState({ stack: String((error && error.stack) || '').split('\n').slice(0, 4).join('\n') });
|
|
2779
|
+
}
|
|
2780
|
+
render() {
|
|
2781
|
+
if (this.state.error) {
|
|
2782
|
+
return h('div', {
|
|
2783
|
+
style: {
|
|
2784
|
+
position: 'absolute', inset: 0, padding: '24px 28px', fontFamily: FONT,
|
|
2785
|
+
fontSize: 12.5, lineHeight: 1.8, color: C.warn, background: C.canvas, overflow: 'auto',
|
|
2786
|
+
},
|
|
2787
|
+
},
|
|
2788
|
+
h('div', { style: { fontWeight: 600, marginBottom: 6 } }, T('error.title')),
|
|
2789
|
+
h('div', { style: { color: C.ink2, fontFamily: MONO } }, this.state.error),
|
|
2790
|
+
h('div', {
|
|
2791
|
+
style: {
|
|
2792
|
+
color: C.ink3, fontFamily: MONO, fontSize: 10.5,
|
|
2793
|
+
whiteSpace: 'pre-wrap', marginTop: 8,
|
|
2794
|
+
},
|
|
2795
|
+
}, this.state.stack),
|
|
2796
|
+
h('button', {
|
|
2797
|
+
onClick: () => this.setState({ error: null, stack: '' }),
|
|
2798
|
+
style: {
|
|
2799
|
+
marginTop: 14, border: '1px solid ' + C.buttonFill, background: C.buttonFill, color: C.onAccent,
|
|
2800
|
+
borderRadius: 8, fontSize: 12, padding: '4px 12px', cursor: 'pointer', fontFamily: FONT,
|
|
2801
|
+
},
|
|
2802
|
+
}, T('error.reload')),
|
|
2803
|
+
h('span', { style: { color: C.ink3, fontSize: 11, marginLeft: 10 } },
|
|
2804
|
+
T('error.hint')));
|
|
2805
|
+
}
|
|
2806
|
+
return this.props.children;
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
|
|
2810
|
+
function Reader({ node, open, onClose }) {
|
|
2811
|
+
useLocale();
|
|
2812
|
+
const panelRef = useRef(null);
|
|
2813
|
+
const closeRef = useRef(null);
|
|
2814
|
+
const close = useRef(onClose);
|
|
2815
|
+
close.current = onClose;
|
|
2816
|
+
useEffect(() => {
|
|
2817
|
+
if (!open) return undefined;
|
|
2818
|
+
const previousFocus = document.activeElement;
|
|
2819
|
+
if (closeRef.current) closeRef.current.focus();
|
|
2820
|
+
const onKey = (event) => {
|
|
2821
|
+
if (event.key === 'Escape') {
|
|
2822
|
+
event.stopPropagation();
|
|
2823
|
+
close.current();
|
|
2824
|
+
} else if (event.key === 'Tab' && panelRef.current) {
|
|
2825
|
+
const controls = panelRef.current.querySelectorAll('button, a[href], input, textarea, [tabindex="0"]');
|
|
2826
|
+
const first = controls[0];
|
|
2827
|
+
const last = controls[controls.length - 1];
|
|
2828
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
2829
|
+
event.preventDefault();
|
|
2830
|
+
if (last) last.focus();
|
|
2831
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
2832
|
+
event.preventDefault();
|
|
2833
|
+
if (first) first.focus();
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
};
|
|
2837
|
+
window.addEventListener('keydown', onKey, true);
|
|
2838
|
+
return () => {
|
|
2839
|
+
window.removeEventListener('keydown', onKey, true);
|
|
2840
|
+
if (previousFocus && previousFocus.isConnected) previousFocus.focus();
|
|
2841
|
+
};
|
|
2842
|
+
}, [open]);
|
|
2843
|
+
|
|
2844
|
+
if (!node) return null;
|
|
2845
|
+
return h(Presence, {
|
|
2846
|
+
show: open,
|
|
2847
|
+
className: 'dsh-tree-reader',
|
|
2848
|
+
onClick: onClose,
|
|
2849
|
+
style: {
|
|
2850
|
+
position: 'absolute', inset: 0, zIndex: 40, background: C.scrim,
|
|
2851
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '28px 34px',
|
|
2852
|
+
},
|
|
2853
|
+
},
|
|
2854
|
+
h('div', {
|
|
2855
|
+
ref: panelRef,
|
|
2856
|
+
className: 'dsh-tree-reader-panel',
|
|
2857
|
+
role: 'dialog',
|
|
2858
|
+
'aria-modal': true,
|
|
2859
|
+
'aria-label': node.question || T('card.noQuestion'),
|
|
2860
|
+
onClick: (event) => event.stopPropagation(),
|
|
2861
|
+
style: {
|
|
2862
|
+
width: '100%', maxWidth: 820, maxHeight: '100%', background: C.card,
|
|
2863
|
+
borderRadius: 14, boxShadow: '0 20px 60px rgba(0,0,0,0.28)',
|
|
2864
|
+
display: 'flex', flexDirection: 'column', overflow: 'hidden',
|
|
2865
|
+
},
|
|
2866
|
+
},
|
|
2867
|
+
h('div', {
|
|
2868
|
+
style: {
|
|
2869
|
+
padding: '14px 20px 11px', borderBottom: '1px solid ' + C.line,
|
|
2870
|
+
display: 'flex', alignItems: 'flex-start', gap: 12,
|
|
2871
|
+
},
|
|
2872
|
+
},
|
|
2873
|
+
h('div', { style: { flex: 1, minWidth: 0 } },
|
|
2874
|
+
h('div', {
|
|
2875
|
+
style: { fontSize: 10.5, color: C.ink3, letterSpacing: '0.05em', marginBottom: 4 },
|
|
2876
|
+
}, (node.kind === 'branch' ? T('reader.branch') : T('reader.mainline')) + ' · ' + (node.id || '')),
|
|
2877
|
+
h('div', {
|
|
2878
|
+
style: { fontSize: 15, fontWeight: 600, lineHeight: 1.5, color: C.ink },
|
|
2879
|
+
}, node.question || T('card.noQuestion'))),
|
|
2880
|
+
h('button', {
|
|
2881
|
+
ref: closeRef,
|
|
2882
|
+
onClick: onClose,
|
|
2883
|
+
title: T('reader.close'),
|
|
2884
|
+
'aria-label': T('reader.closeLabel'),
|
|
2885
|
+
className: 'dsh-tree-btn dsh-tree-btn--icon',
|
|
2886
|
+
style: { width: 30, height: 30, minWidth: 30, minHeight: 30 },
|
|
2887
|
+
}, h(Icon, { name: 'close' }))),
|
|
2888
|
+
h('div', {
|
|
2889
|
+
style: {
|
|
2890
|
+
padding: '18px 22px 22px', overflow: 'auto', flex: 1,
|
|
2891
|
+
...MD.body, fontSize: 13.4, lineHeight: 1.85,
|
|
2892
|
+
},
|
|
2893
|
+
}, node.answer ? renderMarkdown(node.answer) : T('reader.noAnswer')),
|
|
2894
|
+
h('div', {
|
|
2895
|
+
style: {
|
|
2896
|
+
padding: '8px 20px', borderTop: '1px solid ' + C.cardEdge, fontSize: 10.5,
|
|
2897
|
+
color: C.ink3, display: 'flex', gap: 10, alignItems: 'center',
|
|
2898
|
+
},
|
|
2899
|
+
},
|
|
2900
|
+
h('span', null, T('reader.hint')),
|
|
2901
|
+
h('span', { style: { flex: 1 } }),
|
|
2902
|
+
node.cost && (node.cost.ms || node.cost.tokens)
|
|
2903
|
+
? h('span', null, formatCost(node.cost))
|
|
2904
|
+
: null)));
|
|
2905
|
+
}
|
|
2906
|
+
|
|
2907
|
+
function TreeView(props) {
|
|
2908
|
+
const { sessionId, api } = props;
|
|
2909
|
+
useLocale();
|
|
2910
|
+
const rootId = useMemo(() => api.rootOf(sessionId), [sessionId, api]);
|
|
2911
|
+
const store = useMemo(() => api.storeFor(rootId), [rootId, api]);
|
|
2912
|
+
const [, force] = useState(0);
|
|
2913
|
+
const hostRef = useRef(null);
|
|
2914
|
+
const [size, setSize] = useState({ w: 0, h: 0 });
|
|
2915
|
+
|
|
2916
|
+
useEffect(() => store.subscribe(() => force((n) => n + 1)), [store]);
|
|
2917
|
+
useEffect(() => {
|
|
2918
|
+
api.ensureOpen(rootId, store);
|
|
2919
|
+
}, [rootId, store, api]);
|
|
2920
|
+
useEffect(() => {
|
|
2921
|
+
const element = hostRef.current;
|
|
2922
|
+
if (!element) return undefined;
|
|
2923
|
+
const measure = () => setSize({ w: element.clientWidth, h: element.clientHeight });
|
|
2924
|
+
measure();
|
|
2925
|
+
if (typeof ResizeObserver === 'undefined') {
|
|
2926
|
+
window.addEventListener('resize', measure);
|
|
2927
|
+
return () => window.removeEventListener('resize', measure);
|
|
2928
|
+
}
|
|
2929
|
+
const observer = new ResizeObserver(measure);
|
|
2930
|
+
observer.observe(element);
|
|
2931
|
+
return () => observer.disconnect();
|
|
2932
|
+
}, []);
|
|
2933
|
+
|
|
2934
|
+
const snapshot = store.getSnapshot();
|
|
2935
|
+
const reading = snapshot.readingId ? store.getNode(snapshot.readingId) : undefined;
|
|
2936
|
+
const readerNode = useRef(null);
|
|
2937
|
+
if (reading) readerNode.current = reading;
|
|
2938
|
+
|
|
2939
|
+
return h('div', {
|
|
2940
|
+
ref: hostRef,
|
|
2941
|
+
className: 'dsh-tree-root',
|
|
2942
|
+
style: {
|
|
2943
|
+
position: 'relative', width: '100%', height: '100%', minHeight: 420,
|
|
2944
|
+
overflow: 'hidden', fontFamily: FONT, color: C.ink, background: C.canvas,
|
|
2945
|
+
},
|
|
2946
|
+
},
|
|
2947
|
+
h(Boundary, null, h(Canvas, { key: rootId, store, api, rootId, size })),
|
|
2948
|
+
h(Reader, { key: rootId, node: reading || readerNode.current, open: Boolean(reading), onClose: () => store.closeReader() }));
|
|
2949
|
+
}
|
|
2950
|
+
|
|
2951
|
+
// ------------------------------------------------------------------ mount
|
|
2952
|
+
function ensureStyles() {
|
|
2953
|
+
if (typeof document === 'undefined' || !document.head) return;
|
|
2954
|
+
// Always rewrite the sheet. A hot-reloaded module re-runs apply() while the
|
|
2955
|
+
// previous <style> node is still in the document; skipping on "already
|
|
2956
|
+
// present" would freeze the first version's rules forever.
|
|
2957
|
+
let style = document.getElementById('dsh-tree-styles');
|
|
2958
|
+
if (!style) {
|
|
2959
|
+
style = document.createElement('style');
|
|
2960
|
+
style.id = 'dsh-tree-styles';
|
|
2961
|
+
document.head.appendChild(style);
|
|
2962
|
+
}
|
|
2963
|
+
style.textContent = [
|
|
2964
|
+
'@keyframes dshTreeSpin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }',
|
|
2965
|
+
'@keyframes dshTreePulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }',
|
|
2966
|
+
'.dsh-tree-root { --tree-fast: 120ms; --tree-enter: 240ms; --tree-exit: ' + MOTION_EXIT_MS + 'ms; --tree-ease: cubic-bezier(.2,.8,.2,1); --tree-spring: cubic-bezier(.16,1.3,.3,1); }',
|
|
2967
|
+
'@keyframes dshTreeReveal { from { opacity: 0; transform: translateY(6px) scale(.985); } to { opacity: 1; transform: none; } }',
|
|
2968
|
+
'@keyframes dshTreeDismiss { from { opacity: 1; transform: none; } to { opacity: 0; transform: translateY(3px) scale(.99); } }',
|
|
2969
|
+
'@keyframes dshTreeFadeIn { from { opacity: 0; } to { opacity: 1; } }',
|
|
2970
|
+
'@keyframes dshTreeFadeOut { from { opacity: 1; } to { opacity: 0; } }',
|
|
2971
|
+
'.dsh-tree-card { transition: opacity var(--tree-enter) var(--tree-ease), box-shadow var(--tree-enter) var(--tree-ease), border-color var(--tree-fast) ease; }',
|
|
2972
|
+
'.dsh-tree-card--enter { animation: dshTreeReveal var(--tree-enter) var(--tree-ease); }',
|
|
2973
|
+
'.dsh-tree-presence[data-open="true"] { animation: dshTreeReveal var(--tree-enter) var(--tree-ease); }',
|
|
2974
|
+
'.dsh-tree-presence[data-open="false"] { pointer-events: none; animation: dshTreeDismiss var(--tree-exit) ease-in forwards; }',
|
|
2975
|
+
'.dsh-tree-menu { transform-origin: top right; }',
|
|
2976
|
+
'.dsh-tree-menu[data-open="true"] { animation-timing-function: var(--tree-spring); }',
|
|
2977
|
+
'.dsh-tree-composer { transform-origin: top center; }',
|
|
2978
|
+
'.dsh-tree-reader[data-open="true"] { animation: dshTreeFadeIn 180ms ease-out; }',
|
|
2979
|
+
'.dsh-tree-reader[data-open="false"] { animation: dshTreeFadeOut var(--tree-exit) ease-in forwards; }',
|
|
2980
|
+
'.dsh-tree-reader[data-open="true"] .dsh-tree-reader-panel { animation: dshTreeReveal var(--tree-enter) var(--tree-ease); }',
|
|
2981
|
+
'.dsh-tree-reader[data-open="false"] .dsh-tree-reader-panel { animation: dshTreeDismiss var(--tree-exit) ease-in forwards; }',
|
|
2982
|
+
// Every control shares one geometry so the toolbar reads as one row.
|
|
2983
|
+
'.dsh-tree-btn {',
|
|
2984
|
+
' border: 1px solid var(--dsw-alias-border-l1); background: var(--dsw-alias-bg-layer-1);',
|
|
2985
|
+
' color: var(--dsw-alias-label-secondary); border-radius: 8px;',
|
|
2986
|
+
' font-family: inherit; font-size: 12px; line-height: 20px;',
|
|
2987
|
+
' min-height: 32px; min-width: 32px; padding: 5px 10px;',
|
|
2988
|
+
' display: inline-flex; align-items: center; justify-content: center; gap: 6px;',
|
|
2989
|
+
' white-space: nowrap; flex-shrink: 0; box-sizing: border-box; cursor: pointer;',
|
|
2990
|
+
' transition: background var(--tree-fast) ease, border-color var(--tree-fast) ease, color var(--tree-fast) ease, transform 180ms var(--tree-spring);',
|
|
2991
|
+
'}',
|
|
2992
|
+
'.dsh-tree-btn:hover:not([disabled]) {',
|
|
2993
|
+
' background: color-mix(in srgb, var(--dsw-alias-label-primary) 6%, transparent);',
|
|
2994
|
+
' border-color: var(--dsw-alias-border-l2); color: var(--dsw-alias-label-primary);',
|
|
2995
|
+
'}',
|
|
2996
|
+
'.dsh-tree-btn:active:not([disabled]) {',
|
|
2997
|
+
' transform: scale(.96); transition-duration: 70ms;',
|
|
2998
|
+
' background: color-mix(in srgb, var(--dsw-alias-label-primary) 10%, transparent);',
|
|
2999
|
+
'}',
|
|
3000
|
+
'.dsh-tree-btn:focus-visible {',
|
|
3001
|
+
' outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 1px;',
|
|
3002
|
+
'}',
|
|
3003
|
+
'.dsh-tree-btn[disabled] { opacity: 0.45; cursor: default; }',
|
|
3004
|
+
'.dsh-tree-btn--icon { padding: 0; min-width: 30px; min-height: 30px; }',
|
|
3005
|
+
'.dsh-tree-btn--primary {',
|
|
3006
|
+
' background: var(--dsw-alias-button-primary-fill); border-color: var(--dsw-alias-button-primary-fill);',
|
|
3007
|
+
' color: var(--dsw-alias-label-primary-foreground);',
|
|
3008
|
+
'}',
|
|
3009
|
+
'.dsh-tree-btn--primary:hover:not([disabled]) {',
|
|
3010
|
+
' background: var(--dsw-alias-button-primary-hover);',
|
|
3011
|
+
' border-color: var(--dsw-alias-button-primary-hover);',
|
|
3012
|
+
' color: var(--dsw-alias-label-primary-foreground);',
|
|
3013
|
+
'}',
|
|
3014
|
+
'.dsh-tree-btn--sm { min-height: 24px; min-width: 24px; padding: 2px 8px; border-radius: 7px; font-size: 11.5px; }',
|
|
3015
|
+
'.dsh-tree-btn--danger { color: var(--dsw-alias-state-error-primary); }',
|
|
3016
|
+
'.dsh-tree-btn--danger:hover:not([disabled]) {',
|
|
3017
|
+
' color: var(--dsw-alias-state-error-primary);',
|
|
3018
|
+
' border-color: color-mix(in srgb, var(--dsw-alias-state-error-primary) 40%, transparent);',
|
|
3019
|
+
' background: color-mix(in srgb, var(--dsw-alias-state-error-primary) 8%, transparent);',
|
|
3020
|
+
'}',
|
|
3021
|
+
'.dsh-tree-chip {',
|
|
3022
|
+
' border: 1px solid color-mix(in srgb, var(--dsw-alias-brand-primary) 32%, transparent);',
|
|
3023
|
+
' background: color-mix(in srgb, var(--dsw-alias-brand-primary) 8%, transparent);',
|
|
3024
|
+
' color: var(--dsw-alias-brand-primary); border-radius: 999px; font-family: inherit;',
|
|
3025
|
+
' font-size: 11px; line-height: 18px; padding: 2px 10px; cursor: pointer; white-space: nowrap;',
|
|
3026
|
+
// The chevron is a block-level <svg>; without an inline-flex box it takes
|
|
3027
|
+
// its own line and doubles the pill's height.
|
|
3028
|
+
' display: inline-flex; align-items: center; gap: 4px; box-sizing: border-box;',
|
|
3029
|
+
'}',
|
|
3030
|
+
'.dsh-tree-chip:hover {',
|
|
3031
|
+
' background: color-mix(in srgb, var(--dsw-alias-brand-primary) 14%, transparent);',
|
|
3032
|
+
'}',
|
|
3033
|
+
'.dsh-tree-link {',
|
|
3034
|
+
' border: none; background: transparent; font-family: inherit; font-size: 11px;',
|
|
3035
|
+
' line-height: 16px; padding: 2px 0; cursor: pointer; white-space: nowrap;',
|
|
3036
|
+
' display: inline-flex; align-items: center; gap: 4px; color: var(--dsw-alias-label-secondary);',
|
|
3037
|
+
' border-radius: 4px;',
|
|
3038
|
+
'}',
|
|
3039
|
+
'.dsh-tree-link:hover { color: var(--dsw-alias-brand-primary); }',
|
|
3040
|
+
'.dsh-tree-link:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 1px; }',
|
|
3041
|
+
'.dsh-tree-ask svg { transition: transform var(--tree-enter) var(--tree-spring); }',
|
|
3042
|
+
'.dsh-tree-ask[aria-expanded="true"] svg { transform: rotate(45deg); }',
|
|
3043
|
+
'.dsh-tree-chip, .dsh-tree-link { transition: background var(--tree-fast) ease, color var(--tree-fast) ease, transform 180ms var(--tree-spring); }',
|
|
3044
|
+
'.dsh-tree-chip:active, .dsh-tree-link:active { transform: scale(.97); transition-duration: 70ms; }',
|
|
3045
|
+
'.dsh-tree-chip:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 2px; }',
|
|
3046
|
+
'.dsh-tree-link svg, .dsh-tree-chip svg { transition: transform var(--tree-enter) var(--tree-ease); }',
|
|
3047
|
+
'.dsh-tree-textarea { transition: border-color var(--tree-fast) ease, box-shadow var(--tree-fast) ease; }',
|
|
3048
|
+
'.dsh-tree-textarea:focus { border-color: var(--dsw-alias-brand-primary) !important; box-shadow: 0 0 0 3px color-mix(in srgb, var(--dsw-alias-brand-primary) 8%, transparent); }',
|
|
3049
|
+
'@media (hover: hover) { .dsh-tree-btn:hover:not([disabled]):not(:active) { transform: translateY(-1px); } }',
|
|
3050
|
+
'@media (prefers-reduced-motion: reduce) {',
|
|
3051
|
+
' .dsh-tree-root *, .dsh-tree-root *::before, .dsh-tree-root *::after { animation: none !important; transition: none !important; }',
|
|
3052
|
+
' .dsh-tree-root .dsh-tree-btn, .dsh-tree-root .dsh-tree-chip, .dsh-tree-root .dsh-tree-link { transform: none !important; }',
|
|
3053
|
+
' .dsh-tree-presence[data-open="false"] { display: none; }',
|
|
3054
|
+
'}',
|
|
3055
|
+
].join('\n');
|
|
3056
|
+
document.head.appendChild(style);
|
|
3057
|
+
}
|
|
3058
|
+
|
|
3059
|
+
return {
|
|
3060
|
+
inject: ['slots', 'sessions', 'locale'],
|
|
3061
|
+
apply(ctx) {
|
|
3062
|
+
ensureStyles();
|
|
3063
|
+
const runtime = createRuntime(ctx);
|
|
3064
|
+
|
|
3065
|
+
// The locale service owns the visible language. Registering the
|
|
3066
|
+
// dictionary under our own namespace keeps the plugin's copy out of
|
|
3067
|
+
// the shared "common" namespace, and the service's fallback chain
|
|
3068
|
+
// serves English to any language we did not translate.
|
|
3069
|
+
try {
|
|
3070
|
+
ctx.effect(() => ctx.locale.register(NS, {
|
|
3071
|
+
zh: DICT.zh,
|
|
3072
|
+
en: DICT.en,
|
|
3073
|
+
}), 'dsh-explore: locale dictionaries');
|
|
3074
|
+
} catch (error) {
|
|
3075
|
+
// A profile without the locale plugin still gets the built-in table.
|
|
3076
|
+
}
|
|
3077
|
+
|
|
3078
|
+
const bind = () => {
|
|
3079
|
+
try {
|
|
3080
|
+
const translate = ctx.locale.bind(NS);
|
|
3081
|
+
localeBridge.translate = (key, params) => translate(key, params);
|
|
3082
|
+
} catch (error) {
|
|
3083
|
+
localeBridge.translate = fallbackTranslate;
|
|
3084
|
+
}
|
|
3085
|
+
};
|
|
3086
|
+
bind();
|
|
3087
|
+
try {
|
|
3088
|
+
ctx.effect(() => ctx.locale.subscribe(() => {
|
|
3089
|
+
bind();
|
|
3090
|
+
for (const listener of [...localeListeners]) {
|
|
3091
|
+
try { listener(); } catch (error) { /* a listener must not break the rest */ }
|
|
3092
|
+
}
|
|
3093
|
+
}), 'dsh-explore: locale subscription');
|
|
3094
|
+
} catch (error) {
|
|
3095
|
+
/* keep the fallback translator */
|
|
3096
|
+
}
|
|
3097
|
+
|
|
3098
|
+
ctx.slots.inject('conversation.view', () => ctx.slots.register({
|
|
3099
|
+
name: 'conversation.view',
|
|
3100
|
+
id: 'tree',
|
|
3101
|
+
order: 5,
|
|
3102
|
+
// A thunk is re-read on every projection, so the tab follows the
|
|
3103
|
+
// active language instead of freezing one string at mount time.
|
|
3104
|
+
label: () => T('view.tab'),
|
|
3105
|
+
inject: () => ({ api: {
|
|
3106
|
+
rootOf: runtime.rootOf,
|
|
3107
|
+
storeFor: runtime.storeFor,
|
|
3108
|
+
ensureProjection: runtime.ensureProjection,
|
|
3109
|
+
ensureOpen: runtime.ensureOpen,
|
|
3110
|
+
branch: runtime.branch,
|
|
3111
|
+
loadOlder: runtime.loadOlder,
|
|
3112
|
+
deleteSubtree: runtime.deleteSubtree,
|
|
3113
|
+
restoreHidden: runtime.restoreHidden,
|
|
3114
|
+
} }),
|
|
3115
|
+
}, TreeView));
|
|
3116
|
+
},
|
|
3117
|
+
};
|
|
3118
|
+
},
|
|
3119
|
+
});
|