check_ui_copy.py
2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""界面文案检查:防止开发用语和大段解释文字又长回界面上。
./.venv/Scripts/python.exe scripts/check_ui_copy.py # 有问题时退出码 1
查两件事(只看界面上会显示的字符串,不看注释和代码里的名字):
1. 禁用词:开发概念不该让使用者看到;
2. 说明文字过长:hint / desc 超过 MAX_HINT 个字,多半是在解释系统原理。
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SRC = ROOT / "web" / "src"
sys.stdout.reconfigure(encoding="utf-8")
BANNED = ["骨架", "写回层", "四元组", "工作副本", "提示词", "内省", "SOP", "Agent", "eval", "审批中心", "LLM", "大模型"]
# 这些地方就是给实施人员看技术细节的,允许出现技术词
ALLOW_FILES = {"features/mcp", "features/settings", "features/evals", "features/runtime/AuditPage"}
MAX_HINT = 40
COMMENT = re.compile(r"/\*.*?\*/|//[^\n]*", re.S)
STRING = re.compile(r'"((?:[^"\\\n]|\\.)*)"|`((?:[^`\\]|\\.)*)`|>([^<>{}\n]*[一-鿿][^<>{}\n]*)<')
HINT = re.compile(r'\b(?:hint|desc|help)=(?:"([^"]*)"|\{"([^"]*)"\})')
CJK = re.compile(r"[一-鿿]")
def main() -> int:
bad: list[str] = []
for f in sorted(SRC.rglob("*.tsx")):
rel = f.relative_to(SRC).as_posix()
if any(rel.startswith(a) for a in ALLOW_FILES) or ".test." in rel:
continue
raw = f.read_text(encoding="utf-8")
# 注释换成等长空白,行号不变
text = COMMENT.sub(lambda m: re.sub(r"[^\n]", " ", m.group(0)), raw)
for m in STRING.finditer(text):
s = next(g for g in m.groups() if g is not None)
if not CJK.search(s):
continue # 没有中文的是代码里的标识,不是界面文案
line = text.count("\n", 0, m.start()) + 1
for w in BANNED:
if w in s:
bad.append(f"{rel}:{line} 禁用词「{w}」:{s.strip()[:60]}")
for m in HINT.finditer(text):
s = m.group(1) or m.group(2) or ""
if len(s) > MAX_HINT:
line = text.count("\n", 0, m.start()) + 1
bad.append(f"{rel}:{line} 说明过长({len(s)} 字):{s[:50]}…")
for b in bad:
print(b)
print(f"\n{'没有问题' if not bad else f'共 {len(bad)} 处'}")
return 1 if bad else 0
if __name__ == "__main__":
raise SystemExit(main())