@@ -50,19 +50,8 @@ class ChatViewmodel extends StateNotifier<ChatState> {
50
50
}) async {
51
51
final ai = _selectedAIModel;
52
52
if (text.trim ().isEmpty && countAsUser) return ;
53
- if (ai == null &&
54
- type != ChatMessageType .importCurl &&
55
- type != ChatMessageType .importOpenApi) {
56
- debugPrint ('[Chat] No AI model configured' );
57
- _appendSystem (
58
- 'AI model is not configured. Please set one.' ,
59
- type,
60
- );
61
- return ;
62
- }
63
53
64
54
final requestId = _currentRequest? .id ?? 'global' ;
65
- final existingMessages = state.chatSessions[requestId] ?? const [];
66
55
67
56
if (countAsUser) {
68
57
_addMessage (
@@ -77,6 +66,19 @@ class ChatViewmodel extends StateNotifier<ChatState> {
77
66
);
78
67
}
79
68
69
+ if (ai == null &&
70
+ type != ChatMessageType .importCurl &&
71
+ type != ChatMessageType .importOpenApi) {
72
+ debugPrint ('[Chat] No AI model configured' );
73
+ _appendSystem (
74
+ 'AI model is not configured. Please set one.' ,
75
+ type,
76
+ );
77
+ return ;
78
+ }
79
+
80
+ final existingMessages = state.chatSessions[requestId] ?? const [];
81
+
80
82
final lastSystemImport = existingMessages.lastWhere (
81
83
(m) =>
82
84
m.role == MessageRole .system &&
@@ -409,7 +411,6 @@ class ChatViewmodel extends StateNotifier<ChatState> {
409
411
}
410
412
}
411
413
412
-
413
414
String sourceTitle = (payload['sourceName' ] as String ? ) ?? '' ;
414
415
if (sourceTitle.trim ().isEmpty) {
415
416
final specObj = payload['spec' ];
@@ -931,12 +932,10 @@ class ChatViewmodel extends StateNotifier<ChatState> {
931
932
// Helpers
932
933
void _addMessage (String requestId, ChatMessage m) {
933
934
final msgs = state.chatSessions[requestId] ?? const [];
934
- state = state.copyWith (
935
- chatSessions: {
936
- ...state.chatSessions,
937
- requestId: [...msgs, m],
938
- },
939
- );
935
+ final updatedSessions =
936
+ Map <String , List <ChatMessage >>.from (state.chatSessions);
937
+ updatedSessions[requestId] = [...msgs, m];
938
+ state = state.copyWith (chatSessions: updatedSessions);
940
939
}
941
940
942
941
void _appendSystem (String text, ChatMessageType type) {
0 commit comments