Skip to content

Commit 9268eb6

Browse files
committed
fix(copilot): fire-and-forget model with cache fetch to unblock TUI startup
1 parent ae614d9 commit 9268eb6

File tree

1 file changed

+18
-11
lines changed
  • packages/opencode/src/plugin/github-copilot

1 file changed

+18
-11
lines changed

packages/opencode/src/plugin/github-copilot/copilot.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { CopilotModels } from "./models"
88

99
const log = Log.create({ service: "plugin.copilot" })
1010

11+
const modelsCache = new Map<string, Record<string, Model>>()
12+
1113
const 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

Comments
 (0)