Skip to content

Commit 1f337b7

Browse files
committed
fix(gateway): preserve zero-byte artifacts
1 parent 239087d commit 1f337b7

3 files changed

Lines changed: 57 additions & 17 deletions

File tree

‎src/gateway/server-methods/artifacts-base64.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function readArtifactBase64Payload(
5252
value: string | undefined,
5353
opts: { includeData: boolean },
5454
): ArtifactBase64Payload | undefined {
55-
if (!value) {
55+
if (value === undefined) {
5656
return undefined;
5757
}
5858
let encodedLength = 0;
@@ -83,9 +83,6 @@ export function readArtifactBase64Payload(
8383
data += normalizeArtifactBase64Char(char);
8484
}
8585
}
86-
if (encodedLength === 0) {
87-
return undefined;
88-
}
8986
const remainder = encodedLength % 4;
9087
if ((padding > 0 && remainder !== 0) || remainder === 1) {
9188
return undefined;

‎src/gateway/server-methods/artifacts.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,50 @@ describe("artifacts RPC handlers", () => {
409409
expectFields(downloadPayload.artifact, { id: artifactId });
410410
});
411411

412+
it.each([
413+
{ type: "file", data: "", sizeBytes: 0, title: "direct.bin" },
414+
{
415+
type: "file",
416+
source: { data: "", media_type: "application/octet-stream", sizeBytes: 0 },
417+
title: "source.bin",
418+
},
419+
{
420+
type: "file",
421+
data: " data:application/octet-stream;base64, ",
422+
sizeBytes: 0,
423+
title: "data-url.bin",
424+
},
425+
{ data: "", sizeBytes: 0, title: "untyped.bin" },
426+
])("lists, gets, and downloads the zero-byte $title artifact", async (block) => {
427+
mockedMessages([
428+
{
429+
role: "assistant",
430+
content: [block],
431+
__openclaw: { seq: 2 },
432+
},
433+
]);
434+
435+
const listed = await listArtifacts({ sessionKey: "agent:main:main" });
436+
const artifact = expectFirstArtifact(listed.calls);
437+
const artifactId = requireNonEmptyString(artifact?.id, "expected zero-byte artifact id");
438+
expect(artifact).toMatchObject({ sizeBytes: 0, download: { mode: "bytes" } });
439+
expect(artifact).not.toHaveProperty("data");
440+
441+
const get = await getArtifact({ sessionKey: "agent:main:main", artifactId });
442+
const getPayload = expectOkPayload(get.calls) as { artifact?: Record<string, unknown> };
443+
expectFields(getPayload.artifact, { id: artifactId, sizeBytes: 0 });
444+
expectFields(getPayload.artifact?.download, { mode: "bytes" });
445+
expect(getPayload.artifact).not.toHaveProperty("data");
446+
447+
const download = await downloadArtifact({ sessionKey: "agent:main:main", artifactId });
448+
const downloadPayload = expectOkPayload(download.calls) as {
449+
artifact?: Record<string, unknown>;
450+
};
451+
expectFields(downloadPayload, { encoding: "base64", data: "" });
452+
expectFields(downloadPayload.artifact, { id: artifactId, sizeBytes: 0 });
453+
expectFields(downloadPayload.artifact?.download, { mode: "bytes" });
454+
});
455+
412456
it("preserves managed artifact identity and returns a ticketed download URL", async () => {
413457
const artifactId = "artifact_managed_image_11111111-1111-4111-8111-111111111111";
414458
mockedMessages([

‎src/gateway/server-methods/artifacts.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import { createHash } from "node:crypto";
44
import { isHttpUrl } from "@openclaw/net-policy/url-protocol";
55
import { asOptionalRecord } from "@openclaw/normalization-core/record-coerce";
6-
import { normalizeOptionalString as asNonEmptyString } from "@openclaw/normalization-core/string-coerce";
6+
import {
7+
normalizeOptionalString as asNonEmptyString,
8+
readStringValue,
9+
} from "@openclaw/normalization-core/string-coerce";
710
import {
811
ErrorCodes,
912
errorShape,
@@ -247,13 +250,13 @@ function resolveBlockDownload(
247250
mimeType?: string;
248251
sizeBytes?: number;
249252
} {
250-
const data = asNonEmptyString(block.data);
251-
const content = asNonEmptyString(block.content);
253+
const data = readStringValue(block.data)?.trim();
254+
const content = readStringValue(block.content)?.trim();
252255
const url = asNonEmptyString(block.url) ?? asNonEmptyString(block.openUrl);
253256
const imageUrl = mediaUrlValue(block.image_url);
254257
const audioUrl = asNonEmptyString(block.audio_url);
255258
const source = asOptionalRecord(block.source);
256-
const sourceData = asNonEmptyString(source?.data);
259+
const sourceData = readStringValue(source?.data)?.trim();
257260
const sourceUrl = asNonEmptyString(source?.url);
258261
const dataUrl = [url, sourceUrl, imageUrl, audioUrl, data, content, sourceData].find(
259262
(value) => typeof value === "string" && /^data:/i.test(value),
@@ -282,12 +285,7 @@ function resolveBlockDownload(
282285
? Math.floor(explicitSize)
283286
: base64?.sizeBytes;
284287
if (base64) {
285-
return {
286-
mode: "bytes",
287-
...(base64.data ? { data: base64.data } : {}),
288-
mimeType,
289-
sizeBytes,
290-
};
288+
return { mode: "bytes", data: base64.data, mimeType, sizeBytes };
291289
}
292290
if (remoteUrl) {
293291
return { mode: "url", url: remoteUrl, mimeType, sizeBytes };
@@ -310,8 +308,9 @@ function isArtifactBlock(block: Record<string, unknown>): boolean {
310308
) {
311309
return true;
312310
}
313-
return Boolean(
314-
block.url || block.openUrl || block.data || block.source || block.image_url || block.audio_url,
311+
return (
312+
block.data !== undefined ||
313+
Boolean(block.url || block.openUrl || block.source || block.image_url || block.audio_url)
315314
);
316315
}
317316

@@ -378,7 +377,7 @@ function collectArtifactsFromMessage(params: {
378377
messageSeq,
379378
source: "session-transcript",
380379
download: { mode: download.mode },
381-
...(download.data ? { data: download.data } : {}),
380+
...(download.data !== undefined ? { data: download.data } : {}),
382381
...(download.url ? { url: download.url } : {}),
383382
};
384383
params.artifacts.push(summary);

0 commit comments

Comments
 (0)