Skip to content

Commit c5e7fb5

Browse files
committed
fix(builder): 优化上下文提供者处理逻辑
- 修正上下文提供者处理逻辑,确保正确处理 `null` 值。 - 更新提示指令格式,使其更清晰。 - 恢复 `MethodContextProvider` 的正确顺序。 空行后详细解释: - 修正了 `firstNotNullOfOrNull` 的使用,确保返回 `null` 时不会影响后续逻辑。 - 调整了提示指令的格式,增加了更清晰的分隔和描述。 - 恢复了 `MethodContextProvider` 在上下文提供者列表中的正确位置。
1 parent bdf63f5 commit c5e7fb5

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

core/src/main/kotlin/cc/unitmesh/devti/custom/document/CustomLivingDocPromptBuilder.kt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,39 @@ class CustomLivingDocPromptBuilder(
1919
val instruction = StringBuilder(fallbackText)
2020

2121
var inOutString = ""
22-
this.contextProviders.firstNotNullOfOrNull { contextProvider ->
23-
when (val llmQueryContext = contextProvider.from(target)) {
24-
is MethodContext -> {
25-
inOutString = llmQueryContext.inputOutputString()
26-
}
22+
val context = contextProviders.firstNotNullOfOrNull { contextProvider ->
23+
val llmQueryContext = contextProvider.from(target)
24+
25+
if (llmQueryContext != null) {
26+
return@firstNotNullOfOrNull llmQueryContext
27+
}
28+
29+
return@firstNotNullOfOrNull null
30+
}
31+
32+
if (context != null) {
33+
val related = "\nHere is related context information of the method\n\n```${target.language}\n" + context.format() + "\n```\n"
34+
instruction.append(related)
35+
}
36+
37+
when (context) {
38+
is MethodContext -> {
39+
inOutString = context.inputOutputString()
2740
}
2841
}
2942

3043
if (inOutString.isNotEmpty()) {
31-
instruction.append("\nCompare this snippet: \n")
44+
instruction.append("\nInput and output: \n")
3245
instruction.append(inOutString)
3346
instruction.append("\n")
3447
}
3548

3649
val lang = target.language.displayName;
3750
if (config.example != null) {
38-
instruction.append("Q: ```$lang\n${config.example.question}\n```\n")
39-
instruction.append("A: ${config.example.answer}\n")
40-
instruction.append("Q: ```$lang\n${target.text}\n```\n")
41-
instruction.append("A: ")
51+
instruction.append("Question: ```$lang\n${config.example.question}\n```\n")
52+
instruction.append("Answer: ${config.example.answer}\n")
53+
instruction.append("Question: ```$lang\n${target.text}\n```\n")
54+
instruction.append("Answer: ")
4255
}
4356

4457
return@compute instruction.toString();

core/src/main/kotlin/cc/unitmesh/devti/intentions/action/task/LivingDocPromptBuilder.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ open class LivingDocPromptBuilder(
4646
private val logger = logger<LivingDocPromptBuilder>()
4747

4848
protected val contextProviders = listOf(
49-
VariableContextProvider(false, false, false),
49+
MethodContextProvider(true, false),
5050
ClassContextProvider(false),
51-
MethodContextProvider(true, false)
51+
VariableContextProvider(false, false, false)
5252
)
5353

54-
private fun contextInstruction(context: LLMCodeContext?): String? {
54+
protected fun contextInstruction(context: LLMCodeContext?): String? {
5555
return when (context) {
5656
is ClassContext -> classInstruction(context)
5757
is MethodContext -> methodInstruction(context)
@@ -93,7 +93,7 @@ open class LivingDocPromptBuilder(
9393

9494
// here is related context information of the method
9595

96-
val related = "\nHere is related context information of the method\n\n```${context.language}" + context.format() + "\n```\n"
96+
val related = "\nHere is related context information of the method\n\n```${context.language}\n" + context.format() + "\n```\n"
9797
return instruction.trimStart() + related
9898
}
9999

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pluginGroup = com.phodal.autodev
77
pluginName = AutoDev
88
pluginRepositoryUrl = https://github.com/unit-mesh/auto-dev
99
# SemVer format -> https://semver.org
10-
pluginVersion = 1.8.15
10+
pluginVersion = 1.8.16-SNAPSHOT
1111

1212
# Supported IDEs: idea, pycharm
1313
baseIDE=idea

0 commit comments

Comments
 (0)