Skip to content

Commit 36c2a1b

Browse files
committed
refactor(state): remove ToolStateManager and related state tracking #453
Eliminates ToolStateManager and execution state tracking from ToolOrchestrator, simplifying tool execution flow and relocating ToolCall to its own file.
1 parent 156056a commit 36c2a1b

File tree

4 files changed

+29
-132
lines changed

4 files changed

+29
-132
lines changed

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/executor/CodingAgentExecutor.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@ import cc.unitmesh.agent.core.SubAgentManager
66
import cc.unitmesh.agent.tool.ToolResultFormatter
77
import cc.unitmesh.agent.orchestrator.ToolExecutionResult
88
import cc.unitmesh.agent.orchestrator.ToolOrchestrator
9-
import cc.unitmesh.agent.parser.ToolCallParser
10-
import cc.unitmesh.agent.recovery.ErrorRecoveryManager
119
import cc.unitmesh.agent.render.CodingAgentRenderer
1210
import cc.unitmesh.agent.state.ToolCall
1311
import cc.unitmesh.agent.state.ToolExecutionState
1412
import cc.unitmesh.agent.tool.ToolResult
1513
import cc.unitmesh.agent.tool.ToolType
1614
import cc.unitmesh.agent.tool.toToolType
1715
import cc.unitmesh.llm.KoogLLMService
18-
import kotlinx.coroutines.flow.cancellable
1916
import kotlinx.coroutines.yield
20-
import kotlinx.coroutines.async
21-
import kotlinx.coroutines.awaitAll
2217
import kotlinx.coroutines.coroutineScope
2318
import kotlinx.datetime.Clock
2419
import cc.unitmesh.agent.orchestrator.ToolExecutionContext as OrchestratorContext

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/orchestrator/ToolOrchestrator.kt

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import cc.unitmesh.agent.policy.PolicyEngine
88
import cc.unitmesh.agent.render.CodingAgentRenderer
99
import cc.unitmesh.agent.state.ToolCall
1010
import cc.unitmesh.agent.state.ToolExecutionState
11-
import cc.unitmesh.agent.state.ToolStateManager
1211
import cc.unitmesh.agent.tool.*
1312
import cc.unitmesh.agent.tool.impl.WriteFileTool
1413
import cc.unitmesh.agent.tool.registry.ToolRegistry
@@ -27,7 +26,6 @@ class ToolOrchestrator(
2726
private val registry: ToolRegistry,
2827
private val policyEngine: PolicyEngine,
2928
private val renderer: CodingAgentRenderer,
30-
private val stateManager: ToolStateManager = ToolStateManager(),
3129
private val mcpConfigService: McpToolConfigService? = null
3230
) {
3331
private val logger = getLogger("ToolOrchestrator")
@@ -43,16 +41,12 @@ class ToolOrchestrator(
4341
val toolCall = ToolCall.create(toolName, params)
4442
val startTime = Clock.System.now().toEpochMilliseconds()
4543

46-
// Update state to pending
47-
stateManager.updateState(ToolExecutionState.Pending(toolCall.id, toolCall))
48-
4944
try {
5045
// Check permissions
5146
val policyDecision = policyEngine.checkPermission(toolCall, context)
5247
when (policyDecision) {
5348
PolicyDecision.DENY -> {
5449
val error = "Tool execution denied by policy: $toolName"
55-
stateManager.updateState(ToolExecutionState.Failed(toolCall.id, error, 0))
5650
return ToolExecutionResult.failure(
5751
context.executionId, toolName, error, startTime, Clock.System.now().toEpochMilliseconds()
5852
)
@@ -67,10 +61,6 @@ class ToolOrchestrator(
6761
}
6862
}
6963

70-
// Update state to executing
71-
stateManager.updateState(ToolExecutionState.Executing(toolCall.id, startTime))
72-
73-
// Check for cancellation
7464
yield()
7565

7666
// **关键改动**: 检查是否是 Shell 工具且支持 PTY
@@ -183,12 +173,8 @@ class ToolOrchestrator(
183173
val errorMsg = getErrorMessage(result)
184174
ToolExecutionState.Failed(toolCall.id, errorMsg, endTime - startTime)
185175
}
186-
stateManager.updateState(finalState)
187-
188-
// 从 ToolResult 中提取 metadata
176+
189177
val metadata = result.extractMetadata()
190-
191-
// 如果是 live session,添加标记以便跳过输出渲染
192178
val finalMetadata = if (liveSession != null) {
193179
metadata + mapOf("isLiveSession" to "true", "sessionId" to liveSession.sessionId)
194180
} else {
@@ -209,8 +195,7 @@ class ToolOrchestrator(
209195
} catch (e: Exception) {
210196
val endTime = Clock.System.now().toEpochMilliseconds()
211197
val error = "Tool execution failed: ${e.message}"
212-
stateManager.updateState(ToolExecutionState.Failed(toolCall.id, error, endTime - startTime))
213-
198+
214199
return ToolExecutionResult.failure(
215200
context.executionId, toolName, error, startTime, endTime, context.currentRetry
216201
)
@@ -246,31 +231,7 @@ class ToolOrchestrator(
246231

247232
return results
248233
}
249-
250-
/**
251-
* Get current execution state
252-
*/
253-
fun getExecutionState(callId: String): ToolExecutionState? {
254-
return stateManager.getState(callId)
255-
}
256-
257-
/**
258-
* Get all execution states
259-
*/
260-
fun getAllExecutionStates(): Map<String, ToolExecutionState> {
261-
return stateManager.getAllStates()
262-
}
263-
264-
/**
265-
* Clear execution history
266-
*/
267-
fun clearHistory() {
268-
stateManager.clear()
269-
}
270234

271-
/**
272-
* Internal tool execution - delegates to registry
273-
*/
274235
private suspend fun executeToolInternal(
275236
toolName: String,
276237
params: Map<String, Any>,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cc.unitmesh.agent.state
2+
3+
import kotlinx.datetime.Clock
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* Represents a tool call to be executed
8+
*/
9+
@Serializable
10+
data class ToolCall(
11+
val id: String,
12+
val toolName: String,
13+
val params: Map<String, String>,
14+
val timestamp: Long = Clock.System.now().toEpochMilliseconds()
15+
) {
16+
companion object {
17+
private var counter = 0
18+
19+
fun create(toolName: String, params: Map<String, Any>): ToolCall {
20+
return ToolCall(
21+
id = "call_${Clock.System.now().toEpochMilliseconds()}_${++counter}",
22+
toolName = toolName,
23+
params = params.mapValues { it.value.toString() }
24+
)
25+
}
26+
}
27+
}

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/state/ToolExecutionState.kt

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cc.unitmesh.agent.state
22

33
import cc.unitmesh.agent.tool.ToolResult
44
import kotlinx.serialization.Serializable
5-
import kotlinx.datetime.Clock
65

76
/**
87
* Represents the execution state of a tool call
@@ -54,88 +53,3 @@ sealed class ToolExecutionState {
5453
) : ToolExecutionState()
5554
}
5655

57-
/**
58-
* Represents a tool call to be executed
59-
*/
60-
@Serializable
61-
data class ToolCall(
62-
val id: String,
63-
val toolName: String,
64-
val params: Map<String, String>,
65-
val timestamp: Long = Clock.System.now().toEpochMilliseconds()
66-
) {
67-
companion object {
68-
private var counter = 0
69-
70-
fun create(toolName: String, params: Map<String, Any>): ToolCall {
71-
return ToolCall(
72-
id = "call_${Clock.System.now().toEpochMilliseconds()}_${++counter}",
73-
toolName = toolName,
74-
params = params.mapValues { it.value.toString() }
75-
)
76-
}
77-
}
78-
}
79-
80-
/**
81-
* State manager for tracking tool execution states
82-
*/
83-
class ToolStateManager {
84-
private val states = mutableMapOf<String, ToolExecutionState>()
85-
private val history = mutableListOf<ToolExecutionState>()
86-
87-
/**
88-
* Update the state of a tool execution
89-
*/
90-
fun updateState(state: ToolExecutionState) {
91-
states[state.callId] = state
92-
history.add(state)
93-
}
94-
95-
/**
96-
* Get current state of a tool execution
97-
*/
98-
fun getState(callId: String): ToolExecutionState? {
99-
return states[callId]
100-
}
101-
102-
/**
103-
* Get all current states
104-
*/
105-
fun getAllStates(): Map<String, ToolExecutionState> {
106-
return states.toMap()
107-
}
108-
109-
/**
110-
* Get execution history
111-
*/
112-
fun getHistory(): List<ToolExecutionState> {
113-
return history.toList()
114-
}
115-
116-
/**
117-
* Clear all states and history
118-
*/
119-
fun clear() {
120-
states.clear()
121-
history.clear()
122-
}
123-
124-
/**
125-
* Get pending tool calls
126-
*/
127-
fun getPendingCalls(): List<ToolCall> {
128-
return states.values
129-
.filterIsInstance<ToolExecutionState.Pending>()
130-
.map { it.toolCall }
131-
}
132-
133-
/**
134-
* Get executing tool calls
135-
*/
136-
fun getExecutingCalls(): List<String> {
137-
return states.values
138-
.filterIsInstance<ToolExecutionState.Executing>()
139-
.map { it.callId }
140-
}
141-
}

0 commit comments

Comments
 (0)