Skip to content

Commit 0f4ef52

Browse files
committed
feat(template): add overrideTemplate method && closed #54
This commit adds a new method `overrideTemplate` to the `TeamPromptsBuilder` class. This method allows retrieving an overridden template for a given filename. It first checks if there is an override template in the project's team prompts directory, and if found, returns the content of the override template. If no override template is found, it falls back to the default template. This method is useful for customizing templates on a per-project basis.
1 parent 7329ac6 commit 0f4ef52

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/main/kotlin/cc/unitmesh/devti/custom/team/TeamPromptsBuilder.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import com.intellij.openapi.vfs.VirtualFile
1010
@Service(Service.Level.PROJECT)
1111
class TeamPromptsBuilder(private val project: Project) {
1212
val settings = project.teamPromptsSettings
13+
val baseDir = settings.state.teamPromptsDir
14+
1315
fun default(): List<TeamPromptAction> {
14-
val path = settings.state.teamPromptsDir
15-
val promptsDir = project.guessProjectDir()?.findChild(path) ?: return emptyList()
16+
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return emptyList()
1617

1718
val filterPrompts = promptsDir.children.filter { it.name.endsWith(".vm") }
1819
return buildPrompts(filterPrompts)
1920
}
2021

2122
fun quickPrompts(): List<TeamPromptAction> {
22-
val baseDir = settings.state.teamPromptsDir
2323
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return emptyList()
2424
val quickPromptDir = promptsDir.findChild("quick") ?: return emptyList()
2525
val quickPromptFiles = quickPromptDir.children.filter { it.name.endsWith(".vm") }
@@ -38,6 +38,14 @@ class TeamPromptsBuilder(private val project: Project) {
3838
TeamPromptAction(promptName, actionPrompt)
3939
}
4040
}
41+
42+
fun overrideTemplate(pathPrefix: String, filename: String): String? {
43+
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return null
44+
val path = "$pathPrefix/$filename"
45+
46+
val overrideFile = promptsDir.findChild(path) ?: return null
47+
return runReadAction { overrideFile.inputStream.readBytes().toString(Charsets.UTF_8) }
48+
}
4149
}
4250

4351
data class TeamPromptAction(

src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
package cc.unitmesh.devti.template
22

33
import cc.unitmesh.cf.core.llms.LlmMsg
4+
import cc.unitmesh.devti.custom.team.TeamPromptsBuilder
45
import cc.unitmesh.template.TemplateRoleSplitter
6+
import com.intellij.openapi.project.ProjectManager
57
import org.apache.velocity.VelocityContext
68
import org.apache.velocity.app.Velocity
79
import java.io.StringWriter
810
import java.nio.charset.Charset
911

10-
class TemplateRender(pathPrefix: String) {
12+
class TemplateRender(val pathPrefix: String) {
1113
private val defaultPrefix: String = pathPrefix.trimEnd('/')
1214
private val velocityContext = VelocityContext()
1315
private val splitter = TemplateRoleSplitter()
1416
var context: Any = ""
1517

18+
19+
/**
20+
* Retrieves the template for a given filename.
21+
*
22+
* @param filename the name of the file for which the template is requested
23+
* @return the template string for the specified filename, or the default template if no override is found
24+
*/
25+
fun getTemplate(filename: String): String {
26+
val overrideTemplate = ProjectManager.getInstance().openProjects.firstOrNull().let {
27+
TeamPromptsBuilder(it!!).overrideTemplate(pathPrefix, filename)
28+
}
29+
30+
return overrideTemplate ?: getDefaultTemplate(filename)
31+
}
32+
1633
/**
1734
* Retrieves the template content from the specified file.
1835
*
1936
* @param filename the name of the file containing the template
2037
* @return the content of the template as a string
2138
* @throws TemplateNotFoundError if the specified file cannot be found
2239
*/
23-
fun getTemplate(filename: String): String {
40+
private fun getDefaultTemplate(filename: String): String {
2441
val path = "$defaultPrefix/$filename"
2542
val resourceUrl = javaClass.classLoader.getResource(path) ?: throw TemplateNotFoundError(path)
2643
val bytes = resourceUrl.readBytes()

0 commit comments

Comments
 (0)