Files
fanlusky a7779d2b05 refactor(engine): 将 engine 从 submodule 内嵌为仓内一等组件
`vendor/MetasequoiaImeEngine` submodule 改为 `engine/` 目录,导入的是
product-lock.json 原本钉住的提交 c810d201,逐文件核对过 blob,因此这是一次
搬迁而不是引擎升级。引擎自己的嵌套 submodule(googlepinyinime-rev、utfcpp、
miniaudio)按各自 gitlink 一并展开,来源与提交见 engine/UPSTREAM.md。

Windows 专门化裁掉了不属于本产品的部分:whisper.cpp 与本地推理 provider、
macOS 示例宿主、整个 dictionary/ 建库流水线、上游 CI 与 release-please 配置。
词库仍按 product-lock.json 在构建时取发布产物。

引擎不再进 product-lock.json:没有 gitlink 就没有可比对的东西,锁里写一个上游
commit 等于宣称这棵树仍等于上游,而第一个 Windows 专门化改动落地时这句话就假了。
verify-contracts / verify-checkout 随之移除,verify-published 保留。

CI 与工具链:引擎源码并入根 scripts/format.sh(删掉引擎自己的那份,避免两套排除
列表分叉);引擎 voice.yml 里的 silero_vad.onnx 摘要校验移到本仓 lock job;
engine-bump-triage 工作流与其测试删除;gitsubmodule 的 dependabot 频率由 daily
改为 monthly。

README、README.en、AGENTS.md、CONTRIBUTING、docs/product-release.md 及各子目录
文档同步说明新的位置与边界。
2026-09-12 03:40:48 +08:00

82 lines
3.8 KiB
Python

"""Shared asset inventory for builders and pinned consumers; standard library only."""
import json
import hashlib
import tempfile
import zipfile
import importlib.util
import re
from pathlib import Path, PurePosixPath
_dictionary_spec = importlib.util.spec_from_file_location(
"msime_assets_dictionary_contract", Path(__file__).parents[1] / "dictionary/product.py")
_dictionary = importlib.util.module_from_spec(_dictionary_spec)
_dictionary_spec.loader.exec_module(_dictionary)
verify_product = _dictionary.verify_product
CONTRACT = json.loads(Path(__file__).with_name('assets.json').read_text(encoding='utf-8'))
# Fail before generating code or touching an archive if the inventory itself is ambiguous.
_ids, _paths = set(), set()
for _entry in CONTRACT['assets']:
_name = _entry['path']
if (not re.fullmatch(r'[a-z][a-z0-9_]*', _entry['id']) or _entry['id'] in _ids or
_name.casefold() in _paths or PurePosixPath(_name).is_absolute() or
'..' in PurePosixPath(_name).parts or '\\' in _name or
str(PurePosixPath(_name)) != _name):
raise ValueError('Invalid or duplicate assets contract entry')
_ids.add(_entry['id'])
_paths.add(_name.casefold())
VERSION = CONTRACT['contract_version']
ARCHIVE = CONTRACT['archive']
MANIFEST = CONTRACT['manifest']
def assets(profile='desktop'):
if profile != 'desktop':
raise ValueError(f'Unsupported assets profile: {profile}')
return {entry['path']: entry for entry in CONTRACT['assets'] if profile in entry['profiles']}
STATIC_FILES = {name: entry['source'] for name, entry in assets().items()
if entry['source'] != 'dictionary'}
DICTIONARY_FILES = frozenset(assets()) - STATIC_FILES.keys()
HELPCODES = {entry['schema']: entry['path'] for entry in CONTRACT['assets'] if 'schema' in entry}
def sha256(data):
return hashlib.sha256(data).hexdigest()
def verify(archive: Path, expected_sha256: str | None = None) -> dict:
"""Verify the exact payload and optionally its digest pinned by the consuming product."""
if expected_sha256 is not None:
digest = hashlib.sha256()
with Path(archive).open('rb') as stream:
for chunk in iter(lambda: stream.read(1 << 20), b''):
digest.update(chunk)
if digest.hexdigest() != expected_sha256:
raise ValueError('Engine assets archive does not match pinned digest')
with zipfile.ZipFile(archive) as bundle:
names = bundle.namelist()
manifest = json.loads(bundle.read(MANIFEST))
expected = set(assets())
if (manifest.get('manifest_version') != 1 or manifest.get('assets_contract_version') != VERSION or manifest.get('profile') != 'desktop'
or set(manifest.get('files', {})) != expected
or len(names) != len(set(names))
or set(names) != expected | {MANIFEST}):
raise ValueError('Unexpected engine assets manifest or archive contents')
if manifest.get('source', {}).get('repository') != 'metasequoiaime/MSIME-Engine':
raise ValueError('Unexpected Engine assets source repository')
for name, entry in manifest['files'].items():
data = bundle.read(name)
if len(data) != entry['size'] or sha256(data) != entry['sha256']:
raise ValueError(f'Engine asset changed: {name}')
# Reuse the dictionary format contract, including model magic and profile checks.
with tempfile.TemporaryDirectory() as temporary:
directory = Path(temporary)
for name in expected - set(STATIC_FILES):
(directory / name).write_bytes(bundle.read(name))
product = verify_product(directory, 'desktop')
if product['source']['commit'] != manifest['source']['commit']:
raise ValueError('Dictionary and runtime assets must have the same source commit')
return manifest