diff --git a/lib/client.js b/lib/client.js index 339cb3c..a5d6993 100644 --- a/lib/client.js +++ b/lib/client.js @@ -419,9 +419,11 @@ class ClawClient { const raw = readFileSync(configFile, 'utf8'); const config = JSON.parse(raw); const newOrigin = `https://${targetId}.claw.cutos.ai`; - const re = /https:\/\/[^"'\s]+\.claw\.cutos\.ai/g; + const newOriginWss = `wss://${targetId}.claw.cutos.ai`; + const reHttps = /https:\/\/[^"'\s]+\.claw\.cutos\.ai/g; + const reWss = /wss:\/\/[^"'\s]+\.claw\.cutos\.ai/g; - /** 与原 YAML 全文替换等价:遍历 JSON 内所有字符串并替换匹配的 origin */ + /** 遍历 JSON 内字符串,替换已存在的 claw 子域 URL(兼容旧 YAML 全文替换习惯) */ const replaceOriginStrings = (node) => { let changed = false; if (typeof node === 'string') { @@ -431,7 +433,7 @@ class ClawClient { for (let i = 0; i < node.length; i++) { const v = node[i]; if (typeof v === 'string') { - const next = v.replace(re, newOrigin); + const next = v.replace(reHttps, newOrigin).replace(reWss, newOriginWss); if (next !== v) { node[i] = next; changed = true; @@ -446,7 +448,7 @@ class ClawClient { for (const k of Object.keys(node)) { const v = node[k]; if (typeof v === 'string') { - const next = v.replace(re, newOrigin); + const next = v.replace(reHttps, newOrigin).replace(reWss, newOriginWss); if (next !== v) { node[k] = next; changed = true; @@ -460,7 +462,18 @@ class ClawClient { return false; }; - if (!replaceOriginStrings(config)) { + let changed = replaceOriginStrings(config); + + // openclaw.json 可能没有旧 URL 可替换(空配置或仅 token);必须写入 gateway.origin + if (!config.gateway || typeof config.gateway !== 'object') { + config.gateway = {}; + } + if (config.gateway.origin !== newOrigin) { + config.gateway.origin = newOrigin; + changed = true; + } + + if (!changed) { log.info('clawd', `openclaw origin 已是 ${newOrigin},无需变更`); return; }