Skip to content

Fix https://github.com/sunnoy/openclaw-plugin-wecom/issues/49#52

Closed
roamerxv wants to merge 4 commits intosunnoy:mainfrom
roamerxv:main
Closed

Fix https://github.com/sunnoy/openclaw-plugin-wecom/issues/49#52
roamerxv wants to merge 4 commits intosunnoy:mainfrom
roamerxv:main

Conversation

@roamerxv
Copy link
Copy Markdown

@roamerxv roamerxv commented Mar 1, 2026

ix #49

@sunnoy
Copy link
Copy Markdown
Owner

sunnoy commented Mar 2, 2026

感谢 PR!环境变量替换这个需求很实用,不过当前实现有几个问题需要修改:

1. 替换结果未使用(Bug)

const corpId = resolveEnvVars(agent?.corpId);
const corpSecret = resolveEnvVars(agent?.corpSecret);
const agentId = resolveEnvVars(agent?.agentId);

// ❌ 下面判断和返回还是用的原始 agent.xxx,替换后的值被丢弃了
if (!agent?.corpId || !agent?.corpSecret || !agent?.agentId) return null;
return { corpId: agent.corpId, corpSecret: agent.corpSecret, agentId: agent.agentId };

应该改为:

if (!corpId || !corpSecret || !agentId) return null;
return { corpId, corpSecret, agentId };

2. 覆盖面不够

当前只处理了 agent.corpId / corpSecret / agentId 三个字段。实际上 tokenencodingAesKeyagent.tokenagent.encodingAesKey 等字段也可能需要环境变量。在单个函数里逐字段替换不够通用。

3. 建议:在配置加载层统一处理

更好的做法是在配置加载时对整个 config 对象做一次递归的环境变量替换,而不是在 resolveAgentConfig() 里打补丁。例如:

function deepResolveEnvVars(obj) {
  if (typeof obj === "string") {
    return obj.replace(/\$\{([^}]+)\}/g, (m, k) => process.env[k] || m);
  }
  if (Array.isArray(obj)) return obj.map(deepResolveEnvVars);
  if (obj && typeof obj === "object") {
    const result = {};
    for (const [k, v] of Object.entries(obj)) result[k] = deepResolveEnvVars(v);
    return result;
  }
  return obj;
}

可以在 setOpenclawConfig(config) 时统一调用一次,这样所有字段都支持 ${VAR} 语法,后续也不用逐个函数修改。


另外注意 main 分支的 state.js 已经重构过(resolveAgentConfig 现在走 accounts.js 的多账号解析层),当前 PR 的 diff 会有冲突,需要 rebase 后在 accounts.jsbuildAccount()setOpenclawConfig() 中统一做环境变量替换。

@roamerxv
Copy link
Copy Markdown
Author

roamerxv commented Mar 2, 2026 via email

@sunnoy
Copy link
Copy Markdown
Owner

sunnoy commented Mar 18, 2026

感谢提交这个修复方向。不过这条 PR 目前我建议先不要直接合,主要是它已经明显落后于当前主干结构了。

现在的 wecom/state.js 已经切到按 account 解析配置(包括 resolveAgentConfig(accountId) / resolveAccountConfig(accountId) 这一套),而这条 PR 还是基于更早的单账号版本去改 state.js,所以直接合进当前 main 风险会比较高。GitHub 上这条 PR 现在也是 DIRTY,本身就说明它需要先和主干重新对齐。

另外,从改动内容看,这条 PR 的核心目标是支持配置里的 ${VAR} 环境变量占位符解析。这个目标本身没有问题,但建议在当前主干上重做,而不是继续叠加在这条旧分支上:

  1. 先 rebase / 重建到最新 main
  2. 按当前多账号配置结构实现环境变量解析
  3. 明确解析作用域(只解析 wecom 配置,还是递归解析整个插件 config)
  4. 补回归测试,尤其是多账号 + agent/webhook 场景

如果后面基于最新主干重开一个更小、更聚焦的 PR,这个能力我觉得是可以继续评估的。

@sunnoy
Copy link
Copy Markdown
Owner

sunnoy commented Mar 23, 2026

感谢 PR!不过这个方案有几个问题不太适合合并:

  1. 性能与引用问题setOpenclawConfig() 是高频调用路径,每次对整个 config 做深拷贝+递归 resolveEnvVars 开销较大,并且会破坏 config 对象引用一致性(gateway 运行时依赖此特性)
  2. 职责归属:config 中的环境变量解析应该由 OpenClaw 核心框架处理,插件层不应介入。可以在 OpenClaw 主仓库提 feature request
  3. 关联 issue agent 模式下,可以对话。但是要求发送 图片会出错。 #49 已关闭

建议:如果需要在 config 中使用环境变量,可以直接在 openclaw.json 中用 shell 变量,或在启动脚本中做 envsubst 预处理。

关闭此 PR。

@sunnoy
Copy link
Copy Markdown
Owner

sunnoy commented Mar 23, 2026

Closing — related issue #49 is already closed and env var resolution belongs in the OpenClaw core, not the plugin layer.

@sunnoy sunnoy closed this Mar 23, 2026
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.

3 participants