Link tool results to tool calls in chat UI

This commit is contained in:
OpenClaw committed 2026-03-07 08:45:08 +08:00
1 parent 38505e513a
commit 91b3b54b9d
5 files changed
+28 -2

No files matched your search

@@ -23,6 +23,7 @@ data class ChatMessage(
val status: String? = null, val status: String? = null,
val kind: MessageKind = MessageKind.Text, val kind: MessageKind = MessageKind.Text,
val toolName: String? = null, val toolName: String? = null,
val toolCallId: String? = null,
) )
enum class MessageRole { User, Assistant, System } enum class MessageRole { User, Assistant, System }
@@ -187,6 +187,7 @@ class RemoteOpenClawRepository(
status = dto.status ?: "streaming", status = dto.status ?: "streaming",
kind = dto.kind.toMessageKind(), kind = dto.kind.toMessageKind(),
toolName = dto.toolName, toolName = dto.toolName,
toolCallId = dto.toolCallId,
) )
messageStore = messageStore + (sessionId to next) messageStore = messageStore + (sessionId to next)
} }
@@ -202,6 +203,7 @@ class RemoteOpenClawRepository(
status = dto.status ?: "streaming", status = dto.status ?: "streaming",
kind = dto.kind.toMessageKind(), kind = dto.kind.toMessageKind(),
toolName = dto.toolName, toolName = dto.toolName,
toolCallId = dto.toolCallId,
) else msg ) else msg
} }
val exists = updated.any { it.id == dto.id } val exists = updated.any { it.id == dto.id }
@@ -213,6 +215,7 @@ class RemoteOpenClawRepository(
status = dto.status ?: "streaming", status = dto.status ?: "streaming",
kind = dto.kind.toMessageKind(), kind = dto.kind.toMessageKind(),
toolName = dto.toolName, toolName = dto.toolName,
toolCallId = dto.toolCallId,
) )
messageStore = messageStore + (sessionId to next) messageStore = messageStore + (sessionId to next)
updateSessionPreview(sessionId, next.lastOrNull()) updateSessionPreview(sessionId, next.lastOrNull())
@@ -350,6 +353,7 @@ class RemoteOpenClawRepository(
status = status, status = status,
kind = kind.toMessageKind(), kind = kind.toMessageKind(),
toolName = toolName, toolName = toolName,
toolCallId = toolCallId,
) )
} }
@@ -140,8 +140,19 @@ fun ChatScreen(
val shouldAnimate = index == messages.lastIndex && val shouldAnimate = index == messages.lastIndex &&
message.role == MessageRole.Assistant && message.role == MessageRole.Assistant &&
message.createdAt == "现在" message.createdAt == "现在"
val linkedTool = if (message.kind == MessageKind.ToolResult) {
messages.lastOrNull {
it.kind == MessageKind.Tool &&
it.toolCallId != null &&
it.toolCallId == message.toolCallId
}
} else null
MessageBubble(message = message, animateText = shouldAnimate) MessageBubble(
message = message,
animateText = shouldAnimate,
linkedToolName = linkedTool?.toolName,
)
} }
} }
} }
@@ -198,6 +209,7 @@ private fun RowScope.QuickActionButton(text: String, onClick: () -> Unit) {
private fun MessageBubble( private fun MessageBubble(
message: ChatMessage, message: ChatMessage,
animateText: Boolean = false, animateText: Boolean = false,
linkedToolName: String? = null,
) { ) {
val alignment = when (message.role) { val alignment = when (message.role) {
MessageRole.User -> Alignment.CenterEnd MessageRole.User -> Alignment.CenterEnd
@@ -289,7 +301,13 @@ private fun MessageBubble(
} }
isToolResult -> { isToolResult -> {
Text( Text(
text = if (message.status == "tool-error") "工具错误" else "工具结果", text = buildString {
append(if (message.status == "tool-error") "工具错误" else "工具结果")
if (!linkedToolName.isNullOrBlank()) {
append(" · ")
append(linkedToolName)
}
},
style = MaterialTheme.typography.titleSmall, style = MaterialTheme.typography.titleSmall,
) )
Text(text = displayText, style = MaterialTheme.typography.bodyMedium) Text(text = displayText, style = MaterialTheme.typography.bodyMedium)
@@ -39,6 +39,7 @@ data class ChatMessageDto(
val status: String? = null, val status: String? = null,
val kind: String? = null, val kind: String? = null,
val toolName: String? = null, val toolName: String? = null,
val toolCallId: String? = null,
) )
@Serializable @Serializable
@@ -16,6 +16,7 @@ data class MessageStartedEventDto(
val status: String? = null, val status: String? = null,
val kind: String? = null, val kind: String? = null,
val toolName: String? = null, val toolName: String? = null,
val toolCallId: String? = null,
) )
@Serializable @Serializable
@@ -28,6 +29,7 @@ data class MessageDeltaEventDto(
val status: String? = null, val status: String? = null,
val kind: String? = null, val kind: String? = null,
val toolName: String? = null, val toolName: String? = null,
val toolCallId: String? = null,
) )
@Serializable @Serializable