8900 commited on
Commit
3752d78
·
verified ·
1 Parent(s): b2fae5f

Update setup-hf-config.mjs

Browse files
Files changed (1) hide show
  1. setup-hf-config.mjs +95 -108
setup-hf-config.mjs CHANGED
@@ -9,140 +9,127 @@ var CONFIG_PATH = path.join(STATE_DIR, "openclaw.json");
9
  console.log("[setup] Starting… HOME=" + HOME);
10
 
11
  function parseList(val) {
12
- if (!val || !val.trim()) return [];
13
- return val.split(",").map(function(s) { return s.trim(); }).filter(Boolean);
14
  }
15
 
16
  var gatewayToken = (process.env.OPENCLAW_GATEWAY_TOKEN || "").trim();
17
  var gatewayPassword = (process.env.OPENCLAW_GATEWAY_PASSWORD || "").trim();
18
 
19
  if (!gatewayToken && !gatewayPassword) {
20
- console.error("[setup] ERROR: No OPENCLAW_GATEWAY_TOKEN or OPENCLAW_GATEWAY_PASSWORD");
21
- process.exit(0);
22
  }
23
 
24
- // FIX 1: default model uses correct google/ prefix
25
  var defaultModel = (process.env.OPENCLAW_HF_DEFAULT_MODEL || "").trim() ||
26
- "google/gemini-2.0-flash";
27
 
28
- // FIX 2: read GOOGLE_API_KEY first, GEMINI_API_KEY as alias
29
- var geminiKey = (process.env.GOOGLE_API_KEY || process.env.GEMINI_API_KEY || "").trim();
30
- var groqKey = (process.env.GROQ_API_KEY || "").trim();
31
- var anthropicKey = (process.env.ANTHROPIC_API_KEY || "").trim();
32
- var openaiKey = (process.env.OPENAI_API_KEY || "").trim();
33
- var openrouterKey = (process.env.OPENROUTER_API_KEY || "").trim();
34
- var telegramToken = (process.env.TELEGRAM_BOT_TOKEN || "").trim();
 
35
 
36
  var envProxies = parseList(process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES);
37
  var trustedProxies = envProxies.length > 0 ? envProxies : [
38
- "10.16.4.123", "10.16.7.92", "10.16.18.232",
39
- "10.16.34.155","10.16.43.133","10.16.1.206",
40
- "10.20.1.9", "10.20.1.222",
41
- "10.20.26.157","10.20.31.87",
42
- "10.20.0.1", "172.17.0.1"
43
  ];
44
 
45
  var config = {
46
- gateway: {
47
- auth: gatewayToken
48
- ? { mode: "token", token: gatewayToken }
49
- : { mode: "password", password: gatewayPassword },
50
- controlUi: {
51
- dangerouslyDisableDeviceAuth: true,
52
- allowInsecureAuth: true,
53
- dangerouslyAllowHostHeaderOriginFallback: true,
54
- allowedOrigins: ["*"]
55
- },
56
- trustedProxies: trustedProxies
57
- },
58
- agents: {
59
- defaults: {
60
- model: defaultModel
61
- }
62
- },
63
- env: { vars: {} }
64
  };
65
 
66
- // write API keys - GOOGLE_API_KEY is the correct env var name OpenClaw reads
67
- if (geminiKey) {
68
- config.env.vars.GOOGLE_API_KEY = geminiKey;
69
- config.env.vars.GEMINI_API_KEY = geminiKey;
70
- }
71
- if (groqKey) { config.env.vars.GROQ_API_KEY = groqKey; }
72
- if (anthropicKey) { config.env.vars.ANTHROPIC_API_KEY = anthropicKey; }
73
- if (openaiKey) { config.env.vars.OPENAI_API_KEY = openaiKey; }
74
- if (openrouterKey) { config.env.vars.OPENROUTER_API_KEY = openrouterKey; }
75
 
76
- // Telegram - auto detect best node
77
  async function detectTelegramNode(nodes) {
78
- for (var i = 0; i < nodes.length; i++) {
79
- var node = nodes[i];
80
- try {
81
- await new Promise(function(resolve, reject) {
82
- var req = https.get(node + "/bot" + telegramToken + "/getMe", function(res) {
83
- if (res.statusCode === 200) resolve(true);
84
- else reject(new Error("Status " + res.statusCode));
85
- });
86
- req.on("error", reject);
87
- req.setTimeout(4000, function() { req.destroy(); reject(new Error("Timeout")); });
88
- });
89
- console.log("[setup] Telegram node OK: " + node);
90
- // FIX 3: correct regex, was //$/ (broken) now /\/$/ (correct)
91
- return node.replace(/\/$/, "");
92
- } catch (e) {
93
- console.log("[setup] Telegram node failed: " + node + " (" + e.message + ")");
94
- }
95
- }
96
- return null;
97
  }
98
 
99
  var telegramNodes = [
100
- "https://api.telegram.org",
101
- "https://tg-api.vercel.app",
102
- "https://telegram-bot-api.vercel.app"
103
  ];
104
 
105
  async function setupTelegram() {
106
- if (!telegramToken) {
107
- console.log("[setup] Telegram disabled (no token)");
108
- return;
109
- }
110
- var apiRoot = await detectTelegramNode(telegramNodes);
111
- var finalRoot = apiRoot || "https://api.telegram.org";
112
- config.channels = {
113
- telegram: {
114
- enabled: true,
115
- accounts: {
116
- main: { botToken: telegramToken, apiRoot: finalRoot }
117
- }
118
- }
119
- };
120
- console.log("[setup] Telegram enabled, apiRoot=" + finalRoot);
 
121
  }
122
 
123
  (async function main() {
124
- await setupTelegram();
125
-
126
- try {
127
- fs.mkdirSync(STATE_DIR, { recursive: true });
128
- if (fs.existsSync(CONFIG_PATH)) {
129
- fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
130
- }
131
- fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
132
- console.log("[setup] Config written -> " + CONFIG_PATH);
133
- } catch (e) {
134
- console.error("[setup] Write failed: " + e.message);
135
- process.exit(0);
136
- }
137
-
138
- console.log("[setup] auth=" + (gatewayToken ? "token" : "password"));
139
- console.log("[setup] model=" + defaultModel);
140
- console.log("[setup] gemini=" + !!geminiKey);
141
- console.log("[setup] groq=" + !!groqKey);
142
- console.log("[setup] anthropic=" + !!anthropicKey);
143
- console.log("[setup] openai=" + !!openaiKey);
144
- console.log("[setup] telegram=" + !!telegramToken);
145
- })().catch(function(err) {
146
- console.error("[setup] Fatal: " + err.message);
147
- process.exit(0);
148
- });
 
9
  console.log("[setup] Starting… HOME=" + HOME);
10
 
11
  function parseList(val) {
12
+ if (!val || !val.trim()) return [];
13
+ return val.split(",").map(function(s) { return s.trim(); }).filter(Boolean);
14
  }
15
 
16
  var gatewayToken = (process.env.OPENCLAW_GATEWAY_TOKEN || "").trim();
17
  var gatewayPassword = (process.env.OPENCLAW_GATEWAY_PASSWORD || "").trim();
18
 
19
  if (!gatewayToken && !gatewayPassword) {
20
+ console.error("[setup] ERROR: No OPENCLAW_GATEWAY_TOKEN or OPENCLAW_GATEWAY_PASSWORD");
21
+ process.exit(0);
22
  }
23
 
24
+ // 默认模型(仅默认值,不控制可用模型)
25
  var defaultModel = (process.env.OPENCLAW_HF_DEFAULT_MODEL || "").trim() ||
26
+ "google/gemini-2.0-flash";
27
 
28
+ // 👉 动态扫描所有 API Key / BOT Token,用于日志
29
+ var detectedKeys = Object.keys(process.env)
30
+ .filter(k => (k.endsWith("_API_KEY") || k.endsWith("_BOT_TOKEN")) && process.env[k]);
31
+
32
+ // 按字母顺序打印日志,更直观
33
+ detectedKeys.sort();
34
+ console.log("[setup] Detected environment keys:");
35
+ detectedKeys.forEach(k => console.log(" - " + k));
36
 
37
  var envProxies = parseList(process.env.OPENCLAW_GATEWAY_TRUSTED_PROXIES);
38
  var trustedProxies = envProxies.length > 0 ? envProxies : [
39
+ "10.16.4.123", "10.16.7.92", "10.16.18.232",
40
+ "10.16.34.155","10.16.43.133","10.16.1.206",
41
+ "10.20.1.9", "10.20.1.222",
42
+ "10.20.26.157","10.20.31.87",
43
+ "10.20.0.1", "172.17.0.1"
44
  ];
45
 
46
  var config = {
47
+ gateway: {
48
+ auth: gatewayToken
49
+ ? { mode: "token", token: gatewayToken }
50
+ : { mode: "password", password: gatewayPassword },
51
+ controlUi: {
52
+ dangerouslyDisableDeviceAuth: true,
53
+ allowInsecureAuth: true,
54
+ dangerouslyAllowHostHeaderOriginFallback: true,
55
+ allowedOrigins: ["*"]
56
+ },
57
+ trustedProxies: trustedProxies
58
+ },
59
+ agents: {
60
+ defaults: {
61
+ model: defaultModel // ✅ 只保留默认模型
62
+ }
63
+ },
64
+ env: { vars: {} } // ✅ 保留结构,但不写入 API Key
65
  };
66
 
67
+ // 删除:所有 API KEY 写入 config 的逻辑
68
+ // 👉 OpenClaw 会直接从环境变量读取(entrypoint 已经 export 了)
 
 
 
 
 
 
 
69
 
70
+ // ====================== Telegram ======================
71
  async function detectTelegramNode(nodes) {
72
+ for (var i = 0; i < nodes.length; i++) {
73
+ var node = nodes[i];
74
+ try {
75
+ await new Promise(function(resolve, reject) {
76
+ var req = https.get(node + "/bot" + (process.env.TELEGRAM_BOT_TOKEN || "") + "/getMe", function(res) {
77
+ if (res.statusCode === 200) resolve(true);
78
+ else reject(new Error("Status " + res.statusCode));
79
+ });
80
+ req.on("error", reject);
81
+ req.setTimeout(4000, function() { req.destroy(); reject(new Error("Timeout")); });
82
+ });
83
+ console.log("[setup] Telegram node OK: " + node);
84
+ return node.replace(/\/$/, "");
85
+ } catch (e) {
86
+ console.log("[setup] Telegram node failed: " + node + " (" + e.message + ")");
87
+ }
88
+ }
89
+ return null;
 
90
  }
91
 
92
  var telegramNodes = [
93
+ "https://api.telegram.org",
94
+ "https://tg-api.vercel.app",
95
+ "https://telegram-bot-api.vercel.app"
96
  ];
97
 
98
  async function setupTelegram() {
99
+ var telegramToken = process.env.TELEGRAM_BOT_TOKEN || "";
100
+ if (!telegramToken) {
101
+ console.log("[setup] Telegram disabled (no token)");
102
+ return;
103
+ }
104
+ var apiRoot = await detectTelegramNode(telegramNodes);
105
+ var finalRoot = apiRoot || "https://api.telegram.org";
106
+ config.channels = {
107
+ telegram: {
108
+ enabled: true,
109
+ accounts: {
110
+ main: { botToken: telegramToken, apiRoot: finalRoot }
111
+ }
112
+ }
113
+ };
114
+ console.log("[setup] Telegram enabled, apiRoot=" + finalRoot);
115
  }
116
 
117
  (async function main() {
118
+ await setupTelegram();
119
+
120
+ try {
121
+ fs.mkdirSync(STATE_DIR, { recursive: true });
122
+ if (fs.existsSync(CONFIG_PATH)) {
123
+ fs.copyFileSync(CONFIG_PATH, CONFIG_PATH + ".bak");
124
+ }
125
+ fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
126
+ console.log("[setup] Config written -> " + CONFIG_PATH);
127
+ } catch (e) {
128
+ console.error("[setup] Write failed: " + e.message);
129
+ process.exit(0);
130
+ }
131
+
132
+ console.log("[setup] auth=" + (gatewayToken ? "token" : "password"));
133
+ console.log("[setup] default model=" + defaultModel);
134
+ console.log("[setup] Detected keys count=" + detectedKeys.length);
135
+ })();