8900 commited on
Commit
d9ae21e
Β·
verified Β·
1 Parent(s): bdea495

Update setup-hf-config.mjs

Browse files
Files changed (1) hide show
  1. setup-hf-config.mjs +58 -88
setup-hf-config.mjs CHANGED
@@ -5,19 +5,11 @@ import https from "node:https";
5
  // ============================================================
6
  // OpenClaw HF Spaces - Production Config Writer
7
  //
8
- // KEY BEHAVIOR:
9
- // If openclaw.json exists and is valid -> PATCH only (preserve user settings)
10
- // If openclaw.json missing -> write fresh template
11
  //
12
- // What gets patched every run:
13
- // gateway.auth (from env OPENCLAW_GATEWAY_TOKEN)
14
- // env.vars (all provider API keys)
15
- // channels.telegram (from env TELEGRAM_BOT_TOKEN)
16
- // agents.defaults.workspace (ensure correct path)
17
- // gateway.trustedProxies (ensure not empty)
18
- //
19
- // What is NEVER touched when patching:
20
- // All user settings from Control UI (model choices, agent config, etc.)
21
  // ============================================================
22
 
23
  var HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
@@ -85,7 +77,26 @@ var trustedProxies = envProxies.length > 0 ? envProxies : [
85
  "10.20.0.1", "172.17.0.1", "127.0.0.1"
86
  ];
87
 
88
- // –– Telegram helpers –––––––––––––––––––
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  function tgRequest(token, method, body) {
90
  return new Promise(function(resolve) {
91
  var data = JSON.stringify(body || {});
@@ -93,16 +104,11 @@ var req = https.request({
93
  hostname: "api.telegram.org",
94
  path: "/bot" + token + "/" + method,
95
  method: "POST",
96
- headers: {
97
- "Content-Type": "application/json",
98
- "Content-Length": Buffer.byteLength(data)
99
- }
100
  }, function(res) {
101
  var buf = "";
102
  res.on("data", function(c) { buf += c; });
103
- res.on("end", function() {
104
- try { resolve(JSON.parse(buf)); } catch (e) { resolve(null); }
105
- });
106
  });
107
  req.on("error", function() { resolve(null); });
108
  req.setTimeout(8000, function() { req.destroy(); resolve(null); });
@@ -111,11 +117,16 @@ req.end();
111
  });
112
  }
113
 
114
- async function setupWebhook(token) {
115
- if (!SPACE_HOST) {
116
- console.log("[setup] Telegram: set SPACE_HOST for auto webhook");
117
- return;
118
- }
 
 
 
 
 
119
  var url = "https://" + SPACE_HOST + "/tg-webhook";
120
  var r = await tgRequest(token, "setWebhook", {
121
  url: url, drop_pending_updates: true, max_connections: 10
@@ -123,78 +134,47 @@ url: url, drop_pending_updates: true, max_connections: 10
123
  if (r && r.ok) {
124
  console.log("[setup] Telegram: webhook registered -> " + url);
125
  } else {
126
- console.log("[setup] Telegram: webhook failed (HF outbound blocked). Open in browser:");
127
  console.log(" https://api.telegram.org/bot" + token +
128
  "/setWebhook?url=" + url + "&drop_pending_updates=true");
129
  }
 
 
130
  }
131
 
132
- function buildAuth() {
133
- return gatewayToken
134
- ? { mode: "token", token: gatewayToken }
135
- : { mode: "password", password: gatewayPassword };
136
- }
137
-
138
- function buildEnvVars() {
139
- var vars = {};
140
- providerKeys.forEach(function(pk) {
141
- vars[pk] = (process.env[pk] || "").trim();
142
- });
143
- return vars;
144
- }
145
-
146
- // –– main –––––––––––––––––––––––––
147
- (async function() {
148
- fs.mkdirSync(STATE_DIR, { recursive: true });
149
- fs.mkdirSync(WORKSPACE, { recursive: true });
150
- fs.mkdirSync(path.join(WORKSPACE, "memory"), { recursive: true });
151
-
152
- var token = envStr("TELEGRAM_BOT_TOKEN");
153
- if (token) await setupWebhook(token);
154
-
155
- // –– PATCH MODE: existing valid config found ––––
156
  if (fs.existsSync(CONFIG_PATH)) {
157
- var raw = fs.readFileSync(CONFIG_PATH, "utf-8").trim();
158
  var parsed = null;
159
- try { parsed = JSON.parse(raw); } catch (e) { /* invalid, fall through to fresh write */ }
160
 
161
  if (parsed) {
162
- // Patch only env-derived fields, preserve everything else
163
- parsed.gateway = parsed.gateway || {};
164
- parsed.gateway.auth = buildAuth();
165
  if (!parsed.gateway.trustedProxies || parsed.gateway.trustedProxies.length < 5) {
166
- parsed.gateway.trustedProxies = trustedProxies;
167
- }
168
- parsed.agents = parsed.agents || {};
169
- parsed.agents.defaults = parsed.agents.defaults || {};
170
- parsed.agents.defaults.workspace = WORKSPACE;
171
- parsed.env = parsed.env || {};
172
- parsed.env.vars = buildEnvVars();
173
-
174
- if (token) {
175
- parsed.channels = {
176
- telegram: {
177
- enabled: true,
178
- accounts: { main: { botToken: token, apiRoot: "https://api.telegram.org" } }
179
- }
180
- };
181
  }
 
 
 
 
 
 
182
 
183
  fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
184
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(parsed, null, 2), "utf-8");
185
  console.log("[setup] Patched existing config (user settings preserved)");
186
- console.log("[setup] workspace = " + WORKSPACE);
187
  console.log("[setup] env.vars = " + providerKeys.length);
 
188
  return;
189
  }
190
  }
191
 
192
- // –– FRESH MODE: no config or invalid config ––––
193
- console.log("[setup] No valid config found - writing fresh template");
194
-
195
  var config = {
196
  gateway: {
197
- auth: buildAuth(),
198
  controlUi: {
199
  allowInsecureAuth: true,
200
  allowedOrigins: ["*"],
@@ -203,20 +183,10 @@ dangerouslyAllowHostHeaderOriginFallback: true
203
  },
204
  trustedProxies: trustedProxies
205
  },
206
- agents: {
207
- defaults: { model: defaultModel, workspace: WORKSPACE }
208
- },
209
- env: { vars: buildEnvVars() }
210
  };
211
-
212
- if (token) {
213
- config.channels = {
214
- telegram: {
215
- enabled: true,
216
- accounts: { main: { botToken: token, apiRoot: "https://api.telegram.org" } }
217
- }
218
- };
219
- }
220
 
221
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
222
  console.log("[setup] Done.");
 
5
  // ============================================================
6
  // OpenClaw HF Spaces - Production Config Writer
7
  //
8
+ // PATCH MODE: if openclaw.json already exists and is valid JSON,
9
+ // only update: auth, env.vars, channels, workspace path.
10
+ // ALL user settings from Control UI are preserved.
11
  //
12
+ // FRESH MODE: if no config exists, write full template.
 
 
 
 
 
 
 
 
13
  // ============================================================
14
 
15
  var HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
 
77
  "10.20.0.1", "172.17.0.1", "127.0.0.1"
78
  ];
79
 
80
+ // –– helpers ———————————————–
81
+ function buildAuth() {
82
+ return gatewayToken
83
+ ? { mode: "token", token: gatewayToken }
84
+ : { mode: "password", password: gatewayPassword };
85
+ }
86
+ function buildEnvVars() {
87
+ var vars = {};
88
+ providerKeys.forEach(function(pk) { vars[pk] = (process.env[pk] || "").trim(); });
89
+ return vars;
90
+ }
91
+ function buildTelegramChannel(token) {
92
+ return {
93
+ telegram: {
94
+ enabled: true,
95
+ accounts: { main: { botToken: token, apiRoot: "https://api.telegram.org" } }
96
+ }
97
+ };
98
+ }
99
+
100
  function tgRequest(token, method, body) {
101
  return new Promise(function(resolve) {
102
  var data = JSON.stringify(body || {});
 
104
  hostname: "api.telegram.org",
105
  path: "/bot" + token + "/" + method,
106
  method: "POST",
107
+ headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(data) }
 
 
 
108
  }, function(res) {
109
  var buf = "";
110
  res.on("data", function(c) { buf += c; });
111
+ res.on("end", function() { try { resolve(JSON.parse(buf)); } catch (e) { resolve(null); } });
 
 
112
  });
113
  req.on("error", function() { resolve(null); });
114
  req.setTimeout(8000, function() { req.destroy(); resolve(null); });
 
117
  });
118
  }
119
 
120
+ // –– main –––––––––––––––––––––––––
121
+ (async function() {
122
+ fs.mkdirSync(STATE_DIR, { recursive: true });
123
+ fs.mkdirSync(WORKSPACE, { recursive: true });
124
+ fs.mkdirSync(path.join(WORKSPACE, "memory"), { recursive: true });
125
+
126
+ var token = envStr("TELEGRAM_BOT_TOKEN");
127
+
128
+ // Register webhook if possible
129
+ if (token && SPACE_HOST) {
130
  var url = "https://" + SPACE_HOST + "/tg-webhook";
131
  var r = await tgRequest(token, "setWebhook", {
132
  url: url, drop_pending_updates: true, max_connections: 10
 
134
  if (r && r.ok) {
135
  console.log("[setup] Telegram: webhook registered -> " + url);
136
  } else {
137
+ console.log("[setup] Telegram: webhook failed. Open in browser:");
138
  console.log(" https://api.telegram.org/bot" + token +
139
  "/setWebhook?url=" + url + "&drop_pending_updates=true");
140
  }
141
+ } else if (!token) {
142
+ console.log("[setup] Telegram: disabled (no TELEGRAM_BOT_TOKEN)");
143
  }
144
 
145
+ // –– PATCH MODE: existing valid config found ––
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  if (fs.existsSync(CONFIG_PATH)) {
147
+ var raw = fs.readFileSync(CONFIG_PATH, "utf-8").trim();
148
  var parsed = null;
149
+ try { parsed = JSON.parse(raw); } catch (e) { /* fall through to fresh write */ }
150
 
151
  if (parsed) {
152
+ parsed.gateway = parsed.gateway || {};
153
+ parsed.gateway.auth = buildAuth();
 
154
  if (!parsed.gateway.trustedProxies || parsed.gateway.trustedProxies.length < 5) {
155
+ parsed.gateway.trustedProxies = trustedProxies;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  }
157
+ parsed.agents = parsed.agents || {};
158
+ parsed.agents.defaults = parsed.agents.defaults || {};
159
+ parsed.agents.defaults.workspace = WORKSPACE;
160
+ parsed.env = parsed.env || {};
161
+ parsed.env.vars = buildEnvVars();
162
+ if (token) parsed.channels = buildTelegramChannel(token);
163
 
164
  fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
165
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(parsed, null, 2), "utf-8");
166
  console.log("[setup] Patched existing config (user settings preserved)");
 
167
  console.log("[setup] env.vars = " + providerKeys.length);
168
+ console.log("[setup] workspace = " + WORKSPACE);
169
  return;
170
  }
171
  }
172
 
173
+ // –– FRESH MODE: no config or invalid config ––
174
+ console.log("[setup] Writing fresh config template");
 
175
  var config = {
176
  gateway: {
177
+ auth: buildAuth(),
178
  controlUi: {
179
  allowInsecureAuth: true,
180
  allowedOrigins: ["*"],
 
183
  },
184
  trustedProxies: trustedProxies
185
  },
186
+ agents: { defaults: { model: defaultModel, workspace: WORKSPACE } },
187
+ env: { vars: buildEnvVars() }
 
 
188
  };
189
+ if (token) config.channels = buildTelegramChannel(token);
 
 
 
 
 
 
 
 
190
 
191
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
192
  console.log("[setup] Done.");