creditdaddy 1.7.1 → 1.8.0
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.
- package/LICENSE +21 -21
- package/README.md +445 -440
- package/build.json +3 -3
- package/package.json +34 -34
- package/src/accounts.js +15 -0
- package/src/assets/donate-alipay.svg +1 -1
- package/src/assets/donate-ifdian.svg +1 -1
- package/src/assets/donate-wechat.svg +1 -1
- package/src/assets/providers/catpaw.png +0 -0
- package/src/assets/providers/codebuddy-ide.png +0 -0
- package/src/assets/providers/dumate.png +0 -0
- package/src/assets/providers/loomy.png +0 -0
- package/src/assets/providers/meoo.png +0 -0
- package/src/assets/providers/mirasim.png +0 -0
- package/src/authDevice.js +3 -1
- package/src/bgdaemon.js +341 -341
- package/src/buildInfo.js +65 -32
- package/src/catpawClient.js +103 -103
- package/src/catpawLocal.js +236 -236
- package/src/checkin.js +11 -2
- package/src/codebuddyIdeLocal.js +381 -381
- package/src/constants.js +121 -116
- package/src/cookieSites.js +340 -301
- package/src/daemon.js +2682 -2415
- package/src/doubaoClient.js +167 -167
- package/src/dumateClient.js +517 -0
- package/src/dumateLocal.js +298 -0
- package/src/dumateQr.js +337 -0
- package/src/externalQuota.js +577 -574
- package/src/formatDuration.js +17 -17
- package/src/i18n/en.json +2156 -1980
- package/src/i18n/ja.json +2156 -1980
- package/src/i18n/ko.json +2156 -1980
- package/src/i18n/zh-TW.json +2156 -1980
- package/src/i18n.js +47 -47
- package/src/lingxiClient.js +373 -332
- package/src/localDetect.js +147 -147
- package/src/longcatClient.js +260 -260
- package/src/loomyAuth.js +230 -230
- package/src/loomyClient.js +366 -366
- package/src/loomyLocal.js +206 -206
- package/src/meooAuth.js +246 -0
- package/src/meooClient.js +250 -0
- package/src/minimaxClient.js +86 -28
- package/src/minimaxGateway.js +520 -520
- package/src/minimaxLocal.js +344 -322
- package/src/mirasimClient.js +211 -211
- package/src/mirasimLocal.js +392 -392
- package/src/modelscopeClient.js +275 -275
- package/src/panel.html +5964 -5528
- package/src/prodIcons.json +7 -5
- package/src/providers.js +52 -4
- package/src/qoderApp.js +578 -578
- package/src/qoderClient.js +266 -266
- package/src/qoderUmid.js +199 -199
- package/src/quotaRefresh.js +119 -119
- package/src/refreshGuard.js +75 -75
- package/src/riverClient.js +372 -329
- package/src/store.js +3 -1
- package/src/tenrouterAccounts.js +18 -3
- package/src/traeAuth.js +601 -530
- package/src/traeClient.js +356 -337
- package/src/traeGateway.js +875 -812
- package/src/traeLocal.js +463 -371
- package/src/transfer.js +24 -1
- package/src/usageSync.js +535 -523
- package/src/winProc.js +134 -134
- package/src/workbuddyLocal.js +35 -4
- package/src/xiaomiMimoClient.js +206 -206
- package/src/zcodeAuth.js +131 -131
- package/src/zcodeGateway.js +850 -850
- package/src/zcodeLocal.js +340 -340
- package/src/zcrypto.js +160 -160
package/src/riverClient.js
CHANGED
|
@@ -1,329 +1,372 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* River AI(console.river.ai)网页控制台余额抓取(联调:$25 免费额度卡)。
|
|
3
|
-
*
|
|
4
|
-
* 凭据 = WorkOS SPA 会话(localStorage `workos:access-token` / `workos:refresh-token` /
|
|
5
|
-
* `workos:expires-at` / `workos:user`)——console API 只认 `Authorization: Bearer <WorkOS JWT>`,
|
|
6
|
-
* cookie 无关。`rv_` 开头的 API key 只能打 api.river.ai 的 OpenAI 兼容面(/v1/models 等),
|
|
7
|
-
* **看不到余额**(/v1/billing 等是 gRPC UNIMPLEMENTED),所以走网页会话线
|
|
8
|
-
* (issue #23 收割线的 localStorage 变体,desktop 侧按 site.localStorageKeys 收割)。
|
|
9
|
-
*
|
|
10
|
-
* 余额:GET /api/billing/account → { balance_usd: number, ... }(联调实测消费口径)。
|
|
11
|
-
* 刷新:POST https://auth.console.river.ai/user_management/authenticate
|
|
12
|
-
* body { client_id, grant_type: "refresh_token", refresh_token }——WorkOS 单次轮换:
|
|
13
|
-
* 刷新成功旧 refresh 立即作废,新链必须当场原子落盘(与 minimaxClient 同款防护,
|
|
14
|
-
* 绝不依赖调用方传回调)。client_id 公开(GET /api/auth/config → workos_client_id)。
|
|
15
|
-
*
|
|
16
|
-
* 安全纪律:会话 token 只存本机账号库(0600),推送到 10Router 的载荷只带余额数字。
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { fetchJsonRace } from './zcodeClient.js';
|
|
20
|
-
import { FETCH_TIMEOUT_MS } from './constants.js';
|
|
21
|
-
import { t } from './i18n.js';
|
|
22
|
-
import { snapshotAgeTag } from './externalQuota.js';
|
|
23
|
-
|
|
24
|
-
export const RIVER_CONSOLE_BASE = 'https://console.river.ai';
|
|
25
|
-
export const RIVER_BILLING_URL = `${RIVER_CONSOLE_BASE}/api/billing/account`;
|
|
26
|
-
export const RIVER_AUTH_CONFIG_URL = `${RIVER_CONSOLE_BASE}/api/auth/config`;
|
|
27
|
-
/** WorkOS AuthKit SPA 的 localStorage 键(desktop 收割按此名单读) */
|
|
28
|
-
export const RIVER_STORAGE_KEYS = ['workos:access-token', 'workos:refresh-token', 'workos:expires-at', 'workos:user'];
|
|
29
|
-
/** WorkOS 刷新端点(console 配置 workos_api_hostname = auth.console.river.ai) */
|
|
30
|
-
export const RIVER_WORKOS_AUTH_BASE = 'https://auth.console.river.ai';
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (sess?.
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
//
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
1
|
+
/**
|
|
2
|
+
* River AI(console.river.ai)网页控制台余额抓取(联调:$25 免费额度卡)。
|
|
3
|
+
*
|
|
4
|
+
* 凭据 = WorkOS SPA 会话(localStorage `workos:access-token` / `workos:refresh-token` /
|
|
5
|
+
* `workos:expires-at` / `workos:user`)——console API 只认 `Authorization: Bearer <WorkOS JWT>`,
|
|
6
|
+
* cookie 无关。`rv_` 开头的 API key 只能打 api.river.ai 的 OpenAI 兼容面(/v1/models 等),
|
|
7
|
+
* **看不到余额**(/v1/billing 等是 gRPC UNIMPLEMENTED),所以走网页会话线
|
|
8
|
+
* (issue #23 收割线的 localStorage 变体,desktop 侧按 site.localStorageKeys 收割)。
|
|
9
|
+
*
|
|
10
|
+
* 余额:GET /api/billing/account → { balance_usd: number, ... }(联调实测消费口径)。
|
|
11
|
+
* 刷新:POST https://auth.console.river.ai/user_management/authenticate
|
|
12
|
+
* body { client_id, grant_type: "refresh_token", refresh_token }——WorkOS 单次轮换:
|
|
13
|
+
* 刷新成功旧 refresh 立即作废,新链必须当场原子落盘(与 minimaxClient 同款防护,
|
|
14
|
+
* 绝不依赖调用方传回调)。client_id 公开(GET /api/auth/config → workos_client_id)。
|
|
15
|
+
*
|
|
16
|
+
* 安全纪律:会话 token 只存本机账号库(0600),推送到 10Router 的载荷只带余额数字。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { fetchJsonRace } from './zcodeClient.js';
|
|
20
|
+
import { FETCH_TIMEOUT_MS } from './constants.js';
|
|
21
|
+
import { t } from './i18n.js';
|
|
22
|
+
import { snapshotAgeTag } from './externalQuota.js';
|
|
23
|
+
|
|
24
|
+
export const RIVER_CONSOLE_BASE = 'https://console.river.ai';
|
|
25
|
+
export const RIVER_BILLING_URL = `${RIVER_CONSOLE_BASE}/api/billing/account`;
|
|
26
|
+
export const RIVER_AUTH_CONFIG_URL = `${RIVER_CONSOLE_BASE}/api/auth/config`;
|
|
27
|
+
/** WorkOS AuthKit SPA 的 localStorage 键(desktop 收割按此名单读) */
|
|
28
|
+
export const RIVER_STORAGE_KEYS = ['workos:access-token', 'workos:refresh-token', 'workos:expires-at', 'workos:user'];
|
|
29
|
+
/** WorkOS 刷新端点(console 配置 workos_api_hostname = auth.console.river.ai) */
|
|
30
|
+
export const RIVER_WORKOS_AUTH_BASE = 'https://auth.console.river.ai';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 从 JWT payload 的 exp 提取绝对到期时间(ISO 字符串)。
|
|
34
|
+
* @param {string} token
|
|
35
|
+
* @returns {string|null}
|
|
36
|
+
*/
|
|
37
|
+
export function jwtExpiresAt(token) {
|
|
38
|
+
try {
|
|
39
|
+
const part = String(token || '').split('.')[1];
|
|
40
|
+
if (!part) return null;
|
|
41
|
+
const payload = JSON.parse(Buffer.from(part, 'base64url').toString('utf8'));
|
|
42
|
+
return Number.isFinite(payload?.exp) ? new Date(payload.exp * 1000).toISOString() : null;
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 判断 Bearer token 是否已过期。
|
|
50
|
+
* 留 60 秒容差:WorkOS 上游有时钟容差(实测过期 25 秒仍返 200);
|
|
51
|
+
* 过早判定会白刷一次单次轮换的 refresh 链(多消耗一次轮换,且有与浏览器抢刷新的风险)。
|
|
52
|
+
* 只有在已过期且超过 60 秒容差后才视为过期。
|
|
53
|
+
*/
|
|
54
|
+
export function isBearerExpired(expiresAt) {
|
|
55
|
+
if (!expiresAt) return false;
|
|
56
|
+
const t = Date.parse(expiresAt);
|
|
57
|
+
if (!Number.isFinite(t)) return false;
|
|
58
|
+
return Date.now() > t + 60_000;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const RIVER_UA =
|
|
62
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
|
|
63
|
+
|
|
64
|
+
function riverHeaders(token) {
|
|
65
|
+
return {
|
|
66
|
+
Accept: 'application/json, text/plain, */*',
|
|
67
|
+
Authorization: `Bearer ${String(token || '').trim()}`,
|
|
68
|
+
Referer: `${RIVER_CONSOLE_BASE}/billing`,
|
|
69
|
+
'User-Agent': RIVER_UA,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 解析 desktop 收割到的 localStorage 快照 → WorkOS 会话对象。
|
|
75
|
+
* 实测(2026-10-14 联调复盘):console SPA 的 WorkOS AuthKit 把 token 放**内存 Map**
|
|
76
|
+
* (`Co`),localStorage 通常为空;持久会话走 **cookie 通道**(`WORKOS_REFRESH_SESSION`)。
|
|
77
|
+
* 键名仍按标准四键 + 前缀扫描兜底(dev 模式 / 旧版 SDK 可能写盘;refresh 键带 clientId 后缀)。
|
|
78
|
+
* @param {Record<string, string|null>} storage 键值均为 localStorage 原文(user 为 JSON 串)
|
|
79
|
+
* @returns {{ accessToken: string|null, refreshToken: string|null, expiresAt: string|null, user: {id?: string, email?: string, name?: string} }}
|
|
80
|
+
*/
|
|
81
|
+
export function parseRiverSession(storage) {
|
|
82
|
+
const s = storage && typeof storage === 'object' ? storage : {};
|
|
83
|
+
const pick = (k) => {
|
|
84
|
+
const v = s[k];
|
|
85
|
+
return typeof v === 'string' && v.trim() ? v.trim() : null;
|
|
86
|
+
};
|
|
87
|
+
// refresh 键可能是 workos:refresh-token 或 workos:refresh-token:<clientId>(f5() 拼键)
|
|
88
|
+
const refresh = pick('workos:refresh-token')
|
|
89
|
+
|| Object.entries(s).find(([k, v]) => k.startsWith('workos:refresh-token:') && typeof v === 'string' && v.trim())?.[1]?.trim()
|
|
90
|
+
|| null;
|
|
91
|
+
let user = {};
|
|
92
|
+
try {
|
|
93
|
+
const raw = pick('workos:user');
|
|
94
|
+
if (raw) {
|
|
95
|
+
const u = JSON.parse(raw);
|
|
96
|
+
user = {
|
|
97
|
+
id: u.id || u.user_id || null,
|
|
98
|
+
email: u.email || null,
|
|
99
|
+
name: [u.firstName || u.first_name, u.lastName || u.last_name].filter(Boolean).join(' ') || u.name || null,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
} catch { /* user 解析失败不影响 token 可用性 */ }
|
|
103
|
+
const at = pick('workos:access-token');
|
|
104
|
+
const exp = pick('workos:expires-at') || jwtExpiresAt(at);
|
|
105
|
+
return {
|
|
106
|
+
accessToken: at,
|
|
107
|
+
refreshToken: refresh,
|
|
108
|
+
expiresAt: exp,
|
|
109
|
+
user,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 账号 → 会话对象(meta.workosSession 为准,token 字段兜底 accessToken,cookie 取 cookieSiteSession) */
|
|
114
|
+
export function riverSessionOf(account) {
|
|
115
|
+
const cookie = account?.meta?.workosSession?.cookie || account?.meta?.cookieSiteSession?.cookie || '';
|
|
116
|
+
const sess = account?.meta?.workosSession;
|
|
117
|
+
if (sess && (sess.accessToken || account?.token || cookie)) {
|
|
118
|
+
const accessToken = sess.accessToken || account?.token || null;
|
|
119
|
+
const expiresAt = sess.expiresAt || account?.expiresAt || jwtExpiresAt(accessToken);
|
|
120
|
+
return { ...sess, accessToken, expiresAt, cookie: sess.cookie || cookie };
|
|
121
|
+
}
|
|
122
|
+
const accessToken = account?.token || null;
|
|
123
|
+
const expiresAt = account?.expiresAt || jwtExpiresAt(accessToken);
|
|
124
|
+
return (account?.token || cookie)
|
|
125
|
+
? { accessToken, refreshToken: null, expiresAt, user: account?.meta?.workosUser || {}, cookie }
|
|
126
|
+
: null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** 从 billing/account 响应里提取余额数字(宽容解析,联调字段 evolution 兜底) */
|
|
130
|
+
export function parseRiverBalance(body) {
|
|
131
|
+
const d = body?.data || body?.result || body || {};
|
|
132
|
+
const cand = [d.balance_usd, d.balanceUsd, d.balance, d.available_balance_usd, d.availableBalance, body?.balance_usd];
|
|
133
|
+
for (const v of cand) {
|
|
134
|
+
const n = Number(v);
|
|
135
|
+
if (Number.isFinite(n) && n >= 0) return Math.round(n * 100) / 100;
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 刷新 WorkOS 会话(单次轮换)。两种授予:
|
|
142
|
+
* 1) body `refresh_token`(有 refreshToken 链时);
|
|
143
|
+
* 2) **cookie 授予**(useCookie 语义,SPA 重载续期的真实通道):不带 refresh_token 参数,
|
|
144
|
+
* 凭 `WORKOS_REFRESH_SESSION` 等会话 cookie 换新链。
|
|
145
|
+
* **只要账号有 id 就无条件原子落盘**——旧 refresh 刷新成功即作废(MiniMax 同款教训)。
|
|
146
|
+
*/
|
|
147
|
+
export async function refreshRiverSession(account, { fetchImpl, log } = {}) {
|
|
148
|
+
const sess = riverSessionOf(account);
|
|
149
|
+
if (!sess?.refreshToken && !sess?.cookie) {
|
|
150
|
+
const err = new Error('River 会话无 refreshToken/会话 cookie,请重新登录捕获');
|
|
151
|
+
err.authInvalid = true;
|
|
152
|
+
throw err;
|
|
153
|
+
}
|
|
154
|
+
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS, method: init.method, body: init.body }));
|
|
155
|
+
// client_id 公开可查;失败用空(WorkOS 会报错,上层归为刷新失败)
|
|
156
|
+
let clientId = sess.clientId || null;
|
|
157
|
+
if (!clientId) {
|
|
158
|
+
try {
|
|
159
|
+
const r = await doFetch(RIVER_AUTH_CONFIG_URL, { headers: { Accept: 'application/json', 'User-Agent': RIVER_UA } });
|
|
160
|
+
const cfg = await r.json();
|
|
161
|
+
clientId = cfg?.workos_client_id || null;
|
|
162
|
+
if (cfg?.workos_api_hostname) sess.authBase = `https://${cfg.workos_api_hostname}`;
|
|
163
|
+
} catch { /* 用默认端点重试一次 */ }
|
|
164
|
+
}
|
|
165
|
+
const authBase = sess.authBase || RIVER_WORKOS_AUTH_BASE;
|
|
166
|
+
const res = await doFetch(`${authBase}/user_management/authenticate`, {
|
|
167
|
+
method: 'POST',
|
|
168
|
+
headers: {
|
|
169
|
+
'Content-Type': 'application/json',
|
|
170
|
+
Accept: 'application/json',
|
|
171
|
+
'User-Agent': RIVER_UA,
|
|
172
|
+
...(sess.cookie ? { Cookie: String(sess.cookie) } : {}),
|
|
173
|
+
},
|
|
174
|
+
body: JSON.stringify({
|
|
175
|
+
client_id: clientId,
|
|
176
|
+
grant_type: 'refresh_token',
|
|
177
|
+
...(sess.refreshToken ? { refresh_token: sess.refreshToken } : {}), // cookie 授予时省略(SDK useCookie 同款)
|
|
178
|
+
}),
|
|
179
|
+
});
|
|
180
|
+
const data = await res.json().catch(() => null);
|
|
181
|
+
if (!res.ok || !data?.access_token) {
|
|
182
|
+
const err = new Error(`River 会话刷新失败(HTTP ${res.status}${data?.error_description || data?.error ? ':' + (data.error_description || data.error) : ''})`);
|
|
183
|
+
if (res.status === 400 || res.status === 401 || res.status === 403) err.authInvalid = true;
|
|
184
|
+
throw err;
|
|
185
|
+
}
|
|
186
|
+
const parsedUser = parseWorkOsUser(data.user) || sess.user;
|
|
187
|
+
const expiresAt = Number.isFinite(data.expires_in)
|
|
188
|
+
? new Date(Date.now() + data.expires_in * 1000).toISOString()
|
|
189
|
+
: jwtExpiresAt(data.access_token);
|
|
190
|
+
const creds = {
|
|
191
|
+
accessToken: data.access_token,
|
|
192
|
+
refreshToken: data.refresh_token || sess.refreshToken || null, // 无新 refresh 时保留旧链
|
|
193
|
+
expiresAt,
|
|
194
|
+
clientId: clientId || undefined,
|
|
195
|
+
authBase: authBase || undefined,
|
|
196
|
+
user: parsedUser,
|
|
197
|
+
};
|
|
198
|
+
// 无条件落盘兜底(单次轮换防丢链)
|
|
199
|
+
if (account?.id) {
|
|
200
|
+
try {
|
|
201
|
+
const { withAccounts } = await import('./store.js');
|
|
202
|
+
await withAccounts((list) => {
|
|
203
|
+
const cur = list.find((it) => it.id === account.id);
|
|
204
|
+
if (cur) {
|
|
205
|
+
cur.token = creds.accessToken;
|
|
206
|
+
cur.expiresAt = creds.expiresAt;
|
|
207
|
+
cur.meta = {
|
|
208
|
+
...(cur.meta || {}),
|
|
209
|
+
workosSession: { ...(cur.meta?.workosSession || {}), ...creds },
|
|
210
|
+
};
|
|
211
|
+
cur.updatedAt = new Date().toISOString();
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
} catch (saveErr) {
|
|
215
|
+
log?.(`River 刷新凭据落盘失败: ${saveErr.message}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// 同步内存对象,调用方即便不落盘也拿到新链
|
|
219
|
+
if (account) {
|
|
220
|
+
account.token = creds.accessToken;
|
|
221
|
+
account.refreshToken = creds.refreshToken;
|
|
222
|
+
account.expiresAt = creds.expiresAt;
|
|
223
|
+
if (account.meta) account.meta.workosSession = { ...(account.meta.workosSession || {}), ...creds };
|
|
224
|
+
}
|
|
225
|
+
return creds;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function parseWorkOsUser(u) {
|
|
229
|
+
if (!u || typeof u !== 'object') return null;
|
|
230
|
+
return {
|
|
231
|
+
id: u.id || u.user_id || null,
|
|
232
|
+
email: u.email || null,
|
|
233
|
+
name: [u.firstName || u.first_name, u.lastName || u.last_name].filter(Boolean).join(' ') || u.name || null,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 读取 River 控制台余额。按 WorkOS SPA 的真实续期链尝试三通道:
|
|
239
|
+
* 1) 会话 cookie 直连(同源 API 自动带 cookie,无需 Bearer——别混发失效 Bearer 触发
|
|
240
|
+
* "invalid WorkOS access token");
|
|
241
|
+
* 2) Bearer accessToken(内存/写盘通道的残留);
|
|
242
|
+
* 3) cookie/refresh 兑换新 access token(单次轮换落盘)→ Bearer 重试。
|
|
243
|
+
* @returns {Promise<{ok: boolean, balance?: number, raw?: object, error?: string, status?: number, channel?: string, session?: object}>}
|
|
244
|
+
*/
|
|
245
|
+
export async function fetchRiverBalance(account, { fetchImpl, log } = {}) {
|
|
246
|
+
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS }));
|
|
247
|
+
const sess = riverSessionOf(account);
|
|
248
|
+
const cookie = sess?.cookie || account?.meta?.cookieSiteSession?.cookie || '';
|
|
249
|
+
if (!sess?.accessToken && !cookie) {
|
|
250
|
+
return { ok: false, error: 'River 会话缺失(storage token 与会话 cookie 均无),请重新登录捕获' };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const attempt = async ({ token = null, useCookie = false }) => {
|
|
254
|
+
const headers = {
|
|
255
|
+
Accept: 'application/json, text/plain, */*',
|
|
256
|
+
Referer: `${RIVER_CONSOLE_BASE}/billing`,
|
|
257
|
+
'User-Agent': RIVER_UA,
|
|
258
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
259
|
+
...(useCookie && cookie ? { Cookie: String(cookie) } : {}),
|
|
260
|
+
};
|
|
261
|
+
const res = await doFetch(RIVER_BILLING_URL, { headers });
|
|
262
|
+
if (res.status === 401 || res.status === 403) return { ok: false, status: res.status, authExpired: true, error: `会话失效(HTTP ${res.status})` };
|
|
263
|
+
if (!res.ok) return { ok: false, status: res.status, error: `HTTP ${res.status}` };
|
|
264
|
+
const body = await res.json().catch(() => null);
|
|
265
|
+
const balance = parseRiverBalance(body);
|
|
266
|
+
if (balance === null) return { ok: false, status: res.status, error: '未能从响应中提取余额(balance_usd)', raw: body };
|
|
267
|
+
return { ok: true, balance, raw: body };
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// 1) cookie 直连
|
|
271
|
+
const misses = [];
|
|
272
|
+
let authExpired = false;
|
|
273
|
+
if (cookie) {
|
|
274
|
+
const r = await attempt({ useCookie: true }).catch((e) => ({ ok: false, error: e.message || String(e) }));
|
|
275
|
+
if (r.ok) return { ...r, channel: 'cookie' };
|
|
276
|
+
if (r.authExpired) authExpired = true;
|
|
277
|
+
misses.push(`cookie:${r.status || r.error || '?'}`);
|
|
278
|
+
}
|
|
279
|
+
// 2) Bearer:本地已知过期(留 60s 容差)直接跳过,避免每轮无谓 401 浪费网络
|
|
280
|
+
const bearerExpired = isBearerExpired(sess?.expiresAt);
|
|
281
|
+
if (sess?.accessToken) {
|
|
282
|
+
if (bearerExpired) {
|
|
283
|
+
misses.push(t('bearer:过期跳过'));
|
|
284
|
+
} else {
|
|
285
|
+
const r = await attempt({ token: sess.accessToken }).catch((e) => ({ ok: false, error: e.message || String(e) }));
|
|
286
|
+
if (r.ok) return { ...r, channel: 'bearer' };
|
|
287
|
+
if (r.authExpired) authExpired = true;
|
|
288
|
+
misses.push(`bearer:${r.status || r.error || '?'}`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// 3) 刷新兑换(cookie 授予或 refreshToken 授予)→ Bearer 重试
|
|
292
|
+
if (sess?.refreshToken || cookie) {
|
|
293
|
+
try {
|
|
294
|
+
log?.(t('River 直连未过({misses}),正在兑换新 access token…', { misses: misses.join(' / ') || '—' }));
|
|
295
|
+
const creds = await refreshRiverSession(account?.id ? account : { meta: { workosSession: { ...sess, cookie } } }, { fetchImpl, log });
|
|
296
|
+
const r = await attempt({ token: creds.accessToken, useCookie: true }).catch((e) => ({ ok: false, error: e.message || String(e) }));
|
|
297
|
+
if (r.ok) return { ...r, channel: 'refresh', session: creds };
|
|
298
|
+
if (r.authExpired) authExpired = true;
|
|
299
|
+
return { ok: false, status: r.status, authExpired, error: `兑换成功但读余额仍失败:${r.error || ''}` };
|
|
300
|
+
} catch (e) {
|
|
301
|
+
// 刷新端点拒绝(refresh 链已死 / 会话注销)= 明确 auth 失效;网络类错误维持通道实测结论
|
|
302
|
+
return { ok: false, status: 401, authExpired: e?.authInvalid === true || authExpired, error: `River 会话刷新失败:${e.message}` };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return { ok: false, status: 401, authExpired, error: 'River 会话全部通道未通过(cookie/Bearer/刷新兑换),请重新登录捕获' };
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 组装单个 River 账号的 10Router external-quota 载荷(储值余额卡,与 MiMo 卡同口径:
|
|
310
|
+
* 汇总行 summarizeDetail 不进逐包明细——单值余额没有可折叠的明细)。
|
|
311
|
+
* @param {object} account
|
|
312
|
+
* @param {{ balance?: number, error?: string, authInvalid?: boolean }} [liveData]
|
|
313
|
+
* authInvalid = live 拉取(含刷新兑换链)明确会话失效——不回退旧快照,降级 message 状态卡
|
|
314
|
+
* @returns {{ source: string, payload: object }}
|
|
315
|
+
*/
|
|
316
|
+
export function buildRiverQuotaPayload(account, liveData = null) {
|
|
317
|
+
const sess = account?.meta?.workosSession || {};
|
|
318
|
+
const userId = account?.uid || sess.user?.id || '';
|
|
319
|
+
const short = String(userId).toLowerCase().replace(/[^a-z0-9-]/g, '').slice(0, 24);
|
|
320
|
+
const source = short ? `river-${short}`.slice(0, 32) : `river-${(account?.id || 'default').slice(0, 24)}`;
|
|
321
|
+
const nameStr = String(account?.name || '');
|
|
322
|
+
// 邮箱优先取 WorkOS 会话;兜底从账号名里抽(名字本身就是「River AI (邮箱)」形状,
|
|
323
|
+
// 直接整体拼进括号会套成 River AI (River AI (…)) 双层壳)
|
|
324
|
+
const email = sess.user?.email || /[\w.+-]+@[\w-]+\.[\w.]+/.exec(nameStr)?.[0] || '';
|
|
325
|
+
|
|
326
|
+
if (liveData?.authInvalid) {
|
|
327
|
+
return {
|
|
328
|
+
source,
|
|
329
|
+
payload: {
|
|
330
|
+
provider: 'river',
|
|
331
|
+
label: (nameStr || (email ? `River AI (${email})` : 'River AI')).slice(0, 100),
|
|
332
|
+
message: liveData.error || t('River 控制台会话已失效,请在 CreditDaddy「网页会话」页重新登录捕获'),
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
let balance = liveData?.balance;
|
|
338
|
+
let ageTag = '';
|
|
339
|
+
if (!Number.isFinite(balance)) {
|
|
340
|
+
const snap = account?.meta?.quotaSnapshot;
|
|
341
|
+
const rb = account?.meta?.riverBalance;
|
|
342
|
+
balance = Number(snap?.total ?? rb?.balanceUsd);
|
|
343
|
+
// 快照回退(非 live):距今 >24h 的陈粮在副标题标注数据时刻(可见性兜底)
|
|
344
|
+
ageTag = snapshotAgeTag(snap?.at ? snap : rb);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// label = 账号别名(用户可改)——10R 侧「同名认领」的匹配键:与连接同名即把余额挂上连接卡
|
|
348
|
+
const label = (nameStr || (email ? `River AI (${email})` : 'River AI')).slice(0, 100);
|
|
349
|
+
const payload = {
|
|
350
|
+
provider: 'river',
|
|
351
|
+
label,
|
|
352
|
+
plan: t('免费额度余额') + ageTag,
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
if (Number.isFinite(balance) && balance >= 0) {
|
|
356
|
+
payload.quotas = {
|
|
357
|
+
Balance: {
|
|
358
|
+
name: 'Balance (USD)',
|
|
359
|
+
used: 0,
|
|
360
|
+
total: Math.max(0, balance) || 0.000001,
|
|
361
|
+
remainingPercentage: balance > 0 ? 100 : 0,
|
|
362
|
+
displayRemaining: true,
|
|
363
|
+
isCreditBalance: true,
|
|
364
|
+
summarizesDetail: true,
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
} else {
|
|
368
|
+
payload.message = liveData?.error || t('River 控制台会话已失效,请在 CreditDaddy「网页会话」页重新登录捕获');
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return { source, payload };
|
|
372
|
+
}
|