From a6240b26aa67ac045d1756dc415f168262d2d6c1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 2 May 2026 05:48:07 +0100 Subject: [PATCH] fix(minimax): respect usage base url --- CHANGELOG.md | 1 + docs/concepts/usage-tracking.md | 3 ++ docs/providers/minimax.md | 3 +- extensions/minimax/index.test.ts | 43 +++++++++++++++++++ extensions/minimax/provider-registration.ts | 13 +++++- .../provider-usage.fetch.minimax.test.ts | 36 ++++++++++++++++ src/infra/provider-usage.fetch.minimax.ts | 30 ++++++++++++- 7 files changed, 125 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a885cd6a681..d2a513b7ec3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ Docs: https://docs.openclaw.ai - Telegram: inherit the process DNS result order for Bot API transport and downgrade recovered sticky IPv4 fallback promotions to debug logs, while keeping pinned-IP escalation warnings visible. Fixes #75904. Thanks @highfly-hi and @neeravmakwana. - Web search/MiniMax: allow `MINIMAX_OAUTH_TOKEN` to satisfy MiniMax Search credentials, so OAuth-authorized MiniMax Token Plan setups do not need a separate web-search key. Fixes #65768. Thanks @kikibrian and @zhouhe-xydt. +- Providers/MiniMax: derive Coding Plan usage polling from the configured MiniMax base URL, so global setups no longer query the CN usage host. Fixes #65054. Thanks @sixone74 and @Yanhu007. - Subagents: honor `sessions_spawn` with `expectsCompletionMessage: false` by skipping parent completion handoff delivery while still running child cleanup. Fixes #75848. Thanks @alfredjbclaw. - Gateway/logging: keep deferred channel startup logs on the subsystem logger, so Slack, Discord, Telegram, and voice-call startup messages keep timestamped prefixes. Thanks @vincentkoc. - Replies/typing: keep typing alive for queued follow-up messages that are genuinely waiting behind an active run, instead of making chat surfaces look idle while work is queued. Fixes #65685. Thanks @papag00se. diff --git a/docs/concepts/usage-tracking.md b/docs/concepts/usage-tracking.md index 832ec311048..213f7d00429 100644 --- a/docs/concepts/usage-tracking.md +++ b/docs/concepts/usage-tracking.md @@ -39,6 +39,9 @@ title: "Usage tracking" `minimax`, `minimax-cn`, and `minimax-portal` as the same MiniMax quota surface, prefers stored MiniMax OAuth when present, and otherwise falls back to `MINIMAX_CODE_PLAN_KEY`, `MINIMAX_CODING_API_KEY`, or `MINIMAX_API_KEY`. + Usage polling derives the Coding Plan host from `models.providers.minimax-portal.baseUrl` + or `models.providers.minimax.baseUrl` when configured, and otherwise uses the + MiniMax CN host. MiniMax's raw `usage_percent` / `usagePercent` fields mean **remaining** quota, so OpenClaw inverts them before display; count-based fields win when present. diff --git a/docs/providers/minimax.md b/docs/providers/minimax.md index 67f6257ddd3..fc83268929f 100644 --- a/docs/providers/minimax.md +++ b/docs/providers/minimax.md @@ -423,7 +423,8 @@ See [MiniMax Search](/tools/minimax-search) for full web search configuration an - - Coding Plan usage API: `https://api.minimaxi.com/v1/api/openplatform/coding_plan/remains` (requires a coding plan key). + - Coding Plan usage API: `https://api.minimaxi.com/v1/token_plan/remains` or `https://api.minimax.io/v1/token_plan/remains` (requires a coding plan key). + - Usage polling derives the host from `models.providers.minimax-portal.baseUrl` or `models.providers.minimax.baseUrl` when configured, so global setups using `https://api.minimax.io/anthropic` poll `api.minimax.io`. Missing or malformed base URLs keep the CN fallback for compatibility. - OpenClaw normalizes MiniMax coding-plan usage to the same `% left` display used by other providers. MiniMax's raw `usage_percent` / `usagePercent` fields are remaining quota, not consumed quota, so OpenClaw inverts them. Count-based fields win when present. - When the API returns `model_remains`, OpenClaw prefers the chat-model entry, derives the window label from `start_time` / `end_time` when needed, and includes the selected model name in the plan label so coding-plan windows are easier to distinguish. - Usage snapshots treat `minimax`, `minimax-cn`, and `minimax-portal` as the same MiniMax quota surface, and prefer stored MiniMax OAuth before falling back to Coding Plan key env vars. diff --git a/extensions/minimax/index.test.ts b/extensions/minimax/index.test.ts index ca4f075688c..b18bb5c7873 100644 --- a/extensions/minimax/index.test.ts +++ b/extensions/minimax/index.test.ts @@ -276,6 +276,49 @@ describe("minimax provider hooks", () => { expect(resolveApiKeyFromConfigAndStore).not.toHaveBeenCalled(); }); + it("uses the configured MiniMax base URL for usage snapshots", async () => { + const { providers } = await registerProviderPlugin({ + plugin: minimaxProviderPlugin, + id: "minimax", + name: "MiniMax Provider", + }); + const apiProvider = requireRegisteredProvider(providers, "minimax"); + const fetchFn = vi.fn(async (input: string | URL | Request) => { + const url = + typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url; + expect(url).toBe("https://api.minimax.io/v1/token_plan/remains"); + return new Response( + JSON.stringify({ + data: { + current_interval_total_count: 100, + current_interval_usage_count: 98, + }, + }), + { status: 200, headers: { "Content-Type": "application/json" } }, + ); + }); + + const result = await apiProvider.fetchUsageSnapshot?.({ + provider: "minimax", + config: { + models: { + providers: { + minimax: { + baseUrl: "https://api.minimax.io/anthropic", + models: [], + }, + }, + }, + }, + env: {}, + token: "key", + timeoutMs: 5000, + fetchFn: fetchFn as typeof fetch, + } as never); + + expect(result?.windows).toEqual([{ label: "5h", usedPercent: 2, resetAt: undefined }]); + }); + it("writes api and authHeader into the MiniMax portal OAuth config patch", async () => { const { providers } = await registerProviderPlugin({ plugin: minimaxProviderPlugin, diff --git a/extensions/minimax/provider-registration.ts b/extensions/minimax/provider-registration.ts index 812e102d30f..b62ef3a1cf5 100644 --- a/extensions/minimax/provider-registration.ts +++ b/extensions/minimax/provider-registration.ts @@ -1,6 +1,7 @@ import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime"; import type { OpenClawPluginApi, + OpenClawConfig, ProviderAuthContext, ProviderAuthResult, ProviderCatalogContext, @@ -68,6 +69,14 @@ function portalModelRef(modelId: string): string { return `${PORTAL_PROVIDER_ID}/${modelId}`; } +function getProviderBaseUrl(cfg: OpenClawConfig, providerId: string): string | undefined { + return normalizeOptionalString(cfg.models?.providers?.[providerId]?.baseUrl); +} + +function resolveMinimaxUsageBaseUrl(cfg: OpenClawConfig): string | undefined { + return getProviderBaseUrl(cfg, PORTAL_PROVIDER_ID) ?? getProviderBaseUrl(cfg, API_PROVIDER_ID); +} + function buildPortalProviderCatalog(params: { baseUrl: string; apiKey: string }) { return { ...buildMinimaxPortalProvider(), @@ -255,7 +264,9 @@ export function registerMinimaxProviders(api: OpenClawPluginApi) { ...MINIMAX_PROVIDER_HOOKS, isModernModelRef: ({ modelId }) => isMiniMaxModernModelId(modelId), fetchUsageSnapshot: async (ctx) => - await fetchMinimaxUsage(ctx.token, ctx.timeoutMs, ctx.fetchFn), + await fetchMinimaxUsage(ctx.token, ctx.timeoutMs, ctx.fetchFn, { + baseUrl: resolveMinimaxUsageBaseUrl(ctx.config), + }), }); api.registerProvider({ diff --git a/src/infra/provider-usage.fetch.minimax.test.ts b/src/infra/provider-usage.fetch.minimax.test.ts index 7384d9dd8cc..9615705048b 100644 --- a/src/infra/provider-usage.fetch.minimax.test.ts +++ b/src/infra/provider-usage.fetch.minimax.test.ts @@ -22,6 +22,42 @@ async function expectMinimaxUsageResult(params: { } describe("fetchMinimaxUsage", () => { + it.each([ + { + name: "uses the CN usage endpoint by default", + baseUrl: undefined, + expectedUrl: "https://api.minimaxi.com/v1/token_plan/remains", + }, + { + name: "derives the global usage endpoint from an Anthropic-compatible base URL", + baseUrl: "https://api.minimax.io/anthropic", + expectedUrl: "https://api.minimax.io/v1/token_plan/remains", + }, + { + name: "derives the usage endpoint from a configured origin", + baseUrl: "https://api.minimaxi.com", + expectedUrl: "https://api.minimaxi.com/v1/token_plan/remains", + }, + { + name: "falls back to CN when the configured base URL is malformed", + baseUrl: "not a url", + expectedUrl: "https://api.minimaxi.com/v1/token_plan/remains", + }, + ])("$name", async ({ baseUrl, expectedUrl }) => { + const mockFetch = createProviderUsageFetch(async (url) => { + expect(url).toBe(expectedUrl); + return makeResponse(200, { + data: { + current_interval_total_count: 100, + current_interval_usage_count: 98, + }, + }); + }); + + const result = await fetchMinimaxUsage("key", 5000, mockFetch, { baseUrl }); + expect(result.windows).toEqual([{ label: "5h", usedPercent: 2, resetAt: undefined }]); + }); + it.each([ { name: "returns HTTP errors for failed requests", diff --git a/src/infra/provider-usage.fetch.minimax.ts b/src/infra/provider-usage.fetch.minimax.ts index 53eeec56a3a..162767a285b 100644 --- a/src/infra/provider-usage.fetch.minimax.ts +++ b/src/infra/provider-usage.fetch.minimax.ts @@ -19,6 +19,13 @@ type MinimaxUsageResponse = { [key: string]: unknown; }; +type FetchMinimaxUsageOptions = { + baseUrl?: string; +}; + +const DEFAULT_MINIMAX_USAGE_ORIGIN = "https://api.minimaxi.com"; +const MINIMAX_USAGE_PATH = "/v1/token_plan/remains"; + const RESET_KEYS = [ "reset_at", "resetAt", @@ -137,7 +144,7 @@ const REMAINING_KEYS = [ "prompts_left", "promptsLeft", "left", - // MiniMax `/coding_plan/remains` misnames these: values are remaining quota, not consumed. + // MiniMax usage endpoints misname these: values are remaining quota, not consumed. // See https://github.com/MiniMax-AI/MiniMax-M2/issues/99 "current_interval_usage_count", "currentIntervalUsageCount", @@ -366,13 +373,32 @@ function pickChatModelRemains(modelRemains: unknown[]): Record }); } +function resolveMinimaxUsageUrl(baseUrl?: string): string { + const trimmed = baseUrl?.trim(); + if (!trimmed) { + return `${DEFAULT_MINIMAX_USAGE_ORIGIN}${MINIMAX_USAGE_PATH}`; + } + + try { + const parsed = new URL(trimmed); + if (parsed.protocol === "http:" || parsed.protocol === "https:") { + return `${parsed.origin}${MINIMAX_USAGE_PATH}`; + } + } catch { + // Fall through to the stable CN default for malformed config values. + } + + return `${DEFAULT_MINIMAX_USAGE_ORIGIN}${MINIMAX_USAGE_PATH}`; +} + export async function fetchMinimaxUsage( apiKey: string, timeoutMs: number, fetchFn: typeof fetch, + options?: FetchMinimaxUsageOptions, ): Promise { const res = await fetchJson( - "https://api.minimaxi.com/v1/api/openplatform/coding_plan/remains", + resolveMinimaxUsageUrl(options?.baseUrl), { method: "GET", headers: {