Retry agent skill loading after transient errors
This commit is contained in:
+38
-7
@@ -19,6 +19,7 @@ class ChatCubit extends Cubit<ChatState> {
|
||||
final LocalSettingsStore _settingsStore;
|
||||
Timer? _elapsedTimer;
|
||||
Stopwatch? _requestStopwatch;
|
||||
Future<void>? _loadingAgentSkills;
|
||||
int _requestGeneration = 0;
|
||||
List<ChatMessage> _messagesBeforeRequest = const [];
|
||||
bool _requestAppendedUserMessage = true;
|
||||
@@ -196,14 +197,44 @@ class ChatCubit extends Cubit<ChatState> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadAgentSkills() async {
|
||||
try {
|
||||
final available = await _api.getAgentSkills();
|
||||
if (!isClosed) emit(state.copyWith(availableSkills: available));
|
||||
} catch (error) {
|
||||
if (!isClosed) {
|
||||
emit(state.copyWith(error: 'تعذر تحميل مهارات الوكيل: $error'));
|
||||
Future<void> _loadAgentSkills() {
|
||||
final pending = _loadingAgentSkills;
|
||||
if (pending != null) return pending;
|
||||
final loading = _loadAgentSkillsWithRetry();
|
||||
_loadingAgentSkills = loading;
|
||||
return loading.whenComplete(() {
|
||||
if (identical(_loadingAgentSkills, loading)) {
|
||||
_loadingAgentSkills = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _loadAgentSkillsWithRetry() async {
|
||||
const delays = [Duration(milliseconds: 300), Duration(milliseconds: 750)];
|
||||
Object? lastError;
|
||||
for (var attempt = 0; attempt <= delays.length; attempt++) {
|
||||
try {
|
||||
final available = await _api.getAgentSkills();
|
||||
if (!isClosed) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
availableSkills: available,
|
||||
clearError:
|
||||
state.error?.startsWith('تعذر تحميل مهارات الوكيل:') ?? false,
|
||||
),
|
||||
);
|
||||
}
|
||||
return;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (attempt < delays.length) {
|
||||
await Future<void>.delayed(delays[attempt]);
|
||||
if (isClosed) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isClosed) {
|
||||
emit(state.copyWith(error: 'تعذر تحميل مهارات الوكيل: $lastError'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user