8900 commited on
Update setup-hf-config.mjs
Browse files- setup-hf-config.mjs +20 -36
setup-hf-config.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
#!/usr/bin/env node
|
| 2 |
import fs from "node:fs";
|
| 3 |
import path from "node:path";
|
| 4 |
|
|
@@ -6,28 +5,26 @@ const home = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
|
|
| 6 |
const stateDir = path.join(home, ".openclaw");
|
| 7 |
const configPath = path.join(stateDir, "openclaw.json");
|
| 8 |
|
| 9 |
-
// 读取 Gateway Token
|
| 10 |
function readGatewayToken() {
|
| 11 |
const fromEnv = (process.env.OPENCLAW_GATEWAY_TOKEN || "").trim();
|
| 12 |
if (fromEnv) return fromEnv;
|
| 13 |
const filePath = (process.env.OPENCLAW_GATEWAY_TOKEN_FILE || "").trim();
|
| 14 |
if (filePath && fs.existsSync(filePath)) {
|
| 15 |
-
try { return fs.readFileSync(filePath,"utf-8").trim(); } catch { return ""; }
|
| 16 |
}
|
| 17 |
return "";
|
| 18 |
}
|
| 19 |
|
| 20 |
-
// 将逗号分隔的字符串转数组
|
| 21 |
function parseList(val) {
|
| 22 |
if (!val || !val.trim()) return [];
|
| 23 |
return val.split(",").map(s => s.trim()).filter(Boolean);
|
| 24 |
}
|
| 25 |
|
|
|
|
| 26 |
const defaultModel = (process.env.OPENCLAW_HF_DEFAULT_MODEL || "").trim() || "huggingface/deepseek-ai/DeepSeek-R1";
|
| 27 |
const gatewayToken = readGatewayToken();
|
| 28 |
const gatewayPassword = (process.env.OPENCLAW_GATEWAY_PASSWORD || "").trim();
|
| 29 |
|
| 30 |
-
// 默认 HF 代理 IP(可覆盖)
|
| 31 |
const DEFAULT_HF_TRUSTED_PROXY_IPS = [
|
| 32 |
"10.16.4.123","10.16.7.92","10.16.18.232","10.16.34.155",
|
| 33 |
"10.16.43.133","10.20.1.9","10.20.1.222","10.20.26.157",
|
|
@@ -40,25 +37,20 @@ const trustedProxies = trustedProxiesRaw ? parseList(trustedProxiesRaw) : DEFAUL
|
|
| 40 |
const allowedOrigins = parseList(process.env.OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS || "");
|
| 41 |
|
| 42 |
let config = {};
|
| 43 |
-
try {
|
| 44 |
-
if (fs.existsSync(configPath)) {
|
| 45 |
-
config = JSON.parse(fs.readFileSync(configPath,"utf-8"));
|
| 46 |
-
}
|
| 47 |
-
} catch { config = {}; }
|
| 48 |
|
| 49 |
-
|
| 50 |
-
config.agents
|
| 51 |
-
config.agents.defaults
|
| 52 |
-
config.agents.defaults.model ??= {};
|
| 53 |
config.agents.defaults.model.primary = defaultModel;
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
const useTokenAuth = gatewayToken.length > 0;
|
| 58 |
const usePasswordAuth = gatewayPassword.length > 0 && !useTokenAuth;
|
| 59 |
|
| 60 |
if (useTokenAuth || usePasswordAuth) {
|
| 61 |
-
config.gateway.auth
|
| 62 |
if (useTokenAuth) {
|
| 63 |
config.gateway.auth.mode = "token";
|
| 64 |
config.gateway.auth.token = gatewayToken;
|
|
@@ -68,30 +60,22 @@ if (useTokenAuth || usePasswordAuth) {
|
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
-
|
| 72 |
-
config.gateway.controlUi ??= {};
|
| 73 |
config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
|
| 74 |
config.gateway.controlUi.allowInsecureAuth = true;
|
| 75 |
-
if (allowedOrigins.length>0) config.gateway.controlUi.allowedOrigins = allowedOrigins;
|
| 76 |
|
| 77 |
-
|
| 78 |
-
config.gateway.trustedProxies = trustedProxies;
|
| 79 |
|
| 80 |
-
|
| 81 |
-
config.env ??= {};
|
| 82 |
-
config.env.vars = {
|
| 83 |
-
GEMINI_API_KEY: process.env.GEMINI_API_KEY,
|
| 84 |
-
GROQ_API_KEY: process.env.GROQ_API_KEY,
|
| 85 |
-
HF_TOKEN: process.env.HF_TOKEN,
|
| 86 |
-
TELEGRAM_BOT_TOKEN: process.env.TELEGRAM_BOT_TOKEN
|
| 87 |
-
};
|
| 88 |
|
| 89 |
-
|
| 90 |
-
fs.mkdirSync(stateDir, { recursive: true });
|
| 91 |
-
fs.writeFileSync(configPath, JSON.stringify(config,null,2),"utf-8");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
// 日志
|
| 94 |
const authKind = useTokenAuth ? "token" : (usePasswordAuth ? "password" : "none");
|
| 95 |
console.log(`[openclaw-hf-setup] auth=${authKind} trustedProxies=${trustedProxies.length} model=${defaultModel} -> ${configPath}`);
|
| 96 |
-
if (authKind === "none") console.warn("[openclaw-hf-setup] No auth set. Add OPENCLAW_GATEWAY_TOKEN in Space Secrets.");
|
| 97 |
-
console.log("🔥 OpenClaw HF 生产级配置完成");
|
|
|
|
|
|
|
| 1 |
import fs from "node:fs";
|
| 2 |
import path from "node:path";
|
| 3 |
|
|
|
|
| 5 |
const stateDir = path.join(home, ".openclaw");
|
| 6 |
const configPath = path.join(stateDir, "openclaw.json");
|
| 7 |
|
|
|
|
| 8 |
function readGatewayToken() {
|
| 9 |
const fromEnv = (process.env.OPENCLAW_GATEWAY_TOKEN || "").trim();
|
| 10 |
if (fromEnv) return fromEnv;
|
| 11 |
const filePath = (process.env.OPENCLAW_GATEWAY_TOKEN_FILE || "").trim();
|
| 12 |
if (filePath && fs.existsSync(filePath)) {
|
| 13 |
+
try { return fs.readFileSync(filePath, "utf-8").trim(); } catch { return ""; }
|
| 14 |
}
|
| 15 |
return "";
|
| 16 |
}
|
| 17 |
|
|
|
|
| 18 |
function parseList(val) {
|
| 19 |
if (!val || !val.trim()) return [];
|
| 20 |
return val.split(",").map(s => s.trim()).filter(Boolean);
|
| 21 |
}
|
| 22 |
|
| 23 |
+
// 默认模型
|
| 24 |
const defaultModel = (process.env.OPENCLAW_HF_DEFAULT_MODEL || "").trim() || "huggingface/deepseek-ai/DeepSeek-R1";
|
| 25 |
const gatewayToken = readGatewayToken();
|
| 26 |
const gatewayPassword = (process.env.OPENCLAW_GATEWAY_PASSWORD || "").trim();
|
| 27 |
|
|
|
|
| 28 |
const DEFAULT_HF_TRUSTED_PROXY_IPS = [
|
| 29 |
"10.16.4.123","10.16.7.92","10.16.18.232","10.16.34.155",
|
| 30 |
"10.16.43.133","10.20.1.9","10.20.1.222","10.20.26.157",
|
|
|
|
| 37 |
const allowedOrigins = parseList(process.env.OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS || "");
|
| 38 |
|
| 39 |
let config = {};
|
| 40 |
+
try { if (fs.existsSync(configPath)) config = JSON.parse(fs.readFileSync(configPath, "utf-8")); } catch { config = {}; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
if (!config.agents) config.agents = {};
|
| 43 |
+
if (!config.agents.defaults) config.agents.defaults = {};
|
| 44 |
+
if (!config.agents.defaults.model) config.agents.defaults.model = {};
|
|
|
|
| 45 |
config.agents.defaults.model.primary = defaultModel;
|
| 46 |
|
| 47 |
+
if (!config.gateway) config.gateway = {};
|
| 48 |
+
|
| 49 |
const useTokenAuth = gatewayToken.length > 0;
|
| 50 |
const usePasswordAuth = gatewayPassword.length > 0 && !useTokenAuth;
|
| 51 |
|
| 52 |
if (useTokenAuth || usePasswordAuth) {
|
| 53 |
+
if (!config.gateway.auth) config.gateway.auth = {};
|
| 54 |
if (useTokenAuth) {
|
| 55 |
config.gateway.auth.mode = "token";
|
| 56 |
config.gateway.auth.token = gatewayToken;
|
|
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
+
if (!config.gateway.controlUi) config.gateway.controlUi = {};
|
|
|
|
| 64 |
config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
|
| 65 |
config.gateway.controlUi.allowInsecureAuth = true;
|
|
|
|
| 66 |
|
| 67 |
+
if (allowedOrigins.length > 0) config.gateway.controlUi.allowedOrigins = allowedOrigins;
|
|
|
|
| 68 |
|
| 69 |
+
config.gateway.trustedProxies = trustedProxies;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
try {
|
| 72 |
+
fs.mkdirSync(stateDir, { recursive: true });
|
| 73 |
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
|
| 74 |
+
} catch (e) {
|
| 75 |
+
console.error("[openclaw-hf-setup] failed to write config: " + e.message);
|
| 76 |
+
process.exit(0);
|
| 77 |
+
}
|
| 78 |
|
|
|
|
| 79 |
const authKind = useTokenAuth ? "token" : (usePasswordAuth ? "password" : "none");
|
| 80 |
console.log(`[openclaw-hf-setup] auth=${authKind} trustedProxies=${trustedProxies.length} model=${defaultModel} -> ${configPath}`);
|
| 81 |
+
if (authKind === "none") console.warn("[openclaw-hf-setup] No auth set. Add OPENCLAW_GATEWAY_TOKEN in Space Secrets.");
|
|
|