8900 commited on
Commit
984460d
·
verified ·
1 Parent(s): d9b6ed7

Delete hf-sync-manager.mjs

Browse files
Files changed (1) hide show
  1. hf-sync-manager.mjs +0 -194
hf-sync-manager.mjs DELETED
@@ -1,194 +0,0 @@
1
- // ============================================================
2
- // HF Sync Manager - SAFE MODE (No Restart Version)
3
- //
4
- // Only sync workspace/
5
- // Never touches openclaw.json
6
- // Never deletes runtime directories
7
- // No restart triggers
8
- // ============================================================
9
-
10
- import fs from "node:fs";
11
- import path from "node:path";
12
- import { execSync } from "node:child_process";
13
-
14
- // –– config ——————————————————
15
-
16
- const HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
17
- const WORKSPACE = path.join(HOME, ".openclaw", "workspace");
18
-
19
- const HF_TOKEN = (process.env.HF_TOKEN || "").trim();
20
- const DATASET_ID = (process.env.HF_DATASET_ID || "").trim();
21
-
22
- const REPO_DIR = "/tmp/oc-safe-dataset";
23
- const INTERVAL = 30 * 60 * 1000; // 30 min
24
-
25
- // –– helpers ——————————————————
26
-
27
- function log(msg) {
28
- console.log("[hf-safe] " + msg);
29
- }
30
-
31
- function sleep(ms) {
32
- return new Promise(r => setTimeout(r, ms));
33
- }
34
-
35
- function repoUrl() {
36
- return "https://user:" + HF_TOKEN +
37
- "@huggingface.co/datasets/" + DATASET_ID;
38
- }
39
-
40
- function git(args) {
41
- return execSync("git " + args, {
42
- cwd: REPO_DIR,
43
- stdio: "pipe",
44
- timeout: 120000,
45
- env: { ...process.env, GIT_TERMINAL_PROMPT: "0" }
46
- }).toString().trim();
47
- }
48
-
49
- // –– repo setup —————————————————
50
-
51
- function ensureRepo() {
52
- if (fs.existsSync(path.join(REPO_DIR, ".git"))) return;
53
-
54
- execSync("rm -rf " + REPO_DIR, { stdio: "pipe" });
55
-
56
- try {
57
- execSync("git clone --depth 1 " + repoUrl() + " " + REPO_DIR, {
58
- stdio: "pipe"
59
- });
60
- log("Repo cloned");
61
- } catch {
62
- fs.mkdirSync(REPO_DIR, { recursive: true });
63
- execSync("git init " + REPO_DIR);
64
- git("remote add origin " + repoUrl());
65
- }
66
-
67
- git("config user.email openclaw@hf.space");
68
- git("config user.name OpenClaw-Sync");
69
- }
70
-
71
- // –– copy helpers —————————————————
72
-
73
- function copyDir(src, dst) {
74
- if (!fs.existsSync(src)) return 0;
75
-
76
- fs.mkdirSync(dst, { recursive: true });
77
-
78
- let count = 0;
79
-
80
- for (const name of fs.readdirSync(src)) {
81
- const s = path.join(src, name);
82
- const d = path.join(dst, name);
83
- const stat = fs.statSync(s);
84
-
85
- if (stat.isDirectory()) {
86
- count += copyDir(s, d);
87
- } else {
88
- fs.copyFileSync(s, d);
89
- count++;
90
- }
91
- }
92
-
93
- return count;
94
- }
95
-
96
- // –– restore (boot only) ——————————————
97
-
98
- function restoreWorkspace() {
99
- if (!HF_TOKEN || !DATASET_ID) {
100
- log("No dataset configured");
101
- return;
102
- }
103
-
104
- log("Restoring workspace...");
105
-
106
- ensureRepo();
107
-
108
- try {
109
- git("pull --quiet origin main");
110
- } catch {}
111
-
112
- const repoWs = path.join(REPO_DIR, "workspace");
113
-
114
- if (fs.existsSync(repoWs)) {
115
- const count = copyDir(repoWs, WORKSPACE);
116
- log("Restored " + count + " files");
117
- } else {
118
- log("No workspace in dataset yet");
119
- }
120
- }
121
-
122
- // –– sync (safe) ——————————————————
123
-
124
- function syncWorkspace() {
125
- if (!HF_TOKEN || !DATASET_ID) return;
126
-
127
- log("Syncing workspace...");
128
-
129
- ensureRepo();
130
-
131
- try {
132
- git("pull --quiet origin main");
133
- } catch {}
134
-
135
- const repoWs = path.join(REPO_DIR, "workspace");
136
-
137
- // ⚠️ 关键:不删除整个 repo,只覆盖 workspace
138
- fs.mkdirSync(repoWs, { recursive: true });
139
-
140
- const count = copyDir(WORKSPACE, repoWs);
141
-
142
- try {
143
- git("add workspace");
144
-
145
- const changed = git("diff --cached --name-only");
146
- if (!changed) {
147
- log("No changes");
148
- return;
149
- }
150
-
151
- const ts = new Date().toISOString();
152
- git(`commit -m "workspace sync ${ts}"`);
153
-
154
- try {
155
- git("push origin main");
156
- } catch {
157
- git("push origin master");
158
- }
159
-
160
- log("Synced " + count + " files");
161
-
162
- } catch (e) {
163
- log("Sync error: " + e.message);
164
- }
165
- }
166
-
167
- // –– main ————————————————————
168
-
169
- async function main() {
170
- log("SAFE MODE started");
171
- log("Workspace only sync (no restart risk)");
172
-
173
- fs.mkdirSync(WORKSPACE, { recursive: true });
174
-
175
- // Boot restore(只读,不影响运行)
176
- restoreWorkspace();
177
-
178
- if (!HF_TOKEN || !DATASET_ID) {
179
- log("Dataset disabled");
180
- return;
181
- }
182
-
183
- // 延迟,避免启动期干扰
184
- await sleep(10 * 60 * 1000);
185
-
186
- while (true) {
187
- syncWorkspace();
188
- await sleep(INTERVAL);
189
- }
190
- }
191
-
192
- main().catch(e => {
193
- console.error("[hf-safe] Fatal:", e.message);
194
- });