Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

mobile: add Paste to the terminal's actions sheet

jaylfc87c246839d21
diff --git a/mobile/lib/phone/terminal_page.dart b/mobile/lib/phone/terminal_page.dart
index 4d391ea0a..14bf67145 100644
--- a/mobile/lib/phone/terminal_page.dart
+++ b/mobile/lib/phone/terminal_page.dart
@@ -43,6 +43,7 @@ import 'terminal_chrome_scroll.dart';
import 'terminal_header.dart';
import 'terminal_header_action.dart';
import 'terminal_input_dock.dart';
+import 'terminal_paste.dart';
import 'terminal_search.dart';
import 'voice_input_controller.dart';
@@ -1728,6 +1729,7 @@ class _TerminalPageState extends State<TerminalPage>
machine?.machine.displayName ?? '',
agent: agent,
status: headerStatus,
+ session: session,
),
),
],
@@ -1837,6 +1839,7 @@ class _TerminalPageState extends State<TerminalPage>
required String machineName,
required Agent agent,
required PhoneSummary status,
+ TerminalSession? session,
}) {
// Set here, where no build is running, so the sheet opens on the dot the header shows now
// rather than on whatever the last frame handed over. See [_actionsStatus].
@@ -1876,7 +1879,16 @@ class _TerminalPageState extends State<TerminalPage>
// them, where they have room for every row and do not push the rest of this sheet down. The
// chevron is what tells a door from an action.
sections: [
- PhoneSheetSection(actions: _agentActions(agent)),
+ PhoneSheetSection(
+ actions: [
+ // First, because it is the one used mid-conversation: iOS has no paste gesture on a
+ // terminal (long-press is selection there), and the soft keyboard has no paste key.
+ // Hidden rather than dimmed while the stream is read-only: reclaiming is the header's
+ // job, and a paste that silently goes nowhere is worse than no row.
+ if (session != null && session.acceptsInput) _pasteAction(session),
+ ..._agentActions(agent),
+ ],
+ ),
PhoneSheetSection(
caption: 'App',
actions: [
@@ -1919,6 +1931,36 @@ class _TerminalPageState extends State<TerminalPage>
);
}
+ /// Paste: the clipboard into [session]. What it reads and sends lives in [pasteClipboard]; the
+ /// page hands it only what the page alone knows, whether this is still the terminal on screen.
+ PhoneSheetAction _pasteAction(TerminalSession session) => PhoneSheetAction(
+ icon: LucideIcons.clipboardPaste300,
+ label: 'Paste',
+ onTap: () {
+ final messenger = ScaffoldMessenger.maybeOf(context);
+ unawaited(
+ pasteClipboard(
+ session: session,
+ machine: widget.notifier.stateOf(widget.machineId),
+ stillShowing: () =>
+ mounted &&
+ widget.isActive &&
+ identical(
+ widget.notifier
+ .paneOfAgent(widget.machineId, widget.agentId)
+ ?.session,
+ session,
+ ),
+ report: (message) {
+ if (mounted) {
+ messenger?.showSnackBar(SnackBar(content: Text(message)));
+ }
+ },
+ ),
+ );
+ },
+ );
+
/// What acts on this agent and can be taken back — the first card of its sheet.
List<PhoneSheetAction> _agentActions(Agent agent) => [
// Where this agent runs, above the actions that act ON it: the desktop
diff --git a/mobile/lib/phone/terminal_paste.dart b/mobile/lib/phone/terminal_paste.dart
new file mode 100644
index 000000000..8c39c2616
--- /dev/null
+++ b/mobile/lib/phone/terminal_paste.dart
@@ -0,0 +1,65 @@
+import 'package:flutter/services.dart';
+
+import 'package:harness_mobile/state/app_state.dart' show MachineState;
+import 'package:harness_mobile/terminal/terminal_session.dart';
+
+/// Said when the terminal the paste was meant for is no longer the one to send it to.
+const pasteChangedMessage =
+ 'The terminal changed before the paste, so nothing was sent.';
+
+/// Pastes the phone's clipboard text into [session], as one paste rather than as typing.
+///
+/// The Paste row of the terminal's actions sheet (`phone/terminal_page.dart`). iOS has no other way
+/// in: long-press on the terminal is selection there, and the soft keyboard has no paste key.
+///
+/// Text is asked about first with [Clipboard.hasStrings], which iOS answers without its "Allow
+/// Paste" prompt, so an empty clipboard never asks. Unlike the desktop's ⌘V there is no Ctrl+V
+/// fallthrough: the agent runs on another machine, whose engine would read THAT machine's
+/// clipboard rather than this phone's.
+///
+/// [machine] is the session's machine, read for what its CLI can take at the moment of sending.
+/// Every outcome worth telling the person about goes to [report]; a refused prompt says nothing.
+///
+/// ⚠️ **The paste lands only in the stream Paste was tapped on.** Reading the clipboard crosses a
+/// platform boundary and can wait on the system's permission prompt, and in that time the page can
+/// have been swiped away, its pane replaced, or its stream reconnected or taken over. Nothing is
+/// sent unless [stillShowing] holds (the page is still mounted and active, and its pane's session,
+/// looked up afresh, is still [session]), the session still accepts input, and its stream id is
+/// the one it had when Paste was tapped. Otherwise [report] hears [pasteChangedMessage].
+Future<void> pasteClipboard({
+ required TerminalSession session,
+ required MachineState? machine,
+ required bool Function() stillShowing,
+ required void Function(String message) report,
+}) async {
+ final streamId = session.streamId;
+ bool stillOwnsPaste() =>
+ stillShowing() && session.acceptsInput && session.streamId == streamId;
+
+ final String? text;
+ try {
+ if (!await Clipboard.hasStrings()) {
+ report('There is no text on the clipboard.');
+ return;
+ }
+ text = (await Clipboard.getData(Clipboard.kTextPlain))?.text;
+ } on PlatformException {
+ report('Could not read the clipboard.');
+ return;
+ }
+ // Empty here is a refused prompt as often as an empty string: nothing to say about either.
+ if (text == null || text.isEmpty) return;
+ if (!stillOwnsPaste()) {
+ report(pasteChangedMessage);
+ return;
+ }
+ // The same choice the desktop's paste makes: one atomic paste frame where the machine's CLI
+ // knows it, otherwise xterm's own paste (bracketed when the program asked for it).
+ if (machine != null && machine.terminalPasteRawAvailable) {
+ if (!await session.pasteText(text)) {
+ report('The paste could not be sent.');
+ }
+ } else {
+ session.terminal.paste(text);
+ }
+}
diff --git a/mobile/test/terminal_paste_test.dart b/mobile/test/terminal_paste_test.dart
new file mode 100644
index 000000000..bca06902e
--- /dev/null
+++ b/mobile/test/terminal_paste_test.dart
@@ -0,0 +1,134 @@
+import 'package:flutter/services.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/models.dart';
+import 'package:harness_mobile/phone/terminal_paste.dart';
+import 'package:harness_mobile/state/app_state.dart';
+import 'package:harness_mobile/terminal/terminal_binary.dart';
+import 'package:harness_mobile/terminal/terminal_session.dart';
+
+import 'terminal_panel_fixture.dart';
+
+/// The terminal actions sheet's Paste, without the page around it: what it reads off the
+/// clipboard, which way it sends it, and when it refuses to send at all.
+void main() {
+ TestWidgetsFlutterBinding.ensureInitialized();
+ final messenger =
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
+
+ late List<TerminalBinaryFrame> frames;
+ late List<String> reports;
+ late TerminalSession session;
+ late MachineState machine;
+
+ /// The clipboard as Flutter's [Clipboard] sees it. [onRead] runs while the text is being read,
+ /// the window in which iOS can be showing its paste prompt.
+ void clipboardHolds(String? text, {void Function()? onRead}) {
+ messenger.setMockMethodCallHandler(SystemChannels.platform, (call) async {
+ switch (call.method) {
+ case 'Clipboard.hasStrings':
+ return {'value': text != null && text.isNotEmpty};
+ case 'Clipboard.getData':
+ onRead?.call();
+ return text == null ? null : {'text': text};
+ }
+ return null;
+ });
+ }
+
+ Future<void> paste({bool Function()? stillShowing}) => pasteClipboard(
+ session: session,
+ machine: machine,
+ stillShowing: stillShowing ?? () => true,
+ report: reports.add,
+ );
+
+ setUp(() {
+ frames = [];
+ reports = [];
+ session = controllingSession(
+ sendBinary: (frame) async {
+ frames.add(frame);
+ return true;
+ },
+ );
+ machine = MachineState(
+ const Machine(
+ machineId: 'm',
+ authMode: MachineAuthMode.remote,
+ name: 'Test host',
+ ),
+ );
+ });
+
+ tearDown(() {
+ messenger.setMockMethodCallHandler(SystemChannels.platform, null);
+ });
+
+ test('text goes out as one paste frame where the CLI knows it', () async {
+ machine.terminalPasteRawAvailable = true;
+ clipboardHolds('hello');
+
+ await paste();
+
+ expect(reports, isEmpty);
+ expect(frames.map((f) => f.kind), [TerminalBinaryKind.paste]);
+ expect(String.fromCharCodes(frames.single.bytes), 'hello');
+ });
+
+ test("text goes through xterm's own paste on an older CLI", () async {
+ clipboardHolds('hello');
+
+ await paste();
+ // Typed input rides the session's send tail, a microtask or two behind.
+ await pumpEventQueue();
+
+ expect(reports, isEmpty);
+ expect(frames, isNotEmpty);
+ expect(frames.map((f) => f.kind), everyElement(TerminalBinaryKind.input));
+ });
+
+ test('an empty clipboard is said, not sent', () async {
+ machine.terminalPasteRawAvailable = true;
+ clipboardHolds(null);
+
+ await paste();
+
+ expect(frames, isEmpty);
+ expect(reports, ['There is no text on the clipboard.']);
+ });
+
+ test('a stream that changed during the read is not pasted into', () async {
+ machine.terminalPasteRawAvailable = true;
+ // Reconnected while the prompt was up: same session, a different stream.
+ clipboardHolds('hello', onRead: () => session.streamId = 's2');
+
+ await paste();
+
+ expect(frames, isEmpty);
+ expect(reports, [pasteChangedMessage]);
+ });
+
+ test('a page no longer showing this session is not pasted into', () async {
+ machine.terminalPasteRawAvailable = true;
+ var showing = true;
+ clipboardHolds('hello', onRead: () => showing = false);
+
+ await paste(stillShowing: () => showing);
+
+ expect(frames, isEmpty);
+ expect(reports, [pasteChangedMessage]);
+ });
+
+ test('a stream taken over during the read is not pasted into', () async {
+ machine.terminalPasteRawAvailable = true;
+ clipboardHolds(
+ 'hello',
+ onRead: () => session.status = TerminalSessionStatus.takenOver,
+ );
+
+ await paste();
+
+ expect(frames, isEmpty);
+ expect(reports, [pasteChangedMessage]);
+ });
+}