Skip to content

Commit 64b0e86

Browse files
fix(ui): keep attributed avatars beside sent messages (#112472)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent 9bdc37f commit 64b0e86

3 files changed

Lines changed: 236 additions & 7 deletions

File tree

‎ui/src/e2e/chat-attributed-identity.e2e.test.ts‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,122 @@ suite.define(() => {
269269
await context.close();
270270
});
271271

272+
it("keeps attributed user avatars beside their bubbles through send reconciliation", async () => {
273+
const context = await suite.browser.newContext({
274+
viewport: { height: 900, width: 860 },
275+
});
276+
const page = await context.newPage();
277+
const localSenderId = "c3e32452-0467-47e5-aafa-233cd5dae29f";
278+
const peerSenderId = "315ee057-302f-45b4-829d-2c5db1bfed75";
279+
const localAvatarUrl = `/api/users/${localSenderId}/avatar?v=7`;
280+
const priorPrompt = "A prior attributed prompt.";
281+
const peerPrompt = "A peer attributed prompt.";
282+
const prompt = "A newly sent attributed prompt.";
283+
await page.route(`**/api/users/${localSenderId}/avatar*`, async (route) => {
284+
await route.fulfill({
285+
body: `<svg xmlns="http://www.w3.org/2000/svg" width="36" height="36"><rect width="36" height="36" fill="purple"/></svg>`,
286+
contentType: "image/svg+xml",
287+
status: 200,
288+
});
289+
});
290+
const gateway = await installMockGateway(page, {
291+
historyMessages: [
292+
{
293+
__openclaw: { senderId: localSenderId, senderName: "Collin Johnson" },
294+
content: [{ text: priorPrompt, type: "text" }],
295+
role: "user",
296+
timestamp: Date.now() - 3_000,
297+
},
298+
{
299+
__openclaw: { senderId: peerSenderId, senderName: "Riley Chen" },
300+
content: [{ text: peerPrompt, type: "text" }],
301+
role: "user",
302+
timestamp: Date.now() - 2_000,
303+
},
304+
{
305+
content: [{ text: "Ready for the next message.", type: "text" }],
306+
role: "assistant",
307+
timestamp: Date.now() - 1_000,
308+
},
309+
],
310+
presenceUsers: [
311+
{ self: true, id: localSenderId, name: "Collin Johnson", avatarUrl: localAvatarUrl },
312+
{ id: peerSenderId, name: "Riley Chen" },
313+
],
314+
});
315+
316+
const readUserAvatarLayout = async (message: string) => {
317+
const bubble = page.locator(".chat-group.user .chat-bubble", { hasText: message });
318+
await bubble.waitFor();
319+
return await bubble.evaluate((bubbleElement) => {
320+
const group = bubbleElement.closest<HTMLElement>(".chat-group.user");
321+
const avatar = group
322+
? [...group.querySelectorAll<HTMLElement>(".chat-avatar")].find(
323+
(candidate) => getComputedStyle(candidate).display !== "none",
324+
)
325+
: null;
326+
if (!group || !avatar) {
327+
throw new Error("Expected a visible attributed user avatar");
328+
}
329+
const bubbleRect = bubbleElement.getBoundingClientRect();
330+
const avatarRect = avatar.getBoundingClientRect();
331+
return {
332+
avatarLeft: avatarRect.left,
333+
avatarRight: avatarRect.right,
334+
bubbleLeft: bubbleRect.left,
335+
bubbleRight: bubbleRect.right,
336+
isPeer: group.classList.contains("chat-group--peer"),
337+
};
338+
});
339+
};
340+
341+
try {
342+
await page.goto(controlUiSessionUrl(suite.server.baseUrl, "agent:main:main"));
343+
const before = await readUserAvatarLayout(priorPrompt);
344+
const peerBefore = await readUserAvatarLayout(peerPrompt);
345+
346+
await page.locator(".agent-chat__composer-combobox textarea").fill(prompt);
347+
await page.getByRole("button", { name: "Send message" }).click();
348+
const sendRequest = await gateway.waitForRequest("chat.send");
349+
const afterSend = await readUserAvatarLayout(prompt);
350+
const priorAfterSend = await readUserAvatarLayout(priorPrompt);
351+
const peerAfterSend = await readUserAvatarLayout(peerPrompt);
352+
353+
const params = sendRequest.params;
354+
if (!params || typeof params !== "object" || !("idempotencyKey" in params)) {
355+
throw new Error("Expected chat send idempotency key");
356+
}
357+
const runId = params.idempotencyKey;
358+
if (typeof runId !== "string" || !runId.trim()) {
359+
throw new Error("Expected non-empty chat send idempotency key");
360+
}
361+
await gateway.emitChatFinal({ runId, text: "The attributed send completed." });
362+
await page
363+
.locator(".chat-thread .chat-bubble", { hasText: "The attributed send completed." })
364+
.waitFor();
365+
const afterFinal = await readUserAvatarLayout(prompt);
366+
367+
for (const [phase, layout] of [
368+
["initial history", before],
369+
["optimistic send", afterSend],
370+
["prior message after send", priorAfterSend],
371+
["final response", afterFinal],
372+
] as const) {
373+
expect(layout.isPeer, phase).toBe(false);
374+
expect(layout.avatarLeft, phase).toBeGreaterThanOrEqual(layout.bubbleRight + 9);
375+
}
376+
for (const [phase, layout] of [
377+
["peer initial history", peerBefore],
378+
["peer message after send", peerAfterSend],
379+
] as const) {
380+
expect(layout.isPeer, phase).toBe(true);
381+
expect(layout.avatarRight, phase).toBeLessThanOrEqual(layout.bubbleLeft - 9);
382+
}
383+
} finally {
384+
await context.close();
385+
}
386+
});
387+
272388
it("keeps missing local-viewer avatar initials through a live rerender", async () => {
273389
const artifactDir = resolveArtifactDir();
274390
if (artifactDir) {

‎ui/src/pages/chat/chat-responsive.browser.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,120 @@ describeBrowserLayout.concurrent("chat responsive browser layout", () => {
15851585
}
15861586
});
15871587

1588+
it("keeps attributed user avatar fallbacks beside the message after identity resolution", async () => {
1589+
const page = await openBrowserPage(860, 900);
1590+
try {
1591+
await page.setContent(
1592+
`<!doctype html><html><head><style>${readUiCss()}</style></head><body>
1593+
<div class="chat-thread" style="width: 768px;">
1594+
<div class="chat-thread-inner" style="width: 768px;">
1595+
<div class="chat-group user chat-group--with-footer">
1596+
<span class="chat-avatar-slot">
1597+
<img
1598+
class="chat-avatar user"
1599+
alt="Collin Johnson"
1600+
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='36' height='36'%3E%3Crect width='36' height='36' fill='purple'/%3E%3C/svg%3E"
1601+
/>
1602+
<div class="chat-avatar user chat-avatar--sender-initials">C</div>
1603+
</span>
1604+
<div class="chat-group-messages">
1605+
<div class="chat-bubble"><div class="chat-text">A newly sent message.</div></div>
1606+
</div>
1607+
<div class="chat-group-footer">
1608+
<div class="chat-group-footer__meta">
1609+
<span class="chat-sender-name">collinjohnsonw</span>
1610+
<span class="chat-group-timestamp">now</span>
1611+
</div>
1612+
</div>
1613+
</div>
1614+
</div>
1615+
</div>
1616+
<div class="agent-chat__typing-indicator">
1617+
<span class="agent-chat__typing-avatars">
1618+
<div class="chat-avatar user">B</div>
1619+
<span class="chat-avatar-slot">
1620+
<img
1621+
class="chat-avatar user"
1622+
alt="Typing participant"
1623+
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='36' height='36'%3E%3Crect width='36' height='36' fill='purple'/%3E%3C/svg%3E"
1624+
/>
1625+
<div class="chat-avatar user chat-avatar--sender-initials">C</div>
1626+
</span>
1627+
</span>
1628+
</div>
1629+
</body></html>`,
1630+
);
1631+
const messageAvatarSlot = page.locator(".chat-group .chat-avatar-slot");
1632+
await messageAvatarSlot.locator("img").waitFor();
1633+
1634+
const readLayout = async () =>
1635+
await page.evaluate(() => {
1636+
const group = document.querySelector<HTMLElement>(".chat-group.user")!;
1637+
const bubble = group.querySelector<HTMLElement>(".chat-bubble")!;
1638+
const visibleAvatar = [...group.querySelectorAll<HTMLElement>(".chat-avatar")].find(
1639+
(avatar) => getComputedStyle(avatar).display !== "none",
1640+
)!;
1641+
const groupRect = group.getBoundingClientRect();
1642+
const bubbleRect = bubble.getBoundingClientRect();
1643+
const avatarRect = visibleAvatar.getBoundingClientRect();
1644+
return {
1645+
avatarLeft: avatarRect.left,
1646+
avatarRight: avatarRect.right,
1647+
bubbleRight: bubbleRect.right,
1648+
groupRight: groupRect.right,
1649+
};
1650+
});
1651+
1652+
const imageLayout = await readLayout();
1653+
expect(await messageAvatarSlot.evaluate((slot) => getComputedStyle(slot).display)).toBe(
1654+
"grid",
1655+
);
1656+
await messageAvatarSlot.evaluate((slot) => slot.classList.add("is-fallback"));
1657+
const fallbackLayout = await readLayout();
1658+
1659+
for (const layout of [imageLayout, fallbackLayout]) {
1660+
expect(layout.avatarLeft).toBeGreaterThanOrEqual(layout.bubbleRight + 9);
1661+
expect(layout.avatarRight).toBeLessThanOrEqual(layout.groupRight + 1);
1662+
}
1663+
1664+
const typingAvatars = await page.locator(".agent-chat__typing-avatars").evaluate((row) =>
1665+
[...row.children].map((avatar) => {
1666+
const bounds = avatar.getBoundingClientRect();
1667+
return {
1668+
height: bounds.height,
1669+
marginBottom: getComputedStyle(avatar).marginBottom,
1670+
top: bounds.top,
1671+
width: bounds.width,
1672+
};
1673+
}),
1674+
);
1675+
expect(typingAvatars).toHaveLength(2);
1676+
expect(
1677+
typingAvatars.map(({ height, marginBottom, width }) => ({ height, marginBottom, width })),
1678+
).toEqual([
1679+
{ height: 36, marginBottom: "0px", width: 36 },
1680+
{ height: 36, marginBottom: "0px", width: 36 },
1681+
]);
1682+
expect(Math.abs(typingAvatars[0]!.top - typingAvatars[1]!.top)).toBeLessThanOrEqual(0.5);
1683+
1684+
await page
1685+
.locator(".chat-thread")
1686+
.evaluate((thread) => thread.classList.add("chat-thread--direct"));
1687+
expect(await messageAvatarSlot.evaluate((slot) => getComputedStyle(slot).display)).toBe(
1688+
"none",
1689+
);
1690+
await page
1691+
.locator(".chat-thread")
1692+
.evaluate((thread) => thread.classList.remove("chat-thread--direct"));
1693+
await page.setViewportSize({ width: 390, height: 900 });
1694+
expect(await messageAvatarSlot.evaluate((slot) => getComputedStyle(slot).display)).toBe(
1695+
"none",
1696+
);
1697+
} finally {
1698+
await closeBrowserPage(page);
1699+
}
1700+
});
1701+
15881702
it("keeps attached images within narrow message lanes", async () => {
15891703
const page = await openBrowserPage(320, 568);
15901704
try {

‎ui/src/styles/chat/grouped.css‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
justify-content: start;
7373
}
7474

75-
.chat-group.chat-group--with-footer .chat-avatar {
75+
.chat-group.chat-group--with-footer > :is(.chat-avatar, .chat-avatar-slot) {
7676
grid-column: var(--chat-group-avatar-column);
7777
grid-row: 1;
7878
}
@@ -535,10 +535,10 @@ img.chat-avatar.user {
535535
object-fit: cover;
536536
}
537537

538-
/* Sender avatars pair the route image with an initials fallback; the slot
539-
swaps them when the canonical route has no avatar (404). */
538+
/* Keep the image and initials fallback in one stable grid item. display: contents
539+
can lose the avatar column when Lit reconciles an attributed message. */
540540
.chat-avatar-slot {
541-
display: contents;
541+
display: grid;
542542
}
543543

544544
.chat-avatar-slot > .chat-avatar--sender-initials {
@@ -670,8 +670,7 @@ img.chat-avatar.chat-avatar--logo {
670670
/* Direct (1:1) threads drop avatars entirely: one agent plus one user makes
671671
the repeated identity icons pure decoration. Group threads (any labeled
672672
foreign sender) keep avatars as the always-visible identity marker. */
673-
.chat-thread--direct .chat-avatar,
674-
.chat-thread--direct img.chat-avatar {
673+
.chat-thread--direct :is(.chat-avatar, .chat-avatar-slot) {
675674
display: none;
676675
}
677676

@@ -1155,7 +1154,7 @@ details.msg-meta:not([open]) .msg-meta__details {
11551154
}
11561155

11571156
.chat-avatar,
1158-
img.chat-avatar {
1157+
.chat-avatar-slot {
11591158
display: none;
11601159
}
11611160

0 commit comments

Comments
 (0)