FEAT: Debug API prompt for clear analysis

This commit is contained in:
siddu015
2025-03-05 23:12:32 +05:30
parent 2f84eab461
commit ebe4bdba2d
3 changed files with 55 additions and 40 deletions

View File

@@ -1,5 +1,6 @@
import '../../models/request_model.dart';
import 'dart:convert';
import '../services/dashbot_service.dart';
import 'package:apidash/models/request_model.dart';
class DebugFeature {
final DashBotService _service;
@@ -10,35 +11,47 @@ class DebugFeature {
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 method = requestModel.httpRequestModel?.method.toString().split('.').last.toUpperCase() ?? "GET";
final endpoint = requestModel.httpRequestModel?.url ?? "Unknown Endpoint";
final headers = requestModel.httpRequestModel?.enabledHeadersMap ?? {};
final parameters = requestModel.httpRequestModel?.enabledParamsMap ?? {};
final body = requestModel.httpRequestModel?.body;
final rawResponse = responseModel.body;
final responseBody = rawResponse is String ? rawResponse : jsonEncode(rawResponse);
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:
URGENT API DEBUG ANALYSIS
**API Request:**
- Endpoint: `${requestModel.httpRequestModel?.url ?? "unknown"}`
- Method: `${requestModel.httpRequestModel?.method.toString().split('.').last.toUpperCase() ?? "GET"}`
**API Response:**
**Request Overview:**
- Endpoint: $endpoint
- Method: $method
- 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.
**Debugging Instructions:**
Provide a PRECISE, TEXT-ONLY explanation that:
1. Identifies the EXACT problem
2. Explains WHY the request failed
3. Describes SPECIFIC steps to resolve the issue
4. NO CODE SNIPPETS ALLOWED
**Request Details:**
- Headers: ${headers.isNotEmpty ? jsonEncode(headers) : "No headers"}
- Parameters: ${parameters.isNotEmpty ? jsonEncode(parameters) : "No parameters"}
- Request Body: ${body ?? "Empty body"}
**Response Context:**
\`\`\`
$responseBody
\`\`\`
Provide a CLEAR, ACTIONABLE solution in the SIMPLEST possible language.
''';
// Use DashBotService to generate the AI response
return _service.generateResponse(prompt);
}
}