Skip to content

Commit

Permalink
feat: add nature language directory for support Chinese prompts template
Browse files Browse the repository at this point in the history
  • Loading branch information
jialiu-github committed Apr 2, 2024
1 parent fab0f21 commit fd6b889
Show file tree
Hide file tree
Showing 37 changed files with 248 additions and 16 deletions.
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
Expand All @@ -31,7 +32,7 @@ class GenerateEntityAction : ChatBaseIntention() {
if (editor == null || file == null) return
val selectedText = editor.selectionModel.selectedText ?: return

val templateRender = TemplateRender("genius/migration")
val templateRender = TemplateRender(GENIUS_MIGRATION)
val template = templateRender.getTemplate("gen-entity.vm")
templateRender.context = SqlMigrationContext(
lang = file.language.displayName,
Expand Down
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
Expand Down Expand Up @@ -38,7 +39,7 @@ class GenerateFunctionAction : ChatBaseIntention() {
if (editor == null || file == null) return
val selectedText = editor.selectionModel.selectedText ?: return

val templateRender = TemplateRender("genius/migration")
val templateRender = TemplateRender(GENIUS_MIGRATION)
val template = templateRender.getTemplate("gen-function.vm")
templateRender.context = SqlMigrationContext(
lang = file.language.displayName,
Expand Down
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.gui.sendToChatWindow
import cc.unitmesh.devti.intentions.action.base.ChatBaseIntention
import cc.unitmesh.devti.provider.ContextPrompter
import cc.unitmesh.devti.template.GENIUS_MIGRATION
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
Expand All @@ -30,7 +31,7 @@ class GenerateUnittestAction : ChatBaseIntention() {
if (editor == null || file == null) return
val selectedText = editor.selectionModel.selectedText ?: return

val templateRender = TemplateRender("genius/migration")
val templateRender = TemplateRender(GENIUS_MIGRATION)
val template = templateRender.getTemplate("gen-unittest.vm")
templateRender.context = SqlMigrationContext(
lang = file.language.displayName,
Expand Down
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.flow.TaskFlow
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
import cc.unitmesh.devti.llms.LLMProvider
import cc.unitmesh.devti.template.GENIUS_SQL
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.diagnostic.logger
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -43,7 +44,7 @@ class AutoSqlFlow(
}

private fun generateStepOnePrompt(context: AutoSqlContext, actions: DbContextActionProvider): String {
val templateRender = TemplateRender("genius/sql")
val templateRender = TemplateRender(GENIUS_SQL)
val template = templateRender.getTemplate("sql-gen-clarify.vm")

templateRender.context = context
Expand All @@ -60,7 +61,7 @@ class AutoSqlFlow(
actions: DbContextActionProvider,
tableInfos: List<String>
): String {
val templateRender = TemplateRender("genius/sql")
val templateRender = TemplateRender(GENIUS_SQL)
val template = templateRender.getTemplate("sql-gen-design.vm")

genSqlContext.tableInfos = actions.getTableColumns(tableInfos)
Expand Down
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.flow.TaskFlow
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
import cc.unitmesh.devti.llms.LLMProvider
import cc.unitmesh.devti.template.GENIUS_HARMONYOS
import cc.unitmesh.devti.template.TemplateRender
import kotlinx.coroutines.runBlocking

Expand All @@ -22,7 +23,7 @@ class AutoArkUiFlow(val panel: ChatCodingPanel, val llm: LLMProvider, val contex
}

private fun generateStepOnePrompt(context: AutoArkUiContext): String {
val templateRender = TemplateRender("genius/harmonyos")
val templateRender = TemplateRender(GENIUS_HARMONYOS)
val template = templateRender.getTemplate("arkui-clarify.vm")

templateRender.context = context
Expand All @@ -46,7 +47,7 @@ class AutoArkUiFlow(val panel: ChatCodingPanel, val llm: LLMProvider, val contex
}

private fun generateStepTwoPrompt(selectedComponents: List<String>): String {
val templateRender = TemplateRender("genius/harmonyos")
val templateRender = TemplateRender(GENIUS_HARMONYOS)
val template = templateRender.getTemplate("arkui-design.vm")

context.elements = selectedComponents.mapNotNull {
Expand Down
Expand Up @@ -4,6 +4,7 @@ import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.flow.TaskFlow
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
import cc.unitmesh.devti.llms.LLMProvider
import cc.unitmesh.devti.template.GENIUS_PAGE
import cc.unitmesh.devti.template.TemplateRender
import cc.unitmesh.ide.javascript.flow.model.AutoPageContext
import cc.unitmesh.ide.javascript.flow.model.DsComponent
Expand All @@ -24,7 +25,7 @@ class AutoPageFlow(val context: AutoPageContext, val panel: ChatCodingPanel, val
}

private fun generateStepOnePrompt(context: AutoPageContext): String {
val templateRender = TemplateRender("genius/page")
val templateRender = TemplateRender(GENIUS_PAGE)
val template = templateRender.getTemplate("page-gen-clarify.vm")

templateRender.context = context
Expand All @@ -48,7 +49,7 @@ class AutoPageFlow(val context: AutoPageContext, val panel: ChatCodingPanel, val
}

private fun generateStepTwoPrompt(selectedComponents: List<DsComponent>): String {
val templateRender = TemplateRender("genius/page")
val templateRender = TemplateRender(GENIUS_PAGE)
val template = templateRender.getTemplate("page-gen-design.vm")

context.pages = selectedComponents.map { it.format() }
Expand Down
Expand Up @@ -12,6 +12,7 @@ import cc.unitmesh.devti.provider.context.ChatContextProvider
import cc.unitmesh.devti.provider.context.ChatCreationContext
import cc.unitmesh.devti.provider.context.ChatOrigin
import cc.unitmesh.devti.settings.AutoDevSettingsState
import cc.unitmesh.devti.template.GENIUS_PRACTISES
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.dvcs.repo.Repository
import com.intellij.dvcs.repo.VcsRepositoryManager
Expand Down Expand Up @@ -96,7 +97,7 @@ open class CodeReviewAction : ChatBaseAction() {
}

fun doCodeReview(project: Project, context: CodeReviewContext) {
val templateRender = TemplateRender("genius/practises")
val templateRender = TemplateRender(GENIUS_PRACTISES)
val template = templateRender.getTemplate("code-review.vm")
templateRender.context = context
val messages = templateRender.buildMsgs(template)
Expand Down
Expand Up @@ -5,6 +5,7 @@ import cc.unitmesh.devti.AutoDevNotifications
import cc.unitmesh.devti.actions.chat.base.ChatBaseAction
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.llms.LlmFactory
import cc.unitmesh.devti.template.GENIUS_PRACTISES
import cc.unitmesh.devti.vcs.VcsPrompting
import cc.unitmesh.devti.statusbar.AutoDevStatus
import cc.unitmesh.devti.template.TemplateRender
Expand Down Expand Up @@ -149,7 +150,7 @@ class CommitMessageSuggestionAction : ChatBaseAction() {
}

private fun generateCommitMessage(diff: String, project: Project): String {
val templateRender = TemplateRender("genius/practises")
val templateRender = TemplateRender(GENIUS_PRACTISES)
val template = templateRender.getTemplate("gen-commit-msg.vm")

val historyExamples = try {
Expand Down
Expand Up @@ -9,6 +9,7 @@ import cc.unitmesh.devti.llms.LlmFactory
import cc.unitmesh.devti.util.parser.parseCodeFromString
import cc.unitmesh.devti.provider.AutoTestService
import cc.unitmesh.devti.provider.context.*
import cc.unitmesh.devti.template.GENIUS_CODE
import cc.unitmesh.devti.statusbar.AutoDevStatus
import cc.unitmesh.devti.statusbar.AutoDevStatusService
import cc.unitmesh.devti.template.TemplateRender
Expand Down Expand Up @@ -48,7 +49,7 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
val commenter = LanguageCommenters.INSTANCE.forLanguage(request.file.language) ?: null
val comment = commenter?.lineCommentPrefix ?: "//"

val templateRender = TemplateRender("genius/code")
val templateRender = TemplateRender(GENIUS_CODE)
val template = templateRender.getTemplate("test-gen.vm")

override fun run(indicator: ProgressIndicator) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/cc/unitmesh/devti/template/TemplateRender.kt
Expand Up @@ -2,15 +2,25 @@ package cc.unitmesh.devti.template

import cc.unitmesh.cf.core.llms.LlmMsg
import cc.unitmesh.devti.custom.team.TeamPromptsBuilder
import cc.unitmesh.devti.settings.AutoDevSettingsState
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

val ROOT = "genius"
val GENIUS_SRE = "/sre"
val GENIUS_MIGRATION = "/migration"
val GENIUS_SQL = "/sql"
val GENIUS_HARMONYOS = "/harmonyos"
val GENIUS_PAGE = "/page"
val GENIUS_PRACTISES = "/practises"
val GENIUS_CODE = "/code"
val GENIUS_CICD = "/cicd"
val GENIUS_ERROR = "/error"
class TemplateRender(val pathPrefix: String) {
private val defaultPrefix: String = pathPrefix.trimEnd('/')
private val velocityContext = VelocityContext()
private val splitter = TemplateRoleSplitter()
var context: Any = ""
Expand Down Expand Up @@ -41,6 +51,7 @@ 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}".trimEnd('/')
val path = "$defaultPrefix/$filename"
val resourceUrl = javaClass.classLoader.getResource(path) ?: throw TemplateNotFoundError(path)
val bytes = resourceUrl.readBytes()
Expand Down
Expand Up @@ -2,6 +2,7 @@ package cc.unitmesh.genius.actions

import cc.unitmesh.devti.custom.tasks.FileGenerateTask
import cc.unitmesh.devti.provider.BuildSystemProvider
import cc.unitmesh.devti.template.GENIUS_SRE
import cc.unitmesh.devti.template.TemplateRender
import cc.unitmesh.genius.actions.context.DevOpsContext
import com.intellij.openapi.actionSystem.AnAction
Expand All @@ -16,7 +17,7 @@ class GenerateDockerfileAction : AnAction("Generate Dockerfile") {
val project = e.project ?: return

val dockerContexts = BuildSystemProvider.guess(project)
val templateRender = TemplateRender("genius/sre")
val templateRender = TemplateRender(GENIUS_SRE)
templateRender.context = DevOpsContext.from(dockerContexts)
val template = templateRender.getTemplate("generate-dockerfile.vm")

Expand Down
Expand Up @@ -3,6 +3,7 @@ package cc.unitmesh.genius.actions
import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.custom.tasks.FileGenerateTask
import cc.unitmesh.devti.provider.BuildSystemProvider
import cc.unitmesh.devti.template.GENIUS_CICD
import cc.unitmesh.devti.template.TemplateRender
import cc.unitmesh.genius.actions.context.DevOpsContext
import com.intellij.openapi.actionSystem.AnAction
Expand Down Expand Up @@ -40,7 +41,7 @@ class GenerateGitHubActionsAction : AnAction(AutoDevBundle.message("action.new.g

// first, we need to guess language
val githubActions = BuildSystemProvider.guess(project);
val templateRender = TemplateRender("genius/cicd")
val templateRender = TemplateRender(GENIUS_CICD)
templateRender.context = DevOpsContext.from(githubActions)
val template = templateRender.getTemplate("generate-github-action.vm")

Expand Down
Expand Up @@ -4,6 +4,7 @@ package com.intellij.temporary.error
import com.intellij.temporary.AutoPsiUtils
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
import cc.unitmesh.devti.prompting.BasePromptText
import cc.unitmesh.devti.template.GENIUS_ERROR
import cc.unitmesh.devti.template.TemplateRender
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.vfs.VirtualFile
Expand Down Expand Up @@ -47,7 +48,7 @@ class ErrorPromptBuilder(private val maxLength: Int, private val tokenizer: Toke

val errorTextTrimmed = trimTextByTokenizer(errorText, maxLengthForPiece)

val templateRender = TemplateRender("genius/error")
val templateRender = TemplateRender(GENIUS_ERROR)
templateRender.context = ErrorContext(errorTextTrimmed, sourceCode)
val template = templateRender.getTemplate("fix-error.vm")
val prompt = templateRender.renderTemplate(template)
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions src/main/resources/genius/zh/cicd/generate-github-action.vm
@@ -0,0 +1,4 @@
Create build.yml YAML file for GitHub Action for build project and runs tests

- ${context.buildContext}
- OS: latest ubuntu version
Empty file.
26 changes: 26 additions & 0 deletions src/main/resources/genius/zh/code/test-gen.vm
@@ -0,0 +1,26 @@
Write unit test for following ${context.lang} code.

${context.frameworkedContext}

#if( $context.relatedClasses.length() > 0 )
${context.relatedClasses}
#end
#if( $context.currentClass.length() > 0 )
Here is current class information:
${context.currentClass}
#end

Here is the source code to be tested:

```$context.lang
${context.imports}
${context.sourceCode}
```

## if newFile
#if( $context.isNewFile )
Start method test code with Markdown code block here:
#else
Start ${context.testClassName} test code with Markdown code block here:
#end

30 changes: 30 additions & 0 deletions src/main/resources/genius/zh/error/fix-error.vm
@@ -0,0 +1,30 @@
As a helpful assistant with expertise in code debugging, your objective is to identify the roots of runtime problems by analyzing console logs and providing general solutions to fix the issues. When assisting users, follow these rules:

1. Always be helpful and professional.
2. Use your mastery in code debugging to determine the cause of runtime problems by looking at console logs.
3. Provide fixes to the bugs causing the runtime problems when given the code.
4. Ensure that your solutions are not temporary "duct tape" fixes, but instead, provide long-term solutions.
5. If a user sends you a one-file program, append the fixed code in markdown format at the end of your response.
This code will be extracted using re.findall(r"`{{3}}(\w*)\n([\S\s]+?)\n`{{3}}", model_response)
so adhere to this formatting strictly.
6. If you can fix the problem strictly by modifying the code, do so. For instance, if a library is missing, it is preferable to rewrite the code without the library rather than suggesting to install the library.
7. Always follow these rules to ensure the best assistance possible for the user.

Now, consider this user request:

"Please help me understand what the problem is and try to fix the code. Here's the console output and the program text:

Console output:
%s
Texts of programs:
%s
Provide a helpful response that addresses the user's concerns, adheres to the rules, and offers a solution for the runtime problem.

```
${context.errorText}
```

```
${context.soureCode}
```

21 changes: 21 additions & 0 deletions src/main/resources/genius/zh/page/page-gen-clarify.vm
@@ -0,0 +1,21 @@
You are a professional Frontend developer.
According to the user's requirements, you should choose the best components for the user in List.

- Framework: ${context.frameworks}
- Language: ${context.language}
- User component: ${context.componentNames}, ${context.pageNames}

For example:

- Question(requirements): Build a form for user to fill in their information.
- You should anwser: [Input, Select, Radio, Checkbox, Button, Form]

----

Here are the User requirements:

```markdown
${context.requirement}
```

Please choose the best Components for the user, just return the components names in a list, no explain.
29 changes: 29 additions & 0 deletions src/main/resources/genius/zh/page/page-gen-design.vm
@@ -0,0 +1,29 @@
You are a professional Frontend developer.
According to the user's requirements, and Components info, write Component for the user.

- User Components Infos: ${context.components}

For example:

- Question(requirements): Build a form for user to fill in their information.
// componentName: Form, props: { fields: [{name: 'name', type: 'text'}, {name: 'age', type: 'number'}] }
// componentName: Input, props: { name: 'name', type: 'text' }
// componentName: Input, props: { name: 'age', type: 'number' }
- Answer:
```react
<Form>
<Input name="name" type="text" />
<Input name="age" type="number" />
</Form>
```

----

Here are the requirements:

```markdown
${context.requirement}
```

Please write your code with Markdown syntax, no explanation is needed:

17 changes: 17 additions & 0 deletions src/main/resources/genius/zh/practises/code-review.vm
@@ -0,0 +1,17 @@
You are a seasoned software developer, and I'm seeking your expertise to review the following code:

- Focus on critical algorithms, logical flow, and design decisions within the code. Discuss how these changes impact the core functionality and the overall structure of the code.
- Identify and highlight any potential issues or risks introduced by these code changes. This will help reviewers pay special attention to areas that may require improvement or further analysis.
- Emphasize the importance of compatibility and consistency with the existing codebase. Ensure that the code adheres to the established standards and practices for code uniformity and long-term maintainability.

${context.frameworkContext}

#if($context.stories.isNotEmpty())
The following user stories are related to these changes:
${context.stories.joinToString("\n")}
#end

${context.diffContext}

As your Tech lead, I am only concerned with key code review issues. Please provide me with a critical summary.
Submit your key insights under 5 sentences in here:

0 comments on commit fd6b889

Please sign in to comment.