Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/opencode/src/cli/cmd/tui/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Rpc } from "@/util/rpc"
import { type rpc } from "./worker"
import path from "path"
import { fileURLToPath } from "url"
import { spawn } from "child_process"
import { UI } from "@/cli/ui"
import * as Log from "@opencode-ai/core/util/log"
import { errorMessage } from "@/util/error"
Expand Down Expand Up @@ -224,7 +225,17 @@ export const TuiThreadCommand = cmd({
}

setTimeout(() => {
client.call("checkUpgrade", { directory: cwd }).catch(() => {})
client.call("checkUpgrade", { directory: cwd }).then((upgraded: unknown) => {
if (upgraded) {
console.log("Update installed. Restarting...")
const child = spawn(process.execPath, process.argv.slice(1), {
stdio: "inherit",
env: process.env,
})
child.unref()
process.exit(0)
}
}).catch(() => {})
}, 1000).unref?.()

try {
Expand Down
7 changes: 6 additions & 1 deletion packages/opencode/src/cli/cmd/tui/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ export const rpc = {
},
async checkUpgrade(input: { directory: string }) {
await InstanceRuntime.load({ directory: input.directory })
await upgrade().catch(() => {})
const upgraded = await upgrade().catch(() => false)
if (upgraded) {
await InstanceRuntime.disposeAllInstances().catch(() => {})
if (server) await server.stop(true).catch(() => {})
}
return upgraded
},
async reload() {
await AppRuntime.runPromise(
Expand Down
28 changes: 15 additions & 13 deletions packages/opencode/src/cli/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { Installation } from "@/installation"
import { InstallationVersion } from "@opencode-ai/core/installation/version"
import { GlobalBus } from "@/bus/global"

export async function upgrade() {
export async function upgrade(): Promise<boolean> {
const config = await AppRuntime.runPromise(Config.Service.use((cfg) => cfg.getGlobal()))
if (config.autoupdate === false || Flag.OPENCODE_DISABLE_AUTOUPDATE) return
if (config.autoupdate === false || Flag.OPENCODE_DISABLE_AUTOUPDATE) return false
const method = await Installation.method()
const latest = await Installation.latest(method).catch(() => {})
if (!latest) return
const latest = await Installation.latest(method).catch(() => undefined)
if (!latest) return false

if (Flag.OPENCODE_ALWAYS_NOTIFY_UPDATE) {
GlobalBus.emit("event", {
Expand All @@ -20,10 +20,10 @@ export async function upgrade() {
properties: { version: latest },
},
})
return
return false
}

if (InstallationVersion === latest) return
if (InstallationVersion === latest) return false

const kind = Installation.getReleaseType(InstallationVersion, latest)

Expand All @@ -35,19 +35,21 @@ export async function upgrade() {
properties: { version: latest },
},
})
return
return false
}

if (method === "unknown") return
await Installation.upgrade(method, latest)
.then(() =>
if (method === "unknown") return false
const upgraded = await Installation.upgrade(method, latest)
.then(() => {
GlobalBus.emit("event", {
directory: "global",
payload: {
type: Installation.Event.Updated.type,
properties: { version: latest },
},
}),
)
.catch(() => {})
})
return true
})
.catch(() => false)
return upgraded
}
12 changes: 12 additions & 0 deletions packages/opencode/test/cli/upgrade.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, test } from "bun:test"
import { Installation } from "@/installation"

describe("upgrade", () => {
test("Installation.Event.Updated type matches expected value", () => {
expect(Installation.Event.Updated.type).toBe("installation.updated")
})

test("Installation.Event.UpdateAvailable type matches expected value", () => {
expect(Installation.Event.UpdateAvailable.type).toBe("installation.update-available")
})
})
Loading