feat(api): Implement pagination for conversation summaries retrieval
This commit is contained in:
@@ -1131,17 +1131,44 @@ class ApiService {
|
|||||||
String folderId,
|
String folderId,
|
||||||
) async {
|
) async {
|
||||||
_traceApi('Fetching conversation summaries in folder: $folderId');
|
_traceApi('Fetching conversation summaries in folder: $folderId');
|
||||||
final response = await _dio.get('/api/v1/chats/folder/$folderId/list');
|
|
||||||
final data = response.data;
|
// The backend endpoint has a hardcoded limit of 10 items per page,
|
||||||
if (data is! List) {
|
// so we need to paginate through all pages to get all conversations.
|
||||||
return const [];
|
final List<Map<String, dynamic>> allChats = [];
|
||||||
|
int currentPage = 1;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
final response = await _dio.get(
|
||||||
|
'/api/v1/chats/folder/$folderId/list',
|
||||||
|
queryParameters: {'page': currentPage},
|
||||||
|
);
|
||||||
|
|
||||||
|
final data = response.data;
|
||||||
|
if (data is! List || data.isEmpty) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
allChats.addAll(data.whereType<Map<String, dynamic>>());
|
||||||
|
currentPage++;
|
||||||
|
|
||||||
|
// Safety limit to prevent infinite loops (matches getConversations)
|
||||||
|
if (currentPage > 100) {
|
||||||
|
_traceApi(
|
||||||
|
'WARNING: Reached maximum page limit (100) for folder $folderId',
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
final normalized = data
|
|
||||||
.whereType<Map<String, dynamic>>()
|
_traceApi(
|
||||||
|
'Fetched ${allChats.length} conversations in folder $folderId '
|
||||||
|
'across ${currentPage - 1} pages',
|
||||||
|
);
|
||||||
|
|
||||||
|
return allChats
|
||||||
.map(parseConversationSummary)
|
.map(parseConversationSummary)
|
||||||
.map(Conversation.fromJson)
|
.map(Conversation.fromJson)
|
||||||
.toList(growable: false);
|
.toList(growable: false);
|
||||||
return normalized;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
@@ -2838,7 +2865,9 @@ class ApiService {
|
|||||||
} else if (respData['error'] != null) {
|
} else if (respData['error'] != null) {
|
||||||
_traceApi('Server error: ${respData['error']}');
|
_traceApi('Server error: ${respData['error']}');
|
||||||
if (!streamController.isClosed) {
|
if (!streamController.isClosed) {
|
||||||
streamController.addError(Exception(respData['error'].toString()));
|
streamController.addError(
|
||||||
|
Exception(respData['error'].toString()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user