Skip to content

Commit

Permalink
feat: add custom AI engine setting for inlay code complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Jia Liu committed Mar 21, 2024
1 parent 268f309 commit 7de0431
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/222/main/resources/META-INF/autodev-core.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<extensions defaultExtensionNs="JavaScript.JsonSchema">
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevJsonSchemaProviderFactory"/>
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.AutoDevPromptJsonSchemaProviderFactory"/>
<ProviderFactory implementation="cc.unitmesh.devti.custom.schema.InlayCodePromptsJsonSchemaProviderFactory"/>
</extensions>

<extensionPoints>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cc.unitmesh.devti.agent.configurable

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.custom.schema.CUSTOM_AGENT_FILE_NAME
import cc.unitmesh.devti.fullWidthCell
import cc.unitmesh.devti.gui.component.JsonLanguageField
import com.intellij.openapi.Disposable
Expand All @@ -23,7 +24,8 @@ class CoUnitToolConfigurable(val project: Project) : BoundConfigurable(AutoDevBu
val languageField = JsonLanguageField(
project,
state::agentJsonConfig.toString(),
AutoDevBundle.message("counit.agent.json.placeholder")
AutoDevBundle.message("counit.agent.json.placeholder"),
CUSTOM_AGENT_FILE_NAME
)
fullWidthCell(languageField)
.bind(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 INLAY_PROMPTS_FILE_NAME = "AutoDevInlayCodeCompletePromptFile.json"

//todo: refactor provider to reduce duplicate code
class InlayCodeCompletePromptsSchemaFileProvider(project: Project) : JsonSchemaFileProvider {
override fun isAvailable(file: VirtualFile): Boolean {
return runReadAction { isMyFile(file) }
}

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

return file.name == INLAY_PROMPTS_FILE_NAME
}

override fun getName(): String = "AutoDevInlayCodeCompletePromptFile"

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

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

companion object {
private const val inlayPromptsSchemaFile = "auto-dev-inlay-code-complete-prompt-file.json"
}
}
Original file line number Diff line number Diff line change
@@ -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 InlayCodePromptsJsonSchemaProviderFactory: JsonSchemaProviderFactory {
override fun getProviders(project: Project): MutableList<JsonSchemaFileProvider> {
return mutableListOf(InlayCodeCompletePromptsSchemaFileProvider(project))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.intellij.ui.LanguageTextField
import java.awt.Dimension
import java.awt.FontMetrics

class JsonLanguageField(private val myProject: Project, val value: String, private val placeholder: String) :
class JsonLanguageField(private val myProject: Project, val value: String, private val placeholder: String, private val fileName: String) :
LanguageTextField(JsonLanguage.INSTANCE, myProject, value,
object : SimpleDocumentCreator() {
override fun createDocument(value: String?, language: Language?, project: Project?): Document {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package cc.unitmesh.devti.settings.coder

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.custom.schema.INLAY_PROMPTS_FILE_NAME
import cc.unitmesh.devti.fullWidthCell
import cc.unitmesh.devti.gui.component.JsonLanguageField
import cc.unitmesh.devti.settings.ResponseType
import com.intellij.openapi.components.service
import com.intellij.openapi.options.BoundConfigurable
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.DialogPanel
import com.intellij.ui.dsl.builder.*
import com.intellij.ui.dsl.builder.panel
import com.intellij.ui.dsl.builder.toMutableProperty
import com.intellij.util.containers.toArray
import javax.swing.JCheckBox
import javax.swing.JPasswordField
import javax.swing.JTextField

class AutoDevCoderConfigurable(project: Project) : BoundConfigurable(AutoDevBundle.message("settings.autodev.coder")) {
Expand All @@ -21,11 +28,22 @@ class AutoDevCoderConfigurable(project: Project) : BoundConfigurable(AutoDevBund
private val explainCodeField = JTextField()
private val refactorCodeField = JTextField()
private val fixIssueCodeField = JTextField()
private val generateTestField = JTextField()

private val useCustomAIEngineWhenInlayCodeComplete = JCheckBox()
.apply {
toolTipText = "You can use custom LLM to inlay complete code."
}
private val maxTokenLengthParam = JTextField()
private val delaySecondsParam: JTextField = JTextField()
private val customEngineResponseTypeParam: ComboBox<String> = ComboBox(ResponseType.values().map { it.name }.toArray(emptyArray()));
private val customEngineResponseFormatParam = JTextField()
private val customEngineRequestBodyFormatParam = JTextField()
private val customEngineServerParam = JTextField()
private val customEngineTokenParam = JPasswordField()
private val customEnginePrompt = JsonLanguageField(project, "", "Custom your prompt here", INLAY_PROMPTS_FILE_NAME)

private val generateTestField = JTextField()

val settings = project.service<AutoDevCoderSettingService>()
val state = settings.state.copy()

Expand Down Expand Up @@ -107,6 +125,78 @@ class AutoDevCoderConfigurable(project: Project) : BoundConfigurable(AutoDevBund
)
}

row(AutoDevBundle.message("settings.autodev.coder.delaySecondsParam")) {
fullWidthCell(delaySecondsParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::delaySecondsParam.toMutableProperty()
)
}

row(AutoDevBundle.message("settings.autodev.coder.maxTokenLengthParam")) {
fullWidthCell(maxTokenLengthParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::maxTokenLengthParam.toMutableProperty()
)
}

row(AutoDevBundle.message("settings.autodev.coder.customEngineResponseTypeParam")) {
fullWidthCell(customEngineResponseTypeParam)
.bind(
componentGet = { it.selectedItem?.toString() ?: ResponseType.SSE.name },
componentSet = { component, value -> component.selectedItem = value },
prop = state::customEngineResponseTypeParam.toMutableProperty()
)
}

row(AutoDevBundle.message("settings.autodev.coder.customEngineResponseFormatParam")) {
fullWidthCell(customEngineResponseFormatParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::customEngineResponseFormatParam.toMutableProperty()
)
}

row(AutoDevBundle.message("settings.autodev.coder.customEngineRequestBodyFormatParam")) {
fullWidthCell(customEngineRequestBodyFormatParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::customEngineRequestBodyFormatParam.toMutableProperty()
)
}

row(AutoDevBundle.message("settings.autodev.coder.customEngineServerParam")) {
fullWidthCell(customEngineServerParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::customEngineServerParam.toMutableProperty()
)
}
row(AutoDevBundle.message("settings.autodev.coder.customEngineTokenParam")) {
fullWidthCell(customEngineTokenParam)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::customEngineTokenParam.toMutableProperty()
)
}
row(AutoDevBundle.message("settings.autodev.coder.customEnginePrompt")){}
row() {
// TODO: spike better way for support 213 and 221
fullWidthCell(customEnginePrompt)
.bind(
componentGet = { it.text },
componentSet = { component, value -> component.text = value },
prop = state::customEnginePrompt.toMutableProperty()
)
}

onApply {
settings.modify {
it.recordingInLocal = state.recordingInLocal
Expand All @@ -117,6 +207,14 @@ class AutoDevCoderConfigurable(project: Project) : BoundConfigurable(AutoDevBund
it.fixIssueCode = state.fixIssueCode
it.generateTest = state.generateTest
it.useCustomAIEngineWhenInlayCodeComplete = state.useCustomAIEngineWhenInlayCodeComplete
it.delaySecondsParam = state.delaySecondsParam
it.maxTokenLengthParam = state.maxTokenLengthParam
it.customEngineResponseTypeParam = state.customEngineResponseTypeParam
it.customEngineResponseFormatParam = state.customEngineResponseFormatParam
it.customEngineRequestBodyFormatParam = state.customEngineRequestBodyFormatParam
it.customEngineServerParam = state.customEngineServerParam
it.customEngineTokenParam = state.customEngineTokenParam
it.customEnginePrompt = state.customEnginePrompt
it.noChatHistory = state.noChatHistory
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package cc.unitmesh.devti.settings.coder

import cc.unitmesh.devti.settings.LLMParam
import cc.unitmesh.devti.settings.MAX_TOKEN_LENGTH
import cc.unitmesh.devti.settings.ResponseType
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox

val Project.coderSetting: AutoDevCoderSettingService
get() = service<AutoDevCoderSettingService>()
Expand Down Expand Up @@ -31,6 +35,14 @@ class AutoDevCoderSettingService(
var generateTest: String by property("Generate test for \$lang code") { it.isEmpty() }

var useCustomAIEngineWhenInlayCodeComplete by property(false)
var maxTokenLengthParam: String by property(MAX_TOKEN_LENGTH.toString()) { it.isEmpty() }
var delaySecondsParam: String by property("") { it.isEmpty() }
var customEngineResponseTypeParam by property(ResponseType.SSE.name) { it.isEmpty() }
var customEngineResponseFormatParam by property("") { it.isEmpty() }
var customEngineRequestBodyFormatParam by property("") { it.isEmpty() }
var customEngineServerParam by property("") { it.isEmpty() }
var customEngineTokenParam by property("") { it.isEmpty() }
var customEnginePrompt by property("") { it.isEmpty() }
override fun copy(): AutoDevCoderSettings {
val state = AutoDevCoderSettings()
state.copyFrom(this)
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/messages/AutoDevBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ settings.autodev.coder.refactorCode=Refactor code
settings.autodev.coder.fixIssueCode=Fix issue
settings.autodev.coder.generateTest=Generate test
settings.autodev.coder.useCustomerAgentWhenInlayCodeComplete= Use Custormer Agent
settings.autodev.coder.delaySecondsParam=Quest Delay Seconds
settings.autodev.coder.maxTokenLengthParam=Max token length
settings.autodev.coder.customEngineResponseTypeParam=Custom Response Type
settings.autodev.coder.customEngineResponseFormatParam=Custom Response Format (Json Path)
settings.autodev.coder.customEngineRequestBodyFormatParam=Custom Request Body Format (Json)
settings.autodev.coder.customEngineServerParam=Custom Engine Server
settings.autodev.coder.customEngineTokenParam=Custom Engine Token
settings.autodev.coder.customEnginePrompt=Custom Engine Prompt (Json)

settings.welcome.message=Welcome to use AutoDev
settings.welcome.feature.context=Precise context-aware code generate and chat
Expand Down

0 comments on commit 7de0431

Please sign in to comment.