import { useDialog } from "@opencode/ui/context/dialog" import { showToast } from "@opencode/ui/toast" import { createContext, createEffect, createSignal, For, onCleanup, Show, untrack, useContext } from "solid-js" import type { ParentProps } from "solid-js" import { HomeProjectsView, type HomeProjectsViewProps } from "@/home/projects/view" import { PluginHostProvider, usePluginHost, type HostApiFactories } from "@/runtime/plugin/host" import { Contribution, PluginStyles } from "@/runtime/plugin/render" import { PluginServersProvider, usePluginServers, useServerAddItems } from "@/runtime/plugin/servers" import { useLanguage } from "@/runtime/i18n/language" import { PlatformProvider, usePlatform, type Platform } from "@/runtime/platform/platform" import { ServerConnection } from "@/runtime/server/registry" import type { ServerCollectionController } from "@/servers/registry/controller" import { PluginServerRow } from "@/servers/registry/plugin-row" import { builtins } from "../../../gui-plugins/src/renderer" import type { IpcClient, Layout } from "../../../plugin/src/gui/index" import type { Ssh, SshConfig, SshHttp, SshItem, SshStart } from "../../../gui-plugins/src/ssh/contract" import { DialogSsh } from "../../../gui-plugins/src/ssh/dialog" import { createSshController } from "../../../gui-plugins/src/ssh/state" import { createStoryHostApis } from "../../src/plugin" // Settings rows render inside the app's settings page, which brings these styles. import "@/settings/settings.css" // The app API the SSH dialog stories were written against, before SSH moved into its GUI plugin. The SSH // plugin's renderer runs in the app's plugin host; only its main process and the HostApis it reads are // stood in for. Every piece below renders the renderer's contributions as the app does. export { useLanguage } export type { SshItem } export type SshState = { servers: readonly SshItem[] } export type SshPlatform = { getState(): Promise subscribe(callback: (state: SshState) => void): () => void hosts(): Promise start(input: SshStart): Promise resolve(id: string): Promise respond(id: string, prompt: string, value: string): Promise disconnect(id: string): Promise cancel(id: string): Promise forget(id: string): Promise openConfig(): Promise } type SshIpc = IpcClient<(typeof Ssh)["spec"]> const plugin = builtins.find((definition) => definition.id === "ssh")! const none = new Set() const SshPlatformContext = createContext() const SshIpcContext = createContext() function StoryPlatformProvider(props: ParentProps<{ value: Platform & { sshServers?: SshPlatform } }>) { return ( {props.children} ) } // Stands in for the SSH plugin's main entry: the story's platform fixture answers the Ipc. function createSshMain(platform: SshPlatform): SshIpc { const [state, setState] = createSignal<{ servers: readonly SshItem[]; revision: number }>() const publish = (next: SshState) => setState((current) => ({ servers: next.servers, revision: (current?.revision ?? 0) + 1 })) void platform.getState().then(publish) onCleanup(platform.subscribe(publish)) const revision = () => state()?.revision ?? 0 return { state, on: () => () => {}, // Background restores leave the fixture alone: the stories show saved servers this window has not reconnected, // as they did when the app mounted its restore outside them. start: (input) => (input.background ? Promise.resolve(revision()) : platform.start(input).then(revision)), resolve: (input) => platform.resolve(input.id), // Main answers only the challenge the server is waiting on: a confirmation with "yes", a secret with a value. respond: (input) => { const prompt = state()?.servers.find((item) => item.config.id === input.id)?.prompt if (prompt?.id !== input.prompt || !(prompt.confirm ? input.value === "yes" : input.value)) return Promise.resolve() return platform.respond(input.id, input.prompt, input.value) }, cancel: (input) => platform.cancel(input.id), forget: (input) => platform.forget(input.id), } } export function SshProvider(props: ParentProps) { const platform = useContext(SshPlatformContext) const ipc = platform && createSshMain(platform) const apis = createStoryHostApis(usePlatform().platform) const layout = new Proxy({} as Layout, { get: () => () => { throw new Error("The host layout is unavailable in SSH stories") }, }) return ( [plugin]} disabled={() => none} // The SSH plugin's renderer reads no other HostApi in these stories. apis={{ ...apis, layout: () => layout } as HostApiFactories} // The stories have no app interface to wait for: a dialog shows at once. whenMounted={(run) => { run() return () => {} }} ipc={(token) => (token.id === plugin.id ? ipc : undefined)} > {props.children} ) } // The app renders contributions once its plugins are active. function SshHost(props: ParentProps) { const host = usePluginHost() return ( {props.children} ) } export function useSsh() { const ipc = useContext(SshIpcContext) const servers = usePluginServers() const entry = (id: string) => servers.entry(`ssh:${id}`)?.entry return { get servers() { return ipc?.state()?.servers ?? [] }, pending: (id: string) => entry(id)?.state === "starting", connect: (config: SshConfig) => void entry(config.id)?.connect?.(), } } export function useSshAuthenticate() { return ServerConnection.authenticate } function StoryDialogSsh(props: { config?: SshConfig; connect?: boolean }) { if (props.config) return return } // The story opens this inside a dialog of its own; the SSH plugin's "server.add" menu item opens the real one. function AddDialogSsh() { const dialog = useDialog() const items = useServerAddItems() const state = { opened: false } createEffect(() => { const item = items()[0] if (!item || state.opened) return state.opened = true untrack(() => { dialog.close() item.run("") }) }) return null } // No production entry point opens the dialog with a preset host, so these stories render it with a controller of // their own over the same main process. function PresetDialogSsh(props: { config: SshConfig; connect?: boolean }) { const ipc = useContext(SshIpcContext) const language = useLanguage() const ssh = createSshController({ items: () => ipc?.state()?.servers ?? [], api: () => ipc, // The stand-in main publishes an attempt's state before `start` resolves. refresh: () => Promise.resolve(), error: () => showToast({ variant: "error", title: language.t("common.requestFailed") }), }) return ( {() => } ) } namespace StoryServerConnection { export type Ssh = { type: "ssh" id?: string host: string displayName?: string label?: string http: ServerConnection.HttpBase connecting?: boolean authenticationRequired?: boolean } export const key = (conn: Ssh) => ServerConnection.Key.make(`ssh:${conn.id ?? conn.host}`) } // The home list shows the server the SSH plugin contributes for each story server. function StoryHomeProjectsView( props: Omit & { servers: StoryServerConnection.Ssh[] }, ) { const servers = usePluginServers() return ( props.servers.some((server) => StoryServerConnection.key(server) === ServerConnection.key(conn)), )} /> ) } // The cover the SSH plugin contributes over a session of a server that is not ready. export function SshConnectionPanel(props: { item: SshItem; pending?: boolean; onReconnect: () => void }) { const servers = usePluginServers() return ( {(source) => ( {() => source().entry.cover?.({ tab: "story" })} )} ) } // The settings page's rows for the servers the SSH plugin contributes. export function SshServerSettings(props: { filter: string; id?: string; domain: ServerCollectionController }) { const servers = usePluginServers() const keys = () => servers .entries() .filter( (item) => item.plugin === plugin.id && (!props.id || item.entry.id === props.id) && item.entry.name.toLowerCase().includes(props.filter.toLowerCase()), ) .map((item) => ServerConnection.Key.make(item.key)) return {(key) => } } export { StoryDialogSsh as DialogSsh, StoryHomeProjectsView as HomeProjectsView, StoryPlatformProvider as PlatformProvider, StoryServerConnection as ServerConnection, }