@@ -80,28 +80,34 @@ open class CustomSSEProcessor(private val project: Project) {
80
80
sseFlowable
81
81
.doOnError {
82
82
it.printStackTrace()
83
+ trySend(it.message ? : " Error occurs" )
83
84
close()
84
85
}
85
86
.blockingForEach { sse ->
86
87
if (responseFormat.isNotEmpty()) {
87
88
// {"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}}]}
88
89
// 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()
95
105
} else {
96
- logger.error (" Failed to parse response" , e )
106
+ logger.info (" Failed to parse response.origin response is: ${sse.data} " )
97
107
}
98
108
return @blockingForEach
99
109
}
100
110
101
- if (chunk.isEmpty()) {
102
- return @blockingForEach
103
- }
104
-
105
111
hasSuccessRequest = true
106
112
107
113
output + = chunk
@@ -201,4 +207,4 @@ fun CustomRequest.updateCustomFormat(format: String): String {
201
207
val requestContentOri = Json .encodeToString<CustomRequest >(this )
202
208
return Json .parseToJsonElement(requestContentOri)
203
209
.jsonObject.updateCustomBody(format).toString()
204
- }
210
+ }
0 commit comments