From 6ed8e00488f0b38fa634be9c5cdf203c23243707 Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Sat, 6 Sep 2025 01:37:26 +0530 Subject: [PATCH] feat: add quick auto add test to postRequestScript feature --- .../features/chat/models/chat_models.dart | 5 ++- .../chat/view/widgets/chat_bubble.dart | 44 ++++++++++++------- .../chat/viewmodel/chat_viewmodel.dart | 43 +++++++++++++++++- 3 files changed, 71 insertions(+), 21 deletions(-) diff --git a/lib/dashbot/features/chat/models/chat_models.dart b/lib/dashbot/features/chat/models/chat_models.dart index cb1cda60..12e995a8 100644 --- a/lib/dashbot/features/chat/models/chat_models.dart +++ b/lib/dashbot/features/chat/models/chat_models.dart @@ -103,7 +103,7 @@ class ChatAction { const ChatAction({ required this.action, required this.target, - required this.field, + this.field = '', // Default to empty string this.path, this.value, }); @@ -112,7 +112,8 @@ class ChatAction { return ChatAction( action: json['action'] as String, target: json['target'] as String, - field: json['field'] as String, + field: json['field'] as String? ?? + '', // Default to empty string if not provided path: json['path'] as String?, value: json['value'], ); diff --git a/lib/dashbot/features/chat/view/widgets/chat_bubble.dart b/lib/dashbot/features/chat/view/widgets/chat_bubble.dart index 6539d50b..457616f3 100644 --- a/lib/dashbot/features/chat/view/widgets/chat_bubble.dart +++ b/lib/dashbot/features/chat/view/widgets/chat_bubble.dart @@ -107,23 +107,7 @@ class ChatBubble extends ConsumerWidget { if (role == MessageRole.system) ...[ if (action != null) ...[ const SizedBox(height: 4), - ElevatedButton.icon( - onPressed: () async { - final chatViewmodel = - ref.read(chatViewmodelProvider.notifier); - await chatViewmodel.applyAutoFix(action!); - debugPrint('Auto-fix applied successfully!'); - }, - icon: const Icon(Icons.auto_fix_high, size: 16), - label: const Text('Auto Fix'), - style: ElevatedButton.styleFrom( - backgroundColor: Theme.of(context).colorScheme.primary, - foregroundColor: Theme.of(context).colorScheme.onPrimary, - padding: - const EdgeInsets.symmetric(horizontal: 12, vertical: 6), - textStyle: Theme.of(context).textTheme.labelSmall, - ), - ), + _buildActionButton(context, ref, action!), ], const SizedBox(height: 4), IconButton( @@ -140,4 +124,30 @@ class ChatBubble extends ConsumerWidget { ), ); } + + Widget _buildActionButton( + BuildContext context, WidgetRef ref, ChatAction action) { + final isTestAction = action.action == 'other' && action.target == 'test'; + + return ElevatedButton.icon( + onPressed: () async { + final chatViewmodel = ref.read(chatViewmodelProvider.notifier); + await chatViewmodel.applyAutoFix(action); + if (isTestAction) { + debugPrint('Test added to post-request script successfully!'); + } else { + debugPrint('Auto-fix applied successfully!'); + } + }, + icon: Icon(isTestAction ? Icons.playlist_add_check : Icons.auto_fix_high, + size: 16), + label: Text(isTestAction ? 'Add Test' : 'Auto Fix'), + style: ElevatedButton.styleFrom( + backgroundColor: Theme.of(context).colorScheme.primary, + foregroundColor: Theme.of(context).colorScheme.onPrimary, + padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), + textStyle: Theme.of(context).textTheme.labelSmall, + ), + ); + } } diff --git a/lib/dashbot/features/chat/viewmodel/chat_viewmodel.dart b/lib/dashbot/features/chat/viewmodel/chat_viewmodel.dart index 2e27eb70..c5eb87fd 100644 --- a/lib/dashbot/features/chat/viewmodel/chat_viewmodel.dart +++ b/lib/dashbot/features/chat/viewmodel/chat_viewmodel.dart @@ -153,8 +153,6 @@ class ChatViewmodel extends StateNotifier { if (parsed.containsKey('action') && parsed['action'] != null) { fallbackAction = ChatAction.fromJson( parsed['action'] as Map); - debugPrint( - '[Chat] Fallback parsed action: ${fallbackAction.toJson()}'); } } catch (e) { debugPrint('[Chat] Fallback error parsing action: $e'); @@ -220,6 +218,9 @@ class ChatViewmodel extends StateNotifier { case 'update_method': await _applyMethodUpdate(action); break; + case 'other': + await _applyOtherAction(action); + break; default: debugPrint('[Chat] Unsupported action: ${action.action}'); } @@ -333,6 +334,44 @@ class ChatViewmodel extends StateNotifier { collectionNotifier.update(method: method, id: requestId); } + Future _applyOtherAction(ChatAction action) async { + final requestId = _currentRequest?.id; + if (requestId == null) return; + + switch (action.target) { + case 'test': + await _applyTestToPostScript(action); + break; + default: + debugPrint('[Chat] Unsupported other action target: ${action.target}'); + } + } + + Future _applyTestToPostScript(ChatAction action) async { + final requestId = _currentRequest?.id; + if (requestId == null) return; + + final collectionNotifier = + _ref.read(collectionStateNotifierProvider.notifier); + final testCode = action.value as String; + + // Get the current post-request script (if any) + final currentRequest = _currentRequest; + final currentPostScript = currentRequest?.postRequestScript ?? ''; + + // Append the test code to the existing post-request script + final newPostScript = currentPostScript.isEmpty + ? testCode + : '$currentPostScript\n\n// Generated Test\n$testCode'; + + collectionNotifier.update(postRequestScript: newPostScript, id: requestId); + + debugPrint('[Chat] Test code added to post-request script'); + _appendSystem( + 'Test code has been successfully added to the post-request script.', + ChatMessageType.generateTest); + } + // Helpers void _addMessage(String requestId, ChatMessage m) { debugPrint(