Files
pipelines/app/lib/features/chat/conversation/chat_auto_continue.dart
T
alan 633b5953bb Chat: the server continues a usage-refused Claude chat at the reset
When Claude Code refuses an operator chat's turn for a usage limit and the
server does not continue it on another account, the server now queues a
durable prompt (auto-continue-<refusal id>, sent as Pipelines (automatic))
for a minute after the reset: the refused operator message when the turn
did no work, else "continue". The reset comes from the refusal's text,
else the rejected rate_limit_event. Refusals of turns Claude Code started
itself are handled too, with no client attached; a restarted server or a
device scheduling the same refusal adds nothing. The choice is kept per
Claude home beside auto-switch (or by the server for chats without one),
read and set with chat_auto_continue, on by default.

The app's limit banner now finds the refusal as the latest turn, ignoring
notices, agent reports and activity rows after it, reads and writes the
server's choice, and only schedules its own prompt for older servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 19:52:03 +00:00

155 lines
4.6 KiB
Dart

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<SharedPreferences> Function()? preferences;
bool _enabled = true;
bool _loaded = false;
Future<void>? _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<void> load() => _loading ??= _read();
Future<void> _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<void> 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<void>? _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<void> load() => _loading ??= _read();
Future<void> _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<void> 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<JsonMap> _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();
}
}