Update setup-hf-config.mjs
Browse files- setup-hf-config.mjs +141 -353
setup-hf-config.mjs
CHANGED
|
@@ -1,373 +1,161 @@
|
|
| 1 |
-
import fs
|
| 2 |
import path from "node:path";
|
| 3 |
-
import https from "node:https";
|
| 4 |
|
| 5 |
// ============================================================
|
| 6 |
-
//
|
| 7 |
-
//
|
| 8 |
-
//
|
| 9 |
-
//
|
| 10 |
-
//
|
| 11 |
-
//
|
| 12 |
-
// 1. Existing openclaw.json in bucket -> PATCH env vars only
|
| 13 |
-
// 2. /app/openclaw-template.json -> use as base + patch
|
| 14 |
-
// 3. Neither -> minimal fresh + patch
|
| 15 |
-
//
|
| 16 |
-
// Always patched from env vars (never lost on restart):
|
| 17 |
-
// gateway.auth, gateway.controlUi, gateway.trustedProxies
|
| 18 |
-
// agents.defaults.workspace, agents.defaults.model
|
| 19 |
-
// env.vars, models.providers.*.apiKey, channels.telegram
|
| 20 |
// ============================================================
|
| 21 |
-
|
| 22 |
-
var
|
| 23 |
-
var STATE_DIR = path.join(HOME, ".openclaw");
|
| 24 |
var CONFIG_PATH = path.join(STATE_DIR, "openclaw.json");
|
| 25 |
-
var
|
| 26 |
-
var
|
| 27 |
-
var
|
| 28 |
-
|
| 29 |
-
function log(
|
| 30 |
-
function err(
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
// ----------------------------------------------------------------
|
| 35 |
-
// Helpers
|
| 36 |
-
// ----------------------------------------------------------------
|
| 37 |
-
function envStr(key) { return (process.env[key] || "").trim(); }
|
| 38 |
-
|
| 39 |
-
function parseList(val) {
|
| 40 |
-
if (!val || !val.trim()) return [];
|
| 41 |
-
return val.split(",").map(function(s){ return s.trim(); }).filter(Boolean);
|
| 42 |
}
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
// ----------------------------------------------------------------
|
| 47 |
-
var gatewayToken = envStr("OPENCLAW_GATEWAY_TOKEN");
|
| 48 |
-
var gatewayPassword = envStr("OPENCLAW_GATEWAY_PASSWORD");
|
| 49 |
-
if (!gatewayToken && !gatewayPassword) {
|
| 50 |
-
err("FATAL: set OPENCLAW_GATEWAY_TOKEN in Secrets");
|
| 51 |
-
process.exit(0);
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
// ----------------------------------------------------------------
|
| 60 |
-
// Provider API key detection
|
| 61 |
-
// ----------------------------------------------------------------
|
| 62 |
-
var EXCLUDE_PREFIXES = [
|
| 63 |
-
"OPENCLAW_", "SPACE_", "SYSTEM_", "HF_",
|
| 64 |
-
"NODE_", "PATH", "HOME", "USER", "PWD", "LANG", "LC_",
|
| 65 |
-
"npm_", "HOSTNAME", "SHELL", "TERM", "SHLVL"
|
| 66 |
-
];
|
| 67 |
-
var INCLUDE_SUFFIXES = [
|
| 68 |
-
"_API_KEY", "_SECRET_KEY", "_ACCESS_TOKEN",
|
| 69 |
-
"_BOT_TOKEN", "_AUTH_TOKEN", "_APP_KEY"
|
| 70 |
-
];
|
| 71 |
-
|
| 72 |
-
function isProviderKey(k) {
|
| 73 |
-
var i;
|
| 74 |
-
for (i = 0; i < EXCLUDE_PREFIXES.length; i++) {
|
| 75 |
-
if (k.indexOf(EXCLUDE_PREFIXES[i]) === 0) return false;
|
| 76 |
}
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
-
|
| 85 |
-
return isProviderKey(k) && envStr(k);
|
| 86 |
-
}).sort();
|
| 87 |
-
|
| 88 |
-
log("Provider keys (" + providerKeys.length + "): " + providerKeys.join(", "));
|
| 89 |
-
|
| 90 |
-
// ----------------------------------------------------------------
|
| 91 |
-
// Trusted proxies (HF internal + standard RFC1918)
|
| 92 |
-
// ----------------------------------------------------------------
|
| 93 |
-
var envProxies = parseList(envStr("OPENCLAW_GATEWAY_TRUSTED_PROXIES"));
|
| 94 |
-
var trustedProxies = envProxies.length > 0 ? envProxies : [
|
| 95 |
-
"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16",
|
| 96 |
-
"10.16.0.0/12", "10.20.0.0/12",
|
| 97 |
-
"10.16.4.123", "10.16.7.92", "10.16.18.232",
|
| 98 |
-
"10.16.34.155", "10.16.43.133", "10.16.1.206",
|
| 99 |
-
"10.16.37.110", "10.16.43.246",
|
| 100 |
-
"10.20.1.9", "10.20.1.222", "10.20.26.157", "10.20.31.87",
|
| 101 |
-
"10.20.0.1", "172.17.0.1", "127.0.0.1"
|
| 102 |
-
];
|
| 103 |
-
|
| 104 |
-
// ----------------------------------------------------------------
|
| 105 |
-
// Provider -> env var mapping
|
| 106 |
-
// ----------------------------------------------------------------
|
| 107 |
-
var PROVIDER_KEY_MAP = {
|
| 108 |
-
"openrouter": "OPENROUTER_API_KEY",
|
| 109 |
-
"siliconflow": "SILICONFLOW_API_KEY",
|
| 110 |
-
"deepseek": "DEEPSEEK_API_KEY",
|
| 111 |
-
"groq": "GROQ_API_KEY",
|
| 112 |
-
"google": "GOOGLE_API_KEY",
|
| 113 |
-
"gemini": "GEMINI_API_KEY",
|
| 114 |
-
"minimax": "MINIMAX_API_KEY",
|
| 115 |
-
"zai": "ZAI_API_KEY",
|
| 116 |
-
"github": "GITHUB_MODELS_API_KEY",
|
| 117 |
-
"github-copilot": "GITHUB_MODELS_API_KEY",
|
| 118 |
-
"openai": "OPENAI_API_KEY",
|
| 119 |
-
"anthropic": "ANTHROPIC_API_KEY"
|
| 120 |
-
};
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if (!val) {
|
| 130 |
-
// fuzzy match: find any provider key whose name contains the provider name
|
| 131 |
-
var found = providerKeys.find(function(k) {
|
| 132 |
-
return k.toLowerCase().indexOf(lower) >= 0;
|
| 133 |
-
});
|
| 134 |
-
if (found) val = envStr(found);
|
| 135 |
-
}
|
| 136 |
-
if (val) {
|
| 137 |
-
providers[name].apiKey = val;
|
| 138 |
-
log("Provider key set: " + name + " <- " + (envKey || "fuzzy"));
|
| 139 |
-
} else if (providers[name].apiKey === "WILL_BE_SET_BY_ENV") {
|
| 140 |
-
delete providers[name].apiKey;
|
| 141 |
-
log("Provider key missing (skipped): " + name);
|
| 142 |
-
}
|
| 143 |
-
});
|
| 144 |
}
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
//
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
var buf = "";
|
| 162 |
-
res.on("data", function(c) { buf += c; });
|
| 163 |
-
res.on("end", function() {
|
| 164 |
-
try { resolve(JSON.parse(buf)); } catch(e) { resolve(null); }
|
| 165 |
-
});
|
| 166 |
-
});
|
| 167 |
-
req.on("error", function() { resolve(null); });
|
| 168 |
-
req.setTimeout(10000, function() { req.destroy(); resolve(null); });
|
| 169 |
-
req.write(data);
|
| 170 |
-
req.end();
|
| 171 |
-
});
|
| 172 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
if (info && info.ok && info.result && info.result.url === targetUrl) {
|
| 184 |
-
log("Telegram: webhook already set -> " + targetUrl);
|
| 185 |
-
return;
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
var r = await tgRequest(token, "setWebhook", {
|
| 189 |
-
url: targetUrl, drop_pending_updates: true, max_connections: 10
|
| 190 |
-
});
|
| 191 |
-
if (r && r.ok) {
|
| 192 |
-
log("Telegram: webhook registered -> " + targetUrl);
|
| 193 |
-
} else {
|
| 194 |
-
var desc = r && r.description ? r.description : "unknown error";
|
| 195 |
-
log("Telegram: webhook failed (" + desc + ")");
|
| 196 |
-
log("Telegram: set manually at:");
|
| 197 |
-
log(" https://api.telegram.org/bot" + token + "/setWebhook?url=" + targetUrl + "&drop_pending_updates=true");
|
| 198 |
-
}
|
| 199 |
}
|
| 200 |
-
|
| 201 |
-
//
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
function seedWorkspace() {
|
| 205 |
-
var soul = path.join(WORKSPACE, "SOUL.md");
|
| 206 |
-
if (!fs.existsSync(soul)) {
|
| 207 |
-
fs.writeFileSync(soul, [
|
| 208 |
-
"# Soul",
|
| 209 |
-
"",
|
| 210 |
-
"You are a helpful, warm, concise AI assistant.",
|
| 211 |
-
"",
|
| 212 |
-
"## Language",
|
| 213 |
-
"",
|
| 214 |
-
"Default language: Simplified Chinese.",
|
| 215 |
-
"Always reply in Chinese unless the user writes in another language first.",
|
| 216 |
-
"",
|
| 217 |
-
"## Tone",
|
| 218 |
-
"",
|
| 219 |
-
"- Natural and friendly, not overly formal",
|
| 220 |
-
"- Concise and to the point"
|
| 221 |
-
].join("\n") + "\n", "utf-8");
|
| 222 |
-
log("Seeded SOUL.md");
|
| 223 |
-
}
|
| 224 |
-
var mem = path.join(WORKSPACE, "MEMORY.md");
|
| 225 |
-
if (!fs.existsSync(mem)) {
|
| 226 |
-
fs.writeFileSync(mem, [
|
| 227 |
-
"# Long-term Memory",
|
| 228 |
-
"",
|
| 229 |
-
"<!-- OpenClaw writes important facts here. -->"
|
| 230 |
-
].join("\n") + "\n", "utf-8");
|
| 231 |
-
log("Seeded MEMORY.md");
|
| 232 |
-
}
|
| 233 |
}
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
//
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
// Control UI flags required for HF Spaces iframe embedding
|
| 246 |
-
config.gateway.controlUi = config.gateway.controlUi || {};
|
| 247 |
-
config.gateway.controlUi.allowInsecureAuth = true;
|
| 248 |
-
config.gateway.controlUi.allowedOrigins = ["*"];
|
| 249 |
-
config.gateway.controlUi.dangerouslyDisableDeviceAuth = true;
|
| 250 |
-
config.gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback = true;
|
| 251 |
-
|
| 252 |
-
// Always refresh trusted proxies (HF pod IPs can change)
|
| 253 |
-
config.gateway.trustedProxies = trustedProxies;
|
| 254 |
-
|
| 255 |
-
// Agents
|
| 256 |
-
config.agents = config.agents || {};
|
| 257 |
-
config.agents.defaults = config.agents.defaults || {};
|
| 258 |
-
config.agents.defaults.workspace = WORKSPACE;
|
| 259 |
-
if (!config.agents.defaults.model ||
|
| 260 |
-
config.agents.defaults.model === "WILL_BE_SET_BY_ENV") {
|
| 261 |
-
config.agents.defaults.model = defaultModel;
|
| 262 |
-
}
|
| 263 |
-
|
| 264 |
-
// Env vars (all provider keys available inside agent tools)
|
| 265 |
-
config.env = config.env || {};
|
| 266 |
-
config.env.vars = {};
|
| 267 |
-
providerKeys.forEach(function(k) { config.env.vars[k] = envStr(k); });
|
| 268 |
-
|
| 269 |
-
// Model provider API keys
|
| 270 |
-
patchProviderKeys(config);
|
| 271 |
-
|
| 272 |
-
// Telegram channel
|
| 273 |
-
if (tgToken) {
|
| 274 |
-
config.channels = config.channels || {};
|
| 275 |
-
config.channels.telegram = config.channels.telegram || {};
|
| 276 |
-
config.channels.telegram.enabled = true;
|
| 277 |
-
config.channels.telegram.accounts = config.channels.telegram.accounts || {};
|
| 278 |
-
config.channels.telegram.accounts.main = {
|
| 279 |
-
botToken: tgToken,
|
| 280 |
-
apiRoot: "https://api.telegram.org"
|
| 281 |
-
};
|
| 282 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
}
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
(
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
var tgToken = envStr("TELEGRAM_BOT_TOKEN");
|
| 301 |
-
if (tgToken) {
|
| 302 |
-
await setupWebhook(tgToken);
|
| 303 |
-
} else {
|
| 304 |
-
log("Telegram: disabled (no TELEGRAM_BOT_TOKEN)");
|
| 305 |
-
}
|
| 306 |
-
|
| 307 |
-
// Load base config
|
| 308 |
-
// Standard OpenClaw pattern: the bucket config is the single source
|
| 309 |
-
// of truth. It contains everything: models, channels, telegram bindings,
|
| 310 |
-
// devices, credentials. It persists across all restarts unchanged.
|
| 311 |
-
// The template (uploaded openclaw.json) is only used on first boot
|
| 312 |
-
// when no bucket config exists yet.
|
| 313 |
-
//
|
| 314 |
-
// To update models/providers after first boot: use the Control UI.
|
| 315 |
-
// Changes are saved directly to the bucket config automatically.
|
| 316 |
-
var config = null;
|
| 317 |
-
var mode = "";
|
| 318 |
-
|
| 319 |
-
// Primary: bucket config (preserves ALL user settings across restarts)
|
| 320 |
-
if (fs.existsSync(CONFIG_PATH)) {
|
| 321 |
-
try {
|
| 322 |
-
config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8").trim());
|
| 323 |
-
mode = "bucket (full config preserved)";
|
| 324 |
-
} catch(e) {
|
| 325 |
-
log("Bucket config unreadable (" + e.message + ") - falling back to template");
|
| 326 |
-
}
|
| 327 |
-
}
|
| 328 |
-
|
| 329 |
-
// First boot only: use template to seed the bucket
|
| 330 |
-
if (!config && fs.existsSync(TEMPLATE)) {
|
| 331 |
-
try {
|
| 332 |
-
config = JSON.parse(fs.readFileSync(TEMPLATE, "utf-8").trim());
|
| 333 |
-
mode = "template (first boot - seeding bucket)";
|
| 334 |
-
} catch(e) {
|
| 335 |
-
log("Template unreadable (" + e.message + ")");
|
| 336 |
-
}
|
| 337 |
-
}
|
| 338 |
-
|
| 339 |
-
if (!config) {
|
| 340 |
-
config = { gateway: {}, agents: { defaults: {} }, env: { vars: {} } };
|
| 341 |
-
mode = "fresh (no template found)";
|
| 342 |
-
}
|
| 343 |
-
|
| 344 |
-
// Apply env patches
|
| 345 |
-
config = applyEnvPatches(config, tgToken);
|
| 346 |
-
|
| 347 |
-
// Atomic write: backup then overwrite
|
| 348 |
-
if (fs.existsSync(CONFIG_PATH)) {
|
| 349 |
-
fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
|
| 350 |
-
}
|
| 351 |
-
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
|
| 352 |
-
|
| 353 |
-
// Summary
|
| 354 |
-
var provCount = config.models && config.models.providers
|
| 355 |
-
? Object.keys(config.models.providers).length : 0;
|
| 356 |
-
var modelCount = config.models && config.models.providers
|
| 357 |
-
? Object.values(config.models.providers).reduce(function(n, p) {
|
| 358 |
-
return n + (p.models ? p.models.length : 0);
|
| 359 |
-
}, 0) : 0;
|
| 360 |
-
|
| 361 |
-
log("Done. Mode: " + mode);
|
| 362 |
-
log(" auth = " + (gatewayToken ? "token" : "password"));
|
| 363 |
-
log(" model = " + (config.agents.defaults.model || defaultModel));
|
| 364 |
-
log(" workspace = " + WORKSPACE);
|
| 365 |
-
log(" proxies = " + trustedProxies.length);
|
| 366 |
-
log(" env.vars = " + providerKeys.length);
|
| 367 |
-
log(" providers = " + provCount + " (" + modelCount + " models)");
|
| 368 |
-
log(" storage = bucket at /data (100 GB persistent)");
|
| 369 |
-
|
| 370 |
-
})().catch(function(e) {
|
| 371 |
-
err("Fatal: " + e.message);
|
| 372 |
-
process.exit(0);
|
| 373 |
-
});
|
|
|
|
| 1 |
+
import fs from "node:fs";
|
| 2 |
import path from "node:path";
|
|
|
|
| 3 |
|
| 4 |
// ============================================================
|
| 5 |
+
// 生成运行配置 (实时状态在本地磁盘 /home/user)
|
| 6 |
+
// 基础配置优先级:
|
| 7 |
+
// 1. /data/.openclaw/openclaw.json (bucket 里的旧配置, 只读, 已被 2026.9.7 doctor 验证过)
|
| 8 |
+
// 2. /app/openclaw-template.json
|
| 9 |
+
// 3. 最小空配置
|
| 10 |
+
// 每次启动都会用 Secrets 覆盖: 网关令牌 / 模型 API Key / 可选 Telegram 代理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
// ============================================================
|
| 12 |
+
var HOME = process.env.OPENCLAW_HOME || "/home/user";
|
| 13 |
+
var STATE_DIR = path.join(HOME, ".openclaw");
|
|
|
|
| 14 |
var CONFIG_PATH = path.join(STATE_DIR, "openclaw.json");
|
| 15 |
+
var TEMPLATE = "/app/openclaw-template.json";
|
| 16 |
+
var LEGACY = process.env.LEGACY_CONFIG || "/data/.openclaw/openclaw.json";
|
| 17 |
+
var LEGACY_WS = "/data/.openclaw/workspace";
|
| 18 |
+
|
| 19 |
+
function log(m) { console.log("[setup] " + m); }
|
| 20 |
+
function err(m) { console.error("[setup] ERROR: " + m); }
|
| 21 |
+
function envStr(k) { return (process.env[k] || "").trim(); }
|
| 22 |
+
function loadJson(p) {
|
| 23 |
+
try { return JSON.parse(fs.readFileSync(p, "utf-8").trim()); } catch (e) { return null; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
+
function writableDir(d) {
|
| 26 |
+
try { fs.mkdirSync(d, { recursive: true }); fs.accessSync(d, fs.constants.W_OK); return true; }
|
| 27 |
+
catch (e) { return false; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
+
function stripKey(o, k) {
|
| 30 |
+
if (Array.isArray(o)) { o.forEach(function (x) { stripKey(x, k); }); }
|
| 31 |
+
else if (o && typeof o === "object") {
|
| 32 |
+
delete o[k];
|
| 33 |
+
Object.keys(o).forEach(function (kk) { stripKey(o[kk], k); });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
+
}
|
| 36 |
+
function mapStrings(o, from, to) {
|
| 37 |
+
if (Array.isArray(o)) { o.forEach(function (x, i) { if (typeof x === "string") { if (x.indexOf(from) === 0) o[i] = to + x.slice(from.length); } else mapStrings(x, from, to); }); }
|
| 38 |
+
else if (o && typeof o === "object") {
|
| 39 |
+
Object.keys(o).forEach(function (k) {
|
| 40 |
+
var v = o[k];
|
| 41 |
+
if (typeof v === "string") { if (v.indexOf(from) === 0) o[k] = to + v.slice(from.length); }
|
| 42 |
+
else mapStrings(v, from, to);
|
| 43 |
+
});
|
| 44 |
}
|
| 45 |
+
}
|
| 46 |
+
function collectStrings(o, prefix, out) {
|
| 47 |
+
if (Array.isArray(o)) o.forEach(function (x) { collectStrings(x, prefix, out); });
|
| 48 |
+
else if (o && typeof o === "object") Object.keys(o).forEach(function (k) { collectStrings(o[k], prefix, out); });
|
| 49 |
+
else if (typeof o === "string" && o.indexOf(prefix) === 0) out.push(o);
|
| 50 |
}
|
| 51 |
|
| 52 |
+
log("Starting... HOME=" + HOME);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
var gatewayToken = envStr("OPENCLAW_GATEWAY_TOKEN");
|
| 55 |
+
var config = null, mode = "";
|
| 56 |
+
var legacy = loadJson(LEGACY);
|
| 57 |
+
if (legacy) { config = legacy; mode = "legacy bucket config (read-only import)"; }
|
| 58 |
+
else {
|
| 59 |
+
var tpl = loadJson(TEMPLATE);
|
| 60 |
+
if (tpl) { config = tpl; mode = "template"; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
+
if (!config) { config = { gateway: {}, agents: { defaults: {} }, env: { vars: {} } }; mode = "fresh"; }
|
| 63 |
+
|
| 64 |
+
// ── gateway ──
|
| 65 |
+
config.gateway = config.gateway || {};
|
| 66 |
+
config.gateway.mode = "local";
|
| 67 |
+
config.gateway.port = 7860;
|
| 68 |
+
config.gateway.bind = "lan";
|
| 69 |
+
if (gatewayToken) {
|
| 70 |
+
config.gateway.auth = { mode: "token", token: gatewayToken };
|
| 71 |
+
if (!/^[A-Za-z0-9]{32,}$/.test(gatewayToken)) log("WARN: OPENCLAW_GATEWAY_TOKEN should be 32+ letters/digits");
|
| 72 |
+
} else if (config.gateway.auth && (config.gateway.auth.token || config.gateway.auth.password) &&
|
| 73 |
+
config.gateway.auth.token !== "WILL_BE_SET_BY_ENV") {
|
| 74 |
+
log("WARN: OPENCLAW_GATEWAY_TOKEN not set - keeping the token from the imported config (rotate it!)");
|
| 75 |
+
} else {
|
| 76 |
+
err("FATAL: set OPENCLAW_GATEWAY_TOKEN in Space Secrets");
|
| 77 |
+
process.exit(0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
+
config.gateway.controlUi = config.gateway.controlUi || {};
|
| 80 |
+
config.gateway.controlUi.allowInsecureAuth = true;
|
| 81 |
+
config.gateway.controlUi.allowedOrigins = ["*"];
|
| 82 |
+
config.gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback = true;
|
| 83 |
+
delete config.gateway.controlUi.dangerouslyDisableDeviceAuth; // 新版已废弃
|
| 84 |
+
var host = envStr("SPACE_HOST");
|
| 85 |
+
if (host && !config.gateway.publicOrigin) config.gateway.publicOrigin = "https://" + host;
|
| 86 |
+
config.gateway.trustedProxies = [
|
| 87 |
+
"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.1"
|
| 88 |
+
];
|
| 89 |
|
| 90 |
+
// ── agents: 去掉 agentDir, 工作区(纯文件)可留在 bucket, 不可写则落本地 ──
|
| 91 |
+
config.agents = config.agents || {};
|
| 92 |
+
config.agents.defaults = config.agents.defaults || {};
|
| 93 |
+
stripKey(config.agents, "agentDir");
|
| 94 |
+
var wsOnBucket = envStr("WORKSPACE_ON_BUCKET") !== "0" && writableDir(LEGACY_WS);
|
| 95 |
+
var localWs = path.join(STATE_DIR, "workspace");
|
| 96 |
+
if (!wsOnBucket) mapStrings(config.agents, LEGACY_WS, localWs);
|
| 97 |
+
if (!config.agents.defaults.workspace || config.agents.defaults.workspace === "WILL_BE_SET_BY_ENV") {
|
| 98 |
+
config.agents.defaults.workspace = wsOnBucket ? LEGACY_WS : localWs;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
+
var wsList = []; collectStrings(config.agents, wsOnBucket ? LEGACY_WS : localWs, wsList);
|
| 101 |
+
wsList.forEach(function (d) { try { fs.mkdirSync(d, { recursive: true }); } catch (e) { /* ignore */ } });
|
| 102 |
+
if (!config.agents.defaults.model || config.agents.defaults.model === "WILL_BE_SET_BY_ENV") {
|
| 103 |
+
config.agents.defaults.model = envStr("OPENCLAW_HF_DEFAULT_MODEL") || "groq/llama-3.3-70b-versatile";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
+
log("workspace on " + (wsOnBucket ? "bucket (/data)" : "local disk") + ", dirs: " + wsList.length);
|
| 106 |
+
|
| 107 |
+
// seed SOUL/MEMORY if missing
|
| 108 |
+
var mainEntry = config.agents.entries && config.agents.entries.main;
|
| 109 |
+
var mainWs = (mainEntry && mainEntry.workspace) || config.agents.defaults.workspace;
|
| 110 |
+
try {
|
| 111 |
+
fs.mkdirSync(path.join(mainWs, "memory"), { recursive: true });
|
| 112 |
+
var soul = path.join(mainWs, "SOUL.md");
|
| 113 |
+
if (!fs.existsSync(soul)) {
|
| 114 |
+
fs.writeFileSync(soul, "# Soul\n\nYou are a helpful, warm, concise AI assistant.\nDefault language: Simplified Chinese.\n", "utf-8");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
}
|
| 116 |
+
var mem = path.join(mainWs, "MEMORY.md");
|
| 117 |
+
if (!fs.existsSync(mem)) fs.writeFileSync(mem, "# Long-term Memory\n", "utf-8");
|
| 118 |
+
} catch (e) { log("seed skipped: " + e.message); }
|
| 119 |
+
|
| 120 |
+
// ── 模型 Key: 只在 Secrets 里有值时才覆盖 ──
|
| 121 |
+
var MAP = {
|
| 122 |
+
openrouter: "OPENROUTER_API_KEY", siliconflow: "SILICONFLOW_API_KEY", deepseek: "DEEPSEEK_API_KEY",
|
| 123 |
+
groq: "GROQ_API_KEY", google: "GOOGLE_API_KEY", gemini: "GEMINI_API_KEY", minimax: "MINIMAX_API_KEY",
|
| 124 |
+
zai: "ZAI_API_KEY", "github-copilot": "GITHUB_MODELS_API_KEY", github: "GITHUB_MODELS_API_KEY",
|
| 125 |
+
openai: "OPENAI_API_KEY", anthropic: "ANTHROPIC_API_KEY"
|
| 126 |
+
};
|
| 127 |
+
var providers = (config.models && config.models.providers) || {};
|
| 128 |
+
Object.keys(providers).forEach(function (name) {
|
| 129 |
+
var ek = MAP[name.toLowerCase()];
|
| 130 |
+
var v = ek ? envStr(ek) : "";
|
| 131 |
+
if (v) { providers[name].apiKey = v; log("Provider key from Secrets: " + name); }
|
| 132 |
+
else if (providers[name].apiKey === "WILL_BE_SET_BY_ENV") { delete providers[name].apiKey; log("Provider key missing: " + name); }
|
| 133 |
+
});
|
| 134 |
+
config.env = config.env || {};
|
| 135 |
+
config.env.vars = config.env.vars || {};
|
| 136 |
+
Object.keys(process.env).forEach(function (k) {
|
| 137 |
+
if (/^(OPENCLAW_|SPACE_|SYSTEM_|HF_|NODE_|npm_)/.test(k)) return;
|
| 138 |
+
if (/(_API_KEY|_SECRET_KEY|_ACCESS_TOKEN|_AUTH_TOKEN|_APP_KEY)$/.test(k) && envStr(k)) config.env.vars[k] = envStr(k);
|
| 139 |
+
});
|
| 140 |
|
| 141 |
+
// ── Telegram ──
|
| 142 |
+
var tg = config.channels && config.channels.telegram;
|
| 143 |
+
var tgToken = envStr("TELEGRAM_BOT_TOKEN");
|
| 144 |
+
if (!tg && tgToken) {
|
| 145 |
+
config.channels = config.channels || {};
|
| 146 |
+
tg = config.channels.telegram = { enabled: true, accounts: { main: { botToken: tgToken, apiRoot: "https://api.telegram.org" } } };
|
| 147 |
}
|
| 148 |
+
var accNames = tg && tg.accounts ? Object.keys(tg.accounts) : [];
|
| 149 |
+
var proxy = envStr("TELEGRAM_PROXY");
|
| 150 |
+
if (tg && proxy) {
|
| 151 |
+
accNames.forEach(function (n) { tg.accounts[n].proxy = proxy; });
|
| 152 |
+
log("Telegram proxy set on " + accNames.length + " account(s)");
|
| 153 |
+
}
|
| 154 |
+
var apiRoot = envStr("TELEGRAM_API_ROOT");
|
| 155 |
+
if (tg && apiRoot) accNames.forEach(function (n) { tg.accounts[n].apiRoot = apiRoot; });
|
| 156 |
+
log("Telegram accounts: " + (accNames.join(", ") || "none"));
|
| 157 |
+
|
| 158 |
+
// ── 写出 ──
|
| 159 |
+
fs.mkdirSync(STATE_DIR, { recursive: true });
|
| 160 |
+
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { encoding: "utf-8", mode: 0o600 });
|
| 161 |
+
log("Done. base = " + mode + " | auth = token(" + (gatewayToken ? "env" : "imported") + ") | providers = " + Object.keys(providers).length);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|