Skip to content

Commit 5de1db3

Browse files
author
ippan
committed
fix(llm): handle null response in CustomSSEProcessor
- Remove unnecessary 'hasSuccessRequest' toggle and related code. - Introduce 'lastParseFailedResponse' to store the failed response. - Log a detailed error message when a null response is encountered. - Add a check at the end of the stream to notify the user if no successful parse occurred.
1 parent 41f2c72 commit 5de1db3

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import org.jetbrains.annotations.VisibleForTesting
4141
*/
4242
open class CustomSSEProcessor(private val project: Project) {
4343
open var hasSuccessRequest: Boolean = false
44-
private var lastParseFailedResponse: String = ""
44+
private var parseFailedResponses: MutableList<String> = mutableListOf()
4545
open val requestFormat: String = ""
4646
open val responseFormat: String = ""
4747
private val logger = logger<CustomSSEProcessor>()
@@ -92,7 +92,7 @@ open class CustomSSEProcessor(private val project: Project) {
9292

9393
// new JsonPath lib caught the exception, so we need to handle when it is null
9494
if (chunk == null) {
95-
lastParseFailedResponse = sse.data
95+
parseFailedResponses.add(sse.data)
9696
logger.info("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
9797
} else {
9898
hasSuccessRequest = true
@@ -115,13 +115,14 @@ open class CustomSSEProcessor(private val project: Project) {
115115
// if not, notice user check response format
116116
if (!hasSuccessRequest) {
117117
val errorMsg = """
118-
**Failed** to parse response.origin response is:
119-
<code>${lastParseFailedResponse}</code>
120-
please check your response format:
121-
**$responseFormat**\n""".trimIndent()
118+
|**Failed** to parse response.please check your response format:
119+
|**$responseFormat** origin responses is:
120+
|- ${parseFailedResponses.joinToString("\n- ")}
121+
|""".trimMargin()
122122

123123
// TODO add refresh feature
124-
trySend(errorMsg)
124+
// don't use trySend, it may be ignored by 'close()` op
125+
send(errorMsg)
125126
}
126127
recording.write(RecordingInstruction(promptText, output))
127128
close()
@@ -138,6 +139,8 @@ open class CustomSSEProcessor(private val project: Project) {
138139
return callbackFlow {
139140
close()
140141
}
142+
} finally {
143+
parseFailedResponses.clear()
141144
}
142145
}
143146
}

0 commit comments

Comments
 (0)