Files
pipelines/app/lib/features/chat/conversation/chat_draft_store.dart
T
alan 565cdaaa1b feat(app): add native chat composer draft and send state
Persist the exact per-session draft, send one plain prompt with a stable operation/native identity, restore a refused draft without overwriting newer typing, track the reply's queued flag and stop through chat_interrupt. No UI, transport, server or platform change; the optimistic/rejection callbacks and loadDraft are the thread integration seam.
2026-10-21 21:25:51 +01:00

93 lines
3.1 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
/// One conversation's persisted composer draft, keyed by session id.
///
/// The draft is stored exactly as typed (untrimmed); only the wire copy is
/// trimmed. Attachments are intentionally absent: a file handle cannot be
/// serialised, and the web build keeps them memory-only too.
abstract interface class ChatDraftStore {
/// The saved draft, or an empty string when there is none or the store is
/// unavailable. Implementations may throw; the controller treats that as
/// "nothing saved" and keeps its in-memory draft.
Future<String> read(String sessionId);
/// Stores [text] exactly. A blank draft removes the entry so the store does
/// not grow without bound. Returns false when nothing was stored.
Future<bool> write(String sessionId, String text);
}
/// The app's per-session draft keys. Independent of the legacy web/Android
/// storage: no old draft is imported or written under an old key.
const String chatDraftKeyPrefix = 'pipelines.next.chatDraft.v1.';
/// `shared_preferences`-backed draft storage.
///
/// 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();
Future<SharedPreferences?> _preferences() async {
try {
return await SharedPreferences.getInstance();
} catch (_) {
return null;
}
}
@override
Future<String> read(String sessionId) async {
final preferences = await _preferences();
if (preferences == null) return '';
try {
return preferences.getString('$chatDraftKeyPrefix$sessionId') ?? '';
} catch (_) {
return '';
}
}
@override
Future<bool> write(String sessionId, String text) async {
final preferences = await _preferences();
if (preferences == null) return false;
try {
final key = '$chatDraftKeyPrefix$sessionId';
if (text.isEmpty) return await preferences.remove(key);
return await preferences.setString(key, text);
} catch (_) {
return false;
}
}
}
/// An in-memory store for tests and previews. It never touches the platform
/// store, so no production preference can be mutated by a fixture.
class MemoryChatDraftStore implements ChatDraftStore {
MemoryChatDraftStore({Map<String, String>? drafts}) : _drafts = {...?drafts};
final Map<String, String> _drafts;
bool failReads = false;
bool failWrites = false;
/// A snapshot of the stored entries, so a test can tell a removed key from an
/// empty string.
Map<String, String> get entries => Map.unmodifiable(_drafts);
@override
Future<String> read(String sessionId) async {
if (failReads) throw StateError('Draft storage is unavailable.');
return _drafts[sessionId] ?? '';
}
@override
Future<bool> write(String sessionId, String text) async {
if (failWrites) return false;
if (text.isEmpty) {
_drafts.remove(sessionId);
} else {
_drafts[sessionId] = text;
}
return true;
}
}