Skip to content

Commit

Permalink
feat(action-test): add extContext support and RAG execution functiona…
Browse files Browse the repository at this point in the history
…lity #195

This commit introduces the `extContext` property to the `TestCodeGenContext` class to support custom context for AutoDev extensions. Additionally, it implements the execution of a Recurrent Agent (RAG) application within the `TestCodeGenTask` to process source code and enhance testing capabilities.
  • Loading branch information
phodal committed May 29, 2024
1 parent a0abb64 commit 6166f38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package cc.unitmesh.devti.intentions.action.task

import cc.unitmesh.devti.AutoDevBundle
import cc.unitmesh.devti.AutoDevNotifications
import cc.unitmesh.devti.agent.CustomAgentExecutor
import cc.unitmesh.devti.agent.configurable.customAgentSetting
import cc.unitmesh.devti.agent.model.CustomAgentConfig
import cc.unitmesh.devti.context.modifier.CodeModifierProvider
import cc.unitmesh.devti.gui.chat.ChatActionType
import cc.unitmesh.devti.intentions.action.test.TestCodeGenContext
Expand All @@ -28,6 +31,8 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiNameIdentifierOwner
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import kotlinx.serialization.decodeFromString

class TestCodeGenTask(val request: TestCodeGenRequest) :
Task.Backgroundable(request.project, AutoDevBundle.message("intentions.chat.code.test.name")) {
Expand Down Expand Up @@ -104,6 +109,7 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
}

testPromptContext.isNewFile = testContext.isNewFile
testPromptContext.extContext = getRAGContext(testPromptContext)

templateRender.context = testPromptContext
val prompter = templateRender.renderTemplate(template)
Expand Down Expand Up @@ -184,4 +190,39 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
companion object {
private val logger = logger<TestCodeGenTask>()
}

private fun getRAGContext(testPromptContext: TestCodeGenContext): String {
val agent = loadRagApp() ?: return ""

val query = testPromptContext.sourceCode
val stringFlow: Flow<String> = CustomAgentExecutor(project).execute(query, agent) ?: return ""

val responseBuilder = StringBuilder()
runBlocking {
stringFlow.collect { string ->
responseBuilder.append(string)
}
}
return responseBuilder.toString()
}

//this is hard code should be refactoring
private fun loadRagApp(): CustomAgentConfig? {
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig
if (ragsJsonConfig.isEmpty()) return null

val rags = try {
Json.decodeFromString<List<CustomAgentConfig>>(ragsJsonConfig)
} catch (e: Exception) {
logger.warn("Failed to parse custom rag apps", e)
listOf()
}

if (rags.isEmpty()) {
return null
}


return rags.filter { it.name == "@autodev.ext-context.test" }.firstOrNull()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,17 @@ data class TestCodeGenContext(
var sourceCode: String = "",
var testClassName: String = "",
var isNewFile: Boolean = true,
/**
* in issue [195](https://github.com/unit-mesh/auto-dev/issues/195), we introduction autodev ext context for
* user to introduce their own context, this is the context for autodev ext
* ```json
* {
* "name": "@autodev.ext-context.test"
* "description": "AutoTest",
* "url": "http://127.0.0.1:8765/api/agent/auto-test",
* "responseAction": "Direct"
* }
* ```
*/
var extContext: String = "",
) : TemplateContext

0 comments on commit 6166f38

Please sign in to comment.