Skip to content

Commit c34ada3

Browse files
authored
feat(ui): hover-revealed chat message meta with relative timestamps (#104035)
* feat(ui): hover-revealed chat message meta with relative timestamps * fix(ui): derive relative chat timestamps from the injected clock * fix(ui): date far-future chat timestamps instead of clamping to just now * fix(ui): invalidate memoized chat thread on relative-time ticks * fix(ui): keep relative-time ticks out of the chat live region
1 parent eccd4f0 commit c34ada3

6 files changed

Lines changed: 167 additions & 42 deletions

File tree

‎ui/src/lib/format.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import { asDateTimestampMs } from "@openclaw/normalization-core/number-coercion";
33
import { truncateUtf16Safe } from "@openclaw/normalization-core/utf16-slice";
44
import { formatDurationHuman } from "../../../src/infra/format-time/format-duration.ts";
5-
import { formatRelativeTimestamp } from "../../../src/infra/format-time/format-relative.ts";
5+
import {
6+
formatRelativeTimestamp,
7+
formatTimeAgo,
8+
} from "../../../src/infra/format-time/format-relative.ts";
69
import { t } from "../i18n/index.ts";
710

8-
export { formatRelativeTimestamp, formatDurationHuman };
11+
export { formatRelativeTimestamp, formatTimeAgo, formatDurationHuman };
912

1013
export function formatUnknownText(
1114
value: unknown,

‎ui/src/pages/chat/components/chat-message.test.ts‎

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { MessageGroup } from "../../../lib/chat/chat-types.ts";
66
import { normalizeMessage } from "../../../lib/chat/message-normalizer.ts";
77
import { setUiTimeFormatPreference } from "../../../lib/format.ts";
88
import {
9+
formatChatRelativeTimestampLabel,
910
formatChatTimestampForDisplay,
1011
renderMessageGroup,
1112
renderStreamGroup,
@@ -905,7 +906,10 @@ describe("grouped chat rendering", () => {
905906
const time = container.querySelector<HTMLTimeElement>(".chat-group-timestamp");
906907
expect(time).not.toBeNull();
907908
expect(time?.closest("details.msg-meta")).toBeNull();
908-
expect(time?.title).not.toBe("");
909+
// Absolute time lives in the styled tooltip around the relative label.
910+
expect(time?.closest("openclaw-tooltip")?.getAttribute("content")).toBe(
911+
formatChatTimestampForDisplay(1000).label,
912+
);
909913
});
910914

911915
it("uses the largest single assistant call for grouped context usage", () => {
@@ -934,36 +938,71 @@ describe("grouped chat rendering", () => {
934938
expect(container.querySelector(".msg-meta__tokens")?.textContent).toBe("↑214.5k");
935939
});
936940

937-
it("renders full dates with message and streaming timestamps", () => {
938-
const container = document.createElement("div");
939-
const timestamp = Date.UTC(2026, 3, 24, 18, 30);
941+
it("renders relative labels with absolute datetimes for message and streaming timestamps", () => {
942+
vi.useFakeTimers();
943+
try {
944+
const timestamp = Date.UTC(2026, 3, 24, 18, 30);
945+
vi.setSystemTime(timestamp + 5 * 60 * 1000);
946+
const container = document.createElement("div");
940947

941-
renderAssistantMessage(container, {
942-
role: "assistant",
943-
content: "Done",
944-
timestamp,
945-
});
948+
renderAssistantMessage(container, {
949+
role: "assistant",
950+
content: "Done",
951+
timestamp,
952+
});
946953

947-
const time = container.querySelector<HTMLTimeElement>(".chat-group-timestamp");
948-
const display = formatChatTimestampForDisplay(timestamp);
949-
expect(time?.dateTime).toBe(display.dateTime);
950-
expect(time?.textContent?.trim()).toBe(display.label);
954+
const time = container.querySelector<HTMLTimeElement>(".chat-group-timestamp");
955+
const display = formatChatTimestampForDisplay(timestamp);
956+
expect(time?.dateTime).toBe(display.dateTime);
957+
expect(time?.textContent?.trim()).toBe("5m ago");
951958

952-
render(
953-
renderStreamGroup([
954-
{
955-
kind: "stream",
956-
key: `stream:${timestamp}`,
957-
text: "Working",
958-
startedAt: timestamp,
959-
isStreaming: true,
960-
},
961-
]),
962-
container,
959+
render(
960+
renderStreamGroup([
961+
{
962+
kind: "stream",
963+
key: `stream:${timestamp}`,
964+
text: "Working",
965+
startedAt: timestamp,
966+
isStreaming: true,
967+
},
968+
]),
969+
container,
970+
);
971+
972+
const streamingTime = container.querySelector<HTMLTimeElement>(".chat-group-timestamp");
973+
expect(streamingTime?.textContent?.trim()).toBe("5m ago");
974+
} finally {
975+
vi.useRealTimers();
976+
}
977+
});
978+
979+
it("falls back to compact dates for footer labels older than a week", () => {
980+
const timestamp = Date.UTC(2026, 3, 24, 18, 30);
981+
const sameYearNow = Date.UTC(2026, 5, 24, 18, 30);
982+
const nextYearNow = Date.UTC(2027, 0, 2, 18, 30);
983+
expect(formatChatRelativeTimestampLabel(timestamp, sameYearNow)).toBe(
984+
new Date(timestamp).toLocaleDateString([], { month: "short", day: "numeric" }),
985+
);
986+
expect(formatChatRelativeTimestampLabel(timestamp, nextYearNow)).toBe(
987+
new Date(timestamp).toLocaleDateString([], {
988+
month: "short",
989+
day: "numeric",
990+
year: "numeric",
991+
}),
963992
);
993+
});
964994

965-
const streamingTime = container.querySelector<HTMLTimeElement>(".chat-group-timestamp");
966-
expect(streamingTime?.textContent?.trim()).toBe(display.label);
995+
it("clamps skewed-future footer labels but dates far-future ones", () => {
996+
const nowMs = Date.UTC(2026, 3, 24, 18, 30);
997+
expect(formatChatRelativeTimestampLabel(nowMs + 30 * 1000, nowMs)).toBe("just now");
998+
const nextYear = Date.UTC(2027, 3, 24, 18, 30);
999+
expect(formatChatRelativeTimestampLabel(nextYear, nowMs)).toBe(
1000+
new Date(nextYear).toLocaleDateString([], {
1001+
month: "short",
1002+
day: "numeric",
1003+
year: "numeric",
1004+
}),
1005+
);
9671006
});
9681007

9691008
it("omits streaming bubble class for completed stream segments", () => {
@@ -1039,9 +1078,8 @@ describe("grouped chat rendering", () => {
10391078
// One bubble per segment, all under the single group.
10401079
expect(container.querySelectorAll(".chat-bubble")).toHaveLength(3);
10411080
// Footer time anchors to the earliest segment start, not render order.
1042-
const display = formatChatTimestampForDisplay(10);
1043-
expect(container.querySelector(".chat-group-timestamp")?.textContent?.trim()).toBe(
1044-
display.label,
1081+
expect(container.querySelector<HTMLTimeElement>(".chat-group-timestamp")?.dateTime).toBe(
1082+
formatChatTimestampForDisplay(10).dateTime,
10451083
);
10461084
});
10471085

‎ui/src/pages/chat/components/chat-message.ts‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
import type { EmbedSandboxMode } from "../../../lib/chat/tool-display.ts";
4040
import { resolveToolDisplay } from "../../../lib/chat/tool-display.ts";
4141
import { resolveUiHourCycleOptions } from "../../../lib/format.ts";
42-
import { formatCompactTokenCount } from "../../../lib/format.ts";
42+
import { formatCompactTokenCount, formatTimeAgo } from "../../../lib/format.ts";
4343
import "../../../components/tooltip.ts";
4444
import { getMediaFileExtension } from "../../../lib/media-file-extension.ts";
4545
import { openExternalUrlSafe } from "../../../lib/open-external-url.ts";
@@ -109,6 +109,7 @@ export function formatChatTimestampForDisplay(timestamp: number): ChatTimestampD
109109
year: "numeric",
110110
hour: "numeric",
111111
minute: "2-digit",
112+
timeZoneName: "short",
112113
}),
113114
title: date.toLocaleString([], {
114115
...hourCycle,
@@ -125,17 +126,46 @@ export function formatChatTimestampForDisplay(timestamp: number): ChatTimestampD
125126
};
126127
}
127128

129+
const CHAT_RELATIVE_TIMESTAMP_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
130+
const CHAT_RELATIVE_TIMESTAMP_FUTURE_SKEW_MS = 2 * 60 * 1000;
131+
132+
/** Footer label: relative for recent messages, compact date beyond a week. */
133+
export function formatChatRelativeTimestampLabel(timestamp: number, nowMs = Date.now()): string {
134+
const date = new Date(timestamp);
135+
if (!Number.isFinite(date.getTime())) {
136+
return "Unknown date";
137+
}
138+
const ageMs = nowMs - date.getTime();
139+
// Derive from ageMs so the injected clock stays the single time source.
140+
// Slightly-future (clock-skewed) messages clamp to "just now"; anything
141+
// further out falls through to the compact date instead of lying forever.
142+
if (
143+
ageMs >= -CHAT_RELATIVE_TIMESTAMP_FUTURE_SKEW_MS &&
144+
ageMs < CHAT_RELATIVE_TIMESTAMP_MAX_AGE_MS
145+
) {
146+
return formatTimeAgo(Math.max(0, ageMs));
147+
}
148+
return date.toLocaleDateString([], {
149+
month: "short",
150+
day: "numeric",
151+
...(date.getFullYear() === new Date(nowMs).getFullYear() ? {} : { year: "numeric" }),
152+
});
153+
}
154+
155+
// Footer times read relative ("5m ago"); the absolute timestamp lives in the
156+
// tooltip, or in the msg-meta popover when usage metadata makes the
157+
// timestamp interactive (a nested tooltip would fight the popover).
128158
function renderChatTimestamp(timestamp: number, interactive = false) {
129159
const display = formatChatTimestampForDisplay(timestamp);
130-
return html`
131-
<time
132-
class="chat-group-timestamp"
133-
datetime=${display.dateTime}
134-
title=${interactive ? nothing : display.title}
135-
>
136-
${display.label}
160+
const timeEl = html`
161+
<time class="chat-group-timestamp" datetime=${display.dateTime} aria-live="off">
162+
${formatChatRelativeTimestampLabel(timestamp)}
137163
</time>
138164
`;
165+
if (interactive) {
166+
return timeEl;
167+
}
168+
return html`<openclaw-tooltip content=${display.label}>${timeEl}</openclaw-tooltip>`;
139169
}
140170

141171
function resolveMessageMetaDetails(target: EventTarget | null): HTMLDetailsElement | null {
@@ -975,6 +1005,8 @@ function renderMessageMeta(timestamp: number, meta: GroupMeta | null) {
9751005
}
9761006

9771007
const display = formatChatTimestampForDisplay(timestamp);
1008+
// Absolute time leads the popover; the summary label itself stays relative.
1009+
parts.unshift(html`<span class="msg-meta__time">${display.label}</span>`);
9781010

9791011
return html`
9801012
<details

‎ui/src/pages/chat/components/chat-thread.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ type ChatThreadState = {
6565
scrollTop: number;
6666
} | null;
6767
historyRenderAnchorFrame: number | null;
68+
relativeTimeTimer: ReturnType<typeof setInterval> | null;
69+
relativeTimeRequestUpdate: (() => void) | null;
70+
relativeTimeVersion: number;
6871
};
6972

7073
type ChatThreadProps = {
@@ -127,9 +130,26 @@ function createChatThreadState(): ChatThreadState {
127130
historyRenderExpansionFrame: null,
128131
historyRenderAnchorAdjustment: null,
129132
historyRenderAnchorFrame: null,
133+
relativeTimeTimer: null,
134+
relativeTimeRequestUpdate: null,
135+
relativeTimeVersion: 0,
130136
};
131137
}
132138

139+
const RELATIVE_TIME_REFRESH_MS = 60_000;
140+
141+
// Footer timestamps render relative labels ("5m ago") that go stale on idle
142+
// panes; one per-pane minute tick keeps them fresh without per-message timers.
143+
// The version bump must accompany requestUpdate: the message subtree is
144+
// memoized by guard(), so a tick only re-renders it via this dependency.
145+
function ensureRelativeTimeRefresh(state: ChatThreadState, requestUpdate: () => void) {
146+
state.relativeTimeRequestUpdate = requestUpdate;
147+
state.relativeTimeTimer ??= setInterval(() => {
148+
state.relativeTimeVersion = (state.relativeTimeVersion + 1) % Number.MAX_SAFE_INTEGER;
149+
state.relativeTimeRequestUpdate?.();
150+
}, RELATIVE_TIME_REFRESH_MS);
151+
}
152+
133153
const threadStates = new Map<string, ChatThreadState>();
134154

135155
function getChatThreadState(paneId: string): ChatThreadState {
@@ -174,6 +194,11 @@ export function resetChatThreadPresentationState(paneId?: string) {
174194
if (state.historyRenderAnchorFrame != null) {
175195
cancelAnimationFrame(state.historyRenderAnchorFrame);
176196
}
197+
if (state.relativeTimeTimer != null) {
198+
clearInterval(state.relativeTimeTimer);
199+
state.relativeTimeTimer = null;
200+
state.relativeTimeRequestUpdate = null;
201+
}
177202
}
178203
if (paneId) {
179204
threadStates.delete(paneId);
@@ -649,6 +674,7 @@ function renderLoadingSkeleton() {
649674
export function renderChatThread(props: ChatThreadProps) {
650675
const state = getChatThreadState(props.paneId);
651676
const requestUpdate = props.onRequestUpdate ?? (() => {});
677+
ensureRelativeTimeRefresh(state, requestUpdate);
652678
const displayStream = props.stream ?? null;
653679
const activeSession = props.sessions?.sessions?.find((row) => row.key === props.sessionKey);
654680
const reasoningLevel = activeSession?.reasoningLevel ?? "off";
@@ -724,6 +750,7 @@ export function renderChatThread(props: ChatThreadProps) {
724750
deletedChatItemsSignature(deleted, chatItems),
725751
stableBooleanMapSignature(expandedToolCards),
726752
getAssistantAttachmentAvailabilityRenderVersion(),
753+
state.relativeTimeVersion,
727754
props.sessionKey,
728755
props.fullMessageAgentId,
729756
showReasoning,

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,25 @@
4242
justify-content: flex-end;
4343
}
4444

45-
/* Footer at bottom of message group (role + time) */
45+
/* Footer at bottom of message group (role + time). Hidden until the group is
46+
hovered/focused so the thread reads as a clean stream; opacity (not
47+
display) keeps the height reserved and avoids layout shift on reveal. */
4648
.chat-group-footer {
4749
display: flex;
4850
flex-wrap: wrap;
4951
gap: 8px;
5052
align-items: center;
5153
width: 100%;
5254
min-width: 0;
53-
margin-top: 6px;
55+
margin-top: 4px;
56+
opacity: 0;
57+
transition: opacity 120ms ease-out;
58+
}
59+
60+
.chat-group:hover .chat-group-footer,
61+
.chat-group:focus-within .chat-group-footer,
62+
.chat-group-footer:has(details.msg-meta[open]) {
63+
opacity: 1;
5464
}
5565

5666
.chat-group-footer__meta {
@@ -274,7 +284,7 @@ img.chat-avatar {
274284
}
275285

276286
.chat-group.assistant .chat-bubble {
277-
padding: 2px 0;
287+
padding: 4px 0;
278288
border: 0;
279289
background: transparent;
280290
box-shadow: none;
@@ -309,6 +319,10 @@ img.chat-avatar {
309319
@media (hover: none),
310320
(max-width: 768px),
311321
(max-width: 932px) and (max-height: 500px) and (orientation: landscape) {
322+
.chat-group-footer {
323+
opacity: 1;
324+
}
325+
312326
.chat-group-footer-actions button,
313327
.chat-message-actions-row button {
314328
opacity: 1;
@@ -518,6 +532,7 @@ details.msg-meta:not([open]) .msg-meta__details {
518532
left: auto;
519533
}
520534

535+
.msg-meta__time,
521536
.msg-meta__tokens,
522537
.msg-meta__cache,
523538
.msg-meta__cost,

‎ui/src/styles/chat/tool-cards.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
margin-top: 0;
1616
}
1717

18+
/* Flat top-level tool rows align their content with the assistant text edge:
19+
the row's 8px inline inset bleeds left into the avatar gutter (never right,
20+
which could overflow the thread on narrow panes) so the hover pill keeps
21+
its inset without indenting the icon/label. Child combinator keeps shells
22+
nested in .chat-activity-group__body on the boxed 12px inset. */
23+
.chat-group-messages > .chat-bubble--tool-shell .chat-tool-msg-summary {
24+
width: calc(100% + 8px);
25+
margin-left: -8px;
26+
}
27+
1828
.chat-tool-msg-summary {
1929
display: flex;
2030
align-items: center;

0 commit comments

Comments
 (0)