1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
mod commands;
mod daemon;
mod keys;
mod output;
mod pty;
mod render;
use clap::{Parser, Subcommand};
use daemon::protocol::TermSize;
use output::resolve_format;
#[derive(Debug, Parser)]
#[command(
name = "tu",
version,
about = "Headless virtual terminal for AI agents"
)]
struct Cli {
/// Output as JSON (auto-detected when stdout is not a TTY).
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// Print a compact LLM-friendly command reference.
Usage,
/// Spawn a process in a new virtual terminal.
Run {
/// Command to run.
command: String,
/// Arguments to the command.
#[arg(trailing_var_arg = true)]
args: Vec<String>,
/// Session name (default: "default").
#[arg(long)]
name: Option<String>,
/// Terminal size as COLSxROWS (default: 120x40).
#[arg(long, default_value = "120x40", value_parser = parse_size)]
size: TermSize,
/// Scrollback buffer lines (default: 1000).
#[arg(long, default_value = "1000")]
scrollback: usize,
/// Extra environment variables (KEY=VAL).
#[arg(long = "env", value_parser = parse_env)]
envs: Vec<(String, String)>,
/// Working directory.
#[arg(long)]
cwd: Option<String>,
/// TERM environment variable (default: xterm-256color).
#[arg(long, default_value = "xterm-256color")]
term: String,
/// Wrap command in $SHELL -c "...".
#[arg(long)]
shell: bool,
},
/// Kill process and remove session.
Kill {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// List active sessions.
List,
/// Session info: pid, alive/exited, exit code, size.
Status {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Capture the terminal screen.
Screenshot {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
// TODO Phase 2: --png, --ansi, --html, --out
},
/// Print cursor position as row,col.
Cursor {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Print scrollback buffer.
Scrollback {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
/// Number of lines (default: all).
#[arg(long)]
lines: Option<usize>,
},
/// Type literal text into the terminal.
Type {
/// Text to type.
text: String,
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Send keystrokes to the terminal.
Press {
/// Key names (space-separated): Enter, Tab, F1, Ctrl+C, Up, etc.
#[arg(required = true)]
keys: Vec<String>,
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Paste text using bracketed paste mode.
Paste {
/// Text to paste.
text: String,
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Resize the terminal.
Resize {
/// New size as COLSxROWS (e.g. 160x50).
#[arg(value_parser = parse_size)]
size: TermSize,
/// Session name.
#[arg(long, default_value = "default")]
name: String,
},
/// Wait for a condition on the terminal screen.
Wait {
/// Session name.
#[arg(long, default_value = "default")]
name: String,
/// Wait until screen is unchanged for N milliseconds.
#[arg(long)]
stable: Option<u64>,
/// Wait until regex matches screen content.
#[arg(long)]
text: Option<String>,
/// Maximum wait time in milliseconds (default: 5000).
#[arg(long, default_value = "5000")]
timeout: u64,
},
/// Live read-only view of a session.
Monitor {
/// Session name (default: "default").
#[arg(long, default_value = "default")]
name: String,
},
/// Manage the background daemon.
Daemon {
#[command(subcommand)]
action: DaemonAction,
},
}
#[derive(Debug, Subcommand)]
enum DaemonAction {
/// Start the daemon (foreground).
Start,
/// Stop the daemon.
Stop,
/// Show daemon status.
Status,
}
fn parse_size(s: &str) -> Result<TermSize, String> {
let parts: Vec<&str> = s.split('x').collect();
if parts.len() != 2 {
return Err(format!(
"Invalid size format: {s:?}. Expected COLSxROWS (e.g. 120x40)"
));
}
let cols = parts[0]
.parse::<u16>()
.map_err(|_| format!("Invalid columns: {:?}", parts[0]))?;
let rows = parts[1]
.parse::<u16>()
.map_err(|_| format!("Invalid rows: {:?}", parts[1]))?;
Ok(TermSize { cols, rows })
}
fn parse_env(s: &str) -> Result<(String, String), String> {
let pos = s
.find('=')
.ok_or_else(|| format!("Invalid env format: {s:?}. Expected KEY=VALUE"))?;
Ok((s[..pos].to_string(), s[pos + 1..].to_string()))
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
let format = resolve_format(cli.json);
let result = match cli.command {
// These commands don't need the daemon
Command::Usage => {
commands::usage::run().await;
Ok(())
}
Command::Daemon { action } => match action {
DaemonAction::Start => commands::daemon_cmd::start().await,
DaemonAction::Stop => commands::daemon_cmd::stop().await,
DaemonAction::Status => commands::daemon_cmd::status().await,
},
// All other commands talk to the daemon
Command::Run {
command,
args,
name,
size,
scrollback,
envs,
cwd,
term,
shell,
} => {
commands::run::run(
command, args, name, size, scrollback, envs, cwd, term, shell, format,
)
.await
}
Command::Kill { name } => commands::kill::run(name).await,
Command::List => commands::list::run(format).await,
Command::Status { name } => commands::status::run(name, format).await,
Command::Screenshot { name } => commands::screenshot::run(name, format).await,
Command::Cursor { name } => commands::cursor::run(name, format).await,
Command::Scrollback { name, lines } => commands::scrollback::run(name, lines, format).await,
Command::Type { text, name } => commands::type_text::run(name, text).await,
Command::Press { keys, name } => commands::press::run(name, keys).await,
Command::Paste { text, name } => commands::paste::run(name, text).await,
Command::Resize { size, name } => commands::resize::run(name, size).await,
Command::Wait {
name,
stable,
text,
timeout,
} => commands::wait::run(name, stable, text, timeout).await,
Command::Monitor { name } => commands::monitor::run(name).await,
};
if let Err(e) = result {
eprintln!("Error: {e}");
std::process::exit(1);
}
}