> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch41-tui/41.2-tui-interaction-mechanisms.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 41.2 TUI Interaction Mechanisms

> **Generated by**: Claude Opus 5 (claude-opus-5\[1m]) **Token usage**: Input \~220,000 tokens, Output \~9,000 tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

§41.1 covered TUI architecture. This section covers interaction: **how events flow, how commands dispatch, and how local shell execution works.**

## 41.2.1 The Event Handling Pipeline

`src/tui/tui-event-handlers.ts` sits at the center. The TUI has two event sources that must merge into one render loop:

```
keyboard input ──┐
                 ├──→ state update ──→ re-render
Gateway events ──┘
```

Keyboard input is synchronous and local; Gateway events arrive asynchronously over WebSocket. Merging them into a single update path avoids the classic terminal-UI bug where two sources race and produce interleaved output.

## 41.2.2 Gateway Event Handling

The TUI connects as a `role: "operator"` client (§12.5.2) and receives the same event stream as any other operator client.

The events it cares about:

| Event                             | Rendered as                        |
| --------------------------------- | ---------------------------------- |
| `chat`                            | Message deltas and the final reply |
| `agent` with `stream: "tool"`     | Tool execution progress            |
| `agent` with `stream: "thinking"` | The thinking panel (§15.5.7)       |
| `presence` / `health`             | The status bar                     |

`src/tui/tui-stream-assembler.ts` reassembles streamed deltas into displayable text. That's necessary because deltas arrive at network granularity, not at anything you'd want to render — assembling and then re-rendering avoids a flickering screen.

## 41.2.3 The Command System

§33.4.2 covered this from the registry side. From the TUI's side:

```
what a TUI user sees = local UI commands ∪ the chat command registry
```

Local commands (`/exit`, `/settings`, the `/model` picker) are handled in-process; everything else goes to the Gateway.

**Local commands win on a name collision** (`src/tui/commands.ts:118`) — a `/model` that opens an interactive picker is more useful in a terminal than one that requires typing the model name.

Command handling lives in `src/tui/tui-command-handlers.ts`, dispatch and completion in `commands.ts`.

## 41.2.4 Session Operations

`src/tui/tui-session-actions.ts` exposes session management inside the TUI: listing, switching, resetting, and compacting.

These map onto the Gateway methods from Appendix B.4 (`sessions.list` / `sessions.reset` / `sessions.compact`) — the TUI adds no session semantics of its own, it just presents them.

## 41.2.5 Local Shell Execution

`src/tui/tui-local-shell.ts` is the one genuinely local capability: running a shell command **on the machine where the TUI is running**, not through the agent.

Why does that exist? Because the TUI may be connected to a remote Gateway. When you want to check something on your own machine, routing it through the agent would run it on the gateway host instead.

> **Note this bypasses the agent entirely, so none of the exec policy from §22.5.2 applies.** It's a direct operator capability, justified by the fact that whoever runs the TUI already has a shell on that machine.

## 41.2.6 Keyboard Shortcuts and Input Optimization

Two details worth mentioning:

**Multi-line input.** Chat messages frequently need line breaks, but Enter means send. The TUI distinguishes Enter from Shift+Enter, and handles paste containing newlines without sending prematurely.

**Waiting-state feedback.** `src/tui/tui-waiting.ts` renders progress while the agent runs — the terminal equivalent of the typing indicator from §13.6.5, and it faces the same requirement: **it must clear on every exit path**, including errors.

***

## Section Summary

1. **Two event sources merge into one update path** (keyboard + Gateway events), avoiding the terminal-UI race where two sources produce interleaved output.
2. **The TUI connects as an ordinary `operator` client**, receiving the same event stream as any other; `tui-stream-assembler.ts` reassembles network-granularity deltas before rendering to avoid flicker.
3. **What a TUI user sees = local UI commands ∪ the chat command registry**, with **local commands winning on collision** — an interactive `/model` picker beats typing a model name.
4. **Session operations add no semantics** — they present the Gateway's `sessions.*` methods.
5. **Local shell execution bypasses the agent entirely**, so exec policy doesn't apply — justified because whoever runs the TUI already has a shell on that machine. It exists because the TUI may be connected to a *remote* Gateway.
6. **Waiting-state feedback must clear on every exit path**, including errors — the same requirement as the typing indicator.
