Files
oc_session/tools/_wsl_one_shot_fix.py
T

70 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""
_wsl_one_shot_fix.py — 一次性把 Win 侧模型数据 + 插件推给 WSL(立即生效,不等收尸)。
★ 用户终端跑: python <本脚本> (wsl.exe 实际调用在用户终端可执行; 本环境沙箱拦 wsl.exe)
对齐 2026-10-20 重定位后的 oc-model-sync.py:
1. 读 Win opencode.db 的 models-dev:catalog + credential(xiaomi 原厂直连密钥) + model.json
2. 推 catalog + credential + model.json + 插件 blob(tui.js v12 / model-manager.ts / mcpcfg.txt) 给 WSL
3. WSL import: 备份后 catalog 整份覆盖(删旧加新), credential 并集灌入(保留 WSL 独有),
model.json 覆盖, 插件写回 → /models 主列表 + /manage + xiaomi 原厂模型全部对齐 Win
复用 oc-model-sync.py 的导出/导入逻辑, 不重复造轮子。
"""
import importlib.util
import json
import os
import sys
HOME = os.path.expanduser("~")
MSYN = os.path.join(HOME, ".config", "opencode", "oc-model-sync.py")
def main():
spec = importlib.util.spec_from_file_location("ms", MSYN)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
if not os.path.exists(m.WIN_DB):
print("Win opencode.db 缺失: %s" % m.WIN_DB)
return 1
catalog, creds = m.read_win_data()
print("导出: catalog=%dB credentials=%d" % ((len(catalog) if catalog else 0), len(creds)))
if not catalog:
print("Win db 无 catalog, 无需推。")
return 0
# 1) 推 catalog
rc = m.wsl_push(catalog.encode("utf-8"), m.REMOTE_CATALOG, timeout=200)
print("catalog push rc=%d" % rc)
if rc != 0:
return 1
# 2) 推 credentials
rc = m.wsl_push(json.dumps(creds, ensure_ascii=False).encode("utf-8"), m.REMOTE_CREDS, timeout=120)
print("creds push rc=%d" % rc)
if rc != 0:
return 1
# 3) 推 model.json
if os.path.exists(m.WIN_STATE):
rc = m.wsl_push(open(m.WIN_STATE, "rb").read(), m.REMOTE_STATE, timeout=120)
print("state push rc=%d" % rc)
if rc != 0:
return 1
# 4) 推插件 blob
rc = m.wsl_push(json.dumps(m.build_plugin_blob(), ensure_ascii=False).encode("utf-8"),
m.REMOTE_PLUGINS, timeout=120)
print("plugins push rc=%d" % rc)
if rc != 0:
return 1
# 5) WSL import(覆盖 catalog + 并集 credential + 写 model.json + 写插件, 带备份回滚)
rc, _ = m.wsl_py(m.IMPORT_SRC, timeout=200)
print("import rc=%d" % rc)
res = m.wsl_cat(m.REMOTE_RESULT)
print("result:", res[:500])
print("OK. 重启 WSL 侧 OC(收尸 terminate) 后 /models 与 /mcpon /mcpoff /manage 即对齐 Win。")
return 0
if __name__ == "__main__":
sys.exit(main())