@@ -34,13 +34,9 @@ import {
3434 latestRootProviderFailure ,
3535} from "@t3tools/shared/orchestrationV2ThreadError" ;
3636import type { UsageLimitSourceSnapshots } from "@t3tools/contracts" ;
37- import {
38- collectProviderUsageLimits ,
39- hasProviderUsageLimits ,
40- isUsageLimitsCommand ,
41- } from "@t3tools/shared/usageLimits" ;
37+ import { isUsageLimitsCommand } from "@t3tools/shared/usageLimits" ;
4238import { feedbackBannerItem } from "./chat/ComposerFeedback" ;
43- import { usageLimitsBannerItem } from "./chat/ComposerUsageLimits " ;
39+ import { useUsageLimitsPanel } from "./chat/useUsageLimitsPanel " ;
4440import { getTerminalLabel } from "@t3tools/shared/terminalLabels" ;
4541import * as Schema from "effect/Schema" ;
4642import {
@@ -2637,16 +2633,8 @@ export default function ChatView(props: ChatViewProps) {
26372633 hasActionableProposedPlan : hasActionableProposedPlan ( activeProposedPlan ) ,
26382634 hasComposerAttachments : composerHasAttachments ,
26392635 } ) ;
2640- // The open /usage-limits panel for this thread, model and turn. Only the open
2641- // moment is stored: the rows read live provider data, so a redeemed reset
2642- // credit or refreshed probe shows through. Anything that spends quota closes
2643- // it: a new turn from any source, or the agent resuming after an approval or
2644- // answered question.
2645- const [ usageLimitsPanel , setUsageLimitsPanel ] = useState < {
2646- readonly key : string ;
2647- readonly threadKey : string ;
2648- readonly now : number ;
2649- } | null > ( null ) ;
2636+ // A pending approval or question is part of the key: once it is answered,
2637+ // from this client or any other, the agent resumes and spends quota.
26502638 const usageLimitsKey =
26512639 activeProviderInstanceId === null || ( isServerThread && activeThread === undefined )
26522640 ? null
@@ -2656,77 +2644,20 @@ export default function ChatView(props: ChatViewProps) {
26562644 activeThread ?. latestRun ?. runId ?? "" ,
26572645 activePendingApproval ?. requestId ?? activePendingUserInput ?. requestId ?? "" ,
26582646 ] . join ( ":" ) ;
2659- if (
2660- usageLimitsPanel !== null &&
2661- usageLimitsKey !== null &&
2662- usageLimitsPanel . key !== usageLimitsKey
2663- ) {
2664- setUsageLimitsPanel ( null ) ;
2665- }
2666- const usageLimitSources = serverConfig ?. usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES ;
2667- const usageLimitsReport = useMemo (
2668- ( ) =>
2669- usageLimitsPanel !== null &&
2670- usageLimitsKey !== null &&
2671- usageLimitsPanel . key === usageLimitsKey &&
2672- activeProviderInstanceId !== null
2673- ? collectProviderUsageLimits (
2674- activeProviderInstanceId ,
2675- providerStatuses ,
2676- usageLimitSources ,
2677- usageLimitsPanel . now ,
2678- )
2679- : null ,
2680- [
2681- activeProviderInstanceId ,
2682- providerStatuses ,
2683- usageLimitSources ,
2684- usageLimitsKey ,
2685- usageLimitsPanel ,
2686- ] ,
2687- ) ;
2688- const usageLimitsBanner = useMemo (
2689- ( ) =>
2690- usageLimitsReport !== null && usageLimitsPanel !== null
2691- ? usageLimitsBannerItem (
2692- `usage-limits:${ usageLimitsPanel . key } :${ usageLimitsPanel . now } ` ,
2693- usageLimitsReport ,
2694- environmentId ,
2695- ( ) => setUsageLimitsPanel ( null ) ,
2696- )
2697- : null ,
2698- [ environmentId , usageLimitsPanel , usageLimitsReport ] ,
2699- ) ;
2700- // T3 owns /usage-limits only where Limits has data for the selected provider;
2701- // elsewhere the name stays the provider's own and is sent through untouched.
2702- const usageLimitsOffered =
2703- activeProviderStatus !== null &&
2704- hasProviderUsageLimits ( activeProviderStatus . driver , providerStatuses , usageLimitSources ) ;
2705- const openUsageLimits = ( ) => {
2706- const now = Date . now ( ) ;
2707- const report =
2708- activeProviderInstanceId !== null && usageLimitsKey !== null
2709- ? collectProviderUsageLimits (
2710- activeProviderInstanceId ,
2711- providerStatuses ,
2712- usageLimitSources ,
2713- now ,
2714- )
2715- : null ;
2716- if ( report && usageLimitsKey !== null ) {
2717- setUsageLimitsPanel ( { key : usageLimitsKey , threadKey : routeThreadKey , now } ) ;
2718- return true ;
2719- }
2720- setUsageLimitsPanel ( null ) ;
2721- toastManager . add ( { type : "info" , title : "Usage limits are unavailable for this provider" } ) ;
2722- return false ;
2723- } ;
2724- const openUsageLimitsStable = useStableCallback ( openUsageLimits ) ;
2725- // Responses can resolve after navigating away; only the originating thread's panel clears.
2726- const clearUsageLimitsFor = ( threadKey : string ) =>
2727- setUsageLimitsPanel ( ( current ) =>
2728- current !== null && current . threadKey === threadKey ? null : current ,
2729- ) ;
2647+ const {
2648+ banner : usageLimitsBanner ,
2649+ offered : usageLimitsOffered ,
2650+ open : openUsageLimits ,
2651+ clearFor : clearUsageLimitsFor ,
2652+ } = useUsageLimitsPanel ( {
2653+ environmentId,
2654+ threadKey : routeThreadKey ,
2655+ panelKey : usageLimitsKey ,
2656+ providerInstanceId : activeProviderInstanceId ,
2657+ providerDriver : activeProviderStatus ?. driver ?? null ,
2658+ providers : providerStatuses ,
2659+ sources : serverConfig ?. usageLimitSources ?? EMPTY_USAGE_LIMIT_SOURCES ,
2660+ } ) ;
27302661 const {
27312662 beginLocalDispatch,
27322663 resetLocalDispatch,
@@ -10035,7 +9966,7 @@ export default function ChatView(props: ChatViewProps) {
100359966 usageLimitsOffered &&
100369967 usageLimitsKey !== null &&
100379968 ! composerHasNonPromptContent
10038- ? openUsageLimitsStable
9969+ ? openUsageLimits
100399970 : undefined
100409971 }
100419972 environmentUnavailable = { activeEnvironmentUnavailableState }
0 commit comments