feat: add quick auto add test to postRequestScript feature

This commit is contained in:
Udhay-Adithya
2025-09-06 01:37:26 +05:30
parent c16665aa12
commit 6ed8e00488
3 changed files with 71 additions and 21 deletions

View File

@@ -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'],
);

View File

@@ -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,
),
);
}
}

View File

@@ -153,8 +153,6 @@ class ChatViewmodel extends StateNotifier<ChatState> {
if (parsed.containsKey('action') && parsed['action'] != null) {
fallbackAction = ChatAction.fromJson(
parsed['action'] as Map<String, dynamic>);
debugPrint(
'[Chat] Fallback parsed action: ${fallbackAction.toJson()}');
}
} catch (e) {
debugPrint('[Chat] Fallback error parsing action: $e');
@@ -220,6 +218,9 @@ class ChatViewmodel extends StateNotifier<ChatState> {
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<ChatState> {
collectionNotifier.update(method: method, id: requestId);
}
Future<void> _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<void> _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(