Skip to content

Commit

Permalink
refactor(agent): use centralized method to load custom rag apps #195
Browse files Browse the repository at this point in the history
Use centralized method `CustomAgentConfig.loadFromProject` to load custom rag apps in `AutoDevInputSection` and `TestCodeGenTask`. Remove duplicate code and improve code readability.
  • Loading branch information
phodal committed May 29, 2024
1 parent 0dfaa32 commit fe78ae3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class CoUnitProjectSettingsService(
val project: Project,
) : SimplePersistentStateComponent<CoUnitProjectSettingsService.CoUnitProjectSettings>(CoUnitProjectSettings()) {
val enableCustomRag: Boolean get() = state.enableCustomRag

/**
* Use [cc.unitmesh.devti.agent.model.CustomAgentConfig.loadFromProject]
*/
val ragsJsonConfig: String get() = state.agentJsonConfig

fun modify(action: (CoUnitProjectSettings) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,9 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
}

private fun loadRagApps(): List<CustomAgentConfig> {
val ragsJsonConfig = project.customAgentSetting.ragsJsonConfig
if (ragsJsonConfig.isEmpty()) return listOf(defaultRag)

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

if (rags.isEmpty()) return listOf(defaultRag)

return listOf(defaultRag) + rags
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,8 @@ class TestCodeGenTask(val request: TestCodeGenRequest) :
}

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
}

val rags = CustomAgentConfig.loadFromProject(project)
if (rags.isEmpty()) return null

return rags.firstOrNull { it.name == CustomExtContext.TextContext.agentName }
}
Expand Down

0 comments on commit fe78ae3

Please sign in to comment.