1- import type { SessionConfigOption } from "@agentclientprotocol/sdk" ;
1+ import type {
2+ SessionConfigOption ,
3+ SessionConfigSelectOption ,
4+ } from "@agentclientprotocol/sdk" ;
25import {
36 CODEX_MODE_PRESETS ,
47 type CodexModePreset ,
58 type ExecutionMode ,
69 resolveCloudInitialPermissionMode ,
10+ restrictedModelMeta ,
711} from "@posthog/shared" ;
8- import { type GatewayModel , isOpenAIModel } from "../../gateway-models" ;
12+ import {
13+ type GatewayModel ,
14+ isOpenAIModel ,
15+ type ModelInfo ,
16+ } from "../../gateway-models" ;
917import { getReasoningEffortOptions } from "./models" ;
1018
1119/**
@@ -158,7 +166,11 @@ export interface ConfigSelectors {
158166 model : string ;
159167 effort ?: string ;
160168 /** From model/list; falls back to the single current model when empty. */
161- models : Array < { id : string ; name : string } > ;
169+ models : Array < {
170+ id : string ;
171+ name : string ;
172+ _meta ?: Record < string , unknown > ;
173+ } > ;
162174 efforts : string [ ] ;
163175}
164176
@@ -195,7 +207,13 @@ export function buildConfigOptions(s: ConfigSelectors): SessionConfigOption[] {
195207 name : "Model" ,
196208 category : "model" ,
197209 currentValue : s . model ,
198- options : models . map ( ( m ) => ( { name : m . name , value : m . id } ) ) ,
210+ options : models . map (
211+ ( m ) : SessionConfigSelectOption => ( {
212+ name : m . name ,
213+ value : m . id ,
214+ ...( m . _meta ? { _meta : m . _meta } : { } ) ,
215+ } ) ,
216+ ) ,
199217 } as unknown as SessionConfigOption ,
200218 {
201219 type : "select" ,
@@ -226,13 +244,31 @@ export class SessionConfigState {
226244 private _model : string ;
227245 private _effort ?: string ;
228246 private _mode = DEFAULT_MODE ;
229- private models : Array < { id : string ; name : string } > = [ ] ;
247+ private models : Array < {
248+ id : string ;
249+ name : string ;
250+ _meta ?: Record < string , unknown > ;
251+ } > = [ ] ;
230252 private efforts : string [ ] = [ ] ;
231253 private _options : SessionConfigOption [ ] = [ ] ;
254+ private readonly gatewayModels ?: ReadonlyArray < ModelInfo > ;
255+ private readonly allowedModelIds ?: ReadonlySet < string > ;
232256
233- constructor ( model : string , effort ?: string ) {
257+ constructor (
258+ model : string ,
259+ effort ?: string ,
260+ gatewayModels ?: ReadonlyArray < ModelInfo > ,
261+ ) {
234262 this . _model = model ;
235263 this . _effort = effort ;
264+ this . gatewayModels = gatewayModels ?. length ? gatewayModels : undefined ;
265+ this . allowedModelIds = this . gatewayModels
266+ ? new Set (
267+ this . gatewayModels
268+ . filter ( ( gatewayModel ) => gatewayModel . allowed )
269+ . map ( ( gatewayModel ) => gatewayModel . id ) ,
270+ )
271+ : undefined ;
236272 this . rebuild ( ) ;
237273 }
238274
@@ -262,8 +298,12 @@ export class SessionConfigState {
262298 ) : { modeChanged : boolean } {
263299 let modeChanged = false ;
264300 if ( typeof value === "string" ) {
265- if ( configId === "model" ) this . _model = value ;
266- else if ( configId === "effort" ) this . _effort = value ;
301+ if (
302+ configId === "model" &&
303+ ( ! this . gatewayModels || this . allowedModelIds ?. has ( value ) )
304+ ) {
305+ this . _model = value ;
306+ } else if ( configId === "effort" ) this . _effort = value ;
267307 else if ( configId === "mode" ) {
268308 this . _mode = resolveCodexMode ( value ) ;
269309 modeChanged = true ;
@@ -279,13 +319,27 @@ export class SessionConfigState {
279319 * populate efforts, so fall back to the shared codex model→effort map.
280320 */
281321 loadModels ( rawModels : RawModel [ ] ) : void {
282- this . models = rawModels
322+ const liveModels = rawModels
283323 . filter ( ( m ) => ! m ?. hidden )
284324 . filter ( ( m ) => isOpenAIModel ( m as unknown as GatewayModel ) )
285325 . map ( ( m ) => ( {
286326 id : ( m . id ?? m . model ) as string ,
287327 name : ( m . displayName ?? m . id ?? m . model ) as string ,
288328 } ) ) ;
329+ if ( this . gatewayModels ) {
330+ const liveModelsById = new Map (
331+ liveModels . map ( ( model ) => [ model . id , model ] ) ,
332+ ) ;
333+ this . models = this . gatewayModels . map ( ( gatewayModel ) => ( {
334+ ...( liveModelsById . get ( gatewayModel . id ) ?? {
335+ id : gatewayModel . id ,
336+ name : gatewayModel . id ,
337+ } ) ,
338+ ...( gatewayModel . allowed ? { } : { _meta : restrictedModelMeta ( ) } ) ,
339+ } ) ) ;
340+ } else {
341+ this . models = liveModels ;
342+ }
289343 const current = rawModels . find (
290344 ( m ) => m . id === this . _model || m . model === this . _model ,
291345 ) ;
@@ -300,7 +354,12 @@ export class SessionConfigState {
300354
301355 /** Reset the model/effort lists (model/list failed); keeps the current model. */
302356 clearModels ( ) : void {
303- this . models = [ ] ;
357+ this . models =
358+ this . gatewayModels ?. map ( ( gatewayModel ) => ( {
359+ id : gatewayModel . id ,
360+ name : gatewayModel . id ,
361+ ...( gatewayModel . allowed ? { } : { _meta : restrictedModelMeta ( ) } ) ,
362+ } ) ) ?? [ ] ;
304363 this . efforts = [ ] ;
305364 this . rebuild ( ) ;
306365 }
0 commit comments