|
| 1 | +/** @jsxImportSource @opentui/solid */ |
| 2 | +import { createSignal, onCleanup } from 'solid-js'; |
| 3 | +import type { TuiPlugin, TuiPluginModule } from '@opencode-ai/plugin/tui'; |
| 4 | +import type fs from 'node:fs'; |
| 5 | +import type path from 'node:path'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Tool names that trigger a state refresh. |
| 9 | + * |
| 10 | + * Covers both usage modes: |
| 11 | + * - opencode-plugin (bare names, no prefix) |
| 12 | + * - MCP server (tools namespaced with "workflows_" prefix) |
| 13 | + */ |
| 14 | +const WORKFLOW_TOOLS = new Set([ |
| 15 | + // opencode-plugin direct tool names |
| 16 | + 'start_development', |
| 17 | + 'proceed_to_phase', |
| 18 | + 'conduct_review', |
| 19 | + 'reset_development', |
| 20 | + 'setup_project_docs', |
| 21 | + // MCP server tool names (workflows_ namespace prefix) |
| 22 | + 'workflows_start_development', |
| 23 | + 'workflows_proceed_to_phase', |
| 24 | + 'workflows_conduct_review', |
| 25 | + 'workflows_reset_development', |
| 26 | + 'workflows_setup_project_docs', |
| 27 | +]); |
| 28 | + |
| 29 | +interface StateJson { |
| 30 | + currentPhase?: string; |
| 31 | + workflowName?: string; |
| 32 | +} |
| 33 | + |
| 34 | +interface MessagePartUpdatedEvent { |
| 35 | + properties?: { |
| 36 | + part?: { |
| 37 | + sessionID?: string; |
| 38 | + type?: string; |
| 39 | + tool?: string; |
| 40 | + }; |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function readLatestState( |
| 45 | + sessionDir: string |
| 46 | +): { phase: string; workflow: string } | null { |
| 47 | + try { |
| 48 | + // require() is intentional: top-level ESM imports of Node built-ins are not |
| 49 | + // supported in the Bun plugin runtime. |
| 50 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 51 | + const fsSync = require('node:fs') as typeof fs; |
| 52 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 53 | + const pathSync = require('node:path') as typeof path; |
| 54 | + const vibeDir = pathSync.join(sessionDir, '.vibe', 'conversations'); |
| 55 | + const dirs = fsSync.readdirSync(vibeDir); |
| 56 | + let latest: { mtime: number; file: string } | null = null; |
| 57 | + for (const dir of dirs) { |
| 58 | + const file = pathSync.join(vibeDir, dir, 'state.json'); |
| 59 | + try { |
| 60 | + const stat = fsSync.statSync(file); |
| 61 | + if (!latest || stat.mtimeMs > latest.mtime) { |
| 62 | + latest = { mtime: stat.mtimeMs, file }; |
| 63 | + } |
| 64 | + } catch { |
| 65 | + // unreadable entry — skip silently |
| 66 | + } |
| 67 | + } |
| 68 | + if (!latest) return null; |
| 69 | + const state = JSON.parse( |
| 70 | + fsSync.readFileSync(latest.file, 'utf8') |
| 71 | + ) as StateJson; |
| 72 | + if (!state.currentPhase && !state.workflowName) return null; |
| 73 | + return { |
| 74 | + phase: state.currentPhase ?? '—', |
| 75 | + workflow: state.workflowName ?? '—', |
| 76 | + }; |
| 77 | + } catch { |
| 78 | + return null; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// eslint-disable-next-line @typescript-eslint/require-await -- TuiPlugin signature requires Promise<void>; plugin body is synchronous |
| 83 | +const tui: TuiPlugin = async api => { |
| 84 | + api.slots.register({ |
| 85 | + order: 5, |
| 86 | + slots: { |
| 87 | + sidebar_content(_ctx, props) { |
| 88 | + const theme = () => api.theme.current; |
| 89 | + const [state, setState] = createSignal<{ |
| 90 | + phase: string; |
| 91 | + workflow: string; |
| 92 | + } | null>(null); |
| 93 | + |
| 94 | + // Read state eagerly on mount so it's visible immediately on reload, |
| 95 | + // not only after the first tool call. |
| 96 | + const dir = api.state.path.directory; |
| 97 | + if (dir) { |
| 98 | + setState(readLatestState(dir)); |
| 99 | + } |
| 100 | + |
| 101 | + const offPart = api.event.on('message.part.updated', e => { |
| 102 | + const ev = e as MessagePartUpdatedEvent; |
| 103 | + const part = ev.properties?.part; |
| 104 | + if (!part) return; |
| 105 | + if (part.sessionID !== props.session_id) return; |
| 106 | + if (part.type !== 'tool') return; |
| 107 | + if (!part.tool || !WORKFLOW_TOOLS.has(part.tool)) return; |
| 108 | + if (!dir) return; |
| 109 | + setState(readLatestState(dir)); |
| 110 | + }); |
| 111 | + onCleanup(offPart); |
| 112 | + |
| 113 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- JSX element typed as `error` by @opentui/solid's JSX types; safe at runtime |
| 114 | + return ( |
| 115 | + <box flexDirection="column" visible={!!state()}> |
| 116 | + <text fg={theme().text}> |
| 117 | + <b>Workflow</b> |
| 118 | + </text> |
| 119 | + <text fg={theme().textMuted}> |
| 120 | + {state()?.workflow}:{' '} |
| 121 | + {/* eslint-disable-next-line solid/style-prop -- `fg` is an OpenTUI-specific style prop, not a standard CSS property */} |
| 122 | + <span style={{ fg: theme().text }}>{state()?.phase}</span> |
| 123 | + </text> |
| 124 | + </box> |
| 125 | + ); |
| 126 | + }, |
| 127 | + }, |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +const plugin: TuiPluginModule & { id: string } = { |
| 132 | + id: 'workflows-phase', |
| 133 | + tui, |
| 134 | +}; |
| 135 | + |
| 136 | +export default plugin; |
0 commit comments