import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../../../core/api_gateway.dart'; const chatAutoContinuePreferenceKey = 'pipelines.chatAutoContinue.v1'; /// The usage-limit banner's "Auto-continue when limits reset" choice: one /// preference for every chat, on by default, kept in shared preferences so a /// reload remembers it. class ChatAutoContinue extends ChangeNotifier { ChatAutoContinue({this.preferences}); /// The instance chats use unless a test hands them their own. static final ChatAutoContinue shared = ChatAutoContinue( preferences: SharedPreferences.getInstance, ); /// Where the choice is kept; null keeps it in memory. final Future Function()? preferences; bool _enabled = true; bool _loaded = false; Future? _loading; bool get enabled => _enabled; /// Whether the stored choice has been read, so nothing is scheduled on the /// default before an operator's "off" is known. bool get loaded => _loaded; /// Reads the stored choice once. Future load() => _loading ??= _read(); Future _read() async { final preferences = this.preferences; try { if (preferences != null) { final stored = (await preferences()).getBool( chatAutoContinuePreferenceKey, ); // A choice made while reading wins. if (stored != null && !_loaded) _enabled = stored; } } on Object catch (error) { // Without storage the default holds for this app session. debugPrint('Auto-continue: could not read preferences: $error'); } _loaded = true; notifyListeners(); } /// Records the operator's choice. Future set(bool enabled) async { _loaded = true; if (enabled == _enabled) return; _enabled = enabled; notifyListeners(); final preferences = this.preferences; if (preferences == null) return; try { await (await preferences()).setBool( chatAutoContinuePreferenceKey, enabled, ); } on Object catch (error) { debugPrint('Auto-continue: could not save preferences: $error'); } } } /// The "Auto-continue when limits reset" choice the server keeps for one /// chat (`chat_auto_continue`): per Claude home, or the server's own for a /// chat without one. A server that keeps it also schedules the continue /// prompt itself, so the app only shows it. An older server does not answer /// with a choice; then [unsupported] is set and the per-device /// [ChatAutoContinue] preference, and the app's own scheduling, apply as /// before. class ChatServerAutoContinue extends ChangeNotifier { ChatServerAutoContinue({required this.gateway, required this.sessionId}); final ApiGateway gateway; final String sessionId; bool? _enabled; bool _unsupported = false; bool _disposed = false; Future? _loading; /// Whether the server answered with its choice. bool get supported => _enabled != null && !_unsupported; /// Whether the server does not keep the choice (an older server). bool get unsupported => _unsupported; /// Whether the server's answer (or refusal) arrived. bool get loaded => supported || _unsupported; /// The server's choice; on until it says otherwise. bool get enabled => _enabled ?? true; /// Reads the server's choice once. Future load() => _loading ??= _read(); Future _read() async { try { _enabled = _choice(await _call(null)); _unsupported = _enabled == null; } on Object { // An older server does not know the request. _unsupported = true; } _notify(); } /// Records the operator's choice on the server; the shown choice goes /// back when the server does not record it. Future set(bool on) async { final previous = _enabled; _enabled = on; _notify(); try { final recorded = _choice(await _call(on)); if (recorded == null) throw StateError('no auto_continue in the reply'); _enabled = recorded; } on Object catch (error) { debugPrint('Auto-continue: the server did not record it: $error'); _enabled = previous; } _notify(); } Future _call(bool? on) => gateway.call('chat_auto_continue', { 'chat_auto_continue': {'session_id': sessionId, 'auto_continue': ?on}, }); static bool? _choice(JsonMap reply) { final body = reply['chat_auto_continue']; final value = body is Map ? body['auto_continue'] : null; return value is bool ? value : null; } void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } }