e13cb99ade
A headless xterm-compatible emulator (internal/termstate) follows each machine terminal's PTY output under the same lock as the 1 MiB tail. A terminal_output from before the retained tail (new page, reload, gap) now gets output_snapshot: an escape stream that resets the client and restores scrollback (1000 lines), main screen, the alternate screen when in use (via ?1049h so the program's exit brings the shell back), saved cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin, focus, ...), charsets, pen and title, as of output_end; frames continue there. Reads inside the tail stay raw. No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at 0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate runs at 160-200 MB/s. The app treats a snapshot reply as a replay without the dropped-output line, and every replay now starts with RIS so stale modes end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
6.8 KiB
Dart
209 lines
6.8 KiB
Dart
import '../../../core/api_gateway.dart';
|
|
|
|
/// How an operator shell command stands, as the card shows it.
|
|
enum ChatShellRunState { running, succeeded, failed, stopped, interrupted }
|
|
|
|
/// Characters of live output one card keeps; older text is dropped (and said
|
|
/// to be) so a flooding command cannot grow the transcript without bound. The
|
|
/// server keeps its own 1 MiB tail, which Copy output and Load all read.
|
|
const int chatShellLiveOutputLimit = 256 * 1024;
|
|
|
|
/// One command the operator ran in this chat (`chat_shell_run`), as recorded
|
|
/// by the server: never part of the agent's transcript.
|
|
class ChatShellRun {
|
|
const ChatShellRun({
|
|
required this.id,
|
|
required this.command,
|
|
required this.directory,
|
|
required this.status,
|
|
required this.startedAt,
|
|
this.exitCode,
|
|
this.signal = '',
|
|
this.error = '',
|
|
this.endedAt = 0,
|
|
this.durationMs = 0,
|
|
this.output = '',
|
|
this.outputStart = 0,
|
|
this.outputEnd = 0,
|
|
this.outputBytes = 0,
|
|
this.gap = false,
|
|
this.snapshot = false,
|
|
this.terminal = false,
|
|
this.cols = 0,
|
|
this.rows = 0,
|
|
this.text = '',
|
|
this.dismissedAt = 0,
|
|
this.purged = false,
|
|
});
|
|
|
|
final String id;
|
|
final String command;
|
|
final String directory;
|
|
|
|
/// The server's status word: running, exited, stopped, failed, interrupted.
|
|
final String status;
|
|
final int startedAt;
|
|
final int? exitCode;
|
|
final String signal;
|
|
final String error;
|
|
final int endedAt;
|
|
final int durationMs;
|
|
|
|
/// The text held here, covering absolute output bytes from [outputStart]
|
|
/// to [outputEnd] (0 when unknown, for a readback).
|
|
final String output;
|
|
final int outputStart;
|
|
final int outputEnd;
|
|
final int outputBytes;
|
|
|
|
/// True when live frames were missed, so [output] is not one contiguous
|
|
/// stretch of what the command wrote.
|
|
final bool gap;
|
|
|
|
/// True when [output] starts with the server's redraw of the terminal's
|
|
/// state at [outputStart] (screens, scrollback, cursor and modes) instead
|
|
/// of raw output: nothing earlier is missing from what it shows.
|
|
final bool snapshot;
|
|
|
|
/// True when the command runs in a pseudo-terminal of [cols] x [rows]:
|
|
/// [output] is then the raw terminal stream and it accepts typed input
|
|
/// while it runs. Older runs (and servers without terminals) are plain.
|
|
final bool terminal;
|
|
final int cols;
|
|
final int rows;
|
|
|
|
/// The server's plain-text rendering of its retained output (escape codes
|
|
/// removed); only a whole-output readback carries it.
|
|
final String text;
|
|
|
|
/// When the operator dismissed the run (epoch ms; 0 when not dismissed).
|
|
final int dismissedAt;
|
|
|
|
/// True when an older server deleted the dismissed run's command and
|
|
/// output: only its status and times are left, and it cannot be restored.
|
|
final bool purged;
|
|
|
|
/// Whether earlier output is not shown here.
|
|
bool get truncated => !snapshot && (outputStart > 0 || gap);
|
|
|
|
ChatShellRunState get state => switch (status) {
|
|
'running' => ChatShellRunState.running,
|
|
'stopped' => ChatShellRunState.stopped,
|
|
'interrupted' => ChatShellRunState.interrupted,
|
|
'exited' when exitCode == 0 && signal.isEmpty =>
|
|
ChatShellRunState.succeeded,
|
|
_ => ChatShellRunState.failed,
|
|
};
|
|
|
|
bool get running => state == ChatShellRunState.running;
|
|
|
|
static ChatShellRun? fromJson(Object? value) {
|
|
if (value is! Map) return null;
|
|
final id = '${value['id'] ?? ''}'.trim();
|
|
if (id.isEmpty) return null;
|
|
final output = '${value['output'] ?? ''}';
|
|
final start = _int(value['output_start']);
|
|
final bytes = _int(value['output_bytes']);
|
|
return ChatShellRun(
|
|
id: id,
|
|
command: '${value['command'] ?? ''}',
|
|
directory: '${value['directory'] ?? ''}',
|
|
status: '${value['status'] ?? ''}',
|
|
startedAt: _int(value['started_at']),
|
|
exitCode: value['exit_code'] is num
|
|
? (value['exit_code'] as num).toInt()
|
|
: null,
|
|
signal: '${value['signal'] ?? ''}',
|
|
error: '${value['error'] ?? ''}',
|
|
endedAt: _int(value['ended_at']),
|
|
durationMs: _int(value['duration_ms']),
|
|
output: output,
|
|
outputStart: start,
|
|
// A readback's text runs to the newest byte the server had.
|
|
outputEnd: output.isEmpty && start == 0 ? 0 : bytes,
|
|
outputBytes: bytes,
|
|
terminal: value['terminal'] == true,
|
|
cols: _int(value['cols']),
|
|
rows: _int(value['rows']),
|
|
text: '${value['text'] ?? ''}',
|
|
dismissedAt: _int(value['dismissed_at']),
|
|
purged: value['purged'] == true,
|
|
);
|
|
}
|
|
|
|
/// This run with a lifecycle frame's facts; the held output stays.
|
|
ChatShellRun withLifecycle(ChatShellRun next) => ChatShellRun(
|
|
id: id,
|
|
command: next.command.isEmpty ? command : next.command,
|
|
directory: next.directory.isEmpty ? directory : next.directory,
|
|
status: next.status,
|
|
startedAt: next.startedAt == 0 ? startedAt : next.startedAt,
|
|
exitCode: next.exitCode,
|
|
signal: next.signal,
|
|
error: next.error,
|
|
endedAt: next.endedAt,
|
|
durationMs: next.durationMs,
|
|
output: output,
|
|
outputStart: outputStart,
|
|
outputEnd: outputEnd,
|
|
outputBytes: next.outputBytes > outputBytes
|
|
? next.outputBytes
|
|
: outputBytes,
|
|
gap: gap,
|
|
snapshot: snapshot,
|
|
terminal: next.terminal || terminal,
|
|
cols: next.cols == 0 ? cols : next.cols,
|
|
rows: next.rows == 0 ? rows : next.rows,
|
|
text: text,
|
|
dismissedAt: next.dismissedAt,
|
|
purged: next.purged,
|
|
);
|
|
|
|
/// This run with one live output chunk covering bytes [offset, end). A
|
|
/// chunk this copy already holds is ignored; one after a hole starts a new
|
|
/// stretch and marks the gap rather than joining unrelated text.
|
|
ChatShellRun withChunk(String text, int offset, int end) {
|
|
if (end <= outputEnd) return this;
|
|
final contiguous = offset == outputEnd;
|
|
var joined = contiguous ? output + text : text;
|
|
var dropped = !contiguous && outputEnd > 0;
|
|
if (joined.length > chatShellLiveOutputLimit) {
|
|
joined = joined.substring(joined.length - chatShellLiveOutputLimit);
|
|
dropped = true;
|
|
}
|
|
return ChatShellRun(
|
|
id: id,
|
|
command: command,
|
|
directory: directory,
|
|
status: status,
|
|
startedAt: startedAt,
|
|
exitCode: exitCode,
|
|
signal: signal,
|
|
error: error,
|
|
endedAt: endedAt,
|
|
durationMs: durationMs,
|
|
output: joined,
|
|
outputStart: contiguous ? outputStart : offset,
|
|
outputEnd: end,
|
|
outputBytes: end > outputBytes ? end : outputBytes,
|
|
gap: gap || dropped || (!contiguous && offset > 0),
|
|
snapshot: snapshot && contiguous && !dropped,
|
|
terminal: terminal,
|
|
cols: cols,
|
|
rows: rows,
|
|
);
|
|
}
|
|
|
|
JsonMap toRow() => {
|
|
'kind': chatShellRunRowKind,
|
|
'key': 'chat-shell-run-$id',
|
|
'run_id': id,
|
|
'created_at': startedAt,
|
|
};
|
|
}
|
|
|
|
/// The transcript row kind of one operator command card.
|
|
const String chatShellRunRowKind = 'shell_run';
|
|
|
|
int _int(Object? value) => value is num ? value.toInt() : 0;
|