From 31ffeeed229c8d63333195efe13704c6bb6eccec Mon Sep 17 00:00:00 2001 From: Jia Liu Date: Wed, 3 Apr 2024 09:27:03 +0800 Subject: [PATCH] feat: should return en as default prompts template if target language template not exist --- .../cc/unitmesh/devti/template/TemplateRender.kt | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt b/src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt index de8d9ef886..950c3317b0 100644 --- a/src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt +++ b/src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt @@ -7,6 +7,7 @@ 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.File import java.io.StringWriter import java.nio.charset.Charset @@ -51,13 +52,23 @@ class TemplateRender(val pathPrefix: String) { * @throws TemplateNotFoundError if the specified file cannot be found */ private fun getDefaultTemplate(filename: String): String { - val defaultPrefix = "$ROOT/${AutoDevSettingsState.language}/$pathPrefix".trimEnd('/') - val path = "$defaultPrefix/$filename" + val path = getDefaultFilePath(filename) val resourceUrl = javaClass.classLoader.getResource(path) ?: throw TemplateNotFoundError(path) val bytes = resourceUrl.readBytes() return String(bytes, Charset.forName("UTF-8")) } + private fun getDefaultFilePath(filename: String): String { + val languagePrefix = "$ROOT/${AutoDevSettingsState.language}/$pathPrefix".trimEnd('/') + val path = "$languagePrefix/$filename" + if (File(path).exists()) { + return path + } + + val defaultLanguagePrefix = "$ROOT/en/$pathPrefix".trimEnd('/') + return "$defaultLanguagePrefix/$filename" + } + fun buildMsgs(prompt: String): List { val msgs = splitter.split(prompt) val messages = LlmMsg.fromMap(msgs).toMutableList()