Skip to content

Commit

Permalink
feat(template): add overrideTemplate method && closed #54
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Jan 22, 2024
1 parent 7329ac6 commit 0f4ef52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Expand Up @@ -10,16 +10,16 @@ import com.intellij.openapi.vfs.VirtualFile
@Service(Service.Level.PROJECT)
class TeamPromptsBuilder(private val project: Project) {
val settings = project.teamPromptsSettings
val baseDir = settings.state.teamPromptsDir

fun default(): List<TeamPromptAction> {
val path = settings.state.teamPromptsDir
val promptsDir = project.guessProjectDir()?.findChild(path) ?: return emptyList()
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return emptyList()

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

fun quickPrompts(): List<TeamPromptAction> {
val baseDir = settings.state.teamPromptsDir
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return emptyList()
val quickPromptDir = promptsDir.findChild("quick") ?: return emptyList()
val quickPromptFiles = quickPromptDir.children.filter { it.name.endsWith(".vm") }
Expand All @@ -38,6 +38,14 @@ class TeamPromptsBuilder(private val project: Project) {
TeamPromptAction(promptName, actionPrompt)
}
}

fun overrideTemplate(pathPrefix: String, filename: String): String? {
val promptsDir = project.guessProjectDir()?.findChild(baseDir) ?: return null
val path = "$pathPrefix/$filename"

val overrideFile = promptsDir.findChild(path) ?: return null
return runReadAction { overrideFile.inputStream.readBytes().toString(Charsets.UTF_8) }
}
}

data class TeamPromptAction(
Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt
@@ -1,26 +1,43 @@
package cc.unitmesh.devti.template

import cc.unitmesh.cf.core.llms.LlmMsg
import cc.unitmesh.devti.custom.team.TeamPromptsBuilder
import cc.unitmesh.template.TemplateRoleSplitter
import com.intellij.openapi.project.ProjectManager
import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import java.io.StringWriter
import java.nio.charset.Charset

class TemplateRender(pathPrefix: String) {
class TemplateRender(val pathPrefix: String) {
private val defaultPrefix: String = pathPrefix.trimEnd('/')
private val velocityContext = VelocityContext()
private val splitter = TemplateRoleSplitter()
var context: Any = ""


/**
* Retrieves the template for a given filename.
*
* @param filename the name of the file for which the template is requested
* @return the template string for the specified filename, or the default template if no override is found
*/
fun getTemplate(filename: String): String {
val overrideTemplate = ProjectManager.getInstance().openProjects.firstOrNull().let {
TeamPromptsBuilder(it!!).overrideTemplate(pathPrefix, filename)
}

return overrideTemplate ?: getDefaultTemplate(filename)
}

/**
* Retrieves the template content from the specified file.
*
* @param filename the name of the file containing the template
* @return the content of the template as a string
* @throws TemplateNotFoundError if the specified file cannot be found
*/
fun getTemplate(filename: String): String {
private fun getDefaultTemplate(filename: String): String {
val path = "$defaultPrefix/$filename"
val resourceUrl = javaClass.classLoader.getResource(path) ?: throw TemplateNotFoundError(path)
val bytes = resourceUrl.readBytes()
Expand Down

0 comments on commit 0f4ef52

Please sign in to comment.