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.
- package/README.md +440 -423
- package/build.json +3 -3
- package/package.json +2 -2
- package/src/authDevice.js +7 -0
- package/src/buildInfo.js +33 -0
- package/src/catpawLocal.js +8 -1
- package/src/checkin.js +7 -3
- package/src/constants.js +8 -4
- package/src/cookieSites.js +14 -0
- package/src/daemon.js +2443 -2311
- package/src/externalQuota.js +44 -1
- package/src/i18n/en.json +113 -24
- package/src/i18n/ja.json +113 -24
- package/src/i18n/ko.json +113 -24
- package/src/i18n/zh-TW.json +113 -24
- package/src/lingxiClient.js +373 -0
- package/src/loomyClient.js +58 -0
- package/src/loomyLocal.js +7 -1
- package/src/minimaxAuth.js +17 -13
- package/src/minimaxClient.js +136 -37
- package/src/minimaxGateway.js +11 -5
- package/src/minimaxLocal.js +344 -304
- package/src/mirasimLocal.js +392 -386
- package/src/panel.html +365 -82
- package/src/providers.js +30 -5
- package/src/qoderApp.js +578 -550
- package/src/riverClient.js +55 -12
- package/src/store.js +1 -0
- package/src/tenrouterAccounts.js +232 -211
- package/src/traeAuth.js +599 -0
- package/src/traeClient.js +25 -2
- package/src/traeGateway.js +104 -37
- package/src/traeLocal.js +371 -347
- package/src/transfer.js +20 -1
- package/src/winProc.js +134 -36
- package/src/workbuddyLocal.js +35 -4
- package/src/zcodeAutoClaim.js +25 -6
- package/src/zcodeClient.js +18 -0
- package/src/zcodeGateway.js +38 -0
- package/src/zcodeLocal.js +340 -334
package/src/riverClient.js
CHANGED
|
@@ -29,6 +29,35 @@ export const RIVER_STORAGE_KEYS = ['workos:access-token', 'workos:refresh-token'
|
|
|
29
29
|
/** WorkOS 刷新端点(console 配置 workos_api_hostname = auth.console.river.ai) */
|
|
30
30
|
export const RIVER_WORKOS_AUTH_BASE = 'https://auth.console.river.ai';
|
|
31
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
|
+
|
|
32
61
|
const RIVER_UA =
|
|
33
62
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
|
|
34
63
|
|
|
@@ -71,10 +100,12 @@ export function parseRiverSession(storage) {
|
|
|
71
100
|
};
|
|
72
101
|
}
|
|
73
102
|
} catch { /* user 解析失败不影响 token 可用性 */ }
|
|
103
|
+
const at = pick('workos:access-token');
|
|
104
|
+
const exp = pick('workos:expires-at') || jwtExpiresAt(at);
|
|
74
105
|
return {
|
|
75
|
-
accessToken:
|
|
106
|
+
accessToken: at,
|
|
76
107
|
refreshToken: refresh,
|
|
77
|
-
expiresAt:
|
|
108
|
+
expiresAt: exp,
|
|
78
109
|
user,
|
|
79
110
|
};
|
|
80
111
|
}
|
|
@@ -84,10 +115,14 @@ export function riverSessionOf(account) {
|
|
|
84
115
|
const cookie = account?.meta?.workosSession?.cookie || account?.meta?.cookieSiteSession?.cookie || '';
|
|
85
116
|
const sess = account?.meta?.workosSession;
|
|
86
117
|
if (sess && (sess.accessToken || account?.token || cookie)) {
|
|
87
|
-
|
|
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 };
|
|
88
121
|
}
|
|
122
|
+
const accessToken = account?.token || null;
|
|
123
|
+
const expiresAt = account?.expiresAt || jwtExpiresAt(accessToken);
|
|
89
124
|
return (account?.token || cookie)
|
|
90
|
-
? { accessToken
|
|
125
|
+
? { accessToken, refreshToken: null, expiresAt, user: account?.meta?.workosUser || {}, cookie }
|
|
91
126
|
: null;
|
|
92
127
|
}
|
|
93
128
|
|
|
@@ -149,12 +184,13 @@ export async function refreshRiverSession(account, { fetchImpl, log } = {}) {
|
|
|
149
184
|
throw err;
|
|
150
185
|
}
|
|
151
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);
|
|
152
190
|
const creds = {
|
|
153
191
|
accessToken: data.access_token,
|
|
154
192
|
refreshToken: data.refresh_token || sess.refreshToken || null, // 无新 refresh 时保留旧链
|
|
155
|
-
expiresAt
|
|
156
|
-
? new Date(Date.now() + data.expires_in * 1000).toISOString()
|
|
157
|
-
: null,
|
|
193
|
+
expiresAt,
|
|
158
194
|
clientId: clientId || undefined,
|
|
159
195
|
authBase: authBase || undefined,
|
|
160
196
|
user: parsedUser,
|
|
@@ -167,6 +203,7 @@ export async function refreshRiverSession(account, { fetchImpl, log } = {}) {
|
|
|
167
203
|
const cur = list.find((it) => it.id === account.id);
|
|
168
204
|
if (cur) {
|
|
169
205
|
cur.token = creds.accessToken;
|
|
206
|
+
cur.expiresAt = creds.expiresAt;
|
|
170
207
|
cur.meta = {
|
|
171
208
|
...(cur.meta || {}),
|
|
172
209
|
workosSession: { ...(cur.meta?.workosSession || {}), ...creds },
|
|
@@ -182,6 +219,7 @@ export async function refreshRiverSession(account, { fetchImpl, log } = {}) {
|
|
|
182
219
|
if (account) {
|
|
183
220
|
account.token = creds.accessToken;
|
|
184
221
|
account.refreshToken = creds.refreshToken;
|
|
222
|
+
account.expiresAt = creds.expiresAt;
|
|
185
223
|
if (account.meta) account.meta.workosSession = { ...(account.meta.workosSession || {}), ...creds };
|
|
186
224
|
}
|
|
187
225
|
return creds;
|
|
@@ -238,12 +276,17 @@ export async function fetchRiverBalance(account, { fetchImpl, log } = {}) {
|
|
|
238
276
|
if (r.authExpired) authExpired = true;
|
|
239
277
|
misses.push(`cookie:${r.status || r.error || '?'}`);
|
|
240
278
|
}
|
|
241
|
-
// 2) Bearer
|
|
279
|
+
// 2) Bearer:本地已知过期(留 60s 容差)直接跳过,避免每轮无谓 401 浪费网络
|
|
280
|
+
const bearerExpired = isBearerExpired(sess?.expiresAt);
|
|
242
281
|
if (sess?.accessToken) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
+
}
|
|
247
290
|
}
|
|
248
291
|
// 3) 刷新兑换(cookie 授予或 refreshToken 授予)→ Bearer 重试
|
|
249
292
|
if (sess?.refreshToken || cookie) {
|
package/src/store.js
CHANGED
|
@@ -247,6 +247,7 @@ export function publicAccount(a) {
|
|
|
247
247
|
(a.provider.startsWith('workbuddy') && Boolean(a.token)) ||
|
|
248
248
|
a.provider === 'catpaw' ||
|
|
249
249
|
a.provider === 'trae' ||
|
|
250
|
+
a.provider === 'trae-intl' ||
|
|
250
251
|
a.provider === 'loomy'
|
|
251
252
|
),
|
|
252
253
|
canSwitchWorkbuddyClient: Boolean(meta.session?.account || (a.provider.startsWith('workbuddy') && Boolean(a.token))),
|
package/src/tenrouterAccounts.js
CHANGED
|
@@ -1,211 +1,232 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CreditDaddy 账号 → 10Router 连接同步:把本机管理的订阅账号推给 10Router 当上游连接。
|
|
3
|
-
*
|
|
4
|
-
* 通道一(推荐):POST /api/oauth/transfer/import —— 需在 10Router 设置里配置「面板密码」。
|
|
5
|
-
* 完整 OAuth 形态(accessToken + refreshToken),10R 端按 JWT sub 去重并原位更新,反复同步幂等;
|
|
6
|
-
* 先用面板密码登录(/api/auth/login)换 auth_token 会话再推送——transfer 路由只认会话 / CLI token。
|
|
7
|
-
* 通道二(默认):POST /api/providers —— 仅用已配的 sk- 虚拟 key。
|
|
8
|
-
* dashboardGuard 为远程 agent 留的口子(POST /api/providers 认虚拟 key);apikey 形态、
|
|
9
|
-
* 不带 refreshToken,token 过期后需重新同步;同名连接返回 409 且不覆盖。
|
|
10
|
-
*
|
|
11
|
-
* 扩展新产品:在 TENROUTER_PROVIDER_MAP 加一行即可(如 zcode → glm 等反代就绪后补上),
|
|
12
|
-
* 同步主流程只读这张表,无需改动其他代码。
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import crypto from 'node:crypto';
|
|
16
|
-
import { buildExportPayload, sealTransfer } from './transfer.js';
|
|
17
|
-
|
|
18
|
-
/** CreditDaddy provider → 10Router 连接类型。留空 / 缺失 = 该产品暂不同步。 */
|
|
19
|
-
export const TENROUTER_PROVIDER_MAP = {
|
|
20
|
-
qoder: 'qoder',
|
|
21
|
-
'qoder-cn': 'qoder-cn',
|
|
22
|
-
workbuddy: 'codebuddy-cn',
|
|
23
|
-
'workbuddy-intl': 'codebuddy-intl',
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
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
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
if (
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
if (
|
|
206
|
-
if (
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
1
|
+
/**
|
|
2
|
+
* CreditDaddy 账号 → 10Router 连接同步:把本机管理的订阅账号推给 10Router 当上游连接。
|
|
3
|
+
*
|
|
4
|
+
* 通道一(推荐):POST /api/oauth/transfer/import —— 需在 10Router 设置里配置「面板密码」。
|
|
5
|
+
* 完整 OAuth 形态(accessToken + refreshToken),10R 端按 JWT sub 去重并原位更新,反复同步幂等;
|
|
6
|
+
* 先用面板密码登录(/api/auth/login)换 auth_token 会话再推送——transfer 路由只认会话 / CLI token。
|
|
7
|
+
* 通道二(默认):POST /api/providers —— 仅用已配的 sk- 虚拟 key。
|
|
8
|
+
* dashboardGuard 为远程 agent 留的口子(POST /api/providers 认虚拟 key);apikey 形态、
|
|
9
|
+
* 不带 refreshToken,token 过期后需重新同步;同名连接返回 409 且不覆盖。
|
|
10
|
+
*
|
|
11
|
+
* 扩展新产品:在 TENROUTER_PROVIDER_MAP 加一行即可(如 zcode → glm 等反代就绪后补上),
|
|
12
|
+
* 同步主流程只读这张表,无需改动其他代码。
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import crypto from 'node:crypto';
|
|
16
|
+
import { buildExportPayload, sealTransfer } from './transfer.js';
|
|
17
|
+
|
|
18
|
+
/** CreditDaddy provider → 10Router 连接类型。留空 / 缺失 = 该产品暂不同步。 */
|
|
19
|
+
export const TENROUTER_PROVIDER_MAP = {
|
|
20
|
+
qoder: 'qoder',
|
|
21
|
+
'qoder-cn': 'qoder-cn',
|
|
22
|
+
workbuddy: 'codebuddy-cn',
|
|
23
|
+
'workbuddy-intl': 'codebuddy-intl',
|
|
24
|
+
minimax: 'minimax-code', // #55:MiniMax 国内订阅线 → 10R minimax-code
|
|
25
|
+
'minimax-global': 'minimax-code-global', // #55:MiniMax 国际订阅线 → 10R minimax-code-global
|
|
26
|
+
trae: 'trae-cn', // #56:CD 的 Trae 线读国内客户端(#28)→ 10R trae-cn(条目钉死国内站)
|
|
27
|
+
'trae-intl': 'trae', // 国际线(浏览器 OAuth 登录,10R trae 条目缺省国际站);连接同步走 transfer 管线
|
|
28
|
+
tokenharbor: 'tokenharbor-free', // #23:会话 cookie 只喂「额度监控卡」(freeTier,不走路由);tokenharbor 钱包卡留给 sk- 运输 key
|
|
29
|
+
loomy: 'loomy',
|
|
30
|
+
// zcode → glm:等 zcode 反代就绪后补一行(同步流程已就绪,无需改代码)
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const mapProviderId = (provider) => TENROUTER_PROVIDER_MAP[provider] || null;
|
|
34
|
+
|
|
35
|
+
const TIMEOUT_MS = 60_000;
|
|
36
|
+
|
|
37
|
+
async function trFetch(url, { key, cookie, method = 'GET', body } = {}) {
|
|
38
|
+
let res;
|
|
39
|
+
try {
|
|
40
|
+
res = await fetch(url, {
|
|
41
|
+
method,
|
|
42
|
+
headers: {
|
|
43
|
+
Accept: 'application/json',
|
|
44
|
+
...(body ? { 'Content-Type': 'application/json' } : {}),
|
|
45
|
+
...(key ? { Authorization: `Bearer ${key}` } : {}),
|
|
46
|
+
...(cookie ? { Cookie: cookie } : {}),
|
|
47
|
+
},
|
|
48
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
49
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
50
|
+
// 这里的 body 是账号明文 token(/api/oauth/transfer/import)与 apiKey(/api/providers)。
|
|
51
|
+
// 307/308 重定向会把 body 原样重放到新地址,Authorization 反而会被剥掉——所以必须拒绝。
|
|
52
|
+
redirect: 'error',
|
|
53
|
+
});
|
|
54
|
+
} catch (e) {
|
|
55
|
+
// redirect:'error' 抛的是 cause.message === 'unexpected redirect'(顶层 message 只是 "fetch failed")
|
|
56
|
+
if (e?.cause?.message === 'unexpected redirect') {
|
|
57
|
+
throw new Error(`10Router 返回了重定向,已拒绝跟随:账号凭据只发往你填写的地址,请把地址改成最终地址`);
|
|
58
|
+
}
|
|
59
|
+
throw new Error(`无法连接 10Router:${e.cause?.code || e.message}`);
|
|
60
|
+
}
|
|
61
|
+
const text = await res.text();
|
|
62
|
+
let data = null;
|
|
63
|
+
try { data = text ? JSON.parse(text) : null; } catch {}
|
|
64
|
+
return { status: res.status, data, setCookie: res.headers.get('set-cookie') || '' };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const authCookie = (setCookie) => {
|
|
68
|
+
const m = /(?:^|,\s*)auth_token=([^;,\s]+)/.exec(setCookie || '');
|
|
69
|
+
return m ? `auth_token=${m[1]}` : null;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** 用面板密码登录换会话 Cookie;失败抛「面板密码不正确」类错误 */
|
|
73
|
+
async function login(endpoint, password) {
|
|
74
|
+
const r = await trFetch(`${endpoint}/api/auth/login`, { method: 'POST', body: { password } });
|
|
75
|
+
const cookie = authCookie(r.setCookie);
|
|
76
|
+
if (r.status !== 200 || !cookie) {
|
|
77
|
+
throw Object.assign(new Error(r.status === 401 ? '10Router 面板密码不正确' : `10Router 登录失败(HTTP ${r.status})`), { code: 'BAD_PASSWORD' });
|
|
78
|
+
}
|
|
79
|
+
return cookie;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const connName = (a) => a.name || a.email || a.uid || 'account-' + a.id;
|
|
83
|
+
|
|
84
|
+
/** 通道一:transfer/import(OAuth 形态,按目标 provider 分组推送) */
|
|
85
|
+
async function syncViaTransfer({ endpoint, cookie, groups }) {
|
|
86
|
+
const results = [];
|
|
87
|
+
for (const [target, list] of groups) {
|
|
88
|
+
const passphrase = crypto.randomBytes(18).toString('base64url'); // 仅作传输口令,不落盘
|
|
89
|
+
const blob = sealTransfer(buildExportPayload(list), passphrase);
|
|
90
|
+
const r = await trFetch(`${endpoint}/api/oauth/transfer/import`, {
|
|
91
|
+
cookie, method: 'POST', body: { provider: target, passphrase, blob },
|
|
92
|
+
});
|
|
93
|
+
if (r.status !== 200) {
|
|
94
|
+
results.push({ provider: target, count: list.length, status: 'failed', error: r.data?.error || `HTTP ${r.status}` });
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const d = r.data || {};
|
|
98
|
+
results.push({
|
|
99
|
+
provider: target, count: list.length, status: (d.failed || 0) ? 'partial' : 'ok',
|
|
100
|
+
imported: d.imported || 0, updated: d.updated || 0, skipped: d.skipped || 0, failed: d.failed || 0,
|
|
101
|
+
// 10Router issue #44:导入端会探测刚同步过去的网页会话是否仍可用,
|
|
102
|
+
// 失败清单直接回显到本端摘要(见 syncAccountsTo10r 的 sessionWarns)。
|
|
103
|
+
webSessions: d.webSessions || null,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return results;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 通道二:POST /api/providers(apikey 形态,逐账号建连接;同名 409 记「已存在」) */
|
|
110
|
+
async function syncViaProviders({ endpoint, key, groups }) {
|
|
111
|
+
const results = [];
|
|
112
|
+
for (const [target, list] of groups) {
|
|
113
|
+
const row = { provider: target, count: list.length, status: 'ok', imported: 0, skipped: 0, failed: 0 };
|
|
114
|
+
for (const a of list) {
|
|
115
|
+
const body = buildConnPayload(a, target);
|
|
116
|
+
const r = await trFetch(`${endpoint}/api/providers`, { key, method: 'POST', body });
|
|
117
|
+
if (r.status === 201 || r.status === 200) row.imported++;
|
|
118
|
+
else if (r.status === 409) row.skipped++;
|
|
119
|
+
else { row.failed++; row.error = r.data?.error || `HTTP ${r.status}`; }
|
|
120
|
+
}
|
|
121
|
+
row.status = row.failed ? (row.imported ? 'partial' : 'failed') : 'ok';
|
|
122
|
+
results.push(row);
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 连接体(POST /api/providers 的单账号载荷)。网页会话两种走同一形状(issue #23):
|
|
129
|
+
* - qoder 专属老形状 meta.qoderWebSession(issue #44:usage 侧不区分 authType)
|
|
130
|
+
* - 通用站点 meta.cookieSiteSession(tokenharbor 等,10R 卡片直接读 creditDaddyWebSession)
|
|
131
|
+
*/
|
|
132
|
+
export function buildConnPayload(a, target) {
|
|
133
|
+
const body = { provider: target, apiKey: a.token, name: connName(a) };
|
|
134
|
+
const qc = a.meta?.qoderWebSession?.cookie && String(a.provider).startsWith('qoder') ? a.meta.qoderWebSession : null;
|
|
135
|
+
const sc = a.meta?.cookieSiteSession?.cookie ? a.meta.cookieSiteSession : null;
|
|
136
|
+
// creditDaddySync:所有经 CreditDaddy 同步的连接都带标记,10R 连接行据此亮「CD Sync」徽章
|
|
137
|
+
//(此前只有 qoder 网页会话带 creditDaddyWebSession,CodeBuddy 等连接在 10R 上看不出同步来源)
|
|
138
|
+
const psd = { creditDaddySync: true, userId: a.uid || null };
|
|
139
|
+
if (qc) psd.creditDaddyWebSession = { cookie: qc.cookie, capturedAt: qc.capturedAt || null, userId: a.uid || null };
|
|
140
|
+
else if (sc) psd.creditDaddyWebSession = { cookie: sc.cookie, capturedAt: sc.capturedAt || null, userId: null };
|
|
141
|
+
// Trae 签到(10R traeCheckin)优先用「登录时注册的设备」:CD 浏览器登录给每个账号
|
|
142
|
+
// 造了独立指纹(meta.traeDevice.deviceId),随 psd 带过去,避免 10R 端陌生派生号
|
|
143
|
+
// 领积分吃 9074(#31 口径)。私钥只服务本机 refresh 链,不出本机。
|
|
144
|
+
if (a.provider === 'trae' && a.meta?.traeDevice?.deviceId && /^\d{8,}$/.test(String(a.meta.traeDevice.deviceId))) {
|
|
145
|
+
psd.creditDaddyTraeDevice = {
|
|
146
|
+
deviceId: String(a.meta.traeDevice.deviceId),
|
|
147
|
+
...(a.meta.traeDevice.machineId ? { machineId: String(a.meta.traeDevice.machineId) } : {}),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
body.providerSpecificData = psd;
|
|
151
|
+
return body;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 同步账号到 10Router。
|
|
156
|
+
* @returns {{ channel: 'oauth'|'apikey', results: Array, summary: string }}
|
|
157
|
+
*/
|
|
158
|
+
export async function syncAccountsTo10r({ endpoint, key, adminPassword, accounts }) {
|
|
159
|
+
if (!endpoint) throw Object.assign(new Error('尚未配置 10Router 地址'), { code: 'NOT_CONFIGURED' });
|
|
160
|
+
if (!key && !adminPassword) throw Object.assign(new Error('尚未配置 10Router 虚拟 key'), { code: 'NOT_CONFIGURED' });
|
|
161
|
+
|
|
162
|
+
// 按目标 10R 连接类型分组;无映射的产品(zcode/mirasim/catpaw 等)记入跳过原因
|
|
163
|
+
const groups = new Map();
|
|
164
|
+
const unmapped = new Map();
|
|
165
|
+
const traeIntl = [];
|
|
166
|
+
for (const a of accounts || []) {
|
|
167
|
+
if (!a || !a.token) continue;
|
|
168
|
+
// #56:国内线(provider trae)里混进国际账号(旧 meta.isIntl 标记)不往 trae-cn 推——
|
|
169
|
+
// 10R 拿国内端点打国际 token 必然 20101。provider 'trae-intl' 有独立映射(→ trae),不走这。
|
|
170
|
+
if (a.provider === 'trae' && (a.meta?.isIntl || a.isIntl)) {
|
|
171
|
+
traeIntl.push(a);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const target = mapProviderId(a.provider);
|
|
175
|
+
if (!target) {
|
|
176
|
+
if (a.provider) unmapped.set(a.provider, (unmapped.get(a.provider) || 0) + 1);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
if (!groups.has(target)) groups.set(target, []);
|
|
180
|
+
groups.get(target).push(a);
|
|
181
|
+
}
|
|
182
|
+
if (!groups.size) throw new Error('没有可同步的账号(qoder / WorkBuddy / MiniMax / Loomy / Trae 系列);zcode 待反代就绪后支持');
|
|
183
|
+
|
|
184
|
+
let channel = 'apikey';
|
|
185
|
+
let cookie = null;
|
|
186
|
+
const results = [];
|
|
187
|
+
if (adminPassword) {
|
|
188
|
+
channel = 'oauth';
|
|
189
|
+
cookie = await login(endpoint, adminPassword);
|
|
190
|
+
try {
|
|
191
|
+
results.push(...await syncViaTransfer({ endpoint, cookie, groups }));
|
|
192
|
+
} finally {
|
|
193
|
+
trFetch(`${endpoint}/api/auth/logout`, { cookie, method: 'POST' }).catch(() => {});
|
|
194
|
+
}
|
|
195
|
+
} else {
|
|
196
|
+
if (!key) throw Object.assign(new Error('尚未配置 10Router 虚拟 key'), { code: 'NOT_CONFIGURED' });
|
|
197
|
+
results.push(...await syncViaProviders({ endpoint, key, groups }));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const sum = (k) => results.reduce((n, r) => n + (r[k] || 0), 0);
|
|
201
|
+
const failed = results.filter((r) => r.status === 'failed' || r.status === 'partial');
|
|
202
|
+
const parts = [];
|
|
203
|
+
if (sum('imported')) parts.push(`导入 ${sum('imported')}`);
|
|
204
|
+
if (sum('updated')) parts.push(`更新 ${sum('updated')}`);
|
|
205
|
+
if (sum('skipped')) parts.push(`已存在 ${sum('skipped')}`);
|
|
206
|
+
if (sum('failed')) parts.push(`失败 ${sum('failed')}`);
|
|
207
|
+
if (unmapped.size) parts.push('跳过 ' + [...unmapped].map(([p, n]) => `${p} ×${n}`).join('、'));
|
|
208
|
+
if (traeIntl.length) parts.push(`跳过 Trae 国际版 ×${traeIntl.length}(CreditDaddy 的 Trae 线仅支持国内站)`);
|
|
209
|
+
// issue #44:Qoder 网页会话决定 10Router 端「套餐内 Credits」能不能显示。
|
|
210
|
+
// OAuth 通道:10Router 导入时已逐账号探测,失效清单原样带回(deadNames);
|
|
211
|
+
// 两条通道都要点名本地就没有会话的账号(apikey 通道 webSessions 恒为
|
|
212
|
+
// null,条件自动适用;OAuth 通道里已被探测过的组不重复报)。
|
|
213
|
+
const deadNames = [];
|
|
214
|
+
for (const r of results) {
|
|
215
|
+
for (const f of r.webSessions?.failures || []) deadNames.push(f.name);
|
|
216
|
+
}
|
|
217
|
+
const sessionless = [];
|
|
218
|
+
for (const [target, list] of groups) {
|
|
219
|
+
if (!String(target).startsWith('qoder')) continue;
|
|
220
|
+
for (const a of list) {
|
|
221
|
+
if (results.find((r) => r.provider === target)?.webSessions == null && !a.meta?.qoderWebSession?.cookie) {
|
|
222
|
+
sessionless.push(connName(a));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (sessionless.length) parts.push(`未带网页会话 ${sessionless.length}(${sessionless.join('、')},在 CreditDaddy 重新浏览器登录 Qoder 后再同步可补齐逐资源包明细)`);
|
|
227
|
+
if (deadNames.length) parts.push(`网页会话已失效 ${deadNames.length}(${deadNames.join('、')},重新登录 Qoder 网页后再次同步)`);
|
|
228
|
+
const summary = (channel === 'oauth' ? '账号同步完成(OAuth 通道):' : '账号同步完成(apikey 通道,token 过期后需重新同步):')
|
|
229
|
+
+ (parts.join(',') || '无变化')
|
|
230
|
+
+ (failed.length ? `;${failed.map((r) => r.provider + ':' + (r.error || r.status)).join(';')}` : '');
|
|
231
|
+
return { channel, results, summary };
|
|
232
|
+
}
|