Merge branch 'worktree-agent-a26df8b0059ab1b12'
This commit is contained in:
@@ -5,14 +5,22 @@ import 'package:flutter/widgets.dart';
|
||||
abstract final class ChatComposerPasteFocus {
|
||||
static final Set<FocusNode> _composers = {};
|
||||
|
||||
static final Set<FocusNode> _inputs = {};
|
||||
|
||||
static void register(FocusNode focus) => _composers.add(focus);
|
||||
static void unregister(FocusNode focus) => _composers.remove(focus);
|
||||
|
||||
/// An input that is not an [EditableText] (a shell command's terminal)
|
||||
/// registers its focus here: a paste while it has focus is its own.
|
||||
static void registerInput(FocusNode focus) => _inputs.add(focus);
|
||||
static void unregisterInput(FocusNode focus) => _inputs.remove(focus);
|
||||
|
||||
static bool get anotherEditableHasFocus {
|
||||
final primary = FocusManager.instance.primaryFocus;
|
||||
if (primary == null || _composers.any((focus) => focus.hasFocus)) {
|
||||
return false;
|
||||
}
|
||||
if (_inputs.any((focus) => focus.hasFocus)) return true;
|
||||
final context = primary.context;
|
||||
if (context == null) return false;
|
||||
var editable = context.widget is EditableText;
|
||||
|
||||
@@ -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<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
||||
@@ -50,12 +46,12 @@ class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
||||
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<void> _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<ChatShellCardActions> {
|
||||
? () => 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,
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<ChatShellOutputView> 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<ChatShellOutputView> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ChatShellRunCard> {
|
||||
bool _expanded = false;
|
||||
bool _fullScreen = false;
|
||||
int _viewGeneration = 0;
|
||||
|
||||
Future<void> _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<ChatShellRunCard> {
|
||||
const SizedBox(height: 12),
|
||||
ChatShellOutputView(
|
||||
run: run,
|
||||
controller: controller,
|
||||
cupertino: cupertino,
|
||||
expanded: _expanded,
|
||||
viewKey: ValueKey<int>(_viewGeneration),
|
||||
fullScreen: _fullScreen,
|
||||
),
|
||||
if (run.error.isNotEmpty && !run.running)
|
||||
Padding(
|
||||
@@ -129,8 +149,7 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
||||
run: run,
|
||||
controller: controller,
|
||||
cupertino: cupertino,
|
||||
expanded: _expanded,
|
||||
onToggleExpanded: () => setState(() => _expanded = !_expanded),
|
||||
onFullScreen: () => _openFullScreen(cupertino),
|
||||
),
|
||||
if (error != null)
|
||||
Semantics(
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<ChatShellRun> 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<ChatShellRun> 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<void> 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<void> resize(String sessionId, String runId, int cols, int rows) =>
|
||||
gateway.call(
|
||||
'chat_shell_resize',
|
||||
_body(sessionId, runId: runId, cols: cols, rows: rows),
|
||||
);
|
||||
|
||||
Future<void> 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<ChatShellRun> 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<ChatShellRun> 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.
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
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<ChatShellTerminalKeys> createState() => _ChatShellTerminalKeysState();
|
||||
}
|
||||
|
||||
class _ChatShellTerminalKeysState extends State<ChatShellTerminalKeys> {
|
||||
final TextEditingController _text = TextEditingController();
|
||||
bool _hidden = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_text.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Terminal get _terminal => widget.session.terminal;
|
||||
|
||||
Future<void> _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,
|
||||
style: OutlinedButton.styleFrom(visualDensity: VisualDensity.compact),
|
||||
onPressed: onPressed,
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
return IconButton.outlined(
|
||||
key: key,
|
||||
visualDensity: VisualDensity.compact,
|
||||
tooltip: label,
|
||||
onPressed: onPressed,
|
||||
icon: Icon(material),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<void> 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<void>(
|
||||
cupertino
|
||||
? CupertinoPageRoute<void>(builder: page)
|
||||
: MaterialPageRoute<void>(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,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
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);
|
||||
}
|
||||
|
||||
/// The rows the drawn output takes, through the cursor's line, when the
|
||||
/// terminal is [cols] wide: a finished run's box is sized by it before the
|
||||
/// view lays the terminal out, so a card never changes height after it.
|
||||
int rowsAt(int cols) {
|
||||
final buffer = terminal.buffer;
|
||||
final last = buffer.absoluteCursorY.clamp(0, buffer.height - 1);
|
||||
var rows = 0;
|
||||
var length = 0;
|
||||
void close() => rows += length <= 0 ? 1 : (length + cols - 1) ~/ cols;
|
||||
for (var y = 0; y <= last; y++) {
|
||||
final line = buffer.lines[y];
|
||||
if (y > 0 && !line.isWrapped) {
|
||||
close();
|
||||
length = 0;
|
||||
}
|
||||
// A row the next one continues is full to the terminal's width.
|
||||
final continued = y + 1 < buffer.height && buffer.lines[y + 1].isWrapped;
|
||||
length += continued ? line.length : line.getTrimmedLength();
|
||||
}
|
||||
close();
|
||||
return rows;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
/// Narrowing rewraps long lines, and xterm.dart 4.0.0 keeps every blank
|
||||
/// row below the cursor while doing so: the extra rows push the top of
|
||||
/// the output into scrollback. Blank rows below the cursor are dropped
|
||||
/// instead, keeping the cursor on its line.
|
||||
@override
|
||||
void resize(int width, int height, [int? pixelWidth, int? pixelHeight]) {
|
||||
super.resize(width, height, pixelWidth, pixelHeight);
|
||||
final lines = buffer.lines;
|
||||
while (buffer.height > viewHeight &&
|
||||
buffer.height - 1 > buffer.absoluteCursorY &&
|
||||
lines[buffer.height - 1].getText().trim().isEmpty) {
|
||||
lines.pop();
|
||||
buffer.setCursor(buffer.cursorX, buffer.cursorY + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
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 size of one terminal cell in [style] at the context's text scale,
|
||||
/// measured exactly as the emulator measures it.
|
||||
Size chatShellTerminalCellSize(BuildContext context, TerminalStyle style) {
|
||||
final text = style.toTextStyle();
|
||||
final builder = ui.ParagraphBuilder(text.getParagraphStyle())
|
||||
..pushStyle(text.getTextStyle(textScaler: MediaQuery.textScalerOf(context)))
|
||||
..addText('mmmmmmmmmm');
|
||||
final paragraph = builder.build()
|
||||
..layout(const ui.ParagraphConstraints(width: double.infinity));
|
||||
final size = Size(paragraph.maxIntrinsicWidth / 10, paragraph.height);
|
||||
paragraph.dispose();
|
||||
return size;
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:xterm/xterm.dart';
|
||||
|
||||
import '../conversation/chat_composer_paste_focus.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 key
|
||||
/// toolbar offers a text field: the terminal itself takes hardware keys only.
|
||||
bool chatShellTouchFirst() =>
|
||||
defaultTargetPlatform == TargetPlatform.android ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS;
|
||||
|
||||
/// The terminal's own key shortcuts. Cmd-C, Cmd-V and Cmd-A on Apple
|
||||
/// platforms; elsewhere Ctrl-V (and the terminal convention Ctrl-Shift-V)
|
||||
/// paste and Ctrl-Shift-C copies, so every other Ctrl key, Ctrl-A and Ctrl-C
|
||||
/// included, reaches the process.
|
||||
Map<ShortcutActivator, Intent> chatShellTerminalShortcuts() {
|
||||
const paste = PasteTextIntent(SelectionChangedCause.keyboard);
|
||||
final apple =
|
||||
defaultTargetPlatform == TargetPlatform.macOS ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS;
|
||||
if (apple) {
|
||||
return const {
|
||||
SingleActivator(LogicalKeyboardKey.keyC, meta: true):
|
||||
CopySelectionTextIntent.copy,
|
||||
SingleActivator(LogicalKeyboardKey.keyV, meta: true): paste,
|
||||
SingleActivator(LogicalKeyboardKey.keyA, meta: true): SelectAllTextIntent(
|
||||
SelectionChangedCause.keyboard,
|
||||
),
|
||||
};
|
||||
}
|
||||
return const {
|
||||
SingleActivator(LogicalKeyboardKey.keyC, control: true, shift: true):
|
||||
CopySelectionTextIntent.copy,
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true): paste,
|
||||
SingleActivator(LogicalKeyboardKey.keyV, control: true, shift: true): paste,
|
||||
};
|
||||
}
|
||||
|
||||
/// 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<ChatShellTerminalView> createState() => _ChatShellTerminalViewState();
|
||||
}
|
||||
|
||||
class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
|
||||
final TerminalController _controller = TerminalController();
|
||||
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// A paste while the terminal has focus goes to the process, not to the
|
||||
// chat composer.
|
||||
ChatComposerPasteFocus.registerInput(_focus);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
ChatComposerPasteFocus.unregisterInput(_focus);
|
||||
_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);
|
||||
}
|
||||
|
||||
@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,
|
||||
// Keys go straight to the process as key events (a focused text
|
||||
// input would stay closed on the web); touch typing goes through the
|
||||
// key toolbar's field instead.
|
||||
hardwareKeyboardOnly: true,
|
||||
shortcuts: chatShellTerminalShortcuts(),
|
||||
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;
|
||||
final cell = chatShellTerminalCellSize(context, style);
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
// A finished run's box fits the rows its output takes at this width.
|
||||
final cols = ((constraints.maxWidth - padding.horizontal) / cell.width)
|
||||
.floor()
|
||||
.clamp(1, 1000);
|
||||
final rows = live
|
||||
? kChatShellOutputCollapsedLines
|
||||
: session.rowsAt(cols).clamp(1, kChatShellOutputCollapsedLines);
|
||||
// A pixel of slack keeps rounding from losing the last row.
|
||||
return SizedBox(
|
||||
height: rows * cell.height + padding.vertical + 1,
|
||||
child: view,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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<String, ChatShellTerminalSession> _sessions = {};
|
||||
final Map<String, _InputPipe> _inputs = {};
|
||||
final Map<String, Timer> _resizes = {};
|
||||
final Map<String, (int, int)> _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<void> 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<void> _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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
# Interactive chat shell commands (terminal)
|
||||
|
||||
Operator shell commands (`!cmd`, Run command…; see `chat-shell-commands.md`)
|
||||
now run in a real pseudo-terminal and the card draws them with a terminal
|
||||
emulator. Auth flows that print a link, wait for a pasted token and continue
|
||||
(`gh auth login`, `claude login`, `npm login`, ssh prompts) work in the chat.
|
||||
|
||||
## Behaviour
|
||||
|
||||
- **Server (macOS, Linux):** every `chat_shell_run` starts `$SHELL -lc <cmd>`
|
||||
inside a PTY (`github.com/creack/pty` v1.1.24) as the leader of a new
|
||||
session with the PTY as controlling terminal (`Setsid`, `Setctty`), so the
|
||||
session id is the process group Stop signals (SIGINT, then SIGKILL after the
|
||||
grace period). `TERM=xterm-256color`; cwd and the rest of the environment are
|
||||
unchanged. Size comes from the request (`cols`, `rows`), clamped to
|
||||
20–500 x 4–200, default 100x30. The PTY controller is closed when the command
|
||||
ends. Other platforms keep the previous pipe mode; input and resize answer
|
||||
"interactive terminals are not supported on this server".
|
||||
- **Card:** the output area is an `xterm` 4.0.0 (xterm.dart) `TerminalView`
|
||||
fed the raw stream in order. One emulator per run lives in the controller
|
||||
(`ChatShellTerminals`), so scrolling a card away or opening Full screen keeps
|
||||
the same state.
|
||||
- Running: 12 rows in the reading column; click/tap to focus and type
|
||||
(hardware keys, Ctrl-C, arrows, Tab, Esc go to the process). Paste:
|
||||
Cmd-V on Apple platforms, Ctrl-V or Ctrl-Shift-V elsewhere, plus the
|
||||
toolbar's Paste button. Ctrl-A and other Ctrl keys reach the process. A key
|
||||
toolbar under the terminal: Paste, Enter, Ctrl-C, Tab, Esc, arrows. On
|
||||
Android/iOS the toolbar also has a text field ("Type input, then Enter")
|
||||
for the soft keyboard, with a Hide typing toggle; it is emptied when sent.
|
||||
- The terminal is resized to its layout (debounced 150 ms); the process gets
|
||||
SIGWINCH. New runs start at the last laid-out size.
|
||||
- Printed `http(s)` links open in the browser on tap (wrapped links are
|
||||
joined). Other schemes are ignored.
|
||||
- Finished runs are drawn read-only, shrunk to the rows their output takes
|
||||
(at most 12), cursor hidden. Full screen opens the same terminal in a page.
|
||||
- Colours: background/foreground/cursor/selection from the host theme; the
|
||||
16 ANSI colours are xterm.dart's palette, darkened on light surfaces.
|
||||
- Copy output copies the server's plain-text rendering; Send to agent shares
|
||||
a bounded plain-text tail (no escape codes).
|
||||
- Pipe-mode and older runs (no `terminal`) are drawn in the same emulator with
|
||||
`\n` drawn as a new line, read-only.
|
||||
|
||||
## Protocol
|
||||
|
||||
- `chat_shell_run` request adds optional `cols`, `rows`; records add
|
||||
`terminal`, `cols`, `rows` (current size) and, only in
|
||||
`chat_shell_run_output`, `text` (plain-text rendering).
|
||||
- `chat_shell_input {session_id, run_id, data}`: `data` is UTF-8 text written
|
||||
byte-for-byte (its UTF-8 encoding). Control characters are JSON escapes:
|
||||
Ctrl-C `\u0003`, Enter `\r`, arrows `\u001b[A`… Paste is text (bracketed when
|
||||
the program asked). At most 16 KiB per request (clients split longer pastes
|
||||
at 4096 UTF-16 units); invalid UTF-8 is refused. Requests from one
|
||||
connection are applied in order through a 64-entry queue; a full queue is
|
||||
refused ("the command is not reading its input"). Reply has no body.
|
||||
- `chat_shell_resize {session_id, run_id, cols, rows}`: reply echoes the run.
|
||||
The last resize from any client wins.
|
||||
- Output frames are unchanged: raw terminal bytes (UTF-8, partial characters
|
||||
held back) with absolute byte offsets `[offset, end)`, which are the stream
|
||||
sequence numbers. `chat_shell_run_output` takes `after`: the reply's
|
||||
`output_start` is `max(after, retained start)`, so a reconnecting client
|
||||
fetches only what it missed and sees a gap if it fell out of the 1 MiB tail.
|
||||
The app does this on reconnect for runs it holds.
|
||||
- Persistence: `chat_shell_runs` gains `terminal`, `cols`, `rows`,
|
||||
`output_text` (additive columns). `output` is the raw stream tail (1 MiB),
|
||||
`output_text` its rendering (bounded to 1 MiB). Rows recorded before this
|
||||
change get a rendering computed on read.
|
||||
|
||||
## Security
|
||||
|
||||
- Any authenticated client watching the chat may type into or resize a
|
||||
running command, not only the one that started it (the same authority that
|
||||
can already start and stop commands in that chat). Input is checked against
|
||||
the chat's operator session like every other shell request.
|
||||
- Input is never persisted or logged: the server does not log
|
||||
`chat_shell_input`/`chat_shell_resize` request lines at all (keystrokes
|
||||
would otherwise be one log line each), and only what the terminal echoes
|
||||
becomes output. Password prompts disable echo, so hidden input never
|
||||
appears in output, frames, `output` or `output_text`.
|
||||
- The client keeps no copy of typed input: keys go from the emulator straight
|
||||
into a send queue that is cleared as each request is sent (and discarded on
|
||||
a refusal); the mobile field is cleared on send and can hide its text.
|
||||
- When the terminal has focus, the chat composer leaves Cmd/Ctrl-V alone
|
||||
(`ChatComposerPasteFocus.registerInput`), so a pasted token goes only to the
|
||||
process.
|
||||
|
||||
## Limits and known gaps
|
||||
|
||||
- Plain-text rendering is not an emulator: it strips CSI/OSC/DCS sequences,
|
||||
applies `\r`, backspace and line erase; full-screen programs render as their
|
||||
last lines. The card's emulator draws them properly.
|
||||
- Every watching client's emulator answers terminal queries (e.g. cursor
|
||||
position) for a live run, so two open clients send two answers.
|
||||
xterm.dart 4.0.0 answers DSR 6 zero-based; the app overrides it to the
|
||||
correct one-based answer.
|
||||
- xterm.dart 4.0.0 keeps blank rows when it rewraps; the app drops blank rows
|
||||
below the cursor after a resize so the top of the output stays in view.
|
||||
- Desktop typing uses key events (no IME composition for CJK input). Touch
|
||||
platforms use the field.
|
||||
- A descendant that keeps the PTY open after the command exits is read for
|
||||
1 s; on macOS the PTY has no read deadline, so its reader goroutine ends
|
||||
only when that descendant closes it (the run itself is finished and the
|
||||
group killed).
|
||||
- Mobile behaviour (soft keyboard field) is verified by widget tests only; no
|
||||
device test ran. Android/Linux builds were not run (web and macOS were).
|
||||
|
||||
## Files
|
||||
|
||||
- Server: `internal/protocol/chat_shell_terminal.go`, `chat_shell_run.go`;
|
||||
`internal/server/chat_shell_terminal{,_unix,_other}.go`,
|
||||
`chat_shell_plain.go`, `chat_shell_runner.go`, `chat_shell_supervise.go`,
|
||||
`chat_shell_run_handlers.go`, `chat_shell_share.go`, `chat.go`,
|
||||
`websocket.go`; `internal/pipeline/chat_shell_runs.go`; `go.mod`, `go.sum`.
|
||||
- App: `lib/features/chat/shell/chat_shell_terminal_{session,view,keys,theme,links,page}.dart`,
|
||||
`chat_shell_terminals.dart`, `chat_shell_output_view.dart`,
|
||||
`chat_shell_run{,_card}.dart`, `chat_shell_card_actions.dart`,
|
||||
`chat_shell_runs_{controller,repository}.dart`, `chat_shell_metrics.dart`;
|
||||
`lib/features/chat/conversation/chat_composer_paste_focus.dart`;
|
||||
`pubspec.yaml`/`pubspec.lock` (`xterm: ^4.0.0`, pure Dart; pub.dev lists
|
||||
Android, iOS, Linux, macOS, web, Windows); probe fixture
|
||||
`tool/perf/chat_shell_fixture.dart` (`?shell=1`: coloured runs and an
|
||||
interactive `gh auth login` that hides the token and finishes on Enter).
|
||||
|
||||
## Verification
|
||||
|
||||
Flutter 3.44.6 / Dart 3.12.2; macOS host.
|
||||
|
||||
- `go test ./internal/protocol ./internal/pipeline ./internal/server -count=1`:
|
||||
`ok …/protocol 0.205s`, `ok …/pipeline 15.838s`, `ok …/server 140.086s`.
|
||||
Real-PTY tests (`chat_shell_terminal_test.go`, `_ws_test.go`): a typed line
|
||||
reaches `read`; `stty -echo` input is in no frame, output or text; `\x03`
|
||||
interrupts; `stty size` sees the start size and a resize; resume by offset
|
||||
over the websocket from a second client; raw + plain text persisted;
|
||||
input bounds (16 KiB, empty, invalid UTF-8, full queue); size clamping.
|
||||
`go test -run ChatShell -count=5`: ok. `GOOS=freebsd go build
|
||||
./internal/server` (pipe fallback) and `GOOS=linux GOARCH=amd64 go test -c`
|
||||
compile. (`-race` shows an existing data race in
|
||||
`internal/opencode` `nativeRequest`, unrelated to this change.)
|
||||
- `tool/memwatch/full.sh test/features/chat`: `PASS: 1124 passed, 0 failed`.
|
||||
New: `test/features/chat/shell/chat_shell_terminal_test.dart` (desktop keys,
|
||||
Cmd-V paste not taken by the composer, Paste button, touch toolbar and
|
||||
hidden field cleared on send, resize sent, finished run read-only, colours,
|
||||
reconnect resume), `chat_shell_terminal_session_test.dart` (links incl.
|
||||
wrapped and non-http, query answers not resent on replay, one-based DSR,
|
||||
gap redraw, rewrap keeps first line, row fitting).
|
||||
- `tool/analyze.sh`: clean. `flutter build web`: built.
|
||||
`FLUTTER_XCODE_ARCHS=arm64 flutter build macos`: built (34.9MB).
|
||||
- Real Chrome (`tool/capture`, probe `?chats=2&shell=1`, 880x1000, Material and
|
||||
Cupertino, light and dark): `/tmp/chat-interactive-terminal/<platform>-<theme>/`
|
||||
`01-login-prompt.png` (coloured output, underlined link, prompt and cursor),
|
||||
`02-login-focused.png` (focused cursor), `03-login-done.png` (5 typed keys
|
||||
and Enter reached the fixture: "received a 5-character token", nothing
|
||||
echoed; the card shrank to its output, read-only), `04-full-screen.png`.
|
||||
- Scratch server end to end (`go build ./cmd/pipelines-server`, `env -i`
|
||||
with HOME/XDG_DATA_HOME/DB under `/tmp/pty-e2e`, OS-chosen loopback port,
|
||||
operator session inserted with sqlite3, Node websocket driver
|
||||
`/tmp/pty-e2e/drive.mjs`; server stopped afterwards, no processes left):
|
||||
|
||||
```
|
||||
started run_45be206dedb0454409c537b6 terminal=true size=90x20
|
||||
resize reply: chat_shell_resize 120x40
|
||||
input reply: chat_shell_input
|
||||
final: status=exited exit=0 bytes=145
|
||||
"tty: xterm-256color\r\n20 90\r\n\u001b[1;32mOpen https://example.com/login\u001b[0m\r\nPaste token: \r\n40 120\r\ngot 13 characters\r\nName: operator\r\nhello operator\r\n"
|
||||
secret in stream: false
|
||||
resume after 119: output_start=119 output="operator\r\nhello operator\r\n"
|
||||
secret in text: false
|
||||
late input: error this command is not running
|
||||
run_…|exited|0|1|120|40|145|125|0 (sqlite: terminal, cols, rows, raw, text, SECRET offset)
|
||||
```
|
||||
The server log has no `chat_shell_input` or `chat_shell_resize` lines.
|
||||
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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<JsonMap>.broadcast();
|
||||
List<JsonMap> 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<TerminalView>(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,
|
||||
|
||||
@@ -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 = <String>[];
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
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 = <String>[];
|
||||
// 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'),
|
||||
);
|
||||
});
|
||||
|
||||
test('narrowing rewraps without pushing the first line out of view', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('first\r\n${'u' * 30}\r\nprompt: '),
|
||||
onInput: (_) {},
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
session.terminal.resize(20, 6);
|
||||
final buffer = session.terminal.buffer;
|
||||
expect(buffer.height, 6);
|
||||
expect(buffer.lines[0].getText().trim(), 'first');
|
||||
expect(buffer.lines[3].getText().trim(), 'prompt:');
|
||||
expect(buffer.absoluteCursorY, 3);
|
||||
expect(buffer.cursorX, 8);
|
||||
});
|
||||
|
||||
test('a finished run counts the rows its output takes at a width', () {
|
||||
final session = ChatShellTerminalSession(
|
||||
_run('a\r\n${'x' * 50}\r\nend\r\n', status: 'exited'),
|
||||
onInput: (_) {},
|
||||
onResize: (_, _) {},
|
||||
);
|
||||
// The 50-character line wraps at the terminal's 40 columns.
|
||||
expect(session.rowsAt(40), 4);
|
||||
expect(session.rowsAt(20), 5);
|
||||
expect(session.rowsAt(100), 3);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
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<void> 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.keyG);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.digit7);
|
||||
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(), 'g7\x1b[A\x03');
|
||||
// Cmd-V pastes the clipboard into the process.
|
||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
(call) async =>
|
||||
call.method == 'Clipboard.getData' ? {'text': 'tok'} : null,
|
||||
);
|
||||
addTearDown(
|
||||
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||
SystemChannels.platform,
|
||||
null,
|
||||
),
|
||||
);
|
||||
await tester.sendKeyDownEvent(LogicalKeyboardKey.metaLeft);
|
||||
await tester.sendKeyEvent(LogicalKeyboardKey.keyV);
|
||||
await tester.sendKeyUpEvent(LogicalKeyboardKey.metaLeft);
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 20));
|
||||
}
|
||||
expect(typed(), 'g7\x1b[A\x03tok');
|
||||
// The chat composer leaves a paste into the terminal alone.
|
||||
expect(find.text('tok'), findsNothing);
|
||||
expect(find.byType(EditableText), findsWidgets);
|
||||
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<TextField>(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<TextField>(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<TerminalView>(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<TerminalView>(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<void>.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');
|
||||
});
|
||||
}
|
||||
@@ -1,17 +1,30 @@
|
||||
// Operator shell commands for the full-app probe (`?shell=1`, with
|
||||
// `?chats=N`): every fixture chat lists four recorded runs (succeeded, failed,
|
||||
// stopped and one still running), and chat_shell_run/stop/share answer as the
|
||||
// server does, with the frames that follow.
|
||||
// `?chats=N`): every fixture chat lists four recorded terminal runs
|
||||
// (succeeded, failed, stopped and an interactive login still waiting for a
|
||||
// token), and chat_shell_run/stop/share/input answer as the server does, with
|
||||
// the frames that follow. The login prompt hides what is typed, as a real
|
||||
// terminal's password prompt does, and finishes on Enter.
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
|
||||
bool _enabled = false;
|
||||
DateTime _base = DateTime.now();
|
||||
int _loginEnd = utf8.encode(_loginPrompt).length;
|
||||
int _tokenLength = 0;
|
||||
|
||||
void configureChatShellFixture(Map<String, String> params) {
|
||||
_enabled = params['shell'] == '1';
|
||||
_base = DateTime.now().subtract(const Duration(minutes: 3));
|
||||
_loginEnd = utf8.encode(_loginPrompt).length;
|
||||
}
|
||||
|
||||
const String _loginPrompt =
|
||||
'\x1b[1m!\x1b[0m First copy your one-time code: \x1b[1;33mA1B2-C3D4\x1b[0m\r\n'
|
||||
'Open this URL to continue in your web browser: '
|
||||
'\x1b[4mhttps://github.com/login/device\x1b[0m\r\n'
|
||||
'\x1b[32m?\x1b[0m \x1b[1mPaste an authentication token\x1b[0m: ';
|
||||
|
||||
int _at(int seconds) =>
|
||||
_base.add(Duration(seconds: seconds)).millisecondsSinceEpoch;
|
||||
|
||||
@@ -30,6 +43,9 @@ JsonMap _run(
|
||||
int duration = 0,
|
||||
String output = '',
|
||||
}) => {
|
||||
'terminal': true,
|
||||
'cols': 100,
|
||||
'rows': 30,
|
||||
'id': id,
|
||||
'session_id': session,
|
||||
'command': command,
|
||||
@@ -40,7 +56,7 @@ JsonMap _run(
|
||||
if (status != 'running') 'ended_at': _at(started) + duration,
|
||||
'duration_ms': duration,
|
||||
'output': output,
|
||||
'output_bytes': output.length,
|
||||
'output_bytes': utf8.encode(output).length,
|
||||
};
|
||||
|
||||
List<JsonMap> _runs(String session) => [
|
||||
@@ -52,8 +68,8 @@ List<JsonMap> _runs(String session) => [
|
||||
exitCode: 0,
|
||||
duration: 84,
|
||||
output:
|
||||
' M app/lib/features/chat/shell/chat_shell_run_card.dart\n'
|
||||
'?? app/notes/chat-shell-commands.md\n',
|
||||
' \x1b[31mM\x1b[0m app/lib/features/chat/shell/chat_shell_run_card.dart\r\n'
|
||||
'\x1b[31m??\x1b[0m app/notes/chat-shell-commands.md\r\n',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
@@ -63,12 +79,12 @@ List<JsonMap> _runs(String session) => [
|
||||
exitCode: 1,
|
||||
duration: 4210,
|
||||
output:
|
||||
'${[for (final name in _tests) '=== RUN TestChatShell$name\n--- PASS: TestChatShell$name (0.0${name.length % 9}s)\n'].join()}'
|
||||
'--- FAIL: TestChatShellStopInterruptsTheGroup (0.21s)\n'
|
||||
' chat_shell_run_test.go:112: stopped record = {Status:exited}\n'
|
||||
'FAIL\n'
|
||||
'FAIL\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\n'
|
||||
'FAIL\n',
|
||||
'${[for (final name in _tests) '=== RUN TestChatShell$name\r\n\x1b[32m--- PASS\x1b[0m: TestChatShell$name (0.0${name.length % 9}s)\r\n'].join()}'
|
||||
'\x1b[1;31m--- FAIL\x1b[0m: TestChatShellStopInterruptsTheGroup (0.21s)\r\n'
|
||||
' chat_shell_run_test.go:112: stopped record = {Status:exited}\r\n'
|
||||
'\x1b[31mFAIL\x1b[0m\r\n'
|
||||
'\x1b[31mFAIL\x1b[0m\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\r\n'
|
||||
'\x1b[31mFAIL\x1b[0m\r\n',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
@@ -77,17 +93,15 @@ List<JsonMap> _runs(String session) => [
|
||||
40,
|
||||
status: 'stopped',
|
||||
duration: 15200,
|
||||
output: 'Nov 9 15:58:01 host kernel[0]: wake reason: EC.LidOpen\n',
|
||||
output: 'Nov 9 15:58:01 host kernel[0]: wake reason: EC.LidOpen\r\n^C',
|
||||
),
|
||||
_run(
|
||||
session,
|
||||
'run_build',
|
||||
'flutter build web --target tool/search_selection_probe_entry.dart',
|
||||
'run_login',
|
||||
'gh auth login --hostname github.com',
|
||||
60,
|
||||
status: 'running',
|
||||
output:
|
||||
'Compiling lib/main.dart for the Web...\n'
|
||||
'Font asset "CupertinoIcons.ttf" was tree-shaken.\n',
|
||||
output: _loginPrompt,
|
||||
),
|
||||
];
|
||||
|
||||
@@ -129,6 +143,10 @@ JsonMap? chatShellReply(JsonMap request) {
|
||||
};
|
||||
case 'chat_shell_share':
|
||||
return {'id': id, 'type': 'chat_prompt'};
|
||||
case 'chat_shell_input':
|
||||
return {'id': id, 'type': 'chat_shell_input'};
|
||||
case 'chat_shell_resize':
|
||||
return {'id': id, 'type': 'chat_shell_resize'};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -171,6 +189,37 @@ List<JsonMap> chatShellFollowUps(JsonMap request) {
|
||||
return [
|
||||
frame({'run_id': run['id'], 'run': run}),
|
||||
];
|
||||
case 'chat_shell_input':
|
||||
// The token prompt does not echo; Enter completes the login.
|
||||
final body = request['chat_shell_run_request'];
|
||||
final data = body is Map ? '${body['data'] ?? ''}' : '';
|
||||
_tokenLength += data.replaceAll('\r', '').length;
|
||||
if (!data.contains('\r')) return const [];
|
||||
final done =
|
||||
'\r\n\x1b[2m(received a $_tokenLength-character token)\x1b[0m\r\n'
|
||||
'\x1b[32m\u2713\x1b[0m Configured git protocol\r\n'
|
||||
'\x1b[32m\u2713\x1b[0m Logged in as \x1b[1moperator\x1b[0m\r\n';
|
||||
final start = _loginEnd;
|
||||
_loginEnd += utf8.encode(done).length;
|
||||
return [
|
||||
frame({
|
||||
'run_id': 'run_login',
|
||||
'output': done,
|
||||
'offset': start,
|
||||
'end': _loginEnd,
|
||||
}),
|
||||
frame({
|
||||
'run_id': 'run_login',
|
||||
'run': _run(
|
||||
session,
|
||||
'run_login',
|
||||
'gh auth login --hostname github.com',
|
||||
60,
|
||||
exitCode: 0,
|
||||
duration: 48200,
|
||||
)..['output_bytes'] = _loginEnd,
|
||||
}),
|
||||
];
|
||||
}
|
||||
return const [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user