mirror of
https://github.com/foss42/apidash.git
synced 2025-11-30 17:59:18 +08:00
Feat: API Documentation & process general queries
This commit is contained in:
66
lib/dashbot/features/documentation.dart
Normal file
66
lib/dashbot/features/documentation.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'dart:convert';
|
||||
import '../services/dashbot_service.dart';
|
||||
import 'package:apidash/models/request_model.dart';
|
||||
|
||||
class DocumentationFeature {
|
||||
final DashBotService _service;
|
||||
|
||||
DocumentationFeature(this._service);
|
||||
|
||||
Future<String> generateApiDocumentation({
|
||||
required RequestModel? requestModel,
|
||||
required dynamic responseModel,
|
||||
}) async {
|
||||
if (requestModel == null || responseModel == null) {
|
||||
return "No recent API requests found.";
|
||||
}
|
||||
|
||||
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 prompt = """
|
||||
API DOCUMENTATION GENERATION
|
||||
|
||||
**API Details:**
|
||||
- Endpoint: $endpoint
|
||||
- Method: $method
|
||||
- Status Code: $statusCode
|
||||
|
||||
**Request Components:**
|
||||
- Headers: ${headers.isNotEmpty ? jsonEncode(headers) : "None"}
|
||||
- Query Parameters: ${parameters.isNotEmpty ? jsonEncode(parameters) : "None"}
|
||||
- Request Body: ${body != null && body.isNotEmpty ? body : "None"}
|
||||
|
||||
**Response Example:**
|
||||
```
|
||||
$responseBody
|
||||
```
|
||||
|
||||
**Documentation Instructions:**
|
||||
Create comprehensive API documentation that includes:
|
||||
|
||||
1. **Overview**: A clear, concise description of what this API endpoint does
|
||||
2. **Authentication**: Required authentication method based on headers
|
||||
3. **Request Details**: All required and optional parameters with descriptions
|
||||
4. **Response Structure**: Breakdown of response fields and their meanings
|
||||
5. **Error Handling**: Possible error codes and troubleshooting
|
||||
6. **Example Usage**: A complete code example showing how to call this API
|
||||
|
||||
Format in clean markdown with proper sections and code blocks where appropriate.
|
||||
""";
|
||||
|
||||
return _service.generateResponse(prompt);
|
||||
}
|
||||
}
|
||||
45
lib/dashbot/features/general_query.dart
Normal file
45
lib/dashbot/features/general_query.dart
Normal file
@@ -0,0 +1,45 @@
|
||||
import 'package:ollama_dart/ollama_dart.dart';
|
||||
import 'package:apidash/models/request_model.dart';
|
||||
|
||||
class GeneralQueryFeature {
|
||||
final OllamaClient _client;
|
||||
|
||||
GeneralQueryFeature(this._client);
|
||||
|
||||
Future<String> generateResponse(String prompt, {RequestModel? requestModel, dynamic responseModel}) async {
|
||||
// Create a more focused prompt that incorporates request/response context if available
|
||||
String enhancedPrompt = prompt;
|
||||
|
||||
if (requestModel != null && responseModel != null) {
|
||||
final method = requestModel.httpRequestModel?.method.toString().split('.').last.toUpperCase() ?? "GET";
|
||||
final endpoint = requestModel.httpRequestModel?.url ?? "Unknown Endpoint";
|
||||
final statusCode = responseModel.statusCode ?? 0;
|
||||
|
||||
enhancedPrompt = '''
|
||||
CONTEXT-AWARE RESPONSE
|
||||
|
||||
**User Question:**
|
||||
$prompt
|
||||
|
||||
**Related API Context:**
|
||||
- Endpoint: $endpoint
|
||||
- Method: $method
|
||||
- Status Code: $statusCode
|
||||
|
||||
**Instructions:**
|
||||
1. Directly address the user's specific question
|
||||
2. Provide relevant, concise information
|
||||
3. Reference the API context when helpful
|
||||
4. Focus on practical, actionable insights
|
||||
5. Avoid generic explanations or documentation
|
||||
|
||||
Respond in a helpful, direct manner that specifically answers what was asked.
|
||||
''';
|
||||
}
|
||||
|
||||
final response = await _client.generateCompletion(
|
||||
request: GenerateCompletionRequest(model: 'llama3.2:3b', prompt: enhancedPrompt),
|
||||
);
|
||||
return response.response.toString();
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,29 @@
|
||||
import 'package:apidash/dashbot/features/debug.dart';
|
||||
import 'package:apidash/dashbot/features/documentation.dart';
|
||||
import 'package:ollama_dart/ollama_dart.dart';
|
||||
import '../features/explain.dart';
|
||||
import 'package:apidash/dashbot/features/explain.dart';
|
||||
import 'package:apidash/models/request_model.dart';
|
||||
import 'package:apidash/dashbot/features/general_query.dart';
|
||||
|
||||
class DashBotService {
|
||||
final OllamaClient _client;
|
||||
late final ExplainFeature _explainFeature;
|
||||
late final DebugFeature _debugFeature;
|
||||
late final DocumentationFeature _documentationFeature;
|
||||
final GeneralQueryFeature _generalQueryFeature;
|
||||
|
||||
|
||||
DashBotService()
|
||||
: _client = OllamaClient(baseUrl: 'http://127.0.0.1:11434/api') {
|
||||
: _client = OllamaClient(baseUrl: 'http://127.0.0.1:11434/api'),
|
||||
_generalQueryFeature = GeneralQueryFeature(OllamaClient(baseUrl: 'http://127.0.0.1:11434/api')) {
|
||||
|
||||
_explainFeature = ExplainFeature(this);
|
||||
_debugFeature = DebugFeature(this);
|
||||
_documentationFeature = DocumentationFeature(this);
|
||||
}
|
||||
|
||||
Future<String> generateResponse(String prompt) async {
|
||||
final response = await _client.generateCompletion(
|
||||
request: GenerateCompletionRequest(model: 'llama3.2:3b', prompt: prompt),
|
||||
);
|
||||
return response.response.toString();
|
||||
return _generalQueryFeature.generateResponse(prompt);
|
||||
}
|
||||
|
||||
Future<String> handleRequest(
|
||||
@@ -29,8 +34,11 @@ class DashBotService {
|
||||
} else if (input == "Debug API") {
|
||||
return _debugFeature.debugApi(
|
||||
requestModel: requestModel, responseModel: responseModel);
|
||||
} else if (input == "Document API") {
|
||||
return _documentationFeature.generateApiDocumentation(
|
||||
requestModel: requestModel, responseModel: responseModel);
|
||||
}
|
||||
|
||||
return generateResponse(input);
|
||||
return _generalQueryFeature.generateResponse(input, requestModel: requestModel, responseModel: responseModel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,6 +143,14 @@ class _DashBotWidgetState extends ConsumerState<DashBotWidget> {
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () => _sendMessage("Document API"),
|
||||
icon: const Icon(Icons.description_outlined),
|
||||
label: const Text("Document"),
|
||||
style: ElevatedButton.styleFrom (
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,17 +126,17 @@ class Dashboard extends ConsumerWidget {
|
||||
),
|
||||
),
|
||||
// TODO: Release DashBot
|
||||
// floatingActionButton: FloatingActionButton(
|
||||
// onPressed: () => showModalBottomSheet(
|
||||
// context: context,
|
||||
// isScrollControlled: true,
|
||||
// builder: (context) => const Padding(
|
||||
// padding: EdgeInsets.all(16.0),
|
||||
// child: DashBotWidget(),
|
||||
// ),
|
||||
// ),
|
||||
// child: const Icon(Icons.help_outline),
|
||||
// ),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () => showModalBottomSheet(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (context) => const Padding(
|
||||
padding: EdgeInsets.all(16.0),
|
||||
child: DashBotWidget(),
|
||||
),
|
||||
),
|
||||
child: const Icon(Icons.help_outline),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user