@@ -33,6 +33,12 @@ type Analytics = {
3333 breakdown : Array < { provider : string ; model : string ; requests : number ; successRate : number | null ; p50DurationMs ?: number } > ;
3434} ;
3535
36+ type DryRunResult = {
37+ candidates : DryRunCandidate [ ] ;
38+ selectedIndex : number | null ;
39+ trace ?: { profile ?: { revision ?: string } } ;
40+ } ;
41+
3642function fmtMs ( value : number | undefined ) : string {
3743 return value === undefined ? "–" : `${ Math . round ( value ) } ms` ;
3844}
@@ -41,6 +47,14 @@ function fmtRate(value: number | null | undefined): string {
4147 return value === null || value === undefined ? "–" : `${ Math . round ( value * 100 ) } %` ;
4248}
4349
50+ function pickSelectedProfile ( next : ProfileDto [ ] , current : ProfileDto | null ) : ProfileDto | null {
51+ if ( current ) {
52+ const refreshed = next . find ( profile => profile . id === current . id ) ;
53+ if ( refreshed ) return refreshed ;
54+ }
55+ return next [ 0 ] ?? null ;
56+ }
57+
4458export default function RoutingProfiles ( { apiBase } : { apiBase : string } ) {
4559 const t = useT ( ) ;
4660 const [ profiles , setProfiles ] = useState < ProfileDto [ ] > ( [ ] ) ;
@@ -51,10 +65,20 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
5165 const [ tools , setTools ] = useState ( false ) ;
5266 const [ image , setImage ] = useState ( false ) ;
5367 const [ structured , setStructured ] = useState ( false ) ;
54- const [ dryRunResult , setDryRunResult ] = useState < { candidates : DryRunCandidate [ ] ; selectedIndex : number | null ; trace ?: { profile ?: { revision ?: string } } } | null > ( null ) ;
68+ const [ dryRunResult , setDryRunResult ] = useState < DryRunResult | null > ( null ) ;
5569 const [ dryRunError , setDryRunError ] = useState ( "" ) ;
5670 const [ running , setRunning ] = useState ( false ) ;
5771
72+ const clearDryRun = useCallback ( ( ) => {
73+ setDryRunResult ( null ) ;
74+ setDryRunError ( "" ) ;
75+ } , [ ] ) ;
76+
77+ const selectProfile = useCallback ( ( profile : ProfileDto | null ) => {
78+ setSelected ( profile ) ;
79+ clearDryRun ( ) ;
80+ } , [ clearDryRun ] ) ;
81+
5882 const load = useCallback ( async ( ) => {
5983 setLoadError ( "" ) ;
6084 try {
@@ -66,15 +90,22 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
6690 const profilesJson = await profilesRes . json ( ) as { profiles ?: ProfileDto [ ] } ;
6791 const next = profilesJson . profiles ?? [ ] ;
6892 setProfiles ( next ) ;
69- setSelected ( current => current ?? next [ 0 ] ?? null ) ;
93+ setSelected ( current => {
94+ const refreshed = pickSelectedProfile ( next , current ) ;
95+ if ( current && refreshed && current . revision !== refreshed . revision ) {
96+ clearDryRun ( ) ;
97+ }
98+ if ( current && ! refreshed ) clearDryRun ( ) ;
99+ return refreshed ;
100+ } ) ;
70101 if ( analyticsRes . ok ) {
71102 const analyticsJson = await analyticsRes . json ( ) as Analytics ;
72103 setAnalytics ( analyticsJson ) ;
73104 }
74105 } catch ( error ) {
75106 setLoadError ( error instanceof Error ? error . message : String ( error ) ) ;
76107 }
77- } , [ apiBase ] ) ;
108+ } , [ apiBase , clearDryRun ] ) ;
78109
79110 useEffect ( ( ) => {
80111 const timer = window . setTimeout ( ( ) => void load ( ) , 0 ) ;
@@ -84,8 +115,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
84115 const runDryRun = async ( ) => {
85116 if ( ! selected ) return ;
86117 setRunning ( true ) ;
87- setDryRunError ( "" ) ;
88- setDryRunResult ( null ) ;
118+ clearDryRun ( ) ;
89119 try {
90120 const response = await fetch ( `${ apiBase } /api/routing-profiles/dry-run` , {
91121 method : "POST" ,
@@ -111,7 +141,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
111141 setDryRunError ( message ) ;
112142 return ;
113143 }
114- setDryRunResult ( await response . json ( ) as typeof dryRunResult ) ;
144+ setDryRunResult ( await response . json ( ) as DryRunResult ) ;
115145 } catch ( error ) {
116146 setDryRunError ( error instanceof Error ? error . message : String ( error ) ) ;
117147 } finally {
@@ -139,7 +169,7 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
139169 type = "button"
140170 className = "model-card"
141171 style = { { textAlign : "left" , cursor : "pointer" } }
142- onClick = { ( ) => setSelected ( profile ) }
172+ onClick = { ( ) => selectProfile ( profile ) }
143173 aria-pressed = { selected ?. id === profile . id }
144174 >
145175 < div className = "card-badges" >
@@ -165,30 +195,21 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
165195 ) ) }
166196 </ div >
167197 </ div >
168- < div >
169- < span className = "field-label" > { t ( "routing.require" ) } </ span >
170- < pre className = "muted" style = { { margin : 0 , whiteSpace : "pre-wrap" } } >
171- { Object . keys ( selected . require ) . length ? JSON . stringify ( selected . require , null , 2 ) : t ( "routing.none" ) }
172- </ pre >
173- </ div >
174- < div >
175- < span className = "field-label" > { t ( "routing.optimize" ) } </ span >
176- < pre className = "muted" style = { { margin : 0 , whiteSpace : "pre-wrap" } } >
177- { JSON . stringify ( selected . optimize , null , 2 ) }
178- </ pre >
179- </ div >
180- < div >
181- < span className = "field-label" > { t ( "routing.limits" ) } </ span >
182- < pre className = "muted" style = { { margin : 0 , whiteSpace : "pre-wrap" } } >
183- { Object . keys ( selected . limits ) . length ? JSON . stringify ( selected . limits , null , 2 ) : t ( "routing.none" ) }
184- </ pre >
185- </ div >
186- < div >
187- < span className = "field-label" > { t ( "routing.unknownEvidence" ) } </ span >
188- < pre className = "muted" style = { { margin : 0 , whiteSpace : "pre-wrap" } } >
189- { JSON . stringify ( selected . unknownEvidence , null , 2 ) }
190- </ pre >
191- </ div >
198+ { ( [
199+ [ "routing.require" , selected . require , true ] ,
200+ [ "routing.optimize" , selected . optimize , false ] ,
201+ [ "routing.limits" , selected . limits , true ] ,
202+ [ "routing.unknownEvidence" , selected . unknownEvidence , false ] ,
203+ ] as const ) . map ( ( [ labelKey , value , allowEmpty ] ) => (
204+ < div key = { labelKey } >
205+ < span className = "field-label" > { t ( labelKey ) } </ span >
206+ < pre className = "muted" style = { { margin : 0 , whiteSpace : "pre-wrap" } } >
207+ { allowEmpty && Object . keys ( value ) . length === 0
208+ ? t ( "routing.none" )
209+ : JSON . stringify ( value , null , 2 ) }
210+ </ pre >
211+ </ div >
212+ ) ) }
192213 </ div >
193214 ) : null }
194215
@@ -202,19 +223,43 @@ export default function RoutingProfiles({ apiBase }: { apiBase: string }) {
202223 type = "number"
203224 min = { 1 }
204225 value = { context }
205- onChange = { event => setContext ( event . target . value ) }
226+ onChange = { event => {
227+ setContext ( event . target . value ) ;
228+ clearDryRun ( ) ;
229+ } }
206230 />
207231 </ label >
208232 < label className = "checkbox" >
209- < input type = "checkbox" checked = { tools } onChange = { event => setTools ( event . target . checked ) } />
233+ < input
234+ type = "checkbox"
235+ checked = { tools }
236+ onChange = { event => {
237+ setTools ( event . target . checked ) ;
238+ clearDryRun ( ) ;
239+ } }
240+ />
210241 { t ( "routing.dryRunTools" ) }
211242 </ label >
212243 < label className = "checkbox" >
213- < input type = "checkbox" checked = { image } onChange = { event => setImage ( event . target . checked ) } />
244+ < input
245+ type = "checkbox"
246+ checked = { image }
247+ onChange = { event => {
248+ setImage ( event . target . checked ) ;
249+ clearDryRun ( ) ;
250+ } }
251+ />
214252 { t ( "routing.dryRunImage" ) }
215253 </ label >
216254 < label className = "checkbox" >
217- < input type = "checkbox" checked = { structured } onChange = { event => setStructured ( event . target . checked ) } />
255+ < input
256+ type = "checkbox"
257+ checked = { structured }
258+ onChange = { event => {
259+ setStructured ( event . target . checked ) ;
260+ clearDryRun ( ) ;
261+ } }
262+ />
218263 { t ( "routing.dryRunStructured" ) }
219264 </ label >
220265 < button type = "button" className = "btn btn-primary" disabled = { ! selected || running } onClick = { ( ) => void runDryRun ( ) } >
0 commit comments