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.

Phone: setup progress, Start on a new computer, the hotspot's address

bubu-dudu97bb9fb3631d
diff --git a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
index 282c75897..8801a1f2d 100644
--- a/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
+++ b/mobile/android/app/src/main/kotlin/ai/autonomous/harness/android/MainActivity.kt
@@ -22,6 +22,7 @@ import android.os.Handler
import android.os.Looper
import android.provider.Settings
import java.io.ByteArrayOutputStream
+import java.net.Inet4Address
import io.flutter.FlutterInjector
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
@@ -141,7 +142,7 @@ class MainActivity : FlutterActivity() {
private var setupNetwork: ConnectivityManager.NetworkCallback? = null
private fun joinSetupWifi(prefix: String, result: MethodChannel.Result) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { result.success("unsupported"); return }
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { result.success(mapOf("state" to "unsupported")); return }
leaveSetupWifi()
val connectivity = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val specifier = WifiNetworkSpecifier.Builder()
@@ -154,13 +155,16 @@ class MainActivity : FlutterActivity() {
.build()
val main = Handler(Looper.getMainLooper())
var answered = false
- fun answer(value: String) = main.post { if (!answered) { answered = true; result.success(value) } }
+ fun answer(value: Map<String, String?>) = main.post { if (!answered) { answered = true; result.success(value) } }
val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
connectivity.bindProcessToNetwork(network)
- answer("joined")
+ // The address the device's hotspot gave this phone, for the page to show it is through.
+ val address = connectivity.getLinkProperties(network)?.linkAddresses
+ ?.firstOrNull { it.address is Inet4Address }?.address?.hostAddress
+ answer(mapOf("state" to "joined", "address" to address))
}
- override fun onUnavailable() { setupNetwork = null; answer("unavailable") }
+ override fun onUnavailable() { setupNetwork = null; answer(mapOf("state" to "unavailable")) }
override fun onLost(network: Network) { connectivity.bindProcessToNetwork(null) }
}
setupNetwork = callback
diff --git a/mobile/lib/phone/add_device/add_device_page.dart b/mobile/lib/phone/add_device/add_device_page.dart
index d81338dcd..82c9f8221 100644
--- a/mobile/lib/phone/add_device/add_device_page.dart
+++ b/mobile/lib/phone/add_device/add_device_page.dart
@@ -36,6 +36,44 @@ class AddDevicePage extends StatefulWidget {
enum _Step { start, join, networks, password, waiting, done }
+/// Joining the device's hotspot, shown as it goes: the person picking it, the phone on it and
+/// asking the device, then reading its Wi-Fi list.
+enum _Progress { choosing, talking, reading }
+
+/// One line of [_Progress]: a check when done, a spinner while it is going on.
+class _Line extends StatelessWidget {
+ const _Line({required this.done, required this.text});
+
+ final bool done;
+ final String text;
+
+ @override
+ Widget build(BuildContext context) {
+ final tty = Tty.of(context);
+ return Padding(
+ padding: const EdgeInsets.symmetric(vertical: 6),
+ child: Row(
+ children: [
+ SizedBox(
+ width: 18,
+ height: 18,
+ child: done
+ ? Icon(LucideIcons.check300, size: 18, color: tty.green)
+ : CircularProgressIndicator(strokeWidth: 2, color: tty.dim),
+ ),
+ const SizedBox(width: 12),
+ Expanded(
+ child: Text(
+ text,
+ style: tty.style(color: done ? tty.text : tty.dim),
+ ),
+ ),
+ ],
+ ),
+ );
+ }
+}
+
class _AddDevicePageState extends State<AddDevicePage> {
late final SetupHotspot _hotspot = widget.hotspot ?? SetupHotspot();
final _ssid = TextEditingController();
@@ -56,6 +94,11 @@ class _AddDevicePageState extends State<AddDevicePage> {
/// Back to the hotspot right after sending a Wi-Fi: the device may still be trying it.
bool _retrying = false;
+ _Progress _progress = _Progress.choosing;
+
+ /// The address the device's hotspot gave this phone, once Android says it is on it.
+ String? _address;
+
/// The device's last failed Wi-Fi and why, read when the phone joined its hotspot again.
({String ssid, String why})? _failed;
bool _alreadyHere = false;
@@ -117,11 +160,21 @@ class _AddDevicePageState extends State<AddDevicePage> {
Future<void> _join() async {
_show(_Step.join);
- setState(() => _busy = true);
+ setState(() {
+ _busy = true;
+ _progress = _Progress.choosing;
+ _address = null;
+ });
if (SetupHotspot.joinsByItself) {
final joined = await _hotspot.join();
if (!mounted) return;
- if (joined == 'unavailable') {
+ if (joined.state == 'joined') {
+ setState(() {
+ _progress = _Progress.talking;
+ _address = joined.address;
+ });
+ }
+ if (joined.state == 'unavailable') {
_show(
_Step.join,
error: 'No device was chosen. Check it is plugged in and nearby: its Wi-Fi is named "Harness-…".',
@@ -149,6 +202,7 @@ class _AddDevicePageState extends State<AddDevicePage> {
_poll?.cancel();
_device = info;
_retrying = false;
+ setState(() => _progress = _Progress.reading);
final networks = await _hotspot.networks();
if (!mounted) return;
// A failed try before this one: the device says why, once the phone is back on its hotspot.
@@ -255,7 +309,10 @@ class _AddDevicePageState extends State<AddDevicePage> {
}
if (!mounted) return;
await Navigator.of(context).pushReplacement(
- phoneRoute((_) => MachinesTab(notifier: _app, large: false)),
+ phoneRoute(
+ // Its row reads "new", with Start on it.
+ (_) => MachinesTab(notifier: _app, large: false),
+ ),
);
}
@@ -388,12 +445,34 @@ class _AddDevicePageState extends State<AddDevicePage> {
say(
'Open Settings ▸ Wi-Fi on this phone and choose "Harness-…". Then come back here.',
),
- const SizedBox(height: 12),
- say(
- _retrying
- ? 'The device is still trying the Wi-Fi. Its hotspot comes back within a minute.'
- : 'Looking for the device…',
+ if (_retrying) ...[
+ const SizedBox(height: 12),
+ say(
+ 'The device is still trying the Wi-Fi. Its hotspot comes back within a minute.',
+ ),
+ ],
+ const SizedBox(height: 16),
+ // Where it is, as a Wi-Fi settings page shows joining: what is done, and what is going on.
+ _Line(
+ done: _progress.index > _Progress.choosing.index,
+ text: _progress.index > _Progress.choosing.index
+ ? 'Joined the device\'s Wi-Fi${_address == null ? '' : ' · address $_address'}'
+ : SetupHotspot.joinsByItself
+ ? 'Choose "Harness-…" from the list…'
+ : 'Waiting for this phone to join "Harness-…"…',
),
+ if (_progress.index >= _Progress.talking.index)
+ _Line(
+ done: _progress.index > _Progress.talking.index,
+ text: _progress.index > _Progress.talking.index
+ ? 'The device answered'
+ : 'Talking to the device…',
+ ),
+ if (_progress == _Progress.reading)
+ const _Line(
+ done: false,
+ text: 'Reading the Wi-Fi the device sees…',
+ ),
];
case _Step.networks:
return [
diff --git a/mobile/lib/phone/add_device/setup_hotspot.dart b/mobile/lib/phone/add_device/setup_hotspot.dart
index 282c1806d..2cd0d21c1 100644
--- a/mobile/lib/phone/add_device/setup_hotspot.dart
+++ b/mobile/lib/phone/add_device/setup_hotspot.dart
@@ -32,17 +32,22 @@ class SetupHotspot {
/// Whether this phone joins the hotspot by itself, or the person does in Settings.
static bool get joinsByItself => Platform.isAndroid;
- /// `joined`, `unavailable` (the person cancelled, or no device was found), or `unsupported`.
- /// Android's list shows only the `Harness-…` Wi-Fi nearby.
- Future<String> join() async {
- if (!joinsByItself) return 'unsupported';
+ /// `joined` (with the address the hotspot gave this phone), `unavailable` (the person cancelled,
+ /// or no device was found), or `unsupported`. Android's list shows only the `Harness-…` Wi-Fi.
+ Future<({String state, String? address})> join() async {
+ if (!joinsByItself) return (state: 'unsupported', address: null);
try {
- return await _channel.invokeMethod<String>('join', {'prefix': prefix}) ??
- 'unavailable';
+ final reply = await _channel.invokeMethod<Map<Object?, Object?>>('join', {
+ 'prefix': prefix,
+ });
+ return (
+ state: reply?['state'] as String? ?? 'unavailable',
+ address: reply?['address'] as String?,
+ );
} on PlatformException {
- return 'unavailable';
+ return (state: 'unavailable', address: null);
} on MissingPluginException {
- return 'unsupported';
+ return (state: 'unsupported', address: null);
}
}
diff --git a/mobile/lib/phone/machines_tab.dart b/mobile/lib/phone/machines_tab.dart
index 5b4e542d9..3f0e9a053 100644
--- a/mobile/lib/phone/machines_tab.dart
+++ b/mobile/lib/phone/machines_tab.dart
@@ -4,6 +4,8 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
+import 'package:harness_mobile/core/models.dart' show Agent;
+import 'package:harness_mobile/core/project_folder.dart';
import 'package:harness_mobile/shared/theme/app_theme.dart';
import 'package:harness_mobile/shared/widgets/empty_state.dart';
import 'package:harness_mobile/state/app_state.dart';
@@ -341,12 +343,19 @@ class _Body extends StatelessWidget {
if (status == PhoneMachineStatus.ready) {
unawaited(notifier.refreshOsStatus(machineId));
}
+ // Just set up from this phone: Start, to its harness, or to a new OpenCode there when it has none.
+ final start =
+ notifier.isNewMachine(machineId) && status == PhoneMachineStatus.ready;
return FindRow(
title: state.machine.displayName,
detail: ownLine ? detail : null,
minHeight: ownLine ? FindRow.height : _gatheredRowHeight,
state: word,
stateColor: color,
+ accessory: start
+ ? _StartButton(notifier: notifier, machineId: machineId)
+ : null,
+ accessoryRoom: start ? _StartButton.room : 0,
// ⚠️ An offline computer is tappable now. It used to be drawn inert, with nothing a tap could do
// for it — and it is exactly the one somebody wants to try again, rename, or take off the
// account.
@@ -655,3 +664,79 @@ class _RefreshButtonState extends State<_RefreshButton>
);
}
}
+
+
+/// Start, on a computer just set up from this phone: its harness — OpenCode first — or, with none
+/// there yet, a new OpenCode in a fresh project of its own, opened as soon as it is up.
+class _StartButton extends StatefulWidget {
+ const _StartButton({required this.notifier, required this.machineId});
+
+ final AppNotifier notifier;
+ final String machineId;
+
+ /// Kept at the end of the row's first line for it.
+ static const double room = 76;
+
+ @override
+ State<_StartButton> createState() => _StartButtonState();
+}
+
+class _StartButtonState extends State<_StartButton> {
+ bool _busy = false;
+
+ AppNotifier get _app => widget.notifier;
+
+ Agent? _harness() {
+ final agents = _app.machineStates[widget.machineId]?.agents ?? const [];
+ bool opencode(Agent agent) => '${agent.name} ${agent.title ?? ''}'
+ .toLowerCase()
+ .contains('opencode');
+ return agents.where(opencode).firstOrNull ?? agents.firstOrNull;
+ }
+
+ Future<void> _start() async {
+ final messenger = ScaffoldMessenger.maybeOf(context);
+ var harness = _harness();
+ if (harness == null) {
+ setState(() => _busy = true);
+ final failed = await _app.createAgent(
+ widget.machineId,
+ engine: 'opencode',
+ folder: '',
+ projectFolder: const ProjectFolderRequest.newProject(),
+ );
+ if (failed != null) {
+ if (mounted) setState(() => _busy = false);
+ messenger?.showSnackBar(SnackBar(content: Text(failed)));
+ return;
+ }
+ // Up when the computer lists it.
+ final deadline = DateTime.now().add(const Duration(seconds: 30));
+ while ((harness = _harness()) == null &&
+ DateTime.now().isBefore(deadline)) {
+ await Future<void>.delayed(const Duration(seconds: 1));
+ }
+ if (!mounted) return;
+ setState(() => _busy = false);
+ if (harness == null) {
+ messenger?.showSnackBar(
+ const SnackBar(
+ content: Text('OpenCode is still starting there. Try again in a moment.'),
+ ),
+ );
+ return;
+ }
+ }
+ _app.sawMachine(widget.machineId);
+ if (mounted) openAgent(context, _app, widget.machineId, harness.id);
+ }
+
+ @override
+ Widget build(BuildContext context) => TtyTextButton(
+ key: ValueKey('machines-start-${widget.machineId}'),
+ label: _busy ? 'Starting…' : 'Start',
+ color: Tty.of(context).green,
+ weight: FontWeight.w600,
+ onPressed: _busy ? null : () => unawaited(_start()),
+ );
+}