@@ -7,7 +7,13 @@ import type {
77 ProviderInteractionMode ,
88 RuntimeMode ,
99 ServerConfig as T3ServerConfig ,
10+ UsageLimitsReport ,
1011} from "@t3tools/contracts" ;
12+ import {
13+ collectProviderUsageLimits ,
14+ hasProviderUsageLimits ,
15+ isUsageLimitsCommand ,
16+ } from "@t3tools/shared/usageLimits" ;
1117import { StackActions , useFocusEffect , useNavigation } from "@react-navigation/native" ;
1218import type { ReactNode } from "react" ;
1319import {
@@ -20,7 +26,7 @@ import {
2026 useState ,
2127 type RefObject ,
2228} from "react" ;
23- import { ActivityIndicator , Platform , Pressable , View , type ViewStyle } from "react-native" ;
29+ import { ActivityIndicator , Alert , Platform , Pressable , View , type ViewStyle } from "react-native" ;
2430import { FilePreviewModal , type FilePreviewSource } from "../../components/FilePreviewModal" ;
2531import {
2632 composerAttachmentUploadBlockReason ,
@@ -124,6 +130,8 @@ export interface ThreadComposerProps {
124130 readonly onRemoveDraftImage : ( imageId : string ) => void ;
125131 readonly onStopThread : ( ) => void ;
126132 readonly onSendMessage : ( ) => Promise < MessageId | null > ;
133+ /** `/usage-limits` resolves locally; the host decides where the report shows. Null clears it. */
134+ readonly onShowUsageLimits : ( report : UsageLimitsReport | null ) => void ;
127135 readonly onUpdateModelSelection : ( modelSelection : ModelSelection ) => void ;
128136 readonly onUpdateRuntimeMode : ( runtimeMode : RuntimeMode ) => void ;
129137 readonly onUpdateInteractionMode : ( interactionMode : ProviderInteractionMode ) => void ;
@@ -336,6 +344,30 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
336344 ) ;
337345 } , [ props . serverConfig , props . selectedThread . modelSelection . instanceId ] ) ;
338346 const composerOwnerKey = scopedThreadKey ( props . environmentId , props . selectedThread . id ) ;
347+ const { onSendMessage, onChangeDraftMessage, onShowUsageLimits } = props ;
348+ // T3 owns /usage-limits only where Limits has data for the selected provider;
349+ // elsewhere the name stays the provider's own and is sent through untouched.
350+ const usageLimitsOffered =
351+ selectedProviderStatus !== null &&
352+ hasProviderUsageLimits (
353+ selectedProviderStatus . driver ,
354+ props . serverConfig ?. providers ?? [ ] ,
355+ props . serverConfig ?. usageLimitSources ?? [ ] ,
356+ ) ;
357+ // Answered locally from the last Limits snapshot; the agent never sees it.
358+ const openUsageLimits = useCallback ( ( ) => {
359+ const report = collectProviderUsageLimits (
360+ currentModelSelection . instanceId ,
361+ props . serverConfig ?. providers ?? [ ] ,
362+ props . serverConfig ?. usageLimitSources ?? [ ] ,
363+ Date . now ( ) ,
364+ ) ;
365+ onShowUsageLimits ( report ) ;
366+ if ( ! report ) {
367+ Alert . alert ( "Usage limits unavailable" , "This provider does not currently report limits." ) ;
368+ }
369+ return report !== null ;
370+ } , [ currentModelSelection . instanceId , onShowUsageLimits , props . serverConfig ] ) ;
339371
340372 const composerMenu = useComposerCommandMenu ( {
341373 draftMessage : props . draftMessage ,
@@ -350,6 +382,10 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
350382 selectedProviderStatus ?. showInteractionModeToggle === false
351383 ? undefined
352384 : props . onUpdateInteractionMode ,
385+ offersUsageLimits : usageLimitsOffered ,
386+ // With attachments aboard the pick just inserts the text, so it sends as a prompt.
387+ onUsageLimits :
388+ usageLimitsOffered && props . draftAttachments . length === 0 ? openUsageLimits : undefined ,
353389 } ) ;
354390 const voiceInput = useVoiceInputController ( {
355391 ownerKey : composerOwnerKey ,
@@ -428,9 +464,17 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
428464 }
429465 onEditorFocusChange ?.( false ) ;
430466 } , [ onEditorFocusChange , onExpandedChange , settingsSheetPresentation . isActive ] ) ;
431- const { onSendMessage } = props ;
432-
433467 const handleSend = useCallback ( async ( ) => {
468+ // Typed out in full rather than picked from the menu. Attachments mean the
469+ // user is sending a prompt, so those go through as usual.
470+ if (
471+ usageLimitsOffered &&
472+ isUsageLimitsCommand ( props . draftMessage ) &&
473+ props . draftAttachments . length === 0
474+ ) {
475+ if ( openUsageLimits ( ) ) onChangeDraftMessage ( "" ) ;
476+ return ;
477+ }
434478 if ( voiceInput . blocksSubmission ) return ;
435479 const threadKey = scopedThreadKey ( props . environmentId , props . selectedThread . id ) ;
436480 if ( inFlightThreadIdsRef . current . has ( threadKey ) ) return ;
@@ -453,6 +497,11 @@ export const ThreadComposer = memo(function ThreadComposer(props: ThreadComposer
453497 inFlightThreadIdsRef . current . delete ( threadKey ) ;
454498 }
455499 } , [
500+ props . draftMessage ,
501+ props . draftAttachments . length ,
502+ onChangeDraftMessage ,
503+ openUsageLimits ,
504+ usageLimitsOffered ,
456505 onSendMessage ,
457506 props . environmentId ,
458507 props . environmentLabel ,
0 commit comments