feat(api): Improve default model selection with fallback mechanism

This commit is contained in:
cogwheel
2026-02-05 13:19:59 +05:30
parent b1ef5803dc
commit 4b8e0752e5

View File

@@ -794,45 +794,67 @@ class ApiService {
DebugLogger.log('settings-ok', scope: 'api/user-settings'); DebugLogger.log('settings-ok', scope: 'api/user-settings');
final data = response.data; final data = response.data;
if (data is! Map<String, dynamic>) { if (data is Map<String, dynamic>) {
DebugLogger.warning( // Extract default model from ui.models array
'settings-format', final ui = data['ui'];
scope: 'api/user-settings', if (ui is Map<String, dynamic>) {
data: {'type': data.runtimeType}, final models = ui['models'];
); if (models is List && models.isNotEmpty) {
return null; // Return the first model in the user's preferred models list
} final defaultModel = models.first.toString();
DebugLogger.log(
// Extract default model from ui.models array 'default-model',
final ui = data['ui']; scope: 'api/user-settings',
if (ui is Map<String, dynamic>) { data: {'id': defaultModel},
final models = ui['models']; );
if (models is List && models.isNotEmpty) { return defaultModel;
// Return the first model in the user's preferred models list }
final defaultModel = models.first.toString();
DebugLogger.log(
'default-model',
scope: 'api/user-settings',
data: {'id': defaultModel},
);
return defaultModel;
} }
} }
DebugLogger.warning('default-model-missing', scope: 'api/user-settings'); // Fallback: user has no default model configured, pick first available
return null; // This fixes issue #353 where secondary accounts couldn't send messages
DebugLogger.log(
'default-model-fallback',
scope: 'api/user-settings',
);
return _getFirstAvailableModelId();
} catch (e) { } catch (e) {
DebugLogger.error( DebugLogger.error(
'default-model-error', 'default-model-error',
scope: 'api/user-settings', scope: 'api/user-settings',
error: e, error: e,
); );
// Do not call admin-only configs endpoint here; let the caller // Attempt fallback even on error
// handle fallback (e.g., first available model from /api/models). return _getFirstAvailableModelId();
return null;
} }
} }
/// Returns the ID of the first available model, or null if none available.
///
/// Used as a fallback when user has no default model configured.
Future<String?> _getFirstAvailableModelId() async {
try {
final models = await getModels();
if (models.isNotEmpty) {
final fallbackId = models.first.id;
DebugLogger.log(
'default-model-fallback-selected',
scope: 'api/user-settings',
data: {'id': fallbackId},
);
return fallbackId;
}
} catch (e) {
DebugLogger.error(
'default-model-fallback-failed',
scope: 'api/user-settings',
error: e,
);
}
return null;
}
// Conversations - Updated to use correct OpenWebUI API // Conversations - Updated to use correct OpenWebUI API
Future<List<Conversation>> getConversations({int? limit, int? skip}) async { Future<List<Conversation>> getConversations({int? limit, int? skip}) async {
final pinnedFuture = _fetchChatCollection( final pinnedFuture = _fetchChatCollection(