feat: enhance user message display with HTML rendering and preview functionality

This commit is contained in:
mlogclub
2026-04-14 23:26:20 +08:00
parent 12fd382abb
commit 7ab8d4fac6
+161 -12
View File
@@ -9,6 +9,7 @@ import {
} from "lucide-react" } from "lucide-react"
import { toast } from "sonner" import { toast } from "sonner"
import { ImMessageHTML } from "@/components/im-message-html"
import { ListPagination } from "@/components/list-pagination" import { ListPagination } from "@/components/list-pagination"
import { OptionCombobox } from "@/components/option-combobox" import { OptionCombobox } from "@/components/option-combobox"
import { Badge } from "@/components/ui/badge" import { Badge } from "@/components/ui/badge"
@@ -410,9 +411,7 @@ export default function DashboardAgentRunLogsPage() {
{formatDateTime(item.createdAt)} {formatDateTime(item.createdAt)}
</TableCell> </TableCell>
<TableCell> <TableCell>
<div className="line-clamp-2 max-w-[620px] text-sm"> <UserMessagePreview value={item.userMessage} />
{item.userMessage || "-"}
</div>
{item.errorMessage ? ( {item.errorMessage ? (
<div className="mt-1 line-clamp-1 text-xs text-destructive"> <div className="mt-1 line-clamp-1 text-xs text-destructive">
{item.errorMessage} {item.errorMessage}
@@ -585,6 +584,7 @@ export default function DashboardAgentRunLogsPage() {
icon={<BotMessageSquareIcon className="size-4" />} icon={<BotMessageSquareIcon className="size-4" />}
title="用户问题" title="用户问题"
value={activeLog.userMessage} value={activeLog.userMessage}
renderAsHtml
/> />
<TextBlock <TextBlock
icon={<WorkflowIcon className="size-4" />} icon={<WorkflowIcon className="size-4" />}
@@ -684,27 +684,176 @@ function TextBlock({
value, value,
icon, icon,
tone = "default", tone = "default",
renderAsHtml = false,
}: { }: {
title: string title: string
value?: string value?: string
icon?: ReactNode icon?: ReactNode
tone?: "default" | "danger" tone?: "default" | "danger"
renderAsHtml?: boolean
}) { }) {
const normalizedValue = value?.trim() || ""
const html = useMemo(() => {
if (!renderAsHtml || !normalizedValue) {
return ""
}
return sanitizeRichHTML(normalizedValue)
}, [normalizedValue, renderAsHtml])
return ( return (
<div className="rounded-lg border p-4"> <div className="rounded-lg border p-4">
<div className="flex items-center gap-2 text-sm font-medium"> <div className="flex items-center gap-2 text-sm font-medium">
{icon} {icon}
{title} {title}
</div> </div>
<div {renderAsHtml && normalizedValue ? (
className={ <ImMessageHTML
tone === "danger" html={html}
? "mt-3 select-text whitespace-pre-wrap wrap-break-word text-sm text-destructive" className="mt-3 select-text text-muted-foreground"
: "mt-3 select-text whitespace-pre-wrap wrap-break-word text-sm text-muted-foreground" />
} ) : (
> <div
{value?.trim() || "-"} className={
</div> tone === "danger"
? "mt-3 select-text whitespace-pre-wrap wrap-break-word text-sm text-destructive"
: "mt-3 select-text whitespace-pre-wrap wrap-break-word text-sm text-muted-foreground"
}
>
{normalizedValue || "-"}
</div>
)}
</div> </div>
) )
} }
function UserMessagePreview({ value }: { value?: string }) {
const preview = useMemo(() => summarizeUserMessage(value), [value])
return (
<div className="line-clamp-2 max-w-[620px] text-sm text-muted-foreground">
{preview}
</div>
)
}
function summarizeUserMessage(value?: string) {
const normalized = value?.trim()
if (!normalized) {
return "-"
}
const text = extractTextFromHTML(normalized).replace(/\s+/g, " ").trim()
if (text) {
return text
}
if (containsHTML(normalized)) {
if (/<img[\s>]/i.test(normalized)) {
return "[图片]"
}
return "[富文本消息]"
}
return normalized
}
function containsHTML(value: string) {
return /<[^>]+>/.test(value)
}
function extractTextFromHTML(value: string) {
if (typeof window === "undefined") {
return value
}
const doc = new DOMParser().parseFromString(value, "text/html")
return doc.body.textContent || ""
}
function sanitizeRichHTML(value: string) {
if (typeof window === "undefined") {
return value
}
const doc = new DOMParser().parseFromString(value, "text/html")
const allowedTags = new Set([
"a",
"b",
"blockquote",
"br",
"code",
"div",
"em",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"hr",
"img",
"li",
"ol",
"p",
"pre",
"span",
"strong",
"table",
"tbody",
"td",
"th",
"thead",
"tr",
"u",
"ul",
])
const allowedAttrs = new Set(["alt", "class", "colspan", "href", "rel", "rowspan", "src", "target", "title"])
const walker = doc.createTreeWalker(doc.body, NodeFilter.SHOW_ELEMENT)
const elements: Element[] = []
while (walker.nextNode()) {
elements.push(walker.currentNode as Element)
}
for (const element of elements) {
const tag = element.tagName.toLowerCase()
if (!allowedTags.has(tag)) {
element.replaceWith(...Array.from(element.childNodes))
continue
}
for (const attr of Array.from(element.attributes)) {
const name = attr.name.toLowerCase()
const value = attr.value.trim()
if (name.startsWith("on") || !allowedAttrs.has(name)) {
element.removeAttribute(attr.name)
continue
}
if ((name === "href" || name === "src") && !isSafeURL(value)) {
element.removeAttribute(attr.name)
continue
}
}
if (tag === "a") {
element.setAttribute("target", "_blank")
element.setAttribute("rel", "noreferrer noopener")
}
}
return doc.body.innerHTML
}
function isSafeURL(value: string) {
if (!value) {
return false
}
if (value.startsWith("/")) {
return true
}
if (value.startsWith("data:image/")) {
return true
}
try {
const url = new URL(value, window.location.origin)
return ["http:", "https:"].includes(url.protocol)
} catch {
return false
}
}