creditdaddy 1.7.0 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,21 +1,45 @@
1
1
  /**
2
2
  * MiniMax Code 服务端 API 客户端 — 用户信息、额度查询、每日签到与 Token 续期。
3
3
  *
4
- * 官方标准端点:
5
- * - 用户资料:GET https://agent.minimax.cn/matrix/api/v1/user/profile
6
- * - 额度查询:POST https://agent.minimax.cn/matrix/api/v1/commerce/get_membership_info
7
- * - 签到状态:GET https://agent.minimax.cn/minimax-cloud/api/v1/signin/status?timezone_offset=28800&is_desktop=1&client=desktop
8
- * - 签到领取:POST https://agent.minimax.cn/minimax-cloud/api/v1/signin/claim?timezone_offset=28800&is_desktop=1&client=desktop
9
- * - Token 续期:POST https://account.minimax.cn/oauth2/token
4
+ * 双渠道(issue #54):国内 / 国际同协议同 client(mcode-public),只换域名——
5
+ * CN https://agent.minimax.cn + https://account.minimax.cn(官方客户端 NEXT_PUBLIC_LOCALE=zh)
6
+ * Intl https://agent.minimax.io + https://account.minimax.io(NEXT_PUBLIC_LOCALE=en)
7
+ * 端点实证:@mavis/oauth-core/dist/endpoint-config.js 的 ACCOUNT_ORIGINS(2026-11-07 从
8
+ * 官方客户端 app.asar 拆包),与本机 ~/.minimax/auth/prod/<region>/mcode-public/ 布局互证。
9
+ *
10
+ * 官方标准端点(CN,intl 同路径):
11
+ * - 用户资料:GET {agent}/matrix/api/v1/user/profile
12
+ * - 额度查询:POST {agent}/matrix/api/v1/commerce/get_membership_info
13
+ * - 签到状态:GET {agent}/minimax-cloud/api/v1/signin/status?timezone_offset=28800&is_desktop=1&client=desktop
14
+ * - 签到领取:POST {agent}/minimax-cloud/api/v1/signin/claim?timezone_offset=28800&is_desktop=1&client=desktop
15
+ * - Token 续期:POST {account}/oauth2/token
10
16
  *
11
17
  * 补全模型网关端点:
12
- * - Anthropic 兼容端点:POST https://agent.minimax.cn/mavis/api/v1/llm/v1/messages
18
+ * - Anthropic 兼容端点:POST {agent}/mavis/api/v1/llm/v1/messages
13
19
  */
14
20
 
21
+ import { createHash } from 'node:crypto';
15
22
  import { fetchJsonRace } from './zcodeClient.js';
16
23
  import { withRefreshDedupe, refreshFpDead } from './refreshGuard.js';
17
24
  import { FETCH_TIMEOUT_MS } from './constants.js';
18
25
 
26
+ /** MiniMax 渠道档位:cn(国内,agent.minimax.cn)/ global(国际,agent.minimax.io) */
27
+ export const MINIMAX_FLAVORS = {
28
+ cn: { agent: 'https://agent.minimax.cn', account: 'https://account.minimax.cn' },
29
+ global: { agent: 'https://agent.minimax.io', account: 'https://account.minimax.io' },
30
+ };
31
+
32
+ /** 账号档位判定:provider 为 minimax-global 或 meta.minimaxFlavor 标记的走国际线,其余国内 */
33
+ export function minimaxFlavorOf(account) {
34
+ if (account?.provider === 'minimax-global') return 'global';
35
+ return account?.meta?.minimaxFlavor === 'global' ? 'global' : 'cn';
36
+ }
37
+
38
+ function basesOf(account) {
39
+ return MINIMAX_FLAVORS[minimaxFlavorOf(account)] || MINIMAX_FLAVORS.cn;
40
+ }
41
+
42
+ // 兼容旧引用(网关 / minimaxLocal 等按 CN 域名直连的地方);新代码请用 basesOf(account)
19
43
  export const MINIMAX_AGENT_BASE = 'https://agent.minimax.cn';
20
44
  export const MINIMAX_ACCOUNT_BASE = 'https://account.minimax.cn';
21
45
  export const MINIMAX_MESSAGES_URL = `${MINIMAX_AGENT_BASE}/mavis/api/v1/llm/v1/messages`;
@@ -26,6 +50,95 @@ export const SIGNIN_STATUS_URL = `${MINIMAX_AGENT_BASE}/minimax-cloud/api/v1/sig
26
50
  export const SIGNIN_CLAIM_URL = `${MINIMAX_AGENT_BASE}/minimax-cloud/api/v1/signin/claim?timezone_offset=28800&is_desktop=1&client=desktop`;
27
51
  export const TOKEN_REFRESH_URL = `${MINIMAX_ACCOUNT_BASE}/oauth2/token`;
28
52
 
53
+ /** 按账号档位解析各端点(CN / 国际只差域名,路径完全一致) */
54
+ export function minimaxEndpointsOf(account) {
55
+ const { agent, account: accountBase } = basesOf(account);
56
+ return {
57
+ agent,
58
+ account: accountBase,
59
+ messagesUrl: `${agent}/mavis/api/v1/llm/v1/messages`,
60
+ profileUrl: `${agent}/matrix/api/v1/user/profile`,
61
+ membershipUrl: `${agent}/matrix/api/v1/commerce/get_membership_info`,
62
+ signinStatusUrl: `${agent}/minimax-cloud/api/v1/signin/status?timezone_offset=28800&is_desktop=1&client=desktop`,
63
+ signinClaimUrl: `${agent}/minimax-cloud/api/v1/signin/claim?timezone_offset=28800&is_desktop=1&client=desktop`,
64
+ tokenRefreshUrl: `${accountBase}/oauth2/token`,
65
+ };
66
+ }
67
+
68
+ /** 账户动作类端点(签到 / 额度 / 资料)的服务端签名,取自 10Router open-sse 实现 */
69
+ const SIGN_SECRET = 'I*7Cf%WZ#S&%1RlZJ&C2';
70
+ const md5hex = (s) => createHash('md5').update(s).digest('hex');
71
+
72
+ /**
73
+ * 构造 signed account 请求:查询参数与请求头参与签名,签名值绑定
74
+ * URL+时间戳(+body),因此不能与裸 fetch 的查询串混用。
75
+ * @returns {{url: string, headers: object, body?: string}}
76
+ */
77
+ export function signedMiniMaxRequest(flavor, path, { access, userID, body, now = Date.now() } = {}) {
78
+ const lang = flavor === 'global' ? 'en' : 'zh';
79
+ const url = new URL(path, basesOf({ provider: flavor === 'global' ? 'minimax-global' : 'minimax' }).agent);
80
+ url.search = new URLSearchParams({
81
+ device_platform: 'mcode',
82
+ biz_id: '3',
83
+ app_id: '3001',
84
+ version_code: '22201',
85
+ unix: String(now),
86
+ timezone_offset: String(new Date().getTimezoneOffset() * -60),
87
+ sys_language: lang,
88
+ lang,
89
+ device_id: '0',
90
+ os_name: process.platform,
91
+ browser_name: 'mcode',
92
+ user_id: String(userID ?? '').trim() || '0',
93
+ client: 'mcode',
94
+ }).toString();
95
+ const at = url.pathname + url.search;
96
+ const ts = Math.floor(now / 1000);
97
+ const json = body === undefined ? undefined : JSON.stringify(body);
98
+ const headers = {
99
+ Accept: 'application/json',
100
+ 'Content-Type': 'application/json',
101
+ 'User-Agent': 'MiniMaxCode',
102
+ Authorization: `Bearer ${String(access || '').trim()}`,
103
+ yy: json === undefined
104
+ ? md5hex(`${encodeURIComponent(at)}_{}${md5hex(String(now))}ooui`)
105
+ : md5hex(`${encodeURIComponent(at)}_${json}${md5hex(String(now))}ooui`),
106
+ 'x-timestamp': String(ts),
107
+ 'x-signature': json === undefined ? md5hex(`${ts}${SIGN_SECRET}`) : md5hex(`${ts}${SIGN_SECRET}${json}`),
108
+ };
109
+ return { url: url.toString(), headers, body: json };
110
+ }
111
+
112
+ /** 账户端点统一入口:国际版走签名 + 代理优先(CN 网络直连 agent.minimax.io 会被 307 到 CN 域名家族) */
113
+ function signedCall(account, path, { access, userID, body, timeoutMs = FETCH_TIMEOUT_MS } = {}) {
114
+ const flavor = minimaxFlavorOf(account);
115
+ const req = signedMiniMaxRequest(flavor, path, { access, userID, body });
116
+ return fetchJsonRace(req.url, {
117
+ method: req.body === undefined ? 'GET' : 'POST',
118
+ headers: req.headers,
119
+ body: req.body ?? null,
120
+ timeoutMs,
121
+ preferProxy: flavor === 'global',
122
+ });
123
+ }
124
+
125
+ /** 307 到别的域名家族 = 地区限制,本机配置代理即可,token 本身没问题 */
126
+ function geoNote(requestedUrl, res) {
127
+ try {
128
+ const from = new URL(requestedUrl).host;
129
+ const to = new URL(res.url || requestedUrl).host;
130
+ if (from === to) return null;
131
+ return `上游 ${from} 在当前网络被重定向到 ${to}(地区限制)——请为国际版配置代理后重试`;
132
+ } catch {
133
+ return null;
134
+ }
135
+ }
136
+
137
+ /** 签名里的 user_id:账号已解析出 uid 时带上,未解析按 10Router 口径退化为 "0" */
138
+ function uiOf(account) {
139
+ return account?.uid || account?.meta?.realUserID || undefined;
140
+ }
141
+
29
142
  /** 时间戳宽容转换(口径同 traeClient.iso):≤1e11 视为秒,否则视为毫秒;0/无效 → null */
30
143
  function isoMs(v) {
31
144
  const n = Number(v);
@@ -47,7 +160,7 @@ function authHeaders(token) {
47
160
  * @param {string} refreshToken
48
161
  * @returns {Promise<{accessToken: string, refreshToken: string, expiresIn: number}>}
49
162
  */
50
- export async function refreshMiniMaxToken(refreshToken) {
163
+ export async function refreshMiniMaxToken(refreshToken, { flavor = 'cn' } = {}) {
51
164
  if (!refreshToken) throw new Error('缺少 refreshToken');
52
165
  const form = new URLSearchParams();
53
166
  form.set('grant_type', 'refresh_token');
@@ -56,7 +169,7 @@ export async function refreshMiniMaxToken(refreshToken) {
56
169
  form.set('scope', 'agent.default');
57
170
  form.set('audience', 'agent-backend');
58
171
 
59
- const res = await fetchJsonRace(TOKEN_REFRESH_URL, {
172
+ const res = await fetchJsonRace(MINIMAX_FLAVORS[flavor].account + '/oauth2/token', {
60
173
  method: 'POST',
61
174
  headers: {
62
175
  'Content-Type': 'application/x-www-form-urlencoded',
@@ -117,7 +230,7 @@ export function refreshAccountToken(account, ctx = {}) {
117
230
  } catch {}
118
231
  ctx.log?.('MiniMax token 已过期,正在自动刷新…');
119
232
  try {
120
- const refreshed = await refreshMiniMaxToken(a.refreshToken);
233
+ const refreshed = await refreshMiniMaxToken(a.refreshToken, { flavor: minimaxFlavorOf(a) });
121
234
  const expiresAt = new Date(Date.now() + refreshed.expiresIn * 1000).toISOString();
122
235
  const creds = {
123
236
  token: refreshed.accessToken,
@@ -166,8 +279,8 @@ export function refreshAccountToken(account, ctx = {}) {
166
279
  /**
167
280
  * 获取用户信息
168
281
  */
169
- export async function fetchMiniMaxProfile(token) {
170
- const res = await fetchJsonRace(PROFILE_URL, {
282
+ export async function fetchMiniMaxProfile(token, { flavor = 'cn' } = {}) {
283
+ const res = await fetchJsonRace(MINIMAX_FLAVORS[flavor].agent + '/matrix/api/v1/user/profile', {
171
284
  headers: authHeaders(token),
172
285
  timeoutMs: FETCH_TIMEOUT_MS,
173
286
  });
@@ -207,15 +320,7 @@ export async function verifyMiniMaxAccount(account) {
207
320
  */
208
321
  export async function fetchMiniMaxQuota(account, ctx = {}) {
209
322
  return withMiniMaxAuth(account, async (token) => {
210
- const res = await fetchJsonRace(MEMBERSHIP_URL, {
211
- method: 'POST',
212
- headers: {
213
- ...authHeaders(token),
214
- 'Content-Type': 'application/json',
215
- },
216
- body: JSON.stringify({}),
217
- timeoutMs: FETCH_TIMEOUT_MS,
218
- });
323
+ const res = await signedCall(account, '/matrix/api/v1/commerce/get_membership_info', { access: token, userID: uiOf(account), body: {} });
219
324
  if (res.status === 401) {
220
325
  const err = new Error('MiniMax 凭据已失效 (401)');
221
326
  err.auth = true;
@@ -294,10 +399,7 @@ export function normalizeMiniMaxQuota(data) {
294
399
  export async function fetchMiniMaxCheckinStatus(account, ctx = {}) {
295
400
  try {
296
401
  return await withMiniMaxAuth(account, async (token) => {
297
- const statusRes = await fetchJsonRace(SIGNIN_STATUS_URL, {
298
- headers: authHeaders(token),
299
- timeoutMs: FETCH_TIMEOUT_MS,
300
- });
402
+ const statusRes = await signedCall(account, '/minimax-cloud/api/v1/signin/status', { access: token, userID: uiOf(account) });
301
403
  if (!statusRes.ok) return null;
302
404
  const statusData = await statusRes.json().catch(() => null);
303
405
  if (!statusData || statusData.base_resp?.status_code !== 0) return null;
@@ -321,11 +423,10 @@ export async function fetchMiniMaxCheckinStatus(account, ctx = {}) {
321
423
  export async function checkinMiniMax(account, ctx = {}) {
322
424
  return withMiniMaxAuth(account, async (token) => {
323
425
  // 1. 查询签到状态
324
- const statusRes = await fetchJsonRace(SIGNIN_STATUS_URL, {
325
- headers: authHeaders(token),
326
- timeoutMs: FETCH_TIMEOUT_MS,
327
- });
328
- if (statusRes.status === 401) {
426
+ const statusRes = await signedCall(account, '/minimax-cloud/api/v1/signin/status', { access: token, userID: uiOf(account) });
427
+ if (statusRes.status === 401 || statusRes.status === 403) {
428
+ const geo = geoNote(statusRes.url, statusRes);
429
+ if (geo) throw new Error(geo);
329
430
  const err = new Error('MiniMax 凭据已失效 (401)');
330
431
  err.auth = true;
331
432
  throw err;
@@ -349,13 +450,11 @@ export async function checkinMiniMax(account, ctx = {}) {
349
450
  };
350
451
  }
351
452
 
352
- // 2. 发起签到 claim
353
- const claimRes = await fetchJsonRace(SIGNIN_CLAIM_URL, {
354
- method: 'POST',
355
- headers: authHeaders(token),
356
- timeoutMs: FETCH_TIMEOUT_MS,
357
- });
358
- if (claimRes.status === 401) {
453
+ // 2. 发起签到 claim(status=GET,claim=POST;动词错了是裸 404,不是签名错)
454
+ const claimRes = await signedCall(account, '/minimax-cloud/api/v1/signin/claim', { access: token, userID: uiOf(account), body: {} });
455
+ if (claimRes.status === 401 || claimRes.status === 403) {
456
+ const geo = geoNote(claimRes.url, claimRes);
457
+ if (geo) throw new Error(geo);
359
458
  const err = new Error('MiniMax 凭据已失效 (401)');
360
459
  err.auth = true;
361
460
  throw err;
@@ -14,7 +14,7 @@
14
14
 
15
15
  import { fetchJsonRace } from './zcodeClient.js';
16
16
  import { loadAccounts, loadSettings, saveSettings } from './store.js';
17
- import { refreshMiniMaxToken, refreshAccountToken, fetchMiniMaxQuota, MINIMAX_MESSAGES_URL } from './minimaxClient.js';
17
+ import { refreshMiniMaxToken, refreshAccountToken, fetchMiniMaxQuota, minimaxEndpointsOf, minimaxFlavorOf } from './minimaxClient.js';
18
18
  import { clearRefreshDead, __resetRefreshGuardForTests } from './refreshGuard.js';
19
19
  import { refreshContext } from './accounts.js';
20
20
  import { gatewayHostSuggestion } from './tenrouter.js';
@@ -61,6 +61,7 @@ export const stats = {
61
61
  lastCallAt: null,
62
62
  calls: 0,
63
63
  lastAccount: null,
64
+ lastAccountAt: null, // 「接口使用中」徽章的 30 分钟窗口锚点(面板 gwRecentWindow 内才亮)
64
65
  };
65
66
 
66
67
  // 平滑加权轮询(SWRR):剩余积分多的账号分到更多请求,而不是雨露均沾地 round-robin。
@@ -142,6 +143,7 @@ export function __resetForTests() {
142
143
  stats.lastCallAt = null;
143
144
  stats.calls = 0;
144
145
  stats.lastAccount = null;
146
+ stats.lastAccountAt = null;
145
147
  __resetRefreshGuardForTests();
146
148
  }
147
149
 
@@ -198,7 +200,7 @@ const STICKY_MARGIN_ABS = 50; // 且绝对差超过 50 积分
198
200
  * 拉黑/冷却在挂载时剔除;扭转主力粘性见上方说明。
199
201
  */
200
202
  async function rotationQueue() {
201
- const all = (await loadAccounts()).filter((a) => a.provider === 'minimax');
203
+ const all = (await loadAccounts()).filter((a) => a.provider === 'minimax' || a.provider === 'minimax-global');
202
204
  const now = Date.now();
203
205
  const candidates = [];
204
206
  for (const a of all) {
@@ -228,7 +230,7 @@ async function rotationQueue() {
228
230
  }
229
231
 
230
232
  async function unavailableReason() {
231
- const all = (await loadAccounts()).filter((a) => a.provider === 'minimax');
233
+ const all = (await loadAccounts()).filter((a) => a.provider === 'minimax' || a.provider === 'minimax-global');
232
234
  if (!all.length) return '没有 MiniMax 账号:请在 MiniMax Code 登录后在面板进行本机导入,或粘贴 Token 添加';
233
235
  const now = Date.now();
234
236
  const n = { dead: 0, cooling: 0, noToken: 0 };
@@ -352,6 +354,8 @@ export async function handleGateway(req, res) {
352
354
  let upstream;
353
355
 
354
356
  let activeToken = account.token;
357
+ // 按账号档位选上游域名(国内 agent.minimax.cn / 国际 agent.minimax.io,协议同形)
358
+ const upstreamUrl = minimaxEndpointsOf(account).messagesUrl;
355
359
  const sendUpstream = async (tok) => {
356
360
  const headers = {
357
361
  'Content-Type': 'application/json',
@@ -359,7 +363,7 @@ export async function handleGateway(req, res) {
359
363
  Authorization: tok.startsWith('Bearer ') ? tok : `Bearer ${tok}`,
360
364
  'anthropic-version': '2023-06-01',
361
365
  };
362
- return fetchJsonRace(MINIMAX_MESSAGES_URL, {
366
+ return fetchJsonRace(upstreamUrl, {
363
367
  method: 'POST',
364
368
  headers,
365
369
  body: rawBody,
@@ -396,6 +400,7 @@ export async function handleGateway(req, res) {
396
400
 
397
401
  if (upstream.ok) {
398
402
  stats.lastAccount = label;
403
+ stats.lastAccountAt = Date.now();
399
404
  resetCoolingStreak(account.id); // 成功一次即清零连续 429 计数
400
405
  logger.info('MINIMAX-GW', '{label} 补全成功 ({status})', { label, status: upstream.status });
401
406
  try {
@@ -481,7 +486,7 @@ export async function handleGateway(req, res) {
481
486
 
482
487
  export async function gatewayStatus() {
483
488
  const enabled = await gatewayEnabled();
484
- const all = (await loadAccounts()).filter((a) => a.provider === 'minimax');
489
+ const all = (await loadAccounts()).filter((a) => a.provider === 'minimax' || a.provider === 'minimax-global');
485
490
  const now = Date.now();
486
491
  const settings = await loadSettings();
487
492
 
@@ -494,6 +499,7 @@ export async function gatewayStatus() {
494
499
  lastCallAt: stats.lastCallAt,
495
500
  calls: stats.calls,
496
501
  lastAccount: stats.lastAccount,
502
+ lastAccountAt: stats.lastAccountAt,
497
503
  },
498
504
  accounts: all.length,
499
505
  // cooling 保持字符串数组(历史形状);冷却细节走 coolingDetail(与 zcodeGateway 同款)