fix(hooks): use model cache availability with timeout for first-run cache creation

Replace fire-and-forget pattern with await + 10s timeout for initial

cache creation. Check model cache availability (not connected providers)

to properly coordinate with model-cache-warning hook.

Remove non-null assertion and add proper error logging.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim 2026-02-21 18:32:10 +09:00
parent f0204b0514
commit 1c7eb55f9c

View File

@ -1,29 +1,48 @@
import type { PluginInput } from "@opencode-ai/plugin"
import {
hasConnectedProvidersCache,
updateConnectedProvidersCache,
} from "../../../shared/connected-providers-cache"
import { updateConnectedProvidersCache } from "../../../shared/connected-providers-cache"
import { isModelCacheAvailable } from "../../../shared/model-availability"
import { log } from "../../../shared/logger"
export async function updateAndShowConnectedProvidersCacheStatus(ctx: PluginInput): Promise<void> {
const hadCache = hasConnectedProvidersCache()
const CACHE_UPDATE_TIMEOUT_MS = 10000
updateConnectedProvidersCache(ctx.client).catch(() => {})
export async function updateAndShowConnectedProvidersCacheStatus(ctx: PluginInput): Promise<void> {
const hadCache = isModelCacheAvailable()
if (!hadCache) {
await ctx.client.tui
.showToast({
body: {
title: "Connected Providers Cache",
message: "Building provider cache for first time. Restart OpenCode for full model filtering.",
variant: "info" as const,
duration: 8000,
},
})
.catch(() => {})
let timeoutId: ReturnType<typeof setTimeout> | undefined
try {
await Promise.race([
updateConnectedProvidersCache(ctx.client),
new Promise<never>((_, reject) => {
timeoutId = setTimeout(() => reject(new Error("Cache update timed out")), CACHE_UPDATE_TIMEOUT_MS)
}),
])
} catch (err) {
log("[auto-update-checker] Connected providers cache creation failed", { error: String(err) })
} finally {
if (timeoutId) clearTimeout(timeoutId)
}
log("[auto-update-checker] Connected providers cache toast shown (first run)")
if (!isModelCacheAvailable()) {
await ctx.client.tui
.showToast({
body: {
title: "Connected Providers Cache",
message: "Failed to build provider cache. Restart OpenCode to retry.",
variant: "warning" as const,
duration: 8000,
},
})
.catch(() => {})
log("[auto-update-checker] Connected providers cache toast shown (creation failed)")
} else {
log("[auto-update-checker] Connected providers cache created on first run")
}
} else {
updateConnectedProvidersCache(ctx.client).catch((err) => {
log("[auto-update-checker] Background cache update failed", { error: String(err) })
})
log("[auto-update-checker] Connected providers cache exists, updating in background")
}
}