Draw chat shell runs in an xterm terminal that takes typed input

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.
This commit is contained in:
2026-11-09 20:08:33 +00:00
parent 355b722a1d
commit 89882d90f3
20 changed files with 1428 additions and 201 deletions
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
import '../../../core/error_reason.dart';
import 'chat_shell_run.dart';
import 'chat_shell_runs_repository.dart';
import 'chat_shell_terminals.dart';
/// Where one run's "Send to agent" stands.
enum ChatShellShareState { idle, sending, sent }
@@ -19,10 +20,22 @@ enum ChatShellShareState { idle, sending, sent }
class ChatShellRunsController extends ChangeNotifier {
ChatShellRunsController(this.gateway, this.sessionId)
: _repository = ChatShellRunsRepository(gateway) {
terminals = ChatShellTerminals(
_repository,
sessionId,
onError: (id, error) {
if (_disposed) return;
_errors[id] = _reason(error);
_changed();
},
);
_frames = gateway.frames.listen(_onFrame, onError: (_) {});
unawaited(refresh());
}
/// The runs' terminal emulators and their input and size delivery.
late final ChatShellTerminals terminals;
final ApiGateway gateway;
final String sessionId;
final ChatShellRunsRepository _repository;
@@ -54,9 +67,20 @@ class ChatShellRunsController extends ChangeNotifier {
if (_disposed || token != _generation) return;
for (final run in listed) {
final held = _runs[run.id];
_runs[run.id] = held != null && held.outputEnd > run.outputEnd
? held.withLifecycle(run)
: run;
if (held != null &&
held.running &&
held.outputEnd > 0 &&
run.outputBytes > held.outputEnd) {
// A reconnect: fetch only what this copy missed.
unawaited(_resume(held));
continue;
}
final keep = held != null && held.outputEnd >= run.outputEnd;
_runs[run.id] = keep ? held.withLifecycle(run) : run;
if (!keep) terminals.replaced(run);
if (held != null && held.running && !run.running) {
terminals.finished(_runs[run.id]!);
}
}
_changed(membership: true);
} catch (_) {
@@ -67,7 +91,13 @@ class ChatShellRunsController extends ChangeNotifier {
/// Starts [command] in the chat's directory. Returns the refusal, or null.
Future<String?> start(String command) async {
try {
final run = await _repository.run(sessionId, command);
final size = terminals.lastSize;
final run = await _repository.run(
sessionId,
command,
cols: size?.$1,
rows: size?.$2,
);
if (_disposed) return null;
_runs.putIfAbsent(run.id, () => run);
_changed(membership: true);
@@ -107,28 +137,53 @@ class ChatShellRunsController extends ChangeNotifier {
_changed();
}
/// Every byte the server retained for [id]: what Copy output copies and
/// Show all draws. Null when it cannot be read (the error is recorded).
Future<ChatShellRun?> loadAll(String id) async {
final held = _runs[id];
if (held == null) return null;
if (!held.truncated && !held.running) return held;
/// The plain-text rendering of everything the server kept for [id]: what
/// Copy output copies. Null when it cannot be read (the error is recorded).
Future<String?> plainText(String id) async {
try {
final full = await _repository.output(sessionId, id);
if (_disposed) return null;
final latest = _runs[id] ?? held;
// A frame newer than the read keeps the card's own lifecycle facts.
final merged = full.withLifecycle(latest.running ? full : latest);
_runs[id] = merged;
_changed();
return merged;
// A server without terminals records plain output and no rendering.
return full.terminal || full.text.isNotEmpty ? full.text : full.output;
} catch (error) {
if (_disposed) return null;
_errors[id] = _reason(error);
_changed();
return null;
}
}
/// Reads what a running run printed while this copy was disconnected,
/// from the byte it holds up to; a gap redraws from what the server kept.
Future<void> _resume(ChatShellRun held) async {
try {
final tail = await _repository.output(
sessionId,
held.id,
after: held.outputEnd,
);
if (_disposed) return;
final latest = _runs[held.id] ?? held;
final next = latest
.withChunk(tail.output, tail.outputStart, tail.outputBytes)
.withLifecycle(latest.running ? tail : latest);
_runs[held.id] = next;
if (tail.outputStart == latest.outputEnd) {
terminals.appended(
next,
tail.output,
tail.outputStart,
tail.outputBytes,
);
} else {
terminals.replaced(next);
}
if (!next.running) terminals.finished(next);
_changed();
} catch (_) {
// The next frame or refresh repairs the copy.
}
}
void _onFrame(JsonMap envelope) {
if (_disposed || envelope['type'] != 'chat_shell_run_event') return;
final frame = envelope['chat_shell_run_event'];
@@ -137,8 +192,12 @@ class ChatShellRunsController extends ChangeNotifier {
final record = ChatShellRun.fromJson(frame['run']);
if (record != null) {
final held = _runs[record.id];
_runs[record.id] = held == null ? record : held.withLifecycle(record);
if (!record.running) _stopping.remove(record.id);
final next = held == null ? record : held.withLifecycle(record);
_runs[record.id] = next;
if (!record.running) {
_stopping.remove(record.id);
if (held != null && held.running) terminals.finished(next);
}
_changed(membership: held == null);
return;
}
@@ -148,11 +207,12 @@ class ChatShellRunsController extends ChangeNotifier {
unawaited(refresh());
return;
}
_runs[id] = held.withChunk(
'${frame['output'] ?? ''}',
(frame['offset'] as num?)?.toInt() ?? 0,
(frame['end'] as num?)?.toInt() ?? 0,
);
final text = '${frame['output'] ?? ''}';
final offset = (frame['offset'] as num?)?.toInt() ?? 0;
final end = (frame['end'] as num?)?.toInt() ?? 0;
final next = held.withChunk(text, offset, end);
_runs[id] = next;
terminals.appended(next, text, offset, end);
_changed();
}
@@ -169,6 +229,7 @@ class ChatShellRunsController extends ChangeNotifier {
void dispose() {
_disposed = true;
_generation++;
terminals.dispose();
unawaited(_frames?.cancel());
_membership.dispose();
super.dispose();