8900 commited on
Commit
0cd23cb
·
verified ·
1 Parent(s): 7c0d839

Update setup-hf-config.mjs

Browse files
Files changed (1) hide show
  1. setup-hf-config.mjs +36 -73
setup-hf-config.mjs CHANGED
@@ -1,54 +1,32 @@
1
  #!/usr/bin/env node
2
  /**
3
- * OpenClaw HF Spaces 配置脚本 - 最终优化版
4
- * 日志输出清晰、专业、结构化
5
  */
6
 
7
- import fs from "node:fs";
8
- import path from "node:path";
 
9
 
10
  const HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
11
  const STATE_DIR = path.join(HOME, ".openclaw");
12
  const CONFIG_PATH = path.join(STATE_DIR, "openclaw.json");
13
 
14
  console.log("=".repeat(60));
15
- console.log("[openclaw-hf-setup] Starting configuration...");
16
  console.log("=".repeat(60));
17
 
18
- // ====================== 工具函数 ======================
19
- function readGatewayToken() {
20
- const fromEnv = (process.env.OPENCLAW_GATEWAY_TOKEN || "").trim();
21
- if (fromEnv) return fromEnv;
22
-
23
- const filePath = (process.env.OPENCLAW_GATEWAY_TOKEN_FILE || "").trim();
24
- if (filePath && fs.existsSync(filePath)) {
25
- try {
26
- return fs.readFileSync(filePath, "utf-8").trim();
27
- } catch (e) {
28
- console.warn(" ⚠️ Failed to read token file");
29
- }
30
- }
31
- return "";
32
- }
33
-
34
- function parseList(val) {
35
- if (!val || !val.trim()) return [];
36
- return val.split(",").map(s => s.trim()).filter(Boolean);
37
- }
38
-
39
  // ====================== 配置读取 ======================
40
- const defaultModel = (process.env.OPENCLAW_HF_DEFAULT_MODEL || "").trim() || "google/gemini-2.0-flash-exp";
41
- const gatewayToken = readGatewayToken();
42
- const gatewayPassword = (process.env.OPENCLAW_GATEWAY_PASSWORD || "").trim();
43
-
44
- const trustedProxies = parseList(process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES || "").length > 0
45
- ? parseList(process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES)
46
- : [
47
- "10.16.0.0/12", "10.20.0.0/12", "10.0.0.0/8",
48
- "172.16.0.0/12", "192.168.0.0/16"
49
- ];
50
 
51
- const allowedOrigins = parseList(process.env.OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS || "");
 
 
 
 
 
 
52
 
53
  // ====================== 配置合并 ======================
54
  let config = {};
@@ -57,70 +35,55 @@ try {
57
  config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
58
  }
59
  } catch (e) {
60
- console.log(" ⚠️ Config file broken, creating new one");
61
  }
62
 
63
- // Agents
64
  config.agents = config.agents || {};
65
  config.agents.defaults = config.agents.defaults || {};
66
  config.agents.defaults.model = config.agents.defaults.model || {};
67
  config.agents.defaults.model.primary = defaultModel;
68
 
69
- // Gateway Auth
70
  config.gateway = config.gateway || {};
71
- const useToken = gatewayToken.length > 0;
72
- const usePassword = gatewayPassword.length > 0 && !useToken;
 
73
 
74
- if (useToken || usePassword) {
75
- config.gateway.auth = config.gateway.auth || {};
76
- if (useToken) {
77
- config.gateway.auth.mode = "token";
78
- config.gateway.auth.token = gatewayToken;
79
- } else {
80
- config.gateway.auth.mode = "password";
81
- config.gateway.auth.password = gatewayPassword;
82
- }
83
- }
84
-
85
- // Control UI
86
  config.gateway.controlUi = config.gateway.controlUi || {};
87
  config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
88
  config.gateway.controlUi.allowInsecureAuth = true;
89
- config.gateway.controlUi.gatewayUrl = "wss://echo8900-hugging-claw.hf.space";
90
 
91
- if (allowedOrigins.length > 0) {
92
- config.gateway.controlUi.allowedOrigins = allowedOrigins;
93
- }
94
 
95
- // Trusted Proxies
96
  config.gateway.trustedProxies = trustedProxies;
97
 
 
 
 
 
 
98
  // ====================== 写入配置 ======================
99
  try {
100
  fs.mkdirSync(STATE_DIR, { recursive: true });
101
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
102
- console.log("✅ Config successfully written to " + CONFIG_PATH);
103
  } catch (e) {
104
- console.error("❌ Failed to write config: " + e.message);
105
  process.exit(1);
106
  }
107
 
108
- // ====================== 美化日志总结 ======================
109
- const authKind = useToken ? "token" : (usePassword ? "password" : "none");
110
-
111
  console.log("\n" + "=".repeat(60));
112
  console.log("📋 CONFIG SUMMARY");
113
  console.log("=".repeat(60));
114
- console.log(` Auth Mode : ${authKind}`);
115
- console.log(` Trusted Proxies : ${trustedProxies.length} (${trustedProxies.join(", ")})`);
116
  console.log(` Default Model : ${defaultModel}`);
117
  console.log(` WebSocket URL : ${config.gateway.controlUi.gatewayUrl}`);
118
- console.log(` Device Auth : ${config.gateway.controlUi.dangerouslyDisableDeviceAuth ? "Disabled" : "Enabled"}`);
 
119
  console.log("=".repeat(60));
120
 
121
- if (authKind === "none") {
122
- console.warn("⚠️ WARNING: No auth set! Please add OPENCLAW_GATEWAY_TOKEN in Space Secrets.");
123
- }
124
-
125
- console.log("🚀 Gateway should start normally now.");
126
- console.log("=".repeat(60));
 
1
  #!/usr/bin/env node
2
  /**
3
+ * OpenClaw HF Spaces 配置脚本 - 最终推荐版
4
+ * 结构清晰、安全、针对 1005/1008 优化
5
  */
6
 
7
+ import fs from 'fs';
8
+ import path from 'path';
9
+ import os from 'os';
10
 
11
  const HOME = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
12
  const STATE_DIR = path.join(HOME, ".openclaw");
13
  const CONFIG_PATH = path.join(STATE_DIR, "openclaw.json");
14
 
15
  console.log("=".repeat(60));
16
+ console.log("[openclaw-hf-setup] Starting final optimized config...");
17
  console.log("=".repeat(60));
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  // ====================== 配置读取 ======================
20
+ const gatewayToken = process.env.OPENCLAW_GATEWAY_TOKEN || "Li8900";
21
+ const defaultModel = process.env.OPENCLAW_HF_DEFAULT_MODEL || "google/gemini-2.0-flash-exp";
 
 
 
 
 
 
 
 
22
 
23
+ const trustedProxies = [
24
+ "10.16.0.0/12",
25
+ "10.20.0.0/12",
26
+ "10.0.0.0/8",
27
+ "172.16.0.0/12",
28
+ "192.168.0.0/16"
29
+ ];
30
 
31
  // ====================== 配置合并 ======================
32
  let config = {};
 
35
  config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
36
  }
37
  } catch (e) {
38
+ console.log("[openclaw-hf-setup] Config file broken, creating new one");
39
  }
40
 
41
+ // Agents 配置
42
  config.agents = config.agents || {};
43
  config.agents.defaults = config.agents.defaults || {};
44
  config.agents.defaults.model = config.agents.defaults.model || {};
45
  config.agents.defaults.model.primary = defaultModel;
46
 
47
+ // Gateway 配置
48
  config.gateway = config.gateway || {};
49
+ config.gateway.auth = config.gateway.auth || {};
50
+ config.gateway.auth.mode = "token";
51
+ config.gateway.auth.token = gatewayToken;
52
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  config.gateway.controlUi = config.gateway.controlUi || {};
54
  config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
55
  config.gateway.controlUi.allowInsecureAuth = true;
 
56
 
57
+ // 关键修复:强制使用 HF 原生 WebSocket
58
+ config.gateway.controlUi.gatewayUrl = "wss://echo8900-hugging-claw.hf.space";
 
59
 
 
60
  config.gateway.trustedProxies = trustedProxies;
61
 
62
+ // Telegram 自动启用
63
+ config.plugins = config.plugins || {};
64
+ config.plugins.telegram = config.plugins.telegram || {};
65
+ config.plugins.telegram.enabled = true;
66
+
67
  // ====================== 写入配置 ======================
68
  try {
69
  fs.mkdirSync(STATE_DIR, { recursive: true });
70
  fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
71
+ console.log(`[openclaw-hf-setup] ✅ Config successfully saved to ${CONFIG_PATH}`);
72
  } catch (e) {
73
+ console.error(`[openclaw-hf-setup] ❌ Failed to write config: ${e.message}`);
74
  process.exit(1);
75
  }
76
 
77
+ // ====================== 日志总结 ======================
 
 
78
  console.log("\n" + "=".repeat(60));
79
  console.log("📋 CONFIG SUMMARY");
80
  console.log("=".repeat(60));
81
+ console.log(` Token : ${gatewayToken ? "******" + gatewayToken.slice(-4) : "none"}`);
82
+ console.log(` Trusted Proxies : ${trustedProxies.length}`);
83
  console.log(` Default Model : ${defaultModel}`);
84
  console.log(` WebSocket URL : ${config.gateway.controlUi.gatewayUrl}`);
85
+ console.log(` Device Auth : Disabled`);
86
+ console.log(` Telegram : Enabled`);
87
  console.log("=".repeat(60));
88
 
89
+ console.log("[openclaw-hf-setup] 🚀 Configuration completed. Gateway should start normally.");