feat(group-chat): format non-string tool outputs using ToolResultSummary
When tools return structured JSON payloads (like skill_view, read_file, web_search), casting event.data["result"] to String evaluates to null. Fall back to ToolResultSummary.formatToolResultSummary to format structured tool payloads into readable text when expanded in group chat cards.
This commit is contained in:
@@ -18,6 +18,7 @@ import com.m57.hermescontrol.data.ws.HermesWsClient
|
|||||||
import com.m57.hermescontrol.data.ws.WsEvent
|
import com.m57.hermescontrol.data.ws.WsEvent
|
||||||
import com.m57.hermescontrol.data.ws.WsMethods
|
import com.m57.hermescontrol.data.ws.WsMethods
|
||||||
import com.m57.hermescontrol.data.ws.toJsonElement
|
import com.m57.hermescontrol.data.ws.toJsonElement
|
||||||
|
import com.m57.hermescontrol.ui.chat.tool.ToolResultSummary
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.CoroutineDispatcher
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -143,6 +144,21 @@ class GroupChatViewModel(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun extractToolOutput(data: Map<String, Any?>?): String? {
|
||||||
|
if (data == null) return null
|
||||||
|
val rawStr =
|
||||||
|
(data["output"] as? String)
|
||||||
|
?: (data["stdout"] as? String)
|
||||||
|
?: (data["result"] as? String)
|
||||||
|
if (!rawStr.isNullOrBlank()) {
|
||||||
|
return rawStr
|
||||||
|
}
|
||||||
|
|
||||||
|
val resultElement = data["result"]?.toJsonElement() ?: data["output"]?.toJsonElement() ?: data.toJsonElement()
|
||||||
|
val summary = ToolResultSummary.formatToolResultSummary(resultElement)
|
||||||
|
return summary.ifBlank { null }
|
||||||
|
}
|
||||||
|
|
||||||
private fun observeWsEvents() {
|
private fun observeWsEvents() {
|
||||||
viewModelScope.launch(ioDispatcher) {
|
viewModelScope.launch(ioDispatcher) {
|
||||||
HermesWsClient.events.collect { event ->
|
HermesWsClient.events.collect { event ->
|
||||||
@@ -241,10 +257,7 @@ class GroupChatViewModel(
|
|||||||
val streamId = activeStreamMsgId[sid]
|
val streamId = activeStreamMsgId[sid]
|
||||||
val toolId = event.data?.get("tool_id") as? String
|
val toolId = event.data?.get("tool_id") as? String
|
||||||
val toolName = event.name
|
val toolName = event.name
|
||||||
val output =
|
val output = extractToolOutput(event.data)
|
||||||
(event.data?.get("output") as? String)
|
|
||||||
?: (event.data?.get("stdout") as? String)
|
|
||||||
?: (event.data?.get("result") as? String)
|
|
||||||
val exitCode =
|
val exitCode =
|
||||||
when (val rawCode = event.data?.get("exit_code")) {
|
when (val rawCode = event.data?.get("exit_code")) {
|
||||||
is Number -> rawCode.toInt()
|
is Number -> rawCode.toInt()
|
||||||
|
|||||||
Reference in New Issue
Block a user