import { realpath } from "node:fs/promises"; import { startConnector, type ConnectorNotification } from "../connector.js"; import { WorkspaceRegistry } from "../chat/workspace.js"; import { announceDirectory, liveSessionIds, resolvePresencePath } from "../directory-presence.js"; import { FileFolderApprovalStore, resolveFolderApprovalsPath } from "../folder-approvals.js"; import { OpenCodeChatAdapter } from "./chat-adapter.js"; import type { OpenCodeClient } from "./client.js"; import { OpenCodeMcpReader } from "./mcp.js"; import { openRemoteDialog, remoteAccessStores } from "./remote-dialog.js"; import { OpenCodeSessionReader } from "./sessions.js"; import { registerStatusChip } from "./status-chip.js"; /// The dialog surface `/remote` draws on. OpenCode's dialogs are promise-based, so the flow is a /// loop over awaited choices rather than a tree of rendered callbacks. export interface DialogUI { readonly dialog: { select(options: { title: string; placeholder?: string; options: readonly { title: string; value: Value; description?: string; footer?: string }[] }): Promise confirm(options: { title: string; message: string }): Promise alert(options: { title: string; message: string }): Promise } } /// The part of OpenCode's TUI plugin context the connector uses. OpenCode gives a server plugin /// only a narrow, domain-scoped API (no session listing, no message history), while the TUI plugin /// receives the complete client, so the connector lives on the TUI side and is active while a /// TUI is attached to the instance. Structural, so the plugin needs no OpenCode package to build. export interface TuiContext { readonly options: Readonly> readonly location: { readonly directory: string } | undefined readonly client: OpenCodeClient readonly data: { readonly location: { default(): { readonly directory: string } } readonly session?: { root(sessionID: string): string } readonly listen?: (handler: (event: { details: { type: string; data?: Record } }) => void) => () => void } /// Registers commands, including slash and palette entries. Optional because a host that does /// not offer it must still get a working connector, only without `/remote`. readonly keymap?: { layer(input: () => { mode?: string; commands?: readonly KeymapCommand[] }): void } readonly ui: DialogUI & { readonly router?: { current(): { type: string; sessionID?: string } } readonly tabs?: { list(): readonly { sessionID: string }[] } readonly toast: { show(options: { title?: string; message: string; variant?: "info" | "success" | "warning" | "error"; duration?: number }): void } /// Claims a place in the host's slot tree: the always-mounted `app` slot (the one context a /// keymap layer can be registered from) and the footer status slots the Remote chip lives in. readonly slot?: (claim: { append: "app" | "home.footer.status" | "prompt.footer.status"; render: () => unknown }) => () => void } /// Theme tokens, as the host's renderer accepts them. Only the ones the Remote chip uses. readonly theme?: { readonly text: { readonly base: unknown; readonly muted: unknown readonly feedback: { readonly success: { readonly base: unknown } } } } } export interface KeymapCommand { readonly id?: string readonly title?: string readonly description?: string readonly group?: string readonly palette?: true readonly slash?: { readonly name: string; readonly aliases?: string[] } readonly run: () => false | Promise | undefined } /// Starts the connector for one TUI and returns its cleanup. The TUI has no plugin log /// sink, so diagnostics below error level are dropped and errors surface once as a toast; the /// connector's logging rules already forbid sensitive content in every message it emits. export async function setupConnector(context: TuiContext): Promise<() => Promise> { // The TUI's default is its launch directory, while context.location follows the selected // session. Keep the launch project's handles stable even if setup runs after a session move. const directory = context.data.location.default().directory; const options = { ...context.options }; const reported = new Set(); // Registered before the connector starts, and independently of whether it does: `/remote` // carries the local kill switch, which must stay reachable precisely when the relay, the // service or the pairing is broken. const dialogs = new AbortController(); // A UI convenience must never be able to take the connector down with it: registration threw // `Keymap.Provider is missing` on 2.0.14 when it was attempted outside a rendered component, // and that exception aborted setup before the connector ever started. let releaseCommand: () => void = () => undefined; try { releaseCommand = registerRemoteCommand(context, dialogs.signal); } catch (error) { context.ui.toast.show({ title: "Open Remote Code", variant: "warning", duration: 10_000, message: `The /remote command is unavailable: ${error instanceof Error ? error.message : String(error)}` }); } // Same rule for the indicator: it reflects the connector, it must never be able to stop it. let releaseChip: () => void = () => undefined; try { releaseChip = await registerStatusChip(context); } catch { // Without the chip the connector and /remote still work; there is nothing to tell the user. } // Local tab announcements help recover moved sessions in already approved // folders. They are not a source of directory authorization. const canonical = await realpath(directory); const localSessionIds = (): string[] => { const route = context.ui.router?.current(); const ids = [...context.ui.tabs?.list().map((tab) => tab.sessionID) ?? [], ...(route?.type === "session" && route.sessionID ? [route.sessionID] : [])]; return [...new Set(ids.slice(0, 64).map((id) => context.data.session?.root(id) ?? id))]; }; const presencePath = resolvePresencePath(); const approvals = new FileFolderApprovalStore(resolveFolderApprovalsPath()); const registry = new WorkspaceRegistry(undefined, [], () => approvals.list()); const chats = new OpenCodeChatAdapter(context.client, directory, [], registry, async (workspace) => { return [...new Set([...(workspace.path === canonical ? localSessionIds() : []), ...await liveSessionIds(presencePath, workspace.path)])]; }); const presence = announceDirectory({ presencePath, directory, sessionIds: localSessionIds }); const stopEvents = context.data.listen?.(({ details }) => { chats.observeEvent(details); }); const connector = await startConnector({ options, log: (level, message) => { if (level === "error" && !reported.has(message) && reported.size < 8) { reported.add(message); context.ui.toast.show({ title: "Open Remote Code", message, variant: "error", duration: 10_000 }); } return Promise.resolve(); }, notify: (notification: ConnectorNotification) => { context.ui.toast.show({ title: notification.title, message: notification.message, variant: notification.variant, duration: notification.durationMs }); return Promise.resolve(); }, // Resolved lazily, so a host without dialogs still starts; pairing then cannot be approved // and fails closed rather than completing without consent. confirm: (question) => context.ui.dialog.confirm(question), adapters: () => { return { sessions: new OpenCodeSessionReader(context.client, directory, registry), chats, mcp: new OpenCodeMcpReader(context.client, registry), stream: chats }; }, }); return async () => { dialogs.abort(); releaseCommand(); releaseChip(); stopEvents?.(); await presence.stop(); await connector.dispose?.(); }; } /// Registers `/remote`, returning its release. A keymap layer belongs to the component that /// creates it, and `setup()` is not one -- registering from there fails with /// "Keymap.Provider is missing" (2.0.14). The layer is therefore created inside a render, from a /// claim on the always-mounted `app` slot that draws nothing. function registerRemoteCommand(context: TuiContext, signal: AbortSignal): () => void { const keymap = context.keymap; const slot = context.ui.slot; if (!keymap || !slot) return () => undefined; const stores = remoteAccessStores(); let open = false; // "global", not the default "base" mode: a base-mode layer is unreachable while the prompt has // focus, which is exactly where a slash command has to be offered (verified against 2.0.14 -- // the same command is absent from slash completion without this and present with it). const commands = (): { mode: string; commands: readonly KeymapCommand[] } => ({ mode: "global", commands: [{ id: "opencode-remote.open", title: "Open Remote Code", description: "Manage pairing and revoke remote access", group: "Remote", palette: true, slash: { name: "remote" }, run: async () => { // One dialog at a time: a second invocation while the first is open would revoke against // a status read before the first one acted. if (open) return; open = true; try { // Capture the selected session's current location when the command is // invoked, not the TUI's launch directory or a path supplied by a phone. await openRemoteDialog(context.ui, stores, signal, { directory: context.location?.directory ?? context.data.location.default().directory, store: new FileFolderApprovalStore(resolveFolderApprovalsPath()), }); } catch (error) { context.ui.toast.show({ title: "Open Remote Code", variant: "error", message: `Remote access dialog failed: ${error instanceof Error ? error.message : String(error)}`, duration: 10_000 }); } finally { open = false; } }, }], }); return slot({ append: "app", render: () => { keymap.layer(commands); return null; } }); }