* feat(engine): webview 契约给统计概览增加首词命中率字段并支持 null 分支 * feat(server,ui-html): 统计新增首词命中率(决策点记录、stats v2 迁移与设置页展示) * fix(server): 首词命中计数补日语标点排除与并发迁移复查 * fix(ui-html): 统计卡片改用自适应列宽,避免长数字被省略号截断 * fix(server,installer): 修复首词命中率随附测试在 CI 的失败 三条 server 用例是首词命中率改动新加、但从未跑绿:RawDb 在迁移用例里对尚不存在的库开只读写连接,补上 SQLITE_OPEN_CREATE;09-19 写了一次次选,按契约它是一次真实测量(0%),断言却要求 nullopt,改为断言 0.0,并把 nullopt 覆盖移到迁移用例(total==0 的那天)。安装器 preserve-user-data 从 stats_pipe.cpp 取库名,而 EnsureStatsStoreOpen 连同 stats.db 字面量已挪到 stats_candidate_capture.cpp,改到新位置。 * fix(server,ui-html): 首词命中率写库移出按键路径,修正迁移与统计口径 - 选词计数改由后台 SerialTaskRunner 写库,标点收尾与选词路径不再同步等 SQLite 锁 - 迁移以表结构为准,不再被版本号挡住;缺列时在 IMMEDIATE 事务里逐列补齐 - 标点在日期入口上收尾时按实际上屏的首条候选计数 - 五笔四码唯一自动上屏不计入;多段造词任一段非首位即不算命中 - 只有选词计数的日线不算打字日,不影响天数、日均与连续天数 - 卡片复用 formatFirstWordRatio 并展示累计命中率,概览复用 DailyRow::FirstWordRatio --------- Co-authored-by: fanlusky <1730976608@qq.com>
90 lines
4.1 KiB
Python
90 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate checked-in language bindings. --check rejects stale artifacts in CI."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
|
|
|
|
def ts_type(rule):
|
|
if "anyOf" in rule:
|
|
return " | ".join(ts_type(branch) for branch in rule["anyOf"])
|
|
if "enum" in rule:
|
|
return " | ".join(json.dumps(value) for value in rule["enum"])
|
|
if rule.get("type") == "array":
|
|
return "Array<" + ts_type(rule["items"]) + ">"
|
|
if rule.get("type") == "object":
|
|
fields = [f'{json.dumps(key)}{"" if key in rule.get("required", []) else "?"}: {ts_type(value)}'
|
|
for key, value in rule.get("properties", {}).items()]
|
|
if rule.get("additionalProperties"):
|
|
fields.append("[key: string]: unknown")
|
|
return "{ " + "; ".join(fields) + " }"
|
|
return {"integer": "number", "number": "number", "string": "string", "boolean": "boolean", "null": "null"}.get(rule.get("type"), "unknown")
|
|
|
|
|
|
def union(messages):
|
|
entries = []
|
|
for name, spec in messages.items():
|
|
properties = {"type": {"enum": [name]}, "protocolVersion": {"enum": [1]}, **spec.get("properties", {})}
|
|
required = ["type", *spec.get("required", [])]
|
|
if "data" in spec:
|
|
properties["data"] = spec["data"]
|
|
required.append("data")
|
|
entries.append(ts_type({"type": "object", "properties": properties, "required": required,
|
|
"additionalProperties": spec.get("additionalProperties", False)}))
|
|
return "\n | ".join(entries)
|
|
|
|
|
|
def generate():
|
|
schema = json.loads((ROOT / "messages.json").read_text())
|
|
compact = json.dumps(schema, ensure_ascii=False, separators=(",", ":"))
|
|
files = {
|
|
"schema.js": "// Generated by generate.py; edit messages.json.\n(function(root) {\n const schema = " + compact + ";\n root.MsimeMessageSchema = schema;\n if (typeof module !== 'undefined' && module.exports) module.exports = schema;\n})(globalThis);\n",
|
|
"schema.h": '#pragma once\n// Generated by generate.py; edit messages.json.\nnamespace metasequoia::webview {\ninline constexpr int Version = ' + str(schema["version"]) + ';\ninline constexpr char SchemaJson[] = R"msime(' + compact + ')msime";\n}\n',
|
|
"runtime.d.ts": "export {};\n",
|
|
}
|
|
types = "// Generated by generate.py; edit messages.json.\nimport './schema.js';\nimport './runtime.js';\n"
|
|
for name, messages in [("ClientMessage", schema["client"]), ("ServerMessage", schema["server"]),
|
|
("SettingsMessage", {k: v for k, v in schema["client"].items() if "settings" in v["surfaces"]})]:
|
|
types += f"export type {name} =\n | {union(messages)};\n"
|
|
types += '''declare global {
|
|
var MsimeProtocol: {
|
|
validate(message: unknown, direction: 'client' | 'server', surface?: string): boolean;
|
|
serializeClientMessage(message: ClientMessage, surface: string): string;
|
|
version: number;
|
|
};
|
|
}
|
|
export function serializeHostMessage(message: SettingsMessage): string {
|
|
return globalThis.MsimeProtocol.serializeClientMessage(message, 'settings');
|
|
}
|
|
export function isServerMessage(message: unknown): message is ServerMessage {
|
|
return globalThis.MsimeProtocol.validate(message, 'server');
|
|
}
|
|
'''
|
|
files["messages.ts"] = types
|
|
files["schema.d.ts"] = "export {};\n"
|
|
return files
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args()
|
|
for name, content in generate().items():
|
|
path = ROOT / name
|
|
payload = content.encode("utf-8")
|
|
if args.check:
|
|
# Byte comparison, not read_text(): text mode folds CRLF to LF and
|
|
# would hide a line-ending drift in an otherwise identical file.
|
|
if not path.exists() or path.read_bytes() != payload:
|
|
raise SystemExit(f"Stale binding: {path}; run generate.py")
|
|
else:
|
|
# newline="\n" keeps the checked-in bytes platform-independent; text
|
|
# mode's default translation writes CRLF on Windows.
|
|
path.write_text(content, encoding="utf-8", newline="\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|