fix(media-understanding): strip repeated placeholders (#96431) · openclaw/openclaw@f57a302
lin-hongkuan
·
2026-06-25
·
via Recent Commits to openclaw:main
File tree
packages/media-understanding-common/src
| Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,36 @@ describe("formatMediaUnderstandingBody", () => {
|
48 | 48 | expect(body).toBe("[Audio]\nUser text:\ncaption here\nTranscript:\ntranscribed"); |
49 | 49 | }); |
50 | 50 | |
| 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 | + |
51 | 81 | it("keeps user text once when multiple outputs exist", () => { |
52 | 82 | const body = formatMediaUnderstandingBody({ |
53 | 83 | body: "caption here", |
|
| Original file line number | Diff line number | Diff line change |
|---|
|
1 | 1 | // Media Understanding Common helper module supports format behavior. |
2 | 2 | import type { MediaUnderstandingOutput } from "./types.js"; |
3 | 3 | |
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"); |
6 | 7 | |
7 | 8 | /** Extracts user-authored text while ignoring synthetic media placeholder tokens. */ |
8 | 9 | export function extractMediaUserText(body?: string): string | undefined { |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。