@@ -8,6 +8,8 @@ import { CopilotModels } from "./models"
88
99const log = Log . create ( { service : "plugin.copilot" } )
1010
11+ const modelsCache = new Map < string , Record < string , Model > > ( )
12+
1113const CLIENT_ID = "Ov23li8tweQw6odWQebz"
1214// Add a small safety buffer when polling to avoid hitting the server
1315// slightly too early due to clock skew / timer drift.
@@ -47,17 +49,22 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
4749 return Object . fromEntries ( Object . entries ( provider . models ) . map ( ( [ id , model ] ) => [ id , fix ( model ) ] ) )
4850 }
4951
50- return CopilotModels . get (
51- base ( ctx . auth . enterpriseUrl ) ,
52- {
53- Authorization : `Bearer ${ ctx . auth . refresh } ` ,
54- "User-Agent" : `opencode/${ Installation . VERSION } ` ,
55- } ,
56- provider . models ,
57- ) . catch ( ( error ) => {
58- log . error ( "failed to fetch copilot models" , { error } )
59- return Object . fromEntries ( Object . entries ( provider . models ) . map ( ( [ id , model ] ) => [ id , fix ( model ) ] ) )
60- } )
52+ const url = base ( ctx . auth . enterpriseUrl )
53+ const key = `${ url } :${ ctx . auth . refresh } `
54+ const headers = {
55+ Authorization : `Bearer ${ ctx . auth . refresh } ` ,
56+ "User-Agent" : `opencode/${ Installation . VERSION } ` ,
57+ }
58+
59+ // Fire-and-forget background refresh (same pattern as models.dev)
60+ CopilotModels . get ( url , headers , provider . models )
61+ . then ( ( result ) => modelsCache . set ( key , result ) )
62+ . catch ( ( error ) => log . error ( "failed to fetch copilot models" , { error } ) )
63+
64+ // Return cached result immediately, falling back to static models
65+ const cached = modelsCache . get ( key )
66+ if ( cached ) return cached
67+ return Object . fromEntries ( Object . entries ( provider . models ) . map ( ( [ id , model ] ) => [ id , fix ( model ) ] ) )
6168 } ,
6269 } ,
6370 auth : {
0 commit comments