tui.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. Session,
  15. SessionStatus,
  16. TextPart,
  17. Config as SdkConfig,
  18. } from "@opencode-ai/sdk/v2"
  19. import type { CliRenderer, KeyEvent, RGBA, Renderable, SlotMode } from "@opentui/core"
  20. import type { Binding, Keymap } from "@opentui/keymap"
  21. import {
  22. createBindingLookup as createKeymapBindingLookup,
  23. type BindingConfig,
  24. type CreateBindingLookupOptions,
  25. type KeySequenceFormatPart,
  26. type SequenceBindingLike,
  27. } from "@opentui/keymap/extras"
  28. import type { JSX, SolidPlugin } from "@opentui/solid"
  29. import type { Config as PluginConfig, PluginOptions } from "./index.js"
  30. export type { CliRenderer, KeyEvent, Renderable, SlotMode } from "@opentui/core"
  31. export { stringifyKeySequence, stringifyKeyStroke } from "@opentui/keymap"
  32. export type { Binding, KeyLike, KeySequencePart, KeyStringifyInput, StringifyOptions } from "@opentui/keymap"
  33. export { formatCommandBindings, formatKeySequence } from "@opentui/keymap/extras"
  34. export type {
  35. BindingConfig,
  36. BindingLookup,
  37. BindingValue,
  38. CreateBindingLookupOptions,
  39. FormatCommandBindingsOptions,
  40. FormatKeySequenceOptions,
  41. KeySequenceFormatPart,
  42. SequenceBindingLike,
  43. } from "@opentui/keymap/extras"
  44. export function createBindingLookup(
  45. config: BindingConfig<Renderable, KeyEvent> | undefined,
  46. options?: CreateBindingLookupOptions<Renderable, KeyEvent>,
  47. ) {
  48. return createKeymapBindingLookup<Renderable, KeyEvent>(config ?? {}, options)
  49. }
  50. export type TuiRouteCurrent =
  51. | {
  52. name: "home"
  53. }
  54. | {
  55. name: "session"
  56. params: {
  57. sessionID: string
  58. prompt?: unknown
  59. }
  60. }
  61. | {
  62. name: string
  63. params?: Record<string, unknown>
  64. }
  65. export type TuiRouteDefinition = {
  66. name: string
  67. render: (input: { params?: Record<string, unknown> }) => JSX.Element
  68. }
  69. export type TuiKeys = {
  70. formatSequence: (parts: readonly KeySequenceFormatPart[] | undefined) => string
  71. formatBindings: (bindings: readonly SequenceBindingLike[] | undefined) => string | undefined
  72. }
  73. export type TuiKeymap = Keymap<Renderable, KeyEvent>
  74. /**
  75. * Legacy `api.command` shape kept so v1 plugins can initialize. Remove in v2.
  76. *
  77. * @deprecated Use `api.keymap.registerLayer({ commands, bindings })` instead.
  78. */
  79. export type TuiCommand = {
  80. title: string
  81. value: string
  82. description?: string
  83. category?: string
  84. keybind?: string
  85. suggested?: boolean
  86. hidden?: boolean
  87. enabled?: boolean
  88. slash?: {
  89. name: string
  90. aliases?: string[]
  91. }
  92. onSelect?: (dialog?: TuiDialogStack) => void | Promise<void>
  93. }
  94. /**
  95. * Legacy `api.command` API kept so v1 plugins can initialize. Remove in v2.
  96. *
  97. * @deprecated Use `api.keymap.registerLayer`, `api.keymap.dispatchCommand`, and
  98. * `api.keymap.dispatchCommand("command.palette.show")` instead.
  99. */
  100. export type TuiCommandApi = {
  101. /** @deprecated Use `api.keymap.registerLayer({ commands, bindings })` instead. */
  102. register: (cb: () => TuiCommand[]) => () => void
  103. /** @deprecated Use `api.keymap.dispatchCommand(name)` instead. */
  104. trigger: (value: string) => void
  105. /** @deprecated Use `api.keymap.dispatchCommand("command.palette.show")` instead. */
  106. show: () => void
  107. }
  108. export type TuiDialogProps = {
  109. size?: "medium" | "large" | "xlarge"
  110. onClose: () => void
  111. children?: JSX.Element
  112. }
  113. export type TuiDialogStack = {
  114. replace: (render: () => JSX.Element, onClose?: () => void) => void
  115. clear: () => void
  116. setSize: (size: "medium" | "large" | "xlarge") => void
  117. readonly size: "medium" | "large" | "xlarge"
  118. readonly depth: number
  119. readonly open: boolean
  120. }
  121. export type TuiDialogAlertProps = {
  122. title: string
  123. message: string
  124. onConfirm?: () => void
  125. }
  126. export type TuiDialogConfirmProps = {
  127. title: string
  128. message: string
  129. onConfirm?: () => void
  130. onCancel?: () => void
  131. }
  132. export type TuiDialogPromptProps = {
  133. title: string
  134. description?: () => JSX.Element
  135. placeholder?: string
  136. value?: string
  137. busy?: boolean
  138. busyText?: string
  139. onConfirm?: (value: string) => void
  140. onCancel?: () => void
  141. }
  142. export type TuiDialogSelectOption<Value = unknown> = {
  143. title: string
  144. value: Value
  145. description?: string
  146. footer?: JSX.Element | string
  147. category?: string
  148. disabled?: boolean
  149. onSelect?: () => void
  150. }
  151. export type TuiDialogSelectProps<Value = unknown> = {
  152. title: string
  153. placeholder?: string
  154. options: TuiDialogSelectOption<Value>[]
  155. flat?: boolean
  156. onMove?: (option: TuiDialogSelectOption<Value>) => void
  157. onFilter?: (query: string) => void
  158. onSelect?: (option: TuiDialogSelectOption<Value>) => void
  159. skipFilter?: boolean
  160. current?: Value
  161. }
  162. export type TuiPromptInfo = {
  163. input: string
  164. mode?: "normal" | "shell"
  165. parts: (
  166. | Omit<FilePart, "id" | "messageID" | "sessionID">
  167. | Omit<AgentPart, "id" | "messageID" | "sessionID">
  168. | (Omit<TextPart, "id" | "messageID" | "sessionID"> & {
  169. source?: {
  170. text: {
  171. start: number
  172. end: number
  173. value: string
  174. }
  175. }
  176. })
  177. )[]
  178. }
  179. export type TuiPromptRef = {
  180. focused: boolean
  181. current: TuiPromptInfo
  182. set(prompt: TuiPromptInfo): void
  183. reset(): void
  184. blur(): void
  185. focus(): void
  186. submit(): void
  187. }
  188. export type TuiPromptProps = {
  189. sessionID?: string
  190. workspaceID?: string
  191. visible?: boolean
  192. disabled?: boolean
  193. onSubmit?: () => void
  194. ref?: (ref: TuiPromptRef | undefined) => void
  195. hint?: JSX.Element
  196. right?: JSX.Element
  197. showPlaceholder?: boolean
  198. placeholders?: {
  199. normal?: string[]
  200. shell?: string[]
  201. }
  202. }
  203. export type TuiToast = {
  204. variant?: "info" | "success" | "warning" | "error"
  205. title?: string
  206. message: string
  207. duration?: number
  208. }
  209. export type TuiAttentionWhen = "always" | "focused" | "blurred"
  210. export const TuiAttentionSoundNames = ["default", "question", "permission", "error", "done", "subagent_done"] as const
  211. export type TuiAttentionSoundName = (typeof TuiAttentionSoundNames)[number]
  212. export type TuiAttentionSound =
  213. | boolean
  214. | {
  215. name?: TuiAttentionSoundName
  216. volume?: number
  217. when?: TuiAttentionWhen
  218. }
  219. export type TuiAttentionNotification =
  220. | boolean
  221. | {
  222. when?: TuiAttentionWhen
  223. }
  224. export type TuiAttentionSoundPack = {
  225. id: string
  226. name?: string
  227. sounds: Partial<Record<TuiAttentionSoundName, string>>
  228. }
  229. export type TuiAttentionSoundPackInfo = {
  230. id: string
  231. name?: string
  232. active: boolean
  233. builtin: boolean
  234. }
  235. export type TuiAttentionSoundboardActivateOptions = {
  236. persist?: boolean
  237. }
  238. export type TuiAttentionSoundboard = {
  239. registerPack(pack: TuiAttentionSoundPack): () => void
  240. activate(id: string, options?: TuiAttentionSoundboardActivateOptions): boolean
  241. current(): string
  242. list(): ReadonlyArray<TuiAttentionSoundPackInfo>
  243. }
  244. export type TuiAttentionNotifyInput = {
  245. title?: string
  246. message: string
  247. notification?: TuiAttentionNotification
  248. sound?: TuiAttentionSound
  249. }
  250. export type TuiAttentionNotifySkipReason =
  251. | "attention_disabled"
  252. | "empty_message"
  253. | "blurred"
  254. | "focused"
  255. | "focus_unknown"
  256. | "renderer_destroyed"
  257. export type TuiAttentionNotifyResult = {
  258. ok: boolean
  259. notification: boolean
  260. sound: boolean
  261. skipped?: TuiAttentionNotifySkipReason
  262. }
  263. export type TuiAttention = {
  264. notify(input: TuiAttentionNotifyInput): Promise<TuiAttentionNotifyResult>
  265. soundboard: TuiAttentionSoundboard
  266. }
  267. export type TuiThemeCurrent = {
  268. readonly primary: RGBA
  269. readonly secondary: RGBA
  270. readonly accent: RGBA
  271. readonly error: RGBA
  272. readonly warning: RGBA
  273. readonly success: RGBA
  274. readonly info: RGBA
  275. readonly text: RGBA
  276. readonly textMuted: RGBA
  277. readonly selectedListItemText: RGBA
  278. readonly background: RGBA
  279. readonly backgroundPanel: RGBA
  280. readonly backgroundElement: RGBA
  281. readonly backgroundMenu: RGBA
  282. readonly border: RGBA
  283. readonly borderActive: RGBA
  284. readonly borderSubtle: RGBA
  285. readonly diffAdded: RGBA
  286. readonly diffRemoved: RGBA
  287. readonly diffContext: RGBA
  288. readonly diffHunkHeader: RGBA
  289. readonly diffHighlightAdded: RGBA
  290. readonly diffHighlightRemoved: RGBA
  291. readonly diffAddedBg: RGBA
  292. readonly diffRemovedBg: RGBA
  293. readonly diffContextBg: RGBA
  294. readonly diffLineNumber: RGBA
  295. readonly diffAddedLineNumberBg: RGBA
  296. readonly diffRemovedLineNumberBg: RGBA
  297. readonly markdownText: RGBA
  298. readonly markdownHeading: RGBA
  299. readonly markdownLink: RGBA
  300. readonly markdownLinkText: RGBA
  301. readonly markdownCode: RGBA
  302. readonly markdownBlockQuote: RGBA
  303. readonly markdownEmph: RGBA
  304. readonly markdownStrong: RGBA
  305. readonly markdownHorizontalRule: RGBA
  306. readonly markdownListItem: RGBA
  307. readonly markdownListEnumeration: RGBA
  308. readonly markdownImage: RGBA
  309. readonly markdownImageText: RGBA
  310. readonly markdownCodeBlock: RGBA
  311. readonly syntaxComment: RGBA
  312. readonly syntaxKeyword: RGBA
  313. readonly syntaxFunction: RGBA
  314. readonly syntaxVariable: RGBA
  315. readonly syntaxString: RGBA
  316. readonly syntaxNumber: RGBA
  317. readonly syntaxType: RGBA
  318. readonly syntaxOperator: RGBA
  319. readonly syntaxPunctuation: RGBA
  320. readonly thinkingOpacity: number
  321. }
  322. export type TuiTheme = {
  323. readonly current: TuiThemeCurrent
  324. readonly selected: string
  325. has: (name: string) => boolean
  326. set: (name: string) => boolean
  327. install: (jsonPath: string) => Promise<void>
  328. mode: () => "dark" | "light"
  329. readonly ready: boolean
  330. }
  331. export type TuiKV = {
  332. get: <Value = unknown>(key: string, fallback?: Value) => Value
  333. set: (key: string, value: unknown) => void
  334. readonly ready: boolean
  335. }
  336. export type TuiState = {
  337. readonly ready: boolean
  338. readonly config: SdkConfig
  339. readonly provider: ReadonlyArray<Provider>
  340. readonly path: {
  341. state: string
  342. config: string
  343. worktree: string
  344. directory: string
  345. }
  346. readonly vcs: { branch?: string } | undefined
  347. session: {
  348. count: () => number
  349. get: (sessionID: string) => Session | undefined
  350. diff: (sessionID: string) => ReadonlyArray<TuiSidebarFileItem>
  351. todo: (sessionID: string) => ReadonlyArray<TuiSidebarTodoItem>
  352. messages: (sessionID: string) => ReadonlyArray<Message>
  353. status: (sessionID: string) => SessionStatus | undefined
  354. permission: (sessionID: string) => ReadonlyArray<PermissionRequest>
  355. question: (sessionID: string) => ReadonlyArray<QuestionRequest>
  356. }
  357. part: (messageID: string) => ReadonlyArray<Part>
  358. lsp: () => ReadonlyArray<TuiSidebarLspItem>
  359. mcp: () => ReadonlyArray<TuiSidebarMcpItem>
  360. }
  361. type TuiBindingLookupView = {
  362. readonly bindings: ReadonlyArray<Binding<Renderable, KeyEvent>>
  363. get: (command: string) => ReadonlyArray<Binding<Renderable, KeyEvent>>
  364. has: (command: string) => boolean
  365. gather: (name: string, commands: readonly string[]) => ReadonlyArray<Binding<Renderable, KeyEvent>>
  366. pick: (name: string, commands: readonly string[]) => Binding<Renderable, KeyEvent>[]
  367. omit: (name: string, commands: readonly string[]) => Binding<Renderable, KeyEvent>[]
  368. }
  369. type TuiAttentionConfigView = {
  370. enabled: boolean
  371. notifications: boolean
  372. sound: boolean
  373. volume: number
  374. sound_pack: string
  375. sounds: Partial<Record<TuiAttentionSoundName, string>>
  376. }
  377. type TuiConfigView = Pick<PluginConfig, "$schema" | "theme" | "plugin"> &
  378. NonNullable<PluginConfig["tui"]> & {
  379. leader_timeout: number
  380. attention: TuiAttentionConfigView
  381. plugin_enabled?: Record<string, boolean>
  382. keybinds: TuiBindingLookupView
  383. }
  384. export type TuiApp = {
  385. readonly version: string
  386. }
  387. type Frozen<Value> = Value extends (...args: never[]) => unknown
  388. ? Value
  389. : Value extends ReadonlyArray<infer Item>
  390. ? ReadonlyArray<Frozen<Item>>
  391. : Value extends object
  392. ? { readonly [Key in keyof Value]: Frozen<Value[Key]> }
  393. : Value
  394. export type TuiSidebarMcpItem = {
  395. name: string
  396. status: McpStatus["status"]
  397. error?: string
  398. }
  399. export type TuiSidebarLspItem = Pick<LspStatus, "id" | "root" | "status">
  400. export type TuiSidebarTodoItem = Pick<Todo, "content" | "status">
  401. export type TuiSidebarFileItem = {
  402. file: string
  403. additions: number
  404. deletions: number
  405. }
  406. export type TuiHostSlotMap = {
  407. app: {}
  408. app_bottom: {}
  409. home_logo: {}
  410. home_prompt: {
  411. workspace_id?: string
  412. ref?: (ref: TuiPromptRef | undefined) => void
  413. }
  414. home_prompt_right: {
  415. workspace_id?: string
  416. }
  417. session_prompt: {
  418. session_id: string
  419. visible?: boolean
  420. disabled?: boolean
  421. on_submit?: () => void
  422. ref?: (ref: TuiPromptRef | undefined) => void
  423. }
  424. session_prompt_right: {
  425. session_id: string
  426. }
  427. home_bottom: {}
  428. home_footer: {}
  429. sidebar_title: {
  430. session_id: string
  431. title: string
  432. share_url?: string
  433. }
  434. sidebar_content: {
  435. session_id: string
  436. }
  437. sidebar_footer: {
  438. session_id: string
  439. }
  440. }
  441. export type TuiSlotMap<Slots extends Record<string, object> = {}> = TuiHostSlotMap & Slots
  442. type TuiSlotShape<Name extends string, Slots extends Record<string, object>> = Name extends keyof TuiHostSlotMap
  443. ? TuiHostSlotMap[Name]
  444. : Name extends keyof Slots
  445. ? Slots[Name]
  446. : Record<string, unknown>
  447. export type TuiSlotProps<Name extends string = string, Slots extends Record<string, object> = {}> = {
  448. name: Name
  449. mode?: SlotMode
  450. children?: JSX.Element
  451. } & TuiSlotShape<Name, Slots>
  452. export type TuiSlotContext = {
  453. theme: TuiTheme
  454. }
  455. type SlotCore<Slots extends Record<string, object> = {}> = SolidPlugin<TuiSlotMap<Slots>, TuiSlotContext>
  456. export type TuiSlotPlugin<Slots extends Record<string, object> = {}> = Omit<SlotCore<Slots>, "id"> & {
  457. id?: never
  458. }
  459. export type TuiSlots = {
  460. register: {
  461. (plugin: TuiSlotPlugin): string
  462. <Slots extends Record<string, object>>(plugin: TuiSlotPlugin<Slots>): string
  463. }
  464. }
  465. export type TuiEventBus = {
  466. on: <Type extends Event["type"]>(type: Type, handler: (event: Extract<Event, { type: Type }>) => void) => () => void
  467. }
  468. export type TuiDispose = () => void | Promise<void>
  469. export type TuiLifecycle = {
  470. readonly signal: AbortSignal
  471. onDispose: (fn: TuiDispose) => () => void
  472. }
  473. export type TuiPluginState = "first" | "updated" | "same"
  474. export type TuiPluginEntry = {
  475. id: string
  476. source: "file" | "npm" | "internal"
  477. spec: string
  478. target: string
  479. requested?: string
  480. version?: string
  481. modified?: number
  482. first_time: number
  483. last_time: number
  484. time_changed: number
  485. load_count: number
  486. fingerprint: string
  487. }
  488. export type TuiPluginMeta = TuiPluginEntry & {
  489. state: TuiPluginState
  490. }
  491. export type TuiPluginStatus = {
  492. id: string
  493. source: TuiPluginEntry["source"]
  494. spec: string
  495. target: string
  496. enabled: boolean
  497. active: boolean
  498. }
  499. export type TuiPluginInstallOptions = {
  500. global?: boolean
  501. }
  502. export type TuiPluginInstallResult =
  503. | {
  504. ok: true
  505. dir: string
  506. tui: boolean
  507. }
  508. | {
  509. ok: false
  510. message: string
  511. missing?: boolean
  512. }
  513. export type TuiWorkspace = {
  514. current: () => string | undefined
  515. set: (workspaceID?: string) => void
  516. }
  517. export type TuiPluginApi = {
  518. app: TuiApp
  519. attention: TuiAttention
  520. /**
  521. * Legacy `api.command` API kept so v1 plugins can initialize. Remove in v2.
  522. *
  523. * @deprecated Use `api.keymap.registerLayer`, `api.keymap.dispatchCommand`, and
  524. * `api.keymap.dispatchCommand("command.palette.show")` instead.
  525. */
  526. command?: TuiCommandApi
  527. keys: TuiKeys
  528. keymap: TuiKeymap
  529. route: {
  530. register: (routes: TuiRouteDefinition[]) => () => void
  531. navigate: (name: string, params?: Record<string, unknown>) => void
  532. readonly current: TuiRouteCurrent
  533. }
  534. ui: {
  535. Dialog: (props: TuiDialogProps) => JSX.Element
  536. DialogAlert: (props: TuiDialogAlertProps) => JSX.Element
  537. DialogConfirm: (props: TuiDialogConfirmProps) => JSX.Element
  538. DialogPrompt: (props: TuiDialogPromptProps) => JSX.Element
  539. DialogSelect: <Value = unknown>(props: TuiDialogSelectProps<Value>) => JSX.Element
  540. Slot: <Name extends string>(props: TuiSlotProps<Name>) => JSX.Element | null
  541. Prompt: (props: TuiPromptProps) => JSX.Element
  542. toast: (input: TuiToast) => void
  543. dialog: TuiDialogStack
  544. }
  545. readonly tuiConfig: Frozen<TuiConfigView>
  546. kv: TuiKV
  547. state: TuiState
  548. theme: TuiTheme
  549. client: OpencodeClient
  550. event: TuiEventBus
  551. renderer: CliRenderer
  552. slots: TuiSlots
  553. plugins: {
  554. list: () => ReadonlyArray<TuiPluginStatus>
  555. activate: (id: string) => Promise<boolean>
  556. deactivate: (id: string) => Promise<boolean>
  557. add: (spec: string) => Promise<boolean>
  558. install: (spec: string, options?: TuiPluginInstallOptions) => Promise<TuiPluginInstallResult>
  559. }
  560. lifecycle: TuiLifecycle
  561. }
  562. export type TuiPlugin = (api: TuiPluginApi, options: PluginOptions | undefined, meta: TuiPluginMeta) => Promise<void>
  563. export type TuiPluginModule = {
  564. id?: string
  565. tui: TuiPlugin
  566. server?: never
  567. }