herm-tui 1.0.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +54 -0
  3. package/package.json +82 -0
  4. package/scripts/postinstall.ts +29 -0
  5. package/src/app/gateway.tsx +83 -0
  6. package/src/app/gatewayEvents.ts +203 -0
  7. package/src/app/launch.ts +41 -0
  8. package/src/app/skin.tsx +31 -0
  9. package/src/app/spawnHistory.ts +75 -0
  10. package/src/app/tabs.ts +23 -0
  11. package/src/app/turnReducer.ts +390 -0
  12. package/src/app/useAppKeys.ts +268 -0
  13. package/src/app/useAtRefPopover.ts +99 -0
  14. package/src/app/useInputHistory.ts +66 -0
  15. package/src/app/useSession.ts +102 -0
  16. package/src/app/useSlashCommands.ts +70 -0
  17. package/src/app/useSlashPopover.ts +48 -0
  18. package/src/app.tsx +917 -0
  19. package/src/commands/slash.ts +151 -0
  20. package/src/components/avatar/AnimatedAvatar.tsx +66 -0
  21. package/src/components/avatar/eikon.ts +144 -0
  22. package/src/components/avatar/states/error.ts +1155 -0
  23. package/src/components/avatar/states/idle.ts +1155 -0
  24. package/src/components/avatar/states/index.ts +30 -0
  25. package/src/components/avatar/states/listening.ts +1155 -0
  26. package/src/components/avatar/states/speaking.ts +1155 -0
  27. package/src/components/avatar/states/thinking.ts +1155 -0
  28. package/src/components/avatar/states/working.ts +1155 -0
  29. package/src/components/chat/AtRefPopover.tsx +54 -0
  30. package/src/components/chat/CodeBlock.tsx +67 -0
  31. package/src/components/chat/Composer.tsx +347 -0
  32. package/src/components/chat/DiffBlock.tsx +116 -0
  33. package/src/components/chat/ErrorBlock.tsx +70 -0
  34. package/src/components/chat/MediaChip.tsx +114 -0
  35. package/src/components/chat/MessageItem.tsx +282 -0
  36. package/src/components/chat/MessageList.tsx +114 -0
  37. package/src/components/chat/PromptCard.tsx +359 -0
  38. package/src/components/chat/SlashPopover.tsx +158 -0
  39. package/src/components/chat/ThoughtCloud.tsx +185 -0
  40. package/src/components/chat/TypingIndicator.tsx +25 -0
  41. package/src/components/chat/tool/Subagent.tsx +75 -0
  42. package/src/components/chat/tool/frame.tsx +69 -0
  43. package/src/components/chat/tool/index.tsx +65 -0
  44. package/src/components/chat/tool/preview.ts +57 -0
  45. package/src/components/sidebar/ContextGauge.tsx +102 -0
  46. package/src/components/sidebar/Sidebar.tsx +143 -0
  47. package/src/components/tabs/TabBar.tsx +50 -0
  48. package/src/components/ui/FileLink.tsx +52 -0
  49. package/src/config/index.ts +156 -0
  50. package/src/config/lane.ts +161 -0
  51. package/src/config/models.ts +95 -0
  52. package/src/config/rules.ts +80 -0
  53. package/src/config/schema.ts +308 -0
  54. package/src/dialogs/alert.tsx +52 -0
  55. package/src/dialogs/chafa.tsx +72 -0
  56. package/src/dialogs/confirm.tsx +58 -0
  57. package/src/dialogs/curator.tsx +153 -0
  58. package/src/dialogs/eikon-picker.tsx +95 -0
  59. package/src/dialogs/help.tsx +80 -0
  60. package/src/dialogs/history.tsx +92 -0
  61. package/src/dialogs/info.tsx +115 -0
  62. package/src/dialogs/keys.tsx +170 -0
  63. package/src/dialogs/logs.tsx +42 -0
  64. package/src/dialogs/message.tsx +38 -0
  65. package/src/dialogs/model-picker.tsx +123 -0
  66. package/src/dialogs/new-profile.tsx +69 -0
  67. package/src/dialogs/new-task.tsx +103 -0
  68. package/src/dialogs/profile.tsx +55 -0
  69. package/src/dialogs/rollback.tsx +190 -0
  70. package/src/dialogs/spawn-history.tsx +80 -0
  71. package/src/dialogs/text-prompt.tsx +68 -0
  72. package/src/dialogs/theme-picker.tsx +50 -0
  73. package/src/home/index.ts +23 -0
  74. package/src/home/store.ts +267 -0
  75. package/src/index.tsx +113 -0
  76. package/src/keys/catalog.ts +115 -0
  77. package/src/keys/chord.ts +125 -0
  78. package/src/keys/conflicts.ts +48 -0
  79. package/src/keys/context.tsx +112 -0
  80. package/src/keys/index.ts +5 -0
  81. package/src/keys/list.ts +94 -0
  82. package/src/keys/oc-compat.ts +87 -0
  83. package/src/tabs/Agents.tsx +607 -0
  84. package/src/tabs/Analytics.tsx +154 -0
  85. package/src/tabs/Chat.tsx +50 -0
  86. package/src/tabs/Config.tsx +605 -0
  87. package/src/tabs/Context.tsx +599 -0
  88. package/src/tabs/Cron.tsx +294 -0
  89. package/src/tabs/Env.tsx +227 -0
  90. package/src/tabs/Kanban.tsx +367 -0
  91. package/src/tabs/Memory.tsx +294 -0
  92. package/src/tabs/Sessions.tsx +786 -0
  93. package/src/tabs/Skills.tsx +507 -0
  94. package/src/tabs/Toolsets.tsx +266 -0
  95. package/src/theme/builtin.ts +78 -0
  96. package/src/theme/context.tsx +106 -0
  97. package/src/theme/index.ts +4 -0
  98. package/src/theme/resolve.ts +134 -0
  99. package/src/theme/syntax.ts +31 -0
  100. package/src/theme/themes/aura.json +69 -0
  101. package/src/theme/themes/ayu.json +80 -0
  102. package/src/theme/themes/carbonfox.json +248 -0
  103. package/src/theme/themes/catppuccin-frappe.json +233 -0
  104. package/src/theme/themes/catppuccin-macchiato.json +233 -0
  105. package/src/theme/themes/catppuccin.json +112 -0
  106. package/src/theme/themes/cobalt2.json +228 -0
  107. package/src/theme/themes/cursor.json +249 -0
  108. package/src/theme/themes/dracula.json +219 -0
  109. package/src/theme/themes/everforest.json +241 -0
  110. package/src/theme/themes/flexoki.json +237 -0
  111. package/src/theme/themes/github.json +233 -0
  112. package/src/theme/themes/gruvbox.json +242 -0
  113. package/src/theme/themes/kanagawa.json +77 -0
  114. package/src/theme/themes/lucent-orng.json +237 -0
  115. package/src/theme/themes/material.json +235 -0
  116. package/src/theme/themes/matrix.json +77 -0
  117. package/src/theme/themes/mercury.json +252 -0
  118. package/src/theme/themes/monokai.json +221 -0
  119. package/src/theme/themes/nightowl.json +221 -0
  120. package/src/theme/themes/nord.json +223 -0
  121. package/src/theme/themes/one-dark.json +84 -0
  122. package/src/theme/themes/opencode.json +245 -0
  123. package/src/theme/themes/orng.json +249 -0
  124. package/src/theme/themes/osaka-jade.json +93 -0
  125. package/src/theme/themes/palenight.json +222 -0
  126. package/src/theme/themes/rosepine.json +234 -0
  127. package/src/theme/themes/solarized.json +223 -0
  128. package/src/theme/themes/synthwave84.json +226 -0
  129. package/src/theme/themes/tokyonight.json +243 -0
  130. package/src/theme/themes/vercel.json +245 -0
  131. package/src/theme/themes/vesper.json +218 -0
  132. package/src/theme/themes/zenburn.json +223 -0
  133. package/src/theme/types.ts +119 -0
  134. package/src/types/message.ts +97 -0
  135. package/src/ui/ChafaImage.tsx +64 -0
  136. package/src/ui/Splash.tsx +118 -0
  137. package/src/ui/borders.ts +28 -0
  138. package/src/ui/command.tsx +104 -0
  139. package/src/ui/dialog-select.tsx +164 -0
  140. package/src/ui/dialog.tsx +102 -0
  141. package/src/ui/fmt.ts +82 -0
  142. package/src/ui/kv.tsx +28 -0
  143. package/src/ui/shell.tsx +45 -0
  144. package/src/ui/spinner.tsx +59 -0
  145. package/src/ui/splash-art.ts +123 -0
  146. package/src/ui/table.tsx +117 -0
  147. package/src/ui/ticker.tsx +90 -0
  148. package/src/ui/toast.tsx +130 -0
  149. package/src/utils/categorical.ts +77 -0
  150. package/src/utils/chafa.ts +173 -0
  151. package/src/utils/clipboard.ts +67 -0
  152. package/src/utils/context-segments.ts +317 -0
  153. package/src/utils/control.ts +495 -0
  154. package/src/utils/drop.ts +25 -0
  155. package/src/utils/editor.ts +33 -0
  156. package/src/utils/fuzzy.ts +45 -0
  157. package/src/utils/gateway-client.ts +253 -0
  158. package/src/utils/gateway-types.ts +282 -0
  159. package/src/utils/git.ts +57 -0
  160. package/src/utils/hermes-analytics.ts +134 -0
  161. package/src/utils/hermes-home.ts +821 -0
  162. package/src/utils/hermes-kanban.ts +154 -0
  163. package/src/utils/hermes-profiles.ts +217 -0
  164. package/src/utils/interpolate.ts +31 -0
  165. package/src/utils/math-unicode.ts +818 -0
  166. package/src/utils/memory-activity.ts +140 -0
  167. package/src/utils/open-file.ts +13 -0
  168. package/src/utils/paths.ts +52 -0
  169. package/src/utils/perf.ts +235 -0
  170. package/src/utils/preferences.ts +150 -0
  171. package/src/utils/sessions-db.ts +396 -0
  172. package/src/utils/subagent-tree.ts +146 -0
  173. package/src/utils/terminal-reset.ts +129 -0
  174. package/src/utils/tips.ts +67 -0
  175. package/src/utils/tokens.ts +87 -0
@@ -0,0 +1,818 @@
1
+ // Best-effort LaTeX → Unicode for inline / display math captured by the
2
+ // markdown renderer. The terminal can't typeset LaTeX, but Unicode covers
3
+ // most of what models actually emit: Greek letters, blackboard / fraktur /
4
+ // calligraphic capitals, set theory + logic operators, common arrows,
5
+ // sub/superscripts, and `\frac{a}{b}` collapsed to `a/b`.
6
+ //
7
+ // Ported from hermes-agent ui-tui/src/lib/mathUnicode.ts (c3d39feb3 +
8
+ // cb039ac00). Single divergence: `\boxed` / `\fbox` emit markdown-level
9
+ // `**X**` instead of U+0001 / U+0002 sentinels, because herm feeds this
10
+ // output directly to OpenTUI's <markdown> component which has no
11
+ // sentinel-split hook. Upstream's React renderer uses the sentinels to
12
+ // apply inverse+bold; we trade that visual for plain bold and keep the
13
+ // pipeline single-pass.
14
+ //
15
+ // Design rules:
16
+ // • Pure regex pipeline. Anything we don't recognise is preserved
17
+ // verbatim (so a `\foo{bar}` we've never heard of still survives).
18
+ // A real LaTeX parser would be more correct but throws on partial
19
+ // input — terminal users would rather see the raw command than a
20
+ // parse-error placeholder.
21
+ // • Longest-match-first ordering on commands so `\le` doesn't shadow
22
+ // `\leq`, `\sub` doesn't shadow `\subseteq`, etc.
23
+ // • Word-boundary lookahead `(?![A-Za-z])` after each command so
24
+ // `\pix` (made-up command) doesn't get partially substituted as `π`.
25
+ // • `\mathbb{X}`, `\mathcal{X}`, `\mathfrak{X}` only handle a single
26
+ // letter argument — multi-letter `\mathbb{NN}` is rare and would
27
+ // need a real parser to do correctly.
28
+ // • Sub/super scripts only convert if EVERY character has a Unicode
29
+ // equivalent. Mixed content like `^{n+1}` falls back to the raw
30
+ // LaTeX so we don't emit `ⁿ+¹` (which has no `+` superscript glyph
31
+ // in some fonts and reads worse than the source).
32
+
33
+ const SYMBOLS: Record<string, string> = {
34
+ // Greek lowercase
35
+ '\\alpha': 'α',
36
+ '\\beta': 'β',
37
+ '\\gamma': 'γ',
38
+ '\\delta': 'δ',
39
+ '\\epsilon': 'ε',
40
+ '\\varepsilon': 'ε',
41
+ '\\zeta': 'ζ',
42
+ '\\eta': 'η',
43
+ '\\theta': 'θ',
44
+ '\\vartheta': 'ϑ',
45
+ '\\iota': 'ι',
46
+ '\\kappa': 'κ',
47
+ '\\lambda': 'λ',
48
+ '\\mu': 'μ',
49
+ '\\nu': 'ν',
50
+ '\\xi': 'ξ',
51
+ '\\pi': 'π',
52
+ '\\varpi': 'ϖ',
53
+ '\\rho': 'ρ',
54
+ '\\varrho': 'ϱ',
55
+ '\\sigma': 'σ',
56
+ '\\varsigma': 'ς',
57
+ '\\tau': 'τ',
58
+ '\\upsilon': 'υ',
59
+ '\\phi': 'φ',
60
+ '\\varphi': 'φ',
61
+ '\\chi': 'χ',
62
+ '\\psi': 'ψ',
63
+ '\\omega': 'ω',
64
+
65
+ // Greek uppercase
66
+ '\\Gamma': 'Γ',
67
+ '\\Delta': 'Δ',
68
+ '\\Theta': 'Θ',
69
+ '\\Lambda': 'Λ',
70
+ '\\Xi': 'Ξ',
71
+ '\\Pi': 'Π',
72
+ '\\Sigma': 'Σ',
73
+ '\\Upsilon': 'Υ',
74
+ '\\Phi': 'Φ',
75
+ '\\Psi': 'Ψ',
76
+ '\\Omega': 'Ω',
77
+
78
+ // Big operators
79
+ '\\sum': '∑',
80
+ '\\prod': '∏',
81
+ '\\coprod': '∐',
82
+ '\\int': '∫',
83
+ '\\iint': '∬',
84
+ '\\iiint': '∭',
85
+ '\\oint': '∮',
86
+ '\\bigcup': '⋃',
87
+ '\\bigcap': '⋂',
88
+ '\\bigvee': '⋁',
89
+ '\\bigwedge': '⋀',
90
+ '\\bigoplus': '⨁',
91
+ '\\bigotimes': '⨂',
92
+
93
+ // Calculus
94
+ '\\partial': '∂',
95
+ '\\nabla': '∇',
96
+ '\\sqrt': '√',
97
+
98
+ // Sets
99
+ '\\emptyset': '∅',
100
+ '\\varnothing': '∅',
101
+ '\\infty': '∞',
102
+ '\\in': '∈',
103
+ '\\notin': '∉',
104
+ '\\ni': '∋',
105
+ '\\subset': '⊂',
106
+ '\\supset': '⊃',
107
+ '\\subseteq': '⊆',
108
+ '\\supseteq': '⊇',
109
+ '\\subsetneq': '⊊',
110
+ '\\supsetneq': '⊋',
111
+ '\\cup': '∪',
112
+ '\\cap': '∩',
113
+ '\\setminus': '∖',
114
+ '\\complement': '∁',
115
+
116
+ // Logic
117
+ '\\forall': '∀',
118
+ '\\exists': '∃',
119
+ '\\nexists': '∄',
120
+ '\\land': '∧',
121
+ '\\lor': '∨',
122
+ '\\lnot': '¬',
123
+ '\\neg': '¬',
124
+ '\\therefore': '∴',
125
+ '\\because': '∵',
126
+
127
+ // Relations
128
+ '\\le': '≤',
129
+ '\\leq': '≤',
130
+ '\\ge': '≥',
131
+ '\\geq': '≥',
132
+ '\\ne': '≠',
133
+ '\\neq': '≠',
134
+ '\\ll': '≪',
135
+ '\\gg': '≫',
136
+ '\\approx': '≈',
137
+ '\\equiv': '≡',
138
+ '\\cong': '≅',
139
+ '\\sim': '∼',
140
+ '\\simeq': '≃',
141
+ '\\propto': '∝',
142
+ '\\perp': '⊥',
143
+ '\\parallel': '∥',
144
+ '\\models': '⊨',
145
+ '\\vdash': '⊢',
146
+ '\\mid': '∣',
147
+ '\\nmid': '∤',
148
+ '\\divides': '∣',
149
+
150
+ // Common standalone glyphs
151
+ '\\blacksquare': '■',
152
+ '\\square': '□',
153
+ '\\Box': '□',
154
+ '\\qed': '∎',
155
+ '\\bigstar': '★',
156
+
157
+ // Modular arithmetic — the `\pmod{p}` form (with arg) is handled below;
158
+ // the bare `\bmod` / `\mod` commands are simple text substitutions.
159
+ '\\bmod': 'mod',
160
+ '\\mod': 'mod',
161
+
162
+ // Brackets / fences (named delimiter commands; the `\left\X` / `\right\X`
163
+ // unwrapping below leaves these behind for the symbol pass to resolve).
164
+ '\\langle': '⟨',
165
+ '\\rangle': '⟩',
166
+ '\\lceil': '⌈',
167
+ '\\rceil': '⌉',
168
+ '\\lfloor': '⌊',
169
+ '\\rfloor': '⌋',
170
+ '\\|': '‖',
171
+
172
+ // Arrows
173
+ '\\to': '→',
174
+ '\\rightarrow': '→',
175
+ '\\leftarrow': '←',
176
+ '\\leftrightarrow': '↔',
177
+ '\\Rightarrow': '⇒',
178
+ '\\Leftarrow': '⇐',
179
+ '\\Leftrightarrow': '⇔',
180
+ '\\implies': '⟹',
181
+ '\\impliedby': '⟸',
182
+ '\\iff': '⟺',
183
+ '\\mapsto': '↦',
184
+ '\\hookrightarrow': '↪',
185
+ '\\hookleftarrow': '↩',
186
+ '\\uparrow': '↑',
187
+ '\\downarrow': '↓',
188
+ '\\updownarrow': '↕',
189
+
190
+ // Binary operators
191
+ '\\cdot': '⋅',
192
+ '\\cdots': '⋯',
193
+ '\\ldots': '…',
194
+ '\\dots': '…',
195
+ '\\dotsb': '…',
196
+ '\\dotsc': '…',
197
+ '\\vdots': '⋮',
198
+ '\\ddots': '⋱',
199
+ '\\times': '×',
200
+ '\\div': '÷',
201
+ '\\pm': '±',
202
+ '\\mp': '∓',
203
+ '\\circ': '∘',
204
+ '\\bullet': '•',
205
+ '\\star': '⋆',
206
+ '\\ast': '∗',
207
+ '\\oplus': '⊕',
208
+ '\\ominus': '⊖',
209
+ '\\otimes': '⊗',
210
+ '\\odot': '⊙',
211
+ '\\diamond': '⋄',
212
+ '\\angle': '∠',
213
+ '\\triangle': '△',
214
+
215
+ // Spacing — collapse to varying widths of regular space
216
+ '\\,': ' ',
217
+ '\\;': ' ',
218
+ '\\:': ' ',
219
+ '\\!': '',
220
+ '\\ ': ' ',
221
+ '\\quad': ' ',
222
+ '\\qquad': ' ',
223
+
224
+ // Functions (LaTeX renders these in roman; we just keep the name)
225
+ '\\sin': 'sin',
226
+ '\\cos': 'cos',
227
+ '\\tan': 'tan',
228
+ '\\cot': 'cot',
229
+ '\\sec': 'sec',
230
+ '\\csc': 'csc',
231
+ '\\arcsin': 'arcsin',
232
+ '\\arccos': 'arccos',
233
+ '\\arctan': 'arctan',
234
+ '\\sinh': 'sinh',
235
+ '\\cosh': 'cosh',
236
+ '\\tanh': 'tanh',
237
+ '\\log': 'log',
238
+ '\\ln': 'ln',
239
+ '\\exp': 'exp',
240
+ '\\det': 'det',
241
+ '\\dim': 'dim',
242
+ '\\ker': 'ker',
243
+ '\\lim': 'lim',
244
+ '\\liminf': 'liminf',
245
+ '\\limsup': 'limsup',
246
+ '\\sup': 'sup',
247
+ '\\inf': 'inf',
248
+ '\\max': 'max',
249
+ '\\min': 'min',
250
+ '\\arg': 'arg',
251
+ '\\gcd': 'gcd',
252
+
253
+ // Escaped literals — model occasionally emits these for display
254
+ '\\&': '&',
255
+ '\\%': '%',
256
+ '\\$': '$',
257
+ '\\#': '#',
258
+ '\\_': '_',
259
+ '\\{': '{',
260
+ '\\}': '}'
261
+ }
262
+
263
+ const BB: Record<string, string> = {
264
+ A: '𝔸',
265
+ B: '𝔹',
266
+ C: 'ℂ',
267
+ D: '𝔻',
268
+ E: '𝔼',
269
+ F: '𝔽',
270
+ G: '𝔾',
271
+ H: 'ℍ',
272
+ I: '𝕀',
273
+ J: '𝕁',
274
+ K: '𝕂',
275
+ L: '𝕃',
276
+ M: '𝕄',
277
+ N: 'ℕ',
278
+ O: '𝕆',
279
+ P: 'ℙ',
280
+ Q: 'ℚ',
281
+ R: 'ℝ',
282
+ S: '𝕊',
283
+ T: '𝕋',
284
+ U: '𝕌',
285
+ V: '𝕍',
286
+ W: '𝕎',
287
+ X: '𝕏',
288
+ Y: '𝕐',
289
+ Z: 'ℤ'
290
+ }
291
+
292
+ const CAL: Record<string, string> = {
293
+ A: '𝒜',
294
+ B: 'ℬ',
295
+ C: '𝒞',
296
+ D: '𝒟',
297
+ E: 'ℰ',
298
+ F: 'ℱ',
299
+ G: '𝒢',
300
+ H: 'ℋ',
301
+ I: 'ℐ',
302
+ J: '𝒥',
303
+ K: '𝒦',
304
+ L: 'ℒ',
305
+ M: 'ℳ',
306
+ N: '𝒩',
307
+ O: '𝒪',
308
+ P: '𝒫',
309
+ Q: '𝒬',
310
+ R: 'ℛ',
311
+ S: '𝒮',
312
+ T: '𝒯',
313
+ U: '𝒰',
314
+ V: '𝒱',
315
+ W: '𝒲',
316
+ X: '𝒳',
317
+ Y: '𝒴',
318
+ Z: '𝒵'
319
+ }
320
+
321
+ const FRAK: Record<string, string> = {
322
+ A: '𝔄',
323
+ B: '𝔅',
324
+ C: 'ℭ',
325
+ D: '𝔇',
326
+ E: '𝔈',
327
+ F: '𝔉',
328
+ G: '𝔊',
329
+ H: 'ℌ',
330
+ I: 'ℑ',
331
+ J: '𝔍',
332
+ K: '𝔎',
333
+ L: '𝔏',
334
+ M: '𝔐',
335
+ N: '𝔑',
336
+ O: '𝔒',
337
+ P: '𝔓',
338
+ Q: '𝔔',
339
+ R: 'ℜ',
340
+ S: '𝔖',
341
+ T: '𝔗',
342
+ U: '𝔘',
343
+ V: '𝔙',
344
+ W: '𝔚',
345
+ X: '𝔛',
346
+ Y: '𝔜',
347
+ Z: 'ℨ'
348
+ }
349
+
350
+ const SUPERSCRIPT: Record<string, string> = {
351
+ '0': '⁰',
352
+ '1': '¹',
353
+ '2': '²',
354
+ '3': '³',
355
+ '4': '⁴',
356
+ '5': '⁵',
357
+ '6': '⁶',
358
+ '7': '⁷',
359
+ '8': '⁸',
360
+ '9': '⁹',
361
+ '+': '⁺',
362
+ '-': '⁻',
363
+ '=': '⁼',
364
+ '(': '⁽',
365
+ ')': '⁾',
366
+ a: 'ᵃ',
367
+ b: 'ᵇ',
368
+ c: 'ᶜ',
369
+ d: 'ᵈ',
370
+ e: 'ᵉ',
371
+ f: 'ᶠ',
372
+ g: 'ᵍ',
373
+ h: 'ʰ',
374
+ i: 'ⁱ',
375
+ j: 'ʲ',
376
+ k: 'ᵏ',
377
+ l: 'ˡ',
378
+ m: 'ᵐ',
379
+ n: 'ⁿ',
380
+ o: 'ᵒ',
381
+ p: 'ᵖ',
382
+ r: 'ʳ',
383
+ s: 'ˢ',
384
+ t: 'ᵗ',
385
+ u: 'ᵘ',
386
+ v: 'ᵛ',
387
+ w: 'ʷ',
388
+ x: 'ˣ',
389
+ y: 'ʸ',
390
+ z: 'ᶻ'
391
+ }
392
+
393
+ const SUBSCRIPT: Record<string, string> = {
394
+ '0': '₀',
395
+ '1': '₁',
396
+ '2': '₂',
397
+ '3': '₃',
398
+ '4': '₄',
399
+ '5': '₅',
400
+ '6': '₆',
401
+ '7': '₇',
402
+ '8': '₈',
403
+ '9': '₉',
404
+ '+': '₊',
405
+ '-': '₋',
406
+ '=': '₌',
407
+ '(': '₍',
408
+ ')': '₎',
409
+ a: 'ₐ',
410
+ e: 'ₑ',
411
+ h: 'ₕ',
412
+ i: 'ᵢ',
413
+ j: 'ⱼ',
414
+ k: 'ₖ',
415
+ l: 'ₗ',
416
+ m: 'ₘ',
417
+ n: 'ₙ',
418
+ o: 'ₒ',
419
+ p: 'ₚ',
420
+ r: 'ᵣ',
421
+ s: 'ₛ',
422
+ t: 'ₜ',
423
+ u: 'ᵤ',
424
+ v: 'ᵥ',
425
+ x: 'ₓ'
426
+ }
427
+
428
+ // Markers for `\boxed` / `\fbox` regions. Upstream hermes-agent uses
429
+ // non-printable U+0001 / U+0002 sentinels and its React renderer
430
+ // splits on them to apply inverse+bold. Herm feeds the converted
431
+ // string directly to OpenTUI's <markdown> component, which doesn't
432
+ // have a split-and-style hook — so we emit markdown-level `**X**`
433
+ // instead, and let the markdown renderer turn it into bold on its
434
+ // own. This trades the inverse-video visual for standard bold but
435
+ // keeps the pipeline hook-free.
436
+ const BOX_WRAP_OPEN = "**"
437
+ const BOX_WRAP_CLOSE = "**"
438
+
439
+ const escapeRe = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
440
+
441
+ // Pre-compile two symbol regexes: one for letter-ending commands (`\pi`,
442
+ // `\sum`) which need a `(?![A-Za-z])` lookahead so they don't partially
443
+ // match `\pix` or `\summa`, and one for punctuation-ending commands
444
+ // (`\{`, `\,`, `\|`) which must NOT have the lookahead — otherwise
445
+ // `\{p` would refuse to substitute because `p` is a letter.
446
+ //
447
+ // Longest commands first inside each group so `\leq` beats `\le`.
448
+ const splitByEnding = (keys: string[]) => {
449
+ const letter: string[] = []
450
+ const punct: string[] = []
451
+
452
+ for (const k of keys) {
453
+ if (/[A-Za-z]$/.test(k)) {
454
+ letter.push(k)
455
+ } else {
456
+ punct.push(k)
457
+ }
458
+ }
459
+
460
+ return { letter, punct }
461
+ }
462
+
463
+ const buildAlt = (cmds: string[]) =>
464
+ cmds
465
+ .sort((a, b) => b.length - a.length)
466
+ .map(escapeRe)
467
+ .join('|')
468
+
469
+ const { letter: LETTER_CMDS, punct: PUNCT_CMDS } = splitByEnding(Object.keys(SYMBOLS))
470
+
471
+ const SYMBOL_LETTER_RE = new RegExp('(?:' + buildAlt(LETTER_CMDS) + ')(?![A-Za-z])', 'g')
472
+ const SYMBOL_PUNCT_RE = new RegExp('(?:' + buildAlt(PUNCT_CMDS) + ')', 'g')
473
+
474
+ const convertScript = (input: string, table: Record<string, string>, sigil: '^' | '_'): string => {
475
+ let out = ''
476
+ let allMapped = true
477
+
478
+ for (const ch of input) {
479
+ const mapped = table[ch]
480
+
481
+ if (!mapped) {
482
+ allMapped = false
483
+
484
+ break
485
+ }
486
+
487
+ out += mapped
488
+ }
489
+
490
+ if (allMapped) {
491
+ return out
492
+ }
493
+
494
+ // Fallback: if the body is a single visible character (e.g. `∞` after
495
+ // earlier symbol substitution), render it without braces — `^∞` reads
496
+ // far better than `^{∞}` in a terminal. Multi-char bodies that don't
497
+ // fully convert use parens (`e^(iπ)`) instead of braces (`e^{iπ}`)
498
+ // because parens are normal punctuation while braces look like
499
+ // unrendered LaTeX.
500
+ const trimmed = input.trim()
501
+
502
+ if ([...trimmed].length === 1) {
503
+ return `${sigil}${trimmed}`
504
+ }
505
+
506
+ return `${sigil}(${trimmed})`
507
+ }
508
+
509
+ // Walk the string and parse `{...}` honouring nested braces. Unlike a
510
+ // `\{[^{}]*\}` regex this survives `\frac{|t|^{p-1}|P(t)|^p}{...}` where
511
+ // the numerator contains its own braces from a superscript. Returns the
512
+ // inner content (without the outer braces) and the offset just past the
513
+ // closing `}`. Returns null if there is no balanced brace at `start`.
514
+ const readBraced = (s: string, start: number): { content: string; end: number } | null => {
515
+ if (s[start] !== '{') {
516
+ return null
517
+ }
518
+
519
+ let depth = 1
520
+ let i = start + 1
521
+
522
+ while (i < s.length && depth > 0) {
523
+ const c = s[i]
524
+
525
+ // Skip escapes — `\{` and `\}` inside a body are literal braces and
526
+ // should not change the brace counter.
527
+ if (c === '\\' && i + 1 < s.length) {
528
+ i += 2
529
+ continue
530
+ }
531
+
532
+ if (c === '{') {
533
+ depth++
534
+ } else if (c === '}') {
535
+ depth--
536
+ }
537
+
538
+ if (depth > 0) {
539
+ i++
540
+ }
541
+ }
542
+
543
+ if (depth !== 0) {
544
+ return null
545
+ }
546
+
547
+ return { content: s.slice(start + 1, i), end: i + 1 }
548
+ }
549
+
550
+ // Replace every occurrence of `\command{arg}` using balanced-brace parsing
551
+ // (so `\boxed{x^{n+1}}` works where a `[^{}]*` regex would fail). The
552
+ // `render` callback receives the inner content already recursed-into, so
553
+ // `\boxed{\boxed{x}}` resolves outside-in cleanly. Unmatched `\command`
554
+ // (no following `{...}`) is preserved verbatim.
555
+ const replaceBracedCommand = (input: string, command: string, render: (content: string) => string): string => {
556
+ const cmdLen = command.length
557
+ let out = ''
558
+ let i = 0
559
+
560
+ while (i < input.length) {
561
+ const idx = input.indexOf(command, i)
562
+
563
+ if (idx < 0) {
564
+ out += input.slice(i)
565
+
566
+ return out
567
+ }
568
+
569
+ const after = input[idx + cmdLen]
570
+
571
+ if (after && /[A-Za-z]/.test(after)) {
572
+ out += input.slice(i, idx + cmdLen)
573
+ i = idx + cmdLen
574
+ continue
575
+ }
576
+
577
+ out += input.slice(i, idx)
578
+
579
+ let p = idx + cmdLen
580
+
581
+ while (input[p] === ' ' || input[p] === '\t') p++
582
+
583
+ const arg = readBraced(input, p)
584
+
585
+ if (!arg) {
586
+ out += input.slice(idx, p + 1)
587
+ i = p + 1
588
+ continue
589
+ }
590
+
591
+ out += render(replaceBracedCommand(arg.content, command, render))
592
+ i = arg.end
593
+ }
594
+
595
+ return out
596
+ }
597
+
598
+ // Replace every `\frac{num}{den}` with `num/den` (parens around either
599
+ // side when its precedence demands it). The recursion handles nested
600
+ // fractions naturally: `\frac{1}{\frac{1}{x}}` collapses to `1/(1/x)`
601
+ // because we recurse into `den` before deciding whether to parenthesise.
602
+ const replaceFracs = (input: string): string => {
603
+ let out = ''
604
+ let i = 0
605
+
606
+ while (i < input.length) {
607
+ const idx = input.indexOf('\\frac', i)
608
+
609
+ if (idx < 0) {
610
+ out += input.slice(i)
611
+
612
+ return out
613
+ }
614
+
615
+ const after = input[idx + 5]
616
+
617
+ // `(?![A-Za-z])` — protect hypothetical commands like `\fraction`.
618
+ if (after && /[A-Za-z]/.test(after)) {
619
+ out += input.slice(i, idx + 5)
620
+ i = idx + 5
621
+ continue
622
+ }
623
+
624
+ out += input.slice(i, idx)
625
+
626
+ let p = idx + 5
627
+
628
+ while (input[p] === ' ' || input[p] === '\t') p++
629
+
630
+ const num = readBraced(input, p)
631
+
632
+ if (!num) {
633
+ out += input.slice(idx, p + 1)
634
+ i = p + 1
635
+ continue
636
+ }
637
+
638
+ p = num.end
639
+
640
+ while (input[p] === ' ' || input[p] === '\t') p++
641
+
642
+ const den = readBraced(input, p)
643
+
644
+ if (!den) {
645
+ out += input.slice(idx, p + 1)
646
+ i = p + 1
647
+ continue
648
+ }
649
+
650
+ out += `${wrapForFrac(replaceFracs(num.content))}/${wrapForFrac(replaceFracs(den.content))}`
651
+ i = den.end
652
+ }
653
+
654
+ return out
655
+ }
656
+
657
+ // Wrap multi-token expressions in parens so `\frac{a+b}{c}` becomes
658
+ // `(a+b)/c` rather than `a+b/c`. We wrap whenever inline `/` would
659
+ // change the meaning — that's any binary operator (`+`, `-`, `*`, `/`)
660
+ // or whitespace separating tokens. `*` and `/` matter because nested
661
+ // fractions and products like `\frac{a*b}{c}` and `\frac{1/x}{y}` would
662
+ // otherwise read as `a*b/c` (right-associative ambiguity) and `1/x/y`.
663
+ // Atomic factors like `n!`, `x^2`, `\sin x` don't trigger any of these
664
+ // and stay un-parenthesised — wrapping them just clutters the output.
665
+ const wrapForFrac = (expr: string) => {
666
+ const trimmed = expr.trim()
667
+
668
+ if (!trimmed) {
669
+ return trimmed
670
+ }
671
+
672
+ if (/^\(.*\)$/.test(trimmed)) {
673
+ return trimmed
674
+ }
675
+
676
+ if (/[+\-/*]|\s/.test(trimmed)) {
677
+ return `(${trimmed})`
678
+ }
679
+
680
+ return trimmed
681
+ }
682
+
683
+ export function texToUnicode(input: string): string {
684
+ let s = input
685
+
686
+ s = s.replace(/\\mathbb\s*\{([A-Za-z])\}/g, (raw, c: string) => BB[c] ?? raw)
687
+ s = s.replace(/\\mathcal\s*\{([A-Za-z])\}/g, (raw, c: string) => CAL[c] ?? raw)
688
+ s = s.replace(/\\mathfrak\s*\{([A-Za-z])\}/g, (raw, c: string) => FRAK[c] ?? raw)
689
+ s = s.replace(/\\mathbf\s*\{([^{}]+)\}/g, (_, c: string) => c)
690
+ s = s.replace(/\\mathit\s*\{([^{}]+)\}/g, (_, c: string) => c)
691
+ s = s.replace(/\\mathrm\s*\{([^{}]+)\}/g, (_, c: string) => c)
692
+ s = s.replace(/\\text\s*\{([^{}]+)\}/g, (_, c: string) => c)
693
+ s = s.replace(/\\operatorname\s*\{([^{}]+)\}/g, (_, c: string) => c)
694
+
695
+ s = s.replace(/\\overline\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0305`)
696
+ s = s.replace(/\\hat\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0302`)
697
+ s = s.replace(/\\bar\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0304`)
698
+ s = s.replace(/\\tilde\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0303`)
699
+ s = s.replace(/\\vec\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u20D7`)
700
+ s = s.replace(/\\dot\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0307`)
701
+ s = s.replace(/\\ddot\s*\{([^{}]+)\}/g, (_, c: string) => `${c}\u0308`)
702
+
703
+ s = replaceFracs(s)
704
+
705
+ // `\boxed{X}` / `\fbox{X}` highlight a final answer. Terminals can't
706
+ // draw a real box, so we wrap the content in U+0001 / U+0002 control
707
+ // characters — non-printable, never present in real text — and let the
708
+ // markdown renderer split on them and apply a highlight style (inverse
709
+ // video) to the bracketed region. This keeps `texToUnicode` pure-string
710
+ // while letting the React layer do the actual visual emphasis.
711
+ // Argument is parsed with balanced braces so nested `{...}` from
712
+ // superscripts / fractions inside the box survive.
713
+ s = replaceBracedCommand(s, '\\boxed', body => `${BOX_WRAP_OPEN}${body.trim()}${BOX_WRAP_CLOSE}`)
714
+ s = replaceBracedCommand(s, '\\fbox', body => `${BOX_WRAP_OPEN}${body.trim()}${BOX_WRAP_CLOSE}`)
715
+
716
+ // `\xrightarrow{label}` / `\xleftarrow{label}` collapse to an arrow with
717
+ // the label inline. LaTeX renders the label above the arrow; in monospace
718
+ // we put it adjacent — `─label→` is the closest readable approximation.
719
+ // Run before the symbol pass so the label can still pick up Greek and
720
+ // operator substitutions afterwards.
721
+ s = s.replace(/\\xrightarrow\s*\{([^{}]*)\}/g, (_, label: string) => `─${label.trim()}→`)
722
+ s = s.replace(/\\xleftarrow\s*\{([^{}]*)\}/g, (_, label: string) => `←${label.trim()}─`)
723
+ s = s.replace(/\\Longrightarrow/g, '⟹')
724
+ s = s.replace(/\\Longleftarrow/g, '⟸')
725
+ s = s.replace(/\\Longleftrightarrow/g, '⟺')
726
+
727
+ // `\pmod{p}` → ` (mod p)` (LaTeX adds parens automatically); `\pod{p}`
728
+ // is a paren-less variant; `\tag{n}` is the equation-number annotation
729
+ // shown to the right of an equation. Collapse to a single-space-prefixed
730
+ // bracketed form. The leading `\s*` in the pattern absorbs any whitespace
731
+ // already in the source so we don't end up with `b (mod p)` (double
732
+ // space) when the user wrote `b \pmod{p}`.
733
+ s = s.replace(/\s*\\pmod\s*\{([^{}]*)\}/g, (_, p: string) => ` (mod ${p.trim()})`)
734
+ s = s.replace(/\s*\\pod\s*\{([^{}]*)\}/g, (_, p: string) => ` (${p.trim()})`)
735
+ s = s.replace(/\s*\\tag\s*\{([^{}]*)\}/g, (_, n: string) => ` (${n.trim()})`)
736
+
737
+ // `\big`, `\Big`, `\bigg`, `\Bigg` (with optional `l`/`r`/`m` suffix)
738
+ // are sizing wrappers analogous to `\left`/`\right` but without the
739
+ // automatic-pairing semantics. Strip them and leave whatever delimiter
740
+ // follows. The trailing `(?![A-Za-z])` protects `\bigtriangleup` and
741
+ // any other letter-continuation command from being shaved.
742
+ s = s.replace(/\\(?:Bigg|bigg|Big|big)[lrm]?(?![A-Za-z])/g, '')
743
+
744
+ // Style / size hints that don't typeset any glyph and only affect how
745
+ // things would be sized in a real LaTeX engine. In a terminal every
746
+ // glyph is one monospace cell, so there's nothing to do — drop them
747
+ // (with any trailing whitespace) so they don't leak through as raw
748
+ // `\displaystyle` in the output.
749
+ s = s.replace(/\\(?:scriptscriptstyle|displaystyle|scriptstyle|textstyle|nolimits|limits)(?![A-Za-z])\s*/g, '')
750
+
751
+ // `\left` and `\right` are sizing wrappers around any delimiter — bare
752
+ // (`\left(`), escaped (`\left\{`), or named (`\left\langle`). Strip the
753
+ // wrapper unconditionally and let the rest of the pipeline (or the
754
+ // upcoming symbol pass) handle whatever delimiter follows. The optional
755
+ // `.?` consumes `\left.` / `\right.` which mean "no delimiter".
756
+ // Lookahead `(?![A-Za-z])` keeps `\leftarrow` / `\leftrightarrow` safe.
757
+ s = s.replace(/\\left(?![A-Za-z])\.?/g, '')
758
+ s = s.replace(/\\right(?![A-Za-z])\.?/g, '')
759
+
760
+ // Run symbol substitution BEFORE scripts so a body like `^{\infty}`
761
+ // becomes `^{∞}` first; convertScript can then either map ∞ to a
762
+ // superscript (it can't — Unicode lacks one) or fall back to `^∞`
763
+ // by stripping braces around the now-single-character body.
764
+ //
765
+ // Punctuation pass first — these can be followed by letters (`\{p`
766
+ // is "open-brace then p"), so the letter pass's `(?![A-Za-z])` rule
767
+ // would wrongly block them.
768
+ s = s.replace(SYMBOL_PUNCT_RE, m => SYMBOLS[m] ?? m)
769
+ s = s.replace(SYMBOL_LETTER_RE, m => SYMBOLS[m] ?? m)
770
+
771
+ // Bare `^c` / `_c` handles ONLY alphanumerics and `+`/`-`/`=`. Parens
772
+ // are intentionally excluded because the braced-fallback above can
773
+ // emit `(...)` and we don't want a second pass to greedily convert
774
+ // its opening paren into `⁽` and orphan the closing one.
775
+ s = s.replace(/\^\s*\{([^{}]+)\}/g, (_, body: string) => convertScript(body, SUPERSCRIPT, '^'))
776
+ s = s.replace(/\^([A-Za-z0-9+\-=])/g, (raw, ch: string) => SUPERSCRIPT[ch] ?? raw)
777
+ s = s.replace(/_\s*\{([^{}]+)\}/g, (_, body: string) => convertScript(body, SUBSCRIPT, '_'))
778
+ s = s.replace(/_([A-Za-z0-9+\-=])/g, (raw, ch: string) => SUBSCRIPT[ch] ?? raw)
779
+
780
+ return s
781
+ }
782
+
783
+ // Inline-code spans: 1–2 backticks, non-greedy body, same-length closer.
784
+ // Captured so mathify can step over them without a placeholder dance.
785
+ const CODE_SPAN_RE = /(`{1,2})[^`\n]+?\1/g
786
+
787
+ // Math delimiters, tightest-to-loosest. `$$…$$` before `$…$` so the
788
+ // single-dollar branch never eats a display-math opener. `$…$` uses the
789
+ // same no-space-adjacent rule upstream uses to dodge `$5 to $10` prose.
790
+ const MATH_SPAN_RE = new RegExp(
791
+ [
792
+ String.raw`\$\$([^$]+?)\$\$`,
793
+ String.raw`\\\[([\s\S]+?)\\\]`,
794
+ String.raw`\\\(([^\n]+?)\\\)`,
795
+ String.raw`(?<!\$)\$([^\s$](?:[^$\n]*?[^\s$])?)\$(?!\$)`,
796
+ ].join('|'),
797
+ 'g',
798
+ )
799
+
800
+ // Apply texToUnicode only inside math delimiters. texToUnicode is a
801
+ // whole-string transform (it will turn prose `_n` into `ₙ`), so it must
802
+ // never see non-math text. Walk the string, skip inline-code spans
803
+ // verbatim, and rewrite math spans in the gaps.
804
+ export function mathify(md: string): string {
805
+ if (!/[$\\]/.test(md)) return md
806
+ let out = ''
807
+ let i = 0
808
+ CODE_SPAN_RE.lastIndex = 0
809
+ for (const m of md.matchAll(CODE_SPAN_RE)) {
810
+ out += md.slice(i, m.index).replace(MATH_SPAN_RE,
811
+ (_, a, b, c, d) => texToUnicode(a ?? b ?? c ?? d))
812
+ out += m[0]
813
+ i = m.index + m[0].length
814
+ }
815
+ out += md.slice(i).replace(MATH_SPAN_RE,
816
+ (_, a, b, c, d) => texToUnicode(a ?? b ?? c ?? d))
817
+ return out
818
+ }