Skip to content

Commit 77c49e6

Browse files
committed
feat(sketch): add command transpilation and tool integration #259
- Added `transpileCommand` method to `LanguageProcessor` for processing `BuiltinCommand` from `PsiFile`. - Integrated `AgentStateService` to handle tool commands in `AutoSketchMode`. - Updated `AgentState` to accept `BuiltinCommand` and map it to `AgentTool`.
1 parent 90490b8 commit 77c49e6

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

core/src/main/kotlin/cc/unitmesh/devti/observer/agent/AgentState.kt

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

33
import cc.unitmesh.devti.agent.tool.AgentTool
4+
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
45
import cc.unitmesh.devti.llms.custom.Message
56
import com.intellij.openapi.components.Service
67
import com.intellij.util.diff.Diff.Change
@@ -21,8 +22,10 @@ class AgentStateService {
2122
state = AgentState()
2223
}
2324

24-
fun addTools(tools: List<AgentTool>) {
25-
25+
fun addTools(tools: List<BuiltinCommand>) {
26+
state.usedTools = tools.map {
27+
AgentTool(it.commandName, it.description, "")
28+
}
2629
}
2730

2831
fun addChanges(fileName: String) {

core/src/main/kotlin/cc/unitmesh/devti/provider/devins/LanguageProcessor.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cc.unitmesh.devti.provider.devins
22

33
import cc.unitmesh.devti.agent.custom.model.CustomAgentConfig
4+
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
45
import com.intellij.openapi.extensions.ExtensionPointName
56
import com.intellij.openapi.project.Project
7+
import com.intellij.psi.PsiFile
68
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
79

810
data class CustomAgentContext(
@@ -26,6 +28,9 @@ interface LanguageProcessor {
2628
@RequiresBackgroundThread
2729
fun compile(project: Project, text: String): String
2830

31+
@RequiresBackgroundThread
32+
fun transpileCommand(project: Project, psiFile: PsiFile): List<BuiltinCommand>
33+
2934
companion object {
3035
val EP_NAME = ExtensionPointName<LanguageProcessor>("cc.unitmesh.languageProcessor")
3136

core/src/main/kotlin/cc/unitmesh/devti/sketch/AutoSketchMode.kt

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

33
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
4+
import cc.unitmesh.devti.observer.agent.AgentStateService
5+
import cc.unitmesh.devti.provider.devins.LanguageProcessor
46
import cc.unitmesh.devti.provider.toolchain.ToolchainFunctionProvider
57
import cc.unitmesh.devti.util.parser.CodeFence
8+
import com.intellij.openapi.application.invokeLater
69
import com.intellij.openapi.components.Service
710
import com.intellij.openapi.components.service
811
import com.intellij.openapi.diagnostic.logger
912
import com.intellij.openapi.project.Project
13+
import com.intellij.psi.PsiFileFactory
14+
import com.intellij.sql.psi.SqlFile
1015

1116
@Service(Service.Level.PROJECT)
12-
class AutoSketchMode() {
17+
class AutoSketchMode(val project: Project) {
1318
var isEnable: Boolean = false
1419

1520
fun start(text: String, listener: SketchInputListener) {
1621
val codeFenceList = CodeFence.parseAll(text)
17-
val devinCodeFence = codeFenceList.filter { it.language.displayName == "DevIn" }
22+
val devinCodeFence = codeFenceList.filter {
23+
it.language.displayName == "DevIn"
24+
}
25+
26+
val commands: MutableList<BuiltinCommand> = mutableListOf()
1827

1928
val allCode = devinCodeFence.filter {
2029
!it.text.contains("<DevinsError>") && (hasReadCommand(it) || hasToolchainFunctionCommand(it))
2130
}
2231

32+
invokeLater {
33+
val language = CodeFence.findLanguage("DevIn") ?: return@invokeLater
34+
commands += devinCodeFence.mapNotNull {
35+
val psiFile = PsiFileFactory.getInstance(project).createFileFromText(language, it.text)
36+
?: return@mapNotNull null
37+
38+
LanguageProcessor.devin()?.transpileCommand(project, psiFile) ?: emptyList()
39+
}.flatten()
40+
41+
project.getService(AgentStateService::class.java).addTools(commands)
42+
}
43+
2344
if (allCode.isEmpty()) return
2445

2546
val allCodeText = allCode.map { it.text }.distinct().joinToString("\n")

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/DevInsCompiler.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ class DevInsCompiler(
400400

401401
return textSegment.toString()
402402
}
403+
404+
companion object {
405+
fun transpileCommand(file: DevInFile): List<BuiltinCommand> {
406+
val result = file.children.mapNotNull { it ->
407+
when (it.elementType) {
408+
DevInTypes.USED -> {
409+
val used = it as DevInUsed
410+
val firstChild = used.firstChild
411+
val id = firstChild.nextSibling
412+
413+
return@mapNotNull when (firstChild.elementType) {
414+
DevInTypes.COMMAND_START -> {
415+
val originCmdName = id?.text ?: ""
416+
val command = BuiltinCommand.fromString(originCmdName)
417+
if (command == null) {
418+
CustomCommand.fromString(file.project, originCmdName) ?: return@mapNotNull null
419+
}
420+
421+
command
422+
}
423+
else -> null
424+
}
425+
}
426+
else -> null
427+
}
428+
}
429+
430+
return result
431+
}
432+
}
403433
}
404434

405435

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/provider/DevInsPromptProcessor.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
package cc.unitmesh.devti.language.provider
22

33
import cc.unitmesh.devti.AutoDevNotifications
4+
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
45
import cc.unitmesh.devti.language.DevInLanguage
56
import cc.unitmesh.devti.language.compiler.DevInsCompiler
67
import cc.unitmesh.devti.language.psi.DevInFile
78
import cc.unitmesh.devti.provider.devins.CustomAgentContext
89
import cc.unitmesh.devti.provider.devins.LanguageProcessor
910
import cc.unitmesh.devti.util.parser.CodeFence
1011
import com.intellij.openapi.application.runInEdt
12+
import com.intellij.openapi.application.runReadAction
1113
import com.intellij.openapi.editor.Editor
1214
import com.intellij.openapi.fileEditor.FileEditorManager
1315
import com.intellij.openapi.project.Project
1416
import com.intellij.psi.PsiElement
17+
import com.intellij.psi.PsiFile
1518
import com.intellij.psi.PsiWhiteSpace
1619
import com.intellij.psi.util.PsiUtilBase
1720

@@ -48,6 +51,15 @@ class DevInsPromptProcessor : LanguageProcessor {
4851
return result.output
4952
}
5053

54+
override fun transpileCommand(project: Project, psiFile: PsiFile): List<BuiltinCommand> {
55+
if (psiFile !is DevInFile) {
56+
return emptyList()
57+
}
58+
59+
val result = DevInsCompiler.transpileCommand(psiFile)
60+
return result
61+
}
62+
5163
/**
5264
* Creates a new instance of `DevInsCompiler`.
5365
*

0 commit comments

Comments
 (0)