Skip to content

Commit d76bc07

Browse files
committed
feat(schema): add support for custom prompts schema
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.
1 parent c5fa199 commit d76bc07

File tree

6 files changed

+173
-8
lines changed

6 files changed

+173
-8
lines changed

src/233/main/resources/META-INF/autodev-core.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188

189189
<extensions defaultExtensionNs="JavaScript.JsonSchema">
190190
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevJsonSchemaProviderFactory"/>
191+
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevPromptJsonSchemaProviderFactory"/>
191192
</extensions>
192193

193194
<actions>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cc.unitmesh.devti.custom.schema
2+
3+
import com.intellij.openapi.project.Project
4+
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
5+
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
6+
7+
class AutoDevPromptJsonSchemaProviderFactory: JsonSchemaProviderFactory {
8+
override fun getProviders(project: Project): MutableList<JsonSchemaFileProvider> {
9+
return mutableListOf(CustomPromptsSchemaFileProvider(project))
10+
}
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cc.unitmesh.devti.custom.schema
2+
3+
import com.intellij.openapi.application.runReadAction
4+
import com.intellij.openapi.project.Project
5+
import com.intellij.openapi.vfs.VirtualFile
6+
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
7+
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
8+
import com.jetbrains.jsonSchema.extension.SchemaType
9+
10+
const val CUSTOM_PROMPTS_FILE_NAME = "AutoDevCustomsPromptFile.json"
11+
12+
class CustomPromptsSchemaFileProvider(project: Project) : JsonSchemaFileProvider {
13+
override fun isAvailable(file: VirtualFile): Boolean {
14+
return runReadAction { isAutoDevCustomAgentFile(file) }
15+
}
16+
17+
private fun isAutoDevCustomAgentFile(file: VirtualFile): Boolean {
18+
if (!file.isValid) {
19+
return false
20+
}
21+
22+
return file.name == CUSTOM_PROMPTS_FILE_NAME
23+
}
24+
25+
override fun getName(): String = "AutoDevCustomsPromptFile"
26+
27+
override fun getSchemaFile(): VirtualFile? {
28+
return JsonSchemaProviderFactory.getResourceFile(this::class.java, customPromptsSchemaFile)
29+
}
30+
31+
override fun getSchemaType(): SchemaType {
32+
return SchemaType.schema
33+
}
34+
35+
companion object {
36+
private const val customPromptsSchemaFile = "autodev-custom-prompts.json"
37+
}
38+
}

src/main/kotlin/cc/unitmesh/devti/settings/LLMSettingComponent.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package cc.unitmesh.devti.settings
22

33
import cc.unitmesh.devti.AutoDevBundle
4+
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME
5+
import cc.unitmesh.devti.custom.schema.CUSTOM_PROMPTS_FILE_NAME
46
import com.intellij.json.JsonLanguage
7+
import com.intellij.lang.Language
8+
import com.intellij.openapi.editor.Document
59
import com.intellij.openapi.editor.colors.EditorColorsUtil
610
import com.intellij.openapi.editor.ex.EditorEx
11+
import com.intellij.openapi.project.Project
712
import com.intellij.openapi.project.ProjectManager
13+
import com.intellij.psi.PsiFile
814
import com.intellij.ui.LanguageTextField
915
import com.intellij.ui.components.JBLabel
1016
import com.intellij.util.ui.FormBuilder
@@ -47,7 +53,17 @@ class LLMSettingComponent(private val settings: AutoDevSettingsState) {
4753

4854
val project = ProjectManager.getInstance().openProjects.firstOrNull()
4955
private val customEnginePrompt by lazy {
50-
object : LanguageTextField(JsonLanguage.INSTANCE, project, settings.customPrompts) {
56+
object : LanguageTextField(JsonLanguage.INSTANCE, project, settings.customPrompts,
57+
object : SimpleDocumentCreator() {
58+
override fun createDocument(value: String?, language: Language?, project: Project?): Document {
59+
return createDocument(value, language, project, this)
60+
}
61+
62+
override fun customizePsiFile(file: PsiFile?) {
63+
file?.name = CUSTOM_PROMPTS_FILE_NAME
64+
}
65+
}
66+
) {
5167
override fun createEditor(): EditorEx {
5268

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

59-
6075
val scheme = EditorColorsUtil.getColorSchemeForBackground(this.colorsScheme.defaultBackground)
6176
this.colorsScheme = this.createBoundColorSchemeDelegate(scheme)
77+
78+
val metrics: FontMetrics = getFontMetrics(font)
79+
val columnWidth = metrics.charWidth('m')
80+
setOneLineMode(false)
81+
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
6282
}
6383
}
64-
}.apply {
65-
val metrics: FontMetrics = getFontMetrics(font)
66-
val columnWidth = metrics.charWidth('m')
67-
setOneLineMode(false)
68-
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
6984
}
7085
}
7186

src/main/resources/cc/unitmesh/devti/custom/schema/autodev-custom-agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
}
3131
},
3232
"required": ["name", "url", "responseAction"],
33-
"additionalProperties": false
33+
"additionalProperties": true
3434
}
3535
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"properties": {
5+
"spec": {
6+
"type": "object",
7+
"properties": {
8+
"controller": {
9+
"type": "string"
10+
},
11+
"service": {
12+
"type": "string"
13+
},
14+
"entity": {
15+
"type": "string"
16+
},
17+
"repository": {
18+
"type": "string"
19+
},
20+
"ddl": {
21+
"type": "string"
22+
}
23+
},
24+
"required": [],
25+
"additionalProperties": true
26+
},
27+
"prompts": {
28+
"type": "array",
29+
"items": {
30+
"type": "object",
31+
"properties": {
32+
"title": {
33+
"type": "string"
34+
},
35+
"autoInvoke": {
36+
"type": "boolean"
37+
},
38+
"matchRegex": {
39+
"type": "string"
40+
},
41+
"priority": {
42+
"type": "integer"
43+
},
44+
"template": {
45+
"type": "string"
46+
}
47+
},
48+
"required": [
49+
"title",
50+
"template"
51+
]
52+
}
53+
},
54+
"documentations": {
55+
"type": "array",
56+
"items": {
57+
"type": "object",
58+
"properties": {
59+
"title": {
60+
"type": "string"
61+
},
62+
"prompt": {
63+
"type": "string"
64+
},
65+
"start": {
66+
"type": "string"
67+
},
68+
"end": {
69+
"type": "string"
70+
},
71+
"type": {
72+
"type": "string"
73+
},
74+
"example": {
75+
"type": "object",
76+
"properties": {
77+
"question": {
78+
"type": "string"
79+
},
80+
"answer": {
81+
"type": "string"
82+
}
83+
},
84+
"required": [
85+
"question",
86+
"answer"
87+
]
88+
}
89+
},
90+
"required": [
91+
"title",
92+
"prompt",
93+
"example"
94+
]
95+
}
96+
}
97+
},
98+
"required": [],
99+
"additionalProperties": true
100+
}

0 commit comments

Comments
 (0)