mirror of
https://github.com/foss42/apidash.git
synced 2025-12-09 23:02:21 +08:00
Refactor DashBot
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
export 'dashbot_add_test_button.dart';
|
||||
export 'dashbot_apply_curl_button.dart';
|
||||
export 'dashbot_auto_fix_button.dart';
|
||||
export 'dashbot_download_doc_button.dart';
|
||||
export 'dashbot_generate_codeblock.dart';
|
||||
export 'dashbot_generate_language_picker_button.dart';
|
||||
export 'dashbot_import_now_button.dart';
|
||||
export 'dashbot_select_operation_button.dart';
|
||||
export 'dashbot_upload_requests_button.dart';
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class DashbotAddTestButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotAddTestButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return ElevatedButton.icon(
|
||||
onPressed: () async {
|
||||
await ref.read(chatViewmodelProvider.notifier).applyAutoFix(action);
|
||||
},
|
||||
icon: const Icon(Icons.playlist_add_check, size: 16),
|
||||
label: const Text('Add Test'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class DashbotApplyCurlButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotApplyCurlButton({super.key, required this.action});
|
||||
|
||||
String _labelForField(String? field, String? path) {
|
||||
switch (field) {
|
||||
case 'apply_to_selected':
|
||||
return 'Apply to Selected';
|
||||
case 'apply_to_new':
|
||||
return 'Create New Request';
|
||||
case 'select_operation':
|
||||
return path == null || path.isEmpty ? 'Select Operation' : path;
|
||||
default:
|
||||
return 'Apply';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final label = _labelForField(action.field, action.path);
|
||||
final isDestructive = action.field == 'apply_to_selected';
|
||||
return ElevatedButton(
|
||||
onPressed: () async {
|
||||
await ref.read(chatViewmodelProvider.notifier).applyAutoFix(action);
|
||||
},
|
||||
child: Text(
|
||||
label,
|
||||
// Destructive action: highlight with error color
|
||||
style: isDestructive
|
||||
? TextStyle(color: Theme.of(context).colorScheme.error)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotApplyOpenApiButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotApplyOpenApiButton({super.key, required this.action});
|
||||
|
||||
String _labelForField(String? field) {
|
||||
switch (field) {
|
||||
case 'apply_to_selected':
|
||||
return 'Apply to Selected';
|
||||
case 'apply_to_new':
|
||||
return 'Create New Request';
|
||||
default:
|
||||
return 'Apply';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final label = _labelForField(action.field);
|
||||
return ElevatedButton(
|
||||
onPressed: () async {
|
||||
await ref.read(chatViewmodelProvider.notifier).applyAutoFix(action);
|
||||
},
|
||||
child: Text(label),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
class DashbotAutoFixButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotAutoFixButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return ElevatedButton.icon(
|
||||
onPressed: () async {
|
||||
await ref.read(chatViewmodelProvider.notifier).applyAutoFix(action);
|
||||
},
|
||||
icon: const Icon(Icons.auto_fix_high, size: 16),
|
||||
label: const Text('Auto Fix'),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:apidash/utils/utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotDownloadDocButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotDownloadDocButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final docContent = (action.value is String) ? action.value as String : '';
|
||||
final filename = action.path ?? 'api-documentation';
|
||||
|
||||
return ElevatedButton.icon(
|
||||
icon: const Icon(Icons.download, size: 16),
|
||||
label: const Text('Download Documentation'),
|
||||
onPressed: docContent.isEmpty
|
||||
? null
|
||||
: () async {
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
final contentBytes = Uint8List.fromList(docContent.codeUnits);
|
||||
|
||||
await saveToDownloads(
|
||||
scaffoldMessenger,
|
||||
content: contentBytes,
|
||||
mimeType: 'text/markdown',
|
||||
ext: 'md',
|
||||
name: filename,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotGeneratedCodeBlock extends StatefulWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotGeneratedCodeBlock({super.key, required this.action});
|
||||
|
||||
@override
|
||||
State<DashbotGeneratedCodeBlock> createState() =>
|
||||
_DashbotGeneratedCodeBlockState();
|
||||
}
|
||||
|
||||
class _DashbotGeneratedCodeBlockState extends State<DashbotGeneratedCodeBlock> {
|
||||
bool _isCopied = false;
|
||||
|
||||
Future<void> _copyCode(String code) async {
|
||||
await Clipboard.setData(ClipboardData(text: code));
|
||||
setState(() {
|
||||
_isCopied = true;
|
||||
});
|
||||
|
||||
// Reset the icon back to copy after 1.5 seconds
|
||||
Future.delayed(const Duration(milliseconds: 1500), () {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isCopied = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final code =
|
||||
(widget.action.value is String) ? widget.action.value as String : '';
|
||||
final isDark = Theme.of(context).brightness == Brightness.dark;
|
||||
final codeTheme = isDark ? kDarkCodeTheme : kLightCodeTheme;
|
||||
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: codeTheme['root']?.backgroundColor ??
|
||||
Theme.of(context).colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: Theme.of(context).colorScheme.outlineVariant,
|
||||
),
|
||||
),
|
||||
child: Stack(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: code.isNotEmpty ? () => _copyCode(code) : null,
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Text(
|
||||
code.isEmpty ? '// No code returned' : code,
|
||||
style: kCodeStyle.copyWith(
|
||||
fontSize: Theme.of(context).textTheme.bodySmall?.fontSize,
|
||||
color: codeTheme['root']?.color ??
|
||||
Theme.of(context).colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (code.isNotEmpty)
|
||||
Positioned(
|
||||
top: 8,
|
||||
right: 8,
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: ADIconButton(
|
||||
key: ValueKey(_isCopied),
|
||||
icon: _isCopied ? Icons.check : Icons.content_copy,
|
||||
iconSize: 16,
|
||||
tooltip: _isCopied ? 'Copied!' : 'Copy',
|
||||
color: _isCopied
|
||||
? Theme.of(context).colorScheme.primary
|
||||
: (codeTheme['root']?.color ??
|
||||
Theme.of(context).colorScheme.onSurface)
|
||||
.withValues(alpha: 0.6),
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => _copyCode(code),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../constants.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotGenerateLanguagePicker extends ConsumerWidget
|
||||
with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotGenerateLanguagePicker({super.key, required this.action});
|
||||
|
||||
List<String> _extractLanguages(dynamic value) {
|
||||
if (value is List) {
|
||||
return value.whereType<String>().toList();
|
||||
}
|
||||
return const [
|
||||
'JavaScript (fetch)',
|
||||
'Python (requests)',
|
||||
'Dart (http)',
|
||||
'Go (net/http)',
|
||||
'cURL',
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final langs = _extractLanguages(action.value);
|
||||
return Wrap(
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: [
|
||||
for (final l in langs)
|
||||
OutlinedButton(
|
||||
onPressed: () {
|
||||
ref.read(chatViewmodelProvider.notifier).sendMessage(
|
||||
text: 'Please generate code in $l',
|
||||
type: ChatMessageType.generateCode,
|
||||
);
|
||||
},
|
||||
child: Text(l, style: const TextStyle(fontSize: 12)),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'dart:developer';
|
||||
import 'package:apidash_core/apidash_core.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../../services/services.dart';
|
||||
import '../dashbot_action.dart';
|
||||
import '../openapi_operation_picker_dialog.dart';
|
||||
|
||||
class DashbotImportNowButton extends ConsumerWidget with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotImportNowButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
return FilledButton.icon(
|
||||
icon: const Icon(Icons.playlist_add_check, size: 16),
|
||||
label: const Text('Import Now'),
|
||||
onPressed: () async {
|
||||
try {
|
||||
OpenApi? spec;
|
||||
String? sourceName;
|
||||
final overlayNotifier =
|
||||
ref.read(dashbotWindowNotifierProvider.notifier);
|
||||
final chatNotifier = ref.read(chatViewmodelProvider.notifier);
|
||||
if (action.value is Map<String, dynamic>) {
|
||||
final map = action.value as Map<String, dynamic>;
|
||||
sourceName = map['sourceName'] as String?;
|
||||
if (map['spec'] is OpenApi) {
|
||||
spec = map['spec'] as OpenApi;
|
||||
} else if (map['content'] is String) {
|
||||
spec =
|
||||
OpenApiImportService.tryParseSpec(map['content'] as String);
|
||||
}
|
||||
}
|
||||
if (spec == null) return;
|
||||
|
||||
final servers = spec.servers ?? const [];
|
||||
final baseUrl = servers.isNotEmpty ? (servers.first.url ?? '/') : '/';
|
||||
overlayNotifier.hide();
|
||||
final selected = await showOpenApiOperationPickerDialog(
|
||||
context: context,
|
||||
spec: spec,
|
||||
sourceName: sourceName,
|
||||
);
|
||||
overlayNotifier.show();
|
||||
if (selected == null || selected.isEmpty) return;
|
||||
for (final s in selected) {
|
||||
final payload = OpenApiImportService.payloadForOperation(
|
||||
baseUrl: baseUrl,
|
||||
path: s.path,
|
||||
method: s.method,
|
||||
op: s.op,
|
||||
);
|
||||
log("SorceName: $sourceName");
|
||||
payload['sourceName'] =
|
||||
(sourceName != null && sourceName.trim().isNotEmpty)
|
||||
? sourceName
|
||||
: spec.info.title;
|
||||
await chatNotifier.applyAutoFix(ChatAction.fromJson({
|
||||
'action': 'apply_openapi',
|
||||
'actionType': 'apply_openapi',
|
||||
'target': 'httpRequestModel',
|
||||
'targetType': 'httpRequestModel',
|
||||
'field': 'apply_to_new',
|
||||
'value': payload,
|
||||
}));
|
||||
}
|
||||
} catch (_) {}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotSelectOperationButton extends ConsumerWidget
|
||||
with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotSelectOperationButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final operationName = action.path ?? 'Unknown';
|
||||
return OutlinedButton(
|
||||
onPressed: () async {
|
||||
await ref.read(chatViewmodelProvider.notifier).applyAutoFix(action);
|
||||
},
|
||||
child: Text(operationName, style: const TextStyle(fontSize: 12)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:file_selector/file_selector.dart';
|
||||
import '../../constants.dart';
|
||||
import '../../models/models.dart';
|
||||
import '../../providers/providers.dart';
|
||||
import '../dashbot_action.dart';
|
||||
|
||||
class DashbotUploadRequestButton extends ConsumerWidget
|
||||
with DashbotActionMixin {
|
||||
@override
|
||||
final ChatAction action;
|
||||
const DashbotUploadRequestButton({super.key, required this.action});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final label = action.value is Map && (action.value['purpose'] is String)
|
||||
? 'Upload: ${action.value['purpose'] as String}'
|
||||
: 'Upload Attachment';
|
||||
return OutlinedButton.icon(
|
||||
icon: const Icon(Icons.upload_file, size: 16),
|
||||
label: Text(label, overflow: TextOverflow.ellipsis),
|
||||
onPressed: () async {
|
||||
final types = <XTypeGroup>[];
|
||||
if (action.value is Map && action.value['accepted_types'] is List) {
|
||||
final exts = (action.value['accepted_types'] as List)
|
||||
.whereType<String>()
|
||||
.map((e) => e.trim())
|
||||
.toList();
|
||||
if (exts.isNotEmpty) {
|
||||
types.add(XTypeGroup(label: 'Allowed', mimeTypes: exts));
|
||||
}
|
||||
}
|
||||
final file = await openFile(
|
||||
acceptedTypeGroups:
|
||||
types.isEmpty ? [const XTypeGroup(label: 'Any')] : types);
|
||||
if (file == null) return;
|
||||
final bytes = await file.readAsBytes();
|
||||
final att = ref.read(attachmentsProvider.notifier).add(
|
||||
name: file.name,
|
||||
mimeType: file.mimeType ?? 'application/octet-stream',
|
||||
data: bytes,
|
||||
);
|
||||
if (action.field == 'openapi_spec') {
|
||||
await ref
|
||||
.read(chatViewmodelProvider.notifier)
|
||||
.handleOpenApiAttachment(att);
|
||||
} else {
|
||||
ref.read(chatViewmodelProvider.notifier).sendMessage(
|
||||
text:
|
||||
'Attached file ${att.name} (id=${att.id}, mime=${att.mimeType}, size=${att.sizeBytes}). You can request its content if needed.',
|
||||
type: ChatMessageType.general,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user