Skip to content

Commit 72c3e12

Browse files
committed
refactor: Improve provideCompletion method in AgentService
- Refactor provideCompletion to use coroutines and handle cancellation - Add error handling and logging for better debugging - Ensure PsiFile retrieval is performed on the appropriate thread
1 parent ab3b429 commit 72c3e12

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/main/kotlin/ai/devchat/plugin/completion/agent/AgentService.kt

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,63 @@ import com.intellij.psi.PsiFile
1010
import io.ktor.util.*
1111
import kotlinx.coroutines.CoroutineScope
1212
import kotlinx.coroutines.Dispatchers
13+
import kotlinx.coroutines.withContext
14+
import com.intellij.openapi.application.ApplicationManager
15+
import com.intellij.openapi.application.ModalityState
16+
import kotlinx.coroutines.suspendCancellableCoroutine
17+
import kotlin.coroutines.resume
18+
import kotlinx.coroutines.CancellationException
1319

1420
@Service
1521
class AgentService : Disposable {
1622
val scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
1723
private var agent: Agent = Agent(scope)
1824

19-
suspend fun provideCompletion(editor: Editor, offset: Int, manually: Boolean = false): Agent.CompletionResponse? {
20-
return ReadAction.compute<PsiFile, Throwable> {
21-
editor.project?.let { project ->
22-
PsiDocumentManager.getInstance(project).getPsiFile(editor.document)
23-
}
24-
}?.let { file ->
25-
agent.provideCompletions(
26-
Agent.CompletionRequest(
27-
file,
28-
file.getLanguageId(),
29-
offset,
30-
manually,
31-
)
32-
)
25+
suspend fun provideCompletion(editor: Editor, offset: Int, manually: Boolean = false): Agent.CompletionResponse? {
26+
println("Entering provideCompletion method")
27+
return withContext(Dispatchers.Default) {
28+
try {
29+
println("Attempting to get PsiFile")
30+
val file = suspendCancellableCoroutine<PsiFile?> { continuation ->
31+
ApplicationManager.getApplication().invokeLater({
32+
val psiFile = ReadAction.compute<PsiFile?, Throwable> {
33+
editor.project?.let { project ->
34+
PsiDocumentManager.getInstance(project).getPsiFile(editor.document)
35+
}
36+
}
37+
continuation.resume(psiFile)
38+
}, ModalityState.defaultModalityState())
39+
}
40+
41+
println("PsiFile obtained: ${file != null}")
42+
43+
file?.let { psiFile ->
44+
println("Calling agent.provideCompletions")
45+
val result = agent.provideCompletions(
46+
Agent.CompletionRequest(
47+
psiFile,
48+
psiFile.getLanguageId(),
49+
offset,
50+
manually,
51+
)
52+
)
53+
println("agent.provideCompletions returned: $result")
54+
result
55+
}
56+
} catch (e: CancellationException) {
57+
// 方案1:以较低的日志级别记录
58+
println("Completion was cancelled: ${e.message}")
59+
// 或者方案2:完全忽略
60+
// // 不做任何处理
61+
62+
null
63+
} catch (e: Exception) {
64+
println("Exception in provideCompletion: ${e.message}")
65+
e.printStackTrace()
66+
null
67+
}
3368
}
34-
}
69+
}
3570

3671
suspend fun postEvent(event: Agent.LogEventRequest) {
3772
agent.postEvent(event)

0 commit comments

Comments
 (0)