Connect saved machines concurrently in the chat workspace
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user