@byoky/core 0.2.0 → 0.4.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 -0
- package/dist/index.cjs +469 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +238 -2
- package/dist/index.d.ts +238 -2
- package/dist/index.js +449 -2
- package/dist/index.js.map +1 -1
- package/package.json +20 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 byoky contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -24,19 +24,39 @@ __export(index_exports, {
|
|
|
24
24
|
BYOKY_PROVIDER_KEY: () => BYOKY_PROVIDER_KEY,
|
|
25
25
|
ByokyError: () => ByokyError,
|
|
26
26
|
ByokyErrorCode: () => ByokyErrorCode,
|
|
27
|
+
MIN_PASSWORD_LENGTH: () => MIN_PASSWORD_LENGTH,
|
|
27
28
|
PROVIDERS: () => PROVIDERS,
|
|
29
|
+
WS_READY_STATE: () => WS_READY_STATE,
|
|
30
|
+
buildHeaders: () => buildHeaders,
|
|
31
|
+
checkPasswordStrength: () => checkPasswordStrength,
|
|
32
|
+
computeAllowanceCheck: () => computeAllowanceCheck,
|
|
28
33
|
createConnectRequest: () => createConnectRequest,
|
|
29
34
|
createConnectResponse: () => createConnectResponse,
|
|
30
35
|
createErrorMessage: () => createErrorMessage,
|
|
36
|
+
createGiftLink: () => createGiftLink,
|
|
31
37
|
createMessage: () => createMessage,
|
|
38
|
+
decodeGiftLink: () => decodeGiftLink,
|
|
32
39
|
decrypt: () => decrypt,
|
|
33
40
|
deriveKey: () => deriveKey,
|
|
41
|
+
encodeGiftLink: () => encodeGiftLink,
|
|
34
42
|
encrypt: () => encrypt,
|
|
43
|
+
extractUsageFromParsed: () => extractUsageFromParsed,
|
|
35
44
|
getProvider: () => getProvider,
|
|
36
45
|
getProviderIds: () => getProviderIds,
|
|
46
|
+
giftBudgetPercent: () => giftBudgetPercent,
|
|
47
|
+
giftBudgetRemaining: () => giftBudgetRemaining,
|
|
48
|
+
giftLinkToUrl: () => giftLinkToUrl,
|
|
37
49
|
hashPassword: () => hashPassword,
|
|
38
50
|
isByokyMessage: () => isByokyMessage,
|
|
51
|
+
isGiftBudgetExhausted: () => isGiftBudgetExhausted,
|
|
52
|
+
isGiftExpired: () => isGiftExpired,
|
|
39
53
|
maskKey: () => maskKey,
|
|
54
|
+
parseModel: () => parseModel,
|
|
55
|
+
parseRelayMessage: () => parseRelayMessage,
|
|
56
|
+
parseUsage: () => parseUsage,
|
|
57
|
+
sendRelayMessage: () => sendRelayMessage,
|
|
58
|
+
validateGiftLink: () => validateGiftLink,
|
|
59
|
+
validateProxyUrl: () => validateProxyUrl,
|
|
40
60
|
verifyPassword: () => verifyPassword
|
|
41
61
|
});
|
|
42
62
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -52,6 +72,8 @@ var ByokyErrorCode = /* @__PURE__ */ ((ByokyErrorCode2) => {
|
|
|
52
72
|
ByokyErrorCode2["INVALID_KEY"] = "INVALID_KEY";
|
|
53
73
|
ByokyErrorCode2["TOKEN_EXPIRED"] = "TOKEN_EXPIRED";
|
|
54
74
|
ByokyErrorCode2["PROXY_ERROR"] = "PROXY_ERROR";
|
|
75
|
+
ByokyErrorCode2["RELAY_CONNECTION_FAILED"] = "RELAY_CONNECTION_FAILED";
|
|
76
|
+
ByokyErrorCode2["RELAY_DISCONNECTED"] = "RELAY_DISCONNECTED";
|
|
55
77
|
ByokyErrorCode2["UNKNOWN"] = "UNKNOWN";
|
|
56
78
|
return ByokyErrorCode2;
|
|
57
79
|
})(ByokyErrorCode || {});
|
|
@@ -136,7 +158,11 @@ async function verifyPassword(password, storedHash) {
|
|
|
136
158
|
const originalHash = combined.slice(SALT_LENGTH);
|
|
137
159
|
const newHash = await deriveRawHash(password, salt);
|
|
138
160
|
if (originalHash.length !== newHash.length) return false;
|
|
139
|
-
|
|
161
|
+
let result = 0;
|
|
162
|
+
for (let i = 0; i < originalHash.length; i++) {
|
|
163
|
+
result |= originalHash[i] ^ newHash[i];
|
|
164
|
+
}
|
|
165
|
+
return result === 0;
|
|
140
166
|
}
|
|
141
167
|
function maskKey(key) {
|
|
142
168
|
if (key.length <= 8) return "****";
|
|
@@ -204,6 +230,18 @@ var ByokyError = class _ByokyError extends Error {
|
|
|
204
230
|
{ providerId }
|
|
205
231
|
);
|
|
206
232
|
}
|
|
233
|
+
static relayConnectionFailed(reason) {
|
|
234
|
+
return new _ByokyError(
|
|
235
|
+
"RELAY_CONNECTION_FAILED" /* RELAY_CONNECTION_FAILED */,
|
|
236
|
+
`Relay connection failed${reason ? `: ${reason}` : ""}`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
static relayDisconnected() {
|
|
240
|
+
return new _ByokyError(
|
|
241
|
+
"RELAY_DISCONNECTED" /* RELAY_DISCONNECTED */,
|
|
242
|
+
"Relay connection was closed"
|
|
243
|
+
);
|
|
244
|
+
}
|
|
207
245
|
};
|
|
208
246
|
|
|
209
247
|
// src/protocol.ts
|
|
@@ -247,8 +285,93 @@ var PROVIDERS = {
|
|
|
247
285
|
gemini: {
|
|
248
286
|
id: "gemini",
|
|
249
287
|
name: "Google Gemini",
|
|
288
|
+
authMethods: ["api_key", "oauth"],
|
|
289
|
+
baseUrl: "https://generativelanguage.googleapis.com",
|
|
290
|
+
oauthConfig: {
|
|
291
|
+
clientId: "699663966637-gr4d994198r4g6jvip25ffg85kree6ck.apps.googleusercontent.com",
|
|
292
|
+
authorizationUrl: "https://accounts.google.com/o/oauth2/v2/auth",
|
|
293
|
+
tokenUrl: "https://oauth2.googleapis.com/token",
|
|
294
|
+
scopes: ["https://www.googleapis.com/auth/generative-language"],
|
|
295
|
+
extraAuthParams: { access_type: "offline", prompt: "consent" }
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
mistral: {
|
|
299
|
+
id: "mistral",
|
|
300
|
+
name: "Mistral",
|
|
301
|
+
authMethods: ["api_key"],
|
|
302
|
+
baseUrl: "https://api.mistral.ai"
|
|
303
|
+
},
|
|
304
|
+
cohere: {
|
|
305
|
+
id: "cohere",
|
|
306
|
+
name: "Cohere",
|
|
307
|
+
authMethods: ["api_key"],
|
|
308
|
+
baseUrl: "https://api.cohere.com"
|
|
309
|
+
},
|
|
310
|
+
xai: {
|
|
311
|
+
id: "xai",
|
|
312
|
+
name: "xAI (Grok)",
|
|
313
|
+
authMethods: ["api_key"],
|
|
314
|
+
baseUrl: "https://api.x.ai"
|
|
315
|
+
},
|
|
316
|
+
deepseek: {
|
|
317
|
+
id: "deepseek",
|
|
318
|
+
name: "DeepSeek",
|
|
319
|
+
authMethods: ["api_key"],
|
|
320
|
+
baseUrl: "https://api.deepseek.com"
|
|
321
|
+
},
|
|
322
|
+
perplexity: {
|
|
323
|
+
id: "perplexity",
|
|
324
|
+
name: "Perplexity",
|
|
325
|
+
authMethods: ["api_key"],
|
|
326
|
+
baseUrl: "https://api.perplexity.ai"
|
|
327
|
+
},
|
|
328
|
+
groq: {
|
|
329
|
+
id: "groq",
|
|
330
|
+
name: "Groq",
|
|
331
|
+
authMethods: ["api_key"],
|
|
332
|
+
baseUrl: "https://api.groq.com"
|
|
333
|
+
},
|
|
334
|
+
together: {
|
|
335
|
+
id: "together",
|
|
336
|
+
name: "Together AI",
|
|
250
337
|
authMethods: ["api_key"],
|
|
251
|
-
baseUrl: "https://
|
|
338
|
+
baseUrl: "https://api.together.xyz"
|
|
339
|
+
},
|
|
340
|
+
fireworks: {
|
|
341
|
+
id: "fireworks",
|
|
342
|
+
name: "Fireworks AI",
|
|
343
|
+
authMethods: ["api_key"],
|
|
344
|
+
baseUrl: "https://api.fireworks.ai"
|
|
345
|
+
},
|
|
346
|
+
replicate: {
|
|
347
|
+
id: "replicate",
|
|
348
|
+
name: "Replicate",
|
|
349
|
+
authMethods: ["api_key"],
|
|
350
|
+
baseUrl: "https://api.replicate.com"
|
|
351
|
+
},
|
|
352
|
+
openrouter: {
|
|
353
|
+
id: "openrouter",
|
|
354
|
+
name: "OpenRouter",
|
|
355
|
+
authMethods: ["api_key"],
|
|
356
|
+
baseUrl: "https://openrouter.ai/api"
|
|
357
|
+
},
|
|
358
|
+
huggingface: {
|
|
359
|
+
id: "huggingface",
|
|
360
|
+
name: "Hugging Face",
|
|
361
|
+
authMethods: ["api_key", "oauth"],
|
|
362
|
+
baseUrl: "https://api-inference.huggingface.co",
|
|
363
|
+
oauthConfig: {
|
|
364
|
+
clientId: "031aeb11-725b-498a-93f9-d3599d84f57c",
|
|
365
|
+
authorizationUrl: "https://huggingface.co/oauth/authorize",
|
|
366
|
+
tokenUrl: "https://huggingface.co/oauth/token",
|
|
367
|
+
scopes: ["inference-api"]
|
|
368
|
+
}
|
|
369
|
+
},
|
|
370
|
+
azure_openai: {
|
|
371
|
+
id: "azure_openai",
|
|
372
|
+
name: "Azure OpenAI",
|
|
373
|
+
authMethods: ["api_key"],
|
|
374
|
+
baseUrl: "https://YOUR_RESOURCE.openai.azure.com"
|
|
252
375
|
}
|
|
253
376
|
};
|
|
254
377
|
function getProvider(id) {
|
|
@@ -257,25 +380,369 @@ function getProvider(id) {
|
|
|
257
380
|
function getProviderIds() {
|
|
258
381
|
return Object.keys(PROVIDERS);
|
|
259
382
|
}
|
|
383
|
+
|
|
384
|
+
// src/relay.ts
|
|
385
|
+
var WS_READY_STATE = {
|
|
386
|
+
CONNECTING: 0,
|
|
387
|
+
OPEN: 1,
|
|
388
|
+
CLOSING: 2,
|
|
389
|
+
CLOSED: 3
|
|
390
|
+
};
|
|
391
|
+
function parseRelayMessage(data) {
|
|
392
|
+
try {
|
|
393
|
+
const raw = typeof data === "string" ? JSON.parse(data) : data;
|
|
394
|
+
if (!raw || typeof raw !== "object" || typeof raw.type !== "string" || !raw.type.startsWith("relay:")) {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
switch (raw.type) {
|
|
398
|
+
case "relay:hello":
|
|
399
|
+
if (typeof raw.sessionId !== "string") return null;
|
|
400
|
+
break;
|
|
401
|
+
case "relay:request":
|
|
402
|
+
if (typeof raw.requestId !== "string" || typeof raw.providerId !== "string" || typeof raw.url !== "string" || typeof raw.method !== "string") return null;
|
|
403
|
+
break;
|
|
404
|
+
case "relay:response:meta":
|
|
405
|
+
if (typeof raw.requestId !== "string" || typeof raw.status !== "number") return null;
|
|
406
|
+
break;
|
|
407
|
+
case "relay:response:chunk":
|
|
408
|
+
if (typeof raw.requestId !== "string" || typeof raw.chunk !== "string") return null;
|
|
409
|
+
break;
|
|
410
|
+
case "relay:response:done":
|
|
411
|
+
case "relay:response:error":
|
|
412
|
+
if (typeof raw.requestId !== "string") return null;
|
|
413
|
+
break;
|
|
414
|
+
case "relay:ping":
|
|
415
|
+
case "relay:pong":
|
|
416
|
+
if (typeof raw.ts !== "number") return null;
|
|
417
|
+
break;
|
|
418
|
+
default:
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
return raw;
|
|
422
|
+
} catch {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
function sendRelayMessage(ws, msg) {
|
|
427
|
+
if (ws.readyState === WS_READY_STATE.OPEN) {
|
|
428
|
+
ws.send(JSON.stringify(msg));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// src/password-strength.ts
|
|
433
|
+
var COMMON_PASSWORDS = /* @__PURE__ */ new Set([
|
|
434
|
+
"password",
|
|
435
|
+
"12345678",
|
|
436
|
+
"qwerty12",
|
|
437
|
+
"letmein12",
|
|
438
|
+
"welcome1",
|
|
439
|
+
"monkey12",
|
|
440
|
+
"dragon12",
|
|
441
|
+
"master12",
|
|
442
|
+
"abc12345",
|
|
443
|
+
"password1",
|
|
444
|
+
"password12",
|
|
445
|
+
"iloveyou1",
|
|
446
|
+
"sunshine1",
|
|
447
|
+
"trustno1",
|
|
448
|
+
"princess1",
|
|
449
|
+
"football1",
|
|
450
|
+
"shadow123",
|
|
451
|
+
"michael1",
|
|
452
|
+
"jordan123",
|
|
453
|
+
"superman1"
|
|
454
|
+
]);
|
|
455
|
+
function checkPasswordStrength(password) {
|
|
456
|
+
const feedback = [];
|
|
457
|
+
let score = 0;
|
|
458
|
+
if (password.length < 12) {
|
|
459
|
+
feedback.push("Use at least 12 characters");
|
|
460
|
+
if (password.length < 8) {
|
|
461
|
+
return { score: 0, label: "Too weak", feedback };
|
|
462
|
+
}
|
|
463
|
+
} else {
|
|
464
|
+
score++;
|
|
465
|
+
if (password.length >= 16) score++;
|
|
466
|
+
}
|
|
467
|
+
if (COMMON_PASSWORDS.has(password.toLowerCase())) {
|
|
468
|
+
feedback.push("This is a commonly used password");
|
|
469
|
+
return { score: 0, label: "Too weak", feedback };
|
|
470
|
+
}
|
|
471
|
+
const hasLower = /[a-z]/.test(password);
|
|
472
|
+
const hasUpper = /[A-Z]/.test(password);
|
|
473
|
+
const hasDigit = /\d/.test(password);
|
|
474
|
+
const hasSymbol = /[^a-zA-Z0-9]/.test(password);
|
|
475
|
+
const charTypes = [hasLower, hasUpper, hasDigit, hasSymbol].filter(Boolean).length;
|
|
476
|
+
if (charTypes < 2) {
|
|
477
|
+
feedback.push("Mix uppercase, lowercase, numbers, and symbols");
|
|
478
|
+
} else if (charTypes >= 3) {
|
|
479
|
+
score++;
|
|
480
|
+
}
|
|
481
|
+
if (charTypes >= 4) {
|
|
482
|
+
score++;
|
|
483
|
+
}
|
|
484
|
+
if (/(.)\1{3,}/.test(password)) {
|
|
485
|
+
feedback.push("Avoid repeated characters");
|
|
486
|
+
score = Math.max(0, score - 1);
|
|
487
|
+
}
|
|
488
|
+
if (/(?:012|123|234|345|456|567|678|789|abc|bcd|cde|def)/i.test(password)) {
|
|
489
|
+
feedback.push("Avoid sequential patterns");
|
|
490
|
+
score = Math.max(0, score - 1);
|
|
491
|
+
}
|
|
492
|
+
const capped = Math.min(4, Math.max(0, score));
|
|
493
|
+
const labels = {
|
|
494
|
+
0: "Too weak",
|
|
495
|
+
1: "Weak",
|
|
496
|
+
2: "Fair",
|
|
497
|
+
3: "Strong",
|
|
498
|
+
4: "Very strong"
|
|
499
|
+
};
|
|
500
|
+
if (feedback.length === 0 && capped < 3) {
|
|
501
|
+
feedback.push("Add more character variety or length");
|
|
502
|
+
}
|
|
503
|
+
return { score: capped, label: labels[capped], feedback };
|
|
504
|
+
}
|
|
505
|
+
var MIN_PASSWORD_LENGTH = 12;
|
|
506
|
+
|
|
507
|
+
// src/proxy-utils.ts
|
|
508
|
+
function validateProxyUrl(providerId, url) {
|
|
509
|
+
const provider = PROVIDERS[providerId];
|
|
510
|
+
if (!provider) return false;
|
|
511
|
+
try {
|
|
512
|
+
const target = new URL(url);
|
|
513
|
+
if (target.protocol !== "https:") return false;
|
|
514
|
+
const base = new URL(provider.baseUrl);
|
|
515
|
+
return target.origin === base.origin;
|
|
516
|
+
} catch {
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function buildHeaders(providerId, requestHeaders, apiKey, authMethod = "api_key") {
|
|
521
|
+
const headers = {};
|
|
522
|
+
for (const [key, value] of Object.entries(requestHeaders)) {
|
|
523
|
+
headers[key.toLowerCase()] = value;
|
|
524
|
+
}
|
|
525
|
+
delete headers["authorization"];
|
|
526
|
+
delete headers["x-api-key"];
|
|
527
|
+
delete headers["api-key"];
|
|
528
|
+
delete headers["origin"];
|
|
529
|
+
delete headers["referer"];
|
|
530
|
+
for (const key of Object.keys(headers)) {
|
|
531
|
+
if (key.startsWith("x-stainless-")) delete headers[key];
|
|
532
|
+
}
|
|
533
|
+
delete headers["sec-fetch-mode"];
|
|
534
|
+
delete headers["accept-language"];
|
|
535
|
+
delete headers["accept-encoding"];
|
|
536
|
+
delete headers["content-length"];
|
|
537
|
+
if (providerId === "anthropic") {
|
|
538
|
+
if (authMethod === "oauth") {
|
|
539
|
+
headers["authorization"] = `Bearer ${apiKey}`;
|
|
540
|
+
headers["user-agent"] = "claude-cli/2.1.76";
|
|
541
|
+
headers["x-app"] = "cli";
|
|
542
|
+
headers["accept"] = "application/json";
|
|
543
|
+
headers["anthropic-beta"] = "claude-code-20250219,oauth-2025-04-20,fine-grained-tool-streaming-2025-05-14,interleaved-thinking-2025-05-14";
|
|
544
|
+
headers["anthropic-dangerous-direct-browser-access"] = "true";
|
|
545
|
+
} else {
|
|
546
|
+
headers["x-api-key"] = apiKey;
|
|
547
|
+
}
|
|
548
|
+
headers["anthropic-version"] = headers["anthropic-version"] ?? "2023-06-01";
|
|
549
|
+
} else if (providerId === "azure_openai") {
|
|
550
|
+
headers["api-key"] = apiKey;
|
|
551
|
+
} else {
|
|
552
|
+
headers["authorization"] = `Bearer ${apiKey}`;
|
|
553
|
+
}
|
|
554
|
+
return headers;
|
|
555
|
+
}
|
|
556
|
+
function parseModel(body) {
|
|
557
|
+
if (!body) return void 0;
|
|
558
|
+
try {
|
|
559
|
+
const parsed = JSON.parse(body);
|
|
560
|
+
return parsed.model ?? void 0;
|
|
561
|
+
} catch {
|
|
562
|
+
return void 0;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
function parseUsage(providerId, body) {
|
|
566
|
+
try {
|
|
567
|
+
if (body.includes("data: ")) {
|
|
568
|
+
const lines = body.split("\n").filter((l) => l.startsWith("data: ") && !l.includes("[DONE]"));
|
|
569
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
570
|
+
const json = lines[i].replace("data: ", "");
|
|
571
|
+
try {
|
|
572
|
+
const parsed2 = JSON.parse(json);
|
|
573
|
+
const usage = extractUsageFromParsed(providerId, parsed2);
|
|
574
|
+
if (usage) return usage;
|
|
575
|
+
} catch {
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
return void 0;
|
|
580
|
+
}
|
|
581
|
+
const parsed = JSON.parse(body);
|
|
582
|
+
return extractUsageFromParsed(providerId, parsed);
|
|
583
|
+
} catch {
|
|
584
|
+
return void 0;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
function extractUsageFromParsed(providerId, parsed) {
|
|
588
|
+
if (providerId === "anthropic") {
|
|
589
|
+
const usage2 = parsed.usage;
|
|
590
|
+
if (usage2?.input_tokens != null && usage2?.output_tokens != null) {
|
|
591
|
+
return { inputTokens: usage2.input_tokens, outputTokens: usage2.output_tokens };
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (providerId === "gemini") {
|
|
595
|
+
const meta = parsed.usageMetadata;
|
|
596
|
+
if (meta?.promptTokenCount != null) {
|
|
597
|
+
return {
|
|
598
|
+
inputTokens: meta.promptTokenCount,
|
|
599
|
+
outputTokens: meta.candidatesTokenCount ?? 0
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const usage = parsed.usage;
|
|
604
|
+
if (usage?.prompt_tokens != null && usage?.completion_tokens != null) {
|
|
605
|
+
return { inputTokens: usage.prompt_tokens, outputTokens: usage.completion_tokens };
|
|
606
|
+
}
|
|
607
|
+
return void 0;
|
|
608
|
+
}
|
|
609
|
+
function computeAllowanceCheck(allowance, entries, providerId) {
|
|
610
|
+
if (!allowance) return { allowed: true };
|
|
611
|
+
let totalUsed = 0;
|
|
612
|
+
const byProvider = {};
|
|
613
|
+
for (const entry of entries) {
|
|
614
|
+
const tokens = (entry.inputTokens ?? 0) + (entry.outputTokens ?? 0);
|
|
615
|
+
totalUsed += tokens;
|
|
616
|
+
byProvider[entry.providerId] = (byProvider[entry.providerId] ?? 0) + tokens;
|
|
617
|
+
}
|
|
618
|
+
if (allowance.totalLimit != null && totalUsed >= allowance.totalLimit) {
|
|
619
|
+
return { allowed: false, reason: `Token allowance exceeded for ${allowance.origin}` };
|
|
620
|
+
}
|
|
621
|
+
const providerLimit = allowance.providerLimits?.[providerId];
|
|
622
|
+
if (providerLimit != null && (byProvider[providerId] ?? 0) >= providerLimit) {
|
|
623
|
+
return { allowed: false, reason: `Token allowance for ${providerId} exceeded` };
|
|
624
|
+
}
|
|
625
|
+
return { allowed: true };
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// src/gift.ts
|
|
629
|
+
function encodeGiftLink(link) {
|
|
630
|
+
const json = JSON.stringify(link);
|
|
631
|
+
const bytes = new TextEncoder().encode(json);
|
|
632
|
+
return base64UrlEncode(bytes);
|
|
633
|
+
}
|
|
634
|
+
function decodeGiftLink(encoded) {
|
|
635
|
+
try {
|
|
636
|
+
const clean = encoded.replace(/^byoky:\/\/gift\//, "");
|
|
637
|
+
const bytes = base64UrlDecode(clean);
|
|
638
|
+
const json = new TextDecoder().decode(bytes);
|
|
639
|
+
const parsed = JSON.parse(json);
|
|
640
|
+
if (parsed.v !== 1) return null;
|
|
641
|
+
return parsed;
|
|
642
|
+
} catch {
|
|
643
|
+
return null;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function giftLinkToUrl(encoded) {
|
|
647
|
+
return `byoky://gift/${encoded}`;
|
|
648
|
+
}
|
|
649
|
+
function validateGiftLink(link) {
|
|
650
|
+
if (link.v !== 1) return { valid: false, reason: "Unsupported gift version" };
|
|
651
|
+
if (!link.id || typeof link.id !== "string") return { valid: false, reason: "Missing gift ID" };
|
|
652
|
+
if (!link.p || typeof link.p !== "string") return { valid: false, reason: "Missing provider" };
|
|
653
|
+
if (!link.t || typeof link.t !== "string") return { valid: false, reason: "Missing auth token" };
|
|
654
|
+
if (!link.r || typeof link.r !== "string") return { valid: false, reason: "Missing relay URL" };
|
|
655
|
+
if (typeof link.m !== "number" || link.m <= 0) return { valid: false, reason: "Invalid token budget" };
|
|
656
|
+
if (typeof link.e !== "number" || link.e <= Date.now()) return { valid: false, reason: "Gift has expired" };
|
|
657
|
+
try {
|
|
658
|
+
const url = new URL(link.r);
|
|
659
|
+
if (url.protocol !== "ws:" && url.protocol !== "wss:") {
|
|
660
|
+
return { valid: false, reason: "Relay URL must use ws:// or wss://" };
|
|
661
|
+
}
|
|
662
|
+
} catch {
|
|
663
|
+
return { valid: false, reason: "Invalid relay URL" };
|
|
664
|
+
}
|
|
665
|
+
return { valid: true };
|
|
666
|
+
}
|
|
667
|
+
function isGiftExpired(gift) {
|
|
668
|
+
return gift.expiresAt <= Date.now();
|
|
669
|
+
}
|
|
670
|
+
function isGiftBudgetExhausted(gift) {
|
|
671
|
+
return gift.usedTokens >= gift.maxTokens;
|
|
672
|
+
}
|
|
673
|
+
function giftBudgetRemaining(gift) {
|
|
674
|
+
return Math.max(0, gift.maxTokens - gift.usedTokens);
|
|
675
|
+
}
|
|
676
|
+
function giftBudgetPercent(gift) {
|
|
677
|
+
if (gift.maxTokens === 0) return 100;
|
|
678
|
+
return Math.min(100, Math.round(gift.usedTokens / gift.maxTokens * 100));
|
|
679
|
+
}
|
|
680
|
+
function createGiftLink(gift) {
|
|
681
|
+
const provider = PROVIDERS[gift.providerId];
|
|
682
|
+
const link = {
|
|
683
|
+
v: 1,
|
|
684
|
+
id: gift.id,
|
|
685
|
+
p: gift.providerId,
|
|
686
|
+
n: provider?.name ?? gift.providerId,
|
|
687
|
+
s: gift.label,
|
|
688
|
+
t: gift.authToken,
|
|
689
|
+
m: gift.maxTokens,
|
|
690
|
+
e: gift.expiresAt,
|
|
691
|
+
r: gift.relayUrl
|
|
692
|
+
};
|
|
693
|
+
return { encoded: encodeGiftLink(link), link };
|
|
694
|
+
}
|
|
695
|
+
function base64UrlEncode(bytes) {
|
|
696
|
+
let binary = "";
|
|
697
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
698
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
699
|
+
}
|
|
700
|
+
function base64UrlDecode(str) {
|
|
701
|
+
const padded = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
702
|
+
const binary = atob(padded);
|
|
703
|
+
const bytes = new Uint8Array(binary.length);
|
|
704
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
705
|
+
return bytes;
|
|
706
|
+
}
|
|
260
707
|
// Annotate the CommonJS export names for ESM import in node:
|
|
261
708
|
0 && (module.exports = {
|
|
262
709
|
BYOKY_MESSAGE_PREFIX,
|
|
263
710
|
BYOKY_PROVIDER_KEY,
|
|
264
711
|
ByokyError,
|
|
265
712
|
ByokyErrorCode,
|
|
713
|
+
MIN_PASSWORD_LENGTH,
|
|
266
714
|
PROVIDERS,
|
|
715
|
+
WS_READY_STATE,
|
|
716
|
+
buildHeaders,
|
|
717
|
+
checkPasswordStrength,
|
|
718
|
+
computeAllowanceCheck,
|
|
267
719
|
createConnectRequest,
|
|
268
720
|
createConnectResponse,
|
|
269
721
|
createErrorMessage,
|
|
722
|
+
createGiftLink,
|
|
270
723
|
createMessage,
|
|
724
|
+
decodeGiftLink,
|
|
271
725
|
decrypt,
|
|
272
726
|
deriveKey,
|
|
727
|
+
encodeGiftLink,
|
|
273
728
|
encrypt,
|
|
729
|
+
extractUsageFromParsed,
|
|
274
730
|
getProvider,
|
|
275
731
|
getProviderIds,
|
|
732
|
+
giftBudgetPercent,
|
|
733
|
+
giftBudgetRemaining,
|
|
734
|
+
giftLinkToUrl,
|
|
276
735
|
hashPassword,
|
|
277
736
|
isByokyMessage,
|
|
737
|
+
isGiftBudgetExhausted,
|
|
738
|
+
isGiftExpired,
|
|
278
739
|
maskKey,
|
|
740
|
+
parseModel,
|
|
741
|
+
parseRelayMessage,
|
|
742
|
+
parseUsage,
|
|
743
|
+
sendRelayMessage,
|
|
744
|
+
validateGiftLink,
|
|
745
|
+
validateProxyUrl,
|
|
279
746
|
verifyPassword
|
|
280
747
|
});
|
|
281
748
|
//# sourceMappingURL=index.cjs.map
|