feat(app): register the live Chat destination with read-only history

This commit is contained in:
2026-10-21 22:57:08 +01:00
parent 76fb2cdd87
commit 86a9b4a664
19 changed files with 2365 additions and 48 deletions
+32 -6
View File
@@ -10,7 +10,7 @@ enum AppRouteKind { pipelines, runs, chat, settings }
/// malformed path resolves to the default Pipelines list instead of throwing.
/// The serialiser is its inverse for every route the app can produce.
class AppRoute {
const AppRoute(this.kind, {this.runId});
const AppRoute(this.kind, {this.runId, this.chatSessionId});
static const pipelines = AppRoute(AppRouteKind.pipelines);
static const runs = AppRoute(AppRouteKind.runs);
@@ -19,16 +19,27 @@ class AppRoute {
static const run = _RunRouteFactory();
/// One chat session, or the chat list when [id] is not a session id.
static AppRoute chatSession(String id) => isChatSessionId(id)
? AppRoute(AppRouteKind.chat, chatSessionId: id)
: chat;
final AppRouteKind kind;
/// The run shown by the Runs destination, or null for the list.
final int? runId;
/// The chat session shown by the Chat destination, or null for the list.
final String? chatSessionId;
/// The path the app writes into history for this route.
String get path => switch (kind) {
AppRouteKind.pipelines => '/pipelines',
AppRouteKind.runs => runId == null ? '/runs' : '/runs/$runId',
AppRouteKind.chat => '/chat',
AppRouteKind.chat =>
chatSessionId == null || !isChatSessionId(chatSessionId!)
? '/chat'
: '/chat/${Uri.encodeComponent(chatSessionId!)}',
AppRouteKind.settings => '/settings',
};
@@ -65,6 +76,7 @@ class AppRoute {
'runs' when segments.length == 1 => runs,
'runs' when segments.length == 2 => _runRoute(segments[1]),
'chat' when segments.length == 1 => chat,
'chat' when segments.length == 2 => _chatRoute(segments[1]),
'settings' when segments.length == 1 => settings,
_ => pipelines,
};
@@ -77,12 +89,26 @@ class AppRoute {
return AppRoute(AppRouteKind.runs, runId: id);
}
@override
bool operator ==(Object other) =>
other is AppRoute && other.kind == kind && other.runId == runId;
/// A chat session id is nonempty, bounded and URL-safe, matching the web
/// parser. Anything else is malformed rather than a stale session the
/// destination has to guess about.
static bool isChatSessionId(String value) =>
value.isNotEmpty &&
value.length <= 200 &&
RegExp(r'^[A-Za-z0-9_-]+$').hasMatch(value);
static AppRoute _chatRoute(String value) =>
isChatSessionId(value) ? chatSession(value) : pipelines;
@override
int get hashCode => Object.hash(kind, runId);
bool operator ==(Object other) =>
other is AppRoute &&
other.kind == kind &&
other.runId == runId &&
other.chatSessionId == chatSessionId;
@override
int get hashCode => Object.hash(kind, runId, chatSessionId);
@override
String toString() => path;
+13 -3
View File
@@ -27,6 +27,7 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
AppRouterDelegate({
required this.builder,
required this.onPopToRuns,
required this.onPopToChat,
required this.cupertino,
AppRoute initial = AppRoute.pipelines,
}) : _route = initial;
@@ -36,6 +37,9 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
/// Called when a system Back belongs to the Runs detail, not the app stack.
final VoidCallback onPopToRuns;
/// Called when a system Back belongs to one chat session.
final VoidCallback onPopToChat;
final bool cupertino;
final _navigatorKey = GlobalKey<NavigatorState>();
@@ -81,9 +85,15 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
@override
Future<bool> popRoute() async {
if (_route.runId == null) return false;
onPopToRuns();
return true;
if (_route.runId != null) {
onPopToRuns();
return true;
}
if (_route.chatSessionId != null) {
onPopToChat();
return true;
}
return false;
}
/// Records a user move; the Router turns the new configuration into a
+69
View File
@@ -0,0 +1,69 @@
import 'package:flutter/material.dart';
import '../features/chat/screen.dart';
import '../ui/adaptive/host.dart';
import 'app_session.dart';
import 'connection_status.dart';
import 'server_endpoint.dart';
/// The Chat destination: the shared session list and one read-only thread.
///
/// Before the shared socket accepts its first connection nothing is requested,
/// so an offline launch cannot look like an empty chat list. After that the
/// screen stays mounted through drops; a reconnect advances [syncToken] so the
/// open conversation re-subscribes and re-reads without losing its selection.
class ChatDestination extends StatelessWidget {
const ChatDestination({
super.key,
required this.session,
required this.server,
this.initialSessionId,
this.onSessionSelected,
this.onBack,
});
final AppSession session;
final ServerEndpoint server;
final String? initialSessionId;
final ValueChanged<String>? onSessionSelected;
final VoidCallback? onBack;
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: session,
builder: (context, _) {
final cupertino = usesCupertinoHost(context);
if ((server.requiresConfiguration || server.error != null) &&
session.endpoint == null) {
return ConnectionGate(
session: session,
server: server,
cupertino: cupertino,
);
}
if (!session.everConnected) {
if (session.connected) return _chatScreen();
return ConnectionGate(
session: session,
server: server,
cupertino: cupertino,
);
}
return Column(
children: [
if (!session.connected)
ConnectionBanner(session: session, cupertino: cupertino),
Expanded(child: _chatScreen()),
],
);
},
);
Widget _chatScreen() => ChatScreen(
gateway: session.gateway,
initialSessionId: initialSessionId,
syncToken: session.connectionEpoch,
onSessionSelected: onSessionSelected,
onBack: onBack,
);
}
+9
View File
@@ -11,6 +11,7 @@ import 'app.dart';
import 'app_route.dart';
import 'app_router.dart';
import 'app_session.dart';
import 'chat_destination.dart';
import 'pipelines_destination.dart';
import 'runs_destination.dart';
import 'server_endpoint.dart';
@@ -71,6 +72,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
initial: initial,
cupertino: _cupertino,
onPopToRuns: () => _delegate.goTo(AppRoute.runs),
onPopToChat: () => _delegate.goTo(AppRoute.chat),
builder: _buildShell,
);
_config = RouterConfig<AppRoute>(
@@ -110,6 +112,13 @@ class _PipelinesAppState extends State<PipelinesApp> {
onRunSelected: (id) => _delegate.goTo(AppRoute.run(id)),
onBack: () => _delegate.goTo(AppRoute.runs),
),
AppDestination.chat: (_) => ChatDestination(
session: _session,
server: widget.server,
initialSessionId: route.chatSessionId,
onSessionSelected: (id) => _delegate.goTo(AppRoute.chatSession(id)),
onBack: () => _delegate.goTo(AppRoute.chat),
),
AppDestination.settings: (_) => SettingsScreen(
client: _session.client,
initialEndpoint: _session.endpoint,