89882d90f3
The card's output area is now an xterm.dart emulator kept per run by the controller: it is fed the raw stream in order, takes typing and paste while the command runs (with a key toolbar and a soft-keyboard field on touch platforms), resizes the process to its layout, opens printed links, shrinks finished runs to their output and opens full screen. A reconnect resumes from the byte the card holds; Copy output copies the server's plain text.
188 lines
6.0 KiB
Dart
188 lines
6.0 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.terminal = false,
|
|
this.cols = 0,
|
|
this.rows = 0,
|
|
this.text = '',
|
|
});
|
|
|
|
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 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;
|
|
|
|
/// Whether earlier output is not shown here.
|
|
bool get truncated => 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'] ?? ''}',
|
|
);
|
|
}
|
|
|
|
/// 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,
|
|
terminal: next.terminal || terminal,
|
|
cols: next.cols == 0 ? cols : next.cols,
|
|
rows: next.rows == 0 ? rows : next.rows,
|
|
text: text,
|
|
);
|
|
|
|
/// 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),
|
|
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;
|