Skip to content

Commit c78ab07

Browse files
committed
feat(chat): add LLM request cancellation support #394
Implement proper cancellation for chat coding service by adding support for LLMProvider2 cancellation and ensuring cleanup when stopping or clearing sessions. Addresses issue #394.
1 parent f09d66a commit c78ab07

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingService.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import cc.unitmesh.devti.custom.compile.CustomVariable
99
import cc.unitmesh.devti.gui.chat.message.ChatActionType
1010
import cc.unitmesh.devti.gui.chat.message.ChatContext
1111
import cc.unitmesh.devti.gui.chat.message.ChatRole
12+
import cc.unitmesh.devti.llm2.LLMProvider2
1213
import cc.unitmesh.devti.llms.LlmFactory
1314
import cc.unitmesh.devti.llms.custom.Message
1415
import cc.unitmesh.devti.provider.ContextPrompter
@@ -28,9 +29,18 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
2829
fun getLabel(): String = "$actionType Code"
2930

3031
fun stop() {
32+
// Cancel the coroutine job
3133
currentJob?.cancel()
34+
35+
// Also cancel the underlying LLM provider if it supports cancellation
36+
if (llmProvider is LLMProvider2) {
37+
(llmProvider as LLMProvider2).cancelCurrentRequestSync()
38+
}
39+
40+
currentJob = null
3241
}
3342

43+
3444
private var isLastAgent: Boolean = false
3545

3646
fun handlePromptAndResponse(
@@ -130,6 +140,7 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
130140

131141
fun clearSession() {
132142
// recreate session to make sure the history is cleared
143+
stop()
133144
llmProvider = LlmFactory.create(project)
134145
}
135146

0 commit comments

Comments
 (0)