From 89882d90f3ec39a92f93ae13b27e862c092546c5 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 9 Nov 2026 20:08:33 +0000 Subject: [PATCH] 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. --- .../chat/shell/chat_shell_card_actions.dart | 36 ++- .../chat/shell/chat_shell_metrics.dart | 46 +--- .../chat/shell/chat_shell_output_view.dart | 130 +++------- .../features/chat/shell/chat_shell_run.dart | 26 ++ .../chat/shell/chat_shell_run_card.dart | 33 ++- .../shell/chat_shell_runs_controller.dart | 109 +++++++-- .../shell/chat_shell_runs_repository.dart | 55 ++++- .../chat/shell/chat_shell_terminal_keys.dart | 229 ++++++++++++++++++ .../chat/shell/chat_shell_terminal_links.dart | 38 +++ .../chat/shell/chat_shell_terminal_page.dart | 98 ++++++++ .../shell/chat_shell_terminal_session.dart | 112 +++++++++ .../chat/shell/chat_shell_terminal_theme.dart | 85 +++++++ .../chat/shell/chat_shell_terminal_view.dart | 113 +++++++++ .../chat/shell/chat_shell_terminals.dart | 142 +++++++++++ app/pubspec.lock | 40 +++ app/pubspec.yaml | 1 + .../chat/shell/chat_shell_fixtures.dart | 31 ++- .../chat/shell/chat_shell_run_card_test.dart | 31 ++- .../chat_shell_terminal_session_test.dart | 87 +++++++ .../chat/shell/chat_shell_terminal_test.dart | 187 ++++++++++++++ 20 files changed, 1428 insertions(+), 201 deletions(-) create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_keys.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_links.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_page.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_session.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_theme.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminal_view.dart create mode 100644 app/lib/features/chat/shell/chat_shell_terminals.dart create mode 100644 app/test/features/chat/shell/chat_shell_terminal_session_test.dart create mode 100644 app/test/features/chat/shell/chat_shell_terminal_test.dart diff --git a/app/lib/features/chat/shell/chat_shell_card_actions.dart b/app/lib/features/chat/shell/chat_shell_card_actions.dart index e5ddff8e..3eea6a65 100644 --- a/app/lib/features/chat/shell/chat_shell_card_actions.dart +++ b/app/lib/features/chat/shell/chat_shell_card_actions.dart @@ -4,7 +4,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'chat_shell_metrics.dart'; import 'chat_shell_run.dart'; import 'chat_shell_runs_controller.dart'; @@ -15,26 +14,23 @@ const String chatShellCopiedLabel = 'Copied'; const String chatShellShareLabel = 'Send to agent'; const String chatShellSharingLabel = 'Sending…'; const String chatShellSharedLabel = 'Sent to agent'; -const String chatShellExpandLabel = 'Expand'; -const String chatShellCollapseLabel = 'Collapse'; +const String chatShellFullScreenLabel = 'Full screen'; /// The card's host buttons: Stop while running; Copy output, Send to agent -/// once finished; Expand/Collapse for the output area. +/// once finished; Full screen for the terminal. class ChatShellCardActions extends StatefulWidget { const ChatShellCardActions({ super.key, required this.run, required this.controller, required this.cupertino, - required this.expanded, - required this.onToggleExpanded, + required this.onFullScreen, }); final ChatShellRun run; final ChatShellRunsController controller; final bool cupertino; - final bool expanded; - final VoidCallback onToggleExpanded; + final VoidCallback onFullScreen; @override State createState() => _ChatShellCardActionsState(); @@ -50,12 +46,12 @@ class _ChatShellCardActionsState extends State { super.dispose(); } - /// Copies everything the server kept, reading it first when this card - /// holds only the newest part. + /// Copies the plain text of everything the server kept (no terminal + /// escape sequences), read from the server. Future _copy() async { - final full = await widget.controller.loadAll(widget.run.id); - if (full == null) return; - await Clipboard.setData(ClipboardData(text: full.output)); + final text = await widget.controller.plainText(widget.run.id); + if (text == null) return; + await Clipboard.setData(ClipboardData(text: text)); if (!mounted) return; setState(() => _copied = true); _reset?.cancel(); @@ -114,17 +110,13 @@ class _ChatShellCardActionsState extends State { ? () => unawaited(controller.share(run.id)) : null, ), - if (widget.expanded || chatShellOutputMayOverflow(run.output)) + if (run.running || run.output.isNotEmpty) _button( key: const Key('chat-shell-run-expand'), - label: widget.expanded - ? chatShellCollapseLabel - : chatShellExpandLabel, - material: widget.expanded ? Icons.unfold_less : Icons.unfold_more, - cupertino: widget.expanded - ? CupertinoIcons.chevron_up - : CupertinoIcons.chevron_down, - onPressed: widget.onToggleExpanded, + label: chatShellFullScreenLabel, + material: Icons.fullscreen, + cupertino: CupertinoIcons.fullscreen, + onPressed: widget.onFullScreen, ), ], ); diff --git a/app/lib/features/chat/shell/chat_shell_metrics.dart b/app/lib/features/chat/shell/chat_shell_metrics.dart index c375c7c9..e4d0f92a 100644 --- a/app/lib/features/chat/shell/chat_shell_metrics.dart +++ b/app/lib/features/chat/shell/chat_shell_metrics.dart @@ -1,43 +1,5 @@ -import 'package:flutter/widgets.dart'; - -/// The one layout bound of a shell command card: how tall its output area -/// may grow. Collapsed it shows about [kChatShellOutputCollapsedLines] lines -/// of the host's (scaled) monospace text; expanded it may take -/// [kChatShellOutputExpandedShare] of the window height. Everything else in -/// the card is content-sized. +/// The one layout bound of a shell command card: how many terminal rows its +/// output area shows. A running command's terminal has this many rows; a +/// finished one shrinks to the rows its output uses, up to this many, and +/// scrolls within them. Full screen takes the whole window instead. const int kChatShellOutputCollapsedLines = 12; -const double kChatShellOutputExpandedShare = 0.6; - -/// The monospace line height factor the output text is set at. -const double kChatShellOutputLineHeight = 1.35; - -/// Characters of output drawn at once. A command can print far more than a -/// text layout should carry; the card says when it shows only the newest -/// part, and Copy output still copies everything the server kept. -const int kChatShellRenderedOutputLimit = 64 * 1024; - -/// Whether [output] may be taller than the collapsed area, so Expand is worth -/// offering. Counted in lines, with long lines counted as wrapping. -bool chatShellOutputMayOverflow(String output) { - var lines = 0; - for (final line in output.split('\n')) { - lines += 1 + line.length ~/ 80; - if (lines > kChatShellOutputCollapsedLines) return true; - } - return false; -} - -/// The output area's maximum height for this window, text scale and font. -double chatShellOutputMaxHeight( - BuildContext context, { - required double fontSize, - required bool expanded, -}) { - if (expanded) { - return MediaQuery.sizeOf(context).height * kChatShellOutputExpandedShare; - } - final line = - MediaQuery.textScalerOf(context).scale(fontSize) * - kChatShellOutputLineHeight; - return line * kChatShellOutputCollapsedLines; -} diff --git a/app/lib/features/chat/shell/chat_shell_output_view.dart b/app/lib/features/chat/shell/chat_shell_output_view.dart index 6e5421d7..b699c0dc 100644 --- a/app/lib/features/chat/shell/chat_shell_output_view.dart +++ b/app/lib/features/chat/shell/chat_shell_output_view.dart @@ -1,119 +1,65 @@ -import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import '../../../ui/text/monospace.dart'; import '../conversation/chat_card_styles.dart'; -import 'chat_shell_metrics.dart'; import 'chat_shell_run.dart'; +import 'chat_shell_runs_controller.dart'; +import 'chat_shell_terminal_keys.dart'; +import 'chat_shell_terminal_view.dart'; -/// The terminal-style output area of one command card: the host's monospace -/// body on a quiet inset surface, scrollable inside a bounded height and -/// anchored at the newest line while the command runs. -class ChatShellOutputView extends StatefulWidget { +/// The output area of one command card: the run's terminal emulator, which +/// takes typing while the command runs (with the key toolbar below it), or +/// its retained output read-only once it ended. +class ChatShellOutputView extends StatelessWidget { const ChatShellOutputView({ super.key, required this.run, + required this.controller, required this.cupertino, - required this.expanded, + required this.viewKey, + required this.fullScreen, }); final ChatShellRun run; + final ChatShellRunsController controller; final bool cupertino; - final bool expanded; - @override - State createState() => _ChatShellOutputViewState(); -} + /// Replaced when the full-screen view closes, so the card's view sizes the + /// terminal to itself again. + final Key viewKey; -class _ChatShellOutputViewState extends State { - final ScrollController _scroll = ScrollController(); - - @override - void dispose() { - _scroll.dispose(); - super.dispose(); - } + /// True while the full-screen view sizes the terminal. + final bool fullScreen; @override Widget build(BuildContext context) { - final run = widget.run; - final cupertino = widget.cupertino; - final body = chatCardBodyStyle(context, cupertino) ?? const TextStyle(); - final fontSize = (body.fontSize ?? 14) * 0.95; - final style = body.copyWith( - fontFamily: appMonospaceFamily, - fontSize: fontSize, - height: kChatShellOutputLineHeight, - ); - final secondary = chatCardSecondaryStyle(context, cupertino); - var text = run.output; - final clipped = text.length > kChatShellRenderedOutputLimit; - if (clipped) { - text = text.substring(text.length - kChatShellRenderedOutputLimit); + if (!run.running && run.output.isEmpty) { + return Text( + 'No output.', + key: const Key('chat-shell-output-empty'), + style: chatCardSecondaryStyle(context, cupertino), + ); } - // The final newline ends the last line; it is not an empty line to draw. - if (text.endsWith('\n')) text = text.substring(0, text.length - 1); - final notes = [ - if (run.truncated) 'Earlier output was dropped.', - if (clipped) - 'Showing only the newest part; Copy output copies all of it.', - ]; + final session = controller.terminals.session(run); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, + mainAxisSize: MainAxisSize.min, children: [ - if (notes.isNotEmpty) - Padding( - padding: const EdgeInsets.only(bottom: 4), - child: Text( - notes.join(' '), - key: const Key('chat-shell-output-truncated'), - style: secondary, - ), - ), - DecoratedBox( - decoration: BoxDecoration( - color: chatCardFillColor(context, cupertino), - borderRadius: BorderRadius.circular(8), - ), - child: ConstrainedBox( - constraints: BoxConstraints( - maxHeight: chatShellOutputMaxHeight( - context, - fontSize: fontSize, - expanded: widget.expanded, - ), - ), - child: _scrollable( - Padding( - padding: const EdgeInsets.all(10), - child: text.isEmpty - ? Text( - run.running ? 'No output yet.' : 'No output.', - style: secondary, - ) - : Text( - text, - key: const Key('chat-shell-output-text'), - style: style, - ), - ), - ), - ), + ChatShellTerminalView( + key: viewKey, + session: session, + cupertino: cupertino, + autoResize: !fullScreen, ), + if (session.live && !fullScreen) + Padding( + padding: const EdgeInsets.only(top: 8), + child: ChatShellTerminalKeys( + session: session, + cupertino: cupertino, + field: chatShellTouchFirst(), + ), + ), ], ); } - - /// A short output sizes the box to itself; a long one scrolls inside it, - /// starting from (and following) the newest line. - Widget _scrollable(Widget child) { - final scroll = SingleChildScrollView( - controller: _scroll, - reverse: true, - child: SizedBox(width: double.infinity, child: child), - ); - return widget.cupertino - ? CupertinoScrollbar(controller: _scroll, child: scroll) - : Scrollbar(controller: _scroll, child: scroll); - } } diff --git a/app/lib/features/chat/shell/chat_shell_run.dart b/app/lib/features/chat/shell/chat_shell_run.dart index 37ff8917..d23f5e74 100644 --- a/app/lib/features/chat/shell/chat_shell_run.dart +++ b/app/lib/features/chat/shell/chat_shell_run.dart @@ -27,6 +27,10 @@ class ChatShellRun { this.outputEnd = 0, this.outputBytes = 0, this.gap = false, + this.terminal = false, + this.cols = 0, + this.rows = 0, + this.text = '', }); final String id; @@ -53,6 +57,17 @@ class ChatShellRun { /// 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; @@ -92,6 +107,10 @@ class ChatShellRun { // 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'] ?? ''}', ); } @@ -114,6 +133,10 @@ class ChatShellRun { ? 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 @@ -144,6 +167,9 @@ class ChatShellRun { outputEnd: end, outputBytes: end > outputBytes ? end : outputBytes, gap: gap || dropped || (!contiguous && offset > 0), + terminal: terminal, + cols: cols, + rows: rows, ); } diff --git a/app/lib/features/chat/shell/chat_shell_run_card.dart b/app/lib/features/chat/shell/chat_shell_run_card.dart index 628dfd2b..f8324321 100644 --- a/app/lib/features/chat/shell/chat_shell_run_card.dart +++ b/app/lib/features/chat/shell/chat_shell_run_card.dart @@ -10,10 +10,12 @@ import 'chat_shell_output_view.dart'; import 'chat_shell_run.dart'; import 'chat_shell_runs_controller.dart'; import 'chat_shell_status.dart'; +import 'chat_shell_terminal_page.dart'; -/// One operator command in the transcript, as a terminal-style card in the -/// reading column: the command and folder, its live output, a spinner and -/// Stop while it runs, then its exit status, Copy output and Send to agent. +/// One operator command in the transcript, as a terminal card in the reading +/// column: the command and folder, its terminal (typed into while it runs), +/// a spinner and Stop while it runs, then its exit status, Copy output and +/// Send to agent. Full screen opens the same terminal over the whole window. /// /// It listens to [controller] itself, so streamed output rebuilds only this /// card, never the transcript. @@ -32,7 +34,23 @@ class ChatShellRunCard extends StatefulWidget { } class _ChatShellRunCardState extends State { - bool _expanded = false; + bool _fullScreen = false; + int _viewGeneration = 0; + + Future _openFullScreen(bool cupertino) async { + setState(() => _fullScreen = true); + await openChatShellTerminalPage( + context, + controller: widget.controller, + runId: widget.runId, + cupertino: cupertino, + ); + if (!mounted) return; + setState(() { + _fullScreen = false; + _viewGeneration++; + }); + } @override Widget build(BuildContext context) { @@ -113,8 +131,10 @@ class _ChatShellRunCardState extends State { const SizedBox(height: 12), ChatShellOutputView( run: run, + controller: controller, cupertino: cupertino, - expanded: _expanded, + viewKey: ValueKey(_viewGeneration), + fullScreen: _fullScreen, ), if (run.error.isNotEmpty && !run.running) Padding( @@ -129,8 +149,7 @@ class _ChatShellRunCardState extends State { run: run, controller: controller, cupertino: cupertino, - expanded: _expanded, - onToggleExpanded: () => setState(() => _expanded = !_expanded), + onFullScreen: () => _openFullScreen(cupertino), ), if (error != null) Semantics( diff --git a/app/lib/features/chat/shell/chat_shell_runs_controller.dart b/app/lib/features/chat/shell/chat_shell_runs_controller.dart index ba17a18d..24084f35 100644 --- a/app/lib/features/chat/shell/chat_shell_runs_controller.dart +++ b/app/lib/features/chat/shell/chat_shell_runs_controller.dart @@ -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 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 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 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 _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(); diff --git a/app/lib/features/chat/shell/chat_shell_runs_repository.dart b/app/lib/features/chat/shell/chat_shell_runs_repository.dart index c355d173..dfb2a681 100644 --- a/app/lib/features/chat/shell/chat_shell_runs_repository.dart +++ b/app/lib/features/chat/shell/chat_shell_runs_repository.dart @@ -6,19 +6,52 @@ class ChatShellRunsRepository { const ChatShellRunsRepository(this.gateway); final ApiGateway gateway; - JsonMap _body(String sessionId, {String? command, String? runId}) => { + JsonMap _body( + String sessionId, { + String? command, + String? runId, + int? cols, + int? rows, + String? data, + int? after, + }) => { 'chat_shell_run_request': { 'session_id': sessionId, 'command': ?command, 'run_id': ?runId, + 'cols': ?cols, + 'rows': ?rows, + 'data': ?data, + 'after': ?after, }, }; - /// Starts [command]; the reply is its running record. - Future run(String sessionId, String command) async => _record( - await gateway.call('chat_shell_run', _body(sessionId, command: command)), + /// Starts [command] in a terminal of [cols] x [rows] (the server's default + /// size when null); the reply is its running record. + Future run( + String sessionId, + String command, { + int? cols, + int? rows, + }) async => _record( + await gateway.call( + 'chat_shell_run', + _body(sessionId, command: command, cols: cols, rows: rows), + ), ); + /// Writes typed text (control characters and escape sequences included) + /// to a running command's terminal. + Future input(String sessionId, String runId, String data) => gateway + .call('chat_shell_input', _body(sessionId, runId: runId, data: data)); + + /// Sets a running command's terminal size. + Future resize(String sessionId, String runId, int cols, int rows) => + gateway.call( + 'chat_shell_resize', + _body(sessionId, runId: runId, cols: cols, rows: rows), + ); + Future stop(String sessionId, String runId) => gateway.call('chat_shell_stop', _body(sessionId, runId: runId)); @@ -34,9 +67,17 @@ class ChatShellRunsRepository { ]; } - /// One run with the whole output the server retained. - Future output(String sessionId, String runId) async => _record( - await gateway.call('chat_shell_run_output', _body(sessionId, runId: runId)), + /// One run with the whole output the server retained and its plain-text + /// rendering, or only the output from absolute byte [after] on. + Future output( + String sessionId, + String runId, { + int? after, + }) async => _record( + await gateway.call( + 'chat_shell_run_output', + _body(sessionId, runId: runId, after: after), + ), ); /// Queues an ordinary prompt carrying the run for the agent. diff --git a/app/lib/features/chat/shell/chat_shell_terminal_keys.dart b/app/lib/features/chat/shell/chat_shell_terminal_keys.dart new file mode 100644 index 00000000..077a7470 --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_keys.dart @@ -0,0 +1,229 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:xterm/core.dart'; + +import 'chat_shell_terminal_session.dart'; + +/// One key of the terminal toolbar: its label, icons and what it sends. +typedef _Key = ({ + String id, + String label, + IconData? material, + IconData? cupertino, + void Function(Terminal terminal) send, +}); + +final List<_Key> _keys = [ + ( + id: 'enter', + label: 'Enter', + material: Icons.keyboard_return, + cupertino: CupertinoIcons.return_icon, + send: (t) => t.keyInput(TerminalKey.enter), + ), + ( + id: 'ctrl-c', + label: 'Ctrl-C', + material: null, + cupertino: null, + send: (t) => t.textInput('\x03'), + ), + ( + id: 'tab', + label: 'Tab', + material: Icons.keyboard_tab, + cupertino: CupertinoIcons.arrow_right_to_line, + send: (t) => t.keyInput(TerminalKey.tab), + ), + ( + id: 'esc', + label: 'Esc', + material: null, + cupertino: null, + send: (t) => t.keyInput(TerminalKey.escape), + ), + ( + id: 'left', + label: 'Left', + material: Icons.keyboard_arrow_left, + cupertino: CupertinoIcons.arrow_left, + send: (t) => t.keyInput(TerminalKey.arrowLeft), + ), + ( + id: 'up', + label: 'Up', + material: Icons.keyboard_arrow_up, + cupertino: CupertinoIcons.arrow_up, + send: (t) => t.keyInput(TerminalKey.arrowUp), + ), + ( + id: 'down', + label: 'Down', + material: Icons.keyboard_arrow_down, + cupertino: CupertinoIcons.arrow_down, + send: (t) => t.keyInput(TerminalKey.arrowDown), + ), + ( + id: 'right', + label: 'Right', + material: Icons.keyboard_arrow_right, + cupertino: CupertinoIcons.arrow_right, + send: (t) => t.keyInput(TerminalKey.arrowRight), + ), +]; + +/// The keys a touch keyboard lacks, and Paste, for a running terminal; with +/// [field], also a text line whose text is typed into the process on Enter +/// (the soft keyboard's own way in). The field is emptied as soon as its +/// text is sent and can hide what is typed, for a token or password. +class ChatShellTerminalKeys extends StatefulWidget { + const ChatShellTerminalKeys({ + super.key, + required this.session, + required this.cupertino, + required this.field, + }); + + final ChatShellTerminalSession session; + final bool cupertino; + final bool field; + + @override + State createState() => _ChatShellTerminalKeysState(); +} + +class _ChatShellTerminalKeysState extends State { + final TextEditingController _text = TextEditingController(); + bool _hidden = false; + + @override + void dispose() { + _text.dispose(); + super.dispose(); + } + + Terminal get _terminal => widget.session.terminal; + + Future _paste() async { + final data = await Clipboard.getData(Clipboard.kTextPlain); + final text = data?.text; + if (text != null && text.isNotEmpty && widget.session.live) { + _terminal.paste(text); + } + } + + /// Types the field's text and Enter, then forgets it. + void _submit() { + final text = _text.text; + _text.clear(); + if (text.isNotEmpty) _terminal.textInput(text); + _terminal.keyInput(TerminalKey.enter); + } + + @override + Widget build(BuildContext context) { + final keys = Wrap( + spacing: 4, + runSpacing: 4, + children: [ + _button( + 'paste', + 'Paste', + Icons.content_paste, + CupertinoIcons.doc_on_clipboard, + _paste, + ), + for (final key in _keys) + _button( + key.id, + key.label, + key.material, + key.cupertino, + () => key.send(_terminal), + ), + ], + ); + if (!widget.field) return keys; + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + mainAxisSize: MainAxisSize.min, + children: [ + Padding(padding: const EdgeInsets.only(bottom: 8), child: _field()), + keys, + ], + ); + } + + Widget _field() { + final toggle = _hidden ? 'Show typing' : 'Hide typing'; + if (widget.cupertino) { + return CupertinoTextField( + key: const Key('chat-shell-terminal-field'), + controller: _text, + placeholder: 'Type input, then Enter', + obscureText: _hidden, + autocorrect: false, + enableSuggestions: false, + textInputAction: TextInputAction.send, + onSubmitted: (_) => _submit(), + suffix: CupertinoButton( + key: const Key('chat-shell-terminal-hide'), + padding: const EdgeInsets.symmetric(horizontal: 8), + onPressed: () => setState(() => _hidden = !_hidden), + child: Icon( + _hidden ? CupertinoIcons.eye : CupertinoIcons.eye_slash, + semanticLabel: toggle, + ), + ), + ); + } + return TextField( + key: const Key('chat-shell-terminal-field'), + controller: _text, + obscureText: _hidden, + autocorrect: false, + enableSuggestions: false, + textInputAction: TextInputAction.send, + onSubmitted: (_) => _submit(), + decoration: InputDecoration( + labelText: 'Type input, then Enter', + suffixIcon: IconButton( + key: const Key('chat-shell-terminal-hide'), + tooltip: toggle, + onPressed: () => setState(() => _hidden = !_hidden), + icon: Icon(_hidden ? Icons.visibility : Icons.visibility_off), + ), + ), + ); + } + + Widget _button( + String id, + String label, + IconData? material, + IconData? cupertino, + VoidCallback onPressed, + ) { + final key = Key('chat-shell-key-$id'); + if (widget.cupertino) { + return CupertinoButton( + key: key, + padding: const EdgeInsets.symmetric(horizontal: 8), + onPressed: onPressed, + child: cupertino == null + ? Text(label) + : Icon(cupertino, semanticLabel: label), + ); + } + if (material == null) { + return OutlinedButton(key: key, onPressed: onPressed, child: Text(label)); + } + return IconButton.outlined( + key: key, + tooltip: label, + onPressed: onPressed, + icon: Icon(material), + ); + } +} diff --git a/app/lib/features/chat/shell/chat_shell_terminal_links.dart b/app/lib/features/chat/shell/chat_shell_terminal_links.dart new file mode 100644 index 00000000..a445ffa6 --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_links.dart @@ -0,0 +1,38 @@ +import 'package:xterm/core.dart'; + +final RegExp _link = RegExp(r'''https?://[^\s<>"'`]+'''); + +/// The web link drawn at [offset] in [buffer], if any: the operator taps the +/// login URL an auth command printed. A link the terminal wrapped across +/// rows is joined first. Only http and https links are offered. +Uri? chatShellLinkAt(Buffer buffer, CellOffset offset) { + if (offset.y < 0 || offset.y >= buffer.height) return null; + var first = offset.y; + while (first > 0 && buffer.lines[first].isWrapped) { + first--; + } + var last = offset.y; + while (last + 1 < buffer.height && buffer.lines[last + 1].isWrapped) { + last++; + } + final text = StringBuffer(); + var tapped = 0; + for (var y = first; y <= last; y++) { + final line = buffer.lines[y]; + if (y == offset.y) tapped = text.length + offset.x; + for (var x = 0; x < line.length; x++) { + final code = line.getCodePoint(x); + // One character per cell keeps cell and string positions aligned. + text.write(code == 0 || code > 0xFFFF ? ' ' : String.fromCharCode(code)); + } + } + for (final match in _link.allMatches(text.toString())) { + if (tapped < match.start || tapped >= match.end) continue; + final trimmed = match[0]!.replaceFirst(RegExp(r'[.,;:!?)\]}]+$'), ''); + if (tapped >= match.start + trimmed.length) return null; + final uri = Uri.tryParse(trimmed); + if (uri == null || uri.host.isEmpty) return null; + return uri; + } + return null; +} diff --git a/app/lib/features/chat/shell/chat_shell_terminal_page.dart b/app/lib/features/chat/shell/chat_shell_terminal_page.dart new file mode 100644 index 00000000..8786b05a --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_page.dart @@ -0,0 +1,98 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import '../../../ui/text/monospace.dart'; +import 'chat_shell_runs_controller.dart'; +import 'chat_shell_terminal_keys.dart'; +import 'chat_shell_terminal_view.dart'; + +/// Opens [runId]'s terminal full screen: the same emulator the card draws, +/// sized to the window (a running process is resized to match), with the key +/// toolbar while it runs. Resolves when the page is closed. +Future openChatShellTerminalPage( + BuildContext context, { + required ChatShellRunsController controller, + required String runId, + required bool cupertino, +}) { + Widget page(BuildContext _) => ChatShellTerminalPage( + controller: controller, + runId: runId, + cupertino: cupertino, + ); + return Navigator.of(context).push( + cupertino + ? CupertinoPageRoute(builder: page) + : MaterialPageRoute(builder: page), + ); +} + +class ChatShellTerminalPage extends StatelessWidget { + const ChatShellTerminalPage({ + super.key, + required this.controller, + required this.runId, + required this.cupertino, + }); + + final ChatShellRunsController controller; + final String runId; + final bool cupertino; + + @override + Widget build(BuildContext context) { + return ListenableBuilder( + listenable: controller, + builder: (context, _) { + final run = controller.run(runId); + final title = Text( + run == null ? 'Command' : '\$ ${run.command}', + maxLines: 1, + overflow: TextOverflow.ellipsis, + style: const TextStyle(fontFamily: appMonospaceFamily), + ); + Widget body = const SizedBox.shrink(); + if (run != null) { + final session = controller.terminals.session(run); + body = SafeArea( + child: Padding( + padding: const EdgeInsets.all(8), + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Expanded( + child: ChatShellTerminalView( + session: session, + cupertino: cupertino, + fill: true, + autofocus: true, + ), + ), + if (session.live) + Padding( + padding: const EdgeInsets.only(top: 8), + child: ChatShellTerminalKeys( + session: session, + cupertino: cupertino, + field: chatShellTouchFirst(), + ), + ), + ], + ), + ), + ); + } + if (cupertino) { + return CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar(middle: title), + child: body, + ); + } + return Scaffold( + appBar: AppBar(title: title), + body: body, + ); + }, + ); + } +} diff --git a/app/lib/features/chat/shell/chat_shell_terminal_session.dart b/app/lib/features/chat/shell/chat_shell_terminal_session.dart new file mode 100644 index 00000000..aacec792 --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_session.dart @@ -0,0 +1,112 @@ +import 'package:xterm/core.dart'; + +import 'chat_shell_run.dart'; + +/// Lines of scrollback one card's terminal keeps. +const int chatShellTerminalScrollback = 5000; + +/// The terminal emulator state of one run: the xterm [terminal] fed with the +/// run's output in order, and the bridge from what the operator types (or +/// the emulator answers) to the running process. +/// +/// It keeps no copy of typed input: [Terminal.onOutput] hands each key or +/// paste straight to [onInput], which sends it. +class ChatShellTerminalSession { + ChatShellTerminalSession( + ChatShellRun run, { + required this.onInput, + required this.onResize, + }) : terminal = _ChatShellTerminal() { + // Drawn at the process's own size until a view lays it out at its own. + if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows); + terminal.onOutput = _output; + terminal.onResize = (cols, rows, _, _) => onResize(cols, rows); + replay(run); + } + + final Terminal terminal; + + /// Receives input for the process; only called while the run is live. + final void Function(String data) onInput; + + /// Receives the size the view laid the terminal out at. + final void Function(int cols, int rows) onResize; + + int _end = 0; + bool _live = false; + bool _replaying = false; + + /// The absolute output byte this terminal has drawn up to. + int get end => _end; + + /// Whether typing reaches a running process. + bool get live => _live; + + /// Draws [run]'s held output from scratch: on open, after a gap and after + /// a readback replaced the card's copy. Answers the emulator gives to old + /// queries in that output are not sent: the process asked them long ago. + void replay(ChatShellRun run) { + _live = run.running && run.terminal; + _replaying = true; + try { + terminal.useMainBuffer(); + terminal.buffer.clear(); + terminal.buffer.setCursor(0, 0); + terminal.setCursorVisibleMode(true); + terminal.write('\x1b[0m'); + if (run.truncated) { + terminal.write('\x1b[2m[earlier output was dropped]\x1b[0m\r\n'); + } + var output = _drawable(run, run.output); + // A finished run's final newline would leave an empty line below it. + if (!run.running && output.endsWith('\r\n')) { + output = output.substring(0, output.length - 2); + } + terminal.write(output); + if (!run.running) terminal.setCursorVisibleMode(false); + } finally { + _replaying = false; + } + _end = run.outputEnd; + } + + /// Draws one live chunk covering bytes [offset, end) of [run], which + /// already holds it. A chunk after a hole redraws the whole held copy. + void append(ChatShellRun run, String text, int offset, int end) { + if (end <= _end) return; + if (offset != _end) { + replay(run); + return; + } + terminal.write(_drawable(run, text)); + _end = end; + } + + /// The run ended: nothing typed reaches it any more. + void finish(ChatShellRun run) { + if (!_live) return; + _live = false; + terminal.setCursorVisibleMode(false); + } + + void _output(String data) { + if (_live && !_replaying) onInput(data); + } + + /// Plain-pipe output has bare newlines, which a terminal draws as a line + /// feed without a carriage return. + static String _drawable(ChatShellRun run, String text) => + run.terminal ? text : text.replaceAll('\n', '\r\n'); +} + +/// The emulator with the scrollback the cards keep. It answers a cursor +/// position query (DSR 6) with the one-based row and column the protocol +/// specifies; xterm.dart 4.0.0 itself answers zero-based. +class _ChatShellTerminal extends Terminal { + _ChatShellTerminal() : super(maxLines: chatShellTerminalScrollback); + + @override + void sendCursorPosition() { + onOutput?.call('\x1b[${buffer.cursorY + 1};${buffer.cursorX + 1}R'); + } +} diff --git a/app/lib/features/chat/shell/chat_shell_terminal_theme.dart b/app/lib/features/chat/shell/chat_shell_terminal_theme.dart new file mode 100644 index 00000000..88809c31 --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_theme.dart @@ -0,0 +1,85 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:xterm/xterm.dart'; + +import '../../../ui/text/monospace.dart'; +import '../conversation/chat_card_styles.dart'; + +/// The terminal's colours from the host theme: the card's inset surface as +/// background, the body text colour as foreground, the accent as cursor and +/// selection. The sixteen ANSI colours are the emulator's standard palette, +/// darkened on a light surface so coloured output stays readable. +TerminalTheme chatShellTerminalTheme(BuildContext context, bool cupertino) { + final brightness = cupertino + ? CupertinoTheme.brightnessOf(context) + : Theme.of(context).brightness; + final dark = brightness == Brightness.dark; + final background = chatCardFillColor(context, cupertino); + final foreground = cupertino + ? CupertinoColors.label.resolveFrom(context) + : Theme.of(context).colorScheme.onSurface; + final accent = cupertino + ? CupertinoTheme.of(context).primaryColor + : Theme.of(context).colorScheme.primary; + final base = TerminalThemes.defaultTheme; + Color ansi(Color color) { + if (dark) return color; + final hsl = HSLColor.fromColor(color); + return hsl.lightness > 0.4 ? hsl.withLightness(0.36).toColor() : color; + } + + return TerminalTheme( + cursor: accent, + selection: accent.withValues(alpha: 0.35), + foreground: foreground, + // Opaque: the emulator paints cells over its own background. + background: Color.alphaBlend( + background, + cupertino + ? CupertinoColors.systemBackground.resolveFrom(context) + : Theme.of(context).colorScheme.surface, + ), + black: dark ? base.black : foreground, + red: ansi(base.red), + green: ansi(base.green), + yellow: ansi(base.yellow), + blue: ansi(base.blue), + magenta: ansi(base.magenta), + cyan: ansi(base.cyan), + white: dark ? base.white : ansi(base.brightBlack), + brightBlack: ansi(base.brightBlack), + brightRed: ansi(base.brightRed), + brightGreen: ansi(base.brightGreen), + brightYellow: ansi(base.brightYellow), + brightBlue: ansi(base.brightBlue), + brightMagenta: ansi(base.brightMagenta), + brightCyan: ansi(base.brightCyan), + brightWhite: dark ? base.brightWhite : foreground, + searchHitBackground: base.searchHitBackground, + searchHitBackgroundCurrent: base.searchHitBackgroundCurrent, + searchHitForeground: base.searchHitForeground, + ); +} + +/// The terminal's text: the host's monospace family at the card body size. +TerminalStyle chatShellTerminalStyle(BuildContext context, bool cupertino) { + final body = chatCardBodyStyle(context, cupertino) ?? const TextStyle(); + return TerminalStyle( + fontFamily: appMonospaceFamily, + fontSize: (body.fontSize ?? 14) * 0.95, + height: 1.3, + ); +} + +/// The height of one terminal row in [style] at the context's text scale, +/// measured the way the emulator measures its cells. +double chatShellTerminalLineHeight(BuildContext context, TerminalStyle style) { + final painter = TextPainter( + text: TextSpan(text: 'mmmmmmmmmm', style: style.toTextStyle()), + textDirection: TextDirection.ltr, + textScaler: MediaQuery.textScalerOf(context), + )..layout(); + final height = painter.height; + painter.dispose(); + return height; +} diff --git a/app/lib/features/chat/shell/chat_shell_terminal_view.dart b/app/lib/features/chat/shell/chat_shell_terminal_view.dart new file mode 100644 index 00000000..a99c2177 --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminal_view.dart @@ -0,0 +1,113 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:xterm/xterm.dart'; + +import '../ui/transcript/chat_link_launcher.dart'; +import 'chat_shell_metrics.dart'; +import 'chat_shell_terminal_links.dart'; +import 'chat_shell_terminal_session.dart'; +import 'chat_shell_terminal_theme.dart'; + +/// Whether the soft keyboard is the main way to type here, so the terminal +/// takes typing from the key toolbar's field rather than its own hidden one. +bool chatShellTouchFirst() => + defaultTargetPlatform == TargetPlatform.android || + defaultTargetPlatform == TargetPlatform.iOS; + +/// One run's terminal emulator view. In a card it is bounded to +/// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its +/// output); with [fill] it takes the space its parent gives (full screen). +/// It sizes the terminal to its own layout; a running process is resized to +/// match. Tapping a printed web link opens it in the browser. +class ChatShellTerminalView extends StatefulWidget { + const ChatShellTerminalView({ + super.key, + required this.session, + required this.cupertino, + this.fill = false, + this.autofocus = false, + this.autoResize = true, + }); + + final ChatShellTerminalSession session; + final bool cupertino; + final bool fill; + final bool autofocus; + + /// False while another view of the same terminal (full screen) sizes it. + final bool autoResize; + + @override + State createState() => _ChatShellTerminalViewState(); +} + +class _ChatShellTerminalViewState extends State { + final TerminalController _controller = TerminalController(); + final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal'); + int? _fitRows; + + @override + void dispose() { + _controller.dispose(); + _focus.dispose(); + super.dispose(); + } + + void _tapped(TapUpDetails details, CellOffset offset) { + final uri = chatShellLinkAt(widget.session.terminal.buffer, offset); + if (uri != null) openChatLink(context, uri); + } + + /// A finished run's box shrinks to the rows its output uses. + void _fit() { + if (!mounted || widget.fill || widget.session.live) return; + final used = widget.session.terminal.buffer.absoluteCursorY + 1; + final rows = used.clamp(1, kChatShellOutputCollapsedLines); + if (rows != _fitRows) setState(() => _fitRows = rows); + } + + @override + Widget build(BuildContext context) { + final session = widget.session; + final live = session.live; + final style = chatShellTerminalStyle(context, widget.cupertino); + const padding = EdgeInsets.all(8); + Widget view = Semantics( + identifier: 'chat-shell-terminal', + label: live ? 'Terminal, type to send input' : 'Terminal output', + child: TerminalView( + session.terminal, + controller: _controller, + focusNode: _focus, + autofocus: widget.autofocus && live, + autoResize: widget.autoResize, + theme: chatShellTerminalTheme(context, widget.cupertino), + textStyle: style, + padding: padding, + readOnly: !live, + hardwareKeyboardOnly: !live || chatShellTouchFirst(), + keyboardType: TextInputType.visiblePassword, + cursorType: TerminalCursorType.block, + onTapUp: _tapped, + ), + ); + // The emulator would otherwise inset itself by the window's safe area. + view = MediaQuery.removePadding( + context: context, + removeTop: true, + removeBottom: true, + removeLeft: true, + removeRight: true, + child: view, + ); + view = ClipRRect(borderRadius: BorderRadius.circular(8), child: view); + if (widget.fill) return view; + if (!live) WidgetsBinding.instance.addPostFrameCallback((_) => _fit()); + final rows = live + ? kChatShellOutputCollapsedLines + : (_fitRows ?? kChatShellOutputCollapsedLines); + final line = chatShellTerminalLineHeight(context, style); + // A pixel of slack keeps rounding from losing the last row. + return SizedBox(height: rows * line + padding.vertical + 1, child: view); + } +} diff --git a/app/lib/features/chat/shell/chat_shell_terminals.dart b/app/lib/features/chat/shell/chat_shell_terminals.dart new file mode 100644 index 00000000..54b6bb8e --- /dev/null +++ b/app/lib/features/chat/shell/chat_shell_terminals.dart @@ -0,0 +1,142 @@ +import 'dart:async'; + +import 'chat_shell_run.dart'; +import 'chat_shell_runs_repository.dart'; +import 'chat_shell_terminal_session.dart'; + +/// UTF-16 units of typed or pasted text one `chat_shell_input` carries: at +/// most 12 KiB of UTF-8, inside the server's 16 KiB bound. +const int chatShellInputChunk = 4096; + +/// How long a view's size settles before the process is resized. +const Duration chatShellResizeDelay = Duration(milliseconds: 150); + +/// The terminal emulators of one chat's runs, kept outside the cards so a +/// card scrolled out of the transcript (or opened full screen) draws the same +/// state, and the delivery of typed input and sizes to the server. +class ChatShellTerminals { + ChatShellTerminals( + this._repository, + this._sessionId, { + required this.onError, + }); + + final ChatShellRunsRepository _repository; + final String _sessionId; + + /// Reports a refused input or resize of a run, in the server's words. + final void Function(String runId, Object error) onError; + + final Map _sessions = {}; + final Map _inputs = {}; + final Map _resizes = {}; + final Map _sentSizes = {}; + bool _disposed = false; + + /// The size a view last laid a terminal out at: new runs start at it, so + /// their first output already fits the reading column. + (int, int)? lastSize; + + /// The emulator of [run], created (and fed its held output) on first use. + ChatShellTerminalSession session(ChatShellRun run) => _sessions.putIfAbsent( + run.id, + () => ChatShellTerminalSession( + run, + onInput: (data) => _input(run.id, data), + onResize: (cols, rows) => _resized(run.id, cols, rows), + ), + ); + + /// The card's copy of [run] was replaced (readback, gap): redraw it. + void replaced(ChatShellRun run) => _sessions[run.id]?.replay(run); + + /// [run] now holds one more live chunk. + void appended(ChatShellRun run, String text, int offset, int end) => + _sessions[run.id]?.append(run, text, offset, end); + + /// [run] ended: draw its final output read-only. + void finished(ChatShellRun run) { + _resizes.remove(run.id)?.cancel(); + _inputs.remove(run.id); + _sessions[run.id]?.replay(run); + } + + void _input(String runId, String data) { + if (_disposed) return; + _inputs + .putIfAbsent( + runId, + () => _InputPipe( + (data) => _repository.input(_sessionId, runId, data), + (error) => onError(runId, error), + ), + ) + .add(data); + } + + void _resized(String runId, int cols, int rows) { + lastSize = (cols, rows); + if (_disposed || !(_sessions[runId]?.live ?? false)) return; + _resizes.remove(runId)?.cancel(); + _resizes[runId] = Timer(chatShellResizeDelay, () { + _resizes.remove(runId); + if (_sentSizes[runId] == (cols, rows)) return; + _sentSizes[runId] = (cols, rows); + _repository + .resize(_sessionId, runId, cols, rows) + .catchError((Object error) => onError(runId, error)); + }); + } + + void dispose() { + _disposed = true; + for (final timer in _resizes.values) { + timer.cancel(); + } + _resizes.clear(); + _inputs.clear(); + } +} + +/// One run's typed input on its way to the server, in order: what arrives +/// while a request is in flight goes in the next one. Nothing is kept once +/// sent, and a refusal discards what was waiting rather than retyping it. +class _InputPipe { + _InputPipe(this._send, this._failed); + + final Future Function(String data) _send; + final void Function(Object error) _failed; + final StringBuffer _pending = StringBuffer(); + bool _busy = false; + + void add(String data) { + _pending.write(data); + if (!_busy) unawaited(_pump()); + } + + Future _pump() async { + _busy = true; + try { + while (_pending.isNotEmpty) { + var waiting = _pending.toString(); + _pending.clear(); + var cut = waiting.length; + if (cut > chatShellInputChunk) { + cut = chatShellInputChunk; + // Never split a surrogate pair across two requests. + final unit = waiting.codeUnitAt(cut - 1); + if (unit >= 0xD800 && unit <= 0xDBFF) cut--; + _pending.write(waiting.substring(cut)); + } + final chunk = waiting.substring(0, cut); + waiting = ''; + await _send(chunk); + } + } catch (error) { + _pending.clear(); + _failed(error); + } finally { + _busy = false; + } + } +} diff --git a/app/pubspec.lock b/app/pubspec.lock index bce4d8f7..5dea71ee 100644 --- a/app/pubspec.lock +++ b/app/pubspec.lock @@ -113,6 +113,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.19.1" + convert: + dependency: transitive + description: + name: convert + sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68 + url: "https://pub.dev" + source: hosted + version: "3.1.2" cross_file: dependency: transitive description: @@ -153,6 +161,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.15" + equatable: + dependency: transitive + description: + name: equatable + sha256: "3bce007a596ff8b3119c45d68aaef631272537c03d30e5d4534dd24bf4c5eaa2" + url: "https://pub.dev" + source: hosted + version: "2.1.0" fake_async: dependency: transitive description: @@ -575,6 +591,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.1" + quiver: + dependency: transitive + description: + name: quiver + sha256: ea0b925899e64ecdfbf9c7becb60d5b50e706ade44a85b2363be2a22d88117d2 + url: "https://pub.dev" + source: hosted + version: "3.2.2" record_use: dependency: transitive description: @@ -956,6 +980,14 @@ packages: url: "https://pub.dev" source: hosted version: "7.0.1" + xterm: + dependency: "direct main" + description: + name: xterm + sha256: "168dfedca77cba33fdb6f52e2cd001e9fde216e398e89335c19b524bb22da3a2" + url: "https://pub.dev" + source: hosted + version: "4.0.0" yaml: dependency: transitive description: @@ -964,6 +996,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.4" + zmodem: + dependency: transitive + description: + name: zmodem + sha256: "3b7e5b29f3a7d8aee472029b05165a68438eff2f3f7766edf13daba1e297adbf" + url: "https://pub.dev" + source: hosted + version: "0.0.6" sdks: dart: ">=3.12.2 <4.0.0" flutter: ">=3.44.0" diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 79614e63..7dc018df 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -50,6 +50,7 @@ dependencies: file_picker: ^13.3.0 url_launcher: ^6.3.3 bonsoir: ^7.1.5 + xterm: ^4.0.0 dev_dependencies: integration_test: diff --git a/app/test/features/chat/shell/chat_shell_fixtures.dart b/app/test/features/chat/shell/chat_shell_fixtures.dart index 5efeb181..439b8545 100644 --- a/app/test/features/chat/shell/chat_shell_fixtures.dart +++ b/app/test/features/chat/shell/chat_shell_fixtures.dart @@ -2,9 +2,11 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; +import 'package:xterm/xterm.dart'; import '../conversation/chat_thread_fixtures.dart'; @@ -15,6 +17,9 @@ class ShellGateway implements ApiGateway { final _frames = StreamController.broadcast(); List runs = const []; String? refusal; + + /// The reply to a `chat_shell_run_output` that resumes from an offset. + JsonMap? resumeReply; int _serial = 0; @override @@ -47,6 +52,9 @@ class ShellGateway implements ApiGateway { ), }; case 'chat_shell_run_output': + if (body['after'] != null && resumeReply != null) { + return {'chat_shell_run': resumeReply}; + } return { 'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']), }; @@ -84,6 +92,9 @@ JsonMap shellRecord( int? exitCode, String output = '', int durationMs = 0, + bool terminal = false, + String text = '', + int outputStart = 0, }) => { 'id': id, 'session_id': 's', @@ -95,9 +106,27 @@ JsonMap shellRecord( 'ended_at': ?(status == 'running' ? null : 1000 + durationMs), 'duration_ms': durationMs, 'output': output, - 'output_bytes': output.length, + 'output_start': outputStart, + 'output_bytes': outputStart + output.length, + if (terminal) ...{'terminal': true, 'cols': 100, 'rows': 30}, + if (text.isNotEmpty) 'text': text, }; +/// The text the run card's terminal draws, one row per line, trailing blank +/// rows dropped. +String terminalText(WidgetTester tester, {int index = 0}) { + final view = tester.widgetList(find.byType(TerminalView)); + final buffer = view.elementAt(index).terminal.buffer; + final lines = [ + for (var y = 0; y < buffer.height; y++) + buffer.lines[y].getText().trimRight(), + ]; + while (lines.isNotEmpty && lines.last.isEmpty) { + lines.removeLast(); + } + return lines.join('\n'); +} + Widget shellThread(ShellGateway gateway, {bool cupertino = false}) { final thread = ChatThreadView( gateway: gateway, diff --git a/app/test/features/chat/shell/chat_shell_run_card_test.dart b/app/test/features/chat/shell/chat_shell_run_card_test.dart index 292076fd..b58d8e0a 100644 --- a/app/test/features/chat/shell/chat_shell_run_card_test.dart +++ b/app/test/features/chat/shell/chat_shell_run_card_test.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:xterm/xterm.dart'; + import 'chat_shell_fixtures.dart'; void main() { @@ -27,7 +29,7 @@ void main() { expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget); gateway.emitOutput('run_a', 'line 2\n', 7); await tester.pump(); - expect(find.text('line 1\nline 2'), findsOneWidget); + expect(terminalText(tester), 'line 1\nline 2'); expect(find.byKey(const Key('chat-shell-run-share')), findsNothing); await tester.tap(find.byKey(const Key('chat-shell-run-stop'))); @@ -43,7 +45,7 @@ void main() { expect(find.text('Stopped · 2.4 s'), findsOneWidget); expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing); // The streamed output stays after the lifecycle frame. - expect(find.text('line 1\nline 2'), findsOneWidget); + expect(terminalText(tester), 'line 1\nline 2'); }); testWidgets('$host: finished commands show their exit status', ( @@ -69,6 +71,8 @@ void main() { ]; await tester.pumpWidget(shellThread(gateway, cupertino: cupertino)); await tester.pump(); + // Finished terminals shrink to their output after their first frame. + await tester.pumpAndSettle(); expect(card(), findsNWidgets(2)); expect(find.text('Exit 0 · 120 ms'), findsOneWidget); @@ -94,7 +98,7 @@ void main() { }); } - testWidgets('Copy output copies the retained output', (tester) async { + testWidgets('Copy output copies the plain-text rendering', (tester) async { final copied = []; tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( SystemChannels.platform, @@ -112,10 +116,18 @@ void main() { ), ); gateway.runs = [ - shellRecord('run_a', status: 'exited', exitCode: 0, output: 'a\nb\n'), + shellRecord( + 'run_a', + status: 'exited', + exitCode: 0, + terminal: true, + output: '\x1b[32ma\x1b[0m\r\nb\r\n', + text: 'a\nb\n', + ), ]; await tester.pumpWidget(shellThread(gateway)); await tester.pump(); + expect(terminalText(tester), 'a\nb'); await tester.tap(find.byKey(const Key('chat-shell-run-copy'))); await tester.pump(); expect(copied, ['a\nb\n']); @@ -151,7 +163,14 @@ void main() { await tester.ensureVisible(expand); await tester.pump(); await tester.tap(expand); - await tester.pump(); - expect(find.text('Collapse'), findsOneWidget); + await tester.pumpAndSettle(); + // Full screen draws the same terminal over the window. + expect(find.byType(TerminalView), findsOneWidget); + expect(find.byType(TerminalView, skipOffstage: false), findsNWidgets(2)); + expect(tester.takeException(), isNull); + expect(terminalText(tester), contains('line 79')); + await tester.pageBack(); + await tester.pumpAndSettle(); + expect(find.byType(TerminalView), findsOneWidget); }); } diff --git a/app/test/features/chat/shell/chat_shell_terminal_session_test.dart b/app/test/features/chat/shell/chat_shell_terminal_session_test.dart new file mode 100644 index 00000000..f28b80c9 --- /dev/null +++ b/app/test/features/chat/shell/chat_shell_terminal_session_test.dart @@ -0,0 +1,87 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/shell/chat_shell_run.dart'; +import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_links.dart'; +import 'package:pipelines_app/features/chat/shell/chat_shell_terminal_session.dart'; +import 'package:xterm/core.dart'; + +ChatShellRun _run(String output, {String status = 'running'}) => + ChatShellRun.fromJson({ + 'id': 'run_1', + 'command': 'login', + 'directory': '/w', + 'status': status, + 'started_at': 1, + 'output': output, + 'output_bytes': output.length, + 'terminal': true, + 'cols': 40, + 'rows': 10, + })!; + +void main() { + test('a printed link is found under the tap, across a wrapped row', () { + final terminal = Terminal()..resize(20, 5); + terminal.write('Open https://example.com/login/device?code=ABCD, then\r\n'); + // Row 0 holds "Open https://example." and row 1 continues it. + expect( + chatShellLinkAt(terminal.buffer, const CellOffset(8, 0)), + Uri.parse('https://example.com/login/device?code=ABCD'), + ); + expect( + chatShellLinkAt(terminal.buffer, const CellOffset(10, 1)), + Uri.parse('https://example.com/login/device?code=ABCD'), + ); + expect(chatShellLinkAt(terminal.buffer, const CellOffset(1, 0)), isNull); + expect(chatShellLinkAt(terminal.buffer, const CellOffset(0, 4)), isNull); + }); + + test('only http and https links are offered', () { + final terminal = Terminal()..resize(40, 3); + terminal.write('javascript:alert(1) file:///etc/passwd'); + for (var x = 0; x < 38; x++) { + expect(chatShellLinkAt(terminal.buffer, CellOffset(x, 0)), isNull); + } + }); + + test('typing reaches a live run; replayed query answers do not', () { + final sent = []; + // The output asks for the cursor position, which the emulator answers. + final session = ChatShellTerminalSession( + _run('prompt\x1b[6n'), + onInput: sent.add, + onResize: (_, _) {}, + ); + expect(session.live, isTrue); + expect(sent, isEmpty); + session.terminal.textInput('y'); + session.append(_run('prompt\x1b[6nok\x1b[6n'), 'ok\x1b[6n', 10, 14); + // The answer is one-based: row 1, column 9. + expect(sent, ['y', '\x1b[1;9R']); + expect(session.end, 14); + + // A finished run draws read-only and sends nothing. + session.finish(_run('prompt ok', status: 'exited')); + session.replay(_run('prompt ok', status: 'exited')); + session.terminal.textInput('n'); + expect(session.live, isFalse); + expect(sent, hasLength(2)); + }); + + test('a chunk after a hole redraws the held copy', () { + final session = ChatShellTerminalSession( + _run('a\r\n'), + onInput: (_) {}, + onResize: (_, _) {}, + ); + session.append(_run('a\r\nb\r\n'), 'b\r\n', 3, 6); + expect(session.terminal.buffer.lines[1].getText().trim(), 'b'); + // Bytes 6..9 were missed; the run's copy says so and is redrawn. + final gapped = _run('a\r\nb\r\n').withChunk('d\r\n', 9, 12); + session.append(gapped, 'd\r\n', 9, 12); + expect(session.end, 12); + expect( + session.terminal.buffer.lines[0].getText(), + contains('earlier output was dropped'), + ); + }); +} diff --git a/app/test/features/chat/shell/chat_shell_terminal_test.dart b/app/test/features/chat/shell/chat_shell_terminal_test.dart new file mode 100644 index 00000000..14fd50ab --- /dev/null +++ b/app/test/features/chat/shell/chat_shell_terminal_test.dart @@ -0,0 +1,187 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/shell/chat_shell_runs_controller.dart'; +import 'package:xterm/xterm.dart'; + +import 'chat_shell_fixtures.dart'; + +void main() { + late ShellGateway gateway; + + setUp(() => gateway = ShellGateway()); + tearDown(() => gateway.close()); + + /// Everything sent to the process, joined in order. + String typed() => [ + for (final body in gateway.bodies('chat_shell_input')) body['data'], + ].join(); + + Future openRunning(WidgetTester tester, {String output = ''}) async { + gateway.runs = [ + shellRecord( + 'run_t', + command: 'gh auth login', + terminal: true, + output: output, + ), + ]; + await tester.pumpWidget(shellThread(gateway)); + await tester.pump(); + } + + testWidgets( + 'keys typed into a focused desktop terminal reach the process', + (tester) async { + await openRunning(tester, output: 'Paste your token: '); + expect(terminalText(tester), 'Paste your token:'); + // A desktop has no soft-keyboard field: the terminal takes typing itself. + expect(find.byKey(const Key('chat-shell-terminal-field')), findsNothing); + await tester.tap(find.byType(TerminalView)); + await tester.pump(); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp); + await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft); + await tester.sendKeyEvent(LogicalKeyboardKey.keyC); + await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft); + await tester.pump(); + expect(typed(), '\x1b[A\x03'); + await tester.pump(const Duration(milliseconds: 400)); + expect(gateway.bodies('chat_shell_input').first['run_id'], 'run_t'); + }, + variant: TargetPlatformVariant.only(TargetPlatform.macOS), + ); + + testWidgets('Paste types the clipboard text into the process', ( + tester, + ) async { + tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + (call) async => call.method == 'Clipboard.getData' + ? {'text': 'gho_pasted-token'} + : null, + ); + addTearDown( + () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( + SystemChannels.platform, + null, + ), + ); + await openRunning(tester); + await tester.tap(find.byKey(const Key('chat-shell-key-paste'))); + await tester.pump(); + await tester.tap(find.byKey(const Key('chat-shell-key-enter'))); + await tester.pump(); + expect(typed(), 'gho_pasted-token\r'); + }); + + testWidgets('the touch key toolbar and field type into the process', ( + tester, + ) async { + await openRunning(tester, output: 'Password: '); + for (final key in ['ctrl-c', 'tab', 'esc', 'left', 'down', 'right']) { + await tester.tap(find.byKey(Key('chat-shell-key-$key'))); + await tester.pump(); + } + expect(typed(), '\x03\t\x1b\x1b[D\x1b[B\x1b[C'); + gateway.calls.clear(); + + final field = find.byKey(const Key('chat-shell-terminal-field')); + expect(field, findsOneWidget); + await tester.tap(find.byKey(const Key('chat-shell-terminal-hide'))); + await tester.pump(); + expect(tester.widget(field).obscureText, isTrue); + await tester.enterText(field, 'hunter2'); + await tester.testTextInput.receiveAction(TextInputAction.send); + await tester.pump(); + expect(typed(), 'hunter2\r'); + // The field forgets the text once it is sent. + expect(tester.widget(field).controller!.text, isEmpty); + }); + + testWidgets('a running terminal is resized to its view; a finished one is ' + 'read-only', (tester) async { + await openRunning(tester, output: 'working\r\n'); + await tester.pump(const Duration(milliseconds: 200)); + final resize = gateway.bodies('chat_shell_resize'); + expect(resize, hasLength(1)); + expect(resize.single['run_id'], 'run_t'); + expect(resize.single['rows'], 12); + expect(resize.single['cols'], greaterThan(20)); + + gateway.emitRun( + shellRecord( + 'run_t', + command: 'gh auth login', + status: 'exited', + exitCode: 0, + terminal: true, + output: 'working\r\n', + ), + ); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('chat-shell-key-enter')), findsNothing); + expect( + tester.widget(find.byType(TerminalView)).readOnly, + isTrue, + ); + expect(terminalText(tester), 'working'); + gateway.calls.clear(); + await tester.tap(find.byType(TerminalView)); + await tester.sendKeyEvent(LogicalKeyboardKey.keyA); + await tester.pump(const Duration(milliseconds: 400)); + expect(gateway.bodies('chat_shell_input'), isEmpty); + }); + + testWidgets('coloured output is drawn without its escape codes', ( + tester, + ) async { + gateway.runs = [ + shellRecord( + 'run_c', + status: 'exited', + exitCode: 0, + terminal: true, + output: '\x1b[1;31merror\x1b[0m: bad\r\n10%\r100%\r\n', + ), + ]; + await tester.pumpWidget(shellThread(gateway)); + await tester.pumpAndSettle(); + expect(terminalText(tester), 'error: bad\n100%'); + final buffer = tester + .widget(find.byType(TerminalView)) + .terminal + .buffer; + // The red foreground survives as a cell attribute. + expect( + buffer.lines[0].getForeground(0), + isNot(buffer.lines[0].getForeground(7)), + ); + }); + + test('a reconnect resumes the running output from its offset', () async { + gateway.runs = [shellRecord('run_t', terminal: true, output: 'one\r\n')]; + final controller = ChatShellRunsController(gateway, 's'); + addTearDown(controller.dispose); + await controller.refresh(); + final session = controller.terminals.session(controller.run('run_t')!); + expect(session.end, 5); + + gateway.runs = [ + shellRecord('run_t', terminal: true, output: 'one\r\ntwo\r\n'), + ]; + gateway.resumeReply = shellRecord( + 'run_t', + terminal: true, + output: 'two\r\n', + outputStart: 5, + ); + await controller.refresh(); + await Future.delayed(Duration.zero); + final resume = gateway.bodies('chat_shell_run_output').single; + expect(resume['after'], 5); + expect(controller.run('run_t')!.output, 'one\r\ntwo\r\n'); + expect(controller.run('run_t')!.gap, isFalse); + expect(session.end, 10); + expect(session.terminal.buffer.lines[1].getText().trimRight(), 'two'); + }); +}