Skip to content

Commit 561e36a

Browse files
committed
fix(completion): improve chatbot response handling with JSON parsing enhancements
This commit enhances the chatbot response handling by improving the JSON parsing logic. It includes changes to remove the leading and trailing ```json` tags from the string to extract the JSON response correctly. Additionally, the logger has been refactored to use a lambda expression instead of a reflection-based approach for improved performance and readability.
1 parent 69bef3d commit 561e36a

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/main/kotlin/cc/unitmesh/devti/actions/chat/CodeCompleteChatAction.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class CodeCompleteChatAction : AnAction() {
2222
return ActionUpdateThread.BGT
2323
}
2424

25+
private val logger = logger<ChatCodingService>()
26+
2527
override fun actionPerformed(e: AnActionEvent) {
2628
val project = e.project ?: return
2729
val document = e.getData(CommonDataKeys.EDITOR)?.document
@@ -52,7 +54,7 @@ class CodeCompleteChatAction : AnAction() {
5254
val chatCodingService = ChatCodingService(actionType, project)
5355
val toolWindowManager =
5456
ToolWindowManager.getInstance(project).getToolWindow(AutoDevToolWindowFactory.Util.id) ?: run {
55-
logger<ChatCodingService>().warn("Tool window not found")
57+
logger.warn("Tool window not found")
5658
return@runReadAction
5759
}
5860

@@ -65,10 +67,8 @@ class CodeCompleteChatAction : AnAction() {
6567
contentManager.addContent(content)
6668

6769
toolWindowManager.activate {
68-
val chatContext = ChatContext(
69-
null, prefixText, suffixText
70-
)
71-
chatCodingService.handlePromptAndResponse(contentPanel, prompter, chatContext, false)
70+
val chatContext = ChatContext(null, prefixText, suffixText)
71+
chatCodingService.handlePromptAndResponse(contentPanel, prompter, chatContext, true)
7272
}
7373
} catch (ignore: IndexNotReadyException) {
7474
return@runReadAction

src/main/kotlin/cc/unitmesh/devti/counit/CoUnitPreProcessor.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import com.intellij.openapi.components.Service
1414
import com.intellij.openapi.diagnostic.logger
1515
import com.intellij.openapi.project.Project
1616
import kotlinx.coroutines.launch
17-
import kotlinx.serialization.decodeFromString
1817
import kotlinx.serialization.json.Json
1918

2019
const val CO_UNIT = "/counit"
@@ -56,8 +55,7 @@ class CoUnitPreProcessor(val project: Project) {
5655
llmProvider.appendLocalMessage(result, ChatRole.Assistant)
5756

5857
val explain = try {
59-
val fixedResult = fix(result)
60-
json.decodeFromString<ExplainQuery>(fixedResult)
58+
json.decodeFromString<ExplainQuery>(extractJsonResponse(result))
6159
} catch (e: Exception) {
6260
LOG.error("parse result error: $e")
6361
return@launch
@@ -111,8 +109,15 @@ class CoUnitPreProcessor(val project: Project) {
111109
return sb.toString()
112110
}
113111

114-
private fun fix(result: String): String {
115-
// remove start and end ```json
112+
/**
113+
* This method is used to extract JSON response from a given string.
114+
* It removes the leading and trailing ````json` tags from the string,
115+
* which are used to denote JSON code blocks in markdown files.
116+
*
117+
* @param result The string containing the JSON response surrounded by ````json` tags.
118+
* @return The extracted JSON response string without the ````json` tags.
119+
*/
120+
private fun extractJsonResponse(result: String): String {
116121
return result
117122
.removePrefix("```json")
118123
.removeSuffix("```")

src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomLLMProvider.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,9 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
6565
override fun prompt(promptText: String): String = this.prompt(promptText, "")
6666

6767
override fun stream(promptText: String, systemPrompt: String, keepHistory: Boolean): Flow<String> {
68-
if (!keepHistory) {
68+
if (!keepHistory || project.coderSetting.state.noChatHistory) {
6969
clearMessage()
7070
}
71-
if (project.coderSetting.state.noChatHistory) {
72-
messages.clear()
73-
}
7471

7572
messages += Message("user", promptText)
7673

0 commit comments

Comments
 (0)