Skip to content

fix: persist moonshot cn endpoint selection (closes #33637)#33651

Closed
stakeswky wants to merge 1 commit into
openclaw:mainfrom
stakeswky:fix/33637
Closed

fix: persist moonshot cn endpoint selection (closes #33637)#33651
stakeswky wants to merge 1 commit into
openclaw:mainfrom
stakeswky:fix/33637

Conversation

@stakeswky

@stakeswky stakeswky commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Closes #33637

Problem

Moonshot .cn onboarding could still resolve to the default Moonshot .ai endpoint at runtime when provider config was reconstructed from auth profiles, causing valid platform.moonshot.cn keys to fail with HTTP 401.

Fix

Persist the Moonshot endpoint selection (cn) in auth profile metadata during onboarding, and use that metadata when resolving implicit Moonshot provider config so .cn selections map to https://api.moonshot.cn/v1 consistently.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations agents Agent runtime and tooling size: XS experienced-contributor labels Mar 4, 2026
@greptile-apps

greptile-apps Bot commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR attempts to fix Moonshot CN endpoint selection not being persisted in auth profiles, so that provider config reconstruction correctly routes to api.moonshot.cn/v1 instead of defaulting to api.moonshot.ai/v1. The core mechanism — adding an endpoint field to the credential metadata and reading it back in resolveMoonshotFromProfiles — is sound, and the interactive onboarding path (auth-choice.apply.api-providers.ts) is correctly updated.

However, the non-interactive path contains a critical inversion:

  • In src/commands/onboard-non-interactive/local/auth-choice.ts, the "cn" endpoint tag was applied to the moonshot-api-key branch (the .ai endpoint, line 422) instead of the moonshot-api-key-cn branch (line 444–445, which was left unchanged with no endpoint tag).
  • This means non-interactive .ai users will have endpoint: "cn" persisted and will be routed to api.moonshot.cn at runtime, while non-interactive .cn users will have no metadata and fall back to api.moonshot.ai — the exact opposite of the desired behaviour, and the same HTTP 401 bug the PR aims to fix.

The fix is to swap the two setMoonshotApiKey call-sites in that file: remove "cn" from the moonshot-api-key branch and add it to the moonshot-api-key-cn branch.

Confidence Score: 1/5

  • Not safe to merge — the non-interactive path has the endpoint metadata inverted, which reintroduces the HTTP 401 bug for CN users and breaks .ai users.
  • The interactive onboarding path is correctly fixed, but the non-interactive path in onboard-non-interactive/local/auth-choice.ts has the "cn" tag on the wrong branch. This is a direct logic inversion that causes the fix to misbehave for both Moonshot endpoint variants in non-interactive mode.
  • src/commands/onboard-non-interactive/local/auth-choice.ts — the moonshot-api-key and moonshot-api-key-cn branches have their endpoint metadata assignments swapped.

Last reviewed commit: e71515a

Comment on lines 421 to +422
if (resolved.source !== "profile") {
await setMoonshotApiKey(resolved.key);
await setMoonshotApiKey(resolved.key, undefined, "cn");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Endpoint metadata swapped with moonshot-api-key-cn branch

This branch handles the non-CN (moonshot-api-key) path — i.e. https://api.moonshot.ai/v1 — but the PR changed it to persist endpoint: "cn". That means users who pick the .ai endpoint will have their profile metadata set to "cn", causing resolveMoonshotFromProfiles to resolve MOONSHOT_CN_BASE_URL (api.moonshot.cn) at runtime — exactly the wrong URL.

Conversely, line 445 (moonshot-api-key-cn branch) still calls setMoonshotApiKey(resolved.key) with no endpoint argument, so the .cn users end up with no metadata and fall back to MOONSHOT_BASE_URL (api.moonshot.ai).

The two endpoint tags are inverted. The fix should be:

Suggested change
if (resolved.source !== "profile") {
await setMoonshotApiKey(resolved.key);
await setMoonshotApiKey(resolved.key, undefined, "cn");
if (resolved.source !== "profile") {
await setMoonshotApiKey(resolved.key);
}

And the moonshot-api-key-cn branch at line 444–445 should be updated to:

    if (resolved.source !== "profile") {
      await setMoonshotApiKey(resolved.key, undefined, "cn");
    }
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/commands/onboard-non-interactive/local/auth-choice.ts
Line: 421-422

Comment:
**Endpoint metadata swapped with `moonshot-api-key-cn` branch**

This branch handles the non-CN (`moonshot-api-key`) path — i.e. `https://api.moonshot.ai/v1` — but the PR changed it to persist `endpoint: "cn"`. That means users who pick the `.ai` endpoint will have their profile metadata set to `"cn"`, causing `resolveMoonshotFromProfiles` to resolve `MOONSHOT_CN_BASE_URL` (`api.moonshot.cn`) at runtime — exactly the wrong URL.

Conversely, line 445 (`moonshot-api-key-cn` branch) still calls `setMoonshotApiKey(resolved.key)` with no `endpoint` argument, so the `.cn` users end up with no metadata and fall back to `MOONSHOT_BASE_URL` (`api.moonshot.ai`).

The two endpoint tags are inverted. The fix should be:

```suggestion
    if (resolved.source !== "profile") {
      await setMoonshotApiKey(resolved.key);
    }
```

And the `moonshot-api-key-cn` branch at line 444–445 should be updated to:
```ts
    if (resolved.source !== "profile") {
      await setMoonshotApiKey(resolved.key, undefined, "cn");
    }
```

How can I resolve this? If you propose a fix, please make it concise.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e71515aa28

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

}
if (resolved.source !== "profile") {
await setMoonshotApiKey(resolved.key);
await setMoonshotApiKey(resolved.key, undefined, "cn");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep moonshot-api-key mapped to .ai endpoint in non-interactive flow

In applyNonInteractiveAuthChoice, the moonshot-api-key branch now calls setMoonshotApiKey(resolved.key, undefined, "cn"), which persists CN endpoint metadata for the .ai auth choice. When provider config is later rebuilt in resolveImplicitProviders, that metadata switches Moonshot to https://api.moonshot.cn/v1, so non-interactive setups that selected .ai can be routed to the wrong endpoint and fail authentication.

Useful? React with 👍 / 👎.

@MoerAI

MoerAI commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded by #33696 which addresses the same issue (#33637) with a cleaner approach and is already MERGEABLE. Thank you for the contribution!

@MoerAI

MoerAI commented Mar 4, 2026

Copy link
Copy Markdown
Contributor

This PR has merge conflicts and is superseded by #33696, which addresses the same issue (#33637 — Moonshot CN API 401) with a cleaner approach and is already MERGEABLE with CI passing.

Recommend closing this PR in favor of #33696.

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

Labels

agents Agent runtime and tooling commands Command implementations size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Moonshot CN API doesn't work: HTTP 401

3 participants