Skip to content

Commit c8f4393

Browse files
committed
feat(devti): add support for user-defined coding rules #349
- Implement hasRule function in ProjectRule class - Add rule parameter to SketchRunContext - Update context creation to include user-defined rules - Modify sketch.vm templates to display rule information - Update rule.devin example to show new rule usage
1 parent a6c26ce commit c8f4393

File tree

5 files changed

+21
-2
lines changed

5 files changed

+21
-2
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchRunContext.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import cc.unitmesh.devti.provider.context.ChatContextItem
88
import cc.unitmesh.devti.provider.context.ChatContextProvider
99
import cc.unitmesh.devti.provider.context.ChatCreationContext
1010
import cc.unitmesh.devti.provider.context.ChatOrigin
11+
import cc.unitmesh.devti.sketch.rule.ProjectRule
1112
import cc.unitmesh.devti.sketch.run.ShellUtil
1213
import cc.unitmesh.devti.template.context.TemplateContext
1314
import com.intellij.openapi.application.runInEdt
@@ -42,6 +43,7 @@ data class SketchRunContext(
4243
val frameworkContext: String = "",
4344
val buildTool: String = "",
4445
val searchTool: String = "localSearch",
46+
val rule: String = "",
4547
) : TemplateContext {
4648
companion object {
4749
suspend fun create(project: Project, myEditor: Editor?, input: String): SketchRunContext {
@@ -55,7 +57,7 @@ data class SketchRunContext(
5557
FileEditorManager.getInstance(project).selectedFiles.firstOrNull()
5658
}
5759
val psi = currentFile?.let { runReadAction { PsiManager.getInstance(project).findFile(it) } }
58-
val currentElement = editor?.let { runReadAction { psi?.findElementAt(it.caretModel.offset) } }
60+
val currentElement = editor?.let { runReadAction { psi?.findElementAt(it.caretModel.offset) } }
5961
val creationContext =
6062
ChatCreationContext(ChatOrigin.Intention, ChatActionType.CHAT, psi, listOf(), element = psi)
6163

@@ -68,6 +70,13 @@ data class SketchRunContext(
6870

6971
val otherFiles = FileEditorManager.getInstance(project).openFiles.filter { it != currentFile }
7072

73+
val rules = ProjectRule(project).getAllRules().map { it.nameWithoutExtension }
74+
val rule = if (rules.isNotEmpty()) {
75+
"- User custom coding rule: [" + rules.joinToString(",") { it } + "]"
76+
} else {
77+
""
78+
}
79+
7180
return SketchRunContext(
7281
currentFile = currentFile?.relativePath(project),
7382
currentElement = currentElement,
@@ -80,7 +89,8 @@ data class SketchRunContext(
8089
frameworkContext = ChatContextProvider.collectChatContextList(project, creationContext)
8190
.joinToString(",", transform = ChatContextItem::text),
8291
buildTool = buildTool,
83-
searchTool = lookupSearchTool()
92+
searchTool = lookupSearchTool(),
93+
rule = rule,
8494
)
8595
}
8696
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/rule/ProjectRule.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class ProjectRule(private val project: Project) {
2828
return file
2929
}
3030

31+
fun hasRule(filename: String): Boolean {
32+
val fullname = "$RULE_PATH/$filename.md"
33+
val file = project.lookupFile(fullname)
34+
return file != null
35+
}
36+
3137
/**
3238
* Get all available rules
3339
*/

core/src/main/resources/genius/en/code/sketch.vm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Your main goal is to follow the USER's instructions at each message.
1717
- The user's shell is ${context.shell}
1818
- User's workspace context is: ${context.frameworkContext}
1919
- Current time is: ${context.time}
20+
${context.rule}
2021

2122
You have tools at your disposal to solve the coding task. We design a DSL call DevIn for you to call tools. If the USER's
2223
task is general or you already know the answer, just respond without calling tools.

core/src/main/resources/genius/zh/code/sketch.vm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Here is basic information about the current workspace:
1414
- The user's shell is ${context.shell}
1515
- User's workspace context is: ${context.frameworkContext}
1616
- Current time is: ${context.time}
17+
${context.rule}
1718

1819
在编码之前,请确保您拥有足够的上下文信息。请遵循 DevIn <devin /> 指令编写代码,以节省用户的时间。请不要直接基于用户的问题进行编码,而是先通过
1920
tool calls 获取上下文信息来了解用户的代码库。
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
用户可以通过 rule 来自定义编程规范,以生成更符合自己需要的代码。
12
获取 service 相关的规则(将读取 promtps/rule/service.md 文件,请确保文件存在)
23
/rule:service

0 commit comments

Comments
 (0)