Skip to content

Commit cf0e478

Browse files
committed
feat(diff-repair): include user intention in diff repair context
Add user's original intention to the diff repair context for better context awareness during code repair. This involves modifying the `DiffRepairContext` data class, updating template files, and adding a method in `AgentStateService` to retrieve the intention.
1 parent 821d4e3 commit cf0e478

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

core/src/main/kotlin/cc/unitmesh/devti/observer/agent/AgentStateService.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ class AgentStateService {
3535
return messages
3636
}
3737

38+
fun buildOriginIntention(): String? {
39+
val intention = state.messages
40+
.firstOrNull { it.role.lowercase() == "user" }
41+
?.content
42+
43+
if (intention != null) {
44+
state.originIntention = intention
45+
}
46+
47+
return intention
48+
}
49+
3850
fun resolveIssue() {
3951
// todo resolve issue
4052
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/DiffLangSketch.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import com.intellij.ui.components.panels.VerticalLayout
2828
import com.intellij.util.containers.MultiMap
2929
import com.intellij.util.ui.JBUI
3030
import java.awt.BorderLayout
31-
import javax.swing.Box
3231
import javax.swing.BoxLayout
3332
import javax.swing.JButton
3433
import javax.swing.JComponent

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/DiffRepair.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.devti.sketch.ui.patch
22

33
import cc.unitmesh.devti.llm2.model.ModelType
44
import cc.unitmesh.devti.llms.LlmFactory
5+
import cc.unitmesh.devti.observer.agent.AgentStateService
56
import cc.unitmesh.devti.template.GENIUS_CODE
67
import cc.unitmesh.devti.template.TemplateRender
78
import cc.unitmesh.devti.util.AutoDevCoroutineScope
@@ -19,7 +20,9 @@ fun applyDiffRepairSuggestion(project: Project, editor: Editor, oldCode: String,
1920
val templateRender = TemplateRender(GENIUS_CODE)
2021
val template = templateRender.getTemplate("repair-diff.vm")
2122

22-
templateRender.context = DiffRepairContext(oldCode, patchedCode)
23+
val intention = project.getService(AgentStateService::class.java).buildOriginIntention()
24+
25+
templateRender.context = DiffRepairContext(intention, patchedCode, oldCode)
2326
val prompt = templateRender.renderTemplate(template)
2427

2528
val flow: Flow<String> = LlmFactory.create(project, ModelType.FastApply).stream(prompt, "", false)

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffSketch.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ class SingleFileDiffSketch(
226226
}
227227

228228
data class DiffRepairContext(
229-
val oldCode: String,
229+
val intention: String?,
230230
val patchedCode: String,
231+
val oldCode: String,
231232
) : TemplateContext
232233

233234
fun VirtualFile.readText(): String {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Please according the diff to repair the code, and return the repaired final code.
22

3+
User origin intention: ${context.intention}
4+
35
Here is the original code: ${context.oldCode}
46

5-
Here is the patched code:
7+
Here is the failed patched context:
68

79
${context.patchedCode}

core/src/main/resources/genius/zh/code/repair-diff.vm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
请根据我的原始代码和失败的 patch,生成最终的完整代码。
22

3+
用户的原始意图是: ${context.intention}
4+
35
Here is the original code:
46

57
${context.oldCode}
68

7-
Here is the patched code:
9+
Here is the failed patched context:
810

911
${context.patchedCode}
1012

0 commit comments

Comments
 (0)