dsh-architecture-map 0.0.0-stage → 0.2.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/CHANGELOG.md +31 -0
- package/CODE_OF_CONDUCT.md +7 -0
- package/CONTRIBUTING.md +24 -0
- package/LICENSE +21 -0
- package/README.md +264 -2
- package/README.zh.md +247 -0
- package/THIRD_PARTY_NOTICES.md +36 -0
- package/client.js +1171 -0
- package/cordis.patch.yml +5 -0
- package/docs/DETAIL.md +76 -0
- package/docs/DETAIL.zh.md +76 -0
- package/docs/preview.svg +50 -0
- package/icon.svg +9 -0
- package/index.js +1 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +68 -4
- package/skills/architecture-map/SKILL.md +75 -0
- package/skills/architecture-map/references/bootstrap.md +100 -0
- package/skills/architecture-map/references/discussion.md +71 -0
- package/skills/architecture-map/references/incremental.md +80 -0
- package/skills/architecture-map/references/schema.md +94 -0
- package/src/README.md +34 -0
- package/src/host/content/index.js +20 -0
- package/src/host/content/local.js +250 -0
- package/src/host/export.js +228 -0
- package/src/host/graph.js +707 -0
- package/src/host/memory.js +364 -0
- package/src/host/plugin.js +1125 -0
- package/src/host/projects.js +241 -0
- package/src/host/remote/commands.js +227 -0
- package/src/host/remote/consent.js +216 -0
- package/src/host/remote/content.js +391 -0
- package/src/host/remote/indexer.js +180 -0
- package/src/host/remote/maintainer.js +85 -0
- package/src/host/remote/ssh.js +287 -0
- package/src/host/remote/state.js +65 -0
- package/src/host/remote/tools.js +162 -0
- package/src/host/signature.js +42 -0
- package/src/host/workspace.js +131 -0
- package/vendor/elkjs.LICENSE +264 -0
- package/vendor/mermaid.LICENSE +21 -0
- package/vendor/mermaid.esm.min.mjs +4343 -0
package/client.js
ADDED
|
@@ -0,0 +1,1171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Architecture map — Client half.
|
|
3
|
+
*
|
|
4
|
+
* Node 1: right-sidebar tab + /arch + auto-open.
|
|
5
|
+
* Node 3: the tab reads `<session workspace>/.archmap/graph.json` through the
|
|
6
|
+
* workspaceFiles Remote and renders a small, dependency-free SVG map with
|
|
7
|
+
* node details. React Flow comes later.
|
|
8
|
+
*/
|
|
9
|
+
window.__ModuleLoader__.load({
|
|
10
|
+
id: 'dsh-architecture-map',
|
|
11
|
+
factory(require) {
|
|
12
|
+
const React = require('react')
|
|
13
|
+
const h = React.createElement
|
|
14
|
+
|
|
15
|
+
const TYPE_ID = 'dsh-architecture-map/graph'
|
|
16
|
+
const GRAPH_PATH = '.archmap/graph.json'
|
|
17
|
+
const INDEX_PATH = '.archmap/index.json'
|
|
18
|
+
const POLL_MS = 2000
|
|
19
|
+
const POSITIONS_KEY = 'dsh-architecture-map:positions:'
|
|
20
|
+
const SOURCE_KEY = 'dsh-architecture-map:source:'
|
|
21
|
+
const MERMAID_CONFIG_URL = '/x-dsh-architecture-map/config'
|
|
22
|
+
const LOCALE_NS = 'architecture-map'
|
|
23
|
+
const EN = {
|
|
24
|
+
'panel.title': 'Architecture map',
|
|
25
|
+
'panel.refresh': 'Refresh',
|
|
26
|
+
'panel.fit': 'Fit',
|
|
27
|
+
'panel.reset': 'Reset layout',
|
|
28
|
+
'panel.hint': 'drag to pan · wheel to zoom',
|
|
29
|
+
'panel.empty': 'Click a node to see its summary, anchors and edges.',
|
|
30
|
+
'panel.error': 'Graph unavailable',
|
|
31
|
+
'panel.noGraphRemote': 'No graph yet for this remote project.',
|
|
32
|
+
'panel.remoteHint': 'Ask the agent to create one with arch_graph_patch (workspace: remote:<id>).',
|
|
33
|
+
'panel.indexError': 'Project index unavailable; showing the local graph.',
|
|
34
|
+
'panel.noGraph': 'no graph',
|
|
35
|
+
'source.label': 'Source',
|
|
36
|
+
'source.local': 'Local',
|
|
37
|
+
'source.consent': 'consent',
|
|
38
|
+
'legend.fresh': 'fresh',
|
|
39
|
+
'legend.stale': 'stale',
|
|
40
|
+
'legend.proposed': 'proposed',
|
|
41
|
+
'discuss.fork': 'Discuss (fork)',
|
|
42
|
+
'discuss.lite': 'Lite',
|
|
43
|
+
'discuss.starting': 'Starting…',
|
|
44
|
+
'discuss.hint': 'Tool schemas may be inherited, but shell and source-write calls are denied for discussion children; graph changes still need your approval.',
|
|
45
|
+
'detail.kind': 'kind',
|
|
46
|
+
'detail.group': 'group',
|
|
47
|
+
'detail.status': 'status',
|
|
48
|
+
'guide.title': 'Architecture map',
|
|
49
|
+
'guide.description': 'Open the live architecture graph panel',
|
|
50
|
+
}
|
|
51
|
+
const ZH = {
|
|
52
|
+
'panel.title': '架构地图',
|
|
53
|
+
'panel.refresh': '刷新',
|
|
54
|
+
'panel.fit': '适配',
|
|
55
|
+
'panel.reset': '重置布局',
|
|
56
|
+
'panel.hint': '拖动平移 · 滚轮缩放',
|
|
57
|
+
'panel.empty': '点击节点查看摘要、锚点和边。',
|
|
58
|
+
'panel.error': '图不可用',
|
|
59
|
+
'panel.noGraphRemote': '这个远程项目还没有图。',
|
|
60
|
+
'panel.remoteHint': '让 agent 用 arch_graph_patch(workspace: remote:<id>)创建一张。',
|
|
61
|
+
'panel.indexError': '项目索引不可用,已回退到本地图。',
|
|
62
|
+
'panel.noGraph': '暂无图',
|
|
63
|
+
'source.label': '来源',
|
|
64
|
+
'source.local': '本地',
|
|
65
|
+
'source.consent': '授权',
|
|
66
|
+
'legend.fresh': '有效',
|
|
67
|
+
'legend.stale': '过期',
|
|
68
|
+
'legend.proposed': '提案',
|
|
69
|
+
'discuss.fork': '讨论(fork)',
|
|
70
|
+
'discuss.lite': '轻讨论',
|
|
71
|
+
'discuss.starting': '启动中…',
|
|
72
|
+
'discuss.hint': '子会话可能继承工具 schema,但 shell 和源码写调用会被拒绝;改图仍需你确认。',
|
|
73
|
+
'detail.kind': '类型',
|
|
74
|
+
'detail.group': '分组',
|
|
75
|
+
'detail.status': '状态',
|
|
76
|
+
'guide.title': '架构地图',
|
|
77
|
+
'guide.description': '打开实时架构图面板',
|
|
78
|
+
}
|
|
79
|
+
let t = key => EN[key] ?? key
|
|
80
|
+
const MERMAID_RUNTIME_URL = '/x-dsh-architecture-map/vendor/mermaid.esm.min.mjs'
|
|
81
|
+
|
|
82
|
+
const styles = {
|
|
83
|
+
root: {
|
|
84
|
+
height: '100%',
|
|
85
|
+
boxSizing: 'border-box',
|
|
86
|
+
padding: '16px',
|
|
87
|
+
fontFamily: 'inherit',
|
|
88
|
+
color: 'var(--dsw-alias-text-primary, inherit)',
|
|
89
|
+
background: 'var(--dsw-alias-bg-primary, transparent)',
|
|
90
|
+
overflow: 'auto',
|
|
91
|
+
},
|
|
92
|
+
header: {
|
|
93
|
+
display: 'flex',
|
|
94
|
+
alignItems: 'baseline',
|
|
95
|
+
justifyContent: 'space-between',
|
|
96
|
+
gap: '12px',
|
|
97
|
+
marginBottom: '12px',
|
|
98
|
+
},
|
|
99
|
+
title: {
|
|
100
|
+
fontSize: '15px',
|
|
101
|
+
fontWeight: 600,
|
|
102
|
+
margin: 0,
|
|
103
|
+
},
|
|
104
|
+
meta: {
|
|
105
|
+
fontSize: '12px',
|
|
106
|
+
opacity: 0.68,
|
|
107
|
+
},
|
|
108
|
+
button: {
|
|
109
|
+
border: '1px solid var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))',
|
|
110
|
+
background: 'transparent',
|
|
111
|
+
color: 'inherit',
|
|
112
|
+
borderRadius: '6px',
|
|
113
|
+
padding: '3px 8px',
|
|
114
|
+
fontSize: '12px',
|
|
115
|
+
cursor: 'pointer',
|
|
116
|
+
},
|
|
117
|
+
select: {
|
|
118
|
+
border: '1px solid var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))',
|
|
119
|
+
background: 'var(--dsw-alias-bg-secondary, rgba(127,127,127,0.08))',
|
|
120
|
+
color: 'inherit',
|
|
121
|
+
borderRadius: '6px',
|
|
122
|
+
padding: '3px 6px',
|
|
123
|
+
fontSize: '12px',
|
|
124
|
+
maxWidth: '220px',
|
|
125
|
+
},
|
|
126
|
+
sourceBar: {
|
|
127
|
+
fontSize: '11px',
|
|
128
|
+
opacity: 0.7,
|
|
129
|
+
margin: '-4px 0 10px',
|
|
130
|
+
overflow: 'hidden',
|
|
131
|
+
textOverflow: 'ellipsis',
|
|
132
|
+
whiteSpace: 'nowrap',
|
|
133
|
+
},
|
|
134
|
+
empty: {
|
|
135
|
+
border: '1px dashed var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))',
|
|
136
|
+
borderRadius: '10px',
|
|
137
|
+
padding: '20px',
|
|
138
|
+
fontSize: '13px',
|
|
139
|
+
lineHeight: 1.6,
|
|
140
|
+
opacity: 0.8,
|
|
141
|
+
},
|
|
142
|
+
error: {
|
|
143
|
+
border: '1px solid var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))',
|
|
144
|
+
borderRadius: '10px',
|
|
145
|
+
padding: '12px 14px',
|
|
146
|
+
fontSize: '12px',
|
|
147
|
+
lineHeight: 1.5,
|
|
148
|
+
marginBottom: '12px',
|
|
149
|
+
opacity: 0.85,
|
|
150
|
+
},
|
|
151
|
+
toolbar: {
|
|
152
|
+
display: 'flex',
|
|
153
|
+
alignItems: 'center',
|
|
154
|
+
gap: '8px',
|
|
155
|
+
marginBottom: '8px',
|
|
156
|
+
},
|
|
157
|
+
canvas: {
|
|
158
|
+
width: '100%',
|
|
159
|
+
height: 'auto',
|
|
160
|
+
display: 'block',
|
|
161
|
+
overflow: 'visible',
|
|
162
|
+
touchAction: 'none',
|
|
163
|
+
cursor: 'grab',
|
|
164
|
+
userSelect: 'none',
|
|
165
|
+
},
|
|
166
|
+
detail: {
|
|
167
|
+
marginTop: '14px',
|
|
168
|
+
border: '1px solid var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))',
|
|
169
|
+
borderRadius: '10px',
|
|
170
|
+
padding: '12px 14px',
|
|
171
|
+
fontSize: '12px',
|
|
172
|
+
lineHeight: 1.6,
|
|
173
|
+
},
|
|
174
|
+
detailTitle: {
|
|
175
|
+
fontSize: '13px',
|
|
176
|
+
fontWeight: 600,
|
|
177
|
+
margin: '0 0 6px',
|
|
178
|
+
},
|
|
179
|
+
detailRow: {
|
|
180
|
+
margin: '2px 0',
|
|
181
|
+
opacity: 0.85,
|
|
182
|
+
},
|
|
183
|
+
mono: {
|
|
184
|
+
fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace',
|
|
185
|
+
fontSize: '11px',
|
|
186
|
+
},
|
|
187
|
+
discussRow: {
|
|
188
|
+
display: 'flex',
|
|
189
|
+
gap: '8px',
|
|
190
|
+
marginTop: '10px',
|
|
191
|
+
flexWrap: 'wrap',
|
|
192
|
+
},
|
|
193
|
+
discussError: {
|
|
194
|
+
marginTop: '8px',
|
|
195
|
+
fontSize: '12px',
|
|
196
|
+
lineHeight: 1.5,
|
|
197
|
+
color: 'var(--dsw-alias-danger, #d9534f)',
|
|
198
|
+
},
|
|
199
|
+
discussHint: {
|
|
200
|
+
marginTop: '6px',
|
|
201
|
+
fontSize: '11px',
|
|
202
|
+
lineHeight: 1.4,
|
|
203
|
+
opacity: 0.6,
|
|
204
|
+
},
|
|
205
|
+
mermaid: {
|
|
206
|
+
overflow: 'auto',
|
|
207
|
+
maxWidth: '100%',
|
|
208
|
+
border: '1px solid var(--dsw-alias-border-secondary, rgba(127,127,127,0.25))',
|
|
209
|
+
borderRadius: '8px',
|
|
210
|
+
padding: '8px',
|
|
211
|
+
background: 'var(--dsw-alias-bg-primary, transparent)',
|
|
212
|
+
},
|
|
213
|
+
mermaidError: {
|
|
214
|
+
marginTop: '6px',
|
|
215
|
+
fontSize: '12px',
|
|
216
|
+
lineHeight: 1.5,
|
|
217
|
+
opacity: 0.72,
|
|
218
|
+
color: 'var(--dsw-alias-warning, #c58a1a)',
|
|
219
|
+
},
|
|
220
|
+
legend: {
|
|
221
|
+
display: 'flex',
|
|
222
|
+
alignItems: 'center',
|
|
223
|
+
gap: '12px',
|
|
224
|
+
margin: '0 0 8px',
|
|
225
|
+
fontSize: '11px',
|
|
226
|
+
opacity: 0.72,
|
|
227
|
+
flexWrap: 'wrap',
|
|
228
|
+
},
|
|
229
|
+
legendItem: {
|
|
230
|
+
display: 'inline-flex',
|
|
231
|
+
alignItems: 'center',
|
|
232
|
+
gap: '4px',
|
|
233
|
+
},
|
|
234
|
+
legendDot: {
|
|
235
|
+
width: '7px',
|
|
236
|
+
height: '7px',
|
|
237
|
+
borderRadius: '50%',
|
|
238
|
+
display: 'inline-block',
|
|
239
|
+
},
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function clip(text, max) {
|
|
243
|
+
const value = String(text ?? '')
|
|
244
|
+
return value.length <= max ? value : `${value.slice(0, max - 1)}…`
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
let mermaidRuntime
|
|
248
|
+
let mermaidUid = 0
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Load the vendored Mermaid ESM once. The Host's /config route is the
|
|
252
|
+
* renderMermaid switch: when it is false, fences stay plain code blocks and
|
|
253
|
+
* the 5 MB runtime is never fetched.
|
|
254
|
+
*/
|
|
255
|
+
function loadMermaidRuntime() {
|
|
256
|
+
if (mermaidRuntime) return mermaidRuntime
|
|
257
|
+
const dynamicImport = typeof globalThis.__dshMermaidImport === 'function'
|
|
258
|
+
? globalThis.__dshMermaidImport
|
|
259
|
+
: specifier => import(/* @vite-ignore */ specifier)
|
|
260
|
+
mermaidRuntime = fetch(MERMAID_CONFIG_URL, { cache: 'no-store' })
|
|
261
|
+
.then(response => {
|
|
262
|
+
if (!response.ok) throw new Error(`Mermaid config unavailable (${response.status})`)
|
|
263
|
+
return response.json()
|
|
264
|
+
})
|
|
265
|
+
.then(config => {
|
|
266
|
+
if (config?.mermaid === false) throw new Error('Mermaid rendering is disabled')
|
|
267
|
+
return dynamicImport(new URL(MERMAID_RUNTIME_URL, document.baseURI).href)
|
|
268
|
+
})
|
|
269
|
+
.then(module => {
|
|
270
|
+
const api = module.default ?? module
|
|
271
|
+
const dark = window.matchMedia?.('(prefers-color-scheme: dark)')?.matches === true
|
|
272
|
+
api.initialize({
|
|
273
|
+
startOnLoad: false,
|
|
274
|
+
securityLevel: 'strict',
|
|
275
|
+
suppressErrorRendering: true,
|
|
276
|
+
theme: dark ? 'dark' : 'default',
|
|
277
|
+
fontFamily: 'inherit',
|
|
278
|
+
flowchart: { htmlLabels: true, curve: 'basis' },
|
|
279
|
+
})
|
|
280
|
+
return api
|
|
281
|
+
})
|
|
282
|
+
.catch(error => {
|
|
283
|
+
console.warn('[architecture-map] Mermaid runtime unavailable:', error)
|
|
284
|
+
throw error
|
|
285
|
+
})
|
|
286
|
+
return mermaidRuntime
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** Keep a Mermaid render failure inside the fence instead of the whole page. */
|
|
290
|
+
class MermaidErrorBoundary extends React.Component {
|
|
291
|
+
constructor(props) {
|
|
292
|
+
super(props)
|
|
293
|
+
this.state = { failed: false }
|
|
294
|
+
}
|
|
295
|
+
static getDerivedStateFromError() {
|
|
296
|
+
return { failed: true }
|
|
297
|
+
}
|
|
298
|
+
componentDidCatch(error) {
|
|
299
|
+
console.warn('[architecture-map] Mermaid fence failed:', error)
|
|
300
|
+
}
|
|
301
|
+
render() {
|
|
302
|
+
return this.state.failed ? this.props.fallback : this.props.children
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** Chat fence renderer for ```mermaid blocks. Falls back to the code block. */
|
|
307
|
+
function MermaidInner(props) {
|
|
308
|
+
const code = typeof props.code === 'string' ? props.code : ''
|
|
309
|
+
const streaming = props.streaming === true
|
|
310
|
+
const fallback = props.children ?? null
|
|
311
|
+
const [result, setResult] = React.useState({ status: 'idle', svg: null, error: null, renderError: false })
|
|
312
|
+
|
|
313
|
+
React.useEffect(() => {
|
|
314
|
+
if (streaming || code.trim() === '') return undefined
|
|
315
|
+
let disposed = false
|
|
316
|
+
setResult(previous => ({ ...previous, status: 'loading', error: null, renderError: false }))
|
|
317
|
+
const id = `archmap-mermaid-${++mermaidUid}`
|
|
318
|
+
loadMermaidRuntime()
|
|
319
|
+
.then(api => api.render(id, code))
|
|
320
|
+
.then(rendered => {
|
|
321
|
+
if (!disposed) setResult({ status: 'ready', svg: rendered.svg, error: null, renderError: false })
|
|
322
|
+
})
|
|
323
|
+
.catch(error => {
|
|
324
|
+
if (disposed) return
|
|
325
|
+
const message = String(error?.message ?? error)
|
|
326
|
+
const renderError = !/disabled/i.test(message)
|
|
327
|
+
setResult({ status: 'error', svg: null, error: message, renderError })
|
|
328
|
+
})
|
|
329
|
+
return () => { disposed = true }
|
|
330
|
+
}, [code, streaming])
|
|
331
|
+
|
|
332
|
+
if (streaming || result.status !== 'ready') {
|
|
333
|
+
return h(React.Fragment, null,
|
|
334
|
+
fallback,
|
|
335
|
+
result.renderError && result.error
|
|
336
|
+
? h('div', { style: styles.mermaidError }, `Mermaid: ${result.error}`)
|
|
337
|
+
: null,
|
|
338
|
+
)
|
|
339
|
+
}
|
|
340
|
+
return h('div', { style: styles.mermaid, dangerouslySetInnerHTML: { __html: result.svg } })
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function MermaidFence(props) {
|
|
344
|
+
return h(MermaidErrorBoundary, { fallback: props.children ?? null }, h(MermaidInner, props))
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function readSavedPositions(key) {
|
|
348
|
+
if (!key) return {}
|
|
349
|
+
try {
|
|
350
|
+
const raw = window.localStorage.getItem(key)
|
|
351
|
+
if (!raw) return {}
|
|
352
|
+
const parsed = JSON.parse(raw)
|
|
353
|
+
if (!parsed || typeof parsed !== 'object') return {}
|
|
354
|
+
const result = {}
|
|
355
|
+
for (const [id, value] of Object.entries(parsed)) {
|
|
356
|
+
if (value && Number.isFinite(value.x) && Number.isFinite(value.y)) result[id] = { x: value.x, y: value.y }
|
|
357
|
+
}
|
|
358
|
+
return result
|
|
359
|
+
} catch {
|
|
360
|
+
return {}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function writeSavedPositions(key, value) {
|
|
365
|
+
if (!key) return
|
|
366
|
+
try {
|
|
367
|
+
window.localStorage.setItem(key, JSON.stringify(value))
|
|
368
|
+
} catch {
|
|
369
|
+
// Local storage can be unavailable in private contexts; positions are a view preference.
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function clearSavedPositions(key) {
|
|
374
|
+
if (!key) return
|
|
375
|
+
try {
|
|
376
|
+
window.localStorage.removeItem(key)
|
|
377
|
+
} catch {
|
|
378
|
+
// Ignore: the in-memory layout still resets.
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function localSource() {
|
|
383
|
+
return { id: 'local', kind: 'local', label: t('source.local'), graphPath: GRAPH_PATH, meta: '' }
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function sourcesFromIndex(index) {
|
|
387
|
+
const projects = Array.isArray(index?.projects) ? index.projects : []
|
|
388
|
+
const sources = []
|
|
389
|
+
for (const project of projects) {
|
|
390
|
+
if (!project || typeof project.id !== 'string') continue
|
|
391
|
+
if (project.kind === 'local') {
|
|
392
|
+
sources.push(localSource())
|
|
393
|
+
continue
|
|
394
|
+
}
|
|
395
|
+
if (!/^[a-z][a-z0-9_-]{0,63}$/.test(project.id)) continue
|
|
396
|
+
const safeDir = typeof project.dir === 'string'
|
|
397
|
+
&& project.dir !== ''
|
|
398
|
+
&& !project.dir.startsWith('/')
|
|
399
|
+
&& !project.dir.split('/').includes('..')
|
|
400
|
+
? project.dir
|
|
401
|
+
: `projects/${project.id}`
|
|
402
|
+
const parts = [
|
|
403
|
+
project.host,
|
|
404
|
+
project.root,
|
|
405
|
+
project.vcs,
|
|
406
|
+
`${t('source.consent')} ${project.consent?.scope ?? 'none'}`,
|
|
407
|
+
].filter(Boolean)
|
|
408
|
+
sources.push({
|
|
409
|
+
id: project.id,
|
|
410
|
+
kind: 'remote',
|
|
411
|
+
label: project.title || project.id,
|
|
412
|
+
graphPath: `.archmap/${safeDir}/graph.json`,
|
|
413
|
+
meta: parts.join(' · '),
|
|
414
|
+
})
|
|
415
|
+
}
|
|
416
|
+
const local = sources.find(source => source.kind === 'local')
|
|
417
|
+
const rest = sources.filter(source => source.kind !== 'local')
|
|
418
|
+
return local ? [local, ...rest] : [localSource(), ...rest]
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function isMissingFile(error) {
|
|
422
|
+
const text = `${error?.code ?? ''} ${error?.message ?? ''}`.toLowerCase()
|
|
423
|
+
return text.includes('enoent')
|
|
424
|
+
|| text.includes('not found')
|
|
425
|
+
|| text.includes('not_found')
|
|
426
|
+
|| text.includes('fs_not_found')
|
|
427
|
+
|| text.includes('no entry at')
|
|
428
|
+
|| text.includes('does not exist')
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function readSavedSource(cwd) {
|
|
432
|
+
if (!cwd) return null
|
|
433
|
+
try {
|
|
434
|
+
return window.localStorage.getItem(SOURCE_KEY + cwd)
|
|
435
|
+
} catch {
|
|
436
|
+
return null
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function writeSavedSource(cwd, id) {
|
|
441
|
+
if (!cwd) return
|
|
442
|
+
try {
|
|
443
|
+
window.localStorage.setItem(SOURCE_KEY + cwd, id)
|
|
444
|
+
} catch {
|
|
445
|
+
// Source choice is a view preference; the in-memory selection still works.
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function gridLayout(nodes) {
|
|
450
|
+
const count = Math.max(nodes.length, 1)
|
|
451
|
+
const cols = Math.min(2, Math.max(1, Math.ceil(Math.sqrt(count))))
|
|
452
|
+
const rows = Math.ceil(nodes.length / cols)
|
|
453
|
+
const cellW = 236
|
|
454
|
+
const cellH = 108
|
|
455
|
+
const pad = 18
|
|
456
|
+
const nodeW = 196
|
|
457
|
+
const nodeH = 64
|
|
458
|
+
const positions = new Map()
|
|
459
|
+
nodes.forEach((node, index) => {
|
|
460
|
+
positions.set(node.id, {
|
|
461
|
+
x: pad + (index % cols) * cellW,
|
|
462
|
+
y: pad + Math.floor(index / cols) * cellH,
|
|
463
|
+
w: nodeW,
|
|
464
|
+
h: nodeH,
|
|
465
|
+
})
|
|
466
|
+
})
|
|
467
|
+
return {
|
|
468
|
+
positions,
|
|
469
|
+
width: cols * cellW + pad,
|
|
470
|
+
height: Math.max(rows * cellH + pad, 96),
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Layered layout: longest-path levels for a DAG. Cyclic graphs fall back to
|
|
476
|
+
* the grid so a bad edge cannot collapse the canvas into one column.
|
|
477
|
+
*/
|
|
478
|
+
function layout(graph) {
|
|
479
|
+
const nodes = Array.isArray(graph?.nodes) ? graph.nodes : []
|
|
480
|
+
if (nodes.length === 0) return { positions: new Map(), width: 0, height: 96 }
|
|
481
|
+
const ids = new Set(nodes.map(node => node.id))
|
|
482
|
+
const edges = (Array.isArray(graph?.edges) ? graph.edges : [])
|
|
483
|
+
.filter(edge => ids.has(edge.from) && ids.has(edge.to))
|
|
484
|
+
if (edges.length === 0) return gridLayout(nodes)
|
|
485
|
+
const indegree = new Map(nodes.map(node => [node.id, 0]))
|
|
486
|
+
const outgoing = new Map(nodes.map(node => [node.id, []]))
|
|
487
|
+
for (const edge of edges) {
|
|
488
|
+
outgoing.get(edge.from).push(edge.to)
|
|
489
|
+
indegree.set(edge.to, indegree.get(edge.to) + 1)
|
|
490
|
+
}
|
|
491
|
+
const queue = nodes.filter(node => indegree.get(node.id) === 0).map(node => node.id)
|
|
492
|
+
const levels = new Map(nodes.map(node => [node.id, 0]))
|
|
493
|
+
let visited = 0
|
|
494
|
+
while (queue.length > 0) {
|
|
495
|
+
const id = queue.shift()
|
|
496
|
+
visited += 1
|
|
497
|
+
for (const to of outgoing.get(id)) {
|
|
498
|
+
levels.set(to, Math.max(levels.get(to), levels.get(id) + 1))
|
|
499
|
+
indegree.set(to, indegree.get(to) - 1)
|
|
500
|
+
if (indegree.get(to) === 0) queue.push(to)
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (visited !== nodes.length) return gridLayout(nodes)
|
|
504
|
+
|
|
505
|
+
const byLevel = new Map()
|
|
506
|
+
for (const node of nodes) {
|
|
507
|
+
const level = levels.get(node.id) ?? 0
|
|
508
|
+
if (!byLevel.has(level)) byLevel.set(level, [])
|
|
509
|
+
byLevel.get(level).push(node)
|
|
510
|
+
}
|
|
511
|
+
const direction = String(graph?.layout?.direction ?? 'LR').toUpperCase()
|
|
512
|
+
const horizontal = direction !== 'TB' && direction !== 'TD' && direction !== 'BT'
|
|
513
|
+
const nodeW = 196
|
|
514
|
+
const nodeH = 64
|
|
515
|
+
const layerGap = 54
|
|
516
|
+
const nodeGap = 26
|
|
517
|
+
const pad = 24
|
|
518
|
+
const layerCount = Math.max(...byLevel.keys()) + 1
|
|
519
|
+
const widest = Math.max(...[...byLevel.values()].map(list => list.length), 1)
|
|
520
|
+
const width = horizontal
|
|
521
|
+
? pad * 2 + layerCount * nodeW + (layerCount - 1) * layerGap
|
|
522
|
+
: pad * 2 + widest * nodeW + (widest - 1) * nodeGap
|
|
523
|
+
const height = horizontal
|
|
524
|
+
? pad * 2 + widest * nodeH + (widest - 1) * nodeGap
|
|
525
|
+
: pad * 2 + layerCount * nodeH + (layerCount - 1) * layerGap
|
|
526
|
+
const positions = new Map()
|
|
527
|
+
for (const [level, list] of byLevel) {
|
|
528
|
+
list.forEach((node, index) => {
|
|
529
|
+
positions.set(node.id, {
|
|
530
|
+
x: horizontal ? pad + level * (nodeW + layerGap) : pad + index * (nodeW + nodeGap),
|
|
531
|
+
y: horizontal ? pad + index * (nodeH + nodeGap) : pad + level * (nodeH + layerGap),
|
|
532
|
+
w: nodeW,
|
|
533
|
+
h: nodeH,
|
|
534
|
+
})
|
|
535
|
+
})
|
|
536
|
+
}
|
|
537
|
+
return { positions, width, height }
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function readAll(ctx, sessionId, filePath, signal) {
|
|
541
|
+
return (async () => {
|
|
542
|
+
let offset = 1
|
|
543
|
+
let text = ''
|
|
544
|
+
for (let page = 0; page < 100; page += 1) {
|
|
545
|
+
const result = await ctx.remote.workspaceFiles.read(sessionId, filePath, { offset }, signal)
|
|
546
|
+
if (!result || result.ok !== true) {
|
|
547
|
+
const reason = result?.error?.message ?? result?.error?.code ?? 'read failed'
|
|
548
|
+
const error = new Error(reason)
|
|
549
|
+
error.code = result?.error?.code
|
|
550
|
+
throw error
|
|
551
|
+
}
|
|
552
|
+
text += (text === '' ? '' : '\n') + result.value.text
|
|
553
|
+
if (result.value.eof) {
|
|
554
|
+
return JSON.parse(text)
|
|
555
|
+
}
|
|
556
|
+
if (!Number.isInteger(result.value.lines) || result.value.lines <= 0) {
|
|
557
|
+
throw new Error(`${filePath} read made no progress`)
|
|
558
|
+
}
|
|
559
|
+
offset += result.value.lines
|
|
560
|
+
}
|
|
561
|
+
throw new Error(`${filePath} is too large to read in one view`)
|
|
562
|
+
})()
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function NodeShape(props) {
|
|
566
|
+
const { node, box, selected, onPointerDown } = props
|
|
567
|
+
const x = box.x
|
|
568
|
+
const y = box.y
|
|
569
|
+
const fill = selected
|
|
570
|
+
? 'var(--dsw-alias-accent-soft, rgba(79,140,255,0.18))'
|
|
571
|
+
: 'var(--dsw-alias-bg-secondary, rgba(127,127,127,0.08))'
|
|
572
|
+
const stroke = selected
|
|
573
|
+
? 'var(--dsw-alias-accent, #4f8cff)'
|
|
574
|
+
: 'var(--dsw-alias-border-secondary, rgba(127,127,127,0.35))'
|
|
575
|
+
return h(
|
|
576
|
+
'g',
|
|
577
|
+
{ onPointerDown: onPointerDown, style: { cursor: 'grab' } },
|
|
578
|
+
h('rect', {
|
|
579
|
+
x, y, width: box.w, height: box.h, rx: 8,
|
|
580
|
+
fill, stroke, strokeWidth: selected ? 2 : 1,
|
|
581
|
+
}),
|
|
582
|
+
h('text', {
|
|
583
|
+
x: x + 12, y: y + 24, fill: 'currentColor', fontSize: 13, fontWeight: 600,
|
|
584
|
+
}, clip(node.title ?? node.id, 22)),
|
|
585
|
+
h('text', {
|
|
586
|
+
x: x + 12, y: y + 44, fill: 'currentColor', fontSize: 11, opacity: 0.65,
|
|
587
|
+
}, `${node.kind ?? 'module'}${node.group ? ` · ${node.group}` : ''}${node.status === 'stale' ? ` · ${t('legend.stale')}` : ''}`),
|
|
588
|
+
h('circle', {
|
|
589
|
+
cx: x + box.w - 14, cy: y + 14, r: 4,
|
|
590
|
+
fill: node.status === 'stale'
|
|
591
|
+
? 'var(--dsw-alias-warning, #d9a441)'
|
|
592
|
+
: node.status === 'proposed'
|
|
593
|
+
? 'var(--dsw-alias-danger, #e5484d)'
|
|
594
|
+
: 'var(--dsw-alias-success, #4caf7d)',
|
|
595
|
+
}),
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
function createGraphTab(ctx) {
|
|
600
|
+
return function GraphTab(props) {
|
|
601
|
+
const sessionId = props?.sessionId
|
|
602
|
+
const cwd = props.useSessions(sessions => sessions.byId[sessionId]?.cwd)
|
|
603
|
+
const [state, setState] = React.useState({ status: 'loading', graph: null, error: null })
|
|
604
|
+
const [selectedId, setSelectedId] = React.useState(null)
|
|
605
|
+
const [discussState, setDiscussState] = React.useState({ status: 'idle', error: null })
|
|
606
|
+
const [tick, setTick] = React.useState(0)
|
|
607
|
+
const [view, setView] = React.useState({ zoom: 1, x: 0, y: 0 })
|
|
608
|
+
const [positions, setPositions] = React.useState({})
|
|
609
|
+
const [sources, setSources] = React.useState([])
|
|
610
|
+
const [sourceId, setSourceId] = React.useState(null)
|
|
611
|
+
const [indexError, setIndexError] = React.useState(null)
|
|
612
|
+
const svgRef = React.useRef(null)
|
|
613
|
+
const panRef = React.useRef(null)
|
|
614
|
+
const fittedRef = React.useRef(false)
|
|
615
|
+
const trackedRef = React.useRef(null)
|
|
616
|
+
const storageKey = cwd ? `${POSITIONS_KEY}${cwd}:${sourceId ?? 'local'}` : undefined
|
|
617
|
+
|
|
618
|
+
React.useEffect(() => {
|
|
619
|
+
setDiscussState({ status: 'idle', error: null })
|
|
620
|
+
}, [selectedId])
|
|
621
|
+
|
|
622
|
+
React.useEffect(() => {
|
|
623
|
+
if (!sessionId) {
|
|
624
|
+
setSources([localSource()])
|
|
625
|
+
setSourceId('local')
|
|
626
|
+
setIndexError(null)
|
|
627
|
+
return undefined
|
|
628
|
+
}
|
|
629
|
+
let disposed = false
|
|
630
|
+
let controller
|
|
631
|
+
const loadIndex = async () => {
|
|
632
|
+
if (controller) controller.abort()
|
|
633
|
+
controller = new AbortController()
|
|
634
|
+
try {
|
|
635
|
+
const index = await readAll(ctx, sessionId, INDEX_PATH, controller.signal)
|
|
636
|
+
if (!disposed) {
|
|
637
|
+
setSources(sourcesFromIndex(index))
|
|
638
|
+
setIndexError(null)
|
|
639
|
+
}
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (disposed || error?.name === 'AbortError') return
|
|
642
|
+
if (!disposed) {
|
|
643
|
+
setSources([localSource()])
|
|
644
|
+
setIndexError(isMissingFile(error) ? null : (error?.message ?? String(error)))
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
loadIndex()
|
|
649
|
+
return () => {
|
|
650
|
+
disposed = true
|
|
651
|
+
if (controller) controller.abort()
|
|
652
|
+
}
|
|
653
|
+
}, [sessionId, cwd, tick])
|
|
654
|
+
|
|
655
|
+
React.useEffect(() => {
|
|
656
|
+
if (sources.length === 0) return
|
|
657
|
+
setSourceId(current => {
|
|
658
|
+
if (current && sources.some(source => source.id === current)) return current
|
|
659
|
+
const saved = readSavedSource(cwd)
|
|
660
|
+
if (saved && sources.some(source => source.id === saved)) return saved
|
|
661
|
+
return sources.find(source => source.kind === 'local')?.id ?? sources[0].id
|
|
662
|
+
})
|
|
663
|
+
}, [sources, cwd])
|
|
664
|
+
|
|
665
|
+
React.useEffect(() => {
|
|
666
|
+
if (sourceId) writeSavedSource(cwd, sourceId)
|
|
667
|
+
}, [sourceId, cwd])
|
|
668
|
+
|
|
669
|
+
// Selecting a remote source starts the host-side change poller once per
|
|
670
|
+
// session + project. The command returns immediately; failures surface
|
|
671
|
+
// through remote-state.json rather than blocking the panel.
|
|
672
|
+
React.useEffect(() => {
|
|
673
|
+
if (!sessionId || !sourceId) return
|
|
674
|
+
const selected = sources.find(item => item.id === sourceId)
|
|
675
|
+
if (!selected || selected.kind !== 'remote') return
|
|
676
|
+
const key = `${sessionId}:${selected.id}`
|
|
677
|
+
if (trackedRef.current === key) return
|
|
678
|
+
trackedRef.current = key
|
|
679
|
+
Promise.resolve(ctx.remote.commands.execute(sessionId, `/arch remote select ${selected.id}`, [])).catch(() => {})
|
|
680
|
+
}, [sessionId, sourceId, sources])
|
|
681
|
+
|
|
682
|
+
React.useEffect(() => {
|
|
683
|
+
setState({ status: 'loading', graph: null, error: null })
|
|
684
|
+
setSelectedId(null)
|
|
685
|
+
fittedRef.current = false
|
|
686
|
+
}, [sourceId])
|
|
687
|
+
|
|
688
|
+
React.useEffect(() => {
|
|
689
|
+
if (!sessionId) {
|
|
690
|
+
setState({ status: 'error', graph: null, error: 'No session is bound to this tab.' })
|
|
691
|
+
return undefined
|
|
692
|
+
}
|
|
693
|
+
if (!sourceId) {
|
|
694
|
+
setState({ status: 'loading', graph: null, error: null })
|
|
695
|
+
return undefined
|
|
696
|
+
}
|
|
697
|
+
const source = sources.find(item => item.id === sourceId)
|
|
698
|
+
const graphPath = source?.graphPath ?? GRAPH_PATH
|
|
699
|
+
let disposed = false
|
|
700
|
+
let controller
|
|
701
|
+
const load = async () => {
|
|
702
|
+
if (controller) controller.abort()
|
|
703
|
+
controller = new AbortController()
|
|
704
|
+
try {
|
|
705
|
+
const graph = await readAll(ctx, sessionId, graphPath, controller.signal)
|
|
706
|
+
if (!disposed) {
|
|
707
|
+
setState(previous => previous.graph?.rev === graph.rev ? previous : { status: 'live', graph, error: null })
|
|
708
|
+
}
|
|
709
|
+
} catch (error) {
|
|
710
|
+
if (disposed || error?.name === 'AbortError') return
|
|
711
|
+
if (isMissingFile(error)) {
|
|
712
|
+
setState({ status: 'empty', graph: null, error: null })
|
|
713
|
+
return
|
|
714
|
+
}
|
|
715
|
+
setState(previous => ({
|
|
716
|
+
status: previous.graph ? 'live' : 'error',
|
|
717
|
+
graph: previous.graph,
|
|
718
|
+
error: error?.message ?? String(error),
|
|
719
|
+
}))
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
load()
|
|
723
|
+
const timer = setInterval(load, POLL_MS)
|
|
724
|
+
return () => {
|
|
725
|
+
disposed = true
|
|
726
|
+
clearInterval(timer)
|
|
727
|
+
if (controller) controller.abort()
|
|
728
|
+
}
|
|
729
|
+
}, [sessionId, tick, sourceId, sources])
|
|
730
|
+
|
|
731
|
+
const graph = state.graph
|
|
732
|
+
const source = sources.find(item => item.id === sourceId) ?? null
|
|
733
|
+
const nodes = Array.isArray(graph?.nodes) ? graph.nodes : []
|
|
734
|
+
const edges = Array.isArray(graph?.edges) ? graph.edges : []
|
|
735
|
+
const selected = nodes.find(node => node.id === selectedId)
|
|
736
|
+
const selectedEdges = selected
|
|
737
|
+
? edges.filter(edge => edge.from === selected.id || edge.to === selected.id)
|
|
738
|
+
: []
|
|
739
|
+
const layoutView = layout(graph)
|
|
740
|
+
const positionOf = id => positions[id] ?? layoutView.positions.get(id)
|
|
741
|
+
|
|
742
|
+
const fitBoxes = boxes => {
|
|
743
|
+
if (boxes.length === 0) return
|
|
744
|
+
const minX = Math.min(...boxes.map(box => box.x))
|
|
745
|
+
const maxX = Math.max(...boxes.map(box => box.x + box.w))
|
|
746
|
+
const minY = Math.min(...boxes.map(box => box.y))
|
|
747
|
+
const maxY = Math.max(...boxes.map(box => box.y + box.h))
|
|
748
|
+
const pad = 60
|
|
749
|
+
const zoom = Math.min(1000 / (maxX - minX + pad), 700 / (maxY - minY + pad), 1.4)
|
|
750
|
+
setView({
|
|
751
|
+
zoom,
|
|
752
|
+
x: (1000 - (maxX - minX) * zoom) / 2 - minX * zoom,
|
|
753
|
+
y: (700 - (maxY - minY) * zoom) / 2 - minY * zoom,
|
|
754
|
+
})
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
const fitGraph = target => {
|
|
758
|
+
const current = Object.values(positions)
|
|
759
|
+
fitBoxes(current.length > 0 ? current : [...layout(target).positions.values()])
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const resetLayout = () => {
|
|
763
|
+
if (!graph) return
|
|
764
|
+
const base = layout(graph)
|
|
765
|
+
setPositions(Object.fromEntries(base.positions))
|
|
766
|
+
clearSavedPositions(storageKey)
|
|
767
|
+
fitBoxes([...base.positions.values()])
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
React.useEffect(() => {
|
|
771
|
+
if (!graph || !storageKey) return
|
|
772
|
+
const base = layout(graph)
|
|
773
|
+
const saved = readSavedPositions(storageKey)
|
|
774
|
+
setPositions(previous => {
|
|
775
|
+
const next = {}
|
|
776
|
+
for (const [id, box] of base.positions) {
|
|
777
|
+
const old = previous[id] ?? saved[id]
|
|
778
|
+
next[id] = old ? { ...box, x: old.x, y: old.y } : box
|
|
779
|
+
}
|
|
780
|
+
return next
|
|
781
|
+
})
|
|
782
|
+
}, [graph?.rev, storageKey])
|
|
783
|
+
|
|
784
|
+
React.useEffect(() => {
|
|
785
|
+
if (!graph || fittedRef.current) return
|
|
786
|
+
fittedRef.current = true
|
|
787
|
+
fitGraph(graph)
|
|
788
|
+
}, [graph?.rev])
|
|
789
|
+
|
|
790
|
+
const onNodePointerDown = (event, id) => {
|
|
791
|
+
event.stopPropagation()
|
|
792
|
+
if (event.button !== 0) return
|
|
793
|
+
const box = positionOf(id)
|
|
794
|
+
if (!box) return
|
|
795
|
+
const startX = event.clientX
|
|
796
|
+
const startY = event.clientY
|
|
797
|
+
const originX = box.x
|
|
798
|
+
const originY = box.y
|
|
799
|
+
let moved = false
|
|
800
|
+
let last = null
|
|
801
|
+
const move = moveEvent => {
|
|
802
|
+
if (!moved && Math.abs(moveEvent.clientX - startX) + Math.abs(moveEvent.clientY - startY) > 3) {
|
|
803
|
+
moved = true
|
|
804
|
+
}
|
|
805
|
+
if (!moved) return
|
|
806
|
+
const dx = (moveEvent.clientX - startX) / view.zoom
|
|
807
|
+
const dy = (moveEvent.clientY - startY) / view.zoom
|
|
808
|
+
last = { x: originX + dx, y: originY + dy }
|
|
809
|
+
setPositions(previous => {
|
|
810
|
+
const current = previous[id] ?? box
|
|
811
|
+
return {
|
|
812
|
+
...previous,
|
|
813
|
+
[id]: { ...current, ...last },
|
|
814
|
+
}
|
|
815
|
+
})
|
|
816
|
+
}
|
|
817
|
+
const up = () => {
|
|
818
|
+
window.removeEventListener('pointermove', move)
|
|
819
|
+
window.removeEventListener('pointerup', up)
|
|
820
|
+
if (!moved) {
|
|
821
|
+
setSelectedId(id)
|
|
822
|
+
return
|
|
823
|
+
}
|
|
824
|
+
if (last && storageKey) {
|
|
825
|
+
const saved = readSavedPositions(storageKey)
|
|
826
|
+
saved[id] = last
|
|
827
|
+
writeSavedPositions(storageKey, saved)
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
window.addEventListener('pointermove', move)
|
|
831
|
+
window.addEventListener('pointerup', up, { once: true })
|
|
832
|
+
event.preventDefault()
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
const viewPoint = event => {
|
|
836
|
+
const svg = svgRef.current
|
|
837
|
+
if (!svg) return undefined
|
|
838
|
+
const ctm = svg.getScreenCTM()
|
|
839
|
+
if (!ctm) return undefined
|
|
840
|
+
const point = svg.createSVGPoint()
|
|
841
|
+
point.x = event.clientX
|
|
842
|
+
point.y = event.clientY
|
|
843
|
+
return point.matrixTransform(ctm.inverse())
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
const onWheel = event => {
|
|
847
|
+
event.preventDefault()
|
|
848
|
+
// Normalize line/page deltas, and ignore zero/tiny events. Some
|
|
849
|
+
// trackpads/mice emit a deltaY=0 or a tiny settle event after a
|
|
850
|
+
// notch; treating every event as a full 12% step made the zoom jump
|
|
851
|
+
// 140% -> 157% -> 140% without any further user action.
|
|
852
|
+
const raw = event.deltaMode === 1
|
|
853
|
+
? event.deltaY * 16
|
|
854
|
+
: event.deltaMode === 2
|
|
855
|
+
? event.deltaY * 400
|
|
856
|
+
: event.deltaY
|
|
857
|
+
if (!Number.isFinite(raw) || Math.abs(raw) < 1) return
|
|
858
|
+
const vb = viewPoint(event)
|
|
859
|
+
if (!vb) return
|
|
860
|
+
const clamped = Math.max(-160, Math.min(160, raw))
|
|
861
|
+
const factor = Math.exp(-clamped * 0.0015)
|
|
862
|
+
setView(current => {
|
|
863
|
+
const zoom = Math.min(2.5, Math.max(0.35, current.zoom * factor))
|
|
864
|
+
if (zoom === current.zoom) return current
|
|
865
|
+
const worldX = (vb.x - current.x) / current.zoom
|
|
866
|
+
const worldY = (vb.y - current.y) / current.zoom
|
|
867
|
+
return {
|
|
868
|
+
zoom,
|
|
869
|
+
x: vb.x - worldX * zoom,
|
|
870
|
+
y: vb.y - worldY * zoom,
|
|
871
|
+
}
|
|
872
|
+
})
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
React.useEffect(() => {
|
|
876
|
+
const svg = svgRef.current
|
|
877
|
+
if (!svg) return undefined
|
|
878
|
+
const handler = event => onWheel(event)
|
|
879
|
+
svg.addEventListener('wheel', handler, { passive: false })
|
|
880
|
+
return () => svg.removeEventListener('wheel', handler)
|
|
881
|
+
}, [graph])
|
|
882
|
+
|
|
883
|
+
const onPointerDown = event => {
|
|
884
|
+
if (event.button !== 0) return
|
|
885
|
+
const vb = viewPoint(event)
|
|
886
|
+
if (!vb) return
|
|
887
|
+
panRef.current = { startX: vb.x, startY: vb.y, originX: view.x, originY: view.y }
|
|
888
|
+
event.currentTarget.setPointerCapture?.(event.pointerId)
|
|
889
|
+
event.preventDefault()
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const onPointerMove = event => {
|
|
893
|
+
const pan = panRef.current
|
|
894
|
+
if (!pan) return
|
|
895
|
+
const vb = viewPoint(event)
|
|
896
|
+
if (!vb) return
|
|
897
|
+
setView(current => ({
|
|
898
|
+
...current,
|
|
899
|
+
x: pan.originX + (vb.x - pan.startX),
|
|
900
|
+
y: pan.originY + (vb.y - pan.startY),
|
|
901
|
+
}))
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
const onPointerUp = () => {
|
|
905
|
+
panRef.current = null
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
const sourcePicker = sources.length > 1
|
|
909
|
+
? h('select', {
|
|
910
|
+
value: sourceId ?? '',
|
|
911
|
+
style: styles.select,
|
|
912
|
+
title: t('source.label'),
|
|
913
|
+
'aria-label': t('source.label'),
|
|
914
|
+
onChange: event => {
|
|
915
|
+
const next = event.target.value
|
|
916
|
+
setSourceId(next)
|
|
917
|
+
writeSavedSource(cwd, next)
|
|
918
|
+
},
|
|
919
|
+
}, sources.map(item => h('option', { key: item.id, value: item.id },
|
|
920
|
+
item.kind === 'local' ? t('source.local') : item.label)))
|
|
921
|
+
: null
|
|
922
|
+
|
|
923
|
+
const header = h(
|
|
924
|
+
'div',
|
|
925
|
+
{ style: styles.header },
|
|
926
|
+
h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', minWidth: 0 } },
|
|
927
|
+
h('h2', { style: styles.title }, t('panel.title')),
|
|
928
|
+
sourcePicker,
|
|
929
|
+
),
|
|
930
|
+
h(
|
|
931
|
+
'div',
|
|
932
|
+
{ style: { display: 'flex', alignItems: 'center', gap: '8px' } },
|
|
933
|
+
h('span', { style: styles.meta, title: cwd ?? undefined }, graph
|
|
934
|
+
? `rev ${graph.rev} · ${nodes.length} nodes · ${edges.length} edges`
|
|
935
|
+
: state.status === 'empty' ? t('panel.noGraph') : 'loading…'),
|
|
936
|
+
h('button', {
|
|
937
|
+
type: 'button',
|
|
938
|
+
style: styles.button,
|
|
939
|
+
onClick: () => setTick(value => value + 1),
|
|
940
|
+
title: t('panel.refresh'),
|
|
941
|
+
}, t('panel.refresh')),
|
|
942
|
+
),
|
|
943
|
+
)
|
|
944
|
+
|
|
945
|
+
const sourceBar = indexError
|
|
946
|
+
? h('div', { style: styles.sourceBar }, `${t('panel.indexError')} ${indexError}`)
|
|
947
|
+
: source?.kind === 'remote'
|
|
948
|
+
? h('div', { style: styles.sourceBar, title: source.meta }, `${source.label} · ${source.meta}`)
|
|
949
|
+
: null
|
|
950
|
+
|
|
951
|
+
const toolbar = nodes.length === 0
|
|
952
|
+
? null
|
|
953
|
+
: h('div', { style: styles.toolbar },
|
|
954
|
+
h('button', {
|
|
955
|
+
type: 'button',
|
|
956
|
+
style: styles.button,
|
|
957
|
+
onClick: () => fitGraph(graph),
|
|
958
|
+
}, t('panel.fit')),
|
|
959
|
+
h('button', {
|
|
960
|
+
type: 'button',
|
|
961
|
+
style: styles.button,
|
|
962
|
+
onClick: resetLayout,
|
|
963
|
+
title: 'Reset node positions to the automatic layout',
|
|
964
|
+
}, t('panel.reset')),
|
|
965
|
+
h('span', { style: styles.meta }, `${Math.round(view.zoom * 100)}%`),
|
|
966
|
+
h('span', { style: { ...styles.meta, opacity: 0.55 } }, t('panel.hint')))
|
|
967
|
+
|
|
968
|
+
const canvas = nodes.length === 0
|
|
969
|
+
? h('div', { style: styles.empty },
|
|
970
|
+
h('div', { style: { fontWeight: 600, marginBottom: '6px' } },
|
|
971
|
+
source?.kind === 'remote' ? t('panel.noGraphRemote') : 'No graph yet'),
|
|
972
|
+
h('div', null, source?.kind === 'remote'
|
|
973
|
+
? t('panel.remoteHint').replace('<id>', source.id)
|
|
974
|
+
: 'Ask the agent to create one with arch_graph_patch in this session workspace.'),
|
|
975
|
+
cwd ? h('div', { style: { ...styles.mono, marginTop: '8px', opacity: 0.65 } }, cwd) : null,
|
|
976
|
+
state.error ? h('div', { style: { marginTop: '8px', opacity: 0.7 } }, state.error) : null)
|
|
977
|
+
: h(
|
|
978
|
+
'svg',
|
|
979
|
+
{
|
|
980
|
+
ref: svgRef,
|
|
981
|
+
viewBox: '0 0 1000 700',
|
|
982
|
+
preserveAspectRatio: 'xMidYMid meet',
|
|
983
|
+
style: styles.canvas,
|
|
984
|
+
role: 'img',
|
|
985
|
+
'aria-label': 'Architecture graph',
|
|
986
|
+
onPointerDown,
|
|
987
|
+
onPointerMove,
|
|
988
|
+
onPointerUp,
|
|
989
|
+
onPointerCancel: onPointerUp,
|
|
990
|
+
},
|
|
991
|
+
h('defs', null,
|
|
992
|
+
h('marker', {
|
|
993
|
+
id: 'archmap-arrow',
|
|
994
|
+
viewBox: '0 0 10 10',
|
|
995
|
+
refX: '9',
|
|
996
|
+
refY: '5',
|
|
997
|
+
markerWidth: '6',
|
|
998
|
+
markerHeight: '6',
|
|
999
|
+
orient: 'auto-start-reverse',
|
|
1000
|
+
}, h('path', { d: 'M 0 0 L 10 5 L 0 10 z', fill: 'var(--dsw-alias-text-secondary, #888)' }))),
|
|
1001
|
+
h('g', { transform: `translate(${view.x} ${view.y}) scale(${view.zoom})` },
|
|
1002
|
+
edges.map(edge => {
|
|
1003
|
+
const from = positionOf(edge.from)
|
|
1004
|
+
const to = positionOf(edge.to)
|
|
1005
|
+
if (!from || !to) return null
|
|
1006
|
+
return h('line', {
|
|
1007
|
+
key: `edge:${edge.id}`,
|
|
1008
|
+
x1: from.x + from.w, y1: from.y + from.h / 2,
|
|
1009
|
+
x2: to.x, y2: to.y + to.h / 2,
|
|
1010
|
+
stroke: 'var(--dsw-alias-text-secondary, #888)',
|
|
1011
|
+
strokeWidth: 1.2,
|
|
1012
|
+
strokeOpacity: 0.6,
|
|
1013
|
+
markerEnd: 'url(#archmap-arrow)',
|
|
1014
|
+
})
|
|
1015
|
+
}),
|
|
1016
|
+
nodes.map(node => {
|
|
1017
|
+
const box = positionOf(node.id)
|
|
1018
|
+
return box
|
|
1019
|
+
? h(NodeShape, {
|
|
1020
|
+
key: node.id, node, box,
|
|
1021
|
+
selected: node.id === selectedId,
|
|
1022
|
+
onPointerDown: event => onNodePointerDown(event, node.id),
|
|
1023
|
+
})
|
|
1024
|
+
: null
|
|
1025
|
+
}),
|
|
1026
|
+
),
|
|
1027
|
+
)
|
|
1028
|
+
|
|
1029
|
+
const legendColors = {
|
|
1030
|
+
fresh: 'var(--dsw-alias-success, #4caf7d)',
|
|
1031
|
+
stale: 'var(--dsw-alias-warning, #d9a441)',
|
|
1032
|
+
proposed: 'var(--dsw-alias-danger, #e5484d)',
|
|
1033
|
+
}
|
|
1034
|
+
const legend = h('div', { style: styles.legend },
|
|
1035
|
+
Object.entries(legendColors).map(([status, color]) => h('span', { key: status, style: styles.legendItem },
|
|
1036
|
+
h('span', { style: { ...styles.legendDot, background: color } }),
|
|
1037
|
+
t(`legend.${status}`),
|
|
1038
|
+
)))
|
|
1039
|
+
|
|
1040
|
+
const openDiscussion = async mode => {
|
|
1041
|
+
if (!selected || discussState.status === 'starting') return
|
|
1042
|
+
if (!sessionId) {
|
|
1043
|
+
setDiscussState({ status: 'error', error: 'No session is bound to this tab.' })
|
|
1044
|
+
return
|
|
1045
|
+
}
|
|
1046
|
+
setDiscussState({ status: 'starting', error: null })
|
|
1047
|
+
try {
|
|
1048
|
+
const line = `/arch discuss node:${selected.id} ${mode}`
|
|
1049
|
+
const response = await ctx.remote.commands.execute(sessionId, line, [])
|
|
1050
|
+
if (!response.ok) throw new Error(`${response.error.code}: ${response.error.message}`)
|
|
1051
|
+
const outcome = response.value?.result
|
|
1052
|
+
if (!outcome || outcome.kind !== 'success') throw new Error(outcome?.text ?? 'discussion failed')
|
|
1053
|
+
const text = outcome.text ?? ''
|
|
1054
|
+
const match = /\[child=([^\s\]]+) parent=([^\s\]]+) mode=([^\s\]]+)/.exec(text)
|
|
1055
|
+
if (!match) throw new Error(`discussion started but no sidebar address: ${text}`)
|
|
1056
|
+
if (!ctx.sidebarRight || typeof ctx.sidebarRight.openResource !== 'function') {
|
|
1057
|
+
throw new Error('the right sidebar does not support opening a subagent chat')
|
|
1058
|
+
}
|
|
1059
|
+
const address = `dsh-resource://subagentchat/session/${encodeURIComponent(match[1])}?parent=${encodeURIComponent(match[2])}&mode=${encodeURIComponent(match[3])}`
|
|
1060
|
+
ctx.sidebarRight.openResource(address, { kind: 'subagentchat', preferNewPane: true })
|
|
1061
|
+
setDiscussState({ status: 'idle', error: null })
|
|
1062
|
+
} catch (error) {
|
|
1063
|
+
setDiscussState({ status: 'error', error: error?.message ?? String(error) })
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
const detail = selected
|
|
1068
|
+
? h('div', { style: styles.detail },
|
|
1069
|
+
h('p', { style: styles.detailTitle }, `${selected.id} · ${selected.title}`),
|
|
1070
|
+
h('div', { style: styles.detailRow }, `${t('detail.kind')} ${selected.kind}${selected.group ? ` · ${t('detail.group')} ${selected.group}` : ''} · ${t('detail.status')} ${t(`legend.${selected.status}`)}`),
|
|
1071
|
+
selected.summary ? h('div', { style: styles.detailRow }, selected.summary) : null,
|
|
1072
|
+
selected.anchors?.length
|
|
1073
|
+
? h('div', { style: { ...styles.detailRow, ...styles.mono } }, selected.anchors.map(anchor => `${anchor.path}${anchor.symbol ? `#${anchor.symbol}` : ''}`).join(' · '))
|
|
1074
|
+
: null,
|
|
1075
|
+
selectedEdges.length
|
|
1076
|
+
? h('div', { style: styles.detailRow }, selectedEdges.map(edge => `${edge.id}: ${edge.from} → ${edge.to}${edge.label ? ` (${edge.label})` : ''}`).join(' · '))
|
|
1077
|
+
: null,
|
|
1078
|
+
h('div', { style: styles.discussRow },
|
|
1079
|
+
h('button', {
|
|
1080
|
+
type: 'button',
|
|
1081
|
+
style: styles.button,
|
|
1082
|
+
disabled: discussState.status === 'starting',
|
|
1083
|
+
onClick: () => openDiscussion('fork'),
|
|
1084
|
+
}, discussState.status === 'starting' ? t('discuss.starting') : t('discuss.fork')),
|
|
1085
|
+
h('button', {
|
|
1086
|
+
type: 'button',
|
|
1087
|
+
style: styles.button,
|
|
1088
|
+
disabled: discussState.status === 'starting',
|
|
1089
|
+
onClick: () => openDiscussion('lite'),
|
|
1090
|
+
}, t('discuss.lite'))),
|
|
1091
|
+
h('div', { style: styles.discussHint }, t('discuss.hint')),
|
|
1092
|
+
discussState.error ? h('div', { style: styles.discussError }, discussState.error) : null)
|
|
1093
|
+
: h('div', { style: { ...styles.detail, opacity: 0.65 } }, t('panel.empty'))
|
|
1094
|
+
|
|
1095
|
+
return h('div', { style: styles.root },
|
|
1096
|
+
header,
|
|
1097
|
+
sourceBar,
|
|
1098
|
+
state.status === 'error' && state.error
|
|
1099
|
+
? h('div', { style: styles.error }, `${t('panel.error')}: ${state.error}`)
|
|
1100
|
+
: null,
|
|
1101
|
+
toolbar,
|
|
1102
|
+
legend,
|
|
1103
|
+
canvas,
|
|
1104
|
+
nodes.length > 0 ? detail : null,
|
|
1105
|
+
)
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
return {
|
|
1110
|
+
inject: ['slots', 'locale', 'sidebarRightTabs', 'sidebarRight', 'remote', 'remote.commands', 'remote.workspaceFiles'],
|
|
1111
|
+
apply(ctx) {
|
|
1112
|
+
if (ctx.locale) {
|
|
1113
|
+
const register = () => ctx.locale.register(LOCALE_NS, { en: EN, zh: ZH })
|
|
1114
|
+
if (typeof ctx.effect === 'function') ctx.effect(register, 'architecture-map: locale dictionaries')
|
|
1115
|
+
else register()
|
|
1116
|
+
t = ctx.locale.bind(LOCALE_NS)
|
|
1117
|
+
}
|
|
1118
|
+
ctx.sidebarRightTabs.register({
|
|
1119
|
+
id: TYPE_ID,
|
|
1120
|
+
kind: 'archmap.graph',
|
|
1121
|
+
priority: 'extension',
|
|
1122
|
+
title: () => t('panel.title'),
|
|
1123
|
+
guide: [{
|
|
1124
|
+
id: 'open',
|
|
1125
|
+
order: 30,
|
|
1126
|
+
title: () => t('panel.title'),
|
|
1127
|
+
description: () => 'Open the live architecture graph panel',
|
|
1128
|
+
}],
|
|
1129
|
+
})
|
|
1130
|
+
|
|
1131
|
+
const GraphTab = createGraphTab(ctx)
|
|
1132
|
+
ctx.slots.inject('sidebar.right.pane.tab', () => ctx.slots.register({
|
|
1133
|
+
name: 'sidebar.right.pane.tab',
|
|
1134
|
+
key: TYPE_ID,
|
|
1135
|
+
}, GraphTab))
|
|
1136
|
+
|
|
1137
|
+
// Fence rendering is optional: a slot/version mismatch must never stop
|
|
1138
|
+
// the graph tab from mounting. Register it after the panel and contain
|
|
1139
|
+
// any failure to this feature.
|
|
1140
|
+
try {
|
|
1141
|
+
ctx.slots.inject('conversation.chat.markdown.fence', () => {
|
|
1142
|
+
try {
|
|
1143
|
+
return ctx.slots.register({
|
|
1144
|
+
name: 'conversation.chat.markdown.fence',
|
|
1145
|
+
key: 'mermaid',
|
|
1146
|
+
id: 'dsh-architecture-map/fence/mermaid',
|
|
1147
|
+
}, MermaidFence)
|
|
1148
|
+
} catch (error) {
|
|
1149
|
+
console.warn('[architecture-map] Mermaid fence registration failed:', error)
|
|
1150
|
+
return () => {}
|
|
1151
|
+
}
|
|
1152
|
+
})
|
|
1153
|
+
} catch (error) {
|
|
1154
|
+
console.warn('[architecture-map] Mermaid fence injection failed:', error)
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
// `/arch` is a Host command (the composer shows the same blue chip as
|
|
1158
|
+
// /goal). When it succeeds, open the panel here.
|
|
1159
|
+
ctx.on('command/executed', (sessionId, commandName, result) => {
|
|
1160
|
+
if (commandName !== 'arch' || result?.kind !== 'success') return
|
|
1161
|
+
try {
|
|
1162
|
+
ctx.sidebarRight.openTab('archmap.graph')
|
|
1163
|
+
} catch (error) {
|
|
1164
|
+
console.warn('[architecture-map] openTab failed after /arch', error)
|
|
1165
|
+
}
|
|
1166
|
+
})
|
|
1167
|
+
|
|
1168
|
+
},
|
|
1169
|
+
}
|
|
1170
|
+
},
|
|
1171
|
+
})
|