dsh-ruleset 0.0.0-stage → 0.2.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/CHANGELOG.md +36 -0
- package/DEVELOPMENT.md +121 -0
- package/LICENSE +21 -0
- package/README.md +68 -2
- package/README.zh.md +131 -0
- package/client.js +822 -0
- package/cordis.patch.yml +10 -0
- package/docs/PREDICATE-PLAYBOOK.md +42 -0
- package/docs-schema.md +38 -0
- package/examples/policy.example.yaml +50 -0
- package/examples/policy.full.yaml +162 -0
- package/examples/policy.starter.yaml +65 -0
- package/lib/backups.js +77 -0
- package/lib/commands.js +38 -0
- package/lib/dedupe.js +22 -0
- package/lib/explain.js +28 -0
- package/lib/export.js +33 -0
- package/lib/extract.js +104 -0
- package/lib/facts.js +250 -0
- package/lib/findings.js +101 -0
- package/lib/guard.js +76 -0
- package/lib/history.js +71 -0
- package/lib/hygiene.js +86 -0
- package/lib/index.js +70 -0
- package/lib/init.js +29 -0
- package/lib/inject.js +64 -0
- package/lib/injected-log.js +30 -0
- package/lib/introspect.js +55 -0
- package/lib/judge.js +73 -0
- package/lib/llm-parse.js +131 -0
- package/lib/observe.js +132 -0
- package/lib/policy.js +57 -0
- package/lib/predicates.js +444 -0
- package/lib/report.js +66 -0
- package/lib/routes.js +62 -0
- package/lib/rules-edit.js +140 -0
- package/lib/run.js +109 -0
- package/lib/service.js +91 -0
- package/lib/source.js +93 -0
- package/lib/stats.js +63 -0
- package/lib/validate.js +73 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/package.json +68 -3
- package/schema/policy.schema.json +41 -0
- package/test/dedupe.test.mjs +19 -0
- package/test/guard.test.mjs +30 -0
- package/test/load.test.mjs +21 -0
- package/test/predicates.test.mjs +49 -0
- package/test/source.test.mjs +32 -0
- package/test/validate.test.mjs +33 -0
package/client.js
ADDED
|
@@ -0,0 +1,822 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: 'dsh-ruleset',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const React = require('react')
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
const PANEL_ID = 'dsh-ruleset'
|
|
7
|
+
const BUILD = 'ui-210829'
|
|
8
|
+
// 浅色毛玻璃主题:白底、深灰字、荧光红绿标识
|
|
9
|
+
const C = {
|
|
10
|
+
canvas: '#ffffff', surface: 'rgba(255,255,255,0.62)', surfaceHover: 'rgba(255,255,255,0.88)',
|
|
11
|
+
line: 'rgba(20,30,45,0.08)', lineStrong: 'rgba(20,30,45,0.18)',
|
|
12
|
+
text: '#333a44', muted: 'rgba(51,58,68,0.60)',
|
|
13
|
+
pass: '#00c48c', violation: '#ff2e63', warn: '#f0a020', stat: '#7c8db5', unknown: '#9aa4b2', accent: '#0b6bff',
|
|
14
|
+
field: 'rgba(255,255,255,0.75)',
|
|
15
|
+
}
|
|
16
|
+
const glow = (c) => `0 0 6px ${c}30`
|
|
17
|
+
const tint = (c) => `linear-gradient(180deg, ${c}0f, ${c}05)`
|
|
18
|
+
const T = { micro: 10, label: 11, body: 12, base: 13, head: 15, kpi: 22 }
|
|
19
|
+
const RAD = { card: 12, ctrl: 8 }
|
|
20
|
+
|
|
21
|
+
// 宿主命令通道(桌面端 dsh-app:// 下 HTTP 路由可能不可用)
|
|
22
|
+
let remoteCommands = null
|
|
23
|
+
const viaCommand = async (op, args) => {
|
|
24
|
+
if (!remoteCommands?.execute) return null
|
|
25
|
+
try {
|
|
26
|
+
const ac = new AbortController()
|
|
27
|
+
const exec = remoteCommands.execute
|
|
28
|
+
// 兼容两种签名:execute(line, ...) 或 execute(agent, line, ...)
|
|
29
|
+
let out
|
|
30
|
+
try { out = await exec(`/ruleset ${JSON.stringify({ op, args })}`, [], ac.signal) }
|
|
31
|
+
catch (e1) { out = await exec(null, `/ruleset ${JSON.stringify({ op, args })}`, [], ac.signal) }
|
|
32
|
+
const text = out?.result?.text
|
|
33
|
+
if (typeof text !== 'string') return null
|
|
34
|
+
try { return JSON.parse(text) } catch { return null }
|
|
35
|
+
} catch { return null }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const post = async (path, body) => {
|
|
39
|
+
const res = await fetch(path, {
|
|
40
|
+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body || {}),
|
|
41
|
+
})
|
|
42
|
+
const text = await res.text()
|
|
43
|
+
return { res, text }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const opOf = (path) => String(path || '').replace('api/dsh-ruleset/', '')
|
|
47
|
+
const call = async (path, body) => {
|
|
48
|
+
const viaCmd = await viaCommand(opOf(path), body)
|
|
49
|
+
if (viaCmd) return viaCmd
|
|
50
|
+
let { res, text } = await post(path, body)
|
|
51
|
+
// 相对路径在不同 dsh-app:// 基址下可能被吃掉一段,用绝对路径重试一次
|
|
52
|
+
if (!res.ok && !path.startsWith('/')) {
|
|
53
|
+
const retry = await post('/' + path, body)
|
|
54
|
+
if (retry.res.ok) { res = retry.res; text = retry.text }
|
|
55
|
+
else if (retry.res.status !== 404) { res = retry.res; text = retry.text }
|
|
56
|
+
}
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
return { ok: false, error: { code: `http-${res.status}`, message: `${path} → HTTP ${res.status} (${res.url}) : ${text.slice(0, 100)}` } }
|
|
59
|
+
}
|
|
60
|
+
try { return JSON.parse(text) } catch { return { ok: false, error: { code: 'bad-json', message: `${path} 返回非 JSON:${text.slice(0, 100)}` } } }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* ---------- 迷你图 ---------- */
|
|
64
|
+
const Spark = ({ values, color, w = 64, hgt = 22 }) => {
|
|
65
|
+
const v = values.length ? values : [0, 0]
|
|
66
|
+
const max = Math.max(1, ...v)
|
|
67
|
+
const pt = (i) => {
|
|
68
|
+
const x = v.length === 1 ? w / 2 : (i / (v.length - 1)) * (w - 2) + 1
|
|
69
|
+
const y = hgt - 2 - (v[i] / max) * (hgt - 4)
|
|
70
|
+
return [x, y]
|
|
71
|
+
}
|
|
72
|
+
const line = v.map((_, i) => pt(i).map((n) => n.toFixed(1)).join(',')).join(' ')
|
|
73
|
+
const [lx, ly] = pt(v.length - 1)
|
|
74
|
+
return h('svg', { width: w, height: hgt, style: { display: 'block' } },
|
|
75
|
+
h('polyline', { points: line, fill: 'none', stroke: color, strokeWidth: 1.5, strokeLinejoin: 'round', strokeLinecap: 'round' }),
|
|
76
|
+
h('circle', { cx: lx, cy: ly, r: 1.8, fill: color }))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* ---------- 主趋势图 ---------- */
|
|
80
|
+
const TrendChart = ({ series, color }) => {
|
|
81
|
+
const W = 360, H = 84
|
|
82
|
+
const data = series.map((s) => s.summary?.violation ?? 0)
|
|
83
|
+
const max = Math.max(1, ...data)
|
|
84
|
+
const x = (i) => (i / Math.max(1, data.length - 1)) * (W - 2) + 1
|
|
85
|
+
const y = (val) => H - 18 - (val / max) * (H - 26)
|
|
86
|
+
const pts = data.map((val, i) => `${x(i).toFixed(1)},${y(val).toFixed(1)}`)
|
|
87
|
+
const area = `1,${H - 18} ${pts.join(' ')} ${W - 1},${H - 18}`
|
|
88
|
+
const fmt = (iso) => {
|
|
89
|
+
const d = new Date(iso)
|
|
90
|
+
return `${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:00`
|
|
91
|
+
}
|
|
92
|
+
const labels = data.length > 1
|
|
93
|
+
? [{ i: 0, t: fmt(series[0].at) }, { i: data.length - 1, t: fmt(series[data.length - 1].at) }]
|
|
94
|
+
: []
|
|
95
|
+
return h('svg', { width: '100%', viewBox: `0 0 ${W} ${H}`, preserveAspectRatio: 'none', style: { display: 'block' } },
|
|
96
|
+
h('line', { x1: 0, y1: H - 18, x2: W, y2: H - 18, stroke: C.line, strokeWidth: 1 }),
|
|
97
|
+
h('polygon', { points: area, fill: 'url(#rulesetTrend)', opacity: 0.9 }),
|
|
98
|
+
h('linearGradient', { id: 'rulesetTrend', x1: 0, y1: 0, x2: 0, y2: 1 },
|
|
99
|
+
h('stop', { offset: '0%', stopColor: color, stopOpacity: 0.35 }),
|
|
100
|
+
h('stop', { offset: '100%', stopColor: color, stopOpacity: 0 })),
|
|
101
|
+
h('polyline', { points: pts.join(' '), fill: 'none', stroke: color, strokeWidth: 2.6, strokeLinejoin: 'round', strokeLinecap: 'round', style: { filter: `drop-shadow(0 0 3px ${color}66)` } }),
|
|
102
|
+
data.map((val, i) => h('circle', { key: i, cx: x(i), cy: y(val), r: 2.6, fill: color, style: { filter: `drop-shadow(0 0 2px ${color}aa)` } },
|
|
103
|
+
h('title', null, `${new Date(series[i].at).toLocaleString()} · 违规 ${val}`))),
|
|
104
|
+
labels.map((l) => h('text', { key: l.t, x: x(l.i), y: H - 4, fontSize: 9, fill: C.muted, textAnchor: l.i === 0 ? 'start' : 'end' }, l.t)),
|
|
105
|
+
h('text', { x: 1, y: 10, fontSize: 9, fill: C.muted }, `峰值 ${max}`))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* ---------- 一句话 → 判据(复用宿主侧的规则抽取口径) ---------- */
|
|
109
|
+
const SAY = {
|
|
110
|
+
path: '在工作区外删除文件、且没有先确认',
|
|
111
|
+
field: '文件里缺少指定字段',
|
|
112
|
+
count: '同一条命令连续失败后仍在重试',
|
|
113
|
+
outbound: '外发动作没有先确认',
|
|
114
|
+
'search-first': '诊断/开发任务没有先查外部案例就直接上手',
|
|
115
|
+
artifact: '命令里出现特定脚本/命令',
|
|
116
|
+
semantic: '这条属于语义规则,只能提示,无法机械判定',
|
|
117
|
+
process: '暂未识别出可机械判定的判据',
|
|
118
|
+
empty: '还没有描述',
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const DEFAULT_ALTS = [
|
|
122
|
+
{ type: 'field-presence', bind: { type: 'field-presence', ext: '.md', fields: ['sources', 'retrieved_at'] }, say: '记录里必须写清来源列表与检索时间' },
|
|
123
|
+
{ type: 'regex', bind: { type: 'regex', sources: ['fileWrites'], pattern: '(?s)sources?.{0,400}' }, say: '写入内容里必须出现来源段落' },
|
|
124
|
+
{ type: 'count-threshold', bind: { type: 'count-threshold', consecutiveFailures: 2 }, say: '同一检索连续失败后仍在重试' },
|
|
125
|
+
]
|
|
126
|
+
|
|
127
|
+
const slug = (s) => (s || '').toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '')
|
|
128
|
+
|
|
129
|
+
/* ---------- 规则卡片 ---------- */
|
|
130
|
+
const Card = ({ row, series, windowHours, bucket, findings, onTriage, model, open, onToggle, onRemove, onEdit, pending }) => {
|
|
131
|
+
const [hover, setHover] = React.useState(false)
|
|
132
|
+
const color = C[row.status] || C.unknown
|
|
133
|
+
const rates = series.map((s) => s.rules?.[row.id]?.rate ?? 0)
|
|
134
|
+
const rate = row.base ? Math.round(((row.hits || 0) / row.base) * 100) : null
|
|
135
|
+
return h('div', {
|
|
136
|
+
draggable: true,
|
|
137
|
+
onDragStart: (e) => { try { e.dataTransfer.setData('text/plain', row.id); e.dataTransfer.effectAllowed = 'move' } catch { /* ignore */ } },
|
|
138
|
+
onClick: onToggle,
|
|
139
|
+
onMouseEnter: () => setHover(true),
|
|
140
|
+
onMouseLeave: () => setHover(false),
|
|
141
|
+
style: {
|
|
142
|
+
position: 'relative',
|
|
143
|
+
borderStyle: 'solid',
|
|
144
|
+
borderTopWidth: 1, borderRightWidth: 1, borderBottomWidth: 1, borderLeftWidth: 3,
|
|
145
|
+
borderTopColor: hover ? C.lineStrong : C.line,
|
|
146
|
+
borderRightColor: hover ? C.lineStrong : C.line,
|
|
147
|
+
borderBottomColor: hover ? C.lineStrong : C.line,
|
|
148
|
+
borderLeftColor: color,
|
|
149
|
+
borderRadius: RAD.card,
|
|
150
|
+
backgroundImage: `${tint(color)}, ${hover ? 'linear-gradient(rgba(255,255,255,0.9), rgba(255,255,255,0.9))' : 'linear-gradient(rgba(255,255,255,0.55), rgba(255,255,255,0.55))'}`,
|
|
151
|
+
backdropFilter: 'blur(14px) saturate(1.4)', WebkitBackdropFilter: 'blur(14px) saturate(1.4)',
|
|
152
|
+
padding: '11px 12px 10px 13px', cursor: 'pointer',
|
|
153
|
+
boxShadow: hover ? '0 6px 18px rgba(20,30,45,0.10)' : '0 1px 2px rgba(20,30,45,0.05)',
|
|
154
|
+
gridColumn: open ? '1 / -1' : 'auto', transition: 'background .14s, border-color .14s, box-shadow .14s',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 6 } },
|
|
158
|
+
h('span', { style: { fontSize: T.body, fontWeight: 600, flex: 1, lineHeight: 1.35, letterSpacing: '-0.005em' } }, row.title || row.id),
|
|
159
|
+
h('span', { style: { fontSize: T.micro, color, textTransform: 'uppercase', letterSpacing: '0.05em', fontWeight: 500 } }, row.status),
|
|
160
|
+
h('span', {
|
|
161
|
+
onClick: (e) => { e.stopPropagation(); onEdit(row) },
|
|
162
|
+
title: '编辑这条规则',
|
|
163
|
+
style: { fontSize: 11, color: C.muted, cursor: 'pointer', padding: '0 3px' },
|
|
164
|
+
}, '编辑'),
|
|
165
|
+
h('span', {
|
|
166
|
+
onClick: (e) => { e.stopPropagation(); onRemove(row) },
|
|
167
|
+
title: pending ? '再点一次确认删除' : '删除这条规则',
|
|
168
|
+
style: { fontSize: pending ? 10 : 13, color: pending ? C.violation : C.muted, cursor: 'pointer', padding: '0 2px', lineHeight: 1, whiteSpace: 'nowrap' },
|
|
169
|
+
}, pending ? '确认删除?' : '×')),
|
|
170
|
+
h('div', { style: { display: 'flex', alignItems: 'flex-end', gap: 8, marginTop: 6 } },
|
|
171
|
+
h('span', { style: { fontSize: 20, fontWeight: 600, lineHeight: 1, fontVariantNumeric: 'tabular-nums', letterSpacing: '-0.02em' } }, rate === null ? '–' : `${rate}%`),
|
|
172
|
+
h('span', { style: { fontSize: T.micro, color: C.muted, paddingBottom: 2 } }, row.base ? `${row.hits}/${row.base}` : '无样本'),
|
|
173
|
+
row.z !== undefined ? h('span', {
|
|
174
|
+
style: { fontSize: T.micro, paddingBottom: 2, color: row.z < -1.96 ? C.violation : row.z > 1.96 ? C.pass : C.muted },
|
|
175
|
+
}, row.z < -1.96 ? `比基线明显变差(强度 ${Math.abs(row.z)})` : row.z > 1.96 ? `比基线明显变好(强度 ${row.z})` : `与基线无明显变化(强度 ${Math.abs(row.z)})`) : null,
|
|
176
|
+
h('div', { style: { marginLeft: 'auto' } }, rates.length > 1 ? h(Spark, { values: rates, color }) : null)),
|
|
177
|
+
open ? h('div', { style: { marginTop: 8, borderTop: `1px solid ${C.line}`, paddingTop: 8, fontSize: 11, color: C.muted, lineHeight: 1.75 } },
|
|
178
|
+
row.text ? h('div', { style: { marginBottom: 6 } }, h('strong', { style: { color: 'inherit' } }, '规则原文:'), row.text) : null,
|
|
179
|
+
h('div', { style: { marginBottom: 6 } }, h('strong', null, '怎么判:'), row.explain || '(无)'),
|
|
180
|
+
(row.bindTypes || (row.bindType ? [row.bindType] : [])).length
|
|
181
|
+
? h('div', { style: { marginBottom: 6 } }, h('strong', null, '判定方式:'), (row.bindTypes || [row.bindType]).join(' + '))
|
|
182
|
+
: h('div', { style: { marginBottom: 6, color: C.muted } }, '判定方式:无(仅提示,不参与判定)'),
|
|
183
|
+
h('div', { style: { marginBottom: 6 } },
|
|
184
|
+
h('strong', null, '取样周期:'),
|
|
185
|
+
`统计窗口 ${windowHours >= 24 ? `${Math.round(windowHours / 24)} 天` : `${windowHours} 小时`} · 曲线按${bucket === 'week' ? '周' : bucket === 'day' ? '天' : '小时'}聚合 · 当前 ${series.length || 1} 个点`,
|
|
186
|
+
series.length > 1 ? h('div', { style: { marginTop: 6 } }, h(Spark, { values: series.map((x) => x.rules?.[row.id]?.rate ?? 0), color, w: 260, hgt: 42 })) : null,
|
|
187
|
+
series.length > 1 ? h('div', { style: { display: 'flex', justifyContent: 'space-between', fontSize: 9, opacity: 0.7, width: 260 } },
|
|
188
|
+
h('span', null, new Date(series[0].at).toLocaleTimeString()), h('span', null, new Date(series[series.length - 1].at).toLocaleTimeString())) : null),
|
|
189
|
+
row.detail ? h('div', { style: { marginBottom: 6 } }, h('strong', null, '本次结果:'), row.detail) : null,
|
|
190
|
+
row.z !== undefined ? h('div', { style: { marginBottom: 6 } }, h('strong', null, '显著性:'), `z=${row.z}(|z|<1.96 视为与基线无显著差异)`) : null,
|
|
191
|
+
model ? h('div', { style: { marginBottom: 6 } },
|
|
192
|
+
h('strong', null, '模型复核:'),
|
|
193
|
+
h('span', { style: { color: model.verdict === 'violation' ? C.violation : model.verdict === 'pass' ? C.pass : C.muted } }, model.verdict),
|
|
194
|
+
` ${model.reason || ''}`,
|
|
195
|
+
model.evidence ? h('div', { style: { fontSize: 10, opacity: 0.75 } }, model.evidence) : null) : null,
|
|
196
|
+
(findings || []).filter((f) => f.ruleId === row.id && f.status === 'open').length ? h('div', { style: { marginBottom: 6 } },
|
|
197
|
+
h('strong', null, '待处理发现:'),
|
|
198
|
+
findings.filter((f) => f.ruleId === row.id && f.status === 'open').slice(0, 8).map((f) => h('div', { key: f.key, title: f.evidence || '', style: { display: 'flex', gap: 6, alignItems: 'baseline', padding: '3px 0' } },
|
|
199
|
+
h('span', { style: { fontSize: 9, padding: '0 6px', borderRadius: 999, border: `1px solid ${f.level === 'red' ? C.violation : '#e8a33d'}`, color: f.level === 'red' ? C.violation : '#b8791f' } }, f.level),
|
|
200
|
+
h('span', { style: { fontSize: 9, padding: '0 6px', borderRadius: 999, background: f.status === 'open' ? 'rgba(255,46,99,0.10)' : 'rgba(0,196,140,0.12)', color: f.status === 'open' ? C.violation : C.pass } }, f.status),
|
|
201
|
+
h('span', { style: { flex: 1, fontSize: 10.5, lineHeight: 1.5, opacity: 0.9, whiteSpace: 'normal', wordBreak: 'break-word' } },
|
|
202
|
+
String(f.evidence || '').split(' · ').map((seg, si) => h('span', {
|
|
203
|
+
key: si,
|
|
204
|
+
style: si === 0 ? { color: C.muted, marginRight: 6 } : { marginRight: 6 },
|
|
205
|
+
}, seg))),
|
|
206
|
+
h('button', { onClick: (e) => { e.stopPropagation(); onTriage(f.key, 'false_positive') }, style: { fontSize: 9, padding: '1px 6px', borderRadius: 5, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '误报'),
|
|
207
|
+
h('button', { onClick: (e) => { e.stopPropagation(); onTriage(f.key, 'fixed') }, style: { fontSize: 9, padding: '1px 6px', borderRadius: 5, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '已修'),
|
|
208
|
+
h('button', { onClick: (e) => { e.stopPropagation(); onTriage(f.key, 'accepted') }, style: { fontSize: 9, padding: '1px 6px', borderRadius: 5, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '接受')))) : null,
|
|
209
|
+
(row.samples || []).length ? h('div', null, h('strong', null, '证据:'),
|
|
210
|
+
(row.samples || []).slice(0, 3).map((s, i) => h('div', { key: i, style: { fontFamily: 'ui-monospace, monospace', fontSize: 10, opacity: 0.85 } },
|
|
211
|
+
`· ${s.id || s.file || s.seq || ''} ${s.session ? `(${s.session})` : ''}`))) : null,
|
|
212
|
+
) : null,
|
|
213
|
+
)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* ---------- 新增规则卡片:模型拆解 + 手调 ---------- */
|
|
217
|
+
const FALLBACK_TYPES = [
|
|
218
|
+
{ type: 'path-boundary', say: '命令删除了工作区外的路径且没先确认' },
|
|
219
|
+
{ type: 'field-presence', say: '文件里缺少指定字段' },
|
|
220
|
+
{ type: 'count-threshold', say: '同一命令连续失败后仍在重试' },
|
|
221
|
+
{ type: 'turn-condition', say: '长任务里没有出现指定动作' },
|
|
222
|
+
{ type: 'sequence-order', say: '诊断/开发任务没先查外部就动手' },
|
|
223
|
+
{ type: 'requires', say: '做了 X 就必须有 Y' },
|
|
224
|
+
{ type: 'duration', say: '单次调用阻塞超过阈值' },
|
|
225
|
+
{ type: 'metric', say: '指标超过阈值' },
|
|
226
|
+
{ type: 'regex', say: '命令或内容里出现指定模式' },
|
|
227
|
+
]
|
|
228
|
+
|
|
229
|
+
const defaultBind = (type) => {
|
|
230
|
+
switch (type) {
|
|
231
|
+
case 'path-boundary': return { type, pattern: '', requireApproval: true }
|
|
232
|
+
case 'field-presence': return { type, ext: '.md', fields: [] }
|
|
233
|
+
case 'explain-first': return { type }
|
|
234
|
+
case 'count-threshold': return { type, consecutiveFailures: 2 }
|
|
235
|
+
case 'turn-condition': return { type, ifTurnCallMin: null, thenPattern: '' }
|
|
236
|
+
case 'sequence-order': return { type, taskPattern: '', first: ['web_search', 'web_fetch'], then: ['read', 'grep', 'glob'], minSamples: 10 }
|
|
237
|
+
case 'requires': return { type, ifContentPattern: '', minSamples: 1 }
|
|
238
|
+
case 'duration': return { type, minMinutes: 10 }
|
|
239
|
+
case 'metric': return { type, key: 'context_ratio', op: '<=', threshold: 0.4 }
|
|
240
|
+
default: return { type: 'regex', sources: ['commands'], pattern: '' }
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function ParamFields({ bind, setBind, input }) {
|
|
245
|
+
const setP = (k, v) => setBind({ ...(bind || {}), [k]: v })
|
|
246
|
+
const row = (label, node, help) => h('label', { style: { display: 'block', marginTop: 8 } },
|
|
247
|
+
h('span', { style: { fontSize: 11, color: C.muted } }, label),
|
|
248
|
+
h('div', { style: { marginTop: 3 } }, node),
|
|
249
|
+
help ? h('span', { style: { fontSize: 10, color: C.muted } }, help) : null)
|
|
250
|
+
if (!bind) return null
|
|
251
|
+
switch (bind.type) {
|
|
252
|
+
case 'explain-first':
|
|
253
|
+
return h('div', { style: { fontSize: 11, color: C.muted, marginTop: 8 } }, '无需参数:检查每轮第一次调用工具前是否有解释文本')
|
|
254
|
+
case 'path-boundary':
|
|
255
|
+
return h('div', null,
|
|
256
|
+
row('匹配的命令模式(正则)', h('input', { value: bind.pattern || '', onChange: (e) => setP('pattern', e.target.value), style: input })),
|
|
257
|
+
row('豁免路径(逗号分隔,可空)', h('input', { value: (bind.exemptPaths || []).join(','), onChange: (e) => setP('exemptPaths', e.target.value.split(',').map((x) => x.trim()).filter(Boolean)), style: input })))
|
|
258
|
+
case 'field-presence':
|
|
259
|
+
return row('必须出现的字段(逗号分隔)', h('input', { value: (bind.fields || []).join(','), onChange: (e) => setP('fields', e.target.value.split(',').map((x) => x.trim()).filter(Boolean)), style: input }))
|
|
260
|
+
case 'count-threshold':
|
|
261
|
+
return row('连续失败几次算违规', h('input', { type: 'number', value: bind.consecutiveFailures ?? 2, onChange: (e) => setP('consecutiveFailures', Number(e.target.value)), style: input }))
|
|
262
|
+
case 'turn-condition':
|
|
263
|
+
return h('div', null,
|
|
264
|
+
row('单轮调用超过几次算长任务', h('input', { type: 'number', value: bind.ifTurnCallMin ?? 12, onChange: (e) => setP('ifTurnCallMin', Number(e.target.value)), style: input })),
|
|
265
|
+
row('必须出现的动作(正则)', h('input', { value: bind.thenPattern || '', onChange: (e) => setP('thenPattern', e.target.value), style: input })))
|
|
266
|
+
case 'sequence-order':
|
|
267
|
+
return h('div', null,
|
|
268
|
+
row('什么算“该先检索”的任务(正则)', h('input', { value: bind.taskPattern || '', onChange: (e) => setP('taskPattern', e.target.value), style: input })),
|
|
269
|
+
row('至少多少次任务才判定', h('input', { type: 'number', value: bind.minSamples ?? 10, onChange: (e) => setP('minSamples', Number(e.target.value)), style: input })))
|
|
270
|
+
case 'requires':
|
|
271
|
+
return row('触发条件(正则)', h('input', { value: bind.ifPathPattern || bind.ifContentPattern || '', onChange: (e) => setP('ifContentPattern', e.target.value), style: input }))
|
|
272
|
+
case 'duration':
|
|
273
|
+
return row('超过多少分钟算阻塞', h('input', { type: 'number', value: bind.minMinutes ?? 10, onChange: (e) => setP('minMinutes', Number(e.target.value)), style: input }))
|
|
274
|
+
case 'metric':
|
|
275
|
+
return row('阈值(0-1,例如 0.4 = 40%)', h('input', { type: 'number', step: '0.05', value: bind.threshold ?? 0.4, onChange: (e) => setP('threshold', Number(e.target.value)), style: input }))
|
|
276
|
+
default:
|
|
277
|
+
return h('div', null,
|
|
278
|
+
row('匹配模式(正则)', h('input', { value: bind.pattern || '', onChange: (e) => setP('pattern', e.target.value), style: input })),
|
|
279
|
+
row('匹配范围', h('select', { value: (bind.sources || ['commands'])[0], onChange: (e) => setP('sources', [e.target.value]), style: input },
|
|
280
|
+
h('option', { value: 'commands' }, '命令'), h('option', { value: 'fileWrites' }, '写入的内容'))))
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function AddCard({ onDone, preds, tier }) {
|
|
285
|
+
const [open, setOpen] = React.useState(false)
|
|
286
|
+
const [text, setText] = React.useState('')
|
|
287
|
+
const [name, setName] = React.useState('')
|
|
288
|
+
const [bind, setBind] = React.useState(null)
|
|
289
|
+
const [by, setBy] = React.useState('')
|
|
290
|
+
const [msg, setMsg] = React.useState('')
|
|
291
|
+
const [loading, setLoading] = React.useState(false)
|
|
292
|
+
const [ask, setAsk] = React.useState(null)
|
|
293
|
+
const [adv, setAdv] = React.useState(false)
|
|
294
|
+
const [alts, setAlts] = React.useState([])
|
|
295
|
+
const [extra, setExtra] = React.useState([])
|
|
296
|
+
const [custom, setCustom] = React.useState('')
|
|
297
|
+
|
|
298
|
+
React.useEffect(() => {
|
|
299
|
+
if (!text.trim()) { setBind(null); setBy(''); return }
|
|
300
|
+
const t = setTimeout(async () => {
|
|
301
|
+
setLoading(true)
|
|
302
|
+
try {
|
|
303
|
+
const r = await call('api/dsh-ruleset/parse', { text })
|
|
304
|
+
if (r.ok) {
|
|
305
|
+
setBind(r.value.bind || defaultBind('regex'))
|
|
306
|
+
setBy(r.value.by || '')
|
|
307
|
+
setAsk(r.value.question || (r.value.missing?.length ? `需要补充:${r.value.missing.join(', ')}` : null))
|
|
308
|
+
setAlts(r.value.alternatives || [])
|
|
309
|
+
setMsg('')
|
|
310
|
+
}
|
|
311
|
+
} catch { /* 忽略 */ } finally { setLoading(false) }
|
|
312
|
+
}, 350)
|
|
313
|
+
return () => clearTimeout(t)
|
|
314
|
+
}, [text])
|
|
315
|
+
|
|
316
|
+
const setP = (k, v) => setBind({ ...(bind || {}), [k]: v })
|
|
317
|
+
const input = { width: '100%', boxSizing: 'border-box', fontSize: 12, padding: '4px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit' }
|
|
318
|
+
const row = (label, node, help) => h('label', { style: { display: 'block', marginTop: 8 } },
|
|
319
|
+
h('span', { style: { fontSize: 11, color: C.muted } }, label),
|
|
320
|
+
h('div', { style: { marginTop: 3 } }, node),
|
|
321
|
+
help ? h('span', { style: { fontSize: 10, color: C.muted } }, help) : null)
|
|
322
|
+
|
|
323
|
+
const sig = (b) => JSON.stringify(b, Object.keys(b || {}).sort())
|
|
324
|
+
const pushBind = (b) => {
|
|
325
|
+
if (!b || !b.type) return
|
|
326
|
+
setExtra((list) => {
|
|
327
|
+
const all = [bind, ...list].filter((x) => x && x.type).map(sig)
|
|
328
|
+
if (all.includes(sig(b))) return list
|
|
329
|
+
return [...list, b]
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const paramFields = () => h(ParamFields, { bind, setBind, input })
|
|
334
|
+
const currentBinds = () => {
|
|
335
|
+
const seen = new Set()
|
|
336
|
+
return [bind, ...extra].filter((b) => {
|
|
337
|
+
if (!b || !b.type) return false
|
|
338
|
+
const k = JSON.stringify(b, Object.keys(b).sort())
|
|
339
|
+
if (seen.has(k)) return false
|
|
340
|
+
seen.add(k)
|
|
341
|
+
return true
|
|
342
|
+
})
|
|
343
|
+
}
|
|
344
|
+
const list0 = () => currentBinds().length
|
|
345
|
+
|
|
346
|
+
const save = async (asPromptOnly) => {
|
|
347
|
+
if (!text.trim()) return setMsg('先用一句话描述规则')
|
|
348
|
+
const id = `rule-${Date.now().toString(36)}`
|
|
349
|
+
const list = asPromptOnly ? [] : currentBinds()
|
|
350
|
+
const r = await call('api/dsh-ruleset/add-rule', {
|
|
351
|
+
id, title: name || text.slice(0, 30), text: text.slice(0, 200), binds: list, tier,
|
|
352
|
+
})
|
|
353
|
+
if (r.ok) {
|
|
354
|
+
setOpen(false); setText(''); setBind(null); setExtra([]); setName(''); setAsk(null); setMsg('')
|
|
355
|
+
onDone()
|
|
356
|
+
} else setMsg(r.error?.message || '保存失败')
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (!open) {
|
|
360
|
+
return h('div', {
|
|
361
|
+
onClick: () => setOpen(true),
|
|
362
|
+
style: { border: `1px dashed ${C.line}`, borderRadius: RAD.card, minHeight: 96, display: 'flex', alignItems: 'center', justifyContent: 'center', flexDirection: 'column', gap: 4, cursor: 'pointer', color: C.muted, transition: 'background .12s' },
|
|
363
|
+
}, h('div', { style: { fontSize: 22, lineHeight: 1 } }, '+'), h('div', { style: { fontSize: 11 } }, '加规则'))
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return h('div', { style: { border: `1px solid ${C.line}`, borderRadius: 10, background: C.surface, padding: 12, gridColumn: '1 / -1' } },
|
|
367
|
+
h('div', { style: { fontSize: 12, fontWeight: 600, marginBottom: 6 } }, '用一句话描述这条规则'),
|
|
368
|
+
h('textarea', {
|
|
369
|
+
value: text, onChange: (e) => setText(e.target.value), rows: 2,
|
|
370
|
+
placeholder: '例:在工作区外删除文件必须先问我;导出报告必须带 type 字段;上下文超过 50% 要提醒我',
|
|
371
|
+
style: { ...input, resize: 'vertical', padding: 8 },
|
|
372
|
+
}),
|
|
373
|
+
loading ? h('div', { style: { fontSize: 11, color: C.muted, marginTop: 6 } }, '正在理解这句话…') : null,
|
|
374
|
+
ask && !alts.length ? h('div', { style: { fontSize: 11, color: C.violation, marginTop: 6 } }, `需要你确认:${ask}`) : null,
|
|
375
|
+
bind ? h('div', { style: { marginTop: 8, borderTop: `1px solid ${C.line}`, paddingTop: 8 } },
|
|
376
|
+
h('div', { style: { fontSize: T.micro, letterSpacing: '0.05em', textTransform: 'uppercase', color: C.muted } }, '模型选择的判定方式'),
|
|
377
|
+
h('div', { style: { fontSize: 10, color: C.muted, marginTop: 2 } }, by === 'llm' ? '由模型理解原句后选择' : '模型不可用,规则匹配兜底'),
|
|
378
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, marginTop: 6 } },
|
|
379
|
+
h('div', { style: { fontSize: T.body, fontWeight: 600, color: C.accent, textShadow: glow(C.accent) } }, bind.type),
|
|
380
|
+
h('div', { style: { fontSize: T.label, color: C.muted } },
|
|
381
|
+
((preds && preds.length ? preds : FALLBACK_TYPES).find((t) => t.type === bind.type) || {}).say || '')),
|
|
382
|
+
h('button', {
|
|
383
|
+
onClick: () => setAdv(!adv),
|
|
384
|
+
style: { marginTop: 6, fontSize: 10, padding: '2px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
385
|
+
}, adv ? '收起手动指定' : '高级:手动指定判定方式'),
|
|
386
|
+
adv ? h('select', { value: bind.type, onChange: (e) => setBind(defaultBind(e.target.value)), style: { ...input, marginTop: 6 } },
|
|
387
|
+
(preds && preds.length ? preds : FALLBACK_TYPES).map((t) => h('option', { key: t.type, value: t.type }, `${t.type} —— ${t.say}`))) : null,
|
|
388
|
+
paramFields(),
|
|
389
|
+
row('规则名(显示在卡片上,可中文)', h('input', { value: name, onChange: (e) => setName(e.target.value), placeholder: text.slice(0, 20), style: input }))) : null,
|
|
390
|
+
(alts.length || (!bind && text.trim())) ? h('div', { style: { marginTop: 10, borderTop: `1px solid ${C.line}`, paddingTop: 8 } },
|
|
391
|
+
h('div', { style: { fontSize: T.label, color: C.muted, marginBottom: 6 } },
|
|
392
|
+
alts.length ? '模型建议这样拆成可检查的规则:' : '模型没给出可检查判据,这里有几条常见拆法(点一下采用):'),
|
|
393
|
+
(alts.length ? alts : DEFAULT_ALTS).map((a, i) => h('div', { key: i, style: { display: 'flex', alignItems: 'baseline', gap: 8, padding: '5px 0' } },
|
|
394
|
+
h('button', { onClick: () => { setBind(a.bind) }, title: '设为主判据(会替换当前选中的判定方式)', style: { fontSize: 11, padding: '2px 10px', borderRadius: 999, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer' } }, `设为主判据 · ${a.type}`),
|
|
395
|
+
h('span', { style: { fontSize: T.label, color: C.text } }, a.say || ''),
|
|
396
|
+
h('button', { onClick: () => pushBind(a.bind), style: { fontSize: 10, padding: '1px 7px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '+ 追加'))),
|
|
397
|
+
h('div', { style: { marginTop: 8 } },
|
|
398
|
+
h('div', { style: { fontSize: T.label, color: C.muted } }, '其他:自己描述要检查什么'),
|
|
399
|
+
h('input', { value: custom, onChange: (e) => setCustom(e.target.value), placeholder: '例:记录里必须出现 sources 和 retrieved_at 字段', style: { ...input, marginTop: 3 } }),
|
|
400
|
+
h('button', {
|
|
401
|
+
onClick: async () => {
|
|
402
|
+
if (!custom.trim()) return
|
|
403
|
+
const r = await call('api/dsh-ruleset/parse', { text: custom })
|
|
404
|
+
if (r.ok && r.value.bind) pushBind(r.value.bind)
|
|
405
|
+
else setMsg('这句没解析出判据,换个说法或点上面的类型')
|
|
406
|
+
},
|
|
407
|
+
style: { marginTop: 6, fontSize: 11, padding: '3px 10px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit', cursor: 'pointer' },
|
|
408
|
+
}, '解析并追加'))) : null,
|
|
409
|
+
false ? h('div', null,
|
|
410
|
+
h('div', { style: { fontSize: T.label, color: C.muted, marginBottom: 6 } }, '模型建议这样拆成可检查的规则:'),
|
|
411
|
+
[].map((a, i) => h('div', { key: i, style: { display: 'flex', alignItems: 'baseline', gap: 8, padding: '5px 0' } },
|
|
412
|
+
h('button', {
|
|
413
|
+
onClick: () => { setBind(a.bind); setAdv(false); setExtra((x) => [...x, a.bind]) },
|
|
414
|
+
style: { fontSize: 11, padding: '2px 10px', borderRadius: 999, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer' },
|
|
415
|
+
}, a.type),
|
|
416
|
+
h('span', { style: { fontSize: T.label, color: C.text } }, a.say || ''),
|
|
417
|
+
h('button', { onClick: () => pushBind(a.bind), style: { fontSize: 10, padding: '1px 7px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '+ 追加'),
|
|
418
|
+
a.why ? h('span', { style: { fontSize: 10, color: C.muted } }, a.why) : null))) : null,
|
|
419
|
+
list0() ? h('div', { style: { marginTop: 8, fontSize: 11, color: C.muted } },
|
|
420
|
+
`将保存 ${list0()} 个判据:`, currentBinds().map((b, i) => h('span', { key: i, style: { marginRight: 8, color: C.accent } }, b.type))) : null,
|
|
421
|
+
h('div', { style: { display: 'flex', gap: 8, alignItems: 'center', marginTop: 10 } },
|
|
422
|
+
h('button', { onClick: () => save(false), style: { fontSize: 12, padding: '4px 12px' }, disabled: loading }, list0(0) ? `保存(${list0(0)} 个判据)` : '保存'),
|
|
423
|
+
h('button', { onClick: () => save(true), style: { fontSize: 11, padding: '4px 10px', borderRadius: 6, border: `1px dashed ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '仅作提示(不判定)'),
|
|
424
|
+
h('button', { onClick: () => { setOpen(false); setText(''); setBind(null); setName('') }, style: { fontSize: 12, padding: '4px 12px' } }, '取消'),
|
|
425
|
+
h('span', { style: { fontSize: 11, color: msg ? C.violation : C.muted } }, msg || '保存前自动备份 policy.yaml')),
|
|
426
|
+
)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
function EditCard({ row, preds, onDone, onCancel }) {
|
|
430
|
+
const [title, setTitle] = React.useState(row.title || '')
|
|
431
|
+
const [text, setText] = React.useState(row.text || row.title || '')
|
|
432
|
+
const initialBinds = (Array.isArray(row.binds) && row.binds.length ? row.binds : (row.bind ? [row.bind] : []))
|
|
433
|
+
const [bindList, setBindList] = React.useState(initialBinds.length ? initialBinds : [defaultBind(row.predicate || 'regex')])
|
|
434
|
+
const bind = bindList[0]
|
|
435
|
+
const setBind = (b) => setBindList((l) => [b, ...l.slice(1)])
|
|
436
|
+
const [msg, setMsg] = React.useState('')
|
|
437
|
+
const [busy, setBusy] = React.useState(false)
|
|
438
|
+
const [adv, setAdv] = React.useState(false)
|
|
439
|
+
const [hint, setHint] = React.useState('')
|
|
440
|
+
const [sugg, setSugg] = React.useState([])
|
|
441
|
+
const [intro, setIntro] = React.useState(null)
|
|
442
|
+
const [introBusy, setIntroBusy] = React.useState(false)
|
|
443
|
+
const [ask2, setAsk2] = React.useState('')
|
|
444
|
+
|
|
445
|
+
React.useEffect(() => {
|
|
446
|
+
if (!text || text.length < 6) { setSugg([]); return }
|
|
447
|
+
const t = setTimeout(async () => {
|
|
448
|
+
try {
|
|
449
|
+
const r = await call('api/dsh-ruleset/parse', { text })
|
|
450
|
+
if (r.ok) { setSugg(r.value.alternatives || []); setAsk2(r.value.question || '') }
|
|
451
|
+
} catch { /* ignore */ }
|
|
452
|
+
}, 500)
|
|
453
|
+
return () => clearTimeout(t)
|
|
454
|
+
}, [text])
|
|
455
|
+
const input = { width: '100%', boxSizing: 'border-box', fontSize: 12, padding: '4px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit' }
|
|
456
|
+
|
|
457
|
+
const save = async () => {
|
|
458
|
+
setBusy(true); setMsg('')
|
|
459
|
+
try {
|
|
460
|
+
const r = await call('api/dsh-ruleset/update-rule', { id: row.id, title, text, binds: bindList })
|
|
461
|
+
if (r.ok) onDone(); else setMsg(r.error?.message || '保存失败')
|
|
462
|
+
} catch (e) { setMsg(String(e)) } finally { setBusy(false) }
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
return h('div', { style: { border: `1px solid ${C.line}`, borderRadius: 10, background: C.surface, padding: 12, marginBottom: 12, gridColumn: '1 / -1' } },
|
|
466
|
+
h('div', { style: { fontSize: 12, fontWeight: 600, marginBottom: 6 } }, `编辑规则 · ${row.id}`),
|
|
467
|
+
h('label', { style: { display: 'block' } }, h('span', { style: { fontSize: 11, color: C.muted } }, '规则名'),
|
|
468
|
+
h('input', { value: title, onChange: (e) => setTitle(e.target.value), style: { ...input, marginTop: 3 } })),
|
|
469
|
+
h('label', { style: { display: 'block', marginTop: 8 } }, h('span', { style: { fontSize: 11, color: C.muted } }, '规则正文(会注入到提示词)'),
|
|
470
|
+
h('textarea', { value: text, onChange: (e) => setText(e.target.value), rows: 3, style: { ...input, marginTop: 3, resize: 'vertical', padding: 8 } })),
|
|
471
|
+
h('div', { style: { marginTop: 10 } },
|
|
472
|
+
h('div', { style: { fontSize: T.micro, letterSpacing: '0.05em', textTransform: 'uppercase', color: C.muted } }, '判定方式'),
|
|
473
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, marginTop: 4 } },
|
|
474
|
+
h('div', { style: { fontSize: T.base, fontWeight: 600, color: C.accent, textShadow: glow(C.accent) } }, bind?.type || '未设置'),
|
|
475
|
+
h('div', { style: { fontSize: T.label, color: C.muted } },
|
|
476
|
+
((preds && preds.length ? preds : FALLBACK_TYPES).find((t) => t.type === bind?.type) || {}).say || '当前没有机械判据')),
|
|
477
|
+
h('button', {
|
|
478
|
+
onClick: () => setAdv(!adv),
|
|
479
|
+
style: { marginTop: 6, fontSize: 10, padding: '2px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
480
|
+
}, adv ? '收起手动指定' : '高级:手动指定判定方式'),
|
|
481
|
+
adv ? h('select', { value: bind?.type || 'regex', onChange: (e) => setBind(defaultBind(e.target.value)), style: { ...input, marginTop: 6 } },
|
|
482
|
+
(preds && preds.length ? preds : FALLBACK_TYPES).map((t) => h('option', { key: t.type, value: t.type }, `${t.type} —— ${t.say}`))) : null,
|
|
483
|
+
(sugg.length || ask2) ? h('div', { style: { marginTop: 10, borderTop: `1px solid ${C.line}`, paddingTop: 8 } },
|
|
484
|
+
h('div', { style: { fontSize: T.label, color: C.muted, marginBottom: 6 } },
|
|
485
|
+
sugg.length ? '按当前正文,模型建议这样判:' : '模型建议:'),
|
|
486
|
+
ask2 && !sugg.length ? h('div', { style: { fontSize: T.label, color: C.violation } }, ask2) : null,
|
|
487
|
+
sugg.map((a, i) => h('div', { key: i, style: { display: 'flex', alignItems: 'baseline', gap: 8, padding: '4px 0' } },
|
|
488
|
+
h('button', { onClick: () => setBindList([a.bind]), title: '替换全部判据', style: { fontSize: 10, padding: '1px 8px', borderRadius: 999, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer' } }, `设为唯一判据 · ${a.type}`),
|
|
489
|
+
h('button', { onClick: () => setBindList((l) => [...l, a.bind]), title: '并联追加', style: { fontSize: 10, padding: '1px 8px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' } }, '+ 并联'),
|
|
490
|
+
h('span', { style: { fontSize: T.label, color: C.text } }, a.say || '')))) : null,
|
|
491
|
+
h('div', { style: { marginTop: 8 } },
|
|
492
|
+
h('button', {
|
|
493
|
+
onClick: async () => {
|
|
494
|
+
setIntroBusy(true)
|
|
495
|
+
try { const r = await call('api/dsh-ruleset/introspect', { id: row.id }); if (r.ok) setIntro((r.value.items || [])[0]) } catch { /* ignore */ } finally { setIntroBusy(false) }
|
|
496
|
+
},
|
|
497
|
+
style: { fontSize: 11, padding: '3px 10px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit', cursor: 'pointer' },
|
|
498
|
+
}, introBusy ? '理解中…' : '让模型理解这条规则'),
|
|
499
|
+
intro ? h('div', { style: { marginTop: 6, fontSize: 11, color: C.muted, lineHeight: 1.7 } },
|
|
500
|
+
intro.error ? `失败:${intro.error}` : h('div', null,
|
|
501
|
+
h('div', null, `对象:${intro.object}`),
|
|
502
|
+
h('div', null, `触发:${intro.trigger}`),
|
|
503
|
+
h('div', null, `期望:${intro.expectation}`),
|
|
504
|
+
h('div', null, `例外:${intro.exceptions || '(无)'}`),
|
|
505
|
+
h('div', null, `可机械化:${intro.mechanical ? '是' : '否'}${intro.bind ? `(建议 ${intro.bind.type})` : ''}`),
|
|
506
|
+
intro.bind ? h('button', { onClick: () => setBindList([intro.bind]), style: { fontSize: 10, padding: '1px 8px', borderRadius: 999, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer', marginTop: 4 } }, '采用建议判据') : null)) : null),
|
|
507
|
+
h('div', { style: { marginTop: 8 } },
|
|
508
|
+
h('span', { style: { fontSize: T.label, color: C.muted } }, '不满意?用一句话改:'),
|
|
509
|
+
h('input', { value: hint, onChange: (e) => setHint(e.target.value), placeholder: '例:改成检查命令里出现 rm -rf', style: { ...input, marginTop: 3 } }),
|
|
510
|
+
h('button', {
|
|
511
|
+
onClick: async () => {
|
|
512
|
+
if (!hint.trim()) return
|
|
513
|
+
const r = await call('api/dsh-ruleset/parse', { text: hint })
|
|
514
|
+
if (r.ok) { setBind(r.value.bind || bind); setMsg(r.value.explain || '已按模型理解更新') } else setMsg('解析失败')
|
|
515
|
+
},
|
|
516
|
+
style: { marginTop: 6, fontSize: 11, padding: '3px 10px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit', cursor: 'pointer' },
|
|
517
|
+
}, '按这句话重判'))),
|
|
518
|
+
bindList.map((b, i) => h('div', { key: i, style: { marginTop: 10, paddingTop: 8, borderTop: `1px dashed ${C.line}` } },
|
|
519
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8 } },
|
|
520
|
+
h('span', { style: { fontSize: T.body, fontWeight: 600, color: C.accent } }, `${i === 0 ? '主判据' : `并联 ${i}`} · ${b.type}`),
|
|
521
|
+
bindList.length > 1 ? h('button', {
|
|
522
|
+
onClick: () => setBindList((l) => l.filter((_, j) => j !== i)),
|
|
523
|
+
style: { marginLeft: 'auto', fontSize: 10, padding: '1px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
524
|
+
}, '移除') : null),
|
|
525
|
+
h(ParamFields, { bind: b, setBind: (nb) => setBindList((l) => l.map((x, j) => (j === i ? nb : x))), input }))),
|
|
526
|
+
h('div', { style: { display: 'flex', gap: 8, alignItems: 'center', marginTop: 10 } },
|
|
527
|
+
h('button', { onClick: save, disabled: busy, style: { fontSize: 12, padding: '4px 12px' } }, busy ? '…' : '保存修改'),
|
|
528
|
+
h('button', { onClick: onCancel, style: { fontSize: 12, padding: '4px 12px' } }, '取消'),
|
|
529
|
+
h('span', { style: { fontSize: 11, color: msg ? C.violation : C.muted } }, msg || '保存前自动备份 policy.yaml')),
|
|
530
|
+
)
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function PanelIcon({ size = 16 }) {
|
|
534
|
+
return h('svg', { width: size, height: size, viewBox: '0 0 16 16', 'aria-hidden': true },
|
|
535
|
+
h('rect', { x: 2, y: 2, width: 12, height: 12, rx: 2, fill: 'none', stroke: 'currentColor', strokeWidth: 1.4 }),
|
|
536
|
+
h('path', { d: 'M4.5 8.2l2 2 5-4.4', fill: 'none', stroke: 'currentColor', strokeWidth: 1.4, strokeLinecap: 'round' }))
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function Panel() {
|
|
540
|
+
const [data, setData] = React.useState(null)
|
|
541
|
+
const [err, setErr] = React.useState('')
|
|
542
|
+
const [busy, setBusy] = React.useState('')
|
|
543
|
+
const [open, setOpen] = React.useState({})
|
|
544
|
+
const [preds, setPreds] = React.useState(FALLBACK_TYPES)
|
|
545
|
+
const [pending, setPending] = React.useState('')
|
|
546
|
+
const [bucket, setBucket] = React.useState('hour')
|
|
547
|
+
const [judge, setJudge] = React.useState(null)
|
|
548
|
+
const [sugg, setSugg] = React.useState(null)
|
|
549
|
+
const [hyg, setHyg] = React.useState(null)
|
|
550
|
+
const [showGuard, setShowGuard] = React.useState(false)
|
|
551
|
+
const [showBackups, setShowBackups] = React.useState(false)
|
|
552
|
+
const [backups, setBackups] = React.useState([])
|
|
553
|
+
const [base, setBase] = React.useState(null)
|
|
554
|
+
const [judging, setJudging] = React.useState(false)
|
|
555
|
+
const [rangeH, setRangeH] = React.useState(168)
|
|
556
|
+
const [editing, setEditing] = React.useState(null)
|
|
557
|
+
|
|
558
|
+
const load = React.useCallback(async () => {
|
|
559
|
+
setBusy('load'); setErr('')
|
|
560
|
+
try {
|
|
561
|
+
const r = await call('api/dsh-ruleset/report', { window_hours: rangeH })
|
|
562
|
+
if (r.ok) setData(r.value); else setErr(r.error?.message || '读取失败')
|
|
563
|
+
} catch (e) { setErr(String(e)) } finally { setBusy('') }
|
|
564
|
+
}, [])
|
|
565
|
+
|
|
566
|
+
const audit = async (hours) => {
|
|
567
|
+
const h = hours || rangeH
|
|
568
|
+
setBusy('audit'); setErr('')
|
|
569
|
+
try {
|
|
570
|
+
// 走 report 路由的强制刷新,避开 audit 路由在 dsh-app:// 下的 404
|
|
571
|
+
const r = await call('api/dsh-ruleset/report', { window_hours: h, refresh: true })
|
|
572
|
+
if (r.ok) setData(r.value); else setErr(r.error?.message || '审计失败')
|
|
573
|
+
} catch (e) { setErr(String(e)) } finally { setBusy('') }
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
React.useEffect(() => { audit() }, [])
|
|
577
|
+
React.useEffect(() => {
|
|
578
|
+
call('api/dsh-ruleset/predicates').then((r) => { if (r.ok) setPreds(r.value) }).catch(() => {})
|
|
579
|
+
call('api/dsh-ruleset/baseline', { action: 'get' }).then((r) => { if (r.ok) setBase(r.value) }).catch(() => {})
|
|
580
|
+
}, [])
|
|
581
|
+
|
|
582
|
+
const s = data?.summary || { pass: 0, violation: 0, warn: 0, stat: 0, unknown: 0 }
|
|
583
|
+
const rows = data?.rows || []
|
|
584
|
+
const series = (bucket === 'week' ? data?.trendWeek : bucket === 'day' ? data?.trendDay : data?.trend) || data?.series || []
|
|
585
|
+
const triage = async (key, status) => {
|
|
586
|
+
const res = await call('api/dsh-ruleset/finding-status', { key, status })
|
|
587
|
+
if (!res.ok) { setErr(res.error?.message || '标记失败'); return }
|
|
588
|
+
setData((d) => {
|
|
589
|
+
if (!d) return d
|
|
590
|
+
const findings = (d.findings || []).map((f) => (f.key === key ? { ...f, status } : f))
|
|
591
|
+
const marked = (d.findings || []).find((f) => f.key === key)
|
|
592
|
+
const summary = { ...(d.findingSummary || {}) }
|
|
593
|
+
summary.open = Math.max(0, (summary.open || 0) - 1)
|
|
594
|
+
summary[status] = (summary[status] || 0) + 1
|
|
595
|
+
const rows = (d.rows || []).map((r) => {
|
|
596
|
+
if (!marked || r.id !== marked.ruleId) return r
|
|
597
|
+
const hits = Math.max(0, (r.hits || 0) - 1)
|
|
598
|
+
return { ...r, hits, rate: r.base ? hits / r.base : null, status: hits === 0 ? 'pass' : r.status }
|
|
599
|
+
})
|
|
600
|
+
return { ...d, findings, rows, findingSummary: summary }
|
|
601
|
+
})
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
const removeRow = async (row) => {
|
|
605
|
+
if (pending !== row.id) { setPending(row.id); setTimeout(() => setPending((cur) => (cur === row.id ? '' : cur)), 4000); return }
|
|
606
|
+
setPending('')
|
|
607
|
+
const res = await call('api/dsh-ruleset/remove-rule', { id: row.id })
|
|
608
|
+
if (res.ok) { await audit() } else { setErr(res.error?.message || '删除失败') }
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const btn = (label, onClick, key, primary) => h('button', {
|
|
612
|
+
onClick, disabled: Boolean(busy),
|
|
613
|
+
style: {
|
|
614
|
+
fontSize: T.body, fontWeight: 500, padding: '5px 12px', borderRadius: RAD.ctrl,
|
|
615
|
+
border: `1px solid ${primary ? 'transparent' : C.line}`,
|
|
616
|
+
background: primary ? C.accent : 'transparent',
|
|
617
|
+
color: primary ? C.canvas : 'inherit',
|
|
618
|
+
cursor: busy ? 'default' : 'pointer', opacity: busy && busy !== key ? 0.5 : 1,
|
|
619
|
+
transition: 'background .12s, opacity .12s',
|
|
620
|
+
},
|
|
621
|
+
}, busy === key ? '…' : label)
|
|
622
|
+
|
|
623
|
+
return h('div', { style: {
|
|
624
|
+
padding: '16px 18px 28px', fontSize: 13, height: '100%', overflow: 'auto',
|
|
625
|
+
background: C.canvas, color: C.text, boxSizing: 'border-box',
|
|
626
|
+
} },
|
|
627
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 16 } },
|
|
628
|
+
h('div', null,
|
|
629
|
+
h('div', { style: { fontSize: T.head, fontWeight: 600, letterSpacing: '-0.01em' } }, 'Ruleset'),
|
|
630
|
+
h('div', { style: { fontSize: T.micro, color: C.muted, marginTop: 2 } },
|
|
631
|
+
(data?.source ? `规则源 ${data.source.kind}${data.source.draft ? '(草稿)' : ''}` : '规则合规观测') + ' · ' + BUILD)),
|
|
632
|
+
h('div', { style: { marginLeft: 'auto', display: 'flex', gap: 6, alignItems: 'center' } },
|
|
633
|
+
h('div', { style: { display: 'flex', gap: 2, marginRight: 6 } },
|
|
634
|
+
[[24, '近1天'], [168, '近7天'], [720, '近30天']].map(([hv, label]) => h('button', {
|
|
635
|
+
key: hv, onClick: () => { setRangeH(hv); audit(hv) },
|
|
636
|
+
style: {
|
|
637
|
+
fontSize: 10, padding: '3px 9px', borderRadius: 999, cursor: 'pointer',
|
|
638
|
+
border: `1px solid ${rangeH === hv ? C.accent + '55' : C.line}`,
|
|
639
|
+
background: rangeH === hv ? `${C.accent}12` : 'transparent',
|
|
640
|
+
color: rangeH === hv ? C.accent : C.muted,
|
|
641
|
+
},
|
|
642
|
+
}, label))),
|
|
643
|
+
h('button', {
|
|
644
|
+
onClick: async () => {
|
|
645
|
+
setJudging(true); setErr('')
|
|
646
|
+
try {
|
|
647
|
+
const r = await call('api/dsh-ruleset/judge', { window_hours: rangeH })
|
|
648
|
+
if (r.ok) { setJudge(r.value) } else { setErr(r.error?.message || '模型复核失败') }
|
|
649
|
+
} catch (e) { setErr(String(e)) } finally { setJudging(false) }
|
|
650
|
+
},
|
|
651
|
+
disabled: judging,
|
|
652
|
+
title: '只复核没有机械判据的规则',
|
|
653
|
+
style: { fontSize: 10, padding: '3px 9px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
654
|
+
}, judging ? '复核中…' : '模型复核'),
|
|
655
|
+
h('button', {
|
|
656
|
+
onClick: async () => {
|
|
657
|
+
try {
|
|
658
|
+
const [r, h] = await Promise.all([call('api/dsh-ruleset/suggest'), call('api/dsh-ruleset/hygiene')])
|
|
659
|
+
if (r.ok) setSugg(r.value.items)
|
|
660
|
+
if (h.ok) setHyg(h.value)
|
|
661
|
+
} catch { /* ignore */ }
|
|
662
|
+
},
|
|
663
|
+
title: '检查哪些规则还没有机械判据,并给出建议',
|
|
664
|
+
style: { fontSize: 10, padding: '3px 9px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
665
|
+
}, '规则体检'),
|
|
666
|
+
h('button', {
|
|
667
|
+
onClick: async () => {
|
|
668
|
+
try {
|
|
669
|
+
const r = await call('api/dsh-ruleset/baseline', { window_hours: rangeH })
|
|
670
|
+
if (r.ok) { setErr(''); call('api/dsh-ruleset/baseline', { action: 'get' }).then((x) => { if (x.ok) setBase(x.value) }); audit(rangeH) } else setErr('建基线失败')
|
|
671
|
+
} catch (e) { setErr(String(e)) }
|
|
672
|
+
},
|
|
673
|
+
title: '把当前结果记为基线;之后差异不显著的不再飘红',
|
|
674
|
+
style: { fontSize: 10, padding: '3px 9px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
675
|
+
}, '设基线'),
|
|
676
|
+
h('button', {
|
|
677
|
+
onClick: async () => {
|
|
678
|
+
const r = await call('api/dsh-ruleset/backups')
|
|
679
|
+
if (r.ok) { setBackups(r.value.items || []); setShowBackups(!showBackups) }
|
|
680
|
+
},
|
|
681
|
+
title: '查看 policy 备份、回滚或导出以保留规则',
|
|
682
|
+
style: { fontSize: 10, padding: '3px 9px', borderRadius: 999, border: `1px solid ${C.line}`, background: 'transparent', color: C.muted, cursor: 'pointer' },
|
|
683
|
+
}, '备份与回滚'),
|
|
684
|
+
btn('重新审计', () => audit(rangeH), 'audit'), btn('刷新', load, 'load'))),
|
|
685
|
+
|
|
686
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 10, marginBottom: 12 } },
|
|
687
|
+
[['通过', s.pass, C.pass], ['违规', s.violation, C.violation], ['待查', s.warn || 0, C.warn], ['统计', s.stat || 0, C.stat], ['未知', s.unknown, C.unknown]].map(([label, val, color]) =>
|
|
688
|
+
h('div', { key: label, style: { border: `1px solid ${C.line}`, borderRadius: RAD.card, padding: '12px 14px', background: C.surface } },
|
|
689
|
+
h('div', { style: { fontSize: T.micro, letterSpacing: '0.06em', textTransform: 'uppercase', color: C.muted, fontWeight: 500 } }, label),
|
|
690
|
+
h('div', { style: { display: 'flex', alignItems: 'flex-end', gap: 8, marginTop: 6 } },
|
|
691
|
+
h('div', { style: { fontSize: T.kpi, fontWeight: 600, color, lineHeight: 1, fontVariantNumeric: 'tabular-nums' } }, String(val)),
|
|
692
|
+
h('div', { style: { marginLeft: 'auto', opacity: 0.7 } },
|
|
693
|
+
series.length > 1 ? h(Spark, { values: series.map((x) => x.summary?.[label === '通过' ? 'pass' : label === '违规' ? 'violation' : 'unknown'] ?? 0), color, w: 56, hgt: 18 }) : null))))),
|
|
694
|
+
|
|
695
|
+
h('div', { style: { border: `1px solid ${C.line}`, borderRadius: RAD.card, padding: '12px 14px', background: C.surface, marginBottom: 14 } },
|
|
696
|
+
h('div', { style: { display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 8 } },
|
|
697
|
+
h('span', { style: { fontSize: T.micro, letterSpacing: '0.06em', textTransform: 'uppercase', color: C.muted, fontWeight: 500 } }, '违规趋势'),
|
|
698
|
+
h('span', { style: { display: 'flex', gap: 4, alignItems: 'center' } },
|
|
699
|
+
['hour', 'day', 'week'].map((k) => h('button', {
|
|
700
|
+
key: k, onClick: () => setBucket(k),
|
|
701
|
+
style: {
|
|
702
|
+
fontSize: 10, padding: '2px 8px', borderRadius: 999, cursor: 'pointer',
|
|
703
|
+
border: `1px solid ${bucket === k ? C.accent + '55' : C.line}`,
|
|
704
|
+
background: bucket === k ? `${C.accent}12` : 'transparent',
|
|
705
|
+
color: bucket === k ? C.accent : C.muted,
|
|
706
|
+
},
|
|
707
|
+
}, k === 'hour' ? '小时' : k === 'day' ? '天' : '周')),
|
|
708
|
+
h('span', { style: { marginLeft: 4, color: C.muted } },
|
|
709
|
+
series.length > 1 ? `${series.length} 个点` : '再审计一次就能画线'))),
|
|
710
|
+
series.length > 1 ? h(TrendChart, { series, color: C.violation })
|
|
711
|
+
: h('div', { style: { height: 64, display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, color: C.muted } }, '暂无趋势数据(需要至少 2 次审计)')),
|
|
712
|
+
|
|
713
|
+
showBackups ? h('div', { style: { border: `1px solid ${C.line}`, borderRadius: RAD.card, padding: 10, marginBottom: 10, background: 'rgba(255,255,255,0.5)' } },
|
|
714
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 6 } },
|
|
715
|
+
h('strong', { style: { fontSize: 12 } }, 'policy 备份'),
|
|
716
|
+
h('span', { style: { fontSize: 10, color: C.muted } }, `${backups.length} 份 · 每次写规则前自动留档`),
|
|
717
|
+
h('div', { style: { marginLeft: 'auto', display: 'flex', gap: 6 } },
|
|
718
|
+
h('button', {
|
|
719
|
+
onClick: async () => { const r = await call('api/dsh-ruleset/backup', { action: 'manual' }); if (r.ok) { const b = await call('api/dsh-ruleset/backups'); if (b.ok) setBackups(b.value.items || []) } },
|
|
720
|
+
style: { fontSize: 10, padding: '2px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit', cursor: 'pointer' },
|
|
721
|
+
}, '立即备份'),
|
|
722
|
+
h('button', {
|
|
723
|
+
onClick: async () => { const r = await call('api/dsh-ruleset/export'); if (r.ok) setErr(`已导出到 ${r.value.dir}`) },
|
|
724
|
+
style: { fontSize: 10, padding: '2px 8px', borderRadius: 6, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer' },
|
|
725
|
+
}, '导出 policy + AGENTS.md'))),
|
|
726
|
+
backups.length === 0 ? h('div', { style: { fontSize: 11, color: C.muted } }, '还没有备份') :
|
|
727
|
+
backups.slice(0, 8).map((b) => h('div', { key: b.file, style: { display: 'flex', alignItems: 'baseline', gap: 8, fontSize: 10.5, padding: '3px 0', borderTop: `1px dashed ${C.line}` } },
|
|
728
|
+
h('span', { style: { color: C.muted } }, new Date(b.ts.replace(/-/g, ':')).toLocaleString?.() || b.ts),
|
|
729
|
+
h('span', { style: { flex: 1 } }, b.action),
|
|
730
|
+
h('button', {
|
|
731
|
+
onClick: async () => {
|
|
732
|
+
const r = await call('api/dsh-ruleset/restore', { file: b.file })
|
|
733
|
+
if (r.ok) { setErr('已回滚到 ' + b.file); audit(rangeH) } else setErr(r.error?.message || '回滚失败')
|
|
734
|
+
},
|
|
735
|
+
style: { fontSize: 10, padding: '1px 8px', borderRadius: 6, border: `1px solid ${C.line}`, background: 'transparent', color: 'inherit', cursor: 'pointer' },
|
|
736
|
+
}, '回滚到此')))) : null,
|
|
737
|
+
err ? h('div', { style: { color: C.violation, marginBottom: 10, fontSize: 12 } }, err) : null,
|
|
738
|
+
sugg && sugg.length ? h('div', { style: { border: `1px solid ${C.line}`, borderRadius: RAD.card, padding: 10, marginBottom: 10, background: 'rgba(255,255,255,0.5)' } },
|
|
739
|
+
h('div', { style: { fontSize: T.label, color: C.muted, marginBottom: 6 } }, `体检:${sugg.length} 条规则没有机械判据`),
|
|
740
|
+
hyg ? h('div', { style: { fontSize: 10, color: C.muted, marginBottom: 6 } },
|
|
741
|
+
`规则 ${hyg.total} 条 · 约 ${hyg.estimatedTokens} tokens · 重复 ${hyg.dupes.length} 组 · 冲突 ${(hyg.conflicts || []).length} 组 · 无判据 ${hyg.noBind.length} 条` +
|
|
742
|
+
(hyg.cost[0] ? ` · 最长 ${hyg.cost[0].id}(${hyg.cost[0].chars} 字)` : '')) : null,
|
|
743
|
+
(hyg.conflicts || []).map((c, i) => h('div', { key: 'c' + i, style: { display: 'flex', gap: 8, alignItems: 'baseline', padding: '3px 0', color: C.violation } },
|
|
744
|
+
h('span', { style: { fontSize: 10, padding: '0 6px', borderRadius: 999, border: `1px solid ${C.violation}` } }, '冲突'),
|
|
745
|
+
h('span', { style: { fontSize: 11 } }, `${c.a} ↔ ${c.b}`),
|
|
746
|
+
h('span', { style: { fontSize: 10, color: C.muted } }, c.reason))),
|
|
747
|
+
sugg.map((x) => h('div', { key: x.id, style: { display: 'flex', gap: 8, alignItems: 'baseline', padding: '3px 0' } },
|
|
748
|
+
h('span', { style: { flex: '0 0 auto', fontSize: 11 } }, x.title || x.id),
|
|
749
|
+
h('span', { style: { flex: 1, fontSize: 10, color: C.muted } }, x.suggestion ? `建议 ${x.suggestion.type}` : `暂无法机械化(${x.classification})`),
|
|
750
|
+
x.suggestion ? h('button', {
|
|
751
|
+
onClick: async () => {
|
|
752
|
+
const res = await call('api/dsh-ruleset/update-rule', { id: x.id, binds: [x.suggestion] })
|
|
753
|
+
if (res.ok) { setSugg((v) => (v || []).filter((y) => y.id !== x.id)); audit() }
|
|
754
|
+
},
|
|
755
|
+
style: { fontSize: 10, padding: '1px 8px', borderRadius: 999, border: `1px solid ${C.accent}66`, background: 'transparent', color: C.accent, cursor: 'pointer' },
|
|
756
|
+
}, '采用') : null))) : null,
|
|
757
|
+
|
|
758
|
+
h('div', { style: { display: 'grid', gridTemplateColumns: 'repeat(3, minmax(240px, 1fr))', gap: 12, alignItems: 'start' } },
|
|
759
|
+
[['redline', '红线', C.violation], ['hard', '硬约束', C.warn], ['soft', '软规则', C.stat]].map(([tier, label, color]) => {
|
|
760
|
+
const list = rows.filter((r) => (r.tier || 'hard') === tier)
|
|
761
|
+
return h('section', {
|
|
762
|
+
key: tier,
|
|
763
|
+
onDragOver: (e) => { e.preventDefault(); e.dataTransfer.dropEffect = 'move' },
|
|
764
|
+
onDrop: async (e) => {
|
|
765
|
+
e.preventDefault()
|
|
766
|
+
const id = e.dataTransfer.getData('text/plain')
|
|
767
|
+
if (!id) return
|
|
768
|
+
const row = rows.find((r) => r.id === id)
|
|
769
|
+
if (!row || (row.tier || 'hard') === tier) return
|
|
770
|
+
const res = await call('api/dsh-ruleset/set-tier', { id, tier })
|
|
771
|
+
if (res.ok) setData((d) => d ? { ...d, rows: d.rows.map((r) => r.id === id ? { ...r, tier } : r) } : d)
|
|
772
|
+
else setErr(res.error?.message || '调整分级失败')
|
|
773
|
+
},
|
|
774
|
+
style: { border: `1px solid ${C.line}`, borderRadius: RAD.card, padding: 10, background: 'rgba(255,255,255,0.35)' },
|
|
775
|
+
},
|
|
776
|
+
h('div', { style: { display: 'flex', alignItems: 'baseline', gap: 6, marginBottom: 8 } },
|
|
777
|
+
h('span', { style: { width: 8, height: 8, borderRadius: 4, background: color } }),
|
|
778
|
+
h('strong', { style: { fontSize: T.body } }, label),
|
|
779
|
+
h('span', { style: { fontSize: T.micro, color: C.muted } }, `${list.length} 条`)),
|
|
780
|
+
h('div', { style: { display: 'grid', gap: 8 } },
|
|
781
|
+
list.map((r) => h(Card, {
|
|
782
|
+
key: r.id, row: r, series, windowHours: data?.windowHours || rangeH, bucket, findings: data?.findings || [],
|
|
783
|
+
model: (judge?.results || []).find((x) => x.id === r.id) || null,
|
|
784
|
+
open: Boolean(open[r.id]), pending: pending === r.id,
|
|
785
|
+
onEdit: (row) => setEditing(row),
|
|
786
|
+
onToggle: () => setOpen((o) => ({ ...o, [r.id]: !o[r.id] })),
|
|
787
|
+
onTriage: triage,
|
|
788
|
+
onRemove: removeRow,
|
|
789
|
+
})),
|
|
790
|
+
h(AddCard, { key: '__add_' + tier, preds, tier, onDone: () => { audit() } })))
|
|
791
|
+
})),
|
|
792
|
+
h('div', { style: { marginTop: 12, fontSize: 10, color: C.muted } },
|
|
793
|
+
data?.facts ? `${data.facts.sessions} 会话 · ${data.facts.turns} 轮 · ${data.facts.commands} 命令` : ''),
|
|
794
|
+
)
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
class Boundary extends React.Component {
|
|
798
|
+
constructor(props) { super(props); this.state = { err: null } }
|
|
799
|
+
static getDerivedStateFromError(err) { return { err } }
|
|
800
|
+
componentDidCatch(err) { try { console.error('[ruleset] panel crashed', err) } catch { /* ignore */ } }
|
|
801
|
+
render() {
|
|
802
|
+
if (this.state.err) {
|
|
803
|
+
return h('div', { style: { padding: 16, fontFamily: 'ui-monospace, monospace', fontSize: 12, color: '#b3123f', background: '#fff', height: '100%', overflow: 'auto' } },
|
|
804
|
+
h('div', { style: { fontWeight: 600, marginBottom: 8 } }, 'Ruleset 面板渲染失败'),
|
|
805
|
+
h('pre', { style: { whiteSpace: 'pre-wrap' } }, String(this.state.err && (this.state.err.stack || this.state.err.message) || this.state.err)),
|
|
806
|
+
h('button', { onClick: () => this.setState({ err: null }), style: { marginTop: 8 } }, '重试'))
|
|
807
|
+
}
|
|
808
|
+
return this.props.children
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
const SafePanel = (props) => h(Boundary, null, h(Panel, props))
|
|
812
|
+
|
|
813
|
+
return {
|
|
814
|
+
inject: ['slots'],
|
|
815
|
+
apply(ctx) {
|
|
816
|
+
try { remoteCommands = ctx.remote?.commands || null } catch { /* ignore */ }
|
|
817
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({ name: 'sidebar.panellist', id: PANEL_ID, order: 30, label: () => 'Ruleset' }, PanelIcon))
|
|
818
|
+
ctx.slots.inject('main', () => ctx.slots.register({ name: 'main', key: PANEL_ID }, SafePanel))
|
|
819
|
+
},
|
|
820
|
+
}
|
|
821
|
+
},
|
|
822
|
+
})
|