omp-multi-auth 1.0.0 → 1.0.1
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 +1 -3
- package/extensions/lib/core.ts +4 -2
- package/extensions/lib/quota.ts +289 -15
- package/extensions/multi-auth.ts +15 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -98,9 +98,7 @@ Run `/multi-auth limits` to inspect quota and usage information for supported pr
|
|
|
98
98
|
| Global | `~/.omp/agent/multi-auth.json` | Subscriptions and presets |
|
|
99
99
|
| Project | `.omp/multi-auth.json` | `allowedSubs` allow-list |
|
|
100
100
|
|
|
101
|
-
##
|
|
102
|
-
|
|
103
|
-
`MULTI_SUB` identifies the subscription provider name used by the extension when configured by omp.
|
|
101
|
+
## Credits
|
|
104
102
|
|
|
105
103
|
Originally based on [`pi-multi-pass`](https://github.com/hjanuschka/pi-multi-pass).
|
|
106
104
|
|
package/extensions/lib/core.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Cross-module types and zero-dependency helpers.
|
|
2
2
|
import type { ExtensionCommandContext, ExtensionContext, AuthStorage } from "@oh-my-pi/pi-coding-agent";
|
|
3
|
-
import { getBundledModels } from "@oh-my-pi/pi-catalog
|
|
4
|
-
import type { GeneratedProvider } from "@oh-my-pi/pi-catalog/models";
|
|
3
|
+
import { getBundledModels, type GeneratedProvider } from "@oh-my-pi/pi-catalog";
|
|
5
4
|
import type { Api, Model } from "@oh-my-pi/pi-ai";
|
|
5
|
+
import type { GoogleQuotaAccountSnapshot, CodexUsageSnapshot } from "./quota.ts";
|
|
6
6
|
|
|
7
7
|
export function getModels(providerId: string): Model<Api>[] {
|
|
8
8
|
return getBundledModels(providerId as GeneratedProvider) as Model<Api>[];
|
|
@@ -38,6 +38,8 @@ export interface QuotaCheckResult {
|
|
|
38
38
|
summary: string;
|
|
39
39
|
details: string[];
|
|
40
40
|
score: number;
|
|
41
|
+
googleSnapshot?: GoogleQuotaAccountSnapshot;
|
|
42
|
+
codexSnapshot?: CodexUsageSnapshot;
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
export interface ProviderQuotaChecker {
|
package/extensions/lib/quota.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { ExtensionCommandContext, ExtensionContext, AuthStorage } from "@oh
|
|
|
5
5
|
import { BorderedLoader } from "@oh-my-pi/pi-coding-agent";
|
|
6
6
|
import type { Api, Model } from "@oh-my-pi/pi-ai";
|
|
7
7
|
import { getBaseProvider, subDisplayName, PROVIDER_TEMPLATES } from "./providers.ts";
|
|
8
|
-
import { getAuthStorage, subProviderName, type AuthStorageEntry, type MultiAuthConfig, type QuotaAccount, type QuotaCheckResult, type QuotaStatusKind, type ProviderQuotaChecker } from "./core.ts";
|
|
8
|
+
import { getAuthStorage, getModels, subProviderName, type AuthStorageEntry, type MultiAuthConfig, type QuotaAccount, type QuotaCheckResult, type QuotaStatusKind, type ProviderQuotaChecker } from "./core.ts";
|
|
9
9
|
import { loadGlobalConfig, loadProjectConfig, parseEnvConfig, mergeConfigs, normalizeEntries } from "./config.ts";
|
|
10
10
|
import { getWrappedSelectIndex, showWrappedSelect } from "./ui.ts";
|
|
11
11
|
import type { SelectItem } from "@oh-my-pi/pi-tui";
|
|
@@ -73,6 +73,7 @@ export interface GoogleQuotaModelSnapshot {
|
|
|
73
73
|
model: string;
|
|
74
74
|
remainingPercent?: number;
|
|
75
75
|
resetAt?: number;
|
|
76
|
+
key?: string;
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
export interface GoogleQuotaAccountSnapshot {
|
|
@@ -338,6 +339,7 @@ export function formatQuotaOverview(results: QuotaCheckResult[]): string {
|
|
|
338
339
|
export function formatQuotaCurrentHint(
|
|
339
340
|
results: QuotaCheckResult[],
|
|
340
341
|
currentProviderName: string | undefined,
|
|
342
|
+
currentModel?: { id: string; name: string },
|
|
341
343
|
): string | undefined {
|
|
342
344
|
if (!currentProviderName) return undefined;
|
|
343
345
|
|
|
@@ -345,6 +347,13 @@ export function formatQuotaCurrentHint(
|
|
|
345
347
|
if (!current) return undefined;
|
|
346
348
|
|
|
347
349
|
let hint = `Current: ${current.account.displayName} is ${formatQuotaKind(current.kind)}`;
|
|
350
|
+
if (current.googleSnapshot && currentModel) {
|
|
351
|
+
const matches = matchGoogleQuotaModels(current.account.baseProvider, currentModel, current.googleSnapshot);
|
|
352
|
+
const worst = pickWorstQuotaModel(matches);
|
|
353
|
+
if (worst) {
|
|
354
|
+
hint += ` • ${worst.model} ${formatRemainingPercent(worst.remainingPercent)} left`;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
348
357
|
const best = results[0];
|
|
349
358
|
if (best && best.account.providerName !== current.account.providerName) {
|
|
350
359
|
hint += ` • best available: ${best.account.displayName}`;
|
|
@@ -435,7 +444,7 @@ export async function selectQuotaResult(
|
|
|
435
444
|
subtitle: [
|
|
436
445
|
"Select an account to inspect its full quota windows.",
|
|
437
446
|
formatQuotaOverview(results),
|
|
438
|
-
formatQuotaCurrentHint(results, currentProviderName),
|
|
447
|
+
formatQuotaCurrentHint(results, currentProviderName, ctx.model),
|
|
439
448
|
].filter(Boolean).join("\n"),
|
|
440
449
|
items: buildQuotaSelectItems(results, currentProviderName),
|
|
441
450
|
initialValue: currentProviderName,
|
|
@@ -470,14 +479,18 @@ export function updateGoogleQuotaModel(
|
|
|
470
479
|
model: string,
|
|
471
480
|
remainingPercent: number | undefined,
|
|
472
481
|
resetAt: number | undefined,
|
|
482
|
+
key?: string,
|
|
473
483
|
): void {
|
|
474
484
|
const existing = modelsByName.get(model);
|
|
475
485
|
if (!existing) {
|
|
476
|
-
modelsByName.set(model, { model, remainingPercent, resetAt });
|
|
486
|
+
modelsByName.set(model, { model, remainingPercent, resetAt, key });
|
|
477
487
|
return;
|
|
478
488
|
}
|
|
479
489
|
|
|
480
490
|
let next = existing;
|
|
491
|
+
if (existing.key === undefined && key !== undefined) {
|
|
492
|
+
next = { ...next, key };
|
|
493
|
+
}
|
|
481
494
|
if (remainingPercent !== undefined) {
|
|
482
495
|
if (existing.remainingPercent === undefined || remainingPercent < existing.remainingPercent) {
|
|
483
496
|
next = { ...next, remainingPercent };
|
|
@@ -532,20 +545,69 @@ export function parseGoogleGeminiQuotaSnapshot(
|
|
|
532
545
|
|
|
533
546
|
for (const bucketValue of buckets) {
|
|
534
547
|
const bucket = getRecord(bucketValue);
|
|
535
|
-
const
|
|
536
|
-
|
|
537
|
-
|
|
548
|
+
const rawKey = typeof bucket?.modelId === "string" ? bucket.modelId : undefined;
|
|
549
|
+
if (rawKey && (rawKey.toLowerCase().includes("placeholder") || rawKey.toLowerCase().startsWith("tab_"))) {
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
const model = getGoogleGeminiModelLabel(rawKey);
|
|
553
|
+
if (model.toLowerCase().includes("placeholder")) continue;
|
|
538
554
|
const remainingPercent = normalizeGoogleRemainingPercent(bucket?.remainingFraction);
|
|
539
555
|
const resetAt = typeof bucket?.resetTime === "string"
|
|
540
556
|
? parseIsoTimestampSeconds(bucket.resetTime)
|
|
541
557
|
: undefined;
|
|
542
558
|
if (remainingPercent === undefined && resetAt === undefined) continue;
|
|
543
|
-
updateGoogleQuotaModel(modelsByName, model, remainingPercent, resetAt);
|
|
559
|
+
updateGoogleQuotaModel(modelsByName, model, remainingPercent, resetAt, rawKey);
|
|
544
560
|
}
|
|
545
561
|
|
|
546
562
|
return buildGoogleQuotaSnapshot(GOOGLE_GEMINI_QUOTA_ENDPOINT, projectId, modelsByName);
|
|
547
563
|
}
|
|
548
564
|
|
|
565
|
+
export function isGoogleAntigravityPlaceholder(value: string | undefined): boolean {
|
|
566
|
+
if (!value) return true;
|
|
567
|
+
const lower = value.toLowerCase();
|
|
568
|
+
return lower.includes("placeholder") || lower.startsWith("model_");
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
export function isGoogleAntigravityHiddenModel(modelKey: string, displayName?: string): boolean {
|
|
572
|
+
const lowerKey = modelKey.toLowerCase();
|
|
573
|
+
if (
|
|
574
|
+
lowerKey.startsWith("tab_") ||
|
|
575
|
+
lowerKey.startsWith("chat_") ||
|
|
576
|
+
lowerKey.includes("placeholder") ||
|
|
577
|
+
GOOGLE_ANTIGRAVITY_HIDDEN_MODELS.has(lowerKey)
|
|
578
|
+
) {
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
if (displayName) {
|
|
582
|
+
const lowerName = displayName.toLowerCase();
|
|
583
|
+
if (
|
|
584
|
+
lowerName.includes("placeholder") ||
|
|
585
|
+
GOOGLE_ANTIGRAVITY_HIDDEN_MODELS.has(lowerName)
|
|
586
|
+
) {
|
|
587
|
+
return true;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return false;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
export function formatAntigravityModelKey(key: string): string {
|
|
594
|
+
const base = key.replace(/-tiered$/, "");
|
|
595
|
+
try {
|
|
596
|
+
const bundled = getModels("google-antigravity");
|
|
597
|
+
const exact = bundled.find((m) => m.id === key);
|
|
598
|
+
if (exact) return exact.name;
|
|
599
|
+
const baseMatch = bundled.find((m) => m.id === base);
|
|
600
|
+
if (baseMatch) return baseMatch.name;
|
|
601
|
+
} catch {
|
|
602
|
+
// Fall back to title-casing below
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return base
|
|
606
|
+
.split("-")
|
|
607
|
+
.map((part) => (part.length > 0 ? part[0].toUpperCase() + part.slice(1) : part))
|
|
608
|
+
.join(" ");
|
|
609
|
+
}
|
|
610
|
+
|
|
549
611
|
export function parseGoogleAntigravityQuotaSnapshot(
|
|
550
612
|
data: unknown,
|
|
551
613
|
endpoint: string,
|
|
@@ -559,26 +621,95 @@ export function parseGoogleAntigravityQuotaSnapshot(
|
|
|
559
621
|
for (const [modelKey, modelValue] of Object.entries(rawModels)) {
|
|
560
622
|
const model = getRecord(modelValue);
|
|
561
623
|
if (model?.isInternal === true) continue;
|
|
562
|
-
if (
|
|
563
|
-
|
|
624
|
+
if (isGoogleAntigravityHiddenModel(modelKey)) continue;
|
|
625
|
+
|
|
626
|
+
const rawDisplayName = typeof model?.displayName === "string" && model.displayName.length > 0
|
|
564
627
|
? model.displayName
|
|
565
|
-
: typeof model?.model === "string" && model.model.length > 0
|
|
628
|
+
: typeof model?.model === "string" && model.model.length > 0 && !isGoogleAntigravityPlaceholder(model.model)
|
|
566
629
|
? model.model
|
|
567
|
-
:
|
|
568
|
-
|
|
630
|
+
: undefined;
|
|
631
|
+
|
|
632
|
+
const displayName = (rawDisplayName && !isGoogleAntigravityPlaceholder(rawDisplayName))
|
|
633
|
+
? rawDisplayName
|
|
634
|
+
: formatAntigravityModelKey(modelKey);
|
|
635
|
+
|
|
636
|
+
if (isGoogleAntigravityHiddenModel(modelKey, displayName)) continue;
|
|
637
|
+
|
|
569
638
|
const quotaInfo = getRecord(model?.quotaInfo);
|
|
570
639
|
const remainingPercent = normalizeGoogleRemainingPercent(quotaInfo?.remainingFraction);
|
|
571
640
|
const resetAt = typeof quotaInfo?.resetTime === "string"
|
|
572
641
|
? parseIsoTimestampSeconds(quotaInfo.resetTime)
|
|
573
642
|
: undefined;
|
|
574
643
|
if (remainingPercent === undefined && resetAt === undefined) continue;
|
|
575
|
-
updateGoogleQuotaModel(modelsByName, displayName, remainingPercent, resetAt);
|
|
644
|
+
updateGoogleQuotaModel(modelsByName, displayName, remainingPercent, resetAt, modelKey);
|
|
576
645
|
}
|
|
577
646
|
}
|
|
578
647
|
|
|
579
648
|
return buildGoogleQuotaSnapshot(endpoint, projectId, modelsByName);
|
|
580
649
|
}
|
|
581
650
|
|
|
651
|
+
export function normalizeModelToken(value: string): string {
|
|
652
|
+
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export function stripCloneSuffix(name: string): string {
|
|
656
|
+
return name.replace(/\s*\(#\d+\)\s*$/, "");
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
export function getGeminiFamily(model: { id: string; name: string }): "pro" | "flash" | undefined {
|
|
660
|
+
const idNorm = normalizeModelToken(model.id);
|
|
661
|
+
const nameNorm = normalizeModelToken(model.name);
|
|
662
|
+
if (idNorm.includes("pro") || nameNorm.includes("pro")) return "pro";
|
|
663
|
+
if (idNorm.includes("flash") || nameNorm.includes("flash")) return "flash";
|
|
664
|
+
return undefined;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
export function matchGoogleQuotaModels(
|
|
668
|
+
baseProvider: string,
|
|
669
|
+
model: { id: string; name: string },
|
|
670
|
+
snapshot: GoogleQuotaAccountSnapshot,
|
|
671
|
+
): GoogleQuotaModelSnapshot[] {
|
|
672
|
+
if (baseProvider === "google-gemini-cli") {
|
|
673
|
+
const family = getGeminiFamily(model);
|
|
674
|
+
if (!family) return [];
|
|
675
|
+
return snapshot.models.filter((bucket) => bucket.model.toLowerCase() === family);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const mid = normalizeModelToken(model.id);
|
|
679
|
+
const mname = normalizeModelToken(stripCloneSuffix(model.name));
|
|
680
|
+
|
|
681
|
+
return snapshot.models.filter((bucket) => {
|
|
682
|
+
const bkey = normalizeModelToken(bucket.key ?? "");
|
|
683
|
+
const blabel = normalizeModelToken(bucket.model);
|
|
684
|
+
|
|
685
|
+
if (bkey && mid && bkey === mid) return true;
|
|
686
|
+
if (blabel && mname && blabel === mname) return true;
|
|
687
|
+
|
|
688
|
+
if (bkey && mid && Math.min(bkey.length, mid.length) >= 4) {
|
|
689
|
+
if (bkey.startsWith(mid) || mid.startsWith(bkey)) return true;
|
|
690
|
+
}
|
|
691
|
+
if (blabel && mname && Math.min(blabel.length, mname.length) >= 4) {
|
|
692
|
+
if (blabel.startsWith(mname) || mname.startsWith(blabel)) return true;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
return false;
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
export function pickWorstQuotaModel(models: GoogleQuotaModelSnapshot[]): GoogleQuotaModelSnapshot | undefined {
|
|
700
|
+
if (models.length === 0) return undefined;
|
|
701
|
+
let worst = models[0];
|
|
702
|
+
for (let i = 1; i < models.length; i++) {
|
|
703
|
+
const current = models[i];
|
|
704
|
+
const currentPercent = current.remainingPercent ?? 101;
|
|
705
|
+
const worstPercent = worst.remainingPercent ?? 101;
|
|
706
|
+
if (currentPercent < worstPercent) {
|
|
707
|
+
worst = current;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return worst;
|
|
711
|
+
}
|
|
712
|
+
|
|
582
713
|
export function classifyGoogleQuotaKind(snapshot: GoogleQuotaAccountSnapshot): {
|
|
583
714
|
kind: QuotaStatusKind;
|
|
584
715
|
score: number;
|
|
@@ -713,6 +844,7 @@ export function formatGoogleQuotaDetails(
|
|
|
713
844
|
account: QuotaAccount,
|
|
714
845
|
snapshot: GoogleQuotaAccountSnapshot,
|
|
715
846
|
kind: QuotaStatusKind,
|
|
847
|
+
currentModel?: { id: string; name: string },
|
|
716
848
|
): string[] {
|
|
717
849
|
const details = [
|
|
718
850
|
`account: ${account.displayName}`,
|
|
@@ -725,13 +857,29 @@ export function formatGoogleQuotaDetails(
|
|
|
725
857
|
if (snapshot.worstRemainingPercent !== undefined) {
|
|
726
858
|
details.push(`bottleneck: ${formatRemainingPercent(snapshot.worstRemainingPercent)} left`);
|
|
727
859
|
}
|
|
860
|
+
|
|
861
|
+
let currentModelLabels = new Set<string>();
|
|
862
|
+
if (currentModel) {
|
|
863
|
+
const matches = matchGoogleQuotaModels(account.baseProvider, currentModel, snapshot);
|
|
864
|
+
const worst = pickWorstQuotaModel(matches);
|
|
865
|
+
if (worst) {
|
|
866
|
+
details.push(
|
|
867
|
+
`current model (${worst.model}): ${formatRemainingPercent(worst.remainingPercent)} left, resets ${formatResetLong(worst.resetAt)}`,
|
|
868
|
+
);
|
|
869
|
+
currentModelLabels = new Set(matches.map((m) => m.model));
|
|
870
|
+
} else {
|
|
871
|
+
details.push(`current model (${currentModel.name}): not tracked by this provider`);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
728
875
|
for (const model of [...snapshot.models].sort((left, right) => {
|
|
729
876
|
const leftPercent = left.remainingPercent ?? 101;
|
|
730
877
|
const rightPercent = right.remainingPercent ?? 101;
|
|
731
878
|
return leftPercent - rightPercent || left.model.localeCompare(right.model);
|
|
732
879
|
})) {
|
|
880
|
+
const suffix = currentModelLabels.has(model.model) ? " ← current" : "";
|
|
733
881
|
details.push(
|
|
734
|
-
`${model.model}: ${formatRemainingPercent(model.remainingPercent)} left, resets ${formatResetLong(model.resetAt)}`,
|
|
882
|
+
`${model.model}: ${formatRemainingPercent(model.remainingPercent)} left, resets ${formatResetLong(model.resetAt)}${suffix}`,
|
|
735
883
|
);
|
|
736
884
|
}
|
|
737
885
|
details.push(`endpoint: ${snapshot.endpoint}`);
|
|
@@ -811,6 +959,7 @@ export async function checkGoogleQuotaAccount(
|
|
|
811
959
|
summary: `${getGoogleQuotaBucketLabel(account, snapshot.models.length)} | bottleneck ${formatRemainingPercent(snapshot.worstRemainingPercent)} | ${formatQuotaKind(classification.kind)}`,
|
|
812
960
|
details: formatGoogleQuotaDetails(account, snapshot, classification.kind),
|
|
813
961
|
score: classification.score,
|
|
962
|
+
googleSnapshot: snapshot,
|
|
814
963
|
};
|
|
815
964
|
} catch (error: unknown) {
|
|
816
965
|
if (signal?.aborted || isAbortError(error)) throw error;
|
|
@@ -957,6 +1106,7 @@ export const codexQuotaChecker: ProviderQuotaChecker = {
|
|
|
957
1106
|
summary,
|
|
958
1107
|
details,
|
|
959
1108
|
score: classification.score,
|
|
1109
|
+
codexSnapshot: snapshot,
|
|
960
1110
|
};
|
|
961
1111
|
} catch (error: unknown) {
|
|
962
1112
|
if (signal?.aborted || isAbortError(error)) throw error;
|
|
@@ -1001,10 +1151,14 @@ export async function showQuotaDetails(
|
|
|
1001
1151
|
ctx: ExtensionCommandContext,
|
|
1002
1152
|
result: QuotaCheckResult,
|
|
1003
1153
|
): Promise<void> {
|
|
1154
|
+
const details = (result.googleSnapshot && result.account.providerName === ctx.model?.provider && ctx.model)
|
|
1155
|
+
? formatGoogleQuotaDetails(result.account, result.googleSnapshot, result.kind, ctx.model)
|
|
1156
|
+
: result.details;
|
|
1157
|
+
|
|
1004
1158
|
await showWrappedSelect(ctx, {
|
|
1005
1159
|
title: `Limit Details: ${result.account.displayName}`,
|
|
1006
1160
|
subtitle: "Press Enter or Escape to go back to the limits list.",
|
|
1007
|
-
items:
|
|
1161
|
+
items: details.map((detail, index) => ({ value: `${index}:${detail}`, label: detail })),
|
|
1008
1162
|
confirmHint: "back",
|
|
1009
1163
|
cancelHint: "back",
|
|
1010
1164
|
});
|
|
@@ -1044,5 +1198,125 @@ export async function handleSubsLimits(ctx: ExtensionCommandContext): Promise<vo
|
|
|
1044
1198
|
}
|
|
1045
1199
|
}
|
|
1046
1200
|
|
|
1201
|
+
export const QUOTA_STATUS_KEY = "multi-auth-quota";
|
|
1202
|
+
|
|
1203
|
+
export function formatCurrentModelQuota(
|
|
1204
|
+
result: QuotaCheckResult,
|
|
1205
|
+
model: { id: string; name: string; provider: string },
|
|
1206
|
+
): string | undefined {
|
|
1207
|
+
if (result.kind === "missing-auth" || result.kind === "error") {
|
|
1208
|
+
return undefined;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
if (result.googleSnapshot) {
|
|
1212
|
+
const matches = matchGoogleQuotaModels(result.account.baseProvider, model, result.googleSnapshot);
|
|
1213
|
+
const worst = pickWorstQuotaModel(matches);
|
|
1214
|
+
if (!worst) return undefined;
|
|
1215
|
+
return `${worst.model} ${formatRemainingPercent(worst.remainingPercent)} ${formatResetShort(worst.resetAt)}`;
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
if (result.codexSnapshot) {
|
|
1219
|
+
const s = result.codexSnapshot;
|
|
1220
|
+
const five = getCodexWindowRemaining(s.fiveHour);
|
|
1221
|
+
const week = getCodexWindowRemaining(s.weekly);
|
|
1222
|
+
let windowLabel: string | undefined;
|
|
1223
|
+
let remaining: number | undefined;
|
|
1224
|
+
let resetAt: number | undefined;
|
|
1225
|
+
|
|
1226
|
+
if (five !== undefined && week !== undefined) {
|
|
1227
|
+
if (five <= week) {
|
|
1228
|
+
windowLabel = "5h";
|
|
1229
|
+
remaining = five;
|
|
1230
|
+
resetAt = s.fiveHour?.resetAt;
|
|
1231
|
+
} else {
|
|
1232
|
+
windowLabel = "7d";
|
|
1233
|
+
remaining = week;
|
|
1234
|
+
resetAt = s.weekly?.resetAt;
|
|
1235
|
+
}
|
|
1236
|
+
} else if (five !== undefined) {
|
|
1237
|
+
windowLabel = "5h";
|
|
1238
|
+
remaining = five;
|
|
1239
|
+
resetAt = s.fiveHour?.resetAt;
|
|
1240
|
+
} else if (week !== undefined) {
|
|
1241
|
+
windowLabel = "7d";
|
|
1242
|
+
remaining = week;
|
|
1243
|
+
resetAt = s.weekly?.resetAt;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
if (windowLabel === undefined || remaining === undefined) return undefined;
|
|
1247
|
+
return `${windowLabel} ${formatRemainingPercent(remaining)} ${formatResetShort(resetAt)}`;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
return undefined;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
const statusQuotaCache = new Map<string, { at: number; result: QuotaCheckResult }>();
|
|
1254
|
+
const statusQuotaInFlight = new Map<string, Promise<QuotaCheckResult | undefined>>();
|
|
1255
|
+
export const STATUS_QUOTA_TTL_MS = 60_000;
|
|
1256
|
+
|
|
1257
|
+
export async function fetchStatusQuotaResult(
|
|
1258
|
+
ctx: ExtensionContext,
|
|
1259
|
+
providerName: string,
|
|
1260
|
+
baseProvider: string,
|
|
1261
|
+
): Promise<QuotaCheckResult | undefined> {
|
|
1262
|
+
const inFlight = statusQuotaInFlight.get(providerName);
|
|
1263
|
+
if (inFlight) return inFlight;
|
|
1264
|
+
|
|
1265
|
+
const fetchPromise = (async () => {
|
|
1266
|
+
try {
|
|
1267
|
+
const authStorage = getAuthStorage(ctx);
|
|
1268
|
+
const account: QuotaAccount = {
|
|
1269
|
+
providerName,
|
|
1270
|
+
baseProvider,
|
|
1271
|
+
displayName: providerName,
|
|
1272
|
+
auth: authStorage.get(providerName) as AuthStorageEntry | undefined,
|
|
1273
|
+
};
|
|
1274
|
+
const checker = PROVIDER_QUOTA_CHECKERS.find((c) => c.baseProvider === baseProvider);
|
|
1275
|
+
if (!checker) return undefined;
|
|
1276
|
+
const result = await checker.check(account, authStorage);
|
|
1277
|
+
statusQuotaCache.set(providerName, { at: Date.now(), result });
|
|
1278
|
+
return result;
|
|
1279
|
+
} catch {
|
|
1280
|
+
return undefined;
|
|
1281
|
+
} finally {
|
|
1282
|
+
statusQuotaInFlight.delete(providerName);
|
|
1283
|
+
}
|
|
1284
|
+
})();
|
|
1285
|
+
|
|
1286
|
+
statusQuotaInFlight.set(providerName, fetchPromise);
|
|
1287
|
+
return fetchPromise;
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
export function invalidateStatusQuota(providerName: string): void {
|
|
1291
|
+
statusQuotaCache.delete(providerName);
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
export function refreshQuotaStatusLine(ctx: ExtensionContext): void {
|
|
1295
|
+
if (!ctx.hasUI) return;
|
|
1296
|
+
const model = ctx.model;
|
|
1297
|
+
if (!model) {
|
|
1298
|
+
ctx.ui.setStatus(QUOTA_STATUS_KEY, undefined);
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
const base = getBaseProvider(model.provider);
|
|
1302
|
+
if (!base || !PROVIDER_QUOTA_CHECKERS.some((c) => c.baseProvider === base)) {
|
|
1303
|
+
ctx.ui.setStatus(QUOTA_STATUS_KEY, undefined);
|
|
1304
|
+
return;
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
const cached = statusQuotaCache.get(model.provider);
|
|
1308
|
+
if (cached) {
|
|
1309
|
+
ctx.ui.setStatus(QUOTA_STATUS_KEY, formatCurrentModelQuota(cached.result, model) ?? undefined);
|
|
1310
|
+
}
|
|
1311
|
+
if (!cached || Date.now() - cached.at > STATUS_QUOTA_TTL_MS) {
|
|
1312
|
+
fetchStatusQuotaResult(ctx, model.provider, base).then((result) => {
|
|
1313
|
+
if (!result) return;
|
|
1314
|
+
if (ctx.model?.provider === model.provider && ctx.model) {
|
|
1315
|
+
ctx.ui.setStatus(QUOTA_STATUS_KEY, formatCurrentModelQuota(result, ctx.model) ?? undefined);
|
|
1316
|
+
}
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1047
1321
|
// ==========================================================================
|
|
1048
1322
|
// Config persistence (~/.omp/agent/multi-auth.json)
|
package/extensions/multi-auth.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { ExtensionAPI, ExtensionCommandContext, ExtensionContext } from "@o
|
|
|
2
2
|
import { loadGlobalConfig, parseEnvConfig, mergeConfigs, normalizeEntries, loadEffectiveConfig, formatAllowedProviderSummary, getProjectScopedProviderNames, findSelectableModelForProvider, getProviderDisplayName } from "./lib/config.ts";
|
|
3
3
|
import { registerSub, rewriteAntigravitySystemInstruction } from "./lib/providers.ts";
|
|
4
4
|
import { handleSubsMenu, handleSubsList, handleSubsAdd, handleSubsRemove, handleSubsLogin, handleSubsLogout, handleSubsSwitch, handleSubsStatus } from "./lib/commands-subs.ts";
|
|
5
|
-
import { handleSubsLimits } from "./lib/quota.ts";
|
|
5
|
+
import { handleSubsLimits, refreshQuotaStatusLine, invalidateStatusQuota } from "./lib/quota.ts";
|
|
6
6
|
import { handlePresetActivate, handlePresetCreate, handlePresetList, handlePresetToggle, handlePresetRemove, handlePresetMenu } from "./lib/commands-preset.ts";
|
|
7
7
|
|
|
8
8
|
// ========================================================================
|
|
@@ -77,6 +77,11 @@ export default function multiSub(pi: ExtensionAPI) {
|
|
|
77
77
|
ctx.ui.setStatus("multi-auth", `allowed ${allowedSummary}`);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
refreshQuotaStatusLine(ctx);
|
|
81
|
+
if ("setInterval" in ctx && typeof ctx.setInterval === "function") {
|
|
82
|
+
ctx.setInterval(() => refreshQuotaStatusLine(ctx), 60_000);
|
|
83
|
+
}
|
|
84
|
+
|
|
80
85
|
await enforceProjectRestriction(ctx, "session");
|
|
81
86
|
});
|
|
82
87
|
|
|
@@ -89,9 +94,18 @@ export default function multiSub(pi: ExtensionAPI) {
|
|
|
89
94
|
return { action: "continue" as const };
|
|
90
95
|
}
|
|
91
96
|
const ok = await enforceProjectRestriction(ctx, "input");
|
|
97
|
+
if (ok) {
|
|
98
|
+
refreshQuotaStatusLine(ctx);
|
|
99
|
+
}
|
|
92
100
|
return ok ? { action: "continue" as const } : { action: "handled" as const };
|
|
93
101
|
});
|
|
94
102
|
|
|
103
|
+
pi.on("agent_end", (_event, ctx) => {
|
|
104
|
+
if (ctx.model) {
|
|
105
|
+
invalidateStatusQuota(ctx.model.provider);
|
|
106
|
+
}
|
|
107
|
+
refreshQuotaStatusLine(ctx);
|
|
108
|
+
});
|
|
95
109
|
// Register /multi-auth command
|
|
96
110
|
pi.registerCommand("multi-auth", {
|
|
97
111
|
description: "Manage multi-account OAuth subscriptions and accounts",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "omp-multi-auth",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Multi-account OAuth login for omp (Oh My Pi) -- add and manage multiple accounts per provider from one place.",
|
|
6
6
|
"keywords": [
|
|
@@ -20,12 +20,15 @@
|
|
|
20
20
|
"author": "hjanuschka",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/tuandinh0801/omp-multi-auth"
|
|
23
|
+
"url": "git+https://github.com/tuandinh0801/omp-multi-auth.git"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"extensions",
|
|
27
27
|
"README.md"
|
|
28
28
|
],
|
|
29
|
+
"scripts": {
|
|
30
|
+
"test": "node tests/run-all.mjs"
|
|
31
|
+
},
|
|
29
32
|
"publishConfig": {
|
|
30
33
|
"access": "public"
|
|
31
34
|
},
|