Skip to content

Commit b3bf654

Browse files
committed
fix(webchat): announce attachment failures before long replies
1 parent 6e6a0e5 commit b3bf654

3 files changed

Lines changed: 255 additions & 98 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { mkdir, writeFile } from "node:fs/promises";
2+
import path from "node:path";
3+
import { expect, it } from "vitest";
4+
import { installMockGateway } from "../test-helpers/control-ui-e2e.ts";
5+
import { createControlUiE2eSuite } from "./control-ui-e2e-suite.test-support.ts";
6+
7+
const suite = createControlUiE2eSuite({ name: "Attachment failure announcements" });
8+
const captureProof = process.env.OPENCLAW_CAPTURE_UI_PROOF === "1";
9+
const proofDir = path.join(process.cwd(), ".artifacts/control-ui-e2e/attachment-announcement");
10+
11+
suite.define(() => {
12+
it.each(["ordinary", "completed"])(
13+
"announces a long %s reply's named failure without announcing initial history",
14+
async (flow) => {
15+
await suite.withPage(
16+
{ locale: "en-US", serviceWorkers: "block", viewport: { width: 1280, height: 900 } },
17+
async ({ page }) => {
18+
const content = (label: string) => [
19+
{ type: "text", text: "Here is the requested summary. ".repeat(25) },
20+
{
21+
type: "attachment_error",
22+
attachment: { code: "file-not-found", kind: "document", label },
23+
},
24+
];
25+
const existing = {
26+
role: "assistant",
27+
content: content("previous.pdf"),
28+
timestamp: 1,
29+
__openclaw: { id: "existing-reply", seq: 1 },
30+
};
31+
const gateway = await installMockGateway(page, { historyMessages: [existing] });
32+
await page.goto(`${suite.server.baseUrl}chat`);
33+
await page
34+
.locator(".chat-assistant-attachment-card", { hasText: "previous.pdf" })
35+
.waitFor();
36+
const announcement = page.locator(".chat-transcript-announcement");
37+
expect(await announcement.getAttribute("role")).toBe("status");
38+
expect(await announcement.getAttribute("aria-live")).toBe("polite");
39+
expect(await announcement.getAttribute("aria-atomic")).toBe("true");
40+
expect(await announcement.textContent()).toBe("");
41+
if (captureProof) {
42+
await mkdir(proofDir, { recursive: true });
43+
await page.screenshot({ path: path.join(proofDir, `${flow}-initial.png`) });
44+
}
45+
46+
let runId: string | undefined;
47+
if (flow === "completed") {
48+
await page
49+
.locator(".agent-chat__composer-combobox textarea")
50+
.fill("Send the summary PDF");
51+
await page.getByRole("button", { name: "Send message" }).click();
52+
const request = await gateway.waitForRequest("chat.send");
53+
expect(request.params).toMatchObject({
54+
sessionKey: "main",
55+
message: "Send the summary PDF",
56+
deliver: false,
57+
idempotencyKey: expect.any(String),
58+
});
59+
runId = (request.params as { idempotencyKey: string }).idempotencyKey;
60+
}
61+
const reply = {
62+
role: "assistant",
63+
content: content("missing.pdf"),
64+
timestamp: Date.now(),
65+
...(runId ? { runId, phase: "final_answer" } : {}),
66+
__openclaw: { id: "appended-reply", seq: 3 },
67+
};
68+
await gateway.setHistoryMessages([existing, reply]);
69+
if (runId) {
70+
await gateway.emitGatewayEvent("chat", {
71+
message: reply,
72+
runId,
73+
sessionKey: "main",
74+
state: "final",
75+
});
76+
} else {
77+
await gateway.emitGatewayEvent("session.message", {
78+
message: reply,
79+
messageId: "appended-reply",
80+
messageSeq: 3,
81+
sessionKey: "main",
82+
activeRunIds: [],
83+
hasActiveRun: false,
84+
});
85+
}
86+
const card = page.locator(".chat-assistant-attachment-card", { hasText: "missing.pdf" });
87+
await card.waitFor();
88+
await card.scrollIntoViewIfNeeded();
89+
const cardText = await card.textContent();
90+
expect(cardText).toContain("Not sent");
91+
expect(cardText).toContain("File not found. Check the path and try again.");
92+
await expect.poll(() => announcement.textContent()).not.toBe("");
93+
const text = (await announcement.textContent()) ?? "";
94+
if (captureProof) {
95+
const stage = text.includes("missing.pdf") ? "after" : "before";
96+
await page.screenshot({ path: path.join(proofDir, `${flow}-${stage}.png`) });
97+
await writeFile(
98+
path.join(proofDir, `${flow}-${stage}.json`),
99+
JSON.stringify({ flow, text, length: text.length, card: cardText }, null, 2),
100+
);
101+
}
102+
expect(text).toContain(
103+
"missing.pdf: Not sent. File not found. Check the path and try again.",
104+
);
105+
expect(text).toContain("Here is the requested summary.");
106+
expect(text.length).toBe(500);
107+
},
108+
);
109+
},
110+
);
111+
});

‎ui/src/pages/chat/chat-view.test.ts‎

Lines changed: 143 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,109 +1749,155 @@ describe("chat transcript rendering", () => {
17491749
);
17501750
});
17511751

1752-
it("announces named attachment failures in ordinary and completed-run assistant rows", () => {
1753-
const transcript = createTestTranscript();
1754-
const container = document.createElement("div");
1755-
const existing = {
1756-
testVirtualRow: true,
1757-
testVirtualKey: "assistant-existing",
1758-
testVirtualRole: "assistant",
1759-
role: "assistant",
1760-
content: "Existing answer",
1761-
};
1762-
const renderMessages = (messages: unknown[]) =>
1763-
renderChatInto(container, { transcript, messages });
1764-
1765-
renderMessages([existing]);
1766-
const attachmentOnly = {
1767-
testVirtualRow: true,
1768-
testVirtualKey: "assistant-missing-attachment",
1769-
testVirtualRole: "assistant",
1770-
role: "assistant",
1771-
content: [
1772-
{
1773-
type: "attachment_error",
1774-
attachment: { code: "file-not-found", kind: "document", label: "missing.pdf" },
1775-
},
1776-
],
1777-
};
1778-
renderMessages([existing, attachmentOnly]);
1779-
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1780-
"missing.pdf: Not sent. File not found. Check the path and try again.",
1781-
);
1752+
it.each(["ordinary", "active", "completed"])(
1753+
"announces named attachment failures within the cap in %s assistant rows",
1754+
(flow) => {
1755+
const transcript = createTestTranscript();
1756+
const container = document.createElement("div");
1757+
const existing = {
1758+
testVirtualRow: true,
1759+
testVirtualKey: "assistant-existing",
1760+
testVirtualRole: "assistant",
1761+
role: "assistant",
1762+
content: "Existing answer",
1763+
};
1764+
const renderMessages = (messages: unknown[]) =>
1765+
renderChatInto(container, { transcript, messages });
1766+
1767+
renderMessages([existing]);
1768+
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe("");
1769+
const attachmentOnly = {
1770+
testVirtualRow: true,
1771+
testVirtualKey: "assistant-missing-attachment",
1772+
testVirtualRole: "assistant",
1773+
role: "assistant",
1774+
content: [
1775+
{
1776+
type: "attachment_error",
1777+
attachment: { code: "file-not-found", kind: "document", label: "missing.pdf" },
1778+
},
1779+
],
1780+
};
1781+
renderMessages([existing, attachmentOnly]);
1782+
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1783+
"missing.pdf: Not sent. File not found. Check the path and try again.",
1784+
);
17821785

1783-
const mixed = {
1784-
testVirtualRow: true,
1785-
testVirtualKey: "assistant-mixed-attachments",
1786-
testVirtualRole: "assistant",
1787-
role: "assistant",
1788-
content: [
1789-
{ type: "text", text: "Partial result" },
1790-
{
1791-
type: "attachment_error",
1792-
attachment: {
1793-
code: "unsupported-format",
1794-
kind: "document",
1795-
label: "settings.toml",
1786+
const mixed = {
1787+
testVirtualRow: true,
1788+
testVirtualKey: "assistant-mixed-attachments",
1789+
testVirtualRole: "assistant",
1790+
role: "assistant",
1791+
content: [
1792+
{ type: "text", text: "Partial result" },
1793+
{
1794+
type: "attachment_error",
1795+
attachment: {
1796+
code: "unsupported-format",
1797+
kind: "document",
1798+
label: "settings.toml",
1799+
},
17961800
},
1797-
},
1798-
],
1799-
};
1800-
renderMessages([existing, attachmentOnly, mixed]);
1801-
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1802-
"Partial result settings.toml: Not sent. Rejected by the local attachment allowlist. Send a supported file type.",
1803-
);
1801+
],
1802+
};
1803+
renderMessages([existing, attachmentOnly, mixed]);
1804+
const mixedAnnouncement = container.querySelector(
1805+
".chat-transcript-announcement",
1806+
)?.textContent;
18041807

1805-
const runBoundary = {
1806-
kind: "group" as const,
1807-
key: "group:user:attachment-run",
1808-
role: "user" as const,
1809-
messages: [
1810-
{
1811-
key: "user:attachment-run",
1812-
message: {
1813-
role: "user",
1814-
content: "Send the attachment",
1815-
__openclaw: {
1816-
id: "user:attachment-run",
1817-
idempotencyKey: "attachment-run:user",
1808+
const runBoundary = {
1809+
kind: "group" as const,
1810+
key: "group:user:attachment-run",
1811+
role: "user" as const,
1812+
messages: [
1813+
{
1814+
key: "user:attachment-run",
1815+
message: {
1816+
role: "user",
1817+
content: "Send the attachment",
1818+
__openclaw: {
1819+
id: "user:attachment-run",
1820+
idempotencyKey: "attachment-run:user",
1821+
},
18181822
},
18191823
},
1820-
},
1821-
],
1822-
timestamp: 1,
1823-
isStreaming: false,
1824-
};
1825-
const completedFailure = {
1826-
kind: "group" as const,
1827-
key: "group:assistant:attachment-run",
1828-
role: "assistant" as const,
1829-
messages: [
1830-
{
1831-
key: "assistant:attachment-run",
1832-
message: {
1833-
role: "assistant",
1834-
content: attachmentOnly.content,
1835-
runId: "attachment-run",
1824+
],
1825+
timestamp: 1,
1826+
isStreaming: false,
1827+
};
1828+
const completedFailure = {
1829+
kind: "group" as const,
1830+
key: "group:assistant:attachment-run",
1831+
role: "assistant" as const,
1832+
messages: [
1833+
{
1834+
key: "assistant:attachment-run",
1835+
message: {
1836+
role: "assistant",
1837+
content: attachmentOnly.content,
1838+
runId: "attachment-run",
1839+
},
18361840
},
1837-
},
1838-
],
1839-
timestamp: 2,
1840-
isStreaming: false,
1841-
runId: "attachment-run",
1842-
};
1843-
vi.mocked(chatThread.buildCachedChatItems).mockReturnValue([
1844-
runBoundary,
1845-
completedFailure,
1846-
] as ReturnType<typeof chatThread.buildCachedChatItems>);
1847-
renderChatInto(container, {
1848-
transcript,
1849-
messages: [runBoundary, completedFailure],
1850-
});
1851-
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1852-
"missing.pdf: Not sent. File not found. Check the path and try again.",
1853-
);
1854-
});
1841+
],
1842+
timestamp: 2,
1843+
isStreaming: false,
1844+
runId: "attachment-run",
1845+
};
1846+
vi.mocked(chatThread.buildCachedChatItems).mockReturnValue([
1847+
runBoundary,
1848+
completedFailure,
1849+
] as ReturnType<typeof chatThread.buildCachedChatItems>);
1850+
renderChatInto(container, {
1851+
transcript,
1852+
messages: [runBoundary, completedFailure],
1853+
});
1854+
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1855+
"missing.pdf: Not sent. File not found. Check the path and try again.",
1856+
);
1857+
1858+
for (const [index, prose] of [
1859+
"Here is the requested summary. ".repeat(25),
1860+
`${"x".repeat(430)}🦞${"y".repeat(100)}`,
1861+
].entries()) {
1862+
const message = {
1863+
role: "assistant",
1864+
content: [{ type: "text", text: prose }, ...attachmentOnly.content],
1865+
runId: "attachment-run",
1866+
};
1867+
const reply = {
1868+
...completedFailure,
1869+
key: `group:assistant:long-${index}`,
1870+
messages: [{ key: `assistant:long-${index}`, message }],
1871+
isStreaming: flow === "active",
1872+
};
1873+
const items = flow === "ordinary" ? [reply] : [runBoundary, reply];
1874+
vi.mocked(chatThread.buildCachedChatItems).mockReturnValue(items);
1875+
renderChatInto(container, { transcript, messages: items });
1876+
const announcement = expectDefined(
1877+
container.querySelector(".chat-transcript-announcement")?.textContent,
1878+
"attachment failure announcement",
1879+
);
1880+
expect(announcement).toContain(
1881+
"missing.pdf: Not sent. File not found. Check the path and try again.",
1882+
);
1883+
expect(announcement).toContain(prose.slice(0, 30));
1884+
expect(announcement.length).toBe(index === 0 ? 500 : 499);
1885+
expect(announcement).not.toMatch(/[\uD800-\uDFFF]/u);
1886+
1887+
vi.mocked(chatThread.buildCachedChatItems).mockReturnValue([
1888+
...items.slice(0, -1),
1889+
{ ...reply, messages: [{ key: `assistant:long-${index}`, message: mixed }] },
1890+
]);
1891+
renderChatInto(container, { transcript, messages: [mixed] });
1892+
expect(container.querySelector(".chat-transcript-announcement")?.textContent).toBe(
1893+
announcement,
1894+
);
1895+
}
1896+
expect(mixedAnnouncement).toBe(
1897+
"settings.toml: Not sent. Rejected by the local attachment allowlist. Send a supported file type. Partial result",
1898+
);
1899+
},
1900+
);
18551901

18561902
it("announces a run preamble and its later terminal answer separately", () => {
18571903
const transcript = createTestTranscript();

‎ui/src/pages/chat/components/chat-transcript-announcement.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function assistantMessageAttachmentFailureText(message: unknown): string | null
3535
function assistantMessageAnnouncementText(message: unknown): string | null {
3636
const text = extractTextCached(message)?.trim();
3737
const failureText = assistantMessageAttachmentFailureText(message);
38-
return [text, failureText].filter(Boolean).join(" ") || null;
38+
return [failureText, text].filter(Boolean).join(" ") || null;
3939
}
4040

4141
function assistantGroupAnnouncementSource(

0 commit comments

Comments
 (0)