e72bee3faa
- One shared EmptyState (icon on a tonal disc, title, one line, primary action) that follows the design tokens; used by Runs (Start your first run), Pipelines (Browse templates), Schedules and the chat list (Start a chat). - Chat on a server without an agent harness showed "Bad state: No saved machine is connected.": the federated list now keeps the server's own refusal, treats an omitted empty list as no chats, and the list shows a "Set up an agent" state with the harness setup command and Refresh. New chat shows the same step instead of a form that can only be refused. - A run that fails because no harness CLI exists names the setup command beside the raw reason; error advice gains a noHarness kind. - Templates opened before the app connected stayed on "Not connected"; it now reads again when the connection arrives. The Pipelines empty state links to Templates, which had no entry point in the UI. - The first-launch walkthrough waits briefly for the connection, so a connected web app is no longer told to connect; the empty-server check treats omitted lists and no chat harness as empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
258 lines
8.2 KiB
Dart
258 lines
8.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
import '../../core/error_advice.dart';
|
|
import 'first_run_walkthrough.dart';
|
|
import 'walkthrough_store.dart';
|
|
|
|
/// Reports whether the server has no pipelines, runs or chats yet. Returns null
|
|
/// when any of the three lists cannot be read, so an unreadable server never
|
|
/// triggers the walkthrough.
|
|
Future<bool?> serverHasNoWorkYet(ApiGateway? gateway) async {
|
|
if (gateway == null) return null;
|
|
// A server leaves an empty list out of its reply.
|
|
Future<List<Object?>?> list(String type, String key, [JsonMap? fields]) =>
|
|
gateway.call(type, fields ?? const {}).then((reply) {
|
|
final value = reply[key] ?? const <Object?>[];
|
|
return value is List ? List<Object?>.from(value) : null;
|
|
});
|
|
Future<List<Object?>?> chats() async {
|
|
try {
|
|
return await list('chat_sessions', 'chat_sessions');
|
|
} catch (error) {
|
|
// A server without a chat harness has no chats yet; that is the newest
|
|
// server of all, not an unreadable one.
|
|
if (needsHarnessSetup(error)) return const <Object?>[];
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
try {
|
|
final lists = await Future.wait(<Future<List<Object?>?>>[
|
|
list('list_pipelines', 'pipelines', {'include_archived': true}),
|
|
list('list_runs', 'runs'),
|
|
chats(),
|
|
]);
|
|
if (lists.any((list) => list == null)) return null;
|
|
return lists.every((list) => list!.isEmpty);
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// Offers the walkthrough from the Pipelines page, which is the first screen a
|
|
/// new user sees. It is offered when:
|
|
///
|
|
/// * the device has not finished or skipped it yet (first launch, once); or
|
|
/// * the device already has, and the connected server has no pipelines, runs
|
|
/// or chats, and that server has not been offered it before.
|
|
///
|
|
/// The server check needs a connected session. While disconnected it waits, and
|
|
/// it runs again on each new connection until the server answers. A check that
|
|
/// cannot read the server leaves the gate open to retry; a decided check (the
|
|
/// walkthrough shown, or the server has work) ends it for this mount.
|
|
///
|
|
/// Nothing is shown while the store cannot be read, because an unavailable
|
|
/// store reports "seen". The check runs only while [enabled] (the Pipelines tab
|
|
/// is the one showing).
|
|
class FirstRunWalkthroughGate extends StatefulWidget {
|
|
const FirstRunWalkthroughGate({
|
|
super.key,
|
|
required this.child,
|
|
required this.store,
|
|
required this.enabled,
|
|
required this.serverScope,
|
|
required this.connection,
|
|
required this.connected,
|
|
this.gateway,
|
|
this.onOpenConnection,
|
|
this.connectWait = const Duration(seconds: 3),
|
|
});
|
|
|
|
/// How long the first offer waits for a connection before it shows the
|
|
/// walkthrough with its connect page.
|
|
final Duration connectWait;
|
|
|
|
/// Opens Settings' Connection from the walkthrough's connect page.
|
|
final VoidCallback? onOpenConnection;
|
|
|
|
final Widget child;
|
|
final WalkthroughStore store;
|
|
|
|
/// Whether the Pipelines page is the one showing.
|
|
final bool enabled;
|
|
|
|
/// The connected server's scope, recorded when the walkthrough is offered.
|
|
final String serverScope;
|
|
|
|
/// Notifies when the connection may have changed; the gate re-reads
|
|
/// [connected] on each notification.
|
|
final Listenable connection;
|
|
|
|
/// Whether the server session is connected right now.
|
|
final bool Function() connected;
|
|
|
|
/// The connected server's gateway, used for the empty-server check.
|
|
final ApiGateway? gateway;
|
|
|
|
@override
|
|
State<FirstRunWalkthroughGate> createState() =>
|
|
_FirstRunWalkthroughGateState();
|
|
}
|
|
|
|
class _FirstRunWalkthroughGateState extends State<FirstRunWalkthroughGate> {
|
|
/// The offer is decided for this mount: shown, or nothing left to offer.
|
|
bool _decided = false;
|
|
|
|
/// An offer is in flight.
|
|
bool _running = false;
|
|
|
|
/// A new connection arrived while an offer was in flight.
|
|
bool _connectedAgain = false;
|
|
late bool _wasConnected;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_wasConnected = widget.connected();
|
|
widget.connection.addListener(_onConnectionChanged);
|
|
_scheduleIfEnabled();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(FirstRunWalkthroughGate oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.connection != widget.connection) {
|
|
oldWidget.connection.removeListener(_onConnectionChanged);
|
|
widget.connection.addListener(_onConnectionChanged);
|
|
}
|
|
_scheduleIfEnabled();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_endConnectionWait();
|
|
widget.connection.removeListener(_onConnectionChanged);
|
|
super.dispose();
|
|
}
|
|
|
|
/// Acts only on a disconnected-to-connected transition, so frequent session
|
|
/// notifications while connected never repeat the server check.
|
|
void _onConnectionChanged() {
|
|
final connected = widget.connected();
|
|
final became = connected && !_wasConnected;
|
|
_wasConnected = connected;
|
|
if (!became || _decided) return;
|
|
if (_running) {
|
|
_connectedAgain = true;
|
|
return;
|
|
}
|
|
_scheduleIfEnabled();
|
|
}
|
|
|
|
void _scheduleIfEnabled() {
|
|
if (_decided || _running || !widget.enabled) return;
|
|
_running = true;
|
|
// A connection can arrive between frames; the post-frame callback needs a
|
|
// frame, so request one explicitly.
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!_active) {
|
|
// Pipelines is hidden (or the gate is gone): release the flag so a
|
|
// later enable reschedules the offer through didUpdateWidget.
|
|
_running = false;
|
|
return;
|
|
}
|
|
unawaited(_runOffer());
|
|
});
|
|
WidgetsBinding.instance.ensureVisualUpdate();
|
|
}
|
|
|
|
/// The gate may offer only while mounted and while the Pipelines page is the
|
|
/// one showing. Checked after every await and before every show.
|
|
bool get _active => mounted && widget.enabled;
|
|
|
|
Future<void> _runOffer() async {
|
|
try {
|
|
await _offer();
|
|
} finally {
|
|
_running = false;
|
|
final again = _connectedAgain;
|
|
_connectedAgain = false;
|
|
if (again && !_decided) _scheduleIfEnabled();
|
|
}
|
|
}
|
|
|
|
Future<void> _offer() async {
|
|
final seen = await widget.store.isSeen();
|
|
if (!_active) return;
|
|
if (!seen) {
|
|
_decided = true;
|
|
// The app usually connects within a moment of its first frame (always
|
|
// on the web, which is served by the server). Wait briefly so a
|
|
// connected app is not told to connect.
|
|
if (!widget.connected()) await _connection(widget.connectWait);
|
|
await _showIfActive();
|
|
return;
|
|
}
|
|
if (await widget.store.wasOffered(widget.serverScope)) {
|
|
if (!_active) return;
|
|
_decided = true;
|
|
return;
|
|
}
|
|
// Wait for a connection; the connection listener runs the check again.
|
|
if (!widget.connected()) return;
|
|
final empty = await serverHasNoWorkYet(widget.gateway);
|
|
if (!_active) return;
|
|
// Unreadable: stay open so the next connection retries the check.
|
|
if (empty == null) return;
|
|
_decided = true;
|
|
if (!empty) return;
|
|
await widget.store.markOffered(widget.serverScope);
|
|
await _showIfActive();
|
|
}
|
|
|
|
/// The first offer's wait for a connection; ended early by [dispose].
|
|
Timer? _connectTimer;
|
|
Completer<void>? _connectWaiting;
|
|
|
|
/// Completes when the session connects or after [limit], whichever is
|
|
/// first, or as soon as the gate is disposed.
|
|
Future<void> _connection(Duration limit) {
|
|
final done = _connectWaiting = Completer<void>();
|
|
void check() {
|
|
if (widget.connected()) _endConnectionWait();
|
|
}
|
|
|
|
final listenable = widget.connection..addListener(check);
|
|
_connectTimer = Timer(limit, _endConnectionWait);
|
|
return done.future.whenComplete(() => listenable.removeListener(check));
|
|
}
|
|
|
|
void _endConnectionWait() {
|
|
_connectTimer?.cancel();
|
|
_connectTimer = null;
|
|
final waiting = _connectWaiting;
|
|
_connectWaiting = null;
|
|
if (waiting != null && !waiting.isCompleted) waiting.complete();
|
|
}
|
|
|
|
Future<void> _showIfActive() async {
|
|
if (!_active) return;
|
|
await _show();
|
|
}
|
|
|
|
Future<void> _show() async {
|
|
await showFirstRunWalkthrough(
|
|
context,
|
|
onClosed: widget.store.markSeen,
|
|
connected: widget.connected(),
|
|
onOpenConnection: widget.onOpenConnection,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => widget.child;
|
|
}
|