import type { CodeServerConnect, CodeServerDetection, CodeServerServerInfo, CodeServerSharedState, CodeServerStatusResult, } from '../connect' import { computed, onScopeDispose, reactive, ref, shallowRef } from 'vue' import { STATE_KEY } from '../connect' import { useRpc } from './rpc' /** Discrete UI phases, one per launcher story. */ export type CodeServerPhase = 'connecting' | 'not-installed' | 'launch' | 'starting' | 'running' const DETECT = 'devframes:plugin:code-server:detect' const STATUS = 'devframes:plugin:code-server:status' const START = 'devframes:plugin:code-server:start' const STOP = 'devframes:plugin:code-server:stop' // Hub integration, addressed by literal so the plugin keeps no dependency on // `@devframes/hub` and stays inert standalone (the keys/RPC simply never fire). /** Shared-state slot holding the hub's projected dock entries. */ const DOCKS_STATE_KEY = 'devframe:docks' /** Terminals feed dock id (mirrors `@devframes/plugin-terminals`'s `PLUGIN_ID`). */ const TERMINALS_DOCK_ID = 'devframes_plugin_terminals' /** Hub RPC that steers the host shell's focused dock. */ const DOCKS_ACTIVATE = 'hub:docks:activate' /** * Live code-server state, driven off the plugin's shared state plus the * secret-bearing connect info fetched from the `status` RPC. Exposes the * derived {@link CodeServerPhase} and the launch / stop / re-check intents the * launcher surfaces. */ export function useCodeServer() { const rpc = useRpc() const detection = reactive({ checked: false, installed: false, bin: 'code-server', backend: 'code-server', mode: 'local', }) const server = reactive({ status: 'stopped' }) const connect = shallowRef(undefined) const busy = ref(false) /** Whether a hub with the terminals plugin dock is hosting this panel. */ const terminalsDockAvailable = ref(false) /** * The editor's launch can be watched live in the terminals dock, but only * when the process runs as a hub terminal session (`terminalSessionId`) and * that dock is actually mounted. */ const canViewInTerminal = computed(() => terminalsDockAvailable.value && !!server.terminalSessionId) const phase = computed(() => { if (server.status === 'running') // Hold on the progress state until the connect handoff arrives, or the // editor would load before its cookie/token and show a login page. return connect.value ? 'running' : 'starting' if (server.status === 'starting' || (busy.value && server.status !== 'error')) return 'starting' if (!detection.checked) return 'connecting' if (!detection.installed) return 'not-installed' return 'launch' }) function applyResult(result: CodeServerStatusResult | undefined): void { if (!result) return Object.assign(detection, result.detection) Object.assign(server, result.server) if (result.connect) connect.value = result.connect } type LooseCall = (method: string, ...args: unknown[]) => Promise async function call(method: string, ...args: unknown[]): Promise { const client = rpc.value if (!client) return undefined try { return await (client.call as LooseCall)(method, ...args) as T } catch (error) { server.status = 'error' server.error = error instanceof Error ? error.message : String(error) return undefined } } async function launch(): Promise { if (busy.value) return busy.value = true try { applyResult(await call(START, {})) } finally { busy.value = false } } async function stop(): Promise { if (busy.value) return busy.value = true try { applyResult(await call(STOP)) connect.value = undefined } finally { busy.value = false } } /** * Jump the host shell to this editor's session in the terminals dock. Inert * unless a hub with the terminals dock is hosting the panel and the process * runs as a hub terminal session. */ async function viewInTerminal(): Promise { const sessionId = server.terminalSessionId if (!sessionId) return await call(DOCKS_ACTIVATE, { dockId: TERMINALS_DOCK_ID, params: { sessionId }, }) } async function recheck(): Promise { if (busy.value) return busy.value = true try { await call(DETECT) } finally { busy.value = false } } /** Fetch the initial status and subscribe to live shared-state updates. */ async function bootstrap(): Promise { applyResult(await call(STATUS)) const client = rpc.value if (!client) return const state = await client.sharedState.get(STATE_KEY, { initialValue: { detection, server } as CodeServerSharedState, }) const current = state.value() as CodeServerSharedState Object.assign(detection, current.detection) Object.assign(server, current.server) const off = state.on('updated', (full: CodeServerSharedState) => { Object.assign(detection, full.detection) Object.assign(server, full.server) // Shared state never carries connect material; fetch it once running. if (server.status === 'running' && !connect.value) { void call(STATUS).then((result) => { if (result?.connect) connect.value = result.connect }) } if (server.status !== 'running') connect.value = undefined }) onScopeDispose(() => off?.()) await watchTerminalsDock(client) } /** * Track whether the hosting hub has the terminals plugin dock mounted, so the * launcher only offers "view in terminal" when the jump can land somewhere. * Reads the hub's `devframe:docks` slot; outside a hub it never resolves and * the action stays hidden. */ async function watchTerminalsDock(client: NonNullable['value']>): Promise { interface DockEntry { id?: string } const hasTerminals = (docks: readonly DockEntry[] | undefined): boolean => Array.isArray(docks) && docks.some(d => d?.id === TERMINALS_DOCK_ID) try { const slot = await client.sharedState.get(DOCKS_STATE_KEY, { initialValue: [] }) as { value: () => readonly DockEntry[] on: (event: string, cb: (v: readonly DockEntry[]) => void) => (() => void) | void } terminalsDockAvailable.value = hasTerminals(slot.value()) const off = slot.on('updated', (docks) => { terminalsDockAvailable.value = hasTerminals(docks) }) onScopeDispose(() => off?.()) } catch { // No hub or shared state; the terminals jump simply stays unavailable. } } return { detection, server, connect, busy, phase, canViewInTerminal, launch, stop, recheck, viewInTerminal, bootstrap, } }