Skip to content

Commit 1c53270

Browse files
committed
refactor(tool): replace ToolNames with ToolType throughout codebase #453
Replaces deprecated ToolNames constants with ToolType sealed class for tool identification and registration. Updates usages in agents, tools, tests, and JS exports for consistency and type safety. Removes legacy ToolNames references.
1 parent 7f67adf commit 1c53270

File tree

8 files changed

+41
-79
lines changed

8 files changed

+41
-79
lines changed

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/subagent/CodebaseInvestigatorAgent.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cc.unitmesh.agent.model.RunConfig
77
import cc.unitmesh.agent.model.ToolConfig
88
import cc.unitmesh.agent.tool.ToolResult
99
import cc.unitmesh.agent.tool.ToolNames
10+
import cc.unitmesh.agent.tool.ToolType
1011
import cc.unitmesh.llm.KoogLLMService
1112
import cc.unitmesh.llm.ModelConfig
1213
import kotlinx.serialization.Serializable
@@ -48,7 +49,7 @@ class CodebaseInvestigatorAgent(
4849
private val llmService: KoogLLMService
4950
) : SubAgent<InvestigationContext, ToolResult.AgentResult>(
5051
AgentDefinition(
51-
name = ToolNames.CODEBASE_INVESTIGATOR,
52+
name = ToolType.CodebaseInvestigator.name,
5253
displayName = "Codebase Investigator",
5354
description = "Analyzes codebase structure and provides insights using code analysis",
5455
promptConfig = PromptConfig(
@@ -192,9 +193,6 @@ class CodebaseInvestigatorAgent(
192193
)
193194
}
194195

195-
/**
196-
* Build a summary of the investigation results
197-
*/
198196
private fun buildSummary(query: String, findings: List<String>, recommendations: List<String>): String {
199197
return buildString {
200198
appendLine("### Investigation Summary")
@@ -220,10 +218,7 @@ class CodebaseInvestigatorAgent(
220218
}
221219
}
222220
}
223-
224-
/**
225-
* Analyze the investigation query to determine intent and extract keywords
226-
*/
221+
227222
private fun analyzeQuery(query: String): QueryAnalysis {
228223
val lowerQuery = query.lowercase()
229224

@@ -250,9 +245,6 @@ class CodebaseInvestigatorAgent(
250245
.filter { it.isNotBlank() }
251246
}
252247

253-
/**
254-
* Find relevant elements based on keywords and type
255-
*/
256248
private fun findRelevantElements(keywords: List<String>, elementType: String): List<String> {
257249
// Simplified implementation - in a real scenario, this would scan actual files
258250
val findings = mutableListOf<String>()
@@ -276,9 +268,6 @@ class CodebaseInvestigatorAgent(
276268
return findings
277269
}
278270

279-
/**
280-
* Analyze dependency patterns for given keywords
281-
*/
282271
private fun analyzeDependencyPatterns(keywords: List<String>): List<String> {
283272
val findings = mutableListOf<String>()
284273

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/subagent/ErrorRecoveryAgent.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import cc.unitmesh.agent.model.RunConfig
77
import cc.unitmesh.agent.platform.GitOperations
88
import cc.unitmesh.agent.tool.ToolResult
99
import cc.unitmesh.agent.tool.ToolNames
10+
import cc.unitmesh.agent.tool.ToolType
1011
import cc.unitmesh.llm.KoogLLMService
1112
import cc.unitmesh.llm.ModelConfig
1213
import kotlinx.serialization.Serializable
@@ -336,7 +337,7 @@ $context
336337

337338
companion object {
338339
private fun createDefinition() = AgentDefinition(
339-
name = ToolNames.ERROR_RECOVERY,
340+
name = ToolType.ErrorRecovery.name,
340341
displayName = "Error Recovery SubAgent",
341342
description = "Analyzes command failures and provides recovery plans",
342343
promptConfig = PromptConfig(
Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
package cc.unitmesh.agent.tool
22

3-
/**
4-
* Constants for tool names - DEPRECATED
5-
*
6-
* Use ToolType sealed class instead for type-safe tool handling.
7-
* This object is kept for backward compatibility.
8-
*
9-
* @deprecated Use ToolType sealed class instead
10-
*/
113
@Deprecated("Use ToolType sealed class instead", ReplaceWith("ToolType"))
124
object ToolNames {
13-
const val WRITE_FILE = "write-file"
14-
15-
// Search tools
16-
const val GREP = "grep"
17-
const val GLOB = "glob"
18-
19-
// Execution tools
20-
const val SHELL = "shell"
21-
22-
// SubAgent tools
23-
const val ERROR_RECOVERY = "error-recovery"
245
const val CODEBASE_INVESTIGATOR = "codebase-investigator"
256
}

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/tool/impl/GlobTool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class GlobTool(
234234
private val fileSystem: ToolFileSystem
235235
) : BaseExecutableTool<GlobParams, ToolResult>() {
236236

237-
override val name: String = ToolNames.GLOB
237+
override val name: String = ToolType.Glob.name
238238
override val description: String = """
239239
Find files and directories using glob patterns with wildcard support.
240240
Supports recursive search (**), character classes ([abc]), alternatives ({a,b}),

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/tool/impl/GrepTool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class GrepTool(
278278
private val fileSystem: ToolFileSystem
279279
) : BaseExecutableTool<GrepParams, ToolResult>() {
280280

281-
override val name: String = ToolNames.GREP
281+
override val name: String = ToolType.Grep.name
282282
override val description: String = """
283283
Search for patterns in file contents using regular expressions.
284284
Supports file filtering with include/exclude patterns, case-sensitive/insensitive search,

mpp-core/src/commonMain/kotlin/cc/unitmesh/devins/completion/providers/ToolBasedCommandCompletionProvider.kt

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package cc.unitmesh.devins.completion.providers
22

3-
import cc.unitmesh.agent.tool.ToolNames
43
import cc.unitmesh.agent.tool.ToolType
54
import cc.unitmesh.agent.tool.toToolType
65
import cc.unitmesh.agent.tool.registry.GlobalToolRegistry
@@ -32,46 +31,31 @@ class ToolBasedCommandCompletionProvider(
3231
return filterAndSort(completionItems, query)
3332
}
3433

35-
/**
36-
* 根据工具名称获取对应的图标 - 现在使用 ToolType 系统
37-
*/
38-
private fun getToolIcon(toolName: String): String {
39-
val toolType = toolName.toToolType()
40-
return toolType?.tuiEmoji ?: when (toolName) {
41-
// Fallback for legacy tools
42-
ToolType.ReadFile.name -> "📄"
43-
ToolType.WriteFile.name -> "✏️"
44-
"grep" -> "🔍"
45-
"glob" -> "🌐"
46-
"shell" -> "💻"
47-
else -> "🔧"
48-
}
49-
}
34+
private fun getToolIcon(toolName: String): String = toolName.toToolType()?.tuiEmoji ?: "🔧"
5035

51-
/**
52-
* 创建命令插入处理器
53-
* 根据命令类型决定插入空格还是冒号
54-
*/
5536
private fun createCommandInsertHandler(commandName: String): (String, Int) -> InsertResult {
5637
return { fullText, cursorPos ->
5738
val slashPos = fullText.lastIndexOf('/', cursorPos - 1)
5839
if (slashPos >= 0) {
59-
val before = fullText.substring(0, slashPos)
40+
val before = fullText.take(slashPos)
6041
val after = fullText.substring(cursorPos)
6142

62-
// 根据命令类型决定后缀
6343
val suffix = when {
64-
// 需要参数的命令使用冒号
65-
commandName in listOf("read-file", "write-file", "file", "write", "read") -> ":"
66-
// 其他命令使用空格
44+
commandName in listOf(
45+
ToolType.ReadFile.name,
46+
ToolType.WriteFile.name,
47+
ToolType.Glob.name,
48+
ToolType.Grep.name
49+
) -> ":"
50+
6751
else -> " "
6852
}
6953

7054
val newText = before + "/$commandName$suffix" + after
7155
InsertResult(
7256
newText = newText,
7357
newCursorPosition = before.length + commandName.length + 2,
74-
shouldTriggerNextCompletion = suffix == ":" // 如果是冒号,触发下一级补全
58+
shouldTriggerNextCompletion = suffix == ":"
7559
)
7660
} else {
7761
InsertResult(fullText, cursorPos)

mpp-core/src/commonTest/kotlin/cc/unitmesh/agent/tool/ToolRegistryTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class ToolRegistryTest {
2727
val allTools = registry.getAllTools()
2828
assertTrue(allTools.isNotEmpty(), "Should have registered tools")
2929
assertTrue(ToolType.ReadFile.name in allTools.keys, "Should contain read-file tool")
30-
assertTrue(ToolNames.WRITE_FILE in allTools.keys, "Should contain write-file tool")
31-
assertTrue(ToolNames.GREP in allTools.keys, "Should contain grep tool")
32-
assertTrue(ToolNames.GLOB in allTools.keys, "Should contain glob tool")
30+
assertTrue(ToolType.WriteFile.name in allTools.keys, "Should contain write-file tool")
31+
assertTrue(ToolType.Grep.name in allTools.keys, "Should contain grep tool")
32+
assertTrue(ToolType.Glob.name in allTools.keys, "Should contain glob tool")
3333
}
3434

3535
@Test
@@ -56,7 +56,7 @@ class ToolRegistryTest {
5656
@Test
5757
fun testHasToolNamed() {
5858
assertTrue(registry.hasToolNamed(ToolType.ReadFile.name), "Should have read-file tool")
59-
assertTrue(registry.hasToolNamed(ToolNames.WRITE_FILE), "Should have write-file tool")
59+
assertTrue(registry.hasToolNamed(ToolType.WriteFile.name), "Should have write-file tool")
6060
assertTrue(!registry.hasToolNamed("non-existent-tool"), "Should not have non-existent tool")
6161
}
6262

mpp-core/src/jsMain/kotlin/cc/unitmesh/llm/JsExports.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
package cc.unitmesh.llm
44

5+
import cc.unitmesh.agent.tool.ToolType
6+
import cc.unitmesh.agent.tool.filesystem.DefaultToolFileSystem
7+
import cc.unitmesh.agent.tool.impl.GrepParams
8+
import cc.unitmesh.agent.tool.impl.ShellParams
9+
import cc.unitmesh.agent.tool.registry.ToolRegistry
10+
import cc.unitmesh.agent.tool.shell.JsShellExecutor
11+
import cc.unitmesh.devins.compiler.DevInsCompilerFacade
512
import cc.unitmesh.devins.filesystem.EmptyFileSystem
613
import cc.unitmesh.devins.filesystem.ProjectFileSystem
714
import cc.unitmesh.devins.llm.Message
@@ -495,7 +502,7 @@ class JsDevInsCompiler {
495502
emptyMap()
496503
}
497504

498-
val result = cc.unitmesh.devins.compiler.DevInsCompilerFacade.compile(source, varsMap)
505+
val result = DevInsCompilerFacade.compile(source, varsMap)
499506

500507
JsDevInsResult(
501508
success = result.isSuccess(),
@@ -523,7 +530,7 @@ class JsDevInsCompiler {
523530
fun compileToString(source: String): Promise<String> {
524531
return GlobalScope.promise {
525532
try {
526-
cc.unitmesh.devins.compiler.DevInsCompilerFacade.compileToString(source)
533+
DevInsCompilerFacade.compileToString(source)
527534
} catch (e: Exception) {
528535
throw e
529536
}
@@ -571,12 +578,12 @@ private fun CompletionItem.toJsItem(triggerType: CompletionTriggerType, index: I
571578
*/
572579
@JsExport
573580
class JsToolRegistry(projectPath: String) {
574-
private val registry: cc.unitmesh.agent.tool.registry.ToolRegistry
581+
private val registry: ToolRegistry
575582

576583
init {
577-
val fileSystem = cc.unitmesh.agent.tool.filesystem.DefaultToolFileSystem(projectPath)
578-
val shellExecutor = cc.unitmesh.agent.tool.shell.JsShellExecutor()
579-
registry = cc.unitmesh.agent.tool.registry.ToolRegistry(fileSystem, shellExecutor)
584+
val fileSystem = DefaultToolFileSystem(projectPath)
585+
val shellExecutor = JsShellExecutor()
586+
registry = ToolRegistry(fileSystem, shellExecutor)
580587
}
581588

582589
/**
@@ -591,7 +598,7 @@ class JsToolRegistry(projectPath: String) {
591598
startLine = startLine,
592599
endLine = endLine
593600
)
594-
val result = registry.executeTool(cc.unitmesh.agent.tool.ToolType.ReadFile.name, params)
601+
val result = registry.executeTool(ToolType.ReadFile.name, params)
595602
result.toJsToolResult()
596603
} catch (e: Exception) {
597604
JsToolResult(
@@ -616,7 +623,7 @@ class JsToolRegistry(projectPath: String) {
616623
content = content,
617624
createDirectories = createDirectories
618625
)
619-
val result = registry.executeTool(cc.unitmesh.agent.tool.ToolNames.WRITE_FILE, params)
626+
val result = registry.executeTool(ToolType.WriteFile.name, params)
620627
result.toJsToolResult()
621628
} catch (e: Exception) {
622629
JsToolResult(
@@ -641,7 +648,7 @@ class JsToolRegistry(projectPath: String) {
641648
path = path,
642649
includeFileInfo = includeFileInfo
643650
)
644-
val result = registry.executeTool(cc.unitmesh.agent.tool.ToolNames.GLOB, params)
651+
val result = registry.executeTool(ToolType.Glob.name, params)
645652
result.toJsToolResult()
646653
} catch (e: Exception) {
647654
JsToolResult(
@@ -668,15 +675,15 @@ class JsToolRegistry(projectPath: String) {
668675
): Promise<JsToolResult> {
669676
return GlobalScope.promise {
670677
try {
671-
val params = cc.unitmesh.agent.tool.impl.GrepParams(
678+
val params = GrepParams(
672679
pattern = pattern,
673680
path = path,
674681
include = include,
675682
exclude = exclude,
676683
recursive = recursive,
677684
caseSensitive = caseSensitive
678685
)
679-
val result = registry.executeTool(cc.unitmesh.agent.tool.ToolNames.GREP, params)
686+
val result = registry.executeTool(ToolType.Grep.name, params)
680687
result.toJsToolResult()
681688
} catch (e: Exception) {
682689
JsToolResult(
@@ -700,12 +707,12 @@ class JsToolRegistry(projectPath: String) {
700707
): Promise<JsToolResult> {
701708
return GlobalScope.promise {
702709
try {
703-
val params = cc.unitmesh.agent.tool.impl.ShellParams(
710+
val params = ShellParams(
704711
command = command,
705712
workingDirectory = workingDirectory,
706713
timeoutMs = timeoutMs.toLong()
707714
)
708-
val result = registry.executeTool(cc.unitmesh.agent.tool.ToolNames.SHELL, params)
715+
val result = registry.executeTool(ToolType.Shell.name, params)
709716
result.toJsToolResult()
710717
} catch (e: Exception) {
711718
JsToolResult(

0 commit comments

Comments
 (0)