From fe09583c6bf6d88ec16df73405271ba0b433a202 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Sat, 1 Nov 2025 15:21:34 +0530 Subject: [PATCH] feat(api): Improve file and prompt parsing with type safety --- lib/core/services/api_service.dart | 38 ++++++++++++++++++++------ lib/core/services/prompts_service.dart | 7 +---- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/lib/core/services/api_service.dart b/lib/core/services/api_service.dart index d010765..fd5d2f0 100644 --- a/lib/core/services/api_service.dart +++ b/lib/core/services/api_service.dart @@ -15,6 +15,7 @@ import '../models/conversation.dart'; import '../models/chat_message.dart'; import '../models/file_info.dart'; import '../models/knowledge_base.dart'; +import '../models/prompt.dart'; import '../auth/api_auth_interceptor.dart'; import '../error/api_error_interceptor.dart'; // Tool-call details are parsed in the UI layer to render collapsible blocks @@ -1146,7 +1147,7 @@ class ApiService { } // Enhanced File Operations - Future>> searchFiles({ + Future> searchFiles({ String? query, String? contentType, int? limit, @@ -1165,19 +1166,31 @@ class ApiService { ); final data = response.data; if (data is List) { - return data.cast>(); + final normalized = await _normalizeList( + data, + debugLabel: 'parse_file_search', + ); + return normalized + .map(FileInfo.fromJson) + .toList(growable: false); } - return []; + return const []; } - Future>> getAllFiles() async { + Future> getAllFiles() async { _traceApi('Fetching all files (admin)'); final response = await _dio.get('/api/v1/files/all'); final data = response.data; if (data is List) { - return data.cast>(); + final normalized = await _normalizeList( + data, + debugLabel: 'parse_file_all', + ); + return normalized + .map(FileInfo.fromJson) + .toList(growable: false); } - return []; + return const []; } Future uploadFileWithProgress( @@ -1734,14 +1747,21 @@ class ApiService { } // Prompts - Future>> getPrompts() async { + Future> getPrompts() async { _traceApi('Fetching prompts'); final response = await _dio.get('/api/v1/prompts/'); final data = response.data; if (data is List) { - return data.cast>(); + final normalized = await _normalizeList( + data, + debugLabel: 'parse_prompts', + ); + return normalized + .map(Prompt.fromJson) + .where((prompt) => prompt.command.isNotEmpty) + .toList(growable: false); } - return []; + return const []; } // Permissions & Features diff --git a/lib/core/services/prompts_service.dart b/lib/core/services/prompts_service.dart index 2edad0d..2992ee3 100644 --- a/lib/core/services/prompts_service.dart +++ b/lib/core/services/prompts_service.dart @@ -13,12 +13,7 @@ class PromptsService { Future> getPrompts() async { try { - final List> response = await _apiService - .getPrompts(); - return response - .map((item) => Prompt.fromJson(item)) - .where((prompt) => prompt.command.isNotEmpty) - .toList(); + return await _apiService.getPrompts(); } on DioException catch (error) { throw ApiErrorHandler().transformError(error); }