From 75b744d061ea3a3640bec5bc9faa67309b6ce11e Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Sun, 28 Sep 2025 14:17:27 +0530 Subject: [PATCH] fix: status history parsing --- lib/core/models/chat_message.dart | 60 ++++++++++++++++++------ lib/core/services/api_service.dart | 75 ++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 30 deletions(-) diff --git a/lib/core/models/chat_message.dart b/lib/core/models/chat_message.dart index e879121..2d36041 100644 --- a/lib/core/models/chat_message.dart +++ b/lib/core/models/chat_message.dart @@ -157,11 +157,21 @@ List _statusItemsFromJson(dynamic value) { if (value is List) { return value .whereType() - .map( - (item) => ChatStatusItem.fromJson( - item.map((key, v) => MapEntry(key.toString(), v)), - ), - ) + .map((item) { + try { + // Convert Map to Map safely + final Map itemMap = {}; + item.forEach((key, v) { + itemMap[key.toString()] = v; + }); + return ChatStatusItem.fromJson(itemMap); + } catch (e) { + // Skip invalid entries + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const []; @@ -175,11 +185,21 @@ List _executionFilesFromJson(dynamic value) { if (value is List) { return value .whereType() - .map( - (item) => ChatExecutionFile.fromJson( - item.map((key, v) => MapEntry(key.toString(), v)), - ), - ) + .map((item) { + try { + // Convert Map to Map safely + final Map fileMap = {}; + item.forEach((key, v) { + fileMap[key.toString()] = v; + }); + return ChatExecutionFile.fromJson(fileMap); + } catch (e) { + // Skip invalid entries + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const []; @@ -195,11 +215,21 @@ List _sourceRefsFromJson(dynamic value) { if (value is List) { return value .whereType() - .map( - (item) => ChatSourceReference.fromJson( - item.map((key, v) => MapEntry(key.toString(), v)), - ), - ) + .map((item) { + try { + // Convert Map to Map safely + final Map refMap = {}; + item.forEach((key, v) { + refMap[key.toString()] = v; + }); + return ChatSourceReference.fromJson(refMap); + } catch (e) { + // Skip invalid entries + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const []; diff --git a/lib/core/services/api_service.dart b/lib/core/services/api_service.dart index 4dcab27..d36b0b8 100644 --- a/lib/core/services/api_service.dart +++ b/lib/core/services/api_service.dart @@ -1198,11 +1198,26 @@ class ApiService { if (raw is List) { return raw .whereType() - .map( - (entry) => ChatStatusUpdate.fromJson( - entry.map((key, value) => MapEntry(key.toString(), value)), - ), - ) + .map((entry) { + try { + // Convert Map to Map safely + final Map statusMap = {}; + entry.forEach((key, value) { + statusMap[key.toString()] = value; + }); + return ChatStatusUpdate.fromJson(statusMap); + } catch (e) { + // Log the error and skip this entry + DebugLogger.log( + 'status-parse-error', + scope: 'api/chat', + data: {'error': e.toString(), 'entry': entry.toString()}, + ); + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const []; @@ -1226,11 +1241,26 @@ class ApiService { if (raw is List) { return raw .whereType() - .map( - (entry) => ChatCodeExecution.fromJson( - entry.map((key, value) => MapEntry(key.toString(), value)), - ), - ) + .map((entry) { + try { + // Convert Map to Map safely + final Map execMap = {}; + entry.forEach((key, value) { + execMap[key.toString()] = value; + }); + return ChatCodeExecution.fromJson(execMap); + } catch (e) { + // Log the error and skip this entry + DebugLogger.log( + 'code-exec-parse-error', + scope: 'api/chat', + data: {'error': e.toString(), 'entry': entry.toString()}, + ); + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const []; @@ -1240,11 +1270,26 @@ class ApiService { if (raw is List) { return raw .whereType() - .map( - (entry) => ChatSourceReference.fromJson( - entry.map((key, value) => MapEntry(key.toString(), value)), - ), - ) + .map((entry) { + try { + // Convert Map to Map safely + final Map sourceMap = {}; + entry.forEach((key, value) { + sourceMap[key.toString()] = value; + }); + return ChatSourceReference.fromJson(sourceMap); + } catch (e) { + // Log the error and skip this entry + DebugLogger.log( + 'source-parse-error', + scope: 'api/chat', + data: {'error': e.toString(), 'entry': entry.toString()}, + ); + return null; + } + }) + .where((item) => item != null) + .cast() .toList(growable: false); } return const [];