Skip to content

Commit f541490

Browse files
committed
refactor(deps): replace jsonpathkt with json-path
- Remove jsonpathkt dependency - Add json-path dependency - Update JSON parsing logic in LLMProvider2, CustomSSEProcessor, and JSONBodyResponseCallback - Improve error handling and support for different response formats in CustomSSEProcessor
1 parent 8a7adab commit f541490

File tree

5 files changed

+33
-20
lines changed

5 files changed

+33
-20
lines changed

build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,8 @@ project(":core") {
438438

439439
implementation("org.yaml:snakeyaml:2.2")
440440

441-
implementation("com.nfeld.jsonpathkt:jsonpathkt:2.0.1")
442-
443-
implementation("org.jetbrains:markdown:0.6.1")
441+
// implementation("com.nfeld.jsonpathkt:jsonpathkt:2.0.1")
442+
implementation("com.jayway.jsonpath:json-path:2.9.0")
444443

445444
// chocolate factorys
446445
// follow: https://onnxruntime.ai/docs/get-started/with-java.html

core/src/main/kotlin/cc/unitmesh/devti/llm2/LLMProvider2.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import cc.unitmesh.devti.util.AutoDevCoroutineScope
1010
import com.intellij.openapi.diagnostic.Logger
1111
import com.intellij.openapi.diagnostic.logger
1212
import com.intellij.openapi.project.Project
13-
import com.nfeld.jsonpathkt.JsonPath
14-
import com.nfeld.jsonpathkt.extension.read
13+
import com.jayway.jsonpath.JsonPath
1514
import kotlinx.coroutines.*
1615
import kotlinx.coroutines.channels.awaitClose
1716
import kotlinx.coroutines.flow.Flow

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import com.intellij.openapi.application.ApplicationManager
1414
import com.intellij.openapi.components.service
1515
import com.intellij.openapi.diagnostic.logger
1616
import com.intellij.openapi.project.Project
17-
import com.nfeld.jsonpathkt.JsonPath
18-
import com.nfeld.jsonpathkt.extension.read
17+
import com.jayway.jsonpath.JsonPath
1918
import io.reactivex.rxjava3.core.BackpressureStrategy
2019
import io.reactivex.rxjava3.core.Flowable
2120
import io.reactivex.rxjava3.core.FlowableEmitter
@@ -103,19 +102,37 @@ open class CustomSSEProcessor(private val project: Project) {
103102
// in some case, the response maybe not equal to our response format, so we need to ignore it
104103
// {"id":"cmpl-ac26a17e","object":"chat.completion.chunk","created":1858403,"model":"yi-34b-chat","choices":[{"delta":{"role":"assistant"},"index":0}],"content":"","lastOne":false}
105104

106-
val chunk: String? = JsonPath.parse(sse!!.data)?.read(responseFormat)
105+
val chunk: String? = try {
106+
val parsed = JsonPath.parse(sse!!.data)?.read<Any>(responseFormat)
107+
when (parsed) {
108+
is String -> parsed
109+
is ArrayList<*> -> {
110+
parsed.joinToString(" ") { it.toString() }
111+
}
112+
else -> parsed?.toString()
113+
}
114+
} catch (e: Exception) {
115+
null
116+
}
117+
107118
// new JsonPath lib caught the exception, so we need to handle when it is null
108119
if (chunk == null) {
109120
// try handle it's thinking model: $.choices[0].delta.reasoning_content
110-
val reasoningContent: String? = JsonPath.parse(sse.data)?.read("\$.choices[0].delta.reasoning_content")
111-
if (reasoningContent != null) {
112-
reasonerOutput += reasoningContent
113-
ApplicationManager.getApplication().invokeLater {
114-
AutoDevToolWindowFactory.getSketchWindow(project)?.printThinking(reasonerOutput)
121+
try {
122+
val reasoningContent: String? =
123+
JsonPath.parse(sse.data)?.read("\$.choices[0].delta.reasoning_content")
124+
if (reasoningContent != null) {
125+
reasonerOutput += reasoningContent
126+
ApplicationManager.getApplication().invokeLater {
127+
AutoDevToolWindowFactory.getSketchWindow(project)
128+
?.printThinking(reasonerOutput)
129+
}
130+
} else {
131+
parseFailedResponses.add(sse.data)
132+
logger.warn("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
115133
}
116-
} else {
117-
parseFailedResponses.add(sse.data)
118-
logger.warn("Failed to parse response.origin response is: ${sse.data}, response format: $responseFormat")
134+
} catch (e: Exception) {
135+
119136
}
120137
} else {
121138
hasSuccessRequest = true

core/src/main/kotlin/cc/unitmesh/devti/llms/custom/JSONBodyResponseCallback.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cc.unitmesh.devti.llms.custom
22

3-
import com.nfeld.jsonpathkt.JsonPath
4-
import com.nfeld.jsonpathkt.extension.read
3+
import com.jayway.jsonpath.JsonPath
54
import kotlinx.coroutines.runBlocking
65
import okhttp3.Call
76
import okhttp3.Callback

core/src/test/kotlin/cc/unitmesh/devti/settings/LLMSettingComponentKtTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cc.unitmesh.devti.settings
22

3-
import com.nfeld.jsonpathkt.JsonPath
4-
import com.nfeld.jsonpathkt.extension.read
3+
import com.jayway.jsonpath.JsonPath
54
import org.junit.Assert.assertEquals
65
import org.junit.Test
76

0 commit comments

Comments
 (0)