Files
pipelines/app/lib/features/chat/shell/chat_shell_run.dart
T
alan cf9f71f27c Show dismissed commands as an answer footer chip; move Copy to the left gutter
Dismissed shell runs leave the transcript and fold into a "N commands"
chip in the footer of the first answer at or after each run (the last
answer for newer runs, or a row of its own without any answer). The chip
opens the turn sheet (wide) or a full-screen page (narrow) listing the
commands; each opens its output read-only with Copy output, Send to agent
and Restore.

On a wide conversation the answer's Copy button moves out of the footer
into a 48 px gutter left of the reading column (chatCopyGutter), shown on
hover or focus on desktops and always on touch; narrow widths keep it in
the footer.
2026-11-09 23:47:14 +00:00

201 lines
6.5 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 = '',
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 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 => 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,
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),
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;