Run shell commands from the chat composer and show them as cards
A draft starting with ! shows a Shell mode indicator and runs through chat_shell_run instead of being sent to the agent; the chat's actions menu gains Run command. Each run is a terminal-style card among the turns with live output, Stop, its exit status, Copy output and Send to agent.
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
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,
|
||||
});
|
||||
|
||||
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;
|
||||
|
||||
/// 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,
|
||||
);
|
||||
}
|
||||
|
||||
/// 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,
|
||||
);
|
||||
|
||||
/// 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),
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
Reference in New Issue
Block a user