fix(quota): handle MiniMax M3/Token Plan API changes (#1589)

Extract shared MiniMax provider logic into minimax-shared.js factory
module used by both minimax-coding-plan and minimax-cn-coding-plan
as thin wrappers.

Endpoint fallback:
- Try /v1/token_plan/remains (M3/Token Plan) first
- Fall back to legacy /v1/api/openplatform/coding_plan/remains
- fetchEndpoint wrapped in try/catch so network/parse errors
  return null instead of throwing, ensuring fallback always runs

Model selection (pickChatModel):
- Prefer MiniMax-M* entries with non-zero total_count (Token Plan M3)
- Fall back to general/chat/text model names (legacy Coding Plan)
- Fall back to any entry with current_interval_remaining_percent
- Ultimate fallback to model_remains[0]

Usage calculation:
- token_plan endpoint: usage_count = remaining, so used = total - remaining
- coding_plan endpoint: usage_count = consumed (legacy behavior)
- Prefer current_interval_remaining_percent when count fields are zero
  (legacy Coding Plan accounts with percentage-based quotas)
- remains_time used as fallback for window duration (in milliseconds,
  confirmed via live API: 9664502ms = 2.68h in 5h window)

Window status:
- Respect current_weekly_status field: status 3 means the window is
  not applicable for the current plan tier (e.g. legacy plans without
  weekly limits). These windows are omitted from the result.
- Default to active when status field is absent (backward compatible).

Fixes #759 (percentage showing empty/null for legacy Coding Plan
accounts and incorrect percentages for M3/Token Plan accounts).
This commit is contained in:
Baruch Vitorino
2026-06-23 11:31:17 +03:00
committed by GitHub
parent 56ca5bcf74
commit eab8862268
4 changed files with 288 additions and 277 deletions
@@ -1,139 +1,15 @@
import { readAuthFile } from '../../opencode/auth.js';
import {
getAuthEntry,
normalizeAuthEntry,
buildResult,
toUsageWindow,
toNumber,
toTimestamp,
} from '../utils/index.js';
import { createMiniMaxCodingPlanProvider } from './minimax-shared.js';
export const providerId = 'minimax-coding-plan';
export const providerName = 'MiniMax Coding Plan (minimax.io)';
export const aliases = ['minimax-coding-plan'];
const provider = createMiniMaxCodingPlanProvider({
providerId: 'minimax-coding-plan',
providerName: 'MiniMax Coding Plan (minimax.io)',
aliases: ['minimax-coding-plan'],
tokenPlanUrl: 'https://api.minimax.io/v1/token_plan/remains',
codingPlanUrl: 'https://api.minimax.io/v1/api/openplatform/coding_plan/remains',
});
export const isConfigured = () => {
const auth = readAuthFile();
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
return Boolean(entry?.key || entry?.token);
};
export const fetchQuota = async () => {
const auth = readAuthFile();
const entry = normalizeAuthEntry(getAuthEntry(auth, aliases));
const apiKey = entry?.key ?? entry?.token;
if (!apiKey) {
return buildResult({
providerId,
providerName,
ok: false,
configured: false,
error: 'Not configured',
});
}
try {
const response = await fetch(
'https://api.minimax.io/v1/api/openplatform/coding_plan/remains',
{
method: 'GET',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
}
);
if (!response.ok) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: `API error: ${response.status}`,
});
}
const payload = await response.json();
const baseResp = payload?.base_resp;
if (baseResp && baseResp.status_code !== 0) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: baseResp.status_msg || `API error: ${baseResp.status_code}`,
});
}
const firstModel = payload?.model_remains?.[0];
if (!firstModel) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: 'No model quota data available',
});
}
const intervalTotal = toNumber(firstModel.current_interval_total_count);
const intervalUsage = toNumber(firstModel.current_interval_usage_count);
const intervalStartAt = toTimestamp(firstModel.start_time);
const intervalResetAt = toTimestamp(firstModel.end_time);
const weeklyTotal = toNumber(firstModel.current_weekly_total_count);
const weeklyUsage = toNumber(firstModel.current_weekly_usage_count);
const weeklyStartAt = toTimestamp(firstModel.weekly_start_time);
const weeklyResetAt = toTimestamp(firstModel.weekly_end_time);
const intervalUsed = intervalUsage;
const weeklyUsed = weeklyUsage;
const intervalUsedPercent =
intervalTotal > 0 && intervalUsed !== null
? Math.max(0, Math.min(100, (intervalUsed / intervalTotal) * 100))
: null;
const intervalWindowSeconds =
intervalStartAt && intervalResetAt && intervalResetAt > intervalStartAt
? Math.floor((intervalResetAt - intervalStartAt) / 1000)
: null;
const weeklyUsedPercent =
weeklyTotal > 0 && weeklyUsed !== null
? Math.max(0, Math.min(100, (weeklyUsed / weeklyTotal) * 100))
: null;
const weeklyWindowSeconds =
weeklyStartAt && weeklyResetAt && weeklyResetAt > weeklyStartAt
? Math.floor((weeklyResetAt - weeklyStartAt) / 1000)
: null;
const windows = {
'5h': toUsageWindow({
usedPercent: intervalUsedPercent,
windowSeconds: intervalWindowSeconds,
resetAt: intervalResetAt,
}),
weekly: toUsageWindow({
usedPercent: weeklyUsedPercent,
windowSeconds: weeklyWindowSeconds,
resetAt: weeklyResetAt,
}),
};
return buildResult({
providerId,
providerName,
ok: true,
configured: true,
usage: { windows },
});
} catch (error) {
return buildResult({
providerId,
providerName,
ok: false,
configured: true,
error: error instanceof Error ? error.message : 'Request failed',
});
}
};
export const providerId = provider.providerId;
export const providerName = provider.providerName;
export const aliases = provider.aliases;
export const isConfigured = provider.isConfigured;
export const fetchQuota = provider.fetchQuota;