sage-mem 2.33.4 → 3.1.0
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/README.en.md +52 -1
- package/README.md +52 -1
- package/lib/assembly/tools-mount.js +4 -1
- package/lib/autodream/tools.js +5 -0
- package/lib/client.js +1375 -0
- package/lib/index.js +184 -4
- package/lib/memory/conflicts.js +53 -4
- package/lib/shared/codes.js +28 -0
- package/lib/sync/base.js +124 -0
- package/lib/sync/devices.js +237 -0
- package/lib/sync/gate.js +303 -0
- package/lib/sync/instance.js +138 -0
- package/lib/sync/panel.js +143 -0
- package/lib/sync/policy.js +212 -0
- package/lib/sync/pull.js +6 -0
- package/lib/sync/push.js +522 -0
- package/lib/sync/remote.js +139 -10
- package/lib/sync/resolve.js +358 -0
- package/lib/sync/upload.js +372 -0
- package/lib/tools/sync-tool.js +127 -1
- package/lib/typert/schemas.js +107 -0
- package/lib/typert.host.js +47 -0
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -1952,6 +1952,43 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
1952
1952
|
return v;
|
|
1953
1953
|
},
|
|
1954
1954
|
};
|
|
1955
|
+
// ── 3.0.0:跨设备裁决(applyResolution)──
|
|
1956
|
+
// 入参 `{id, choice}`:**正好两键**。多传的键宿主 strict 会剥掉,但界面那一层
|
|
1957
|
+
// 「以为它生效了」最难查 —— 所以在这里就拒。`choice` 只校字符串、**不校枚举**:
|
|
1958
|
+
// 客户端只负责传,合法取值是宿主 schema(z.enum(RESOLVE_CHOICES))的事,
|
|
1959
|
+
// 客户端再抄一份枚举,多出来的那一种裁法就会在面板上永远点不动。
|
|
1960
|
+
const conflictApplyOptsSchema = {
|
|
1961
|
+
parse(v) {
|
|
1962
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: expected object");
|
|
1963
|
+
const keys = Object.keys(v);
|
|
1964
|
+
if (keys.length !== 2 || !("id" in v) || !("choice" in v)) {
|
|
1965
|
+
throw new Error("sage-mem: expected exactly {id, choice}");
|
|
1966
|
+
}
|
|
1967
|
+
isStr(v.id);
|
|
1968
|
+
isStr(v.choice);
|
|
1969
|
+
return v;
|
|
1970
|
+
},
|
|
1971
|
+
};
|
|
1972
|
+
// 结果:六个「回声」字段(choice / wrote / baseUpdated / status / recordId + ok 本身)
|
|
1973
|
+
// 成功失败都在 —— 界面要靠 `wrote` 分辨「写了远端」与「一个字节都没动」。
|
|
1974
|
+
// 失败分支仍先认 `{ok:false, code, error?}` 这一支(与上面六条同一条纪律:
|
|
1975
|
+
// 混合版本环境里,旧宿主 / 半态收据不能被 strict 判成坏结果)。
|
|
1976
|
+
const conflictApplySchema = {
|
|
1977
|
+
parse(v) {
|
|
1978
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: invalid conflict result");
|
|
1979
|
+
if (conflictFailure(v)) return v;
|
|
1980
|
+
isBool(v.ok);
|
|
1981
|
+
isStr(v.choice);
|
|
1982
|
+
isBool(v.baseUpdated);
|
|
1983
|
+
isStr(v.status);
|
|
1984
|
+
isStr(v.recordId);
|
|
1985
|
+
if (v.wrote !== "remote" && v.wrote !== "local" && v.wrote !== "none") {
|
|
1986
|
+
throw new Error("sage-mem: invalid wrote");
|
|
1987
|
+
}
|
|
1988
|
+
if ("baseError" in v) isStr(v.baseError);
|
|
1989
|
+
return v;
|
|
1990
|
+
},
|
|
1991
|
+
};
|
|
1955
1992
|
// 5 个方法都是「一个对象参数」:{id} / {id,file?,note?}。可选字段传了就必须是字符串,
|
|
1956
1993
|
// 没传就交给宿主按空串处理 —— 不在这里编默认值。
|
|
1957
1994
|
const conflictOptsSchema = {
|
|
@@ -1964,6 +2001,67 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
1964
2001
|
},
|
|
1965
2002
|
};
|
|
1966
2003
|
|
|
2004
|
+
// ── 3.1.0:跨设备同步(sync/devices · sync/run)──
|
|
2005
|
+
// devices 的两种「不行」必须分开:`ok:false` = 这次调用没走通(该看日志),
|
|
2006
|
+
// `available:false` = 设备发现用不了(没装 tailscale / 没登录,该提示手打别名)。
|
|
2007
|
+
// 所以 ok / available 都必填,devices 永远是数组(失败时是空数组,不是缺键)。
|
|
2008
|
+
const syncDeviceSchema = parseSchema({ "host": isStr, "dnsName": isStr, "ips": isStrArr, "os": isStr, "online": isBool });
|
|
2009
|
+
const syncDevicesBase = optionalFields(parseSchema({ "ok": isBool, "available": isBool }), { "reason": isStr, "code": isStr, "error": isStr });
|
|
2010
|
+
const syncDevicesSchema = {
|
|
2011
|
+
// `fields` / `elementFields` 不是装饰:`client-exec-test` 第 3.1 组拿它们与宿主
|
|
2012
|
+
// zod 的 shape 逐条对表 —— 少了就等于这一条 strict codec 不参与漂移检测。
|
|
2013
|
+
fields: syncDevicesBase.fields.concat(["devices"]),
|
|
2014
|
+
elementFields: syncDeviceSchema.fields,
|
|
2015
|
+
parse(v) {
|
|
2016
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: invalid sync devices");
|
|
2017
|
+
syncDevicesBase.parse(v);
|
|
2018
|
+
if (!Array.isArray(v.devices)) throw new Error("sage-mem: invalid devices list");
|
|
2019
|
+
v.devices.forEach((d) => syncDeviceSchema.parse(d));
|
|
2020
|
+
return v;
|
|
2021
|
+
},
|
|
2022
|
+
};
|
|
2023
|
+
// 入参:`{action, host?, remotePath?, apply?, confirm?}`。
|
|
2024
|
+
// **只产出声明过的键** —— 多传的键在这里就拒(宿主 strict 会剥掉它,但「界面以为
|
|
2025
|
+
// 自己传了」最难查)。`action` 只校字符串、**不校枚举**:合法取值是宿主 schema
|
|
2026
|
+
// (z.enum(PANEL_ACTIONS))与 `runSyncAction` 的事,客户端再抄一份枚举,
|
|
2027
|
+
// 多出来的那一种动作就会在面板上永远点不动(与 conflictApplyOptsSchema 同一条理由)。
|
|
2028
|
+
const SYNC_RUN_KEYS = ["action", "host", "remotePath", "apply", "confirm"];
|
|
2029
|
+
const syncRunArgsSchema = {
|
|
2030
|
+
parse(v) {
|
|
2031
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: expected object");
|
|
2032
|
+
const unknown = Object.keys(v).filter((k) => SYNC_RUN_KEYS.indexOf(k) < 0);
|
|
2033
|
+
if (unknown.length > 0) throw new Error("sage-mem: unexpected sync arg " + unknown.join(","));
|
|
2034
|
+
if (!("action" in v)) throw new Error("sage-mem: missing action");
|
|
2035
|
+
isStr(v.action);
|
|
2036
|
+
const out = { action: v.action };
|
|
2037
|
+
if ("host" in v) { isStr(v.host); out.host = v.host; }
|
|
2038
|
+
if ("remotePath" in v) { isStr(v.remotePath); out.remotePath = v.remotePath; }
|
|
2039
|
+
if ("apply" in v) { isBool(v.apply); out.apply = v.apply; }
|
|
2040
|
+
if ("confirm" in v) { isBool(v.confirm); out.confirm = v.confirm; }
|
|
2041
|
+
return out;
|
|
2042
|
+
},
|
|
2043
|
+
};
|
|
2044
|
+
// 结果:只有三种动作**都成立**的那几个字段(action / applied / report)。
|
|
2045
|
+
// 刻意不声明 summary / plan / results —— 三个动作的 summary 形状各不相同
|
|
2046
|
+
// (pull 是 import/overwrite/…,push 是 upload/overwrite/…,check 连 summary 都没有),
|
|
2047
|
+
// 声明一份合并形状只会被迫放宽校验或逼下游造假字段。面板渲染 report(markdown)就够。
|
|
2048
|
+
const syncGateSchema = parseSchema({ "allow": isBool, "reason": isStr, "needConfirm": isBool, "deviceId": isStr, "deviceLabel": isStr });
|
|
2049
|
+
const syncRunSchema = {
|
|
2050
|
+
fields: ["ok", "action", "applied", "report", "gate", "code", "error"],
|
|
2051
|
+
parse(v) {
|
|
2052
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) throw new Error("sage-mem: invalid sync result");
|
|
2053
|
+
isBool(v.ok);
|
|
2054
|
+
isStr(v.action);
|
|
2055
|
+
isBool(v.applied);
|
|
2056
|
+
isStr(v.report);
|
|
2057
|
+
// gate 只有 push 会给(写入门);其余动作没有这个键。
|
|
2058
|
+
if ("gate" in v) syncGateSchema.parse(v.gate);
|
|
2059
|
+
if ("code" in v) isStr(v.code);
|
|
2060
|
+
if ("error" in v) isStr(v.error);
|
|
2061
|
+
return v;
|
|
2062
|
+
},
|
|
2063
|
+
};
|
|
2064
|
+
|
|
1967
2065
|
const TYPERT_REMOTE = {
|
|
1968
2066
|
package: "sage-mem",
|
|
1969
2067
|
descriptors: [
|
|
@@ -2443,6 +2541,7 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2443
2541
|
// (宿主 remote 清单是构建期产物),不在这里声明 `ctx.get("remote.conflict")`
|
|
2444
2542
|
// 就永远是 undefined —— 界面表现为「一整个页签消失」。
|
|
2445
2543
|
// 6 条都在同一个 `conflict` 面上:列表 / 读正文 / 标失效 / 标记已裁决 / 撤销 / 删记录。
|
|
2544
|
+
// 3.0.0 再加第 7 条 `applyResolution`(跨设备冲突的三种裁决),见本段末尾。
|
|
2446
2545
|
{
|
|
2447
2546
|
id: "sage-mem#conflict/list",
|
|
2448
2547
|
service: "conflict",
|
|
@@ -2521,6 +2620,50 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2521
2620
|
result: { mode: "strict", typeSymbol: "sage-mem/types#ConflictRemoveResult", schema: conflictRemoveSchema, create: () => conflictRemoveSchema },
|
|
2522
2621
|
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2523
2622
|
},
|
|
2623
|
+
{
|
|
2624
|
+
// 3.0.0:跨设备冲突的裁决 —— 留本地(推上去)/ 留远端(写回本地)/ 手工合并(不写)。
|
|
2625
|
+
// 这是这一面唯一**会改记忆正文**的方法,也是唯一带 `choice` 入参的方法。
|
|
2626
|
+
id: "sage-mem#conflict/applyResolution",
|
|
2627
|
+
service: "conflict",
|
|
2628
|
+
namespace: "conflict",
|
|
2629
|
+
method: "applyResolution",
|
|
2630
|
+
invocation: { kind: "direct" },
|
|
2631
|
+
parameters: [
|
|
2632
|
+
{ name: "opts", wire: "opts", source: "json", codec: { mode: "strict", typeSymbol: "sage-mem/types#ConflictApplyArgs", schema: conflictApplyOptsSchema, create: () => conflictApplyOptsSchema } },
|
|
2633
|
+
],
|
|
2634
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#ConflictApplyResult", schema: conflictApplySchema, create: () => conflictApplySchema },
|
|
2635
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2636
|
+
},
|
|
2637
|
+
// ── 3.1.0:跨设备同步(`sync` 面)──
|
|
2638
|
+
// 第五个 namespace:与 conflict 同一个坑 —— 不在这里声明就没有 `ctx.get("remote.sync")`,
|
|
2639
|
+
// 面板的同步页签会整块消失(宿主 remote 清单是构建期产物)。
|
|
2640
|
+
// 两条:设备候选(无参)与一次动作(一个 opts)。
|
|
2641
|
+
{
|
|
2642
|
+
// 无入参:超时 / 测试注入点都不走 wire(它们是后端自己的事)。
|
|
2643
|
+
id: "sage-mem#sync/devices",
|
|
2644
|
+
service: "sync",
|
|
2645
|
+
namespace: "sync",
|
|
2646
|
+
method: "devices",
|
|
2647
|
+
invocation: { kind: "direct" },
|
|
2648
|
+
parameters: [],
|
|
2649
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#SyncDevicesResult", schema: syncDevicesSchema, create: () => syncDevicesSchema },
|
|
2650
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2651
|
+
},
|
|
2652
|
+
{
|
|
2653
|
+
// check(看清楚)/ pull(搬回来)/ push(推上去)。
|
|
2654
|
+
// host / remotePath 平时不传 —— 宿主拿设置里的「跨设备」兜底;只有用户临时
|
|
2655
|
+
// 改了没保存时才传。所以客户端这里只做类型校验,**不填默认值**。
|
|
2656
|
+
id: "sage-mem#sync/run",
|
|
2657
|
+
service: "sync",
|
|
2658
|
+
namespace: "sync",
|
|
2659
|
+
method: "run",
|
|
2660
|
+
invocation: { kind: "direct" },
|
|
2661
|
+
parameters: [
|
|
2662
|
+
{ name: "opts", wire: "opts", source: "json", codec: { mode: "strict", typeSymbol: "sage-mem/types#SyncRunArgs", schema: syncRunArgsSchema, create: () => syncRunArgsSchema } },
|
|
2663
|
+
],
|
|
2664
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#SyncRunResult", schema: syncRunSchema, create: () => syncRunSchema },
|
|
2665
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2666
|
+
},
|
|
2524
2667
|
],
|
|
2525
2668
|
};
|
|
2526
2669
|
|
|
@@ -4469,6 +4612,101 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
4469
4612
|
"ext.intake.err.noRemote": "宿主侧还没有外部来件接口(插件版本较旧)",
|
|
4470
4613
|
"ext.intake.err.rejected": "处理被拒绝",
|
|
4471
4614
|
"ext.intake.err.noClipboard": "剪贴板不可用,请手动复制",
|
|
4615
|
+
// ── 3.0.0 跨设备差异(第 4 步的裁决 UI)─────────────────────
|
|
4616
|
+
// 只描述「跨设备同步」这一类记录。措辞里不出现「冲突」二字是有意的:
|
|
4617
|
+
// 这一屏列的是**同一份记忆在两台机器上各长出一版**,不是逻辑矛盾。
|
|
4618
|
+
"ext.sync.title": "跨设备差异",
|
|
4619
|
+
"ext.sync.count": "{count} 条待裁决",
|
|
4620
|
+
"ext.sync.countNone": "无待裁决",
|
|
4621
|
+
"ext.sync.empty": "没有待裁决的跨设备差异",
|
|
4622
|
+
"ext.sync.what": "跨设备同步撞车的地方:两边都改过同一个文件,或者各自新建了同名的一个。这里只列这一种记录 —— 自动整理造出来的、以及没有来源标记的老记录都不在这张卡里。",
|
|
4623
|
+
"ext.sync.refresh": "刷新",
|
|
4624
|
+
"ext.sync.path": "远端路径:{path}",
|
|
4625
|
+
"ext.sync.truncated": "记录里的正文被截断过,对比看到的是留下来的那一段",
|
|
4626
|
+
"ext.sync.diffOn": "逐行对比",
|
|
4627
|
+
"ext.sync.diffOff": "收起对比",
|
|
4628
|
+
"ext.sync.diffReading": "正在取两份正文…",
|
|
4629
|
+
"ext.sync.diffLines": "本地 {a} 行 · 远端 {b} 行",
|
|
4630
|
+
"ext.sync.diffAll": "展开全部",
|
|
4631
|
+
"ext.sync.diffFold": "只看差异",
|
|
4632
|
+
"ext.sync.skip": "…(跳过 {count} 行)",
|
|
4633
|
+
"ext.sync.same": "两份逐行相同(差别在元数据或行尾,不在正文)",
|
|
4634
|
+
"ext.sync.tooBig": "两份各 {a} / {b} 行,太大不做逐行对比;用 read_memory 分别看",
|
|
4635
|
+
"ext.sync.side.local": "本地",
|
|
4636
|
+
"ext.sync.side.remote": "远端",
|
|
4637
|
+
"ext.sync.local": "留本地",
|
|
4638
|
+
"ext.sync.remote": "留远端",
|
|
4639
|
+
"ext.sync.manual": "手工合并",
|
|
4640
|
+
"ext.sync.busy": "处理中…",
|
|
4641
|
+
"ext.sync.confirm.localTitle": "用本地这一版覆盖远端?",
|
|
4642
|
+
"ext.sync.confirm.localBody": "远端那份会被覆盖,改不回来;本地这一版不受影响。裁决后这条记录标成已裁决。",
|
|
4643
|
+
"ext.sync.confirm.remoteTitle": "用远端这一版覆盖本地?",
|
|
4644
|
+
"ext.sync.confirm.remoteBody": "本地那份会被覆盖,改不回来;这一版的正文还留在这条记录里,需要时能对照。裁决后这条记录标成已裁决。",
|
|
4645
|
+
"ext.sync.confirm.yes": "覆盖并裁决",
|
|
4646
|
+
"ext.sync.notice.local": "已把本地这一版推到远端,记录标成已裁决",
|
|
4647
|
+
"ext.sync.notice.remote": "已把远端这一版写回本地,记录标成已裁决",
|
|
4648
|
+
"ext.sync.notice.manual": "已标记,去改文件;改完跑一次检查",
|
|
4649
|
+
"ext.sync.err.list": "读不到冲突队列:{code} {error}",
|
|
4650
|
+
"ext.sync.err.read": "读不到这条记录:{code} {error}",
|
|
4651
|
+
"ext.sync.err.act": "裁决没有完成:{code} {error}",
|
|
4652
|
+
"ext.sync.err.base": "文件已经写成,但基线没更新:{error}(下次检查会把同一个冲突再造一次)",
|
|
4653
|
+
"ext.sync.err.noAct": "这个宿主没有 applyResolution(升级宿主后再试)",
|
|
4654
|
+
// ── 3.1.0 跨设备同步(第 5 步的「先跑一次」)─────────────────────
|
|
4655
|
+
// 与上面那一组分开命名是有意的:上面 `ext.sync.*` 回答「对不上时听谁的」,
|
|
4656
|
+
// 这一组回答「怎么让两台机器对上」。两组键逐个都有人用(client-exec-test 查死键)。
|
|
4657
|
+
"ext.syncPanel.title": "跨设备同步",
|
|
4658
|
+
"ext.syncPanel.what": "两台机器共用一份记忆:先「检查」看清楚差在哪,再决定「拉取」(把远端那份搬回来)或「推送」(把本地这份推上去)。拉取与推送都先预演、后执行,真要写盘之前还会再问你一次。",
|
|
4659
|
+
"ext.syncPanel.host": "远端主机",
|
|
4660
|
+
"ext.syncPanel.hostPlaceholder": "SSH 别名,例如 greycloud",
|
|
4661
|
+
"ext.syncPanel.hostHint": "填 ~/.ssh/config 里的 Host 名;改完离开输入框就存。",
|
|
4662
|
+
"ext.syncPanel.devices": "看看有哪些设备",
|
|
4663
|
+
"ext.syncPanel.devicesNone": "Tailscale 里没有别的设备",
|
|
4664
|
+
"ext.syncPanel.devicesUnavailable": "没检测到 Tailscale,手打 SSH 别名即可",
|
|
4665
|
+
"ext.syncPanel.devicesFailed": "读不到设备列表:{code} {error}",
|
|
4666
|
+
"ext.syncPanel.deviceOnline": "在线",
|
|
4667
|
+
"ext.syncPanel.deviceOffline": "离线",
|
|
4668
|
+
"ext.syncPanel.remotePath": "远端记忆目录",
|
|
4669
|
+
"ext.syncPanel.remotePathPlaceholder": "~/shengdian/memory",
|
|
4670
|
+
"ext.syncPanel.pathHint": "那台机器上记忆目录的位置(~ 也行);两边同名文件才算同一份记忆。",
|
|
4671
|
+
"ext.syncPanel.saved": "已保存",
|
|
4672
|
+
"ext.syncPanel.check": "检查",
|
|
4673
|
+
"ext.syncPanel.pull": "拉取",
|
|
4674
|
+
"ext.syncPanel.push": "推送",
|
|
4675
|
+
"ext.syncPanel.pullApply": "执行拉取",
|
|
4676
|
+
"ext.syncPanel.pushApply": "执行推送",
|
|
4677
|
+
"ext.syncPanel.actionsHint": "拉取 / 推送先预演:报告出来之后那颗按钮才变成「执行…」,点它还会再问一次。检查只跑只读的 ssh,一个字节都不写。",
|
|
4678
|
+
"ext.syncPanel.status.idle": "还没跑过",
|
|
4679
|
+
"ext.syncPanel.status.staged": "预演完成 · 待执行{action}",
|
|
4680
|
+
"ext.syncPanel.status.done": "上次:{action}",
|
|
4681
|
+
"ext.syncPanel.busy.check": "检查中…(要跑几条 ssh,慢链路十几秒很正常)",
|
|
4682
|
+
"ext.syncPanel.busy.pull": "正在预演拉取…",
|
|
4683
|
+
"ext.syncPanel.busy.push": "正在预演推送…",
|
|
4684
|
+
"ext.syncPanel.busy.apply": "正在写盘…",
|
|
4685
|
+
"ext.syncPanel.busy.devices": "正在问 Tailscale…",
|
|
4686
|
+
"ext.syncPanel.busy.save": "保存中…",
|
|
4687
|
+
"ext.syncPanel.empty": "还没有结果 —— 先点「检查」看一眼差在哪",
|
|
4688
|
+
"ext.syncPanel.report.title": "结果",
|
|
4689
|
+
"ext.syncPanel.report.preview": "预演(一个字节都没写)",
|
|
4690
|
+
"ext.syncPanel.report.applied": "已执行(写盘了)",
|
|
4691
|
+
"ext.syncPanel.report.empty": "这次没有正文报告(看下面那句结论)",
|
|
4692
|
+
"ext.syncPanel.confirm.pullTitle": "用远端那一版覆盖本地?",
|
|
4693
|
+
"ext.syncPanel.confirm.pullBody": "本地改过的文件会换成远端那份,改不回来;旧版会先进备份。预演报告就在上面。",
|
|
4694
|
+
"ext.syncPanel.confirm.pushTitle": "把本地这一版推到远端?",
|
|
4695
|
+
"ext.syncPanel.confirm.pushBody": "远端同名文件会换成本地这份,改不回来;旧版会先进备份。预演报告就在上面。",
|
|
4696
|
+
"ext.syncPanel.confirm.yes": "确认并执行",
|
|
4697
|
+
"ext.syncPanel.confirm.busy": "执行中…",
|
|
4698
|
+
"ext.syncPanel.gate.title": "首次向这台设备推送",
|
|
4699
|
+
"ext.syncPanel.gate.body": "远端库属于 {who}。确认它就是你自己的库,再继续。",
|
|
4700
|
+
"ext.syncPanel.gate.noDevice": "一台还没登记过的设备",
|
|
4701
|
+
"ext.syncPanel.gate.yes": "确认并推送",
|
|
4702
|
+
"ext.syncPanel.gate.cancel": "先不推",
|
|
4703
|
+
"ext.syncPanel.failed": "这一步没走通:{code} {error}",
|
|
4704
|
+
"ext.syncPanel.notice.check": "检查完了,差异在下面的报告里",
|
|
4705
|
+
"ext.syncPanel.notice.preview": "预演完了 —— 看着没问题,再点「执行…」",
|
|
4706
|
+
"ext.syncPanel.notice.pull": "拉取执行完了,报告在下面",
|
|
4707
|
+
"ext.syncPanel.notice.push": "推送执行完了,报告在下面",
|
|
4708
|
+
"ext.syncPanel.err.noRemote": "这个宿主还没有跨设备同步接口(重启 DSH 后再试)",
|
|
4709
|
+
"ext.syncPanel.err.noSettings": "宿主没有提供设置接口(版本偏旧)",
|
|
4472
4710
|
"lab.title": "MMR 去重(实验性)",
|
|
4473
4711
|
"lab.switch": "启用 MMR 去重",
|
|
4474
4712
|
"lab.switch.onTitle": "现在开启:注入时优先让不同主题的记忆占名额",
|
|
@@ -5210,6 +5448,96 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
5210
5448
|
"ext.intake.err.noRemote": "The host has no incoming-submissions API yet (older plugin version)",
|
|
5211
5449
|
"ext.intake.err.rejected": "The action was rejected",
|
|
5212
5450
|
"ext.intake.err.noClipboard": "Clipboard unavailable, please copy manually",
|
|
5451
|
+
"ext.sync.title": "Cross-device differences",
|
|
5452
|
+
"ext.sync.count": "{count} waiting",
|
|
5453
|
+
"ext.sync.countNone": "None waiting",
|
|
5454
|
+
"ext.sync.empty": "No cross-device differences waiting",
|
|
5455
|
+
"ext.sync.what": "Where sync collided: both sides changed the same file, or each created one with the same name. Only those records are listed here - conflicts made by auto-organize, and old records without an origin, stay out.",
|
|
5456
|
+
"ext.sync.refresh": "Refresh",
|
|
5457
|
+
"ext.sync.path": "Remote path: {path}",
|
|
5458
|
+
"ext.sync.truncated": "The body stored in this record was truncated; the diff shows what is left",
|
|
5459
|
+
"ext.sync.diffOn": "Line diff",
|
|
5460
|
+
"ext.sync.diffOff": "Hide diff",
|
|
5461
|
+
"ext.sync.diffReading": "Reading both versions...",
|
|
5462
|
+
"ext.sync.diffLines": "local {a} lines, remote {b} lines",
|
|
5463
|
+
"ext.sync.diffAll": "Show all",
|
|
5464
|
+
"ext.sync.diffFold": "Diff only",
|
|
5465
|
+
"ext.sync.skip": "... {count} lines skipped",
|
|
5466
|
+
"ext.sync.same": "Both sides are identical line by line (they differ in metadata or line endings, not in text)",
|
|
5467
|
+
"ext.sync.tooBig": "local {a} / remote {b} lines - too large for a line diff; open each one with read_memory",
|
|
5468
|
+
"ext.sync.side.local": "local",
|
|
5469
|
+
"ext.sync.side.remote": "remote",
|
|
5470
|
+
"ext.sync.local": "Keep local",
|
|
5471
|
+
"ext.sync.remote": "Keep remote",
|
|
5472
|
+
"ext.sync.manual": "Merge by hand",
|
|
5473
|
+
"ext.sync.busy": "Working...",
|
|
5474
|
+
"ext.sync.confirm.localTitle": "Overwrite the remote copy with this local one?",
|
|
5475
|
+
"ext.sync.confirm.localBody": "The remote copy will be overwritten and cannot be restored; the local copy stays as it is. The record is then marked resolved.",
|
|
5476
|
+
"ext.sync.confirm.remoteTitle": "Overwrite the local copy with this remote one?",
|
|
5477
|
+
"ext.sync.confirm.remoteBody": "The local copy will be overwritten and cannot be restored; this version stays inside the record for reference. The record is then marked resolved.",
|
|
5478
|
+
"ext.sync.confirm.yes": "Overwrite and resolve",
|
|
5479
|
+
"ext.sync.notice.local": "Local version pushed to the remote; the record is marked resolved",
|
|
5480
|
+
"ext.sync.notice.remote": "Remote version written back locally; the record is marked resolved",
|
|
5481
|
+
"ext.sync.notice.manual": "Marked; go edit the file, then run a check",
|
|
5482
|
+
"ext.sync.err.list": "Cannot read the conflict queue: {code} {error}",
|
|
5483
|
+
"ext.sync.err.read": "Cannot read this record: {code} {error}",
|
|
5484
|
+
"ext.sync.err.act": "Resolution did not complete: {code} {error}",
|
|
5485
|
+
"ext.sync.err.base": "File written, but the baseline was not updated: {error} (the next check will raise the same conflict again)",
|
|
5486
|
+
"ext.sync.err.noAct": "This host has no applyResolution (upgrade the host and retry)",
|
|
5487
|
+
// ── 3.1.0 cross-device sync (step 5: run one sync first) ──
|
|
5488
|
+
"ext.syncPanel.title": "Cross-device sync",
|
|
5489
|
+
"ext.syncPanel.what": "Two machines share one memory: run Check to see what differs, then Pull (bring the remote copy back) or Push (send this copy up). Pull and push always rehearse first, and both ask once more before writing.",
|
|
5490
|
+
"ext.syncPanel.host": "Remote host",
|
|
5491
|
+
"ext.syncPanel.hostPlaceholder": "SSH alias, e.g. greycloud",
|
|
5492
|
+
"ext.syncPanel.hostHint": "The Host name from ~/.ssh/config; saved as soon as you leave the field.",
|
|
5493
|
+
"ext.syncPanel.devices": "List devices",
|
|
5494
|
+
"ext.syncPanel.devicesNone": "No other devices in Tailscale",
|
|
5495
|
+
"ext.syncPanel.devicesUnavailable": "No Tailscale detected - type the SSH alias by hand",
|
|
5496
|
+
"ext.syncPanel.devicesFailed": "Cannot read the device list: {code} {error}",
|
|
5497
|
+
"ext.syncPanel.deviceOnline": "online",
|
|
5498
|
+
"ext.syncPanel.deviceOffline": "offline",
|
|
5499
|
+
"ext.syncPanel.remotePath": "Remote memory dir",
|
|
5500
|
+
"ext.syncPanel.remotePathPlaceholder": "~/shengdian/memory",
|
|
5501
|
+
"ext.syncPanel.pathHint": "Where the memory directory lives on that machine (~ works); the same file name is the same memory.",
|
|
5502
|
+
"ext.syncPanel.saved": "Saved",
|
|
5503
|
+
"ext.syncPanel.check": "Check",
|
|
5504
|
+
"ext.syncPanel.pull": "Pull",
|
|
5505
|
+
"ext.syncPanel.push": "Push",
|
|
5506
|
+
"ext.syncPanel.pullApply": "Run pull",
|
|
5507
|
+
"ext.syncPanel.pushApply": "Run push",
|
|
5508
|
+
"ext.syncPanel.actionsHint": "Pull and push rehearse first: the button becomes Run ... once the report is in, and it asks once more. Check only runs read-only ssh and writes nothing.",
|
|
5509
|
+
"ext.syncPanel.status.idle": "Not run yet",
|
|
5510
|
+
"ext.syncPanel.status.staged": "Rehearsed - ready to run {action}",
|
|
5511
|
+
"ext.syncPanel.status.done": "Last: {action}",
|
|
5512
|
+
"ext.syncPanel.busy.check": "Checking... (a few ssh round trips; ten seconds on a slow link is normal)",
|
|
5513
|
+
"ext.syncPanel.busy.pull": "Rehearsing pull...",
|
|
5514
|
+
"ext.syncPanel.busy.push": "Rehearsing push...",
|
|
5515
|
+
"ext.syncPanel.busy.apply": "Writing...",
|
|
5516
|
+
"ext.syncPanel.busy.devices": "Asking Tailscale...",
|
|
5517
|
+
"ext.syncPanel.busy.save": "Saving...",
|
|
5518
|
+
"ext.syncPanel.empty": "No result yet - run Check to see what differs",
|
|
5519
|
+
"ext.syncPanel.report.title": "Result",
|
|
5520
|
+
"ext.syncPanel.report.preview": "Rehearsal (nothing written)",
|
|
5521
|
+
"ext.syncPanel.report.applied": "Executed (written to disk)",
|
|
5522
|
+
"ext.syncPanel.report.empty": "No report body for this run (the line below is the verdict)",
|
|
5523
|
+
"ext.syncPanel.confirm.pullTitle": "Overwrite the local copy with the remote one?",
|
|
5524
|
+
"ext.syncPanel.confirm.pullBody": "Locally changed files will be replaced by the remote versions and cannot be restored; the old versions go to the backup first. The rehearsal report is above.",
|
|
5525
|
+
"ext.syncPanel.confirm.pushTitle": "Push this copy to the remote?",
|
|
5526
|
+
"ext.syncPanel.confirm.pushBody": "Remote files with the same name will be replaced by this copy and cannot be restored; the old versions go to the backup first. The rehearsal report is above.",
|
|
5527
|
+
"ext.syncPanel.confirm.yes": "Confirm and run",
|
|
5528
|
+
"ext.syncPanel.confirm.busy": "Running...",
|
|
5529
|
+
"ext.syncPanel.gate.title": "First push to this device",
|
|
5530
|
+
"ext.syncPanel.gate.body": "The remote library belongs to {who}. Confirm it is your own library before continuing.",
|
|
5531
|
+
"ext.syncPanel.gate.noDevice": "a device that has never been recorded",
|
|
5532
|
+
"ext.syncPanel.gate.yes": "Confirm and push",
|
|
5533
|
+
"ext.syncPanel.gate.cancel": "Not now",
|
|
5534
|
+
"ext.syncPanel.failed": "This step did not go through: {code} {error}",
|
|
5535
|
+
"ext.syncPanel.notice.check": "Check finished; the differences are in the report below",
|
|
5536
|
+
"ext.syncPanel.notice.preview": "Rehearsal finished - click Run ... once it looks right",
|
|
5537
|
+
"ext.syncPanel.notice.pull": "Pull finished; the report is below",
|
|
5538
|
+
"ext.syncPanel.notice.push": "Push finished; the report is below",
|
|
5539
|
+
"ext.syncPanel.err.noRemote": "This host has no cross-device sync API yet (restart DSH and retry)",
|
|
5540
|
+
"ext.syncPanel.err.noSettings": "This host exposes no settings API",
|
|
5213
5541
|
"lab.title": "MMR de-duplication (experimental)",
|
|
5214
5542
|
"lab.switch": "Enable MMR de-duplication",
|
|
5215
5543
|
"lab.switch.onTitle": "On: injection prefers memories on different topics for the same slots",
|
|
@@ -8567,6 +8895,17 @@ return h("div", { className: "smem-external smem-stack" }, [
|
|
|
8567
8895
|
// 与主卡**平级**:以前它嵌在主卡里面,三层边框(外卡 → 分区 → 内卡)挤在一起(审计 P2-3)。
|
|
8568
8896
|
h(ExternalIntakeCard, { key: "intake", ctx: props.ctx }),
|
|
8569
8897
|
|
|
8898
|
+
// ── 3.1.0 跨设备同步:先把这次同步跑起来 ──
|
|
8899
|
+
// 与来件卡平级,且**排在差异卡之前**:先「触发一次同步」,再「裁决它产生的差异」。
|
|
8900
|
+
// 反过来摆的话,人得先滚过一列待裁决记录,才看得见「其实还没连过远端」这件事。
|
|
8901
|
+
// 卡本身在 `71-sync-panel.js` —— 它自带样式,且要跟 `sync.run` 的三种动作一起看。
|
|
8902
|
+
h(ExternalSyncPanelCard, { key: "syncPanel", ctx: props.ctx }),
|
|
8903
|
+
|
|
8904
|
+
// ── 3.0.0 跨设备同步:撞车的地方 ──
|
|
8905
|
+
// 与来件卡平级(同一件事的两个方向:外面的东西进不进我的库 / 我的库与别的设备
|
|
8906
|
+
// 对不上时听谁的)。卡本身在 `70-ext-sync.js` —— 它要跟 `smemLineDiff` 一起看。
|
|
8907
|
+
h(ExternalSyncCard, { key: "sync", ctx: props.ctx }),
|
|
8908
|
+
|
|
8570
8909
|
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
8571
8910
|
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8572
8911
|
]);
|
|
@@ -8777,6 +9116,1042 @@ return h("div", { className: "smem-external smem-stack" }, [
|
|
|
8777
9116
|
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8778
9117
|
]);
|
|
8779
9118
|
}
|
|
9119
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9120
|
+
// 3.0.0 第 4 步 · 跨设备差异卡(外部访问页)+ 行级 diff 纯函数
|
|
9121
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9122
|
+
//
|
|
9123
|
+
// 这一屏**不发明任何新机制**:记录的形状 / 状态机 / 读写函数都是 `conflicts.js` 里
|
|
9124
|
+
// 跑了很久的那一套(面板「库健康 → 冲突队列」是它的第一个消费者)。这里做的是三件事:
|
|
9125
|
+
// ① 把「跨设备造成的冲突」从同一张队列里**按来源筛出来**(`origin === 'sync'`);
|
|
9126
|
+
// ② 让两份正文能**逐行对比**,而不只是摊开两段并排;
|
|
9127
|
+
// ③ 给三个裁决动作(留本地 / 留远端 / 手工合并)接上界面,语义照设计稿 §3.4。
|
|
9128
|
+
//
|
|
9129
|
+
// 为什么单独一个分片:它同时有**纯函数**(diff)与**一张卡**,而 68 只是「挂载点」。
|
|
9130
|
+
// 顺序上 68 在前、本分片在后 —— 没关系:整个 client 是一个 IIFE,所有分片在首次
|
|
9131
|
+
// 渲染之前就全部求值完,68 的渲染函数里引用 `ExternalSyncCard` 时它早就存在了。
|
|
9132
|
+
|
|
9133
|
+
// ── 行级 diff(纯函数,无 React、无 IO)────────────────────────────────
|
|
9134
|
+
|
|
9135
|
+
/** 单侧超过这个行数就不做逐行对比:只报行数,让用户各自去看(设计稿 §3.3)。 */
|
|
9136
|
+
const SMEM_DIFF_MAX_LINES = 2000;
|
|
9137
|
+
|
|
9138
|
+
/**
|
|
9139
|
+
* 剥掉公共前后缀之后,中间还剩超过这个行数就退化成「整块删 + 整块增」。
|
|
9140
|
+
*
|
|
9141
|
+
* 为什么需要这道退化:LCS 是 O(n·m)。两侧各 800 行就是 64 万个格子(Int32 表约
|
|
9142
|
+
* 2.5 MB),已经是我们愿意在面板里花的极限。退化之后仍然回答得了「哪一段两边不一样」,
|
|
9143
|
+
* 只是不再逐行对齐 —— 而 800 行以上的大段改写,逐行对齐本来也没有人看得下去。
|
|
9144
|
+
*/
|
|
9145
|
+
const SMEM_DIFF_LCS_MAX = 800;
|
|
9146
|
+
|
|
9147
|
+
/** 差异块前后各保留几行上下文;其余折成一行「跳过 N 行」。 */
|
|
9148
|
+
const SMEM_DIFF_CONTEXT = 3;
|
|
9149
|
+
|
|
9150
|
+
/**
|
|
9151
|
+
* 正文 → 行数组。
|
|
9152
|
+
*
|
|
9153
|
+
* 行尾先归一(`\r\n` 与单独 `\r` 都变 `\n`):同一份记忆在两台机器上各存过一次,
|
|
9154
|
+
* 就可能一边 CRLF 一边 LF。按行尾切会得到「每一行都不同」——那是**假差异**,
|
|
9155
|
+
* 比不做 diff 更误导人。
|
|
9156
|
+
*
|
|
9157
|
+
* 末尾那一个换行不算多一行:`"a\n"` 与 `"a"` 都是 1 行(编辑器里的行号也是这么数的)。
|
|
9158
|
+
*/
|
|
9159
|
+
function smemDiffLinesOf(text) {
|
|
9160
|
+
const s = String(text == null ? "" : text).replace(/\r\n?/g, "\n");
|
|
9161
|
+
if (s === "") return [];
|
|
9162
|
+
const lines = s.split("\n");
|
|
9163
|
+
if (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
|
|
9164
|
+
return lines;
|
|
9165
|
+
}
|
|
9166
|
+
|
|
9167
|
+
/**
|
|
9168
|
+
* 两段(已剥掉公共前后缀的)行数组的 LCS 对齐。
|
|
9169
|
+
*
|
|
9170
|
+
* 返回 `{ tag: 'ctx'|'del'|'ins', text, i?, j? }`,`i` / `j` 是**段内**下标,
|
|
9171
|
+
* 由调用方加上前后缀的偏移换成 1 起算的行号。
|
|
9172
|
+
*/
|
|
9173
|
+
function smemDiffLcsOf(a, b) {
|
|
9174
|
+
const n = a.length;
|
|
9175
|
+
const m = b.length;
|
|
9176
|
+
const w = m + 1;
|
|
9177
|
+
// 从后往前填:dp[i][j] = a[i..] 与 b[j..] 的 LCS 长度。表 ≤ (800+1)² 个 Int32。
|
|
9178
|
+
const dp = new Int32Array((n + 1) * w);
|
|
9179
|
+
for (let i = n - 1; i >= 0; i--) {
|
|
9180
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
9181
|
+
dp[i * w + j] = a[i] === b[j]
|
|
9182
|
+
? dp[(i + 1) * w + (j + 1)] + 1
|
|
9183
|
+
: Math.max(dp[(i + 1) * w + j], dp[i * w + (j + 1)]);
|
|
9184
|
+
}
|
|
9185
|
+
}
|
|
9186
|
+
const out = [];
|
|
9187
|
+
let i = 0;
|
|
9188
|
+
let j = 0;
|
|
9189
|
+
while (i < n && j < m) {
|
|
9190
|
+
if (a[i] === b[j]) { out.push({ tag: "ctx", text: a[i], i: i, j: j }); i++; j++; }
|
|
9191
|
+
// 相等时先「删」再「增」:差异块里永远是 − 在上、+ 在下(设计稿 §3.3 的示意顺序)。
|
|
9192
|
+
else if (dp[(i + 1) * w + j] >= dp[i * w + (j + 1)]) { out.push({ tag: "del", text: a[i], i: i }); i++; }
|
|
9193
|
+
else { out.push({ tag: "ins", text: b[j], j: j }); j++; }
|
|
9194
|
+
}
|
|
9195
|
+
while (i < n) { out.push({ tag: "del", text: a[i], i: i }); i++; }
|
|
9196
|
+
while (j < m) { out.push({ tag: "ins", text: b[j], j: j }); j++; }
|
|
9197
|
+
return out;
|
|
9198
|
+
}
|
|
9199
|
+
|
|
9200
|
+
/**
|
|
9201
|
+
* 退化路径:整块删 + 整块增,不做逐行对齐(见 `SMEM_DIFF_LCS_MAX` 的理由)。
|
|
9202
|
+
*
|
|
9203
|
+
* 保留它是**有意的**:一段 2000 行的改写用 LCS 会慢,而结果对人也没有意义 ——
|
|
9204
|
+
* 「这一整段被换掉了」就是事实。退化时行号仍然准确(删的行给本地号、增的行给远端号)。
|
|
9205
|
+
*/
|
|
9206
|
+
function smemDiffBlockOf(a, b) {
|
|
9207
|
+
const out = [];
|
|
9208
|
+
for (let i = 0; i < a.length; i++) out.push({ tag: "del", text: a[i], i: i });
|
|
9209
|
+
for (let j = 0; j < b.length; j++) out.push({ tag: "ins", text: b[j], j: j });
|
|
9210
|
+
return out;
|
|
9211
|
+
}
|
|
9212
|
+
|
|
9213
|
+
/**
|
|
9214
|
+
* 行级逐行对比(U3 / U4 的被测对象)。
|
|
9215
|
+
*
|
|
9216
|
+
* @param {string} aText 「本地」那一版(`conflict.read` 的 `onDisk`)
|
|
9217
|
+
* @param {string} bText 「远端」那一版(`conflict.read` 的 `incoming`)
|
|
9218
|
+
* @param {{ context?: number | 'all' }} [options] 上下文行数,默认 3;`'all'` = 不折叠
|
|
9219
|
+
* @returns {{
|
|
9220
|
+
* tooBig: boolean,
|
|
9221
|
+
* aLines: number, bLines: number,
|
|
9222
|
+
* same: boolean,
|
|
9223
|
+
* ops: Array<{tag: 'ctx'|'del'|'ins'|'skip', text: string, aLine?: number, bLine?: number, count?: number}>,
|
|
9224
|
+
* full: Array<{tag: 'ctx'|'del'|'ins', text: string, aLine?: number, bLine?: number}>,
|
|
9225
|
+
* folded: number,
|
|
9226
|
+
* }}
|
|
9227
|
+
*
|
|
9228
|
+
* 三种「特殊返回」都写在这里,免得调用方各猜一套:
|
|
9229
|
+
* - `tooBig: true` → 任一侧超过 2000 行,`ops` / `full` 为空,界面只说行数;
|
|
9230
|
+
* - `same: true` → 两侧逐行相同,`ops` 为空(铺一屏一样的正文没有意义),`full` 是全文;
|
|
9231
|
+
* - `tag: 'skip'` → 折叠标记,`count` 是被跳过(两侧一致)的行数,`text` 恒为空串。
|
|
9232
|
+
*
|
|
9233
|
+
* 行号一律 1 起算:`del` 给 `aLine`、`ins` 给 `bLine`、`ctx` 两个都给。
|
|
9234
|
+
*/
|
|
9235
|
+
function smemLineDiff(aText, bText, options) {
|
|
9236
|
+
const opts = options || {};
|
|
9237
|
+
const context = opts.context === "all"
|
|
9238
|
+
? -1
|
|
9239
|
+
: (typeof opts.context === "number" && opts.context >= 0
|
|
9240
|
+
? Math.floor(opts.context)
|
|
9241
|
+
: SMEM_DIFF_CONTEXT);
|
|
9242
|
+
const a = smemDiffLinesOf(aText);
|
|
9243
|
+
const b = smemDiffLinesOf(bText);
|
|
9244
|
+
|
|
9245
|
+
if (a.length > SMEM_DIFF_MAX_LINES || b.length > SMEM_DIFF_MAX_LINES) {
|
|
9246
|
+
return { tooBig: true, aLines: a.length, bLines: b.length, same: false, ops: [], full: [], folded: 0 };
|
|
9247
|
+
}
|
|
9248
|
+
|
|
9249
|
+
// ① 公共前后缀:跨设备冲突绝大多数只是「某一处不一样」,先剥掉两头,LCS 只看见那一段。
|
|
9250
|
+
const shorter = Math.min(a.length, b.length);
|
|
9251
|
+
let head = 0;
|
|
9252
|
+
while (head < shorter && a[head] === b[head]) head++;
|
|
9253
|
+
let tail = 0;
|
|
9254
|
+
while (tail < shorter - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail++;
|
|
9255
|
+
|
|
9256
|
+
const amid = a.slice(head, a.length - tail);
|
|
9257
|
+
const bmid = b.slice(head, b.length - tail);
|
|
9258
|
+
const rows = [];
|
|
9259
|
+
for (let k = 0; k < head; k++) rows.push({ tag: "ctx", text: a[k], aLine: k + 1, bLine: k + 1 });
|
|
9260
|
+
const mid = (amid.length > SMEM_DIFF_LCS_MAX || bmid.length > SMEM_DIFF_LCS_MAX)
|
|
9261
|
+
? smemDiffBlockOf(amid, bmid)
|
|
9262
|
+
: smemDiffLcsOf(amid, bmid);
|
|
9263
|
+
for (let k = 0; k < mid.length; k++) {
|
|
9264
|
+
const m = mid[k];
|
|
9265
|
+
if (m.tag === "ctx") rows.push({ tag: "ctx", text: m.text, aLine: head + m.i + 1, bLine: head + m.j + 1 });
|
|
9266
|
+
else if (m.tag === "del") rows.push({ tag: "del", text: m.text, aLine: head + m.i + 1 });
|
|
9267
|
+
else rows.push({ tag: "ins", text: m.text, bLine: head + m.j + 1 });
|
|
9268
|
+
}
|
|
9269
|
+
// 后缀:a 的第 k 行对应 b 的第 `bLen - (aLen - k)` 行(两边尾部是同一段)。
|
|
9270
|
+
for (let k = a.length - tail; k < a.length; k++) {
|
|
9271
|
+
rows.push({ tag: "ctx", text: a[k], aLine: k + 1, bLine: b.length - (a.length - k) + 1 });
|
|
9272
|
+
}
|
|
9273
|
+
|
|
9274
|
+
const same = rows.every(function (r) { return r.tag === "ctx"; });
|
|
9275
|
+
if (same) {
|
|
9276
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: true, ops: [], full: rows, folded: 0 };
|
|
9277
|
+
}
|
|
9278
|
+
if (context < 0) {
|
|
9279
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: false, ops: rows.slice(), full: rows, folded: 0 };
|
|
9280
|
+
}
|
|
9281
|
+
|
|
9282
|
+
// ② 只留差异块 ±context 行:**逐个**差异行标记,两个差异块离得远时中间会折起来。
|
|
9283
|
+
const keep = [];
|
|
9284
|
+
for (let i = 0; i < rows.length; i++) keep.push(false);
|
|
9285
|
+
for (let i = 0; i < rows.length; i++) {
|
|
9286
|
+
if (rows[i].tag === "ctx") continue;
|
|
9287
|
+
const from = Math.max(0, i - context);
|
|
9288
|
+
const to = Math.min(rows.length - 1, i + context);
|
|
9289
|
+
for (let k = from; k <= to; k++) keep[k] = true;
|
|
9290
|
+
}
|
|
9291
|
+
const ops = [];
|
|
9292
|
+
let folded = 0;
|
|
9293
|
+
let skipped = 0;
|
|
9294
|
+
for (let i = 0; i < rows.length; i++) {
|
|
9295
|
+
if (keep[i]) {
|
|
9296
|
+
if (skipped > 0) { ops.push({ tag: "skip", text: "", count: skipped }); folded += skipped; skipped = 0; }
|
|
9297
|
+
ops.push(rows[i]);
|
|
9298
|
+
} else skipped++;
|
|
9299
|
+
}
|
|
9300
|
+
if (skipped > 0) { ops.push({ tag: "skip", text: "", count: skipped }); folded += skipped; }
|
|
9301
|
+
|
|
9302
|
+
return { tooBig: false, aLines: a.length, bLines: b.length, same: false, ops: ops, full: rows, folded: folded };
|
|
9303
|
+
}
|
|
9304
|
+
|
|
9305
|
+
// ── 卡片自己的样式 ─────────────────────────────────────────────────────
|
|
9306
|
+
//
|
|
9307
|
+
// 为什么不并进 `40-styles.js` 的那一份:那一份是「拼接时就固定的字符串」,本分片在它
|
|
9308
|
+
// 之后求值,改不动它。所以这里照 `11-starmap-styles.js` 的老办法自带一段、幂等注入
|
|
9309
|
+
// (同一个 `data-plugin-css` 只挂一次)—— 唯一的代价是它进快照里的 `<style>`,
|
|
9310
|
+
// 于是改这里会让**全部屏**的哈希一起变(快照套件头注已说明这一点)。
|
|
9311
|
+
const EXT_SYNC_CSS = [
|
|
9312
|
+
".smem-sync-list{display:flex;flex-direction:column;gap:8px;}",
|
|
9313
|
+
".smem-sync-item{display:flex;flex-direction:column;gap:4px;padding:8px 10px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;}",
|
|
9314
|
+
".smem-sync-line{display:flex;align-items:center;gap:4px;flex-wrap:wrap;font-size:12px;}",
|
|
9315
|
+
".smem-sync-host{font-weight:600;color:var(--dsw-alias-label-primary);}",
|
|
9316
|
+
".smem-sync-file{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:var(--dsw-alias-label-primary);overflow-wrap:anywhere;}",
|
|
9317
|
+
".smem-sync-why{font-size:11.5px;color:var(--dsw-alias-label-secondary);overflow-wrap:anywhere;}",
|
|
9318
|
+
".smem-sync-acts{display:flex;gap:6px;flex-wrap:wrap;margin-top:2px;}",
|
|
9319
|
+
".smem-sync-diff{display:flex;flex-direction:column;gap:4px;margin-top:4px;}",
|
|
9320
|
+
".smem-sync-diffhead{display:flex;align-items:center;gap:8px;flex-wrap:wrap;font-size:11.5px;}",
|
|
9321
|
+
// 单栏 diff 流:行号 / 符号 / 正文三列,**不并排双栏**(面板 420px 起,双栏中文会碎成一列字)。
|
|
9322
|
+
".smem-sync-diffbody{border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-layer-1);max-height:320px;overflow:auto;padding:4px 0;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:11.5px;line-height:1.55;}",
|
|
9323
|
+
".smem-diff-row{display:flex;gap:6px;padding:0 8px;white-space:pre-wrap;overflow-wrap:anywhere;}",
|
|
9324
|
+
".smem-diff-n{flex:none;width:34px;text-align:right;color:var(--dsw-alias-label-secondary);font-variant-numeric:tabular-nums;}",
|
|
9325
|
+
".smem-diff-sign{flex:none;width:9px;text-align:center;color:var(--dsw-alias-label-secondary);}",
|
|
9326
|
+
".smem-diff-text{flex:1;min-width:0;color:var(--dsw-alias-label-primary);}",
|
|
9327
|
+
// 差异行只强调**符号那一位**:整行染红会让一屏里几十行都在喊(彩色面积要小)。
|
|
9328
|
+
".smem-diff-del{background:var(--dsw-alias-bg-layer-2);}",
|
|
9329
|
+
".smem-diff-ins{background:var(--dsw-alias-bg-layer-2);}",
|
|
9330
|
+
".smem-diff-del .smem-diff-sign{color:var(--dsw-alias-state-error-primary);font-weight:600;}",
|
|
9331
|
+
".smem-diff-ins .smem-diff-sign{color:var(--dsw-alias-state-success-primary);font-weight:600;}",
|
|
9332
|
+
".smem-diff-skip{padding:2px 8px;font-size:11px;color:var(--dsw-alias-label-secondary);}",
|
|
9333
|
+
].join("\n");
|
|
9334
|
+
|
|
9335
|
+
/** 幂等注入(同一个 id 只挂一次;`document` 不在时安静跳过,测试环境就这么用)。 */
|
|
9336
|
+
function injectExtSyncStyle() {
|
|
9337
|
+
try {
|
|
9338
|
+
if (typeof document === "undefined" || !document || typeof document.createElement !== "function") return;
|
|
9339
|
+
const tagId = "sage-mem/ext-sync.css";
|
|
9340
|
+
if (typeof document.querySelector === "function"
|
|
9341
|
+
&& document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") !== null) return;
|
|
9342
|
+
const tag = document.createElement("style");
|
|
9343
|
+
if (tag.dataset) { tag.dataset.plugin = "sage-mem"; tag.dataset.pluginCss = tagId; }
|
|
9344
|
+
tag.textContent = EXT_SYNC_CSS;
|
|
9345
|
+
if (document.head && typeof document.head.appendChild === "function") document.head.appendChild(tag);
|
|
9346
|
+
} catch (e) { /* 样式注入失败不该让界面白屏 */ }
|
|
9347
|
+
}
|
|
9348
|
+
injectExtSyncStyle();
|
|
9349
|
+
|
|
9350
|
+
// ── 卡片 ───────────────────────────────────────────────────────────────
|
|
9351
|
+
|
|
9352
|
+
/**
|
|
9353
|
+
* 「跨设备差异」(3.0.0 第 4 步)—— 挂在「外部访问」页的来件卡之后。
|
|
9354
|
+
*
|
|
9355
|
+
* 三条不许变的产品规则:
|
|
9356
|
+
* 1. **只列 `origin === 'sync'` 且还开着的记录**。`autodream` 造的冲突、以及没有
|
|
9357
|
+
* `origin` 的老记录一条都不许进来 —— 后者是「同步撞车」吗?不知道,那就别猜。
|
|
9358
|
+
* 也不能靠 `kind` 猜:拉取与推送两边都写 `kind:'external_change'`,而 autodream
|
|
9359
|
+
* 的 `external_change` 语义上也是「两边都动过」。类别词在 `note` 里,原样显示。
|
|
9360
|
+
* 2. **留本地 / 留远端必须二次确认**(两个动作都会盖掉一份真实文件),「手工合并」
|
|
9361
|
+
* 不弹模态 —— 它一个字节都不写。
|
|
9362
|
+
* 3. **失败不许吞**:`code` + `error` 落在**出错的那一条卡片里**,不是整屏一条。
|
|
9363
|
+
*
|
|
9364
|
+
* 宿主没有 conflict 面(旧宿主)时整张卡不渲染:这一卡的全部数据都来自那个面,
|
|
9365
|
+
* 画一个空壳只会让人以为「同步功能坏了」。
|
|
9366
|
+
*/
|
|
9367
|
+
function ExternalSyncCard(props) {
|
|
9368
|
+
const remote = stableRemote(props.ctx, "remote.conflict");
|
|
9369
|
+
const listable = !!remote && typeof remote.list === "function";
|
|
9370
|
+
const sItems = react.useState(null); const items = sItems[0], setItems = sItems[1];
|
|
9371
|
+
const sErr = react.useState(""); const err = sErr[0], setErr = sErr[1];
|
|
9372
|
+
const sNotice = react.useState(""); const notice = sNotice[0], setNotice = sNotice[1];
|
|
9373
|
+
const sBusy = react.useState(""); const busy = sBusy[0], setBusy = sBusy[1];
|
|
9374
|
+
const sOpen = react.useState({}); const open = sOpen[0], setOpen = sOpen[1];
|
|
9375
|
+
const sDiffs = react.useState({}); const diffs = sDiffs[0], setDiffs = sDiffs[1];
|
|
9376
|
+
const sAll = react.useState({}); const allRows = sAll[0], setAllRows = sAll[1];
|
|
9377
|
+
const sConfirm = react.useState(null); const confirm = sConfirm[0], setConfirm = sConfirm[1];
|
|
9378
|
+
const sItemErr = react.useState({}); const itemErr = sItemErr[0], setItemErr = sItemErr[1];
|
|
9379
|
+
|
|
9380
|
+
/** 拉队列并**只留跨设备那几条**。 */
|
|
9381
|
+
function load() {
|
|
9382
|
+
if (!listable) return;
|
|
9383
|
+
Promise.resolve(remote.list()).then(function (res) {
|
|
9384
|
+
const d = unwrap(res) || {};
|
|
9385
|
+
if (d.ok === false) {
|
|
9386
|
+
setItems(null);
|
|
9387
|
+
setErr(t("ext.sync.err.list", { code: String(d.code || "?"), error: String(d.error || "") }));
|
|
9388
|
+
return;
|
|
9389
|
+
}
|
|
9390
|
+
const rows = Array.isArray(d.items) ? d.items : [];
|
|
9391
|
+
setItems(rows.filter(function (it) {
|
|
9392
|
+
return !!it && it.origin === "sync" && it.status === "open";
|
|
9393
|
+
}));
|
|
9394
|
+
setErr("");
|
|
9395
|
+
}, function (e) {
|
|
9396
|
+
setItems(null);
|
|
9397
|
+
setErr(t("ext.sync.err.list", { code: "", error: detail(e) }));
|
|
9398
|
+
});
|
|
9399
|
+
}
|
|
9400
|
+
|
|
9401
|
+
react.useEffect(function () { load(); }, []);
|
|
9402
|
+
|
|
9403
|
+
/** 记一条**这一条**的错。 */
|
|
9404
|
+
function failOf(id, text) {
|
|
9405
|
+
setItemErr(function (prev) {
|
|
9406
|
+
const next = Object.assign({}, prev);
|
|
9407
|
+
next[id] = text;
|
|
9408
|
+
return next;
|
|
9409
|
+
});
|
|
9410
|
+
}
|
|
9411
|
+
|
|
9412
|
+
function clearOf(id) {
|
|
9413
|
+
setItemErr(function (prev) {
|
|
9414
|
+
if (!Object.prototype.hasOwnProperty.call(prev, id)) return prev;
|
|
9415
|
+
const next = Object.assign({}, prev);
|
|
9416
|
+
delete next[id];
|
|
9417
|
+
return next;
|
|
9418
|
+
});
|
|
9419
|
+
}
|
|
9420
|
+
|
|
9421
|
+
/** 展开 / 收起逐行对比。第一次展开才去取两份正文(列表里不搬运 96 KB 的记录)。 */
|
|
9422
|
+
function toggleDiff(item) {
|
|
9423
|
+
const id = String(item && item.id ? item.id : "");
|
|
9424
|
+
if (id === "") return;
|
|
9425
|
+
if (open[id] === true) {
|
|
9426
|
+
const next = Object.assign({}, open);
|
|
9427
|
+
delete next[id];
|
|
9428
|
+
setOpen(next);
|
|
9429
|
+
return;
|
|
9430
|
+
}
|
|
9431
|
+
const opened = Object.assign({}, open);
|
|
9432
|
+
opened[id] = true;
|
|
9433
|
+
setOpen(opened);
|
|
9434
|
+
if (diffs[id]) return; // 取过就不再打一次 RPC
|
|
9435
|
+
if (!remote || typeof remote.read !== "function") {
|
|
9436
|
+
failOf(id, t("ext.sync.err.read", { code: "unsupported", error: "conflict.read" }));
|
|
9437
|
+
return;
|
|
9438
|
+
}
|
|
9439
|
+
setBusy(id);
|
|
9440
|
+
clearOf(id);
|
|
9441
|
+
Promise.resolve(remote.read({ id: id })).then(function (res) {
|
|
9442
|
+
setBusy("");
|
|
9443
|
+
const d = unwrap(res) || {};
|
|
9444
|
+
if (d.ok === false || !d.record) {
|
|
9445
|
+
failOf(id, t("ext.sync.err.read", { code: String(d.code || "?"), error: String(d.error || "") }));
|
|
9446
|
+
return;
|
|
9447
|
+
}
|
|
9448
|
+
// a = 本地(onDisk)、b = 远端(incoming):看的是「本地这一版 vs 远端那一版」。
|
|
9449
|
+
const diff = smemLineDiff(String(d.record.onDisk || ""), String(d.record.incoming || ""));
|
|
9450
|
+
setDiffs(function (prev) {
|
|
9451
|
+
const next = Object.assign({}, prev);
|
|
9452
|
+
next[id] = diff;
|
|
9453
|
+
return next;
|
|
9454
|
+
});
|
|
9455
|
+
}, function (e) {
|
|
9456
|
+
setBusy("");
|
|
9457
|
+
failOf(id, t("ext.sync.err.read", { code: "", error: detail(e) }));
|
|
9458
|
+
});
|
|
9459
|
+
}
|
|
9460
|
+
|
|
9461
|
+
/** 三个动作的唯一出口。`choice` 的语义在宿主 `sync/resolve.js`,这里只负责传。 */
|
|
9462
|
+
function resolve(id, choice) {
|
|
9463
|
+
if (!remote || typeof remote.applyResolution !== "function") {
|
|
9464
|
+
setConfirm(null);
|
|
9465
|
+
failOf(id, t("ext.sync.err.noAct"));
|
|
9466
|
+
return;
|
|
9467
|
+
}
|
|
9468
|
+
setBusy(id);
|
|
9469
|
+
setNotice("");
|
|
9470
|
+
clearOf(id);
|
|
9471
|
+
Promise.resolve(remote.applyResolution({ id: id, choice: choice })).then(function (res) {
|
|
9472
|
+
setBusy("");
|
|
9473
|
+
setConfirm(null);
|
|
9474
|
+
const r = unwrap(res) || {};
|
|
9475
|
+
if (r.ok === false) {
|
|
9476
|
+
failOf(id, t("ext.sync.err.act", { code: String(r.code || "?"), error: String(r.error || "") }));
|
|
9477
|
+
load();
|
|
9478
|
+
return;
|
|
9479
|
+
}
|
|
9480
|
+
setNotice(choice === "manual"
|
|
9481
|
+
? t("ext.sync.notice.manual")
|
|
9482
|
+
: (choice === "local" ? t("ext.sync.notice.local") : t("ext.sync.notice.remote")));
|
|
9483
|
+
// 基线没跟上是**部分成功**:文件已经写成,所以不能说「失败了」;但它会
|
|
9484
|
+
// 让同一个冲突在下次检查时复活,所以也不能不提(设计稿 §4.1 / §4.3)。
|
|
9485
|
+
if (r.baseError) setErr(t("ext.sync.err.base", { error: String(r.baseError) }));
|
|
9486
|
+
load();
|
|
9487
|
+
}, function (e) {
|
|
9488
|
+
setBusy("");
|
|
9489
|
+
setConfirm(null);
|
|
9490
|
+
failOf(id, t("ext.sync.err.act", { code: "", error: detail(e) }));
|
|
9491
|
+
});
|
|
9492
|
+
}
|
|
9493
|
+
|
|
9494
|
+
/** 确认框:只接「会写文件」的两个动作;文案必须说清哪一份会被盖掉。 */
|
|
9495
|
+
function confirmBlock() {
|
|
9496
|
+
if (!confirm) return null;
|
|
9497
|
+
const isLocal = confirm.choice === "local";
|
|
9498
|
+
return h(Confirm, {
|
|
9499
|
+
key: "confirm",
|
|
9500
|
+
title: isLocal ? t("ext.sync.confirm.localTitle") : t("ext.sync.confirm.remoteTitle"),
|
|
9501
|
+
confirmLabel: t("ext.sync.confirm.yes"),
|
|
9502
|
+
busyLabel: t("ext.sync.busy"),
|
|
9503
|
+
busy: busy === confirm.id,
|
|
9504
|
+
danger: true,
|
|
9505
|
+
onCancel: function () { setConfirm(null); },
|
|
9506
|
+
onConfirm: function () { resolve(confirm.id, confirm.choice); },
|
|
9507
|
+
}, [
|
|
9508
|
+
h("div", { key: "body" }, isLocal ? t("ext.sync.confirm.localBody") : t("ext.sync.confirm.remoteBody")),
|
|
9509
|
+
]);
|
|
9510
|
+
}
|
|
9511
|
+
|
|
9512
|
+
/** 单栏 diff 流的一行:行号 / 符号 / 正文。 */
|
|
9513
|
+
function diffRowOf(row, i) {
|
|
9514
|
+
if (row.tag === "skip") {
|
|
9515
|
+
return h("div", { className: "smem-diff-row smem-diff-skip", key: "k" + i },
|
|
9516
|
+
t("ext.sync.skip", { count: row.count }));
|
|
9517
|
+
}
|
|
9518
|
+
const num = row.tag === "ins" ? row.bLine : row.aLine;
|
|
9519
|
+
const sign = row.tag === "del" ? "-" : (row.tag === "ins" ? "+" : " ");
|
|
9520
|
+
return h("div", { className: "smem-diff-row smem-diff-" + row.tag, key: "r" + i }, [
|
|
9521
|
+
h("span", { className: "smem-diff-n", key: "n" }, num == null ? "" : String(num)),
|
|
9522
|
+
h("span", { className: "smem-diff-sign", key: "s" }, sign),
|
|
9523
|
+
// 空行给一个空格:否则那一行高度塌掉,diff 看起来像少了几行。
|
|
9524
|
+
h("span", { className: "smem-diff-text", key: "t" }, row.text === "" ? " " : row.text),
|
|
9525
|
+
]);
|
|
9526
|
+
}
|
|
9527
|
+
|
|
9528
|
+
function diffBoxOf(item) {
|
|
9529
|
+
const id = String(item && item.id ? item.id : "");
|
|
9530
|
+
const diff = diffs[id] || null;
|
|
9531
|
+
if (!diff) {
|
|
9532
|
+
return busy === id
|
|
9533
|
+
? h("div", { className: "smem-sync-diff smem-muted", key: "diff" }, t("ext.sync.diffReading"))
|
|
9534
|
+
: null;
|
|
9535
|
+
}
|
|
9536
|
+
if (diff.tooBig) {
|
|
9537
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9538
|
+
h("div", { className: "smem-muted", key: "big" },
|
|
9539
|
+
t("ext.sync.tooBig", { a: diff.aLines, b: diff.bLines })),
|
|
9540
|
+
]);
|
|
9541
|
+
}
|
|
9542
|
+
const head = h("div", { className: "smem-sync-diffhead", key: "head" }, [
|
|
9543
|
+
h("span", { className: "smem-muted", key: "n" },
|
|
9544
|
+
t("ext.sync.diffLines", { a: diff.aLines, b: diff.bLines })),
|
|
9545
|
+
h("span", { className: "smem-muted", key: "side" },
|
|
9546
|
+
t("ext.sync.side.local") + " - / " + t("ext.sync.side.remote") + " +"),
|
|
9547
|
+
diff.same
|
|
9548
|
+
? null
|
|
9549
|
+
: h("button", {
|
|
9550
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "all",
|
|
9551
|
+
onClick: function () {
|
|
9552
|
+
const next = Object.assign({}, allRows);
|
|
9553
|
+
next[id] = true;
|
|
9554
|
+
setAllRows(next);
|
|
9555
|
+
},
|
|
9556
|
+
}, t("ext.sync.diffAll")),
|
|
9557
|
+
allRows[id] === true
|
|
9558
|
+
? h("button", {
|
|
9559
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "fold",
|
|
9560
|
+
onClick: function () {
|
|
9561
|
+
const next = Object.assign({}, allRows);
|
|
9562
|
+
delete next[id];
|
|
9563
|
+
setAllRows(next);
|
|
9564
|
+
},
|
|
9565
|
+
}, t("ext.sync.diffFold"))
|
|
9566
|
+
: null,
|
|
9567
|
+
]);
|
|
9568
|
+
if (diff.same) {
|
|
9569
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9570
|
+
head,
|
|
9571
|
+
h("div", { className: "smem-muted", key: "same" }, t("ext.sync.same")),
|
|
9572
|
+
]);
|
|
9573
|
+
}
|
|
9574
|
+
const rows = allRows[id] === true ? diff.full : diff.ops;
|
|
9575
|
+
return h("div", { className: "smem-sync-diff", key: "diff" }, [
|
|
9576
|
+
head,
|
|
9577
|
+
h("div", { className: "smem-sync-diffbody", key: "body" }, rows.map(diffRowOf)),
|
|
9578
|
+
]);
|
|
9579
|
+
}
|
|
9580
|
+
|
|
9581
|
+
function renderItem(item) {
|
|
9582
|
+
const id = String(item.id || "");
|
|
9583
|
+
const acting = busy === id;
|
|
9584
|
+
const expanded = open[id] === true;
|
|
9585
|
+
const canAct = !!remote && typeof remote.applyResolution === "function";
|
|
9586
|
+
const actTitle = canAct ? undefined : t("ext.sync.err.noAct");
|
|
9587
|
+
const parts = [];
|
|
9588
|
+
|
|
9589
|
+
// 折叠态:`<host> · <file>` + note。类别词在 note 里(宿主写的),不在这里按 kind 猜。
|
|
9590
|
+
parts.push(h("div", { className: "smem-sync-line", key: "line" }, [
|
|
9591
|
+
h("span", { className: "smem-sync-host", key: "h" }, String(item.host || "?")),
|
|
9592
|
+
h("span", { className: "smem-muted", key: "sep" }, "·"),
|
|
9593
|
+
h("span", { className: "smem-sync-file", key: "f" }, String(item.file || "")),
|
|
9594
|
+
item.at ? h("span", { className: "smem-muted", key: "at" }, String(item.at)) : null,
|
|
9595
|
+
]));
|
|
9596
|
+
if (item.note) parts.push(h("div", { className: "smem-sync-why", key: "why" }, String(item.note)));
|
|
9597
|
+
if (item.remotePath) {
|
|
9598
|
+
parts.push(h("div", { className: "smem-sync-why", key: "path" },
|
|
9599
|
+
t("ext.sync.path", { path: String(item.remotePath) })));
|
|
9600
|
+
}
|
|
9601
|
+
if (item.truncated) {
|
|
9602
|
+
parts.push(h("div", { className: "smem-conf-warn", key: "tr" }, t("ext.sync.truncated")));
|
|
9603
|
+
}
|
|
9604
|
+
|
|
9605
|
+
parts.push(h("div", { className: "smem-sync-acts", key: "acts" }, [
|
|
9606
|
+
h("button", {
|
|
9607
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "diff", disabled: acting,
|
|
9608
|
+
onClick: function () { toggleDiff(item); },
|
|
9609
|
+
}, expanded ? t("ext.sync.diffOff") : t("ext.sync.diffOn")),
|
|
9610
|
+
h("button", {
|
|
9611
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "local",
|
|
9612
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9613
|
+
onClick: function () { setConfirm({ id: id, choice: "local" }); },
|
|
9614
|
+
}, t("ext.sync.local")),
|
|
9615
|
+
h("button", {
|
|
9616
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "remote",
|
|
9617
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9618
|
+
onClick: function () { setConfirm({ id: id, choice: "remote" }); },
|
|
9619
|
+
}, t("ext.sync.remote")),
|
|
9620
|
+
// 手工合并不写文件 → 不设二次确认(那也是「多点一次」的成本,换不来任何保护)。
|
|
9621
|
+
h("button", {
|
|
9622
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "manual",
|
|
9623
|
+
disabled: acting || !canAct, title: actTitle,
|
|
9624
|
+
onClick: function () { resolve(id, "manual"); },
|
|
9625
|
+
}, t("ext.sync.manual")),
|
|
9626
|
+
]));
|
|
9627
|
+
|
|
9628
|
+
if (expanded) parts.push(diffBoxOf(item));
|
|
9629
|
+
if (itemErr[id]) parts.push(h("div", { className: "smem-err", key: "ierr" }, "⚠ " + itemErr[id]));
|
|
9630
|
+
return h("div", { className: "smem-sync-item", key: id }, parts);
|
|
9631
|
+
}
|
|
9632
|
+
|
|
9633
|
+
if (!listable) return null;
|
|
9634
|
+
const list = items || [];
|
|
9635
|
+
|
|
9636
|
+
return h("div", { className: "smem-sync smem-module-card" }, [
|
|
9637
|
+
h("div", { className: "smem-modcard-head", key: "head" }, [
|
|
9638
|
+
h("span", { className: "smem-modcard-title", key: "t" }, t("ext.sync.title")),
|
|
9639
|
+
// 数字只在**真的取到队列**之后才算:加载中 / 失败时写「0 条」等于撒谎。
|
|
9640
|
+
h("span", { className: "smem-modcard-status", key: "s" },
|
|
9641
|
+
items === null || list.length === 0
|
|
9642
|
+
? t("ext.sync.countNone")
|
|
9643
|
+
: t("ext.sync.count", { count: list.length })),
|
|
9644
|
+
h("button", {
|
|
9645
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "re",
|
|
9646
|
+
disabled: busy !== "", onClick: function () { load(); },
|
|
9647
|
+
}, t("ext.sync.refresh")),
|
|
9648
|
+
]),
|
|
9649
|
+
h("div", { className: "smem-autodream-note", key: "what" }, t("ext.sync.what")),
|
|
9650
|
+
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
9651
|
+
items !== null && list.length === 0
|
|
9652
|
+
? h("div", { className: "smem-muted", key: "empty" }, t("ext.sync.empty"))
|
|
9653
|
+
: null,
|
|
9654
|
+
list.length > 0
|
|
9655
|
+
? h("div", { className: "smem-sync-list", key: "list" }, list.map(renderItem))
|
|
9656
|
+
: null,
|
|
9657
|
+
confirmBlock(),
|
|
9658
|
+
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
9659
|
+
]);
|
|
9660
|
+
}
|
|
9661
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9662
|
+
// 3.1.0 第 5 步 · 跨设备同步卡(「外部访问」页)+ 它自己的样式
|
|
9663
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
9664
|
+
//
|
|
9665
|
+
// 这张卡回答「怎么让两台机器对上」:填主机与远端目录 → 触发一次同步 → 再回下面那张
|
|
9666
|
+
// 「跨设备差异」(`70-ext-sync.js`)理它造出来的差异。**顺序是有理由的:先跑,再裁决。**
|
|
9667
|
+
//
|
|
9668
|
+
// 三条不许变的产品规则:
|
|
9669
|
+
// 1. **不主动起子进程**。设备候选(`sync.devices()`)背后是一条 `tailscale`,所以只在
|
|
9670
|
+
// 用户点「看看有哪些设备」时才调 —— 进页面就打,等于每次开设置都付一次进程开销。
|
|
9671
|
+
// `available:false`(没装 / 没登录 / 没别的机子)不是错误,是一句人话 + 照旧手打。
|
|
9672
|
+
// 2. **拉取 / 推送先预演**(`apply:false`)。报告出来之后那颗按钮才变成「执行…」,
|
|
9673
|
+
// 点了还要过二次确认模态 —— 它是这一屏**唯一会覆写真实文件**的路径。
|
|
9674
|
+
// 3. **失败不许吞**:`code` + `error` 落在卡片里。`need_confirm` 不是失败,是一句
|
|
9675
|
+
// 「首次向这台设备推送」+ 一颗 `[确认并推送]`(带 `confirm:true, apply:true` 重跑)。
|
|
9676
|
+
//
|
|
9677
|
+
// 数据全走已有的 remote:
|
|
9678
|
+
// - `memory.getSettings()` → `.sync = { host, remotePath }` 与 `.syncDiagnostics`
|
|
9679
|
+
// - `memory.setSettings({ sync: { host } })` → 失焦即存,**一次只带一个键**
|
|
9680
|
+
// - `sync.devices()` → `{ ok, available, devices, reason?, code?, error? }`
|
|
9681
|
+
// - `sync.run({ action, apply?, confirm? })` → `{ ok, action, applied, report, gate?, code?, error? }`
|
|
9682
|
+
//
|
|
9683
|
+
// `report` 是 markdown。这里**不自己写一个 markdown 解析器**:星图那一套
|
|
9684
|
+
// `renderMarkdown` 在 `StarmapRuntime` 那个 IIFE 里(`client-scope-test` 第 7 组盯着这堵墙),
|
|
9685
|
+
// 工厂作用域拿不到;所以照 60 / 69 的老办法原样放进等宽的 `<pre>` —— 报告是给人读的
|
|
9686
|
+
// 纯文本,多一层解析只会多一处会坏的地方。
|
|
9687
|
+
|
|
9688
|
+
// ── 卡片自己的样式 ─────────────────────────────────────────────────────
|
|
9689
|
+
//
|
|
9690
|
+
// 为什么不并进 `40-styles.js` 的那一份:那一份是「拼接时就固定的字符串」,本分片在它
|
|
9691
|
+
// 之后求值,改不动它。所以照 `11-starmap-styles.js` / 70 的老办法自带一段、幂等注入
|
|
9692
|
+
// (同一个 `data-plugin-css` 只挂一次)。
|
|
9693
|
+
// 只用宿主 token:`--dsw-alias-*`;不出现 `state-danger-*`(没有这个 token)与 `bg-l*`。
|
|
9694
|
+
const SYNC_PANEL_CSS = [
|
|
9695
|
+
// 输入框在这一行里吃掉剩余宽度:`.smem-autodream-row` 是 flex + wrap,`.smem-input`
|
|
9696
|
+
// 自带 `width:100%`,不收一下就会把旁边那颗按钮顶到下一行(面板 420px 起)。
|
|
9697
|
+
".smem-syncp .smem-input{flex:1 1 150px;min-width:0;}",
|
|
9698
|
+
// 设备候选**一列**排开(单栏)。用按钮而不是 `Chip` 组件:Chip 表达「开 / 关」,
|
|
9699
|
+
// 这里表达「选这台」,而且一行里要装三样东西(主机名 / 系统 / 在线点)。
|
|
9700
|
+
".smem-syncp-devlist{display:flex;flex-direction:column;gap:6px;}",
|
|
9701
|
+
".smem-syncp-dev{display:flex;align-items:center;gap:8px;width:100%;box-sizing:border-box;text-align:left;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:5px 9px;font-size:12px;cursor:pointer;}",
|
|
9702
|
+
".smem-syncp-dev:hover:not(:disabled){border-color:var(--dsw-alias-border-l2);background:var(--dsw-alias-bg-layer-2);}",
|
|
9703
|
+
".smem-syncp-dev--on{border-color:var(--dsw-alias-brand-primary);}",
|
|
9704
|
+
".smem-syncp-devhost{font-weight:600;overflow-wrap:anywhere;}",
|
|
9705
|
+
// 在线点:颜色靠背景,尺寸固定 7px(比文字小,不抢眼)。离线是灰的、不是红的 ——
|
|
9706
|
+
// 离线不是错误,别拿错误色去喊它。
|
|
9707
|
+
".smem-syncp-dot{flex:none;width:7px;height:7px;border-radius:50%;margin-left:auto;background:var(--dsw-alias-label-secondary);}",
|
|
9708
|
+
".smem-syncp-dot[data-on=\"1\"]{background:var(--dsw-alias-state-success-primary);}",
|
|
9709
|
+
// 首次推送那道门:用 warn 而不是 error —— 它不是失败,是要人点一次头。
|
|
9710
|
+
".smem-syncp-gate{display:flex;flex-direction:column;gap:6px;border:1px solid var(--dsw-alias-state-warn-primary,#d97706);border-radius:10px;background:var(--dsw-alias-bg-layer-1);padding:10px 12px;}",
|
|
9711
|
+
".smem-syncp-gatetitle{font-size:12.5px;font-weight:600;color:var(--dsw-alias-label-primary);}",
|
|
9712
|
+
".smem-syncp-result{display:flex;flex-direction:column;gap:6px;}",
|
|
9713
|
+
// 报告:等宽原样。`pre-wrap` 让 markdown 的长行折在卡片里,而不是把面板撑出横向滚动。
|
|
9714
|
+
".smem-syncp-report{margin:0;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;background:var(--dsw-alias-bg-base);color:var(--dsw-alias-label-primary);padding:8px 10px;max-height:340px;overflow:auto;white-space:pre-wrap;overflow-wrap:anywhere;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:11.5px;line-height:1.55;}",
|
|
9715
|
+
].join("\n");
|
|
9716
|
+
|
|
9717
|
+
/** 幂等注入(同一个 id 只挂一次;`document` 不在时安静跳过,测试环境就这么用)。 */
|
|
9718
|
+
function injectSyncPanelStyle() {
|
|
9719
|
+
try {
|
|
9720
|
+
if (typeof document === "undefined" || !document || typeof document.createElement !== "function") return;
|
|
9721
|
+
const tagId = "sage-mem/sync-panel.css";
|
|
9722
|
+
if (typeof document.querySelector === "function"
|
|
9723
|
+
&& document.querySelector("style[data-plugin-css=" + JSON.stringify(tagId) + "]") !== null) return;
|
|
9724
|
+
const tag = document.createElement("style");
|
|
9725
|
+
if (tag.dataset) { tag.dataset.plugin = "sage-mem"; tag.dataset.pluginCss = tagId; }
|
|
9726
|
+
tag.textContent = SYNC_PANEL_CSS;
|
|
9727
|
+
if (document.head && typeof document.head.appendChild === "function") document.head.appendChild(tag);
|
|
9728
|
+
} catch (e) { /* 样式注入失败不该让界面白屏 */ }
|
|
9729
|
+
}
|
|
9730
|
+
injectSyncPanelStyle();
|
|
9731
|
+
|
|
9732
|
+
// ── 卡片 ───────────────────────────────────────────────────────────────
|
|
9733
|
+
|
|
9734
|
+
/**
|
|
9735
|
+
* 「跨设备同步」(3.1.0)—— 挂在「外部访问」页的来件卡之后、「跨设备差异」卡**之前**。
|
|
9736
|
+
*
|
|
9737
|
+
* 为什么在差异卡之前:先「触发一次同步」,再「裁决它产生的差异」。反过来摆的话,
|
|
9738
|
+
* 人得先滚过一列待裁决记录才看得见「其实还没连过远端」这件事。
|
|
9739
|
+
*
|
|
9740
|
+
* 宿主没有 `sync` 面(旧宿主)时**卡还画**,只是按钮禁用并在 title 里点名缺哪个接口 ——
|
|
9741
|
+
* 输入框那半边是 `memory.setSettings`,它跟同步面没关系,仍然有用(配置可以先前置)。
|
|
9742
|
+
* 这与 `70-ext-sync.js` 那张「整块不渲染」不同:那一卡的数据**全部**来自 `conflict` 面,
|
|
9743
|
+
* 画一个空壳只会让人以为同步坏了;这一卡有一半是设置。
|
|
9744
|
+
*/
|
|
9745
|
+
function ExternalSyncPanelCard(props) {
|
|
9746
|
+
const memRemote = stableRemote(props.ctx, "remote.memory");
|
|
9747
|
+
const syncRemote = stableRemote(props.ctx, "remote.sync");
|
|
9748
|
+
const canRun = !!syncRemote && typeof syncRemote.run === "function";
|
|
9749
|
+
const canDevices = !!syncRemote && typeof syncRemote.devices === "function";
|
|
9750
|
+
|
|
9751
|
+
const sSet = react.useState(null); const memSet = sSet[0], setMemSet = sSet[1];
|
|
9752
|
+
const sHost = react.useState(""); const hostDraft = sHost[0], setHostDraft = sHost[1];
|
|
9753
|
+
const sPath = react.useState(""); const pathDraft = sPath[0], setPathDraft = sPath[1];
|
|
9754
|
+
const sDevs = react.useState(null); const devs = sDevs[0], setDevs = sDevs[1];
|
|
9755
|
+
const sDevErr = react.useState(""); const devErr = sDevErr[0], setDevErr = sDevErr[1];
|
|
9756
|
+
const sBusy = react.useState(""); const busy = sBusy[0], setBusy = sBusy[1];
|
|
9757
|
+
const sReport = react.useState(null); const report = sReport[0], setReport = sReport[1];
|
|
9758
|
+
const sFail = react.useState(null); const fail = sFail[0], setFail = sFail[1];
|
|
9759
|
+
const sNotice = react.useState(""); const notice = sNotice[0], setNotice = sNotice[1];
|
|
9760
|
+
const sStaged = react.useState(""); const staged = sStaged[0], setStaged = sStaged[1];
|
|
9761
|
+
const sGate = react.useState(null); const gate = sGate[0], setGate = sGate[1];
|
|
9762
|
+
const sConfirm = react.useState(null); const confirm = sConfirm[0], setConfirm = sConfirm[1];
|
|
9763
|
+
|
|
9764
|
+
/** 服务端**已保存**的那份(失焦保存只跟它比,不与草稿比)。 */
|
|
9765
|
+
const saved = (memSet && memSet.sync) || null;
|
|
9766
|
+
const diagnostics = (memSet && Array.isArray(memSet.syncDiagnostics)) ? memSet.syncDiagnostics : [];
|
|
9767
|
+
const savedHost = String((saved && saved.host) || "");
|
|
9768
|
+
const savedPath = String((saved && saved.remotePath) || "");
|
|
9769
|
+
const frozen = busy !== "";
|
|
9770
|
+
|
|
9771
|
+
function load() {
|
|
9772
|
+
if (!memRemote || typeof memRemote.getSettings !== "function") {
|
|
9773
|
+
setFail({ code: "", error: t("ext.syncPanel.err.noSettings") });
|
|
9774
|
+
return;
|
|
9775
|
+
}
|
|
9776
|
+
Promise.resolve(memRemote.getSettings()).then(function (res) {
|
|
9777
|
+
const d = unwrap(res) || {};
|
|
9778
|
+
setMemSet(d);
|
|
9779
|
+
const sy = d.sync || {};
|
|
9780
|
+
setHostDraft(String(sy.host == null ? "" : sy.host));
|
|
9781
|
+
setPathDraft(String(sy.remotePath == null ? "" : sy.remotePath));
|
|
9782
|
+
}, function (e) { setFail({ code: "", error: detail(e) }); });
|
|
9783
|
+
}
|
|
9784
|
+
|
|
9785
|
+
react.useEffect(function () { load(); }, []);
|
|
9786
|
+
|
|
9787
|
+
/**
|
|
9788
|
+
* 存一项。**一次只带一个键**。
|
|
9789
|
+
*
|
|
9790
|
+
* 宿主按整段合并(`{...现有, ...patch}`),两个键一起发就等于替用户改了这次没碰的
|
|
9791
|
+
* 那一项 —— 尤其「远端目录」还在草稿里没失焦的时候。
|
|
9792
|
+
*/
|
|
9793
|
+
function saveOne(key, value, okMsg) {
|
|
9794
|
+
if (!memRemote || typeof memRemote.setSettings !== "function") {
|
|
9795
|
+
setFail({ code: "", error: t("ext.syncPanel.err.noSettings") });
|
|
9796
|
+
return;
|
|
9797
|
+
}
|
|
9798
|
+
const patch = key === "host" ? { sync: { host: value } } : { sync: { remotePath: value } };
|
|
9799
|
+
if (key === "host") setHostDraft(value); else setPathDraft(value);
|
|
9800
|
+
setBusy("save"); setFail(null); setNotice("");
|
|
9801
|
+
Promise.resolve(memRemote.setSettings(patch)).then(function (out) {
|
|
9802
|
+
setBusy("");
|
|
9803
|
+
// `setSettings` 自带 `{ok, settings}`:直接读,**不要** unwrap(68 / 64 同一条)。
|
|
9804
|
+
if (out && out.ok === false) {
|
|
9805
|
+
setFail({ code: out.code ? String(out.code) : "", error: String(out.error || "") });
|
|
9806
|
+
return;
|
|
9807
|
+
}
|
|
9808
|
+
if (out && out.settings) setMemSet(out.settings);
|
|
9809
|
+
setNotice(okMsg || t("ext.syncPanel.saved"));
|
|
9810
|
+
}, function (e) {
|
|
9811
|
+
setBusy("");
|
|
9812
|
+
setFail({ code: "", error: detail(e) });
|
|
9813
|
+
});
|
|
9814
|
+
}
|
|
9815
|
+
|
|
9816
|
+
/** 失焦即存(仓库惯例)。只在**真的变了**才发请求,否则点进点出会白刷一次提示。 */
|
|
9817
|
+
function saveHostIfChanged() {
|
|
9818
|
+
const v = String(hostDraft == null ? "" : hostDraft).trim();
|
|
9819
|
+
if (v === savedHost) return;
|
|
9820
|
+
saveOne("host", v);
|
|
9821
|
+
}
|
|
9822
|
+
|
|
9823
|
+
function savePathIfChanged() {
|
|
9824
|
+
const v = String(pathDraft == null ? "" : pathDraft).trim();
|
|
9825
|
+
if (v === savedPath) return;
|
|
9826
|
+
saveOne("remotePath", v);
|
|
9827
|
+
}
|
|
9828
|
+
|
|
9829
|
+
/** 回车 = 立即保存并失焦。 */
|
|
9830
|
+
function onEnter(handler) {
|
|
9831
|
+
return function (e) {
|
|
9832
|
+
if (e.key !== "Enter") return;
|
|
9833
|
+
e.preventDefault();
|
|
9834
|
+
handler();
|
|
9835
|
+
if (e.target && typeof e.target.blur === "function") e.target.blur();
|
|
9836
|
+
};
|
|
9837
|
+
}
|
|
9838
|
+
|
|
9839
|
+
/** 设备候选 —— **只在点按钮时**调(它背后是一条 `tailscale`)。 */
|
|
9840
|
+
function loadDevices() {
|
|
9841
|
+
if (!canDevices) {
|
|
9842
|
+
setDevErr(t("ext.syncPanel.err.noRemote"));
|
|
9843
|
+
return;
|
|
9844
|
+
}
|
|
9845
|
+
setBusy("devices"); setDevErr("");
|
|
9846
|
+
Promise.resolve(syncRemote.devices()).then(function (res) {
|
|
9847
|
+
setBusy("");
|
|
9848
|
+
const d = unwrap(res) || {};
|
|
9849
|
+
if (d.ok === false) {
|
|
9850
|
+
setDevs(null);
|
|
9851
|
+
setDevErr(t("ext.syncPanel.devicesFailed", {
|
|
9852
|
+
code: String(d.code || "?"), error: String(d.error || ""),
|
|
9853
|
+
}));
|
|
9854
|
+
return;
|
|
9855
|
+
}
|
|
9856
|
+
setDevs({
|
|
9857
|
+
available: d.available !== false,
|
|
9858
|
+
reason: String(d.reason == null ? "" : d.reason),
|
|
9859
|
+
devices: Array.isArray(d.devices) ? d.devices : [],
|
|
9860
|
+
});
|
|
9861
|
+
}, function (e) {
|
|
9862
|
+
setBusy("");
|
|
9863
|
+
setDevs(null);
|
|
9864
|
+
setDevErr(t("ext.syncPanel.devicesFailed", { code: "", error: detail(e) }));
|
|
9865
|
+
});
|
|
9866
|
+
}
|
|
9867
|
+
|
|
9868
|
+
/**
|
|
9869
|
+
* 点候选 = 选这台。**点了就存**,不只是填进输入框。
|
|
9870
|
+
*
|
|
9871
|
+
* 理由:`sync.run()` 不带 `host` 时读的是**设置里**的值。只填不存的话,下一步
|
|
9872
|
+
* 「检查」会拿旧的别名去跑 —— 那是「按了没反应」的另一种长相,而且很难自己看出来。
|
|
9873
|
+
*/
|
|
9874
|
+
function pickDevice(host) {
|
|
9875
|
+
const v = String(host == null ? "" : host);
|
|
9876
|
+
setHostDraft(v);
|
|
9877
|
+
if (v === savedHost) return;
|
|
9878
|
+
saveOne("host", v);
|
|
9879
|
+
}
|
|
9880
|
+
|
|
9881
|
+
function labelOf(action) {
|
|
9882
|
+
return action === "pull"
|
|
9883
|
+
? t("ext.syncPanel.pull")
|
|
9884
|
+
: (action === "push" ? t("ext.syncPanel.push") : t("ext.syncPanel.check"));
|
|
9885
|
+
}
|
|
9886
|
+
|
|
9887
|
+
/**
|
|
9888
|
+
* 一次动作的唯一出口。`apply` / `confirm` **原样**交给宿主:预演与点头都是宿主的
|
|
9889
|
+
* 判据,这一层越权改写等于偷偷替人点了头。
|
|
9890
|
+
*/
|
|
9891
|
+
function callRun(action, apply, confirmFlag) {
|
|
9892
|
+
if (!canRun) {
|
|
9893
|
+
setFail({ code: "unsupported", error: t("ext.syncPanel.err.noRemote") });
|
|
9894
|
+
return;
|
|
9895
|
+
}
|
|
9896
|
+
const opts = { action: action };
|
|
9897
|
+
// 拉取 / 推送**显式**带 `apply`(预演就是 `apply:false`):宿主只认 `=== true`,
|
|
9898
|
+
// 但请求里写出来这件事本身是给人看的 —— 读日志时不必再猜「没带 = 预演?」。
|
|
9899
|
+
// `check` 不写:它没有写盘的路,带一个恒 false 的开关只是噪音。
|
|
9900
|
+
if (action !== "check") opts.apply = apply === true;
|
|
9901
|
+
if (confirmFlag === true) opts.confirm = true;
|
|
9902
|
+
setBusy(apply === true ? "apply" : action);
|
|
9903
|
+
setFail(null); setNotice("");
|
|
9904
|
+
Promise.resolve(syncRemote.run(opts)).then(function (res) {
|
|
9905
|
+
setBusy("");
|
|
9906
|
+
setConfirm(null);
|
|
9907
|
+
const d = unwrap(res) || {};
|
|
9908
|
+
const g = d.gate && typeof d.gate === "object" ? d.gate : null;
|
|
9909
|
+
// 门的两种形状都认:结构化码(`ok:false, code:'need_confirm'`)与 `gate.needConfirm`。
|
|
9910
|
+
const needConfirm = (d.ok === false && d.code === "need_confirm")
|
|
9911
|
+
|| (!!g && g.needConfirm === true);
|
|
9912
|
+
const text = String(d.report == null ? "" : d.report);
|
|
9913
|
+
if (needConfirm && action === "push") {
|
|
9914
|
+
// 不是失败:远端库还没被认过。报告照旧摊开,另给一句人话 + 一颗确认按钮。
|
|
9915
|
+
setGate({
|
|
9916
|
+
deviceId: String((g && g.deviceId) || ""),
|
|
9917
|
+
deviceLabel: String((g && g.deviceLabel) || ""),
|
|
9918
|
+
});
|
|
9919
|
+
setStaged("");
|
|
9920
|
+
setReport(text === "" ? null : { action: action, text: text, applied: false });
|
|
9921
|
+
return;
|
|
9922
|
+
}
|
|
9923
|
+
setGate(null);
|
|
9924
|
+
setReport(text === "" ? null : { action: action, text: text, applied: d.applied === true });
|
|
9925
|
+
if (d.ok === false) {
|
|
9926
|
+
setStaged("");
|
|
9927
|
+
setFail({ code: String(d.code || "?"), error: String(d.error || "") });
|
|
9928
|
+
return;
|
|
9929
|
+
}
|
|
9930
|
+
setFail(null);
|
|
9931
|
+
if (action === "check") { setStaged(""); setNotice(t("ext.syncPanel.notice.check")); return; }
|
|
9932
|
+
if (apply === true) {
|
|
9933
|
+
setStaged("");
|
|
9934
|
+
setNotice(action === "pull" ? t("ext.syncPanel.notice.pull") : t("ext.syncPanel.notice.push"));
|
|
9935
|
+
return;
|
|
9936
|
+
}
|
|
9937
|
+
// 预演:按钮换成「执行…」,等第二次点击(第二次还要过模态)。
|
|
9938
|
+
setStaged(action);
|
|
9939
|
+
setNotice(t("ext.syncPanel.notice.preview"));
|
|
9940
|
+
}, function (e) {
|
|
9941
|
+
setBusy("");
|
|
9942
|
+
setConfirm(null);
|
|
9943
|
+
setFail({ code: "", error: detail(e) });
|
|
9944
|
+
});
|
|
9945
|
+
}
|
|
9946
|
+
|
|
9947
|
+
/** 一颗动作按钮。`staged` 的那颗升成主按钮:下一步该点哪个,一眼看得出来。 */
|
|
9948
|
+
function actionButton(action, label, onClick) {
|
|
9949
|
+
return h("button", {
|
|
9950
|
+
type: "button",
|
|
9951
|
+
className: "smem-btn smem-btn-xs" + (staged === action ? " smem-btn-primary" : ""),
|
|
9952
|
+
key: action,
|
|
9953
|
+
disabled: frozen || !canRun,
|
|
9954
|
+
title: canRun ? undefined : t("ext.syncPanel.err.noRemote"),
|
|
9955
|
+
onClick: onClick,
|
|
9956
|
+
}, label);
|
|
9957
|
+
}
|
|
9958
|
+
|
|
9959
|
+
function deviceListBlock() {
|
|
9960
|
+
if (devErr) return h("div", { className: "smem-err", key: "deverr" }, "⚠ " + devErr);
|
|
9961
|
+
if (!devs) return null;
|
|
9962
|
+
if (devs.available === false) {
|
|
9963
|
+
// 降级路径:这不是错误,是一句人话;输入框照旧手打。
|
|
9964
|
+
return h("div", { className: "smem-muted", key: "reason" },
|
|
9965
|
+
devs.reason === "" ? t("ext.syncPanel.devicesUnavailable") : devs.reason);
|
|
9966
|
+
}
|
|
9967
|
+
if (devs.devices.length === 0) {
|
|
9968
|
+
return h("div", { className: "smem-muted", key: "none" }, t("ext.syncPanel.devicesNone"));
|
|
9969
|
+
}
|
|
9970
|
+
return h("div", { className: "smem-syncp-devlist", key: "list" }, devs.devices.map(function (d, i) {
|
|
9971
|
+
const host = String((d && d.host) || "");
|
|
9972
|
+
const online = !!d && d.online === true;
|
|
9973
|
+
const ips = d && Array.isArray(d.ips) ? d.ips.join(", ") : "";
|
|
9974
|
+
const tip = String((d && d.dnsName) || host) + (ips === "" ? "" : " · " + ips);
|
|
9975
|
+
return h("button", {
|
|
9976
|
+
type: "button",
|
|
9977
|
+
className: "smem-syncp-dev" + (hostDraft === host ? " smem-syncp-dev--on" : ""),
|
|
9978
|
+
key: "dev-" + i + "-" + host,
|
|
9979
|
+
disabled: frozen,
|
|
9980
|
+
title: tip,
|
|
9981
|
+
onClick: function () { pickDevice(host); },
|
|
9982
|
+
}, [
|
|
9983
|
+
h("span", { className: "smem-syncp-devhost", key: "h" }, host === "" ? "?" : host),
|
|
9984
|
+
d && d.os ? h("span", { className: "smem-muted", key: "o" }, String(d.os)) : null,
|
|
9985
|
+
h("span", {
|
|
9986
|
+
className: "smem-syncp-dot", key: "dot",
|
|
9987
|
+
"data-on": online ? "1" : "0",
|
|
9988
|
+
"aria-hidden": "true",
|
|
9989
|
+
}),
|
|
9990
|
+
h("span", { className: "smem-muted", key: "st" },
|
|
9991
|
+
online ? t("ext.syncPanel.deviceOnline") : t("ext.syncPanel.deviceOffline")),
|
|
9992
|
+
]);
|
|
9993
|
+
}));
|
|
9994
|
+
}
|
|
9995
|
+
|
|
9996
|
+
function gateBlock() {
|
|
9997
|
+
if (!gate) return null;
|
|
9998
|
+
const who = gate.deviceLabel !== ""
|
|
9999
|
+
? gate.deviceLabel + (gate.deviceId === "" ? "" : "(" + gate.deviceId + ")")
|
|
10000
|
+
: t("ext.syncPanel.gate.noDevice");
|
|
10001
|
+
return h("div", { className: "smem-syncp-gate", key: "gate" }, [
|
|
10002
|
+
h("div", { className: "smem-syncp-gatetitle", key: "t" }, t("ext.syncPanel.gate.title")),
|
|
10003
|
+
h("div", { className: "smem-autodream-note", key: "b" }, t("ext.syncPanel.gate.body", { who: who })),
|
|
10004
|
+
h("div", { className: "smem-row", key: "acts" }, [
|
|
10005
|
+
h("button", {
|
|
10006
|
+
type: "button", className: "smem-btn smem-btn-xs smem-btn-primary", key: "yes",
|
|
10007
|
+
disabled: frozen,
|
|
10008
|
+
onClick: function () { callRun("push", true, true); },
|
|
10009
|
+
}, t("ext.syncPanel.gate.yes")),
|
|
10010
|
+
h("button", {
|
|
10011
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "no",
|
|
10012
|
+
disabled: frozen,
|
|
10013
|
+
onClick: function () { setGate(null); },
|
|
10014
|
+
}, t("ext.syncPanel.gate.cancel")),
|
|
10015
|
+
]),
|
|
10016
|
+
]);
|
|
10017
|
+
}
|
|
10018
|
+
|
|
10019
|
+
function confirmBlock() {
|
|
10020
|
+
if (!confirm) return null;
|
|
10021
|
+
const isPull = confirm.action === "pull";
|
|
10022
|
+
return h(Confirm, {
|
|
10023
|
+
key: "confirm",
|
|
10024
|
+
title: isPull ? t("ext.syncPanel.confirm.pullTitle") : t("ext.syncPanel.confirm.pushTitle"),
|
|
10025
|
+
confirmLabel: t("ext.syncPanel.confirm.yes"),
|
|
10026
|
+
busyLabel: t("ext.syncPanel.confirm.busy"),
|
|
10027
|
+
busy: busy === "apply",
|
|
10028
|
+
danger: true,
|
|
10029
|
+
onCancel: function () { setConfirm(null); },
|
|
10030
|
+
onConfirm: function () { callRun(confirm.action, true, false); },
|
|
10031
|
+
}, [
|
|
10032
|
+
h("div", { key: "body" }, isPull
|
|
10033
|
+
? t("ext.syncPanel.confirm.pullBody")
|
|
10034
|
+
: t("ext.syncPanel.confirm.pushBody")),
|
|
10035
|
+
]);
|
|
10036
|
+
}
|
|
10037
|
+
|
|
10038
|
+
function resultBlock() {
|
|
10039
|
+
if (!report) {
|
|
10040
|
+
return h("div", { className: "smem-autodream-note", key: "none" }, t("ext.syncPanel.empty"));
|
|
10041
|
+
}
|
|
10042
|
+
return h("div", { className: "smem-syncp-result", key: "res" }, [
|
|
10043
|
+
h("div", { className: "smem-autodream-row", key: "head" }, [
|
|
10044
|
+
h("span", { className: "smem-autodream-key", key: "k" }, t("ext.syncPanel.report.title")),
|
|
10045
|
+
h("span", { className: "smem-muted", key: "kind" }, report.action === "check"
|
|
10046
|
+
? t("ext.syncPanel.report.preview")
|
|
10047
|
+
: (report.applied ? t("ext.syncPanel.report.applied") : t("ext.syncPanel.report.preview"))),
|
|
10048
|
+
]),
|
|
10049
|
+
// markdown 原样(见文件头:不在这里再写一个解析器)。
|
|
10050
|
+
h("pre", { className: "smem-syncp-report", key: "pre" },
|
|
10051
|
+
report.text === "" ? t("ext.syncPanel.report.empty") : report.text),
|
|
10052
|
+
]);
|
|
10053
|
+
}
|
|
10054
|
+
|
|
10055
|
+
const busyText = busy === "check" ? t("ext.syncPanel.busy.check")
|
|
10056
|
+
: busy === "devices" ? t("ext.syncPanel.busy.devices")
|
|
10057
|
+
: busy === "save" ? t("ext.syncPanel.busy.save")
|
|
10058
|
+
: busy === "apply" ? t("ext.syncPanel.busy.apply")
|
|
10059
|
+
: busy === "pull" ? t("ext.syncPanel.busy.pull")
|
|
10060
|
+
: busy === "push" ? t("ext.syncPanel.busy.push")
|
|
10061
|
+
: "";
|
|
10062
|
+
const statusText = busy !== ""
|
|
10063
|
+
? busyText
|
|
10064
|
+
: (staged !== ""
|
|
10065
|
+
? t("ext.syncPanel.status.staged", { action: labelOf(staged) })
|
|
10066
|
+
// 跑过一次之后别再说「还没跑过」——报告就在下面,两句话会互相打脸。
|
|
10067
|
+
: (report
|
|
10068
|
+
? t("ext.syncPanel.status.done", { action: labelOf(report.action) })
|
|
10069
|
+
: t("ext.syncPanel.status.idle")));
|
|
10070
|
+
|
|
10071
|
+
return h("div", { className: "smem-syncp smem-module-card" }, [
|
|
10072
|
+
h("div", { className: "smem-modcard-head", key: "head" }, [
|
|
10073
|
+
h("span", { className: "smem-modcard-title", key: "t" }, t("ext.syncPanel.title")),
|
|
10074
|
+
h("span", { className: "smem-modcard-status", key: "s" }, statusText),
|
|
10075
|
+
]),
|
|
10076
|
+
h("div", { className: "smem-autodream-note", key: "what" }, t("ext.syncPanel.what")),
|
|
10077
|
+
|
|
10078
|
+
// ── 主机 + 设备候选 ──
|
|
10079
|
+
h("div", { className: "smem-autodream-sec", key: "host" }, [
|
|
10080
|
+
h("div", { className: "smem-autodream-row", key: "row" }, [
|
|
10081
|
+
h("span", { className: "smem-autodream-key", key: "k" }, t("ext.syncPanel.host")),
|
|
10082
|
+
h("input", {
|
|
10083
|
+
className: "smem-input", key: "i", type: "text",
|
|
10084
|
+
value: hostDraft,
|
|
10085
|
+
placeholder: t("ext.syncPanel.hostPlaceholder"),
|
|
10086
|
+
onChange: function (e) { setHostDraft(e.target.value); },
|
|
10087
|
+
onBlur: saveHostIfChanged,
|
|
10088
|
+
onKeyDown: onEnter(saveHostIfChanged),
|
|
10089
|
+
}),
|
|
10090
|
+
h("button", {
|
|
10091
|
+
type: "button", className: "smem-btn smem-btn-xs", key: "dev",
|
|
10092
|
+
disabled: frozen || !canDevices,
|
|
10093
|
+
title: canDevices ? undefined : t("ext.syncPanel.err.noRemote"),
|
|
10094
|
+
onClick: loadDevices,
|
|
10095
|
+
}, t("ext.syncPanel.devices")),
|
|
10096
|
+
]),
|
|
10097
|
+
h("div", { className: "smem-autodream-note", key: "hint" }, t("ext.syncPanel.hostHint")),
|
|
10098
|
+
deviceListBlock(),
|
|
10099
|
+
]),
|
|
10100
|
+
|
|
10101
|
+
// ── 远端记忆目录 ──
|
|
10102
|
+
h("div", { className: "smem-autodream-sec", key: "path" }, [
|
|
10103
|
+
h("div", { className: "smem-autodream-row", key: "row" }, [
|
|
10104
|
+
h("span", { className: "smem-autodream-key", key: "k" }, t("ext.syncPanel.remotePath")),
|
|
10105
|
+
h("input", {
|
|
10106
|
+
className: "smem-input", key: "i", type: "text",
|
|
10107
|
+
value: pathDraft,
|
|
10108
|
+
placeholder: t("ext.syncPanel.remotePathPlaceholder"),
|
|
10109
|
+
onChange: function (e) { setPathDraft(e.target.value); },
|
|
10110
|
+
onBlur: savePathIfChanged,
|
|
10111
|
+
onKeyDown: onEnter(savePathIfChanged),
|
|
10112
|
+
}),
|
|
10113
|
+
]),
|
|
10114
|
+
h("div", { className: "smem-autodream-note", key: "hint" }, t("ext.syncPanel.pathHint")),
|
|
10115
|
+
]),
|
|
10116
|
+
|
|
10117
|
+
// ── 三个动作 ──
|
|
10118
|
+
h("div", { className: "smem-autodream-sec", key: "acts" }, [
|
|
10119
|
+
h("div", { className: "smem-row", key: "row" }, [
|
|
10120
|
+
actionButton("check", t("ext.syncPanel.check"), function () {
|
|
10121
|
+
if (busy !== "") return;
|
|
10122
|
+
callRun("check", false, false);
|
|
10123
|
+
}),
|
|
10124
|
+
actionButton("pull", staged === "pull" ? t("ext.syncPanel.pullApply") : t("ext.syncPanel.pull"), function () {
|
|
10125
|
+
if (busy !== "") return;
|
|
10126
|
+
// 第二次点击只开模态;**真正写盘**在模态的确认键上。
|
|
10127
|
+
if (staged === "pull") { setConfirm({ action: "pull" }); return; }
|
|
10128
|
+
callRun("pull", false, false);
|
|
10129
|
+
}),
|
|
10130
|
+
actionButton("push", staged === "push" ? t("ext.syncPanel.pushApply") : t("ext.syncPanel.push"), function () {
|
|
10131
|
+
if (busy !== "") return;
|
|
10132
|
+
if (staged === "push") { setConfirm({ action: "push" }); return; }
|
|
10133
|
+
callRun("push", false, false);
|
|
10134
|
+
}),
|
|
10135
|
+
]),
|
|
10136
|
+
h("div", { className: "smem-autodream-note", key: "hint" }, t("ext.syncPanel.actionsHint")),
|
|
10137
|
+
]),
|
|
10138
|
+
|
|
10139
|
+
// ── 结果 ──
|
|
10140
|
+
h("div", { className: "smem-autodream-sec", key: "result" }, [resultBlock()]),
|
|
10141
|
+
|
|
10142
|
+
gateBlock(),
|
|
10143
|
+
confirmBlock(),
|
|
10144
|
+
fail
|
|
10145
|
+
? h("div", { className: "smem-err", key: "fail" }, "⚠ " + (fail.code === ""
|
|
10146
|
+
? String(fail.error)
|
|
10147
|
+
: t("ext.syncPanel.failed", { code: String(fail.code), error: String(fail.error) })))
|
|
10148
|
+
: null,
|
|
10149
|
+
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
10150
|
+
diagnostics.length > 0
|
|
10151
|
+
? h("div", { className: "smem-err", key: "diag" }, "⚠ " + diagnostics.join(" · "))
|
|
10152
|
+
: null,
|
|
10153
|
+
]);
|
|
10154
|
+
}
|
|
8780
10155
|
// ── 文件列表这一屏(1.5.3 从 Section 里抽出;D23 计划的 72 片)──────────
|
|
8781
10156
|
// 只负责「按状态/筛选把卡片摆出来 + 四种空态」;状态、筛选语义、动作回调全部由 Section 传。
|
|
8782
10157
|
// 抽出的判据:它是 Section 里最大的一块纯渲染,而卡片组件(Card / ArchivedCard)本来就在 50 共享层。
|