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,151 @@
1
+ /**
2
+ * Slash command definitions for the chat input.
3
+ *
4
+ * Commands are fetched dynamically from Hermes `GET /api/commands` — the
5
+ * unified registry of built-ins + skills + plugins + MCP prompts. There is
6
+ * no static fallback: if the gateway is unreachable, no slash commands are
7
+ * available (the popover simply won't open).
8
+ *
9
+ * `target` is derived: "local" if the name is a client-handled command,
10
+ * otherwise "gateway" (forwarded as /{name} to the Hermes API).
11
+ */
12
+
13
+ export type SlashSource = "command" | "skill" | "plugin" | "mcp" | "local"
14
+
15
+ export type SlashCommand = {
16
+ readonly name: string
17
+ readonly description: string
18
+ readonly category: string
19
+ readonly aliases: ReadonlyArray<string>
20
+ readonly argsHint: string
21
+ readonly subcommands: ReadonlyArray<string>
22
+ readonly source: SlashSource
23
+ readonly target: "local" | "gateway"
24
+ readonly keybind?: string
25
+ }
26
+
27
+ /**
28
+ * Names of purely client-side commands — intercepted before gateway dispatch.
29
+ * These are always treated as local regardless of what the gateway returns.
30
+ * Anything that must act on the *live* gateway session (not the slash-worker
31
+ * subprocess) belongs here — see docs/slash-parity-audit.md.
32
+ */
33
+ export const LOCAL_NAMES = new Set([
34
+ "clear", "new", "theme", "help", "keys", "logs", "eikon", "title",
35
+ "rollback", "save", "history", "status", "usage", "profile", "steer",
36
+ "reload", "reload-mcp", "chafa", "splash",
37
+ // parity: session-mutating commands the slash-worker can't service
38
+ "resume", "branch", "compress", "undo", "retry", "model", "quit",
39
+ "copy", "paste", "image", "background", "voice", "mouse", "redraw", "queue",
40
+ // Ink-only UI toggles — local no-op with a note
41
+ "compact", "setup",
42
+ ])
43
+
44
+ /**
45
+ * Descriptions for locally-handled commands. Used to render them in the
46
+ * popover when the gateway registry doesn't include them (or to override
47
+ * the gateway's description for things like /new, which we intercept).
48
+ */
49
+ export const LOCAL_COMMANDS: ReadonlyArray<SlashCommand> = [
50
+ { name: "clear", description: "Clear chat messages", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
51
+ { name: "new", description: "Start a new session", category: "Client", aliases: ["reset"], argsHint: "", subcommands: [], source: "local", target: "local" },
52
+ { name: "theme", description: "Switch color theme", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
53
+ { name: "help", description: "Show keyboard shortcuts", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
54
+ { name: "keys", description: "Rebind keyboard shortcuts", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
55
+ { name: "logs", description: "Show gateway stderr log", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
56
+ { name: "eikon", description: "Pick sidebar avatar", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
57
+ { name: "title", description: "Set session title", category: "Client", aliases: [], argsHint: "[text]", subcommands: [], source: "local", target: "local" },
58
+ { name: "rollback", description: "Browse & restore checkpoints", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
59
+ { name: "history", description: "Server-side transcript viewer", category: "Info", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
60
+ { name: "status", description: "Version, model, paths", category: "Info", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
61
+ { name: "usage", description: "Tokens, context fill, cost", category: "Info", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
62
+ { name: "profile", description: "Active profile details", category: "Info", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
63
+ { name: "steer", description: "Inject a note mid-turn (no interrupt)", category: "Session", aliases: [], argsHint: "[text]", subcommands: [], source: "local", target: "local" },
64
+ { name: "reload-mcp", description: "Restart MCP servers & rediscover tools", category: "Session", aliases: [], argsHint: "[now|always]", subcommands: ["now", "always"], source: "local", target: "local" },
65
+ { name: "reload", description: "Hot-reload ~/.hermes/.env (API keys)", category: "Session", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
66
+ { name: "chafa", description: "Render image via chafa (demo)", category: "Client", aliases: [], argsHint: "<path>", subcommands: [], source: "local", target: "local" },
67
+ { name: "splash", description: "Show the launch splash", category: "Client", aliases: [], argsHint: "", subcommands: [], source: "local", target: "local" },
68
+ ]
69
+
70
+ /** Filter commands by prefix (text after `/`). Searches names + aliases. */
71
+ export function filter(list: ReadonlyArray<SlashCommand>, prefix: string): SlashCommand[] {
72
+ if (!prefix) return [...list]
73
+ const q = prefix.toLowerCase()
74
+ return list.filter(c =>
75
+ c.name.toLowerCase().startsWith(q) ||
76
+ c.aliases.some(a => a.toLowerCase().startsWith(q))
77
+ )
78
+ }
79
+
80
+ type Resolved =
81
+ | { hit: SlashCommand }
82
+ | { miss: true }
83
+ | { ambiguous: string[] }
84
+
85
+ /**
86
+ * Resolve a typed command name to a single SlashCommand. Exact name or
87
+ * alias wins even when it's also a prefix of something longer (`/status`
88
+ * vs `/statusbar`). Otherwise a unique prefix match across the names +
89
+ * aliases space resolves; multiple distinct targets is ambiguous.
90
+ */
91
+ export function resolve(list: ReadonlyArray<SlashCommand>, name: string): Resolved {
92
+ const q = name.toLowerCase()
93
+ for (const c of list)
94
+ if (c.name.toLowerCase() === q || c.aliases.some(a => a.toLowerCase() === q))
95
+ return { hit: c }
96
+ const hits = new Set<SlashCommand>()
97
+ for (const c of list)
98
+ for (const n of [c.name, ...c.aliases])
99
+ if (n.toLowerCase().startsWith(q)) { hits.add(c); break }
100
+ if (hits.size === 1) return { hit: [...hits][0] }
101
+ if (hits.size === 0) return { miss: true }
102
+ return { ambiguous: [...hits].map(c => `/${c.name}`).sort() }
103
+ }
104
+
105
+ /**
106
+ * If input matches `/cmd <sub>` (with space) and the command has declared
107
+ * subcommands, return synthetic entries for subcommand completion.
108
+ */
109
+ export function matchSub(list: ReadonlyArray<SlashCommand>, input: string): SlashCommand[] | null {
110
+ const m = input.match(/^\/(\w+)\s+(\S*)$/)
111
+ if (!m) return null
112
+ const name = m[1]
113
+ const sub = m[2]
114
+ const cmd = list.find(c => c.name === name || c.aliases.includes(name))
115
+ if (!cmd || cmd.subcommands.length === 0) return null
116
+ const q = sub.toLowerCase()
117
+ const matches = cmd.subcommands.filter(s => s.toLowerCase().startsWith(q))
118
+ if (matches.length === 0) return null
119
+ return matches.map(s => ({
120
+ ...cmd,
121
+ name: `${cmd.name} ${s}`,
122
+ description: `${cmd.name} → ${s}`,
123
+ argsHint: "",
124
+ subcommands: [],
125
+ }))
126
+ }
127
+
128
+ /** Category ordering for display. Unknown categories fall to the end. */
129
+ const CATEGORY_ORDER = [
130
+ "Client",
131
+ "Session",
132
+ "Configuration",
133
+ "Config",
134
+ "Tools & Skills",
135
+ "Skills",
136
+ "Plugins",
137
+ "MCP",
138
+ "Info",
139
+ "Exit",
140
+ ] as const
141
+
142
+ export function sort(list: ReadonlyArray<SlashCommand>): SlashCommand[] {
143
+ const idx = (c: string) => {
144
+ const i = (CATEGORY_ORDER as readonly string[]).indexOf(c)
145
+ return i < 0 ? 999 : i
146
+ }
147
+ return [...list].sort((a, b) => {
148
+ const ca = idx(a.category) - idx(b.category)
149
+ return ca !== 0 ? ca : a.name.localeCompare(b.name)
150
+ })
151
+ }
@@ -0,0 +1,66 @@
1
+ import { useState, useEffect, useRef, memo } from "react"
2
+ import { STATE_FRAMES, FPS, type AvatarState } from "./states"
3
+ import type { ParsedEikon } from "./eikon"
4
+ import { useTheme } from "../../theme"
5
+ import * as perf from "../../utils/perf"
6
+
7
+ /**
8
+ * Animation loop:
9
+ * 1. Pause on frame 0 for PAUSE_FIRST ms
10
+ * 2. Play forward (0 → last) at FPS
11
+ * 3. Pause on last frame for PAUSE_LAST ms
12
+ * 4. Play reverse (last → 0) at FPS
13
+ * 5. Goto 1
14
+ *
15
+ * Restarts from frame 0 whenever `state` changes.
16
+ *
17
+ * When an `eikon` is supplied and it defines the current state, its
18
+ * frames and fps take precedence over the baked-in set; states missing
19
+ * from the eikon fall back to the baked-in frames.
20
+ */
21
+
22
+ export const AnimatedAvatar = memo(({ state = "idle", eikon }: { state?: AvatarState; eikon?: ParsedEikon }) => {
23
+ const theme = useTheme().theme
24
+ const [frame, setFrame] = useState(0)
25
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
26
+
27
+ const ext = eikon?.states.get(state)
28
+ const frames: string[][] = ext ? ext.frames : STATE_FRAMES[state]
29
+ const fps = ext?.fps ?? FPS
30
+ const count = frames.length
31
+
32
+ // Restart animation loop when state or source changes
33
+ useEffect(() => {
34
+ let idx = 0
35
+ let dir = 1
36
+
37
+ const tick = () => {
38
+ perf.count("avatar:tick")
39
+ idx += dir
40
+ if (idx >= count - 1) dir = -1
41
+ if (idx <= 0) dir = 1
42
+ setFrame(idx)
43
+ timer.current = setTimeout(tick, 1000 / fps)
44
+ }
45
+
46
+ setFrame(0)
47
+ timer.current = setTimeout(tick, 1000 / fps)
48
+ return () => { if (timer.current) clearTimeout(timer.current) }
49
+ }, [state, count, fps])
50
+
51
+ const end = perf.mark("avatar:render")
52
+ const idx = Math.min(frame, count - 1)
53
+ const lines = frames[idx] ?? []
54
+
55
+ const result = (
56
+ <box flexDirection="column">
57
+ {lines.map((line, i) => (
58
+ <text key={i}>
59
+ <span fg={theme.hermAvatar}>{line}</span>
60
+ </text>
61
+ ))}
62
+ </box>
63
+ )
64
+ end()
65
+ return result
66
+ })
@@ -0,0 +1,144 @@
1
+ /**
2
+ * .eikon file parser — NDJSON stateful ASCII animation format.
3
+ *
4
+ * Line 1 is a header object. Subsequent lines are either state
5
+ * declarations (`{state, fps, frame_count, ...}`) or frame objects
6
+ * (`{f, data, ...}`) belonging to the most recent state. Unknown
7
+ * fields are ignored; unknown format versions are tolerated.
8
+ *
9
+ * Spec: https://github.com/liftaris/eikon (separate repo).
10
+ */
11
+
12
+ import { readdirSync, openSync, readSync, closeSync } from "fs"
13
+ import { join } from "path"
14
+
15
+ export type EikonMeta = {
16
+ version: number
17
+ name: string
18
+ author?: string
19
+ width: number
20
+ height: number
21
+ states: string[]
22
+ [k: string]: unknown
23
+ }
24
+
25
+ export type EikonState = {
26
+ fps: number
27
+ /** Each frame as an array of lines (row-per-string). */
28
+ frames: string[][]
29
+ }
30
+
31
+ export type ParsedEikon = {
32
+ meta: EikonMeta
33
+ states: Map<string, EikonState>
34
+ }
35
+
36
+ type Row = Record<string, unknown>
37
+
38
+ const num = (v: unknown, d: number) => typeof v === "number" && isFinite(v) ? v : d
39
+ const str = (v: unknown, d = "") => typeof v === "string" ? v : d
40
+
41
+ function parse(line: string, n: number): Row {
42
+ try { return JSON.parse(line) as Row }
43
+ catch (e) {
44
+ const msg = e instanceof Error ? e.message : String(e)
45
+ throw new Error(`eikon: malformed JSON on line ${n}: ${msg}`)
46
+ }
47
+ }
48
+
49
+ function median(xs: number[]): number {
50
+ const s = [...xs].sort((a, b) => a - b)
51
+ const m = s.length >> 1
52
+ return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2
53
+ }
54
+
55
+ /** Parse a full .eikon NDJSON document. Throws with line number on bad JSON. */
56
+ export function parseEikon(text: string): ParsedEikon {
57
+ const lines = text.split("\n")
58
+ if (!lines[0]?.trim()) throw new Error("eikon: empty file (no header on line 1)")
59
+
60
+ const head = parse(lines[0], 1)
61
+ const meta: EikonMeta = {
62
+ ...head,
63
+ version: num(head.eikon ?? head.version, 1),
64
+ name: str(head.name, "unnamed"),
65
+ author: typeof head.author === "string" ? head.author : undefined,
66
+ width: num(head.width, 0),
67
+ height: num(head.height, 0),
68
+ states: Array.isArray(head.states) ? (head.states as string[]) : [],
69
+ }
70
+
71
+ const states = new Map<string, EikonState>()
72
+ let cur: { name: string; fps?: number; frames: string[][]; durs: number[] } | null = null
73
+
74
+ const seal = () => {
75
+ if (!cur) return
76
+ const fps = cur.fps ?? (cur.durs.length ? Math.round(1000 / median(cur.durs)) || 12 : 12)
77
+ states.set(cur.name, { fps, frames: cur.frames })
78
+ cur = null
79
+ }
80
+
81
+ for (let i = 1; i < lines.length; i++) {
82
+ const raw = lines[i]
83
+ if (!raw || !raw.trim()) continue
84
+ const obj = parse(raw, i + 1)
85
+
86
+ if (typeof obj.state === "string") {
87
+ seal()
88
+ cur = {
89
+ name: obj.state,
90
+ fps: typeof obj.fps === "number" ? obj.fps : undefined,
91
+ frames: [],
92
+ durs: [],
93
+ }
94
+ continue
95
+ }
96
+
97
+ // Frame line — attach to current state.
98
+ if (!cur) continue
99
+ const data = typeof obj.data === "string"
100
+ ? obj.data.split("\n")
101
+ : Array.isArray(obj.lines) ? (obj.lines as string[]) : []
102
+ cur.frames.push(data)
103
+ const ms = num(obj.duration_ms, 0) || num(obj.pause, 0) * 1000
104
+ if (ms > 0) cur.durs.push(ms)
105
+ }
106
+ seal()
107
+
108
+ if (meta.states.length === 0) meta.states = Array.from(states.keys())
109
+ return { meta, states }
110
+ }
111
+
112
+ /** Read just the header (line 1) of a .eikon file without loading the rest. */
113
+ function peek(path: string): EikonMeta | null {
114
+ const fd = openSync(path, "r")
115
+ try {
116
+ const buf = Buffer.alloc(8192)
117
+ const n = readSync(fd, buf, 0, buf.length, 0)
118
+ const first = buf.toString("utf8", 0, n).split("\n", 1)[0]
119
+ if (!first) return null
120
+ return parseEikon(first).meta
121
+ } catch {
122
+ return null
123
+ } finally {
124
+ closeSync(fd)
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Scan directories (non-recursive per dir, but walks one level deep) for
130
+ * `*.eikon` files and return their header metadata. Missing dirs are
131
+ * silently skipped.
132
+ */
133
+ export function listEikons(dirs: string[]): { path: string; meta: EikonMeta }[] {
134
+ return dirs.flatMap(dir => {
135
+ let entries: string[]
136
+ try { entries = readdirSync(dir, { recursive: true }) as string[] }
137
+ catch { return [] }
138
+ return entries
139
+ .filter(e => e.endsWith(".eikon"))
140
+ .map(e => join(dir, e))
141
+ .map(path => ({ path, meta: peek(path) }))
142
+ .filter((x): x is { path: string; meta: EikonMeta } => x.meta !== null)
143
+ })
144
+ }