Skip to content

Commit

Permalink
feat(schema): add support for custom prompts schema
Browse files Browse the repository at this point in the history
This commit introduces support for custom prompts schema in the AutoDev platform. It adds a new schema provider factory, `AutoDevPromptJsonSchemaProviderFactory`, which creates a `CustomPromptsSchemaFileProvider` for each project. The `CustomPromptsSchemaFileProvider` is responsible for providing the custom prompts schema file, which is expected to have the name `AutoDevCustomsPromptFile.json`.

The commit also modifies the `LLMSettingComponent` to include a `LanguageTextField` for editing the custom prompts schema. This component is now initialized with a `SimpleDocumentCreator` that generates a document based on the provided value, language, and project, and also sets the file name to `CUSTOM_PROMPTS_FILE_NAME`.

Finally, a new extension is added to the `autodev-core.xml` file, which registers the `AutoDevPromptJsonSchemaProviderFactory` as a provider factory for the `JavaScript.JsonSchema` extension. This enables the custom prompts schema support in the IDE.
  • Loading branch information
phodal committed Mar 13, 2024
1 parent c5fa199 commit d76bc07
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/233/main/resources/META-INF/autodev-core.xml
Expand Up @@ -188,6 +188,7 @@

<extensions defaultExtensionNs="JavaScript.JsonSchema">
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevJsonSchemaProviderFactory"/>
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevPromptJsonSchemaProviderFactory"/>
</extensions>

<actions>
Expand Down
@@ -0,0 +1,11 @@
package cc.unitmesh.devti.custom.schema

import com.intellij.openapi.project.Project
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory

class AutoDevPromptJsonSchemaProviderFactory: JsonSchemaProviderFactory {
override fun getProviders(project: Project): MutableList<JsonSchemaFileProvider> {
return mutableListOf(CustomPromptsSchemaFileProvider(project))
}
}
@@ -0,0 +1,38 @@
package cc.unitmesh.devti.custom.schema

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
import com.jetbrains.jsonSchema.extension.SchemaType

const val CUSTOM_PROMPTS_FILE_NAME = "AutoDevCustomsPromptFile.json"

class CustomPromptsSchemaFileProvider(project: Project) : JsonSchemaFileProvider {
override fun isAvailable(file: VirtualFile): Boolean {
return runReadAction { isAutoDevCustomAgentFile(file) }
}

private fun isAutoDevCustomAgentFile(file: VirtualFile): Boolean {
if (!file.isValid) {
return false
}

return file.name == CUSTOM_PROMPTS_FILE_NAME
}

override fun getName(): String = "AutoDevCustomsPromptFile"

override fun getSchemaFile(): VirtualFile? {
return JsonSchemaProviderFactory.getResourceFile(this::class.java, customPromptsSchemaFile)
}

override fun getSchemaType(): SchemaType {
return SchemaType.schema
}

companion object {
private const val customPromptsSchemaFile = "autodev-custom-prompts.json"
}
}
29 changes: 22 additions & 7 deletions src/main/kotlin/cc/unitmesh/devti/settings/LLMSettingComponent.kt
@@ -1,10 +1,16 @@
package cc.unitmesh.devti.settings

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME
import cc.unitmesh.devti.custom.schema.CUSTOM_PROMPTS_FILE_NAME
import com.intellij.json.JsonLanguage
import com.intellij.lang.Language
import com.intellij.openapi.editor.Document
import com.intellij.openapi.editor.colors.EditorColorsUtil
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.psi.PsiFile
import com.intellij.ui.LanguageTextField
import com.intellij.ui.components.JBLabel
import com.intellij.util.ui.FormBuilder
Expand Down Expand Up @@ -47,7 +53,17 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {

val project = ProjectManager.getInstance().openProjects.firstOrNull()
private val customEnginePrompt by lazy {
object : LanguageTextField(JsonLanguage.INSTANCE, project, settings.customPrompts) {
object : LanguageTextField(JsonLanguage.INSTANCE, project, settings.customPrompts,
object : SimpleDocumentCreator() {
override fun createDocument(value: String?, language: Language?, project: Project?): Document {
return createDocument(value, language, project, this)
}

override fun customizePsiFile(file: PsiFile?) {
file?.name = CUSTOM_PROMPTS_FILE_NAME
}
}
) {
override fun createEditor(): EditorEx {

return super.createEditor().apply {
Expand All @@ -56,16 +72,15 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
setVerticalScrollbarVisible(true)
setPlaceholder(AutoDevBundle.message("autodev.custom.prompt.placeholder"))


val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground)
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme)

val metrics: FontMetrics = getFontMetrics(font)
val columnWidth = metrics.charWidth('m')
setOneLineMode(false)
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
}
}
}.apply {
val metrics: FontMetrics = getFontMetrics(font)
val columnWidth = metrics.charWidth('m')
setOneLineMode(false)
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
}
}

Expand Down
Expand Up @@ -30,6 +30,6 @@
}
},
"required": ["name", "url", "responseAction"],
"additionalProperties": false
"additionalProperties": true
}
}
@@ -0,0 +1,100 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"spec": {
"type": "object",
"properties": {
"controller": {
"type": "string"
},
"service": {
"type": "string"
},
"entity": {
"type": "string"
},
"repository": {
"type": "string"
},
"ddl": {
"type": "string"
}
},
"required": [],
"additionalProperties": true
},
"prompts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"autoInvoke": {
"type": "boolean"
},
"matchRegex": {
"type": "string"
},
"priority": {
"type": "integer"
},
"template": {
"type": "string"
}
},
"required": [
"title",
"template"
]
}
},
"documentations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"prompt": {
"type": "string"
},
"start": {
"type": "string"
},
"end": {
"type": "string"
},
"type": {
"type": "string"
},
"example": {
"type": "object",
"properties": {
"question": {
"type": "string"
},
"answer": {
"type": "string"
}
},
"required": [
"question",
"answer"
]
}
},
"required": [
"title",
"prompt",
"example"
]
}
}
},
"required": [],
"additionalProperties": true
}

0 comments on commit d76bc07

Please sign in to comment.