mirror of
https://github.com/foss42/apidash.git
synced 2025-12-08 22:20:44 +08:00
Fix: ChatBot UI Alignment
This commit is contained in:
@@ -69,6 +69,7 @@ Analysis: [structured analysis]''';
|
|||||||
return generateResponse(prompt);
|
return generateResponse(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debugging Failed API Requests
|
||||||
Future<String> debugApi({required dynamic requestModel, required dynamic responseModel}) async {
|
Future<String> debugApi({required dynamic requestModel, required dynamic responseModel}) async {
|
||||||
if (requestModel == null || responseModel == null) {
|
if (requestModel == null || responseModel == null) {
|
||||||
return "There are no recent API Requests to debug.";
|
return "There are no recent API Requests to debug.";
|
||||||
@@ -100,6 +101,7 @@ Analysis: [structured analysis]''';
|
|||||||
return generateResponse(prompt);
|
return generateResponse(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generating test cases for API
|
||||||
Future<String> generateTestCases({required dynamic requestModel, required dynamic responseModel}) async {
|
Future<String> generateTestCases({required dynamic requestModel, required dynamic responseModel}) async {
|
||||||
|
|
||||||
final method = requestModel.httpRequestModel?.method
|
final method = requestModel.httpRequestModel?.method
|
||||||
@@ -136,6 +138,7 @@ here is an example test case for the given:$exampleParams
|
|||||||
return generateResponse(prompt);
|
return generateResponse(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generating Example Programming on API for different languages
|
||||||
Future<Map<String, dynamic>> generateExampleParams({required dynamic requestModel, required dynamic responseModel,}) async {
|
Future<Map<String, dynamic>> generateExampleParams({required dynamic requestModel, required dynamic responseModel,}) async {
|
||||||
final ollamaService = OllamaService();
|
final ollamaService = OllamaService();
|
||||||
|
|
||||||
@@ -195,29 +198,46 @@ generate same to same type of test case url for test purpose
|
|||||||
final headers = requestModel.httpRequestModel?.enabledHeadersMap ?? {};
|
final headers = requestModel.httpRequestModel?.enabledHeadersMap ?? {};
|
||||||
final params = requestModel.httpRequestModel?.enabledParamsMap ?? {};
|
final params = requestModel.httpRequestModel?.enabledParamsMap ?? {};
|
||||||
final body = requestModel.httpRequestModel?.body;
|
final body = requestModel.httpRequestModel?.body;
|
||||||
|
final responseBody = responseModel.body;
|
||||||
final isFrontend = language.contains('(UI)');
|
|
||||||
final baseLanguage = language.replaceAll(' (UI)', '').replaceAll(' (Console)', '');
|
|
||||||
|
|
||||||
final prompt = '''
|
final prompt = '''
|
||||||
Generate complete, runnable $baseLanguage code for this API call:
|
Generate complete $language code for this API integration:
|
||||||
|
|
||||||
API Details:
|
API Request:
|
||||||
- URL: $endpoint
|
- URL: $endpoint
|
||||||
- Method: $method
|
- Method: $method
|
||||||
- Headers: ${headers.isEmpty ? 'None' : jsonEncode(headers)}
|
- Headers: ${headers.isEmpty ? 'None' : jsonEncode(headers)}
|
||||||
- Params: ${params.isEmpty ? 'None' : jsonEncode(params)}
|
- Params: ${params.isEmpty ? 'None' : jsonEncode(params)}
|
||||||
- Body: ${body ?? 'None'}
|
- Body: ${body ?? 'None'}
|
||||||
|
|
||||||
Requirements:
|
Response Structure:
|
||||||
1. Generate complete code that runs directly when copied.
|
${_formatResponse(responseBody)}
|
||||||
2. ${isFrontend ? 'Include UI components to display response data.' : 'Print the response to the console.'}
|
|
||||||
3. Handle all parameter types (query, headers, body).
|
|
||||||
4. Add proper error handling.
|
|
||||||
|
|
||||||
Generate only the code with necessary imports.
|
Requirements:
|
||||||
|
1. Single-file solution with no external config
|
||||||
|
2. Direct API URL implementation
|
||||||
|
3. Error handling for network/status errors
|
||||||
|
4. UI components matching response data
|
||||||
|
5. Ready-to-run code with example data display
|
||||||
|
|
||||||
|
Generate complete implementation code only.
|
||||||
''';
|
''';
|
||||||
|
|
||||||
return generateResponse(prompt);
|
return generateResponse(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String _formatResponse(dynamic response) {
|
||||||
|
if (response is Map) {
|
||||||
|
return response.entries
|
||||||
|
.map((e) => '${e.key}: ${_valueType(e.value)}')
|
||||||
|
.join('\n');
|
||||||
|
}
|
||||||
|
return response?.toString() ?? 'No response body';
|
||||||
|
}
|
||||||
|
|
||||||
|
String _valueType(dynamic value) {
|
||||||
|
if (value is List) return 'List[${value.isNotEmpty ? _valueType(value.first) : '?'}]';
|
||||||
|
if (value is Map) return 'Object';
|
||||||
|
return value.runtimeType.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,19 +22,24 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
context: context,
|
context: context,
|
||||||
builder: (context) => AlertDialog(
|
builder: (context) => AlertDialog(
|
||||||
title: const Text('Select Language'),
|
title: const Text('Select Language'),
|
||||||
content: SizedBox(
|
content: SingleChildScrollView(
|
||||||
width: 300,
|
child: Column(
|
||||||
child: ListView(
|
mainAxisSize: MainAxisSize.min,
|
||||||
shrinkWrap: true,
|
|
||||||
children: [
|
children: [
|
||||||
_buildLanguageTile('Flutter (UI)'),
|
for (var lang in [
|
||||||
_buildLanguageTile('React (UI)'),
|
'Flutter (UI)',
|
||||||
_buildLanguageTile('Dart (Console)'),
|
'React (UI)',
|
||||||
_buildLanguageTile('Python'),
|
'Dart (Console)',
|
||||||
_buildLanguageTile('JavaScript'),
|
'Python',
|
||||||
_buildLanguageTile('Node.js'),
|
'JavaScript',
|
||||||
_buildLanguageTile('Java'),
|
'Node.js',
|
||||||
_buildLanguageTile('C#'),
|
'Java',
|
||||||
|
'C#'
|
||||||
|
])
|
||||||
|
ListTile(
|
||||||
|
title: Text(lang),
|
||||||
|
onTap: () => Navigator.pop(context, lang),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -46,13 +51,6 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ListTile _buildLanguageTile(String language) {
|
|
||||||
return ListTile(
|
|
||||||
title: Text(language),
|
|
||||||
onTap: () => Navigator.pop(context, language),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _sendMessage(String message) async {
|
void _sendMessage(String message) async {
|
||||||
if (message.trim().isEmpty) return;
|
if (message.trim().isEmpty) return;
|
||||||
final ollamaService = ref.read(ollamaServiceProvider);
|
final ollamaService = ref.read(ollamaServiceProvider);
|
||||||
@@ -97,7 +95,7 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
_messages.add({
|
_messages.add({
|
||||||
'role': 'bot',
|
'role': 'bot',
|
||||||
'message': response.contains("```") ? response : "```$response```"
|
'message': response.contains("```") ? response : "```\n$response\n```"
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -116,7 +114,7 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
final showDebugButton = statusCode != null && statusCode >= 400;
|
final showDebugButton = statusCode != null && statusCode >= 400;
|
||||||
|
|
||||||
return Container(
|
return Container(
|
||||||
height: 400,
|
height: 450,
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: Theme.of(context).colorScheme.surface,
|
color: Theme.of(context).colorScheme.surface,
|
||||||
@@ -127,36 +125,35 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
),
|
),
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
Row(
|
Wrap(
|
||||||
|
spacing: 8,
|
||||||
|
runSpacing: 8,
|
||||||
|
alignment: WrapAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
ElevatedButton.icon(
|
ElevatedButton.icon(
|
||||||
onPressed: () => _sendMessage("Explain API"),
|
onPressed: () => _sendMessage("Explain API"),
|
||||||
icon: const Icon(Icons.info_outline),
|
icon: const Icon(Icons.info_outline),
|
||||||
label: const Text("Explain API"),
|
label: const Text("Explain API"),
|
||||||
),
|
),
|
||||||
if (showDebugButton) ...[
|
if (showDebugButton)
|
||||||
const SizedBox(width: 8),
|
|
||||||
ElevatedButton.icon(
|
ElevatedButton.icon(
|
||||||
onPressed: () => _sendMessage("Debug API"),
|
onPressed: () => _sendMessage("Debug API"),
|
||||||
icon: const Icon(Icons.bug_report),
|
icon: const Icon(Icons.bug_report),
|
||||||
label: const Text("Debug"),
|
label: const Text("Debug"),
|
||||||
),
|
),
|
||||||
],
|
|
||||||
const SizedBox(width: 8),
|
|
||||||
ElevatedButton.icon(
|
ElevatedButton.icon(
|
||||||
onPressed: () => _sendMessage("Generate Test Case"),
|
onPressed: () => _sendMessage("Generate Test Case"),
|
||||||
icon: const Icon(Icons.developer_mode),
|
icon: const Icon(Icons.developer_mode),
|
||||||
label: const Text("Test Case"),
|
label: const Text("Test Case"),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
|
||||||
ElevatedButton.icon(
|
ElevatedButton.icon(
|
||||||
onPressed: _handleCodeGeneration,
|
onPressed: _handleCodeGeneration,
|
||||||
icon: const Icon(Icons.code),
|
icon: const Icon(Icons.code),
|
||||||
label: const Text("Generate Code"),
|
label: const Text("Generate Code"),
|
||||||
),
|
),
|
||||||
const Spacer(),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
reverse: true,
|
reverse: true,
|
||||||
@@ -175,15 +172,21 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
padding: EdgeInsets.all(8.0),
|
padding: EdgeInsets.all(8.0),
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
),
|
),
|
||||||
Row(
|
const SizedBox(height: 10),
|
||||||
|
Container(
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
borderRadius: BorderRadius.circular(8),
|
||||||
|
color: Theme.of(context).colorScheme.surfaceVariant,
|
||||||
|
),
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||||
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _controller,
|
controller: _controller,
|
||||||
decoration: InputDecoration(
|
decoration: const InputDecoration(
|
||||||
hintText: 'Ask something...',
|
hintText: 'Ask something...',
|
||||||
border: OutlineInputBorder(
|
border: InputBorder.none,
|
||||||
borderRadius: BorderRadius.circular(8)),
|
|
||||||
),
|
),
|
||||||
onSubmitted: _sendMessage,
|
onSubmitted: _sendMessage,
|
||||||
),
|
),
|
||||||
@@ -194,6 +197,7 @@ class _ChatbotWidgetState extends ConsumerState<ChatbotWidget> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -211,7 +215,7 @@ class ChatBubble extends StatelessWidget {
|
|||||||
return Align(
|
return Align(
|
||||||
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
|
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
|
||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.symmetric(vertical: 4),
|
margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 12),
|
||||||
padding: const EdgeInsets.all(12),
|
padding: const EdgeInsets.all(12),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: isUser
|
color: isUser
|
||||||
|
|||||||
Reference in New Issue
Block a user