The Terminal UI (TUI)
Relevant source files
The following files were used as context for generating this wiki page:- packages/opencode/src/cli/cmd/tui/app.tsx
- packages/opencode/src/cli/cmd/tui/routes/session/index.tsx
- packages/opencode/src/cli/cmd/tui/context/keybind.tsx
- packages/opencode/src/util/keybind.ts
- CONTRIBUTING.md
- packages/console/app/src/routes/index.tsx
- packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json
- packages/opencode/src/session/prompt/gemini.txt
- README.md
The OpenCode Terminal UI (TUI) is the primary interface for developers to interact with the AI coding agent directly from the terminal. It is a responsive, native, and themeable application built using SolidJS and the opentui library. The TUI provides a rich, interactive experience for managing multiple agent sessions, exploring code, and applying AI-generated changes.
Key features of the TUI include Language Server Protocol (LSP) integration, multi-session support, shareable session links, and the ability to use a wide variety of AI models from different providers. Its architecture is centered around a client/server model, allowing the TUI to act as a frontend to the core OpenCode agent logic.
Sources: CONTRIBUTING.md:19, packages/console/app/src/routes/index.tsx:210, README.md:12
Architecture and Core Components
The TUI is a SolidJS application. The main entry point is app.tsx, which sets up the core providers and routing for the entire application. It leverages the opentui library for rendering components within the terminal environment.
Sources: CONTRIBUTING.md:19, packages/opencode/src/cli/cmd/tui/app.tsx:3-10
Component and Provider Structure
The application's root component, App, orchestrates several context providers to manage global state and functionality. This includes theming, keybindings, and navigation.
The diagram below illustrates the high-level component and provider hierarchy.
Key Features
The TUI offers several features designed to enhance the developer's interaction with the AI agent.
| Feature | Description | Source |
|---|---|---|
| Native TUI | A responsive, native, and themeable terminal user interface. | packages/console/app/src/routes/index.tsx:213 |
| LSP Enabled | Automatically loads the appropriate Language Server Protocols for the LLM. | packages/console/app/src/routes/index.tsx:219 |
| Multi-session | Allows starting and managing multiple agent sessions in parallel on the same project. | packages/console/app/src/routes/index.tsx:225 |
| Shareable Links | Provides the ability to share a link to any session for reference or debugging. | packages/console/app/src/routes/index.tsx:231 |
| Model Agnostic | Supports over 75 LLM providers, including local models, through integrations like Models.dev. | packages/console/app/src/routes/index.tsx:243 |
| Editor Agnostic | Runs in the terminal and can be paired with any IDE. | packages/console/app/src/routes/index.tsx:249 |
Theming
The TUI is fully themeable, allowing users to customize its appearance. The theming system is managed by the ThemeProvider and uses JSON files to define color schemes for both dark and light modes.
Sources: packages/opencode/src/cli/cmd/tui/app.tsx:13, packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json
Theme Definition
A theme is defined in a JSON file that adheres to a specific schema. It contains a defs section for color variables and a theme section that maps these variables to specific UI elements.
The diagram below shows the structure of a theme definition file.
Example Theme: Kanagawa
The kanagawa.json theme provides a practical example of how colors are defined and applied.
| Property | Dark Mode Color | Light Mode Color | Description |
|---|---|---|---|
primary | crystalBlue (#7E9CD8) | waveBlue (#2D4F67) | Primary interactive elements. |
background | sumiInk0 (#1F1F28) | lightBg (#F2E9DE) | The main background color. |
backgroundPanel | sumiInk1 (#2A2A37) | lightPaper (#EAE4D7) | Background for panel elements. |
text | fujiWhite (#DCD7BA) | lightText (#54433A) | Default text color. |
syntaxKeyword | oniViolet (#957FB8) | oniViolet (#957FB8) | Color for syntax-highlighted keywords. |
syntaxString | lotusGreen (#98BB6C) | lotusGreen (#98BB6C) | Color for syntax-highlighted strings. |
Sources: packages/opencode/src/cli/cmd/tui/context/theme/kanagawa.json:20-56
Keybindings
Keybindings are a central part of the TUI's user experience, providing keyboard-driven shortcuts for various actions. The KeybindProvider and useKeybind hook are responsible for managing and executing these bindings.
Sources: packages/opencode/src/cli/cmd/tui/app.tsx:14, packages/opencode/src/cli/cmd/tui/context/keybind.tsx:10-15
Keybinding Management
The KeybindProvider maintains a list of active keybindings. Components can register or unregister keybindings using the useKeybind hook. This hook provides add and remove functions to dynamically manage the keybinding stack.
The following sequence diagram shows how a key press is processed and an action is triggered.
Default Keybindings
Keybindings are registered within components to provide context-specific actions. For example, in the Session component, several keybindings are set up for application-level commands.
| Keybinding | Action | Description |
|---|---|---|
ctrl+p | app.palette.open | Opens the command palette. |
ctrl+k | app.palette.open | Also opens the command palette. |
ctrl+l | app.log.open | Opens the application log. |
ctrl+s | app.session.new | Creates a new session. |
ctrl+w | app.session.stop | Stops the current session. |
Sources: packages/opencode/src/cli/cmd/tui/routes/session/index.tsx:39-50
Keybinding Utilities
The packages/opencode/src/util/keybind.ts file provides helper functions for parsing and formatting keybinding strings, ensuring a consistent representation throughout the application.
parse(value: string): Converts a string like"ctrl+p"into a structuredKeyobject.format(key: Key): Converts aKeyobject back into its string representation.
Sources: packages/opencode/src/util/keybind.ts
Agent Behavior and Prompts
The behavior of the AI agent within the TUI is defined by a set of system prompts. These prompts are text files that instruct the agent on its personality, core mandates, workflows, and operational guidelines. Different prompts can be used to configure agents for specific tasks or with different underlying LLMs (e.g., Gemini, Codex, Anthropic).
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt
Agent Workflow
The prompts establish a clear workflow for handling software engineering tasks. The gemini.txt prompt, for example, outlines a five-step process.
This flowchart visualizes the primary workflow for a software engineering task.
Agent Personas and Rules
Different prompt files establish different agent personas and operational rules. This allows OpenCode to be flexible and adapt its interaction style.
| Prompt File | Key Instruction / Mandate | Tone / Style |
|---|---|---|
gemini.txt | Rigorously adhere to existing project conventions. Mimic style, structure, and framework choices. | Concise & Direct. Minimal output. No chitchat. |
codex.txt | Obey instructions in AGENTS.md files. Use todowrite tool for planning. | Concise, direct, and friendly. |
beast.txt | Must use webfetch for internet research. Must plan extensively and reflect on tool call outcomes. | Casual, friendly yet professional. |
anthropic.txt | Use TodoWrite tools frequently. Prioritize technical accuracy over validating user beliefs. | Short and concise. |
Sources: packages/opencode/src/session/prompt/gemini.txt:6-10, 71-76, packages/opencode/src/session/prompt/codex.txt:17-25, 50-55, packages/opencode/src/session/prompt/beast.txt:13-18, 114-119, packages/opencode/src/session/prompt/anthropic.txt:26-31, 41-44
Summary
The OpenCode TUI is a sophisticated, terminal-native application that serves as the command center for interacting with the AI agent. Built with SolidJS and opentui, it provides a flexible and powerful user experience through its themeable interface, robust keybinding system, and multi-session capabilities. The agent's behavior is dynamically configured through a system of detailed prompts, allowing it to adapt to various tasks, coding styles, and user preferences, making the TUI an integral part of the OpenCode ecosystem.