@@ -15,14 +15,16 @@ import {
1515 inferenceHarness ,
1616 isCustomInferenceBackend ,
1717 isInferenceProviderConfig ,
18+ isInferenceRoutingEnabled ,
1819 isManagedSettingPath ,
20+ ProviderDriverKind ,
1921 type InferenceProxyStatus ,
2022 type ServerProvider ,
2123 type InferenceDriver ,
2224 type InferenceProxyAction ,
2325 type InferenceUpstream ,
2426} from "@t3tools/contracts" ;
25- import { useRef , useState } from "react" ;
27+ import { useEffect , useRef , useState } from "react" ;
2628import { Linking , TextInput , View } from "react-native" ;
2729
2830import { AppText } from "../../components/ui/AppText" ;
@@ -105,11 +107,11 @@ function ProviderRoutingSwitch({
105107 const { instanceId } = provider ;
106108 const config = environment . serverConfig . settings . providerInstances [ instanceId ] ?. config ;
107109 const route = isInferenceProviderConfig ( config ) ? config : null ;
108- const enabled = route ?. inferenceEnabled === true ;
110+ const enabled = isInferenceRoutingEnabled ( config ) ;
109111 const error = failure && failure . target !== enabled ? failure . message : "" ;
110112 const locked = isManagedSettingPath (
111113 environment . serverConfig . managedPolicy ,
112- `providerInstances.${ instanceId } ` ,
114+ `providerInstances.${ instanceId } .config.inferenceEnabled ` ,
113115 ) ;
114116 async function change ( value : boolean ) {
115117 if ( inFlight . current ) return ;
@@ -121,7 +123,9 @@ function ProviderRoutingSwitch({
121123 consumer : { environmentId, label : environment . label } ,
122124 environments,
123125 driver,
124- ...( route ? { preferredHostId : route . inferenceHostId } : { } ) ,
126+ ...( route
127+ ? { preferredHostId : route . inferenceHostId , preserveProviderEnabled : true }
128+ : { } ) ,
125129 } )
126130 : await manage ( { environmentId, input : { operation : "removeRoute" , instanceId } } ) ;
127131 inFlight . current = false ;
@@ -143,7 +147,9 @@ function ProviderRoutingSwitch({
143147 subtitle = {
144148 error ||
145149 ( enabled
146- ? `Using accounts on ${ route . inferenceHostLabel || environment . label } .`
150+ ? route
151+ ? `Using accounts on ${ route . inferenceHostLabel || environment . label } .`
152+ : "This inference route is incomplete. Turn it off to use the native provider."
147153 : "Use accounts on this environment or a paired inference host." )
148154 }
149155 value = { enabled }
@@ -225,10 +231,12 @@ function InferenceControls({
225231 environment,
226232 setupDriver,
227233 onConnected,
234+ onOpenHostingSettings,
228235} : {
229236 environment : SettingsTarget ;
230237 setupDriver : InferenceDriver | null ;
231238 onConnected : ( ) => void ;
239+ onOpenHostingSettings : ( ) => void ;
232240} ) {
233241 const { environmentId } = environment ;
234242 const { environments } = useEnvironments ( ) ;
@@ -242,10 +250,21 @@ function InferenceControls({
242250 const [ login , setLogin ] = useState < { url : string ; state : string } | null > ( null ) ;
243251 const [ callback , setCallback ] = useState ( "" ) ;
244252 const [ notice , setNotice ] = useState ( "" ) ;
253+ const accountSetup = useRef ( { active : false } ) ;
254+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- prevents an account operation from routing after its setup panel closes
255+ useEffect ( ( ) => {
256+ const setup = { active : true } ;
257+ accountSetup . current = setup ;
258+ return ( ) => {
259+ setup . active = false ;
260+ } ;
261+ } , [ ] ) ;
245262 async function run ( input : InferenceProxyAction ) {
263+ const setup = accountSetup . current ;
246264 setPending ( true ) ;
247265 setNotice ( "" ) ;
248266 const result = await command ( { environmentId, input } ) ;
267+ if ( ! setup . active ) return ;
249268 if ( result . _tag === "Failure" ) {
250269 setPending ( false ) ;
251270 setNotice ( String ( squashAtomCommandFailure ( result ) ) ) ;
@@ -273,12 +292,19 @@ function InferenceControls({
273292 ( result . value . status === "ok" ||
274293 ( input . operation === "configure" && input . enabled && input . expectedEnabled === false ) )
275294 ) {
295+ const instanceId = defaultInstanceIdForDriver ( ProviderDriverKind . make ( setupDriver ) ) ;
276296 const connected = await enableRouting ( {
277297 consumer : { environmentId, label : environment . label } ,
278298 environments,
279299 driver : setupDriver ,
280- preferredHostId : environmentId ,
300+ onlyHostId : environmentId ,
301+ ...( isInferenceProviderConfig (
302+ environment . serverConfig . settings . providerInstances [ instanceId ] ?. config ,
303+ )
304+ ? { preserveProviderEnabled : true }
305+ : { } ) ,
281306 } ) ;
307+ if ( ! setup . active ) return ;
282308 if ( connected . _tag === "Success" ) onConnected ( ) ;
283309 else {
284310 const failure = squashAtomCommandFailure ( connected ) ;
@@ -296,29 +322,42 @@ function InferenceControls({
296322 ) ;
297323 const available = updateQuery . data ?. update ?. available ;
298324 const disabled = pending || ! permitted ;
325+ const requiresHostingSettings =
326+ setupDriver !== null &&
327+ host ?. enabled === false &&
328+ environment . serverConfig . environment . capabilities . inferenceConfigureGuard !== true ;
299329 return (
300330 < View className = "gap-1" >
301331 { host ? (
302332 < >
303- < SettingsSwitchRow
304- label = "Host accounts here"
305- subtitle = {
306- host . problem ??
307- ( host . running
308- ? `CLIProxyAPI ${ host . version } is running.`
309- : `Enable to install and start CLIProxyAPI ${ host . version } .` )
310- }
311- disabled = { disabled }
312- value = { host . enabled }
313- onValueChange = { ( enabled ) =>
314- void run ( {
315- operation : "configure" ,
316- enabled,
317- sharing : false ,
318- expectedEnabled : host . enabled ,
319- } )
320- }
321- />
333+ { requiresHostingSettings ? (
334+ < ListActionRow
335+ label = "Manage inference hosting"
336+ description = "Start hosting in Inference accounts, then turn on routing."
337+ disabled = { disabled }
338+ onPress = { onOpenHostingSettings }
339+ />
340+ ) : (
341+ < SettingsSwitchRow
342+ label = "Host accounts here"
343+ subtitle = {
344+ host . problem ??
345+ ( host . running
346+ ? `CLIProxyAPI ${ host . version } is running.`
347+ : `Enable to install and start CLIProxyAPI ${ host . version } .` )
348+ }
349+ disabled = { disabled }
350+ value = { host . enabled }
351+ onValueChange = { ( enabled ) =>
352+ void run ( {
353+ operation : "configure" ,
354+ enabled,
355+ sharing : false ,
356+ expectedEnabled : host . enabled ,
357+ } )
358+ }
359+ />
360+ ) }
322361 { host . enabled && (
323362 < >
324363 < ListActionRow
@@ -498,7 +537,8 @@ export function InferenceAccounts({
498537 key = { panel }
499538 environment = { environment }
500539 setupDriver = { panel === "all" ? null : panel }
501- onConnected = { ( ) => setPanel ( null ) }
540+ onConnected = { ( ) => setPanel ( ( current ) => ( current === panel ? null : current ) ) }
541+ onOpenHostingSettings = { ( ) => setPanel ( "all" ) }
502542 />
503543 ) }
504544 </ >
0 commit comments