Skip to content

Commit 3a999f7

Browse files
committed
feat(agent): add token tracking to CodingAgentRenderer #453
Introduce updateTokenInfo to CodingAgentRenderer for direct token updates, replacing reflection-based invocation and enabling consistent token tracking across renderers.
1 parent 1554a39 commit 3a999f7

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,7 @@ class CodingAgentExecutor(
5353

5454
// Set up token tracking callback to update renderer
5555
conversationManager?.onTokenUpdate = { tokenInfo ->
56-
// Update renderer if it supports token tracking
57-
try {
58-
val updateMethod = renderer::class.members.find { it.name == "updateTokenInfo" }
59-
updateMethod?.call(renderer, tokenInfo)
60-
} catch (e: Exception) {
61-
// Renderer doesn't support token tracking, ignore
62-
}
56+
renderer.updateTokenInfo(tokenInfo)
6357
}
6458

6559
val initialUserMessage = buildInitialUserMessage(task)

mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/render/CodingAgentRenderer.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cc.unitmesh.agent.render
22

3+
import cc.unitmesh.llm.compression.TokenInfo
4+
35
interface CodingAgentRenderer {
46
fun renderIterationHeader(current: Int, max: Int)
57
fun renderLLMResponseStart()
@@ -22,6 +24,8 @@ interface CodingAgentRenderer {
2224

2325
fun renderRecoveryAdvice(recoveryAdvice: String)
2426

27+
fun updateTokenInfo(tokenInfo: TokenInfo) {}
28+
2529
fun renderUserConfirmationRequest(toolName: String, params: Map<String, Any>)
2630

2731
fun addLiveTerminal(

mpp-ui/src/commonMain/kotlin/cc/unitmesh/devins/ui/compose/agent/ComposeRenderer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ class ComposeRenderer : BaseRenderer() {
581581
* Update token information from LLM response
582582
* Called when StreamFrame.End is received with token metadata
583583
*/
584-
fun updateTokenInfo(tokenInfo: TokenInfo) {
584+
override fun updateTokenInfo(tokenInfo: TokenInfo) {
585585
_lastMessageTokenInfo = tokenInfo
586586
// Accumulate total tokens
587587
_totalTokenInfo = TokenInfo(

0 commit comments

Comments
 (0)