@@ -267,27 +267,23 @@ export interface ParsedOpenCodeModelSlug {
267267 readonly modelID : string ;
268268}
269269
270- export type OpenCodeVariantCache = Map <
271- string ,
272- { readonly variants : ReadonlySet < string > ; readonly contextWindow : number | undefined } | undefined
273- > ;
270+ export interface OpenCodeCatalogModel {
271+ readonly variants : ReadonlySet < string > ;
272+ readonly contextWindow : number | undefined ;
273+ }
274274
275- export const resolveOpenCodeVariant = Effect . fnUntraced ( function * (
275+ export type OpenCodeVariantCache = Map < string , OpenCodeCatalogModel | undefined > ;
276+
277+ /** The model's catalog entry; a failed read returns undefined and is not cached. */
278+ export const readOpenCodeCatalogModel = Effect . fnUntraced ( function * (
276279 client : OpencodeClient ,
277- model : ParsedOpenCodeModelSlug | undefined ,
278- variant : string | undefined ,
280+ model : ParsedOpenCodeModelSlug ,
279281 abortSignal ?: AbortSignal ,
280282 cache ?: OpenCodeVariantCache ,
281283) {
282- if ( ! variant || ! model ) return variant ;
283-
284284 const key = `${ model . providerID } /${ model . modelID } ` ;
285- const cached = cache ?. get ( key ) ;
286- if ( cache ?. has ( key ) ) {
287- return cached !== undefined && ! cached . variants . has ( variant ) ? undefined : variant ;
288- }
285+ if ( cache ?. has ( key ) ) return cache . get ( key ) ;
289286
290- // Only a confirmed catalog mismatch can invalidate a persisted selection.
291287 const response = yield * runOpenCodeSdk ( "provider.list" , ( signal ) =>
292288 client . provider . list ( undefined , {
293289 signal : abortSignal ? AbortSignal . any ( [ signal , abortSignal ] ) : signal ,
@@ -296,24 +292,30 @@ export const resolveOpenCodeVariant = Effect.fnUntraced(function* (
296292 Effect . timeout ( "10 seconds" ) ,
297293 Effect . orElseSucceed ( ( ) => undefined ) ,
298294 ) ;
299- if ( response ?. data === undefined ) return variant ;
295+ if ( response ?. data === undefined ) return undefined ;
300296
301297 const catalogModel = response . data . all . find ( ( provider ) => provider . id === model . providerID )
302298 ?. models [ model . modelID ] ;
303- const variants = catalogModel ? new Set ( Object . keys ( catalogModel . variants ?? { } ) ) : undefined ;
304- if ( cache !== undefined ) {
305- // Keep only what this model needs; provider.list can return several megabytes.
306- cache . set (
307- key ,
308- catalogModel && variants
309- ? {
310- variants,
311- contextWindow : catalogModel . limit ?. input ?? catalogModel . limit ?. context ,
312- }
313- : undefined ,
314- ) ;
315- }
316- return variants !== undefined && ! variants . has ( variant ) ? undefined : variant ;
299+ const entry : OpenCodeCatalogModel | undefined = catalogModel && {
300+ variants : new Set ( Object . keys ( catalogModel . variants ?? { } ) ) ,
301+ contextWindow : catalogModel . limit ?. input ?? catalogModel . limit ?. context ,
302+ } ;
303+ // Keep only what this model needs; provider.list can return several megabytes.
304+ cache ?. set ( key , entry ) ;
305+ return entry ;
306+ } ) ;
307+
308+ export const resolveOpenCodeVariant = Effect . fnUntraced ( function * (
309+ client : OpencodeClient ,
310+ model : ParsedOpenCodeModelSlug | undefined ,
311+ variant : string | undefined ,
312+ abortSignal ?: AbortSignal ,
313+ cache ?: OpenCodeVariantCache ,
314+ ) {
315+ if ( ! variant || ! model ) return variant ;
316+ // Only a confirmed catalog mismatch can invalidate a persisted selection.
317+ const entry = yield * readOpenCodeCatalogModel ( client , model , abortSignal , cache ) ;
318+ return entry !== undefined && ! entry . variants . has ( variant ) ? undefined : variant ;
317319} ) ;
318320
319321export interface OpenCodeSkill {
0 commit comments