Skip to content

Commit 36a2b5b

Browse files
authored
fix(media): preserve bare relative attachments in ordered reply segments (#118746)
1 parent dc1412c commit 36a2b5b

3 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎src/media/parse.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe("splitMediaFromOutput", () => {
3636

3737
function expectAcceptedMediaPathCase(expectedPath: string, input: string) {
3838
expectParsedMediaOutputCase(input, { mediaUrls: [expectedPath] });
39+
expect(splitMediaFromOutput(input).segments).toEqual([{ type: "media", url: expectedPath }]);
3940
}
4041

4142
function expectRejectedMediaPathCase(input: string) {
@@ -95,6 +96,47 @@ describe("splitMediaFromOutput", () => {
9596
expectAcceptedMediaPathCase(expectedPath, input);
9697
});
9798

99+
it.each([
100+
["bare image", "Generated image\nMEDIA:image.png", ["image.png"]],
101+
["bare audio", "Generated audio\nMEDIA:voice.ogg", ["voice.ogg"]],
102+
["bare document", "Generated document\nMEDIA:report.pdf", ["report.pdf"]],
103+
["caption after bare filename", "MEDIA:image.png\nGenerated image", ["image.png"]],
104+
["quoted bare filename", 'Generated image\nMEDIA:"image.png"', ["image.png"]],
105+
[
106+
"quoted bare filename with spaces",
107+
'Generated image\nMEDIA:"render final.png"',
108+
["render final.png"],
109+
],
110+
["unquoted bare filename with spaces", "MEDIA:render final.png", ["render final.png"]],
111+
[
112+
"remote followed by bare filename",
113+
"MEDIA:https://example.com/remote.png\nMEDIA:image.png",
114+
["https://example.com/remote.png", "image.png"],
115+
],
116+
[
117+
"bare filenames surrounding remote media",
118+
"MEDIA:image.png\nMEDIA:https://example.com/remote.png\nMEDIA:voice.ogg",
119+
["image.png", "https://example.com/remote.png", "voice.ogg"],
120+
],
121+
["explicit relative sibling", "MEDIA:./image.png", ["./image.png"]],
122+
["absolute sibling", "MEDIA:/tmp/image.png", ["/tmp/image.png"]],
123+
[
124+
"multiple paths on one directive",
125+
"MEDIA:/tmp/image.png /tmp/voice.ogg",
126+
["/tmp/image.png", "/tmp/voice.ogg"],
127+
],
128+
] as const)(
129+
"projects every accepted media URL into ordered segments: %s",
130+
(_name, input, urls) => {
131+
const result = splitMediaFromOutput(input);
132+
133+
expect(result.mediaUrls).toEqual(urls);
134+
expect(result.segments?.filter((segment) => segment.type === "media")).toEqual(
135+
urls.map((url) => ({ type: "media", url })),
136+
);
137+
},
138+
);
139+
98140
it.each([
99141
["MEDIA:/tmp/a.png /tmp/b.png", ["/tmp/a.png", "/tmp/b.png"]],
100142
["MEDIA:media/a.png media/b.png", ["media/a.png", "media/b.png"]],

‎src/media/parse.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,6 @@ export function splitMediaFromOutput(
647647
media.splice(mediaStartIndex, media.length - mediaStartIndex, fallback);
648648
hasValidMedia = true;
649649
foundMediaToken = true;
650-
validCount = 1;
651650
invalidParts.length = 0;
652651
}
653652
}
@@ -658,7 +657,6 @@ export function splitMediaFromOutput(
658657
media.splice(mediaStartIndex, media.length - mediaStartIndex, spacedFallback);
659658
hasValidMedia = true;
660659
foundMediaToken = true;
661-
validCount = 1;
662660
invalidParts.length = 0;
663661
}
664662
}
@@ -679,7 +677,7 @@ export function splitMediaFromOutput(
679677
lineSegments.push({ type: "text", text: beforeText });
680678
}
681679
pieces.length = 0;
682-
for (const url of media.slice(mediaStartIndex, mediaStartIndex + validCount)) {
680+
for (const url of media.slice(mediaStartIndex)) {
683681
lineSegments.push({ type: "media", url });
684682
}
685683
if (invalidParts.length > 0) {

‎ui/src/lib/chat/message-normalizer.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,65 @@ describe("message-normalizer", () => {
650650
expect(result.content).toEqual([{ type: "text", text: "MEDIA:chart.png" }]);
651651
});
652652

653+
it.each([
654+
["bare image", "Generated image\nMEDIA:image.png", "Generated image\nMEDIA:image.png"],
655+
["bare audio", "Generated audio\nMEDIA:voice.ogg", "Generated audio\nMEDIA:voice.ogg"],
656+
[
657+
"bare document",
658+
"Generated document\nMEDIA:report.pdf",
659+
"Generated document\nMEDIA:report.pdf",
660+
],
661+
[
662+
"caption after bare filename",
663+
"MEDIA:image.png\nGenerated image",
664+
"MEDIA:image.png\nGenerated image",
665+
],
666+
[
667+
"quoted bare filename",
668+
'Generated image\nMEDIA:"image.png"',
669+
"Generated image\nMEDIA:image.png",
670+
],
671+
[
672+
"quoted bare filename with spaces",
673+
'Generated image\nMEDIA:"render final.png"',
674+
"Generated image\nMEDIA:render final.png",
675+
],
676+
[
677+
"explicit relative sibling",
678+
"Generated image\nMEDIA:./image.png",
679+
"Generated image\nMEDIA:./image.png",
680+
],
681+
] as const)(
682+
"preserves relative assistant media beside its caption: %s",
683+
(_name, input, text) => {
684+
expect(normalizeMessage({ role: "assistant", content: input }).content).toEqual([
685+
{ type: "text", text },
686+
]);
687+
},
688+
);
689+
690+
it("preserves bare assistant media references around a renderable attachment", () => {
691+
expect(
692+
normalizeMessage({
693+
role: "assistant",
694+
content:
695+
"Generated artifacts\nMEDIA:image.png\nMEDIA:https://example.com/remote.png\nMEDIA:voice.ogg",
696+
}).content,
697+
).toEqual([
698+
{ type: "text", text: "Generated artifacts\nMEDIA:image.png" },
699+
{
700+
type: "attachment",
701+
attachment: {
702+
url: "https://example.com/remote.png",
703+
kind: "image",
704+
label: "remote.png",
705+
mimeType: "image/png",
706+
},
707+
},
708+
{ type: "text", text: "MEDIA:voice.ogg" },
709+
]);
710+
});
711+
653712
it("strips reply_to_current without rendering a quoted preview", () => {
654713
const result = normalizeMessage({
655714
role: "assistant",

0 commit comments

Comments
 (0)