From 684e9728dd7dd6c9d2e44fbd178b4f275c1dedba Mon Sep 17 00:00:00 2001 From: stswangzhiping <59632378+stswangzhiping@users.noreply.github.com> Date: Tue, 5 May 2026 20:49:04 +0800 Subject: [PATCH] feat: write searxng config via .env + openclaw.json on activation/reconnect; bump to 1.3.4 Co-authored-by: Cursor --- lib/openclaw-provider.js | 69 ++++++++++++++++++++++++++-------------- package.json | 2 +- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/lib/openclaw-provider.js b/lib/openclaw-provider.js index 8d9ba59..c0de8d7 100644 --- a/lib/openclaw-provider.js +++ b/lib/openclaw-provider.js @@ -125,14 +125,6 @@ function removeProviderByName(providerId) { delete config.auth.profiles[`${providerId}:default`]; } - // web search: 解绑时精确删除 searxng 相关配置 - if (config.plugins?.entries?.searxng) { - delete config.plugins.entries.searxng; - } - if (config.tools?.web?.search?.provider === 'searxng') { - delete config.tools.web.search; - } - writeJsonFile(configFile, config); log.info('openclaw-provider', `provider 已移除: ${providerId}`); } @@ -155,28 +147,59 @@ function removeProviderFromConfig(config, providerId) { const WEB_SEARCH_BASE_URL = 'https://web-search.cutos.ai/'; /** - * 检查并补全 searxng web search 配置,返回 true 表示有修改。 - * 激活和重连时均调用,确保配置存在且正确。 + * 写入 .env 中的 KEY=value,已存在相同行则跳过,存在不同值则替换,不存在则追加。 */ -function ensureWebSearchConfig(config) { +function ensureEnvVar(envFile, key, value) { + const line = `${key}="${value}"`; + let content = ''; + try { content = fs.readFileSync(envFile, 'utf8'); } catch (_) {} + + const re = new RegExp(`^${key}=.*`, 'm'); + if (re.test(content)) { + const updated = content.replace(re, line); + if (updated !== content) { + fs.writeFileSync(envFile, updated, 'utf8'); + } + } else { + const sep = content.length && !content.endsWith('\n') ? '\n' : ''; + fs.writeFileSync(envFile, content + sep + line + '\n', 'utf8'); + } +} + +/** + * 检查并补全 searxng web search 配置(openclaw.json),返回 true 表示有修改。 + */ +function ensureWebSearchConfig(configFile, config) { let dirty = false; - const expectedSearch = { provider: 'searxng' }; - const expectedSearxng = { config: { webSearch: { baseUrl: WEB_SEARCH_BASE_URL } } }; - const curSearch = config.tools?.web?.search; - const curSearxng = config.plugins?.entries?.searxng; + // .env: SEARXNG_BASE_URL + try { + const envFile = path.join(path.dirname(configFile), '.env'); + ensureEnvVar(envFile, 'SEARXNG_BASE_URL', WEB_SEARCH_BASE_URL); + } catch (e) { + log.warn('openclaw-provider', `ensureEnvVar failed: ${e.message}`); + } - if (!curSearch || curSearch.provider !== 'searxng') { + // tools.web.search / tools.web.fetch + const curSearch = config.tools?.web?.search; + const curFetch = config.tools?.web?.fetch; + if (!curSearch?.openaiCodex || curFetch?.enabled !== true) { if (!config.tools) config.tools = {}; if (!config.tools.web) config.tools.web = {}; - config.tools.web.search = expectedSearch; + config.tools.web.search = { openaiCodex: {} }; + config.tools.web.fetch = { enabled: true }; dirty = true; } - if (!curSearxng || curSearxng.config?.webSearch?.baseUrl !== WEB_SEARCH_BASE_URL) { + // plugins.entries.searxng + const cur = config.plugins?.entries?.searxng; + if (!cur || cur.enabled !== true || cur.config?.webSearch?.baseUrl !== WEB_SEARCH_BASE_URL) { if (!config.plugins) config.plugins = {}; if (!config.plugins.entries) config.plugins.entries = {}; - config.plugins.entries.searxng = expectedSearxng; + config.plugins.entries.searxng = { + config: { webSearch: { baseUrl: WEB_SEARCH_BASE_URL } }, + enabled: true, + }; dirty = true; } @@ -226,7 +249,7 @@ function addProviderSync(configFile, providerId, baseUrl, apiKey, models, defaul mode: 'api_key', }; - ensureWebSearchConfig(config); + ensureWebSearchConfig(configFile, config); writeJsonFile(configFile, config); log.info('openclaw-provider', `provider 已写入: ${providerId}(${models.length} 个模型)`); @@ -364,12 +387,12 @@ function refreshModelsIfChanged(onDone) { const newMd5 = computeModelsMd5(newModels); if (currentMd5 === newMd5) { - // 模型未变,但仍检查 searxng 配置是否缺失或过期 + // 模型未变,但仍检查 web search 配置是否缺失或过期 try { const cfg = readJsonFile(configFile); - if (ensureWebSearchConfig(cfg)) { + if (ensureWebSearchConfig(configFile, cfg)) { writeJsonFile(configFile, cfg); - log.info('openclaw-provider', 'searxng web search config repaired'); + log.info('openclaw-provider', 'web search config repaired'); } else { log.info('openclaw-provider', `模型列表未变化(${newModels.length} 个),跳过更新`); } diff --git a/package.json b/package.json index 8621610..c3b7e34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clawd", - "version": "1.3.3", + "version": "1.3.4", "description": "Claw Box daemon - connects local Linux box to claw.cutos.ai via WebSocket", "main": "lib/client.js", "bin": {