opencode/packages/tui/src/plugin/api.tsx
2026-11-11 14:43:23 +00:00

486 lines
16 KiB
TypeScript

import { PluginContextProvider } from "@opencode/plugin/tui"
import {
children,
createMemo,
createRoot,
createUniqueId,
ErrorBoundary,
getOwner,
onCleanup,
onMount,
runWithOwner,
untrack,
type JSX,
type ParentProps,
} from "solid-js"
import type {
Context,
Dialog,
DialogSelectOptions,
Page,
SlotClaim,
SlotMap,
SlotPath,
Toast,
} from "@opencode/plugin/tui/context"
import type { Placement, PlacementKind } from "./structure"
import { infoStringToFiletype, type MarkdownCodeBlockRenderer } from "@opentui/core"
import { useRenderer } from "@opentui/solid"
import { useClient } from "../context/client"
import { useData } from "../context/data"
import { Keymap } from "../context/keymap"
import { useRoute } from "../context/route"
import { useTuiApp, useTuiPaths } from "../context/runtime"
import { useLocation } from "../context/location"
import { useThemes } from "../context/theme"
import { DialogAlert } from "../ui/dialog-alert"
import { DialogConfirm } from "../ui/dialog-confirm"
import { DialogPrompt } from "../ui/dialog-prompt"
import { DialogSelect } from "../ui/dialog-select"
import { useDialog } from "../ui/dialog"
import { useToast } from "../ui/toast"
import { useAttention } from "../context/attention"
import { useStorage } from "../context/storage"
import { useSessionTabs } from "../context/session-tabs"
import { useOptionalPanel } from "../context/panel"
import { useLocal } from "../context/local"
import { errorMessage } from "../util/error"
import { abbreviateHome } from "../util/path-format"
export type Dispose = () => Promise<void>
// Slot inputs erased to their union: the registry stores one render shape
// regardless of which path a claim targets.
export type SlotRender = (input: SlotMap[SlotPath]) => JSX.Element
// A registered claim as stored by the plugin provider's registry.
export type RegisteredSlot = {
readonly placement: Placement
readonly render: SlotRender
}
const placements = ["prepend", "append", "before", "after", "replace"] as const satisfies readonly PlacementKind[]
// The provider's registration store, narrowed to what a plugin context needs:
// route/slot registration lands there, but ordering and lifecycle stay owned
// by the provider.
export type Registry = {
has(kind: "routes" | "slots" | "markdown", name: string): boolean
set(kind: "routes", name: string, page: Page): void
set(kind: "slots", name: string, claim: RegisteredSlot): void
set(kind: "markdown", name: string, render: MarkdownCodeBlockRenderer): void
remove(kind: "routes" | "slots" | "markdown", name: string): void
active(): boolean
}
// The host services a plugin context adapts. Collected once by the provider
// (hooks must run during component setup) and shared by every activation.
export function usePluginHost() {
return {
owner: getOwner(),
renderer: useRenderer(),
client: useClient(),
data: useData(),
route: useRoute(),
keymap: Keymap.use(),
shortcuts: Keymap.useShortcuts(),
keymapState: Keymap.useState(),
app: useTuiApp(),
paths: useTuiPaths(),
location: useLocation(),
themes: useThemes(),
dialog: useDialog(),
toast: useToast(),
attention: useAttention(),
storage: useStorage(),
sessionTabs: useSessionTabs(),
panel: useOptionalPanel(),
local: useLocal(),
}
}
// Build the API surface handed to one plugin activation: host services
// adapted to the plugin contract, with everything registered through it
// unwinding via `owned` when the activation is disposed.
export function createPluginContext(input: {
host: ReturnType<typeof usePluginHost>
id: string
options: Readonly<Record<string, any>> | undefined
owned: Dispose[]
registry: Registry
}): Context {
const host = input.host
input.owned.push(async () => host.panel?.release(input.id))
let context: Context
let claims = 0
// Every dialog and registered render is wrapped so plugin components can
// reach their own context through usePlugin().
const provide = (render: () => JSX.Element) => (
<PluginContextProvider value={context}>{render()}</PluginContextProvider>
)
const dialogApi = createDialogApi(host.dialog, (render) => (
<PluginBoundary id={input.id} where="dialog" onCrash={() => host.dialog.clear()}>
{provide(render)}
</PluginBoundary>
))
const toastApi: Toast = {
show(options) {
const toast = {
title: options.title,
message: options.message,
variant: options.variant ?? "info",
duration: options.duration,
}
const sessionID = options.sessionID
if (sessionID === undefined) {
host.toast.show(toast)
return
}
const route = host.route.data
if (route.type === "session" && host.data.session.root(route.sessionID) === host.data.session.root(sessionID)) {
host.toast.show(toast)
return
}
host.toast.show({
...toast,
title: toast.title ?? host.data.session.get(sessionID)?.title,
action: { label: "Open", run: () => host.route.navigate({ type: "session", sessionID }) },
})
},
}
// Unregistering after deactivation is a no-op: deactivate already resets
// the registration's routes and slots wholesale.
const registration = (kind: "routes" | "slots" | "markdown", name: string) => {
let registered = true
const unregister = () => {
if (!registered) return
registered = false
if (!input.registry.active()) return
input.registry.remove(kind, name)
}
input.owned.push(async () => unregister())
return unregister
}
let cleanups: Set<() => void> | undefined = new Set()
input.owned.push(async () => {
const active = cleanups
cleanups = undefined
active?.forEach((dispose) => dispose())
})
context = {
options: input.options ?? {},
get location() {
return host.location.current
},
app: { version: host.app.version, channel: host.app.channel },
renderer: host.renderer,
client: host.client.api,
data: host.data,
attention: host.attention,
get theme() {
return host.themes.current
},
get themeMode() {
return host.themes.mode()
},
markdown: {
registerCodeBlockRenderer(language, render) {
const name = infoStringToFiletype(language)
if (!name) throw new Error("Markdown code-block language is required")
if (input.registry.has("markdown", name)) {
throw new Error(`Markdown code-block renderer already registered: ${name}`)
}
input.registry.set("markdown", name, render)
return registration("markdown", name)
},
},
keymap: {
layer(factory) {
const active = cleanups
if (!active) return
// Validate outside Solid, whose error routing would bypass the caller.
Keymap.validateCommands(untrack(factory).commands)
const caller = getOwner()
createRoot((dispose) => {
active.add(dispose)
onCleanup(() => active.delete(dispose))
if (caller) runWithOwner(caller, () => onCleanup(dispose))
Keymap.createLayer(factory)
}, caller ?? host.owner)
},
dispatch: host.keymap.dispatch,
shortcuts: host.shortcuts.list,
commands: host.keymapState.commands,
pending: host.keymapState.pending,
active: host.keymapState.active,
mode: host.keymap.mode,
},
storage: {
store: (key, options) => host.storage.store(`plugin.${input.id}.${key}`, options),
memory: (key, options) => host.storage.memory(`plugin.${input.id}.${key}`, options),
},
ui: {
dialog: dialogApi,
toast: toastApi,
format: {
path: (value) => abbreviateHome(value, host.paths.home),
},
router: {
register(page) {
if (input.registry.has("routes", page.name)) throw new Error(`Route already registered: ${page.name}`)
input.registry.set("routes", page.name, {
...page,
render: (data) => provide(() => page.render(data)),
})
return registration("routes", page.name)
},
navigate(destination) {
if (destination.type === "plugin") {
host.route.navigate({ ...destination, id: "id" in destination ? destination.id : input.id })
return
}
host.route.navigate(destination)
},
current() {
return host.route.data
},
},
panel: {
open(name, options) {
if (!host.panel || !input.registry.active()) return false
const route = host.route.data
if (route.type !== "session") return false
host.panel.open({ plugin: input.id, name, sessionID: route.sessionID }, options?.presentation)
return true
},
close: () => host.panel?.release(input.id),
current() {
const current = host.panel?.current()
if (current?.plugin !== input.id) return
return { name: current.name, sessionID: current.sessionID }
},
},
tabs: {
enabled: host.sessionTabs.enabled,
list: () =>
host.sessionTabs.tabs().map((tab) => {
const status = host.sessionTabs.status(tab.sessionID)
return {
...tab,
active: host.sessionTabs.current() === tab.sessionID,
...status,
attention: Boolean(status.attention),
}
}),
open(sessionID) {
if (!host.sessionTabs.enabled()) return false
host.sessionTabs.open(sessionID)
return true
},
focus(sessionID) {
if (!host.sessionTabs.enabled()) return false
host.sessionTabs.select(sessionID)
return true
},
move(sessionID, index) {
if (!host.sessionTabs.enabled()) return false
const target = host.data.session.root(sessionID)
if (!host.sessionTabs.tabs().some((tab) => tab.sessionID === target)) return false
host.sessionTabs.move(target, index)
return true
},
close(sessionID) {
if (!host.sessionTabs.enabled()) return false
const target = sessionID ?? host.sessionTabs.current()
if (!target || !host.sessionTabs.tabs().some((tab) => tab.sessionID === target)) return false
host.sessionTabs.close(target)
return true
},
},
model: {
current() {
const selection = host.local.model.selection()
if (!selection) return
return { providerID: selection.providerID, modelID: selection.modelID, variant: selection.variant }
},
variant: {
list: () => host.local.model.variant.list(),
set(variant) {
if (!host.local.model.selection()) return false
if (variant !== undefined && !host.local.model.variant.list().includes(variant)) return false
host.local.model.variant.set(variant)
return true
},
},
},
slot(value: SlotClaim) {
// Keys are counter-suffixed so one plugin may claim several places;
// order within the plugin is registration order.
const key = `slot#${claims++}`
// Exactly one placement kind, enforced at runtime for untyped plugins.
const kinds = placements.filter((item) => value[item] !== undefined)
if (kinds.length !== 1) throw new Error("Slot claim requires exactly one placement key")
const kind = kinds[0]
input.registry.set("slots", key, {
placement: { kind, target: value[kind] as string },
// The registration map erases the path-specific input type.
render: (slotInput) => provide(() => (value.render as SlotRender)(slotInput)),
})
return registration("slots", key)
},
},
}
return context
}
// A dialog promise must settle exactly once even when confirm and close
// callbacks both fire.
function settle<T>(resolve: (value: T) => void) {
let settled = false
return (value: T) => {
if (settled) return
settled = true
resolve(value)
}
}
export function createDialogApi(
dialog: ReturnType<typeof useDialog>,
provide: (render: () => JSX.Element) => JSX.Element,
) {
const api: Dialog = {
show(render, onClose) {
dialog.replace(() => provide(render), onClose)
},
set(options) {
dialog.setSize(options.size ?? "medium")
dialog.setCentered(options.centered ?? false)
},
clear() {
dialog.clear()
},
alert(options) {
return new Promise<void>((resolve) => {
const done = settle(resolve)
api.show(() => <DialogAlert title={options.title} message={options.message} onConfirm={done} />, done)
})
},
confirm(options) {
return new Promise<boolean | undefined>((resolve) => {
const done = settle(resolve)
api.show(
() => (
<DialogConfirm
title={options.title}
message={options.message}
label={options.label}
onConfirm={() => done(true)}
onCancel={() => done(false)}
/>
),
() => done(undefined),
)
})
},
prompt(options) {
return new Promise<string | undefined>((resolve) => {
const done = settle(resolve)
api.show(
() => (
<DialogPrompt
title={options.title}
description={options.description ? () => <text>{options.description}</text> : undefined}
placeholder={options.placeholder}
value={options.value}
onConfirm={(value) => {
done(value)
api.clear()
}}
/>
),
() => done(undefined),
)
})
},
select<Value>(options: DialogSelectOptions<Value>) {
return new Promise<Value | undefined>((resolve) => {
const done = settle<Value | undefined>(resolve)
const search = options.search
const id = createUniqueId()
api.show(
() => (
<DialogSelect<Value>
title={options.title}
placeholder={options.placeholder}
options={options.options.map((option) => ({ ...option }))}
current={options.current}
search={
search &&
((query) =>
search(
query,
options.options.filter((option) => !option.disabled),
))
}
actions={options.actions?.map((action, index) => {
const base = {
command: `plugin.dialog.select.${id}.${index}`,
title: action.title,
side: action.side,
bind: action.bind,
}
if (action.selection === "none")
return { ...base, selection: action.selection, onTrigger: action.onTrigger }
return { ...base, onTrigger: (option) => action.onTrigger(option.value) }
})}
onSelect={(option) => {
done(option.value)
api.clear()
}}
/>
),
() => done(undefined),
)
})
},
}
return api
}
// Contain render-time plugin crashes: a throwing slot, route or dialog must not
// take down the app or the other plugins. The crash surfaces as one error toast.
export function PluginBoundary(props: ParentProps<{ id: string; where: string; onCrash?: () => void }>) {
const toast = useToast()
return (
<ErrorBoundary
fallback={(error) => {
// One toast per crash: onMount is untracked, so prop updates while
// the boundary is latched cannot re-toast.
onMount(() => {
toast.show({
variant: "error",
title: "Plugin",
message: `${props.id} crashed in ${props.where}: ${errorMessage(error)}`,
})
props.onCrash?.()
})
return null
}}
>
<PluginOutput>{props.children}</PluginOutput>
</ErrorBoundary>
)
}
// Bare text is only valid under <text>. Left to the host's insert, it would
// throw outside the boundary and crash the app, so reject it here instead.
function PluginOutput(props: ParentProps) {
const output = children(() => props.children)
const nodes = createMemo(() => {
const resolved = output.toArray()
if (resolved.some((node) => typeof node === "string" || typeof node === "number"))
throw new Error("rendered bare text; wrap it in <text>")
return resolved
})
return <>{nodes()}</>
}