Skip to content

Commit f57a302

Browse files
fix(media-understanding): strip repeated placeholders (#96431)
Merged via squash. Prepared head SHA: 4b00486 Co-authored-by: lin-hongkuan <[email protected]> Co-authored-by: vincentkoc <[email protected]> Reviewed-by: @vincentkoc
1 parent bcbd521 commit f57a302

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

packages/media-understanding-common/src/format.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ describe("formatMediaUnderstandingBody", () => {
4848
expect(body).toBe("[Audio]\nUser text:\ncaption here\nTranscript:\ntranscribed");
4949
});
5050

51+
it("strips repeated leading media placeholders from user text", () => {
52+
const body = formatMediaUnderstandingBody({
53+
body: "<media:image> <media:audio> caption here",
54+
outputs: [
55+
{
56+
kind: "audio.transcription",
57+
attachmentIndex: 0,
58+
text: "transcribed",
59+
provider: "groq",
60+
},
61+
],
62+
});
63+
expect(body).toBe("[Audio]\nUser text:\ncaption here\nTranscript:\ntranscribed");
64+
});
65+
66+
it("treats repeated media placeholders without captions as synthetic text", () => {
67+
const body = formatMediaUnderstandingBody({
68+
body: "<media:image> <media:audio>",
69+
outputs: [
70+
{
71+
kind: "image.description",
72+
attachmentIndex: 0,
73+
text: "a chart",
74+
provider: "openai",
75+
},
76+
],
77+
});
78+
expect(body).toBe("[Image]\nDescription:\na chart");
79+
});
80+
5181
it("keeps user text once when multiple outputs exist", () => {
5282
const body = formatMediaUnderstandingBody({
5383
body: "caption here",

packages/media-understanding-common/src/format.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Media Understanding Common helper module supports format behavior.
22
import type { MediaUnderstandingOutput } from "./types.js";
33

4-
const MEDIA_PLACEHOLDER_RE = /^<media:[^>]+>(\s*\([^)]*\))?$/i;
5-
const MEDIA_PLACEHOLDER_TOKEN_RE = /^<media:[^>]+>(\s*\([^)]*\))?\s*/i;
4+
const MEDIA_PLACEHOLDER_TOKEN = String.raw`<media:[^>]+>(?:\s*\([^)]*\))?`;
5+
const MEDIA_PLACEHOLDER_RE = new RegExp(String.raw`^(?:${MEDIA_PLACEHOLDER_TOKEN}\s*)+$`, "i");
6+
const MEDIA_PLACEHOLDER_TOKEN_RE = new RegExp(String.raw`^(?:${MEDIA_PLACEHOLDER_TOKEN}\s*)+`, "i");
67

78
/** Extracts user-authored text while ignoring synthetic media placeholder tokens. */
89
export function extractMediaUserText(body?: string): string | undefined {

0 commit comments

Comments
 (0)