Merge branch 'worktree-agent-a26df8b0059ab1b12'
This commit is contained in:
@@ -5,14 +5,22 @@ import 'package:flutter/widgets.dart';
|
|||||||
abstract final class ChatComposerPasteFocus {
|
abstract final class ChatComposerPasteFocus {
|
||||||
static final Set<FocusNode> _composers = {};
|
static final Set<FocusNode> _composers = {};
|
||||||
|
|
||||||
|
static final Set<FocusNode> _inputs = {};
|
||||||
|
|
||||||
static void register(FocusNode focus) => _composers.add(focus);
|
static void register(FocusNode focus) => _composers.add(focus);
|
||||||
static void unregister(FocusNode focus) => _composers.remove(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 {
|
static bool get anotherEditableHasFocus {
|
||||||
final primary = FocusManager.instance.primaryFocus;
|
final primary = FocusManager.instance.primaryFocus;
|
||||||
if (primary == null || _composers.any((focus) => focus.hasFocus)) {
|
if (primary == null || _composers.any((focus) => focus.hasFocus)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (_inputs.any((focus) => focus.hasFocus)) return true;
|
||||||
final context = primary.context;
|
final context = primary.context;
|
||||||
if (context == null) return false;
|
if (context == null) return false;
|
||||||
var editable = context.widget is EditableText;
|
var editable = context.widget is EditableText;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import 'package:flutter/cupertino.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import 'chat_shell_metrics.dart';
|
|
||||||
import 'chat_shell_run.dart';
|
import 'chat_shell_run.dart';
|
||||||
import 'chat_shell_runs_controller.dart';
|
import 'chat_shell_runs_controller.dart';
|
||||||
|
|
||||||
@@ -15,26 +14,23 @@ const String chatShellCopiedLabel = 'Copied';
|
|||||||
const String chatShellShareLabel = 'Send to agent';
|
const String chatShellShareLabel = 'Send to agent';
|
||||||
const String chatShellSharingLabel = 'Sending…';
|
const String chatShellSharingLabel = 'Sending…';
|
||||||
const String chatShellSharedLabel = 'Sent to agent';
|
const String chatShellSharedLabel = 'Sent to agent';
|
||||||
const String chatShellExpandLabel = 'Expand';
|
const String chatShellFullScreenLabel = 'Full screen';
|
||||||
const String chatShellCollapseLabel = 'Collapse';
|
|
||||||
|
|
||||||
/// The card's host buttons: Stop while running; Copy output, Send to agent
|
/// 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 {
|
class ChatShellCardActions extends StatefulWidget {
|
||||||
const ChatShellCardActions({
|
const ChatShellCardActions({
|
||||||
super.key,
|
super.key,
|
||||||
required this.run,
|
required this.run,
|
||||||
required this.controller,
|
required this.controller,
|
||||||
required this.cupertino,
|
required this.cupertino,
|
||||||
required this.expanded,
|
required this.onFullScreen,
|
||||||
required this.onToggleExpanded,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
final ChatShellRun run;
|
final ChatShellRun run;
|
||||||
final ChatShellRunsController controller;
|
final ChatShellRunsController controller;
|
||||||
final bool cupertino;
|
final bool cupertino;
|
||||||
final bool expanded;
|
final VoidCallback onFullScreen;
|
||||||
final VoidCallback onToggleExpanded;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
State<ChatShellCardActions> createState() => _ChatShellCardActionsState();
|
||||||
@@ -50,12 +46,12 @@ class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
|||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copies everything the server kept, reading it first when this card
|
/// Copies the plain text of everything the server kept (no terminal
|
||||||
/// holds only the newest part.
|
/// escape sequences), read from the server.
|
||||||
Future<void> _copy() async {
|
Future<void> _copy() async {
|
||||||
final full = await widget.controller.loadAll(widget.run.id);
|
final text = await widget.controller.plainText(widget.run.id);
|
||||||
if (full == null) return;
|
if (text == null) return;
|
||||||
await Clipboard.setData(ClipboardData(text: full.output));
|
await Clipboard.setData(ClipboardData(text: text));
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() => _copied = true);
|
setState(() => _copied = true);
|
||||||
_reset?.cancel();
|
_reset?.cancel();
|
||||||
@@ -114,17 +110,13 @@ class _ChatShellCardActionsState extends State<ChatShellCardActions> {
|
|||||||
? () => unawaited(controller.share(run.id))
|
? () => unawaited(controller.share(run.id))
|
||||||
: null,
|
: null,
|
||||||
),
|
),
|
||||||
if (widget.expanded || chatShellOutputMayOverflow(run.output))
|
if (run.running || run.output.isNotEmpty)
|
||||||
_button(
|
_button(
|
||||||
key: const Key('chat-shell-run-expand'),
|
key: const Key('chat-shell-run-expand'),
|
||||||
label: widget.expanded
|
label: chatShellFullScreenLabel,
|
||||||
? chatShellCollapseLabel
|
material: Icons.fullscreen,
|
||||||
: chatShellExpandLabel,
|
cupertino: CupertinoIcons.fullscreen,
|
||||||
material: widget.expanded ? Icons.unfold_less : Icons.unfold_more,
|
onPressed: widget.onFullScreen,
|
||||||
cupertino: widget.expanded
|
|
||||||
? CupertinoIcons.chevron_up
|
|
||||||
: CupertinoIcons.chevron_down,
|
|
||||||
onPressed: widget.onToggleExpanded,
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,43 +1,5 @@
|
|||||||
import 'package:flutter/widgets.dart';
|
/// 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
|
||||||
/// The one layout bound of a shell command card: how tall its output area
|
/// finished one shrinks to the rows its output uses, up to this many, and
|
||||||
/// may grow. Collapsed it shows about [kChatShellOutputCollapsedLines] lines
|
/// scrolls within them. Full screen takes the whole window instead.
|
||||||
/// of the host's (scaled) monospace text; expanded it may take
|
|
||||||
/// [kChatShellOutputExpandedShare] of the window height. Everything else in
|
|
||||||
/// the card is content-sized.
|
|
||||||
const int kChatShellOutputCollapsedLines = 12;
|
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 'package:flutter/material.dart';
|
||||||
|
|
||||||
import '../../../ui/text/monospace.dart';
|
|
||||||
import '../conversation/chat_card_styles.dart';
|
import '../conversation/chat_card_styles.dart';
|
||||||
import 'chat_shell_metrics.dart';
|
|
||||||
import 'chat_shell_run.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
|
/// The output area of one command card: the run's terminal emulator, which
|
||||||
/// body on a quiet inset surface, scrollable inside a bounded height and
|
/// takes typing while the command runs (with the key toolbar below it), or
|
||||||
/// anchored at the newest line while the command runs.
|
/// its retained output read-only once it ended.
|
||||||
class ChatShellOutputView extends StatefulWidget {
|
class ChatShellOutputView extends StatelessWidget {
|
||||||
const ChatShellOutputView({
|
const ChatShellOutputView({
|
||||||
super.key,
|
super.key,
|
||||||
required this.run,
|
required this.run,
|
||||||
|
required this.controller,
|
||||||
required this.cupertino,
|
required this.cupertino,
|
||||||
required this.expanded,
|
required this.viewKey,
|
||||||
|
required this.fullScreen,
|
||||||
});
|
});
|
||||||
|
|
||||||
final ChatShellRun run;
|
final ChatShellRun run;
|
||||||
|
final ChatShellRunsController controller;
|
||||||
final bool cupertino;
|
final bool cupertino;
|
||||||
final bool expanded;
|
|
||||||
|
|
||||||
@override
|
/// Replaced when the full-screen view closes, so the card's view sizes the
|
||||||
State<ChatShellOutputView> createState() => _ChatShellOutputViewState();
|
/// terminal to itself again.
|
||||||
}
|
final Key viewKey;
|
||||||
|
|
||||||
class _ChatShellOutputViewState extends State<ChatShellOutputView> {
|
/// True while the full-screen view sizes the terminal.
|
||||||
final ScrollController _scroll = ScrollController();
|
final bool fullScreen;
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_scroll.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final run = widget.run;
|
if (!run.running && run.output.isEmpty) {
|
||||||
final cupertino = widget.cupertino;
|
return Text(
|
||||||
final body = chatCardBodyStyle(context, cupertino) ?? const TextStyle();
|
'No output.',
|
||||||
final fontSize = (body.fontSize ?? 14) * 0.95;
|
key: const Key('chat-shell-output-empty'),
|
||||||
final style = body.copyWith(
|
style: chatCardSecondaryStyle(context, cupertino),
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
// The final newline ends the last line; it is not an empty line to draw.
|
final session = controller.terminals.session(run);
|
||||||
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.',
|
|
||||||
];
|
|
||||||
return Column(
|
return Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
if (notes.isNotEmpty)
|
ChatShellTerminalView(
|
||||||
Padding(
|
key: viewKey,
|
||||||
padding: const EdgeInsets.only(bottom: 4),
|
session: session,
|
||||||
child: Text(
|
cupertino: cupertino,
|
||||||
notes.join(' '),
|
autoResize: !fullScreen,
|
||||||
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,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
|
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.outputEnd = 0,
|
||||||
this.outputBytes = 0,
|
this.outputBytes = 0,
|
||||||
this.gap = false,
|
this.gap = false,
|
||||||
|
this.terminal = false,
|
||||||
|
this.cols = 0,
|
||||||
|
this.rows = 0,
|
||||||
|
this.text = '',
|
||||||
});
|
});
|
||||||
|
|
||||||
final String id;
|
final String id;
|
||||||
@@ -53,6 +57,17 @@ class ChatShellRun {
|
|||||||
/// stretch of what the command wrote.
|
/// stretch of what the command wrote.
|
||||||
final bool gap;
|
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.
|
/// Whether earlier output is not shown here.
|
||||||
bool get truncated => outputStart > 0 || gap;
|
bool get truncated => outputStart > 0 || gap;
|
||||||
|
|
||||||
@@ -92,6 +107,10 @@ class ChatShellRun {
|
|||||||
// A readback's text runs to the newest byte the server had.
|
// A readback's text runs to the newest byte the server had.
|
||||||
outputEnd: output.isEmpty && start == 0 ? 0 : bytes,
|
outputEnd: output.isEmpty && start == 0 ? 0 : bytes,
|
||||||
outputBytes: 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
|
? next.outputBytes
|
||||||
: outputBytes,
|
: outputBytes,
|
||||||
gap: gap,
|
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
|
/// This run with one live output chunk covering bytes [offset, end). A
|
||||||
@@ -144,6 +167,9 @@ class ChatShellRun {
|
|||||||
outputEnd: end,
|
outputEnd: end,
|
||||||
outputBytes: end > outputBytes ? end : outputBytes,
|
outputBytes: end > outputBytes ? end : outputBytes,
|
||||||
gap: gap || dropped || (!contiguous && offset > 0),
|
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_run.dart';
|
||||||
import 'chat_shell_runs_controller.dart';
|
import 'chat_shell_runs_controller.dart';
|
||||||
import 'chat_shell_status.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
|
/// One operator command in the transcript, as a terminal card in the reading
|
||||||
/// reading column: the command and folder, its live output, a spinner and
|
/// column: the command and folder, its terminal (typed into while it runs),
|
||||||
/// Stop while it runs, then its exit status, Copy output and Send to agent.
|
/// 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
|
/// It listens to [controller] itself, so streamed output rebuilds only this
|
||||||
/// card, never the transcript.
|
/// card, never the transcript.
|
||||||
@@ -32,7 +34,23 @@ class ChatShellRunCard extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -113,8 +131,10 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
|||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
ChatShellOutputView(
|
ChatShellOutputView(
|
||||||
run: run,
|
run: run,
|
||||||
|
controller: controller,
|
||||||
cupertino: cupertino,
|
cupertino: cupertino,
|
||||||
expanded: _expanded,
|
viewKey: ValueKey<int>(_viewGeneration),
|
||||||
|
fullScreen: _fullScreen,
|
||||||
),
|
),
|
||||||
if (run.error.isNotEmpty && !run.running)
|
if (run.error.isNotEmpty && !run.running)
|
||||||
Padding(
|
Padding(
|
||||||
@@ -129,8 +149,7 @@ class _ChatShellRunCardState extends State<ChatShellRunCard> {
|
|||||||
run: run,
|
run: run,
|
||||||
controller: controller,
|
controller: controller,
|
||||||
cupertino: cupertino,
|
cupertino: cupertino,
|
||||||
expanded: _expanded,
|
onFullScreen: () => _openFullScreen(cupertino),
|
||||||
onToggleExpanded: () => setState(() => _expanded = !_expanded),
|
|
||||||
),
|
),
|
||||||
if (error != null)
|
if (error != null)
|
||||||
Semantics(
|
Semantics(
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import '../../../core/api_gateway.dart';
|
|||||||
import '../../../core/error_reason.dart';
|
import '../../../core/error_reason.dart';
|
||||||
import 'chat_shell_run.dart';
|
import 'chat_shell_run.dart';
|
||||||
import 'chat_shell_runs_repository.dart';
|
import 'chat_shell_runs_repository.dart';
|
||||||
|
import 'chat_shell_terminals.dart';
|
||||||
|
|
||||||
/// Where one run's "Send to agent" stands.
|
/// Where one run's "Send to agent" stands.
|
||||||
enum ChatShellShareState { idle, sending, sent }
|
enum ChatShellShareState { idle, sending, sent }
|
||||||
@@ -19,10 +20,22 @@ enum ChatShellShareState { idle, sending, sent }
|
|||||||
class ChatShellRunsController extends ChangeNotifier {
|
class ChatShellRunsController extends ChangeNotifier {
|
||||||
ChatShellRunsController(this.gateway, this.sessionId)
|
ChatShellRunsController(this.gateway, this.sessionId)
|
||||||
: _repository = ChatShellRunsRepository(gateway) {
|
: _repository = ChatShellRunsRepository(gateway) {
|
||||||
|
terminals = ChatShellTerminals(
|
||||||
|
_repository,
|
||||||
|
sessionId,
|
||||||
|
onError: (id, error) {
|
||||||
|
if (_disposed) return;
|
||||||
|
_errors[id] = _reason(error);
|
||||||
|
_changed();
|
||||||
|
},
|
||||||
|
);
|
||||||
_frames = gateway.frames.listen(_onFrame, onError: (_) {});
|
_frames = gateway.frames.listen(_onFrame, onError: (_) {});
|
||||||
unawaited(refresh());
|
unawaited(refresh());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The runs' terminal emulators and their input and size delivery.
|
||||||
|
late final ChatShellTerminals terminals;
|
||||||
|
|
||||||
final ApiGateway gateway;
|
final ApiGateway gateway;
|
||||||
final String sessionId;
|
final String sessionId;
|
||||||
final ChatShellRunsRepository _repository;
|
final ChatShellRunsRepository _repository;
|
||||||
@@ -54,9 +67,20 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
if (_disposed || token != _generation) return;
|
if (_disposed || token != _generation) return;
|
||||||
for (final run in listed) {
|
for (final run in listed) {
|
||||||
final held = _runs[run.id];
|
final held = _runs[run.id];
|
||||||
_runs[run.id] = held != null && held.outputEnd > run.outputEnd
|
if (held != null &&
|
||||||
? held.withLifecycle(run)
|
held.running &&
|
||||||
: run;
|
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);
|
_changed(membership: true);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -67,7 +91,13 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
/// Starts [command] in the chat's directory. Returns the refusal, or null.
|
/// Starts [command] in the chat's directory. Returns the refusal, or null.
|
||||||
Future<String?> start(String command) async {
|
Future<String?> start(String command) async {
|
||||||
try {
|
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;
|
if (_disposed) return null;
|
||||||
_runs.putIfAbsent(run.id, () => run);
|
_runs.putIfAbsent(run.id, () => run);
|
||||||
_changed(membership: true);
|
_changed(membership: true);
|
||||||
@@ -107,28 +137,53 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
_changed();
|
_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Every byte the server retained for [id]: what Copy output copies and
|
/// The plain-text rendering of everything the server kept for [id]: what
|
||||||
/// Show all draws. Null when it cannot be read (the error is recorded).
|
/// Copy output copies. Null when it cannot be read (the error is recorded).
|
||||||
Future<ChatShellRun?> loadAll(String id) async {
|
Future<String?> plainText(String id) async {
|
||||||
final held = _runs[id];
|
|
||||||
if (held == null) return null;
|
|
||||||
if (!held.truncated && !held.running) return held;
|
|
||||||
try {
|
try {
|
||||||
final full = await _repository.output(sessionId, id);
|
final full = await _repository.output(sessionId, id);
|
||||||
if (_disposed) return null;
|
// A server without terminals records plain output and no rendering.
|
||||||
final latest = _runs[id] ?? held;
|
return full.terminal || full.text.isNotEmpty ? full.text : full.output;
|
||||||
// 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;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (_disposed) return null;
|
||||||
_errors[id] = _reason(error);
|
_errors[id] = _reason(error);
|
||||||
_changed();
|
_changed();
|
||||||
return null;
|
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) {
|
void _onFrame(JsonMap envelope) {
|
||||||
if (_disposed || envelope['type'] != 'chat_shell_run_event') return;
|
if (_disposed || envelope['type'] != 'chat_shell_run_event') return;
|
||||||
final frame = envelope['chat_shell_run_event'];
|
final frame = envelope['chat_shell_run_event'];
|
||||||
@@ -137,8 +192,12 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
final record = ChatShellRun.fromJson(frame['run']);
|
final record = ChatShellRun.fromJson(frame['run']);
|
||||||
if (record != null) {
|
if (record != null) {
|
||||||
final held = _runs[record.id];
|
final held = _runs[record.id];
|
||||||
_runs[record.id] = held == null ? record : held.withLifecycle(record);
|
final next = held == null ? record : held.withLifecycle(record);
|
||||||
if (!record.running) _stopping.remove(record.id);
|
_runs[record.id] = next;
|
||||||
|
if (!record.running) {
|
||||||
|
_stopping.remove(record.id);
|
||||||
|
if (held != null && held.running) terminals.finished(next);
|
||||||
|
}
|
||||||
_changed(membership: held == null);
|
_changed(membership: held == null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -148,11 +207,12 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
unawaited(refresh());
|
unawaited(refresh());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_runs[id] = held.withChunk(
|
final text = '${frame['output'] ?? ''}';
|
||||||
'${frame['output'] ?? ''}',
|
final offset = (frame['offset'] as num?)?.toInt() ?? 0;
|
||||||
(frame['offset'] as num?)?.toInt() ?? 0,
|
final end = (frame['end'] as num?)?.toInt() ?? 0;
|
||||||
(frame['end'] as num?)?.toInt() ?? 0,
|
final next = held.withChunk(text, offset, end);
|
||||||
);
|
_runs[id] = next;
|
||||||
|
terminals.appended(next, text, offset, end);
|
||||||
_changed();
|
_changed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,6 +229,7 @@ class ChatShellRunsController extends ChangeNotifier {
|
|||||||
void dispose() {
|
void dispose() {
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
_generation++;
|
_generation++;
|
||||||
|
terminals.dispose();
|
||||||
unawaited(_frames?.cancel());
|
unawaited(_frames?.cancel());
|
||||||
_membership.dispose();
|
_membership.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|||||||
@@ -6,19 +6,52 @@ class ChatShellRunsRepository {
|
|||||||
const ChatShellRunsRepository(this.gateway);
|
const ChatShellRunsRepository(this.gateway);
|
||||||
final ApiGateway 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': {
|
'chat_shell_run_request': {
|
||||||
'session_id': sessionId,
|
'session_id': sessionId,
|
||||||
'command': ?command,
|
'command': ?command,
|
||||||
'run_id': ?runId,
|
'run_id': ?runId,
|
||||||
|
'cols': ?cols,
|
||||||
|
'rows': ?rows,
|
||||||
|
'data': ?data,
|
||||||
|
'after': ?after,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Starts [command]; the reply is its running record.
|
/// Starts [command] in a terminal of [cols] x [rows] (the server's default
|
||||||
Future<ChatShellRun> run(String sessionId, String command) async => _record(
|
/// size when null); the reply is its running record.
|
||||||
await gateway.call('chat_shell_run', _body(sessionId, command: command)),
|
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) =>
|
Future<void> stop(String sessionId, String runId) =>
|
||||||
gateway.call('chat_shell_stop', _body(sessionId, runId: 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.
|
/// One run with the whole output the server retained and its plain-text
|
||||||
Future<ChatShellRun> output(String sessionId, String runId) async => _record(
|
/// rendering, or only the output from absolute byte [after] on.
|
||||||
await gateway.call('chat_shell_run_output', _body(sessionId, runId: runId)),
|
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.
|
/// 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"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.19.1"
|
version: "1.19.1"
|
||||||
|
convert:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: convert
|
||||||
|
sha256: b30acd5944035672bc15c6b7a8b47d773e41e2f17de064350988c5d02adb1c68
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.1.2"
|
||||||
cross_file:
|
cross_file:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -153,6 +161,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.7.15"
|
version: "0.7.15"
|
||||||
|
equatable:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: "3bce007a596ff8b3119c45d68aaef631272537c03d30e5d4534dd24bf4c5eaa2"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.0"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -575,6 +591,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.2.1"
|
version: "2.2.1"
|
||||||
|
quiver:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: quiver
|
||||||
|
sha256: ea0b925899e64ecdfbf9c7becb60d5b50e706ade44a85b2363be2a22d88117d2
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "3.2.2"
|
||||||
record_use:
|
record_use:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -956,6 +980,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "7.0.1"
|
version: "7.0.1"
|
||||||
|
xterm:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: xterm
|
||||||
|
sha256: "168dfedca77cba33fdb6f52e2cd001e9fde216e398e89335c19b524bb22da3a2"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.0"
|
||||||
yaml:
|
yaml:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -964,6 +996,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.4"
|
version: "3.1.4"
|
||||||
|
zmodem:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: zmodem
|
||||||
|
sha256: "3b7e5b29f3a7d8aee472029b05165a68438eff2f3f7766edf13daba1e297adbf"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.0.6"
|
||||||
sdks:
|
sdks:
|
||||||
dart: ">=3.12.2 <4.0.0"
|
dart: ">=3.12.2 <4.0.0"
|
||||||
flutter: ">=3.44.0"
|
flutter: ">=3.44.0"
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ dependencies:
|
|||||||
file_picker: ^13.3.0
|
file_picker: ^13.3.0
|
||||||
url_launcher: ^6.3.3
|
url_launcher: ^6.3.3
|
||||||
bonsoir: ^7.1.5
|
bonsoir: ^7.1.5
|
||||||
|
xterm: ^4.0.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
integration_test:
|
integration_test:
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ import 'dart:async';
|
|||||||
|
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.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/core/api_gateway.dart';
|
||||||
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.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:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||||
|
import 'package:xterm/xterm.dart';
|
||||||
|
|
||||||
import '../conversation/chat_thread_fixtures.dart';
|
import '../conversation/chat_thread_fixtures.dart';
|
||||||
|
|
||||||
@@ -15,6 +17,9 @@ class ShellGateway implements ApiGateway {
|
|||||||
final _frames = StreamController<JsonMap>.broadcast();
|
final _frames = StreamController<JsonMap>.broadcast();
|
||||||
List<JsonMap> runs = const [];
|
List<JsonMap> runs = const [];
|
||||||
String? refusal;
|
String? refusal;
|
||||||
|
|
||||||
|
/// The reply to a `chat_shell_run_output` that resumes from an offset.
|
||||||
|
JsonMap? resumeReply;
|
||||||
int _serial = 0;
|
int _serial = 0;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -47,6 +52,9 @@ class ShellGateway implements ApiGateway {
|
|||||||
),
|
),
|
||||||
};
|
};
|
||||||
case 'chat_shell_run_output':
|
case 'chat_shell_run_output':
|
||||||
|
if (body['after'] != null && resumeReply != null) {
|
||||||
|
return {'chat_shell_run': resumeReply};
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
'chat_shell_run': runs.firstWhere((r) => r['id'] == body['run_id']),
|
||||||
};
|
};
|
||||||
@@ -84,6 +92,9 @@ JsonMap shellRecord(
|
|||||||
int? exitCode,
|
int? exitCode,
|
||||||
String output = '',
|
String output = '',
|
||||||
int durationMs = 0,
|
int durationMs = 0,
|
||||||
|
bool terminal = false,
|
||||||
|
String text = '',
|
||||||
|
int outputStart = 0,
|
||||||
}) => {
|
}) => {
|
||||||
'id': id,
|
'id': id,
|
||||||
'session_id': 's',
|
'session_id': 's',
|
||||||
@@ -95,9 +106,27 @@ JsonMap shellRecord(
|
|||||||
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
'ended_at': ?(status == 'running' ? null : 1000 + durationMs),
|
||||||
'duration_ms': durationMs,
|
'duration_ms': durationMs,
|
||||||
'output': output,
|
'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}) {
|
Widget shellThread(ShellGateway gateway, {bool cupertino = false}) {
|
||||||
final thread = ChatThreadView(
|
final thread = ChatThreadView(
|
||||||
gateway: gateway,
|
gateway: gateway,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
|
import 'package:xterm/xterm.dart';
|
||||||
|
|
||||||
import 'chat_shell_fixtures.dart';
|
import 'chat_shell_fixtures.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -27,7 +29,7 @@ void main() {
|
|||||||
expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget);
|
expect(find.byKey(const Key('chat-shell-run-running')), findsOneWidget);
|
||||||
gateway.emitOutput('run_a', 'line 2\n', 7);
|
gateway.emitOutput('run_a', 'line 2\n', 7);
|
||||||
await tester.pump();
|
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);
|
expect(find.byKey(const Key('chat-shell-run-share')), findsNothing);
|
||||||
|
|
||||||
await tester.tap(find.byKey(const Key('chat-shell-run-stop')));
|
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.text('Stopped · 2.4 s'), findsOneWidget);
|
||||||
expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing);
|
expect(find.byKey(const Key('chat-shell-run-stop')), findsNothing);
|
||||||
// The streamed output stays after the lifecycle frame.
|
// 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', (
|
testWidgets('$host: finished commands show their exit status', (
|
||||||
@@ -69,6 +71,8 @@ void main() {
|
|||||||
];
|
];
|
||||||
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
await tester.pumpWidget(shellThread(gateway, cupertino: cupertino));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
|
// Finished terminals shrink to their output after their first frame.
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
|
||||||
expect(card(), findsNWidgets(2));
|
expect(card(), findsNWidgets(2));
|
||||||
expect(find.text('Exit 0 · 120 ms'), findsOneWidget);
|
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>[];
|
final copied = <String>[];
|
||||||
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
||||||
SystemChannels.platform,
|
SystemChannels.platform,
|
||||||
@@ -112,10 +116,18 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
gateway.runs = [
|
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.pumpWidget(shellThread(gateway));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
|
expect(terminalText(tester), 'a\nb');
|
||||||
await tester.tap(find.byKey(const Key('chat-shell-run-copy')));
|
await tester.tap(find.byKey(const Key('chat-shell-run-copy')));
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
expect(copied, ['a\nb\n']);
|
expect(copied, ['a\nb\n']);
|
||||||
@@ -151,7 +163,14 @@ void main() {
|
|||||||
await tester.ensureVisible(expand);
|
await tester.ensureVisible(expand);
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
await tester.tap(expand);
|
await tester.tap(expand);
|
||||||
await tester.pump();
|
await tester.pumpAndSettle();
|
||||||
expect(find.text('Collapse'), findsOneWidget);
|
// 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
|
// Operator shell commands for the full-app probe (`?shell=1`, with
|
||||||
// `?chats=N`): every fixture chat lists four recorded runs (succeeded, failed,
|
// `?chats=N`): every fixture chat lists four recorded terminal runs
|
||||||
// stopped and one still running), and chat_shell_run/stop/share answer as the
|
// (succeeded, failed, stopped and an interactive login still waiting for a
|
||||||
// server does, with the frames that follow.
|
// 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';
|
import 'package:pipelines_app/core/api_gateway.dart';
|
||||||
|
|
||||||
bool _enabled = false;
|
bool _enabled = false;
|
||||||
DateTime _base = DateTime.now();
|
DateTime _base = DateTime.now();
|
||||||
|
int _loginEnd = utf8.encode(_loginPrompt).length;
|
||||||
|
int _tokenLength = 0;
|
||||||
|
|
||||||
void configureChatShellFixture(Map<String, String> params) {
|
void configureChatShellFixture(Map<String, String> params) {
|
||||||
_enabled = params['shell'] == '1';
|
_enabled = params['shell'] == '1';
|
||||||
_base = DateTime.now().subtract(const Duration(minutes: 3));
|
_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) =>
|
int _at(int seconds) =>
|
||||||
_base.add(Duration(seconds: seconds)).millisecondsSinceEpoch;
|
_base.add(Duration(seconds: seconds)).millisecondsSinceEpoch;
|
||||||
|
|
||||||
@@ -30,6 +43,9 @@ JsonMap _run(
|
|||||||
int duration = 0,
|
int duration = 0,
|
||||||
String output = '',
|
String output = '',
|
||||||
}) => {
|
}) => {
|
||||||
|
'terminal': true,
|
||||||
|
'cols': 100,
|
||||||
|
'rows': 30,
|
||||||
'id': id,
|
'id': id,
|
||||||
'session_id': session,
|
'session_id': session,
|
||||||
'command': command,
|
'command': command,
|
||||||
@@ -40,7 +56,7 @@ JsonMap _run(
|
|||||||
if (status != 'running') 'ended_at': _at(started) + duration,
|
if (status != 'running') 'ended_at': _at(started) + duration,
|
||||||
'duration_ms': duration,
|
'duration_ms': duration,
|
||||||
'output': output,
|
'output': output,
|
||||||
'output_bytes': output.length,
|
'output_bytes': utf8.encode(output).length,
|
||||||
};
|
};
|
||||||
|
|
||||||
List<JsonMap> _runs(String session) => [
|
List<JsonMap> _runs(String session) => [
|
||||||
@@ -52,8 +68,8 @@ List<JsonMap> _runs(String session) => [
|
|||||||
exitCode: 0,
|
exitCode: 0,
|
||||||
duration: 84,
|
duration: 84,
|
||||||
output:
|
output:
|
||||||
' M app/lib/features/chat/shell/chat_shell_run_card.dart\n'
|
' \x1b[31mM\x1b[0m app/lib/features/chat/shell/chat_shell_run_card.dart\r\n'
|
||||||
'?? app/notes/chat-shell-commands.md\n',
|
'\x1b[31m??\x1b[0m app/notes/chat-shell-commands.md\r\n',
|
||||||
),
|
),
|
||||||
_run(
|
_run(
|
||||||
session,
|
session,
|
||||||
@@ -63,12 +79,12 @@ List<JsonMap> _runs(String session) => [
|
|||||||
exitCode: 1,
|
exitCode: 1,
|
||||||
duration: 4210,
|
duration: 4210,
|
||||||
output:
|
output:
|
||||||
'${[for (final name in _tests) '=== RUN TestChatShell$name\n--- PASS: TestChatShell$name (0.0${name.length % 9}s)\n'].join()}'
|
'${[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()}'
|
||||||
'--- FAIL: TestChatShellStopInterruptsTheGroup (0.21s)\n'
|
'\x1b[1;31m--- FAIL\x1b[0m: TestChatShellStopInterruptsTheGroup (0.21s)\r\n'
|
||||||
' chat_shell_run_test.go:112: stopped record = {Status:exited}\n'
|
' chat_shell_run_test.go:112: stopped record = {Status:exited}\r\n'
|
||||||
'FAIL\n'
|
'\x1b[31mFAIL\x1b[0m\r\n'
|
||||||
'FAIL\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\n'
|
'\x1b[31mFAIL\x1b[0m\tgit.a-wan.net/alan/pipelines/internal/server\t0.611s\r\n'
|
||||||
'FAIL\n',
|
'\x1b[31mFAIL\x1b[0m\r\n',
|
||||||
),
|
),
|
||||||
_run(
|
_run(
|
||||||
session,
|
session,
|
||||||
@@ -77,17 +93,15 @@ List<JsonMap> _runs(String session) => [
|
|||||||
40,
|
40,
|
||||||
status: 'stopped',
|
status: 'stopped',
|
||||||
duration: 15200,
|
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(
|
_run(
|
||||||
session,
|
session,
|
||||||
'run_build',
|
'run_login',
|
||||||
'flutter build web --target tool/search_selection_probe_entry.dart',
|
'gh auth login --hostname github.com',
|
||||||
60,
|
60,
|
||||||
status: 'running',
|
status: 'running',
|
||||||
output:
|
output: _loginPrompt,
|
||||||
'Compiling lib/main.dart for the Web...\n'
|
|
||||||
'Font asset "CupertinoIcons.ttf" was tree-shaken.\n',
|
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -129,6 +143,10 @@ JsonMap? chatShellReply(JsonMap request) {
|
|||||||
};
|
};
|
||||||
case 'chat_shell_share':
|
case 'chat_shell_share':
|
||||||
return {'id': id, 'type': 'chat_prompt'};
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -171,6 +189,37 @@ List<JsonMap> chatShellFollowUps(JsonMap request) {
|
|||||||
return [
|
return [
|
||||||
frame({'run_id': run['id'], 'run': run}),
|
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 [];
|
return const [];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ require (
|
|||||||
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
|
github.com/charmbracelet/x/cellbuf v0.0.13 // indirect
|
||||||
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
|
github.com/charmbracelet/x/exp/slice v0.0.0-20250327172914-2fdc97757edf // indirect
|
||||||
github.com/charmbracelet/x/term v0.2.1 // indirect
|
github.com/charmbracelet/x/term v0.2.1 // indirect
|
||||||
|
github.com/creack/pty v1.1.24 // indirect
|
||||||
github.com/dlclark/regexp2 v1.11.0 // indirect
|
github.com/dlclark/regexp2 v1.11.0 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/ebitengine/purego v0.11.2 // indirect
|
github.com/ebitengine/purego v0.11.2 // indirect
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQ
|
|||||||
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
||||||
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNUA=
|
||||||
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||||
|
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||||
|
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||||
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
|
||||||
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
|
|
||||||
// initChatShellRuns creates the additive table of operator shell commands. A
|
// initChatShellRuns creates the additive table of operator shell commands. A
|
||||||
// run belongs to its chat session and is Pipelines' own record: it is never
|
// run belongs to its chat session and is Pipelines' own record: it is never
|
||||||
// part of the harness transcript. Output holds only the retained tail.
|
// part of the harness transcript. Output holds only the retained tail (the raw
|
||||||
|
// terminal stream of a terminal run); output_text is its plain-text rendering.
|
||||||
|
// Typed input is never stored.
|
||||||
func (s *Store) initChatShellRuns() error {
|
func (s *Store) initChatShellRuns() error {
|
||||||
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_shell_runs (
|
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS chat_shell_runs (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
@@ -28,7 +30,20 @@ output TEXT NOT NULL DEFAULT '',
|
|||||||
output_start INTEGER NOT NULL DEFAULT 0,
|
output_start INTEGER NOT NULL DEFAULT 0,
|
||||||
output_bytes INTEGER NOT NULL DEFAULT 0);
|
output_bytes INTEGER NOT NULL DEFAULT 0);
|
||||||
CREATE INDEX IF NOT EXISTS chat_shell_runs_session ON chat_shell_runs(session_id, started_at);`)
|
CREATE INDEX IF NOT EXISTS chat_shell_runs_session ON chat_shell_runs(session_id, started_at);`)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, column := range [][2]string{
|
||||||
|
{"terminal", "INTEGER NOT NULL DEFAULT 0"},
|
||||||
|
{"cols", "INTEGER NOT NULL DEFAULT 0"},
|
||||||
|
{"rows", "INTEGER NOT NULL DEFAULT 0"},
|
||||||
|
{"output_text", "TEXT NOT NULL DEFAULT ''"},
|
||||||
|
} {
|
||||||
|
if err := s.addColumnIfMissing("chat_shell_runs", column[0], column[1]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveChatShellRun inserts or replaces one run. Inserting a new run prunes the
|
// SaveChatShellRun inserts or replaces one run. Inserting a new run prunes the
|
||||||
@@ -44,13 +59,16 @@ func (s *Store) SaveChatShellRun(ctx context.Context, run protocol.ChatShellRunR
|
|||||||
exit = *run.ExitCode
|
exit = *run.ExitCode
|
||||||
}
|
}
|
||||||
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_shell_runs
|
if _, err := tx.ExecContext(ctx, `INSERT INTO chat_shell_runs
|
||||||
(id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes)
|
(id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes,
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
terminal, cols, rows, output_text)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT(id) DO UPDATE SET status = excluded.status, exit_code = excluded.exit_code, signal = excluded.signal,
|
ON CONFLICT(id) DO UPDATE SET status = excluded.status, exit_code = excluded.exit_code, signal = excluded.signal,
|
||||||
error = excluded.error, ended_at = excluded.ended_at, duration_ms = excluded.duration_ms, output = excluded.output,
|
error = excluded.error, ended_at = excluded.ended_at, duration_ms = excluded.duration_ms, output = excluded.output,
|
||||||
output_start = excluded.output_start, output_bytes = excluded.output_bytes`,
|
output_start = excluded.output_start, output_bytes = excluded.output_bytes, terminal = excluded.terminal,
|
||||||
|
cols = excluded.cols, rows = excluded.rows, output_text = excluded.output_text`,
|
||||||
run.ID, run.SessionID, run.Command, run.Directory, run.Status, exit, run.Signal, run.Error,
|
run.ID, run.SessionID, run.Command, run.Directory, run.Status, exit, run.Signal, run.Error,
|
||||||
run.StartedAt, run.EndedAt, run.DurationMS, run.Output, run.OutputStart, run.OutputBytes); err != nil {
|
run.StartedAt, run.EndedAt, run.DurationMS, run.Output, run.OutputStart, run.OutputBytes,
|
||||||
|
run.Terminal, run.Cols, run.Rows, run.Text); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if keep > 0 {
|
if keep > 0 {
|
||||||
@@ -104,13 +122,14 @@ duration_ms = CASE WHEN ? > started_at THEN ? - started_at ELSE 0 END WHERE stat
|
|||||||
return result.RowsAffected()
|
return result.RowsAffected()
|
||||||
}
|
}
|
||||||
|
|
||||||
const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes FROM chat_shell_runs`
|
const chatShellRunSelect = `SELECT id, session_id, command, directory, status, exit_code, signal, error, started_at, ended_at, duration_ms, output, output_start, output_bytes, terminal, cols, rows, output_text FROM chat_shell_runs`
|
||||||
|
|
||||||
func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) {
|
func scanChatShellRun(row chatQueueScanner) (protocol.ChatShellRunRecord, error) {
|
||||||
var run protocol.ChatShellRunRecord
|
var run protocol.ChatShellRunRecord
|
||||||
var exit sql.NullInt64
|
var exit sql.NullInt64
|
||||||
err := row.Scan(&run.ID, &run.SessionID, &run.Command, &run.Directory, &run.Status, &exit, &run.Signal, &run.Error,
|
err := row.Scan(&run.ID, &run.SessionID, &run.Command, &run.Directory, &run.Status, &exit, &run.Signal, &run.Error,
|
||||||
&run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes)
|
&run.StartedAt, &run.EndedAt, &run.DurationMS, &run.Output, &run.OutputStart, &run.OutputBytes,
|
||||||
|
&run.Terminal, &run.Cols, &run.Rows, &run.Text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return protocol.ChatShellRunRecord{}, err
|
return protocol.ChatShellRunRecord{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,14 +18,18 @@ const (
|
|||||||
// body is ChatQuery.
|
// body is ChatQuery.
|
||||||
ChatShellRuns = "chat_shell_runs"
|
ChatShellRuns = "chat_shell_runs"
|
||||||
// ChatShellRunOutput reads one run with its whole retained output (at most
|
// ChatShellRunOutput reads one run with its whole retained output (at most
|
||||||
// ChatShellRunRetained bytes). The body is ChatShellRunRequest.
|
// ChatShellRunRetained bytes) and its plain-text rendering. The body is
|
||||||
|
// ChatShellRunRequest; a nonzero After asks only for the output from that
|
||||||
|
// absolute byte on (a reconnecting client resuming its copy).
|
||||||
ChatShellRunOutput = "chat_shell_run_output"
|
ChatShellRunOutput = "chat_shell_run_output"
|
||||||
// ChatShellShare queues an ordinary prompt carrying the run's command, exit
|
// ChatShellShare queues an ordinary prompt carrying the run's command, exit
|
||||||
// status and bounded output tail. The body is ChatShellRunRequest; the
|
// status and bounded output tail. The body is ChatShellRunRequest; the
|
||||||
// reply is the chat_prompt acknowledgement.
|
// reply is the chat_prompt acknowledgement.
|
||||||
ChatShellShare = "chat_shell_share"
|
ChatShellShare = "chat_shell_share"
|
||||||
// ChatShellRunEvent is pushed to every client watching the session: a
|
// ChatShellRunEvent is pushed to every client watching the session: a
|
||||||
// lifecycle change (Run set) or an output chunk (Output set).
|
// lifecycle change (Run set) or an output chunk (Output set). Output
|
||||||
|
// chunks of one run are pushed in order; their byte offsets are the
|
||||||
|
// stream's sequence numbers.
|
||||||
ChatShellRunEvent = "chat_shell_run_event"
|
ChatShellRunEvent = "chat_shell_run_event"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,11 +64,18 @@ const (
|
|||||||
|
|
||||||
// ChatShellRunRequest names one session and either a new command or a run.
|
// ChatShellRunRequest names one session and either a new command or a run.
|
||||||
// OperationID is the optional retry identity of chat_shell_share's prompt.
|
// OperationID is the optional retry identity of chat_shell_share's prompt.
|
||||||
|
// Cols and Rows size a new run's terminal (chat_shell_run) or resize a running
|
||||||
|
// one (chat_shell_resize); Data is chat_shell_input's typed text; After is
|
||||||
|
// chat_shell_run_output's resume offset.
|
||||||
type ChatShellRunRequest struct {
|
type ChatShellRunRequest struct {
|
||||||
SessionID string `json:"session_id"`
|
SessionID string `json:"session_id"`
|
||||||
Command string `json:"command,omitempty"`
|
Command string `json:"command,omitempty"`
|
||||||
RunID string `json:"run_id,omitempty"`
|
RunID string `json:"run_id,omitempty"`
|
||||||
OperationID string `json:"operation_id,omitempty"`
|
OperationID string `json:"operation_id,omitempty"`
|
||||||
|
Cols int `json:"cols,omitempty"`
|
||||||
|
Rows int `json:"rows,omitempty"`
|
||||||
|
Data string `json:"data,omitempty"`
|
||||||
|
After int64 `json:"after,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatShellRunRecord is one operator command and what it produced. Output is
|
// ChatShellRunRecord is one operator command and what it produced. Output is
|
||||||
@@ -91,6 +102,16 @@ type ChatShellRunRecord struct {
|
|||||||
// Truncated is true when Output does not hold everything the command
|
// Truncated is true when Output does not hold everything the command
|
||||||
// wrote (OutputStart > 0).
|
// wrote (OutputStart > 0).
|
||||||
Truncated bool `json:"truncated,omitempty"`
|
Truncated bool `json:"truncated,omitempty"`
|
||||||
|
// Terminal is true when the command runs in a pseudo-terminal of Cols x
|
||||||
|
// Rows: Output is then the raw terminal stream, and input is accepted
|
||||||
|
// while it runs.
|
||||||
|
Terminal bool `json:"terminal,omitempty"`
|
||||||
|
Cols int `json:"cols,omitempty"`
|
||||||
|
Rows int `json:"rows,omitempty"`
|
||||||
|
// Text is the plain-text rendering of Output (escape sequences removed,
|
||||||
|
// carriage-return redraws applied): what Copy output copies and Send to
|
||||||
|
// agent shares. Only chat_shell_run_output carries it.
|
||||||
|
Text string `json:"text,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChatShellRunEventFrame is one live change to a run. A lifecycle frame sets
|
// ChatShellRunEventFrame is one live change to a run. A lifecycle frame sets
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package protocol
|
||||||
|
|
||||||
|
// Interactive operator shell commands. On macOS and Linux every chat_shell_run
|
||||||
|
// runs inside a pseudo-terminal (ChatShellRunRecord.Terminal): the command sees
|
||||||
|
// a TTY of the requested size with TERM=xterm-256color, its output is the raw
|
||||||
|
// terminal stream (escape sequences included) and any authenticated client
|
||||||
|
// watching the chat may type into it until it ends. Other platforms keep
|
||||||
|
// plain pipes and refuse input.
|
||||||
|
const (
|
||||||
|
// ChatShellInput writes Data to a running terminal command's input. The
|
||||||
|
// body is ChatShellRunRequest (session_id, run_id, data). Data is the
|
||||||
|
// UTF-8 text of what was typed or pasted, control characters included
|
||||||
|
// (Ctrl-C is "\u0003", Enter is "\r", arrow keys are their escape
|
||||||
|
// sequences); the bytes written are exactly its UTF-8 encoding. Input is
|
||||||
|
// never persisted or logged; the reply carries no body.
|
||||||
|
ChatShellInput = "chat_shell_input"
|
||||||
|
// ChatShellResize sets a running terminal command's size (cols, rows); the
|
||||||
|
// process receives SIGWINCH. The last resize from any client wins. The
|
||||||
|
// reply echoes the run.
|
||||||
|
ChatShellResize = "chat_shell_resize"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bounds of interactive terminals.
|
||||||
|
const (
|
||||||
|
// ChatShellInputLimit bounds the UTF-8 bytes of one chat_shell_input.
|
||||||
|
// Clients split a longer paste into several requests.
|
||||||
|
ChatShellInputLimit = 16 << 10
|
||||||
|
// ChatShellInputQueue bounds the input requests waiting for the command
|
||||||
|
// to read them; beyond it chat_shell_input is refused.
|
||||||
|
ChatShellInputQueue = 64
|
||||||
|
// Terminal sizes are clamped to these bounds; a run started without a
|
||||||
|
// size gets the defaults.
|
||||||
|
ChatShellTerminalMinCols = 20
|
||||||
|
ChatShellTerminalMaxCols = 500
|
||||||
|
ChatShellTerminalMinRows = 4
|
||||||
|
ChatShellTerminalMaxRows = 200
|
||||||
|
ChatShellTerminalDefaultCols = 100
|
||||||
|
ChatShellTerminalDefaultRows = 30
|
||||||
|
)
|
||||||
|
|
||||||
|
// ChatShellTerminalUnsupported is the refusal of chat_shell_input and
|
||||||
|
// chat_shell_resize on a server or run without a pseudo-terminal.
|
||||||
|
const ChatShellTerminalUnsupported = "interactive terminals are not supported on this server"
|
||||||
@@ -258,7 +258,8 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
|||||||
case protocol.ChatShellOutput:
|
case protocol.ChatShellOutput:
|
||||||
handleChatShellOutput(ctx, request, broker, registry, write)
|
handleChatShellOutput(ctx, request, broker, registry, write)
|
||||||
return true
|
return true
|
||||||
case protocol.ChatShellRun, protocol.ChatShellStop, protocol.ChatShellRuns, protocol.ChatShellRunOutput, protocol.ChatShellShare:
|
case protocol.ChatShellRun, protocol.ChatShellStop, protocol.ChatShellRuns, protocol.ChatShellRunOutput, protocol.ChatShellShare,
|
||||||
|
protocol.ChatShellInput, protocol.ChatShellResize:
|
||||||
handleChatShellRunRequest(ctx, request, broker, hub, registry, clientID, write)
|
handleChatShellRunRequest(ctx, request, broker, hub, registry, clientID, write)
|
||||||
return true
|
return true
|
||||||
case protocol.ChatAttachmentImage:
|
case protocol.ChatAttachmentImage:
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// chatShellPlainText renders a raw terminal stream as plain text, as Copy
|
||||||
|
// output and Send to agent show it: escape sequences (colours, cursor moves,
|
||||||
|
// titles, hyperlinks) are removed, a carriage return or backspace redraws the
|
||||||
|
// current line the way a terminal would, and the line erase is applied. It is
|
||||||
|
// not a terminal emulator: full-screen programs render as their final lines.
|
||||||
|
// The result is bounded by protocol.ChatShellRunRetained bytes (its tail).
|
||||||
|
func chatShellPlainText(raw string) string {
|
||||||
|
var r chatShellPlainRenderer
|
||||||
|
for _, c := range raw {
|
||||||
|
r.feed(c)
|
||||||
|
}
|
||||||
|
r.out.WriteString(strings.TrimRight(string(r.line), " "))
|
||||||
|
text := r.out.String()
|
||||||
|
if len(text) > protocol.ChatShellRunRetained {
|
||||||
|
cut := runeStartAfter([]byte(text), len(text)-protocol.ChatShellRunRetained)
|
||||||
|
text = text[cut:]
|
||||||
|
}
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
|
||||||
|
type chatShellPlainState int
|
||||||
|
|
||||||
|
const (
|
||||||
|
plainText chatShellPlainState = iota
|
||||||
|
plainEscape
|
||||||
|
plainEscapeIntermediate
|
||||||
|
plainCSI
|
||||||
|
// plainString is an OSC, DCS, SOS, PM or APC string, ended by BEL or ST.
|
||||||
|
plainString
|
||||||
|
plainStringEscape
|
||||||
|
)
|
||||||
|
|
||||||
|
type chatShellPlainRenderer struct {
|
||||||
|
out strings.Builder
|
||||||
|
line []rune
|
||||||
|
col int
|
||||||
|
state chatShellPlainState
|
||||||
|
params []rune
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chatShellPlainRenderer) feed(c rune) {
|
||||||
|
switch r.state {
|
||||||
|
case plainEscape:
|
||||||
|
r.state = plainText
|
||||||
|
switch {
|
||||||
|
case c == '[':
|
||||||
|
r.state, r.params = plainCSI, r.params[:0]
|
||||||
|
case c == ']' || c == 'P' || c == 'X' || c == '^' || c == '_':
|
||||||
|
r.state = plainString
|
||||||
|
case c >= 0x20 && c <= 0x2f:
|
||||||
|
r.state = plainEscapeIntermediate
|
||||||
|
}
|
||||||
|
case plainEscapeIntermediate:
|
||||||
|
if c < 0x20 || c > 0x2f {
|
||||||
|
r.state = plainText
|
||||||
|
}
|
||||||
|
case plainCSI:
|
||||||
|
switch {
|
||||||
|
case c >= 0x40 && c <= 0x7e:
|
||||||
|
r.state = plainText
|
||||||
|
r.csi(c)
|
||||||
|
case c >= 0x20 && c <= 0x3f:
|
||||||
|
if len(r.params) < 32 {
|
||||||
|
r.params = append(r.params, c)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
r.state = plainText
|
||||||
|
}
|
||||||
|
case plainString:
|
||||||
|
switch c {
|
||||||
|
case 0x07:
|
||||||
|
r.state = plainText
|
||||||
|
case 0x1b:
|
||||||
|
r.state = plainStringEscape
|
||||||
|
}
|
||||||
|
case plainStringEscape:
|
||||||
|
r.state = plainText
|
||||||
|
default:
|
||||||
|
r.text(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chatShellPlainRenderer) text(c rune) {
|
||||||
|
switch {
|
||||||
|
case c == 0x1b:
|
||||||
|
r.state = plainEscape
|
||||||
|
case c == 0x9b:
|
||||||
|
r.state, r.params = plainCSI, r.params[:0]
|
||||||
|
case c == '\n':
|
||||||
|
r.out.WriteString(strings.TrimRight(string(r.line), " "))
|
||||||
|
r.out.WriteByte('\n')
|
||||||
|
r.line, r.col = r.line[:0], 0
|
||||||
|
case c == '\r':
|
||||||
|
r.col = 0
|
||||||
|
case c == '\b':
|
||||||
|
r.col = max(r.col-1, 0)
|
||||||
|
case c == '\t':
|
||||||
|
r.put('\t')
|
||||||
|
case c < 0x20 || (c >= 0x7f && c < 0xa0):
|
||||||
|
// Other control characters draw nothing.
|
||||||
|
default:
|
||||||
|
r.put(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *chatShellPlainRenderer) put(c rune) {
|
||||||
|
for len(r.line) < r.col {
|
||||||
|
r.line = append(r.line, ' ')
|
||||||
|
}
|
||||||
|
if r.col < len(r.line) {
|
||||||
|
r.line[r.col] = c
|
||||||
|
} else {
|
||||||
|
r.line = append(r.line, c)
|
||||||
|
}
|
||||||
|
r.col++
|
||||||
|
}
|
||||||
|
|
||||||
|
// csi applies the few control sequences that change a line's text.
|
||||||
|
func (r *chatShellPlainRenderer) csi(final rune) {
|
||||||
|
params := string(r.params)
|
||||||
|
n, err := strconv.Atoi(params)
|
||||||
|
if err != nil || n < 1 {
|
||||||
|
n = 1
|
||||||
|
}
|
||||||
|
switch final {
|
||||||
|
case 'K':
|
||||||
|
switch params {
|
||||||
|
case "", "0":
|
||||||
|
r.line = r.line[:min(r.col, len(r.line))]
|
||||||
|
case "2":
|
||||||
|
r.line = r.line[:0]
|
||||||
|
}
|
||||||
|
case 'G':
|
||||||
|
r.col = min(n-1, protocol.ChatShellTerminalMaxCols)
|
||||||
|
case 'C':
|
||||||
|
r.col = min(r.col+n, protocol.ChatShellTerminalMaxCols)
|
||||||
|
case 'D':
|
||||||
|
r.col = max(r.col-n, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChatShellPlainTextRendersTerminalStream(t *testing.T) {
|
||||||
|
for _, tc := range []struct{ raw, want string }{
|
||||||
|
{"plain\nlines\n", "plain\nlines\n"},
|
||||||
|
{"\x1b[1;32mok\x1b[0m done\r\n", "ok done\n"},
|
||||||
|
{"10%\r50%\r100%\r\n", "100%\n"},
|
||||||
|
{"long line\rab\x1b[K\r\n", "ab\n"},
|
||||||
|
{"abc\b\bX\n", "aXc\n"},
|
||||||
|
{"\x1b]0;window title\x07shown\n", "shown\n"},
|
||||||
|
{"\x1b]8;;https://x.test\x1b\\link\x1b]8;;\x1b\\\n", "link\n"},
|
||||||
|
{"\x1b(Bcharset\x1b=\n", "charset\n"},
|
||||||
|
{"col\x1b[10Gx\n", "col x\n"},
|
||||||
|
{"bell\x07 and nul\x00\n", "bell and nul\n"},
|
||||||
|
{"\x1b[2Kgone\r\x1b[2Kkept", "kept"},
|
||||||
|
{"unterminated \x1b[31", "unterminated"},
|
||||||
|
} {
|
||||||
|
if got := chatShellPlainText(tc.raw); got != tc.want {
|
||||||
|
t.Errorf("plain(%q) = %q, want %q", tc.raw, got, tc.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellPlainTextIsBounded(t *testing.T) {
|
||||||
|
raw := strings.Repeat("\x1b[500Gx\n", protocol.ChatShellRunRetained/64)
|
||||||
|
if got := chatShellPlainText(raw); len(got) > protocol.ChatShellRunRetained || !strings.HasSuffix(got, "x\n") {
|
||||||
|
t.Fatalf("rendered %d bytes", len(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
// handleChatShellRunRequest answers the operator shell-command requests. Every
|
// handleChatShellRunRequest answers the operator shell-command requests. Every
|
||||||
// one names an operator chat session the registry records: the command runs
|
// one names an operator chat session the registry records: the command runs
|
||||||
// only in that chat's own directory on this server's machine, and a run is
|
// only in that chat's own directory on this server's machine, and a run is
|
||||||
// only readable or stoppable through the session that started it.
|
// only readable, stoppable or typed into through the session that started it.
|
||||||
|
// Any authenticated client of that session may type into a running command,
|
||||||
|
// not only the one that started it.
|
||||||
func handleChatShellRunRequest(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
|
func handleChatShellRunRequest(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
|
||||||
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
|
fail := func(err error) { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) }
|
||||||
runner := hub.shellRunner()
|
runner := hub.shellRunner()
|
||||||
@@ -49,7 +51,7 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br
|
|||||||
}
|
}
|
||||||
switch request.Type {
|
switch request.Type {
|
||||||
case protocol.ChatShellRun:
|
case protocol.ChatShellRun:
|
||||||
record, err := runner.Start(ctx, sessionID, session.Directory, body.Command)
|
record, err := runner.Start(ctx, sessionID, session.Directory, body.Command, body.Cols, body.Rows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err)
|
fail(err)
|
||||||
return
|
return
|
||||||
@@ -69,12 +71,26 @@ func handleChatShellRunRequest(ctx context.Context, request protocol.Message, br
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs})
|
_ = write(protocol.Message{Type: protocol.ChatShellRuns, ID: request.ID, ChatShellRuns: runs})
|
||||||
|
case protocol.ChatShellInput:
|
||||||
|
if err := runner.Input(sessionID, body.RunID, body.Data); err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = write(protocol.Message{Type: protocol.ChatShellInput, ID: request.ID})
|
||||||
|
case protocol.ChatShellResize:
|
||||||
|
record, err := runner.Resize(sessionID, body.RunID, body.Cols, body.Rows)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = write(protocol.Message{Type: protocol.ChatShellResize, ID: request.ID, ChatShellRun: &record})
|
||||||
case protocol.ChatShellRunOutput:
|
case protocol.ChatShellRunOutput:
|
||||||
record, err := runner.Read(ctx, sessionID, body.RunID)
|
record, err := runner.Read(ctx, sessionID, body.RunID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fail(err)
|
fail(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
record = chatShellRecordAfter(record, body.After)
|
||||||
_ = write(protocol.Message{Type: protocol.ChatShellRunOutput, ID: request.ID, ChatShellRun: &record})
|
_ = write(protocol.Message{Type: protocol.ChatShellRunOutput, ID: request.ID, ChatShellRun: &record})
|
||||||
case protocol.ChatShellShare:
|
case protocol.ChatShellShare:
|
||||||
record, err := runner.Read(ctx, sessionID, body.RunID)
|
record, err := runner.Read(ctx, sessionID, body.RunID)
|
||||||
@@ -104,9 +120,10 @@ func (r *chatShellRunner) List(ctx context.Context, sessionID string) ([]protoco
|
|||||||
for i, run := range runs {
|
for i, run := range runs {
|
||||||
if p := r.activeProcess(sessionID, run.ID); p != nil {
|
if p := r.activeProcess(sessionID, run.ID); p != nil {
|
||||||
runs[i] = p.snapshot(protocol.ChatShellRunListOutput)
|
runs[i] = p.snapshot(protocol.ChatShellRunListOutput)
|
||||||
continue
|
} else {
|
||||||
|
runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput)
|
||||||
}
|
}
|
||||||
runs[i] = chatShellRecordTail(run, protocol.ChatShellRunListOutput)
|
runs[i].Text = ""
|
||||||
}
|
}
|
||||||
return runs, nil
|
return runs, nil
|
||||||
}
|
}
|
||||||
@@ -123,9 +140,27 @@ func (r *chatShellRunner) Read(ctx context.Context, sessionID, runID string) (pr
|
|||||||
if !found {
|
if !found {
|
||||||
return protocol.ChatShellRunRecord{}, errors.New("this chat has no such command")
|
return protocol.ChatShellRunRecord{}, errors.New("this chat has no such command")
|
||||||
}
|
}
|
||||||
|
if record.Text == "" && record.Output != "" {
|
||||||
|
// A run recorded before plain-text renderings were stored.
|
||||||
|
record.Text = chatShellPlainText(record.Output)
|
||||||
|
}
|
||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// chatShellRecordAfter keeps only the output from absolute byte after on; an
|
||||||
|
// offset before the retained tail keeps all of it (OutputStart says where it
|
||||||
|
// starts, so the reader sees the gap).
|
||||||
|
func chatShellRecordAfter(record protocol.ChatShellRunRecord, after int64) protocol.ChatShellRunRecord {
|
||||||
|
if after <= record.OutputStart {
|
||||||
|
return record
|
||||||
|
}
|
||||||
|
skip := min(after-record.OutputStart, int64(len(record.Output)))
|
||||||
|
cut := runeStartAfter([]byte(record.Output), int(skip))
|
||||||
|
record.Output = record.Output[cut:]
|
||||||
|
record.OutputStart += int64(cut)
|
||||||
|
return record
|
||||||
|
}
|
||||||
|
|
||||||
// chatShellRecordTail trims a stored record's output to its last max bytes.
|
// chatShellRecordTail trims a stored record's output to its last max bytes.
|
||||||
func chatShellRecordTail(record protocol.ChatShellRunRecord, max int) protocol.ChatShellRunRecord {
|
func chatShellRecordTail(record protocol.ChatShellRunRecord, max int) protocol.ChatShellRunRecord {
|
||||||
if len(record.Output) <= max {
|
if len(record.Output) <= max {
|
||||||
|
|||||||
@@ -99,13 +99,13 @@ func TestChatShellRunStreamsToEveryWatcherAndPersists(t *testing.T) {
|
|||||||
if final.Status != protocol.ChatShellRunExited || final.ExitCode == nil || *final.ExitCode != 3 || final.EndedAt < final.StartedAt {
|
if final.Status != protocol.ChatShellRunExited || final.ExitCode == nil || *final.ExitCode != 3 || final.EndedAt < final.StartedAt {
|
||||||
t.Fatalf("final record = %+v", final)
|
t.Fatalf("final record = %+v", final)
|
||||||
}
|
}
|
||||||
if output != dir+"\nout\nerr\n" {
|
if output != dir+"\r\nout\r\nerr\r\n" || !final.Terminal || final.Cols != protocol.ChatShellTerminalDefaultCols || final.Rows != protocol.ChatShellTerminalDefaultRows {
|
||||||
t.Fatalf("streamed output = %q", output)
|
t.Fatalf("streamed output = %q, final %+v", output, final)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatShellRuns, ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
|
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatShellRuns, ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
|
||||||
list := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "list" })
|
list := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "list" })
|
||||||
if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].Output != dir+"\nout\nerr\n" || *list.ChatShellRuns[0].ExitCode != 3 {
|
if len(list.ChatShellRuns) != 1 || list.ChatShellRuns[0].Output != dir+"\r\nout\r\nerr\r\n" || list.ChatShellRuns[0].Text != "" || *list.ChatShellRuns[0].ExitCode != 3 {
|
||||||
t.Fatalf("persisted runs = %+v", list)
|
t.Fatalf("persisted runs = %+v", list)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// chatShellRunner owns the operator's own commands: each runs `$SHELL -lc` in
|
// chatShellRunner owns the operator's own commands: each runs `$SHELL -lc` in
|
||||||
// its chat's directory, in its own process group, with stdout and stderr
|
// its chat's directory, in its own process group, in a pseudo-terminal where
|
||||||
// merged into one bounded tail that is streamed to the session's watchers and
|
// the platform has one (else with stdout and stderr on one pipe), its output
|
||||||
|
// kept as one bounded tail that is streamed to the session's watchers and
|
||||||
// persisted as Pipelines' record of the run.
|
// persisted as Pipelines' record of the run.
|
||||||
type chatShellRunner struct {
|
type chatShellRunner struct {
|
||||||
base context.Context
|
base context.Context
|
||||||
@@ -60,6 +61,13 @@ type chatShellProcess struct {
|
|||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
stopped bool
|
stopped bool
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
|
// terminal is the pseudo-terminal's controller side (nil in pipe mode);
|
||||||
|
// input feeds it in order (chat_shell_terminal.go).
|
||||||
|
terminal *os.File
|
||||||
|
input chan []byte
|
||||||
|
// closed is set once the reader is closed; a read still blocked on a
|
||||||
|
// terminal a descendant holds open records nothing after it.
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newChatShellRunner(base context.Context, store chatShellRunStore, publish func(string, protocol.ChatShellRunEventFrame)) *chatShellRunner {
|
func newChatShellRunner(base context.Context, store chatShellRunStore, publish func(string, protocol.ChatShellRunEventFrame)) *chatShellRunner {
|
||||||
@@ -117,7 +125,8 @@ func newChatShellRunID() (string, error) {
|
|||||||
|
|
||||||
// Start launches command for sessionID in directory and returns its running
|
// Start launches command for sessionID in directory and returns its running
|
||||||
// record. Validation and start failures are returned without a record.
|
// record. Validation and start failures are returned without a record.
|
||||||
func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, command string) (protocol.ChatShellRunRecord, error) {
|
// cols and rows size its terminal (zero picks the defaults).
|
||||||
|
func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, command string, cols, rows int) (protocol.ChatShellRunRecord, error) {
|
||||||
if r.store == nil {
|
if r.store == nil {
|
||||||
return protocol.ChatShellRunRecord{}, errors.New("shell commands need the server's store, which is not configured")
|
return protocol.ChatShellRunRecord{}, errors.New("shell commands need the server's store, which is not configured")
|
||||||
}
|
}
|
||||||
@@ -149,23 +158,18 @@ func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, comma
|
|||||||
if running >= protocol.ChatShellRunConcurrent {
|
if running >= protocol.ChatShellRunConcurrent {
|
||||||
return protocol.ChatShellRunRecord{}, fmt.Errorf("this chat already runs %d commands; stop one or wait for it to finish", running)
|
return protocol.ChatShellRunRecord{}, fmt.Errorf("this chat already runs %d commands; stop one or wait for it to finish", running)
|
||||||
}
|
}
|
||||||
reader, writer, err := os.Pipe()
|
|
||||||
if err != nil {
|
|
||||||
return protocol.ChatShellRunRecord{}, err
|
|
||||||
}
|
|
||||||
cmd := exec.Command(r.shell(), "-lc", command)
|
cmd := exec.Command(r.shell(), "-lc", command)
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
cmd.Env = os.Environ()
|
cols, rows = chatShellTerminalSize(cols, rows)
|
||||||
cmd.Stdout, cmd.Stderr = writer, writer
|
reader, terminal, err := startChatShellCommand(cmd, cols, rows)
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
if err != nil {
|
||||||
if err := cmd.Start(); err != nil {
|
|
||||||
reader.Close()
|
|
||||||
writer.Close()
|
|
||||||
return protocol.ChatShellRunRecord{}, fmt.Errorf("could not start the command: %v", err)
|
return protocol.ChatShellRunRecord{}, fmt.Errorf("could not start the command: %v", err)
|
||||||
}
|
}
|
||||||
writer.Close()
|
|
||||||
record := protocol.ChatShellRunRecord{ID: id, SessionID: sessionID, Command: command, Directory: dir, Status: protocol.ChatShellRunRunning, StartedAt: r.now().UnixMilli()}
|
record := protocol.ChatShellRunRecord{ID: id, SessionID: sessionID, Command: command, Directory: dir, Status: protocol.ChatShellRunRunning, StartedAt: r.now().UnixMilli()}
|
||||||
p := &chatShellProcess{record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, done: make(chan struct{})}
|
if terminal != nil {
|
||||||
|
record.Terminal, record.Cols, record.Rows = true, cols, rows
|
||||||
|
}
|
||||||
|
p := &chatShellProcess{record: record, out: newChatShellOutput(protocol.ChatShellRunRetained), cmd: cmd, terminal: terminal, done: make(chan struct{})}
|
||||||
if err := r.store.SaveChatShellRun(ctx, record, protocol.ChatShellRunHistory); err != nil {
|
if err := r.store.SaveChatShellRun(ctx, record, protocol.ChatShellRunHistory); err != nil {
|
||||||
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
reader.Close()
|
reader.Close()
|
||||||
@@ -175,6 +179,10 @@ func (r *chatShellRunner) Start(ctx context.Context, sessionID, directory, comma
|
|||||||
r.active[id] = p
|
r.active[id] = p
|
||||||
r.wg.Add(1)
|
r.wg.Add(1)
|
||||||
go r.supervise(p, reader)
|
go r.supervise(p, reader)
|
||||||
|
if terminal != nil {
|
||||||
|
p.input = make(chan []byte, protocol.ChatShellInputQueue)
|
||||||
|
go p.pumpInput()
|
||||||
|
}
|
||||||
r.publishRun(record)
|
r.publishRun(record)
|
||||||
return record, nil
|
return record, nil
|
||||||
}
|
}
|
||||||
@@ -216,14 +224,16 @@ func (r *chatShellRunner) activeProcess(sessionID, runID string) *chatShellProce
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// snapshot is the process's current record carrying at most max output bytes.
|
// snapshot is the process's current record carrying at most max output bytes
|
||||||
|
// and their plain-text rendering.
|
||||||
func (p *chatShellProcess) snapshot(max int) protocol.ChatShellRunRecord {
|
func (p *chatShellProcess) snapshot(max int) protocol.ChatShellRunRecord {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
defer p.mu.Unlock()
|
|
||||||
record := p.record
|
record := p.record
|
||||||
record.Output, record.OutputStart = p.out.tail(max)
|
record.Output, record.OutputStart = p.out.tail(max)
|
||||||
record.OutputBytes = p.out.total
|
record.OutputBytes = p.out.total
|
||||||
record.Truncated = record.OutputStart > 0
|
record.Truncated = record.OutputStart > 0
|
||||||
|
p.mu.Unlock()
|
||||||
|
record.Text = chatShellPlainText(record.Output)
|
||||||
return record
|
return record
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,7 +241,7 @@ func (r *chatShellRunner) publishRun(record protocol.ChatShellRunRecord) {
|
|||||||
if r.publish == nil {
|
if r.publish == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
record.Output = ""
|
record.Output, record.Text = "", ""
|
||||||
r.publish(record.SessionID, protocol.ChatShellRunEventFrame{SessionID: record.SessionID, RunID: record.ID, Run: &record})
|
r.publish(record.SessionID, protocol.ChatShellRunEventFrame{SessionID: record.SessionID, RunID: record.ID, Run: &record})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,11 +44,20 @@ func testShellRunner(t *testing.T, ctx context.Context) (*chatShellRunner, *shel
|
|||||||
|
|
||||||
func TestChatShellStopKillsAfterGrace(t *testing.T) {
|
func TestChatShellStopKillsAfterGrace(t *testing.T) {
|
||||||
runner, frames := testShellRunner(t, context.Background())
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `trap "" INT; echo ready; while :; do sleep 0.05; done`)
|
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `trap "" INT; echo ready; while :; do sleep 0.05; done`, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
time.Sleep(150 * time.Millisecond)
|
// The trap must be in place before the stop: wait for the command's
|
||||||
|
// output rather than a fixed delay (a login shell can be slow to start).
|
||||||
|
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(10 * time.Millisecond) {
|
||||||
|
if record, err := runner.Read(context.Background(), "ses_1", run.ID); err == nil && strings.Contains(record.Output, "ready") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if time.Now().After(deadline) {
|
||||||
|
t.Fatal("the command never printed ready")
|
||||||
|
}
|
||||||
|
}
|
||||||
if _, err := runner.Stop("ses_2", run.ID); err == nil {
|
if _, err := runner.Stop("ses_2", run.ID); err == nil {
|
||||||
t.Fatal("another session stopped this run")
|
t.Fatal("another session stopped this run")
|
||||||
}
|
}
|
||||||
@@ -65,7 +74,7 @@ func TestChatShellStopKillsAfterGrace(t *testing.T) {
|
|||||||
func TestChatShellRetainsBoundedTail(t *testing.T) {
|
func TestChatShellRetainsBoundedTail(t *testing.T) {
|
||||||
runner, frames := testShellRunner(t, context.Background())
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
total := protocol.ChatShellRunRetained + 200_000
|
total := protocol.ChatShellRunRetained + 200_000
|
||||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `head -c 1248576 /dev/zero | tr '\0' a; printf END`)
|
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), `head -c 1248576 /dev/zero | tr '\0' a; printf END`, 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -93,7 +102,7 @@ func TestChatShellRetainsBoundedTail(t *testing.T) {
|
|||||||
func TestChatShellShutdownAndRestartInterrupt(t *testing.T) {
|
func TestChatShellShutdownAndRestartInterrupt(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
runner, frames := testShellRunner(t, ctx)
|
runner, frames := testShellRunner(t, ctx)
|
||||||
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "sleep 30")
|
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "sleep 30", 0, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -102,7 +111,7 @@ func TestChatShellShutdownAndRestartInterrupt(t *testing.T) {
|
|||||||
if final := frames.final(run.ID); final == nil || final.Status != protocol.ChatShellRunInterrupted {
|
if final := frames.final(run.ID); final == nil || final.Status != protocol.ChatShellRunInterrupted {
|
||||||
t.Fatalf("shutdown final = %+v", final)
|
t.Fatalf("shutdown final = %+v", final)
|
||||||
}
|
}
|
||||||
if _, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "true"); err == nil {
|
if _, err := runner.Start(context.Background(), "ses_1", t.TempDir(), "true", 0, 0); err == nil {
|
||||||
t.Fatal("a stopping server started a command")
|
t.Fatal("a stopping server started a command")
|
||||||
}
|
}
|
||||||
// A record a crashed process left running is interrupted on start.
|
// A record a crashed process left running is interrupted on start.
|
||||||
|
|||||||
@@ -9,27 +9,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// chatShellSharePrompt is the ordinary prompt chat_shell_share queues: the
|
// chatShellSharePrompt is the ordinary prompt chat_shell_share queues: the
|
||||||
// command, where it ran, how it ended and a bounded tail of its output. It is
|
// command, where it ran, how it ended and a bounded tail of the plain-text
|
||||||
// the only path by which an operator command reaches the agent.
|
// rendering of its output (no terminal escape sequences). It is the only path
|
||||||
|
// by which an operator command reaches the agent.
|
||||||
func chatShellSharePrompt(record protocol.ChatShellRunRecord) string {
|
func chatShellSharePrompt(record protocol.ChatShellRunRecord) string {
|
||||||
output := chatShellRecordTail(record, protocol.ChatShellRunShareOutput)
|
text := record.Text
|
||||||
|
if text == "" {
|
||||||
|
text = chatShellPlainText(record.Output)
|
||||||
|
}
|
||||||
|
shown := text
|
||||||
|
if len(shown) > protocol.ChatShellRunShareOutput {
|
||||||
|
shown = shown[runeStartAfter([]byte(shown), len(shown)-protocol.ChatShellRunShareOutput):]
|
||||||
|
}
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
fmt.Fprintf(&b, "I ran this shell command in %s:\n\n", record.Directory)
|
fmt.Fprintf(&b, "I ran this shell command in %s:\n\n", record.Directory)
|
||||||
fence := chatShellFence(record.Command)
|
fence := chatShellFence(record.Command)
|
||||||
fmt.Fprintf(&b, "%ssh\n$ %s\n%s\n\n", fence, record.Command, fence)
|
fmt.Fprintf(&b, "%ssh\n$ %s\n%s\n\n", fence, record.Command, fence)
|
||||||
fmt.Fprintf(&b, "%s.\n\n", chatShellOutcome(record))
|
fmt.Fprintf(&b, "%s.\n\n", chatShellOutcome(record))
|
||||||
if output.Output == "" {
|
if strings.TrimSpace(shown) == "" {
|
||||||
b.WriteString("It printed no output.")
|
b.WriteString("It printed no output.")
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
if output.OutputStart > 0 {
|
if len(shown) < len(text) || record.OutputStart > 0 {
|
||||||
fmt.Fprintf(&b, "Output (the last %s of %s):\n\n", chatShellBytes(int64(len(output.Output))), chatShellBytes(output.OutputBytes))
|
fmt.Fprintf(&b, "Output (the last %s of the %s it wrote, as plain text):\n\n", chatShellBytes(int64(len(shown))), chatShellBytes(record.OutputBytes))
|
||||||
} else {
|
} else {
|
||||||
b.WriteString("Output:\n\n")
|
b.WriteString("Output:\n\n")
|
||||||
}
|
}
|
||||||
fence = chatShellFence(output.Output)
|
fence = chatShellFence(shown)
|
||||||
text := strings.TrimRight(output.Output, "\n")
|
fmt.Fprintf(&b, "%stext\n%s\n%s", fence, strings.TrimRight(shown, "\n"), fence)
|
||||||
fmt.Fprintf(&b, "%stext\n%s\n%s", fence, text, fence)
|
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
// supervise reads one command's merged output, streams it in batches, saves
|
// supervise reads one command's merged output (or terminal stream), streams it in batches, saves
|
||||||
// it periodically and records how the command ended. It owns the reader.
|
// it periodically and records how the command ended. It owns the reader.
|
||||||
func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
||||||
defer r.wg.Done()
|
defer r.wg.Done()
|
||||||
@@ -24,7 +24,9 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
|||||||
n, err := reader.Read(buffer)
|
n, err := reader.Read(buffer)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
p.out.write(buffer[:n])
|
if !p.closed {
|
||||||
|
p.out.write(buffer[:n])
|
||||||
|
}
|
||||||
p.mu.Unlock()
|
p.mu.Unlock()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,6 +41,7 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
|||||||
defer flush.Stop()
|
defer flush.Stop()
|
||||||
lastSaved := r.now()
|
lastSaved := r.now()
|
||||||
var waitErr error
|
var waitErr error
|
||||||
|
var drainDone <-chan time.Time
|
||||||
exited, drained, shutdown := false, false, false
|
exited, drained, shutdown := false, false, false
|
||||||
base := r.base.Done()
|
base := r.base.Done()
|
||||||
for !exited || !drained {
|
for !exited || !drained {
|
||||||
@@ -51,9 +54,14 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
|||||||
}
|
}
|
||||||
case waitErr = <-waitDone:
|
case waitErr = <-waitDone:
|
||||||
exited = true
|
exited = true
|
||||||
// Descendants may hold the pipe open; read what they write
|
// Descendants may hold the pipe or terminal open; read what
|
||||||
// briefly, then stop reading.
|
// they write briefly, then stop reading. A terminal may not
|
||||||
|
// support deadlines, so the timer also ends the wait.
|
||||||
_ = reader.SetReadDeadline(time.Now().Add(chatShellDrain))
|
_ = reader.SetReadDeadline(time.Now().Add(chatShellDrain))
|
||||||
|
drainDone = time.After(chatShellDrain + chatShellDrain/2)
|
||||||
|
case <-drainDone:
|
||||||
|
drained = true
|
||||||
|
readDone = nil
|
||||||
case <-readDone:
|
case <-readDone:
|
||||||
drained = true
|
drained = true
|
||||||
readDone = nil
|
readDone = nil
|
||||||
@@ -65,7 +73,10 @@ func (r *chatShellRunner) supervise(p *chatShellProcess, reader *os.File) {
|
|||||||
}
|
}
|
||||||
// Nothing in the command's group outlives it.
|
// Nothing in the command's group outlives it.
|
||||||
_ = syscall.Kill(-pid, syscall.SIGKILL)
|
_ = syscall.Kill(-pid, syscall.SIGKILL)
|
||||||
|
p.mu.Lock()
|
||||||
reader.Close()
|
reader.Close()
|
||||||
|
p.closed = true
|
||||||
|
p.mu.Unlock()
|
||||||
r.emitOutput(p, true)
|
r.emitOutput(p, true)
|
||||||
p.mu.Lock()
|
p.mu.Lock()
|
||||||
finishChatShellRecord(&p.record, waitErr, p.stopped, shutdown || r.base.Err() != nil, r.now())
|
finishChatShellRecord(&p.record, waitErr, p.stopped, shutdown || r.base.Err() != nil, r.now())
|
||||||
|
|||||||
@@ -0,0 +1,138 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// startChatShellCommand starts cmd with the server's environment, in a
|
||||||
|
// pseudo-terminal of cols x rows where the platform has one (TERM is then
|
||||||
|
// xterm-256color) and otherwise in its own process group with stdout and
|
||||||
|
// stderr on one pipe. It returns the output reader and, in a terminal, the
|
||||||
|
// terminal itself (the same file).
|
||||||
|
func startChatShellCommand(cmd *exec.Cmd, cols, rows int) (reader, terminal *os.File, err error) {
|
||||||
|
if chatShellTerminals {
|
||||||
|
cmd.Env = chatShellTerminalEnv(os.Environ())
|
||||||
|
terminal, err = startChatShellTerminal(cmd, cols, rows)
|
||||||
|
return terminal, terminal, err
|
||||||
|
}
|
||||||
|
reader, writer, err := os.Pipe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
cmd.Env = os.Environ()
|
||||||
|
cmd.Stdout, cmd.Stderr = writer, writer
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
err = cmd.Start()
|
||||||
|
writer.Close()
|
||||||
|
if err != nil {
|
||||||
|
reader.Close()
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return reader, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// chatShellTerminalEnv is env with TERM naming the terminal the clients draw.
|
||||||
|
func chatShellTerminalEnv(env []string) []string {
|
||||||
|
out := make([]string, 0, len(env)+1)
|
||||||
|
for _, entry := range env {
|
||||||
|
if !strings.HasPrefix(entry, "TERM=") {
|
||||||
|
out = append(out, entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(out, "TERM=xterm-256color")
|
||||||
|
}
|
||||||
|
|
||||||
|
// chatShellTerminalSize clamps a requested size; zero picks the default.
|
||||||
|
func chatShellTerminalSize(cols, rows int) (int, int) {
|
||||||
|
if cols <= 0 {
|
||||||
|
cols = protocol.ChatShellTerminalDefaultCols
|
||||||
|
}
|
||||||
|
if rows <= 0 {
|
||||||
|
rows = protocol.ChatShellTerminalDefaultRows
|
||||||
|
}
|
||||||
|
return min(max(cols, protocol.ChatShellTerminalMinCols), protocol.ChatShellTerminalMaxCols),
|
||||||
|
min(max(rows, protocol.ChatShellTerminalMinRows), protocol.ChatShellTerminalMaxRows)
|
||||||
|
}
|
||||||
|
|
||||||
|
// terminalProcess is runID's running terminal command of sessionID.
|
||||||
|
func (r *chatShellRunner) terminalProcess(sessionID, runID string) (*chatShellProcess, error) {
|
||||||
|
p := r.activeProcess(sessionID, runID)
|
||||||
|
if p == nil {
|
||||||
|
return nil, errors.New("this command is not running")
|
||||||
|
}
|
||||||
|
if p.terminal == nil {
|
||||||
|
return nil, errors.New(protocol.ChatShellTerminalUnsupported)
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input queues data for a running terminal command, in arrival order. It is
|
||||||
|
// written as typed and kept nowhere else: what the process echoes is output.
|
||||||
|
func (r *chatShellRunner) Input(sessionID, runID, data string) error {
|
||||||
|
if data == "" {
|
||||||
|
return errors.New("input is required")
|
||||||
|
}
|
||||||
|
if len(data) > protocol.ChatShellInputLimit {
|
||||||
|
return errors.New("the input exceeds 16 KiB; send it in parts")
|
||||||
|
}
|
||||||
|
if !utf8.ValidString(data) {
|
||||||
|
return errors.New("input must be UTF-8 text")
|
||||||
|
}
|
||||||
|
p, err := r.terminalProcess(sessionID, runID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-p.done:
|
||||||
|
return errors.New("this command is not running")
|
||||||
|
case p.input <- []byte(data):
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return errors.New("the command is not reading its input; wait for it or stop it")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resize sets a running terminal command's size and returns its record.
|
||||||
|
func (r *chatShellRunner) Resize(sessionID, runID string, cols, rows int) (protocol.ChatShellRunRecord, error) {
|
||||||
|
if cols <= 0 || rows <= 0 {
|
||||||
|
return protocol.ChatShellRunRecord{}, errors.New("positive cols and rows are required")
|
||||||
|
}
|
||||||
|
p, err := r.terminalProcess(sessionID, runID)
|
||||||
|
if err != nil {
|
||||||
|
return protocol.ChatShellRunRecord{}, err
|
||||||
|
}
|
||||||
|
cols, rows = chatShellTerminalSize(cols, rows)
|
||||||
|
p.mu.Lock()
|
||||||
|
defer p.mu.Unlock()
|
||||||
|
if p.closed {
|
||||||
|
return protocol.ChatShellRunRecord{}, errors.New("this command is not running")
|
||||||
|
}
|
||||||
|
if err := resizeChatShellTerminal(p.terminal, cols, rows); err != nil {
|
||||||
|
return protocol.ChatShellRunRecord{}, err
|
||||||
|
}
|
||||||
|
p.record.Cols, p.record.Rows = cols, rows
|
||||||
|
record := p.record
|
||||||
|
return record, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// pumpInput writes queued input to the terminal until the command ends. A
|
||||||
|
// write blocks while the command does not read, which is what fills the queue.
|
||||||
|
func (p *chatShellProcess) pumpInput() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-p.done:
|
||||||
|
return
|
||||||
|
case data := <-p.input:
|
||||||
|
if _, err := p.terminal.Write(data); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
//go:build !(darwin || linux)
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// chatShellTerminals is false here: operator commands keep plain pipes, and
|
||||||
|
// input and resize are refused with protocol.ChatShellTerminalUnsupported.
|
||||||
|
const chatShellTerminals = false
|
||||||
|
|
||||||
|
func startChatShellTerminal(*exec.Cmd, int, int) (*os.File, error) {
|
||||||
|
return nil, errors.New(protocol.ChatShellTerminalUnsupported)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resizeChatShellTerminal(*os.File, int, int) error {
|
||||||
|
return errors.New(protocol.ChatShellTerminalUnsupported)
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
//go:build darwin || linux
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// startTerminal starts command in a real pseudo-terminal of cols x rows.
|
||||||
|
func startTerminal(t *testing.T, runner *chatShellRunner, command string, cols, rows int) protocol.ChatShellRunRecord {
|
||||||
|
t.Helper()
|
||||||
|
run, err := runner.Start(context.Background(), "ses_1", t.TempDir(), command, cols, rows)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !run.Terminal {
|
||||||
|
t.Fatalf("run is not in a terminal: %+v", run)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_, _ = runner.Stop("ses_1", run.ID)
|
||||||
|
runner.Wait()
|
||||||
|
})
|
||||||
|
return run
|
||||||
|
}
|
||||||
|
|
||||||
|
// waitOutput waits until the run's retained output contains want.
|
||||||
|
func waitOutput(t *testing.T, runner *chatShellRunner, runID, want string) {
|
||||||
|
t.Helper()
|
||||||
|
deadline := time.Now().Add(10 * time.Second)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
record, err := runner.Read(context.Background(), "ses_1", runID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if strings.Contains(record.Output, want) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
}
|
||||||
|
record, _ := runner.Read(context.Background(), "ses_1", runID)
|
||||||
|
t.Fatalf("output never contained %q: %q", want, record.Output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func finishedTerminal(t *testing.T, runner *chatShellRunner, frames *shellFrames, runID string) protocol.ChatShellRunRecord {
|
||||||
|
t.Helper()
|
||||||
|
runner.Wait()
|
||||||
|
if final := frames.final(runID); final == nil {
|
||||||
|
t.Fatal("no final lifecycle frame")
|
||||||
|
}
|
||||||
|
stored, err := runner.Read(context.Background(), "ses_1", runID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return stored
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalReadsTypedLine(t *testing.T) {
|
||||||
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
|
run := startTerminal(t, runner, `[ -t 0 ] && echo tty:$TERM; printf 'name? '; read -r x; echo got:$x`, 0, 0)
|
||||||
|
waitOutput(t, runner, run.ID, "name? ")
|
||||||
|
if err := runner.Input("ses_2", run.ID, "intruder\r"); err == nil {
|
||||||
|
t.Fatal("another session typed into the run")
|
||||||
|
}
|
||||||
|
if err := runner.Input("ses_1", run.ID, "hello world\r"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||||
|
if !strings.Contains(stored.Output, "tty:xterm-256color") || !strings.Contains(stored.Output, "got:hello world") || stored.ExitCode == nil || *stored.ExitCode != 0 {
|
||||||
|
t.Fatalf("stored = %+v", stored)
|
||||||
|
}
|
||||||
|
if err := runner.Input("ses_1", run.ID, "late\r"); err == nil {
|
||||||
|
t.Fatal("input reached a finished run")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalHiddenInputIsNotRecorded(t *testing.T) {
|
||||||
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
|
run := startTerminal(t, runner, `stty -echo; printf 'Password: '; read -r pw; stty echo; echo; echo length:${#pw}`, 0, 0)
|
||||||
|
waitOutput(t, runner, run.ID, "Password: ")
|
||||||
|
if err := runner.Input("ses_1", run.ID, "s3cret-token\r"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||||
|
if !strings.Contains(stored.Text, "length:12") {
|
||||||
|
t.Fatalf("text = %q", stored.Text)
|
||||||
|
}
|
||||||
|
if strings.Contains(stored.Output, "s3cret") || strings.Contains(stored.Text, "s3cret") {
|
||||||
|
t.Fatalf("hidden input reached the record: %q", stored.Output)
|
||||||
|
}
|
||||||
|
frames.mu.Lock()
|
||||||
|
defer frames.mu.Unlock()
|
||||||
|
for _, frame := range frames.frames {
|
||||||
|
if strings.Contains(frame.Output, "s3cret") {
|
||||||
|
t.Fatalf("hidden input reached a frame: %q", frame.Output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalControlCInterrupts(t *testing.T) {
|
||||||
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
|
run := startTerminal(t, runner, `echo ready; sleep 30; echo never`, 0, 0)
|
||||||
|
waitOutput(t, runner, run.ID, "ready")
|
||||||
|
started := time.Now()
|
||||||
|
if err := runner.Input("ses_1", run.ID, "\x03"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||||
|
if time.Since(started) > 5*time.Second || strings.Contains(stored.Output, "never") || stored.Status != protocol.ChatShellRunExited {
|
||||||
|
t.Fatalf("after Ctrl-C = %+v", stored)
|
||||||
|
}
|
||||||
|
if stored.Signal != "interrupt" && (stored.ExitCode == nil || *stored.ExitCode != 130) {
|
||||||
|
t.Fatalf("Ctrl-C ended it with %q / %v", stored.Signal, stored.ExitCode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalResizeReachesTheProcess(t *testing.T) {
|
||||||
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
|
run := startTerminal(t, runner, `stty size; echo ready; read -r x; stty size`, 90, 25)
|
||||||
|
if run.Cols != 90 || run.Rows != 25 {
|
||||||
|
t.Fatalf("started at %dx%d", run.Cols, run.Rows)
|
||||||
|
}
|
||||||
|
waitOutput(t, runner, run.ID, "ready")
|
||||||
|
if _, err := runner.Resize("ses_1", run.ID, 0, 10); err == nil {
|
||||||
|
t.Fatal("a zero size was accepted")
|
||||||
|
}
|
||||||
|
record, err := runner.Resize("ses_1", run.ID, 132, 41)
|
||||||
|
if err != nil || record.Cols != 132 || record.Rows != 41 {
|
||||||
|
t.Fatalf("resize = %+v, %v", record, err)
|
||||||
|
}
|
||||||
|
// Sizes are clamped to the protocol's bounds.
|
||||||
|
if record, err = runner.Resize("ses_1", run.ID, 5000, 1); err != nil || record.Cols != protocol.ChatShellTerminalMaxCols || record.Rows != protocol.ChatShellTerminalMinRows {
|
||||||
|
t.Fatalf("clamped resize = %+v, %v", record, err)
|
||||||
|
}
|
||||||
|
if _, err = runner.Resize("ses_1", run.ID, 132, 41); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := runner.Input("ses_1", run.ID, "\r"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||||
|
if !strings.Contains(stored.Text, "25 90\n") || !strings.Contains(stored.Text, "41 132\n") || stored.Cols != 132 || stored.Rows != 41 {
|
||||||
|
t.Fatalf("sizes seen = %q (%dx%d)", stored.Text, stored.Cols, stored.Rows)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalPersistsRawAndPlainText(t *testing.T) {
|
||||||
|
runner, frames := testShellRunner(t, context.Background())
|
||||||
|
run := startTerminal(t, runner, `printf '\033[31mred\033[0m\n'; printf 'progress 10%%\rprogress 100%%\n'; printf '\033]8;;https://example.com\033\\link\033]8;;\033\\\n'`, 0, 0)
|
||||||
|
stored := finishedTerminal(t, runner, frames, run.ID)
|
||||||
|
if !strings.Contains(stored.Output, "\x1b[31mred\x1b[0m\r\n") || !strings.Contains(stored.Output, "progress 10%\rprogress 100%") {
|
||||||
|
t.Fatalf("raw output = %q", stored.Output)
|
||||||
|
}
|
||||||
|
if stored.Text != "red\nprogress 100%\nlink\n" {
|
||||||
|
t.Fatalf("plain text = %q", stored.Text)
|
||||||
|
}
|
||||||
|
if !stored.Terminal || stored.Cols != protocol.ChatShellTerminalDefaultCols {
|
||||||
|
t.Fatalf("stored terminal facts = %+v", stored)
|
||||||
|
}
|
||||||
|
list, err := runner.List(context.Background(), "ses_1")
|
||||||
|
if err != nil || len(list) != 1 || list[0].Text != "" || list[0].Output != stored.Output {
|
||||||
|
t.Fatalf("list = %+v, %v", list, err)
|
||||||
|
}
|
||||||
|
if prompt := chatShellSharePrompt(stored); strings.Contains(prompt, "\x1b") || !strings.Contains(prompt, "red\nprogress 100%\nlink\n```") {
|
||||||
|
t.Fatalf("shared prompt = %q", prompt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellTerminalInputBounds(t *testing.T) {
|
||||||
|
runner, _ := testShellRunner(t, context.Background())
|
||||||
|
// The command never reads, so queued input piles up.
|
||||||
|
run := startTerminal(t, runner, `stty -icanon -echo min 1; sleep 30`, 0, 0)
|
||||||
|
if err := runner.Input("ses_1", run.ID, strings.Repeat("x", protocol.ChatShellInputLimit+1)); err == nil {
|
||||||
|
t.Fatal("oversized input was accepted")
|
||||||
|
}
|
||||||
|
if err := runner.Input("ses_1", run.ID, ""); err == nil {
|
||||||
|
t.Fatal("empty input was accepted")
|
||||||
|
}
|
||||||
|
if err := runner.Input("ses_1", run.ID, "\xff"); err == nil {
|
||||||
|
t.Fatal("invalid UTF-8 was accepted")
|
||||||
|
}
|
||||||
|
chunk := strings.Repeat("y", protocol.ChatShellInputLimit)
|
||||||
|
refused := false
|
||||||
|
for range protocol.ChatShellInputQueue + 64 {
|
||||||
|
if err := runner.Input("ses_1", run.ID, chunk); err != nil {
|
||||||
|
refused = strings.Contains(err.Error(), "not reading")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !refused {
|
||||||
|
t.Fatal("input to a command that never reads was never refused")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestChatShellRecordAfterResumes(t *testing.T) {
|
||||||
|
record := protocol.ChatShellRunRecord{Output: "héllo world", OutputStart: 100, OutputBytes: 112}
|
||||||
|
if got := chatShellRecordAfter(record, 107); got.Output != "world" || got.OutputStart != 107 {
|
||||||
|
t.Fatalf("resume = %q at %d", got.Output, got.OutputStart)
|
||||||
|
}
|
||||||
|
if got := chatShellRecordAfter(record, 102); got.Output != "llo world" || got.OutputStart != 103 {
|
||||||
|
t.Fatalf("resume inside a character = %q at %d", got.Output, got.OutputStart)
|
||||||
|
}
|
||||||
|
if got := chatShellRecordAfter(record, 50); got.Output != record.Output || got.OutputStart != 100 {
|
||||||
|
t.Fatalf("resume before the tail = %q at %d", got.Output, got.OutputStart)
|
||||||
|
}
|
||||||
|
if got := chatShellRecordAfter(record, 500); got.Output != "" || got.OutputStart != 112 {
|
||||||
|
t.Fatalf("resume after the end = %q at %d", got.Output, got.OutputStart)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
//go:build darwin || linux
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/creack/pty"
|
||||||
|
)
|
||||||
|
|
||||||
|
// chatShellTerminals is true where operator commands run in a pseudo-terminal.
|
||||||
|
const chatShellTerminals = true
|
||||||
|
|
||||||
|
// startChatShellTerminal starts cmd as the leader of a new session whose
|
||||||
|
// controlling terminal is a fresh pseudo-terminal of cols x rows, and returns
|
||||||
|
// the terminal's controller side: the command's merged output is read from
|
||||||
|
// it, typed input written to it. The session id is also its process group,
|
||||||
|
// so Stop's group signals reach everything it started.
|
||||||
|
func startChatShellTerminal(cmd *exec.Cmd, cols, rows int) (*os.File, error) {
|
||||||
|
return pty.StartWithAttrs(cmd, chatShellWinsize(cols, rows), &syscall.SysProcAttr{Setsid: true, Setctty: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
// resizeChatShellTerminal sets the terminal's size; the kernel signals the
|
||||||
|
// foreground process group with SIGWINCH.
|
||||||
|
func resizeChatShellTerminal(terminal *os.File, cols, rows int) error {
|
||||||
|
return pty.Setsize(terminal, chatShellWinsize(cols, rows))
|
||||||
|
}
|
||||||
|
|
||||||
|
func chatShellWinsize(cols, rows int) *pty.Winsize {
|
||||||
|
return &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows)}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
//go:build darwin || linux
|
||||||
|
|
||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// One client starts an interactive command; the other client watching the
|
||||||
|
// chat types into it, resizes it, and resumes its output by offset.
|
||||||
|
func TestChatShellTerminalOverWebsocket(t *testing.T) {
|
||||||
|
ctx, conns, _ := shellChat(t)
|
||||||
|
start := shellRequest(protocol.ChatShellRun, "r1", "", `printf 'token? '; read -r tok; stty size; echo got:$tok`)
|
||||||
|
start.ChatShellRunRequest.Cols, start.ChatShellRunRequest.Rows = 80, 24
|
||||||
|
writeChat(t, ctx, conns[0], start)
|
||||||
|
reply := readShellFrame(t, ctx, conns[0], func(m protocol.Message) bool { return m.ID == "r1" })
|
||||||
|
run := reply.ChatShellRun
|
||||||
|
if reply.Type != protocol.ChatShellRun || run == nil || !run.Terminal || run.Cols != 80 || run.Rows != 24 {
|
||||||
|
t.Fatalf("start reply = %+v", reply)
|
||||||
|
}
|
||||||
|
// The prompt arrives as an output frame before anything is typed.
|
||||||
|
var seen strings.Builder
|
||||||
|
var end int64
|
||||||
|
for !strings.Contains(seen.String(), "token? ") {
|
||||||
|
frame := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool {
|
||||||
|
return m.Type == protocol.ChatShellRunEvent && m.ChatShellRunEvent.RunID == run.ID && m.ChatShellRunEvent.Run == nil
|
||||||
|
}).ChatShellRunEvent
|
||||||
|
if frame.Offset != end {
|
||||||
|
t.Fatalf("frame at %d after %d", frame.Offset, end)
|
||||||
|
}
|
||||||
|
seen.WriteString(frame.Output)
|
||||||
|
end = frame.End
|
||||||
|
}
|
||||||
|
resize := shellRequest(protocol.ChatShellResize, "size", run.ID, "")
|
||||||
|
resize.ChatShellRunRequest.Cols, resize.ChatShellRunRequest.Rows = 120, 33
|
||||||
|
writeChat(t, ctx, conns[1], resize)
|
||||||
|
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "size" }); got.Type != protocol.ChatShellResize || got.ChatShellRun.Cols != 120 {
|
||||||
|
t.Fatalf("resize reply = %+v", got)
|
||||||
|
}
|
||||||
|
input := shellRequest(protocol.ChatShellInput, "in", run.ID, "")
|
||||||
|
input.ChatShellRunRequest.Data = "abc123\r"
|
||||||
|
writeChat(t, ctx, conns[1], input)
|
||||||
|
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.ChatShellInput {
|
||||||
|
t.Fatalf("input reply = %+v", got)
|
||||||
|
}
|
||||||
|
final, _ := finishedShell(t, ctx, conns[0], run.ID)
|
||||||
|
if final.Status != protocol.ChatShellRunExited {
|
||||||
|
t.Fatalf("final = %+v", final)
|
||||||
|
}
|
||||||
|
// A reconnecting client asks only for what follows its copy.
|
||||||
|
resume := shellRequest(protocol.ChatShellRunOutput, "resume", run.ID, "")
|
||||||
|
resume.ChatShellRunRequest.After = end
|
||||||
|
writeChat(t, ctx, conns[1], resume)
|
||||||
|
got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "resume" }).ChatShellRun
|
||||||
|
if got == nil || got.OutputStart != end || strings.Contains(got.Output, "token?") || !strings.Contains(got.Output, "33 120\r\ngot:abc123\r\n") || !strings.Contains(got.Text, "got:abc123") {
|
||||||
|
t.Fatalf("resumed = %+v", got)
|
||||||
|
}
|
||||||
|
// Input to a finished run is refused.
|
||||||
|
writeChat(t, ctx, conns[1], input)
|
||||||
|
if got := readShellFrame(t, ctx, conns[1], func(m protocol.Message) bool { return m.ID == "in" }); got.Type != protocol.Error {
|
||||||
|
t.Fatalf("late input reply = %+v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -250,7 +250,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
|||||||
if err := wsjson.Read(connCtx, conn, &request); err != nil {
|
if err := wsjson.Read(connCtx, conn, &request); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
|
if request.Type != protocol.ChatShellInput && request.Type != protocol.ChatShellResize {
|
||||||
|
// Keystrokes would be one line each; their data is never logged.
|
||||||
|
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
|
||||||
|
}
|
||||||
if request.Type == protocol.Submit && !options.AllowAuthoring {
|
if request.Type == protocol.Submit && !options.AllowAuthoring {
|
||||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"})
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"})
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user