8900 commited on
Commit
c4e4f48
Β·
verified Β·
1 Parent(s): a7e5574

Update setup-hf-config.mjs

Browse files
Files changed (1) hide show
  1. setup-hf-config.mjs +42 -90
setup-hf-config.mjs CHANGED
@@ -4,6 +4,8 @@ import https from "node:https";
4
 
5
  // ============================================================
6
  // OpenClaw HF Spaces - Production Config Writer
 
 
7
  // ============================================================
8
 
9
  var HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
@@ -23,16 +25,6 @@ function envStr(key) {
23
  return (process.env[key] || "").trim();
24
  }
25
 
26
- function isValidJson(p) {
27
- try {
28
- if (!fs.existsSync(p)) return false;
29
- var c = fs.readFileSync(p, "utf-8").trim();
30
- if (!c) return false;
31
- JSON.parse(c);
32
- return true;
33
- } catch (e) { return false; }
34
- }
35
-
36
  // –– auth –––––––––––––––––––––––––
37
 
38
  var gatewayToken = envStr("OPENCLAW_GATEWAY_TOKEN");
@@ -95,6 +87,34 @@ var trustedProxies = envProxies.length > 0 ? envProxies : [
95
  "127.0.0.1"
96
  ];
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  // –– Telegram –––––––––––––––––––––––
99
 
100
  function tgRequest(token, method, body) {
@@ -122,8 +142,12 @@ req.end();
122
  });
123
  }
124
 
125
- async function buildTelegramChannel(token) {
126
- if (!token) return null;
 
 
 
 
127
 
128
  if (SPACE_HOST) {
129
  var webhookUrl = "https://" + SPACE_HOST + "/tg-webhook";
@@ -141,10 +165,10 @@ console.log(" https://api.telegram.org/bot" + token +
141
  "/setWebhook?url=" + webhookUrl + "&drop_pending_updates=true");
142
  }
143
  } else {
144
- console.log("[setup] Telegram: set SPACE_HOST for auto webhook registration");
145
  }
146
 
147
- return {
148
  telegram: {
149
  enabled: true,
150
  accounts: {
@@ -152,93 +176,21 @@ main: { botToken: token, apiRoot: "https://api.telegram.org" }
152
  }
153
  }
154
  };
 
155
  }
156
 
157
  // –– main –––––––––––––––––––––––––
158
 
159
- (async function main() {
160
  fs.mkdirSync(STATE_DIR, { recursive: true });
161
  fs.mkdirSync(WORKSPACE, { recursive: true });
162
  fs.mkdirSync(path.join(WORKSPACE, "memory"), { recursive: true });
163
 
164
- var token = envStr("TELEGRAM_BOT_TOKEN");
165
-
166
- if (isValidJson(CONFIG_PATH)) {
167
- console.log("[setup] Existing config found - patching only env.vars and channels");
168
-
169
- var existing = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
170
-
171
- if (!existing.env) existing.env = {};
172
- if (!existing.env.vars) existing.env.vars = {};
173
- providerKeys.forEach(function(pk) {
174
- existing.env.vars[pk] = (process.env[pk] || "").trim();
175
- });
176
-
177
- if (token) {
178
- var ch = await buildTelegramChannel(token);
179
- if (ch) existing.channels = ch;
180
- console.log("[setup] Telegram: configured");
181
- }
182
-
183
- if (!existing.agents) existing.agents = {};
184
- if (!existing.agents.defaults) existing.agents.defaults = {};
185
- if (!existing.agents.defaults.workspace) {
186
- existing.agents.defaults.workspace = WORKSPACE;
187
- }
188
-
189
- if (!existing.gateway) existing.gateway = {};
190
- if (!existing.gateway.trustedProxies ||
191
- existing.gateway.trustedProxies.length === 0) {
192
- existing.gateway.trustedProxies = trustedProxies;
193
- }
194
 
 
195
  fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
196
- fs.writeFileSync(CONFIG_PATH, JSON.stringify(existing, null, 2), "utf-8");
197
-
198
- console.log("[setup] Patch done (user settings preserved).");
199
- console.log("[setup] env.vars = " + providerKeys.length);
200
- console.log("[setup] config = " + CONFIG_PATH);
201
- return;
202
  }
203
-
204
- console.log("[setup] No existing config - writing fresh template");
205
-
206
- var config = {
207
- gateway: {
208
- auth: gatewayToken
209
- ? { mode: "token", token: gatewayToken }
210
- : { mode: "password", password: gatewayPassword },
211
- controlUi: {
212
- allowInsecureAuth: true,
213
- allowedOrigins: ["*"],
214
- dangerouslyDisableDeviceAuth: true,
215
- dangerouslyAllowHostHeaderOriginFallback: true
216
- },
217
- trustedProxies: trustedProxies
218
- },
219
- agents: {
220
- defaults: {
221
- model: defaultModel,
222
- workspace: WORKSPACE
223
- }
224
- },
225
- env: { vars: {} }
226
- };
227
-
228
- providerKeys.forEach(function(pk) {
229
- config.env.vars[pk] = (process.env[pk] || "").trim();
230
- });
231
-
232
- if (token) {
233
- var channels = await buildTelegramChannel(token);
234
- if (channels) {
235
- config.channels = channels;
236
- console.log("[setup] Telegram: configured");
237
- }
238
- } else {
239
- console.log("[setup] Telegram: disabled (no TELEGRAM_BOT_TOKEN)");
240
- }
241
-
242
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
243
 
244
  console.log("[setup] Done.");
 
4
 
5
  // ============================================================
6
  // OpenClaw HF Spaces - Production Config Writer
7
+ // Runs ONCE per container lifetime (entrypoint.sh marker).
8
+ // Always writes fresh config from env vars.
9
  // ============================================================
10
 
11
  var HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
 
25
  return (process.env[key] || "").trim();
26
  }
27
 
 
 
 
 
 
 
 
 
 
 
28
  // –– auth –––––––––––––––––––––––––
29
 
30
  var gatewayToken = envStr("OPENCLAW_GATEWAY_TOKEN");
 
87
  "127.0.0.1"
88
  ];
89
 
90
+ // –– build config β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
91
+
92
+ var config = {
93
+ gateway: {
94
+ auth: gatewayToken
95
+ ? { mode: "token", token: gatewayToken }
96
+ : { mode: "password", password: gatewayPassword },
97
+ controlUi: {
98
+ allowInsecureAuth: true,
99
+ allowedOrigins: ["*"],
100
+ dangerouslyDisableDeviceAuth: true,
101
+ dangerouslyAllowHostHeaderOriginFallback: true
102
+ },
103
+ trustedProxies: trustedProxies
104
+ },
105
+ agents: {
106
+ defaults: {
107
+ model: defaultModel,
108
+ workspace: WORKSPACE
109
+ }
110
+ },
111
+ env: { vars: {} }
112
+ };
113
+
114
+ providerKeys.forEach(function(pk) {
115
+ config.env.vars[pk] = (process.env[pk] || "").trim();
116
+ });
117
+
118
  // –– Telegram –––––––––––––––––––––––
119
 
120
  function tgRequest(token, method, body) {
 
142
  });
143
  }
144
 
145
+ async function setupTelegram() {
146
+ var token = envStr("TELEGRAM_BOT_TOKEN");
147
+ if (!token) {
148
+ console.log("[setup] Telegram: disabled (no TELEGRAM_BOT_TOKEN)");
149
+ return;
150
+ }
151
 
152
  if (SPACE_HOST) {
153
  var webhookUrl = "https://" + SPACE_HOST + "/tg-webhook";
 
165
  "/setWebhook?url=" + webhookUrl + "&drop_pending_updates=true");
166
  }
167
  } else {
168
+ console.log("[setup] Telegram: set SPACE_HOST for auto webhook");
169
  }
170
 
171
+ config.channels = {
172
  telegram: {
173
  enabled: true,
174
  accounts: {
 
176
  }
177
  }
178
  };
179
+ console.log("[setup] Telegram: configured");
180
  }
181
 
182
  // –– main –––––––––––––––––––––––––
183
 
184
+ (async function() {
185
  fs.mkdirSync(STATE_DIR, { recursive: true });
186
  fs.mkdirSync(WORKSPACE, { recursive: true });
187
  fs.mkdirSync(path.join(WORKSPACE, "memory"), { recursive: true });
188
 
189
+ await setupTelegram();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
 
191
+ if (fs.existsSync(CONFIG_PATH)) {
192
  fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
 
 
 
 
 
 
193
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
195
 
196
  console.log("[setup] Done.");