diff --git a/packages/opencode/src/cli/cmd/upgrade.ts b/packages/opencode/src/cli/cmd/upgrade.ts index 3c1604a0b83..e64529e050a 100644 --- a/packages/opencode/src/cli/cmd/upgrade.ts +++ b/packages/opencode/src/cli/cmd/upgrade.ts @@ -19,8 +19,12 @@ export const UpgradeCommand = { type: "string", choices: ["curl", "npm", "pnpm", "bun", "brew", "choco", "scoop"], }) + .option("major", { + describe: "upgrade to OpenCode 2 and remove OpenCode 1", + type: "boolean", + }) }, - handler: async (args: { target?: string; method?: string }) => { + handler: async (args: { target?: string; method?: string; major?: boolean }) => { UI.empty() UI.println(UI.logo(" ")) UI.empty() @@ -43,10 +47,29 @@ export const UpgradeCommand = { } } prompts.log.info("Using method: " + method) + + if (args.major) { + prompts.log.info(`From ${InstallationVersion} → OpenCode 2`) + const spinner = prompts.spinner() + spinner.start("Installing OpenCode 2...") + const err = await Installation.upgradeMajor(method).catch((err) => err) + if (err) { + spinner.stop("Upgrade failed", 1) + if (err instanceof Error) prompts.log.error(err.message) + prompts.log.info(`To install OpenCode 2 manually, see ${Installation.MAJOR_INSTRUCTIONS}`) + prompts.outro("Done") + return + } + spinner.stop("OpenCode 2 installed") + prompts.outro("Done") + return + } + const target = args.target ? args.target.replace(/^v/, "") : await Installation.latest() if (InstallationVersion === target) { prompts.log.warn(`opencode upgrade skipped: ${target} is already installed`) + prompts.log.info("OpenCode 2 is available. Upgrade with `opencode upgrade --major`") prompts.outro("Done") return } @@ -69,6 +92,7 @@ export const UpgradeCommand = { return } spinner.stop("Upgrade complete") + prompts.log.info("OpenCode 2 is available. Upgrade with `opencode upgrade --major`") prompts.outro("Done") }, } diff --git a/packages/opencode/src/installation/index.ts b/packages/opencode/src/installation/index.ts index 4300220255b..b91a0314d26 100644 --- a/packages/opencode/src/installation/index.ts +++ b/packages/opencode/src/installation/index.ts @@ -77,8 +77,36 @@ export interface Interface { readonly method: () => Effect.Effect readonly latest: (method?: Method) => Effect.Effect readonly upgrade: (method: Method, target: string) => Effect.Effect + readonly upgradeMajor: (method: Method) => Effect.Effect } +// OpenCode 2 ships under different package names but the same `opencode` binary, so package +// manager installs remove V1 first and restore it if the V2 install fails. +const MAJOR_INSTALL: Partial> = { + npm: { + remove: ["npm", "uninstall", "-g", "opencode-ai"], + install: ["npm", "install", "-g", "@opencode/cli"], + restore: ["npm", "install", "-g", `opencode-ai@${InstallationVersion}`], + }, + pnpm: { + remove: ["pnpm", "remove", "-g", "opencode-ai"], + install: ["pnpm", "add", "-g", "--allow-build=@opencode/cli", "@opencode/cli"], + restore: ["pnpm", "add", "-g", `opencode-ai@${InstallationVersion}`], + }, + bun: { + remove: ["bun", "remove", "-g", "opencode-ai"], + install: ["bun", "install", "-g", "--trust", "@opencode/cli"], + restore: ["bun", "install", "-g", `opencode-ai@${InstallationVersion}`], + }, + yarn: { + remove: ["yarn", "global", "remove", "opencode-ai"], + install: ["yarn", "global", "add", "@opencode/cli"], + restore: ["yarn", "global", "add", `opencode-ai@${InstallationVersion}`], + }, +} + +export const MAJOR_INSTRUCTIONS = "https://opencode.ai/v2/docs" + export class Service extends Context.Service()("@opencode/Installation") {} export const use = serviceUse(Service) @@ -164,6 +192,40 @@ const layer: Layer.Layer new UpgradeFailedError({ stderr: upgradeFailure("curl") })), ) + const majorUpgrade = Effect.fnUntraced(function* (m: Method) { + if (m === "curl") { + // The V2 installer writes to the same ~/.opencode/bin/opencode path, replacing V1 in place. + const response = yield* httpOk + .execute(HttpClientRequest.get("https://opencode.ai/v2/install")) + .pipe(Effect.mapError(() => new UpgradeFailedError({ stderr: upgradeFailure("curl") }))) + const body = yield* response.text.pipe( + Effect.mapError(() => new UpgradeFailedError({ stderr: upgradeFailure("curl") })), + ) + return yield* run([yield* upgradeScriptShell(), "-c", body]) + } + + if (m === "brew") { + const formula = yield* getBrewFormula() + const env = { HOMEBREW_NO_AUTO_UPDATE: "1" } + const removed = yield* run(["brew", "uninstall", formula], { env }) + if (removed.code !== 0) return removed + const installed = yield* run(["brew", "install", "anomalyco/tap/opencode-v2"], { env }) + if (installed.code !== 0) yield* run(["brew", "install", formula], { env }) + return installed + } + + const steps = MAJOR_INSTALL[m] + if (!steps) + return yield* new UpgradeFailedError({ + stderr: `OpenCode 2 can't be installed automatically with ${m}. See ${MAJOR_INSTRUCTIONS}`, + }) + const removed = yield* run(steps.remove) + if (removed.code !== 0) return removed + const installed = yield* run(steps.install) + if (installed.code !== 0) yield* run(steps.restore) + return installed + }) + const result: Interface = { info: Effect.fn("Installation.info")(function* () { return { @@ -319,6 +381,11 @@ const layer: Layer.Layer) => runPromise((s) => s.latest(...args)) export const method = () => runPromise((s) => s.method()) export const upgrade = (...args: Parameters) => runPromise((s) => s.upgrade(...args)) +export const upgradeMajor = (...args: Parameters) => + runPromise((s) => s.upgradeMajor(...args)) export * as Installation from "." diff --git a/packages/opencode/test/installation/installation.test.ts b/packages/opencode/test/installation/installation.test.ts index 3d4b4cba31a..891725dfbee 100644 --- a/packages/opencode/test/installation/installation.test.ts +++ b/packages/opencode/test/installation/installation.test.ts @@ -237,4 +237,102 @@ describe("installation", () => { }), ) }) + + describe("upgradeMajor", () => { + const calls: string[] = [] + testEffect( + testLayer( + () => jsonResponse({}), + (cmd, args) => { + calls.push([cmd, ...args].join(" ")) + return "" + }, + ), + ).effect("replaces the npm package with OpenCode 2", () => + Effect.gen(function* () { + calls.length = 0 + yield* Installation.use.upgradeMajor("npm") + expect(calls.slice(0, 2)).toEqual(["npm uninstall -g opencode-ai", "npm install -g @opencode/cli"]) + }), + ) + + const restoreCalls: string[] = [] + testEffect( + testLayer( + () => jsonResponse({}), + (cmd, args) => { + restoreCalls.push([cmd, ...args].join(" ")) + if (args.includes("@opencode/cli")) return { code: 1, stderr: "install failed" } + return "" + }, + ), + ).effect("restores OpenCode 1 when the OpenCode 2 install fails", () => + Effect.gen(function* () { + restoreCalls.length = 0 + const error = yield* Effect.flip(Installation.use.upgradeMajor("npm")) + expect(error.stderr).toBe("Upgrade failed for npm (exit code 1).") + expect(restoreCalls[2]).toMatch(/^npm install -g opencode-ai@/) + }), + ) + + const brewCalls: string[] = [] + testEffect( + testLayer( + () => jsonResponse({}), + (cmd, args) => { + brewCalls.push([cmd, ...args].join(" ")) + if (args.join(" ") === "list --formula anomalyco/tap/opencode") return "opencode" + return "" + }, + ), + ).effect("replaces the brew formula with the OpenCode 2 formula", () => + Effect.gen(function* () { + brewCalls.length = 0 + yield* Installation.use.upgradeMajor("brew") + expect(brewCalls).toContain("brew uninstall anomalyco/tap/opencode") + expect(brewCalls).toContain("brew install anomalyco/tap/opencode-v2") + }), + ) + + const curlCalls: string[] = [] + const curlUrls: string[] = [] + testEffect( + testLayer( + (request) => { + curlUrls.push(request.url) + return new Response("echo v2", { status: 200 }) + }, + (cmd, args) => { + curlCalls.push(cmd) + if (cmd === "bash" && args[0] === "--version") return "GNU bash" + return "" + }, + ), + ).effect("runs the OpenCode 2 install script for curl installs", () => + Effect.gen(function* () { + curlUrls.length = 0 + yield* Installation.use.upgradeMajor("curl") + expect(curlUrls).toEqual(["https://opencode.ai/v2/install"]) + expect(curlCalls).toContain("bash") + }), + ) + + const chocoCalls: string[] = [] + testEffect( + testLayer( + () => jsonResponse({}), + (cmd) => { + chocoCalls.push(cmd) + return "" + }, + ), + ).effect("leaves package managers without OpenCode 2 untouched", () => + Effect.gen(function* () { + chocoCalls.length = 0 + const error = yield* Effect.flip(Installation.use.upgradeMajor("choco")) + expect(error.stderr).toContain(Installation.MAJOR_INSTRUCTIONS) + expect(chocoCalls).toEqual([]) + }), + ) + }) })