feat(app): configure and safely switch multi-address servers

This commit is contained in:
2026-10-24 03:04:53 +01:00
parent 47cfb31e7d
commit b499da304d
38 changed files with 1764 additions and 65 deletions
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../../core/api_gateway.dart';
import '../../../core/servers/server_scope.dart';
import '../data/chat_repository.dart';
import 'chat_draft_store.dart';
@@ -47,7 +48,11 @@ class ChatComposerController extends ChangeNotifier {
this.onOptimistic,
this.onRejected,
this.newOperationId,
}) : _drafts = drafts ?? const SharedPreferencesChatDraftStore() {
}) : _drafts =
drafts ??
SharedPreferencesChatDraftStore(
serverId: serverStorageScope(serverIdentity(gateway)),
) {
_subscription = gateway.frames.listen(_onFrame);
}
@@ -1,4 +1,5 @@
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/servers/server_scope.dart';
/// One conversation's persisted composer draft, keyed by session id.
///
@@ -25,7 +26,10 @@ const String chatDraftKeyPrefix = 'pipelines.next.chatDraft.v1.';
/// Reads are tolerant and a write reports failure instead of throwing, so the
/// composer can keep the in-memory draft and say plainly that it was not saved.
class SharedPreferencesChatDraftStore implements ChatDraftStore {
const SharedPreferencesChatDraftStore();
const SharedPreferencesChatDraftStore({this.serverId = ''});
final String serverId;
String _key(String sessionId) =>
scopedServerKey('$chatDraftKeyPrefix$sessionId', serverId);
Future<SharedPreferences?> _preferences() async {
try {
@@ -40,7 +44,7 @@ class SharedPreferencesChatDraftStore implements ChatDraftStore {
final preferences = await _preferences();
if (preferences == null) return '';
try {
return preferences.getString('$chatDraftKeyPrefix$sessionId') ?? '';
return preferences.getString(_key(sessionId)) ?? '';
} catch (_) {
return '';
}
@@ -51,7 +55,7 @@ class SharedPreferencesChatDraftStore implements ChatDraftStore {
final preferences = await _preferences();
if (preferences == null) return false;
try {
final key = '$chatDraftKeyPrefix$sessionId';
final key = _key(sessionId);
if (text.isEmpty) return await preferences.remove(key);
return await preferences.setString(key, text);
} catch (_) {
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../core/servers/server_scope.dart';
/// Whether a new chat starts with a prompt, and which, for one harness and
/// directory: the last choice made there.
@@ -13,10 +14,13 @@ abstract interface class ChatCreatePromptStore {
}
class SharedPreferencesChatCreatePromptStore implements ChatCreatePromptStore {
const SharedPreferencesChatCreatePromptStore();
const SharedPreferencesChatCreatePromptStore({this.serverId = ''});
final String serverId;
static String _key(String backend, String directory) =>
'pipelines.chat.createPrompt.v1.$backend|$directory';
String _key(String backend, String directory) => scopedServerKey(
'pipelines.chat.createPrompt.v1.$backend|$directory',
serverId,
);
@override
Future<ChatCreatePrompt?> read(String backend, String directory) async {
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../core/servers/server_scope.dart';
/// Device-local list preferences, mirroring the web client's C13 behaviour:
/// hidden ids and the newest session recency this device has read.
@@ -124,7 +125,9 @@ abstract interface class ChatSessionLocalStore {
/// `shared_preferences`-backed storage; unavailable storage degrades to the
/// in-memory choice instead of failing the list.
class SharedPreferencesChatSessionLocalStore implements ChatSessionLocalStore {
const SharedPreferencesChatSessionLocalStore();
const SharedPreferencesChatSessionLocalStore({this.serverId = ''});
final String serverId;
String get _key => scopedServerKey(key, serverId);
static const key = 'pipelines.chat.local.v1';
@@ -140,7 +143,7 @@ class SharedPreferencesChatSessionLocalStore implements ChatSessionLocalStore {
Future<ChatSessionLocal> load() async {
final preferences = await _preferences();
try {
return ChatSessionLocal.decode(preferences?.getString(key));
return ChatSessionLocal.decode(preferences?.getString(_key));
} catch (_) {
return const ChatSessionLocal();
}
@@ -150,7 +153,7 @@ class SharedPreferencesChatSessionLocalStore implements ChatSessionLocalStore {
Future<void> save(ChatSessionLocal state) async {
final preferences = await _preferences();
try {
await preferences?.setString(key, state.encode());
await preferences?.setString(_key, state.encode());
} catch (_) {
// Storage may be unavailable; the in-memory choice still applies.
}
@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../../../core/api_gateway.dart';
import '../../../../core/servers/server_scope.dart';
import '../../../../core/transport/connection_state.dart';
import '../../data/chat_repository.dart';
import '../../domain/chat_sessions.dart';
@@ -17,7 +18,10 @@ import 'chat_session_local.dart';
class ChatSessionsController extends ChangeNotifier {
ChatSessionsController(this.repository, {ChatSessionLocalStore? localStore})
: _localStore =
localStore ?? const SharedPreferencesChatSessionLocalStore() {
localStore ??
SharedPreferencesChatSessionLocalStore(
serverId: serverStorageScope(serverIdentity(repository.gateway)),
) {
_listenFrames();
}
@@ -4,6 +4,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../core/servers/server_scope.dart';
import '../../data/chat_create_prompt_store.dart';
import '../../data/chat_repository.dart';
import '../../domain/chat_commands.dart';
@@ -42,8 +43,7 @@ Future<JsonMap?> showChatCreateDialog(
required String operationId,
required Future<JsonMap> Function(ChatDraft draft, String operationId)
onSubmit,
ChatCreatePromptStore prompts =
const SharedPreferencesChatCreatePromptStore(),
ChatCreatePromptStore? prompts,
}) {
final dialog = CreateChatDialog(
repository: repository,
@@ -62,11 +62,11 @@ class CreateChatDialog extends StatefulWidget {
required this.repository,
required this.operationId,
required this.onSubmit,
this.prompts = const SharedPreferencesChatCreatePromptStore(),
this.prompts,
});
/// The last first-prompt choice per harness and directory.
final ChatCreatePromptStore prompts;
final ChatCreatePromptStore? prompts;
final ChatRepository repository;
final String operationId;
@@ -77,6 +77,11 @@ class CreateChatDialog extends StatefulWidget {
}
class _CreateChatDialogState extends State<CreateChatDialog> {
late final ChatCreatePromptStore _prompts =
widget.prompts ??
SharedPreferencesChatCreatePromptStore(
serverId: serverStorageScope(serverIdentity(widget.repository.gateway)),
);
final _instructions = TextEditingController(
text: chatDefaultSessionInstructions,
);
@@ -161,7 +166,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
_name.text = defaultChatName(directory, chatBackendLabel(backend));
}
final recall = ++_recall;
final saved = await widget.prompts.read(backend, directory);
final saved = await _prompts.read(backend, directory);
if (!mounted || recall != _recall) return;
if (_promptEdited) return;
setState(() {
@@ -193,7 +198,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
);
// Next time in this folder with this harness, the same choice.
unawaited(
widget.prompts.write(backend, directory, (
_prompts.write(backend, directory, (
send: _sendPrompt,
text: _instructions.text,
)),