mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
fix(chat): preserve sender labels in dashboard history
This commit is contained in:
@@ -116,9 +116,10 @@ export function renderMessageGroup(
|
||||
) {
|
||||
const normalizedRole = normalizeRoleForGrouping(group.role);
|
||||
const assistantName = opts.assistantName ?? "Assistant";
|
||||
const userLabel = group.senderLabel?.trim();
|
||||
const who =
|
||||
normalizedRole === "user"
|
||||
? "You"
|
||||
? (userLabel ?? "You")
|
||||
: normalizedRole === "assistant"
|
||||
? assistantName
|
||||
: normalizedRole;
|
||||
|
||||
@@ -29,6 +29,7 @@ describe("message-normalizer", () => {
|
||||
content: [{ type: "text", text: "Hello world" }],
|
||||
timestamp: 1000,
|
||||
id: "msg-1",
|
||||
senderLabel: null,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -110,6 +111,16 @@ describe("message-normalizer", () => {
|
||||
|
||||
expect(result.content[0].args).toEqual({ foo: "bar" });
|
||||
});
|
||||
|
||||
it("preserves top-level sender labels", () => {
|
||||
const result = normalizeMessage({
|
||||
role: "user",
|
||||
content: "Hello from Telegram",
|
||||
senderLabel: "Iris",
|
||||
});
|
||||
|
||||
expect(result.senderLabel).toBe("Iris");
|
||||
});
|
||||
});
|
||||
|
||||
describe("normalizeRoleForGrouping", () => {
|
||||
|
||||
@@ -50,6 +50,8 @@ export function normalizeMessage(message: unknown): NormalizedMessage {
|
||||
|
||||
const timestamp = typeof m.timestamp === "number" ? m.timestamp : Date.now();
|
||||
const id = typeof m.id === "string" ? m.id : undefined;
|
||||
const senderLabel =
|
||||
typeof m.senderLabel === "string" && m.senderLabel.trim() ? m.senderLabel.trim() : null;
|
||||
|
||||
// Strip AI-injected metadata prefix blocks from user messages before display.
|
||||
if (role === "user" || role === "User") {
|
||||
@@ -61,7 +63,7 @@ export function normalizeMessage(message: unknown): NormalizedMessage {
|
||||
});
|
||||
}
|
||||
|
||||
return { role, content, timestamp, id };
|
||||
return { role, content, timestamp, id, senderLabel };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,7 @@ export type MessageGroup = {
|
||||
kind: "group";
|
||||
key: string;
|
||||
role: string;
|
||||
senderLabel?: string | null;
|
||||
messages: Array<{ message: unknown; key: string }>;
|
||||
timestamp: number;
|
||||
isStreaming: boolean;
|
||||
@@ -33,6 +34,7 @@ export type NormalizedMessage = {
|
||||
content: MessageContentItem[];
|
||||
timestamp: number;
|
||||
id?: string;
|
||||
senderLabel?: string | null;
|
||||
};
|
||||
|
||||
/** Tool card representation for tool calls and results */
|
||||
|
||||
@@ -225,4 +225,62 @@ describe("chat view", () => {
|
||||
expect(onNewSession).toHaveBeenCalledTimes(1);
|
||||
expect(container.textContent).not.toContain("Stop");
|
||||
});
|
||||
|
||||
it("shows sender labels from sanitized gateway messages instead of generic You", () => {
|
||||
const container = document.createElement("div");
|
||||
render(
|
||||
renderChat(
|
||||
createProps({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "hello from topic",
|
||||
senderLabel: "Iris",
|
||||
timestamp: 1000,
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
container,
|
||||
);
|
||||
|
||||
const senderLabels = Array.from(container.querySelectorAll(".chat-sender-name")).map((node) =>
|
||||
node.textContent?.trim(),
|
||||
);
|
||||
expect(senderLabels).toContain("Iris");
|
||||
expect(senderLabels).not.toContain("You");
|
||||
});
|
||||
|
||||
it("keeps consecutive user messages from different senders in separate groups", () => {
|
||||
const container = document.createElement("div");
|
||||
render(
|
||||
renderChat(
|
||||
createProps({
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "first",
|
||||
senderLabel: "Iris",
|
||||
timestamp: 1000,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: "second",
|
||||
senderLabel: "Joaquin De Rojas",
|
||||
timestamp: 1001,
|
||||
},
|
||||
],
|
||||
}),
|
||||
),
|
||||
container,
|
||||
);
|
||||
|
||||
const groups = container.querySelectorAll(".chat-group.user");
|
||||
expect(groups).toHaveLength(2);
|
||||
const senderLabels = Array.from(container.querySelectorAll(".chat-sender-name")).map((node) =>
|
||||
node.textContent?.trim(),
|
||||
);
|
||||
expect(senderLabels).toContain("Iris");
|
||||
expect(senderLabels).toContain("Joaquin De Rojas");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -498,9 +498,14 @@ function groupMessages(items: ChatItem[]): Array<ChatItem | MessageGroup> {
|
||||
|
||||
const normalized = normalizeMessage(item.message);
|
||||
const role = normalizeRoleForGrouping(normalized.role);
|
||||
const senderLabel = role.toLowerCase() === "user" ? (normalized.senderLabel ?? null) : null;
|
||||
const timestamp = normalized.timestamp || Date.now();
|
||||
|
||||
if (!currentGroup || currentGroup.role !== role) {
|
||||
if (
|
||||
!currentGroup ||
|
||||
currentGroup.role !== role ||
|
||||
(role.toLowerCase() === "user" && currentGroup.senderLabel !== senderLabel)
|
||||
) {
|
||||
if (currentGroup) {
|
||||
result.push(currentGroup);
|
||||
}
|
||||
@@ -508,6 +513,7 @@ function groupMessages(items: ChatItem[]): Array<ChatItem | MessageGroup> {
|
||||
kind: "group",
|
||||
key: `group:${role}:${item.key}`,
|
||||
role,
|
||||
senderLabel,
|
||||
messages: [{ message: item.message, key: item.key }],
|
||||
timestamp,
|
||||
isStreaming: false,
|
||||
|
||||
Reference in New Issue
Block a user