Skip to content

Commit d52cf76

Browse files
committed
feat(gui): add support for custom agent file schema validation #101
Add support for custom agent file schema validation by integrating a new JsonSchemaProviderFactory and a custom agent file provider. This enhancement ensures that the JSON files used for custom agent configurations adhere to a predefined schema, improving the reliability and consistency of the configuration files.
1 parent 60f39be commit d52cf76

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@
186186
<chatContextProvider implementation="cc.unitmesh.devti.provider.builtin.LanguageContextProvider"/>
187187
</extensions>
188188

189+
<extensions defaultExtensionNs="JavaScript.JsonSchema">
190+
<ProviderFactory implementation="cc.unitmesh.devti.gui.component.AutoDevJsonJsonSchemaProviderFactory"/>
191+
</extensions>
192+
189193
<actions>
190194
<group id="AutoDevIntentionsActionGroup" class="cc.unitmesh.devti.intentions.IntentionsActionGroup"
191195
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT" searchable="false">

src/main/kotlin/cc/unitmesh/devti/gui/component/JsonLanguageField.kt

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
package cc.unitmesh.devti.gui.component
22

33
import com.intellij.json.JsonLanguage
4+
import com.intellij.lang.Language
5+
import com.intellij.openapi.application.runReadAction
6+
import com.intellij.openapi.editor.Document
47
import com.intellij.openapi.editor.colors.EditorColorsUtil
58
import com.intellij.openapi.editor.ex.EditorEx
69
import com.intellij.openapi.project.Project
10+
import com.intellij.openapi.vfs.VirtualFile
11+
import com.intellij.psi.PsiFile
712
import com.intellij.ui.LanguageTextField
13+
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
14+
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
15+
import com.jetbrains.jsonSchema.extension.SchemaType
816
import java.awt.Dimension
917
import java.awt.FontMetrics
1018

19+
private const val CUSTOM_AGENT_NAME = "AutoDevCustomAgentFile.json"
20+
1121
class JsonLanguageField(private val myProject: Project, val value: String, private val placeholder: String) :
12-
LanguageTextField(JsonLanguage.INSTANCE, myProject, value) {
22+
LanguageTextField(JsonLanguage.INSTANCE, myProject, value,
23+
object : SimpleDocumentCreator() {
24+
override fun createDocument(value: String?, language: Language?, project: Project?): Document {
25+
return createDocument(value, language, project, this)
26+
}
27+
28+
override fun customizePsiFile(file: PsiFile?) {
29+
file?.name = CUSTOM_AGENT_NAME
30+
}
31+
}
32+
) {
33+
1334
override fun createEditor(): EditorEx {
1435
return super.createEditor().apply {
1536
setShowPlaceholderWhenFocused(true)
@@ -26,4 +47,38 @@ class JsonLanguageField(private val myProject: Project, val value: String, priva
2647
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
2748
}
2849
}
29-
}
50+
}
51+
52+
internal class AutoDevJsonJsonSchemaProviderFactory: JsonSchemaProviderFactory {
53+
override fun getProviders(project: Project): MutableList<JsonSchemaFileProvider> {
54+
return mutableListOf(AutoDevJsonSchemaFileProvider(project))
55+
}
56+
}
57+
58+
class AutoDevJsonSchemaFileProvider(val project: Project): JsonSchemaFileProvider {
59+
override fun isAvailable(file: VirtualFile): Boolean {
60+
return runReadAction { isAutoDevCustomAgentFile(file) }
61+
}
62+
63+
private fun isAutoDevCustomAgentFile(file: VirtualFile): Boolean {
64+
if (!file.isValid) {
65+
return false
66+
}
67+
68+
return file.name == CUSTOM_AGENT_NAME
69+
}
70+
71+
override fun getName(): String = "AutoDevCustomAgentFile"
72+
73+
override fun getSchemaFile(): VirtualFile? {
74+
return JsonSchemaProviderFactory.getResourceFile(this::class.java, schemaFileName)
75+
}
76+
77+
override fun getSchemaType(): SchemaType {
78+
return SchemaType.schema
79+
}
80+
81+
companion object {
82+
private const val schemaFileName = "autodev-custom-agent.json"
83+
}
84+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "array",
4+
"items": {
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string"
9+
},
10+
"url": {
11+
"type": "string",
12+
"format": "uri"
13+
},
14+
"auth": {
15+
"type": "object",
16+
"properties": {
17+
"type": {
18+
"type": "string",
19+
"enum": ["Bearer"]
20+
},
21+
"token": {
22+
"type": "string"
23+
}
24+
},
25+
"required": ["type", "token"]
26+
},
27+
"responseAction": {
28+
"type": "string",
29+
"enum": ["Direct", "TextChunk", "WebView"]
30+
}
31+
},
32+
"required": ["name", "url", "responseAction"],
33+
"additionalProperties": false
34+
}
35+
}

0 commit comments

Comments
 (0)