66 */
77import type { ModelSelection , ServerProvider , ServerProviderUsageWindow } from "@t3tools/contracts" ;
88
9+ import { exhaustedUsageWindow , rankLimitSwitchCandidates } from "@t3tools/shared/usageLimitSwitch" ;
10+
911import { resolveProviderInstanceDisplayName } from "./providerInstanceDisplay.ts" ;
1012
1113export interface LimitSwitchTarget {
@@ -15,12 +17,6 @@ export interface LimitSwitchTarget {
1517 readonly remainingPercent : number | null ;
1618}
1719
18- function remainingPercent ( provider : ServerProvider ) : number | null {
19- const usage = provider . usageLimits ;
20- if ( ! usage || usage . unavailable || usage . windows . length === 0 ) return null ;
21- return Math . max ( 0 , 100 - Math . max ( ...usage . windows . map ( ( window ) => window . usedPercent ) ) ) ;
22- }
23-
2420/**
2521 * The window an account is closest to running out of, for a compact "where do I
2622 * stand" readout. Null when the account reports no windows, so the caller shows
@@ -46,86 +42,21 @@ export function tightestUsageWindow(
4642 : null ;
4743}
4844
49- /** The used-up window that reopens last, which is the one the account is waiting on. */
50- function exhaustedWindow (
51- provider : ServerProvider ,
52- nowMs : number ,
53- ) : ServerProviderUsageWindow | undefined {
54- return provider . usageLimits ?. windows
55- . filter (
56- ( window ) =>
57- window . usedPercent >= 100 &&
58- ( window . resetsAt === undefined || Date . parse ( window . resetsAt ) > nowMs ) ,
59- )
60- . reduce < ServerProviderUsageWindow | undefined > (
61- ( latest , window ) =>
62- latest === undefined ||
63- Date . parse ( window . resetsAt ?? "" ) > Date . parse ( latest . resetsAt ?? "" ) ||
64- window . resetsAt === undefined
65- ? window
66- : latest ,
67- undefined ,
68- ) ;
69- }
70-
71- function isExhausted ( provider : ServerProvider , nowMs : number ) : boolean {
72- return exhaustedWindow ( provider , nowMs ) !== undefined ;
73- }
74-
7545/**
7646 * Other ready, signed-in instances of the current instance's driver that offer
77- * the thread's model and are not known to be out of quota. Instances sharing the
78- * thread's continuation group come first, since they resume the native thread;
79- * after that, the most headroom wins.
47+ * the thread's model and are not known to be out of quota, best first. See
48+ * `rankLimitSwitchCandidates` for the order.
8049 */
8150export function listLimitSwitchTargets ( input : {
8251 readonly providers : ReadonlyArray < ServerProvider > ;
8352 readonly current : ModelSelection ;
8453 readonly nowMs : number ;
8554} ) : ReadonlyArray < LimitSwitchTarget > {
86- const source = input . providers . find (
87- ( provider ) => provider . instanceId === input . current . instanceId ,
88- ) ;
89- if ( ! source ) return [ ] ;
90- const candidates = input . providers . flatMap ( ( provider ) => {
91- if (
92- provider . instanceId === source . instanceId ||
93- provider . driver !== source . driver ||
94- ! provider . enabled ||
95- ! provider . installed ||
96- provider . availability === "unavailable" ||
97- provider . status !== "ready" ||
98- provider . auth . status !== "authenticated" ||
99- isExhausted ( provider , input . nowMs )
100- ) {
101- return [ ] ;
102- }
103- const model = provider . models . find (
104- ( entry ) => entry . slug === input . current . model || entry . aliases ?. includes ( input . current . model ) ,
105- ) ;
106- if ( ! model ) return [ ] ;
107- const sameContinuation =
108- source . continuation !== undefined &&
109- source . continuation . groupKey === provider . continuation ?. groupKey ;
110- return [
111- {
112- sameContinuation,
113- target : {
114- selection : { ...input . current , instanceId : provider . instanceId , model : model . slug } ,
115- label : resolveProviderInstanceDisplayName ( provider ) ,
116- remainingPercent : remainingPercent ( provider ) ,
117- } satisfies LimitSwitchTarget ,
118- } ,
119- ] ;
120- } ) ;
121- return [ ...candidates ]
122- . sort (
123- ( left , right ) =>
124- Number ( right . sameContinuation ) - Number ( left . sameContinuation ) ||
125- ( right . target . remainingPercent ?? - 1 ) - ( left . target . remainingPercent ?? - 1 ) ||
126- left . target . label . localeCompare ( right . target . label ) ,
127- )
128- . map ( ( candidate ) => candidate . target ) ;
55+ return rankLimitSwitchCandidates ( input ) . map ( ( candidate ) => ( {
56+ selection : candidate . selection ,
57+ label : resolveProviderInstanceDisplayName ( candidate . provider ) ,
58+ remainingPercent : candidate . remainingPercent ,
59+ } ) ) ;
12960}
13061
13162/** At or below this headroom the selected account is worth a heads-up. */
@@ -160,7 +91,7 @@ export function suggestLimitSwitch(input: {
16091 ) ;
16192 if ( ! source ) return null ;
16293 const window =
163- exhaustedWindow ( source , input . nowMs ) ??
94+ exhaustedUsageWindow ( source , input . nowMs ) ??
16495 source . usageLimits ?. windows
16596 . filter (
16697 ( candidate ) =>
0 commit comments