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:
parent
f0204b0514
commit
1c7eb55f9c
@ -1,29 +1,48 @@
|
|||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import {
|
import { updateConnectedProvidersCache } from "../../../shared/connected-providers-cache"
|
||||||
hasConnectedProvidersCache,
|
import { isModelCacheAvailable } from "../../../shared/model-availability"
|
||||||
updateConnectedProvidersCache,
|
|
||||||
} from "../../../shared/connected-providers-cache"
|
|
||||||
import { log } from "../../../shared/logger"
|
import { log } from "../../../shared/logger"
|
||||||
|
|
||||||
export async function updateAndShowConnectedProvidersCacheStatus(ctx: PluginInput): Promise<void> {
|
const CACHE_UPDATE_TIMEOUT_MS = 10000
|
||||||
const hadCache = hasConnectedProvidersCache()
|
|
||||||
|
|
||||||
updateConnectedProvidersCache(ctx.client).catch(() => {})
|
export async function updateAndShowConnectedProvidersCacheStatus(ctx: PluginInput): Promise<void> {
|
||||||
|
const hadCache = isModelCacheAvailable()
|
||||||
|
|
||||||
if (!hadCache) {
|
if (!hadCache) {
|
||||||
await ctx.client.tui
|
let timeoutId: ReturnType<typeof setTimeout> | undefined
|
||||||
.showToast({
|
try {
|
||||||
body: {
|
await Promise.race([
|
||||||
title: "Connected Providers Cache",
|
updateConnectedProvidersCache(ctx.client),
|
||||||
message: "Building provider cache for first time. Restart OpenCode for full model filtering.",
|
new Promise<never>((_, reject) => {
|
||||||
variant: "info" as const,
|
timeoutId = setTimeout(() => reject(new Error("Cache update timed out")), CACHE_UPDATE_TIMEOUT_MS)
|
||||||
duration: 8000,
|
}),
|
||||||
},
|
])
|
||||||
})
|
} catch (err) {
|
||||||
.catch(() => {})
|
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 {
|
} 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")
|
log("[auto-update-checker] Connected providers cache exists, updating in background")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user