mirror of
https://github.com/foss42/apidash.git
synced 2025-12-01 10:17:47 +08:00
Feat:Debug API
This commit is contained in:
44
lib/dashbot/features/debug.dart
Normal file
44
lib/dashbot/features/debug.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import '../../models/request_model.dart';
|
||||
import '../services/dashbot_service.dart';
|
||||
|
||||
class DebugFeature {
|
||||
final DashBotService _service;
|
||||
|
||||
DebugFeature(this._service);
|
||||
|
||||
Future<String> debugApi({
|
||||
required RequestModel? requestModel,
|
||||
required dynamic responseModel,
|
||||
}) async {
|
||||
// Handle case where no request or response is available
|
||||
if (requestModel == null || responseModel == null) {
|
||||
return "No recent API requests found.";
|
||||
}
|
||||
|
||||
// Extract status code and error message from the response
|
||||
final statusCode = responseModel.statusCode ?? 0;
|
||||
final errorMessage = responseModel.body ?? "No error message available.";
|
||||
|
||||
// Create a prompt for the AI to analyze the request and response
|
||||
final prompt = '''
|
||||
Debug this API request based on the status code and error message:
|
||||
|
||||
**API Request:**
|
||||
- Endpoint: `${requestModel.httpRequestModel?.url ?? "unknown"}`
|
||||
- Method: `${requestModel.httpRequestModel?.method.toString().split('.').last.toUpperCase() ?? "GET"}`
|
||||
|
||||
**API Response:**
|
||||
- Status Code: $statusCode
|
||||
- Error Message: $errorMessage
|
||||
|
||||
**Instructions:**
|
||||
- Explain what the status code $statusCode typically means.
|
||||
- Suggest possible reasons for the error based on the status code and error message.
|
||||
- Provide actionable steps to resolve the issue.
|
||||
- Use Markdown for formatting with headings and bullet points.
|
||||
''';
|
||||
|
||||
// Use DashBotService to generate the AI response
|
||||
return _service.generateResponse(prompt);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,10 @@ final chatMessagesProvider = StateNotifierProvider<ChatMessagesNotifier, List<Ma
|
||||
(ref) => ChatMessagesNotifier(),
|
||||
);
|
||||
|
||||
final dashBotServiceProvider = Provider<DashBotService>((ref) {
|
||||
return DashBotService();
|
||||
});
|
||||
|
||||
class ChatMessagesNotifier extends StateNotifier<List<Map<String, dynamic>>> {
|
||||
ChatMessagesNotifier() : super([]) {
|
||||
_loadMessages();
|
||||
@@ -37,7 +41,3 @@ class ChatMessagesNotifier extends StateNotifier<List<Map<String, dynamic>>> {
|
||||
_saveMessages();
|
||||
}
|
||||
}
|
||||
|
||||
final dashBotServiceProvider = Provider<DashBotService>((ref) {
|
||||
return DashBotService();
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'package:apidash/dashbot/features/debug.dart';
|
||||
import 'package:ollama_dart/ollama_dart.dart';
|
||||
import '../features/explain.dart';
|
||||
import 'package:apidash/models/request_model.dart';
|
||||
@@ -6,9 +6,11 @@ import 'package:apidash/models/request_model.dart';
|
||||
class DashBotService {
|
||||
final OllamaClient _client;
|
||||
late final ExplainFeature _explainFeature;
|
||||
late final DebugFeature _debugFeature;
|
||||
|
||||
DashBotService() : _client = OllamaClient(baseUrl: 'http://127.0.0.1:11434/api') {
|
||||
_explainFeature = ExplainFeature(this);
|
||||
_debugFeature = DebugFeature(this);
|
||||
}
|
||||
|
||||
Future<String> generateResponse(String prompt) async {
|
||||
@@ -21,6 +23,8 @@ class DashBotService {
|
||||
Future<String> handleRequest(String input, RequestModel? requestModel, dynamic responseModel) async {
|
||||
if (input == "Explain API") {
|
||||
return _explainFeature.explainLatestApi(requestModel: requestModel, responseModel: responseModel);
|
||||
} else if(input == "Debug API") {
|
||||
_debugFeature.debugApi(requestModel: requestModel, responseModel: responseModel);
|
||||
}
|
||||
return generateResponse(input);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user