2026-02-08 15:01:42 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
|
|
|
|
import { runBunInstall } from "../../../cli/config-manager"
|
|
|
|
|
import { log } from "../../../shared/logger"
|
|
|
|
|
import { invalidatePackage } from "../cache"
|
|
|
|
|
import { PACKAGE_NAME } from "../constants"
|
|
|
|
|
import { extractChannel } from "../version-channel"
|
2026-03-11 07:42:08 -06:00
|
|
|
import { findPluginEntry, getCachedVersion, getLatestVersion, syncCachePackageJsonToIntent } from "../checker"
|
2026-02-08 15:01:42 +09:00
|
|
|
import { showAutoUpdatedToast, showUpdateAvailableToast } from "./update-toasts"
|
|
|
|
|
|
2026-02-21 02:24:43 +09:00
|
|
|
function getPinnedVersionToastMessage(latestVersion: string): string {
|
|
|
|
|
return `Update available: ${latestVersion} (version pinned, update manually)`
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
async function runBunInstallSafe(): Promise<boolean> {
|
|
|
|
|
try {
|
|
|
|
|
return await runBunInstall()
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const errorMessage = err instanceof Error ? err.message : String(err)
|
|
|
|
|
log("[auto-update-checker] bun install error:", errorMessage)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function runBackgroundUpdateCheck(
|
|
|
|
|
ctx: PluginInput,
|
|
|
|
|
autoUpdate: boolean,
|
|
|
|
|
getToastMessage: (isUpdate: boolean, latestVersion?: string) => string
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const pluginInfo = findPluginEntry(ctx.directory)
|
|
|
|
|
if (!pluginInfo) {
|
|
|
|
|
log("[auto-update-checker] Plugin not found in config")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const cachedVersion = getCachedVersion()
|
|
|
|
|
const currentVersion = cachedVersion ?? pluginInfo.pinnedVersion
|
|
|
|
|
if (!currentVersion) {
|
|
|
|
|
log("[auto-update-checker] No version found (cached or pinned)")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const channel = extractChannel(pluginInfo.pinnedVersion ?? currentVersion)
|
|
|
|
|
const latestVersion = await getLatestVersion(channel)
|
|
|
|
|
if (!latestVersion) {
|
|
|
|
|
log("[auto-update-checker] Failed to fetch latest version for channel:", channel)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentVersion === latestVersion) {
|
|
|
|
|
log("[auto-update-checker] Already on latest version for channel:", channel)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log(`[auto-update-checker] Update available (${channel}): ${currentVersion} → ${latestVersion}`)
|
|
|
|
|
|
|
|
|
|
if (!autoUpdate) {
|
|
|
|
|
await showUpdateAvailableToast(ctx, latestVersion, getToastMessage)
|
|
|
|
|
log("[auto-update-checker] Auto-update disabled, notification only")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pluginInfo.isPinned) {
|
2026-02-21 02:24:43 +09:00
|
|
|
await showUpdateAvailableToast(ctx, latestVersion, () => getPinnedVersionToastMessage(latestVersion))
|
2026-02-11 15:39:15 +09:00
|
|
|
log(`[auto-update-checker] User-pinned version detected (${pluginInfo.entry}), skipping auto-update. Notification only.`)
|
|
|
|
|
return
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 07:42:08 -06:00
|
|
|
// Sync cache package.json to match opencode.json intent before updating
|
|
|
|
|
// This handles the case where user switched from pinned version to tag (e.g., 3.10.0 -> @latest)
|
|
|
|
|
const syncResult = syncCachePackageJsonToIntent(pluginInfo)
|
|
|
|
|
|
|
|
|
|
// Abort on ANY sync error to prevent corrupting a bad state further
|
|
|
|
|
if (syncResult.error) {
|
|
|
|
|
log(`[auto-update-checker] Sync failed with error: ${syncResult.error}`, syncResult.message)
|
|
|
|
|
await showUpdateAvailableToast(ctx, latestVersion, getToastMessage)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
invalidatePackage(PACKAGE_NAME)
|
|
|
|
|
|
|
|
|
|
const installSuccess = await runBunInstallSafe()
|
|
|
|
|
|
|
|
|
|
if (installSuccess) {
|
|
|
|
|
await showAutoUpdatedToast(ctx, currentVersion, latestVersion)
|
|
|
|
|
log(`[auto-update-checker] Update installed: ${currentVersion} → ${latestVersion}`)
|
2026-02-11 11:56:05 +09:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await showUpdateAvailableToast(ctx, latestVersion, getToastMessage)
|
|
|
|
|
log("[auto-update-checker] bun install failed; update not installed (falling back to notification-only)")
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|