openclaw-sync-assistant 0.1.4 → 0.1.5
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/index.js +1197 -64
- package/openclaw.plugin.json +27 -2
- package/package.json +6 -1
- package/src/github-sync.js +607 -70
- package/src/p2p-sync.js +649 -0
- package/src/sync-items.js +103 -0
- package/.eslintrc.json +0 -15
- package/.trae/documents/distributed_sync_plan.md +0 -105
- package/test.js +0 -10
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
|
|
3
|
+
const SYNC_ITEM_REGISTRY = {
|
|
4
|
+
Config: {
|
|
5
|
+
id: "Config",
|
|
6
|
+
label: "Config",
|
|
7
|
+
paths: ["config", "openclaw.json"],
|
|
8
|
+
sensitive: false,
|
|
9
|
+
verificationMode: "any",
|
|
10
|
+
},
|
|
11
|
+
Auth: {
|
|
12
|
+
id: "Auth",
|
|
13
|
+
label: "Auth",
|
|
14
|
+
paths: ["auth"],
|
|
15
|
+
sensitive: true,
|
|
16
|
+
verificationMode: "any",
|
|
17
|
+
},
|
|
18
|
+
Sessions: {
|
|
19
|
+
id: "Sessions",
|
|
20
|
+
label: "Sessions",
|
|
21
|
+
paths: ["sessions", "history", "agent-state"],
|
|
22
|
+
sensitive: false,
|
|
23
|
+
verificationMode: "any",
|
|
24
|
+
},
|
|
25
|
+
ChannelState: {
|
|
26
|
+
id: "ChannelState",
|
|
27
|
+
label: "ChannelState",
|
|
28
|
+
paths: ["channels", "channel-state", "whatsapp", "telegram"],
|
|
29
|
+
sensitive: true,
|
|
30
|
+
verificationMode: "any",
|
|
31
|
+
},
|
|
32
|
+
WorkspaceFiles: {
|
|
33
|
+
id: "WorkspaceFiles",
|
|
34
|
+
label: "WorkspaceFiles",
|
|
35
|
+
paths: ["workspace", "MEMORY.md", "USER.md", "skills", "prompts"],
|
|
36
|
+
sensitive: false,
|
|
37
|
+
verificationMode: "any",
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const SYNC_ITEM_ALIASES = {
|
|
42
|
+
Workspace: "WorkspaceFiles",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function normalizeSyncItem(item) {
|
|
46
|
+
if (!item || typeof item !== "string") {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const normalizedItem = SYNC_ITEM_ALIASES[item] || item;
|
|
51
|
+
return SYNC_ITEM_REGISTRY[normalizedItem] ? normalizedItem : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function normalizeSyncItems(syncItems) {
|
|
55
|
+
if (!Array.isArray(syncItems)) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return [
|
|
60
|
+
...new Set(
|
|
61
|
+
syncItems
|
|
62
|
+
.map((item) => normalizeSyncItem(item))
|
|
63
|
+
.filter((item) => item && SYNC_ITEM_REGISTRY[item]),
|
|
64
|
+
),
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function getSyncItemDefinition(item) {
|
|
69
|
+
const normalizedItem = normalizeSyncItem(item);
|
|
70
|
+
return normalizedItem ? SYNC_ITEM_REGISTRY[normalizedItem] : null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getRecommendedSyncItems() {
|
|
74
|
+
return Object.keys(SYNC_ITEM_REGISTRY);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getSyncItemDefinitions(syncItems = getRecommendedSyncItems()) {
|
|
78
|
+
return normalizeSyncItems(syncItems)
|
|
79
|
+
.map((item) => getSyncItemDefinition(item))
|
|
80
|
+
.filter(Boolean);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function resolveSyncEntries(openclawDir, syncDir, syncItems) {
|
|
84
|
+
return getSyncItemDefinitions(syncItems).flatMap((definition) =>
|
|
85
|
+
definition.paths.map((relativePath) => ({
|
|
86
|
+
item: definition.id,
|
|
87
|
+
relativePath,
|
|
88
|
+
source: path.join(openclawDir, relativePath),
|
|
89
|
+
target: path.join(syncDir, relativePath),
|
|
90
|
+
})),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
module.exports = {
|
|
95
|
+
SYNC_ITEM_REGISTRY,
|
|
96
|
+
SYNC_ITEM_ALIASES,
|
|
97
|
+
normalizeSyncItem,
|
|
98
|
+
normalizeSyncItems,
|
|
99
|
+
getSyncItemDefinition,
|
|
100
|
+
getRecommendedSyncItems,
|
|
101
|
+
getSyncItemDefinitions,
|
|
102
|
+
resolveSyncEntries,
|
|
103
|
+
};
|
package/.eslintrc.json
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
# 分布式数据同步项目 (OpenClaw Sync Assistant) 实施计划
|
|
2
|
-
|
|
3
|
-
## 摘要 (Summary)
|
|
4
|
-
|
|
5
|
-
本项目旨在开发一个**开箱即用**的分布式数据同步扩展,中文名为 **“OpenClaw 同步助手”**,英文名为 **OpenClaw Sync Assistant**。
|
|
6
|
-
该项目将在 `D:\ai_project\openclaw-sync-assistant` 下作为独立项目开发,并严格遵循 OpenClaw 的 **原生插件 (Native Plugin) 标准**。开发完成后将开源到 GitHub,用户完全可以通过 OpenClaw 的**官方插件命令直接安装和卸载**。底层引入基于 Node.js 的 **Holepunch (原 Hypercore Protocol)** 开源栈(`hyperswarm` + `hyperdrive`),自带 P2P 内网穿透与端到端加密,解决跨网同步痛点。
|
|
7
|
-
|
|
8
|
-
## 极简安装与官方集成 (Official Installation & UX)
|
|
9
|
-
|
|
10
|
-
### 1. 官方原生插件机制安装/卸载
|
|
11
|
-
|
|
12
|
-
为了满足“必须通过官方插件安装方法直接安装卸载”的要求,本项目将打包为标准 npm 包,并支持通过 GitHub 或 NPM 仓库一键安装:
|
|
13
|
-
|
|
14
|
-
- **安装 (Installation)**:
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
# 从开源后的 GitHub 仓库直接安装
|
|
18
|
-
openclaw plugin install github:您的用户名/openclaw-sync-assistant
|
|
19
|
-
|
|
20
|
-
# 或者从 npm 官方仓库安装 (若发布)
|
|
21
|
-
openclaw plugin install openclaw-sync-assistant
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
- **卸载 (Uninstallation)**:
|
|
25
|
-
|
|
26
|
-
```bash
|
|
27
|
-
openclaw plugin uninstall openclaw-sync-assistant
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### 2. 初始化与配置向导
|
|
31
|
-
|
|
32
|
-
安装后,通过官方 CLI 扩展注册机制提供交互式配置:
|
|
33
|
-
|
|
34
|
-
```bash
|
|
35
|
-
$ openclaw sync setup
|
|
36
|
-
? 请输入您的同步密钥 (Sync Secret, 用于生成 P2P 发现的 Topic 和加密数据): [********]
|
|
37
|
-
? 请选择要同步的内容: [x] Config [x] Auth [ ] Sessions [x] Workspace
|
|
38
|
-
✔ 配置完成!同步助手后台服务已接管状态同步。
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### 3. Skill 对话集成
|
|
42
|
-
|
|
43
|
-
内置 `skills/sync.md`,用户可直接与大模型对话:
|
|
44
|
-
|
|
45
|
-
- `@OpenClaw 我的公司电脑连上了吗?` -> Agent 调用内部接口查看连接状态。
|
|
46
|
-
|
|
47
|
-
- `@OpenClaw 帮我处理一下配置冲突。` -> 辅助合并 `.conflict` 文件。
|
|
48
|
-
|
|
49
|
-
## 拟议变更与独立建库 (Proposed Changes & Independent Repo)
|
|
50
|
-
|
|
51
|
-
项目在 `D:\ai_project\openclaw-sync-assistant` 下创建,但内部结构完全遵循 OpenClaw 插件规范:
|
|
52
|
-
|
|
53
|
-
### 1. 独立项目初始化步骤
|
|
54
|
-
|
|
55
|
-
1. 在 `D:\ai_project` 下新建 `openclaw-sync-assistant`。
|
|
56
|
-
2. `npm init -y` 并安装 P2P 核心依赖:`hyperswarm`, `hyperdrive`, `localdrive` 等。
|
|
57
|
-
3. 初始化 Git,准备开源推送到 GitHub `main` 分支。
|
|
58
|
-
|
|
59
|
-
### 2. 核心模块与官方插件结构
|
|
60
|
-
|
|
61
|
-
- **`openclaw.plugin.json`**:OpenClaw 插件清单。声明 `id: "sync-assistant"`、技能路径、配置 Schema(如 `syncSecret`)。
|
|
62
|
-
|
|
63
|
-
- **`package.json`**:声明入口点 `main: "dist/index.js"`,确保 OpenClaw 运行时可以正确加载。
|
|
64
|
-
|
|
65
|
-
- **`src/index.ts`**:插件入口,必须导出一个 `export async function register(api: OpenClawPluginAPI)` 函数。在此处拦截生命周期:
|
|
66
|
-
- 注册 CLI 命令 `openclaw sync`。
|
|
67
|
-
|
|
68
|
-
- 启动后台的 `hyperswarm` 守护进程,监听 `$OPENCLAW_STATE_DIR`。
|
|
69
|
-
|
|
70
|
-
- **`src/p2p-network.ts`**:利用 `hyperswarm` 进行 DHT 穿透和对等节点发现。
|
|
71
|
-
|
|
72
|
-
- **`src/drive-manager.ts`**:将 `~/.openclaw` 中的指定目录映射为 `localdrive`,与远端的 `hyperdrive` 建立双向镜像。
|
|
73
|
-
|
|
74
|
-
- **`src/conflict.ts`**:文件冲突时保留本地修改,远端重命名为 `.conflict.<timestamp>`。
|
|
75
|
-
|
|
76
|
-
- **`skills/sync.md`**:官方技能描述文件。
|
|
77
|
-
|
|
78
|
-
## 现有开源方案对比与本方案优化 (Reference & Optimization)
|
|
79
|
-
|
|
80
|
-
**社区现有参考方案**:`awesome-openclaw-skills` 中的 `/sync` 技能。
|
|
81
|
-
|
|
82
|
-
- **工作原理**:基于 `Tailscale + SSH + rsync`。
|
|
83
|
-
|
|
84
|
-
- **致命缺点**:需要用户手动配置 VPN、配置 SSH 密钥、依赖系统级命令,**极难一键安装**,且在 Windows 上体验差。
|
|
85
|
-
|
|
86
|
-
**本项目极致优化**:
|
|
87
|
-
采用 **Hypercore / Hyperswarm / Hyperdrive** 栈全面替代:
|
|
88
|
-
|
|
89
|
-
- **无外部依赖 (Pure Node.js)**:不需要 Tailscale,不需要 SSH 密钥。所有逻辑打包在一个 NPM 插件内。
|
|
90
|
-
|
|
91
|
-
- **自带 P2P 打洞 (NAT Traversal)**:利用 `hyperswarm` 自动穿透防火墙。
|
|
92
|
-
|
|
93
|
-
- **完全符合官方插件生命周期**:随 OpenClaw 启动而启动,随 `openclaw plugin uninstall` 干净卸载,无需残留第三方守护进程。
|
|
94
|
-
|
|
95
|
-
## 假设与决策 (Assumptions & Decisions)
|
|
96
|
-
|
|
97
|
-
1. **运行时接管**:插件将在 `register(api)` 阶段读取 OpenClaw 的上下文路径(`api.paths.stateDir` 等),确保同步的绝对路径完全动态适应当前运行的 OpenClaw 实例。
|
|
98
|
-
2. **多写一致性决策**:采用“Last-Write-Wins (最后写入者赢) + 冲突文件留存”的策略,确保任何情况都不丢失本地数据。
|
|
99
|
-
|
|
100
|
-
## 验证步骤 (Verification Steps)
|
|
101
|
-
|
|
102
|
-
1. **插件打包与安装验证**:在独立项目内运行构建后,使用 `openclaw plugin install /path/to/local/folder` 验证官方安装流程是否成功。
|
|
103
|
-
2. **网络穿透测试**:在两台不同网络的机器上分别安装该插件,输入相同密钥,确认打洞成功。
|
|
104
|
-
3. **OpenClaw 挂载与卸载测试**:运行 `openclaw plugin uninstall openclaw-sync-assistant`,确认相关进程优雅退出且配置清理干净。
|
|
105
|
-
4. **开源发布**:确认代码无敏感信息,推送到您的 GitHub 仓库供社区通过 `openclaw plugin install github:...` 安装。
|
package/test.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const assert = require("assert");
|
|
2
|
-
const plugin = require("./index.js");
|
|
3
|
-
|
|
4
|
-
try {
|
|
5
|
-
assert.ok(plugin !== undefined, "Plugin module should be exported properly");
|
|
6
|
-
console.log("✅ Test passed: Plugin loads successfully.");
|
|
7
|
-
} catch (error) {
|
|
8
|
-
console.error("❌ Test failed:", error.message);
|
|
9
|
-
process.exit(1);
|
|
10
|
-
}
|