Wait for plugins before using the OpenCode default model on cold locations

This commit is contained in:
Chris Scott committed 2026-11-19 22:35:10 +00:00
1 parent 3796ba15eb
commit afde60ddf7
3 files changed
+198 -4

No files matched your search

+31 -3
View File
@@ -80,13 +80,18 @@ function sleep(ms: number, signal: AbortSignal): Promise<void> {
})
}
type PollingResult<T> = { ok: true; value: T } | { ok: false }
type PollingResult<T> = { ok: true; value: T } | { ok: false; retry?: true }
const abortLikeErrorSchema = z.object({
name: z.string().optional(),
cause: z.unknown().optional(),
})
const pluginsActivatingErrorSchema = z.object({
_tag: z.literal('ServiceUnavailableError'),
service: z.literal('plugin'),
})
function isAbortLike(cause: unknown, signal: AbortSignal): boolean {
if (cause === signal.reason) {
return true
@@ -119,10 +124,18 @@ async function runPollingRequest<T>(
return { ok: false }
}
if (pluginsActivatingErrorSchema.safeParse(error).success) {
return { ok: false, retry: true }
}
throw error
}
}
function isRetrying<T>(result: PollingResult<T> | undefined): boolean {
return result !== undefined && !result.ok && result.retry === true
}
async function readConfiguredRef(
client: OpenCodeClient,
location: ReturnType<typeof openCodeLocation>,
@@ -164,7 +177,7 @@ export async function resolveOpenCodeModel(
break
}
const [listResult, defaultResult] = await Promise.all([
const [listResult, defaultResult, pluginResult] = await Promise.all([
runPollingRequest(
() => client.api.model.list(location, { signal }),
external,
@@ -175,6 +188,13 @@ export async function resolveOpenCodeModel(
external,
timeoutController.signal,
),
targetRef === undefined
? runPollingRequest(
() => client.api.plugin.list(location, { signal }),
external,
timeoutController.signal,
)
: undefined,
])
if (listResult.ok) {
@@ -185,6 +205,11 @@ export async function resolveOpenCodeModel(
defaultModel = defaultResult.value.data
}
if (isRetrying(listResult) || isRetrying(defaultResult) || isRetrying(pluginResult)) {
await sleep(MODEL_LOAD_POLL_MS, signal)
continue
}
if (!listResult.ok || !defaultResult.ok) {
break
}
@@ -195,8 +220,11 @@ export async function resolveOpenCodeModel(
break
}
const pluginsLoaded = pluginResult === undefined
|| (pluginResult.ok && pluginResult.value.data.length > 0)
const resolved = resolveFromLoadedModels(models, defaultModel, configuredRef, preferredRef)
if (resolved && isTargetLoaded(models, targetRef)) {
if (resolved && isTargetLoaded(models, targetRef) && pluginsLoaded) {
return resolved
}