Skip to content

Commit 7e60675

Browse files
author
ippan
committed
fix(core): handle null response in JsonPath parsing
The commit fixes the CustomSSEProcessor by handling null responses from the new JsonPath library. It adds a check for null values when parsing the SSE data and notifies the user if the response format is incorrect. This prevents the application from crashing and provides a better user experience. Additionally, it removes unnecessary logging and redundant code.
1 parent d4eee77 commit 7e60675

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,34 @@ open class CustomSSEProcessor(private val project: Project) {
8080
sseFlowable
8181
.doOnError {
8282
it.printStackTrace()
83+
trySend(it.message ?: "Error occurs")
8384
close()
8485
}
8586
.blockingForEach { sse ->
8687
if (responseFormat.isNotEmpty()) {
8788
// {"id":"cmpl-a22a0d78fcf845be98660628fe5d995b","object":"chat.completion.chunk","created":822330,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{},"finish_reason":"stop","usage":{"prompt_tokens":434,"completion_tokens":68,"total_tokens":502}}]}
8889
// in some case, the response maybe not equal to our response format, so we need to ignore it
89-
// logger.info("SSE: ${sse.data}")
90-
val chunk: String = try {
91-
JsonPath.parse(sse!!.data)?.read(responseFormat) ?: ""
92-
} catch (e: Exception) {
93-
if (hasSuccessRequest) {
94-
logger.info("Failed to parse response", e)
90+
val chunk: String? = JsonPath.parse(sse!!.data)?.read(responseFormat)
91+
92+
// new JsonPath lib caught the exception, so we need to handle when it is null
93+
if (chunk == null) {
94+
// if first chunk parse failed, notice user to check response format
95+
if(hasSuccessRequest) {
96+
val errorMsg = """
97+
**Failed** to parse response.origin response is:
98+
<code>${sse.data}</code>
99+
please check your response format:
100+
**$responseFormat**\n""".trimIndent()
101+
102+
// TODO add refresh feature
103+
trySend(errorMsg)
104+
close()
95105
} else {
96-
logger.error("Failed to parse response", e)
106+
logger.info("Failed to parse response.origin response is: ${sse.data}")
97107
}
98108
return@blockingForEach
99109
}
100110

101-
if (chunk.isEmpty()) {
102-
return@blockingForEach
103-
}
104-
105111
hasSuccessRequest = true
106112

107113
output += chunk
@@ -201,4 +207,4 @@ fun CustomRequest.updateCustomFormat(format: String): String {
201207
val requestContentOri = Json.encodeToString<CustomRequest>(this)
202208
return Json.parseToJsonElement(requestContentOri)
203209
.jsonObject.updateCustomBody(format).toString()
204-
}
210+
}

0 commit comments

Comments
 (0)