Skip to content

Commit 1ba485c

Browse files
committed
feat(opencode): add Ink-based TUI components for oclite
Ink (React for terminals) TUI implementation: - Components: App, MessageList, StreamingProse, InputLine, SelectMenu, StatusBar, ToolDisplay, TaskDisplay - State management with useReducer pattern - Theme system with consistent color tokens - Custom type declarations for Ink/React 18 compatibility - Preload.ts excludes cli/ink/ from SolidJS transform - 90 comprehensive tests Part of Epic anomalyco#11762 Fixes anomalyco#11763, anomalyco#11764, anomalyco#11765, anomalyco#11766, anomalyco#11767, anomalyco#11768, anomalyco#11769, anomalyco#11770, anomalyco#11774, anomalyco#11776
1 parent 8425e77 commit 1ba485c

35 files changed

Lines changed: 2086 additions & 46 deletions

‎bun.lock‎

Lines changed: 98 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/opencode/global.d.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
declare module "babel-preset-solid" {
2+
import type { ConfigAPI, PluginObj } from "@babel/core"
3+
type PluginOptions = object | undefined | false
4+
5+
export default function preset(api: ConfigAPI, options?: PluginOptions): PluginObj<unknown> | void
6+
}
7+
8+
declare module "@babel/preset-typescript" {
9+
import type { ConfigAPI, PluginObj } from "@babel/core"
10+
type PluginOptions = object | undefined | false
11+
12+
export default function preset(api: ConfigAPI, options?: PluginOptions): PluginObj<unknown> | void
13+
}
14+
15+
declare module "@babel/preset-react" {
16+
import type { ConfigAPI, PluginObj } from "@babel/core"
17+
type PluginOptions = object | undefined | false
18+
19+
export default function preset(api: ConfigAPI, options?: PluginOptions): PluginObj<unknown> | void
20+
}
21+
22+
declare module "@opentui/solid/scripts/solid-plugin" {
23+
import type { BunPlugin } from "bun"
24+
25+
const plugin: BunPlugin
26+
export default plugin
27+
}

‎packages/opencode/package.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
"@tsconfig/bun": "catalog:",
4141
"@types/babel__core": "7.20.5",
4242
"@types/bun": "catalog:",
43+
"@types/react": "18.3.12",
4344
"@types/turndown": "5.0.5",
4445
"@types/yargs": "17.0.33",
4546
"@typescript/native-preview": "catalog:",
47+
"ink-testing-library": "4.0.0",
4648
"typescript": "catalog:",
4749
"vscode-languageserver-types": "3.17.5",
4850
"why-is-node-running": "3.2.2",
@@ -104,11 +106,15 @@
104106
"hono": "catalog:",
105107
"hono-openapi": "catalog:",
106108
"ignore": "7.0.5",
109+
"ink": "6.6.0",
110+
"ink-spinner": "5.0.0",
111+
"ink-text-input": "6.0.0",
107112
"jsonc-parser": "3.3.1",
108113
"minimatch": "10.0.3",
109114
"open": "10.1.2",
110115
"opentui-spinner": "0.0.6",
111116
"partial-json": "0.1.7",
117+
"react": "18.3.1",
112118
"remeda": "catalog:",
113119
"solid-js": "catalog:",
114120
"strip-ansi": "7.1.2",

‎packages/opencode/preload.ts‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { plugin, type BunPlugin } from "bun"
2+
import { transformAsync } from "@babel/core"
3+
import solid from "babel-preset-solid"
4+
import react from "@babel/preset-react"
5+
import ts from "@babel/preset-typescript"
6+
7+
const inkExcludedPlugin: BunPlugin = {
8+
name: "solid-with-ink-exclusion",
9+
setup(build) {
10+
build.onLoad({ filter: /[\\/]node_modules[\\/]solid-js[\\/]dist[\\/]server\.js$/ }, async (args) => {
11+
try {
12+
const path = args.path.replace(/(\.server\.js)$/, ".solid.js")
13+
const file = Bun.file(path)
14+
if (!(await file.exists())) return undefined
15+
const code = await file.text()
16+
return { contents: code, loader: "js" }
17+
} catch {
18+
return undefined
19+
}
20+
})
21+
22+
build.onLoad({ filter: /[\\/]node_modules[\\/]solid-js[\\/]store[\\/]dist[\\/]server\.js$/ }, async (args) => {
23+
try {
24+
const path = args.path.replace(/(\.server\.js)$/, ".store.js")
25+
const file = Bun.file(path)
26+
if (!(await file.exists())) return undefined
27+
const code = await file.text()
28+
return { contents: code, loader: "js" }
29+
} catch {
30+
return undefined
31+
}
32+
})
33+
34+
build.onLoad({ filter: /\.(tsx|jsx)$/ }, async (args) => {
35+
if (args.path.includes("node_modules")) {
36+
return undefined
37+
}
38+
39+
const code = await Bun.file(args.path).text()
40+
41+
// Detect if file is Ink-related (React-based)
42+
const isInkFile =
43+
/from\s*["']ink["']/.test(code) ||
44+
/from\s*["']ink-/.test(code) ||
45+
/require\(["']ink["']\)/.test(code) ||
46+
/@jsxImportSource\s+react/.test(code) ||
47+
args.path.includes("/cli/ink/") ||
48+
args.path.includes("/test/cli/ink/")
49+
50+
if (isInkFile) {
51+
// Transform with Babel React preset
52+
const result = await transformAsync(code, {
53+
filename: args.path,
54+
presets: [
55+
["@babel/preset-react", { runtime: "automatic", importSource: "react" }],
56+
[ts, { isTSX: true, allExtensions: true }],
57+
],
58+
})
59+
return { contents: result?.code ?? "", loader: "js" }
60+
}
61+
62+
try {
63+
// Transform with SolidJS preset
64+
const transforms = await transformAsync(code, {
65+
filename: args.path,
66+
presets: [
67+
[
68+
solid,
69+
{
70+
moduleName: "@opentui/solid",
71+
generate: "universal",
72+
},
73+
],
74+
[ts],
75+
],
76+
})
77+
78+
if (!transforms || !transforms.code) {
79+
throw new Error(`Transform produced no output: ${args.path}`)
80+
}
81+
82+
return {
83+
contents: transforms.code,
84+
loader: "js",
85+
}
86+
} catch (error) {
87+
const errorMessage = error instanceof Error ? error.message : String(error)
88+
const errorStack = error instanceof Error ? error.stack : undefined
89+
const fullMessage = errorStack
90+
? `SolidJS transform failed for ${args.path}: ${errorMessage}\n${errorStack}`
91+
: `SolidJS transform failed for ${args.path}: ${errorMessage}`
92+
throw new Error(fullMessage)
93+
}
94+
})
95+
},
96+
}
97+
98+
plugin(inkExcludedPlugin)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** @jsxImportSource react */
2+
import { useReducer } from "react"
3+
import type { ReactElement } from "react"
4+
import { Box, Text } from "ink"
5+
import { appReducer, initialState } from "./state/reducer"
6+
import { theme } from "./theme"
7+
8+
export const App = (): ReactElement => {
9+
const [state, dispatch] = useReducer(appReducer, initialState)
10+
11+
return (
12+
<Box flexDirection="column">
13+
{/* Streaming content */}
14+
{state.streaming.text && (
15+
<Box>
16+
<Text>{state.streaming.text}</Text>
17+
</Box>
18+
)}
19+
20+
{/* Input prompt */}
21+
<Box>
22+
<Text color={theme.prompt.agent}>{state.session.agent}</Text>
23+
<Text color={theme.prompt.separator}> {theme.prompt.symbol} </Text>
24+
</Box>
25+
</Box>
26+
)
27+
}
28+
29+
export default App
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Ink TUI Components
2+
3+
React-based terminal UI components using [Ink](https://github.com/vadimdemedes/ink).
4+
5+
## Architecture
6+
7+
- Components use React 19 with `/** @jsxImportSource react */` pragma
8+
- SolidJS preload excludes `cli/ink/` and `test/cli/ink/` directories
9+
- State managed via useReducer pattern
10+
11+
## Running
12+
13+
```bash
14+
# Run Ink test file
15+
bun --no-plugins src/cli/ink/test.tsx
16+
17+
# Run tests
18+
bun test test/cli/ink/
19+
```
20+
21+
## Known Issues
22+
23+
### React Hook Warnings in Tests
24+
25+
Tests show "Invalid hook call" warnings due to [email protected] not fully supporting React 19.2.4. These warnings are **cosmetic only**:
26+
27+
- ✅ All test assertions pass
28+
- ✅ Runtime works correctly
29+
- ✅ No functional impact
30+
31+
The warnings will resolve when ink-testing-library releases React 19 support.
32+
33+
## Components
34+
35+
- `App` - Root application component
36+
- `MessageList` - Static message history
37+
- `StreamingProse` - Live text with cursor
38+
- `InputLine` - Text input
39+
- `SelectMenu` - Option selector
40+
- `StatusBar` - Agent/model/status display
41+
- `ToolDisplay` - Tool execution status
42+
- `TaskDisplay` - Subagent task status
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/** @jsxImportSource react */
2+
import { useState, useCallback } from "react"
3+
import type { ReactElement } from "react"
4+
import { Box, Text } from "ink"
5+
import TextInput from "ink-text-input"
6+
import { theme } from "../theme"
7+
8+
interface InputLineProps {
9+
onSubmit: (value: string) => void
10+
prompt?: string
11+
placeholder?: string
12+
initialValue?: string
13+
disabled?: boolean
14+
focus?: boolean
15+
}
16+
17+
export const InputLine = ({
18+
onSubmit,
19+
prompt,
20+
placeholder = "",
21+
initialValue = "",
22+
disabled = false,
23+
focus = true,
24+
}: InputLineProps): ReactElement => {
25+
const [value, setValue] = useState(initialValue)
26+
27+
const handleSubmit = useCallback(
28+
(text: string) => {
29+
onSubmit(text)
30+
setValue("")
31+
},
32+
[onSubmit],
33+
)
34+
35+
const displayPrompt = prompt ?? `${theme.prompt.symbol} `
36+
37+
return (
38+
<Box>
39+
<Text color={theme.prompt.agent}>{displayPrompt}</Text>
40+
{disabled ? (
41+
<Text dimColor>{value || placeholder}</Text>
42+
) : (
43+
<TextInput value={value} onChange={setValue} onSubmit={handleSubmit} placeholder={placeholder} focus={focus} />
44+
)}
45+
</Box>
46+
)
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @jsxImportSource react */
2+
import type { ReactElement } from "react"
3+
import { Static, Box, Text } from "ink"
4+
import type { Message } from "../state/types"
5+
import { theme } from "../theme"
6+
7+
interface MessageListProps {
8+
messages: Message[]
9+
}
10+
11+
export const MessageList = ({ messages }: MessageListProps): ReactElement => (
12+
<Static items={messages}>
13+
{(message) => (
14+
<Box key={message.id} flexDirection="column" marginBottom={1}>
15+
<Text color={message.role === "user" ? theme.colors.primary : theme.colors.success}>
16+
{message.role === "user" ? "you" : "assistant"}
17+
</Text>
18+
{message.parts.map((part, i) => (
19+
<Text key={i}>{part.content}</Text>
20+
))}
21+
</Box>
22+
)}
23+
</Static>
24+
)

0 commit comments

Comments
 (0)