Connect saved machines concurrently in the chat workspace
This commit is contained in:
@@ -91,7 +91,11 @@ mixin _ChatScreenWorkspace on State<ChatScreen> {
|
||||
_workspaceScope = scope;
|
||||
_workspaceBinding = ChatWorkspaceBinding(
|
||||
controller: _workspace,
|
||||
store: SharedPreferencesChatWorkspaceStore(serverScope: scope),
|
||||
store: widget.gateway is FederatedChatGateway
|
||||
? FederatedWorkspaceStore(
|
||||
primary: (widget.gateway as FederatedChatGateway).primary(),
|
||||
)
|
||||
: SharedPreferencesChatWorkspaceStore(serverScope: scope),
|
||||
sidebarMode: () => _sidebarMode.name,
|
||||
onRestored: (snapshot) {
|
||||
if (!mounted) return;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import '../../../core/servers/chat/chat_identity.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
@@ -41,8 +42,15 @@ const String chatUnsentKeyPrefix = 'pipelines.next.chatUnsent.v1.';
|
||||
class SharedPreferencesChatDraftStore implements ChatDraftStore {
|
||||
const SharedPreferencesChatDraftStore({this.serverId = ''});
|
||||
final String serverId;
|
||||
String _native(String sessionId) {
|
||||
final parsed = parseFederatedChatId(sessionId);
|
||||
return parsed != null && serverStorageScope(parsed.server) == serverId
|
||||
? parsed.native
|
||||
: sessionId;
|
||||
}
|
||||
|
||||
String _key(String sessionId) =>
|
||||
scopedServerKey('$chatDraftKeyPrefix$sessionId', serverId);
|
||||
scopedServerKey('$chatDraftKeyPrefix${_native(sessionId)}', serverId);
|
||||
|
||||
Future<SharedPreferences?> _preferences() async {
|
||||
try {
|
||||
@@ -77,7 +85,7 @@ class SharedPreferencesChatDraftStore implements ChatDraftStore {
|
||||
}
|
||||
|
||||
String _unsentKey(String sessionId) =>
|
||||
scopedServerKey('$chatUnsentKeyPrefix$sessionId', serverId);
|
||||
scopedServerKey('$chatUnsentKeyPrefix${_native(sessionId)}', serverId);
|
||||
|
||||
@override
|
||||
Future<List<Object?>> readUnsent(String sessionId) async {
|
||||
|
||||
@@ -28,15 +28,10 @@ FileUrls? fileUrls(String configuredEndpoint, String path) {
|
||||
final value = path;
|
||||
if (value.trim().isEmpty) return null;
|
||||
try {
|
||||
final endpoint = ServerUrl.validate(configuredEndpoint);
|
||||
final base = Uri.parse(
|
||||
endpoint.httpUrl,
|
||||
).replace(path: '/files', query: null, fragment: null);
|
||||
Uri make({required bool download}) => base.replace(
|
||||
queryParameters: <String, String>{
|
||||
'path': value,
|
||||
if (download) 'download': '1',
|
||||
},
|
||||
Uri make({required bool download}) => ServerUrl.resourceUri(
|
||||
configuredEndpoint,
|
||||
'/files',
|
||||
query: <String, String>{'path': value, if (download) 'download': '1'},
|
||||
);
|
||||
return FileUrls(
|
||||
view: make(download: false),
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import 'workspace/federated_workspace_store.dart';
|
||||
import '../../core/servers/chat/chat_identity.dart';
|
||||
import '../../core/servers/chat/federated_chat_gateway.dart';
|
||||
import '../../core/servers/server_context.dart';
|
||||
import '../../shell/destination.dart';
|
||||
import '../../shell/shell_bar.dart';
|
||||
import '../../shell/shell_destination_menu.dart';
|
||||
@@ -449,7 +453,9 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
final start = _startNotice;
|
||||
final thread = ChatThreadView(
|
||||
key: ValueKey<String>('chat-session-$selected'),
|
||||
gateway: widget.gateway,
|
||||
gateway: widget.gateway is FederatedChatGateway
|
||||
? (widget.gateway as FederatedChatGateway).forChat(selected)
|
||||
: widget.gateway,
|
||||
sessionId: selected,
|
||||
title: '${row?['title'] ?? ''}'.trim(),
|
||||
session: row,
|
||||
@@ -504,7 +510,11 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
return ChatComposerFocusScope(
|
||||
sessionId: selected,
|
||||
requests: _composerFocus,
|
||||
child: thread,
|
||||
child: ServerContext.forServer(
|
||||
context,
|
||||
'${row?['_server_id'] ?? parseFederatedChatId(selected)?.server ?? ''}',
|
||||
thread,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import '../../../../core/servers/chat/federated_chat_gateway.dart';
|
||||
import '../../../../core/servers/server_context.dart';
|
||||
import 'dart:async';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
@@ -77,9 +78,12 @@ class ChatCompactSidebar extends StatelessWidget {
|
||||
final running = row['running'] == true;
|
||||
final backgroundTasks = chatSessionBackgroundTasks(row);
|
||||
final title = chatSessionTitle(row);
|
||||
final machineName =
|
||||
ServerContext.nameFor(context, '${row['_server_id'] ?? ''}') ??
|
||||
row['_server_name'] as String?;
|
||||
final details = [
|
||||
title,
|
||||
?ServerContext.maybeOf(context)?.name,
|
||||
?(machineName ?? ServerContext.maybeOf(context)?.name),
|
||||
if (chatSessionSubtitle(row).isNotEmpty) chatSessionSubtitle(row),
|
||||
if (relativeChatSessionTime(row['updated_at']).isNotEmpty)
|
||||
'Updated ${relativeChatSessionTime(row['updated_at'])} ago',
|
||||
@@ -107,16 +111,29 @@ class ChatCompactSidebar extends StatelessWidget {
|
||||
context,
|
||||
details,
|
||||
ExcludeSemantics(
|
||||
child: ChatCompactAvatar(
|
||||
title: title,
|
||||
color: color,
|
||||
active: selected,
|
||||
visible: visible,
|
||||
running: running,
|
||||
backgroundTasks: backgroundTasks,
|
||||
unread: unread,
|
||||
cupertino:
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ChatCompactAvatar(
|
||||
title: title,
|
||||
color: color,
|
||||
active: selected,
|
||||
visible: visible,
|
||||
running: running,
|
||||
backgroundTasks: backgroundTasks,
|
||||
unread: unread,
|
||||
cupertino:
|
||||
chatControlsStyleOf(context) ==
|
||||
ChatControlsStyle.cupertino,
|
||||
),
|
||||
if (machineName case final name?)
|
||||
Text(
|
||||
name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: Theme.of(context).textTheme.labelSmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
() {
|
||||
@@ -148,7 +165,9 @@ class ChatCompactSidebar extends StatelessWidget {
|
||||
: Icons.menu_open,
|
||||
),
|
||||
const Text('Expand', textAlign: TextAlign.center),
|
||||
if (ServerContext.maybeOf(context)?.name case final name?)
|
||||
if (controller.repository.gateway is FederatedChatGateway)
|
||||
const Text('All machines', textAlign: TextAlign.center)
|
||||
else if (ServerContext.maybeOf(context)?.name case final name?)
|
||||
Text(
|
||||
name,
|
||||
maxLines: 2,
|
||||
|
||||
@@ -10,6 +10,7 @@ Future<SavedServer?> chooseChatMachine(BuildContext context) async {
|
||||
final scope = ServerContext.maybeOf(context);
|
||||
final servers = scope?.notifier?.catalog.servers ?? const <SavedServer>[];
|
||||
if (servers.isEmpty) return null;
|
||||
if (servers.length == 1) return servers.single;
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return showCupertinoModalPopup<SavedServer>(
|
||||
context: context,
|
||||
|
||||
@@ -65,6 +65,7 @@ class ChatSessionSubtitle extends StatelessWidget {
|
||||
const ChatSessionSubtitle({
|
||||
super.key,
|
||||
required this.metadata,
|
||||
this.machineName,
|
||||
required this.closed,
|
||||
required this.active,
|
||||
required this.visible,
|
||||
@@ -73,6 +74,7 @@ class ChatSessionSubtitle extends StatelessWidget {
|
||||
required this.unread,
|
||||
});
|
||||
final String metadata;
|
||||
final String? machineName;
|
||||
final bool closed, active, visible, running, unread;
|
||||
final int backgroundTasks;
|
||||
@override
|
||||
@@ -88,7 +90,11 @@ class ChatSessionSubtitle extends StatelessWidget {
|
||||
style: TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
if (closed && metadata.isNotEmpty) const TextSpan(text: ' · '),
|
||||
TextSpan(text: ServerContext.metadata(context, metadata)),
|
||||
TextSpan(
|
||||
text: machineName == null
|
||||
? ServerContext.metadata(context, metadata)
|
||||
: '$machineName${metadata.isEmpty ? '' : ' · $metadata'}',
|
||||
),
|
||||
],
|
||||
),
|
||||
style: TextStyle(color: chatMetadataForeground(context)),
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import '../../../../core/servers/chat/federated_chat_gateway.dart';
|
||||
import 'federated_chat_session_local.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
@@ -34,9 +36,15 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
this.activeAckDelay = const Duration(milliseconds: 1500),
|
||||
}) : _localStore =
|
||||
localStore ??
|
||||
SharedPreferencesChatSessionLocalStore(
|
||||
serverId: serverStorageScope(serverIdentity(repository.gateway)),
|
||||
),
|
||||
(repository.gateway is FederatedChatGateway
|
||||
? FederatedChatSessionLocalStore(
|
||||
() => (repository.gateway as FederatedChatGateway).serverIds,
|
||||
)
|
||||
: SharedPreferencesChatSessionLocalStore(
|
||||
serverId: serverStorageScope(
|
||||
serverIdentity(repository.gateway),
|
||||
),
|
||||
)),
|
||||
_nowMillis = nowMillis ?? _wallClockMillis {
|
||||
_listenFrames();
|
||||
_bindAckSync(ackSync ?? attentionAckSyncOf(repository.gateway));
|
||||
@@ -377,13 +385,14 @@ class ChatSessionsController extends ChangeNotifier {
|
||||
/// Creates one session and returns the server's `chat_session`. A reply
|
||||
/// without one is refused rather than opening an invented row.
|
||||
Future<JsonMap> createOrThrow({
|
||||
ChatRepository? targetRepository,
|
||||
required String title,
|
||||
required String directory,
|
||||
required String backend,
|
||||
required String instructions,
|
||||
required String operationId,
|
||||
}) async {
|
||||
final reply = await repository.create(
|
||||
final reply = await (targetRepository ?? repository).create(
|
||||
title: title,
|
||||
directory: directory,
|
||||
backend: backend,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import '../../../../core/servers/chat/federated_chat_gateway.dart';
|
||||
import '../../../../core/servers/server_context.dart';
|
||||
import 'chat_machine_picker.dart';
|
||||
import '../../../../ui/adaptive/segmented_bar.dart';
|
||||
@@ -197,10 +198,23 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
|
||||
Future<void> _newChat({bool chooseMachine = true}) async {
|
||||
final scope = ServerContext.maybeOf(context);
|
||||
var repository = _controller.repository;
|
||||
if (chooseMachine && scope != null) {
|
||||
final selected = await chooseChatMachine(context);
|
||||
if (!mounted || selected == null) return;
|
||||
if (selected.id != scope.serverId) {
|
||||
if (repository.gateway is FederatedChatGateway) {
|
||||
try {
|
||||
repository = ChatRepository(
|
||||
(repository.gateway as FederatedChatGateway).forServer(selected.id),
|
||||
);
|
||||
} catch (_) {
|
||||
await showChatMachineError(
|
||||
context,
|
||||
'Sign in to ${selected.name} or retry its connection before creating a chat.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
} else if (selected.id != scope.serverId) {
|
||||
final switched = await scope.notifier!.requestNewChat(selected.id);
|
||||
if (!switched && mounted) {
|
||||
await showChatMachineError(
|
||||
@@ -213,11 +227,13 @@ class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
}
|
||||
// One id per create intent: a refusal retried in the dialog reuses it.
|
||||
final operationId = 'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
||||
if (!mounted) return;
|
||||
final session = await showChatCreateDialog(
|
||||
context,
|
||||
repository: _controller.repository,
|
||||
repository: repository,
|
||||
operationId: operationId,
|
||||
onSubmit: (draft, id) => _controller.createOrThrow(
|
||||
targetRepository: repository,
|
||||
title: draft.title,
|
||||
directory: draft.directory,
|
||||
backend: draft.backend,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import '../../../../core/servers/server_context.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
@@ -47,11 +48,15 @@ Future<JsonMap?> showChatCreateDialog(
|
||||
onSubmit,
|
||||
ChatCreatePromptStore? prompts,
|
||||
}) {
|
||||
final dialog = CreateChatDialog(
|
||||
repository: repository,
|
||||
operationId: operationId,
|
||||
onSubmit: onSubmit,
|
||||
prompts: prompts,
|
||||
final dialog = ServerContext.forServer(
|
||||
context,
|
||||
serverIdentity(repository.gateway) ?? '',
|
||||
CreateChatDialog(
|
||||
repository: repository,
|
||||
operationId: operationId,
|
||||
onSubmit: onSubmit,
|
||||
prompts: prompts,
|
||||
),
|
||||
);
|
||||
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? showCupertinoDialog<JsonMap>(context: context, builder: (_) => dialog)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../../../core/servers/chat/chat_identity.dart';
|
||||
import '../../../../core/servers/server_scope.dart';
|
||||
import 'chat_session_local.dart';
|
||||
|
||||
/// Import old per-machine read/hidden choices into the combined list once.
|
||||
class FederatedChatSessionLocalStore implements ChatSessionLocalStore {
|
||||
FederatedChatSessionLocalStore(this.serverIds);
|
||||
final Iterable<String> Function() serverIds;
|
||||
static const scope = 'all-saved-servers';
|
||||
final combined = const SharedPreferencesChatSessionLocalStore(
|
||||
serverId: scope,
|
||||
);
|
||||
@override
|
||||
Future<ChatSessionLocal> load() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (prefs.containsKey(
|
||||
scopedServerKey(SharedPreferencesChatSessionLocalStore.key, scope),
|
||||
)) {
|
||||
return combined.load();
|
||||
}
|
||||
final hidden = <String>[], unseen = <String>[];
|
||||
final seen = <String, int>{};
|
||||
for (final server in serverIds()) {
|
||||
final old = await SharedPreferencesChatSessionLocalStore(
|
||||
serverId: serverStorageScope(server),
|
||||
).load();
|
||||
String id(String value) => federatedChatId(server, value);
|
||||
hidden.addAll(old.hidden.map(id));
|
||||
unseen.addAll(old.unseen.map(id));
|
||||
seen.addAll({
|
||||
for (final entry in old.seenAt.entries) id(entry.key): entry.value,
|
||||
});
|
||||
}
|
||||
final result = ChatSessionLocal(
|
||||
hidden: hidden,
|
||||
unseen: unseen,
|
||||
seenAt: seen,
|
||||
);
|
||||
await combined.save(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> save(ChatSessionLocal state) => combined.save(state);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import '../../../../core/servers/server_context.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -129,6 +130,12 @@ class ChatSessionRow extends StatelessWidget {
|
||||
),
|
||||
// `Closed` leads the line of a closed chat, then the model and folder.
|
||||
subtitle: ChatSessionSubtitle(
|
||||
machineName:
|
||||
ServerContext.nameFor(
|
||||
context,
|
||||
'${session['_server_id'] ?? ''}',
|
||||
) ??
|
||||
session['_server_name'] as String?,
|
||||
metadata: text,
|
||||
closed: closed,
|
||||
active: selected,
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
import '../../../core/servers/chat/chat_identity.dart';
|
||||
import '../../../core/servers/server_scope.dart';
|
||||
import 'chat_workspace_nodes.dart';
|
||||
import 'chat_workspace_snapshot.dart';
|
||||
import 'chat_workspace_store.dart';
|
||||
|
||||
/// Adopt the previous primary-machine layout once, without altering its copy.
|
||||
/// Thereafter all panes live together independently of the pipelines/run default.
|
||||
class FederatedWorkspaceStore implements ChatWorkspaceStore {
|
||||
FederatedWorkspaceStore({
|
||||
required this.primary,
|
||||
ChatWorkspaceStore? combined,
|
||||
ChatWorkspaceStore? legacy,
|
||||
}) : combined =
|
||||
combined ??
|
||||
const SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: 'all-saved-servers',
|
||||
),
|
||||
legacy =
|
||||
legacy ??
|
||||
SharedPreferencesChatWorkspaceStore(
|
||||
serverScope: serverStorageScope(primary),
|
||||
);
|
||||
final String? primary;
|
||||
final ChatWorkspaceStore combined, legacy;
|
||||
@override
|
||||
Future<ChatWorkspaceSnapshot?> read() async {
|
||||
final saved = await combined.read();
|
||||
if (saved != null || primary == null) return saved;
|
||||
final old = await legacy.read();
|
||||
if (old == null) return null;
|
||||
ChatWorkspaceNode map(ChatWorkspaceNode node) => switch (node) {
|
||||
ChatWorkspacePane() => ChatWorkspacePane(
|
||||
node.id,
|
||||
parseFederatedChatId(node.sessionId) == null
|
||||
? federatedChatId(primary!, node.sessionId)
|
||||
: node.sessionId,
|
||||
),
|
||||
ChatWorkspaceSplit() => ChatWorkspaceSplit(
|
||||
node.id,
|
||||
node.axis,
|
||||
map(node.first),
|
||||
map(node.second),
|
||||
ratio: node.ratio,
|
||||
),
|
||||
};
|
||||
final migrated = ChatWorkspaceSnapshot.fromLayout(
|
||||
old.root == null ? null : map(old.root!),
|
||||
old.activePaneId,
|
||||
sidebarMode: old.sidebarMode,
|
||||
mobilePeekFraction: old.mobilePeekFraction,
|
||||
);
|
||||
if (migrated != null) await combined.write(migrated);
|
||||
return migrated;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> write(ChatWorkspaceSnapshot snapshot) =>
|
||||
combined.write(snapshot);
|
||||
}
|
||||
Reference in New Issue
Block a user