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.

fix(desktop): the dial scrolls a focused cloud agent's chat pane (#1289)

0xdiego2582173e78309a
diff --git a/desktop/lib/state/app_state.dart b/desktop/lib/state/app_state.dart
index d54e8e3db..68cbf3de4 100644
--- a/desktop/lib/state/app_state.dart
+++ b/desktop/lib/state/app_state.dart
@@ -3400,6 +3400,27 @@ class AppNotifier extends ChangeNotifier {
/// instead; those are the transport-wide events, and they are marked.
TerminalSession? get activeTerminal => focusedPane?.session;
+ /// Dial scroll handlers of the mounted cloud chat panes. A cloud harness has no
+ /// terminal session to scroll, so its conversation registers here, by machine
+ /// and agent, for as long as it is on screen.
+ final _cloudScrollHandlers =
+ <(String, String), void Function(int phase, int dy, int velocity)>{};
+
+ /// Called by a cloud chat pane while mounted; returns how to withdraw it.
+ VoidCallback registerCloudScroll(
+ String machineId,
+ String agentId,
+ void Function(int phase, int dy, int velocity) handler,
+ ) {
+ final key = (machineId, agentId);
+ _cloudScrollHandlers[key] = handler;
+ return () {
+ if (identical(_cloudScrollHandlers[key], handler)) {
+ _cloudScrollHandlers.remove(key);
+ }
+ };
+ }
+
bool get canAddPane => panes.length < maxPanes;
Iterable<TerminalPane> panesFor(String machineId) =>
@@ -17198,11 +17219,17 @@ class AppNotifier extends ChangeNotifier {
// A finger going down is the gesture; moves and the inertial tail
// must not keep asking macOS for focus after the person switches away.
if (phase == 0) _requestDeviceWindow(machine);
- activeTerminal?.scroll(
- phase,
- (payload['dy'] as num?)?.round() ?? 0,
- (payload['velocity'] as num?)?.round() ?? 0,
- );
+ final dy = (payload['dy'] as num?)?.round() ?? 0;
+ final velocity = (payload['velocity'] as num?)?.round() ?? 0;
+ final pane = focusedPane;
+ final cloudScroll = pane?.agentId == null
+ ? null
+ : _cloudScrollHandlers[(pane!.machineId, pane.agentId!)];
+ if (cloudScroll != null) {
+ cloudScroll(phase, dy, velocity);
+ } else {
+ activeTerminal?.scroll(phase, dy, velocity);
+ }
return;
case 'dial_selection':
final reply = const ['search', 'match'].contains(payload['op'])
diff --git a/desktop/lib/widgets/cloud_chat_pane.dart b/desktop/lib/widgets/cloud_chat_pane.dart
index e11f8ae86..836cfc049 100644
--- a/desktop/lib/widgets/cloud_chat_pane.dart
+++ b/desktop/lib/widgets/cloud_chat_pane.dart
@@ -1,4 +1,5 @@
import 'dart:async';
+import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
@@ -71,15 +72,85 @@ class _CloudChatPaneState extends State<CloudChatPane> {
String? _error;
int _readAt = -1;
+ // The dial scrolls this conversation as it scrolls a terminal (same scale, speed
+ // and fling decay as terminal_panel.dart), except that this list is reversed:
+ // its offset grows toward OLDER messages, so the sign flips to keep the
+ // direction on screen the same.
+ static const _dialScale = 2.5;
+ static const _dialStopVelocity = 40.0;
+ static const _dialDecayPerSecond = 0.002;
+ VoidCallback? _unregisterDial;
+ Timer? _dialInertia;
+ double _dialVelocity = 0;
+ int? _lastInertiaMicros;
+
+ void _registerDial() {
+ _unregisterDial?.call();
+ _unregisterDial = widget.notifier.registerCloudScroll(
+ widget.machineId,
+ widget.agent.id,
+ _dialScroll,
+ );
+ }
+
+ void _dialScroll(int phase, int dy, int velocity) {
+ if (phase == 0) _cancelDialInertia();
+ if (dy != 0) _applyDialDelta(dy * _dialScale);
+ if (phase == 2) _startDialInertia(velocity.toDouble());
+ }
+
+ void _applyDialDelta(double delta) {
+ if (!_scroll.hasClients) return;
+ final position = _scroll.position;
+ position.jumpTo(
+ (position.pixels + delta)
+ .clamp(position.minScrollExtent, position.maxScrollExtent)
+ .toDouble(),
+ );
+ }
+
+ void _startDialInertia(double velocity) {
+ _cancelDialInertia();
+ if (velocity.abs() < _dialStopVelocity) return;
+ _dialVelocity = velocity;
+ _lastInertiaMicros = DateTime.now().microsecondsSinceEpoch;
+ _dialInertia = Timer.periodic(const Duration(milliseconds: 16), (_) {
+ if (!mounted) {
+ _cancelDialInertia();
+ return;
+ }
+ final now = DateTime.now().microsecondsSinceEpoch;
+ final elapsed = math.min((now - (_lastInertiaMicros ?? now)) / 1e6, 0.05);
+ _lastInertiaMicros = now;
+ _applyDialDelta(_dialVelocity * elapsed * _dialScale);
+ _dialVelocity *= math.pow(_dialDecayPerSecond, elapsed).toDouble();
+ if (_dialVelocity.abs() < _dialStopVelocity) _cancelDialInertia();
+ });
+ }
+
+ void _cancelDialInertia() {
+ _dialInertia?.cancel();
+ _dialInertia = null;
+ _dialVelocity = 0;
+ _lastInertiaMicros = null;
+ }
+
@override
void initState() {
super.initState();
+ _registerDial();
unawaited(_load());
}
@override
void didUpdateWidget(CloudChatPane old) {
super.didUpdateWidget(old);
+ if (old.agent.id != widget.agent.id ||
+ old.machineId != widget.machineId ||
+ old.notifier != widget.notifier) {
+ _cancelDialInertia();
+ _registerDial();
+ }
// The frame says how many messages its conversation holds: read again when that moves.
if (old.agent.id != widget.agent.id ||
(widget.agent.cloud?.messages ?? 0) != _readAt) {
@@ -89,6 +160,8 @@ class _CloudChatPaneState extends State<CloudChatPane> {
@override
void dispose() {
+ _unregisterDial?.call();
+ _cancelDialInertia();
_prompt.dispose();
_promptFocus.dispose();
_scroll.dispose();
diff --git a/desktop/test/cloud_agents_test.dart b/desktop/test/cloud_agents_test.dart
index 17df14ec9..a3a955075 100644
--- a/desktop/test/cloud_agents_test.dart
+++ b/desktop/test/cloud_agents_test.dart
@@ -23,6 +23,7 @@ class CloudConnection extends RenameConnection {
final sent = <Map<String, dynamic>>[];
final raw = <(String, Map<String, dynamic>)>[];
Map<String, dynamic> sendReply = {'ok': true};
+ int extraMessages = 0;
@override
Future<Map<String, dynamic>> request(
@@ -47,8 +48,10 @@ class CloudConnection extends RenameConnection {
'from': 'agent',
'text': 'They have 9:30 or 11:00. Which one?',
},
+ for (var i = 0; i < extraMessages; i++)
+ {'id': 'x$i', 'from': 'agent', 'text': 'Older line number $i'},
],
- 'total': 2,
+ 'total': 2 + extraMessages,
};
}
return super.request(type, payload: payload, timeout: timeout);
@@ -421,6 +424,45 @@ void main() {
);
});
+ testWidgets('the dial scrolls a focused cloud conversation', (tester) async {
+ final (app, connection) = await setUp(tester, cloudFrame());
+ connection.extraMessages = 60;
+ await app.addAgentToSwarm('m', 'cloud_1');
+ await mount(tester, app);
+ await tester.pump(const Duration(milliseconds: 50));
+ await tester.pumpAndSettle();
+ final list = find.byKey(const ValueKey('cloud-conversation'));
+ ScrollPosition position() =>
+ tester.widget<ListView>(list).controller!.position;
+ expect(app.focusedPane?.agentId, 'cloud_1');
+ expect(position().pixels, 0);
+ expect(position().maxScrollExtent, greaterThan(100));
+
+ Future<void> dial(Map<String, dynamic> p) =>
+ app.handleEventForTest('m', {'type': 'dial_scroll', 'payload': p});
+ await dial({'phase': 'down', 'dy': 0});
+ // Terminal convention: a positive dy shows earlier lines, here older messages.
+ await dial({'phase': 'move', 'dy': 20});
+ expect(position().pixels, 50);
+ await dial({'phase': 'move', 'dy': -8});
+ expect(position().pixels, 30);
+ await dial({'phase': 'move', 'dy': -100});
+ expect(position().pixels, 0, reason: 'clamped at the newest message');
+ await dial({'phase': 'move', 'dy': 20});
+ await dial({'phase': 'up', 'dy': 0, 'velocity': 400});
+ final released = position().pixels;
+ await tester.pump(const Duration(milliseconds: 200));
+ expect(position().pixels, greaterThan(released), reason: 'the fling');
+ await dial({'phase': 'down', 'dy': 0});
+ final stopped = position().pixels;
+ await tester.pump(const Duration(milliseconds: 200));
+ expect(position().pixels, stopped, reason: 'a touch stops the fling');
+
+ // Gone from the screen, the pane no longer takes the dial.
+ await tester.pumpWidget(const SizedBox());
+ await dial({'phase': 'move', 'dy': 20});
+ });
+
testWidgets(
'in the workspace, a cloud harness\'s pane is its conversation, not a terminal waiting for one',
(tester) async {