Spaces:
Running
Running
Create setup-hf-config.mjs
Browse files- setup-hf-config.mjs +40 -0
setup-hf-config.mjs
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env node
|
| 2 |
+
/**
|
| 3 |
+
* One-time setup for OpenClaw on Hugging Face Spaces.
|
| 4 |
+
* Writes or merges openclaw.json so that:
|
| 5 |
+
* - agents.defaults.model.primary uses Hugging Face Inference (from OPENCLAW_HF_DEFAULT_MODEL or default).
|
| 6 |
+
* - gateway.auth.token is set from OPENCLAW_GATEWAY_TOKEN if provided.
|
| 7 |
+
* HF_TOKEN is read by the gateway from the environment (Space Secrets); this script only sets the default model and optional token in config.
|
| 8 |
+
*/
|
| 9 |
+
import fs from "node:fs";
|
| 10 |
+
import path from "node:path";
|
| 11 |
+
|
| 12 |
+
const home = process.env.OPENCLAW_HOME || process.env.HOME || "/home/user";
|
| 13 |
+
const stateDir = path.join(home, ".openclaw");
|
| 14 |
+
const configPath = path.join(stateDir, "openclaw.json");
|
| 15 |
+
|
| 16 |
+
const defaultModel =
|
| 17 |
+
process.env.OPENCLAW_HF_DEFAULT_MODEL?.trim() || "huggingface/deepseek-ai/DeepSeek-R1";
|
| 18 |
+
const gatewayToken = process.env.OPENCLAW_GATEWAY_TOKEN?.trim();
|
| 19 |
+
|
| 20 |
+
let config = {};
|
| 21 |
+
if (fs.existsSync(configPath)) {
|
| 22 |
+
try {
|
| 23 |
+
config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
| 24 |
+
} catch {
|
| 25 |
+
// keep config empty
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
if (!config.agents) config.agents = {};
|
| 30 |
+
if (!config.agents.defaults) config.agents.defaults = {};
|
| 31 |
+
if (!config.agents.defaults.model) config.agents.defaults.model = {};
|
| 32 |
+
config.agents.defaults.model.primary = defaultModel;
|
| 33 |
+
|
| 34 |
+
if (gatewayToken && !config.gateway) config.gateway = {};
|
| 35 |
+
if (gatewayToken && !config.gateway.auth) config.gateway.auth = {};
|
| 36 |
+
if (gatewayToken) config.gateway.auth.mode = "token";
|
| 37 |
+
if (gatewayToken) config.gateway.auth.token = gatewayToken;
|
| 38 |
+
|
| 39 |
+
fs.mkdirSync(stateDir, { recursive: true });
|
| 40 |
+
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
|