Skip to content

Commit 0f5fd12

Browse files
committed
fix(mpp-idea): fix prompt enhancement functionality
1. Add replaceText() method to IdeaDevInInput for setting text content - Renamed from setText() to avoid conflict with EditorTextField.setText() - Uses WriteCommandAction for proper document modification 2. Fix prompt enhancement in IdeaDevInInputArea: - Use IntelliJ Logger instead of KLogger (not available in mpp-idea) - Move logger to file-level to avoid Composable context issues - Use Dispatchers.IO for LLM calls - Use ApplicationManager.invokeLater for EDT updates - Add proper logging for debugging 3. Add logging to IdeaPromptEnhancer: - Log enhancement progress and results - Log model configuration and LLM response details - Log errors with stack traces
1 parent 1b647b3 commit 0f5fd12

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/editor/IdeaDevInInput.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ class IdeaDevInInput(
242242
})
243243
}
244244

245+
/**
246+
* Replace the text content of the input.
247+
* Clears existing content and sets new text.
248+
*/
249+
fun replaceText(newText: String) {
250+
WriteCommandAction.runWriteCommandAction(project, "Replace text", "intentions.write.action", {
251+
val document = this.editor?.document ?: return@runWriteCommandAction
252+
document.setText(newText)
253+
})
254+
}
255+
245256
/**
246257
* Clear the input and recreate document.
247258
*/

mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/editor/IdeaPromptEnhancer.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cc.unitmesh.devins.ui.config.ConfigManager
44
import cc.unitmesh.llm.KoogLLMService
55
import com.intellij.openapi.application.runReadAction
66
import com.intellij.openapi.components.Service
7+
import com.intellij.openapi.diagnostic.Logger
78
import com.intellij.openapi.project.Project
89
import com.intellij.openapi.project.guessProjectDir
910
import kotlinx.coroutines.Dispatchers
@@ -21,6 +22,7 @@ import kotlinx.coroutines.withContext
2122
*/
2223
@Service(Service.Level.PROJECT)
2324
class IdeaPromptEnhancer(private val project: Project) {
25+
private val logger = Logger.getInstance(IdeaPromptEnhancer::class.java)
2426

2527
/**
2628
* Enhance the user's prompt using LLM.
@@ -30,13 +32,23 @@ class IdeaPromptEnhancer(private val project: Project) {
3032
*/
3133
suspend fun enhance(input: String): String = withContext(Dispatchers.IO) {
3234
try {
35+
logger.info("Starting enhancement for input: ${input.take(50)}...")
36+
3337
val dict = loadDomainDict()
3438
val readme = loadReadme()
39+
logger.info("Loaded domain dict (${dict.length} chars), readme (${readme.length} chars)")
40+
3541
val prompt = buildEnhancePrompt(input, dict, readme)
42+
logger.info("Built enhancement prompt (${prompt.length} chars)")
3643

3744
val config = ConfigManager.load()
3845
val modelConfig = config.getActiveModelConfig()
39-
?: return@withContext input
46+
if (modelConfig == null) {
47+
logger.warn("No active model config found, returning original input")
48+
return@withContext input
49+
}
50+
51+
logger.info("Using model: ${modelConfig.modelName}")
4052

4153
val llmService = KoogLLMService(modelConfig)
4254
val result = StringBuilder()
@@ -46,8 +58,17 @@ class IdeaPromptEnhancer(private val project: Project) {
4658
result.append(chunk)
4759
}
4860

49-
extractEnhancedPrompt(result.toString()) ?: input
61+
logger.info("LLM response received (${result.length} chars)")
62+
val enhanced = extractEnhancedPrompt(result.toString())
63+
if (enhanced != null) {
64+
logger.info("Extracted enhanced prompt (${enhanced.length} chars)")
65+
} else {
66+
logger.warn("Failed to extract enhanced prompt from response")
67+
}
68+
69+
enhanced ?: input
5070
} catch (e: Exception) {
71+
logger.error("Enhancement failed: ${e.message}", e)
5172
// Return original input if enhancement fails
5273
input
5374
}

mpp-idea/src/main/kotlin/cc/unitmesh/devins/idea/toolwindow/IdeaDevInInputArea.kt

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ import androidx.compose.ui.unit.dp
1414
import cc.unitmesh.devins.idea.editor.*
1515
import cc.unitmesh.llm.NamedModelConfig
1616
import com.intellij.openapi.Disposable
17+
import com.intellij.openapi.application.ApplicationManager
18+
import com.intellij.openapi.diagnostic.Logger
1719
import com.intellij.openapi.editor.ex.EditorEx
1820
import com.intellij.openapi.project.Project
1921
import com.intellij.openapi.util.Disposer
22+
import kotlinx.coroutines.Dispatchers
2023
import kotlinx.coroutines.launch
24+
import kotlinx.coroutines.withContext
2125
import org.jetbrains.jewel.foundation.theme.JewelTheme
2226
import java.awt.BorderLayout
2327
import java.awt.Dimension
@@ -38,6 +42,8 @@ import javax.swing.JPanel
3842
*
3943
* Layout: Unified border around the entire input area for a cohesive look.
4044
*/
45+
private val inputAreaLogger = Logger.getInstance("IdeaDevInInputArea")
46+
4147
@Composable
4248
fun IdeaDevInInputArea(
4349
project: Project,
@@ -180,20 +186,35 @@ fun IdeaDevInInputArea(
180186
onStopClick = onAbort,
181187
onPromptOptimizationClick = {
182188
val currentText = devInInput?.text?.trim() ?: inputText.trim()
189+
inputAreaLogger.info("Prompt optimization clicked, text length: ${currentText.length}")
190+
183191
if (currentText.isNotBlank() && !isEnhancing && !isProcessing) {
184192
isEnhancing = true
185-
scope.launch {
193+
scope.launch(Dispatchers.IO) {
186194
try {
195+
inputAreaLogger.info("Starting prompt enhancement...")
187196
val enhancer = IdeaPromptEnhancer.getInstance(project)
188197
val enhanced = enhancer.enhance(currentText)
189-
if (enhanced != currentText) {
190-
devInInput?.setText(enhanced)
191-
inputText = enhanced
198+
inputAreaLogger.info("Enhancement completed, result length: ${enhanced.length}")
199+
200+
if (enhanced != currentText && enhanced.isNotBlank()) {
201+
// Update UI on EDT
202+
withContext(Dispatchers.Main) {
203+
ApplicationManager.getApplication().invokeLater {
204+
devInInput?.replaceText(enhanced)
205+
inputText = enhanced
206+
inputAreaLogger.info("Text updated in input field")
207+
}
208+
}
209+
} else {
210+
inputAreaLogger.info("No enhancement made (same text or empty result)")
192211
}
193212
} catch (e: Exception) {
194-
// Silently fail - keep original text
213+
inputAreaLogger.error("Prompt enhancement failed: ${e.message}", e)
195214
} finally {
196-
isEnhancing = false
215+
withContext(Dispatchers.Main) {
216+
isEnhancing = false
217+
}
197218
}
198219
}
199220
}

0 commit comments

Comments
 (0)