From c31ac3c9194f9b76795710ea119e27c9fab343c3 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Tue, 15 Sep 2026 11:45:55 +0800 Subject: [PATCH] fix(desktop): migrate mac beta to stable installer (#48724) Co-authored-by: Hona <10430890+Hona@users.noreply.github.com> Co-authored-by: Brendonovich <14191578+Brendonovich@users.noreply.github.com> --- .../app/src/runtime/i18n/desktop-native.ts | 1 - packages/app/src/runtime/i18n/en.ts | 4 + packages/app/src/shell/commands/desktop.tsx | 9 ++ packages/app/src/shell/errors/error.tsx | 3 +- packages/app/src/shell/shell.tsx | 13 +-- packages/app/src/shell/titlebar/titlebar.tsx | 12 ++- packages/app/src/shell/updates/action.test.ts | 4 + packages/app/src/shell/updates/action.ts | 46 +++++---- packages/app/src/shell/updates/download.tsx | 57 ++++++++++++ packages/app/src/shell/updates/types.ts | 1 + packages/desktop/src/main/native/menu.ts | 6 +- .../desktop/src/main/updater/index.test.ts | 70 +++++++++++++- packages/desktop/src/main/updater/index.ts | 93 ++++++++++++++++--- packages/desktop/src/main/updater/live.ts | 1 + .../src/main/updater/migration.test.ts | 32 +++++++ .../desktop/src/main/updater/migration.ts | 20 ++++ packages/desktop/src/main/updater/platform.ts | 16 +++- .../desktop/src/shared/ipc-rpc/updater.ts | 1 + 18 files changed, 338 insertions(+), 51 deletions(-) create mode 100644 packages/app/src/shell/updates/download.tsx create mode 100644 packages/desktop/src/main/updater/migration.test.ts create mode 100644 packages/desktop/src/main/updater/migration.ts diff --git a/packages/app/src/runtime/i18n/desktop-native.ts b/packages/app/src/runtime/i18n/desktop-native.ts index d7d232f5ecd..92bdff31052 100644 --- a/packages/app/src/runtime/i18n/desktop-native.ts +++ b/packages/app/src/runtime/i18n/desktop-native.ts @@ -283,7 +283,6 @@ export const DESKTOP_NATIVE_ENGLISH = { "desktop.updater.dialog.ready.title": "Update Ready", "desktop.updater.dialog.restart": "Restart", "desktop.updater.dialog.later": "Later", - "desktop.cli.installed.title": "CLI Installed", "desktop.cli.installed.message": "CLI installed to {{path}}\n\nRestart your terminal to use the 'opencode' command.", "desktop.cli.failed.title": "Installation Failed", diff --git a/packages/app/src/runtime/i18n/en.ts b/packages/app/src/runtime/i18n/en.ts index 5c88c59a78d..87a7650fd7b 100644 --- a/packages/app/src/runtime/i18n/en.ts +++ b/packages/app/src/runtime/i18n/en.ts @@ -1226,6 +1226,10 @@ export const dict = { "settings.updates.action.checkNow": "Check now", "settings.updates.action.checking": "Checking…", "settings.updates.action.downloading": "Downloading…", + "settings.updates.action.download": "Download OpenCode", + "settings.updates.migration.title": "Move from Beta to Stable", + "settings.updates.migration.description": + "For now, OpenCode Beta is moving to OpenCode Stable. Download version {{version}}, then install OpenCode from the disk image to continue receiving updates.", "settings.updates.action.installing": "Installing…", "settings.updates.toast.latest.title": "You're up to date", "settings.updates.toast.latest.description": "You're running the latest version of OpenCode.", diff --git a/packages/app/src/shell/commands/desktop.tsx b/packages/app/src/shell/commands/desktop.tsx index 71e3ba3d40c..0c217ea0e35 100644 --- a/packages/app/src/shell/commands/desktop.tsx +++ b/packages/app/src/shell/commands/desktop.tsx @@ -3,15 +3,24 @@ import { usePlatform } from "@/runtime/platform/platform" import { useCommand, type CommandOption } from "./command" import { useDialog } from "@opencode/ui/context/dialog" import { DialogSsh } from "@/servers/ssh/dialog" +import { useUpdaterAction } from "@/shell/updates/action" export function DesktopCommands() { const command = useCommand() const language = useLanguage() const platform = usePlatform() const dialog = useDialog() + const updater = useUpdaterAction() command.register("desktop", () => { const commands: CommandOption[] = [] + if (platform.platform === "desktop") + commands.push({ + id: "app.checkForUpdates", + title: language.t("desktop.menu.checkForUpdates"), + hidden: true, + onSelect: () => void updater.check(), + }) if (platform.sshServers) commands.push({ id: "server.ssh.add", diff --git a/packages/app/src/shell/errors/error.tsx b/packages/app/src/shell/errors/error.tsx index 6d05d34afa7..2312bb9248a 100644 --- a/packages/app/src/shell/errors/error.tsx +++ b/packages/app/src/shell/errors/error.tsx @@ -267,8 +267,7 @@ export const ErrorPage: Component = (props) => { const updateVersion = () => { const state = platform.updater?.state() - if (state?.status !== "ready") return - return state.version + return state?.status === "ready" || state?.status === "download-required" ? state.version : undefined } async function exportDebugLogs() { diff --git a/packages/app/src/shell/shell.tsx b/packages/app/src/shell/shell.tsx index 8e65e36a5d4..b781f3e2620 100644 --- a/packages/app/src/shell/shell.tsx +++ b/packages/app/src/shell/shell.tsx @@ -9,6 +9,7 @@ import { TitlebarRightProvider } from "@/shell/titlebar/right-slot" import { useSettingsSurface } from "@/settings/surface" import { useSettings } from "@/settings/model" import { SshAuthentication } from "@/servers/ssh/authentication" +import { useUpdaterInstall } from "@/shell/updates/download" const DebugBar = lazy(() => import("@/shell/debug/debug-bar").then((module) => ({ default: module.DebugBar }))) @@ -16,6 +17,7 @@ export default function Layout(props: ParentProps) { const platform = usePlatform() const settings = useSettingsSurface() const preferences = useSettings() + const installUpdate = useUpdaterInstall() const mobile = createMediaQuery("(max-width: 767px)") const [state, setState] = createStore({ debugTools: false, @@ -26,15 +28,10 @@ export default function Layout(props: ParentProps) { const bottomTitlebar = () => mobile() && preferences.general.mobileTitlebarPosition() === "bottom" const update: TitlebarUpdate = { - get version() { - const state = platform.updater?.state() - if (state?.status !== "ready") return undefined - return state.version + get state() { + return platform.updater?.state() }, - get installing() { - return platform.updater?.state().status === "installing" - }, - install: () => void platform.updater?.install(), + install: installUpdate, } return ( diff --git a/packages/app/src/shell/titlebar/titlebar.tsx b/packages/app/src/shell/titlebar/titlebar.tsx index fbe82fa803b..cd3bdb8c736 100644 --- a/packages/app/src/shell/titlebar/titlebar.tsx +++ b/packages/app/src/shell/titlebar/titlebar.tsx @@ -31,6 +31,8 @@ import { SessionTabAvatar } from "@/shell/layout/session-tab-avatar" import { SessionProgressIndicatorV2 } from "@opencode/session-ui/v2/session-progress-indicator-v2" import { projectForSession } from "@/shell/layout/helpers" import { useSettingsDialog } from "@/settings/command" +import { updaterAction } from "@/shell/updates/action" +import type { UpdaterState } from "@/shell/updates/types" import devIcon from "../../../../desktop/icons/dev/64x64.png" import betaIcon from "../../../../desktop/icons/beta/64x64.png" @@ -43,8 +45,7 @@ const macTrafficLightsBaseWidth = 68 const macTrafficLightsTopClearance = 28 export type TitlebarUpdate = { - version: string | undefined - installing: boolean + state: UpdaterState | undefined install: () => void } @@ -96,13 +97,14 @@ export function Titlebar(props: { }) const updateState = createMemo(() => { - const installing = props.update?.installing ?? false - const version = props.update?.version + const state = props.update?.state + const installing = state?.status === "installing" + const version = state?.status === "ready" || state?.status === "download-required" ? state.version : undefined return { visible: version !== undefined || installing, installing, label: language.t("titlebar.update"), - ariaLabel: language.t("toast.update.action.installRestart"), + ariaLabel: language.t(updaterAction(state).label), title: version ? language.t("titlebar.updateVersion", { version }) : undefined, onInstall: () => props.update?.install(), } diff --git a/packages/app/src/shell/updates/action.test.ts b/packages/app/src/shell/updates/action.test.ts index 63d713a4361..774b786e31d 100644 --- a/packages/app/src/shell/updates/action.test.ts +++ b/packages/app/src/shell/updates/action.test.ts @@ -19,6 +19,10 @@ describe("updaterAction", () => { label: "toast.update.action.installRestart", run: "install", }) + expect(updaterAction({ status: "download-required", version: "2.0.0" })).toEqual({ + label: "settings.updates.action.download", + run: "install", + }) expect(updaterAction({ status: "installing", version: "2.0.0" })).toEqual({ label: "settings.updates.action.installing", }) diff --git a/packages/app/src/shell/updates/action.ts b/packages/app/src/shell/updates/action.ts index 857eea3aa2d..fad8c564cba 100644 --- a/packages/app/src/shell/updates/action.ts +++ b/packages/app/src/shell/updates/action.ts @@ -3,7 +3,7 @@ import type { UpdaterState } from "./types" import { usePlatform } from "@/runtime/platform/platform" import { useLanguage } from "@/runtime/i18n/language" import { showToast } from "@/shell/notifications/toast" -import { formatServerError } from "@/runtime/server/errors" +import { useUpdaterInstall } from "@/shell/updates/download" export function updaterAction(state: UpdaterState | undefined) { if (!state) return { label: "settings.updates.action.checkNow" as const } @@ -14,6 +14,8 @@ export function updaterAction(state: UpdaterState | undefined) { return { label: "settings.updates.action.downloading" as const } case "ready": return { label: "toast.update.action.installRestart" as const, run: "install" as const } + case "download-required": + return { label: "settings.updates.action.download" as const, run: "install" as const } case "installing": return { label: "settings.updates.action.installing" as const } case "disabled": @@ -26,34 +28,38 @@ export function updaterAction(state: UpdaterState | undefined) { export function useUpdaterAction() { const platform = usePlatform() const language = useLanguage() + const install = useUpdaterInstall() const action = createMemo(() => updaterAction(platform.updater?.state())) + const check = async () => { + const state = await platform.updater?.check() + if (state?.status === "download-required") { + install() + return + } + if (state?.status === "up-to-date") { + showToast({ + variant: "success", + icon: "circle-check", + title: language.t("settings.updates.toast.latest.title"), + description: language.t("settings.updates.toast.latest.description", { version: platform.version ?? "" }), + }) + } + if (state?.status === "error") { + showToast({ title: language.t("common.requestFailed"), description: state.message }) + } + } return { action, + check, async run() { const run = action().run if (run === "install") { - return platform.updater?.install().catch((error) => { - showToast({ - title: language.t("common.requestFailed"), - description: formatServerError(error, language.t, language.t("common.requestFailed")), - }) - }) + install() + return } if (run !== "check") return - - const state = await platform.updater?.check() - if (state?.status === "up-to-date") { - showToast({ - variant: "success", - icon: "circle-check", - title: language.t("settings.updates.toast.latest.title"), - description: language.t("settings.updates.toast.latest.description", { version: platform.version ?? "" }), - }) - } - if (state?.status === "error") { - showToast({ title: language.t("common.requestFailed"), description: state.message }) - } + await check() }, } } diff --git a/packages/app/src/shell/updates/download.tsx b/packages/app/src/shell/updates/download.tsx new file mode 100644 index 00000000000..8ac4bd075cd --- /dev/null +++ b/packages/app/src/shell/updates/download.tsx @@ -0,0 +1,57 @@ +import { Button } from "@opencode/ui/button" +import { useDialog } from "@opencode/ui/context/dialog" +import { Dialog, DialogFooter, DialogHeader, DialogTitleGroup } from "@opencode/ui/dialog" +import { useLanguage } from "@/runtime/i18n/language" +import { usePlatform } from "@/runtime/platform/platform" +import { formatServerError } from "@/runtime/server/errors" +import { showToast } from "@/shell/notifications/toast" + +export function useUpdaterInstall() { + const dialog = useDialog() + const language = useLanguage() + const platform = usePlatform() + const download = () => + platform.updater?.install().catch((error) => { + showToast({ + title: language.t("common.requestFailed"), + description: formatServerError(error, language.t, language.t("common.requestFailed")), + }) + }) + + return () => { + const state = platform.updater?.state() + if (state?.status !== "download-required") { + void download() + return + } + void dialog.show(() => ) + } +} + +export function DialogStableDownload(props: { version: string; download: () => Promise | undefined }) { + const dialog = useDialog() + const language = useLanguage() + const download = () => { + dialog.close() + void props.download() + } + + return ( + + + + + + + + + + ) +} diff --git a/packages/app/src/shell/updates/types.ts b/packages/app/src/shell/updates/types.ts index 2d8c271705b..b7209d8522a 100644 --- a/packages/app/src/shell/updates/types.ts +++ b/packages/app/src/shell/updates/types.ts @@ -6,6 +6,7 @@ export type UpdaterState = | { status: "checking" } | { status: "downloading"; version: string } | { status: "ready"; version: string } + | { status: "download-required"; version: string } | { status: "up-to-date" } | { status: "installing"; version: string } | { status: "error"; message: string } diff --git a/packages/desktop/src/main/native/menu.ts b/packages/desktop/src/main/native/menu.ts index d8640337e7c..b572a8aad37 100644 --- a/packages/desktop/src/main/native/menu.ts +++ b/packages/desktop/src/main/native/menu.ts @@ -9,7 +9,7 @@ import { import { MenuCommandTriggered } from "../../shared/ipc-rpc/events" import { emitIpcEvent } from "../ipc-events" -import { UPDATER_ENABLED } from "../constants" +import { CHANNEL, UPDATER_ENABLED } from "../constants" import { runDesktopMenuAction } from "./menu-actions" import { nativeT } from "./translations" @@ -58,6 +58,10 @@ function nativeItem(entry: DesktopMenuEntry, deps: Deps): MenuItemConstructorOpt } if (entry.action) { const action = entry.action + if (action === "app.checkForUpdates" && CHANNEL === "beta") { + item.click = () => deps.trigger(action) + return item + } item.click = () => runDesktopMenuAction(BrowserWindow.getFocusedWindow(), action, { checkForUpdates: deps.checkForUpdates, diff --git a/packages/desktop/src/main/updater/index.test.ts b/packages/desktop/src/main/updater/index.test.ts index af0d17d4690..17758792a37 100644 --- a/packages/desktop/src/main/updater/index.test.ts +++ b/packages/desktop/src/main/updater/index.test.ts @@ -17,6 +17,8 @@ function setup(input?: { latest?: () => string stage?: () => Promise install?: () => Promise + external?: boolean + open?: () => Promise }) { const calls: string[] = [] let ready = input?.ready @@ -26,7 +28,10 @@ function setup(input?: { checkForUpdate: Effect.try({ try: () => { calls.push("check") - return input?.latest?.() ?? "2.0.0" + const version = input?.latest?.() ?? "2.0.0" + return input?.external + ? { mode: "external" as const, version, url: `https://files.test/${version}.dmg` } + : { mode: "restart" as const, version } }, catch: (error) => error, }), @@ -42,6 +47,13 @@ function setup(input?: { catch: (error) => error, }) }), + externalInstall: input?.external + ? (url) => + Effect.tryPromise(async () => { + calls.push(`external:${url}`) + await input.open?.() + }) + : undefined, dispose: () => {}, }, prepareToRestart: Effect.sync(() => { @@ -83,6 +95,62 @@ describe("updater", () => { expect(app.getReady()).toEqual({ version: "2.0.0" }) }) + test("offers an external installer without staging or preparing to restart", async () => { + const app = setup({ external: true }) + + await app.updater.start() + expect(app.calls).toEqual(["check"]) + expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" }) + expect(app.getReady()).toBeUndefined() + + await app.updater.install() + expect(app.calls).toEqual(["check", "check", "external:https://files.test/2.0.0.dmg"]) + expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" }) + }) + + test("offers an external installer when its version equals the running beta", async () => { + const app = setup({ currentVersion: "2.0.0", external: true }) + + await app.updater.start() + + expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" }) + }) + + test("keeps an external installer available when a refresh fails", async () => { + let offline = false + const app = setup({ + external: true, + latest: () => { + if (offline) throw new Error("offline") + return "2.0.0" + }, + }) + await app.updater.start() + + offline = true + await app.updater.check() + + expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" }) + }) + + test("opens an external installer once for concurrent clicks", async () => { + let release = () => {} + const app = setup({ + external: true, + open: () => new Promise((resolve) => (release = resolve)), + }) + await app.updater.start() + + const first = app.updater.install() + const second = app.updater.install() + await new Promise((resolve) => setTimeout(resolve, 0)) + + expect(app.calls).toEqual(["check", "check", "external:https://files.test/2.0.0.dmg"]) + release() + await Promise.all([first, second]) + expect(await app.updater.getState()).toEqual({ status: "download-required", version: "2.0.0" }) + }) + test("reports up to date and clears the record once the update is installed", async () => { const app = setup({ currentVersion: "2.0.0", ready: { version: "2.0.0" } }) diff --git a/packages/desktop/src/main/updater/index.ts b/packages/desktop/src/main/updater/index.ts index 3a2f0500a22..d6042287469 100644 --- a/packages/desktop/src/main/updater/index.ts +++ b/packages/desktop/src/main/updater/index.ts @@ -6,10 +6,15 @@ import type { UpdaterState } from "@opencode/app/updater" import { UpdaterStateChanged } from "../../shared/ipc-rpc/events" import { emitIpcEvent } from "../ipc-events" +export type UpdateTarget = + | { readonly mode: "restart"; readonly version: string } + | { readonly mode: "external"; readonly version: string; readonly url: string } + export type Platform = { - readonly checkForUpdate: Effect.Effect + readonly checkForUpdate: Effect.Effect readonly stageUpdate: (options: { readonly differential: boolean }) => Effect.Effect readonly installAndRestart: Effect.Effect + readonly externalInstall?: (url: string) => Effect.Effect readonly dispose: () => void } @@ -59,6 +64,7 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc // the last download. Once a release is staged without installing, the two no longer match and every later delta fails // its checksum before falling back to a full download, so remember which release the cache holds and skip the attempt. let downloaded: string | undefined + let target: UpdateTarget | undefined const stage = (platform: Platform, version: string) => Effect.gen(function* () { if (downloaded) @@ -74,14 +80,19 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc const findAndStage = (platform: Platform) => Effect.gen(function* () { yield* Effect.sync(() => transition({ status: "checking" })) - const version = yield* platform.checkForUpdate - if (!version || version === dependencies.currentVersion) { + const next = yield* platform.checkForUpdate + if (!next || (next.mode === "restart" && next.version === dependencies.currentVersion)) { yield* dependencies.persistence.clear return transition({ status: "up-to-date" }) } - transition({ status: "downloading", version }) - yield* stage(platform, version) - return transition({ status: "ready", version }) + target = next + if (next.mode === "external") { + yield* dependencies.persistence.clear + return transition({ status: "download-required", version: next.version }) + } + transition({ status: "downloading", version: next.version }) + yield* stage(platform, next.version) + return transition({ status: "ready", version: next.version }) }).pipe( Effect.catch((error) => Effect.sync(() => @@ -91,10 +102,16 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc ) const refreshStaged = (platform: Platform, staged: string) => Effect.gen(function* () { - const version = yield* platform.checkForUpdate - if (!version || version === staged || version === dependencies.currentVersion) return state - yield* stage(platform, version) - return transition({ status: installing ? "installing" : "ready", version }) + const next = yield* platform.checkForUpdate + if (!next) return state + target = next + if (next.mode === "external") { + yield* dependencies.persistence.clear + return transition({ status: "download-required", version: next.version }) + } + if (next.version === staged || next.version === dependencies.currentVersion) return state + yield* stage(platform, next.version) + return transition({ status: installing ? "installing" : "ready", version: next.version }) }).pipe( Effect.catch((error) => Effect.sync(() => { @@ -108,13 +125,38 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc }), ), ) + const refreshExternal = (platform: Platform) => + Effect.gen(function* () { + const next = yield* platform.checkForUpdate + if (!next || next.mode !== "external") return state + target = next + if (state.status === "download-required" && state.version === next.version) return state + return transition({ status: "download-required", version: next.version }) + }).pipe( + Effect.catch((error) => + Effect.sync(() => { + runFork( + Effect.logWarning("external updater refresh failed, keeping download available", { + message: error instanceof Error ? error.message : String(error), + }), + ) + return state + }), + ), + ) const check = Effect.suspend(() => { const platform = dependencies.platform if (!platform || state.status === "installing") return Effect.succeed(state) if (pending) return Deferred.await(pending) const deferred = Deferred.makeUnsafe() pending = deferred - return (state.status === "ready" ? refreshStaged(platform, state.version) : findAndStage(platform)).pipe( + const update = + state.status === "ready" + ? refreshStaged(platform, state.version) + : state.status === "download-required" + ? refreshExternal(platform) + : findAndStage(platform) + return update.pipe( Effect.tap((result) => Deferred.succeed(deferred, result)), Effect.ensuring(Effect.sync(() => (pending = undefined))), ) @@ -122,7 +164,34 @@ export const make = Effect.fn("Updater.make")(function* (dependencies: Dependenc const install = Effect.suspend(() => { if (installing) return Deferred.await(installing) const platform = dependencies.platform - if (!platform || state.status !== "ready") return Effect.fail(new Error("Update is not ready to install")) + if (!platform) return Effect.fail(new Error("Update is not ready to install")) + if (state.status === "download-required") { + const staged = state.version + const deferred = Deferred.makeUnsafe() + installing = deferred + return Effect.gen(function* () { + yield* pending ? Deferred.await(pending) : refreshExternal(platform) + if (!target || target.mode !== "external" || !platform.externalInstall) + return yield* Effect.fail(new Error("External installer is unavailable")) + transition({ status: "installing", version: target.version }) + return yield* platform.externalInstall(target.url) + }).pipe( + Effect.exit, + Effect.flatMap((exit) => + Deferred.done(deferred, exit).pipe( + Effect.andThen( + Effect.sync(() => { + installing = undefined + if (state.status === "installing") + transition({ status: "download-required", version: target?.version ?? staged }) + }), + ), + Effect.andThen(Deferred.await(deferred)), + ), + ), + ) + } + if (state.status !== "ready") return Effect.fail(new Error("Update is not ready to install")) const staged = state.version transition({ status: "installing", version: staged }) const deferred = Deferred.makeUnsafe() diff --git a/packages/desktop/src/main/updater/live.ts b/packages/desktop/src/main/updater/live.ts index 149918a1303..828f78d0bf3 100644 --- a/packages/desktop/src/main/updater/live.ts +++ b/packages/desktop/src/main/updater/live.ts @@ -65,6 +65,7 @@ const show = Effect.fn("Updater.show")(function* ( ) return } + if (state.status === "download-required") return if (state.status !== "ready") return const response = yield* promise(() => diff --git a/packages/desktop/src/main/updater/migration.test.ts b/packages/desktop/src/main/updater/migration.test.ts new file mode 100644 index 00000000000..b42929cd302 --- /dev/null +++ b/packages/desktop/src/main/updater/migration.test.ts @@ -0,0 +1,32 @@ +import { expect, test } from "bun:test" +import { requiresStableMacInstaller, stableMacDownload } from "./migration" + +test("uses the external stable installer only for macOS beta", () => { + expect(requiresStableMacInstaller("darwin", "beta")).toBe(true) + expect(requiresStableMacInstaller("darwin", "prod")).toBe(false) + expect(requiresStableMacInstaller("win32", "beta")).toBe(false) + expect(requiresStableMacInstaller("linux", "beta")).toBe(false) +}) + +test("selects the signed stable installer for the current Mac architecture", () => { + const artifact = { + version: "2.0.2", + metadata: { + files: { + "opencode-desktop-mac-arm64.dmg": { url: "https://files.test/OpenCode-arm64.dmg" }, + "opencode-desktop-mac-x64.dmg": { url: "https://files.test/OpenCode-x64.dmg" }, + }, + }, + } + + expect(stableMacDownload(artifact, "arm64")).toEqual({ + version: "2.0.2", + url: "https://files.test/OpenCode-arm64.dmg", + }) + expect(stableMacDownload(artifact, "x64")).toEqual({ + version: "2.0.2", + url: "https://files.test/OpenCode-x64.dmg", + }) + expect(stableMacDownload(artifact, "ia32")).toBeUndefined() + expect(stableMacDownload({ version: "2.0.2", metadata: { files: {} } }, "arm64")).toBeUndefined() +}) diff --git a/packages/desktop/src/main/updater/migration.ts b/packages/desktop/src/main/updater/migration.ts new file mode 100644 index 00000000000..5ab50519272 --- /dev/null +++ b/packages/desktop/src/main/updater/migration.ts @@ -0,0 +1,20 @@ +import { Option, Schema } from "effect" + +const Artifact = Schema.Struct({ + version: Schema.String, + metadata: Schema.Struct({ + files: Schema.Record(Schema.String, Schema.Struct({ url: Schema.String })), + }), +}) +const decode = Schema.decodeUnknownOption(Artifact) + +export function requiresStableMacInstaller(platform: string, channel: string) { + return platform === "darwin" && channel === "beta" +} + +export function stableMacDownload(input: unknown, arch: string) { + if (arch !== "arm64" && arch !== "x64") return undefined + const artifact = Option.getOrUndefined(decode(input)) + const file = artifact?.metadata.files[`opencode-desktop-mac-${arch}.dmg`] + return file && { version: artifact.version, url: file.url } +} diff --git a/packages/desktop/src/main/updater/platform.ts b/packages/desktop/src/main/updater/platform.ts index 31354c13de0..a75ddaa4d51 100644 --- a/packages/desktop/src/main/updater/platform.ts +++ b/packages/desktop/src/main/updater/platform.ts @@ -1,13 +1,18 @@ import { app, autoUpdater } from "electron" import pkg from "electron-updater" import { Effect } from "effect" +import { CHANNEL } from "../constants" +import { openExternalURL } from "../files" import { setAppQuitting } from "../windows" import type { Platform } from "./index" +import { requiresStableMacInstaller, stableMacDownload } from "./migration" const updateClient = pkg.autoUpdater const restartTimeout = 10_000 +const stableArtifact = "https://opencode.ai/update/api/latest/desktop/opencode" export const make = Effect.gen(function* () { + const external = requiresStableMacInstaller(process.platform, CHANNEL) const runFork = Effect.runForkWith(yield* Effect.context()) updateClient.logger = { info: (...args) => runFork(Effect.logInfo(...args)), @@ -32,13 +37,22 @@ export const make = Effect.gen(function* () { return { checkForUpdate: Effect.tryPromise({ try: async () => { + if (external) { + const response = await fetch(stableArtifact) + if (!response.ok) throw new Error(`Stable OpenCode update check failed: ${response.status}`) + const download = stableMacDownload(await response.json(), process.arch) + if (!download) throw new Error("Stable OpenCode download is unavailable") + return { mode: "external", ...download } as const + } const result = await updateClient.checkForUpdates() - return result?.isUpdateAvailable ? result.updateInfo.version : undefined + if (!result?.isUpdateAvailable) return undefined + return { mode: "restart", version: result.updateInfo.version } as const }, catch: (error) => error, }), stageUpdate, installAndRestart, + externalInstall: external ? (url) => openExternalURL(url) : undefined, dispose: () => autoUpdater.off("before-quit-for-update", beforeQuit), } satisfies Platform }) diff --git a/packages/desktop/src/shared/ipc-rpc/updater.ts b/packages/desktop/src/shared/ipc-rpc/updater.ts index 6c25ac305e9..bc9e601be06 100644 --- a/packages/desktop/src/shared/ipc-rpc/updater.ts +++ b/packages/desktop/src/shared/ipc-rpc/updater.ts @@ -7,6 +7,7 @@ export const UpdaterStateSchema = Schema.Union([ Schema.Struct({ status: Schema.Literal("checking") }), Schema.Struct({ status: Schema.Literal("downloading"), version: Schema.String }), Schema.Struct({ status: Schema.Literal("ready"), version: Schema.String }), + Schema.Struct({ status: Schema.Literal("download-required"), version: Schema.String }), Schema.Struct({ status: Schema.Literal("up-to-date") }), Schema.Struct({ status: Schema.Literal("installing"), version: Schema.String }), Schema.Struct({ status: Schema.Literal("error"), message: Schema.String }),