mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
refactor(tui): remove the import cycle in plugin (#62884)
This commit is contained in:
@@ -1,5 +1,18 @@
|
||||
import { PluginContextProvider } from "@opencode/plugin/tui"
|
||||
import { createRoot, createUniqueId, getOwner, onCleanup, runWithOwner, untrack, type JSX } from "solid-js"
|
||||
import {
|
||||
children,
|
||||
createMemo,
|
||||
createRoot,
|
||||
createUniqueId,
|
||||
ErrorBoundary,
|
||||
getOwner,
|
||||
onCleanup,
|
||||
onMount,
|
||||
runWithOwner,
|
||||
untrack,
|
||||
type JSX,
|
||||
type ParentProps,
|
||||
} from "solid-js"
|
||||
import type {
|
||||
Context,
|
||||
Dialog,
|
||||
@@ -25,13 +38,13 @@ import { DialogConfirm } from "../ui/dialog-confirm"
|
||||
import { DialogPrompt } from "../ui/dialog-prompt"
|
||||
import { DialogSelect } from "../ui/dialog-select"
|
||||
import { useDialog } from "../ui/dialog"
|
||||
import { PluginBoundary } from "./render"
|
||||
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>
|
||||
@@ -433,3 +446,41 @@ export function createDialogApi(
|
||||
}
|
||||
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()}</>
|
||||
}
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import {
|
||||
children,
|
||||
createComponent,
|
||||
createContext,
|
||||
createMemo,
|
||||
ErrorBoundary,
|
||||
For,
|
||||
mergeProps,
|
||||
onCleanup,
|
||||
onMount,
|
||||
Show,
|
||||
useContext,
|
||||
type JSX,
|
||||
@@ -15,51 +12,11 @@ import {
|
||||
} from "solid-js"
|
||||
import { isShallowEqual } from "remeda"
|
||||
import type { SlotMap, SlotPath } from "@opencode/plugin/tui/context"
|
||||
import type { SlotRender } from "./api"
|
||||
import { PluginBoundary, type SlotRender } from "./api"
|
||||
import { contains, emptySlotted, type Claim } from "./structure"
|
||||
import { useRoute } from "../context/route"
|
||||
import { useToast } from "../ui/toast"
|
||||
import { errorMessage } from "../util/error"
|
||||
import { usePlugin } from "./context"
|
||||
|
||||
// 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()}</>
|
||||
}
|
||||
|
||||
export function PluginRoute(props: { readonly fallback: (id: string, name: string) => JSX.Element }) {
|
||||
const plugins = usePlugin()
|
||||
const route = useRoute()
|
||||
|
||||
Reference in New Issue
Block a user