Skip to content

Commit a2893fb

Browse files
author
Peter Steinberger
committed
fix(ui): decode artifact previews as UTF-8
1 parent 8d7b858 commit a2893fb

2 files changed

Lines changed: 98 additions & 11 deletions

File tree

‎ui/src/pages/chat/components/chat-session-workspace.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,95 @@ describe("custodian panel toggle", () => {
6262
});
6363
});
6464

65+
describe("session workspace artifacts", () => {
66+
function createArtifactHost(params: { data: string; mimeType: string; title?: string }) {
67+
const handleOpenSidebar = vi.fn();
68+
const request = vi.fn().mockResolvedValue({
69+
artifact: {
70+
id: "artifact-1",
71+
mimeType: params.mimeType,
72+
title: params.title ?? "Unicode artifact",
73+
},
74+
data: params.data,
75+
encoding: "base64",
76+
});
77+
const state = {
78+
client: { request },
79+
connected: true,
80+
handleOpenSidebar,
81+
hello: gatewayHello([]),
82+
sessionKey: "agent:main:current",
83+
sessions: {},
84+
} as unknown as SessionWorkspaceHost;
85+
return { handleOpenSidebar, request, state };
86+
}
87+
88+
it.each([
89+
{
90+
content: "Résumé 東京 🦀",
91+
fence: "```",
92+
mimeType: "text/plain",
93+
},
94+
{
95+
content: JSON.stringify({ message: "Résumé 東京 🦀" }),
96+
fence: "```json",
97+
mimeType: "application/json",
98+
},
99+
])(
100+
"decodes UTF-8 $mimeType artifacts without corrupting visible or raw text",
101+
async (testCase) => {
102+
const data = btoa(String.fromCharCode(...new TextEncoder().encode(testCase.content)));
103+
const { handleOpenSidebar, state } = createArtifactHost({
104+
data,
105+
mimeType: testCase.mimeType,
106+
});
107+
108+
createSessionWorkspaceProps(state).onOpenArtifact("artifact-1");
109+
110+
await vi.waitFor(() => expect(handleOpenSidebar).toHaveBeenCalledOnce());
111+
expect(handleOpenSidebar.mock.calls[0]?.[0]).toEqual({
112+
kind: "markdown",
113+
content: `# Unicode artifact\n\n${testCase.fence}\n${testCase.content}\n\`\`\``,
114+
rawText: testCase.content,
115+
});
116+
},
117+
);
118+
119+
it("preserves inline image artifacts as their original base64 data URLs", async () => {
120+
const data = "iVBORw0KGgo=";
121+
const { handleOpenSidebar, state } = createArtifactHost({
122+
data,
123+
mimeType: "image/png",
124+
title: "preview.png",
125+
});
126+
127+
createSessionWorkspaceProps(state).onOpenArtifact("artifact-1");
128+
129+
await vi.waitFor(() => expect(handleOpenSidebar).toHaveBeenCalledOnce());
130+
expect(handleOpenSidebar.mock.calls[0]?.[0]).toEqual({
131+
kind: "image",
132+
mimeType: "image/png",
133+
rawText: null,
134+
src: `data:image/png;base64,${data}`,
135+
title: "preview.png",
136+
});
137+
});
138+
139+
it("reports malformed base64 artifact data as a visible workspace error", async () => {
140+
const { handleOpenSidebar, state } = createArtifactHost({
141+
data: "not-base64!",
142+
mimeType: "text/plain",
143+
});
144+
145+
createSessionWorkspaceProps(state).onOpenArtifact("artifact-1");
146+
147+
await vi.waitFor(() =>
148+
expect(createSessionWorkspaceProps(state).error).toMatch(/InvalidCharacterError|invalid/i),
149+
);
150+
expect(handleOpenSidebar).not.toHaveBeenCalled();
151+
});
152+
});
153+
65154
describe("openSessionWorkspaceFile", () => {
66155
it("opens Markdown with a canonical Gateway- and pane-scoped draft identity", async () => {
67156
const handleOpenSidebar = vi.fn();

‎ui/src/pages/chat/components/chat-session-workspace.ts‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,17 @@ function artifactSidebarContent(params: {
281281
rawText: url ?? null,
282282
};
283283
}
284-
if (encoding === "base64" && data && mimeType === "application/json") {
285-
const decoded = globalThis.atob(data);
286-
return {
287-
kind: "markdown",
288-
content: `# ${title}\n\n\`\`\`json\n${decoded}\n\`\`\``,
289-
rawText: decoded,
290-
};
291-
}
292-
if (encoding === "base64" && data && mimeType.startsWith("text/")) {
293-
const decoded = globalThis.atob(data);
284+
if (
285+
encoding === "base64" &&
286+
data &&
287+
(mimeType === "application/json" || mimeType.startsWith("text/"))
288+
) {
289+
const bytes = Uint8Array.from(globalThis.atob(data), (char) => char.charCodeAt(0));
290+
const decoded = new TextDecoder().decode(bytes);
291+
const language = mimeType === "application/json" ? "json" : "";
294292
return {
295293
kind: "markdown",
296-
content: `# ${title}\n\n\`\`\`\n${decoded}\n\`\`\``,
294+
content: `# ${title}\n\n\`\`\`${language}\n${decoded}\n\`\`\``,
297295
rawText: decoded,
298296
};
299297
}

0 commit comments

Comments
 (0)