tui-config.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ── TUI presentation ─────────────────────────────────────────────
  2. /**
  3. * Settings for the `custom-command` status line item.
  4. *
  5. * The command only runs when `tui.statusLine` explicitly names
  6. * `custom-command`, so this block alone never spawns anything. The TUI feeds
  7. * the command a small JSON payload on stdin. By default, the first stdout line
  8. * is one ordinary status item; block display adds rows around the existing line.
  9. */
  10. export interface TuiCustomStatusLineConfig {
  11. /**
  12. * Command line to execute. Tokenized like `externalEditorCommand` (no shell
  13. * pipes on POSIX); wrap paths with spaces in quotes.
  14. */
  15. command?: string;
  16. /** Display as one inline item (default), or a separate block. */
  17. display?: 'inline' | 'block';
  18. /** Block position relative to the original status line. Default above; ignored inline. */
  19. position?: 'above' | 'below';
  20. /** Plain muted text (default), or allow ANSI foreground/background colors and resets. */
  21. colorMode?: 'plain' | 'ansi';
  22. /** Maximum block rows. Default 3, clamped to 1–5; ignored for inline display. */
  23. maxLines?: number;
  24. /** Per-run timeout in milliseconds. The TUI clamps it to 500–30000; default 5000. */
  25. timeoutMs?: number;
  26. /**
  27. * Optional periodic refresh in seconds. Default 0 (event-driven only);
  28. * positive values are floored to a 10s minimum by the TUI.
  29. */
  30. intervalSeconds?: number;
  31. }
  32. export interface TuiConfig {
  33. /** Terminal notification policy. Unknown focus falls back to notifying. */
  34. notifications?: {
  35. when?: 'unfocused' | 'always' | 'never';
  36. method?: 'auto' | 'osc9' | 'osc777' | 'bel';
  37. };
  38. /** Show contextual Tips in the idle composer header. Defaults to true. */
  39. showTips?: boolean;
  40. /**
  41. * Status line items, in display order.
  42. *
  43. * Item ids are kebab-case, for example
  44. * `["model-with-reasoning", "context-remaining", "current-dir"]`.
  45. * The TUI owns the item catalog and ignores ids it does not recognize, so a
  46. * config written for a newer build still renders.
  47. *
  48. * `undefined` means "use the build default". An empty array is honoured as a
  49. * deliberately blank status line.
  50. */
  51. statusLine?: readonly string[];
  52. /** Settings for the opt-in `custom-command` status line item. */
  53. customStatusLine?: TuiCustomStatusLineConfig;
  54. }
  55. // ── TUI config parsing ──────────────────────────────────────────
  56. /**
  57. * Parses TUI presentation settings.
  58. *
  59. * Item ids are validated by the TUI, not here: config only guarantees the shape
  60. * (array of non-empty strings). A malformed value falls back to `undefined` so
  61. * the TUI renders its build default rather than an empty line.
  62. */
  63. export function parseTuiConfig(raw: Record<string, unknown>): TuiConfig {
  64. const rawTui = raw.tui;
  65. if (rawTui == null || typeof rawTui !== 'object' || Array.isArray(rawTui)) return {};
  66. const tui = rawTui as Record<string, unknown>;
  67. const rawNotifications = tui.notifications;
  68. let notifications: TuiConfig['notifications'];
  69. if (
  70. rawNotifications &&
  71. typeof rawNotifications === 'object' &&
  72. !Array.isArray(rawNotifications)
  73. ) {
  74. const { when, method } = rawNotifications as Record<string, unknown>;
  75. notifications = {
  76. ...(when === 'unfocused' || when === 'always' || when === 'never' ? { when } : {}),
  77. ...(method === 'auto' || method === 'osc9' || method === 'osc777' || method === 'bel'
  78. ? { method }
  79. : {}),
  80. };
  81. }
  82. const rawStatusLine = tui.statusLine;
  83. const statusLine = Array.isArray(rawStatusLine)
  84. ? rawStatusLine
  85. .filter((item): item is string => typeof item === 'string')
  86. .map((item) => item.trim())
  87. .filter(Boolean)
  88. : undefined;
  89. const customStatusLine = parseTuiCustomStatusLineConfig(tui.customStatusLine);
  90. return {
  91. ...(typeof tui.showTips === 'boolean' ? { showTips: tui.showTips } : {}),
  92. ...(notifications ? { notifications } : {}),
  93. ...(statusLine ? { statusLine } : {}),
  94. ...(customStatusLine ? { customStatusLine } : {}),
  95. };
  96. }
  97. /**
  98. * Parses the `custom-command` status item settings.
  99. *
  100. * A block without a usable `command` is dropped entirely: the TUI treats a
  101. * missing block as "item configured but inert", so partial or malformed config
  102. * can never spawn a process. Numeric bounds are enforced by the TUI, keeping
  103. * one owner for the clamping behaviour.
  104. */
  105. function parseTuiCustomStatusLineConfig(value: unknown): TuiCustomStatusLineConfig | undefined {
  106. if (value == null || typeof value !== 'object' || Array.isArray(value)) return undefined;
  107. const raw = value as Record<string, unknown>;
  108. const command = typeof raw.command === 'string' ? raw.command.trim() : '';
  109. if (!command) return undefined;
  110. return {
  111. command,
  112. ...(raw.display === 'inline' || raw.display === 'block' ? { display: raw.display } : {}),
  113. ...(raw.position === 'above' || raw.position === 'below' ? { position: raw.position } : {}),
  114. ...(raw.colorMode === 'plain' || raw.colorMode === 'ansi' ? { colorMode: raw.colorMode } : {}),
  115. ...(typeof raw.maxLines === 'number' && Number.isFinite(raw.maxLines)
  116. ? { maxLines: raw.maxLines }
  117. : {}),
  118. ...(typeof raw.timeoutMs === 'number' && Number.isFinite(raw.timeoutMs)
  119. ? { timeoutMs: raw.timeoutMs }
  120. : {}),
  121. ...(typeof raw.intervalSeconds === 'number' && Number.isFinite(raw.intervalSeconds)
  122. ? { intervalSeconds: raw.intervalSeconds }
  123. : {}),
  124. };
  125. }