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,266 @@
1
+ import { useState, useEffect, useCallback, useRef, memo, type ReactNode } from "react";
2
+ import { useGateway } from "../app/gateway";
3
+ import { useListKeys, useFollow } from "../keys";
4
+ import { useTheme } from "../theme";
5
+ import { useDialog } from "../ui/dialog";
6
+ import { useToast } from "../ui/toast";
7
+ import { TabShell } from "../ui/shell";
8
+ import { KVBlock } from "../ui/kv";
9
+ import { Col, Hdr, VBAR } from "../ui/table";
10
+
11
+ // ─── Types ────────────────────────────────────────────────────────────
12
+
13
+ // Wire shape from tui_gateway's `toolsets.list`. v1 only guarantees the
14
+ // first four; the rest are optional so a future tui_gateway that passes
15
+ // get_toolset_info()/get_available_toolsets() through lands here without
16
+ // a client change.
17
+ type Toolset = {
18
+ name: string
19
+ description: string
20
+ tool_count: number
21
+ enabled: boolean
22
+ tools?: string[]
23
+ includes?: string[]
24
+ available?: boolean
25
+ requirements?: string[]
26
+ }
27
+
28
+ type Kind = "core" | "platform" | "mcp"
29
+
30
+ // ─── Classification ───────────────────────────────────────────────────
31
+
32
+ // Name-only heuristic — tui_gateway doesn't expose includes/is_composite
33
+ // on the wire yet, so composites fold into their name-derived bucket.
34
+ // `hermes-*` are the per-platform bundles that all clone _HERMES_CORE_TOOLS
35
+ // upstream; separating them cuts ~20 near-identical rows out of "core".
36
+ const kindOf = (ts: Toolset): Kind =>
37
+ ts.name.includes(":") ? "mcp"
38
+ : ts.name.startsWith("hermes-") ? "platform"
39
+ : "core"
40
+
41
+ const KIND_LABEL: Record<Kind, string> = {
42
+ core: "core",
43
+ platform: "platform bundles",
44
+ mcp: "mcp",
45
+ }
46
+
47
+ type Section = { kind: Kind; items: Toolset[] }
48
+
49
+ const group = (list: Toolset[]): Section[] => {
50
+ const by = { core: [], platform: [], mcp: [] } as Record<Kind, Toolset[]>
51
+ for (const ts of list) by[kindOf(ts)].push(ts)
52
+ return (["core", "platform", "mcp"] as const)
53
+ .filter(k => by[k].length > 0)
54
+ .map(k => ({ kind: k, items: by[k] }))
55
+ }
56
+
57
+ // ─── Toolset Row ──────────────────────────────────────────────────────
58
+
59
+ const Row = memo((props: {
60
+ id: string;
61
+ ts: Toolset;
62
+ selected: boolean;
63
+ onSelect: () => void;
64
+ onHover: () => void;
65
+ }) => {
66
+ const theme = useTheme().theme;
67
+ const ts = props.ts;
68
+ const bg = props.selected ? theme.backgroundElement : undefined;
69
+ const unavail = ts.available === false;
70
+ const glyph = unavail ? "◌" : ts.enabled ? "●" : "○";
71
+ const glyphFg = unavail ? theme.warning : ts.enabled ? theme.success : theme.textMuted;
72
+
73
+ return (
74
+ <box id={props.id} flexDirection="row" height={1} backgroundColor={bg}
75
+ onMouseDown={props.onSelect} onMouseMove={props.onHover}>
76
+ <Col w={2} fg={props.selected ? theme.primary : theme.text}>{props.selected ? "▸ " : " "}</Col>
77
+ <Col w={2} fg={glyphFg}>{`${glyph} `}</Col>
78
+ <Col grow fg={props.selected ? theme.accent : theme.text}>{ts.name}</Col>
79
+ <Col w={9} fg={theme.info} right>{`${ts.tool_count} tools`}</Col>
80
+ </box>
81
+ );
82
+ });
83
+
84
+ // ─── Detail Panel ─────────────────────────────────────────────────────
85
+
86
+ const DetailPanel = memo((props: { ts: Toolset }) => {
87
+ const theme = useTheme().theme;
88
+ const ts = props.ts;
89
+ const unavail = ts.available === false;
90
+
91
+ return (
92
+ <box
93
+ flexDirection="column" padding={1} border
94
+ borderColor={theme.border} backgroundColor={theme.backgroundPanel} width="40%"
95
+ >
96
+ <box height={1}><text fg={theme.accent}><strong>{ts.name}</strong></text></box>
97
+ <box height={1}><text fg={theme.textMuted}>{KIND_LABEL[kindOf(ts)]}</text></box>
98
+ <box height={1} />
99
+ <KVBlock rows={[
100
+ ["Status", unavail ? "unavailable" : ts.enabled ? "enabled" : "disabled",
101
+ unavail ? theme.warning : ts.enabled ? theme.success : theme.textMuted],
102
+ ["Tools", String(ts.tool_count), theme.info],
103
+ ["Includes", ts.includes?.length ? ts.includes.join(", ") : undefined, theme.text],
104
+ ["Requires", ts.requirements?.length ? ts.requirements.join(", ") : undefined,
105
+ unavail ? theme.warning : theme.text],
106
+ ]} />
107
+ <box height={1} />
108
+ <box minHeight={1}>
109
+ <text wrapMode="word" fg={theme.text}>{ts.description || "—"}</text>
110
+ </box>
111
+ {ts.tools?.length ? (
112
+ <>
113
+ <box height={1} />
114
+ <box height={1}><text fg={theme.textMuted}>Tools ({ts.tools.length}):</text></box>
115
+ <scrollbox flexGrow={1} scrollY>
116
+ {ts.tools.map(n => <text key={n} fg={theme.text}>· {n}</text>)}
117
+ </scrollbox>
118
+ </>
119
+ ) : null}
120
+ </box>
121
+ );
122
+ });
123
+
124
+ // ─── Main Component ───────────────────────────────────────────────────
125
+
126
+ export const Toolsets = memo((props: { focused?: boolean }) => {
127
+ const theme = useTheme().theme;
128
+ const gw = useGateway();
129
+ const dialog = useDialog();
130
+ const toast = useToast();
131
+ const [list, setList] = useState<Toolset[]>([]);
132
+ const [sel, setSel] = useState(0);
133
+ const [err, setErr] = useState<string | null>(null);
134
+
135
+ // Flat nav list derived from grouped sections, so ↑/↓ crosses section
136
+ // boundaries in render order. `live` mirrors for callbacks that must
137
+ // read current selection without closing over stale state.
138
+ const secs = group(list);
139
+ const flat = secs.flatMap(s => s.items);
140
+ const live = useRef({ flat, sel });
141
+ live.current = { flat, sel };
142
+
143
+ const load = useCallback(() => {
144
+ gw.request<{ toolsets?: Toolset[] }>("toolsets.list", {})
145
+ .then(r => { setList(r.toolsets ?? []); setErr(null); })
146
+ .catch(e => setErr(e instanceof Error ? e.message : String(e)));
147
+ }, [gw]);
148
+
149
+ useEffect(() => { load(); }, [load]);
150
+
151
+ // tools.configure response (tui_gateway/server.py @method("tools.configure")).
152
+ // `enabled_toolsets` is the authoritative post-change list — reconcile
153
+ // from it instead of a round-trip `toolsets.list`. `unknown` = names
154
+ // the gateway's whitelist rejected (e.g. platform bundles, composites).
155
+ type ConfigureResult = {
156
+ changed: string[]
157
+ enabled_toolsets: string[]
158
+ missing_servers: string[]
159
+ unknown: string[]
160
+ }
161
+
162
+ const toggle = useCallback(() => {
163
+ const ts = live.current.flat[live.current.sel];
164
+ if (!ts) return;
165
+ if (ts.available === false) {
166
+ toast.show({ variant: "warning", message: `${ts.name} is unavailable` });
167
+ return;
168
+ }
169
+ const action = ts.enabled ? "disable" : "enable";
170
+ const was = ts.enabled;
171
+ // optimistic flip
172
+ setList(prev => prev.map(t => t.name === ts.name ? { ...t, enabled: !t.enabled } : t));
173
+ gw.request<ConfigureResult>("tools.configure", { action, names: [ts.name] })
174
+ .then(r => {
175
+ if (r.unknown?.includes(ts.name)) {
176
+ // Gateway rejected the name — revert the optimistic flip and tell
177
+ // the user why (matches config.yaml whitelist in hermes_cli/tools_config.py).
178
+ setList(prev => prev.map(t => t.name === ts.name ? { ...t, enabled: was } : t));
179
+ toast.show({ variant: "warning", message: `${ts.name} is not configurable` });
180
+ return;
181
+ }
182
+ if (r.missing_servers?.length && ts.name.includes(":")) {
183
+ const server = ts.name.split(":", 1)[0];
184
+ if (r.missing_servers.includes(server)) {
185
+ setList(prev => prev.map(t => t.name === ts.name ? { ...t, enabled: was } : t));
186
+ toast.show({ variant: "warning", message: `MCP server '${server}' not in config` });
187
+ return;
188
+ }
189
+ }
190
+ // Reconcile from the authoritative list. Tolerant of future shape
191
+ // changes (missing `enabled_toolsets` falls back to `load()`).
192
+ if (Array.isArray(r.enabled_toolsets)) {
193
+ const on = new Set(r.enabled_toolsets);
194
+ setList(prev => prev.map(t => ({ ...t, enabled: on.has(t.name) })));
195
+ } else {
196
+ load();
197
+ }
198
+ })
199
+ .catch((e: Error) => {
200
+ setList(prev => prev.map(t => t.name === ts.name ? { ...t, enabled: was } : t));
201
+ toast.show({ variant: "error", message: e.message });
202
+ });
203
+ }, [gw, toast, load]);
204
+
205
+ const count = flat.length;
206
+ const ts = flat[sel] ?? null;
207
+ const follow = useFollow("ts");
208
+
209
+ const keys = useListKeys({
210
+ active: !!props.focused && dialog.stack.length === 0,
211
+ count, setSel, ...follow.opts,
212
+ onToggle: toggle,
213
+ onRefresh: () => { load(); toast.show({ variant: "info", message: "Reloaded", duration: 1000 }) },
214
+ });
215
+
216
+ return (
217
+ <box flexDirection="row" flexGrow={1}>
218
+ <TabShell
219
+ title={`Toolsets (${count})`}
220
+ error={err}
221
+ hint={`↑↓ nav ${keys.print("list.toggle")} toggle ${keys.print("list.refresh")} refresh`}
222
+ >
223
+ <Hdr>
224
+ <Col w={4} fg={theme.textMuted}>{""}</Col>
225
+ <Col grow fg={theme.textMuted} bold>Name</Col>
226
+ <Col w={9} fg={theme.textMuted} bold right>Tools</Col>
227
+ </Hdr>
228
+ <box height={1} />
229
+
230
+ {count === 0 ? (
231
+ <box key="empty" flexGrow={1} padding={2}>
232
+ <text fg={theme.textMuted}>No toolsets found</text>
233
+ </box>
234
+ ) : (
235
+ <scrollbox ref={follow.ref} key="list" scrollY flexGrow={1}
236
+ verticalScrollbarOptions={VBAR}>
237
+ {secs.reduce<{ base: number; out: ReactNode[] }>((acc, s) => {
238
+ acc.out.push(
239
+ <box key={`§${s.kind}`} height={1} marginTop={acc.base > 0 ? 1 : 0}>
240
+ <text fg={theme.textMuted}>─ {KIND_LABEL[s.kind]} ({s.items.length}) </text>
241
+ </box>
242
+ );
243
+ s.items.forEach((t, j) => {
244
+ const i = acc.base + j;
245
+ acc.out.push(
246
+ <Row
247
+ key={t.name}
248
+ id={follow.id(i)}
249
+ ts={t}
250
+ selected={i === sel}
251
+ onSelect={() => setSel(i)}
252
+ onHover={() => setSel(i)}
253
+ />
254
+ );
255
+ });
256
+ acc.base += s.items.length;
257
+ return acc;
258
+ }, { base: 0, out: [] }).out}
259
+ </scrollbox>
260
+ )}
261
+ </TabShell>
262
+
263
+ {ts ? <DetailPanel ts={ts} /> : null}
264
+ </box>
265
+ );
266
+ });
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Built-in themes — imported statically from JSON files.
3
+ * Same set as OpenCode.
4
+ */
5
+
6
+ import type { ThemeJson } from "./types";
7
+
8
+ import aura from "./themes/aura.json";
9
+ import ayu from "./themes/ayu.json";
10
+ import carbonfox from "./themes/carbonfox.json";
11
+ import catppuccin from "./themes/catppuccin.json";
12
+ import catppuccinFrappe from "./themes/catppuccin-frappe.json";
13
+ import catppuccinMacchiato from "./themes/catppuccin-macchiato.json";
14
+ import cobalt2 from "./themes/cobalt2.json";
15
+ import cursor from "./themes/cursor.json";
16
+ import dracula from "./themes/dracula.json";
17
+ import everforest from "./themes/everforest.json";
18
+ import flexoki from "./themes/flexoki.json";
19
+ import github from "./themes/github.json";
20
+ import gruvbox from "./themes/gruvbox.json";
21
+ import kanagawa from "./themes/kanagawa.json";
22
+ import lucentOrng from "./themes/lucent-orng.json";
23
+ import material from "./themes/material.json";
24
+ import matrix from "./themes/matrix.json";
25
+ import mercury from "./themes/mercury.json";
26
+ import monokai from "./themes/monokai.json";
27
+ import nightowl from "./themes/nightowl.json";
28
+ import nord from "./themes/nord.json";
29
+ import oneDark from "./themes/one-dark.json";
30
+ import opencode from "./themes/opencode.json";
31
+ import orng from "./themes/orng.json";
32
+ import osakaJade from "./themes/osaka-jade.json";
33
+ import palenight from "./themes/palenight.json";
34
+ import rosepine from "./themes/rosepine.json";
35
+ import solarized from "./themes/solarized.json";
36
+ import synthwave84 from "./themes/synthwave84.json";
37
+ import tokyonight from "./themes/tokyonight.json";
38
+ import vercel from "./themes/vercel.json";
39
+ import vesper from "./themes/vesper.json";
40
+ import zenburn from "./themes/zenburn.json";
41
+
42
+ export const DEFAULT_THEMES: Record<string, ThemeJson> = {
43
+ aura: aura as ThemeJson,
44
+ ayu: ayu as ThemeJson,
45
+ carbonfox: carbonfox as ThemeJson,
46
+ catppuccin: catppuccin as ThemeJson,
47
+ "catppuccin-frappe": catppuccinFrappe as ThemeJson,
48
+ "catppuccin-macchiato": catppuccinMacchiato as ThemeJson,
49
+ cobalt2: cobalt2 as ThemeJson,
50
+ cursor: cursor as ThemeJson,
51
+ dracula: dracula as ThemeJson,
52
+ everforest: everforest as ThemeJson,
53
+ flexoki: flexoki as ThemeJson,
54
+ github: github as ThemeJson,
55
+ gruvbox: gruvbox as ThemeJson,
56
+ kanagawa: kanagawa as ThemeJson,
57
+ "lucent-orng": lucentOrng as ThemeJson,
58
+ material: material as ThemeJson,
59
+ matrix: matrix as ThemeJson,
60
+ mercury: mercury as ThemeJson,
61
+ monokai: monokai as ThemeJson,
62
+ nightowl: nightowl as ThemeJson,
63
+ nord: nord as ThemeJson,
64
+ "one-dark": oneDark as ThemeJson,
65
+ opencode: opencode as ThemeJson,
66
+ orng: orng as ThemeJson,
67
+ "osaka-jade": osakaJade as ThemeJson,
68
+ palenight: palenight as ThemeJson,
69
+ rosepine: rosepine as ThemeJson,
70
+ solarized: solarized as ThemeJson,
71
+ synthwave84: synthwave84 as ThemeJson,
72
+ tokyonight: tokyonight as ThemeJson,
73
+ vercel: vercel as ThemeJson,
74
+ vesper: vesper as ThemeJson,
75
+ zenburn: zenburn as ThemeJson,
76
+ };
77
+
78
+ export const DEFAULT_THEME = "tokyonight";
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Theme React context — provides resolved theme to all components.
3
+ *
4
+ * Usage:
5
+ * // In app root:
6
+ * <ThemeProvider><App /></ThemeProvider>
7
+ *
8
+ * // In any component:
9
+ * const { theme, name, set, names } = useTheme();
10
+ * <box backgroundColor={theme.backgroundPanel}>
11
+ * <text fg={theme.text}>
12
+ */
13
+
14
+ import { createContext, useContext, useState, useCallback, useMemo } from "react";
15
+ import type { ReactNode } from "react";
16
+ import type { SyntaxStyle } from "@opentui/core";
17
+ import type { Theme, ThemeJson } from "./types";
18
+ import { resolveTheme } from "./resolve";
19
+ import { DEFAULT_THEMES, DEFAULT_THEME } from "./builtin";
20
+ import { syntax } from "./syntax";
21
+ import * as preferences from "../utils/preferences";
22
+
23
+ interface ThemeContext {
24
+ /** Resolved theme — all RGBA values ready for JSX props */
25
+ theme: Theme;
26
+ /** SyntaxStyle for code/markdown rendering */
27
+ syntaxStyle: SyntaxStyle;
28
+ /** Currently active theme name */
29
+ name: string;
30
+ /** Dark or light mode */
31
+ mode: "dark" | "light";
32
+ /** Switch to a theme by name. Returns false if not found. */
33
+ set: (name: string) => boolean;
34
+ /** All available theme names, sorted */
35
+ names: string[];
36
+ /** Check if a theme exists */
37
+ has: (name: string) => boolean;
38
+ }
39
+
40
+ const Ctx = createContext<ThemeContext | null>(null);
41
+
42
+ interface ThemeProviderProps {
43
+ children: ReactNode;
44
+ initial?: string;
45
+ mode?: "dark" | "light";
46
+ }
47
+
48
+ export const ThemeProvider = ({
49
+ children,
50
+ initial,
51
+ mode: initialMode = "dark",
52
+ }: ThemeProviderProps) => {
53
+ const [active, setActive] = useState(initial ?? DEFAULT_THEME);
54
+ const [mode] = useState(initialMode);
55
+ const [themes] = useState<Record<string, ThemeJson>>(DEFAULT_THEMES);
56
+
57
+ const resolved = useMemo(() => {
58
+ const json = themes[active] ?? themes[DEFAULT_THEME];
59
+ try {
60
+ return resolveTheme(json, mode);
61
+ } catch {
62
+ return resolveTheme(themes[DEFAULT_THEME], mode);
63
+ }
64
+ }, [active, mode, themes]);
65
+
66
+ const names = useMemo(
67
+ () => Object.keys(themes).sort(),
68
+ [themes],
69
+ );
70
+
71
+ const set = useCallback(
72
+ (name: string) => {
73
+ if (!themes[name]) return false;
74
+ setActive(name);
75
+ preferences.set("theme", name);
76
+ return true;
77
+ },
78
+ [themes],
79
+ );
80
+
81
+ const has = useCallback(
82
+ (name: string) => themes[name] !== undefined,
83
+ [themes],
84
+ );
85
+
86
+ const syntaxStyle = useMemo(() => syntax(resolved), [resolved]);
87
+
88
+ const value = useMemo<ThemeContext>(() => ({
89
+ theme: resolved,
90
+ syntaxStyle,
91
+ name: active,
92
+ mode,
93
+ set,
94
+ names,
95
+ has,
96
+ }), [resolved, syntaxStyle, active, mode, set, names, has]);
97
+
98
+ return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
99
+ };
100
+
101
+ /** Access the current theme. Must be inside <ThemeProvider>. */
102
+ export const useTheme = (): ThemeContext => {
103
+ const ctx = useContext(Ctx);
104
+ if (!ctx) throw new Error("useTheme() must be used inside <ThemeProvider>");
105
+ return ctx;
106
+ };
@@ -0,0 +1,4 @@
1
+ export { ThemeProvider, useTheme } from "./context";
2
+ export { resolveTheme } from "./resolve";
3
+ export { DEFAULT_THEMES, DEFAULT_THEME } from "./builtin";
4
+ export type { ThemeCurrent, Theme, ThemeJson, ThemeColor } from "./types";
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Theme resolver — takes a ThemeJson + mode, returns a fully resolved Theme.
3
+ *
4
+ * Handles: hex colors, defs references, cross-token references,
5
+ * dark/light variants, ANSI codes, transparent/none, circular detection.
6
+ */
7
+
8
+ import { RGBA } from "@opentui/core";
9
+ import type { Theme, ThemeJson, ThemeColor, ColorValue, HermTokens } from "./types";
10
+
11
+ /** Resolve a ThemeJson into a concrete Theme with all RGBA values */
12
+ export function resolveTheme(
13
+ theme: ThemeJson,
14
+ mode: "dark" | "light",
15
+ ): Theme {
16
+ const defs = theme.defs ?? {};
17
+
18
+ function resolve(c: ColorValue, chain: string[] = []): RGBA {
19
+ if (c instanceof RGBA) return c;
20
+
21
+ if (typeof c === "string") {
22
+ if (c === "transparent" || c === "none") return RGBA.fromInts(0, 0, 0, 0);
23
+ if (c.startsWith("#")) return RGBA.fromHex(c);
24
+
25
+ // Reference — check for circular
26
+ if (chain.includes(c)) {
27
+ throw new Error(`Circular color reference: ${[...chain, c].join(" -> ")}`);
28
+ }
29
+ const next = defs[c] ?? theme.theme[c as ThemeColor];
30
+ if (next === undefined) {
31
+ throw new Error(`Color reference "${c}" not found in defs or theme`);
32
+ }
33
+ return resolve(next, [...chain, c]);
34
+ }
35
+
36
+ if (typeof c === "number") return ansiToRgba(c);
37
+
38
+ // Variant — pick dark or light
39
+ return resolve(c[mode], chain);
40
+ }
41
+
42
+ const resolved = Object.fromEntries(
43
+ Object.entries(theme.theme)
44
+ .filter(
45
+ ([key]) =>
46
+ key !== "selectedListItemText" &&
47
+ key !== "backgroundMenu" &&
48
+ key !== "thinkingOpacity",
49
+ )
50
+ .map(([key, value]) => [key, resolve(value as ColorValue)]),
51
+ ) as Partial<Record<ThemeColor, RGBA>>;
52
+
53
+ // Optional tokens with fallbacks
54
+ const hasList = theme.theme.selectedListItemText !== undefined;
55
+ resolved.selectedListItemText = hasList
56
+ ? resolve(theme.theme.selectedListItemText!)
57
+ : resolved.background;
58
+
59
+ resolved.backgroundMenu =
60
+ theme.theme.backgroundMenu !== undefined
61
+ ? resolve(theme.theme.backgroundMenu)
62
+ : resolved.backgroundElement;
63
+
64
+ const base = {
65
+ ...resolved,
66
+ _hasSelectedListItemText: hasList,
67
+ thinkingOpacity: theme.theme.thinkingOpacity ?? 0.6,
68
+ };
69
+
70
+ // Compute herm-specific tokens (accent is a required token)
71
+ const herm = resolveHermTokens(resolved.accent!);
72
+
73
+ return { ...base, ...herm } as Theme;
74
+ }
75
+
76
+ /**
77
+ * Compute herm pillar tokens.
78
+ * hermAvatar and hermBody default to accent.
79
+ * hermBodyText is auto-computed for legibility against hermBody.
80
+ */
81
+ function resolveHermTokens(accent: RGBA): HermTokens {
82
+ const text = contrastText(accent);
83
+ return {
84
+ hermAvatar: accent,
85
+ hermBody: accent,
86
+ hermBodyText: text,
87
+ hermBodyTextMuted: mix(text, accent, 0.45),
88
+ };
89
+ }
90
+
91
+ /** Linear mix a→b by t (0=a, 1=b). */
92
+ function mix(a: RGBA, b: RGBA, t: number): RGBA {
93
+ const l = (x: number, y: number) => x + (y - x) * t;
94
+ return RGBA.fromValues(l(a.r, b.r), l(a.g, b.g), l(a.b, b.b), 1);
95
+ }
96
+
97
+ /**
98
+ * Pick black or white text for maximum legibility on a background.
99
+ * Uses WCAG relative luminance formula.
100
+ */
101
+ function contrastText(bg: RGBA): RGBA {
102
+ // RGBA stores 0-1 floats
103
+ const lum = 0.299 * bg.r + 0.587 * bg.g + 0.114 * bg.b;
104
+ return lum > 0.5
105
+ ? RGBA.fromInts(0, 0, 0) // dark text on light bg
106
+ : RGBA.fromInts(255, 255, 255); // light text on dark bg
107
+ }
108
+
109
+ /** Convert ANSI 256-color code to RGBA */
110
+ function ansiToRgba(code: number): RGBA {
111
+ if (code < 16) {
112
+ const colors = [
113
+ "#000000", "#800000", "#008000", "#808000",
114
+ "#000080", "#800080", "#008080", "#c0c0c0",
115
+ "#808080", "#ff0000", "#00ff00", "#ffff00",
116
+ "#0000ff", "#ff00ff", "#00ffff", "#ffffff",
117
+ ];
118
+ return RGBA.fromHex(colors[code] ?? "#000000");
119
+ }
120
+ if (code < 232) {
121
+ const idx = code - 16;
122
+ const val = (x: number) => (x === 0 ? 0 : x * 40 + 55);
123
+ return RGBA.fromInts(
124
+ val(Math.floor(idx / 36)),
125
+ val(Math.floor(idx / 6) % 6),
126
+ val(idx % 6),
127
+ );
128
+ }
129
+ if (code < 256) {
130
+ const gray = (code - 232) * 10 + 8;
131
+ return RGBA.fromInts(gray, gray, gray);
132
+ }
133
+ return RGBA.fromInts(0, 0, 0);
134
+ }
@@ -0,0 +1,31 @@
1
+ import { SyntaxStyle, type ThemeTokenStyle } from "@opentui/core"
2
+ import type { Theme } from "./types"
3
+
4
+ export function syntax(theme: Theme): SyntaxStyle {
5
+ return SyntaxStyle.fromTheme(rules(theme))
6
+ }
7
+
8
+ function rules(theme: Theme): ThemeTokenStyle[] {
9
+ return [
10
+ { scope: ["default"], style: { foreground: theme.text } },
11
+ { scope: ["comment", "comment.documentation"], style: { foreground: theme.syntaxComment, italic: true } },
12
+ { scope: ["string", "symbol"], style: { foreground: theme.syntaxString } },
13
+ { scope: ["number", "boolean"], style: { foreground: theme.syntaxNumber } },
14
+ { scope: ["keyword"], style: { foreground: theme.syntaxKeyword, italic: true } },
15
+ { scope: ["keyword.return", "keyword.conditional", "keyword.repeat"], style: { foreground: theme.syntaxKeyword, italic: true } },
16
+ { scope: ["keyword.type"], style: { foreground: theme.syntaxType, bold: true } },
17
+ { scope: ["keyword.function", "function.method"], style: { foreground: theme.syntaxFunction } },
18
+ { scope: ["function", "function.call"], style: { foreground: theme.syntaxFunction } },
19
+ { scope: ["variable", "variable.parameter"], style: { foreground: theme.syntaxVariable } },
20
+ { scope: ["type"], style: { foreground: theme.syntaxType } },
21
+ { scope: ["operator"], style: { foreground: theme.syntaxOperator } },
22
+ { scope: ["punctuation", "punctuation.bracket", "punctuation.delimiter"], style: { foreground: theme.syntaxPunctuation } },
23
+ // Markdown-specific
24
+ { scope: ["markup.heading"], style: { foreground: theme.markdownHeading, bold: true } },
25
+ { scope: ["markup.strong"], style: { foreground: theme.markdownStrong, bold: true } },
26
+ { scope: ["markup.italic"], style: { foreground: theme.markdownEmph, italic: true } },
27
+ { scope: ["markup.link"], style: { foreground: theme.markdownLink, underline: true } },
28
+ { scope: ["markup.raw"], style: { foreground: theme.markdownCode } },
29
+ { scope: ["markup.list"], style: { foreground: theme.markdownListItem } },
30
+ ]
31
+ }