Spaces:
Sleeping
Sleeping
Create setup-hf-config.mjs
Browse files- setup-hf-config.mjs +116 -0
setup-hf-config.mjs
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env node
|
| 2 |
+
/**
|
| 3 |
+
* setup-hf-config.mjs
|
| 4 |
+
* Schreibt/merged ~/.openclaw/openclaw.json für Hugging Face Spaces.
|
| 5 |
+
*
|
| 6 |
+
* Erwartete ENV/Secrets (HF Settings → Variables and secrets):
|
| 7 |
+
* - Secrets:
|
| 8 |
+
* - OPENCLAW_GATEWAY_TOKEN (oder Fallback: OPENCLAW_TOKEN)
|
| 9 |
+
* - HF_TOKEN (optional, je nach OpenClaw/HF-Use)
|
| 10 |
+
* - Variables:
|
| 11 |
+
* - OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS="https://morris5444-openclaw.hf.space"
|
| 12 |
+
* - OPENCLAW_GATEWAY_TRUSTED_PROXIES="10.0.0.0/8" oder IP-Liste
|
| 13 |
+
* - OPENCLAW_HF_DEFAULT_MODEL="huggingface/Qwen/Qwen2.5-Coder-32B-Instruct" (optional)
|
| 14 |
+
*/
|
| 15 |
+
|
| 16 |
+
import fs from "node:fs";
|
| 17 |
+
import path from "node:path";
|
| 18 |
+
|
| 19 |
+
const home = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
|
| 20 |
+
const stateDir = path.join(home, ".openclaw");
|
| 21 |
+
const configPath = path.join(stateDir, "openclaw.json");
|
| 22 |
+
|
| 23 |
+
function readGatewayToken() {
|
| 24 |
+
// Fallback: du nutzt aktuell OPENCLAW_TOKEN. Das akzeptieren wir.
|
| 25 |
+
const tok =
|
| 26 |
+
process.env.OPENCLAW_GATEWAY_TOKEN?.trim() ||
|
| 27 |
+
process.env.OPENCLAW_TOKEN?.trim() ||
|
| 28 |
+
"";
|
| 29 |
+
return tok;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
function splitCsv(val) {
|
| 33 |
+
return val
|
| 34 |
+
.split(",")
|
| 35 |
+
.map((s) => s.trim())
|
| 36 |
+
.filter(Boolean);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
const defaultModel =
|
| 40 |
+
process.env.OPENCLAW_HF_DEFAULT_MODEL?.trim() ||
|
| 41 |
+
"huggingface/Qwen/Qwen2.5-Coder-32B-Instruct";
|
| 42 |
+
|
| 43 |
+
const gatewayToken = readGatewayToken();
|
| 44 |
+
const gatewayPassword = process.env.OPENCLAW_GATEWAY_PASSWORD?.trim() || "";
|
| 45 |
+
|
| 46 |
+
// Diese IPs sind aus funktionierenden HF/OpenClaw-Setups bekannt.
|
| 47 |
+
// Du kannst stattdessen OPENCLAW_GATEWAY_TRUSTED_PROXIES=10.0.0.0/8 setzen,
|
| 48 |
+
// falls OpenClaw CIDR akzeptiert – sonst nimm die Liste.
|
| 49 |
+
const DEFAULT_HF_TRUSTED_PROXY_IPS = [
|
| 50 |
+
"10.16.4.123",
|
| 51 |
+
"10.16.34.155",
|
| 52 |
+
"10.20.1.9",
|
| 53 |
+
"10.20.1.222",
|
| 54 |
+
"10.20.26.157",
|
| 55 |
+
"10.20.31.87",
|
| 56 |
+
];
|
| 57 |
+
|
| 58 |
+
const trustedProxiesRaw = process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES?.trim() || "";
|
| 59 |
+
const trustedProxies =
|
| 60 |
+
trustedProxiesRaw.length > 0 ? splitCsv(trustedProxiesRaw) : DEFAULT_HF_TRUSTED_PROXY_IPS;
|
| 61 |
+
|
| 62 |
+
const allowedOriginsRaw = process.env.OPENCLAW_CONTROL_UI_ALLOWED_ORIGINS?.trim() || "";
|
| 63 |
+
const allowedOrigins = allowedOriginsRaw.length > 0 ? splitCsv(allowedOriginsRaw) : [];
|
| 64 |
+
|
| 65 |
+
let config = {};
|
| 66 |
+
if (fs.existsSync(configPath)) {
|
| 67 |
+
try {
|
| 68 |
+
config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
| 69 |
+
} catch {
|
| 70 |
+
config = {};
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
// --- Merge / Defaults ---
|
| 75 |
+
config.gateway ??= {};
|
| 76 |
+
config.gateway.mode = "local";
|
| 77 |
+
config.gateway.bind = "lan";
|
| 78 |
+
|
| 79 |
+
// Trusted proxies (HF Reverse Proxy)
|
| 80 |
+
config.gateway.trustedProxies = trustedProxies;
|
| 81 |
+
|
| 82 |
+
// Default model
|
| 83 |
+
config.agents ??= {};
|
| 84 |
+
config.agents.defaults ??= {};
|
| 85 |
+
config.agents.defaults.model ??= {};
|
| 86 |
+
config.agents.defaults.model.primary = defaultModel;
|
| 87 |
+
|
| 88 |
+
// Auth
|
| 89 |
+
if (gatewayToken || gatewayPassword) {
|
| 90 |
+
config.gateway.auth ??= {};
|
| 91 |
+
if (gatewayToken) {
|
| 92 |
+
config.gateway.auth.mode = "token";
|
| 93 |
+
config.gateway.auth.token = gatewayToken;
|
| 94 |
+
} else {
|
| 95 |
+
config.gateway.auth.mode = "password";
|
| 96 |
+
config.gateway.auth.password = gatewayPassword;
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
// HF Spaces: Device Auth ist oft unpraktisch.
|
| 100 |
+
config.gateway.controlUi ??= {};
|
| 101 |
+
config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Control UI allowed origins (wichtig für HF Domain)
|
| 105 |
+
if (allowedOrigins.length > 0) {
|
| 106 |
+
config.gateway.controlUi ??= {};
|
| 107 |
+
config.gateway.controlUi.allowedOrigins = allowedOrigins;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// Persist
|
| 111 |
+
fs.mkdirSync(stateDir, { recursive: true });
|
| 112 |
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
|
| 113 |
+
|
| 114 |
+
console.log(
|
| 115 |
+
`[openclaw-hf-setup] wrote ${configPath} trustedProxies=${trustedProxies.length} allowedOrigins=${allowedOrigins.length}`
|
| 116 |
+
);
|