mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 02:39:19 +08:00
feat: implement code generation method
This commit is contained in:
@@ -89,6 +89,7 @@ enum ChatMessageType {
|
|||||||
debugError,
|
debugError,
|
||||||
generateTest,
|
generateTest,
|
||||||
generateDoc,
|
generateDoc,
|
||||||
|
generateCode,
|
||||||
general
|
general
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,7 +72,21 @@ class ChatViewmodel extends StateNotifier<ChatState> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final systemPrompt = _composeSystemPrompt(_currentRequest, type);
|
// Special handling: generateCode flow has two steps
|
||||||
|
String? systemPrompt;
|
||||||
|
if (type == ChatMessageType.generateCode) {
|
||||||
|
// If the user message includes a language (heuristic), go straight to code gen; else show intro + language choices
|
||||||
|
final detectedLang = _detectLanguageFromText(text);
|
||||||
|
systemPrompt = _composeSystemPrompt(
|
||||||
|
_currentRequest,
|
||||||
|
detectedLang == null
|
||||||
|
? ChatMessageType.generateCode
|
||||||
|
: ChatMessageType.generateCode,
|
||||||
|
overrideLanguage: detectedLang,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
systemPrompt = _composeSystemPrompt(_currentRequest, type);
|
||||||
|
}
|
||||||
final userPrompt = (text.trim().isEmpty && !countAsUser)
|
final userPrompt = (text.trim().isEmpty && !countAsUser)
|
||||||
? 'Please complete the task based on the provided context.'
|
? 'Please complete the task based on the provided context.'
|
||||||
: text;
|
: text;
|
||||||
@@ -403,11 +417,13 @@ class ChatViewmodel extends StateNotifier<ChatState> {
|
|||||||
|
|
||||||
String _composeSystemPrompt(
|
String _composeSystemPrompt(
|
||||||
RequestModel? req,
|
RequestModel? req,
|
||||||
ChatMessageType type,
|
ChatMessageType type, {
|
||||||
) {
|
String? overrideLanguage,
|
||||||
|
}) {
|
||||||
final history = _buildHistoryBlock();
|
final history = _buildHistoryBlock();
|
||||||
final contextBlock = _buildContextBlock(req);
|
final contextBlock = _buildContextBlock(req);
|
||||||
final task = _buildTaskPrompt(req, type);
|
final task =
|
||||||
|
_buildTaskPrompt(req, type, overrideLanguage: overrideLanguage);
|
||||||
return [
|
return [
|
||||||
if (task != null) task,
|
if (task != null) task,
|
||||||
if (contextBlock != null) contextBlock,
|
if (contextBlock != null) contextBlock,
|
||||||
@@ -450,7 +466,8 @@ class ChatViewmodel extends StateNotifier<ChatState> {
|
|||||||
</request_context>''';
|
</request_context>''';
|
||||||
}
|
}
|
||||||
|
|
||||||
String? _buildTaskPrompt(RequestModel? req, ChatMessageType type) {
|
String? _buildTaskPrompt(RequestModel? req, ChatMessageType type,
|
||||||
|
{String? overrideLanguage}) {
|
||||||
if (req == null) return null;
|
if (req == null) return null;
|
||||||
final http = req.httpRequestModel;
|
final http = req.httpRequestModel;
|
||||||
final resp = req.httpResponseModel;
|
final resp = req.httpResponseModel;
|
||||||
@@ -493,10 +510,47 @@ class ChatViewmodel extends StateNotifier<ChatState> {
|
|||||||
headersMap: http?.headersMap,
|
headersMap: http?.headersMap,
|
||||||
body: http?.body,
|
body: http?.body,
|
||||||
);
|
);
|
||||||
|
case ChatMessageType.generateCode:
|
||||||
|
// If a language is provided, go for code generation; else ask for language first
|
||||||
|
if (overrideLanguage == null || overrideLanguage.isEmpty) {
|
||||||
|
return prompts.codeGenerationIntroPrompt(
|
||||||
|
url: http?.url,
|
||||||
|
method: http?.method.name.toUpperCase(),
|
||||||
|
headersMap: http?.headersMap,
|
||||||
|
body: http?.body,
|
||||||
|
bodyContentType: http?.bodyContentType.name,
|
||||||
|
paramsMap: http?.paramsMap,
|
||||||
|
authType: http?.authModel?.type.name,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return prompts.generateCodePrompt(
|
||||||
|
url: http?.url,
|
||||||
|
method: http?.method.name.toUpperCase(),
|
||||||
|
headersMap: http?.headersMap,
|
||||||
|
body: http?.body,
|
||||||
|
bodyContentType: http?.bodyContentType.name,
|
||||||
|
paramsMap: http?.paramsMap,
|
||||||
|
authType: http?.authModel?.type.name,
|
||||||
|
language: overrideLanguage,
|
||||||
|
);
|
||||||
|
}
|
||||||
case ChatMessageType.general:
|
case ChatMessageType.general:
|
||||||
return prompts.generalInteractionPrompt();
|
return prompts.generalInteractionPrompt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Very light heuristic to detect language keywords in user text
|
||||||
|
String? _detectLanguageFromText(String text) {
|
||||||
|
final t = text.toLowerCase();
|
||||||
|
if (t.contains('python')) return 'Python (requests)';
|
||||||
|
if (t.contains('dart')) return 'Dart (http)';
|
||||||
|
if (t.contains('golang') || t.contains('go ')) return 'Go (net/http)';
|
||||||
|
if (t.contains('javascript') || t.contains('js') || t.contains('fetch')) {
|
||||||
|
return 'JavaScript (fetch)';
|
||||||
|
}
|
||||||
|
if (t.contains('curl')) return 'cURL';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final chatViewmodelProvider = StateNotifierProvider<ChatViewmodel, ChatState>((
|
final chatViewmodelProvider = StateNotifierProvider<ChatViewmodel, ChatState>((
|
||||||
|
|||||||
Reference in New Issue
Block a user