Skip to content

Commit e089d02

Browse files
committed
feat(custom-agent): add custom agent support #51
This commit introduces support for custom agents in the project. It includes the necessary changes to the `CustomAgentHandler` class to execute queries against a custom agent server. The `CustomAgentChatProcessor` and `AutoDevInputSection` classes have been modified to integrate with the new custom agent functionality. Additionally, the `CoUnitSettingService` has been updated to provide access to the custom agent settings.
1 parent 197b069 commit e089d02

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

src/main/kotlin/cc/unitmesh/devti/counit/CustomAgentChatProcessor.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class CustomAgentChatProcessor(val project: Project) {
2727
val request = originPrompt.trim()
2828
val selectedAgent: CustomAgentConfig = ui.getSelectedCustomAgent()
2929

30-
val response = customAgentHandler.executeQuery(request, selectedAgent)
30+
val response = customAgentHandler.execute(request, selectedAgent)
3131
if (response == null) {
32-
logger.error("can not find intention for request: $request")
32+
logger.error("error for custom agent: $selectedAgent with request: $request")
3333
return
3434
}
3535

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
package cc.unitmesh.devti.counit
22

3+
import cc.unitmesh.devti.counit.configurable.customAgentSetting
34
import com.intellij.openapi.components.Service
45
import com.intellij.openapi.project.Project
6+
import okhttp3.MediaType.Companion.toMediaTypeOrNull
7+
import okhttp3.OkHttpClient
8+
import okhttp3.Request
9+
import okhttp3.RequestBody
10+
import java.time.Duration
511

612
@Service(Service.Level.PROJECT)
713
class CustomAgentHandler(val project: Project) {
8-
fun executeQuery(input: String, selectedAgent: Any): String? {
9-
return null
14+
private var client = OkHttpClient()
15+
16+
fun execute(input: String, selectedAgent: Any): String? {
17+
val serverAddress = project.customAgentSetting.serverAddress ?: return null
18+
val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), input)
19+
val builder = Request.Builder()
20+
21+
client = client.newBuilder().build()
22+
val call = client.newCall(builder.url(serverAddress).post(body).build())
23+
24+
call.execute().use { response ->
25+
if (!response.isSuccessful) {
26+
return null
27+
}
28+
return response.body?.string()
29+
}
1030
}
1131
}

src/main/kotlin/cc/unitmesh/devti/counit/configurable/CoUnitSettingService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package cc.unitmesh.devti.counit.configurable
33
import com.intellij.openapi.components.*
44
import com.intellij.openapi.project.Project
55

6-
val Project.customRagSettings: CoUnitProjectSettingsService
6+
val Project.customAgentSetting: CoUnitProjectSettingsService
77
get() = service<CoUnitProjectSettingsService>()
88

99
@Service(Service.Level.PROJECT)

src/main/kotlin/cc/unitmesh/devti/gui/chat/AutoDevInputSection.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cc.unitmesh.devti.gui.chat
22

33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
5-
import cc.unitmesh.devti.counit.configurable.customRagSettings
5+
import cc.unitmesh.devti.counit.configurable.customAgentSetting
66
import cc.unitmesh.devti.counit.model.CustomAgentConfig
77
import cc.unitmesh.devti.llms.tokenizer.Tokenizer
88
import cc.unitmesh.devti.llms.tokenizer.TokenizerImpl
@@ -33,7 +33,6 @@ import com.intellij.util.ui.JBEmptyBorder
3333
import com.intellij.util.ui.JBUI
3434
import com.intellij.util.ui.UIUtil
3535
import com.intellij.util.ui.components.BorderLayoutPanel
36-
import kotlinx.serialization.decodeFromString
3736
import kotlinx.serialization.json.Json
3837
import java.awt.Color
3938
import java.awt.Component
@@ -55,7 +54,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
5554
private val buttonPresentation: Presentation
5655
private val button: ActionButton
5756

58-
private val defaultRag: CustomAgentConfig = CustomAgentConfig("Normal", "Normal")
57+
private val defaultRag: CustomAgentConfig = CustomAgentConfig("<Select Custom Agent>", "Normal")
5958
private var customRag: ComboBox<CustomAgentConfig> = ComboBox(MutableCollectionComboBoxModel(listOf()))
6059

6160
private val logger = logger<AutoDevInputSection>()
@@ -118,7 +117,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
118117
)
119118
layoutPanel.setOpaque(false)
120119

121-
if (project.customRagSettings.enableCustomRag) {
120+
if (project.customAgentSetting.enableCustomRag) {
122121
customRag = ComboBox(MutableCollectionComboBoxModel(loadRagApps()))
123122
customRag.setRenderer(SimpleListCellRenderer.create { label: JBLabel, value: CustomAgentConfig?, _: Int ->
124123
if (value != null) {
@@ -151,7 +150,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
151150

152151

153152
private fun loadRagApps(): List<CustomAgentConfig> {
154-
val ragsJsonConfig = project.customRagSettings.ragsJsonConfig
153+
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig
155154
if (ragsJsonConfig.isEmpty()) return listOf(defaultRag)
156155

157156
val rags = try {
@@ -212,7 +211,7 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
212211
}
213212

214213
fun hasSelectedAgent(): Boolean {
215-
if (!project.customRagSettings.enableCustomRag) return false
214+
if (!project.customAgentSetting.enableCustomRag) return false
216215
if (customRag.selectedItem == null) return false
217216
return customRag.selectedItem != defaultRag
218217
}

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cc.unitmesh.cf.core.llms.LlmMsg
44
import cc.unitmesh.devti.AutoDevBundle
55
import cc.unitmesh.devti.util.LLMCoroutineScope
66
import cc.unitmesh.devti.counit.CustomAgentChatProcessor
7-
import cc.unitmesh.devti.counit.configurable.customRagSettings
7+
import cc.unitmesh.devti.counit.configurable.customAgentSetting
88
import cc.unitmesh.devti.llms.LlmFactory
99
import cc.unitmesh.devti.util.parser.PostCodeProcessor
1010
import cc.unitmesh.devti.provider.ContextPrompter
@@ -34,7 +34,7 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
3434
val requestPrompt = prompter.requestPrompt()
3535
val displayPrompt = prompter.displayPrompt()
3636

37-
if (project.customRagSettings.enableCustomRag && ui.hasSelectedCustomAgent()) {
37+
if (project.customAgentSetting.enableCustomRag && ui.hasSelectedCustomAgent()) {
3838
counitProcessor.handleChat(prompter, ui, context)
3939
return
4040
}

0 commit comments

Comments
 (0)