From b03809b2d79d48b7f70d45e9efd9519c4872583d Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 15:58:09 -0500 Subject: [PATCH] =?UTF-8?q?feat(app):=20check=20for=20and=20install=20plug?= =?UTF-8?q?in=20updates=20in=20Settings=20=E2=80=BA=20Extensions=20(#64511?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: James Long --- .../components-lab/dialog-story-runtime.tsx | 32 ++++- packages/app/src/runtime/i18n/en.ts | 5 + .../src/runtime/plugin/extension-plugins.tsx | 61 ++++++++- .../app/src/settings/providers/extensions.tsx | 112 ++++++++++++++-- .../providers/plugin-update-machine.test.ts | 121 +++++++++++++++++ .../providers/plugin-update-machine.ts | 123 ++++++++++++++++++ .../src/settings/shell.standalone.stories.tsx | 8 +- 7 files changed, 445 insertions(+), 17 deletions(-) create mode 100644 packages/app/src/settings/providers/plugin-update-machine.test.ts create mode 100644 packages/app/src/settings/providers/plugin-update-machine.ts diff --git a/packages/app/src/components-lab/dialog-story-runtime.tsx b/packages/app/src/components-lab/dialog-story-runtime.tsx index 23b0a41d06e..8ad8d06cc8a 100644 --- a/packages/app/src/components-lab/dialog-story-runtime.tsx +++ b/packages/app/src/components-lab/dialog-story-runtime.tsx @@ -1,4 +1,4 @@ -import type { IntegrationInfo } from "@opencode/client/promise" +import type { IntegrationInfo, PluginInfo } from "@opencode/client/promise" import { QueryClient, QueryClientProvider } from "@tanstack/solid-query" import type { ParentProps } from "solid-js" import { LocalProvider } from "@/providers/models/selection" @@ -503,6 +503,28 @@ type DialogStoryRuntimeOptions = { const REDIRECT_PASTE = ["openai", "gitlab", "snowflake-cortex"] +/** Package plugins for the Plugins tab: two with a newer version, so its update controls show. */ +const storyPlugins: PluginInfo[] = [ + { + id: "wakatime", + source: { type: "package", target: "opencode-wakatime@latest", version: "1.2.0", outdated: true }, + features: { server: true }, + state: { status: "active" }, + }, + { + id: "notifier", + source: { type: "package", target: "opencode-notifier@latest", version: "0.4.1", outdated: true }, + features: { server: true, tui: true }, + state: { status: "active" }, + }, + { + id: "review", + source: { type: "package", target: "@acme/opencode-review@latest", version: "2.0.3" }, + features: { server: true }, + state: { status: "active" }, + }, +] + export function createStoryFetch(overrides: IntegrationInfo[] = []): typeof fetch { const integrations = [ ...overrides, @@ -715,6 +737,14 @@ export function createStoryFetch(overrides: IntegrationInfo[] = []): typeof fetc if (pathname === "/api/session/active") return jsonResponse({ data: {} }) + if (pathname === "/api/plugin") return jsonResponse({ location, data: storyPlugins, ready: true }) + + if (pathname === "/api/plugin/check") return jsonResponse({ location, data: storyPlugins }) + + // Long enough to see Updating; the list keeps its flags, as a server whose check has not caught up would. + if (pathname === "/api/plugin/update") + return new Promise((resolve) => setTimeout(() => resolve(noContentResponse()), 1500)) + if (pathname === "/api/config" || pathname === "/api/config/shell") return jsonResponse([]) if (pathname === "/api/mcp/resource") return jsonResponse({ location, data: { resources: [], templates: [] } }) diff --git a/packages/app/src/runtime/i18n/en.ts b/packages/app/src/runtime/i18n/en.ts index ab271237129..09217244b77 100644 --- a/packages/app/src/runtime/i18n/en.ts +++ b/packages/app/src/runtime/i18n/en.ts @@ -927,6 +927,11 @@ export const dict = { "sidebar.view.markAllRead": "Mark all as read", "sidebar.view.noMatch": "No other sessions match", "settings.plugins.reload": "Reload", + "settings.plugins.check": "Check for updates", + "settings.plugins.checking": "Checking for updates", + "settings.plugins.update": "Update", + "settings.plugins.updating": "Updating", + "settings.plugins.update.label": "Update {{name}}", "settings.plugins.status.loading": "Loading", "settings.plugins.status.failed": "Failed", "settings.plugins.error.notFound": "Plugin not found", diff --git a/packages/app/src/runtime/plugin/extension-plugins.tsx b/packages/app/src/runtime/plugin/extension-plugins.tsx index 27cdb45f726..c8b58897bdf 100644 --- a/packages/app/src/runtime/plugin/extension-plugins.tsx +++ b/packages/app/src/runtime/plugin/extension-plugins.tsx @@ -10,16 +10,28 @@ import { useLanguage } from "@/runtime/i18n/language" import { usePlatform } from "@/runtime/platform/platform" import { SettingsList } from "@/settings/list" import { showToast } from "@/shell/notifications/toast" +import type { PluginUpdateView } from "@/settings/providers/plugin-update-machine" import "@/settings/settings.css" import { builtins } from "./builtins" import { usePluginHost } from "./host" import { useReservedPlugins } from "./root" +/** Update controls for the listed plugins' packages; without them the rows offer no update. */ +export type ExtensionPluginUpdates = { + readonly view: (source: Extract) => PluginUpdateView + readonly update: (target: string) => void +} + /** * The plugins a server lists, for the Plugins tab of Extensions. Only the desktop app's own server (`own`) runs its * plugins' GUIs in this window, so only its rows add the window's controls; other servers and the web list names. + * Any server's package plugins can be updated. */ -export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: boolean }) { +export function ExtensionPlugins(props: { + plugins: readonly PluginInfo[] + own: boolean + updates?: ExtensionPluginUpdates +}) { const listed = () => props.plugins.filter((plugin) => plugin.source.type !== "builtin") return ( @@ -34,13 +46,14 @@ export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: b {pluginLabel(plugin)} + )} } > - + ) } @@ -50,7 +63,7 @@ export function ExtensionPlugins(props: { plugins: readonly PluginInfo[]; own: b * switch that turns off only the desktop UI, Reload, status and errors, and rows for plugins main lists that the * server list does not name. Built-ins always run and stay hidden unless they failed, which only this list reports. */ -function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { +function OwnServerPlugins(props: { plugins: readonly PluginInfo[]; updates?: ExtensionPluginUpdates }) { const language = useLanguage() const host = usePluginHost() const bridge = usePlatform().plugins @@ -136,7 +149,7 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { run(bridge?.manager.reload(id)) } - const row = (input: { id: string | undefined; name: string; builtin?: boolean }) => { + const row = (input: { id: string | undefined; name: string; builtin?: boolean; plugin?: PluginInfo }) => { const state = () => (input.builtin ? "loaded" : kind(input.id)) const part = input.builtin ? "builtin" : "config" const live = () => (state() === "loaded" ? input.id : undefined) @@ -184,9 +197,15 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { + + {(plugin) => } + {(id) => (
+ + {(plugin) => } + @@ -206,7 +225,7 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { return ( <> - {(plugin) => row({ id: plugin.id, name: pluginLabel(plugin) })} + {(plugin) => row({ id: plugin.id, name: pluginLabel(plugin), plugin })} {(id) => row({ id, name: id })} {(id) => row({ id, name: id, builtin: true })} @@ -217,6 +236,38 @@ function OwnServerPlugins(props: { plugins: readonly PluginInfo[] }) { ) } +/** Update for a package plugin with a newer version, or the update in progress; nothing otherwise. */ +function PluginUpdateButton(props: { plugin: PluginInfo; updates?: ExtensionPluginUpdates }) { + const language = useLanguage() + + const view = createMemo(() => { + const source = props.plugin.source + + if (source.type !== "package" || !props.updates) return undefined + + const current = props.updates.view(source) + + return current.outdated || current.updating ? { ...current, target: source.target } : undefined + }) + + return ( + + {(current) => ( + + )} + + ) +} + /** A plugin's error: its first line, the message, with the rest (such as a stack trace) behind a toggle. */ function PluginError(props: { error: string }) { const language = useLanguage() diff --git a/packages/app/src/settings/providers/extensions.tsx b/packages/app/src/settings/providers/extensions.tsx index 56533f7f082..d57e39944ce 100644 --- a/packages/app/src/settings/providers/extensions.tsx +++ b/packages/app/src/settings/providers/extensions.tsx @@ -1,5 +1,16 @@ import type { McpServer } from "@opencode/client/promise" -import { Component, For, Show, createEffect, createMemo, onCleanup, type JSX } from "solid-js" +import { + Component, + For, + Show, + createEffect, + createMemo, + createSignal, + on, + onCleanup, + untrack, + type JSX, +} from "solid-js" import { useDialog } from "@opencode/ui/context/dialog" import { Icon } from "@opencode/ui/icon" import { Button } from "@opencode/ui/button" @@ -10,6 +21,8 @@ import { useLanguage } from "@/runtime/i18n/language" import { useData } from "@/runtime/server/current" import { useServerSDK } from "@/runtime/server/client" import { ServerConnection } from "@/runtime/server/registry" +import { formatServerError } from "@/runtime/server/errors" +import { showToast } from "@/shell/notifications/toast" import { useMcpController } from "@/providers/connect/mcp" import { mcpRowView } from "@/providers/connect/mcp-machine" import { DialogMcp } from "@/providers/connect/mcp-dialog" @@ -20,6 +33,14 @@ import { SettingsList } from "@/settings/list" import { createExtensionLoad, ExtensionLoadFailed } from "@/settings/extension-load" import type { SettingsView } from "@/settings/surface" import { ConsoleName, ConsoleReplaced, createConsoleSources } from "./console-link" +import { + createPluginUpdateState, + pluginUpdateChecking, + pluginUpdateView, + transitionPluginUpdate, + type PluginUpdateEffect, + type PluginUpdateEvent, +} from "./plugin-update-machine" import "@/settings/settings.css" import "@/shell/search-list.css" @@ -72,11 +93,9 @@ export const SettingsExtensions: Component<{ task: (_, signal) => serverSdk.api.plugin.list(undefined, { signal }).then((result) => result.data), }) - onCleanup( - serverSdk.event.on("plugin.updated", (event) => { - if (!event.location) plugins.invalidate() - }), - ) + // Plugin registries are per Location, so every `plugin.updated` names one; the default Location's names its resolved + // directory rather than none, and a filter on a missing location never matched. Listing again is cheap and coalesced. + onCleanup(serverSdk.event.on("plugin.updated", () => plugins.invalidate())) const pluginList = createMemo(() => { const current = plugins.view() @@ -84,6 +103,66 @@ export const SettingsExtensions: Component<{ return current.status === "ready" ? current.value : undefined }) + const [updateState, setUpdateState] = createSignal(createPluginUpdateState()) + + const dispatchUpdate = (event: PluginUpdateEvent) => { + const previous = untrack(updateState) + const result = transitionPluginUpdate(previous, event) + + if (result.state !== previous) setUpdateState(() => result.state) + result.effects.forEach(runUpdate) + } + + const runUpdate = (effect: PluginUpdateEffect) => { + if (effect.type === "refresh") return plugins.invalidate() + + if (effect.type === "failed") + return showToast({ + title: language.t("common.requestFailed"), + description: effect.message, + }) + + if (effect.type === "check") { + const attempt = effect.attempt + + return void serverSdk.api.plugin.check().then( + (result) => + dispatchUpdate({ + type: "check.succeeded", + attempt, + outdated: Object.fromEntries( + result.data.flatMap((plugin) => + plugin.source.type === "package" ? [[plugin.source.target, plugin.source.outdated === true]] : [], + ), + ), + }), + (error) => dispatchUpdate({ type: "check.failed", attempt, message: formatServerError(error, language.t) }), + ) + } + + const id = effect.id + + void serverSdk.api.plugin.update({ targets: [effect.target] }).then( + () => dispatchUpdate({ type: "update.succeeded", id }), + (error) => dispatchUpdate({ type: "update.failed", id, message: formatServerError(error, language.t) }), + ) + } + + createEffect( + on( + () => serverSdk.scope, + () => dispatchUpdate({ type: "server.changed" }), + { defer: true }, + ), + ) + createEffect(on(pluginList, (list) => list && dispatchUpdate({ type: "list.loaded" }), { defer: true })) + onCleanup(() => dispatchUpdate({ type: "disposed" })) + + const pluginUpdates = { + view: (source: Parameters[1]) => pluginUpdateView(updateState(), source), + update: (target: string) => dispatchUpdate({ type: "update.requested", target }), + } + // The desktop window runs the GUIs of its own server's plugins, so only that server's rows control them. const own = () => !!platform.plugins && ServerConnection.builtin(serverSdk.server) @@ -247,13 +326,26 @@ export const SettingsExtensions: Component<{ {language.t("settings.extensions.availableAll")} - - {language.t("settings.extensions.manageConfig")} - +
+ + {language.t("settings.extensions.manageConfig")} + + +
{/* The desktop's own server also lists the plugins main runs, so its rows stay while the list loads. */} - +

{language.t("common.loading")}

diff --git a/packages/app/src/settings/providers/plugin-update-machine.test.ts b/packages/app/src/settings/providers/plugin-update-machine.test.ts new file mode 100644 index 00000000000..0cbb1f3886c --- /dev/null +++ b/packages/app/src/settings/providers/plugin-update-machine.test.ts @@ -0,0 +1,121 @@ +import { expect, test } from "bun:test" +import { + createPluginUpdateState, + pluginUpdateChecking, + pluginUpdateView, + transitionPluginUpdate, + type PluginUpdateEffect, + type PluginUpdateEvent, +} from "./plugin-update-machine" + +/** Drives the machine as the Extensions page does, recording every effect it asks for. */ +function harness() { + const machine = { state: createPluginUpdateState() } + const effects: PluginUpdateEffect[] = [] + + const dispatch = (event: PluginUpdateEvent) => { + const next = transitionPluginUpdate(machine.state, event) + + machine.state = next.state + effects.push(...next.effects) + + return next + } + + return { machine, effects, dispatch } +} + +const source = { target: "opencode-plugin-a@latest" } as const + +test("a check claims itself at once, ignores a second request, and its result overrides the listed flags", () => { + const run = harness() + + run.dispatch({ type: "check.requested" }) + const checking = run.machine.state + run.dispatch({ type: "check.requested" }) + + expect(run.machine.state).toBe(checking) + expect(pluginUpdateChecking(run.machine.state)).toBe(true) + expect(run.effects).toEqual([{ type: "check", attempt: 0 }]) + + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + + expect(pluginUpdateChecking(run.machine.state)).toBe(false) + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: true, updating: false }) + + run.dispatch({ type: "list.loaded" }) + + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) + expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ + outdated: true, + updating: false, + }) +}) + +test("a failed check ends idle and reports the error", () => { + const run = harness() + const message = "offline" + + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "check.failed", attempt: 0, message }) + + expect(pluginUpdateChecking(run.machine.state)).toBe(false) + expect(run.effects.at(-1)).toEqual({ type: "failed", message }) +}) + +test("an update runs once per package, then refreshes the list and hides the button", () => { + const run = harness() + const outdated = { ...source, outdated: true } as const + + run.dispatch({ type: "update.requested", target: source.target }) + const updating = run.machine.state + run.dispatch({ type: "update.requested", target: source.target }) + + expect(run.machine.state).toBe(updating) + expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: true, updating: true }) + + run.dispatch({ type: "update.succeeded", id: 0 }) + + expect(run.effects).toEqual([{ type: "update", id: 0, target: source.target }, { type: "refresh" }]) + // The list still carries the old flag until it loads again. + expect(pluginUpdateView(run.machine.state, outdated)).toEqual({ outdated: false, updating: false }) +}) + +test("a failed update keeps the package outdated and reports the error", () => { + const run = harness() + const message = "npm failed" + + run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "update.failed", id: 0, message }) + + expect(pluginUpdateView(run.machine.state, { ...source, outdated: true })).toEqual({ + outdated: true, + updating: false, + }) + expect(run.effects.at(-1)).toEqual({ type: "failed", message }) +}) + +test("results from before a server change or after dispose are ignored", () => { + const run = harness() + + run.dispatch({ type: "check.requested" }) + run.dispatch({ type: "update.requested", target: source.target }) + run.dispatch({ type: "server.changed" }) + const changed = run.machine.state + + run.dispatch({ type: "check.succeeded", attempt: 0, outdated: { [source.target]: true } }) + run.dispatch({ type: "update.failed", id: 1, message: "late" }) + + expect(run.machine.state).toBe(changed) + expect(pluginUpdateView(run.machine.state, source)).toEqual({ outdated: false, updating: false }) + + run.dispatch({ type: "disposed" }) + const disposed = run.machine.state + run.dispatch({ type: "check.requested" }) + + expect(run.machine.state).toBe(disposed) + expect(run.effects).toEqual([ + { type: "check", attempt: 0 }, + { type: "update", id: 1, target: source.target }, + ]) +}) diff --git a/packages/app/src/settings/providers/plugin-update-machine.ts b/packages/app/src/settings/providers/plugin-update-machine.ts new file mode 100644 index 00000000000..078cc994dee --- /dev/null +++ b/packages/app/src/settings/providers/plugin-update-machine.ts @@ -0,0 +1,123 @@ +/** One package update on its way; `id` tells it apart from an update of the same package on an earlier server. */ +export type RunningPluginUpdate = { readonly id: number; readonly target: string } + +/** + * Checking for and installing plugin package updates from Settings › Extensions. `outdated` holds what the latest + * check or update learned per package, which is fresher than the listed plugins until the list loads again. + */ +export type PluginUpdateState = + | { + readonly status: "idle" + readonly nextId: number + readonly outdated: Readonly> + readonly updating: readonly RunningPluginUpdate[] + } + | { + readonly status: "checking" + readonly attempt: number + readonly nextId: number + readonly outdated: Readonly> + readonly updating: readonly RunningPluginUpdate[] + } + | { readonly status: "disposed" } + +export type PluginUpdateEvent = + | { readonly type: "check.requested" } + /** `outdated` maps every package the check covered to whether it has a newer version. */ + | { readonly type: "check.succeeded"; readonly attempt: number; readonly outdated: Readonly> } + | { readonly type: "check.failed"; readonly attempt: number; readonly message: string } + | { readonly type: "update.requested"; readonly target: string } + | { readonly type: "update.succeeded"; readonly id: number } + | { readonly type: "update.failed"; readonly id: number; readonly message: string } + /** The plugin list loaded again, so its own flags are current. */ + | { readonly type: "list.loaded" } + /** Another server: forget what this one's checks and updates learned. */ + | { readonly type: "server.changed" } + | { readonly type: "disposed" } + +export type PluginUpdateEffect = + | { readonly type: "check"; readonly attempt: number } + | { readonly type: "update"; readonly id: number; readonly target: string } + | { readonly type: "refresh" } + | { readonly type: "failed"; readonly message: string } + +export type PluginUpdateTransition = { + readonly state: PluginUpdateState + readonly effects: readonly PluginUpdateEffect[] +} + +const none: readonly PluginUpdateEffect[] = [] + +const step = (state: PluginUpdateState, effects = none): PluginUpdateTransition => ({ state, effects }) + +export function createPluginUpdateState(): PluginUpdateState { + return { status: "idle", nextId: 0, outdated: {}, updating: [] } +} + +export function transitionPluginUpdate(state: PluginUpdateState, event: PluginUpdateEvent): PluginUpdateTransition { + if (state.status === "disposed") return step(state) + + if (event.type === "disposed") return step({ status: "disposed" }) + + if (event.type === "server.changed") return step({ status: "idle", nextId: state.nextId, outdated: {}, updating: [] }) + + if (event.type === "list.loaded") { + if (Object.keys(state.outdated).length === 0) return step(state) + + return step({ ...state, outdated: {} }) + } + + if (event.type === "check.requested") { + if (state.status === "checking") return step(state) + + const attempt = state.nextId + + return step({ ...state, status: "checking", attempt, nextId: attempt + 1 }, [{ type: "check", attempt }]) + } + + if (event.type === "check.succeeded" || event.type === "check.failed") { + if (state.status !== "checking" || state.attempt !== event.attempt) return step(state) + + const idle = { status: "idle", nextId: state.nextId, outdated: state.outdated, updating: state.updating } as const + + if (event.type === "check.failed") return step(idle, [{ type: "failed", message: event.message }]) + + return step({ ...idle, outdated: event.outdated }) + } + + if (event.type === "update.requested") { + if (state.updating.some((item) => item.target === event.target)) return step(state) + + const id = state.nextId + + return step({ ...state, nextId: id + 1, updating: [...state.updating, { id, target: event.target }] }, [ + { type: "update", id, target: event.target }, + ]) + } + + const running = state.updating.find((item) => item.id === event.id) + + if (!running) return step(state) + + const updating = state.updating.filter((item) => item !== running) + + if (event.type === "update.failed") return step({ ...state, updating }, [{ type: "failed", message: event.message }]) + + return step({ ...state, updating, outdated: { ...state.outdated, [running.target]: false } }, [{ type: "refresh" }]) +} + +export type PluginUpdateView = { readonly outdated: boolean; readonly updating: boolean } + +/** What the Plugins tab shows for a package, given the flags the listed plugin carries. */ +export function pluginUpdateView( + state: PluginUpdateState, + source: { readonly target: string; readonly outdated?: true; readonly updating?: true }, +): PluginUpdateView { + if (state.status === "disposed") return { outdated: false, updating: false } + + const updating = source.updating === true || state.updating.some((item) => item.target === source.target) + + return { outdated: state.outdated[source.target] ?? source.outdated === true, updating } +} + +export const pluginUpdateChecking = (state: PluginUpdateState) => state.status === "checking" diff --git a/packages/app/src/settings/shell.standalone.stories.tsx b/packages/app/src/settings/shell.standalone.stories.tsx index c0ca4d9c26e..e8142fb76c1 100644 --- a/packages/app/src/settings/shell.standalone.stories.tsx +++ b/packages/app/src/settings/shell.standalone.stories.tsx @@ -5,11 +5,13 @@ import { SettingsScreen } from "@/settings/shell" import { SettingsSurfaceProvider, type SettingsRootTab } from "@/settings/surface" import { LayoutProvider } from "@/shell/state/layout" -function SettingsStory(props: { tab: SettingsRootTab; locked?: boolean }) { +function SettingsStory(props: { tab: SettingsRootTab; subtab?: string; locked?: boolean }) { const history = createMemoryHistory() const query = new URLSearchParams({ tab: props.tab }) + if (props.subtab) query.set("subtab", props.subtab) + if (props.locked) query.set("server", ServerConnection.key(STORY_LOCKED_SERVER)) history.set({ value: `/settings?${query}`, replace: true, scroll: false }) @@ -51,4 +53,8 @@ export const Models = { render: () => , } +export const Plugins = { + render: () => , +} + export const UnavailableServer = { render: () => }