Skip to content

Commit a844e3e

Browse files
committed
fix(ui): retain the longest-lived media ticket
1 parent 461f1c4 commit a844e3e

3 files changed

Lines changed: 70 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ export function managedAttachmentRefreshDelayMs(refreshAttempts: number): number
114114
return ASSISTANT_ATTACHMENT_UNAVAILABLE_RETRY_MS * 2 ** Math.max(0, refreshAttempts - 1);
115115
}
116116

117+
export function selectLaterExpiringManagedAttachment(
118+
current: Extract<ManagedAttachmentAvailability, { status: "available" }> | null,
119+
incoming: Extract<ManagedAttachmentAvailability, { status: "available" }>,
120+
): Extract<ManagedAttachmentAvailability, { status: "available" }> {
121+
return current?.expiresAt !== undefined && current.expiresAt >= (incoming.expiresAt ?? 0)
122+
? current
123+
: incoming;
124+
}
125+
117126
export function isManagedOutgoingMediaSource(source: string): boolean {
118127
try {
119128
const parsed = new URL(source, window.location.origin);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
managedAttachmentRefreshDelayMs,
1919
resolveManagedOutgoingMediaSessionKey,
2020
scheduleAssistantAttachmentRefresh,
21+
selectLaterExpiringManagedAttachment,
2122
setAssistantAttachmentAvailability,
2223
type AssistantAttachmentAvailability,
2324
type ManagedAttachmentAvailability,
@@ -439,9 +440,11 @@ function resolveManagedAttachmentAvailability(
439440
url,
440441
expiresAt,
441442
};
442-
const retained =
443-
retainManagedAttachmentUntilExpiry(resource, current, refreshAttempts) ??
444-
retainManagedAttachmentUntilExpiry(resource, incoming, refreshAttempts);
443+
const retained = retainManagedAttachmentUntilExpiry(
444+
resource,
445+
selectLaterExpiringManagedAttachment(current, incoming),
446+
refreshAttempts,
447+
);
445448
if (retained) {
446449
return retained;
447450
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,61 @@ describe("grouped chat rendering", () => {
34043404
expect(container.querySelector(".chat-assistant-attachment-card--blocked")).not.toBeNull();
34053405
});
34063406

3407+
it("retains a longer-lived incoming managed ticket after refresh exhaustion", async () => {
3408+
vi.useFakeTimers();
3409+
vi.setSystemTime(new Date("2026-07-29T00:00:00.000Z"));
3410+
const initialExpiry = Date.now() + 20_000;
3411+
const source = `/api/chat/media/outgoing/agent%3Amain%3Amain/${crypto.randomUUID()}/full`;
3412+
const artifactId = `artifact_managed_media_${crypto.randomUUID()}`;
3413+
const resolveArtifactDownload = vi.fn(async () => ({
3414+
url: `${source}?mediaTicket=short-${resolveArtifactDownload.mock.calls.length}`,
3415+
expiresAt: new Date(
3416+
resolveArtifactDownload.mock.calls.length === 3 ? Date.now() + 20_000 : initialExpiry,
3417+
).toISOString(),
3418+
}));
3419+
const container = document.body.appendChild(document.createElement("div"));
3420+
container.dataset.mediaPlayerTestFixture = "";
3421+
const rerender = () =>
3422+
renderAssistantMessage(
3423+
container,
3424+
{
3425+
id: "assistant-managed-later-ticket",
3426+
role: "assistant",
3427+
content: [
3428+
{
3429+
type: "audio",
3430+
artifactId,
3431+
url: source,
3432+
fileName: "voice.mp3",
3433+
mimeType: "audio/mpeg",
3434+
playback: "native",
3435+
},
3436+
],
3437+
timestamp: Date.now(),
3438+
},
3439+
{ showToolCalls: false, onRequestUpdate: rerender, resolveArtifactDownload },
3440+
);
3441+
3442+
rerender();
3443+
await flushAssistantAttachmentAvailabilityChecks();
3444+
await vi.advanceTimersByTimeAsync(5_000);
3445+
await flushAssistantAttachmentAvailabilityChecks();
3446+
await vi.advanceTimersByTimeAsync(10_000);
3447+
await flushAssistantAttachmentAvailabilityChecks();
3448+
3449+
expect(resolveArtifactDownload).toHaveBeenCalledTimes(3);
3450+
expect(
3451+
container.querySelector("openclaw-chat-audio-player audio")?.getAttribute("src"),
3452+
).toContain("mediaTicket=short-3");
3453+
3454+
await vi.advanceTimersByTimeAsync(5_000);
3455+
await flushAssistantAttachmentAvailabilityChecks();
3456+
expect(container.querySelector("openclaw-chat-audio-player")).not.toBeNull();
3457+
await vi.advanceTimersByTimeAsync(15_000);
3458+
await flushAssistantAttachmentAvailabilityChecks();
3459+
expect(container.querySelector(".chat-assistant-attachment-card--blocked")).not.toBeNull();
3460+
});
3461+
34073462
it("refreshes a managed attachment that arrives with an initial ticket", async () => {
34083463
vi.useFakeTimers();
34093464
vi.setSystemTime(new Date("2026-07-29T00:00:00.000Z"));

0 commit comments

Comments
 (0)