Skip to content

Commit 2ce2e76

Browse files
committed
fix(devti): optimize diff repair prompt and streaming processing
- Update variable names for clarity (result -> code, intention -> originIntention) - Improve real-time and batch processing of LLM flow: - Add check for new code before invoking callback to reduce redundant calls - Enhance batch processing to handle partial results and avoid duplicates - Refactor createDiffRepairPrompt to use updated template rendering
1 parent de652f7 commit 2ce2e76

File tree

1 file changed

+24
-9
lines changed
  • core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch

1 file changed

+24
-9
lines changed

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,57 @@ object DiffRepair {
4141
val prompt = createDiffRepairPrompt(project, oldCode, patchedCode)
4242
val flow = LlmFactory.create(project, ModelType.FastApply).stream(prompt, "You are professional program", false)
4343

44-
processStreamBatch(project, flow) { result ->
45-
callback.invoke(result)
44+
processStreamBatch(project, flow) { code ->
45+
callback.invoke(code)
4646
}
4747
}
48-
48+
4949
private fun createDiffRepairPrompt(project: Project, oldCode: String, patchedCode: String): String {
5050
val templateRender = TemplateRender(GENIUS_CODE)
5151
val template = templateRender.getTemplate(TEMPLATE_NAME)
5252
val intention = project.getService(AgentStateService::class.java).buildOriginIntention()
53-
53+
5454
templateRender.context = DiffRepairContext(intention, patchedCode, oldCode)
5555
return templateRender.renderTemplate(template)
5656
}
5757

5858
private fun processStreamRealtime(project: Project, flow: Flow<String>, onCodeChange: (String) -> Unit) {
5959
AutoDevCoroutineScope.Companion.scope(project).launch {
6060
val suggestion = StringBuilder()
61+
var lastProcessedCode = ""
62+
6163
flow.cancellable().collect { char ->
6264
suggestion.append(char)
6365
val code = CodeFence.Companion.parse(suggestion.toString())
64-
if (code.text.isNotEmpty()) {
66+
if (code.text.isNotEmpty() && code.text != lastProcessedCode) {
67+
lastProcessedCode = code.text
6568
onCodeChange(code.text)
6669
}
6770
}
6871
}
6972
}
70-
73+
7174
private fun processStreamBatch(project: Project, flow: Flow<String>, onComplete: (String) -> Unit) {
7275
AutoDevCoroutineScope.Companion.scope(project).launch {
7376
val suggestion = StringBuilder()
77+
var lastProcessedCode = ""
78+
7479
flow.cancellable().collect { char ->
7580
suggestion.append(char)
81+
val code = CodeFence.Companion.parse(suggestion.toString())
82+
if (code.text.isNotEmpty() && code.text != lastProcessedCode) {
83+
lastProcessedCode = code.text
84+
}
85+
}
86+
87+
if (lastProcessedCode.isNotEmpty()) {
88+
onComplete(lastProcessedCode)
89+
} else {
90+
val code = CodeFence.Companion.parse(suggestion.toString())
91+
if (code.text.isNotEmpty()) {
92+
onComplete(code.text)
93+
}
7694
}
77-
78-
val code = CodeFence.Companion.parse(suggestion.toString())
79-
onComplete(code.text)
8095
}
8196
}
8297
}

0 commit comments

Comments
 (0)