Skip to content

fix(byted-web-search): .env 回退路径不再硬编码 ~/.openclaw,新增 XDG 通用路径 - #267

Open
AbelWangYaBo wants to merge 2 commits into
bytedance:mainfrom
AbelWangYaBo:fix/web-search-env-path
Open

AbelWangYaBo wants to merge 2 commits into
bytedance:mainfrom
AbelWangYaBo:fix/web-search-env-path

Conversation

@AbelWangYaBo

Copy link
Copy Markdown

问题

byted-web-search 的 .env 回退路径硬编码为 OpenClaw 私有目录:

LEGACY_ENV_PATH = "/root/.openclaw/.env"
USER_ENV_PATH   = str(Path.home() / ".openclaw/.env")

配合 skill 根目录 .env(scripts/web_search.py:430),共三处。

对非 OpenClaw 用户而言:前两处无效;第三处位于受 skills CLI 管理的目录内,更新时可能被清除。
结果是没有任何一个「跨 Agent 且不被 update 覆盖」的配置位置可用。

影响场景

以守护进程方式启动的 Agent(如 launchd 拉起的网关、GUI 启动的桌面客户端)不继承 shell 中 export 的环境变量,
因此 references/setup-guide.md 中「export WEB_SEARCH_API_KEY 写入 ~/.bashrc」的建议对这些 Agent 不生效。

修复

新增 XDG 通用路径 $XDG_CONFIG_HOME/byted-web-search/.env(未设置时回落 ~/.config/byted-web-search/.env),原有路径全部保留。

由于加载采用 os.environ.setdefault(先到先得),优先级为:

~/.openclaw/.env  >  /root/.openclaw/.env  >  $XDG_CONFIG_HOME/byted-web-search/.env

原路径仍排在前面,既有 OpenClaw 用户行为完全不变。同时补充了 references/setup-guide.md 的配置说明。

验证

在 env -i 干净环境下(零 shell 继承,模拟守护进程场景)实测:

用例 期望 结果
仅 $XDG_CONFIG_HOME/byted-web-search/.env 读到 ✅
仅 ~/.openclaw/.env 读到(向后兼容) ✅
二者同时存在 OpenClaw 优先,行为不变 ✅
自定义 XDG_CONFIG_HOME 读到 ✅
都不存在 报「未找到凭证」 ✅

未采纳的方案

不新增 cwd 的 .env:Agent 可能在任意项目目录下调用脚本,从 cwd 读取 .env 会让任意仓库具备注入环境变量的能力。

Generated with AI assistance

The .env fallback paths were hardcoded to OpenClaw-private directories
(/root/.openclaw/.env and ~/.openclaw/.env). Together with the skill-root
.env -- which lives in a directory managed by the skills CLI and may be
wiped on update -- non-OpenClaw users had no agent-agnostic,
update-safe place to put WEB_SEARCH_API_KEY.

This matters because agents launched as daemons (launchd-managed
gateways, GUI apps) do not inherit shell exports, so the currently
documented `export WEB_SEARCH_API_KEY` approach does not work for them.

Add $XDG_CONFIG_HOME/byted-web-search/.env (falling back to
~/.config/byted-web-search/.env), keeping the existing paths.

Because loading uses os.environ.setdefault (first wins), precedence is
unchanged for existing OpenClaw users:
  ~/.openclaw/.env > /root/.openclaw/.env > $XDG_CONFIG_HOME/...

Verified with a clean environment (env -i, no shell inheritance):
- XDG path only          -> key loaded
- ~/.openclaw/.env only  -> key loaded (backwards compatible)
- both present           -> OpenClaw wins, behavior unchanged
- custom XDG_CONFIG_HOME -> key loaded
- none present           -> reports missing credentials

A cwd .env was deliberately not added: agents may invoke the script from
arbitrary project directories, which would let any repository inject
environment variables.

AI-assisted commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant