wyxrouter 0.4.72 → 0.4.73
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/open-sse/services/usage.js +48 -11
- package/package.json +1 -1
|
@@ -53,7 +53,7 @@ const CLAUDE_CONFIG = {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
const CODEBUDDY_CONFIG = {
|
|
56
|
-
usageUrl: "https://www.codebuddy.ai/billing/meter/get-user-resource",
|
|
56
|
+
usageUrl: "https://www.codebuddy.ai/v2/billing/meter/get-user-resource",
|
|
57
57
|
productCode: "p_tcaca",
|
|
58
58
|
packageCodes: {
|
|
59
59
|
free: "TCACA_code_001_PqouKr6QWV",
|
|
@@ -112,15 +112,46 @@ export async function getUsageForProvider(connection, proxyOptions = null) {
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
async function fetchCodeBuddyUid(accessToken, providerSpecificData = {}, proxyOptions = null) {
|
|
116
|
+
const cachedUid = providerSpecificData?.uid || providerSpecificData?.rawAuth?.uid;
|
|
117
|
+
if (cachedUid) return { uid: cachedUid, enterpriseId: providerSpecificData?.enterpriseId || null };
|
|
118
|
+
|
|
119
|
+
const domain = providerSpecificData?.domain || providerSpecificData?.rawAuth?.domain || "www.codebuddy.ai";
|
|
120
|
+
try {
|
|
121
|
+
const response = await proxyAwareFetch(`https://${domain}/v2/plugin/accounts`, {
|
|
122
|
+
method: "GET",
|
|
123
|
+
headers: {
|
|
124
|
+
Authorization: `Bearer ${accessToken}`,
|
|
125
|
+
Accept: "application/json",
|
|
126
|
+
"X-Domain": domain,
|
|
127
|
+
},
|
|
128
|
+
}, proxyOptions);
|
|
129
|
+
|
|
130
|
+
if (!response.ok) return { uid: null, enterpriseId: null };
|
|
131
|
+
|
|
132
|
+
const body = await response.json();
|
|
133
|
+
const accounts = body?.data?.accounts || [];
|
|
134
|
+
const account = accounts.find((a) => a.lastLogin) || accounts[0] || {};
|
|
135
|
+
return {
|
|
136
|
+
uid: account.uid || null,
|
|
137
|
+
enterpriseId: account.enterpriseId || null,
|
|
138
|
+
};
|
|
139
|
+
} catch {
|
|
140
|
+
return { uid: null, enterpriseId: null };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
115
144
|
async function getCodeBuddyUsage(accessToken, providerSpecificData = {}, proxyOptions = null) {
|
|
116
145
|
if (!accessToken) {
|
|
117
146
|
return { plan: "CodeBuddy", message: "CodeBuddy access token not available.", quotas: {} };
|
|
118
147
|
}
|
|
119
148
|
|
|
120
149
|
try {
|
|
150
|
+
const { uid, enterpriseId } = await fetchCodeBuddyUid(accessToken, providerSpecificData, proxyOptions);
|
|
151
|
+
|
|
121
152
|
const response = await proxyAwareFetch(CODEBUDDY_CONFIG.usageUrl, {
|
|
122
153
|
method: "POST",
|
|
123
|
-
headers: buildCodeBuddyUsageHeaders(accessToken, providerSpecificData),
|
|
154
|
+
headers: buildCodeBuddyUsageHeaders(accessToken, providerSpecificData, uid, enterpriseId),
|
|
124
155
|
body: JSON.stringify(buildCodeBuddyUsageBody()),
|
|
125
156
|
}, proxyOptions);
|
|
126
157
|
|
|
@@ -135,7 +166,7 @@ async function getCodeBuddyUsage(accessToken, providerSpecificData = {}, proxyOp
|
|
|
135
166
|
if (response.status === 401 || response.status === 403) {
|
|
136
167
|
return {
|
|
137
168
|
plan: "CodeBuddy",
|
|
138
|
-
message:
|
|
169
|
+
message: `CodeBuddy quota: auth failed (${response.status}). uid=${uid ? "yes" : "missing"}.`,
|
|
139
170
|
quotas: {},
|
|
140
171
|
};
|
|
141
172
|
}
|
|
@@ -209,20 +240,26 @@ function buildCodeBuddyUsageBody() {
|
|
|
209
240
|
};
|
|
210
241
|
}
|
|
211
242
|
|
|
212
|
-
function buildCodeBuddyUsageHeaders(accessToken, providerSpecificData = {}) {
|
|
243
|
+
function buildCodeBuddyUsageHeaders(accessToken, providerSpecificData = {}, uid = null, enterpriseId = null) {
|
|
213
244
|
const domain = providerSpecificData?.domain || providerSpecificData?.rawAuth?.domain || "www.codebuddy.ai";
|
|
214
|
-
const webCookie = providerSpecificData?.webCookie || providerSpecificData?.cookie || providerSpecificData?.codeBuddyCookie;
|
|
215
245
|
|
|
216
|
-
|
|
217
|
-
|
|
246
|
+
const headers = {
|
|
247
|
+
Authorization: `Bearer ${accessToken}`,
|
|
218
248
|
Accept: "application/json, text/plain, */*",
|
|
249
|
+
"Accept-Language": "zh-CN,zh;q=0.9",
|
|
219
250
|
"Content-Type": "application/json",
|
|
220
|
-
"User-Agent": "Mozilla/5.0 CodeBuddy quota probe",
|
|
221
|
-
"X-Requested-With": "XMLHttpRequest",
|
|
222
251
|
"X-Domain": domain,
|
|
223
|
-
Origin: `https://${domain}`,
|
|
224
|
-
Referer: `https://${domain}/profile/usage`,
|
|
225
252
|
};
|
|
253
|
+
|
|
254
|
+
if (uid) {
|
|
255
|
+
headers["X-User-Id"] = uid;
|
|
256
|
+
}
|
|
257
|
+
if (enterpriseId) {
|
|
258
|
+
headers["X-Enterprise-Id"] = enterpriseId;
|
|
259
|
+
headers["X-Tenant-Id"] = enterpriseId;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return headers;
|
|
226
263
|
}
|
|
227
264
|
|
|
228
265
|
function parseCodeBuddyUsage(payload) {
|