mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(tui): tell users to start OpenCode again after an update restart (#58139)
Co-authored-by: vimtor <36263538+vimtor@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
62c9002819
commit
58d5c02e03
@@ -11,6 +11,7 @@ import { ClipboardProvider, useClipboard } from "./context/clipboard"
|
||||
import { LogProvider, type LogSink } from "./context/log"
|
||||
import { ExitProvider, useExit } from "./context/exit"
|
||||
import { EpilogueProvider } from "./context/epilogue"
|
||||
import { epilogue, type SessionEpilogue } from "./util/presentation"
|
||||
import { Selection } from "./util/selection"
|
||||
import {
|
||||
CliRenderEvents,
|
||||
@@ -236,7 +237,11 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
|
||||
restart: managed.restart,
|
||||
}
|
||||
: undefined
|
||||
const exit = { epilogue: undefined as string | undefined, reason: undefined as unknown }
|
||||
const exit = {
|
||||
session: undefined as SessionEpilogue | undefined,
|
||||
updatedVersion: undefined as string | undefined,
|
||||
reason: undefined as unknown,
|
||||
}
|
||||
const result = yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const options = {
|
||||
@@ -317,7 +322,12 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
|
||||
destroyRenderer(renderer)
|
||||
}}
|
||||
>
|
||||
<EpilogueProvider set={(value) => (exit.epilogue = value)}>
|
||||
<EpilogueProvider
|
||||
value={{
|
||||
session: (value) => (exit.session = value),
|
||||
updatedVersion: (version) => (exit.updatedVersion = version),
|
||||
}}
|
||||
>
|
||||
<TuiAppProvider value={input.app}>
|
||||
<ErrorBoundary
|
||||
fallback={(error, reset) => (
|
||||
@@ -457,13 +467,14 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
|
||||
}
|
||||
})
|
||||
yield* shutdown.await
|
||||
return { epilogue: exit.epilogue, reason: exit.reason }
|
||||
return { session: exit.session, updatedVersion: exit.updatedVersion, reason: exit.reason }
|
||||
}),
|
||||
)
|
||||
yield* Effect.sync(() => {
|
||||
if (result.reason !== undefined)
|
||||
process.stderr.write((cliErrorMessage(result.reason) ?? errorFormat(result.reason)) + "\n")
|
||||
if (result.epilogue) process.stdout.write(result.epilogue + "\n")
|
||||
const text = epilogue({ session: result.session, updatedVersion: result.updatedVersion })
|
||||
if (text) process.stdout.write(text + "\n")
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ export function DialogUpdate(props: {
|
||||
</Match>
|
||||
<Match when={current.type === "installed"}>
|
||||
<text fg={theme.text.muted} wrapMode="word">
|
||||
Update successful! A restart is required. Any active sessions will be resumed automatically.
|
||||
Update successful! Restart closes OpenCode, then run opencode to use the new version.
|
||||
</text>
|
||||
</Match>
|
||||
<Match when={current.type === "current"}>
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import { createSimpleContext } from "./helper"
|
||||
import type { SessionEpilogue } from "../util/presentation"
|
||||
|
||||
// What the banner printed after the TUI closes should mention.
|
||||
export type Epilogue = {
|
||||
session(value?: SessionEpilogue): void
|
||||
// The version a local update installed while the TUI was running.
|
||||
updatedVersion(version: string): void
|
||||
}
|
||||
|
||||
export const { use: useEpilogue, provider: EpilogueProvider } = createSimpleContext({
|
||||
name: "Epilogue",
|
||||
init: (props: { set(value?: string): void }) => props.set,
|
||||
init: (props: { value: Epilogue }) => props.value,
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useStorage } from "./storage"
|
||||
import { useEvent } from "./event"
|
||||
import { errorMessage } from "../util/error"
|
||||
import { useExit } from "./exit"
|
||||
import { useEpilogue } from "./epilogue"
|
||||
import { useDialog } from "../ui/dialog"
|
||||
import { DialogUpdate } from "../component/dialog-update"
|
||||
|
||||
@@ -30,6 +31,7 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider
|
||||
init: (props: { updater?: UpdateSource }) => {
|
||||
const event = useEvent()
|
||||
const exit = useExit()
|
||||
const epilogue = useEpilogue()
|
||||
const dialog = useDialog()
|
||||
const log = useLog({ component: "update-notification" })
|
||||
const [state, setState] = createSignal<UpdateState>()
|
||||
@@ -40,6 +42,9 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider
|
||||
|
||||
const notify = (notice: Notice) => {
|
||||
if (!props.updater) return
|
||||
// Every way out of the TUI after a local install tells the user to start the new version.
|
||||
if (notice.type === "installed" && !(notice.source === "server" && notice.remote))
|
||||
epilogue.updatedVersion(notice.version)
|
||||
if (
|
||||
notifications.versions.includes(`${notice.source}:${notice.version}`) ||
|
||||
(notice.source === "client" && notifications.versions.includes(notice.version))
|
||||
@@ -67,7 +72,10 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider
|
||||
if (!updater || !current || current.type !== "available") return
|
||||
setState({ type: "installing", version: current.version })
|
||||
await updater.apply(current.version).then(
|
||||
() => setState({ type: "installed", version: current.version }),
|
||||
() => {
|
||||
epilogue.updatedVersion(current.version)
|
||||
setState({ type: "installed", version: current.version })
|
||||
},
|
||||
(error) => setState({ type: "failed", message: errorMessage(error) }),
|
||||
)
|
||||
}
|
||||
@@ -84,6 +92,7 @@ export const { use: useUpdateNotification, provider: UpdateNotificationProvider
|
||||
})
|
||||
if (signal.aborted) return
|
||||
if (result?.type === "unavailable") return result.message
|
||||
if (result?.type === "installed") epilogue.updatedVersion(result.version)
|
||||
setState(result)
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,6 @@ import { PermissionPrompt } from "./permission"
|
||||
import { FormPrompt } from "./form"
|
||||
import { DialogExportOptions } from "../../ui/dialog-export-options"
|
||||
import { DialogExportResult } from "../../ui/dialog-export-result"
|
||||
import { sessionEpilogue } from "../../util/presentation"
|
||||
import { useConfig, useNewDesigns } from "../../config"
|
||||
import { useClipboard } from "../../context/clipboard"
|
||||
import { nextThinkingMode, type ThinkingMode } from "../../context/thinking"
|
||||
@@ -157,7 +156,7 @@ export function Session(props: {
|
||||
onTerminalPicker?: (show: (() => void) | undefined) => void
|
||||
width?: number
|
||||
}) {
|
||||
const setEpilogue = useEpilogue()
|
||||
const epilogue = useEpilogue()
|
||||
const clipboard = useClipboard()
|
||||
const writeExport = async (file: string, content: string) => {
|
||||
await mkdir(path.dirname(file), { recursive: true })
|
||||
@@ -196,9 +195,9 @@ export function Session(props: {
|
||||
|
||||
createEffect(() => {
|
||||
const title = Locale.truncate(session()?.title ?? "", 50)
|
||||
setEpilogue(sessionEpilogue({ title, sessionID: session()?.id }))
|
||||
epilogue.session({ title, sessionID: session()?.id })
|
||||
})
|
||||
onCleanup(() => setEpilogue())
|
||||
onCleanup(() => epilogue.session())
|
||||
const descendantSessionIDs = createMemo(() => {
|
||||
if (session()?.parentID) return []
|
||||
return data.session.family(route.sessionID).filter((id) => id !== route.sessionID)
|
||||
|
||||
@@ -23,13 +23,20 @@ function wordmark(pad = "") {
|
||||
})
|
||||
}
|
||||
|
||||
export function sessionEpilogue(input: { title: string; sessionID?: string }) {
|
||||
const weak = (text: string) => `${dim}${text.padEnd(10, " ")}${reset}`
|
||||
export type SessionEpilogue = { readonly title: string; readonly sessionID?: string }
|
||||
|
||||
// Printed after the TUI closes.
|
||||
export function epilogue(input: { readonly session?: SessionEpilogue; readonly updatedVersion?: string }) {
|
||||
if (!input.session && !input.updatedVersion) return
|
||||
const row = (label: string, value: string) => ` ${dim}${label.padEnd(10, " ")}${reset}${bold}${value}${reset}`
|
||||
return [
|
||||
...wordmark(" "),
|
||||
"",
|
||||
` ${weak("Session")}${bold}${input.title}${reset}`,
|
||||
` ${weak("Continue")}${bold}opencode -s ${input.sessionID}${reset}`,
|
||||
"",
|
||||
...(input.updatedVersion
|
||||
? [` ${dim}Run ${reset}${bold}opencode${reset}${dim} to use ${reset}${bold}${input.updatedVersion}${reset}`, ""]
|
||||
: []),
|
||||
...(input.session
|
||||
? [row("Session", input.session.title), row("Continue", `opencode -s ${input.session.sessionID}`), ""]
|
||||
: []),
|
||||
].join("\n")
|
||||
}
|
||||
|
||||
@@ -2771,3 +2771,119 @@ test.each([44, 100])(
|
||||
},
|
||||
15_000,
|
||||
)
|
||||
|
||||
test("restarting after an update tells the user to start OpenCode again", async () => {
|
||||
await using state = await tmpdir()
|
||||
const setup = await createTestRenderer({ width: 80, height: 24 })
|
||||
await setup.renderer.setupTerminal()
|
||||
const started = Promise.withResolvers<void>()
|
||||
const setTitle = setup.renderer.setTerminalTitle.bind(setup.renderer)
|
||||
setup.renderer.setTerminalTitle = (title) => {
|
||||
if (title === "OpenCode") started.resolve()
|
||||
setTitle(title)
|
||||
}
|
||||
const events = createEventStream()
|
||||
const calls = createFetch(undefined, events)
|
||||
const server = Bun.serve({ port: 0, fetch: (request) => calls.fetch(request) })
|
||||
const originalWrite = process.stdout.write.bind(process.stdout)
|
||||
let stdout = ""
|
||||
process.stdout.write = ((chunk: string | Uint8Array) => {
|
||||
stdout += String(chunk)
|
||||
return true
|
||||
}) as typeof process.stdout.write
|
||||
|
||||
try {
|
||||
const { run } = await import("../src/app")
|
||||
const task = Effect.runPromise(
|
||||
run({
|
||||
app: { name: "test", version: "test", channel: "test" },
|
||||
server: { endpoint: { url: server.url.toString() } },
|
||||
config: { get: async () => ({}), update: async () => ({}) },
|
||||
packages: { prepare: async () => ({ directory: "" }) },
|
||||
terminalHandoff: async () => ({ renderer: setup.renderer, mode: "dark", complete: () => {} }),
|
||||
updater: {
|
||||
remote: false,
|
||||
subscribe: () => () => {},
|
||||
check: async () => ({ type: "installed", version: "2.0.0" }),
|
||||
apply: async () => {},
|
||||
},
|
||||
args: {},
|
||||
log: () => {},
|
||||
}).pipe(Effect.provide(Global.layerWith({ state: state.path })), Effect.provide(FileSystem.layerNoop({}))),
|
||||
)
|
||||
|
||||
await started.promise
|
||||
await setup.waitForFrame((frame) => frame.includes("commands"))
|
||||
await setup.mockInput.typeText("/update")
|
||||
await setup.waitForFrame((frame) => frame.includes("Update OpenCode"))
|
||||
setup.mockInput.pressEnter()
|
||||
await setup.waitForFrame((frame) => frame.includes("Update successful") && frame.includes("Restart"))
|
||||
setup.mockInput.pressEnter()
|
||||
await task
|
||||
|
||||
expect(Bun.stripANSI(stdout)).toContain(" Run opencode to use 2.0.0\n")
|
||||
} finally {
|
||||
process.stdout.write = originalWrite
|
||||
if (!setup.renderer.isDestroyed) setup.renderer.destroy()
|
||||
await server.stop()
|
||||
}
|
||||
})
|
||||
|
||||
test("quitting after a background update tells the user to start OpenCode again", async () => {
|
||||
await using state = await tmpdir()
|
||||
const setup = await createTestRenderer({ width: 80, height: 24 })
|
||||
await setup.renderer.setupTerminal()
|
||||
const started = Promise.withResolvers<void>()
|
||||
const setTitle = setup.renderer.setTerminalTitle.bind(setup.renderer)
|
||||
setup.renderer.setTerminalTitle = (title) => {
|
||||
if (title === "OpenCode") started.resolve()
|
||||
setTitle(title)
|
||||
}
|
||||
const events = createEventStream()
|
||||
const calls = createFetch(undefined, events)
|
||||
const server = Bun.serve({ port: 0, fetch: (request) => calls.fetch(request) })
|
||||
const originalWrite = process.stdout.write.bind(process.stdout)
|
||||
let stdout = ""
|
||||
process.stdout.write = ((chunk: string | Uint8Array) => {
|
||||
stdout += String(chunk)
|
||||
return true
|
||||
}) as typeof process.stdout.write
|
||||
|
||||
try {
|
||||
const { run } = await import("../src/app")
|
||||
const task = Effect.runPromise(
|
||||
run({
|
||||
app: { name: "test", version: "test", channel: "test" },
|
||||
server: { endpoint: { url: server.url.toString() } },
|
||||
config: { get: async () => ({}), update: async () => ({}) },
|
||||
packages: { prepare: async () => ({ directory: "" }) },
|
||||
terminalHandoff: async () => ({ renderer: setup.renderer, mode: "dark", complete: () => {} }),
|
||||
updater: {
|
||||
remote: false,
|
||||
subscribe: (notify) => {
|
||||
notify({ type: "installed", version: "2.0.0" })
|
||||
return () => {}
|
||||
},
|
||||
check: async () => undefined,
|
||||
apply: async () => {},
|
||||
},
|
||||
args: {},
|
||||
log: () => {},
|
||||
}).pipe(Effect.provide(Global.layerWith({ state: state.path })), Effect.provide(FileSystem.layerNoop({}))),
|
||||
)
|
||||
|
||||
await started.promise
|
||||
await setup.waitForFrame((frame) => frame.includes("commands"))
|
||||
await setup.waitForFrame((frame) => frame.includes("restart to use v2.0.0"))
|
||||
await setup.mockInput.typeText("/exit")
|
||||
await setup.waitForFrame((frame) => frame.includes("Exit the app"))
|
||||
setup.mockInput.pressEnter()
|
||||
await task
|
||||
|
||||
expect(Bun.stripANSI(stdout)).toContain(" Run opencode to use 2.0.0\n")
|
||||
} finally {
|
||||
process.stdout.write = originalWrite
|
||||
if (!setup.renderer.isDestroyed) setup.renderer.destroy()
|
||||
await server.stop()
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,8 +1,30 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { sessionEpilogue } from "../../src/util/presentation"
|
||||
import { epilogue } from "../../src/util/presentation"
|
||||
|
||||
const rows = (text: string | undefined) =>
|
||||
Bun.stripANSI(text ?? "")
|
||||
.split("\n")
|
||||
.filter((line) => line.trim() && !/[▀▄█]/.test(line))
|
||||
|
||||
test("prints nothing without a session or update", () => {
|
||||
expect(epilogue({})).toBeUndefined()
|
||||
})
|
||||
|
||||
test("formats session continuation summary", () => {
|
||||
const epilogue = sessionEpilogue({ title: "A session", sessionID: "ses_123" })
|
||||
expect(epilogue).toContain("A session")
|
||||
expect(epilogue).toContain("opencode -s ses_123")
|
||||
expect(rows(epilogue({ session: { title: "A session", sessionID: "ses_123" } }))).toEqual([
|
||||
" Session A session",
|
||||
" Continue opencode -s ses_123",
|
||||
])
|
||||
})
|
||||
|
||||
test("tells the user to start OpenCode again after an update", () => {
|
||||
expect(rows(epilogue({ updatedVersion: "2.0.0" }))).toEqual([" Run opencode to use 2.0.0"])
|
||||
})
|
||||
|
||||
test("keeps the session continuation after an update", () => {
|
||||
expect(rows(epilogue({ session: { title: "A session", sessionID: "ses_123" }, updatedVersion: "2.0.0" }))).toEqual([
|
||||
" Run opencode to use 2.0.0",
|
||||
" Session A session",
|
||||
" Continue opencode -s ses_123",
|
||||
])
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user