@cueloop/client 0.1.0-alpha.7 → 0.1.0-alpha.70

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 (248) hide show
  1. package/native/build-pty.sh +51 -0
  2. package/native/build.sh +82 -0
  3. package/native/darwin-arm64/libcuelooppty.dylib +0 -0
  4. package/native/darwin-arm64/libcueloopvt.dylib +0 -0
  5. package/native/src/pty.zig +193 -0
  6. package/native/src/shim.zig +114 -0
  7. package/package.json +39 -32
  8. package/src/App.appearance.test.tsx +53 -0
  9. package/src/App.autoclose.test.tsx +76 -50
  10. package/src/App.diff.test.tsx +156 -42
  11. package/src/App.export.test.tsx +58 -40
  12. package/src/App.plan-review.test.tsx +363 -0
  13. package/src/App.prototype-markdown.test.tsx +92 -0
  14. package/src/App.prototype.test.tsx +176 -0
  15. package/src/App.readonly.test.tsx +128 -67
  16. package/src/App.rendering.test.tsx +61 -35
  17. package/src/App.submit-confirm.test.tsx +191 -0
  18. package/src/App.test.tsx +310 -114
  19. package/src/App.toast.test.tsx +89 -0
  20. package/src/App.tsx +1039 -799
  21. package/src/App.walk.test.tsx +298 -0
  22. package/src/App.workbench.test.tsx +461 -0
  23. package/src/__snapshots__/view-plan.regression.test.ts.snap +100 -0
  24. package/src/annotation-palette.ts +61 -0
  25. package/src/app-key-state.ts +61 -0
  26. package/src/app-screens.tsx +432 -0
  27. package/src/app-view-model.ts +159 -0
  28. package/src/attribution.test.ts +31 -0
  29. package/src/attribution.ts +42 -0
  30. package/src/clipboard.test.ts +23 -0
  31. package/src/clipboard.ts +33 -0
  32. package/src/components/AnnotatableFileView.stories.tsx +42 -0
  33. package/src/components/AnnotatableFileView.test.tsx +102 -0
  34. package/src/components/AnnotatableFileView.tsx +126 -0
  35. package/src/components/AnnotationCards.stories.tsx +87 -0
  36. package/src/components/AnnotationCards.tsx +303 -0
  37. package/src/components/AppHeader.stories.tsx +50 -0
  38. package/src/components/AppHeader.tsx +83 -0
  39. package/src/components/AppShell.stories.tsx +89 -0
  40. package/src/components/AppShell.tsx +286 -0
  41. package/src/components/AppShell.zoom.test.tsx +94 -0
  42. package/src/components/BareWorkbenchFileView.stories.tsx +27 -0
  43. package/src/components/BareWorkbenchFileView.tsx +64 -0
  44. package/src/components/Breadcrumb.stories.tsx +19 -0
  45. package/src/components/Breadcrumb.tsx +45 -0
  46. package/src/components/ChangesColumn.stories.tsx +39 -0
  47. package/src/components/ChangesColumn.tsx +171 -0
  48. package/src/components/CodeBlock.stories.tsx +42 -0
  49. package/src/components/CodeBlock.tsx +84 -0
  50. package/src/components/CompletionOverlay.stories.tsx +23 -0
  51. package/src/components/CompletionOverlay.tsx +60 -0
  52. package/src/components/ConfirmCard.stories.tsx +35 -0
  53. package/src/components/ConfirmCard.tsx +128 -0
  54. package/src/components/ConfirmDialog.stories.tsx +21 -0
  55. package/src/components/ConfirmDialog.tsx +66 -0
  56. package/src/components/DiffContentView.scroll.test.tsx +121 -0
  57. package/src/components/DiffContentView.split.test.tsx +140 -0
  58. package/src/components/DiffContentView.stories.tsx +80 -0
  59. package/src/components/DiffContentView.syntax.test.tsx +82 -0
  60. package/src/components/DiffContentView.tsx +859 -0
  61. package/src/components/DiscussionMarkerRail.stories.tsx +54 -0
  62. package/src/components/DiscussionMarkerRail.tsx +181 -0
  63. package/src/components/EditorGrid.stories.tsx +59 -0
  64. package/src/components/EditorGrid.tabs.test.tsx +118 -0
  65. package/src/components/EditorGrid.tsx +368 -0
  66. package/src/components/EditorGrid.zoom.test.tsx +68 -0
  67. package/src/components/FileContentsView.stories.tsx +16 -0
  68. package/src/components/FileContentsView.test.tsx +29 -0
  69. package/src/components/FileContentsView.tsx +98 -0
  70. package/src/components/KeybindsDialog.stories.tsx +31 -0
  71. package/src/components/KeybindsDialog.tsx +103 -0
  72. package/src/components/MenuBar.stories.tsx +28 -0
  73. package/src/components/MenuBar.tsx +44 -0
  74. package/src/components/OverlayScrollbar.stories.tsx +33 -0
  75. package/src/components/OverlayScrollbar.test.tsx +139 -0
  76. package/src/components/OverlayScrollbar.tsx +129 -0
  77. package/src/components/PaneGrid.stories.tsx +84 -0
  78. package/src/components/PaneGrid.tsx +128 -0
  79. package/src/components/PanelColumn.stories.tsx +25 -0
  80. package/src/components/PanelColumn.tsx +95 -0
  81. package/src/components/ProjectTreeView.stories.tsx +25 -0
  82. package/src/components/ProjectTreeView.test.tsx +32 -0
  83. package/src/components/ProjectTreeView.tsx +83 -0
  84. package/src/components/PromptDialog.stories.tsx +36 -0
  85. package/src/components/PromptDialog.tsx +87 -0
  86. package/src/components/PrototypeContentView.stories.tsx +19 -0
  87. package/src/components/PrototypeContentView.tsx +446 -0
  88. package/src/components/RootOverlay.stories.tsx +44 -0
  89. package/src/components/RootOverlay.tsx +35 -0
  90. package/src/components/ScrollArea.stories.tsx +20 -0
  91. package/src/components/ScrollArea.tsx +44 -0
  92. package/src/components/SettingsDialog.stories.tsx +117 -0
  93. package/src/components/SettingsDialog.tsx +195 -0
  94. package/src/components/SettingsRows.stories.tsx +18 -0
  95. package/src/components/SettingsRows.tsx +112 -0
  96. package/src/components/ShellHeader.stories.tsx +50 -0
  97. package/src/components/ShellHeader.tsx +137 -0
  98. package/src/components/ThreadFooter.stories.tsx +30 -0
  99. package/src/components/ThreadFooter.test.tsx +31 -0
  100. package/src/components/ThreadFooter.tsx +54 -0
  101. package/src/components/ThreadTree.stories.tsx +46 -0
  102. package/src/components/ThreadTree.tsx +219 -0
  103. package/src/components/ThreadView.selection.test.tsx +812 -0
  104. package/src/components/ThreadView.slash.test.tsx +158 -0
  105. package/src/components/ThreadView.stories.tsx +26 -0
  106. package/src/components/ThreadView.tsx +330 -0
  107. package/src/components/ThreadsSidebar.stories.tsx +36 -0
  108. package/src/components/ThreadsSidebar.tsx +71 -0
  109. package/src/components/Toast.stories.tsx +12 -0
  110. package/src/components/Toast.tsx +45 -0
  111. package/src/components/Tooltip.stories.tsx +26 -0
  112. package/src/components/Tooltip.tsx +72 -0
  113. package/src/components/TreePane.stories.tsx +106 -0
  114. package/src/components/TreePane.tsx +157 -0
  115. package/src/components/WalkWizard.stories.tsx +100 -0
  116. package/src/components/WalkWizard.tsx +152 -0
  117. package/src/components/WelcomePlayground.stories.tsx +12 -0
  118. package/src/components/WelcomePlayground.test.tsx +47 -0
  119. package/src/components/WelcomePlayground.tsx +140 -0
  120. package/src/components/__snapshots__/stories.test.tsx.snap +1845 -0
  121. package/src/components/agent-launcher.stories.tsx +26 -0
  122. package/src/components/agent-launcher.test.tsx +50 -0
  123. package/src/components/agent-launcher.tsx +149 -0
  124. package/src/components/diff-content-view-layout.test.ts +198 -0
  125. package/src/components/diff-content-view-layout.ts +84 -0
  126. package/src/components/editor-grid.test.ts +86 -0
  127. package/src/components/editor-grid.ts +138 -0
  128. package/src/components/fade-title.test.ts +49 -0
  129. package/src/components/fade-title.ts +61 -0
  130. package/src/components/file-tree.test.ts +60 -0
  131. package/src/components/file-tree.ts +112 -0
  132. package/src/components/pages/Workspace.stories.tsx +181 -0
  133. package/src/components/primitives/Button.stories.tsx +32 -0
  134. package/src/components/primitives/Button.tsx +52 -0
  135. package/src/components/primitives/Card.stories.tsx +25 -0
  136. package/src/components/primitives/Card.tsx +60 -0
  137. package/src/components/primitives/Dialog.stories.tsx +24 -0
  138. package/src/components/primitives/Dialog.tsx +66 -0
  139. package/src/components/primitives/IconButton.stories.tsx +36 -0
  140. package/src/components/primitives/IconButton.tsx +62 -0
  141. package/src/components/primitives/StatusBar.stories.tsx +11 -0
  142. package/src/components/primitives/StatusBar.tsx +23 -0
  143. package/src/components/primitives/Tabs.stories.tsx +29 -0
  144. package/src/components/primitives/Tabs.tsx +102 -0
  145. package/src/components/primitives/Toolbar.stories.tsx +17 -0
  146. package/src/components/primitives/Toolbar.tsx +14 -0
  147. package/src/components/primitives/Tree.stories.tsx +124 -0
  148. package/src/components/primitives/Tree.tsx +96 -0
  149. package/src/components/primitives/frame.test.ts +12 -0
  150. package/src/components/primitives/frame.ts +14 -0
  151. package/src/components/primitives/icons.ts +61 -0
  152. package/src/components/primitives/tree-model.test.ts +99 -0
  153. package/src/components/primitives/tree-model.ts +137 -0
  154. package/src/components/quick-actions-editor.stories.tsx +54 -0
  155. package/src/components/quick-actions-editor.test.tsx +49 -0
  156. package/src/components/quick-actions-editor.tsx +150 -0
  157. package/src/components/session-tree.test.ts +134 -0
  158. package/src/components/session-tree.ts +108 -0
  159. package/src/components/stories-app.tsx +184 -0
  160. package/src/components/stories.test.tsx +66 -0
  161. package/src/components/story-fixtures.ts +102 -0
  162. package/src/components/story-tree.test.ts +100 -0
  163. package/src/components/story.ts +142 -0
  164. package/src/components/syntax-highlight.ts +110 -0
  165. package/src/components/terminal-pane.test.tsx +40 -0
  166. package/src/components/terminal-pane.ts +199 -0
  167. package/src/components/theme-context.stories.tsx +46 -0
  168. package/src/components/theme-context.tsx +37 -0
  169. package/src/components/truncate-text.ts +7 -0
  170. package/src/config.test.ts +461 -31
  171. package/src/config.ts +389 -57
  172. package/src/diff-fold.test.ts +78 -0
  173. package/src/diff-fold.ts +126 -0
  174. package/src/diff-hunk-curate.test.ts +315 -0
  175. package/src/diff-hunk-curate.ts +153 -0
  176. package/src/diff-intraline.test.ts +165 -0
  177. package/src/diff-intraline.ts +146 -0
  178. package/src/diff-syntax.test.ts +112 -0
  179. package/src/diff-syntax.ts +179 -0
  180. package/src/discussions.ts +79 -0
  181. package/src/editor.test.ts +149 -0
  182. package/src/editor.ts +139 -9
  183. package/src/ghostty-terminal.test.ts +85 -0
  184. package/src/ghostty-terminal.ts +199 -0
  185. package/src/herdr.test.ts +9 -34
  186. package/src/herdr.ts +25 -21
  187. package/src/index.ts +11 -0
  188. package/src/integrations.test.ts +41 -0
  189. package/src/integrations.ts +32 -0
  190. package/src/intent-dispatch.test.ts +698 -0
  191. package/src/intent-dispatch.ts +700 -0
  192. package/src/key-bindings.test.ts +156 -0
  193. package/src/key-bindings.ts +465 -0
  194. package/src/keymap.test.ts +611 -0
  195. package/src/keymap.ts +410 -0
  196. package/src/kitty-image.test.ts +66 -0
  197. package/src/kitty-image.ts +138 -0
  198. package/src/mark-runs.ts +120 -0
  199. package/src/prototype-browser.test.ts +56 -0
  200. package/src/prototype-browser.ts +307 -0
  201. package/src/prototype-pixels.tsx +35 -0
  202. package/src/pty.test.ts +81 -0
  203. package/src/pty.ts +245 -0
  204. package/src/ready-signal.test.ts +85 -0
  205. package/src/ready-signal.ts +52 -0
  206. package/src/run.ts +30 -9
  207. package/src/serve.test.ts +24 -12
  208. package/src/serve.ts +20 -9
  209. package/src/share.ts +215 -0
  210. package/src/skills.test.ts +74 -0
  211. package/src/skills.ts +68 -0
  212. package/src/slash-palette.test.ts +54 -0
  213. package/src/slash-palette.ts +104 -0
  214. package/src/split-diff.test.ts +110 -0
  215. package/src/split-diff.ts +149 -0
  216. package/src/test-support.test.ts +264 -0
  217. package/src/test-support.ts +355 -0
  218. package/src/theme-presets.test.ts +51 -0
  219. package/src/theme-presets.ts +214 -0
  220. package/src/theme.ts +84 -19
  221. package/src/thread-chords.test.ts +170 -0
  222. package/src/thread-chords.ts +169 -0
  223. package/src/thread-controller.curate.test.ts +438 -0
  224. package/src/thread-controller.live-diff.test.ts +199 -0
  225. package/src/thread-controller.share.test.ts +472 -0
  226. package/src/thread-controller.tree.test.ts +255 -0
  227. package/src/thread-controller.ts +1509 -0
  228. package/src/thread-selection.test.ts +210 -0
  229. package/src/thread-selection.ts +243 -0
  230. package/src/tree-view.test.ts +128 -0
  231. package/src/tree-view.ts +148 -0
  232. package/src/use-annotation-surface.tsx +851 -0
  233. package/src/use-changes-workbench.ts +151 -0
  234. package/src/use-frame-measure.ts +38 -0
  235. package/src/use-settings-dialog.tsx +274 -0
  236. package/src/use-terminal-virtualizer.ts +233 -0
  237. package/src/version.ts +5 -0
  238. package/src/view-diff.test.ts +66 -9
  239. package/src/view-diff.ts +231 -23
  240. package/src/view-plan.regression.test.ts +60 -0
  241. package/src/view-plan.test.ts +406 -0
  242. package/src/view-plan.ts +570 -0
  243. package/src/walk.test.ts +121 -0
  244. package/src/walk.ts +74 -0
  245. package/src/syntax.test.ts +0 -41
  246. package/src/syntax.ts +0 -88
  247. package/src/view.test.ts +0 -157
  248. package/src/view.ts +0 -312
@@ -0,0 +1,142 @@
1
+ /**
2
+ * The story contract. Every component file in components/ ships a
3
+ * colocated *.stories.tsx that exports `meta` plus named Story objects.
4
+ * The catalog IS the regression suite: stories.test.tsx walks every story,
5
+ * snapshots its char frame, and asserts declared colors against the styled
6
+ * spans; the `bun run stories` TUI renders the same objects interactively.
7
+ */
8
+
9
+ import type React from "react";
10
+ import type { TreeNode } from "./primitives/tree-model";
11
+
12
+ export interface StoryMeta {
13
+ /** Catalog path, e.g. "primitives/Card". */
14
+ title: string;
15
+ }
16
+
17
+ export interface Story {
18
+ render: () => React.ReactNode;
19
+ /**
20
+ * Colors (hex tokens) that must appear among the rendered frame's styled
21
+ * spans - the color regression net for color-bearing stories.
22
+ */
23
+ expectedColors?: string[];
24
+ /** Virtual terminal size; defaults to 80x24. */
25
+ size?: { width: number; height: number };
26
+ }
27
+
28
+ export interface LoadedStory {
29
+ moduleTitle: string;
30
+ storyName: string;
31
+ story: Story;
32
+ }
33
+
34
+ export function isStory(value: unknown): value is Story {
35
+ return (
36
+ typeof value === "object" &&
37
+ value !== null &&
38
+ "render" in value &&
39
+ typeof value.render === "function"
40
+ );
41
+ }
42
+
43
+ function isStoryMeta(value: unknown): value is StoryMeta {
44
+ return (
45
+ typeof value === "object" &&
46
+ value !== null &&
47
+ "title" in value &&
48
+ typeof value.title === "string"
49
+ );
50
+ }
51
+
52
+ /** Import every *.stories.tsx next to the components and flatten the exports. */
53
+ export async function loadStories(): Promise<LoadedStory[]> {
54
+ const glob = new Bun.Glob("**/*.stories.tsx");
55
+ const files = [...glob.scanSync({ cwd: import.meta.dir })].sort();
56
+ const loaded: LoadedStory[] = [];
57
+
58
+ for (const file of files) {
59
+ const moduleExports = await import(`${import.meta.dir}/${file}`);
60
+ const meta = moduleExports["meta"];
61
+ const moduleTitle = isStoryMeta(meta) ? meta.title : file.replace(/\.stories\.tsx$/, "");
62
+
63
+ for (const [exportName, exported] of Object.entries(moduleExports)) {
64
+ if (exportName === "meta" || !isStory(exported)) continue;
65
+ loaded.push({ moduleTitle, storyName: exportName, story: exported });
66
+ }
67
+ }
68
+
69
+ return loaded;
70
+ }
71
+
72
+ /** A folder node keeps its children array live so the builder can append to it. */
73
+ interface StoryTreeFolder extends TreeNode {
74
+ children: TreeNode[];
75
+ }
76
+
77
+ // A component folder holding one story of its own name reads as one row, not a folder wrapping a single leaf.
78
+ function hoistSingleStoryFolders(nodes: TreeNode[]): TreeNode[] {
79
+ return nodes.map((node) => {
80
+ if (node.children === undefined) return node;
81
+ const children = hoistSingleStoryFolders(node.children);
82
+ const onlyChild = children.length === 1 ? children[0] : undefined;
83
+ if (
84
+ onlyChild !== undefined &&
85
+ onlyChild.children === undefined &&
86
+ onlyChild.label === node.label
87
+ ) {
88
+ return onlyChild;
89
+ }
90
+
91
+ return { ...node, children };
92
+ });
93
+ }
94
+
95
+ /**
96
+ * Nest the flat catalog into folder/component groups by splitting each
97
+ * moduleTitle on "/". Each story becomes a leaf keyed `${moduleTitle}/${storyName}`
98
+ * under its component node; a component holding a single story of its own name
99
+ * collapses so its row IS that story leaf.
100
+ */
101
+ export function buildStoryTree(stories: LoadedStory[]): TreeNode[] {
102
+ const roots: TreeNode[] = [];
103
+ const foldersByPath = new Map<string, StoryTreeFolder>();
104
+
105
+ for (const { moduleTitle, storyName } of stories) {
106
+ let siblings = roots;
107
+ let path = "";
108
+
109
+ for (const segment of moduleTitle.split("/")) {
110
+ path = path === "" ? segment : `${path}/${segment}`;
111
+ let folder = foldersByPath.get(path);
112
+ if (folder === undefined) {
113
+ folder = { id: path, label: segment, children: [] };
114
+ foldersByPath.set(path, folder);
115
+ siblings.push(folder);
116
+ }
117
+ siblings = folder.children;
118
+ }
119
+
120
+ siblings.push({ id: `${moduleTitle}/${storyName}`, label: storyName });
121
+ }
122
+
123
+ return hoistSingleStoryFolders(roots);
124
+ }
125
+
126
+ /** Component files that must carry stories: every .tsx that is not a story/test/prototype. */
127
+ export function componentFilesMissingStories(): string[] {
128
+ const componentGlob = new Bun.Glob("**/*.tsx");
129
+ const files = [...componentGlob.scanSync({ cwd: import.meta.dir })];
130
+ const missing: string[] = [];
131
+
132
+ for (const file of files) {
133
+ if (file.endsWith(".stories.tsx") || file.endsWith(".test.tsx")) continue;
134
+ if (file.endsWith(".prototype.tsx")) continue;
135
+ if (file === "stories-app.tsx") continue;
136
+ const storiesSibling = file.replace(/\.tsx$/, ".stories.tsx");
137
+
138
+ if (!files.includes(storiesSibling)) missing.push(file);
139
+ }
140
+
141
+ return missing;
142
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Code highlighting tokens: the theme's named colors projected onto tree-sitter
3
+ * capture names. One group -> color map is the single source of truth, feeding
4
+ * both the native code renderable (via a cached SyntaxStyle per theme) and the
5
+ * diff sheet's per-row spans. Unknown languages simply render unstyled -
6
+ * highlighting is an enhancement, never a blocker.
7
+ */
8
+
9
+ import {
10
+ SyntaxStyle,
11
+ infoStringToFiletype,
12
+ extensionToFiletype,
13
+ basenameToFiletype,
14
+ } from "@opentui/core";
15
+ import type { Theme } from "../theme";
16
+
17
+ /** Markdown fence info -> tree-sitter filetype, with the aliases we accept. */
18
+ export function filetypeFor(language?: string): string | undefined {
19
+ const info = (language ?? "").toLowerCase();
20
+
21
+ if (!info) return undefined;
22
+ const aliases: SyntaxAliases = {
23
+ ts: "typescript",
24
+ js: "javascript",
25
+ py: "python",
26
+ rb: "ruby",
27
+ rs: "rust",
28
+ sh: "bash",
29
+ shell: "bash",
30
+ zsh: "bash",
31
+ yml: "yaml",
32
+ };
33
+
34
+ return infoStringToFiletype(aliases[info] ?? info) ?? aliases[info] ?? info;
35
+ }
36
+
37
+ /** A diff/code file path -> tree-sitter filetype, by basename then extension. */
38
+ export function filetypeForPath(path: string): string | undefined {
39
+ const basename = path.split("/").pop() ?? path;
40
+ const byBasename = basenameToFiletype.get(basename);
41
+
42
+ if (byBasename) return byBasename;
43
+ const dotIndex = basename.lastIndexOf(".");
44
+
45
+ if (dotIndex <= 0) return undefined;
46
+
47
+ return extensionToFiletype.get(basename.slice(dotIndex + 1).toLowerCase());
48
+ }
49
+
50
+ interface SyntaxAliases {
51
+ [alias: string]: string;
52
+ }
53
+
54
+ interface SyntaxGroupStyle {
55
+ fg: string;
56
+ italic?: boolean;
57
+ }
58
+
59
+ interface SyntaxGroupStyles {
60
+ [group: string]: SyntaxGroupStyle;
61
+ }
62
+
63
+ /** The one group -> style map, keyed by tree-sitter capture name. */
64
+ function syntaxGroupStyles(theme: Theme): SyntaxGroupStyles {
65
+ return {
66
+ default: { fg: theme.textMuted },
67
+ comment: { fg: theme.textDim, italic: true },
68
+ string: { fg: theme.green },
69
+ number: { fg: theme.blue },
70
+ constant: { fg: theme.blue },
71
+ boolean: { fg: theme.blue },
72
+ keyword: { fg: theme.accent },
73
+ operator: { fg: theme.textDim },
74
+ punctuation: { fg: theme.textDim },
75
+ function: { fg: theme.text },
76
+ method: { fg: theme.text },
77
+ type: { fg: theme.blue },
78
+ constructor: { fg: theme.blue },
79
+ variable: { fg: theme.textMuted },
80
+ property: { fg: theme.textMuted },
81
+ parameter: { fg: theme.textMuted },
82
+ tag: { fg: theme.accent },
83
+ attribute: { fg: theme.blue },
84
+ };
85
+ }
86
+
87
+ const syntaxStyleCache = new WeakMap<Theme, SyntaxStyle>();
88
+
89
+ export function syntaxStyleFor(theme: Theme): SyntaxStyle {
90
+ const cached = syntaxStyleCache.get(theme);
91
+
92
+ if (cached) return cached;
93
+ const style = SyntaxStyle.fromStyles(syntaxGroupStyles(theme));
94
+
95
+ syntaxStyleCache.set(theme, style);
96
+
97
+ return style;
98
+ }
99
+
100
+ /** The foreground for a tree-sitter capture group; undefined when unstyled. */
101
+ export function colorForSyntaxGroup(group: string, theme: Theme): string | undefined {
102
+ const styles = syntaxGroupStyles(theme);
103
+ const direct = styles[group];
104
+
105
+ if (direct) return direct.fg;
106
+ // capture names are dotted (e.g. "keyword.control"); match the broadest prefix
107
+ const base = group.split(".")[0]!;
108
+
109
+ return styles[base]?.fg;
110
+ }
@@ -0,0 +1,40 @@
1
+ /** The embedded terminal renderable runs a real PTY child through Ghostty's VT and paints its screen; a live `printf` child must show up in the OpenTUI frame. Skipped where no prebuilt libghostty-vt ships for the platform. */
2
+
3
+ import { describe, expect, test } from "bun:test";
4
+ import React from "react";
5
+ import { testRender } from "@opentui/react/test-utils";
6
+ import { waitForText } from "../test-support";
7
+ import {
8
+ embeddedTerminalAvailable,
9
+ registerTerminalPane,
10
+ TerminalPaneRenderable,
11
+ } from "./terminal-pane";
12
+
13
+ registerTerminalPane();
14
+
15
+ describe("TerminalPaneRenderable", () => {
16
+ test.skipIf(!embeddedTerminalAvailable())(
17
+ "paints a live child process's output into the box",
18
+ async () => {
19
+ // Arrange / Act - a child that prints a known line
20
+ const setup = await testRender(
21
+ React.createElement("terminalPane", {
22
+ command: "/bin/sh",
23
+ args: ["-c", "printf 'HELLO-EMBED\\n'; sleep 5"],
24
+ style: { width: 40, height: 6 },
25
+ }),
26
+ { width: 40, height: 6 },
27
+ );
28
+
29
+ // Assert - the child's output reaches the painted frame via Ghostty
30
+ await waitForText(setup, "HELLO-EMBED");
31
+ setup.renderer.destroy();
32
+ },
33
+ );
34
+
35
+ test("exposes availability + the renderable class", () => {
36
+ // Assert - the module loads and reports platform support honestly
37
+ expect([true, false]).toContain(embeddedTerminalAvailable());
38
+ expect(TerminalPaneRenderable).toBeDefined();
39
+ });
40
+ });
@@ -0,0 +1,199 @@
1
+ /**
2
+ * An OpenTUI renderable that runs a real child process (a shell, cc, pi, codex)
3
+ * on a PTY and paints its live screen into the box - the terminal-in-the-rail
4
+ * primitive. It wires the forkpty shim (child + tty, ../pty) to a Ghostty VT emulator
5
+ * (ghostty-terminal.ts) and blits the emulator's cell grid every frame via
6
+ * OptimizedBuffer.setCell. Register once with `registerTerminalPane`, then use
7
+ * `<terminalPane command="claude" ... />` in the OpenTUI React tree.
8
+ */
9
+
10
+ import { Renderable, RGBA, createTextAttributes, type RenderContext } from "@opentui/core";
11
+ import type { OptimizedBuffer } from "@opentui/core";
12
+ import type { RenderableOptions } from "@opentui/core";
13
+ import { extend } from "@opentui/react";
14
+ import { spawn, ptyAvailable, type IPty } from "../pty";
15
+ import {
16
+ loadGhosttyTerminals,
17
+ type GhosttyColor,
18
+ type GhosttyTerminal,
19
+ type GhosttyTerminalFactory,
20
+ } from "../ghostty-terminal";
21
+
22
+ /** One shared VT library load; null when this platform ships no prebuilt dylib. */
23
+ let ghosttyFactory: GhosttyTerminalFactory | null | undefined;
24
+
25
+ function factory(): GhosttyTerminalFactory | null {
26
+ if (ghosttyFactory === undefined) ghosttyFactory = loadGhosttyTerminals();
27
+
28
+ return ghosttyFactory;
29
+ }
30
+
31
+ /** Whether the embedded terminal can run here: both native shims ship for this
32
+ * platform (the Ghostty VT renderer and the forkpty PTY). */
33
+ export function embeddedTerminalAvailable(): boolean {
34
+ return factory() !== null && ptyAvailable();
35
+ }
36
+
37
+ /** Props for `<terminalPane>`: the child to run plus its cwd/env and a plan-context seed. */
38
+ export interface TerminalPaneOptions extends RenderableOptions {
39
+ /** The program to run, e.g. "claude" / "pi" / "codex" / a shell. */
40
+ command?: string;
41
+ args?: string[];
42
+ cwd?: string;
43
+ env?: Record<string, string>;
44
+ /** Text left unsubmitted in the child after it starts (plan-context seed). */
45
+ seedText?: string;
46
+ /** Fired when the child process exits. */
47
+ onExit?: (exitCode: number) => void;
48
+ }
49
+
50
+ function terminalEnvironment(env: NodeJS.ProcessEnv) {
51
+ return Object.fromEntries(
52
+ Object.entries(env).filter((entry): entry is [string, string] => entry[1] !== undefined),
53
+ );
54
+ }
55
+
56
+ const encoder = new TextEncoder();
57
+ const DEFAULT_FG = RGBA.fromInts(208, 208, 208, 255);
58
+ const TRANSPARENT = RGBA.fromValues(0, 0, 0, 0);
59
+ /** Glyph color under the block cursor (dark ink on the light cursor block). */
60
+ const CURSOR_INK = RGBA.fromInts(20, 20, 24, 255);
61
+
62
+ /** Runs a PTY child and paints its Ghostty-rendered screen into this box. */
63
+ export class TerminalPaneRenderable extends Renderable {
64
+ private pty: IPty | null = null;
65
+ private vt: GhosttyTerminal | null = null;
66
+ private cols = 0;
67
+ private rows = 0;
68
+ // The seed is held until the child's first output, so it lands in a ready
69
+ // prompt rather than racing the harness's startup (readline/TUI not up yet).
70
+ private pendingSeed: string | undefined;
71
+ private readonly opts: TerminalPaneOptions;
72
+
73
+ constructor(ctx: RenderContext, options: TerminalPaneOptions) {
74
+ super(ctx, options);
75
+ this.opts = options;
76
+ this.focusable = true;
77
+ }
78
+
79
+ /** Start the child + emulator once real layout dimensions are known. */
80
+ private start(cols: number, rows: number): void {
81
+ const terminals = factory();
82
+
83
+ if (!terminals) return;
84
+ const vt = terminals.create(cols, rows);
85
+
86
+ if (!vt) return; // no emulator, no visible screen - do not spawn a blind child
87
+ this.vt = vt;
88
+ this.cols = cols;
89
+ this.rows = rows;
90
+ this.pendingSeed = this.opts.seedText;
91
+ this.pty = spawn(this.opts.command ?? process.env.SHELL ?? "/bin/sh", this.opts.args ?? [], {
92
+ name: "xterm-256color",
93
+ cols,
94
+ rows,
95
+ cwd: this.opts.cwd ?? process.cwd(),
96
+ env: this.opts.env ?? terminalEnvironment(process.env),
97
+ });
98
+ this.pty.onData((data) => {
99
+ // the shim streams a UTF-8-decoded string (split multibyte is handled); a
100
+ // rare non-UTF-8 byte arrives as U+FFFD - acceptable for agent TUIs.
101
+ this.vt?.write(encoder.encode(data));
102
+ if (this.pendingSeed !== undefined) {
103
+ this.pty?.write(this.pendingSeed);
104
+ this.pendingSeed = undefined;
105
+ }
106
+ this.requestRender();
107
+ });
108
+ this.pty.onExit(({ exitCode }) => this.opts.onExit?.(exitCode));
109
+ }
110
+
111
+ /** Send raw bytes to the child (a key's escape sequence, or pasted text). */
112
+ write(data: string): void {
113
+ this.pty?.write(data);
114
+ }
115
+
116
+ protected onResize(width: number, height: number): void {
117
+ if (width <= 0 || height <= 0) return;
118
+ if (!this.pty) {
119
+ this.start(width, height);
120
+
121
+ return;
122
+ }
123
+ if (width === this.cols && height === this.rows) return;
124
+ this.cols = width;
125
+ this.rows = height;
126
+ this.vt?.resize(width, height);
127
+ this.pty.resize(width, height);
128
+ }
129
+
130
+ protected renderSelf(buffer: OptimizedBuffer): void {
131
+ if (!this.vt) return;
132
+ for (let y = 0; y < this.height; y++) {
133
+ for (let x = 0; x < this.width; x++) {
134
+ const cell = this.vt.readCell(x, y);
135
+
136
+ // Skip an empty normal-width cell; the buffer already starts blank.
137
+ if (cell && cell.width === 0 && cell.codepoint === 0) continue;
138
+ const char = cell && cell.codepoint ? String.fromCodePoint(cell.codepoint) : " ";
139
+ let fg = cell ? resolveColor(cell.fg, DEFAULT_FG) : DEFAULT_FG;
140
+ let bg = cell ? resolveColor(cell.bg, TRANSPARENT) : TRANSPARENT;
141
+
142
+ if (cell?.inverse) [fg, bg] = [bg, fg];
143
+ const attributes = cell
144
+ ? createTextAttributes({
145
+ bold: cell.bold,
146
+ italic: cell.italic,
147
+ underline: cell.underline,
148
+ dim: cell.faint,
149
+ strikethrough: cell.strikethrough,
150
+ })
151
+ : 0;
152
+
153
+ buffer.setCell(this.x + x, this.y + y, char, fg, bg, attributes);
154
+ }
155
+ }
156
+ // cursor is active-area coords; equal to viewport at the bottom (the live
157
+ // typing case). It can drift if the child scrolls the viewport back.
158
+ const cursor = this.vt.readCursor();
159
+
160
+ if (cursor.visible && cursor.x < this.width && cursor.y < this.height) {
161
+ const under = this.vt.readCell(cursor.x, cursor.y);
162
+ const char = under && under.codepoint ? String.fromCodePoint(under.codepoint) : " ";
163
+
164
+ // a block cursor: paint the cell with fg/bg swapped
165
+ buffer.setCell(this.x + cursor.x, this.y + cursor.y, char, CURSOR_INK, DEFAULT_FG, 0);
166
+ }
167
+ }
168
+
169
+ /** Kill the child and free the VT. Idempotent - the detach path calls this
170
+ * directly because the React reconciler removes a child without destroying it. */
171
+ shutdown(): void {
172
+ this.pty?.kill();
173
+ this.vt?.free();
174
+ this.pty = null;
175
+ this.vt = null;
176
+ }
177
+
178
+ protected destroySelf(): void {
179
+ this.shutdown();
180
+ super.destroySelf();
181
+ }
182
+ }
183
+
184
+ /** Map a Ghostty cell color to an OpenTUI RGBA, using `fallback` for the terminal default. */
185
+ function resolveColor(color: GhosttyColor, fallback: RGBA): RGBA {
186
+ if (color.kind === "rgb") return RGBA.fromInts(color.r, color.g, color.b, 255);
187
+ if (color.kind === "palette") return RGBA.fromIndex(color.index);
188
+
189
+ return fallback;
190
+ }
191
+
192
+ let registered = false;
193
+
194
+ /** Register `<terminalPane>` with the OpenTUI React reconciler (idempotent). */
195
+ export function registerTerminalPane(): void {
196
+ if (registered) return;
197
+ registered = true;
198
+ extend({ terminalPane: TerminalPaneRenderable });
199
+ }
@@ -0,0 +1,46 @@
1
+ import React from "react";
2
+ import { DARK, type Theme } from "../theme";
3
+ import type { Story, StoryMeta } from "./story";
4
+ import { ThemeProvider } from "./theme-context";
5
+ import { Card } from "./primitives/Card";
6
+
7
+ export const meta: StoryMeta = { title: "Foundations/Theme" };
8
+
9
+ /** A high-contrast variant to prove tokens flow through the provider. */
10
+ const HIGH_CONTRAST: Theme = { ...DARK, accent: "#00ffff", elevated: "#000000", border: "#ffffff" };
11
+
12
+ export const ContextDefault: Story = {
13
+ render: () => (
14
+ <Card title=" default dark theme " contentRows={1}>
15
+ <text>no provider needed</text>
16
+ </Card>
17
+ ),
18
+ expectedColors: [DARK.elevated],
19
+ };
20
+
21
+ export const ProviderSwap: Story = {
22
+ render: () => (
23
+ <ThemeProvider theme={HIGH_CONTRAST}>
24
+ <Card title=" provider theme " contentRows={1} borderColor={HIGH_CONTRAST.accent}>
25
+ <text>tokens come from the provider</text>
26
+ </Card>
27
+ </ThemeProvider>
28
+ ),
29
+ expectedColors: ["#00ffff", "#000000"],
30
+ };
31
+
32
+ export const PropOverride: Story = {
33
+ render: () => (
34
+ <ThemeProvider theme={DARK}>
35
+ <Card
36
+ title=" prop-overridden theme "
37
+ contentRows={1}
38
+ theme={HIGH_CONTRAST}
39
+ borderColor={HIGH_CONTRAST.accent}
40
+ >
41
+ <text>the theme prop beats the context</text>
42
+ </Card>
43
+ </ThemeProvider>
44
+ ),
45
+ expectedColors: ["#00ffff"],
46
+ };
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Provider theming for the component system. The context default is the
3
+ * built-in dark theme, so embedding a component (or rendering a story) needs
4
+ * no provider at all. One ThemeProvider swap switches the live theme; every
5
+ * component also accepts an optional `theme` prop that overrides the context
6
+ * for explicit control. Components consume named tokens only - a hardcoded
7
+ * color inside components/ is a review rejection.
8
+ */
9
+
10
+ import React, { createContext, useContext } from "react";
11
+ import { DARK, type Theme } from "../theme";
12
+
13
+ const ThemeContext = createContext<Theme>(DARK);
14
+
15
+ export function ThemeProvider({
16
+ theme,
17
+ children,
18
+ }: {
19
+ theme: Theme;
20
+ children: React.ReactNode;
21
+ }): React.ReactNode {
22
+ return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>;
23
+ }
24
+
25
+ export function useTheme(): Theme {
26
+ return useContext(ThemeContext);
27
+ }
28
+
29
+ /**
30
+ * Token resolution for a component: the explicit `theme` prop wins over the
31
+ * provider context, which defaults to the built-in dark theme.
32
+ */
33
+ export function useComponentTheme(override?: Theme): Theme {
34
+ const contextTheme = useContext(ThemeContext);
35
+
36
+ return override ?? contextTheme;
37
+ }
@@ -0,0 +1,7 @@
1
+ /** Truncate text to one line of at most maxLength characters, ellipsis-terminated. */
2
+
3
+ export function truncateToSingleLine(value: string, maxLength: number): string {
4
+ const oneLine = value.replace(/\n/g, " ");
5
+
6
+ return oneLine.length > maxLength ? oneLine.slice(0, maxLength - 1) + "…" : oneLine;
7
+ }