tui.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. import type {
  2. AgentPart,
  3. OpencodeClient,
  4. Event,
  5. FilePart,
  6. LspStatus,
  7. McpStatus,
  8. Todo,
  9. Message,
  10. Part,
  11. Provider,
  12. PermissionRequest,
  13. QuestionRequest,
  14. SessionStatus,
  15. TextPart,
  16. Config as SdkConfig,
  17. } from "@opencode-ai/sdk/v2"
  18. import type { CliRenderer, KeyEvent, RGBA, Renderable, SlotMode } from "@opentui/core"
  19. import type { Binding, Keymap } from "@opentui/keymap"
  20. import {
  21. resolveBindingSections as resolveKeymapBindingSections,
  22. type BindingSectionsConfig,
  23. type KeySequenceFormatPart,
  24. type SequenceBindingLike,
  25. } from "@opentui/keymap/extras"
  26. import type { JSX, SolidPlugin } from "@opentui/solid"
  27. import type { Config as PluginConfig, PluginOptions } from "./index.js"
  28. export type { CliRenderer, KeyEvent, Renderable, SlotMode } from "@opentui/core"
  29. export { stringifyKeySequence, stringifyKeyStroke } from "@opentui/keymap"
  30. export type { Binding, KeyLike, KeySequencePart, KeyStringifyInput, StringifyOptions } from "@opentui/keymap"
  31. export { formatCommandBindings, formatKeySequence } from "@opentui/keymap/extras"
  32. export type {
  33. BindingSectionsConfig,
  34. BindingValue,
  35. FormatCommandBindingsOptions,
  36. FormatKeySequenceOptions,
  37. KeySequenceFormatPart,
  38. SequenceBindingLike,
  39. } from "@opentui/keymap/extras"
  40. export function resolveBindingSections<Section extends string>(
  41. config: BindingSectionsConfig<Renderable, KeyEvent> | undefined,
  42. options: { sections: readonly Section[] },
  43. ) {
  44. return resolveKeymapBindingSections<Renderable, KeyEvent, BindingSectionsConfig<Renderable, KeyEvent>, Section>(
  45. config ?? {},
  46. options,
  47. )
  48. }
  49. export type TuiRouteCurrent =
  50. | {
  51. name: "home"
  52. }
  53. | {
  54. name: "session"
  55. params: {
  56. sessionID: string
  57. prompt?: unknown
  58. }
  59. }
  60. | {
  61. name: string
  62. params?: Record<string, unknown>
  63. }
  64. export type TuiRouteDefinition = {
  65. name: string
  66. render: (input: { params?: Record<string, unknown> }) => JSX.Element
  67. }
  68. export type TuiKeys = {
  69. formatSequence: (parts: readonly KeySequenceFormatPart[] | undefined) => string
  70. formatBindings: (bindings: readonly SequenceBindingLike[] | undefined) => string | undefined
  71. }
  72. export type TuiKeymap = Keymap<Renderable, KeyEvent>
  73. export type TuiDialogProps = {
  74. size?: "medium" | "large" | "xlarge"
  75. onClose: () => void
  76. children?: JSX.Element
  77. }
  78. export type TuiDialogStack = {
  79. replace: (render: () => JSX.Element, onClose?: () => void) => void
  80. clear: () => void
  81. setSize: (size: "medium" | "large" | "xlarge") => void
  82. readonly size: "medium" | "large" | "xlarge"
  83. readonly depth: number
  84. readonly open: boolean
  85. }
  86. export type TuiDialogAlertProps = {
  87. title: string
  88. message: string
  89. onConfirm?: () => void
  90. }
  91. export type TuiDialogConfirmProps = {
  92. title: string
  93. message: string
  94. onConfirm?: () => void
  95. onCancel?: () => void
  96. }
  97. export type TuiDialogPromptProps = {
  98. title: string
  99. description?: () => JSX.Element
  100. placeholder?: string
  101. value?: string
  102. busy?: boolean
  103. busyText?: string
  104. onConfirm?: (value: string) => void
  105. onCancel?: () => void
  106. }
  107. export type TuiDialogSelectOption<Value = unknown> = {
  108. title: string
  109. value: Value
  110. description?: string
  111. footer?: JSX.Element | string
  112. category?: string
  113. disabled?: boolean
  114. onSelect?: () => void
  115. }
  116. export type TuiDialogSelectProps<Value = unknown> = {
  117. title: string
  118. placeholder?: string
  119. options: TuiDialogSelectOption<Value>[]
  120. flat?: boolean
  121. onMove?: (option: TuiDialogSelectOption<Value>) => void
  122. onFilter?: (query: string) => void
  123. onSelect?: (option: TuiDialogSelectOption<Value>) => void
  124. skipFilter?: boolean
  125. current?: Value
  126. }
  127. export type TuiPromptInfo = {
  128. input: string
  129. mode?: "normal" | "shell"
  130. parts: (
  131. | Omit<FilePart, "id" | "messageID" | "sessionID">
  132. | Omit<AgentPart, "id" | "messageID" | "sessionID">
  133. | (Omit<TextPart, "id" | "messageID" | "sessionID"> & {
  134. source?: {
  135. text: {
  136. start: number
  137. end: number
  138. value: string
  139. }
  140. }
  141. })
  142. )[]
  143. }
  144. export type TuiPromptRef = {
  145. focused: boolean
  146. current: TuiPromptInfo
  147. set(prompt: TuiPromptInfo): void
  148. reset(): void
  149. blur(): void
  150. focus(): void
  151. submit(): void
  152. }
  153. export type TuiPromptProps = {
  154. sessionID?: string
  155. workspaceID?: string
  156. visible?: boolean
  157. disabled?: boolean
  158. onSubmit?: () => void
  159. ref?: (ref: TuiPromptRef | undefined) => void
  160. hint?: JSX.Element
  161. right?: JSX.Element
  162. showPlaceholder?: boolean
  163. placeholders?: {
  164. normal?: string[]
  165. shell?: string[]
  166. }
  167. }
  168. export type TuiToast = {
  169. variant?: "info" | "success" | "warning" | "error"
  170. title?: string
  171. message: string
  172. duration?: number
  173. }
  174. export type TuiThemeCurrent = {
  175. readonly primary: RGBA
  176. readonly secondary: RGBA
  177. readonly accent: RGBA
  178. readonly error: RGBA
  179. readonly warning: RGBA
  180. readonly success: RGBA
  181. readonly info: RGBA
  182. readonly text: RGBA
  183. readonly textMuted: RGBA
  184. readonly selectedListItemText: RGBA
  185. readonly background: RGBA
  186. readonly backgroundPanel: RGBA
  187. readonly backgroundElement: RGBA
  188. readonly backgroundMenu: RGBA
  189. readonly border: RGBA
  190. readonly borderActive: RGBA
  191. readonly borderSubtle: RGBA
  192. readonly diffAdded: RGBA
  193. readonly diffRemoved: RGBA
  194. readonly diffContext: RGBA
  195. readonly diffHunkHeader: RGBA
  196. readonly diffHighlightAdded: RGBA
  197. readonly diffHighlightRemoved: RGBA
  198. readonly diffAddedBg: RGBA
  199. readonly diffRemovedBg: RGBA
  200. readonly diffContextBg: RGBA
  201. readonly diffLineNumber: RGBA
  202. readonly diffAddedLineNumberBg: RGBA
  203. readonly diffRemovedLineNumberBg: RGBA
  204. readonly markdownText: RGBA
  205. readonly markdownHeading: RGBA
  206. readonly markdownLink: RGBA
  207. readonly markdownLinkText: RGBA
  208. readonly markdownCode: RGBA
  209. readonly markdownBlockQuote: RGBA
  210. readonly markdownEmph: RGBA
  211. readonly markdownStrong: RGBA
  212. readonly markdownHorizontalRule: RGBA
  213. readonly markdownListItem: RGBA
  214. readonly markdownListEnumeration: RGBA
  215. readonly markdownImage: RGBA
  216. readonly markdownImageText: RGBA
  217. readonly markdownCodeBlock: RGBA
  218. readonly syntaxComment: RGBA
  219. readonly syntaxKeyword: RGBA
  220. readonly syntaxFunction: RGBA
  221. readonly syntaxVariable: RGBA
  222. readonly syntaxString: RGBA
  223. readonly syntaxNumber: RGBA
  224. readonly syntaxType: RGBA
  225. readonly syntaxOperator: RGBA
  226. readonly syntaxPunctuation: RGBA
  227. readonly thinkingOpacity: number
  228. }
  229. export type TuiTheme = {
  230. readonly current: TuiThemeCurrent
  231. readonly selected: string
  232. has: (name: string) => boolean
  233. set: (name: string) => boolean
  234. install: (jsonPath: string) => Promise<void>
  235. mode: () => "dark" | "light"
  236. readonly ready: boolean
  237. }
  238. export type TuiKV = {
  239. get: <Value = unknown>(key: string, fallback?: Value) => Value
  240. set: (key: string, value: unknown) => void
  241. readonly ready: boolean
  242. }
  243. export type TuiState = {
  244. readonly ready: boolean
  245. readonly config: SdkConfig
  246. readonly provider: ReadonlyArray<Provider>
  247. readonly path: {
  248. state: string
  249. config: string
  250. worktree: string
  251. directory: string
  252. }
  253. readonly vcs: { branch?: string } | undefined
  254. session: {
  255. count: () => number
  256. diff: (sessionID: string) => ReadonlyArray<TuiSidebarFileItem>
  257. todo: (sessionID: string) => ReadonlyArray<TuiSidebarTodoItem>
  258. messages: (sessionID: string) => ReadonlyArray<Message>
  259. status: (sessionID: string) => SessionStatus | undefined
  260. permission: (sessionID: string) => ReadonlyArray<PermissionRequest>
  261. question: (sessionID: string) => ReadonlyArray<QuestionRequest>
  262. }
  263. part: (messageID: string) => ReadonlyArray<Part>
  264. lsp: () => ReadonlyArray<TuiSidebarLspItem>
  265. mcp: () => ReadonlyArray<TuiSidebarMcpItem>
  266. }
  267. type TuiConfigView = Pick<PluginConfig, "$schema" | "theme" | "keybinds" | "plugin"> &
  268. NonNullable<PluginConfig["tui"]> & {
  269. plugin_enabled?: Record<string, boolean>
  270. keymap: {
  271. leader: string
  272. leader_timeout: number
  273. sections: Record<string, ReadonlyArray<Binding<Renderable, KeyEvent>>>
  274. get: (section: string, cmd: string) => ReadonlyArray<Binding<Renderable, KeyEvent>> | undefined
  275. pick: (section: string, commands: readonly string[]) => Binding<Renderable, KeyEvent>[]
  276. omit: (section: string, commands: readonly string[]) => Binding<Renderable, KeyEvent>[]
  277. }
  278. }
  279. export type TuiApp = {
  280. readonly version: string
  281. }
  282. type Frozen<Value> = Value extends (...args: never[]) => unknown
  283. ? Value
  284. : Value extends ReadonlyArray<infer Item>
  285. ? ReadonlyArray<Frozen<Item>>
  286. : Value extends object
  287. ? { readonly [Key in keyof Value]: Frozen<Value[Key]> }
  288. : Value
  289. export type TuiSidebarMcpItem = {
  290. name: string
  291. status: McpStatus["status"]
  292. error?: string
  293. }
  294. export type TuiSidebarLspItem = Pick<LspStatus, "id" | "root" | "status">
  295. export type TuiSidebarTodoItem = Pick<Todo, "content" | "status">
  296. export type TuiSidebarFileItem = {
  297. file: string
  298. additions: number
  299. deletions: number
  300. }
  301. export type TuiHostSlotMap = {
  302. app: {}
  303. app_bottom: {}
  304. home_logo: {}
  305. home_prompt: {
  306. workspace_id?: string
  307. ref?: (ref: TuiPromptRef | undefined) => void
  308. }
  309. home_prompt_right: {
  310. workspace_id?: string
  311. }
  312. session_prompt: {
  313. session_id: string
  314. visible?: boolean
  315. disabled?: boolean
  316. on_submit?: () => void
  317. ref?: (ref: TuiPromptRef | undefined) => void
  318. }
  319. session_prompt_right: {
  320. session_id: string
  321. }
  322. home_bottom: {}
  323. home_footer: {}
  324. sidebar_title: {
  325. session_id: string
  326. title: string
  327. share_url?: string
  328. }
  329. sidebar_content: {
  330. session_id: string
  331. }
  332. sidebar_footer: {
  333. session_id: string
  334. }
  335. }
  336. export type TuiSlotMap<Slots extends Record<string, object> = {}> = TuiHostSlotMap & Slots
  337. type TuiSlotShape<Name extends string, Slots extends Record<string, object>> = Name extends keyof TuiHostSlotMap
  338. ? TuiHostSlotMap[Name]
  339. : Name extends keyof Slots
  340. ? Slots[Name]
  341. : Record<string, unknown>
  342. export type TuiSlotProps<Name extends string = string, Slots extends Record<string, object> = {}> = {
  343. name: Name
  344. mode?: SlotMode
  345. children?: JSX.Element
  346. } & TuiSlotShape<Name, Slots>
  347. export type TuiSlotContext = {
  348. theme: TuiTheme
  349. }
  350. type SlotCore<Slots extends Record<string, object> = {}> = SolidPlugin<TuiSlotMap<Slots>, TuiSlotContext>
  351. export type TuiSlotPlugin<Slots extends Record<string, object> = {}> = Omit<SlotCore<Slots>, "id"> & {
  352. id?: never
  353. }
  354. export type TuiSlots = {
  355. register: {
  356. (plugin: TuiSlotPlugin): string
  357. <Slots extends Record<string, object>>(plugin: TuiSlotPlugin<Slots>): string
  358. }
  359. }
  360. export type TuiEventBus = {
  361. on: <Type extends Event["type"]>(type: Type, handler: (event: Extract<Event, { type: Type }>) => void) => () => void
  362. }
  363. export type TuiDispose = () => void | Promise<void>
  364. export type TuiLifecycle = {
  365. readonly signal: AbortSignal
  366. onDispose: (fn: TuiDispose) => () => void
  367. }
  368. export type TuiPluginState = "first" | "updated" | "same"
  369. export type TuiPluginEntry = {
  370. id: string
  371. source: "file" | "npm" | "internal"
  372. spec: string
  373. target: string
  374. requested?: string
  375. version?: string
  376. modified?: number
  377. first_time: number
  378. last_time: number
  379. time_changed: number
  380. load_count: number
  381. fingerprint: string
  382. }
  383. export type TuiPluginMeta = TuiPluginEntry & {
  384. state: TuiPluginState
  385. }
  386. export type TuiPluginStatus = {
  387. id: string
  388. source: TuiPluginEntry["source"]
  389. spec: string
  390. target: string
  391. enabled: boolean
  392. active: boolean
  393. }
  394. export type TuiPluginInstallOptions = {
  395. global?: boolean
  396. }
  397. export type TuiPluginInstallResult =
  398. | {
  399. ok: true
  400. dir: string
  401. tui: boolean
  402. }
  403. | {
  404. ok: false
  405. message: string
  406. missing?: boolean
  407. }
  408. export type TuiWorkspace = {
  409. current: () => string | undefined
  410. set: (workspaceID?: string) => void
  411. }
  412. export type TuiPluginApi = {
  413. app: TuiApp
  414. keys: TuiKeys
  415. keymap: TuiKeymap
  416. route: {
  417. register: (routes: TuiRouteDefinition[]) => () => void
  418. navigate: (name: string, params?: Record<string, unknown>) => void
  419. readonly current: TuiRouteCurrent
  420. }
  421. ui: {
  422. Dialog: (props: TuiDialogProps) => JSX.Element
  423. DialogAlert: (props: TuiDialogAlertProps) => JSX.Element
  424. DialogConfirm: (props: TuiDialogConfirmProps) => JSX.Element
  425. DialogPrompt: (props: TuiDialogPromptProps) => JSX.Element
  426. DialogSelect: <Value = unknown>(props: TuiDialogSelectProps<Value>) => JSX.Element
  427. Slot: <Name extends string>(props: TuiSlotProps<Name>) => JSX.Element | null
  428. Prompt: (props: TuiPromptProps) => JSX.Element
  429. toast: (input: TuiToast) => void
  430. dialog: TuiDialogStack
  431. }
  432. readonly tuiConfig: Frozen<TuiConfigView>
  433. kv: TuiKV
  434. state: TuiState
  435. theme: TuiTheme
  436. client: OpencodeClient
  437. event: TuiEventBus
  438. renderer: CliRenderer
  439. slots: TuiSlots
  440. plugins: {
  441. list: () => ReadonlyArray<TuiPluginStatus>
  442. activate: (id: string) => Promise<boolean>
  443. deactivate: (id: string) => Promise<boolean>
  444. add: (spec: string) => Promise<boolean>
  445. install: (spec: string, options?: TuiPluginInstallOptions) => Promise<TuiPluginInstallResult>
  446. }
  447. lifecycle: TuiLifecycle
  448. }
  449. export type TuiPlugin = (api: TuiPluginApi, options: PluginOptions | undefined, meta: TuiPluginMeta) => Promise<void>
  450. export type TuiPluginModule = {
  451. id?: string
  452. tui: TuiPlugin
  453. server?: never
  454. }