Skip to content

Commit

Permalink
feat(gui): add support for custom agent file schema validation #101
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
phodal committed Mar 13, 2024
1 parent 60f39be commit d52cf76
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/233/main/resources/META-INF/autodev-core.xml
Expand Up @@ -186,6 +186,10 @@
<chatContextProvider implementation="cc.unitmesh.devti.provider.builtin.LanguageContextProvider"/>
</extensions>

<extensions defaultExtensionNs="JavaScript.JsonSchema">
<ProviderFactory implementation="cc.unitmesh.devti.gui.component.AutoDevJsonJsonSchemaProviderFactory"/>
</extensions>

<actions>
<group id="AutoDevIntentionsActionGroup" class="cc.unitmesh.devti.intentions.IntentionsActionGroup"
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT" searchable="false">
Expand Down
@@ -1,15 +1,36 @@
package cc.unitmesh.devti.gui.component

import com.intellij.json.JsonLanguage
import com.intellij.lang.Language
import com.intellij.openapi.application.runReadAction
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.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.ui.LanguageTextField
import com.jetbrains.jsonSchema.extension.JsonSchemaFileProvider
import com.jetbrains.jsonSchema.extension.JsonSchemaProviderFactory
import com.jetbrains.jsonSchema.extension.SchemaType
import java.awt.Dimension
import java.awt.FontMetrics

private const val CUSTOM_AGENT_NAME = "AutoDevCustomAgentFile.json"

class JsonLanguageField(private val myProject: Project, val value: String, private val placeholder: String) :
LanguageTextField(JsonLanguage.INSTANCE, myProject, value) {
LanguageTextField(JsonLanguage.INSTANCE, myProject, value,
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_AGENT_NAME
}
}
) {

override fun createEditor(): EditorEx {
return super.createEditor().apply {
setShowPlaceholderWhenFocused(true)
Expand All @@ -26,4 +47,38 @@ class JsonLanguageField(private val myProject: Project, val value: String, priva
preferredSize = Dimension(25 * columnWidth, 25 * metrics.height)
}
}
}
}

internal class AutoDevJsonJsonSchemaProviderFactory: JsonSchemaProviderFactory {
override fun getProviders(project: Project): MutableList<JsonSchemaFileProvider> {
return mutableListOf(AutoDevJsonSchemaFileProvider(project))
}
}

class AutoDevJsonSchemaFileProvider(val 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_AGENT_NAME
}

override fun getName(): String = "AutoDevCustomAgentFile"

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

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

companion object {
private const val schemaFileName = "autodev-custom-agent.json"
}
}
@@ -0,0 +1,35 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string",
"format": "uri"
},
"auth": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["Bearer"]
},
"token": {
"type": "string"
}
},
"required": ["type", "token"]
},
"responseAction": {
"type": "string",
"enum": ["Direct", "TextChunk", "WebView"]
}
},
"required": ["name", "url", "responseAction"],
"additionalProperties": false
}
}

0 comments on commit d52cf76

Please sign in to comment.