Connect saved machines concurrently in the chat workspace

This commit is contained in:
2026-11-08 22:21:17 +00:00
parent 663d5fb5c9
commit e4904395f0
44 changed files with 2574 additions and 60 deletions
+16 -4
View File
@@ -1,3 +1,5 @@
import '../core/servers/chat/chat_server_pool.dart';
import '../core/servers/chat/chat_server_status.dart';
import 'package:flutter/material.dart';
import '../features/chat/screen.dart';
@@ -18,6 +20,7 @@ class ChatDestination extends StatelessWidget {
super.key,
required this.session,
required this.server,
this.pool,
this.initialSessionId,
this.onSessionSelected,
this.onBack,
@@ -26,6 +29,7 @@ class ChatDestination extends StatelessWidget {
});
final AppSession session;
final ChatServerPool? pool;
final ServerEndpoint server;
final String? initialSessionId;
final ValueChanged<String>? onSessionSelected;
@@ -35,9 +39,15 @@ class ChatDestination extends StatelessWidget {
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: session,
animation: Listenable.merge([session, ?pool]),
builder: (context, _) {
final cupertino = usesCupertinoHost(context);
if (pool != null && pool!.servers.catalog.servers.isNotEmpty) {
return TopBarNotices(
notices: [ChatServerStatusBar(pool: pool!)],
child: _chatScreen(),
);
}
if ((server.requiresConfiguration || server.error != null) &&
session.endpoint == null) {
return ConnectionGate(
@@ -68,10 +78,12 @@ class ChatDestination extends StatelessWidget {
);
Widget _chatScreen() => ChatScreen(
gateway: session.gateway,
gateway: pool?.gateway ?? session.gateway,
persistWorkspace: true,
initialSessionId: initialSessionId,
syncToken: session.connectionEpoch,
initialSessionId: initialSessionId == null
? null
: pool?.gateway.normalizeId(initialSessionId!) ?? initialSessionId,
syncToken: pool?.revision ?? session.connectionEpoch,
onSessionSelected: onSessionSelected,
onBack: onBack,
onWorkspaceEmpty: onWorkspaceEmpty,
+25 -2
View File
@@ -1,3 +1,4 @@
import '../core/servers/chat/chat_server_pool.dart';
import '../core/servers/server_context.dart';
import '../ui/adaptive/platform.dart';
import 'dart:async';
@@ -68,6 +69,7 @@ class PipelinesApp extends StatefulWidget {
this.serverAddresses,
this.serverId,
this.servers,
this.chatPool,
this.resetRoute = false,
this.legacyPendingEndpoints = const [],
this.webPlatform = kIsWeb,
@@ -95,6 +97,7 @@ class PipelinesApp extends StatefulWidget {
final List<Uri>? serverAddresses;
final String? serverId;
final ServerConnections? servers;
final ChatServerPool? chatPool;
final bool resetRoute;
final List<String> legacyPendingEndpoints;
final bool webPlatform;
@@ -223,11 +226,19 @@ class _PipelinesAppState extends State<PipelinesApp>
// The comparison includes the query: `?visit=N` is part of the route, while
// a dropped or unknown query is canonicalised away.
final location = routeLocation(_provider.value.uri);
final initial =
var initial =
widget.servers?.newChatServerId == widget.serverId &&
widget.serverId != null
? AppRoute.chat
: AppRoute.parse(location);
if (widget.chatPool != null) {
final id = initial.chatSessionId;
if (id != null) {
initial = AppRoute.chatSession(
widget.chatPool!.gateway.normalizeId(id),
);
}
}
final nativeDefault =
widget.routeInformationProvider == null &&
!kIsWeb &&
@@ -615,6 +626,14 @@ class _PipelinesAppState extends State<PipelinesApp>
void _routeChanged() {
final shown = _delegate.route;
final id = shown.chatSessionId;
if (id != null && widget.chatPool != null) {
final scoped = widget.chatPool!.gateway.normalizeId(id);
if (scoped != id) {
_delegate.goTo(AppRoute.chatSession(scoped));
return;
}
}
_tabLocations.record(shown);
_rememberBodyRoute(_delegate.shellRoute);
_rememberChatRoute(shown);
@@ -626,7 +645,10 @@ class _PipelinesAppState extends State<PipelinesApp>
}
Future<String?> _chatSessionTitle(String id) async {
final reply = await _session.gateway.call('chat_sessions');
id = widget.chatPool?.gateway.normalizeId(id) ?? id;
final reply = await (widget.chatPool?.gateway ?? _session.gateway).call(
'chat_sessions',
);
final sessions = reply['chat_sessions'];
if (sessions is! List) return null;
for (final value in sessions) {
@@ -756,6 +778,7 @@ class _PipelinesAppState extends State<PipelinesApp>
),
),
AppDestination.chat: (_) => ChatDestination(
pool: widget.chatPool,
session: _session,
server: widget.server,
initialSessionId: chatRoute.chatSessionId,
+38 -7
View File
@@ -1,3 +1,5 @@
import 'dart:async';
import '../core/servers/chat/chat_server_pool.dart';
import '../ui/adaptive/platform.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
@@ -34,6 +36,8 @@ class PipelinesRoot extends StatefulWidget {
this.web = kIsWeb,
this.clientFactory,
this.nativeUpdatesSupported,
this.federatedChats = true,
this.chatPoolFactory,
});
final ServerEndpoint server;
@@ -50,6 +54,8 @@ class PipelinesRoot extends StatefulWidget {
/// Optional capability override for platform hosts and isolated widget tests.
final bool? nativeUpdatesSupported;
final bool federatedChats;
final ChatServerPool Function(ServerConnections)? chatPoolFactory;
final PipelinesClient Function()? clientFactory;
@override
@@ -65,6 +71,15 @@ class _PipelinesRootState extends State<PipelinesRoot> {
int _activation = 0;
PipelinesClient? _client;
SavedServer? _activeServer;
ChatServerPool? _chatPool;
void _ensureChatPool() {
if (widget.federatedChats && _chatPool == null) {
_chatPool =
widget.chatPoolFactory?.call(_servers) ??
ChatServerPool(_servers, web: widget.web);
}
}
late final ServerConnections _servers = ServerConnections(
store: widget.catalogStore,
onSelected: _selectServer,
@@ -87,6 +102,7 @@ class _PipelinesRootState extends State<PipelinesRoot> {
_servers.restoreNewChatIntent(id);
}
}
_ensureChatPool();
setState(() {
_booting = false;
_onboarding = false;
@@ -156,16 +172,20 @@ class _PipelinesRootState extends State<PipelinesRoot> {
}
if (!mounted || token != _activation) return;
_activeServer = server;
_ensureChatPool();
setState(() {
_server = ServerEndpoint.resolve(
base: Uri.base,
web: false,
configured: server?.endpoints.first ?? '',
);
_onboarding =
server == null ||
(ServerSession.cookie == null &&
!(initial && widget.signedIn && server.id == 'legacy-server'));
_onboarding = widget.federatedChats
? _servers.catalog.servers.isEmpty
: server == null ||
(ServerSession.cookie == null &&
!(initial &&
widget.signedIn &&
server.id == 'legacy-server'));
_refused = false;
_generation++;
_booting = false;
@@ -178,6 +198,11 @@ class _PipelinesRootState extends State<PipelinesRoot> {
/// A kept session the server now refuses asks for the password again.
Future<void> _verify(Uri endpoint) async {
if (widget.federatedChats &&
_chatPool != null &&
_servers.catalog.servers.isNotEmpty) {
return;
}
final token = _activation;
final result = await probeServerAddresses(
[
@@ -206,7 +231,9 @@ class _PipelinesRootState extends State<PipelinesRoot> {
}
void _done(Uri endpoint) {
_ensureChatPool();
_activeServer = _servers.catalog.active;
if (_activeServer != null) unawaited(_chatPool?.retry(_activeServer!.id));
setState(() {
_server = ServerEndpoint.resolve(
base: Uri.base,
@@ -220,16 +247,17 @@ class _PipelinesRootState extends State<PipelinesRoot> {
});
}
Future<void> _saveOnboardingServer(String url) async {
Future<void> _saveOnboardingServer(String url, {String? name}) async {
final token = _activation;
final active = _servers.catalog.active;
SavedServer? target;
for (final server in _servers.catalog.servers) {
if (server.endpoints.contains(url)) target = server;
}
target ??= SavedServer.validated(
id: 'server-${DateTime.now().microsecondsSinceEpoch}',
name: active == null ? 'My server' : 'New server',
name: name?.trim().isNotEmpty == true
? name!.trim()
: Uri.parse(url).host,
endpoints: [url],
);
if (!await _servers.save(target, activate: false) ||
@@ -250,6 +278,7 @@ class _PipelinesRootState extends State<PipelinesRoot> {
@override
void dispose() {
_activation++;
_chatPool?.dispose();
_servers.dispose();
super.dispose();
}
@@ -280,6 +309,7 @@ class _PipelinesRootState extends State<PipelinesRoot> {
if (!_onboarding) {
return PipelinesApp(
androidSupported: widget.nativeUpdatesSupported,
chatPool: _chatPool,
key: ValueKey(_generation),
server: _server,
client: _client,
@@ -297,6 +327,7 @@ class _PipelinesRootState extends State<PipelinesRoot> {
initialServer: _server.uri?.toString() ?? widget.buildDefault,
startAtPassword: _refused,
saveServer: _saveOnboardingServer,
saveServerProfile: (url, name) => _saveOnboardingServer(url, name: name),
onDone: _done,
savedServers: SavedServersSection(connections: _servers),
discovery: platformServerDiscovery(web: widget.web),