Skip to content

[good first issue] fix(offload): resolve local-llm apiKey from auth-profile store as fallback#150

Open
MicroGery wants to merge 2 commits into
TencentCloud:mainfrom
MicroGery:fix/offload-auth-profile-key
Open

[good first issue] fix(offload): resolve local-llm apiKey from auth-profile store as fallback#150
MicroGery wants to merge 2 commits into
TencentCloud:mainfrom
MicroGery:fix/offload-auth-profile-key

Conversation

@MicroGery

@MicroGery MicroGery commented Jun 4, 2026

Copy link
Copy Markdown

Description | 描述

offload.model 引用的 provider 通过 OpenClaw 的 auth-profiles 机制管理密钥时(即使用 openclaw auth 录入,OpenClaw 推荐的默认方式),local-llm 初始化逻辑只从models.providers[provider].apiKey(配置树明文)读取密钥。
如果密钥不在配置树里,LocalLlmClient 构建被跳过,L1/L1.5/L2 全部禁用,并报出误导性的 missing apiKey 错误——即便主对话用同一个 provider 正常运行。

修复方案:
新增 src/offload/auth-profile-key.ts,一个同步 helper:

  • 通过 openclaw/plugin-sdk/provider-auth 加载 auth-profile store
  • 按 provider 名找到第一个 api_key 类型的 credential,返回明文 key
  • 整个调用链用 try/catch 包裹:老版本 OpenClaw 不暴露该 SDK 子路径时,静默退回原有行为

index.ts 中,apiKey 取值改为:providerCfg?.apiKey ?? resolveApiKeyFromAuthProfile(api, providerKey, logger)
registerOffload() 维持同步契约,backendClient 赋值线程安全、无竞争。

Related Issue | 关联 Issue

Fix #90

Change Type | 修改类型

  • Bug fix | Bug 修复
  • New feature | 新功能
  • Documentation update | 文档更新
  • Code optimization | 代码优化

Self-test Checklist | 自测清单

  • Verified locally | 本地验证通过
  • No existing features affected | 无影响现有功能

修复后检查日志:[context-offload] [local-llm] Initialized: model=mimo-v2-pro, baseUrl=https://api.xiaomimimo.com/v1

…lback

When offload.model references a provider whose apiKey is managed via
OpenClaw's auth-profiles mechanism (the recommended default, e.g. set up
with 'openclaw auth'), the local-llm initializer previously only read
models.providers[provider].apiKey in the config tree. If the key was
absent there, LocalLlmClient construction was skipped and L1/L1.5/L2
were disabled with a misleading "missing apiKey" error -- even though the
key existed and the main agent model worked fine.

Fix: add src/offload/auth-profile-key.ts, a synchronous helper that
loads the auth-profile store via openclaw/plugin-sdk/provider-auth and
returns the first api_key-type credential for the provider. The helper
is guarded with try/catch so older OpenClaw versions that do not expose
this SDK subpath degrade silently to the previous behavior.

In index.ts, the apiKey lookup becomes:
  providerCfg?.apiKey ?? resolveApiKeyFromAuthProfile(api, providerKey, logger)

registerOffload() keeps its synchronous contract; no race is introduced
around backendClient assignment.

Closes TencentCloud#90
Signed-off-by: MicroGrey <changyuhang@outlook.com>
@MicroGery MicroGery changed the title fix(offload): resolve local-llm apiKey from auth-profile store as fallback [good first issue] fix(offload): resolve local-llm apiKey from auth-profile store as fallback Jun 4, 2026
@Maxwell-Code07

Copy link
Copy Markdown
Collaborator

Thank you for participating in our good first issue event, we will complete the review internally as soon as possible!

@Maxwell-Code07 Maxwell-Code07 added the good first issue Good for newcomers label Jun 4, 2026
@honchow

honchow commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

感谢提供PR,麻烦补充下单测吧,我们尽快合入

@MicroGery

Copy link
Copy Markdown
Author

感谢提供PR,麻烦补充下单测吧,我们尽快合入

好的,我来补充一下测试用例

Add unit tests for resolveApiKeyFromAuthProfile covering the four
resolution branches: api_key profile returns the plaintext key,
oauth/token-only providers return undefined, providers with no profile
return undefined, and keyRef-only (no plaintext) profiles are skipped.
Also covers the older-host case where the provider-auth SDK subpath is
unavailable.

To keep the test self-contained (no real OpenClaw install or on-disk
auth-profile store), resolveApiKeyFromAuthProfile gains an optional
fourth parameter to inject a fake provider-auth SDK loader. Production
callers pass three arguments and keep loading the real SDK via require.

Signed-off-by: MicroGrey <changyuhang@outlook.com>
@MicroGery

Copy link
Copy Markdown
Author

感谢提供PR,麻烦补充下单测吧,我们尽快合入

请检视

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

Labels

good first issue Good for newcomers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[good first issue]🎯 context-offload local-llm 无法使用 OpenClaw auth-profiles 密钥管理机制

3 participants