@louisultra/dsh-verymath 0.5.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.js CHANGED
@@ -8,94 +8,667 @@
8
8
  *
9
9
  * 入口机制(读宿主包源码确认,不是猜):
10
10
  * - `@deepseek-ai/dsh-client-ui-sidebar` 在 root 作用域声明 `sidebar.panellist`
11
- * (kind: list):插件注册 `{ id, order, label }` + 图标组件,左栏就多一行;
11
+ * (kind: list):注册 `{ id, order, label }` + 图标组件,左栏就多一行;
12
12
  * - 点开时按**同一 id** 去 `@deepseek-ai/dsh-client-ui-layout` 的 `main`
13
- * (kind: keyed,root 作用域)取组件,所以主面板注册必须带 `key: <同一 id>`;
14
- * - 注册 API:`ctx.slots.inject(slot, () => ctx.slots.register(options, Component))`,
15
- * `ctx.effect(...)` 保证插件卸载时一并移除。
13
+ * (kind: keyed,root 作用域)取组件,所以主面板注册必须带 `key: <同一 id>`。
14
+ *
15
+ * 面板只做「对话里做起来烦」的事,不做对话能一步完成的事:
16
+ * 环境 —— 本机工具链就绪度 + 可复制的修复命令 + 可导出报告
17
+ * 术语库 —— 可选可查可改可删(手改 JSON 的痛苦正是它存在的理由)
18
+ * 论文图 —— 依赖图可点节点看陈述/度数/依赖关系
19
+ * 技能 —— 随包 16 个技能与其说明
20
+ *
21
+ * 数据全部走宿主侧 `/api/verymath/*` 精确路由(`src/http.ts`),与工具共用同一份
22
+ * 核心实现:面板改了术语库,对话里的 `verymath_glossary_query` 立刻看到。
16
23
  */
17
24
  window.__ModuleLoader__.load({
18
25
  id: '@louisultra/dsh-verymath',
19
- factory: (require) => {
26
+ factory: (platformRequire) => {
20
27
  const module = { exports: {} }
21
28
  const exports = module.exports
22
- const react = require('react')
29
+ const react = platformRequire('react')
23
30
  const h = react.createElement
31
+ const useState = react.useState
32
+ const useEffect = react.useEffect
33
+ const useMemo = react.useMemo
34
+ const useCallback = react.useCallback
24
35
 
25
36
  const PANEL_ID = 'verymath'
26
37
 
27
- /** 左栏图标:一个极简的求和符号,避免额外资源文件。 */
38
+ // ------------------------------------------------------------ 取数
39
+
40
+ async function request(path, init) {
41
+ const response = await fetch(path, init)
42
+ const text = await response.text()
43
+ let body
44
+ try {
45
+ body = text.length > 0 ? JSON.parse(text) : {}
46
+ } catch {
47
+ throw new Error(`接口返回的不是 JSON(HTTP ${response.status}):${text.slice(0, 120)}`)
48
+ }
49
+ if (!response.ok || body.error) throw new Error(body.error || `HTTP ${response.status}`)
50
+ return body
51
+ }
52
+
53
+ const api = (path) => request(path)
54
+ const postJson = (path, payload) => request(path, {
55
+ method: 'POST',
56
+ headers: { 'content-type': 'application/json' },
57
+ body: JSON.stringify(payload),
58
+ })
59
+
60
+ async function copyText(text) {
61
+ try {
62
+ await navigator.clipboard.writeText(text)
63
+ return true
64
+ } catch {
65
+ return false
66
+ }
67
+ }
68
+
69
+ // ------------------------------------------------------------ 样式
70
+
71
+ const S = {
72
+ wrap: { display: 'flex', flexDirection: 'column', height: '100%', minHeight: 0, fontSize: 13, color: 'var(--dsh-text-primary, #111)' },
73
+ head: {
74
+ display: 'flex', alignItems: 'center', gap: 12, padding: '11px 16px',
75
+ borderBottom: '1px solid var(--dsh-border, rgba(120,120,120,.22))', flexShrink: 0,
76
+ },
77
+ title: { fontSize: 14, fontWeight: 600, letterSpacing: '.01em' },
78
+ tabs: { display: 'flex', gap: 2, marginLeft: 'auto', background: 'var(--dsh-fill-secondary, rgba(120,120,120,.1))', padding: 3, borderRadius: 9 },
79
+ tab: {
80
+ border: 'none', background: 'transparent', cursor: 'pointer', fontSize: 12.5,
81
+ padding: '5px 12px', borderRadius: 7, color: 'var(--dsh-text-secondary, #666)', transition: 'background .12s',
82
+ },
83
+ tabOn: { background: 'var(--dsh-surface, #fff)', color: 'var(--dsh-text-primary, #111)', fontWeight: 600, boxShadow: '0 1px 2px rgba(0,0,0,.12)' },
84
+ body: { overflow: 'auto', padding: '16px 18px 28px', minHeight: 0, flex: 1 },
85
+ toolbar: { display: 'flex', gap: 8, alignItems: 'center', flexWrap: 'wrap', marginBottom: 12 },
86
+ btn: {
87
+ border: '1px solid var(--dsh-border, rgba(120,120,120,.3))', background: 'transparent', cursor: 'pointer',
88
+ fontSize: 12, padding: '4px 10px', borderRadius: 7, color: 'inherit',
89
+ },
90
+ btnPrimary: { border: '1px solid transparent', background: '#2563eb', color: '#fff', cursor: 'pointer', fontSize: 12, padding: '5px 12px', borderRadius: 7 },
91
+ btnDanger: { border: '1px solid rgba(220,38,38,.45)', background: 'transparent', color: '#dc2626', cursor: 'pointer', fontSize: 12, padding: '4px 9px', borderRadius: 7 },
92
+ hint: { color: 'var(--dsh-text-tertiary, #888)', fontSize: 12, lineHeight: 1.7 },
93
+ card: { border: '1px solid var(--dsh-border, rgba(120,120,120,.22))', borderRadius: 10, padding: '11px 13px', marginBottom: 9 },
94
+ code: { fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', fontSize: 11.5, color: 'var(--dsh-text-secondary, #666)' },
95
+ select: { fontSize: 12, padding: '5px 8px', borderRadius: 7, maxWidth: 460, border: '1px solid var(--dsh-border, rgba(120,120,120,.3))', background: 'transparent', color: 'inherit' },
96
+ input: { fontSize: 12.5, padding: '6px 9px', borderRadius: 7, boxSizing: 'border-box', border: '1px solid var(--dsh-border, rgba(120,120,120,.3))', background: 'transparent', color: 'inherit', outline: 'none' },
97
+ stat: { display: 'flex', gap: 16, flexWrap: 'wrap', marginBottom: 12 },
98
+ statNum: { fontSize: 17, fontWeight: 650, lineHeight: 1.1 },
99
+ statLabel: { ...{ color: 'var(--dsh-text-tertiary, #888)' }, fontSize: 11.5 },
100
+ dot: (state) => ({ width: 8, height: 8, borderRadius: 4, flexShrink: 0, marginTop: 5, background: state === 'ready' ? '#16a34a' : state === 'partial' ? '#d97706' : '#dc2626' }),
101
+ }
102
+
103
+ function Loading(props) {
104
+ return h('div', { style: { ...S.hint, display: 'flex', alignItems: 'center', gap: 8, padding: '10px 0' } },
105
+ h('span', {
106
+ style: {
107
+ width: 12, height: 12, borderRadius: 6, border: '1.5px solid rgba(120,120,120,.35)',
108
+ borderTopColor: '#2563eb', animation: 'verymath-spin .7s linear infinite', display: 'inline-block',
109
+ },
110
+ }),
111
+ props.label || '读取中…')
112
+ }
113
+
114
+ function Failure(props) {
115
+ return h('div', { style: { ...S.card, borderColor: 'rgba(220,38,38,.45)', background: 'rgba(220,38,38,.06)' } },
116
+ h('div', { style: { fontWeight: 600, marginBottom: 3 } }, '读不到数据'),
117
+ h('div', { style: { ...S.hint, color: 'inherit' } }, props.message),
118
+ props.onRetry ? h('button', { onClick: props.onRetry, style: { ...S.btn, marginTop: 8 } }, '重试') : null)
119
+ }
120
+
121
+ function Empty(props) {
122
+ return h('div', { style: { padding: '22px 0', ...S.hint } }, props.children)
123
+ }
124
+
125
+ function useEndpoint(path) {
126
+ const [state, setState] = useState({ status: 'loading', data: undefined, error: '', tick: 0 })
127
+ const reload = useCallback(() => setState((prev) => ({ ...prev, tick: prev.tick + 1 })), [])
128
+ useEffect(() => {
129
+ if (path === undefined || path === null) return undefined
130
+ let alive = true
131
+ setState((prev) => ({ ...prev, status: 'loading' }))
132
+ api(path)
133
+ .then((data) => { if (alive) setState((prev) => ({ ...prev, status: 'ready', data, error: '' })) })
134
+ .catch((error) => { if (alive) setState((prev) => ({ ...prev, status: 'failed', error: String(error && error.message ? error.message : error) })) })
135
+ return () => { alive = false }
136
+ }, [path, state.tick])
137
+ return { ...state, reload }
138
+ }
139
+
140
+ // ---------------------------------------------------------- 环境页签
141
+
142
+ const ENV_ORDER = ['python', 'pyyaml', 'latexmk', 'xelatex', 'osqp', 'sage', 'lean']
143
+
144
+ function envReport(data) {
145
+ const lines = [`# VeryMath 环境报告`, ``, `工作区:${data.workspace || '(未知)'}`, `就绪:${data.ready}/${data.total}`, ``]
146
+ for (const row of data.rows || []) {
147
+ lines.push(`- ${row.state === 'ready' ? '✅' : '❌'} ${row.label}${row.detail ? ` — ${row.detail}` : ''}`)
148
+ if (row.state !== 'ready' && row.hint) lines.push(` - 安装:${row.hint}`)
149
+ }
150
+ return lines.join('\n')
151
+ }
152
+
153
+ function EnvTab() {
154
+ const { status, data, error, reload } = useEndpoint('/api/verymath/env')
155
+ const [copied, setCopied] = useState('')
156
+ const flash = useCallback((id) => { setCopied(id); setTimeout(() => setCopied(''), 1400) }, [])
157
+
158
+ if (status === 'loading' && data === undefined) return h(Loading, { label: '正在探测本机工具链(实际执行 --version / import)…' })
159
+ if (status === 'failed') return h(Failure, { message: error, onRetry: reload })
160
+
161
+ const rows = (data.rows || []).slice().sort((left, right) => {
162
+ const leftIndex = ENV_ORDER.indexOf(left.id)
163
+ const rightIndex = ENV_ORDER.indexOf(right.id)
164
+ return (leftIndex < 0 ? 99 : leftIndex) - (rightIndex < 0 ? 99 : rightIndex)
165
+ })
166
+ const ready = data.ready ?? 0
167
+ const total = data.total ?? rows.length
168
+
169
+ return h('div', null,
170
+ h('div', { style: S.stat },
171
+ h('div', null, h('div', { style: S.statNum }, `${ready}/${total}`), h('div', { style: S.statLabel }, '就绪')),
172
+ h('div', null, h('div', { style: S.statNum }, String(total - ready)), h('div', { style: S.statLabel }, '缺失')),
173
+ h('div', { style: { marginLeft: 'auto', display: 'flex', gap: 8, alignItems: 'flex-start' } },
174
+ h('button', { onClick: reload, style: S.btn }, '重新探测'),
175
+ h('button', {
176
+ onClick: async () => {
177
+ const ok = await copyText(envReport(data))
178
+ flash(ok ? 'report' : 'fail')
179
+ },
180
+ style: S.btn,
181
+ }, copied === 'report' ? '已复制' : copied === 'fail' ? '复制失败' : '复制报告'),
182
+ ),
183
+ ),
184
+ h('div', { style: { ...S.hint, marginBottom: 12 } }, `工作区:${data.workspace || '(未知)'}`),
185
+ ...rows.map((row) => h('div', { key: row.id, style: { ...S.card, display: 'flex', gap: 10 } },
186
+ h('div', { style: S.dot(row.state) }),
187
+ h('div', { style: { flex: 1, minWidth: 0 } },
188
+ h('div', { style: { display: 'flex', gap: 8, alignItems: 'baseline' } },
189
+ h('span', { style: { fontWeight: 600 } }, row.label),
190
+ row.detail ? h('span', { style: S.code }, row.detail) : null,
191
+ h('span', { style: { ...S.hint, marginLeft: 'auto' } }, row.state === 'ready' ? '可用' : '缺失'),
192
+ ),
193
+ h('div', { style: S.hint }, row.enables),
194
+ row.hint
195
+ ? h('div', { style: { display: 'flex', gap: 8, alignItems: 'baseline', marginTop: 4 } },
196
+ h('code', { style: { ...S.code, color: '#b45309', flex: 1 } }, row.hint),
197
+ h('button', {
198
+ onClick: async () => { await copyText(row.hint); flash(row.id) },
199
+ style: { ...S.btn, padding: '2px 8px' },
200
+ }, copied === row.id ? '已复制' : '复制'),
201
+ )
202
+ : null,
203
+ ),
204
+ )),
205
+ )
206
+ }
207
+
208
+ // -------------------------------------------------------- 术语库页签
209
+
210
+ function Stat(props) {
211
+ return h('div', null,
212
+ h('div', { style: S.statNum }, String(props.value)),
213
+ h('div', { style: S.statLabel }, props.label))
214
+ }
215
+
216
+ function TermEditor(props) {
217
+ const draftFor = (term) => ({
218
+ term_id: term ? term.term_id : undefined,
219
+ en: term ? term.en : '',
220
+ cn: term ? term.cn : '',
221
+ pos: term && term.pos ? term.pos : '',
222
+ field: term && term.field ? term.field.join(', ') : '',
223
+ source: term && term.source ? term.source : '',
224
+ exampleEn: term && term.examples && term.examples[0] ? term.examples[0].en : '',
225
+ exampleCn: term && term.examples && term.examples[0] ? term.examples[0].cn : '',
226
+ })
227
+ const [draft, setDraft] = useState(() => draftFor(props.term))
228
+ const [busy, setBusy] = useState(false)
229
+ const [error, setError] = useState('')
230
+
231
+ // Reset the draft only when a *different* row is edited. Depending on the whole
232
+ // `props.term` object reset the draft whenever a background refresh produced a
233
+ // new object for the same term, silently discarding what the user had typed
234
+ // (the list reloads after every save).
235
+ const termId = props.term === undefined ? '' : props.term.term_id
236
+
237
+ useEffect(() => { setDraft(draftFor(props.term)) }, [termId])
238
+
239
+ const field = (key) => (event) => setDraft((prev) => ({ ...prev, [key]: event.target.value }))
240
+ const input = (key, placeholder, extra) => h('input', {
241
+ value: draft[key], onChange: field(key), placeholder, style: { ...S.input, ...(extra || {}) },
242
+ })
243
+
244
+ const save = async () => {
245
+ setBusy(true); setError('')
246
+ try {
247
+ const payload = { en: draft.en.trim(), cn: draft.cn.trim() }
248
+ if (draft.term_id) payload.term_id = draft.term_id
249
+ if (draft.pos.trim()) payload.pos = draft.pos.trim()
250
+ if (draft.field.trim()) payload.field = draft.field.split(',').map((part) => part.trim()).filter(Boolean)
251
+ if (draft.source.trim()) payload.source = draft.source.trim()
252
+ if (draft.exampleEn.trim() && draft.exampleCn.trim()) payload.example = { en: draft.exampleEn.trim(), cn: draft.exampleCn.trim() }
253
+ await props.onSave(payload)
254
+ props.onDone()
255
+ } catch (cause) {
256
+ setError(String(cause && cause.message ? cause.message : cause))
257
+ } finally {
258
+ setBusy(false)
259
+ }
260
+ }
261
+
262
+ return h('div', { style: { ...S.card, background: 'var(--dsh-fill-secondary, rgba(120,120,120,.06))' } },
263
+ h('div', { style: { fontWeight: 600, marginBottom: 8 } },
264
+ draft.term_id ? `编辑 ${draft.term_id}` : '新增术语'),
265
+ h('div', { style: { display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 } },
266
+ h('label', { style: S.hint }, '英文(en)', h('div', { style: { marginTop: 2 } }, input('en', 'manifold'))),
267
+ h('label', { style: S.hint }, '中文(cn)', h('div', { style: { marginTop: 2 } }, input('cn', '流形'))),
268
+ h('label', { style: S.hint }, '词性(可选)', h('div', { style: { marginTop: 2 } }, input('pos', '名词'))),
269
+ h('label', { style: S.hint }, '子领域(逗号分隔)', h('div', { style: { marginTop: 2 } }, input('field', '代数拓扑, 微分几何'))),
270
+ h('label', { style: S.hint }, '来源(可选)', h('div', { style: { marginTop: 2 } }, input('source', '新英汉数学词汇'))),
271
+ h('label', { style: S.hint }, '例句 英文(可选)', h('div', { style: { marginTop: 2 } }, input('exampleEn', 'a smooth manifold'))),
272
+ ),
273
+ h('div', { style: { marginTop: 8 } },
274
+ h('label', { style: S.hint }, '例句 中文(可选)',
275
+ h('div', { style: { marginTop: 2 } }, input('exampleCn', '一个光滑流形', { width: '100%' })))),
276
+ draft.term_id
277
+ ? h('div', { style: { ...S.hint, marginTop: 6 } },
278
+ '带 term_id 提交 = 纠正这一行的中文与可选字段;英文必须与该行一致(改名不支持)。')
279
+ : h('div', { style: { ...S.hint, marginTop: 6 } },
280
+ '不带 term_id = 新增;同一英文的另一种译法会存成 en__N 别名行,不会覆盖现有译名。'),
281
+ error ? h('div', { style: { ...S.hint, color: '#dc2626', marginTop: 6 } }, error) : null,
282
+ h('div', { style: { display: 'flex', gap: 8, marginTop: 10 } },
283
+ h('button', { onClick: save, disabled: busy, style: { ...S.btnPrimary, opacity: busy ? 0.6 : 1 } },
284
+ busy ? '保存中…' : '保存'),
285
+ h('button', { onClick: props.onDone, style: S.btn }, '取消'),
286
+ ),
287
+ )
288
+ }
289
+
290
+ function TermRow(props) {
291
+ const term = props.term
292
+ if (props.editing) {
293
+ return h(TermEditor, {
294
+ term,
295
+ onSave: props.onUpsert,
296
+ onDone: props.onDoneEditing,
297
+ })
298
+ }
299
+ return h('div', { style: S.card },
300
+ h('div', { style: { display: 'flex', gap: 8, alignItems: 'baseline' } },
301
+ h('span', { style: { fontWeight: 600 } }, term.en),
302
+ h('span', { style: { color: 'var(--dsh-text-tertiary, #999)' } }, '→'),
303
+ h('span', null, term.cn),
304
+ h('span', { style: { ...S.code, marginLeft: 'auto' } }, term.term_id),
305
+ h('button', { onClick: () => props.onStartEditing(term.term_id), style: { ...S.btn, padding: '2px 8px' } }, '编辑'),
306
+ h('button', {
307
+ onClick: async () => {
308
+ if (!window.confirm(`删除 ${term.term_id}(${term.en} / ${term.cn})?此操作直接改 glossary.json。`)) return
309
+ try { await props.onDelete(term.term_id) } catch (cause) { window.alert(String(cause && cause.message ? cause.message : cause)) }
310
+ },
311
+ style: { ...S.btnDanger },
312
+ }, '删除'),
313
+ ),
314
+ h('div', { style: { ...S.hint, display: 'flex', gap: 10, flexWrap: 'wrap' } },
315
+ term.matchedBy ? h('span', null, `命中:${term.matchedBy}`) : null,
316
+ term.pos ? h('span', null, `词性:${term.pos}`) : null,
317
+ term.field && term.field.length > 0 ? h('span', null, `子领域:${term.field.join(' / ')}`) : null,
318
+ term.source ? h('span', null, `来源:${term.source}`) : null,
319
+ term.updatedAt ? h('span', null, `更新:${term.updatedAt}`) : null,
320
+ ),
321
+ ...(term.examples || []).map((example, index) => h('div', {
322
+ key: index,
323
+ style: { ...S.hint, marginTop: 5, paddingLeft: 9, borderLeft: '2px solid rgba(120,120,120,.3)' },
324
+ }, `${example.en} — ${example.cn}`)),
325
+ )
326
+ }
327
+
328
+ function GlossaryTab() {
329
+ const list = useEndpoint('/api/verymath/glossaries')
330
+ const [selected, setSelected] = useState('')
331
+ const [query, setQuery] = useState('')
332
+ const [showGaps, setShowGaps] = useState(false)
333
+ // One editor at a time: two open forms on the same document invite saving a
334
+ // stale draft over a fresh edit.
335
+ const [adding, setAdding] = useState(false)
336
+ const [editingId, setEditingId] = useState('')
337
+
338
+ const glossaries = (list.data && list.data.glossaries) || []
339
+ useEffect(() => {
340
+ if (selected === '' && glossaries.length > 0) setSelected(glossaries[0].path)
341
+ }, [glossaries.length, selected])
342
+
343
+ const endpoint = selected === ''
344
+ ? undefined
345
+ : `/api/verymath/glossary?path=${encodeURIComponent(selected)}&q=${encodeURIComponent(query)}`
346
+ const result = useEndpoint(endpoint)
347
+
348
+ const refresh = useCallback(() => { list.reload(); result.reload() }, [list.reload, result.reload])
349
+
350
+ const upsert = useCallback(async (payload) => {
351
+ await postJson('/api/verymath/glossary/upsert', { path: selected, terms: [payload] })
352
+ refresh()
353
+ }, [selected, refresh])
354
+
355
+ const remove = useCallback(async (termId) => {
356
+ await postJson('/api/verymath/glossary/delete', { path: selected, term_id: termId })
357
+ refresh()
358
+ }, [selected, refresh])
359
+
360
+ if (list.status === 'loading' && list.data === undefined) return h(Loading, { label: '正在扫描工作区里的 glossary.json…' })
361
+ if (list.status === 'failed') return h(Failure, { message: list.error, onRetry: list.reload })
362
+ if (glossaries.length === 0) {
363
+ return h(Empty, null,
364
+ '工作区里没有 glossary.json(扫描深度 3 层,文件名含 glossary 或「术语」)。',
365
+ h('div', { style: { marginTop: 8 } },
366
+ '让 Agent 说一句「从 source.csv 构建一份中英数学术语表,输出 glossary/glossary.json」,回来这里就能编辑了。'))
367
+ }
368
+
369
+ const stats = (result.data && result.data.stats) || {}
370
+ const terms = (result.data && result.data.terms) || []
371
+ const gaps = showGaps && stats.missingExampleCount + stats.missingFieldCount > 0
372
+ ? { examples: stats.missingExamples || [], fields: stats.missingField || [] }
373
+ : undefined
374
+
375
+ return h('div', null,
376
+ h('div', { style: S.toolbar },
377
+ h('select', { value: selected, onChange: (event) => setSelected(event.target.value), style: S.select },
378
+ ...glossaries.map((item) => h('option', { key: item.path, value: item.path }, `${item.path}(${item.terms} 条)`))),
379
+ h('button', { onClick: () => { setEditingId(''); setAdding(true) }, style: S.btnPrimary }, '新增术语'),
380
+ h('button', { onClick: refresh, style: S.btn }, '刷新'),
381
+ ),
382
+ h('div', { style: S.stat },
383
+ h(Stat, { value: stats.total ?? 0, label: '术语' }),
384
+ h(Stat, { value: stats.withExamples ?? 0, label: '带例句' }),
385
+ h(Stat, { value: stats.aliases ?? 0, label: '别名行' }),
386
+ h(Stat, { value: (stats.fields || []).length, label: '子领域' }),
387
+ h(Stat, { value: stats.missingExampleCount ?? 0, label: '缺例句' }),
388
+ ),
389
+ (stats.missingExampleCount > 0 || stats.missingFieldCount > 0)
390
+ ? h('div', { style: { marginBottom: 10 } },
391
+ h('button', { onClick: () => setShowGaps((value) => !value), style: S.btn },
392
+ showGaps ? '收起缺口清单' : `看缺口清单(缺例句 ${stats.missingExampleCount} / 缺子领域 ${stats.missingFieldCount})`),
393
+ gaps
394
+ ? h('div', { style: { ...S.card, marginTop: 8 } },
395
+ gaps.examples.length > 0
396
+ ? h('div', null,
397
+ h('div', { style: { fontWeight: 600, marginBottom: 3 } }, '缺例句'),
398
+ h('div', { style: S.code }, gaps.examples.join(', ')),
399
+ h('div', { style: { ...S.hint, marginTop: 4 } }, '技能脚本可补:glossary_enrich_examples.py(需显式 --model / --api-key)。'))
400
+ : null,
401
+ gaps.fields.length > 0
402
+ ? h('div', { style: { marginTop: 8 } },
403
+ h('div', { style: { fontWeight: 600, marginBottom: 3 } }, '缺子领域'),
404
+ h('div', { style: S.code }, gaps.fields.join(', ')))
405
+ : null,
406
+ )
407
+ : null,
408
+ )
409
+ : null,
410
+ h('input', {
411
+ value: query, onChange: (event) => setQuery(event.target.value),
412
+ placeholder: '查术语:英文、中文、term_id 或子领域(留空只看统计)', style: { ...S.input, width: '100%' },
413
+ }),
414
+ adding ? h(TermEditor, { term: undefined, onSave: upsert, onDone: () => setAdding(false) }) : null,
415
+ h('div', { style: { marginTop: 12 } },
416
+ // Stale-while-revalidate: a reload must NOT unmount the list. Rendering only
417
+ // on `status === 'ready'` tore down the open editor on every 刷新 (and on the
418
+ // reload that follows a save), which discarded the user's un-saved input.
419
+ result.status === 'loading' && result.data === undefined ? h(Loading, null) : null,
420
+ result.status === 'loading' && result.data !== undefined
421
+ ? h('div', { style: { ...S.hint, marginBottom: 6 } }, '刷新中…')
422
+ : null,
423
+ result.status === 'failed' ? h(Failure, { message: result.error, onRetry: result.reload }) : null,
424
+ result.data !== undefined && query.trim().length > 0 && terms.length === 0
425
+ ? h(Empty, null, `没有匹配「${query}」。试试英文词根,或换个子领域名。`)
426
+ : null,
427
+ result.data !== undefined && terms.length > 0
428
+ ? h('div', null,
429
+ h('div', { style: { ...S.hint, marginBottom: 8 } },
430
+ `命中 ${result.data.matched} 条${result.data.truncated ? `,只显示前 ${terms.length} 条` : ''}`),
431
+ ...terms.map((term) => h(TermRow, {
432
+ key: term.term_id,
433
+ term,
434
+ editing: editingId === term.term_id,
435
+ onStartEditing: (id) => { setAdding(false); setEditingId(id) },
436
+ onDoneEditing: () => setEditingId(''),
437
+ onUpsert: upsert,
438
+ onDelete: remove,
439
+ })),
440
+ )
441
+ : null,
442
+ result.data !== undefined && query.trim().length === 0 && terms.length === 0
443
+ ? h('div', { style: { ...S.hint, marginTop: 8 } }, '输入关键词开始查;或点上面的「新增术语」。')
444
+ : null,
445
+ ),
446
+ )
447
+ }
448
+
449
+ // -------------------------------------------------------- 论文图页签
450
+
451
+ const TYPE_COLOR = {
452
+ THEOREM: '#7c3aed', COROLLARY: '#7c3aed', LEMMA: '#2563eb', PROPOSITION: '#2563eb',
453
+ DEFINITION: '#0f766e', CONSTRUCTION: '#0f766e', CLAIM: '#b45309', OBSERVATION: '#b45309', REMARK: '#6b7280',
454
+ }
455
+
456
+ function GraphView(props) {
457
+ const graph = props.graph
458
+ const [picked, setPicked] = useState('')
459
+ const nodes = graph.nodes || []
460
+ const byId = useMemo(() => {
461
+ const map = new Map()
462
+ for (const node of nodes) map.set(node.id, node)
463
+ return map
464
+ }, [nodes])
465
+
466
+ const pickedNode = picked === '' ? undefined : byId.get(picked)
467
+ const related = pickedNode === undefined ? [] : (graph.edges || [])
468
+ .filter((edge) => edge.from === picked || edge.to === picked)
469
+ .map((edge) => ({ edge, other: edge.from === picked ? edge.to : edge.from, outward: edge.from === picked }))
470
+
471
+ return h('div', null,
472
+ h('div', { style: { ...S.card, padding: 0, overflow: 'auto', maxHeight: 460, background: 'var(--dsh-surface, #fff)' } },
473
+ h('svg', {
474
+ width: graph.width, height: graph.height,
475
+ style: { display: 'block' },
476
+ viewBox: `0 0 ${graph.width} ${graph.height}`,
477
+ },
478
+ // 边先画,节点后画,避免线压在方块上
479
+ ...(graph.edges || []).filter((edge) => byId.has(edge.from) && byId.has(edge.to)).map((edge, index) => {
480
+ const from = byId.get(edge.from)
481
+ const to = byId.get(edge.to)
482
+ const x1 = from.x + from.width / 2
483
+ const y1 = from.y + from.height
484
+ const x2 = to.x + to.width / 2
485
+ const y2 = to.y
486
+ const mid = (y1 + y2) / 2
487
+ const hot = picked !== '' && (edge.from === picked || edge.to === picked)
488
+ return h('path', {
489
+ key: `e${index}`,
490
+ d: `M${x1} ${y1} C${x1} ${mid} ${x2} ${mid} ${x2} ${y2}`,
491
+ fill: 'none',
492
+ stroke: hot ? '#2563eb' : edge.uncertain ? '#d97706' : '#b6c2d1',
493
+ strokeWidth: hot ? 1.8 : 1.2,
494
+ strokeDasharray: edge.uncertain ? '4 3' : undefined,
495
+ })
496
+ }),
497
+ ...nodes.map((node) => {
498
+ const color = TYPE_COLOR[node.type] || '#6b7280'
499
+ const active = picked === node.id
500
+ return h('g', {
501
+ key: node.id,
502
+ onClick: () => setPicked(active ? '' : node.id),
503
+ style: { cursor: 'pointer' },
504
+ },
505
+ h('rect', {
506
+ x: node.x, y: node.y, width: node.width, height: node.height, rx: 8,
507
+ fill: active ? 'rgba(37,99,235,.1)' : 'var(--dsh-surface, #fff)',
508
+ stroke: color, strokeWidth: node.shared || active ? 2 : 1.1,
509
+ }),
510
+ h('text', { x: node.x + 10, y: node.y + 13, fontSize: 8.5, fill: color, fontFamily: 'ui-sans-serif' },
511
+ `${node.type}${node.shared ? ' · 枢纽' : ''}${node.supportsMainTheorem ? '' : ' · 孤立于主定理'}`),
512
+ h('text', { x: node.x + 10, y: node.y + 26, fontSize: 10.5, fill: 'var(--dsh-text-primary, #111)', fontFamily: 'ui-sans-serif' },
513
+ node.label.length > 24 ? `${node.label.slice(0, 23)}…` : node.label),
514
+ h('text', { x: node.x + 10, y: node.y + 35, fontSize: 8.5, fill: 'var(--dsh-text-tertiary, #999)', fontFamily: 'ui-sans-serif' },
515
+ `in ${node.inDegree} / out ${node.outDegree}`),
516
+ )
517
+ }),
518
+ ),
519
+ ),
520
+ h('div', { style: { ...S.hint, marginTop: 6 } }, '点节点看详情与依赖关系;再点一次取消选中。'),
521
+ pickedNode === undefined
522
+ ? null
523
+ : h('div', { style: { ...S.card, marginTop: 8 } },
524
+ h('div', { style: { fontWeight: 600 } }, `${pickedNode.label} · ${pickedNode.type}`),
525
+ h('div', { style: { ...S.hint, marginTop: 3 } }, pickedNode.statement),
526
+ h('div', { style: { ...S.hint, marginTop: 6 } },
527
+ `被引用 ${pickedNode.inDegree} · 依赖 ${pickedNode.outDegree}` +
528
+ `${pickedNode.shared ? ' · 枢纽节点' : ''}` +
529
+ `${pickedNode.supportsMainTheorem ? ' · 支撑主定理' : ' · 不支撑任何主定理'}`),
530
+ related.length > 0
531
+ ? h('div', { style: { marginTop: 8 } },
532
+ h('div', { style: { fontWeight: 600, marginBottom: 3 } }, '关系'),
533
+ ...related.map((item, index) => h('div', { key: index, style: S.hint },
534
+ `${item.outward ? '依赖' : '被'} ${item.other}${item.edge.uncertain ? '(不确定)' : ''}`)))
535
+ : null,
536
+ ),
537
+ )
538
+ }
539
+
540
+ function PaperTab() {
541
+ const list = useEndpoint('/api/verymath/structures')
542
+ const [selected, setSelected] = useState('')
543
+ const [focus, setFocus] = useState('')
544
+
545
+ const structures = (list.data && list.data.structures) || []
546
+ useEffect(() => {
547
+ if (selected === '' && structures.length > 0) setSelected(structures[0].path)
548
+ }, [structures.length, selected])
549
+
550
+ const endpoint = selected === ''
551
+ ? undefined
552
+ : `/api/verymath/paper?path=${encodeURIComponent(selected)}&focus=${encodeURIComponent(focus)}`
553
+ const result = useEndpoint(endpoint)
554
+
555
+ if (list.status === 'loading' && list.data === undefined) return h(Loading, { label: '正在扫描工作区里的 *_structure.json…' })
556
+ if (list.status === 'failed') return h(Failure, { message: list.error, onRetry: list.reload })
557
+ if (structures.length === 0) {
558
+ return h(Empty, null,
559
+ '工作区里没有 *_structure.json(math-paper-reading 的结构产物)。',
560
+ h('div', { style: { marginTop: 8 } },
561
+ '让 Agent 说一句「读 papers/xxx/fulltext.txt 的第 1 节,把定义提取成结构 JSON」,产物出现后这里会列出来。'))
562
+ }
563
+
564
+ const data = result.data
565
+ return h('div', null,
566
+ h('div', { style: S.toolbar },
567
+ h('select', { value: selected, onChange: (event) => setSelected(event.target.value), style: S.select },
568
+ ...structures.map((item) => h('option', { key: item.path, value: item.path }, `${item.title}(${item.entities} 实体)`))),
569
+ h('input', {
570
+ value: focus, onChange: (event) => setFocus(event.target.value),
571
+ placeholder: '聚焦某条定理看依赖闭包(可留空)', style: { ...S.input, width: 250 },
572
+ }),
573
+ ),
574
+ result.status === 'loading' ? h(Loading, null) : null,
575
+ result.status === 'failed' ? h(Failure, { message: result.error, onRetry: result.reload }) : null,
576
+ result.status === 'ready' && data
577
+ ? h('div', null,
578
+ h('div', { style: S.stat },
579
+ h(Stat, { value: data.entities ?? 0, label: '实体' }),
580
+ h(Stat, { value: (data.sharedNodes || []).length, label: '枢纽' }),
581
+ h(Stat, { value: (data.cycles || []).length, label: '环' }),
582
+ h(Stat, { value: (data.isolated || []).length, label: '孤立' }),
583
+ h(Stat, { value: (data.longestChain || []).length, label: '最长链' }),
584
+ ),
585
+ (data.cycles || []).length > 0
586
+ ? h('div', { style: { ...S.card, borderColor: 'rgba(217,119,6,.5)', background: 'rgba(217,119,6,.07)' } },
587
+ h('div', { style: { fontWeight: 600 } }, '⚠ 依赖图里有环(上游要求人工判断)'),
588
+ ...data.cycles.map((cycle, index) => h('div', { key: index, style: S.hint }, cycle.join(' → '))))
589
+ : null,
590
+ data.focus
591
+ ? h('div', { style: { ...S.card, borderColor: data.focus.found ? undefined : 'rgba(180,83,9,.5)' } },
592
+ h('div', { style: { fontWeight: 600, marginBottom: 3 } },
593
+ data.focus.found ? `聚焦 ${data.focus.label}` : `聚焦 ${data.focus.label}(未找到)`),
594
+ data.focus.found
595
+ ? h('div', { style: S.hint }, '自底向上阅读层:' + data.focus.levels.map((level) => `[${level.join('、')}]`).join(' → '))
596
+ : h('div', { style: S.hint }, '这个标签不在合并后的实体里。'))
597
+ : null,
598
+ data.graph && data.graph.nodes
599
+ ? h(GraphView, { graph: data.graph })
600
+ : null,
601
+ h('div', { style: { ...S.card, marginTop: 10 } },
602
+ h('div', { style: { ...S.hint, marginBottom: 4 } }, '复现顺序(自底向上)'),
603
+ h('div', { style: S.code }, (data.bottomUpLevels || []).map((level) => `[${level.join('、')}]`).join(' → ')),
604
+ h('div', { style: { ...S.hint, marginTop: 8, marginBottom: 4 } }, '阅读顺序(自顶向下)'),
605
+ h('div', { style: S.code }, (data.topDownOrder || []).join(' → ')),
606
+ ),
607
+ )
608
+ : null,
609
+ )
610
+ }
611
+
612
+ // ------------------------------------------------------------ 技能页签
613
+
614
+ function SkillsTab() {
615
+ const { status, data, error, reload } = useEndpoint('/api/verymath/skills')
616
+ if (status === 'loading' && data === undefined) return h(Loading, { label: '读取随包技能…' })
617
+ if (status === 'failed') return h(Failure, { message: error, onRetry: reload })
618
+ const skills = (data && data.skills) || []
619
+ return h('div', null,
620
+ h('div', { style: S.toolbar },
621
+ h('div', { style: { fontSize: 14, fontWeight: 600 } }, `${skills.length} 个随包技能`),
622
+ h('div', { style: S.hint }, '与工具同版本发布'),
623
+ h('button', { onClick: reload, style: { ...S.btn, marginLeft: 'auto' } }, '刷新'),
624
+ ),
625
+ ...skills.map((skill) => h('div', { key: skill.name, style: S.card },
626
+ h('div', { style: { display: 'flex', gap: 8, alignItems: 'baseline' } },
627
+ h('span', { style: { fontWeight: 600 } }, skill.name),
628
+ h('span', { style: { ...S.code, marginLeft: 'auto' } }, `${skill.files} 个文件`)),
629
+ h('div', { style: { ...S.hint, marginTop: 3 } }, skill.description),
630
+ )),
631
+ )
632
+ }
633
+
634
+ // -------------------------------------------------------------- 外壳
635
+
28
636
  function VeryMathIcon(props) {
29
637
  const size = (props && props.size) || 16
30
638
  return h('svg', {
31
- width: size,
32
- height: size,
33
- viewBox: '0 0 16 16',
34
- fill: 'none',
35
- stroke: 'currentColor',
36
- strokeWidth: 1.6,
37
- strokeLinecap: 'round',
38
- 'aria-hidden': 'true',
39
- },
40
- h('path', { d: 'M12 3H4l4 5-4 5h8' }),
41
- )
639
+ width: size, height: size, viewBox: '0 0 16 16', fill: 'none',
640
+ stroke: 'currentColor', strokeWidth: 1.6, strokeLinecap: 'round', 'aria-hidden': 'true',
641
+ }, h('path', { d: 'M12 3H4l4 5-4 5h8' }))
42
642
  }
43
643
 
44
- /** 一行小标题 */
45
- function Section(props) {
46
- return h('div', {
47
- style: {
48
- fontSize: 11,
49
- letterSpacing: '.08em',
50
- textTransform: 'uppercase',
51
- color: 'var(--dsh-text-tertiary, #888)',
52
- margin: '14px 0 6px',
53
- },
54
- }, props.children)
55
- }
56
-
57
- const TOOLS = [
58
- ['verymath_glossary_query', '查双语术语库'],
59
- ['verymath_glossary_upsert', '存词 / 纠正译名'],
60
- ['verymath_glossary_build', '从 CSV 建库(Python)'],
61
- ['verymath_python_check', 'Python 可用性'],
62
- ['verymath_paper_validate', '论文结构 JSON 校验'],
63
- ['verymath_paper_graph', '定理依赖图'],
64
- ['verymath_ocr_scan', 'OCR 后错误定位'],
65
- ['verymath_osqp_check', 'OSQP 求解栈探测'],
66
- ['verymath_osqp_solve', '凸二次规划求解'],
67
- ['verymath_lean_detect_sorry', 'Lean 禁用词扫描'],
68
- ['verymath_sage_check', 'SageMath 探测'],
69
- ['verymath_sage_search', 'Sage 文档反幻觉查询'],
70
- ['verymath_research_gate', '研究工作区三道闸门'],
71
- ['verymath_beamer_check', 'Beamer TeX 工具链'],
644
+ const TABS = [
645
+ { id: 'env', label: '环境' },
646
+ { id: 'glossary', label: '术语库' },
647
+ { id: 'paper', label: '论文图' },
648
+ { id: 'skills', label: '技能' },
72
649
  ]
73
650
 
74
- /** 主面板:能力清单与当前状态。 */
75
651
  function VeryMathPanel() {
76
- return h('div', { style: { padding: '16px 18px', fontSize: 13, lineHeight: 1.6 } },
77
- h('div', { style: { fontSize: 15, fontWeight: 600 } }, 'VeryMath 数学科研'),
78
- h('div', { style: { color: 'var(--dsh-text-secondary, #666)', marginTop: 2 } },
79
- 'VeryMath 能力接入:14 个工具 · 16 个技能'),
80
- h(Section, null, '工具'),
81
- h('div', { style: { display: 'grid', gap: 2 } },
82
- ...TOOLS.map(([name, desc]) => h('div', {
83
- key: name,
84
- style: { display: 'flex', gap: 8, alignItems: 'baseline' },
85
- },
86
- h('code', { style: { fontSize: 11.5, color: 'var(--dsh-text-secondary, #666)' } }, name),
87
- h('span', { style: { fontSize: 12 } }, desc),
88
- )),
652
+ const [tab, setTab] = useState('env')
653
+ return h('div', { style: S.wrap },
654
+ h('style', null, '@keyframes verymath-spin{to{transform:rotate(360deg)}}'),
655
+ h('div', { style: S.head },
656
+ h('div', { style: S.title }, 'VeryMath 数学科研'),
657
+ h('div', { style: S.tabs }, ...TABS.map((item) => h('button', {
658
+ key: item.id, onClick: () => setTab(item.id),
659
+ style: item.id === tab ? { ...S.tab, ...S.tabOn } : S.tab,
660
+ }, item.label))),
661
+ ),
662
+ h('div', { style: S.body },
663
+ tab === 'env' ? h(EnvTab, null) : null,
664
+ tab === 'glossary' ? h(GlossaryTab, null) : null,
665
+ tab === 'paper' ? h(PaperTab, null) : null,
666
+ tab === 'skills' ? h(SkillsTab, null) : null,
89
667
  ),
90
- h(Section, null, '怎么用'),
91
- h('div', { style: { color: 'var(--dsh-text-secondary, #666)' } },
92
- '全部能力都在对话里调用:「帮我查一下 manifold 的中文术语」「读这篇论文的定理依赖」。',
93
- '这个面板只是入口与状态指示,不做计算——计算都在工具里,结果写进会话工作区。'),
94
668
  )
95
669
  }
96
670
 
97
671
  function apply(ctx) {
98
- // 左栏入口(list 槽,每个插件一行)
99
672
  ctx.effect(() => ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
100
673
  name: 'sidebar.panellist',
101
674
  id: PANEL_ID,
@@ -103,7 +676,6 @@ window.__ModuleLoader__.load({
103
676
  label: 'VeryMath 数学科研',
104
677
  }, VeryMathIcon)), 'verymath: sidebar entry')
105
678
 
106
- // 主面板(keyed 槽,键必须与上面 id 一致)
107
679
  ctx.effect(() => ctx.slots.inject('main', () => ctx.slots.register({
108
680
  name: 'main',
109
681
  key: PANEL_ID,